1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/menu_osx.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
19 #include "wx/wxprec.h"
30 #include "wx/menuitem.h"
33 #include "wx/osx/private.h"
35 // other standard headers
36 // ----------------------
39 IMPLEMENT_ABSTRACT_CLASS( wxMenuImpl
, wxObject
)
41 wxMenuImpl::~wxMenuImpl()
45 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
46 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
48 // the (popup) menu title has this special id
49 static const int idMenuTitle
= -3;
51 // ============================================================================
53 // ============================================================================
54 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
) ;
55 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
);
59 // Construct a menu with optional title (then use append)
63 _wxMenuAt(const wxMenuList
&menuList
, size_t pos
)
65 wxMenuList::compatibility_iterator menuIter
= menuList
.GetFirst();
68 menuIter
= menuIter
->GetNext();
70 return menuIter
->GetData() ;
76 m_startRadioGroup
= -1;
77 m_allowRearrange
= true;
78 m_noEventsMode
= false;
80 m_peer
= wxMenuImpl::Create( this, wxStripMenuCodes(m_title
) );
83 // if we have a title, insert it in the beginning of the menu
84 if ( !m_title
.empty() )
86 Append(idMenuTitle
, m_title
) ;
96 WXHMENU
wxMenu::GetHMenu() const
99 return m_peer
->GetHMenu();
105 // not available on the mac platform
108 void wxMenu::Attach(wxMenuBarBase
*menubar
)
110 wxMenuBase::Attach(menubar
);
115 void wxMenu::SetAllowRearrange( bool allow
)
117 m_allowRearrange
= allow
;
120 void wxMenu::SetNoEventsMode( bool noEvents
)
122 m_noEventsMode
= noEvents
;
125 // function appends a new item or submenu to the menu
126 // append a new item or submenu to the menu
127 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
129 wxASSERT_MSG( pItem
!= NULL
, wxT("can't append NULL item to the menu") );
130 m_peer
->InsertOrAppend( pItem
, pos
);
132 if ( pItem
->IsSeparator() )
134 // nothing to do here
138 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
139 if ( pSubMenu
!= NULL
)
141 wxASSERT_MSG( pSubMenu
->GetHMenu() != NULL
, wxT("invalid submenu added"));
142 pSubMenu
->m_menuParent
= this ;
144 pSubMenu
->DoRearrange();
148 if ( pItem
->GetId() == idMenuTitle
)
149 pItem
->GetMenu()->Enable( idMenuTitle
, false );
153 // if we're already attached to the menubar, we must update it
154 if ( IsAttached() && GetMenuBar()->IsAttached() )
155 GetMenuBar()->Refresh();
160 void wxMenu::EndRadioGroup()
162 // we're not inside a radio group any longer
163 m_startRadioGroup
= -1;
166 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
168 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
172 if ( item
->GetKind() == wxITEM_RADIO
)
174 int count
= GetMenuItemCount();
176 if ( m_startRadioGroup
== -1 )
178 // start a new radio group
179 m_startRadioGroup
= count
;
181 // for now it has just one element
182 item
->SetAsRadioGroupStart();
183 item
->SetRadioGroupEnd(m_startRadioGroup
);
185 // ensure that we have a checked item in the radio group
188 else // extend the current radio group
190 // we need to update its end item
191 item
->SetRadioGroupStart(m_startRadioGroup
);
192 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
196 node
->GetData()->SetRadioGroupEnd(count
);
200 wxFAIL_MSG( wxT("where is the radio group start item?") );
204 else // not a radio item
209 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
213 // check the item initially
219 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
221 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
227 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
230 // we need to find the items position in the child list
232 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
234 for ( pos = 0; node; pos++ )
236 if ( node->GetData() == item )
239 node = node->GetNext();
242 // DoRemove() (unlike Remove) can only be called for existing item!
243 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
245 wxOSXMenuRemoveItem(m_hMenu , pos );
247 m_peer
->Remove( item
);
248 // and from internal data structures
249 return wxMenuBase::DoRemove(item
);
252 void wxMenu::SetTitle(const wxString
& label
)
255 m_peer
->SetTitle( wxStripMenuCodes( label
) );
258 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
260 bool processed
= false;
262 // Try the menu's event handler
263 if ( /* !processed && */ GetEventHandler())
264 processed
= GetEventHandler()->SafelyProcessEvent(event
);
266 // Try the window the menu was popped up from
267 // (and up through the hierarchy)
268 wxWindow
*win
= GetInvokingWindow();
269 if ( !processed
&& win
)
270 processed
= win
->HandleWindowEvent(event
);
275 // ---------------------------------------------------------------------------
277 // ---------------------------------------------------------------------------
279 wxWindow
*wxMenu::GetWindow() const
281 if ( m_invokingWindow
!= NULL
)
282 return m_invokingWindow
;
283 else if ( GetMenuBar() != NULL
)
284 return (wxWindow
*) GetMenuBar()->GetFrame();
289 // MacOS needs to know about submenus somewhere within this menu
290 // before it can be displayed, also hide special menu items
291 // like preferences that are handled by the OS
292 void wxMenu::DoRearrange()
294 if ( !AllowRearrange() )
297 wxMenuItem
* previousItem
= NULL
;
299 wxMenuItemList::compatibility_iterator node
;
302 for (pos
= 0, node
= GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
304 item
= (wxMenuItem
*)node
->GetData();
305 wxMenu
* subMenu
= item
->GetSubMenu() ;
312 // what we do here is to hide the special items which are
313 // shown in the application menu anyhow -- it doesn't make
314 // sense to show them in their normal place as well
315 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
||
316 item
->GetId() == wxApp::s_macPreferencesMenuItemId
||
317 item
->GetId() == wxApp::s_macExitMenuItemId
)
320 item
->GetPeer()->Hide( true );
322 // also check for a separator which was used just to
323 // separate this item from the others, so don't leave
324 // separator at the menu start or end nor 2 consecutive
326 wxMenuItemList::compatibility_iterator nextNode
= node
->GetNext();
327 wxMenuItem
*next
= nextNode
? nextNode
->GetData() : NULL
;
329 wxMenuItem
*sepToHide
= 0;
330 if ( !previousItem
&& next
&& next
->IsSeparator() )
332 // next (i.e. second as we must be first) item is
333 // the separator to hide
334 wxASSERT_MSG( pos
== 0, wxT("should be the menu start") );
337 else if ( GetMenuItems().GetCount() == pos
+ 1 &&
338 previousItem
!= NULL
&&
339 previousItem
->IsSeparator() )
341 // prev item is a trailing separator we want to hide
342 sepToHide
= previousItem
;
344 else if ( previousItem
&& previousItem
->IsSeparator() &&
345 next
&& next
->IsSeparator() )
347 // two consecutive separators, this is one too many
353 // hide the separator as well
354 sepToHide
->GetPeer()->Hide( true );
359 previousItem
= item
;
364 bool wxMenu::HandleCommandUpdateStatus( wxMenuItem
* item
, wxWindow
* senderWindow
)
366 int id
= item
? item
->GetId() : 0;
367 wxUpdateUIEvent
event(id
);
368 event
.SetEventObject( this );
370 bool processed
= false;
372 // Try the menu's event handler
374 wxEvtHandler
*handler
= GetEventHandler();
376 processed
= handler
->ProcessEvent(event
);
379 // Try the window the menu was popped up from
380 // (and up through the hierarchy)
383 const wxMenuBase
*menu
= this;
386 wxWindow
*win
= menu
->GetInvokingWindow();
389 processed
= win
->HandleWindowEvent(event
);
393 menu
= menu
->GetParent();
397 if ( !processed
&& senderWindow
!= NULL
)
399 processed
= senderWindow
->HandleWindowEvent(event
);
404 // if anything changed, update the changed attribute
405 if (event
.GetSetText())
406 SetLabel(id
, event
.GetText());
407 if (event
.GetSetChecked())
408 Check(id
, event
.GetChecked());
409 if (event
.GetSetEnabled())
410 Enable(id
, event
.GetEnabled());
415 bool wxMenu::HandleCommandProcess( wxMenuItem
* item
, wxWindow
* senderWindow
)
417 int id
= item
? item
->GetId() : 0;
418 bool processed
= false;
419 if (item
->IsCheckable())
420 item
->Check( !item
->IsChecked() ) ;
422 if ( SendEvent( id
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
426 if ( senderWindow
!= NULL
)
428 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, id
);
429 event
.SetEventObject(senderWindow
);
430 event
.SetInt(item
->IsCheckable() ? item
->IsChecked() : -1);
432 if ( senderWindow
->HandleWindowEvent(event
) )
439 void wxMenu::HandleMenuItemHighlighted( wxMenuItem
* item
)
441 int id
= item
? item
->GetId() : 0;
442 wxMenuEvent
wxevent(wxEVT_MENU_HIGHLIGHT
, id
, this);
443 DoHandleMenuEvent( wxevent
);
446 void wxMenu::HandleMenuOpened()
448 wxMenuEvent
wxevent(wxEVT_MENU_OPEN
, 0, this);
449 DoHandleMenuEvent( wxevent
);
452 void wxMenu::HandleMenuClosed()
454 wxMenuEvent
wxevent(wxEVT_MENU_CLOSE
, 0, this);
455 DoHandleMenuEvent( wxevent
);
458 bool wxMenu::DoHandleMenuEvent(wxEvent
& wxevent
)
460 wxevent
.SetEventObject(this);
461 wxEvtHandler
* handler
= GetEventHandler();
462 if (handler
&& handler
->ProcessEvent(wxevent
))
468 wxWindow
*win
= GetInvokingWindow();
471 if ( win
->HandleWindowEvent(wxevent
) )
482 Mac Implementation note :
484 The Mac has only one global menubar, so we attempt to install the currently
485 active menubar from a frame, we currently don't take into account mdi-frames
486 which would ask for menu-merging
488 Secondly there is no mac api for changing a menubar that is not the current
489 menubar, so we have to wait for preparing the actual menubar until the
490 wxMenubar is to be used
492 We can in subsequent versions use MacInstallMenuBar to provide some sort of
493 auto-merge for MDI in case this will be necessary
497 wxMenuBar
* wxMenuBar::s_macInstalledMenuBar
= NULL
;
498 wxMenuBar
* wxMenuBar::s_macCommonMenuBar
= NULL
;
499 bool wxMenuBar::s_macAutoWindowMenu
= true ;
500 WXHMENU
wxMenuBar::s_macWindowMenuHandle
= NULL
;
502 void wxMenuBar::Init()
504 m_eventHandler
= this;
505 m_menuBarFrame
= NULL
;
506 m_invokingWindow
= NULL
;
507 m_rootMenu
= new wxMenu();
508 m_appleMenu
= new wxMenu();
509 m_appleMenu
->SetAllowRearrange(false);
510 m_appleMenu
->Append( wxApp::s_macAboutMenuItemId
, "About..." );
511 m_appleMenu
->AppendSeparator();
512 #if !wxOSX_USE_CARBON
513 m_appleMenu
->Append( wxApp::s_macPreferencesMenuItemId
, "Preferences..." );
514 m_appleMenu
->AppendSeparator();
515 m_appleMenu
->Append( wxApp::s_macExitMenuItemId
, "Quit\tCtrl+Q" );
518 m_rootMenu
->AppendSubMenu(m_appleMenu
, "\x14") ;
521 wxMenuBar::wxMenuBar()
526 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
531 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
535 m_titles
.Alloc(count
);
537 for ( size_t i
= 0; i
< count
; i
++ )
539 m_menus
.Append(menus
[i
]);
540 m_titles
.Add(titles
[i
]);
542 menus
[i
]->Attach(this);
543 Append( menus
[i
], titles
[i
] );
547 wxMenuBar::~wxMenuBar()
549 if (s_macCommonMenuBar
== this)
550 s_macCommonMenuBar
= NULL
;
552 if (s_macInstalledMenuBar
== this)
554 s_macInstalledMenuBar
= NULL
;
558 void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground
), const wxRect
*WXUNUSED(rect
))
560 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
563 void wxMenuBar::MacInstallMenuBar()
565 if ( s_macInstalledMenuBar
== this )
568 m_rootMenu
->GetPeer()->MakeRoot();
569 // DisableMenuCommand( NULL , kHICommandPreferences ) ;
572 MenuBarHandle menubar
= NULL
;
574 menubar
= NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ;
576 ::SetMenuBar( menubar
) ;
577 DisposeMenuBar( menubar
) ;
578 MenuHandle appleMenu
= NULL
;
580 verify_noerr( CreateNewMenu( kwxMacAppleMenuId
, 0 , &appleMenu
) ) ;
581 verify_noerr( SetMenuTitleWithCFString( appleMenu
, CFSTR( "\x14" ) ) );
583 // Add About/Preferences separator only on OS X
584 // KH/RN: Separator is always present on 10.3 but not on 10.2
585 // However, the change from 10.2 to 10.3 suggests it is preferred
586 InsertMenuItemTextWithCFString( appleMenu
,
587 CFSTR(""), 0, kMenuItemAttrSeparator
, 0);
588 InsertMenuItemTextWithCFString( appleMenu
,
589 CFSTR("About..."), 0, 0, 0);
590 MacInsertMenu( appleMenu
, 0 ) ;
592 // if we have a mac help menu, clean it up before adding new items
593 MenuHandle helpMenuHandle
;
594 MenuItemIndex firstUserHelpMenuItem
;
596 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle
, &firstUserHelpMenuItem
) == noErr
)
598 for ( int i
= CountMenuItems( helpMenuHandle
) ; i
>= firstUserHelpMenuItem
; --i
)
599 DeleteMenuItem( helpMenuHandle
, i
) ;
603 helpMenuHandle
= NULL
;
606 if ( wxApp::s_macPreferencesMenuItemId
)
608 wxMenuItem
*item
= FindItem( wxApp::s_macPreferencesMenuItemId
, NULL
) ;
609 if ( item
== NULL
|| !(item
->IsEnabled()) )
610 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
612 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
615 // Unlike preferences which may or may not exist, the Quit item should be always
616 // enabled unless it is added by the application and then disabled, otherwise
617 // a program would be required to add an item with wxID_EXIT in order to get the
618 // Quit menu item to be enabled, which seems a bit burdensome.
619 if ( wxApp::s_macExitMenuItemId
)
621 wxMenuItem
*item
= FindItem( wxApp::s_macExitMenuItemId
, NULL
) ;
622 if ( item
!= NULL
&& !(item
->IsEnabled()) )
623 DisableMenuCommand( NULL
, kHICommandQuit
) ;
625 EnableMenuCommand( NULL
, kHICommandQuit
) ;
628 wxString strippedHelpMenuTitle
= wxStripMenuCodes( wxApp::s_macHelpMenuTitleName
) ;
629 wxString strippedTranslatedHelpMenuTitle
= wxStripMenuCodes( wxString( _("&Help") ) ) ;
630 wxMenuList::compatibility_iterator menuIter
= m_menus
.GetFirst();
631 for (size_t i
= 0; i
< m_menus
.GetCount(); i
++, menuIter
= menuIter
->GetNext())
633 wxMenuItemList::compatibility_iterator node
;
635 wxMenu
* menu
= menuIter
->GetData() , *subMenu
= NULL
;
636 wxString strippedMenuTitle
= wxStripMenuCodes(m_titles
[i
]);
638 if ( strippedMenuTitle
== wxT("?") || strippedMenuTitle
== strippedHelpMenuTitle
|| strippedMenuTitle
== strippedTranslatedHelpMenuTitle
)
640 for (node
= menu
->GetMenuItems().GetFirst(); node
; node
= node
->GetNext())
642 item
= (wxMenuItem
*)node
->GetData();
643 subMenu
= item
->GetSubMenu() ;
646 // we don't support hierarchical menus in the help menu yet
647 UMAAppendMenuItem(mh
, wxStripMenuCodes(item
->GetText()) , wxFont::GetDefaultEncoding() );
648 MenuItemIndex position
= CountMenuItems(mh
);
649 ::SetMenuItemHierarchicalMenu(mh
, position
, MAC_WXHMENU(subMenu
->GetHMenu()));
653 if ( item
->GetId() != wxApp::s_macAboutMenuItemId
)
655 // we have found a user help menu and an item other than the about item,
656 // so we can create the mac help menu now, if we haven't created it yet
657 if ( helpMenuHandle
== NULL
)
659 if ( UMAGetHelpMenu( &helpMenuHandle
, &firstUserHelpMenuItem
) != noErr
)
661 helpMenuHandle
= NULL
;
667 if ( item
->IsSeparator() )
669 if ( helpMenuHandle
)
670 AppendMenuItemTextWithCFString( helpMenuHandle
,
671 CFSTR(""), kMenuItemAttrSeparator
, 0,NULL
);
676 entry
= wxAcceleratorEntry::Create( item
->GetItemLabel() ) ;
678 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
)
680 // this will be taken care of below
684 if ( helpMenuHandle
)
686 UMAAppendMenuItem(helpMenuHandle
, wxStripMenuCodes(item
->GetItemLabel()) , wxFont::GetDefaultEncoding(), entry
);
687 SetMenuItemCommandID( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , wxIdToMacCommand ( item
->GetId() ) ) ;
688 SetMenuItemRefCon( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , (URefCon
) item
) ;
698 else if ( ( m_titles
[i
] == wxT("Window") || m_titles
[i
] == wxT("&Window") )
699 && GetAutoWindowMenu() )
701 if ( MacGetWindowMenuHMenu() == NULL
)
703 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
706 MenuRef wm
= (MenuRef
)MacGetWindowMenuHMenu();
710 // get the insertion point in the standard menu
711 MenuItemIndex winListStart
;
712 GetIndMenuItemWithCommandID(wm
,
713 kHICommandWindowListSeparator
, 1, NULL
, &winListStart
);
715 // add a separator so that the standard items and the custom items
716 // aren't mixed together, but only if this is the first run
717 OSStatus err
= GetIndMenuItemWithCommandID(wm
,
718 'WXWM', 1, NULL
, NULL
);
720 if ( err
== menuItemNotFoundErr
)
722 InsertMenuItemTextWithCFString( wm
,
723 CFSTR(""), winListStart
-1, kMenuItemAttrSeparator
, 'WXWM');
726 wxInsertMenuItemsInMenu(menu
, wm
, winListStart
);
730 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , m_titles
[i
], GetFont().GetEncoding() ) ;
731 menu
->MacBeforeDisplay(false) ;
733 ::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus
, i
)->GetHMenu()), 0);
737 // take care of the about menu item wherever it is
740 wxMenuItem
*aboutMenuItem
= FindItem(wxApp::s_macAboutMenuItemId
, &aboutMenu
) ;
744 entry
= wxAcceleratorEntry::Create( aboutMenuItem
->GetItemLabel() ) ;
745 UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId
) , 1 , wxStripMenuCodes ( aboutMenuItem
->GetItemLabel() ) , wxFont::GetDefaultEncoding() );
746 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId
) , 1 , true );
747 SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId
) , 1 , kHICommandAbout
) ;
748 SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId
) , 1 , (URefCon
)aboutMenuItem
) ;
749 UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId
) , 1 , entry
) ;
754 if ( GetAutoWindowMenu() )
756 if ( MacGetWindowMenuHMenu() == NULL
)
757 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
759 InsertMenu( (MenuHandle
) MacGetWindowMenuHMenu() , 0 ) ;
765 s_macInstalledMenuBar
= this;
768 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
770 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
772 m_rootMenu
->FindItemByPosition( pos
)->Enable(enable
);
777 bool wxMenuBar::Enable(bool enable
)
779 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
782 for (i
= 0; i
< GetMenuCount(); i
++)
783 EnableTop(i
, enable
);
788 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
790 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
792 m_titles
[pos
] = label
;
797 _wxMenuAt(m_menus
, pos
)->SetTitle( label
) ;
800 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
802 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
803 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
805 return m_titles
[pos
];
808 int wxMenuBar::FindMenu(const wxString
& title
)
810 wxString menuTitle
= wxStripMenuCodes(title
);
812 size_t count
= GetMenuCount();
813 for ( size_t i
= 0; i
< count
; i
++ )
815 wxString title
= wxStripMenuCodes(m_titles
[i
]);
816 if ( menuTitle
== title
)
823 // ---------------------------------------------------------------------------
824 // wxMenuBar construction
825 // ---------------------------------------------------------------------------
827 const int firstMenuPos
= 1; // to account for the 0th application menu on mac
829 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
831 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
835 m_titles
[pos
] = title
;
837 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
838 m_rootMenu
->Remove(item
);
839 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
841 if (m_invokingWindow
)
842 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
847 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
849 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
852 m_titles
.Insert(title
, pos
);
854 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
856 if (m_invokingWindow
)
857 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
862 wxMenu
*wxMenuBar::Remove(size_t pos
)
864 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
868 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
869 m_rootMenu
->Remove(item
);
871 m_titles
.RemoveAt(pos
);
876 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
878 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
879 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
881 if ( !wxMenuBarBase::Append(menu
, title
) )
886 m_rootMenu
->AppendSubMenu(menu
, title
);
888 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
889 // adding menu later on.
890 if (m_invokingWindow
)
891 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
896 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
)
898 menu
->SetInvokingWindow( NULL
);
899 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
903 wxMenuItem
*menuitem
= node
->GetData();
904 if (menuitem
->IsSubMenu())
905 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu() );
907 node
= node
->GetNext();
911 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
913 menu
->SetInvokingWindow( win
);
914 wxMenuItem
*menuitem
;
915 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
919 menuitem
= node
->GetData();
920 if (menuitem
->IsSubMenu())
921 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu() , win
);
923 node
= node
->GetNext();
927 void wxMenuBar::UnsetInvokingWindow()
929 m_invokingWindow
= NULL
;
930 wxMenubarUnsetInvokingWindow(m_appleMenu
);
933 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
937 menu
= node
->GetData();
938 wxMenubarUnsetInvokingWindow( menu
);
940 node
= node
->GetNext();
944 void wxMenuBar::SetInvokingWindow(wxFrame
*frame
)
946 m_invokingWindow
= frame
;
947 wxMenubarSetInvokingWindow(m_appleMenu
, frame
);
950 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
954 menu
= node
->GetData();
955 wxMenubarSetInvokingWindow( menu
, frame
);
957 node
= node
->GetNext();
961 void wxMenuBar::Detach()
963 wxMenuBarBase::Detach() ;
966 void wxMenuBar::Attach(wxFrame
*frame
)
968 wxMenuBarBase::Attach( frame
) ;
971 // ---------------------------------------------------------------------------
972 // wxMenuBar searching for menu items
973 // ---------------------------------------------------------------------------
975 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
976 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
977 const wxString
& itemString
) const
979 wxString menuLabel
= wxStripMenuCodes(menuString
);
980 size_t count
= GetMenuCount();
981 for ( size_t i
= 0; i
< count
; i
++ )
983 wxString title
= wxStripMenuCodes(m_titles
[i
]);
984 if ( menuLabel
== title
)
985 return _wxMenuAt(m_menus
, i
)->FindItem(itemString
);
991 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
996 wxMenuItem
*item
= NULL
;
997 size_t count
= GetMenuCount();
998 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
999 item
= _wxMenuAt(m_menus
, i
)->FindItem(id
, itemMenu
);