- Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ;
- wxString message ;
- wxCHECK_RET( menubar != NULL, "can't read MBAR resource" );
- ::SetMenuBar( menubar ) ;
- ::DisposeHandle( menubar ) ;
-
- MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ;
- ::AppendResMenu(menu, 'DRVR');
-
- for (int i = 0; i < m_menus.GetCount(); i++)
- {
- Str255 label;
- wxNode *node;
- wxMenuItem *item;
- int pos ;
- wxMenu* menu = m_menus[i] , *subMenu = NULL ;
-
-
- if( m_titles[i] == "?" || m_titles[i] == wxApp::s_macHelpMenuTitleName )
- {
- MenuHandle mh = NULL ;
- if ( HMGetHelpMenuHandle( &mh ) != noErr )
- {
- continue ;
- }
- if ( formerHelpMenuItems == 0 )
- {
- if( mh )
- formerHelpMenuItems = CountMenuItems( mh ) ;
- }
-
- for (pos = 0 , node = menu->GetMenuItems().First(); node; node = node->Next(), pos++)
- {
- item = (wxMenuItem *)node->Data();
- subMenu = item->GetSubMenu() ;
- if (subMenu)
- {
- // we don't support hierarchical menus in the help menu yet
- }
- else
- {
- Str255 label ;
- wxMacBuildMenuString( label , NULL , NULL , item->GetText(), item->GetId() != wxApp::s_macAboutMenuItemId); // no shortcut in about menu
- if ( label[0] == 0 )
- {
- // we cannot add empty menus on mac
- label[0] = 1 ;
- label[1] = ' ' ;
- }
- if ( item->GetId() == wxApp::s_macAboutMenuItemId )
- {
- ::SetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , label );
- // ::EnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 );
- ::EnableItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 );
- }
- else
- {
- if ( mh )
- ::AppendMenu(mh, label );
- }
- }
- }
- }
- else
- {
- wxMacBuildMenuString( label, NULL , NULL , m_titles[i] , false );
- UMASetMenuTitle( menu->GetHMenu() , label ) ;
- for (pos = 0, node = menu->GetMenuItems().First(); node; node = node->Next(), pos++)
- {
- item = (wxMenuItem *)node->Data();
- subMenu = item->GetSubMenu() ;
- if (subMenu)
- {
- ::InsertMenu( subMenu->GetHMenu() , -1 ) ;
- }
- }
- ::InsertMenu(m_menus[i]->GetHMenu(), 0);
- }
- }
- ::DrawMenuBar() ;
-
- s_macInstalledMenuBar = this;
+ if ( s_macInstalledMenuBar == this )
+ return ;
+
+ MenuBarHandle menubar = NULL ;
+
+#if TARGET_API_MAC_OSX
+ menubar = NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ;
+#else
+ menubar = NewHandleClear( 12 ) ;
+ (*menubar)[3] = 0x0a ;
+#endif
+
+ ::SetMenuBar( menubar ) ;
+ DisposeMenuBar( menubar ) ;
+ MenuHandle appleMenu = NULL ;
+
+ verify_noerr( CreateNewMenu( kwxMacAppleMenuId , 0 , &appleMenu ) ) ;
+ verify_noerr( SetMenuTitleWithCFString( appleMenu , CFSTR( "\x14" ) ) );
+
+ // Add About/Preferences separator only on OS X
+ // KH/RN: Separator is always present on 10.3 but not on 10.2
+ // However, the change from 10.2 to 10.3 suggests it is preferred
+#if TARGET_API_MAC_OSX
+ InsertMenuItemTextWithCFString( appleMenu,
+ CFSTR(""), 0, kMenuItemAttrSeparator, 0);
+#endif
+ InsertMenuItemTextWithCFString( appleMenu,
+ CFSTR("About..."), 0, 0, 0);
+ MacInsertMenu( appleMenu , 0 ) ;
+
+ // if we have a mac help menu, clean it up before adding new items
+ MenuHandle helpMenuHandle ;
+ MenuItemIndex firstUserHelpMenuItem ;
+
+ if ( UMAGetHelpMenuDontCreate( &helpMenuHandle , &firstUserHelpMenuItem) == noErr )
+ {
+ for ( int i = CountMenuItems( helpMenuHandle ) ; i >= firstUserHelpMenuItem ; --i )
+ DeleteMenuItem( helpMenuHandle , i ) ;
+ }
+ else
+ {
+ helpMenuHandle = NULL ;
+ }
+
+#if TARGET_CARBON
+ if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId)
+ {
+ wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ;
+ if ( item == NULL || !(item->IsEnabled()) )
+ DisableMenuCommand( NULL , kHICommandPreferences ) ;
+ else
+ EnableMenuCommand( NULL , kHICommandPreferences ) ;
+ }
+
+ // Unlike preferences which may or may not exist, the Quit item should be always
+ // enabled unless it is added by the application and then disabled, otherwise
+ // a program would be required to add an item with wxID_EXIT in order to get the
+ // Quit menu item to be enabled, which seems a bit burdensome.
+ if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macExitMenuItemId)
+ {
+ wxMenuItem *item = FindItem( wxApp::s_macExitMenuItemId , NULL ) ;
+ if ( item != NULL && !(item->IsEnabled()) )
+ DisableMenuCommand( NULL , kHICommandQuit ) ;
+ else
+ EnableMenuCommand( NULL , kHICommandQuit ) ;
+ }
+#endif
+
+ wxString strippedHelpMenuTitle = wxStripMenuCodes( wxApp::s_macHelpMenuTitleName ) ;
+ wxString strippedTranslatedHelpMenuTitle = wxStripMenuCodes( wxString( _("&Help") ) ) ;
+ wxMenuList::compatibility_iterator menuIter = m_menus.GetFirst();
+ for (size_t i = 0; i < m_menus.GetCount(); i++, menuIter = menuIter->GetNext())
+ {
+ wxMenuItemList::compatibility_iterator node;
+ wxMenuItem *item;
+ wxMenu* menu = menuIter->GetData() , *subMenu = NULL ;
+ wxString strippedMenuTitle = wxStripMenuCodes(m_titles[i]);
+
+ if ( strippedMenuTitle == wxT("?") || strippedMenuTitle == strippedHelpMenuTitle || strippedMenuTitle == strippedTranslatedHelpMenuTitle )
+ {
+ for (node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext())
+ {
+ item = (wxMenuItem *)node->GetData();
+ subMenu = item->GetSubMenu() ;
+ if (subMenu)
+ {
+ // we don't support hierarchical menus in the help menu yet
+ }
+ else
+ {
+ if ( item->GetId() != wxApp::s_macAboutMenuItemId )
+ {
+ // we have found a user help menu and an item other than the about item,
+ // so we can create the mac help menu now, if we haven't created it yet
+ if ( helpMenuHandle == NULL )
+ {
+ if ( UMAGetHelpMenu( &helpMenuHandle , &firstUserHelpMenuItem) != noErr )
+ {
+ helpMenuHandle = NULL ;
+ break ;
+ }
+ }
+ }
+
+ if ( item->IsSeparator() )
+ {
+ if ( helpMenuHandle )
+ AppendMenuItemTextWithCFString( helpMenuHandle,
+ CFSTR(""), kMenuItemAttrSeparator, 0,NULL);
+ }
+ else
+ {
+ wxAcceleratorEntry*
+ entry = wxAcceleratorEntry::Create( item->GetText() ) ;
+
+ if ( item->GetId() == wxApp::s_macAboutMenuItemId )
+ {
+ // this will be taken care of below
+ }
+ else
+ {
+ if ( helpMenuHandle )
+ {
+ UMAAppendMenuItem(helpMenuHandle, wxStripMenuCodes(item->GetText()) , wxFont::GetDefaultEncoding(), entry);
+ SetMenuItemCommandID( helpMenuHandle , CountMenuItems(helpMenuHandle) , wxIdToMacCommand ( item->GetId() ) ) ;
+ SetMenuItemRefCon( helpMenuHandle , CountMenuItems(helpMenuHandle) , (URefCon) item ) ;
+ }
+ }
+
+ delete entry ;
+ }
+ }
+ }
+ }
+
+ else if ( ( m_titles[i] == wxT("Window") || m_titles[i] == wxT("&Window") )
+ && GetAutoWindowMenu() )
+ {
+ if ( MacGetWindowMenuHMenu() == NULL )
+ {
+ CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ;
+ }
+
+ MenuRef wm = (MenuRef)MacGetWindowMenuHMenu();
+ if ( wm == NULL )
+ break;
+
+ // get the insertion point in the standard menu
+ MenuItemIndex winListStart;
+ GetIndMenuItemWithCommandID(wm,
+ kHICommandWindowListSeparator, 1, NULL, &winListStart);
+
+ // add a separator so that the standard items and the custom items
+ // aren't mixed together, but only if this is the first run
+ OSStatus err = GetIndMenuItemWithCommandID(wm,
+ 'WXWM', 1, NULL, NULL);
+
+ if ( err == menuItemNotFoundErr )
+ {
+ InsertMenuItemTextWithCFString( wm,
+ CFSTR(""), winListStart-1, kMenuItemAttrSeparator, 'WXWM');
+ }
+
+ wxInsertMenuItemsInMenu(menu, wm, winListStart);
+ }
+ else
+ {
+ UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ;
+ menu->MacBeforeDisplay(false) ;
+
+ ::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus, i)->GetHMenu()), 0);
+ }
+ }
+
+ // take care of the about menu item wherever it is
+ {
+ wxMenu* aboutMenu ;
+ wxMenuItem *aboutMenuItem = FindItem(wxApp::s_macAboutMenuItemId , &aboutMenu) ;
+ if ( aboutMenuItem )
+ {
+ wxAcceleratorEntry*
+ entry = wxAcceleratorEntry::Create( aboutMenuItem->GetText() ) ;
+ UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , wxStripMenuCodes ( aboutMenuItem->GetText() ) , wxFont::GetDefaultEncoding() );
+ UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true );
+ SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , kHICommandAbout ) ;
+ SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId ) , 1 , (URefCon)aboutMenuItem ) ;
+ UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ;
+ }
+ }
+
+ if ( GetAutoWindowMenu() )
+ {
+ if ( MacGetWindowMenuHMenu() == NULL )
+ CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ;
+
+ InsertMenu( (MenuHandle) MacGetWindowMenuHMenu() , 0 ) ;
+ }
+
+ ::DrawMenuBar() ;
+ s_macInstalledMenuBar = this;