]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/menu.cpp
Fixed incorrect clipping region on wxGTK
[wxWidgets.git] / src / mac / carbon / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "menu.h"
14 #pragma implementation "menuitem.h"
15 #endif
16
17 // ============================================================================
18 // headers & declarations
19 // ============================================================================
20
21 // wxWidgets headers
22 // -----------------
23
24 #include "wx/app.h"
25 #include "wx/menu.h"
26 #include "wx/menuitem.h"
27 #include "wx/window.h"
28 #include "wx/log.h"
29 #include "wx/utils.h"
30 #include "wx/frame.h"
31
32 #include "wx/mac/uma.h"
33
34 // other standard headers
35 // ----------------------
36 #include <string.h>
37
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
40 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
41 #endif
42
43 // the (popup) menu title has this special id
44 static const int idMenuTitle = -3;
45 static MenuItemIndex firstUserHelpMenuItem = 0 ;
46
47 const short kwxMacMenuBarResource = 1 ;
48 const short kwxMacAppleMenuId = 1 ;
49
50
51 // Find an item given the Macintosh Menu Reference
52
53 wxList wxWinMacMenuList(wxKEY_INTEGER);
54 wxMenu *wxFindMenuFromMacMenu(MenuRef inMenuRef)
55 {
56 wxNode *node = wxWinMacMenuList.Find((long)inMenuRef);
57 if (!node)
58 return NULL;
59 return (wxMenu *)node->GetData();
60 }
61
62 void wxAssociateMenuWithMacMenu(MenuRef inMenuRef, wxMenu *menu) ;
63 void wxAssociateMenuWithMacMenu(MenuRef inMenuRef, wxMenu *menu)
64 {
65 // adding NULL MenuRef is (first) surely a result of an error and
66 // (secondly) breaks menu command processing
67 wxCHECK_RET( inMenuRef != (MenuRef) NULL, wxT("attempt to add a NULL MenuRef to menu list") );
68
69 if ( !wxWinMacMenuList.Find((long)inMenuRef) )
70 wxWinMacMenuList.Append((long)inMenuRef, menu);
71 }
72
73 void wxRemoveMacMenuAssociation(wxMenu *menu) ;
74 void wxRemoveMacMenuAssociation(wxMenu *menu)
75 {
76 wxWinMacMenuList.DeleteObject(menu);
77 }
78
79
80 // ============================================================================
81 // implementation
82 // ============================================================================
83 static void wxMenubarUnsetInvokingWindow( wxMenu *menu ) ;
84 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win );
85
86 // Menus
87
88 // Construct a menu with optional title (then use append)
89
90 #ifdef __DARWIN__
91 short wxMenu::s_macNextMenuId = 3 ;
92 #else
93 short wxMenu::s_macNextMenuId = 2 ;
94 #endif
95
96 void wxMenu::Init()
97 {
98 m_doBreak = FALSE;
99 m_startRadioGroup = -1;
100
101 // create the menu
102 m_macMenuId = s_macNextMenuId++;
103 m_hMenu = UMANewMenu(m_macMenuId, m_title, wxFont::GetDefaultEncoding() );
104
105 if ( !m_hMenu )
106 {
107 wxLogLastError(wxT("UMANewMenu failed"));
108 }
109
110 wxAssociateMenuWithMacMenu( (MenuRef)m_hMenu , this ) ;
111
112 // if we have a title, insert it in the beginning of the menu
113 if ( !!m_title )
114 {
115 Append(idMenuTitle, m_title) ;
116 AppendSeparator() ;
117 }
118 }
119
120 wxMenu::~wxMenu()
121 {
122 wxRemoveMacMenuAssociation( this ) ;
123 if (MAC_WXHMENU(m_hMenu))
124 ::DisposeMenu(MAC_WXHMENU(m_hMenu));
125 }
126
127 void wxMenu::Break()
128 {
129 // not available on the mac platform
130 }
131
132 void wxMenu::Attach(wxMenuBarBase *menubar)
133 {
134 wxMenuBase::Attach(menubar);
135
136 EndRadioGroup();
137 }
138
139 // function appends a new item or submenu to the menu
140 // append a new item or submenu to the menu
141 bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
142 {
143 wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") );
144
145 if ( pItem->IsSeparator() )
146 {
147 if ( pos == (size_t)-1 )
148 MacAppendMenu(MAC_WXHMENU(m_hMenu), "\p-");
149 else
150 MacInsertMenuItem(MAC_WXHMENU(m_hMenu), "\p-" , pos);
151 }
152 else
153 {
154 wxMenu *pSubMenu = pItem->GetSubMenu() ;
155 if ( pSubMenu != NULL )
156 {
157 wxASSERT_MSG( pSubMenu->m_hMenu != NULL , wxT("invalid submenu added"));
158 pSubMenu->m_menuParent = this ;
159
160 if (wxMenuBar::MacGetInstalledMenuBar() == GetMenuBar())
161 {
162 pSubMenu->MacBeforeDisplay( true ) ;
163 }
164
165 if ( pos == (size_t)-1 )
166 UMAAppendSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pSubMenu->m_macMenuId);
167 else
168 UMAInsertSubMenuItem(MAC_WXHMENU(m_hMenu), pItem->GetText(), wxFont::GetDefaultEncoding() , pos, pSubMenu->m_macMenuId);
169 pItem->UpdateItemBitmap() ;
170 pItem->UpdateItemStatus() ;
171 }
172 else
173 {
174 if ( pos == (size_t)-1 )
175 {
176 UMAAppendMenuItem(MAC_WXHMENU(m_hMenu), wxT("a") , wxFont::GetDefaultEncoding() );
177 pos = CountMenuItems(MAC_WXHMENU(m_hMenu)) ;
178 }
179 else
180 {
181 // MacOS counts menu items from 1 and inserts after, therefore having the
182 // same effect as wx 0 based and inserting before, we must correct pos
183 // after however for updates to be correct
184 UMAInsertMenuItem(MAC_WXHMENU(m_hMenu), wxT("a"), wxFont::GetDefaultEncoding(), pos);
185 pos += 1 ;
186 }
187
188 SetMenuItemCommandID( MAC_WXHMENU(m_hMenu) , pos , pItem->GetId() ) ;
189 SetMenuItemRefCon( MAC_WXHMENU(m_hMenu) , pos , (UInt32) pItem ) ;
190 pItem->UpdateItemText() ;
191 pItem->UpdateItemBitmap() ;
192 pItem->UpdateItemStatus() ;
193
194 if ( pItem->GetId() == idMenuTitle )
195 {
196 UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , pos , false ) ;
197 }
198 }
199 }
200 // if we're already attached to the menubar, we must update it
201 if ( IsAttached() )
202 {
203 GetMenuBar()->Refresh();
204 }
205 return TRUE ;
206 }
207
208 void wxMenu::EndRadioGroup()
209 {
210 // we're not inside a radio group any longer
211 m_startRadioGroup = -1;
212 }
213
214 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
215 {
216 wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
217
218 bool check = FALSE;
219
220 if ( item->GetKind() == wxITEM_RADIO )
221 {
222 int count = GetMenuItemCount();
223
224 if ( m_startRadioGroup == -1 )
225 {
226 // start a new radio group
227 m_startRadioGroup = count;
228
229 // for now it has just one element
230 item->SetAsRadioGroupStart();
231 item->SetRadioGroupEnd(m_startRadioGroup);
232
233 // ensure that we have a checked item in the radio group
234 check = TRUE;
235 }
236 else // extend the current radio group
237 {
238 // we need to update its end item
239 item->SetRadioGroupStart(m_startRadioGroup);
240 wxMenuItemList::Node *node = GetMenuItems().Item(m_startRadioGroup);
241
242 if ( node )
243 {
244 node->GetData()->SetRadioGroupEnd(count);
245 }
246 else
247 {
248 wxFAIL_MSG( _T("where is the radio group start item?") );
249 }
250 }
251 }
252 else // not a radio item
253 {
254 EndRadioGroup();
255 }
256
257 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
258 {
259 return NULL;
260 }
261
262 if ( check )
263 {
264 // check the item initially
265 item->Check(TRUE);
266 }
267
268 return item;
269 }
270
271 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
272 {
273 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
274 return item;
275 else
276 return NULL;
277 }
278
279 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
280 {
281 // we need to find the items position in the child list
282 size_t pos;
283 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
284 for ( pos = 0; node; pos++ )
285 {
286 if ( node->GetData() == item )
287 break;
288
289 node = node->GetNext();
290 }
291
292 // DoRemove() (unlike Remove) can only be called for existing item!
293 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
294
295 ::DeleteMenuItem(MAC_WXHMENU(m_hMenu) , pos + 1);
296
297 if ( IsAttached() )
298 {
299 // otherwise, the change won't be visible
300 GetMenuBar()->Refresh();
301 }
302
303 // and from internal data structures
304 return wxMenuBase::DoRemove(item);
305 }
306
307 void wxMenu::SetTitle(const wxString& label)
308 {
309 m_title = label ;
310 UMASetMenuTitle(MAC_WXHMENU(m_hMenu) , label , wxFont::GetDefaultEncoding() ) ;
311 }
312 bool wxMenu::ProcessCommand(wxCommandEvent & event)
313 {
314 bool processed = FALSE;
315
316 // Try the menu's event handler
317 if ( !processed && GetEventHandler())
318 {
319 processed = GetEventHandler()->ProcessEvent(event);
320 }
321
322 // Try the window the menu was popped up from (and up through the
323 // hierarchy)
324 wxWindow *win = GetInvokingWindow();
325 if ( !processed && win )
326 processed = win->GetEventHandler()->ProcessEvent(event);
327
328 return processed;
329 }
330
331
332 // ---------------------------------------------------------------------------
333 // other
334 // ---------------------------------------------------------------------------
335
336 wxWindow *wxMenu::GetWindow() const
337 {
338 if ( m_invokingWindow != NULL )
339 return m_invokingWindow;
340 else if ( GetMenuBar() != NULL)
341 return (wxWindow *) GetMenuBar()->GetFrame();
342
343 return NULL;
344 }
345
346 // helper functions returning the mac menu position for a certain item, note that this is
347 // mac-wise 1 - based, i.e. the first item has index 1 whereas on MSWin it has pos 0
348
349 int wxMenu::MacGetIndexFromId( int id )
350 {
351 size_t pos;
352 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
353 for ( pos = 0; node; pos++ )
354 {
355 if ( node->GetData()->GetId() == id )
356 break;
357
358 node = node->GetNext();
359 }
360
361 if (!node)
362 return 0;
363
364 return pos + 1 ;
365 }
366
367 int wxMenu::MacGetIndexFromItem( wxMenuItem *pItem )
368 {
369 size_t pos;
370 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
371 for ( pos = 0; node; pos++ )
372 {
373 if ( node->GetData() == pItem )
374 break;
375
376 node = node->GetNext();
377 }
378
379 if (!node)
380 return 0;
381
382 return pos + 1 ;
383 }
384
385 void wxMenu::MacEnableMenu( bool bDoEnable )
386 {
387 UMAEnableMenuItem(MAC_WXHMENU(m_hMenu) , 0 , bDoEnable ) ;
388
389 ::DrawMenuBar() ;
390 }
391
392 // MacOS needs to know about submenus somewhere within this menu
393 // before it can be displayed , also hide special menu items like preferences
394 // that are handled by the OS
395 void wxMenu::MacBeforeDisplay( bool isSubMenu )
396 {
397 wxMenuItem* previousItem = NULL ;
398 size_t pos ;
399 wxMenuItemList::Node *node;
400 wxMenuItem *item;
401 for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
402 {
403 item = (wxMenuItem *)node->GetData();
404 wxMenu* subMenu = item->GetSubMenu() ;
405 if (subMenu)
406 {
407 subMenu->MacBeforeDisplay( true ) ;
408 }
409 else
410 {
411 #if TARGET_CARBON
412 if ( UMAGetSystemVersion() >= 0x1000 )
413 {
414 if ( item->GetId() == wxApp::s_macPreferencesMenuItemId || item->GetId() == wxApp::s_macExitMenuItemId)
415 {
416 ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos + 1, kMenuItemAttrHidden, 0 );
417 if ( GetMenuItems().GetCount() == pos + 1 &&
418 previousItem != NULL &&
419 previousItem->IsSeparator() )
420 {
421 ChangeMenuItemAttributes( MAC_WXHMENU( GetHMenu() ) , pos , kMenuItemAttrHidden, 0 );
422 }
423 }
424 }
425 #endif
426 }
427 previousItem = item ;
428 }
429
430 if ( isSubMenu )
431 ::InsertMenu(MAC_WXHMENU( GetHMenu()), -1);
432
433 }
434 // undo all changes from the MacBeforeDisplay call
435 void wxMenu::MacAfterDisplay( bool isSubMenu )
436 {
437 if ( isSubMenu )
438 ::DeleteMenu(MacGetMenuId());
439
440 wxMenuItem* previousItem = NULL ;
441 int pos ;
442 wxMenuItemList::Node *node;
443 wxMenuItem *item;
444 for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
445 {
446 item = (wxMenuItem *)node->GetData();
447 wxMenu* subMenu = item->GetSubMenu() ;
448 if (subMenu)
449 {
450 subMenu->MacAfterDisplay( true ) ;
451 }
452 else
453 {
454 // no need to undo hidings
455 }
456 previousItem = item ;
457 }
458 }
459
460 // Menu Bar
461
462 /*
463
464 Mac Implementation note :
465
466 The Mac has only one global menubar, so we attempt to install the currently
467 active menubar from a frame, we currently don't take into account mdi-frames
468 which would ask for menu-merging
469
470 Secondly there is no mac api for changing a menubar that is not the current
471 menubar, so we have to wait for preparing the actual menubar until the
472 wxMenubar is to be used
473
474 We can in subsequent versions use MacInstallMenuBar to provide some sort of
475 auto-merge for MDI in case this will be necessary
476
477 */
478
479 wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ;
480 wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ;
481
482 void wxMenuBar::Init()
483 {
484 m_eventHandler = this;
485 m_menuBarFrame = NULL;
486 m_invokingWindow = (wxWindow*) NULL;
487 }
488
489 wxMenuBar::wxMenuBar()
490 {
491 Init();
492 }
493
494 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
495 {
496 Init();
497 }
498
499
500 wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
501 {
502 Init();
503
504 m_titles.Alloc(count);
505
506 for ( int i = 0; i < count; i++ )
507 {
508 m_menus.Append(menus[i]);
509 m_titles.Add(titles[i]);
510
511 menus[i]->Attach(this);
512 }
513 }
514
515 wxMenuBar::~wxMenuBar()
516 {
517 if (s_macCommonMenuBar == this)
518 s_macCommonMenuBar = NULL;
519 if (s_macInstalledMenuBar == this)
520 {
521 ::ClearMenuBar();
522 s_macInstalledMenuBar = NULL;
523 }
524
525 }
526
527 void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect))
528 {
529 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
530
531 DrawMenuBar();
532 }
533
534 void wxMenuBar::MacInstallMenuBar()
535 {
536 if ( s_macInstalledMenuBar == this )
537 return ;
538
539 MenuBarHandle menubar = NULL ;
540 #if TARGET_API_MAC_OSX
541 menubar = NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ;
542 #else
543 menubar = NewHandleClear( 12 ) ;
544 (*menubar)[3] = 0x0a ;
545 #endif
546 ::SetMenuBar( menubar ) ;
547 DisposeMenuBar( menubar ) ;
548 MenuHandle appleMenu = NULL ;
549 char appleMenuTitle[3] = { 01 , kMenuAppleLogoFilledGlyph , 0 } ;
550
551 verify_noerr( CreateNewMenu( kwxMacAppleMenuId , 0 , &appleMenu ) ) ;
552 verify_noerr( SetMenuTitle( appleMenu , (ConstStr255Param) appleMenuTitle ) );
553 MacInsertMenuItem( appleMenu , "\pAbout..." , 0 ) ;
554 MacInsertMenu( appleMenu , 0 ) ;
555
556 // clean-up the help menu before adding new items
557 MenuHandle mh = NULL ;
558 if ( UMAGetHelpMenu( &mh , &firstUserHelpMenuItem) == noErr )
559 {
560 for ( int i = CountMenuItems( mh ) ; i >= firstUserHelpMenuItem ; --i )
561 {
562 DeleteMenuItem( mh , i ) ;
563 }
564 }
565 else
566 {
567 mh = NULL ;
568 }
569 #if TARGET_CARBON
570 if ( UMAGetSystemVersion() >= 0x1000 && wxApp::s_macPreferencesMenuItemId)
571 {
572 wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ;
573 if ( item == NULL || !(item->IsEnabled()) )
574 DisableMenuCommand( NULL , kHICommandPreferences ) ;
575 else
576 EnableMenuCommand( NULL , kHICommandPreferences ) ;
577 }
578 #endif
579 for (size_t i = 0; i < m_menus.GetCount(); i++)
580 {
581 wxMenuItemList::Node *node;
582 wxMenuItem *item;
583 int pos ;
584 wxMenu* menu = m_menus[i] , *subMenu = NULL ;
585
586 if( m_titles[i] == wxT("?") || m_titles[i] == wxT("&?") || m_titles[i] == wxApp::s_macHelpMenuTitleName )
587 {
588 if ( mh == NULL )
589 {
590 continue ;
591 }
592
593 for (pos = 0 , node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
594 {
595 item = (wxMenuItem *)node->GetData();
596 subMenu = item->GetSubMenu() ;
597 if (subMenu)
598 {
599 // we don't support hierarchical menus in the help menu yet
600 }
601 else
602 {
603 if ( item->IsSeparator() )
604 {
605 if ( mh )
606 MacAppendMenu(mh, "\p-" );
607 }
608 else
609 {
610 wxAcceleratorEntry* entry = wxGetAccelFromString( item->GetText() ) ;
611
612 if ( item->GetId() == wxApp::s_macAboutMenuItemId )
613 {
614 UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetText() , wxFont::GetDefaultEncoding() );
615 UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true );
616 SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , item->GetId() ) ;
617 SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId ) , 1 , (UInt32)item ) ;
618 UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ;
619 }
620 else
621 {
622 if ( mh )
623 {
624 UMAAppendMenuItem(mh, item->GetText() , wxFont::GetDefaultEncoding(), entry);
625 SetMenuItemCommandID( mh , CountMenuItems(mh) , item->GetId() ) ;
626 SetMenuItemRefCon( mh , CountMenuItems(mh) , (UInt32)item ) ;
627 }
628 }
629
630 delete entry ;
631 }
632 }
633 }
634 }
635 else
636 {
637 UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], m_font.GetEncoding() ) ;
638 m_menus[i]->MacBeforeDisplay(false) ;
639 ::InsertMenu(MAC_WXHMENU(m_menus[i]->GetHMenu()), 0);
640 }
641 }
642 ::DrawMenuBar() ;
643 s_macInstalledMenuBar = this;
644 }
645
646 void wxMenuBar::EnableTop(size_t pos, bool enable)
647 {
648 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
649 m_menus[pos]->MacEnableMenu( enable ) ;
650 Refresh();
651 }
652
653 bool wxMenuBar::Enable( bool enable)
654 {
655 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
656 size_t i;
657 for (i = 0; i < GetMenuCount(); i++)
658 {
659 EnableTop(i, enable);
660 }
661 return true;
662 }
663
664 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
665 {
666 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
667
668 m_titles[pos] = label;
669
670 if ( !IsAttached() )
671 {
672 return;
673 }
674
675 m_menus[pos]->SetTitle( label ) ;
676 if (wxMenuBar::s_macInstalledMenuBar == this) // are we currently installed ?
677 {
678 ::SetMenuBar( GetMenuBar() ) ;
679 ::InvalMenuBar() ;
680 }
681 }
682
683 wxString wxMenuBar::GetLabelTop(size_t pos) const
684 {
685 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
686 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
687
688 return m_titles[pos];
689 }
690
691 int wxMenuBar::FindMenu(const wxString& title)
692 {
693 wxString menuTitle = wxStripMenuCodes(title);
694
695 size_t count = GetMenuCount();
696 for ( size_t i = 0; i < count; i++ )
697 {
698 wxString title = wxStripMenuCodes(m_titles[i]);
699 if ( menuTitle == title )
700 return i;
701 }
702
703 return wxNOT_FOUND;
704
705 }
706
707
708 // ---------------------------------------------------------------------------
709 // wxMenuBar construction
710 // ---------------------------------------------------------------------------
711
712 // ---------------------------------------------------------------------------
713 // wxMenuBar construction
714 // ---------------------------------------------------------------------------
715
716 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
717 {
718 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
719 if ( !menuOld )
720 return FALSE;
721 m_titles[pos] = title;
722
723 if ( IsAttached() )
724 {
725 if (s_macInstalledMenuBar == this)
726 {
727 menuOld->MacAfterDisplay( false ) ;
728 ::DeleteMenu( menuOld->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
729 {
730 menu->MacBeforeDisplay( false ) ;
731 UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
732 if ( pos == m_menus.GetCount() - 1)
733 {
734 ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
735 }
736 else
737 {
738 ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ;
739 }
740 }
741 }
742
743 Refresh();
744 }
745
746 return menuOld;
747 }
748
749 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
750 {
751 if ( !wxMenuBarBase::Insert(pos, menu, title) )
752 return FALSE;
753
754 m_titles.Insert(title, pos);
755
756 UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
757
758 if ( IsAttached() && s_macInstalledMenuBar == this )
759 {
760 if (s_macInstalledMenuBar == this)
761 {
762 menu->MacBeforeDisplay( false ) ;
763 if ( pos == (size_t) -1 || pos + 1 == m_menus.GetCount() )
764 {
765 ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
766 }
767 else
768 {
769 ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , m_menus[pos+1]->MacGetMenuId() ) ;
770 }
771 }
772 Refresh();
773 }
774
775 return TRUE;
776 }
777
778 wxMenu *wxMenuBar::Remove(size_t pos)
779 {
780 wxMenu *menu = wxMenuBarBase::Remove(pos);
781 if ( !menu )
782 return NULL;
783
784 if ( IsAttached() )
785 {
786 if (s_macInstalledMenuBar == this)
787 {
788 ::DeleteMenu( menu->MacGetMenuId() /* m_menus[pos]->MacGetMenuId() */ ) ;
789 }
790
791 Refresh();
792 }
793
794 m_titles.RemoveAt(pos);
795
796 return menu;
797 }
798
799 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
800 {
801 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
802 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
803
804 if ( !wxMenuBarBase::Append(menu, title) )
805 return FALSE;
806
807 m_titles.Add(title);
808
809 UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , title , m_font.GetEncoding() ) ;
810
811 if ( IsAttached() )
812 {
813 if (s_macInstalledMenuBar == this)
814 {
815 ::InsertMenu( MAC_WXHMENU(menu->GetHMenu()) , 0 ) ;
816 }
817
818 Refresh();
819 }
820
821 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
822 // adding menu later on.
823 if (m_invokingWindow)
824 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
825
826 return TRUE;
827 }
828
829 static void wxMenubarUnsetInvokingWindow( wxMenu *menu )
830 {
831 menu->SetInvokingWindow( (wxWindow*) NULL );
832
833 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
834 while (node)
835 {
836 wxMenuItem *menuitem = node->GetData();
837 if (menuitem->IsSubMenu())
838 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu() );
839 node = node->GetNext();
840 }
841 }
842
843 static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
844 {
845 menu->SetInvokingWindow( win );
846
847 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
848 while (node)
849 {
850 wxMenuItem *menuitem = node->GetData();
851 if (menuitem->IsSubMenu())
852 wxMenubarSetInvokingWindow( menuitem->GetSubMenu() , win );
853 node = node->GetNext();
854 }
855 }
856
857 void wxMenuBar::UnsetInvokingWindow()
858 {
859 m_invokingWindow = (wxWindow*) NULL;
860 wxMenuList::Node *node = m_menus.GetFirst();
861 while (node)
862 {
863 wxMenu *menu = node->GetData();
864 wxMenubarUnsetInvokingWindow( menu );
865 node = node->GetNext();
866 }
867 }
868
869 void wxMenuBar::SetInvokingWindow(wxFrame *frame)
870 {
871 m_invokingWindow = frame;
872 wxMenuList::Node *node = m_menus.GetFirst();
873 while (node)
874 {
875 wxMenu *menu = node->GetData();
876 wxMenubarSetInvokingWindow( menu, frame );
877 node = node->GetNext();
878 }
879 }
880
881 void wxMenuBar::Detach()
882 {
883 wxMenuBarBase::Detach() ;
884 }
885
886 void wxMenuBar::Attach(wxFrame *frame)
887 {
888 wxMenuBarBase::Attach( frame ) ;
889 }
890 // ---------------------------------------------------------------------------
891 // wxMenuBar searching for menu items
892 // ---------------------------------------------------------------------------
893
894 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
895 int wxMenuBar::FindMenuItem(const wxString& menuString,
896 const wxString& itemString) const
897 {
898 wxString menuLabel = wxStripMenuCodes(menuString);
899 size_t count = GetMenuCount();
900 for ( size_t i = 0; i < count; i++ )
901 {
902 wxString title = wxStripMenuCodes(m_titles[i]);
903 if ( menuLabel == title )
904 return m_menus[i]->FindItem(itemString);
905 }
906
907 return wxNOT_FOUND;
908 }
909
910 wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const
911 {
912 if ( itemMenu )
913 *itemMenu = NULL;
914
915 wxMenuItem *item = NULL;
916 size_t count = GetMenuCount();
917 for ( size_t i = 0; !item && (i < count); i++ )
918 {
919 item = m_menus[i]->FindItem(id, itemMenu);
920 }
921
922 return item;
923 }
924
925