1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/menu_osx.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
19 #include "wx/wxprec.h"
30 #include "wx/dialog.h"
31 #include "wx/menuitem.h"
34 #include "wx/osx/private.h"
36 // other standard headers
37 // ----------------------
40 IMPLEMENT_ABSTRACT_CLASS( wxMenuImpl
, wxObject
)
42 wxMenuImpl::~wxMenuImpl()
46 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
47 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
49 // the (popup) menu title has this special id
50 static const int idMenuTitle
= -3;
52 // ============================================================================
54 // ============================================================================
58 // Construct a menu with optional title (then use append)
62 _wxMenuAt(const wxMenuList
&menuList
, size_t pos
)
64 wxMenuList::compatibility_iterator menuIter
= menuList
.GetFirst();
67 menuIter
= menuIter
->GetNext();
69 return menuIter
->GetData() ;
75 m_startRadioGroup
= -1;
76 m_allowRearrange
= true;
77 m_noEventsMode
= false;
79 m_peer
= wxMenuImpl::Create( this, wxStripMenuCodes(m_title
) );
82 // if we have a title, insert it in the beginning of the menu
83 if ( !m_title
.empty() )
85 Append(idMenuTitle
, m_title
) ;
95 WXHMENU
wxMenu::GetHMenu() const
98 return m_peer
->GetHMenu();
104 // not available on the mac platform
107 void wxMenu::Attach(wxMenuBarBase
*menubar
)
109 wxMenuBase::Attach(menubar
);
114 void wxMenu::SetAllowRearrange( bool allow
)
116 m_allowRearrange
= allow
;
119 void wxMenu::SetNoEventsMode( bool noEvents
)
121 m_noEventsMode
= noEvents
;
124 // function appends a new item or submenu to the menu
125 // append a new item or submenu to the menu
126 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
128 wxASSERT_MSG( pItem
!= NULL
, wxT("can't append NULL item to the menu") );
129 m_peer
->InsertOrAppend( pItem
, pos
);
131 if ( pItem
->IsSeparator() )
133 // nothing to do here
137 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
138 if ( pSubMenu
!= NULL
)
140 wxASSERT_MSG( pSubMenu
->GetHMenu() != NULL
, wxT("invalid submenu added"));
141 pSubMenu
->m_menuParent
= this ;
143 pSubMenu
->DoRearrange();
147 if ( pItem
->GetId() == idMenuTitle
)
148 pItem
->GetMenu()->Enable( idMenuTitle
, false );
152 // if we're already attached to the menubar, we must update it
153 if ( IsAttached() && GetMenuBar()->IsAttached() )
154 GetMenuBar()->Refresh();
159 void wxMenu::EndRadioGroup()
161 // we're not inside a radio group any longer
162 m_startRadioGroup
= -1;
165 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
167 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
171 if ( item
->GetKind() == wxITEM_RADIO
)
173 int count
= GetMenuItemCount();
175 if ( m_startRadioGroup
== -1 )
177 // start a new radio group
178 m_startRadioGroup
= count
;
180 // for now it has just one element
181 item
->SetAsRadioGroupStart();
182 item
->SetRadioGroupEnd(m_startRadioGroup
);
184 // ensure that we have a checked item in the radio group
187 else // extend the current radio group
189 // we need to update its end item
190 item
->SetRadioGroupStart(m_startRadioGroup
);
191 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
195 node
->GetData()->SetRadioGroupEnd(count
);
199 wxFAIL_MSG( wxT("where is the radio group start item?") );
203 else // not a radio item
208 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
212 // check the item initially
218 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
220 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
226 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
229 // we need to find the items position in the child list
231 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
233 for ( pos = 0; node; pos++ )
235 if ( node->GetData() == item )
238 node = node->GetNext();
241 // DoRemove() (unlike Remove) can only be called for existing item!
242 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
244 wxOSXMenuRemoveItem(m_hMenu , pos );
246 m_peer
->Remove( item
);
247 // and from internal data structures
248 return wxMenuBase::DoRemove(item
);
251 void wxMenu::SetTitle(const wxString
& label
)
254 m_peer
->SetTitle( wxStripMenuCodes( label
) );
257 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
259 bool processed
= false;
261 // Try the menu's event handler
262 if ( /* !processed && */ GetEventHandler())
263 processed
= GetEventHandler()->SafelyProcessEvent(event
);
265 // Try the window the menu was popped up from
266 // (and up through the hierarchy)
267 wxWindow
*win
= GetWindow();
268 if ( !processed
&& win
)
269 processed
= win
->HandleWindowEvent(event
);
274 // ---------------------------------------------------------------------------
276 // ---------------------------------------------------------------------------
278 // MacOS needs to know about submenus somewhere within this menu
279 // before it can be displayed, also hide special menu items
280 // like preferences that are handled by the OS
281 void wxMenu::DoRearrange()
283 if ( !AllowRearrange() )
286 wxMenuItem
* previousItem
= NULL
;
288 wxMenuItemList::compatibility_iterator node
;
291 for (pos
= 0, node
= GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
293 item
= (wxMenuItem
*)node
->GetData();
294 wxMenu
* subMenu
= item
->GetSubMenu() ;
301 // what we do here is to hide the special items which are
302 // shown in the application menu anyhow -- it doesn't make
303 // sense to show them in their normal place as well
304 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
||
305 item
->GetId() == wxApp::s_macPreferencesMenuItemId
||
306 item
->GetId() == wxApp::s_macExitMenuItemId
)
309 item
->GetPeer()->Hide( true );
311 // also check for a separator which was used just to
312 // separate this item from the others, so don't leave
313 // separator at the menu start or end nor 2 consecutive
315 wxMenuItemList::compatibility_iterator nextNode
= node
->GetNext();
316 wxMenuItem
*next
= nextNode
? nextNode
->GetData() : NULL
;
318 wxMenuItem
*sepToHide
= 0;
319 if ( !previousItem
&& next
&& next
->IsSeparator() )
321 // next (i.e. second as we must be first) item is
322 // the separator to hide
323 wxASSERT_MSG( pos
== 0, wxT("should be the menu start") );
326 else if ( GetMenuItems().GetCount() == pos
+ 1 &&
327 previousItem
!= NULL
&&
328 previousItem
->IsSeparator() )
330 // prev item is a trailing separator we want to hide
331 sepToHide
= previousItem
;
333 else if ( previousItem
&& previousItem
->IsSeparator() &&
334 next
&& next
->IsSeparator() )
336 // two consecutive separators, this is one too many
342 // hide the separator as well
343 sepToHide
->GetPeer()->Hide( true );
348 previousItem
= item
;
353 bool wxMenu::HandleCommandUpdateStatus( wxMenuItem
* item
, wxWindow
* senderWindow
)
355 int id
= item
? item
->GetId() : 0;
356 wxUpdateUIEvent
event(id
);
357 event
.SetEventObject( this );
359 bool processed
= false;
361 // Try the menu's event handler
363 wxEvtHandler
*handler
= GetEventHandler();
365 processed
= handler
->ProcessEvent(event
);
368 // Try the window the menu was popped up from
369 // (and up through the hierarchy)
372 wxWindow
*win
= GetWindow();
374 processed
= win
->HandleWindowEvent(event
);
377 if ( !processed
&& senderWindow
!= NULL
)
379 processed
= senderWindow
->HandleWindowEvent(event
);
384 // if anything changed, update the changed attribute
385 if (event
.GetSetText())
386 SetLabel(id
, event
.GetText());
387 if (event
.GetSetChecked())
388 Check(id
, event
.GetChecked());
389 if (event
.GetSetEnabled())
390 Enable(id
, event
.GetEnabled());
395 // these two items are also managed by the Carbon Menu Manager, therefore we must
396 // always reset them ourselves
399 if ( id
== wxApp::s_macExitMenuItemId
)
401 cmd
= kHICommandQuit
;
403 else if (id
== wxApp::s_macPreferencesMenuItemId
)
405 cmd
= kHICommandPreferences
;
410 if ( !item
->IsEnabled() || wxDialog::OSXHasModalDialogsOpen() )
411 DisableMenuCommand( NULL
, cmd
) ;
413 EnableMenuCommand( NULL
, cmd
) ;
422 bool wxMenu::HandleCommandProcess( wxMenuItem
* item
, wxWindow
* senderWindow
)
424 int id
= item
? item
->GetId() : 0;
425 bool processed
= false;
426 if (item
->IsCheckable())
427 item
->Check( !item
->IsChecked() ) ;
429 if ( SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
433 if ( senderWindow
!= NULL
)
435 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
436 event
.SetEventObject(senderWindow
);
437 event
.SetInt(item
->IsCheckable() ? item
->IsChecked() : -1);
439 if ( senderWindow
->HandleWindowEvent(event
) )
446 void wxMenu::HandleMenuItemHighlighted( wxMenuItem
* item
)
448 int id
= item
? item
->GetId() : 0;
449 wxMenuEvent
wxevent(wxEVT_MENU_HIGHLIGHT
, id
, this);
450 DoHandleMenuEvent( wxevent
);
453 void wxMenu::HandleMenuOpened()
455 wxMenuEvent
wxevent(wxEVT_MENU_OPEN
, 0, this);
456 DoHandleMenuEvent( wxevent
);
459 void wxMenu::HandleMenuClosed()
461 wxMenuEvent
wxevent(wxEVT_MENU_CLOSE
, 0, this);
462 DoHandleMenuEvent( wxevent
);
465 bool wxMenu::DoHandleMenuEvent(wxEvent
& wxevent
)
467 wxevent
.SetEventObject(this);
468 wxEvtHandler
* handler
= GetEventHandler();
469 if (handler
&& handler
->ProcessEvent(wxevent
))
475 wxWindow
*win
= GetWindow();
478 if ( win
->HandleWindowEvent(wxevent
) )
489 Mac Implementation note :
491 The Mac has only one global menubar, so we attempt to install the currently
492 active menubar from a frame, we currently don't take into account mdi-frames
493 which would ask for menu-merging
495 Secondly there is no mac api for changing a menubar that is not the current
496 menubar, so we have to wait for preparing the actual menubar until the
497 wxMenubar is to be used
499 We can in subsequent versions use MacInstallMenuBar to provide some sort of
500 auto-merge for MDI in case this will be necessary
504 wxMenuBar
* wxMenuBar::s_macInstalledMenuBar
= NULL
;
505 wxMenuBar
* wxMenuBar::s_macCommonMenuBar
= NULL
;
506 bool wxMenuBar::s_macAutoWindowMenu
= true ;
507 WXHMENU
wxMenuBar::s_macWindowMenuHandle
= NULL
;
509 void wxMenuBar::Init()
511 m_eventHandler
= this;
512 m_menuBarFrame
= NULL
;
513 m_rootMenu
= new wxMenu();
514 m_rootMenu
->Attach(this);
516 m_appleMenu
= new wxMenu();
517 m_appleMenu
->SetAllowRearrange(false);
519 // Create standard items unless the application explicitly disabled this by
520 // setting the corresponding ids to wxID_NONE: although this is not
521 // recommended, sometimes these items really don't make sense.
522 if ( wxApp::s_macAboutMenuItemId
!= wxID_NONE
)
524 wxString
aboutLabel("About");
526 aboutLabel
<< ' ' << wxTheApp
->GetAppDisplayName();
529 m_appleMenu
->Append( wxApp::s_macAboutMenuItemId
, aboutLabel
);
530 m_appleMenu
->AppendSeparator();
533 #if !wxOSX_USE_CARBON
534 if ( wxApp::s_macPreferencesMenuItemId
!= wxID_NONE
)
536 m_appleMenu
->Append( wxApp::s_macPreferencesMenuItemId
,
537 "Preferences...\tCtrl+," );
538 m_appleMenu
->AppendSeparator();
541 // Do always add "Quit" item unconditionally however, it can't be disabled.
542 m_appleMenu
->Append( wxApp::s_macExitMenuItemId
, "Quit\tCtrl+Q" );
543 #endif // !wxOSX_USE_CARBON
545 m_rootMenu
->AppendSubMenu(m_appleMenu
, "\x14") ;
548 wxMenuBar::wxMenuBar()
553 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
558 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
562 m_titles
.Alloc(count
);
564 for ( size_t i
= 0; i
< count
; i
++ )
566 m_menus
.Append(menus
[i
]);
567 m_titles
.Add(titles
[i
]);
569 menus
[i
]->Attach(this);
570 Append( menus
[i
], titles
[i
] );
574 wxMenuBar::~wxMenuBar()
576 if (s_macCommonMenuBar
== this)
577 s_macCommonMenuBar
= NULL
;
579 if (s_macInstalledMenuBar
== this)
581 s_macInstalledMenuBar
= NULL
;
585 void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground
), const wxRect
*WXUNUSED(rect
))
587 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
590 void wxMenuBar::MacInstallMenuBar()
592 if ( s_macInstalledMenuBar
== this )
595 m_rootMenu
->GetPeer()->MakeRoot();
599 MenuBarHandle menubar
= NULL
;
601 menubar
= NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ;
603 ::SetMenuBar( menubar
) ;
604 DisposeMenuBar( menubar
) ;
605 MenuHandle appleMenu
= NULL
;
607 verify_noerr( CreateNewMenu( kwxMacAppleMenuId
, 0 , &appleMenu
) ) ;
608 verify_noerr( SetMenuTitleWithCFString( appleMenu
, CFSTR( "\x14" ) ) );
610 // Add About/Preferences separator only on OS X
611 // KH/RN: Separator is always present on 10.3 but not on 10.2
612 // However, the change from 10.2 to 10.3 suggests it is preferred
613 InsertMenuItemTextWithCFString( appleMenu
,
614 CFSTR(""), 0, kMenuItemAttrSeparator
, 0);
615 InsertMenuItemTextWithCFString( appleMenu
,
616 CFSTR("About..."), 0, 0, 0);
617 MacInsertMenu( appleMenu
, 0 ) ;
619 // if we have a mac help menu, clean it up before adding new items
620 MenuHandle helpMenuHandle
;
621 MenuItemIndex firstUserHelpMenuItem
;
623 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle
, &firstUserHelpMenuItem
) == noErr
)
625 for ( int i
= CountMenuItems( helpMenuHandle
) ; i
>= firstUserHelpMenuItem
; --i
)
626 DeleteMenuItem( helpMenuHandle
, i
) ;
630 helpMenuHandle
= NULL
;
633 if ( wxApp::s_macPreferencesMenuItemId
)
635 wxMenuItem
*item
= FindItem( wxApp::s_macPreferencesMenuItemId
, NULL
) ;
636 if ( item
== NULL
|| !(item
->IsEnabled()) )
637 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
639 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
642 // Unlike preferences which may or may not exist, the Quit item should be always
643 // enabled unless it is added by the application and then disabled, otherwise
644 // a program would be required to add an item with wxID_EXIT in order to get the
645 // Quit menu item to be enabled, which seems a bit burdensome.
646 if ( wxApp::s_macExitMenuItemId
)
648 wxMenuItem
*item
= FindItem( wxApp::s_macExitMenuItemId
, NULL
) ;
649 if ( item
!= NULL
&& !(item
->IsEnabled()) )
650 DisableMenuCommand( NULL
, kHICommandQuit
) ;
652 EnableMenuCommand( NULL
, kHICommandQuit
) ;
655 wxString strippedHelpMenuTitle
= wxStripMenuCodes( wxApp::s_macHelpMenuTitleName
) ;
656 wxString strippedTranslatedHelpMenuTitle
= wxStripMenuCodes( wxString( _("&Help") ) ) ;
657 wxMenuList::compatibility_iterator menuIter
= m_menus
.GetFirst();
658 for (size_t i
= 0; i
< m_menus
.GetCount(); i
++, menuIter
= menuIter
->GetNext())
660 wxMenuItemList::compatibility_iterator node
;
662 wxMenu
* menu
= menuIter
->GetData() , *subMenu
= NULL
;
663 wxString strippedMenuTitle
= wxStripMenuCodes(m_titles
[i
]);
665 if ( strippedMenuTitle
== wxT("?") || strippedMenuTitle
== strippedHelpMenuTitle
|| strippedMenuTitle
== strippedTranslatedHelpMenuTitle
)
667 for (node
= menu
->GetMenuItems().GetFirst(); node
; node
= node
->GetNext())
669 item
= (wxMenuItem
*)node
->GetData();
670 subMenu
= item
->GetSubMenu() ;
673 UMAAppendMenuItem(mh
, wxStripMenuCodes(item
->GetText()) , wxFont::GetDefaultEncoding() );
674 MenuItemIndex position
= CountMenuItems(mh
);
675 ::SetMenuItemHierarchicalMenu(mh
, position
, MAC_WXHMENU(subMenu
->GetHMenu()));
679 if ( item
->GetId() != wxApp::s_macAboutMenuItemId
)
681 // we have found a user help menu and an item other than the about item,
682 // so we can create the mac help menu now, if we haven't created it yet
683 if ( helpMenuHandle
== NULL
)
685 if ( UMAGetHelpMenu( &helpMenuHandle
, &firstUserHelpMenuItem
) != noErr
)
687 helpMenuHandle
= NULL
;
693 if ( item
->IsSeparator() )
695 if ( helpMenuHandle
)
696 AppendMenuItemTextWithCFString( helpMenuHandle
,
697 CFSTR(""), kMenuItemAttrSeparator
, 0,NULL
);
702 entry
= wxAcceleratorEntry::Create( item
->GetItemLabel() ) ;
704 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
)
706 // this will be taken care of below
710 if ( helpMenuHandle
)
712 UMAAppendMenuItem(helpMenuHandle
, wxStripMenuCodes(item
->GetItemLabel()) , wxFont::GetDefaultEncoding(), entry
);
713 SetMenuItemCommandID( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , wxIdToMacCommand ( item
->GetId() ) ) ;
714 SetMenuItemRefCon( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , (URefCon
) item
) ;
724 else if ( ( m_titles
[i
] == wxT("Window") || m_titles
[i
] == wxT("&Window") )
725 && GetAutoWindowMenu() )
727 if ( MacGetWindowMenuHMenu() == NULL
)
729 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
732 MenuRef wm
= (MenuRef
)MacGetWindowMenuHMenu();
736 // get the insertion point in the standard menu
737 MenuItemIndex winListStart
;
738 GetIndMenuItemWithCommandID(wm
,
739 kHICommandWindowListSeparator
, 1, NULL
, &winListStart
);
741 // add a separator so that the standard items and the custom items
742 // aren't mixed together, but only if this is the first run
743 OSStatus err
= GetIndMenuItemWithCommandID(wm
,
744 'WXWM', 1, NULL
, NULL
);
746 if ( err
== menuItemNotFoundErr
)
748 InsertMenuItemTextWithCFString( wm
,
749 CFSTR(""), winListStart
-1, kMenuItemAttrSeparator
, 'WXWM');
752 wxInsertMenuItemsInMenu(menu
, wm
, winListStart
);
756 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , m_titles
[i
], GetFont().GetEncoding() ) ;
757 menu
->MacBeforeDisplay(false) ;
759 ::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus
, i
)->GetHMenu()), 0);
763 // take care of the about menu item wherever it is
766 wxMenuItem
*aboutMenuItem
= FindItem(wxApp::s_macAboutMenuItemId
, &aboutMenu
) ;
770 entry
= wxAcceleratorEntry::Create( aboutMenuItem
->GetItemLabel() ) ;
771 UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId
) , 1 , wxStripMenuCodes ( aboutMenuItem
->GetItemLabel() ) , wxFont::GetDefaultEncoding() );
772 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId
) , 1 , true );
773 SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId
) , 1 , kHICommandAbout
) ;
774 SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId
) , 1 , (URefCon
)aboutMenuItem
) ;
775 UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId
) , 1 , entry
) ;
780 if ( GetAutoWindowMenu() )
782 if ( MacGetWindowMenuHMenu() == NULL
)
783 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
785 InsertMenu( (MenuHandle
) MacGetWindowMenuHMenu() , 0 ) ;
791 s_macInstalledMenuBar
= this;
794 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
796 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
798 m_rootMenu
->FindItemByPosition( pos
)->Enable(enable
);
803 bool wxMenuBar::Enable(bool enable
)
805 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
808 for (i
= 0; i
< GetMenuCount(); i
++)
809 EnableTop(i
, enable
);
814 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
816 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
818 m_titles
[pos
] = label
;
823 _wxMenuAt(m_menus
, pos
)->SetTitle( label
) ;
826 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
828 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
829 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
831 return m_titles
[pos
];
834 int wxMenuBar::FindMenu(const wxString
& title
)
836 wxString menuTitle
= wxStripMenuCodes(title
);
838 size_t count
= GetMenuCount();
839 for ( size_t i
= 0; i
< count
; i
++ )
841 wxString title
= wxStripMenuCodes(m_titles
[i
]);
842 if ( menuTitle
== title
)
849 // ---------------------------------------------------------------------------
850 // wxMenuBar construction
851 // ---------------------------------------------------------------------------
853 const int firstMenuPos
= 1; // to account for the 0th application menu on mac
855 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
857 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
861 m_titles
[pos
] = title
;
863 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
864 m_rootMenu
->Remove(item
);
865 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
870 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
872 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
875 m_titles
.Insert(title
, pos
);
877 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
882 wxMenu
*wxMenuBar::Remove(size_t pos
)
884 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
888 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
889 m_rootMenu
->Remove(item
);
891 m_titles
.RemoveAt(pos
);
896 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
898 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
899 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
901 if ( !wxMenuBarBase::Append(menu
, title
) )
906 m_rootMenu
->AppendSubMenu(menu
, title
);
911 void wxMenuBar::Detach()
913 wxMenuBarBase::Detach() ;
916 void wxMenuBar::Attach(wxFrame
*frame
)
918 wxMenuBarBase::Attach( frame
) ;
921 // ---------------------------------------------------------------------------
922 // wxMenuBar searching for menu items
923 // ---------------------------------------------------------------------------
925 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
926 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
927 const wxString
& itemString
) const
929 wxString menuLabel
= wxStripMenuCodes(menuString
);
930 size_t count
= GetMenuCount();
931 for ( size_t i
= 0; i
< count
; i
++ )
933 wxString title
= wxStripMenuCodes(m_titles
[i
]);
934 if ( menuLabel
== title
)
935 return _wxMenuAt(m_menus
, i
)->FindItem(itemString
);
941 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
946 wxMenuItem
*item
= NULL
;
947 size_t count
= GetMenuCount();
948 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
949 item
= _wxMenuAt(m_menus
, i
)->FindItem(id
, itemMenu
);