1 /////////////////////////////////////////////////////////////////////////////
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"
23 #include "wx/menuitem.h"
24 #include "wx/window.h"
29 #include "wx/mac/uma.h"
31 // other standard headers
32 // ----------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
36 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
38 // the (popup) menu title has this special id
39 static const int idMenuTitle
= -3;
41 const short kwxMacMenuBarResource
= 1 ;
42 const short kwxMacAppleMenuId
= 1 ;
45 // Find an item given the Macintosh Menu Reference
47 WX_DECLARE_HASH_MAP(MenuRef
, wxMenu
*, wxPointerHash
, wxPointerEqual
, MacMenuMap
);
49 static MacMenuMap wxWinMacMenuList
;
51 wxMenu
*wxFindMenuFromMacMenu(MenuRef inMenuRef
)
53 MacMenuMap::iterator node
= wxWinMacMenuList
.find(inMenuRef
);
55 return (node
== wxWinMacMenuList
.end()) ? NULL
: node
->second
;
58 void wxAssociateMenuWithMacMenu(MenuRef inMenuRef
, wxMenu
*menu
) ;
59 void wxAssociateMenuWithMacMenu(MenuRef inMenuRef
, wxMenu
*menu
)
61 // adding NULL MenuRef is (first) surely a result of an error and
62 // (secondly) breaks menu command processing
63 wxCHECK_RET( inMenuRef
!= (MenuRef
) NULL
, wxT("attempt to add a NULL MenuRef to menu list") );
65 wxWinMacMenuList
[inMenuRef
] = menu
;
68 void wxRemoveMacMenuAssociation(wxMenu
*menu
) ;
69 void wxRemoveMacMenuAssociation(wxMenu
*menu
)
71 // iterate over all the elements in the class
72 MacMenuMap::iterator it
;
73 for ( it
= wxWinMacMenuList
.begin(); it
!= wxWinMacMenuList
.end(); ++it
)
75 if ( it
->second
== menu
)
77 wxWinMacMenuList
.erase(it
);
83 // ============================================================================
85 // ============================================================================
86 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
) ;
87 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
);
91 // Construct a menu with optional title (then use append)
94 short wxMenu::s_macNextMenuId
= 3 ;
96 short wxMenu::s_macNextMenuId
= 2 ;
101 _wxMenuAt(const wxMenuList
&menuList
, size_t pos
)
103 wxMenuList::compatibility_iterator menuIter
= menuList
.GetFirst();
105 while (pos
-- > 0) menuIter
= menuIter
->GetNext();
107 return menuIter
->GetData() ;
113 m_startRadioGroup
= -1;
116 m_macMenuId
= s_macNextMenuId
++;
117 m_hMenu
= UMANewMenu(m_macMenuId
, m_title
, wxFont::GetDefaultEncoding() );
121 wxLogLastError(wxT("UMANewMenu failed"));
124 wxAssociateMenuWithMacMenu( (MenuRef
)m_hMenu
, this ) ;
126 // if we have a title, insert it in the beginning of the menu
127 if ( !m_title
.empty() )
129 Append(idMenuTitle
, m_title
) ;
136 wxRemoveMacMenuAssociation( this ) ;
137 if (MAC_WXHMENU(m_hMenu
))
138 ::DisposeMenu(MAC_WXHMENU(m_hMenu
));
143 // not available on the mac platform
146 void wxMenu::Attach(wxMenuBarBase
*menubar
)
148 wxMenuBase::Attach(menubar
);
153 // function appends a new item or submenu to the menu
154 // append a new item or submenu to the menu
155 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
157 wxASSERT_MSG( pItem
!= NULL
, wxT("can't append NULL item to the menu") );
159 if ( pItem
->IsSeparator() )
161 if ( pos
== (size_t)-1 )
162 MacAppendMenu(MAC_WXHMENU(m_hMenu
), "\p-");
164 MacInsertMenuItem(MAC_WXHMENU(m_hMenu
), "\p-" , pos
);
168 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
169 if ( pSubMenu
!= NULL
)
171 wxASSERT_MSG( pSubMenu
->m_hMenu
!= NULL
, wxT("invalid submenu added"));
172 pSubMenu
->m_menuParent
= this ;
174 if (wxMenuBar::MacGetInstalledMenuBar() == GetMenuBar())
176 pSubMenu
->MacBeforeDisplay( true ) ;
179 if ( pos
== (size_t)-1 )
180 UMAAppendSubMenuItem(MAC_WXHMENU(m_hMenu
), pItem
->GetText(), wxFont::GetDefaultEncoding() , pSubMenu
->m_macMenuId
);
182 UMAInsertSubMenuItem(MAC_WXHMENU(m_hMenu
), pItem
->GetText(), wxFont::GetDefaultEncoding() , pos
, pSubMenu
->m_macMenuId
);
183 pItem
->UpdateItemBitmap() ;
184 pItem
->UpdateItemStatus() ;
188 if ( pos
== (size_t)-1 )
190 UMAAppendMenuItem(MAC_WXHMENU(m_hMenu
), wxT("a") , wxFont::GetDefaultEncoding() );
191 pos
= CountMenuItems(MAC_WXHMENU(m_hMenu
)) ;
195 // MacOS counts menu items from 1 and inserts after, therefore having the
196 // same effect as wx 0 based and inserting before, we must correct pos
197 // after however for updates to be correct
198 UMAInsertMenuItem(MAC_WXHMENU(m_hMenu
), wxT("a"), wxFont::GetDefaultEncoding(), pos
);
202 SetMenuItemCommandID( MAC_WXHMENU(m_hMenu
) , pos
, pItem
->GetId() ) ;
203 SetMenuItemRefCon( MAC_WXHMENU(m_hMenu
) , pos
, (UInt32
) pItem
) ;
204 pItem
->UpdateItemText() ;
205 pItem
->UpdateItemBitmap() ;
206 pItem
->UpdateItemStatus() ;
208 if ( pItem
->GetId() == idMenuTitle
)
210 UMAEnableMenuItem(MAC_WXHMENU(m_hMenu
) , pos
, false ) ;
214 // if we're already attached to the menubar, we must update it
215 if ( IsAttached() && GetMenuBar()->IsAttached() )
217 GetMenuBar()->Refresh();
222 void wxMenu::EndRadioGroup()
224 // we're not inside a radio group any longer
225 m_startRadioGroup
= -1;
228 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
230 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
234 if ( item
->GetKind() == wxITEM_RADIO
)
236 int count
= GetMenuItemCount();
238 if ( m_startRadioGroup
== -1 )
240 // start a new radio group
241 m_startRadioGroup
= count
;
243 // for now it has just one element
244 item
->SetAsRadioGroupStart();
245 item
->SetRadioGroupEnd(m_startRadioGroup
);
247 // ensure that we have a checked item in the radio group
250 else // extend the current radio group
252 // we need to update its end item
253 item
->SetRadioGroupStart(m_startRadioGroup
);
254 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
258 node
->GetData()->SetRadioGroupEnd(count
);
262 wxFAIL_MSG( _T("where is the radio group start item?") );
266 else // not a radio item
271 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
278 // check the item initially
285 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
287 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
293 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
295 // we need to find the items position in the child list
297 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
298 for ( pos
= 0; node
; pos
++ )
300 if ( node
->GetData() == item
)
303 node
= node
->GetNext();
306 // DoRemove() (unlike Remove) can only be called for existing item!
307 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
309 ::DeleteMenuItem(MAC_WXHMENU(m_hMenu
) , pos
+ 1);
311 if ( IsAttached() && GetMenuBar()->IsAttached() )
313 // otherwise, the change won't be visible
314 GetMenuBar()->Refresh();
317 // and from internal data structures
318 return wxMenuBase::DoRemove(item
);
321 void wxMenu::SetTitle(const wxString
& label
)
324 UMASetMenuTitle(MAC_WXHMENU(m_hMenu
) , label
, wxFont::GetDefaultEncoding() ) ;
327 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
329 bool processed
= false;
331 // Try the menu's event handler
332 if ( /* !processed && */ GetEventHandler())
334 processed
= GetEventHandler()->ProcessEvent(event
);
337 // Try the window the menu was popped up from (and up through the
339 wxWindow
*win
= GetInvokingWindow();
340 if ( !processed
&& win
)
341 processed
= win
->GetEventHandler()->ProcessEvent(event
);
347 // ---------------------------------------------------------------------------
349 // ---------------------------------------------------------------------------
351 wxWindow
*wxMenu::GetWindow() const
353 if ( m_invokingWindow
!= NULL
)
354 return m_invokingWindow
;
355 else if ( GetMenuBar() != NULL
)
356 return (wxWindow
*) GetMenuBar()->GetFrame();
361 // helper functions returning the mac menu position for a certain item, note that this is
362 // mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0
364 int wxMenu::MacGetIndexFromId( int id
)
367 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
368 for ( pos
= 0; node
; pos
++ )
370 if ( node
->GetData()->GetId() == id
)
373 node
= node
->GetNext();
382 int wxMenu::MacGetIndexFromItem( wxMenuItem
*pItem
)
385 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
386 for ( pos
= 0; node
; pos
++ )
388 if ( node
->GetData() == pItem
)
391 node
= node
->GetNext();
400 void wxMenu::MacEnableMenu( bool bDoEnable
)
402 UMAEnableMenuItem(MAC_WXHMENU(m_hMenu
) , 0 , bDoEnable
) ;
407 // MacOS needs to know about submenus somewhere within this menu
408 // before it can be displayed , also hide special menu items like preferences
409 // that are handled by the OS
410 void wxMenu::MacBeforeDisplay( bool isSubMenu
)
412 wxMenuItem
* previousItem
= NULL
;
414 wxMenuItemList::compatibility_iterator node
;
416 for (pos
= 0, node
= GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
418 item
= (wxMenuItem
*)node
->GetData();
419 wxMenu
* subMenu
= item
->GetSubMenu() ;
422 subMenu
->MacBeforeDisplay( true ) ;
427 // what we do here is to hide the special items which are
428 // shown in the application menu anyhow -- it doesn't make
429 // sense to show them in their normal place as well
430 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
||
431 ( UMAGetSystemVersion() >= 0x1000 && (
432 item
->GetId() == wxApp::s_macPreferencesMenuItemId
||
433 item
->GetId() == wxApp::s_macExitMenuItemId
) ) )
436 ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ),
437 pos
+ 1, kMenuItemAttrHidden
, 0 );
439 // also check for a separator which was used just to
440 // separate this item from the others, so don't leave
441 // separator at the menu start or end nor 2 consecutive
443 wxMenuItemList::compatibility_iterator nextNode
= node
->GetNext();
444 wxMenuItem
*next
= nextNode
? nextNode
->GetData() : NULL
;
447 if ( !previousItem
&& next
&& next
->IsSeparator() )
449 // next (i.e. second as we must be first) item is
450 // the separator to hide
451 wxASSERT_MSG( pos
== 0, _T("should be the menu start") );
454 else if ( GetMenuItems().GetCount() == pos
+ 1 &&
455 previousItem
!= NULL
&&
456 previousItem
->IsSeparator() )
458 // prev item is a trailing separator we want to hide
461 else if ( previousItem
&& previousItem
->IsSeparator() &&
462 next
&& next
->IsSeparator() )
464 // two consecutive separators, this is one too many
467 else // no separators to hide
474 // hide the separator as well
475 ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ),
481 #endif // TARGET_CARBON
483 previousItem
= item
;
487 ::InsertMenu(MAC_WXHMENU( GetHMenu()), -1);
490 // undo all changes from the MacBeforeDisplay call
491 void wxMenu::MacAfterDisplay( bool isSubMenu
)
494 ::DeleteMenu(MacGetMenuId());
496 wxMenuItem
* previousItem
= NULL
;
498 wxMenuItemList::compatibility_iterator node
;
500 for (pos
= 0, node
= GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
502 item
= (wxMenuItem
*)node
->GetData();
503 wxMenu
* subMenu
= item
->GetSubMenu() ;
506 subMenu
->MacAfterDisplay( true ) ;
510 // no need to undo hidings
512 previousItem
= item
;
520 Mac Implementation note :
522 The Mac has only one global menubar, so we attempt to install the currently
523 active menubar from a frame, we currently don't take into account mdi-frames
524 which would ask for menu-merging
526 Secondly there is no mac api for changing a menubar that is not the current
527 menubar, so we have to wait for preparing the actual menubar until the
528 wxMenubar is to be used
530 We can in subsequent versions use MacInstallMenuBar to provide some sort of
531 auto-merge for MDI in case this will be necessary
535 wxMenuBar
* wxMenuBar::s_macInstalledMenuBar
= NULL
;
536 wxMenuBar
* wxMenuBar::s_macCommonMenuBar
= NULL
;
537 bool wxMenuBar::s_macAutoWindowMenu
= true ;
538 WXHMENU
wxMenuBar::s_macWindowMenuHandle
= NULL
;
540 void wxMenuBar::Init()
542 m_eventHandler
= this;
543 m_menuBarFrame
= NULL
;
544 m_invokingWindow
= (wxWindow
*) NULL
;
547 wxMenuBar::wxMenuBar()
552 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);
573 wxMenuBar::~wxMenuBar()
575 if (s_macCommonMenuBar
== this)
576 s_macCommonMenuBar
= NULL
;
577 if (s_macInstalledMenuBar
== this)
580 s_macInstalledMenuBar
= NULL
;
585 void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground
), const wxRect
*WXUNUSED(rect
))
587 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
592 void wxMenuBar::MacInstallMenuBar()
594 if ( s_macInstalledMenuBar
== this )
597 MenuBarHandle menubar
= NULL
;
598 #if TARGET_API_MAC_OSX
599 menubar
= NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ;
601 menubar
= NewHandleClear( 12 ) ;
602 (*menubar
)[3] = 0x0a ;
604 ::SetMenuBar( menubar
) ;
605 DisposeMenuBar( menubar
) ;
606 MenuHandle appleMenu
= NULL
;
607 char appleMenuTitle
[3] = { 01 , kMenuAppleLogoFilledGlyph
, 0 } ;
609 verify_noerr( CreateNewMenu( kwxMacAppleMenuId
, 0 , &appleMenu
) ) ;
610 verify_noerr( SetMenuTitle( appleMenu
, (ConstStr255Param
) appleMenuTitle
) );
612 // Add About/Preferences separator only on OS X
613 // KH/RN: Separator is always present on 10.3 but not on 10.2
614 // However, the change from 10.2 to 10.3 suggests it is preferred
615 #if TARGET_API_MAC_OSX
616 MacInsertMenuItem( appleMenu
, "\p-" , 0 ) ;
619 MacInsertMenuItem( appleMenu
, "\pAbout..." , 0 ) ;
620 MacInsertMenu( appleMenu
, 0 ) ;
622 // clean-up the help menu before adding new items
623 static MenuHandle mh
= NULL
;
627 MenuItemIndex firstUserHelpMenuItem
;
628 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) == noErr
)
630 for ( int i
= CountMenuItems( mh
) ; i
>= firstUserHelpMenuItem
; --i
)
632 DeleteMenuItem( mh
, i
) ;
641 if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId
)
643 wxMenuItem
*item
= FindItem( wxApp::s_macPreferencesMenuItemId
, NULL
) ;
644 if ( item
== NULL
|| !(item
->IsEnabled()) )
645 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
647 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
649 // Unlike preferences which may or may not exist, the Quit item should be always
650 // enabled unless it is added by the application and then disabled, otherwise
651 // a program would be required to add an item with wxID_EXIT in order to get the
652 // Quit menu item to be enabled, which seems a bit burdensome.
653 if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macExitMenuItemId
)
655 wxMenuItem
*item
= FindItem( wxApp::s_macExitMenuItemId
, NULL
) ;
656 if ( item
!= NULL
&& !(item
->IsEnabled()) )
657 DisableMenuCommand( NULL
, kHICommandQuit
) ;
659 EnableMenuCommand( NULL
, kHICommandQuit
) ;
662 wxMenuList::compatibility_iterator menuIter
= m_menus
.GetFirst();
664 for (size_t i
= 0; i
< m_menus
.GetCount(); i
++, menuIter
= menuIter
->GetNext())
666 wxMenuItemList::compatibility_iterator node
;
669 wxMenu
* menu
= menuIter
->GetData() , *subMenu
= NULL
;
671 if( m_titles
[i
] == wxT("?") || m_titles
[i
] == wxT("&?") || m_titles
[i
] == wxApp::s_macHelpMenuTitleName
)
673 for (pos
= 0 , node
= menu
->GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
675 item
= (wxMenuItem
*)node
->GetData();
676 subMenu
= item
->GetSubMenu() ;
679 // we don't support hierarchical menus in the help menu yet
683 if ( item
->GetId() != wxApp::s_macAboutMenuItemId
)
687 MenuItemIndex firstUserHelpMenuItem
;
688 if ( UMAGetHelpMenu( &mh
, &firstUserHelpMenuItem
) == noErr
)
698 if ( item
->IsSeparator() )
701 MacAppendMenu(mh
, "\p-" );
705 wxAcceleratorEntry
* entry
= wxGetAccelFromString( item
->GetText() ) ;
707 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
)
709 // this will be taken care of below
715 UMAAppendMenuItem(mh
, item
->GetText() , wxFont::GetDefaultEncoding(), entry
);
716 SetMenuItemCommandID( mh
, CountMenuItems(mh
) , item
->GetId() ) ;
717 SetMenuItemRefCon( mh
, CountMenuItems(mh
) , (UInt32
)item
) ;
728 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , m_titles
[i
], m_font
.GetEncoding() ) ;
729 menu
->MacBeforeDisplay(false) ;
730 ::InsertMenu(MAC_WXHMENU(_wxMenuAt(m_menus
, i
)->GetHMenu()), 0);
733 // take care of the about menu item wherever it is
736 wxMenuItem
*aboutMenuItem
= FindItem(wxApp::s_macAboutMenuItemId
, &aboutMenu
) ;
739 wxAcceleratorEntry
* entry
= wxGetAccelFromString( aboutMenuItem
->GetText() ) ;
740 UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId
) , 1 , aboutMenuItem
->GetText() , wxFont::GetDefaultEncoding() );
741 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId
) , 1 , true );
742 SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId
) , 1 , aboutMenuItem
->GetId() ) ;
743 SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId
) , 1 , (UInt32
)aboutMenuItem
) ;
744 UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId
) , 1 , entry
) ;
747 if ( GetAutoWindowMenu() )
749 if ( MacGetWindowMenuHMenu() == NULL
)
751 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
753 InsertMenu( (MenuHandle
) MacGetWindowMenuHMenu() , 0 ) ;
756 s_macInstalledMenuBar
= this;
759 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
761 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
762 _wxMenuAt(m_menus
, pos
)->MacEnableMenu( enable
) ;
766 bool wxMenuBar::Enable( bool enable
)
768 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
770 for (i
= 0; i
< GetMenuCount(); i
++)
772 EnableTop(i
, enable
);
777 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
779 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
781 m_titles
[pos
] = label
;
788 _wxMenuAt(m_menus
, pos
)->SetTitle( label
) ;
790 if (wxMenuBar::s_macInstalledMenuBar
== this) // are we currently installed ?
792 ::SetMenuBar( GetMenuBar() ) ;
797 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
799 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
800 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
802 return m_titles
[pos
];
805 int wxMenuBar::FindMenu(const wxString
& title
)
807 wxString menuTitle
= wxStripMenuCodes(title
);
809 size_t count
= GetMenuCount();
810 for ( size_t i
= 0; i
< count
; i
++ )
812 wxString title
= wxStripMenuCodes(m_titles
[i
]);
813 if ( menuTitle
== title
)
822 // ---------------------------------------------------------------------------
823 // wxMenuBar construction
824 // ---------------------------------------------------------------------------
826 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
828 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
831 m_titles
[pos
] = title
;
835 if (s_macInstalledMenuBar
== this)
837 menuOld
->MacAfterDisplay( false ) ;
838 ::DeleteMenu( menuOld
->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
840 menu
->MacBeforeDisplay( false ) ;
841 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , title
, m_font
.GetEncoding() ) ;
842 if ( pos
== m_menus
.GetCount() - 1)
844 ::InsertMenu( MAC_WXHMENU(menu
->GetHMenu()) , 0 ) ;
848 ::InsertMenu( MAC_WXHMENU(menu
->GetHMenu()) , _wxMenuAt(m_menus
, pos
+1)->MacGetMenuId() ) ;
855 if (m_invokingWindow
)
856 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
861 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
863 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
866 m_titles
.Insert(title
, pos
);
868 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , title
, m_font
.GetEncoding() ) ;
870 if ( IsAttached() && s_macInstalledMenuBar
== this )
872 if (s_macInstalledMenuBar
== this)
874 menu
->MacBeforeDisplay( false ) ;
875 if ( pos
== (size_t) -1 || pos
+ 1 == m_menus
.GetCount() )
877 ::InsertMenu( MAC_WXHMENU(menu
->GetHMenu()) , 0 ) ;
881 ::InsertMenu( MAC_WXHMENU(menu
->GetHMenu()) , _wxMenuAt(m_menus
, pos
+1)->MacGetMenuId() ) ;
886 if (m_invokingWindow
)
887 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
892 wxMenu
*wxMenuBar::Remove(size_t pos
)
894 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
900 if (s_macInstalledMenuBar
== this)
902 ::DeleteMenu( menu
->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
908 m_titles
.RemoveAt(pos
);
913 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
915 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
916 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
918 if ( !wxMenuBarBase::Append(menu
, title
) )
923 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , title
, m_font
.GetEncoding() ) ;
927 if (s_macInstalledMenuBar
== this)
929 menu
->MacBeforeDisplay( false ) ;
930 ::InsertMenu( MAC_WXHMENU(menu
->GetHMenu()) , 0 ) ;
936 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
937 // adding menu later on.
938 if (m_invokingWindow
)
939 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
944 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
)
946 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
948 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
951 wxMenuItem
*menuitem
= node
->GetData();
952 if (menuitem
->IsSubMenu())
953 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu() );
954 node
= node
->GetNext();
958 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
960 menu
->SetInvokingWindow( win
);
962 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
965 wxMenuItem
*menuitem
= node
->GetData();
966 if (menuitem
->IsSubMenu())
967 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu() , win
);
968 node
= node
->GetNext();
972 void wxMenuBar::UnsetInvokingWindow()
974 m_invokingWindow
= (wxWindow
*) NULL
;
975 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
978 wxMenu
*menu
= node
->GetData();
979 wxMenubarUnsetInvokingWindow( menu
);
980 node
= node
->GetNext();
984 void wxMenuBar::SetInvokingWindow(wxFrame
*frame
)
986 m_invokingWindow
= frame
;
987 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
990 wxMenu
*menu
= node
->GetData();
991 wxMenubarSetInvokingWindow( menu
, frame
);
992 node
= node
->GetNext();
996 void wxMenuBar::Detach()
998 wxMenuBarBase::Detach() ;
1001 void wxMenuBar::Attach(wxFrame
*frame
)
1003 wxMenuBarBase::Attach( frame
) ;
1005 // ---------------------------------------------------------------------------
1006 // wxMenuBar searching for menu items
1007 // ---------------------------------------------------------------------------
1009 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1010 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1011 const wxString
& itemString
) const
1013 wxString menuLabel
= wxStripMenuCodes(menuString
);
1014 size_t count
= GetMenuCount();
1015 for ( size_t i
= 0; i
< count
; i
++ )
1017 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1018 if ( menuLabel
== title
)
1019 return _wxMenuAt(m_menus
, i
)->FindItem(itemString
);
1025 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
1030 wxMenuItem
*item
= NULL
;
1031 size_t count
= GetMenuCount();
1032 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
1034 item
= _wxMenuAt(m_menus
, i
)->FindItem(id
, itemMenu
);