]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/menu.cpp
MacOS conforming activate/deactivate
[wxWidgets.git] / src / mac / carbon / menu.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12
13// ============================================================================
14// headers & declarations
15// ============================================================================
16
17// wxWindows headers
18// -----------------
19
20#ifdef __GNUG__
21#pragma implementation "menu.h"
22#pragma implementation "menuitem.h"
23#endif
24
25#include "wx/app.h"
26#include "wx/menu.h"
27#include "wx/menuitem.h"
28#include "wx/window.h"
29#include "wx/log.h"
30#include "wx/utils.h"
31
32#include "wx/mac/uma.h"
33
34// other standard headers
35// ----------------------
36#include <string.h>
37
38#if !USE_SHARED_LIBRARY
39IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
40IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
41#endif
42
43// the (popup) menu title has this special id
44static const int idMenuTitle = -2;
45static MenuItemIndex firstUserHelpMenuItem = 0 ;
46
47const short kwxMacMenuBarResource = 1 ;
48const short kwxMacAppleMenuId = 1 ;
49
50// ============================================================================
51// implementation
52// ============================================================================
53
54
55// Menus
56
57// Construct a menu with optional title (then use append)
58
59#ifdef __DARWIN__
60short wxMenu::s_macNextMenuId = 3 ;
61#else
62short wxMenu::s_macNextMenuId = 2 ;
63#endif
64
65void wxMenu::Init()
66{
67 m_doBreak = FALSE;
68
69 // create the menu
70 Str255 label;
71 wxMenuItem::MacBuildMenuString( label, NULL , NULL , m_title , false );
72 m_macMenuId = s_macNextMenuId++;
73 wxCHECK_RET( s_macNextMenuId < 236 , "menu ids > 235 cannot be used for submenus on mac" );
74 m_hMenu = ::NewMenu(m_macMenuId, label);
75
76 if ( !m_hMenu )
77 {
78 wxLogLastError("CreatePopupMenu");
79 }
80
81 // if we have a title, insert it in the beginning of the menu
82 if ( !!m_title )
83 {
84 Append(idMenuTitle, m_title) ;
85 AppendSeparator() ;
86 }
87}
88
89wxMenu::~wxMenu()
90{
91 if (m_hMenu)
92 ::DisposeMenu(m_hMenu);
93
94#if wxUSE_ACCEL
95 // delete accels
96 WX_CLEAR_ARRAY(m_accels);
97#endif // wxUSE_ACCEL
98}
99
100void wxMenu::Break()
101{
102 // not available on the mac platform
103}
104
105#if wxUSE_ACCEL
106
107int wxMenu::FindAccel(int id) const
108{
109 size_t n, count = m_accels.GetCount();
110 for ( n = 0; n < count; n++ )
111 {
112 if ( m_accels[n]->m_command == id )
113 return n;
114 }
115
116 return wxNOT_FOUND;
117}
118
119void wxMenu::UpdateAccel(wxMenuItem *item)
120{
121 // find the (new) accel for this item
122 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
123 if ( accel )
124 accel->m_command = item->GetId();
125
126 // find the old one
127 int n = FindAccel(item->GetId());
128 if ( n == wxNOT_FOUND )
129 {
130 // no old, add new if any
131 if ( accel )
132 m_accels.Add(accel);
133 else
134 return; // skipping RebuildAccelTable() below
135 }
136 else
137 {
138 // replace old with new or just remove the old one if no new
139 delete m_accels[n];
140 if ( accel )
141 m_accels[n] = accel;
142 else
143 m_accels.RemoveAt(n);
144 }
145
146 if ( IsAttached() )
147 {
148 m_menuBar->RebuildAccelTable();
149 }
150}
151
152#endif // wxUSE_ACCEL
153
154// function appends a new item or submenu to the menu
155// append a new item or submenu to the menu
156bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
157{
158 wxASSERT_MSG( pItem != NULL, "can't append NULL item to the menu" );
159#if wxUSE_ACCEL
160 UpdateAccel(pItem);
161#endif // wxUSE_ACCEL
162
163 if ( pItem->IsSeparator() )
164 {
165 if ( pos == (size_t)-1 )
166 {
167 MacAppendMenu(m_hMenu, "\p-");
168 }
169 else
170 {
171 MacInsertMenuItem(m_hMenu, "\p-" , pos);
172 }
173 }
174 else
175 {
176 wxMenu *pSubMenu = pItem->GetSubMenu() ;
177 if ( pSubMenu != NULL )
178 {
179 Str255 label;
180 wxASSERT_MSG( pSubMenu->m_hMenu != NULL , "invalid submenu added");
181 pSubMenu->m_menuParent = this ;
182 wxMenuItem::MacBuildMenuString( label , NULL , NULL , pItem->GetText() ,false);
183
184 if (wxMenuBar::MacGetInstalledMenuBar() == m_menuBar)
185 {
186 ::InsertMenu( pSubMenu->m_hMenu , -1 ) ;
187 }
188
189 if ( pos == (size_t)-1 )
190 {
191 UMAAppendSubMenuItem(m_hMenu, label, pSubMenu->m_macMenuId);
192 }
193 else
194 {
195 UMAInsertSubMenuItem(m_hMenu, label , pos, pSubMenu->m_macMenuId);
196 }
197 }
198 else
199 {
200 Str255 label ;
201 UInt8 modifiers ;
202 SInt16 key ;
203 wxMenuItem::MacBuildMenuString( label, &key , &modifiers , pItem->GetText(), pItem->GetId() == wxApp::s_macAboutMenuItemId);
204 if ( label[0] == 0 )
205 {
206 // we cannot add empty menus on mac
207 label[0] = 1 ;
208 label[1] = ' ' ;
209 }
210 if ( pos == (size_t)-1 )
211 {
212 UMAAppendMenuItem(m_hMenu, label,key,modifiers);
213 }
214 else
215 {
216 UMAInsertMenuItem(m_hMenu, label , pos,key,modifiers);
217 }
218 if ( pItem->GetId() == idMenuTitle )
219 {
220 if ( pos == (size_t)-1 )
221 {
222 UMADisableMenuItem( m_hMenu , CountMenuItems( m_hMenu ) ) ;
223 }
224 else
225 {
226 UMADisableMenuItem( m_hMenu , pos + 1 ) ;
227 }
228 }
229 }
230 }
231 // if we're already attached to the menubar, we must update it
232 if ( IsAttached() )
233 {
234 m_menuBar->Refresh();
235 }
236 return TRUE ;
237}
238
239bool wxMenu::DoAppend(wxMenuItem *item)
240{
241 return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item);
242}
243
244bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
245{
246 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
247}
248
249wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
250{
251 // we need to find the items position in the child list
252 size_t pos;
253 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
254 for ( pos = 0; node; pos++ )
255 {
256 if ( node->GetData() == item )
257 break;
258
259 node = node->GetNext();
260 }
261
262 // DoRemove() (unlike Remove) can only be called for existing item!
263 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
264
265#if wxUSE_ACCEL
266 // remove the corresponding accel from the accel table
267 int n = FindAccel(item->GetId());
268 if ( n != wxNOT_FOUND )
269 {
270 delete m_accels[n];
271
272 m_accels.RemoveAt(n);
273 }
274 //else: this item doesn't have an accel, nothing to do
275#endif // wxUSE_ACCEL
276
277 ::DeleteMenuItem( m_hMenu , pos + 1);
278
279 if ( IsAttached() )
280 {
281 // otherwise, the chane won't be visible
282 m_menuBar->Refresh();
283 }
284
285 // and from internal data structures
286 return wxMenuBase::DoRemove(item);
287}
288
289// ---------------------------------------------------------------------------
290// accelerator helpers
291// ---------------------------------------------------------------------------
292
293#if wxUSE_ACCEL
294
295// create the wxAcceleratorEntries for our accels and put them into provided
296// array - return the number of accels we have
297size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
298{
299 size_t count = GetAccelCount();
300 for ( size_t n = 0; n < count; n++ )
301 {
302 *accels++ = *m_accels[n];
303 }
304
305 return count;
306}
307
308#endif // wxUSE_ACCEL
309
310void wxMenu::SetTitle(const wxString& label)
311{
312 Str255 title ;
313 m_title = label ;
314 wxMenuItem::MacBuildMenuString( title, NULL , NULL , label , false );
315 UMASetMenuTitle( m_hMenu , title ) ;
316}
317bool wxMenu::ProcessCommand(wxCommandEvent & event)
318{
319 bool processed = FALSE;
320
321#if WXWIN_COMPATIBILITY
322 // Try a callback
323 if (m_callback)
324 {
325 (void)(*(m_callback))(*this, event);
326 processed = TRUE;
327 }
328#endif WXWIN_COMPATIBILITY
329
330 // Try the menu's event handler
331 if ( !processed && GetEventHandler())
332 {
333 processed = GetEventHandler()->ProcessEvent(event);
334 }
335
336 // Try the window the menu was popped up from (and up through the
337 // hierarchy)
338 wxWindow *win = GetInvokingWindow();
339 if ( !processed && win )
340 processed = win->GetEventHandler()->ProcessEvent(event);
341
342 return processed;
343}
344
345
346// ---------------------------------------------------------------------------
347// other
348// ---------------------------------------------------------------------------
349
350wxWindow *wxMenu::GetWindow() const
351{
352 if ( m_invokingWindow != NULL )
353 return m_invokingWindow;
354 else if ( m_menuBar != NULL)
355 return (wxWindow *) m_menuBar->GetFrame();
356
357 return NULL;
358}
359
360// helper functions returning the mac menu position for a certain item, note that this is
361// mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0
362
363int wxMenu::MacGetIndexFromId( int id )
364{
365 size_t pos;
366 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
367 for ( pos = 0; node; pos++ )
368 {
369 if ( node->GetData()->GetId() == id )
370 break;
371
372 node = node->GetNext();
373 }
374
375 if (!node)
376 return 0;
377
378 return pos + 1 ;
379}
380
381int wxMenu::MacGetIndexFromItem( wxMenuItem *pItem )
382{
383 size_t pos;
384 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
385 for ( pos = 0; node; pos++ )
386 {
387 if ( node->GetData() == pItem )
388 break;
389
390 node = node->GetNext();
391 }
392
393 if (!node)
394 return 0;
395
396 return pos + 1 ;
397}
398
399void wxMenu::MacEnableMenu( bool bDoEnable )
400{
401 if ( bDoEnable )
402 UMAEnableMenuItem( m_hMenu , 0 ) ;
403 else
404 UMADisableMenuItem( m_hMenu , 0 ) ;
405
406 ::DrawMenuBar() ;
407}
408
409bool wxMenu::MacMenuSelect( wxEvtHandler* handler, long when , int macMenuId, int macMenuItemNum )
410{
411 int pos;
412 wxNode *node;
413
414 if ( m_macMenuId == macMenuId )
415 {
416 node = GetMenuItems().Nth(macMenuItemNum-1);
417 if (node)
418 {
419 wxMenuItem *pItem = (wxMenuItem*)node->Data();
420
421 if (pItem->IsCheckable())
422 pItem->Check(! pItem->IsChecked());
423
424 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
425 event.m_timeStamp = when;
426 event.SetEventObject(handler);
427 event.SetInt( pItem->GetId() );
428 {
429 bool processed = false ;
430
431#if WXWIN_COMPATIBILITY
432 // Try a callback
433 if (m_callback)
434 {
435 (void) (*(m_callback)) (*this, event);
436 processed = TRUE;
437 }
438#endif
439 // Try the menu's event handler
440 if ( !processed && handler)
441 {
442 processed = handler->ProcessEvent(event);
443 }
444
445 // Try the window the menu was popped up from (and up
446 // through the hierarchy)
447 if ( !processed && GetInvokingWindow())
448 processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
449 }
450 return true ;
451 }
452 }
453#ifndef __DARWIN__
454 else if ( macMenuId == kHMHelpMenuID )
455 {
456 int menuItem = firstUserHelpMenuItem-1 ;
457 for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++)
458 {
459 wxMenuItem * pItem = (wxMenuItem *) node->Data() ;
460
461 wxMenu *pSubMenu = pItem->GetSubMenu() ;
462 if ( pSubMenu != NULL )
463 {
464 }
465 else
466 {
467 if ( pItem->GetId() != wxApp::s_macAboutMenuItemId )
468 ++menuItem ;
469
470 if ( menuItem == macMenuItemNum )
471 {
472 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, pItem->GetId());
473 event.m_timeStamp = when;
474 event.SetEventObject(handler);
475 event.SetInt( pItem->GetId() );
476 {
477 bool processed = false ;
478#if WXWIN_COMPATIBILITY
479 // Try a callback
480 if (m_callback)
481 {
482 (void) (*(m_callback)) (*this, event);
483 processed = TRUE;
484 }
485#endif
486 // Try the menu's event handler
487 if ( !processed && handler)
488 {
489 processed = handler->ProcessEvent(event);
490 }
491
492 // Try the window the menu was popped up from (and up
493 // through the hierarchy)
494 if ( !processed && GetInvokingWindow())
495 processed = GetInvokingWindow()->GetEventHandler()->ProcessEvent(event);
496 }
497 return true ;
498 }
499 }
500 }
501 }
502#endif // __DARWIN__
503
504 for (pos = 0, node = GetMenuItems().First(); node; node = node->Next(), pos++)
505 {
506 wxMenuItem * pItem = (wxMenuItem *) node->Data() ;
507
508 wxMenu *pSubMenu = pItem->GetSubMenu() ;
509 if ( pSubMenu != NULL )
510 {
511 if ( pSubMenu->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
512 return true ;
513 }
514 }
515
516 return false ;
517}
518
519// Menu Bar
520
521/*
522
523Mac Implementation note :
524
525The Mac has only one global menubar, so we attempt to install the currently
526active menubar from a frame, we currently don't take into account mdi-frames
527which would ask for menu-merging
528
529Secondly there is no mac api for changing a menubar that is not the current
530menubar, so we have to wait for preparing the actual menubar until the
531wxMenubar is to be used
532
533We can in subsequent versions use MacInstallMenuBar to provide some sort of
534auto-merge for MDI in case this will be necessary
535
536*/
537
538wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ;
539
540void wxMenuBar::Init()
541{
542 m_eventHandler = this;
543 m_menuBarFrame = NULL;
544}
545
546wxMenuBar::wxMenuBar()
547{
548 Init();
549}
550
551wxMenuBar::wxMenuBar( long WXUNUSED(style) )
552{
553 Init();
554}
555
556
557wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
558{
559 Init();
560
561 m_titles.Alloc(count);
562
563 for ( int i = 0; i < count; i++ )
564 {
565 m_menus.Append(menus[i]);
566 m_titles.Add(titles[i]);
567
568 menus[i]->Attach(this);
569 }
570}
571
572wxMenuBar::~wxMenuBar()
573{
574 if (s_macInstalledMenuBar == this)
575 {
576 ::ClearMenuBar();
577 s_macInstalledMenuBar = NULL;
578 }
579
580}
581
582void wxMenuBar::Refresh()
583{
584 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
585
586 DrawMenuBar();
587}
588
589#if wxUSE_ACCEL
590
591void wxMenuBar::RebuildAccelTable()
592{
593 // merge the accelerators of all menus into one accel table
594 size_t nAccelCount = 0;
595 size_t i, count = GetMenuCount();
596 for ( i = 0; i < count; i++ )
597 {
598 nAccelCount += m_menus[i]->GetAccelCount();
599 }
600
601 if ( nAccelCount )
602 {
603 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
604
605 nAccelCount = 0;
606 for ( i = 0; i < count; i++ )
607 {
608 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
609 }
610
611 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
612
613 delete [] accelEntries;
614 }
615}
616
617#endif // wxUSE_ACCEL
618
619
620void wxMenuBar::MacInstallMenuBar()
621{
622 if ( s_macInstalledMenuBar == this )
623 return ;
624
625 Handle menubar = ::GetNewMBar( kwxMacMenuBarResource ) ;
626 wxString message ;
627 wxCHECK_RET( menubar != NULL, "can't read MBAR resource" );
628 ::SetMenuBar( menubar ) ;
629 ::DisposeHandle( menubar ) ;
630
631 MenuHandle menu = ::GetMenuHandle( kwxMacAppleMenuId ) ;
632 ::AppendResMenu(menu, 'DRVR');
633
634 for (int i = 0; i < m_menus.GetCount(); i++)
635 {
636 Str255 label;
637 wxNode *node;
638 wxMenuItem *item;
639 int pos ;
640 wxMenu* menu = m_menus[i] , *subMenu = NULL ;
641
642 if( m_titles[i] == "?" || m_titles[i] == "&?" || m_titles[i] == wxApp::s_macHelpMenuTitleName )
643 {
644 MenuHandle mh = NULL ;
645 if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) != noErr )
646 {
647 continue ;
648 }
649
650 for (pos = 0 , node = menu->GetMenuItems().First(); node; node = node->Next(), pos++)
651 {
652 item = (wxMenuItem *)node->Data();
653 subMenu = item->GetSubMenu() ;
654 if (subMenu)
655 {
656 // we don't support hierarchical menus in the help menu yet
657 }
658 else
659 {
660 if ( item->IsSeparator() )
661 {
662 if ( mh )
663 MacAppendMenu(mh, "\p-" );
664 }
665 else
666 {
667 Str255 label ;
668 UInt8 modifiers ;
669 SInt16 key ;
670 wxMenuItem::MacBuildMenuString( label, &key , &modifiers , item->GetText(), item->GetId() != wxApp::s_macAboutMenuItemId); // no shortcut in about menu
671 if ( label[0] == 0 )
672 {
673 // we cannot add empty menus on mac
674 label[0] = 1 ;
675 label[1] = ' ' ;
676 }
677 if ( item->GetId() == wxApp::s_macAboutMenuItemId )
678 {
679 ::SetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , label );
680 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 );
681 }
682 else
683 {
684 if ( mh )
685 UMAAppendMenuItem(mh, label , key , modifiers );
686 }
687 }
688 }
689 }
690 }
691 else
692 {
693 wxMenuItem::MacBuildMenuString( label, NULL , NULL , m_titles[i] , false );
694 UMASetMenuTitle( menu->GetHMenu() , label ) ;
695 wxArrayPtrVoid submenus ;
696
697 for (pos = 0, node = menu->GetMenuItems().First(); node; node = node->Next(), pos++)
698 {
699 item = (wxMenuItem *)node->Data();
700 subMenu = item->GetSubMenu() ;
701 if (subMenu)
702 {
703 submenus.Add(subMenu) ;
704 }
705 }
706 ::InsertMenu(m_menus[i]->GetHMenu(), 0);
707 for ( int i = 0 ; i < submenus.GetCount() ; ++i )
708 {
709 wxMenu* submenu = (wxMenu*) submenus[i] ;
710 wxNode *subnode;
711 wxMenuItem *subitem;
712 int subpos ;
713 for ( subpos = 0 , subnode = submenu->GetMenuItems().First(); subnode; subnode = subnode->Next(), subpos++)
714 {
715 subitem = (wxMenuItem *)subnode->Data();
716 wxMenu* itsSubMenu = subitem->GetSubMenu() ;
717 if (itsSubMenu)
718 {
719 submenus.Add(itsSubMenu) ;
720 }
721 }
722 ::InsertMenu( submenu->GetHMenu() , -1 ) ;
723 }
724 }
725 }
726 ::DrawMenuBar() ;
727
728 s_macInstalledMenuBar = this;
729}
730
731void wxMenuBar::EnableTop(size_t pos, bool enable)
732{
733 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
734 m_menus[pos]->MacEnableMenu( enable ) ;
735 Refresh();
736}
737
738void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
739{
740 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
741
742 m_titles[pos] = label;
743
744 if ( !IsAttached() )
745 {
746 return;
747 }
748
749 m_menus[pos]->SetTitle( label ) ;
750 if (wxMenuBar::s_macInstalledMenuBar == this) // are we currently installed ?
751 {
752 ::SetMenuBar( GetMenuBar() ) ;
753 ::InvalMenuBar() ;
754 }
755}
756
757wxString wxMenuBar::GetLabelTop(size_t pos) const
758{
759 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
760 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
761
762 return m_titles[pos];
763}
764
765int wxMenuBar::FindMenu(const wxString& title)
766{
767 wxString menuTitle = wxStripMenuCodes(title);
768
769 size_t count = GetMenuCount();
770 for ( size_t i = 0; i < count; i++ )
771 {
772 wxString title = wxStripMenuCodes(m_titles[i]);
773 if ( menuTitle == title )
774 return i;
775 }
776
777 return wxNOT_FOUND;
778
779}
780
781
782// ---------------------------------------------------------------------------
783// wxMenuBar construction
784// ---------------------------------------------------------------------------
785
786// ---------------------------------------------------------------------------
787// wxMenuBar construction
788// ---------------------------------------------------------------------------
789
790wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
791{
792 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
793 if ( !menuOld )
794 return FALSE;
795 m_titles[pos] = title;
796
797 if ( IsAttached() )
798 {
799 if (s_macInstalledMenuBar == this)
800 {
801 ::DeleteMenu( menuOld->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
802 {
803 Str255 label;
804 wxMenuItem::MacBuildMenuString( label, NULL , NULL , title , false );
805 UMASetMenuTitle( menu->GetHMenu() , label ) ;
806 if ( pos == m_menus.GetCount() - 1)
807 {
808 ::InsertMenu( menu->GetHMenu() , 0 ) ;
809 }
810 else
811 {
812 ::InsertMenu( menu->GetHMenu() , m_menus[pos+1]->MacGetMenuId() ) ;
813 }
814 }
815 }
816
817
818#if wxUSE_ACCEL
819 if ( menuOld->HasAccels() || menu->HasAccels() )
820 {
821 // need to rebuild accell table
822 RebuildAccelTable();
823 }
824#endif // wxUSE_ACCEL
825
826 Refresh();
827 }
828
829 return menuOld;
830}
831
832bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
833{
834 if ( !wxMenuBarBase::Insert(pos, menu, title) )
835 return FALSE;
836
837 m_titles.Insert(title, pos);
838
839 menu->Attach(this);
840
841 if ( IsAttached() )
842 {
843 if ( pos == (size_t) -1 )
844 {
845 ::InsertMenu( menu->GetHMenu() , 0 ) ;
846 }
847 else
848 {
849 ::InsertMenu( menu->GetHMenu() , m_menus[pos+1]->MacGetMenuId() ) ;
850 }
851
852#if wxUSE_ACCEL
853 if ( menu->HasAccels() )
854 {
855 // need to rebuild accell table
856 RebuildAccelTable();
857 }
858#endif // wxUSE_ACCEL
859
860 Refresh();
861 }
862
863 return TRUE;
864}
865
866void wxMenuBar::MacMenuSelect(wxEvtHandler* handler, long when , int macMenuId, int macMenuItemNum)
867{
868 // first scan fast for direct commands, i.e. menus which have these commands directly in their own list
869
870 if ( macMenuId == kwxMacAppleMenuId && macMenuItemNum == 1 )
871 {
872 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macAboutMenuItemId );
873 event.m_timeStamp = when;
874 event.SetEventObject(handler);
875 event.SetInt( wxApp::s_macAboutMenuItemId );
876 handler->ProcessEvent(event);
877 }
878 else
879 {
880 for (int i = 0; i < m_menus.GetCount() ; i++)
881 {
882 if ( m_menus[i]->MacGetMenuId() == macMenuId
883#ifndef __DARWIN__
884 ||
885 ( macMenuId == kHMHelpMenuID && ( m_titles[i] == "?" || m_titles[i] == "&?" || m_titles[i] == wxApp::s_macHelpMenuTitleName ) )
886#endif
887 )
888 {
889 if ( m_menus[i]->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
890 return ;
891 else
892 {
893 //TODO flag this as an error since it must contain the item
894 return ;
895 }
896 }
897 }
898
899 for (int i = 0; i < m_menus.GetCount(); i++)
900 {
901 if ( m_menus[i]->MacMenuSelect( handler , when , macMenuId , macMenuItemNum ) )
902 {
903 break ;
904 }
905 }
906 }
907}
908
909wxMenu *wxMenuBar::Remove(size_t pos)
910{
911 wxMenu *menu = wxMenuBarBase::Remove(pos);
912 if ( !menu )
913 return NULL;
914
915 if ( IsAttached() )
916 {
917 if (s_macInstalledMenuBar == this)
918 {
919 ::DeleteMenu( menu->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
920 }
921
922 menu->Detach();
923
924#if wxUSE_ACCEL
925 if ( menu->HasAccels() )
926 {
927 // need to rebuild accell table
928 RebuildAccelTable();
929 }
930#endif // wxUSE_ACCEL
931
932 Refresh();
933 }
934
935 m_titles.Remove(pos);
936
937 return menu;
938}
939
940bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
941{
942 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
943 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
944
945 if ( !wxMenuBarBase::Append(menu, title) )
946 return FALSE;
947
948 m_titles.Add(title);
949
950 if ( IsAttached() )
951 {
952 if (s_macInstalledMenuBar == this)
953 {
954 ::InsertMenu( menu->GetHMenu() , 0 ) ;
955 }
956
957#if wxUSE_ACCEL
958 if ( menu->HasAccels() )
959 {
960 // need to rebuild accell table
961 RebuildAccelTable();
962 }
963#endif // wxUSE_ACCEL
964
965 Refresh();
966 }
967
968 return TRUE;
969}
970
971void wxMenuBar::Detach()
972{
973 wxMenuBarBase::Detach() ;
974}
975
976void wxMenuBar::Attach(wxFrame *frame)
977{
978 wxMenuBarBase::Attach( frame ) ;
979
980#if wxUSE_ACCEL
981 RebuildAccelTable();
982#endif // wxUSE_ACCEL
983}
984// ---------------------------------------------------------------------------
985// wxMenuBar searching for menu items
986// ---------------------------------------------------------------------------
987
988// Find the itemString in menuString, and return the item id or wxNOT_FOUND
989int wxMenuBar::FindMenuItem(const wxString& menuString,
990 const wxString& itemString) const
991{
992 wxString menuLabel = wxStripMenuCodes(menuString);
993 size_t count = GetMenuCount();
994 for ( size_t i = 0; i < count; i++ )
995 {
996 wxString title = wxStripMenuCodes(m_titles[i]);
997 if ( menuString == title )
998 return m_menus[i]->FindItem(itemString);
999 }
1000
1001 return wxNOT_FOUND;
1002}
1003
1004wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const
1005{
1006 if ( itemMenu )
1007 *itemMenu = NULL;
1008
1009 wxMenuItem *item = NULL;
1010 size_t count = GetMenuCount();
1011 for ( size_t i = 0; !item && (i < count); i++ )
1012 {
1013 item = m_menus[i]->FindItem(id, itemMenu);
1014 }
1015
1016 return item;
1017}
1018
1019