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