+//----------------------------------------------------------------------
+// Macintosh CommandID support - converting between native and wx IDs
+//----------------------------------------------------------------------
+
+// if no native match they just return the passed-in id
+
+struct IdPair
+{
+ UInt32 macId ;
+ int wxId ;
+} ;
+
+IdPair gCommandIds [] =
+{
+ { kHICommandCut , wxID_CUT } ,
+ { kHICommandCopy , wxID_COPY } ,
+ { kHICommandPaste , wxID_PASTE } ,
+ { kHICommandSelectAll , wxID_SELECTALL } ,
+ { kHICommandClear , wxID_CLEAR } ,
+ { kHICommandUndo , wxID_UNDO } ,
+ { kHICommandRedo , wxID_REDO } ,
+} ;
+
+int wxMacCommandToId( UInt32 macCommandId )
+{
+ int wxid = 0 ;
+
+ switch ( macCommandId )
+ {
+ case kHICommandPreferences :
+ wxid = wxApp::s_macPreferencesMenuItemId ;
+ break ;
+
+ case kHICommandQuit :
+ wxid = wxApp::s_macExitMenuItemId ;
+ break ;
+
+ case kHICommandAbout :
+ wxid = wxApp::s_macAboutMenuItemId ;
+ break ;
+
+ default :
+ {
+ for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
+ {
+ if ( gCommandIds[i].macId == macCommandId )
+ {
+ wxid = gCommandIds[i].wxId ;
+ break ;
+ }
+ }
+ }
+ break ;
+ }
+
+ if ( wxid == 0 )
+ wxid = (int) macCommandId ;
+
+ return wxid ;
+}
+
+UInt32 wxIdToMacCommand( int wxId )
+{
+ UInt32 macId = 0 ;
+
+ if ( wxId == wxApp::s_macPreferencesMenuItemId )
+ macId = kHICommandPreferences ;
+ else if (wxId == wxApp::s_macExitMenuItemId)
+ macId = kHICommandQuit ;
+ else if (wxId == wxApp::s_macAboutMenuItemId)
+ macId = kHICommandAbout ;
+ else
+ {
+ for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
+ {
+ if ( gCommandIds[i].wxId == wxId )
+ {
+ macId = gCommandIds[i].macId ;
+ break ;
+ }
+ }
+ }
+
+ if ( macId == 0 )
+ macId = (int) wxId ;
+
+ return macId ;
+}
+
+wxMenu* wxFindMenuFromMacCommand( const HICommand &command , wxMenuItem* &item )
+{
+ wxMenu* itemMenu = NULL ;
+ int id = 0 ;
+
+ // for 'standard' commands which don't have a wx-menu
+ if ( command.commandID == kHICommandPreferences || command.commandID == kHICommandQuit || command.commandID == kHICommandAbout )
+ {
+ id = wxMacCommandToId( command.commandID ) ;
+
+ wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
+ if ( mbar )
+ item = mbar->FindItem( id , &itemMenu ) ;
+ }
+ else if ( command.commandID != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
+ {
+ id = wxMacCommandToId( command.commandID ) ;
+ // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
+ MenuItemIndex firstUserHelpMenuItem ;
+ static MenuHandle mh = NULL ;
+ if ( mh == NULL )
+ {
+ if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) != noErr )
+ mh = NULL ;
+ }
+
+ // is it part of the application or the Help menu, then look for the id directly
+ if ( ( GetMenuHandle( kwxMacAppleMenuId ) != NULL && command.menu.menuRef == GetMenuHandle( kwxMacAppleMenuId ) ) ||
+ ( mh != NULL && command.menu.menuRef == mh ) )
+ {
+ wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
+ if ( mbar )
+ item = mbar->FindItem( id , &itemMenu ) ;
+ }
+ else
+ {
+ UInt32 refCon ;
+
+ GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
+ itemMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
+ if ( itemMenu != NULL )
+ item = (wxMenuItem*) refCon ;
+ }
+ }
+
+ return itemMenu ;
+}
+