1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
14 // headers & declarations
15 // ============================================================================
21 #pragma implementation "menu.h"
22 #pragma implementation "menuitem.h"
26 #include "wx/menuitem.h"
30 #include "wx/mac/uma.h"
32 // other standard headers
33 // ----------------------
36 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
37 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
39 // the (popup) menu title has this special id
40 static const int idMenuTitle
= -2;
41 static int formerHelpMenuItems
= 0 ;
43 const short kwxMacMenuBarResource
= 1 ;
44 const short kwxMacAppleMenuId
= 1 ;
46 // ============================================================================
48 // ============================================================================
51 // Helper Functions to get Mac Menus the way they should be ;-)
54 void wxMacCtoPString(const char* theCString
, Str255 thePString
);
56 // remove inappropriate characters, if useShortcuts is false, the ampersand will not auto-generate a mac menu-shortcut
58 void wxMacBuildMenuString(StringPtr outMacItemText
, char *outMacShortcutChar
, short *outMacModifiers
, const char *inItemText
, bool useShortcuts
)
60 char *p
= (char *) &outMacItemText
[1] ;
61 short macModifiers
= 0 ;
62 char macShortCut
= 0 ;
63 const char *inItemName
;
64 wxString inItemTextMac
;
66 if (wxApp::s_macDefaultEncodingIsPC
)
68 inItemTextMac
= wxMacMakeMacStringFromPC( inItemText
) ;
69 inItemName
= inItemTextMac
;
73 inItemName
= inItemText
;
76 if ( useShortcuts
&& !wxApp::s_macSupportPCMenuShortcuts
)
77 useShortcuts
= false ;
79 // we have problems with a leading hypen - it will be taken as a separator
81 while ( *inItemName
== '-' )
86 switch ( *inItemName
)
88 // special characters for macintosh menus -> use some replacement
118 macShortCut
= *inItemName
;
124 // win-like accelerators
130 if (strncmp("Ctrl", inItemName
, 4) == 0)
132 inItemName
= inItemName
+ 5;
133 macShortCut
= *inItemName
;
135 else if (strncmp("Cntrl", inItemName
, 5) == 0)
137 inItemName
= inItemName
+ 6;
138 macShortCut
= *inItemName
;
140 else if (strncmp("Alt", inItemName
, 3) == 0)
142 inItemName
= inItemName
+ 4;
143 macModifiers
|= kMenuOptionModifier
;
144 macShortCut
= *inItemName
;
146 else if (strncmp("Shift", inItemName
, 5) == 0)
148 inItemName
= inItemName
+ 6;
149 macModifiers
|= kMenuShiftModifier
;
150 macShortCut
= *inItemName
;
152 else if (strncmp("F", inItemName
, 1) == 0)
154 inItemName
+= strlen( inItemName
) ;
155 // no function keys at the moment
156 // macModifiers |= kMenuShiftModifier ;
157 // macShortCut = *inItemName ;
165 if ( *inItemName
== 0 )
176 outMacItemText
[0] = (p
- (char *)outMacItemText
) - 1;
177 if ( outMacShortcutChar
)
178 *outMacShortcutChar
= macShortCut
;
179 if ( outMacModifiers
)
180 *outMacModifiers
= macModifiers
;
183 int pos
= outMacItemText
[0] ;
184 outMacItemText
[++pos
] = '/';
185 outMacItemText
[++pos
] = toupper( macShortCut
);
186 outMacItemText
[0] = pos
;
192 // Construct a menu with optional title (then use append)
194 short wxMenu::s_macNextMenuId
= 2 ;
202 wxMacBuildMenuString( label
, NULL
, NULL
, m_title
, false );
203 m_macMenuId
= s_macNextMenuId
++;
204 wxCHECK_RET( s_macNextMenuId
< 236 , "menu ids > 235 cannot be used for submenus on mac" );
205 m_hMenu
= ::NewMenu(m_macMenuId
, label
);
209 wxLogLastError("CreatePopupMenu");
212 // if we have a title, insert it in the beginning of the menu
215 Append(idMenuTitle
, m_title
) ;
223 ::DisposeMenu(m_hMenu
);
227 WX_CLEAR_ARRAY(m_accels
);
228 #endif // wxUSE_ACCEL
233 // not available on the mac platform
238 int wxMenu::FindAccel(int id
) const
240 size_t n
, count
= m_accels
.GetCount();
241 for ( n
= 0; n
< count
; n
++ )
243 if ( m_accels
[n
]->m_command
== id
)
250 void wxMenu::UpdateAccel(wxMenuItem
*item
)
252 // find the (new) accel for this item
253 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
255 accel
->m_command
= item
->GetId();
258 int n
= FindAccel(item
->GetId());
259 if ( n
== wxNOT_FOUND
)
261 // no old, add new if any
265 return; // skipping RebuildAccelTable() below
269 // replace old with new or just remove the old one if no new
279 m_menuBar
->RebuildAccelTable();
283 #endif // wxUSE_ACCEL
285 // function appends a new item or submenu to the menu
286 // append a new item or submenu to the menu
287 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
289 wxASSERT_MSG( pItem
!= NULL
, "can't append NULL item to the menu" );
292 #endif // wxUSE_ACCEL
294 if ( pItem
->IsSeparator() )
296 if ( pos
== (size_t)-1 )
298 MacAppendMenu(m_hMenu
, "\p-");
302 MacInsertMenuItem(m_hMenu
, "\p-" , pos
);
307 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
308 if ( pSubMenu
!= NULL
)
311 wxASSERT_MSG( pSubMenu
->m_hMenu
!= NULL
, "invalid submenu added");
312 pSubMenu
->m_menuParent
= this ;
313 wxMacBuildMenuString( label
, NULL
, NULL
, pItem
->GetText() ,false);
315 // hardcoded adding of the submenu combination for mac
317 int theEnd
= label
[0] + 1;
319 theEnd
= 251; // mac allows only 255 characters
320 label
[theEnd
++] = '/';
321 label
[theEnd
++] = hMenuCmd
;
322 label
[theEnd
++] = '!';
323 label
[theEnd
++] = pSubMenu
->m_macMenuId
;
324 label
[theEnd
] = 0x00;
327 if (wxMenuBar::MacGetInstalledMenuBar() == m_menuBar
)
329 ::InsertMenu( pSubMenu
->m_hMenu
, -1 ) ;
332 if ( pos
== (size_t)-1 )
334 MacAppendMenu(m_hMenu
, label
);
338 MacInsertMenuItem(m_hMenu
, label
, pos
);
344 wxMacBuildMenuString( label
, NULL
, NULL
, pItem
->GetText(), pItem
->GetId() == wxApp::s_macAboutMenuItemId
);
347 // we cannot add empty menus on mac
351 if ( pos
== (size_t)-1 )
353 MacAppendMenu(m_hMenu
, label
);
357 MacInsertMenuItem(m_hMenu
, label
, pos
);
359 if ( pItem
->GetId() == idMenuTitle
)
361 if ( pos
== (size_t)-1 )
363 UMADisableMenuItem( m_hMenu
, CountMItems( m_hMenu
) ) ;
367 UMADisableMenuItem( m_hMenu
, pos
+ 1 ) ;
372 // if we're already attached to the menubar, we must update it
375 m_menuBar
->Refresh();
380 bool wxMenu::DoAppend(wxMenuItem
*item
)
382 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
385 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
387 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
390 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
392 // we need to find the items position in the child list
394 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
395 for ( pos
= 0; node
; pos
++ )
397 if ( node
->GetData() == item
)
400 node
= node
->GetNext();
403 // DoRemove() (unlike Remove) can only be called for existing item!
404 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
407 // remove the corresponding accel from the accel table
408 int n
= FindAccel(item
->GetId());
409 if ( n
!= wxNOT_FOUND
)
415 //else: this item doesn't have an accel, nothing to do
416 #endif // wxUSE_ACCEL
418 ::DeleteMenuItem( m_hMenu
, pos
+ 1);
422 // otherwise, the chane won't be visible
423 m_menuBar
->Refresh();
426 // and from internal data structures
427 return wxMenuBase::DoRemove(item
);
430 // ---------------------------------------------------------------------------
431 // accelerator helpers
432 // ---------------------------------------------------------------------------
436 // create the wxAcceleratorEntries for our accels and put them into provided
437 // array - return the number of accels we have
438 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
440 size_t count
= GetAccelCount();
441 for ( size_t n
= 0; n
< count
; n
++ )
443 *accels
++ = *m_accels
[n
];
449 #endif // wxUSE_ACCEL
451 void wxMenu::SetTitle(const wxString
& label
)
455 wxMacBuildMenuString( title
, NULL
, NULL
, label
, false );
456 UMASetMenuTitle( m_hMenu
, title
) ;
461 void wxMenu::SetLabel(int id, const wxString& label)
465 wxMenuItem *item = FindItemForId(id) ;
469 index = MacGetIndexFromItem( item ) ;
473 if (item->GetSubMenu()==NULL)
475 wxMacBuildMenuString( maclabel , NULL , NULL , label , false );
476 ::SetMenuItemText( m_hMenu , index , maclabel ) ;
480 wxMacBuildMenuString( maclabel , NULL , NULL , label , false );
481 ::SetMenuItemText( m_hMenu , index , maclabel ) ;
483 item->SetName(label);
486 wxString wxMenu::GetLabel(int Id) const
488 wxMenuItem *pItem = FindItemForId(Id) ;
489 return pItem->GetName() ;
492 // Finds the item id matching the given string, -1 if not found.
493 int wxMenu::FindItem (const wxString& itemString) const
497 wxStripMenuCodes ((char *)(const char *)itemString, buf1);
499 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
501 wxMenuItem *item = (wxMenuItem *) node->Data ();
502 if (item->GetSubMenu())
504 int ans = item->GetSubMenu()->FindItem(itemString);
508 if ( !item->IsSeparator() )
510 wxStripMenuCodes((char *)item->GetName().c_str(), buf2);
511 if (strcmp(buf1, buf2) == 0)
512 return item->GetId();
519 wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
523 for (wxNode * node = m_menuItems.First (); node; node = node->Next ())
525 wxMenuItem *item = (wxMenuItem *) node->Data ();
527 if (item->GetId() == itemId)
530 *itemMenu = (wxMenu *) this;
534 if (item->GetSubMenu())
536 wxMenuItem *ans = item->GetSubMenu()->FindItemForId (itemId, itemMenu);
547 void wxMenu::SetHelpString(int itemId, const wxString& helpString)
549 wxMenuItem *item = FindItemForId (itemId);
551 item->SetHelp(helpString);
554 wxString wxMenu::GetHelpString (int itemId) const
556 wxMenuItem *item = FindItemForId (itemId);
558 return (item == NULL) ? str : item->GetHelp();
562 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
564 bool processed
= FALSE
;
566 #if WXWIN_COMPATIBILITY
570 (void)(*(m_callback
))(*this, event
);
573 #endif WXWIN_COMPATIBILITY
575 // Try the menu's event handler
576 if ( !processed
&& GetEventHandler())
578 processed
= GetEventHandler()->ProcessEvent(event
);
581 // Try the window the menu was popped up from (and up through the
583 wxWindow
*win
= GetInvokingWindow();
584 if ( !processed
&& win
)
585 processed
= win
->GetEventHandler()->ProcessEvent(event
);
591 // ---------------------------------------------------------------------------
593 // ---------------------------------------------------------------------------
595 void wxMenu::Attach(wxMenuBar
*menubar
)
597 // menu can be in at most one menubar because otherwise they would both
598 // delete the menu pointer
599 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
604 void wxMenu::Detach()
606 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
611 wxWindow
*wxMenu::GetWindow() const
613 if ( m_invokingWindow
!= NULL
)
614 return m_invokingWindow
;
615 else if ( m_menuBar
!= NULL
)
616 return m_menuBar
->GetFrame();
621 // helper functions returning the mac menu position for a certain item, note that this is
622 // mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0
624 int wxMenu::MacGetIndexFromId( int id
)
627 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
628 for ( pos
= 0; node
; pos
++ )
630 if ( node
->GetData()->GetId() == id
)
633 node
= node
->GetNext();
642 int wxMenu::MacGetIndexFromItem( wxMenuItem
*pItem
)
645 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
646 for ( pos
= 0; node
; pos
++ )
648 if ( node
->GetData() == pItem
)
651 node
= node
->GetNext();
660 void wxMenu::MacEnableMenu( bool bDoEnable
)
663 UMAEnableMenuItem( m_hMenu
, 0 ) ;
665 UMADisableMenuItem( m_hMenu
, 0 ) ;
670 bool wxMenu::MacMenuSelect( wxEvtHandler
* handler
, long when
, int macMenuId
, int macMenuItemNum
)
675 if ( m_macMenuId
== macMenuId
)
677 node
= GetMenuItems().Nth(macMenuItemNum
-1);
680 wxMenuItem
*pItem
= (wxMenuItem
*)node
->Data();
682 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, pItem
->GetId());
683 event
.m_timeStamp
= when
;
684 event
.SetEventObject(handler
);
685 event
.SetInt( pItem
->GetId() );
687 bool processed
= false ;
689 #if WXWIN_COMPATIBILITY
693 (void) (*(m_callback
)) (*this, event
);
697 // Try the menu's event handler
698 if ( !processed
&& handler
)
700 processed
= handler
->ProcessEvent(event
);
703 // Try the window the menu was popped up from (and up
704 // through the hierarchy)
705 if ( !processed
&& GetInvokingWindow())
706 processed
= GetInvokingWindow()->GetEventHandler()->ProcessEvent(event
);
711 else if ( macMenuId
== kHMHelpMenuID
)
713 int menuItem
= formerHelpMenuItems
;
714 for (pos
= 0, node
= GetMenuItems().First(); node
; node
= node
->Next(), pos
++)
716 wxMenuItem
* pItem
= (wxMenuItem
*) node
->Data() ;
718 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
719 if ( pSubMenu
!= NULL
)
724 if ( pItem
->GetId() != wxApp::s_macAboutMenuItemId
)
727 if ( menuItem
== macMenuItemNum
)
729 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, pItem
->GetId());
730 event
.m_timeStamp
= when
;
731 event
.SetEventObject(handler
);
732 event
.SetInt( pItem
->GetId() );
734 bool processed
= false ;
735 #if WXWIN_COMPATIBILITY
739 (void) (*(m_callback
)) (*this, event
);
743 // Try the menu's event handler
744 if ( !processed
&& handler
)
746 processed
= handler
->ProcessEvent(event
);
749 // Try the window the menu was popped up from (and up
750 // through the hierarchy)
751 if ( !processed
&& GetInvokingWindow())
752 processed
= GetInvokingWindow()->GetEventHandler()->ProcessEvent(event
);
760 for (pos
= 0, node
= GetMenuItems().First(); node
; node
= node
->Next(), pos
++)
762 wxMenuItem
* pItem
= (wxMenuItem
*) node
->Data() ;
764 wxMenu
*pSubMenu
= pItem
->GetSubMenu() ;
765 if ( pSubMenu
!= NULL
)
767 if ( pSubMenu
->MacMenuSelect( handler
, when
, macMenuId
, macMenuItemNum
) )
779 Mac Implementation note :
781 The Mac has only one global menubar, so we attempt to install the currently
782 active menubar from a frame, we currently don't take into account mdi-frames
783 which would ask for menu-merging
785 Secondly there is no mac api for changing a menubar that is not the current
786 menubar, so we have to wait for preparing the actual menubar until the
787 wxMenubar is to be used
789 We can in subsequent versions use MacInstallMenuBar to provide some sort of
790 auto-merge for MDI in case this will be necessary
794 wxMenuBar
* wxMenuBar::s_macInstalledMenuBar
= NULL
;
796 void wxMenuBar::Init()
798 m_eventHandler
= this;
799 m_menuBarFrame
= NULL
;
802 wxMenuBar::wxMenuBar()
807 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
813 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
817 m_titles
.Alloc(count
);
819 for ( int i
= 0; i
< count
; i
++ )
821 m_menus
.Append(menus
[i
]);
822 m_titles
.Add(titles
[i
]);
824 menus
[i
]->Attach(this);
828 wxMenuBar::~wxMenuBar()
830 if (s_macInstalledMenuBar
== this)
833 s_macInstalledMenuBar
= NULL
;
838 void wxMenuBar::Refresh()
840 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
847 void wxMenuBar::RebuildAccelTable()
849 // merge the accelerators of all menus into one accel table
850 size_t nAccelCount
= 0;
851 size_t i
, count
= GetMenuCount();
852 for ( i
= 0; i
< count
; i
++ )
854 nAccelCount
+= m_menus
[i
]->GetAccelCount();
859 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
862 for ( i
= 0; i
< count
; i
++ )
864 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
867 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
869 delete [] accelEntries
;
873 #endif // wxUSE_ACCEL
876 void wxMenuBar::MacInstallMenuBar()
878 Handle menubar
= ::GetNewMBar( kwxMacMenuBarResource
) ;
880 wxCHECK_RET( menubar
!= NULL
, "can't read MBAR resource" );
881 ::SetMenuBar( menubar
) ;
882 ::DisposeHandle( menubar
) ;
884 MenuHandle menu
= ::GetMenuHandle( kwxMacAppleMenuId
) ;
885 ::AppendResMenu(menu
, 'DRVR');
887 for (int i
= 0; i
< m_menus
.GetCount(); i
++)
893 wxMenu
* menu
= m_menus
[i
] , *subMenu
= NULL
;
896 if( m_titles
[i
] == "?" || m_titles
[i
] == "&?" || m_titles
[i
] == wxApp::s_macHelpMenuTitleName
)
898 MenuHandle mh
= NULL
;
899 if ( HMGetHelpMenuHandle( &mh
) != noErr
)
903 if ( formerHelpMenuItems
== 0 )
906 formerHelpMenuItems
= CountMenuItems( mh
) ;
909 for (pos
= 0 , node
= menu
->GetMenuItems().First(); node
; node
= node
->Next(), pos
++)
911 item
= (wxMenuItem
*)node
->Data();
912 subMenu
= item
->GetSubMenu() ;
915 // we don't support hierarchical menus in the help menu yet
919 if ( item
->IsSeparator() )
922 ::AppendMenu(mh
, "\p-" );
927 wxMacBuildMenuString( label
, NULL
, NULL
, item
->GetText(), item
->GetId() != wxApp::s_macAboutMenuItemId
); // no shortcut in about menu
930 // we cannot add empty menus on mac
934 if ( item
->GetId() == wxApp::s_macAboutMenuItemId
)
936 ::SetMenuItemText( GetMenuHandle( kwxMacAppleMenuId
) , 1 , label
);
937 // ::EnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 );
938 ::EnableItem( GetMenuHandle( kwxMacAppleMenuId
) , 1 );
943 ::AppendMenu(mh
, label
);
951 wxMacBuildMenuString( label
, NULL
, NULL
, m_titles
[i
] , false );
952 UMASetMenuTitle( menu
->GetHMenu() , label
) ;
953 for (pos
= 0, node
= menu
->GetMenuItems().First(); node
; node
= node
->Next(), pos
++)
955 item
= (wxMenuItem
*)node
->Data();
956 subMenu
= item
->GetSubMenu() ;
959 ::InsertMenu( subMenu
->GetHMenu() , -1 ) ;
962 ::InsertMenu(m_menus
[i
]->GetHMenu(), 0);
967 s_macInstalledMenuBar
= this;
970 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
972 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
973 m_menus
[pos
]->MacEnableMenu( enable
) ;
977 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
979 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
981 m_titles
[pos
] = label
;
988 m_menus
[pos
]->SetTitle( label
) ;
989 if (wxMenuBar::s_macInstalledMenuBar
== this) // are we currently installed ?
991 ::SetMenuBar( GetMenuBar() ) ;
996 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
998 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
999 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1001 return m_titles
[pos
];
1004 int wxMenuBar::FindMenu(const wxString
& title
)
1006 wxString menuTitle
= wxStripMenuCodes(title
);
1008 size_t count
= GetMenuCount();
1009 for ( size_t i
= 0; i
< count
; i
++ )
1011 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1012 if ( menuTitle
== title
)
1021 // ---------------------------------------------------------------------------
1022 // wxMenuBar construction
1023 // ---------------------------------------------------------------------------
1025 // ---------------------------------------------------------------------------
1026 // wxMenuBar construction
1027 // ---------------------------------------------------------------------------
1029 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1031 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1034 m_titles
[pos
] = title
;
1038 if (s_macInstalledMenuBar
== this)
1040 ::DeleteMenu( menuOld
->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
1043 wxMacBuildMenuString( label
, NULL
, NULL
, title
, false );
1044 UMASetMenuTitle( menu
->GetHMenu() , label
) ;
1045 if ( pos
== m_menus
.GetCount() - 1)
1047 ::InsertMenu( menu
->GetHMenu() , 0 ) ;
1051 ::InsertMenu( menu
->GetHMenu() , m_menus
[pos
+1]->MacGetMenuId() ) ;
1058 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1060 // need to rebuild accell table
1061 RebuildAccelTable();
1063 #endif // wxUSE_ACCEL
1071 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1073 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1076 m_titles
.Insert(title
, pos
);
1082 if ( pos
== (size_t) -1 )
1084 ::InsertMenu( menu
->GetHMenu() , 0 ) ;
1088 ::InsertMenu( menu
->GetHMenu() , m_menus
[pos
+1]->MacGetMenuId() ) ;
1092 if ( menu
->HasAccels() )
1094 // need to rebuild accell table
1095 RebuildAccelTable();
1097 #endif // wxUSE_ACCEL
1105 void wxMenuBar::MacMenuSelect(wxEvtHandler
* handler
, long when
, int macMenuId
, int macMenuItemNum
)
1107 // first scan fast for direct commands, i.e. menus which have these commands directly in their own list
1109 if ( macMenuId
== kwxMacAppleMenuId
&& macMenuItemNum
== 1 )
1111 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
, wxApp::s_macAboutMenuItemId
);
1112 event
.m_timeStamp
= when
;
1113 event
.SetEventObject(handler
);
1114 event
.SetInt( wxApp::s_macAboutMenuItemId
);
1115 handler
->ProcessEvent(event
);
1119 for (int i
= 0; i
< m_menus
.GetCount() ; i
++)
1121 if ( m_menus
[i
]->MacGetMenuId() == macMenuId
||
1122 ( macMenuId
== kHMHelpMenuID
&& ( m_titles
[i
] == "?" || m_titles
[i
] == "&?" || m_titles
[i
] == wxApp::s_macHelpMenuTitleName
) )
1125 if ( m_menus
[i
]->MacMenuSelect( handler
, when
, macMenuId
, macMenuItemNum
) )
1129 //TODO flag this as an error since it must contain the item
1135 for (int i
= 0; i
< m_menus
.GetCount(); i
++)
1137 if ( m_menus
[i
]->MacMenuSelect( handler
, when
, macMenuId
, macMenuItemNum
) )
1145 wxMenu
*wxMenuBar::Remove(size_t pos
)
1147 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1153 if (s_macInstalledMenuBar
== this)
1155 ::DeleteMenu( menu
->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
1161 if ( menu
->HasAccels() )
1163 // need to rebuild accell table
1164 RebuildAccelTable();
1166 #endif // wxUSE_ACCEL
1171 m_titles
.Remove(pos
);
1176 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1178 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1179 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
1181 if ( !wxMenuBarBase::Append(menu
, title
) )
1186 m_titles
.Add(title
);
1190 if (s_macInstalledMenuBar
== this)
1192 ::InsertMenu( menu
->GetHMenu() , 0 ) ;
1196 if ( menu
->HasAccels() )
1198 // need to rebuild accell table
1199 RebuildAccelTable();
1201 #endif // wxUSE_ACCEL
1209 // ---------------------------------------------------------------------------
1210 // wxMenuBar searching for menu items
1211 // ---------------------------------------------------------------------------
1213 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1214 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1215 const wxString
& itemString
) const
1217 wxString menuLabel
= wxStripMenuCodes(menuString
);
1218 size_t count
= GetMenuCount();
1219 for ( size_t i
= 0; i
< count
; i
++ )
1221 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1222 if ( menuString
== title
)
1223 return m_menus
[i
]->FindItem(itemString
);
1229 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
1234 wxMenuItem
*item
= NULL
;
1235 size_t count
= GetMenuCount();
1236 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
1238 item
= m_menus
[i
]->FindItem(id
, itemMenu
);