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 // the (popup) menu title has this special menuid
47 static const int idMenuTitle
= -3;
49 // ============================================================================
51 // ============================================================================
55 // Construct a menu with optional title (then use append)
60 m_startRadioGroup
= -1;
61 m_allowRearrange
= true;
62 m_noEventsMode
= false;
64 m_peer
= wxMenuImpl::Create( this, wxStripMenuCodes(m_title
) );
67 // if we have a title, insert it in the beginning of the menu
68 if ( !m_title
.empty() )
70 Append(idMenuTitle
, m_title
) ;
80 WXHMENU
wxMenu::GetHMenu() const
83 return m_peer
->GetHMenu();
89 // not available on the mac platform
92 void wxMenu::Attach(wxMenuBarBase
*menubar
)
94 wxMenuBase::Attach(menubar
);
99 void wxMenu::SetAllowRearrange( bool allow
)
101 m_allowRearrange
= allow
;
104 void wxMenu::SetNoEventsMode( bool noEvents
)
106 m_noEventsMode
= noEvents
;
109 // function appends a new item or submenu to the menu
110 // append a new item or submenu to the menu
111 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
113 wxASSERT_MSG( pItem
!= NULL
, wxT("can't append NULL item to the menu") );
114 GetPeer()->InsertOrAppend( pItem
, pos
);
116 if ( pItem
->IsSeparator() )
118 // nothing to do here
122 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
123 if ( pSubMenu
!= NULL
)
125 wxASSERT_MSG( pSubMenu
->GetHMenu() != NULL
, wxT("invalid submenu added"));
126 pSubMenu
->m_menuParent
= this ;
128 pSubMenu
->DoRearrange();
132 if ( pItem
->GetId() == idMenuTitle
)
133 pItem
->GetMenu()->Enable( idMenuTitle
, false );
137 // if we're already attached to the menubar, we must update it
138 if ( IsAttached() && GetMenuBar()->IsAttached() )
139 GetMenuBar()->Refresh();
144 void wxMenu::EndRadioGroup()
146 // we're not inside a radio group any longer
147 m_startRadioGroup
= -1;
150 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
152 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
156 if ( item
->GetKind() == wxITEM_RADIO
)
158 int count
= GetMenuItemCount();
160 if ( m_startRadioGroup
== -1 )
162 // start a new radio group
163 m_startRadioGroup
= count
;
165 // for now it has just one element
166 item
->SetAsRadioGroupStart();
167 item
->SetRadioGroupEnd(m_startRadioGroup
);
169 // ensure that we have a checked item in the radio group
172 else // extend the current radio group
174 // we need to update its end item
175 item
->SetRadioGroupStart(m_startRadioGroup
);
176 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
180 node
->GetData()->SetRadioGroupEnd(count
);
184 wxFAIL_MSG( wxT("where is the radio group start item?") );
188 else // not a radio item
193 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
197 // check the item initially
203 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
205 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
211 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
213 if ( m_startRadioGroup
!= -1 )
215 // Check if we're removing the item starting the radio group
216 if ( GetMenuItems().Item(m_startRadioGroup
)->GetData() == item
)
218 // Yes, we do, so reset its index as the next item added shouldn't
219 // count as part of the same radio group anyhow.
220 m_startRadioGroup
= -1;
225 // we need to find the items position in the child list
227 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
229 for ( pos = 0; node; pos++ )
231 if ( node->GetData() == item )
234 node = node->GetNext();
237 // DoRemove() (unlike Remove) can only be called for existing item!
238 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
240 wxOSXMenuRemoveItem(m_hMenu , pos );
242 GetPeer()->Remove( item
);
243 // and from internal data structures
244 return wxMenuBase::DoRemove(item
);
247 void wxMenu::SetTitle(const wxString
& label
)
250 GetPeer()->SetTitle( wxStripMenuCodes( label
) );
253 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
255 bool processed
= false;
257 // Try the menu's event handler
258 if ( /* !processed && */ GetEventHandler())
259 processed
= GetEventHandler()->SafelyProcessEvent(event
);
261 // Try the window the menu was popped up from
262 // (and up through the hierarchy)
263 wxWindow
*win
= GetWindow();
264 if ( !processed
&& win
)
265 processed
= win
->HandleWindowEvent(event
);
270 // ---------------------------------------------------------------------------
272 // ---------------------------------------------------------------------------
274 // MacOS needs to know about submenus somewhere within this menu
275 // before it can be displayed, also hide special menu items
276 // like preferences that are handled by the OS
277 void wxMenu::DoRearrange()
279 if ( !AllowRearrange() )
282 wxMenuItem
* previousItem
= NULL
;
284 wxMenuItemList::compatibility_iterator node
;
287 for (pos
= 0, node
= GetMenuItems().GetFirst(); node
; node
= node
->GetNext(), pos
++)
289 item
= (wxMenuItem
*)node
->GetData();
290 wxMenu
* subMenu
= item
->GetSubMenu() ;
297 // what we do here is to hide the special items which are
298 // shown in the application menu anyhow -- it doesn't make
299 // sense to show them in their normal place as well
300 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
||
301 item
->GetId() == wxApp::s_macPreferencesMenuItemId
||
302 item
->GetId() == wxApp::s_macExitMenuItemId
)
305 item
->GetPeer()->Hide( true );
307 // also check for a separator which was used just to
308 // separate this item from the others, so don't leave
309 // separator at the menu start or end nor 2 consecutive
311 wxMenuItemList::compatibility_iterator nextNode
= node
->GetNext();
312 wxMenuItem
*next
= nextNode
? nextNode
->GetData() : NULL
;
314 wxMenuItem
*sepToHide
= 0;
315 if ( !previousItem
&& next
&& next
->IsSeparator() )
317 // next (i.e. second as we must be first) item is
318 // the separator to hide
319 wxASSERT_MSG( pos
== 0, wxT("should be the menu start") );
322 else if ( GetMenuItems().GetCount() == pos
+ 1 &&
323 previousItem
!= NULL
&&
324 previousItem
->IsSeparator() )
326 // prev item is a trailing separator we want to hide
327 sepToHide
= previousItem
;
329 else if ( previousItem
&& previousItem
->IsSeparator() &&
330 next
&& next
->IsSeparator() )
332 // two consecutive separators, this is one too many
338 // hide the separator as well
339 sepToHide
->GetPeer()->Hide( true );
344 previousItem
= item
;
349 bool wxMenu::HandleCommandUpdateStatus( wxMenuItem
* item
, wxWindow
* senderWindow
)
351 int menuid
= item
? item
->GetId() : 0;
352 wxUpdateUIEvent
event(menuid
);
353 event
.SetEventObject( this );
355 bool processed
= false;
357 // Try the menu's event handler
359 wxEvtHandler
*handler
= GetEventHandler();
361 processed
= handler
->ProcessEvent(event
);
364 // Try the window the menu was popped up from
365 // (and up through the hierarchy)
368 wxWindow
*win
= GetWindow();
370 processed
= win
->HandleWindowEvent(event
);
373 if ( !processed
&& senderWindow
!= NULL
)
375 processed
= senderWindow
->HandleWindowEvent(event
);
380 // if anything changed, update the changed attribute
381 if (event
.GetSetText())
382 SetLabel(menuid
, event
.GetText());
383 if (event
.GetSetChecked())
384 Check(menuid
, event
.GetChecked());
385 if (event
.GetSetEnabled())
386 Enable(menuid
, event
.GetEnabled());
391 // these two items are also managed by the Carbon Menu Manager, therefore we must
392 // always reset them ourselves
395 if ( menuid
== wxApp::s_macExitMenuItemId
)
397 cmd
= kHICommandQuit
;
399 else if (menuid
== wxApp::s_macPreferencesMenuItemId
)
401 cmd
= kHICommandPreferences
;
406 if ( !item
->IsEnabled() || wxDialog::OSXHasModalDialogsOpen() )
407 DisableMenuCommand( NULL
, cmd
) ;
409 EnableMenuCommand( NULL
, cmd
) ;
418 bool wxMenu::HandleCommandProcess( wxMenuItem
* item
, wxWindow
* senderWindow
)
420 int menuid
= item
? item
->GetId() : 0;
421 bool processed
= false;
422 if (item
->IsCheckable())
423 item
->Check( !item
->IsChecked() ) ;
425 if ( SendEvent( menuid
, item
->IsCheckable() ? item
->IsChecked() : -1 ) )
429 if ( senderWindow
!= NULL
)
431 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, menuid
);
432 event
.SetEventObject(senderWindow
);
433 event
.SetInt(item
->IsCheckable() ? item
->IsChecked() : -1);
435 if ( senderWindow
->HandleWindowEvent(event
) )
440 if(!processed
&& item
)
442 processed
= item
->GetPeer()->DoDefault();
448 void wxMenu::HandleMenuItemHighlighted( wxMenuItem
* item
)
450 int menuid
= item
? item
->GetId() : 0;
451 wxMenuEvent
wxevent(wxEVT_MENU_HIGHLIGHT
, menuid
, this);
452 DoHandleMenuEvent( wxevent
);
455 void wxMenu::DoHandleMenuOpenedOrClosed(wxEventType evtType
)
457 // Popup menu being currently shown or NULL, defined in wincmn.cpp.
458 extern wxMenu
*wxCurrentPopupMenu
;
460 // Set the id to allow wxMenuEvent::IsPopup() to work correctly.
461 int menuid
= this == wxCurrentPopupMenu
? wxID_ANY
: 0;
462 wxMenuEvent
wxevent(evtType
, menuid
, this);
463 DoHandleMenuEvent( wxevent
);
466 void wxMenu::HandleMenuOpened()
468 DoHandleMenuOpenedOrClosed(wxEVT_MENU_OPEN
);
471 void wxMenu::HandleMenuClosed()
473 DoHandleMenuOpenedOrClosed(wxEVT_MENU_CLOSE
);
476 bool wxMenu::DoHandleMenuEvent(wxEvent
& wxevent
)
478 wxevent
.SetEventObject(this);
479 wxEvtHandler
* handler
= GetEventHandler();
480 if (handler
&& handler
->ProcessEvent(wxevent
))
486 wxWindow
*win
= GetWindow();
489 if ( win
->HandleWindowEvent(wxevent
) )
500 Mac Implementation note :
502 The Mac has only one global menubar, so we attempt to install the currently
503 active menubar from a frame, we currently don't take into account mdi-frames
504 which would ask for menu-merging
506 Secondly there is no mac api for changing a menubar that is not the current
507 menubar, so we have to wait for preparing the actual menubar until the
508 wxMenubar is to be used
510 We can in subsequent versions use MacInstallMenuBar to provide some sort of
511 auto-merge for MDI in case this will be necessary
515 wxMenuBar
* wxMenuBar::s_macInstalledMenuBar
= NULL
;
516 wxMenuBar
* wxMenuBar::s_macCommonMenuBar
= NULL
;
517 bool wxMenuBar::s_macAutoWindowMenu
= true ;
518 WXHMENU
wxMenuBar::s_macWindowMenuHandle
= NULL
;
520 const int firstMenuPos
= 1; // to account for the 0th application menu on mac
522 void wxMenuBar::Init()
524 m_eventHandler
= this;
525 m_menuBarFrame
= NULL
;
526 m_rootMenu
= new wxMenu();
527 m_rootMenu
->Attach(this);
529 m_appleMenu
= new wxMenu();
530 m_appleMenu
->SetAllowRearrange(false);
532 // Create standard items unless the application explicitly disabled this by
533 // setting the corresponding ids to wxID_NONE: although this is not
534 // recommended, sometimes these items really don't make sense.
535 if ( wxApp::s_macAboutMenuItemId
!= wxID_NONE
)
537 wxString
aboutLabel(_("About"));
539 aboutLabel
<< ' ' << wxTheApp
->GetAppDisplayName();
542 m_appleMenu
->Append( wxApp::s_macAboutMenuItemId
, aboutLabel
);
543 m_appleMenu
->AppendSeparator();
546 #if !wxOSX_USE_CARBON
547 if ( wxApp::s_macPreferencesMenuItemId
!= wxID_NONE
)
549 m_appleMenu
->Append( wxApp::s_macPreferencesMenuItemId
,
550 _("Preferences...") + "\tCtrl+," );
551 m_appleMenu
->AppendSeparator();
554 // standard menu items, handled in wxMenu::HandleCommandProcess(), see above:
555 wxString
hideLabel(_("Hide"));
557 hideLabel
<< ' ' << wxTheApp
->GetAppDisplayName();
558 hideLabel
<< "\tCtrl+H";
559 m_appleMenu
->Append( wxID_OSX_HIDE
, hideLabel
);
560 m_appleMenu
->Append( wxID_OSX_HIDEOTHERS
, _("Hide Others")+"\tAlt+Ctrl+H" );
561 m_appleMenu
->Append( wxID_OSX_SHOWALL
, _("Show All") );
562 m_appleMenu
->AppendSeparator();
564 // Do always add "Quit" item unconditionally however, it can't be disabled.
565 wxString
quitLabel(_("Quit"));
567 quitLabel
<< ' ' << wxTheApp
->GetAppDisplayName();
568 quitLabel
<< "\tCtrl+Q";
569 m_appleMenu
->Append( wxApp::s_macExitMenuItemId
, quitLabel
);
570 #endif // !wxOSX_USE_CARBON
572 m_rootMenu
->AppendSubMenu(m_appleMenu
, "\x14") ;
575 wxMenuBar::wxMenuBar()
580 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
585 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
589 for ( size_t i
= 0; i
< count
; i
++ )
591 m_menus
.Append(menus
[i
]);
593 menus
[i
]->Attach(this);
594 Append( menus
[i
], titles
[i
] );
598 wxMenuBar::~wxMenuBar()
600 if (s_macCommonMenuBar
== this)
601 s_macCommonMenuBar
= NULL
;
603 if (s_macInstalledMenuBar
== this)
605 s_macInstalledMenuBar
= NULL
;
609 void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground
), const wxRect
*WXUNUSED(rect
))
611 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
614 void wxMenuBar::MacInstallMenuBar()
616 if ( s_macInstalledMenuBar
== this )
619 m_rootMenu
->GetPeer()->MakeRoot();
621 // hide items in the apple menu that don't exist in the wx menubar
624 wxMenuItem
* appleItem
= NULL
;
625 wxMenuItem
* wxItem
= NULL
;
627 menuid
= wxApp::s_macAboutMenuItemId
;
628 appleItem
= m_appleMenu
->FindItem(menuid
);
629 wxItem
= FindItem(menuid
);
630 if ( appleItem
!= NULL
)
632 if ( wxItem
== NULL
)
633 appleItem
->GetPeer()->Hide();
635 appleItem
->SetItemLabel(wxItem
->GetItemLabel());
638 menuid
= wxApp::s_macPreferencesMenuItemId
;
639 appleItem
= m_appleMenu
->FindItem(menuid
);
640 wxItem
= FindItem(menuid
);
641 if ( appleItem
!= NULL
)
643 if ( wxItem
== NULL
)
644 appleItem
->GetPeer()->Hide();
646 appleItem
->SetItemLabel(wxItem
->GetItemLabel());
652 // if we have a mac help menu, clean it up before adding new items
653 MenuHandle helpMenuHandle
;
654 MenuItemIndex firstUserHelpMenuItem
;
656 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle
, &firstUserHelpMenuItem
) == noErr
)
658 for ( int i
= CountMenuItems( helpMenuHandle
) ; i
>= firstUserHelpMenuItem
; --i
)
659 DeleteMenuItem( helpMenuHandle
, i
) ;
663 helpMenuHandle
= NULL
;
666 if ( wxApp::s_macPreferencesMenuItemId
)
668 wxMenuItem
*item
= FindItem( wxApp::s_macPreferencesMenuItemId
, NULL
) ;
669 if ( item
== NULL
|| !(item
->IsEnabled()) )
670 DisableMenuCommand( NULL
, kHICommandPreferences
) ;
672 EnableMenuCommand( NULL
, kHICommandPreferences
) ;
675 // Unlike preferences which may or may not exist, the Quit item should be always
676 // enabled unless it is added by the application and then disabled, otherwise
677 // a program would be required to add an item with wxID_EXIT in order to get the
678 // Quit menu item to be enabled, which seems a bit burdensome.
679 if ( wxApp::s_macExitMenuItemId
)
681 wxMenuItem
*item
= FindItem( wxApp::s_macExitMenuItemId
, NULL
) ;
682 if ( item
!= NULL
&& !(item
->IsEnabled()) )
683 DisableMenuCommand( NULL
, kHICommandQuit
) ;
685 EnableMenuCommand( NULL
, kHICommandQuit
) ;
688 wxString strippedHelpMenuTitle
= wxStripMenuCodes( wxApp::s_macHelpMenuTitleName
) ;
689 wxString strippedTranslatedHelpMenuTitle
= wxStripMenuCodes( wxString( _("&Help") ) ) ;
690 wxMenuList::compatibility_iterator menuIter
= m_menus
.GetFirst();
691 for (size_t i
= 0; i
< m_menus
.GetCount(); i
++, menuIter
= menuIter
->GetNext())
693 wxMenuItemList::compatibility_iterator node
;
695 wxMenu
* menu
= menuIter
->GetData() , *subMenu
= NULL
;
696 wxString strippedMenuTitle
= wxStripMenuCodes(m_titles
[i
]);
698 if ( strippedMenuTitle
== wxT("?") || strippedMenuTitle
== strippedHelpMenuTitle
|| strippedMenuTitle
== strippedTranslatedHelpMenuTitle
)
700 for (node
= menu
->GetMenuItems().GetFirst(); node
; node
= node
->GetNext())
702 item
= (wxMenuItem
*)node
->GetData();
703 subMenu
= item
->GetSubMenu() ;
706 UMAAppendMenuItem(mh
, wxStripMenuCodes(item
->GetText()) , wxFont::GetDefaultEncoding() );
707 MenuItemIndex position
= CountMenuItems(mh
);
708 ::SetMenuItemHierarchicalMenu(mh
, position
, MAC_WXHMENU(subMenu
->GetHMenu()));
712 if ( item
->GetId() != wxApp::s_macAboutMenuItemId
)
714 // we have found a user help menu and an item other than the about item,
715 // so we can create the mac help menu now, if we haven't created it yet
716 if ( helpMenuHandle
== NULL
)
718 if ( UMAGetHelpMenu( &helpMenuHandle
, &firstUserHelpMenuItem
) != noErr
)
720 helpMenuHandle
= NULL
;
726 if ( item
->IsSeparator() )
728 if ( helpMenuHandle
)
729 AppendMenuItemTextWithCFString( helpMenuHandle
,
730 CFSTR(""), kMenuItemAttrSeparator
, 0,NULL
);
735 entry
= wxAcceleratorEntry::Create( item
->GetItemLabel() ) ;
737 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
)
739 // this will be taken care of below
743 if ( helpMenuHandle
)
745 UMAAppendMenuItem(helpMenuHandle
, wxStripMenuCodes(item
->GetItemLabel()) , wxFont::GetDefaultEncoding(), entry
);
746 SetMenuItemCommandID( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , wxIdToMacCommand ( item
->GetId() ) ) ;
747 SetMenuItemRefCon( helpMenuHandle
, CountMenuItems(helpMenuHandle
) , (URefCon
) item
) ;
757 else if ( ( m_titles
[i
] == wxT("Window") || m_titles
[i
] == wxT("&Window") )
758 && GetAutoWindowMenu() )
760 if ( MacGetWindowMenuHMenu() == NULL
)
762 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
765 MenuRef wm
= (MenuRef
)MacGetWindowMenuHMenu();
769 // get the insertion point in the standard menu
770 MenuItemIndex winListStart
;
771 GetIndMenuItemWithCommandID(wm
,
772 kHICommandWindowListSeparator
, 1, NULL
, &winListStart
);
774 // add a separator so that the standard items and the custom items
775 // aren't mixed together, but only if this is the first run
776 OSStatus err
= GetIndMenuItemWithCommandID(wm
,
777 'WXWM', 1, NULL
, NULL
);
779 if ( err
== menuItemNotFoundErr
)
781 InsertMenuItemTextWithCFString( wm
,
782 CFSTR(""), winListStart
-1, kMenuItemAttrSeparator
, 'WXWM');
785 wxInsertMenuItemsInMenu(menu
, wm
, winListStart
);
789 UMASetMenuTitle( MAC_WXHMENU(menu
->GetHMenu()) , m_titles
[i
], GetFont().GetEncoding() ) ;
790 menu
->MacBeforeDisplay(false) ;
792 ::InsertMenu(MAC_WXHMENU(GetMenu(i
)->GetHMenu()), 0);
796 // take care of the about menu item wherever it is
799 wxMenuItem
*aboutMenuItem
= FindItem(wxApp::s_macAboutMenuItemId
, &aboutMenu
) ;
803 entry
= wxAcceleratorEntry::Create( aboutMenuItem
->GetItemLabel() ) ;
804 UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId
) , 1 , wxStripMenuCodes ( aboutMenuItem
->GetItemLabel() ) , wxFont::GetDefaultEncoding() );
805 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId
) , 1 , true );
806 SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId
) , 1 , kHICommandAbout
) ;
807 SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId
) , 1 , (URefCon
)aboutMenuItem
) ;
808 UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId
) , 1 , entry
) ;
813 if ( GetAutoWindowMenu() )
815 if ( MacGetWindowMenuHMenu() == NULL
)
816 CreateStandardWindowMenu( 0 , (MenuHandle
*) &s_macWindowMenuHandle
) ;
818 InsertMenu( (MenuHandle
) MacGetWindowMenuHMenu() , 0 ) ;
824 s_macInstalledMenuBar
= this;
827 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
829 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
831 m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
)->Enable(enable
);
836 bool wxMenuBar::IsEnabledTop(size_t pos
) const
838 wxCHECK_MSG( IsAttached(), true,
839 wxT("doesn't work with unattached menubars") );
841 wxMenuItem
* const item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
842 wxCHECK_MSG( item
, false, wxT("invalid menu index") );
844 return item
->IsEnabled();
847 bool wxMenuBar::Enable(bool enable
)
849 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
852 for (i
= 0; i
< GetMenuCount(); i
++)
853 EnableTop(i
, enable
);
858 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
860 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
862 GetMenu(pos
)->SetTitle( label
) ;
865 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
867 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
868 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
870 return GetMenu(pos
)->GetTitle();
873 // ---------------------------------------------------------------------------
874 // wxMenuBar construction
875 // ---------------------------------------------------------------------------
877 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
879 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
883 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
884 m_rootMenu
->Remove(item
);
885 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
890 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
892 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
895 m_rootMenu
->Insert( pos
+firstMenuPos
, wxMenuItem::New( m_rootMenu
, wxID_ANY
, title
, "", wxITEM_NORMAL
, menu
) );
900 wxMenu
*wxMenuBar::Remove(size_t pos
)
902 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
906 wxMenuItem
* item
= m_rootMenu
->FindItemByPosition(pos
+firstMenuPos
);
907 m_rootMenu
->Remove(item
);
912 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
914 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
915 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
917 if ( !wxMenuBarBase::Append(menu
, title
) )
920 m_rootMenu
->AppendSubMenu(menu
, title
);
921 menu
->SetTitle(title
);
926 void wxMenuBar::Detach()
928 wxMenuBarBase::Detach() ;
931 void wxMenuBar::Attach(wxFrame
*frame
)
933 wxMenuBarBase::Attach( frame
) ;
936 #endif // wxUSE_MENUS