X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c8bef6e3791718544f7dfdccc857df3d9f397829..c02aa4b3a1cb95dce9232adc99876567085e6aff:/src/osx/menu_osx.cpp diff --git a/src/osx/menu_osx.cpp b/src/osx/menu_osx.cpp index 3c8220ef75..4ad6fa1fdf 100644 --- a/src/osx/menu_osx.cpp +++ b/src/osx/menu_osx.cpp @@ -43,10 +43,7 @@ wxMenuImpl::~wxMenuImpl() { } -IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) -IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) - -// the (popup) menu title has this special id +// the (popup) menu title has this special menuid static const int idMenuTitle = -3; // ============================================================================ @@ -57,18 +54,6 @@ static const int idMenuTitle = -3; // Construct a menu with optional title (then use append) -static -wxMenu * -_wxMenuAt(const wxMenuList &menuList, size_t pos) -{ - wxMenuList::compatibility_iterator menuIter = menuList.GetFirst(); - - while (pos-- > 0) - menuIter = menuIter->GetNext(); - - return menuIter->GetData() ; -} - void wxMenu::Init() { m_doBreak = false; @@ -126,7 +111,7 @@ void wxMenu::SetNoEventsMode( bool noEvents ) bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) { wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") ); - m_peer->InsertOrAppend( pItem, pos ); + GetPeer()->InsertOrAppend( pItem, pos ); if ( pItem->IsSeparator() ) { @@ -225,6 +210,17 @@ wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) { + if ( m_startRadioGroup != -1 ) + { + // Check if we're removing the item starting the radio group + if ( GetMenuItems().Item(m_startRadioGroup)->GetData() == item ) + { + // Yes, we do, so reset its index as the next item added shouldn't + // count as part of the same radio group anyhow. + m_startRadioGroup = -1; + } + } + /* // we need to find the items position in the child list size_t pos; @@ -243,7 +239,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) wxOSXMenuRemoveItem(m_hMenu , pos ); */ - m_peer->Remove( item ); + GetPeer()->Remove( item ); // and from internal data structures return wxMenuBase::DoRemove(item); } @@ -251,7 +247,7 @@ wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) void wxMenu::SetTitle(const wxString& label) { m_title = label ; - m_peer->SetTitle( wxStripMenuCodes( label ) ); + GetPeer()->SetTitle( wxStripMenuCodes( label ) ); } bool wxMenu::ProcessCommand(wxCommandEvent & event) @@ -352,8 +348,8 @@ void wxMenu::DoRearrange() bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow ) { - int id = item ? item->GetId() : 0; - wxUpdateUIEvent event(id); + int menuid = item ? item->GetId() : 0; + wxUpdateUIEvent event(menuid); event.SetEventObject( this ); bool processed = false; @@ -383,11 +379,11 @@ bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow { // if anything changed, update the changed attribute if (event.GetSetText()) - SetLabel(id, event.GetText()); + SetLabel(menuid, event.GetText()); if (event.GetSetChecked()) - Check(id, event.GetChecked()); + Check(menuid, event.GetChecked()); if (event.GetSetEnabled()) - Enable(id, event.GetEnabled()); + Enable(menuid, event.GetEnabled()); } else { @@ -396,11 +392,11 @@ bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow // always reset them ourselves UInt32 cmd = 0; - if ( id == wxApp::s_macExitMenuItemId ) + if ( menuid == wxApp::s_macExitMenuItemId ) { cmd = kHICommandQuit; } - else if (id == wxApp::s_macPreferencesMenuItemId ) + else if (menuid == wxApp::s_macPreferencesMenuItemId ) { cmd = kHICommandPreferences; } @@ -421,45 +417,60 @@ bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow bool wxMenu::HandleCommandProcess( wxMenuItem* item, wxWindow* senderWindow ) { - int id = item ? item->GetId() : 0; + int menuid = item ? item->GetId() : 0; bool processed = false; if (item->IsCheckable()) item->Check( !item->IsChecked() ) ; - if ( SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ) + if ( SendEvent( menuid , item->IsCheckable() ? item->IsChecked() : -1 ) ) processed = true ; else { if ( senderWindow != NULL ) { - wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , id); - event.SetEventObject(senderWindow); + wxCommandEvent event(wxEVT_MENU , menuid); + event.SetEventObject(this); event.SetInt(item->IsCheckable() ? item->IsChecked() : -1); if ( senderWindow->HandleWindowEvent(event) ) processed = true ; } } + + if(!processed && item) + { + processed = item->GetPeer()->DoDefault(); + } + return processed; } void wxMenu::HandleMenuItemHighlighted( wxMenuItem* item ) { - int id = item ? item->GetId() : 0; - wxMenuEvent wxevent(wxEVT_MENU_HIGHLIGHT, id, this); + int menuid = item ? item->GetId() : 0; + wxMenuEvent wxevent(wxEVT_MENU_HIGHLIGHT, menuid, this); DoHandleMenuEvent( wxevent ); } -void wxMenu::HandleMenuOpened() +void wxMenu::DoHandleMenuOpenedOrClosed(wxEventType evtType) { - wxMenuEvent wxevent(wxEVT_MENU_OPEN, 0, this); + // Popup menu being currently shown or NULL, defined in wincmn.cpp. + extern wxMenu *wxCurrentPopupMenu; + + // Set the id to allow wxMenuEvent::IsPopup() to work correctly. + int menuid = this == wxCurrentPopupMenu ? wxID_ANY : 0; + wxMenuEvent wxevent(evtType, menuid, this); DoHandleMenuEvent( wxevent ); } +void wxMenu::HandleMenuOpened() +{ + DoHandleMenuOpenedOrClosed(wxEVT_MENU_OPEN); +} + void wxMenu::HandleMenuClosed() { - wxMenuEvent wxevent(wxEVT_MENU_CLOSE, 0, this); - DoHandleMenuEvent( wxevent ); + DoHandleMenuOpenedOrClosed(wxEVT_MENU_CLOSE); } bool wxMenu::DoHandleMenuEvent(wxEvent& wxevent) @@ -506,8 +517,37 @@ wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ; bool wxMenuBar::s_macAutoWindowMenu = true ; WXHMENU wxMenuBar::s_macWindowMenuHandle = NULL ; + +wxMenu* emptyMenuBar = NULL; + +const int firstMenuPos = 1; // to account for the 0th application menu on mac + void wxMenuBar::Init() { + if ( emptyMenuBar == NULL ) + { + emptyMenuBar = new wxMenu(); + + wxMenu* appleMenu = new wxMenu(); + appleMenu->SetAllowRearrange(false); +#if !wxOSX_USE_CARBON + // standard menu items, handled in wxMenu::HandleCommandProcess(), see above: + wxString hideLabel; + hideLabel = wxString::Format(_("Hide %s"), wxTheApp ? wxTheApp->GetAppDisplayName() : _("Application")); + appleMenu->Append( wxID_OSX_HIDE, hideLabel + "\tCtrl+H" ); + appleMenu->Append( wxID_OSX_HIDEOTHERS, _("Hide Others")+"\tAlt+Ctrl+H" ); + appleMenu->Append( wxID_OSX_SHOWALL, _("Show All") ); + appleMenu->AppendSeparator(); + + // Do always add "Quit" item unconditionally however, it can't be disabled. + wxString quitLabel; + quitLabel = wxString::Format(_("Quit %s"), wxTheApp ? wxTheApp->GetAppDisplayName() : _("Application")); + appleMenu->Append( wxApp::s_macExitMenuItemId, quitLabel + "\tCtrl+Q" ); +#endif // !wxOSX_USE_CARBON + + emptyMenuBar->AppendSubMenu(appleMenu, "\x14") ; + } + m_eventHandler = this; m_menuBarFrame = NULL; m_rootMenu = new wxMenu(); @@ -521,7 +561,7 @@ void wxMenuBar::Init() // recommended, sometimes these items really don't make sense. if ( wxApp::s_macAboutMenuItemId != wxID_NONE ) { - wxString aboutLabel("About"); + wxString aboutLabel(_("About")); if ( wxTheApp ) aboutLabel << ' ' << wxTheApp->GetAppDisplayName(); else @@ -533,12 +573,23 @@ void wxMenuBar::Init() #if !wxOSX_USE_CARBON if ( wxApp::s_macPreferencesMenuItemId != wxID_NONE ) { - m_appleMenu->Append( wxApp::s_macPreferencesMenuItemId, "Preferences..." ); + m_appleMenu->Append( wxApp::s_macPreferencesMenuItemId, + _("Preferences...") + "\tCtrl+," ); m_appleMenu->AppendSeparator(); } + // standard menu items, handled in wxMenu::HandleCommandProcess(), see above: + wxString hideLabel; + hideLabel = wxString::Format(_("Hide %s"), wxTheApp ? wxTheApp->GetAppDisplayName() : _("Application")); + m_appleMenu->Append( wxID_OSX_HIDE, hideLabel + "\tCtrl+H" ); + m_appleMenu->Append( wxID_OSX_HIDEOTHERS, _("Hide Others")+"\tAlt+Ctrl+H" ); + m_appleMenu->Append( wxID_OSX_SHOWALL, _("Show All") ); + m_appleMenu->AppendSeparator(); + // Do always add "Quit" item unconditionally however, it can't be disabled. - m_appleMenu->Append( wxApp::s_macExitMenuItemId, "Quit\tCtrl+Q" ); + wxString quitLabel; + quitLabel = wxString::Format(_("Quit %s"), wxTheApp ? wxTheApp->GetAppDisplayName() : _("Application")); + m_appleMenu->Append( wxApp::s_macExitMenuItemId, quitLabel + "\tCtrl+Q" ); #endif // !wxOSX_USE_CARBON m_rootMenu->AppendSubMenu(m_appleMenu, "\x14") ; @@ -558,12 +609,9 @@ wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], lon { Init(); - m_titles.Alloc(count); - for ( size_t i = 0; i < count; i++ ) { m_menus.Append(menus[i]); - m_titles.Add(titles[i]); menus[i]->Attach(this); Append( menus[i], titles[i] ); @@ -577,8 +625,16 @@ wxMenuBar::~wxMenuBar() if (s_macInstalledMenuBar == this) { + emptyMenuBar->GetPeer()->MakeRoot(); s_macInstalledMenuBar = NULL; } + wxDELETE( m_rootMenu ); + // apple menu is a submenu, therefore we don't have to delete it + m_appleMenu = NULL; + + // deleting the root menu also removes all its wxMenu* submenus, therefore + // we must avoid double deleting them in the superclass destructor + m_menus.clear(); } void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect)) @@ -592,29 +648,37 @@ void wxMenuBar::MacInstallMenuBar() return ; m_rootMenu->GetPeer()->MakeRoot(); - + + // hide items in the apple menu that don't exist in the wx menubar + + wxMenuItem* appleItem = NULL; + wxMenuItem* wxItem = NULL; + + int menuid = wxApp::s_macAboutMenuItemId; + appleItem = m_appleMenu->FindItem(menuid); + wxItem = FindItem(menuid); + if ( appleItem != NULL ) + { + if ( wxItem == NULL ) + appleItem->GetPeer()->Hide(); + else + appleItem->SetItemLabel(wxItem->GetItemLabel()); + } + + menuid = wxApp::s_macPreferencesMenuItemId; + appleItem = m_appleMenu->FindItem(menuid); + wxItem = FindItem(menuid); + if ( appleItem != NULL ) + { + if ( wxItem == NULL ) + appleItem->GetPeer()->Hide(); + else + appleItem->SetItemLabel(wxItem->GetItemLabel()); + } + + #if 0 - MenuBarHandle menubar = NULL ; - - menubar = NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ; - - ::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 - InsertMenuItemTextWithCFString( appleMenu, - CFSTR(""), 0, kMenuItemAttrSeparator, 0); - 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 ; @@ -755,7 +819,7 @@ void wxMenuBar::MacInstallMenuBar() UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], GetFont().GetEncoding() ) ; menu->MacBeforeDisplay(false) ; - ::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus, i)->GetHMenu()), 0); + ::InsertMenu(MAC_WXHMENU(GetMenu(i)->GetHMenu()), 0); } } @@ -794,11 +858,22 @@ void wxMenuBar::EnableTop(size_t pos, bool enable) { wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); - m_rootMenu->FindItemByPosition( pos )->Enable(enable); + m_rootMenu->FindItemByPosition(pos+firstMenuPos)->Enable(enable); Refresh(); } +bool wxMenuBar::IsEnabledTop(size_t pos) const +{ + wxCHECK_MSG( IsAttached(), true, + wxT("doesn't work with unattached menubars") ); + + wxMenuItem* const item = m_rootMenu->FindItemByPosition(pos+firstMenuPos); + wxCHECK_MSG( item, false, wxT("invalid menu index") ); + + return item->IsEnabled(); +} + bool wxMenuBar::Enable(bool enable) { wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") ); @@ -814,12 +889,7 @@ void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label) { wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); - m_titles[pos] = label; - - if ( !IsAttached() ) - return; - - _wxMenuAt(m_menus, pos)->SetTitle( label ) ; + GetMenu(pos)->SetTitle( label ) ; } wxString wxMenuBar::GetMenuLabel(size_t pos) const @@ -827,38 +897,19 @@ wxString wxMenuBar::GetMenuLabel(size_t pos) const wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, wxT("invalid menu index in wxMenuBar::GetMenuLabel") ); - return m_titles[pos]; -} - -int wxMenuBar::FindMenu(const wxString& title) -{ - wxString menuTitle = wxStripMenuCodes(title); - - size_t count = GetMenuCount(); - for ( size_t i = 0; i < count; i++ ) - { - wxString title = wxStripMenuCodes(m_titles[i]); - if ( menuTitle == title ) - return i; - } - - return wxNOT_FOUND; + return GetMenu(pos)->GetTitle(); } // --------------------------------------------------------------------------- // wxMenuBar construction // --------------------------------------------------------------------------- -const int firstMenuPos = 1; // to account for the 0th application menu on mac - wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) { wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); if ( !menuOld ) return NULL; - m_titles[pos] = title; - wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos); m_rootMenu->Remove(item); m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) ); @@ -871,9 +922,8 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Insert(pos, menu, title) ) return false; - m_titles.Insert(title, pos); - m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) ); + menu->SetTitle(title); return true; } @@ -887,8 +937,6 @@ wxMenu *wxMenuBar::Remove(size_t pos) wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos); m_rootMenu->Remove(item); - m_titles.RemoveAt(pos); - return menu; } @@ -900,9 +948,8 @@ bool wxMenuBar::Append(wxMenu *menu, const wxString& title) if ( !wxMenuBarBase::Append(menu, title) ) return false; - m_titles.Add(title); - m_rootMenu->AppendSubMenu(menu, title); + menu->SetTitle(title); return true; } @@ -917,37 +964,4 @@ void wxMenuBar::Attach(wxFrame *frame) wxMenuBarBase::Attach( frame ) ; } -// --------------------------------------------------------------------------- -// wxMenuBar searching for menu items -// --------------------------------------------------------------------------- - -// Find the itemString in menuString, and return the item id or wxNOT_FOUND -int wxMenuBar::FindMenuItem(const wxString& menuString, - const wxString& itemString) const -{ - wxString menuLabel = wxStripMenuCodes(menuString); - size_t count = GetMenuCount(); - for ( size_t i = 0; i < count; i++ ) - { - wxString title = wxStripMenuCodes(m_titles[i]); - if ( menuLabel == title ) - return _wxMenuAt(m_menus, i)->FindItem(itemString); - } - - return wxNOT_FOUND; -} - -wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const -{ - if ( itemMenu ) - *itemMenu = NULL; - - wxMenuItem *item = NULL; - size_t count = GetMenuCount(); - for ( size_t i = 0; !item && (i < count); i++ ) - item = _wxMenuAt(m_menus, i)->FindItem(id, itemMenu); - - return item; -} - -#endif +#endif // wxUSE_MENUS