]>
Commit | Line | Data |
---|---|---|
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 | ||
40 | IMPLEMENT_ABSTRACT_CLASS( wxMenuImpl , wxObject ) | |
41 | ||
42 | wxMenuImpl::~wxMenuImpl() | |
43 | { | |
44 | } | |
45 | ||
524c47aa SC |
46 | // the (popup) menu title has this special id |
47 | static 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 |
57 | void 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 | ||
75 | wxMenu::~wxMenu() | |
76 | { | |
77 | delete m_peer; | |
78 | } | |
79 | ||
80 | WXHMENU wxMenu::GetHMenu() const | |
81 | { | |
82 | if ( m_peer ) | |
83 | return m_peer->GetHMenu(); | |
84 | return NULL; | |
85 | } | |
86 | ||
87 | void wxMenu::Break() | |
88 | { | |
89 | // not available on the mac platform | |
90 | } | |
91 | ||
92 | void wxMenu::Attach(wxMenuBarBase *menubar) | |
93 | { | |
94 | wxMenuBase::Attach(menubar); | |
95 | ||
96 | EndRadioGroup(); | |
97 | } | |
98 | ||
99 | void wxMenu::SetAllowRearrange( bool allow ) | |
100 | { | |
101 | m_allowRearrange = allow; | |
102 | } | |
103 | ||
104 | void 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 | |
111 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
112 | { | |
113 | wxASSERT_MSG( pItem != NULL, wxT("can't append NULL item to the menu") ); | |
114 | m_peer->InsertOrAppend( pItem, pos ); | |
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 | ||
144 | void wxMenu::EndRadioGroup() | |
145 | { | |
146 | // we're not inside a radio group any longer | |
147 | m_startRadioGroup = -1; | |
148 | } | |
149 | ||
150 | wxMenuItem* 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 | ||
203 | wxMenuItem* 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 | ||
211 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
212 | { | |
213 | /* | |
214 | // we need to find the items position in the child list | |
215 | size_t pos; | |
216 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
217 | ||
218 | for ( pos = 0; node; pos++ ) | |
219 | { | |
220 | if ( node->GetData() == item ) | |
221 | break; | |
222 | ||
223 | node = node->GetNext(); | |
224 | } | |
225 | ||
226 | // DoRemove() (unlike Remove) can only be called for existing item! | |
227 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
228 | ||
229 | wxOSXMenuRemoveItem(m_hMenu , pos ); | |
230 | */ | |
231 | m_peer->Remove( item ); | |
232 | // and from internal data structures | |
233 | return wxMenuBase::DoRemove(item); | |
234 | } | |
235 | ||
236 | void wxMenu::SetTitle(const wxString& label) | |
237 | { | |
238 | m_title = label ; | |
239 | m_peer->SetTitle( wxStripMenuCodes( label ) ); | |
240 | } | |
241 | ||
242 | bool wxMenu::ProcessCommand(wxCommandEvent & event) | |
243 | { | |
244 | bool processed = false; | |
245 | ||
246 | // Try the menu's event handler | |
247 | if ( /* !processed && */ GetEventHandler()) | |
248 | processed = GetEventHandler()->SafelyProcessEvent(event); | |
249 | ||
250 | // Try the window the menu was popped up from | |
251 | // (and up through the hierarchy) | |
6a57bd93 | 252 | wxWindow *win = GetWindow(); |
524c47aa SC |
253 | if ( !processed && win ) |
254 | processed = win->HandleWindowEvent(event); | |
255 | ||
256 | return processed; | |
257 | } | |
258 | ||
259 | // --------------------------------------------------------------------------- | |
260 | // other | |
261 | // --------------------------------------------------------------------------- | |
262 | ||
524c47aa SC |
263 | // MacOS needs to know about submenus somewhere within this menu |
264 | // before it can be displayed, also hide special menu items | |
265 | // like preferences that are handled by the OS | |
266 | void wxMenu::DoRearrange() | |
267 | { | |
268 | if ( !AllowRearrange() ) | |
269 | return; | |
03647350 | 270 | |
524c47aa SC |
271 | wxMenuItem* previousItem = NULL ; |
272 | size_t pos ; | |
273 | wxMenuItemList::compatibility_iterator node; | |
274 | wxMenuItem *item; | |
275 | ||
276 | for (pos = 0, node = GetMenuItems().GetFirst(); node; node = node->GetNext(), pos++) | |
277 | { | |
278 | item = (wxMenuItem *)node->GetData(); | |
279 | wxMenu* subMenu = item->GetSubMenu() ; | |
280 | if (subMenu) | |
281 | { | |
282 | // already done | |
283 | } | |
284 | else // normal item | |
285 | { | |
286 | // what we do here is to hide the special items which are | |
287 | // shown in the application menu anyhow -- it doesn't make | |
288 | // sense to show them in their normal place as well | |
289 | if ( item->GetId() == wxApp::s_macAboutMenuItemId || | |
290 | item->GetId() == wxApp::s_macPreferencesMenuItemId || | |
291 | item->GetId() == wxApp::s_macExitMenuItemId ) | |
292 | ||
293 | { | |
294 | item->GetPeer()->Hide( true ); | |
295 | ||
296 | // also check for a separator which was used just to | |
297 | // separate this item from the others, so don't leave | |
298 | // separator at the menu start or end nor 2 consecutive | |
299 | // separators | |
300 | wxMenuItemList::compatibility_iterator nextNode = node->GetNext(); | |
301 | wxMenuItem *next = nextNode ? nextNode->GetData() : NULL; | |
302 | ||
303 | wxMenuItem *sepToHide = 0; | |
304 | if ( !previousItem && next && next->IsSeparator() ) | |
305 | { | |
306 | // next (i.e. second as we must be first) item is | |
307 | // the separator to hide | |
9a83f860 | 308 | wxASSERT_MSG( pos == 0, wxT("should be the menu start") ); |
524c47aa SC |
309 | sepToHide = next; |
310 | } | |
311 | else if ( GetMenuItems().GetCount() == pos + 1 && | |
312 | previousItem != NULL && | |
313 | previousItem->IsSeparator() ) | |
314 | { | |
315 | // prev item is a trailing separator we want to hide | |
316 | sepToHide = previousItem; | |
317 | } | |
318 | else if ( previousItem && previousItem->IsSeparator() && | |
319 | next && next->IsSeparator() ) | |
320 | { | |
321 | // two consecutive separators, this is one too many | |
322 | sepToHide = next; | |
323 | } | |
324 | ||
325 | if ( sepToHide ) | |
326 | { | |
327 | // hide the separator as well | |
328 | sepToHide->GetPeer()->Hide( true ); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | previousItem = item ; | |
334 | } | |
335 | } | |
336 | ||
337 | ||
338 | bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item, wxWindow* senderWindow ) | |
339 | { | |
340 | int id = item ? item->GetId() : 0; | |
341 | wxUpdateUIEvent event(id); | |
342 | event.SetEventObject( this ); | |
343 | ||
344 | bool processed = false; | |
345 | ||
346 | // Try the menu's event handler | |
347 | { | |
348 | wxEvtHandler *handler = GetEventHandler(); | |
349 | if ( handler ) | |
350 | processed = handler->ProcessEvent(event); | |
351 | } | |
352 | ||
353 | // Try the window the menu was popped up from | |
354 | // (and up through the hierarchy) | |
355 | if ( !processed ) | |
356 | { | |
6a57bd93 VZ |
357 | wxWindow *win = GetWindow(); |
358 | if ( win ) | |
359 | processed = win->HandleWindowEvent(event); | |
524c47aa SC |
360 | } |
361 | ||
362 | if ( !processed && senderWindow != NULL) | |
363 | { | |
364 | processed = senderWindow->HandleWindowEvent(event); | |
365 | } | |
366 | ||
367 | if ( processed ) | |
368 | { | |
369 | // if anything changed, update the changed attribute | |
370 | if (event.GetSetText()) | |
371 | SetLabel(id, event.GetText()); | |
372 | if (event.GetSetChecked()) | |
373 | Check(id, event.GetChecked()); | |
374 | if (event.GetSetEnabled()) | |
375 | Enable(id, event.GetEnabled()); | |
376 | } | |
6a57bd93 | 377 | else |
eb68a54a SC |
378 | { |
379 | #if wxOSX_USE_CARBON | |
380 | // these two items are also managed by the Carbon Menu Manager, therefore we must | |
381 | // always reset them ourselves | |
382 | UInt32 cmd = 0; | |
6a57bd93 | 383 | |
eb68a54a SC |
384 | if ( id == wxApp::s_macExitMenuItemId ) |
385 | { | |
386 | cmd = kHICommandQuit; | |
387 | } | |
388 | else if (id == wxApp::s_macPreferencesMenuItemId ) | |
389 | { | |
390 | cmd = kHICommandPreferences; | |
391 | } | |
6a57bd93 | 392 | |
eb68a54a SC |
393 | if ( cmd != 0 ) |
394 | { | |
395 | if ( !item->IsEnabled() || wxDialog::OSXHasModalDialogsOpen() ) | |
396 | DisableMenuCommand( NULL , cmd ) ; | |
397 | else | |
398 | EnableMenuCommand( NULL , cmd ) ; | |
6a57bd93 | 399 | |
eb68a54a SC |
400 | } |
401 | #endif | |
402 | } | |
403 | ||
524c47aa SC |
404 | return processed; |
405 | } | |
406 | ||
407 | bool wxMenu::HandleCommandProcess( wxMenuItem* item, wxWindow* senderWindow ) | |
408 | { | |
409 | int id = item ? item->GetId() : 0; | |
410 | bool processed = false; | |
411 | if (item->IsCheckable()) | |
412 | item->Check( !item->IsChecked() ) ; | |
413 | ||
414 | if ( SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ) | |
415 | processed = true ; | |
416 | else | |
417 | { | |
418 | if ( senderWindow != NULL ) | |
419 | { | |
420 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED , id); | |
421 | event.SetEventObject(senderWindow); | |
422 | event.SetInt(item->IsCheckable() ? item->IsChecked() : -1); | |
423 | ||
424 | if ( senderWindow->HandleWindowEvent(event) ) | |
425 | processed = true ; | |
426 | } | |
427 | } | |
c46d0503 SC |
428 | |
429 | if(!processed && item) | |
430 | { | |
431 | processed = item->GetPeer()->DoDefault(); | |
432 | } | |
433 | ||
524c47aa SC |
434 | return processed; |
435 | } | |
436 | ||
437 | void wxMenu::HandleMenuItemHighlighted( wxMenuItem* item ) | |
438 | { | |
439 | int id = item ? item->GetId() : 0; | |
440 | wxMenuEvent wxevent(wxEVT_MENU_HIGHLIGHT, id, this); | |
441 | DoHandleMenuEvent( wxevent ); | |
442 | } | |
443 | ||
444 | void wxMenu::HandleMenuOpened() | |
445 | { | |
446 | wxMenuEvent wxevent(wxEVT_MENU_OPEN, 0, this); | |
447 | DoHandleMenuEvent( wxevent ); | |
448 | } | |
449 | ||
450 | void wxMenu::HandleMenuClosed() | |
451 | { | |
452 | wxMenuEvent wxevent(wxEVT_MENU_CLOSE, 0, this); | |
453 | DoHandleMenuEvent( wxevent ); | |
454 | } | |
455 | ||
456 | bool wxMenu::DoHandleMenuEvent(wxEvent& wxevent) | |
457 | { | |
458 | wxevent.SetEventObject(this); | |
459 | wxEvtHandler* handler = GetEventHandler(); | |
460 | if (handler && handler->ProcessEvent(wxevent)) | |
461 | { | |
462 | return true; | |
463 | } | |
464 | else | |
465 | { | |
6a57bd93 | 466 | wxWindow *win = GetWindow(); |
524c47aa SC |
467 | if (win) |
468 | { | |
469 | if ( win->HandleWindowEvent(wxevent) ) | |
470 | return true; | |
471 | } | |
472 | } | |
473 | return false; | |
474 | } | |
475 | ||
476 | // Menu Bar | |
477 | ||
478 | /* | |
479 | ||
480 | Mac Implementation note : | |
481 | ||
482 | The Mac has only one global menubar, so we attempt to install the currently | |
483 | active menubar from a frame, we currently don't take into account mdi-frames | |
484 | which would ask for menu-merging | |
485 | ||
486 | Secondly there is no mac api for changing a menubar that is not the current | |
487 | menubar, so we have to wait for preparing the actual menubar until the | |
488 | wxMenubar is to be used | |
489 | ||
490 | We can in subsequent versions use MacInstallMenuBar to provide some sort of | |
491 | auto-merge for MDI in case this will be necessary | |
492 | ||
493 | */ | |
494 | ||
495 | wxMenuBar* wxMenuBar::s_macInstalledMenuBar = NULL ; | |
496 | wxMenuBar* wxMenuBar::s_macCommonMenuBar = NULL ; | |
497 | bool wxMenuBar::s_macAutoWindowMenu = true ; | |
498 | WXHMENU wxMenuBar::s_macWindowMenuHandle = NULL ; | |
499 | ||
500 | void wxMenuBar::Init() | |
501 | { | |
502 | m_eventHandler = this; | |
503 | m_menuBarFrame = NULL; | |
524c47aa | 504 | m_rootMenu = new wxMenu(); |
6a57bd93 VZ |
505 | m_rootMenu->Attach(this); |
506 | ||
1ea5ef01 SC |
507 | m_appleMenu = new wxMenu(); |
508 | m_appleMenu->SetAllowRearrange(false); | |
4f1b95ea VZ |
509 | |
510 | // Create standard items unless the application explicitly disabled this by | |
511 | // setting the corresponding ids to wxID_NONE: although this is not | |
512 | // recommended, sometimes these items really don't make sense. | |
513 | if ( wxApp::s_macAboutMenuItemId != wxID_NONE ) | |
514 | { | |
c46d0503 | 515 | wxString aboutLabel(_("About")); |
c8bef6e3 VZ |
516 | if ( wxTheApp ) |
517 | aboutLabel << ' ' << wxTheApp->GetAppDisplayName(); | |
518 | else | |
519 | aboutLabel << "..."; | |
520 | m_appleMenu->Append( wxApp::s_macAboutMenuItemId, aboutLabel); | |
4f1b95ea VZ |
521 | m_appleMenu->AppendSeparator(); |
522 | } | |
523 | ||
1ea5ef01 | 524 | #if !wxOSX_USE_CARBON |
4f1b95ea VZ |
525 | if ( wxApp::s_macPreferencesMenuItemId != wxID_NONE ) |
526 | { | |
86646f2a | 527 | m_appleMenu->Append( wxApp::s_macPreferencesMenuItemId, |
c46d0503 | 528 | _("Preferences...") + "\tCtrl+," ); |
4f1b95ea VZ |
529 | m_appleMenu->AppendSeparator(); |
530 | } | |
531 | ||
c46d0503 SC |
532 | // standard menu items, handled in wxMenu::HandleCommandProcess(), see above: |
533 | wxString hideLabel(_("Hide")); | |
534 | if ( wxTheApp ) | |
535 | hideLabel << ' ' << wxTheApp->GetAppDisplayName(); | |
536 | hideLabel << "\tCtrl+H"; | |
537 | m_appleMenu->Append( wxID_OSX_HIDE, hideLabel ); | |
538 | m_appleMenu->Append( wxID_OSX_HIDEOTHERS, _("Hide Others")+"\tAlt+Ctrl+H" ); | |
539 | m_appleMenu->Append( wxID_OSX_SHOWALL, _("Show All") ); | |
540 | m_appleMenu->AppendSeparator(); | |
541 | ||
4f1b95ea | 542 | // Do always add "Quit" item unconditionally however, it can't be disabled. |
c46d0503 SC |
543 | wxString quitLabel(_("Quit")); |
544 | if ( wxTheApp ) | |
545 | quitLabel << ' ' << wxTheApp->GetAppDisplayName(); | |
546 | quitLabel << "\tCtrl+Q"; | |
547 | m_appleMenu->Append( wxApp::s_macExitMenuItemId, quitLabel ); | |
4f1b95ea | 548 | #endif // !wxOSX_USE_CARBON |
524c47aa | 549 | |
1ea5ef01 | 550 | m_rootMenu->AppendSubMenu(m_appleMenu, "\x14") ; |
524c47aa SC |
551 | } |
552 | ||
553 | wxMenuBar::wxMenuBar() | |
554 | { | |
555 | Init(); | |
556 | } | |
557 | ||
558 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
559 | { | |
560 | Init(); | |
561 | } | |
562 | ||
563 | wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style)) | |
564 | { | |
565 | Init(); | |
566 | ||
524c47aa SC |
567 | for ( size_t i = 0; i < count; i++ ) |
568 | { | |
569 | m_menus.Append(menus[i]); | |
524c47aa SC |
570 | |
571 | menus[i]->Attach(this); | |
572 | Append( menus[i], titles[i] ); | |
573 | } | |
574 | } | |
575 | ||
576 | wxMenuBar::~wxMenuBar() | |
577 | { | |
578 | if (s_macCommonMenuBar == this) | |
579 | s_macCommonMenuBar = NULL; | |
580 | ||
581 | if (s_macInstalledMenuBar == this) | |
582 | { | |
583 | s_macInstalledMenuBar = NULL; | |
584 | } | |
585 | } | |
586 | ||
587 | void wxMenuBar::Refresh(bool WXUNUSED(eraseBackground), const wxRect *WXUNUSED(rect)) | |
588 | { | |
589 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
590 | } | |
591 | ||
592 | void wxMenuBar::MacInstallMenuBar() | |
593 | { | |
594 | if ( s_macInstalledMenuBar == this ) | |
595 | return ; | |
03647350 | 596 | |
524c47aa | 597 | m_rootMenu->GetPeer()->MakeRoot(); |
eb68a54a | 598 | |
524c47aa SC |
599 | #if 0 |
600 | ||
601 | MenuBarHandle menubar = NULL ; | |
602 | ||
603 | menubar = NewHandleClear( 6 /* sizeof( MenuBarHeader ) */ ) ; | |
604 | ||
605 | ::SetMenuBar( menubar ) ; | |
606 | DisposeMenuBar( menubar ) ; | |
607 | MenuHandle appleMenu = NULL ; | |
608 | ||
609 | verify_noerr( CreateNewMenu( kwxMacAppleMenuId , 0 , &appleMenu ) ) ; | |
610 | verify_noerr( SetMenuTitleWithCFString( appleMenu , CFSTR( "\x14" ) ) ); | |
611 | ||
612 | // Add About/Preferences separator only on OS X | |
613 | // KH/RN: Separator is always present on 10.3 but not on 10.2 | |
614 | // However, the change from 10.2 to 10.3 suggests it is preferred | |
615 | InsertMenuItemTextWithCFString( appleMenu, | |
616 | CFSTR(""), 0, kMenuItemAttrSeparator, 0); | |
617 | InsertMenuItemTextWithCFString( appleMenu, | |
618 | CFSTR("About..."), 0, 0, 0); | |
619 | MacInsertMenu( appleMenu , 0 ) ; | |
620 | ||
621 | // if we have a mac help menu, clean it up before adding new items | |
622 | MenuHandle helpMenuHandle ; | |
623 | MenuItemIndex firstUserHelpMenuItem ; | |
624 | ||
625 | if ( UMAGetHelpMenuDontCreate( &helpMenuHandle , &firstUserHelpMenuItem) == noErr ) | |
626 | { | |
627 | for ( int i = CountMenuItems( helpMenuHandle ) ; i >= firstUserHelpMenuItem ; --i ) | |
628 | DeleteMenuItem( helpMenuHandle , i ) ; | |
629 | } | |
630 | else | |
631 | { | |
632 | helpMenuHandle = NULL ; | |
633 | } | |
634 | ||
635 | if ( wxApp::s_macPreferencesMenuItemId) | |
636 | { | |
637 | wxMenuItem *item = FindItem( wxApp::s_macPreferencesMenuItemId , NULL ) ; | |
638 | if ( item == NULL || !(item->IsEnabled()) ) | |
639 | DisableMenuCommand( NULL , kHICommandPreferences ) ; | |
640 | else | |
641 | EnableMenuCommand( NULL , kHICommandPreferences ) ; | |
642 | } | |
643 | ||
644 | // Unlike preferences which may or may not exist, the Quit item should be always | |
645 | // enabled unless it is added by the application and then disabled, otherwise | |
646 | // a program would be required to add an item with wxID_EXIT in order to get the | |
647 | // Quit menu item to be enabled, which seems a bit burdensome. | |
648 | if ( wxApp::s_macExitMenuItemId) | |
649 | { | |
650 | wxMenuItem *item = FindItem( wxApp::s_macExitMenuItemId , NULL ) ; | |
651 | if ( item != NULL && !(item->IsEnabled()) ) | |
652 | DisableMenuCommand( NULL , kHICommandQuit ) ; | |
653 | else | |
654 | EnableMenuCommand( NULL , kHICommandQuit ) ; | |
655 | } | |
656 | ||
657 | wxString strippedHelpMenuTitle = wxStripMenuCodes( wxApp::s_macHelpMenuTitleName ) ; | |
658 | wxString strippedTranslatedHelpMenuTitle = wxStripMenuCodes( wxString( _("&Help") ) ) ; | |
659 | wxMenuList::compatibility_iterator menuIter = m_menus.GetFirst(); | |
660 | for (size_t i = 0; i < m_menus.GetCount(); i++, menuIter = menuIter->GetNext()) | |
661 | { | |
662 | wxMenuItemList::compatibility_iterator node; | |
663 | wxMenuItem *item; | |
664 | wxMenu* menu = menuIter->GetData() , *subMenu = NULL ; | |
665 | wxString strippedMenuTitle = wxStripMenuCodes(m_titles[i]); | |
666 | ||
667 | if ( strippedMenuTitle == wxT("?") || strippedMenuTitle == strippedHelpMenuTitle || strippedMenuTitle == strippedTranslatedHelpMenuTitle ) | |
668 | { | |
669 | for (node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext()) | |
670 | { | |
671 | item = (wxMenuItem *)node->GetData(); | |
672 | subMenu = item->GetSubMenu() ; | |
673 | if (subMenu) | |
674 | { | |
e8fdf364 JS |
675 | UMAAppendMenuItem(mh, wxStripMenuCodes(item->GetText()) , wxFont::GetDefaultEncoding() ); |
676 | MenuItemIndex position = CountMenuItems(mh); | |
677 | ::SetMenuItemHierarchicalMenu(mh, position, MAC_WXHMENU(subMenu->GetHMenu())); | |
524c47aa SC |
678 | } |
679 | else | |
680 | { | |
681 | if ( item->GetId() != wxApp::s_macAboutMenuItemId ) | |
682 | { | |
683 | // we have found a user help menu and an item other than the about item, | |
684 | // so we can create the mac help menu now, if we haven't created it yet | |
685 | if ( helpMenuHandle == NULL ) | |
686 | { | |
687 | if ( UMAGetHelpMenu( &helpMenuHandle , &firstUserHelpMenuItem) != noErr ) | |
688 | { | |
689 | helpMenuHandle = NULL ; | |
690 | break ; | |
691 | } | |
692 | } | |
693 | } | |
694 | ||
695 | if ( item->IsSeparator() ) | |
696 | { | |
697 | if ( helpMenuHandle ) | |
698 | AppendMenuItemTextWithCFString( helpMenuHandle, | |
699 | CFSTR(""), kMenuItemAttrSeparator, 0,NULL); | |
700 | } | |
701 | else | |
702 | { | |
703 | wxAcceleratorEntry* | |
704 | entry = wxAcceleratorEntry::Create( item->GetItemLabel() ) ; | |
705 | ||
706 | if ( item->GetId() == wxApp::s_macAboutMenuItemId ) | |
707 | { | |
708 | // this will be taken care of below | |
709 | } | |
710 | else | |
711 | { | |
712 | if ( helpMenuHandle ) | |
713 | { | |
714 | UMAAppendMenuItem(helpMenuHandle, wxStripMenuCodes(item->GetItemLabel()) , wxFont::GetDefaultEncoding(), entry); | |
715 | SetMenuItemCommandID( helpMenuHandle , CountMenuItems(helpMenuHandle) , wxIdToMacCommand ( item->GetId() ) ) ; | |
716 | SetMenuItemRefCon( helpMenuHandle , CountMenuItems(helpMenuHandle) , (URefCon) item ) ; | |
717 | } | |
718 | } | |
719 | ||
720 | delete entry ; | |
721 | } | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | else if ( ( m_titles[i] == wxT("Window") || m_titles[i] == wxT("&Window") ) | |
727 | && GetAutoWindowMenu() ) | |
728 | { | |
729 | if ( MacGetWindowMenuHMenu() == NULL ) | |
730 | { | |
731 | CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ; | |
732 | } | |
733 | ||
734 | MenuRef wm = (MenuRef)MacGetWindowMenuHMenu(); | |
735 | if ( wm == NULL ) | |
736 | break; | |
737 | ||
738 | // get the insertion point in the standard menu | |
739 | MenuItemIndex winListStart; | |
740 | GetIndMenuItemWithCommandID(wm, | |
741 | kHICommandWindowListSeparator, 1, NULL, &winListStart); | |
742 | ||
743 | // add a separator so that the standard items and the custom items | |
744 | // aren't mixed together, but only if this is the first run | |
745 | OSStatus err = GetIndMenuItemWithCommandID(wm, | |
746 | 'WXWM', 1, NULL, NULL); | |
747 | ||
748 | if ( err == menuItemNotFoundErr ) | |
749 | { | |
750 | InsertMenuItemTextWithCFString( wm, | |
751 | CFSTR(""), winListStart-1, kMenuItemAttrSeparator, 'WXWM'); | |
752 | } | |
753 | ||
754 | wxInsertMenuItemsInMenu(menu, wm, winListStart); | |
755 | } | |
756 | else | |
757 | { | |
758 | UMASetMenuTitle( MAC_WXHMENU(menu->GetHMenu()) , m_titles[i], GetFont().GetEncoding() ) ; | |
759 | menu->MacBeforeDisplay(false) ; | |
760 | ||
8a145966 | 761 | ::InsertMenu(MAC_WXHMENU(GetMenu(i)->GetHMenu()), 0); |
524c47aa SC |
762 | } |
763 | } | |
764 | ||
765 | // take care of the about menu item wherever it is | |
766 | { | |
767 | wxMenu* aboutMenu ; | |
768 | wxMenuItem *aboutMenuItem = FindItem(wxApp::s_macAboutMenuItemId , &aboutMenu) ; | |
769 | if ( aboutMenuItem ) | |
770 | { | |
771 | wxAcceleratorEntry* | |
772 | entry = wxAcceleratorEntry::Create( aboutMenuItem->GetItemLabel() ) ; | |
773 | UMASetMenuItemText( GetMenuHandle( kwxMacAppleMenuId ) , 1 , wxStripMenuCodes ( aboutMenuItem->GetItemLabel() ) , wxFont::GetDefaultEncoding() ); | |
774 | UMAEnableMenuItem( GetMenuHandle( kwxMacAppleMenuId ) , 1 , true ); | |
775 | SetMenuItemCommandID( GetMenuHandle( kwxMacAppleMenuId ) , 1 , kHICommandAbout ) ; | |
776 | SetMenuItemRefCon(GetMenuHandle( kwxMacAppleMenuId ) , 1 , (URefCon)aboutMenuItem ) ; | |
777 | UMASetMenuItemShortcut( GetMenuHandle( kwxMacAppleMenuId ) , 1 , entry ) ; | |
778 | delete entry; | |
779 | } | |
780 | } | |
781 | ||
782 | if ( GetAutoWindowMenu() ) | |
783 | { | |
784 | if ( MacGetWindowMenuHMenu() == NULL ) | |
785 | CreateStandardWindowMenu( 0 , (MenuHandle*) &s_macWindowMenuHandle ) ; | |
786 | ||
787 | InsertMenu( (MenuHandle) MacGetWindowMenuHMenu() , 0 ) ; | |
788 | } | |
789 | ||
790 | ::DrawMenuBar() ; | |
791 | #endif | |
792 | ||
793 | s_macInstalledMenuBar = this; | |
794 | } | |
795 | ||
796 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
797 | { | |
798 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); | |
03647350 | 799 | |
524c47aa SC |
800 | m_rootMenu->FindItemByPosition( pos )->Enable(enable); |
801 | ||
802 | Refresh(); | |
803 | } | |
804 | ||
805 | bool wxMenuBar::Enable(bool enable) | |
806 | { | |
807 | wxCHECK_MSG( IsAttached(), false, wxT("doesn't work with unattached menubars") ); | |
808 | ||
809 | size_t i; | |
810 | for (i = 0; i < GetMenuCount(); i++) | |
811 | EnableTop(i, enable); | |
812 | ||
813 | return true; | |
814 | } | |
815 | ||
816 | void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label) | |
817 | { | |
818 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
819 | ||
8a145966 | 820 | GetMenu(pos)->SetTitle( label ) ; |
524c47aa SC |
821 | } |
822 | ||
823 | wxString wxMenuBar::GetMenuLabel(size_t pos) const | |
824 | { | |
825 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, | |
826 | wxT("invalid menu index in wxMenuBar::GetMenuLabel") ); | |
827 | ||
46405e36 | 828 | return GetMenu(pos)->GetTitle(); |
524c47aa SC |
829 | } |
830 | ||
831 | // --------------------------------------------------------------------------- | |
832 | // wxMenuBar construction | |
833 | // --------------------------------------------------------------------------- | |
834 | ||
835 | const int firstMenuPos = 1; // to account for the 0th application menu on mac | |
836 | ||
837 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
838 | { | |
839 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); | |
840 | if ( !menuOld ) | |
841 | return NULL; | |
842 | ||
524c47aa SC |
843 | wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos); |
844 | m_rootMenu->Remove(item); | |
845 | m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) ); | |
846 | ||
524c47aa SC |
847 | return menuOld; |
848 | } | |
849 | ||
850 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
851 | { | |
852 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
853 | return false; | |
854 | ||
524c47aa SC |
855 | m_rootMenu->Insert( pos+firstMenuPos, wxMenuItem::New( m_rootMenu, wxID_ANY, title, "", wxITEM_NORMAL, menu ) ); |
856 | ||
524c47aa SC |
857 | return true; |
858 | } | |
859 | ||
860 | wxMenu *wxMenuBar::Remove(size_t pos) | |
861 | { | |
862 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
863 | if ( !menu ) | |
864 | return NULL; | |
865 | ||
866 | wxMenuItem* item = m_rootMenu->FindItemByPosition(pos+firstMenuPos); | |
867 | m_rootMenu->Remove(item); | |
868 | ||
524c47aa SC |
869 | return menu; |
870 | } | |
871 | ||
872 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
873 | { | |
874 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
875 | wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") ); | |
876 | ||
877 | if ( !wxMenuBarBase::Append(menu, title) ) | |
878 | return false; | |
879 | ||
524c47aa | 880 | m_rootMenu->AppendSubMenu(menu, title); |
46405e36 | 881 | menu->SetTitle(title); |
524c47aa | 882 | |
524c47aa SC |
883 | return true; |
884 | } | |
885 | ||
524c47aa SC |
886 | void wxMenuBar::Detach() |
887 | { | |
888 | wxMenuBarBase::Detach() ; | |
889 | } | |
890 | ||
891 | void wxMenuBar::Attach(wxFrame *frame) | |
892 | { | |
893 | wxMenuBarBase::Attach( frame ) ; | |
894 | } | |
895 | ||
46405e36 | 896 | #endif // wxUSE_MENUS |