]> git.saurik.com Git - wxWidgets.git/blame - src/osx/menu_osx.cpp
Use best controller size in wxBookCtrl best size calculation.
[wxWidgets.git] / src / osx / menu_osx.cpp
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/menu_osx.cpp
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
b5b208a1 7// RCS-ID: $Id$
524c47aa
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16// wxWidgets headers
17// -----------------
18
19#include "wx/wxprec.h"
20
11fed901
SC
21#if wxUSE_MENUS
22
524c47aa
SC
23#include "wx/menu.h"
24
25#ifndef WX_PRECOMP
26 #include "wx/log.h"
27 #include "wx/app.h"
28 #include "wx/utils.h"
29 #include "wx/frame.h"
42a81643 30 #include "wx/dialog.h"
524c47aa
SC
31 #include "wx/menuitem.h"
32#endif
33
34#include "wx/osx/private.h"
35
36// other standard headers
37// ----------------------
38#include <string.h>
39
40IMPLEMENT_ABSTRACT_CLASS( wxMenuImpl , wxObject )
41
42wxMenuImpl::~wxMenuImpl()
43{
44}
45
c1ec7ee8 46// the (popup) menu title has this special menuid
524c47aa
SC
47static const int idMenuTitle = -3;
48
49// ============================================================================
50// implementation
51// ============================================================================
524c47aa
SC
52
53// Menus
54
55// Construct a menu with optional title (then use append)
56
524c47aa
SC
57void wxMenu::Init()
58{
59 m_doBreak = false;
60 m_startRadioGroup = -1;
61 m_allowRearrange = true;
62 m_noEventsMode = false;
03647350 63
524c47aa
SC
64 m_peer = wxMenuImpl::Create( this, wxStripMenuCodes(m_title) );
65
66
67 // if we have a title, insert it in the beginning of the menu
68 if ( !m_title.empty() )
69 {
70 Append(idMenuTitle, m_title) ;
71 AppendSeparator() ;
72 }
73}
74
75wxMenu::~wxMenu()
76{
77 delete m_peer;
78}
79
80WXHMENU wxMenu::GetHMenu() const
81{
82 if ( m_peer )
83 return m_peer->GetHMenu();
84 return NULL;
85}
86
87void wxMenu::Break()
88{
89 // not available on the mac platform
90}
91
92void wxMenu::Attach(wxMenuBarBase *menubar)
93{
94 wxMenuBase::Attach(menubar);
95
96 EndRadioGroup();
97}
98
99void wxMenu::SetAllowRearrange( bool allow )
100{
101 m_allowRearrange = allow;
102}
103
104void wxMenu::SetNoEventsMode( bool noEvents )
105{
106 m_noEventsMode = noEvents;
107}
108
109// function appends a new item or submenu to the menu
110// append a new item or submenu to the menu
111bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
112{
113 wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") );
22756322 114 GetPeer()->InsertOrAppend( pItem, pos );
524c47aa
SC
115
116 if ( pItem->IsSeparator() )
117 {
118 // nothing to do here
119 }
120 else
121 {
122 wxMenu *pSubMenu = pItem->GetSubMenu() ;
123 if ( pSubMenu != NULL )
124 {
125 wxASSERT_MSG( pSubMenu->GetHMenu() != NULL , wxT("invalid submenu added"));
126 pSubMenu->m_menuParent = this ;
127
128 pSubMenu->DoRearrange();
129 }
130 else
03647350 131 {
524c47aa
SC
132 if ( pItem->GetId() == idMenuTitle )
133 pItem->GetMenu()->Enable( idMenuTitle, false );
134 }
135 }
136
137 // if we're already attached to the menubar, we must update it
138 if ( IsAttached() && GetMenuBar()->IsAttached() )
139 GetMenuBar()->Refresh();
140
141 return true ;
142}
143
144void wxMenu::EndRadioGroup()
145{
146 // we're not inside a radio group any longer
147 m_startRadioGroup = -1;
148}
149
150wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
151{
9a83f860 152 wxCHECK_MSG( item, NULL, wxT("NULL item in wxMenu::DoAppend") );
524c47aa
SC
153
154 bool check = false;
155
156 if ( item->GetKind() == wxITEM_RADIO )
157 {
158 int count = GetMenuItemCount();
159
160 if ( m_startRadioGroup == -1 )
161 {
162 // start a new radio group
163 m_startRadioGroup = count;
164
165 // for now it has just one element
166 item->SetAsRadioGroupStart();
167 item->SetRadioGroupEnd(m_startRadioGroup);
168
169 // ensure that we have a checked item in the radio group
170 check = true;
171 }
172 else // extend the current radio group
173 {
174 // we need to update its end item
175 item->SetRadioGroupStart(m_startRadioGroup);
176 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
177
178 if ( node )
179 {
180 node->GetData()->SetRadioGroupEnd(count);
181 }
182 else
183 {
9a83f860 184 wxFAIL_MSG( wxT("where is the radio group start item?") );
524c47aa
SC
185 }
186 }
187 }
188 else // not a radio item
189 {
190 EndRadioGroup();
191 }
192
193 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
194 return NULL;
195
196 if ( check )
197 // check the item initially
198 item->Check(true);
199
200 return item;
201}
202
203wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
204{
205 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
206 return item;
207
208 return NULL;
209}
210
211wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
212{
09fbcf2a
VZ
213 if ( m_startRadioGroup != -1 )
214 {
215 // Check if we're removing the item starting the radio group
216 if ( GetMenuItems().Item(m_startRadioGroup)->GetData() == item )
217 {
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;
221 }
222 }
223
524c47aa
SC
224/*
225 // we need to find the items position in the child list
226 size_t pos;
227 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
228
229 for ( pos = 0; node; pos++ )
230 {
231 if ( node->GetData() == item )
232 break;
233
234 node = node->GetNext();
235 }
236
237 // DoRemove() (unlike Remove) can only be called for existing item!
238 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
239
240 wxOSXMenuRemoveItem(m_hMenu , pos );
241 */
22756322 242 GetPeer()->Remove( item );
524c47aa
SC
243 // and from internal data structures
244 return wxMenuBase::DoRemove(item);
245}
246
247void wxMenu::SetTitle(const wxString& label)
248{
249 m_title = label ;
22756322 250 GetPeer()->SetTitle( wxStripMenuCodes( label ) );
524c47aa
SC
251}
252
253bool wxMenu::ProcessCommand(wxCommandEvent & event)
254{
255 bool processed = false;
256
257 // Try the menu's event handler
258 if ( /* !processed && */ GetEventHandler())
259 processed = GetEventHandler()->SafelyProcessEvent(event);
260
261 // Try the window the menu was popped up from
262 // (and up through the hierarchy)
6a57bd93 263 wxWindow *win = GetWindow();
524c47aa
SC
264 if ( !processed && win )
265 processed = win->HandleWindowEvent(event);
266
267 return processed;
268}
269
270// ---------------------------------------------------------------------------
271// other
272// ---------------------------------------------------------------------------
273
524c47aa
SC
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
277void wxMenu::DoRearrange()
278{
279 if ( !AllowRearrange() )
280 return;
03647350 281
524c47aa
SC
282 wxMenuItem* previousItem = NULL ;
283 size_t pos ;
284 wxMenuItemList::compatibility_iterator node;
285 wxMenuItem *item;
286
287 for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++)
288 {
289 item = (wxMenuItem *)node->GetData();
290 wxMenu* subMenu = item->GetSubMenu() ;
291 if (subMenu)
292 {
293 // already done
294 }
295 else // normal item
296 {
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 )
303
304 {
305 item->GetPeer()->Hide( true );
306
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
310 // separators
311 wxMenuItemList::compatibility_iterator nextNode = node->GetNext();
312 wxMenuItem *next = nextNode ? nextNode->GetData() : NULL;
313
314 wxMenuItem *sepToHide = 0;
315 if ( !previousItem && next && next->IsSeparator() )
316 {
317 // next (i.e. second as we must be first) item is
318 // the separator to hide
9a83f860 319 wxASSERT_MSG( pos == 0, wxT("should be the menu start") );
524c47aa
SC
320 sepToHide = next;
321 }
322 else if ( GetMenuItems().GetCount() == pos + 1 &&
323 previousItem != NULL &&
324 previousItem->IsSeparator() )
325 {
326 // prev item is a trailing separator we want to hide
327 sepToHide = previousItem;
328 }
329 else if ( previousItem && previousItem->IsSeparator() &&
330 next && next->IsSeparator() )
331 {
332 // two consecutive separators, this is one too many
333 sepToHide = next;
334 }
335
336 if ( sepToHide )
337 {
338 // hide the separator as well
339 sepToHide->GetPeer()->Hide( true );
340 }
341 }
342 }
343
344 previousItem = item ;
345 }
346}
347
348
349bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow )
350{
c1ec7ee8
SC
351 int menuid = item ? item->GetId() : 0;
352 wxUpdateUIEvent event(menuid);
524c47aa
SC
353 event.SetEventObject( this );
354
355 bool processed = false;
356
357 // Try the menu's event handler
358 {
359 wxEvtHandler *handler = GetEventHandler();
360 if ( handler )
361 processed = handler->ProcessEvent(event);
362 }
363
364 // Try the window the menu was popped up from
365 // (and up through the hierarchy)
366 if ( !processed )
367 {
6a57bd93
VZ
368 wxWindow *win = GetWindow();
369 if ( win )
370 processed = win->HandleWindowEvent(event);
524c47aa
SC
371 }
372
373 if ( !processed && senderWindow != NULL)
374 {
375 processed = senderWindow->HandleWindowEvent(event);
376 }
377
378 if ( processed )
379 {
380 // if anything changed, update the changed attribute
381 if (event.GetSetText())
c1ec7ee8 382 SetLabel(menuid, event.GetText());
524c47aa 383 if (event.GetSetChecked())
c1ec7ee8 384 Check(menuid, event.GetChecked());
524c47aa 385 if (event.GetSetEnabled())
c1ec7ee8 386 Enable(menuid, event.GetEnabled());
524c47aa 387 }
6a57bd93 388 else
eb68a54a
SC
389 {
390#if wxOSX_USE_CARBON
391 // these two items are also managed by the Carbon Menu Manager, therefore we must
392 // always reset them ourselves
393 UInt32 cmd = 0;
6a57bd93 394
c1ec7ee8 395 if ( menuid == wxApp::s_macExitMenuItemId )
eb68a54a
SC
396 {
397 cmd = kHICommandQuit;
398 }
c1ec7ee8 399 else if (menuid == wxApp::s_macPreferencesMenuItemId )
eb68a54a
SC
400 {
401 cmd = kHICommandPreferences;
402 }
6a57bd93 403
eb68a54a
SC
404 if ( cmd != 0 )
405 {
406 if ( !item->IsEnabled() || wxDialog::OSXHasModalDialogsOpen() )
407 DisableMenuCommand( NULL , cmd ) ;
408 else
409 EnableMenuCommand( NULL , cmd ) ;
6a57bd93 410
eb68a54a
SC
411 }
412#endif
413 }
414
524c47aa
SC
415 return processed;
416}
417
418bool wxMenu::HandleCommandProcess( wxMenuItem* item, wxWindow* senderWindow )
419{
c1ec7ee8 420 int menuid = item ? item->GetId() : 0;
524c47aa
SC
421 bool processed = false;
422 if (item->IsCheckable())
423 item->Check( !item->IsChecked() ) ;
424
c1ec7ee8 425 if ( SendEvent( menuid , item->IsCheckable() ? item->IsChecked() : -1 ) )
524c47aa
SC
426 processed = true ;
427 else
428 {
429 if ( senderWindow != NULL )
430 {
c1ec7ee8 431 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , menuid);
524c47aa
SC
432 event.SetEventObject(senderWindow);
433 event.SetInt(item->IsCheckable() ? item->IsChecked() : -1);
434
435 if ( senderWindow->HandleWindowEvent(event) )
436 processed = true ;
437 }
438 }
c46d0503
SC
439
440 if(!processed && item)
441 {
442 processed = item->GetPeer()->DoDefault();
443 }
444
524c47aa
SC
445 return processed;
446}
447
448void wxMenu::HandleMenuItemHighlighted( wxMenuItem* item )
449{
c1ec7ee8
SC
450 int menuid = item ? item->GetId() : 0;
451 wxMenuEvent wxevent(wxEVT_MENU_HIGHLIGHT, menuid, this);
524c47aa
SC
452 DoHandleMenuEvent( wxevent );
453}
454
7f3f059a 455void wxMenu::DoHandleMenuOpenedOrClosed(wxEventType evtType)
524c47aa 456{
7f3f059a
VZ
457 // Popup menu being currently shown or NULL, defined in wincmn.cpp.
458 extern wxMenu *wxCurrentPopupMenu;
459
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);
524c47aa
SC
463 DoHandleMenuEvent( wxevent );
464}
465
7f3f059a
VZ
466void wxMenu::HandleMenuOpened()
467{
468 DoHandleMenuOpenedOrClosed(wxEVT_MENU_OPEN);
469}
470
524c47aa
SC
471void wxMenu::HandleMenuClosed()
472{
7f3f059a 473 DoHandleMenuOpenedOrClosed(wxEVT_MENU_CLOSE);
524c47aa
SC
474}
475
476bool wxMenu::DoHandleMenuEvent(wxEvent& wxevent)
477{
478 wxevent.SetEventObject(this);
479 wxEvtHandler* handler = GetEventHandler();
480 if (handler && handler->ProcessEvent(wxevent))
481 {
482 return true;
483 }
484 else
485 {
6a57bd93 486 wxWindow *win = GetWindow();
524c47aa
SC
487 if (win)
488 {
489 if ( win->HandleWindowEvent(wxevent) )
490 return true;
491 }
492 }
493 return false;
494}
495
496// Menu Bar
497
498/*
499
500Mac Implementation note :
501
502The Mac has only one global menubar, so we attempt to install the currently
503active menubar from a frame, we currently don't take into account mdi-frames
504which would ask for menu-merging
505
506Secondly there is no mac api for changing a menubar that is not the current
507menubar, so we have to wait for preparing the actual menubar until the
508wxMenubar is to be used
509
510We can in subsequent versions use MacInstallMenuBar to provide some sort of
511auto-merge for MDI in case this will be necessary
512
513*/
514
515wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ;
516wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ;
517bool wxMenuBar::s_macAutoWindowMenu = true ;
518WXHMENU wxMenuBar::s_macWindowMenuHandle = NULL ;
519
8ceae028
SC
520const int firstMenuPos = 1; // to account for the 0th application menu on mac
521
524c47aa
SC
522void wxMenuBar::Init()
523{
524 m_eventHandler = this;
525 m_menuBarFrame = NULL;
524c47aa 526 m_rootMenu = new wxMenu();
6a57bd93
VZ
527 m_rootMenu->Attach(this);
528
1ea5ef01
SC
529 m_appleMenu = new wxMenu();
530 m_appleMenu->SetAllowRearrange(false);
4f1b95ea
VZ
531
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 )
536 {
c46d0503 537 wxString aboutLabel(_("About"));
c8bef6e3
VZ
538 if ( wxTheApp )
539 aboutLabel << ' ' << wxTheApp->GetAppDisplayName();
540 else
541 aboutLabel << "...";
542 m_appleMenu->Append( wxApp::s_macAboutMenuItemId, aboutLabel);
4f1b95ea
VZ
543 m_appleMenu->AppendSeparator();
544 }
545
1ea5ef01 546#if !wxOSX_USE_CARBON
4f1b95ea
VZ
547 if ( wxApp::s_macPreferencesMenuItemId != wxID_NONE )
548 {
86646f2a 549 m_appleMenu->Append( wxApp::s_macPreferencesMenuItemId,
c46d0503 550 _("Preferences...") + "\tCtrl+," );
4f1b95ea
VZ
551 m_appleMenu->AppendSeparator();
552 }
553
c46d0503
SC
554 // standard menu items, handled in wxMenu::HandleCommandProcess(), see above:
555 wxString hideLabel(_("Hide"));
556 if ( wxTheApp )
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();
563
4f1b95ea 564 // Do always add "Quit" item unconditionally however, it can't be disabled.
c46d0503
SC
565 wxString quitLabel(_("Quit"));
566 if ( wxTheApp )
567 quitLabel << ' ' << wxTheApp->GetAppDisplayName();
568 quitLabel << "\tCtrl+Q";
569 m_appleMenu->Append( wxApp::s_macExitMenuItemId, quitLabel );
4f1b95ea 570#endif // !wxOSX_USE_CARBON
524c47aa 571
1ea5ef01 572 m_rootMenu->AppendSubMenu(m_appleMenu, "\x14") ;
524c47aa
SC
573}
574
575wxMenuBar::wxMenuBar()
576{
577 Init();
578}
579
580wxMenuBar::wxMenuBar( long WXUNUSED(style) )
581{
582 Init();
583}
584
585wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
586{
587 Init();
588
524c47aa
SC
589 for ( size_t i = 0; i < count; i++ )
590 {
591 m_menus.Append(menus[i]);
524c47aa
SC
592
593 menus[i]->Attach(this);
594 Append( menus[i], titles[i] );
595 }
596}
597
598wxMenuBar::~wxMenuBar()
599{
600 if (s_macCommonMenuBar == this)
601 s_macCommonMenuBar = NULL;
602
603 if (s_macInstalledMenuBar == this)
604 {
605 s_macInstalledMenuBar = NULL;
606 }
607}
608
609void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect))
610{
611 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
612}
613
614void wxMenuBar::MacInstallMenuBar()
615{
616 if ( s_macInstalledMenuBar == this )
617 return ;
03647350 618
524c47aa 619 m_rootMenu->GetPeer()->MakeRoot();
cc5fe8d4
SC
620
621 // hide items in the apple menu that don't exist in the wx menubar
622
c1ec7ee8 623 int menuid = 0;
cc5fe8d4 624 wxMenuItem* appleItem = NULL;
aa3e3741 625 wxMenuItem* wxItem = NULL;
eb68a54a 626
c1ec7ee8
SC
627 menuid = wxApp::s_macAboutMenuItemId;
628 appleItem = m_appleMenu->FindItem(menuid);
629 wxItem = FindItem(menuid);
cc5fe8d4 630 if ( appleItem != NULL )
aa3e3741
SC
631 {
632 if ( wxItem == NULL )
633 appleItem->GetPeer()->Hide();
634 else
635 appleItem->SetItemLabel(wxItem->GetItemLabel());
636 }
cc5fe8d4 637
c1ec7ee8
SC
638 menuid = wxApp::s_macPreferencesMenuItemId;
639 appleItem = m_appleMenu->FindItem(menuid);
640 wxItem = FindItem(menuid);
cc5fe8d4 641 if ( appleItem != NULL )
aa3e3741
SC
642 {
643 if ( wxItem == NULL )
644 appleItem->GetPeer()->Hide();
645 else
646 appleItem->SetItemLabel(wxItem->GetItemLabel());
647 }
cc5fe8d4
SC
648
649
524c47aa
SC
650#if 0
651
524c47aa
SC
652 // if we have a mac help menu, clean it up before adding new items
653 MenuHandle helpMenuHandle ;
654 MenuItemIndex firstUserHelpMenuItem ;
655
656 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle , &firstUserHelpMenuItem) == noErr )
657 {
658 for ( int i = CountMenuItems( helpMenuHandle ) ; i >= firstUserHelpMenuItem ; --i )
659 DeleteMenuItem( helpMenuHandle , i ) ;
660 }
661 else
662 {
663 helpMenuHandle = NULL ;
664 }
665
666 if ( wxApp::s_macPreferencesMenuItemId)
667 {
668 wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ;
669 if ( item == NULL || !(item->IsEnabled()) )
670 DisableMenuCommand( NULL , kHICommandPreferences ) ;
671 else
672 EnableMenuCommand( NULL , kHICommandPreferences ) ;
673 }
674
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)
680 {
681 wxMenuItem *item = FindItem( wxApp::s_macExitMenuItemId , NULL ) ;
682 if ( item != NULL && !(item->IsEnabled()) )
683 DisableMenuCommand( NULL , kHICommandQuit ) ;
684 else
685 EnableMenuCommand( NULL , kHICommandQuit ) ;
686 }
687
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())
692 {
693 wxMenuItemList::compatibility_iterator node;
694 wxMenuItem *item;
695 wxMenu* menu = menuIter->GetData() , *subMenu = NULL ;
696 wxString strippedMenuTitle = wxStripMenuCodes(m_titles[i]);
697
698 if ( strippedMenuTitle == wxT("?") || strippedMenuTitle == strippedHelpMenuTitle || strippedMenuTitle == strippedTranslatedHelpMenuTitle )
699 {
700 for (node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext())
701 {
702 item = (wxMenuItem *)node->GetData();
703 subMenu = item->GetSubMenu() ;
704 if (subMenu)
705 {
e8fdf364
JS
706 UMAAppendMenuItem(mh, wxStripMenuCodes(item->GetText()) , wxFont::GetDefaultEncoding() );
707 MenuItemIndex position = CountMenuItems(mh);
708 ::SetMenuItemHierarchicalMenu(mh, position, MAC_WXHMENU(subMenu->GetHMenu()));
524c47aa
SC
709 }
710 else
711 {
712 if ( item->GetId() != wxApp::s_macAboutMenuItemId )
713 {
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 )
717 {
718 if ( UMAGetHelpMenu( &helpMenuHandle , &firstUserHelpMenuItem) != noErr )
719 {
720 helpMenuHandle = NULL ;
721 break ;
722 }
723 }
724 }
725
726 if ( item->IsSeparator() )
727 {
728 if ( helpMenuHandle )
729 AppendMenuItemTextWithCFString( helpMenuHandle,
730 CFSTR(""), kMenuItemAttrSeparator, 0,NULL);
731 }
732 else
733 {
734 wxAcceleratorEntry*
735 entry = wxAcceleratorEntry::Create( item->GetItemLabel() ) ;
736
737 if ( item->GetId() == wxApp::s_macAboutMenuItemId )
738 {
739 // this will be taken care of below
740 }
741 else
742 {
743 if ( helpMenuHandle )
744 {
745 UMAAppendMenuItem(helpMenuHandle, wxStripMenuCodes(item->GetItemLabel()) , wxFont::GetDefaultEncoding(), entry);
746 SetMenuItemCommandID( helpMenuHandle , CountMenuItems(helpMenuHandle) , wxIdToMacCommand ( item->GetId() ) ) ;
747 SetMenuItemRefCon( helpMenuHandle , CountMenuItems(helpMenuHandle) , (URefCon) item ) ;
748 }
749 }
750
751 delete entry ;
752 }
753 }
754 }
755 }
756
757 else if ( ( m_titles[i] == wxT("Window") || m_titles[i] == wxT("&Window") )
758 && GetAutoWindowMenu() )
759 {
760 if ( MacGetWindowMenuHMenu() == NULL )
761 {
762 CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ;
763 }
764
765 MenuRef wm = (MenuRef)MacGetWindowMenuHMenu();
766 if ( wm == NULL )
767 break;
768
769 // get the insertion point in the standard menu
770 MenuItemIndex winListStart;
771 GetIndMenuItemWithCommandID(wm,
772 kHICommandWindowListSeparator, 1, NULL, &winListStart);
773
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);
778
779 if ( err == menuItemNotFoundErr )
780 {
781 InsertMenuItemTextWithCFString( wm,
782 CFSTR(""), winListStart-1, kMenuItemAttrSeparator, 'WXWM');
783 }
784
785 wxInsertMenuItemsInMenu(menu, wm, winListStart);
786 }
787 else
788 {
789 UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], GetFont().GetEncoding() ) ;
790 menu->MacBeforeDisplay(false) ;
791
8a145966 792 ::InsertMenu(MAC_WXHMENU(GetMenu(i)->GetHMenu()), 0);
524c47aa
SC
793 }
794 }
795
796 // take care of the about menu item wherever it is
797 {
798 wxMenu* aboutMenu ;
799 wxMenuItem *aboutMenuItem = FindItem(wxApp::s_macAboutMenuItemId , &aboutMenu) ;
800 if ( aboutMenuItem )
801 {
802 wxAcceleratorEntry*
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 ) ;
809 delete entry;
810 }
811 }
812
813 if ( GetAutoWindowMenu() )
814 {
815 if ( MacGetWindowMenuHMenu() == NULL )
816 CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ;
817
818 InsertMenu( (MenuHandle) MacGetWindowMenuHMenu() , 0 ) ;
819 }
820
821 ::DrawMenuBar() ;
822#endif
823
824 s_macInstalledMenuBar = this;
825}
826
827void wxMenuBar::EnableTop(size_t pos, bool enable)
828{
829 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
03647350 830
8ceae028 831 m_rootMenu->FindItemByPosition(pos+firstMenuPos)->Enable(enable);
524c47aa
SC
832
833 Refresh();
834}
835
836bool wxMenuBar::Enable(bool enable)
837{
838 wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") );
839
840 size_t i;
841 for (i = 0; i < GetMenuCount(); i++)
842 EnableTop(i, enable);
843
844 return true;
845}
846
847void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
848{
849 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
850
8a145966 851 GetMenu(pos)->SetTitle( label ) ;
524c47aa
SC
852}
853
854wxString wxMenuBar::GetMenuLabel(size_t pos) const
855{
856 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
857 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
858
46405e36 859 return GetMenu(pos)->GetTitle();
524c47aa
SC
860}
861
862// ---------------------------------------------------------------------------
863// wxMenuBar construction
864// ---------------------------------------------------------------------------
865
524c47aa
SC
866wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
867{
868 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
869 if ( !menuOld )
870 return NULL;
871
524c47aa
SC
872 wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos);
873 m_rootMenu->Remove(item);
874 m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) );
875
524c47aa
SC
876 return menuOld;
877}
878
879bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
880{
881 if ( !wxMenuBarBase::Insert(pos, menu, title) )
882 return false;
883
524c47aa
SC
884 m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) );
885
524c47aa
SC
886 return true;
887}
888
889wxMenu *wxMenuBar::Remove(size_t pos)
890{
891 wxMenu *menu = wxMenuBarBase::Remove(pos);
892 if ( !menu )
893 return NULL;
894
895 wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos);
896 m_rootMenu->Remove(item);
897
524c47aa
SC
898 return menu;
899}
900
901bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
902{
903 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
904 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
905
906 if ( !wxMenuBarBase::Append(menu, title) )
907 return false;
908
524c47aa 909 m_rootMenu->AppendSubMenu(menu, title);
46405e36 910 menu->SetTitle(title);
524c47aa 911
524c47aa
SC
912 return true;
913}
914
524c47aa
SC
915void wxMenuBar::Detach()
916{
917 wxMenuBarBase::Detach() ;
918}
919
920void wxMenuBar::Attach(wxFrame *frame)
921{
922 wxMenuBarBase::Attach( frame ) ;
923}
924
46405e36 925#endif // wxUSE_MENUS