Fix menu event handlers calling order.
[wxWidgets.git] / src / common / menucmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 26.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_MENUS
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/menu.h"
33 #include "wx/frame.h"
34 #endif
35
36 #include "wx/stockitem.h"
37
38 // ----------------------------------------------------------------------------
39 // template lists
40 // ----------------------------------------------------------------------------
41
42 #include "wx/listimpl.cpp"
43
44 WX_DEFINE_LIST(wxMenuList)
45 WX_DEFINE_LIST(wxMenuItemList)
46
47 // ============================================================================
48 // implementation
49 // ============================================================================
50
51 // ----------------------------------------------------------------------------
52 // XTI for wxMenu(Bar)
53 // ----------------------------------------------------------------------------
54
55 wxDEFINE_FLAGS( wxMenuStyle )
56 wxBEGIN_FLAGS( wxMenuStyle )
57 wxFLAGS_MEMBER(wxMENU_TEAROFF)
58 wxEND_FLAGS( wxMenuStyle )
59
60 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler, "wx/menu.h")
61 wxCOLLECTION_TYPE_INFO( wxMenuItem *, wxMenuItemList ) ;
62
63 #if wxUSE_EXTENDED_RTTI
64 template<> void wxCollectionToVariantArray( wxMenuItemList const &theList,
65 wxAnyList &value)
66 {
67 wxListCollectionToAnyList<wxMenuItemList::compatibility_iterator>( theList, value ) ;
68 }
69 #endif
70
71 wxBEGIN_PROPERTIES_TABLE(wxMenu)
72 wxEVENT_PROPERTY( Select, wxEVT_MENU, wxCommandEvent)
73
74 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString(), \
75 0 /*flags*/, wxT("Helpstring"), wxT("group") )
76
77 wxREADONLY_PROPERTY_FLAGS( MenuStyle, wxMenuStyle, long, GetStyle, \
78 wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, wxT("Helpstring"), \
79 wxT("group")) // style
80
81 wxPROPERTY_COLLECTION( MenuItems, wxMenuItemList, wxMenuItem*, Append, \
82 GetMenuItems, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
83 wxEND_PROPERTIES_TABLE()
84
85 wxEMPTY_HANDLERS_TABLE(wxMenu)
86
87 wxDIRECT_CONSTRUCTOR_2( wxMenu, wxString, Title, long, MenuStyle )
88
89 wxDEFINE_FLAGS( wxMenuBarStyle )
90
91 wxBEGIN_FLAGS( wxMenuBarStyle )
92 wxFLAGS_MEMBER(wxMB_DOCKABLE)
93 wxEND_FLAGS( wxMenuBarStyle )
94
95 #if wxUSE_EXTENDED_RTTI
96 // the negative id would lead the window (its superclass !) to
97 // vetoe streaming out otherwise
98 bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxObjectWriter *,
99 wxObjectWriterCallback *, const wxStringToAnyHashMap & )
100 {
101 return true;
102 }
103 #endif
104
105 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow, "wx/menu.h", \
106 wxMenuBarStreamingCallback)
107
108
109 #if wxUSE_EXTENDED_RTTI
110 WX_DEFINE_LIST( wxMenuInfoHelperList )
111
112 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfoHelper, wxObject, "wx/menu.h")
113
114 wxBEGIN_PROPERTIES_TABLE(wxMenuInfoHelper)
115 wxREADONLY_PROPERTY( Menu, wxMenu*, GetMenu, wxEMPTY_PARAMETER_VALUE, \
116 0 /*flags*/, wxT("Helpstring"), wxT("group"))
117
118 wxREADONLY_PROPERTY( Title, wxString, GetTitle, wxString(), \
119 0 /*flags*/, wxT("Helpstring"), wxT("group"))
120 wxEND_PROPERTIES_TABLE()
121
122 wxEMPTY_HANDLERS_TABLE(wxMenuInfoHelper)
123
124 wxCONSTRUCTOR_2( wxMenuInfoHelper, wxMenu*, Menu, wxString, Title )
125
126 wxCOLLECTION_TYPE_INFO( wxMenuInfoHelper *, wxMenuInfoHelperList ) ;
127
128 template<> void wxCollectionToVariantArray( wxMenuInfoHelperList const &theList,
129 wxAnyList &value)
130 {
131 wxListCollectionToAnyList<wxMenuInfoHelperList::compatibility_iterator>( theList, value ) ;
132 }
133
134 #endif
135
136 wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
137 wxPROPERTY_COLLECTION( MenuInfos, wxMenuInfoHelperList, wxMenuInfoHelper*, AppendMenuInfo, \
138 GetMenuInfos, 0 /*flags*/, wxT("Helpstring"), wxT("group"))
139 wxEND_PROPERTIES_TABLE()
140
141 wxEMPTY_HANDLERS_TABLE(wxMenuBar)
142
143 wxCONSTRUCTOR_DUMMY( wxMenuBar )
144
145 #if wxUSE_EXTENDED_RTTI
146
147 const wxMenuInfoHelperList& wxMenuBarBase::GetMenuInfos() const
148 {
149 wxMenuInfoHelperList* list = const_cast< wxMenuInfoHelperList* > (& m_menuInfos);
150 WX_CLEAR_LIST( wxMenuInfoHelperList, *list);
151 for (size_t i = 0 ; i < GetMenuCount(); ++i)
152 {
153 wxMenuInfoHelper* info = new wxMenuInfoHelper();
154 info->Create( GetMenu(i), GetMenuLabel(i));
155 list->Append(info);
156 }
157 return m_menuInfos;
158 }
159
160 #endif
161
162 // ----------------------------------------------------------------------------
163 // XTI for wxMenuItem
164 // ----------------------------------------------------------------------------
165
166 #if wxUSE_EXTENDED_RTTI
167
168 bool wxMenuItemStreamingCallback( const wxObject *object, wxObjectWriter *,
169 wxObjectWriterCallback *, const wxStringToAnyHashMap & )
170 {
171 const wxMenuItem * mitem = wx_dynamic_cast(const wxMenuItem*, object);
172 if ( mitem->GetMenu() && !mitem->GetMenu()->GetTitle().empty() )
173 {
174 // we don't stream out the first two items for menus with a title,
175 // they will be reconstructed
176 if ( mitem->GetMenu()->FindItemByPosition(0) == mitem ||
177 mitem->GetMenu()->FindItemByPosition(1) == mitem )
178 return false;
179 }
180 return true;
181 }
182
183 #endif
184
185 wxBEGIN_ENUM( wxItemKind )
186 wxENUM_MEMBER( wxITEM_SEPARATOR )
187 wxENUM_MEMBER( wxITEM_NORMAL )
188 wxENUM_MEMBER( wxITEM_CHECK )
189 wxENUM_MEMBER( wxITEM_RADIO )
190 wxEND_ENUM( wxItemKind )
191
192 wxIMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuItem, wxObject, "wx/menuitem.h", \
193 wxMenuItemStreamingCallback)
194
195 wxBEGIN_PROPERTIES_TABLE(wxMenuItem)
196 wxPROPERTY( Parent, wxMenu*, SetMenu, GetMenu, wxEMPTY_PARAMETER_VALUE, \
197 0 /*flags*/, wxT("Helpstring"), wxT("group") )
198 wxPROPERTY( Id, int, SetId, GetId, wxEMPTY_PARAMETER_VALUE, \
199 0 /*flags*/, wxT("Helpstring"), wxT("group") )
200 wxPROPERTY( ItemLabel, wxString, SetItemLabel, GetItemLabel, wxString(), \
201 0 /*flags*/, wxT("Helpstring"), wxT("group") )
202 wxPROPERTY( Help, wxString, SetHelp, GetHelp, wxString(), \
203 0 /*flags*/, wxT("Helpstring"), wxT("group") )
204 wxREADONLY_PROPERTY( Kind, wxItemKind, GetKind, wxEMPTY_PARAMETER_VALUE, \
205 0 /*flags*/, wxT("Helpstring"), wxT("group") )
206 wxPROPERTY( SubMenu, wxMenu*, SetSubMenu, GetSubMenu, wxEMPTY_PARAMETER_VALUE, \
207 0 /*flags*/, wxT("Helpstring"), wxT("group") )
208 wxPROPERTY( Enabled, bool, Enable, IsEnabled, wxAny((bool)true), \
209 0 /*flags*/, wxT("Helpstring"), wxT("group") )
210 wxPROPERTY( Checked, bool, Check, IsChecked, wxAny((bool)false), \
211 0 /*flags*/, wxT("Helpstring"), wxT("group") )
212 wxPROPERTY( Checkable, bool, SetCheckable, IsCheckable, wxAny((bool)false), \
213 0 /*flags*/, wxT("Helpstring"), wxT("group") )
214 wxEND_PROPERTIES_TABLE()
215
216 wxEMPTY_HANDLERS_TABLE(wxMenuItem)
217
218 wxDIRECT_CONSTRUCTOR_6( wxMenuItem, wxMenu*, Parent, int, Id, wxString, \
219 Text, wxString, Help, wxItemKind, Kind, wxMenu*, SubMenu )
220
221 // ----------------------------------------------------------------------------
222 // wxMenuItemBase
223 // ----------------------------------------------------------------------------
224
225 wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu,
226 int itemid,
227 const wxString& text,
228 const wxString& help,
229 wxItemKind kind,
230 wxMenu *subMenu)
231 {
232 switch ( itemid )
233 {
234 case wxID_ANY:
235 m_id = wxWindow::NewControlId();
236 break;
237
238 case wxID_SEPARATOR:
239 m_id = wxID_SEPARATOR;
240
241 // there is a lot of existing code just doing Append(wxID_SEPARATOR)
242 // and it makes sense to omit the following optional parameters,
243 // including the kind one which doesn't default to wxITEM_SEPARATOR,
244 // of course, so override it here
245 kind = wxITEM_SEPARATOR;
246 break;
247
248 case wxID_NONE:
249 // (popup) menu titles in wxMSW use this ID to indicate that
250 // it's not a real menu item, so we don't want the check below to
251 // apply to it
252 m_id = itemid;
253 break;
254
255 default:
256 // ids are limited to 16 bits under MSW so portable code shouldn't
257 // use ids outside of this range (negative ids generated by wx are
258 // fine though)
259 wxASSERT_MSG( (itemid >= 0 && itemid < SHRT_MAX) ||
260 (itemid >= wxID_AUTO_LOWEST && itemid <= wxID_AUTO_HIGHEST),
261 wxS("invalid itemid value") );
262 m_id = itemid;
263 }
264
265 // notice that parentMenu can be NULL: the item can be attached to the menu
266 // later with SetMenu()
267
268 m_parentMenu = parentMenu;
269 m_subMenu = subMenu;
270 m_isEnabled = true;
271 m_isChecked = false;
272 m_kind = kind;
273
274 SetItemLabel(text);
275 SetHelp(help);
276 }
277
278 wxMenuItemBase::~wxMenuItemBase()
279 {
280 delete m_subMenu;
281 }
282
283 #if wxUSE_ACCEL
284
285 wxAcceleratorEntry *wxMenuItemBase::GetAccel() const
286 {
287 return wxAcceleratorEntry::Create(GetItemLabel());
288 }
289
290 void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel)
291 {
292 wxString text = m_text.BeforeFirst(wxT('\t'));
293 if ( accel )
294 {
295 text += wxT('\t');
296 text += accel->ToString();
297 }
298
299 SetItemLabel(text);
300 }
301
302 #endif // wxUSE_ACCEL
303
304 void wxMenuItemBase::SetItemLabel(const wxString& str)
305 {
306 m_text = str;
307
308 if ( m_text.empty() && !IsSeparator() )
309 {
310 wxASSERT_MSG( wxIsStockID(GetId()),
311 wxT("A non-stock menu item with an empty label?") );
312 m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
313 wxSTOCK_WITH_MNEMONIC);
314 }
315 }
316
317 void wxMenuItemBase::SetHelp(const wxString& str)
318 {
319 m_help = str;
320
321 if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) )
322 {
323 // get a stock help string
324 m_help = wxGetStockHelpString(GetId());
325 }
326 }
327
328 #ifndef __WXPM__
329 wxString wxMenuItemBase::GetLabelText(const wxString& text)
330 {
331 return wxStripMenuCodes(text);
332 }
333 #endif
334
335 #if WXWIN_COMPATIBILITY_2_8
336 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
337 {
338 return GetLabelText(text);
339 }
340 #endif
341
342 bool wxMenuBase::ms_locked = true;
343
344 // ----------------------------------------------------------------------------
345 // wxMenu ctor and dtor
346 // ----------------------------------------------------------------------------
347
348 void wxMenuBase::Init(long style)
349 {
350 m_menuBar = NULL;
351 m_menuParent = NULL;
352
353 m_invokingWindow = NULL;
354 m_style = style;
355 m_clientData = NULL;
356 m_eventHandler = this;
357 }
358
359 wxMenuBase::~wxMenuBase()
360 {
361 WX_CLEAR_LIST(wxMenuItemList, m_items);
362 }
363
364 // ----------------------------------------------------------------------------
365 // wxMenu item adding/removing
366 // ----------------------------------------------------------------------------
367
368 void wxMenuBase::AddSubMenu(wxMenu *submenu)
369 {
370 wxCHECK_RET( submenu, wxT("can't add a NULL submenu") );
371
372 submenu->SetParent((wxMenu *)this);
373 }
374
375 wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item)
376 {
377 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") );
378
379 m_items.Append(item);
380 item->SetMenu((wxMenu*)this);
381 if ( item->IsSubMenu() )
382 {
383 AddSubMenu(item->GetSubMenu());
384 }
385
386 return item;
387 }
388
389 wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item)
390 {
391 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") );
392
393 if ( pos == GetMenuItemCount() )
394 {
395 return DoAppend(item);
396 }
397 else
398 {
399 wxCHECK_MSG( pos < GetMenuItemCount(), NULL,
400 wxT("invalid index in wxMenu::Insert") );
401
402 return DoInsert(pos, item);
403 }
404 }
405
406 wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item)
407 {
408 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") );
409
410 wxMenuItemList::compatibility_iterator node = m_items.Item(pos);
411 wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") );
412
413 m_items.Insert(node, item);
414 item->SetMenu((wxMenu*)this);
415 if ( item->IsSubMenu() )
416 {
417 AddSubMenu(item->GetSubMenu());
418 }
419
420 return item;
421 }
422
423 wxMenuItem *wxMenuBase::Remove(wxMenuItem *item)
424 {
425 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") );
426
427 return DoRemove(item);
428 }
429
430 wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item)
431 {
432 wxMenuItemList::compatibility_iterator node = m_items.Find(item);
433
434 // if we get here, the item is valid or one of Remove() functions is broken
435 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
436
437 // we detach the item, but we do delete the list node (i.e. don't call
438 // DetachNode() here!)
439 m_items.Erase(node);
440
441 // item isn't attached to anything any more
442 item->SetMenu(NULL);
443 wxMenu *submenu = item->GetSubMenu();
444 if ( submenu )
445 {
446 submenu->SetParent(NULL);
447 if ( submenu->IsAttached() )
448 submenu->Detach();
449 }
450
451 return item;
452 }
453
454 bool wxMenuBase::Delete(wxMenuItem *item)
455 {
456 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") );
457
458 return DoDelete(item);
459 }
460
461 bool wxMenuBase::DoDelete(wxMenuItem *item)
462 {
463 wxMenuItem *item2 = DoRemove(item);
464 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
465
466 // don't delete the submenu
467 item2->SetSubMenu(NULL);
468
469 delete item2;
470
471 return true;
472 }
473
474 bool wxMenuBase::Destroy(wxMenuItem *item)
475 {
476 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") );
477
478 return DoDestroy(item);
479 }
480
481 bool wxMenuBase::DoDestroy(wxMenuItem *item)
482 {
483 wxMenuItem *item2 = DoRemove(item);
484 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
485
486 delete item2;
487
488 return true;
489 }
490
491 // ----------------------------------------------------------------------------
492 // wxMenu searching for items
493 // ----------------------------------------------------------------------------
494
495 // Finds the item id matching the given string, wxNOT_FOUND if not found.
496 int wxMenuBase::FindItem(const wxString& text) const
497 {
498 wxString label = wxMenuItem::GetLabelText(text);
499 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
500 node;
501 node = node->GetNext() )
502 {
503 wxMenuItem *item = node->GetData();
504 if ( item->IsSubMenu() )
505 {
506 int rc = item->GetSubMenu()->FindItem(label);
507 if ( rc != wxNOT_FOUND )
508 return rc;
509 }
510
511 // we execute this code for submenus as well to alllow finding them by
512 // name just like the ordinary items
513 if ( !item->IsSeparator() )
514 {
515 if ( item->GetItemLabelText() == label )
516 return item->GetId();
517 }
518 }
519
520 return wxNOT_FOUND;
521 }
522
523 // recursive search for item by id
524 wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const
525 {
526 if ( itemMenu )
527 *itemMenu = NULL;
528
529 wxMenuItem *item = NULL;
530 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
531 node && !item;
532 node = node->GetNext() )
533 {
534 item = node->GetData();
535
536 if ( item->GetId() == itemId )
537 {
538 if ( itemMenu )
539 *itemMenu = (wxMenu *)this;
540 }
541 else if ( item->IsSubMenu() )
542 {
543 item = item->GetSubMenu()->FindItem(itemId, itemMenu);
544 }
545 else
546 {
547 // don't exit the loop
548 item = NULL;
549 }
550 }
551
552 return item;
553 }
554
555 // non recursive search
556 wxMenuItem *wxMenuBase::FindChildItem(int itemid, size_t *ppos) const
557 {
558 wxMenuItem *item = NULL;
559 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
560
561 size_t pos;
562 for ( pos = 0; node; pos++ )
563 {
564 if ( node->GetData()->GetId() == itemid )
565 {
566 item = node->GetData();
567
568 break;
569 }
570
571 node = node->GetNext();
572 }
573
574 if ( ppos )
575 {
576 *ppos = item ? pos : (size_t)wxNOT_FOUND;
577 }
578
579 return item;
580 }
581
582 // find by position
583 wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const
584 {
585 wxCHECK_MSG( position < m_items.GetCount(), NULL,
586 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
587
588 return m_items.Item( position )->GetData();
589 }
590
591 // ----------------------------------------------------------------------------
592 // wxMenu helpers used by derived classes
593 // ----------------------------------------------------------------------------
594
595 // Update a menu and all submenus recursively. source is the object that has
596 // the update event handlers defined for it. If NULL, the menu or associated
597 // window will be used.
598 void wxMenuBase::UpdateUI(wxEvtHandler* source)
599 {
600 wxWindow * const win = GetWindow();
601
602 if ( !source && win )
603 source = win->GetEventHandler();
604 if ( !source )
605 source = GetEventHandler();
606 if ( !source )
607 source = this;
608
609 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
610 while ( node )
611 {
612 wxMenuItem* item = node->GetData();
613 if ( !item->IsSeparator() )
614 {
615 wxWindowID itemid = item->GetId();
616 wxUpdateUIEvent event(itemid);
617 event.SetEventObject( this );
618
619 if ( source->ProcessEvent(event) )
620 {
621 // if anything changed, update the changed attribute
622 if (event.GetSetText())
623 SetLabel(itemid, event.GetText());
624 if (event.GetSetChecked())
625 Check(itemid, event.GetChecked());
626 if (event.GetSetEnabled())
627 Enable(itemid, event.GetEnabled());
628 }
629
630 // recurse to the submenus
631 if ( item->GetSubMenu() )
632 item->GetSubMenu()->UpdateUI(source);
633 }
634 //else: item is a separator (which doesn't process update UI events)
635
636 node = node->GetNext();
637 }
638 }
639
640 bool wxMenuBase::SendEvent(int itemid, int checked)
641 {
642 wxCommandEvent event(wxEVT_MENU, itemid);
643 event.SetEventObject(this);
644 event.SetInt(checked);
645
646 wxWindow * const win = GetWindow();
647
648 // Try the menu's event handler first
649 wxEvtHandler *handler = GetEventHandler();
650 if ( handler )
651 {
652 // Indicate to the event processing code that we're going to pass this
653 // event to another handler if it's not processed here to prevent it
654 // from passing the event to wxTheApp: this will be done below if we do
655 // have the associated window.
656 if ( win )
657 event.SetWillBeProcessedAgain();
658
659 if ( handler->SafelyProcessEvent(event) )
660 return true;
661 }
662
663 // Try the window the menu was popped up from or its menu bar belongs to
664 if ( win && win->HandleWindowEvent(event) )
665 return true;
666
667 // Not processed.
668 return false;
669 }
670
671 // ----------------------------------------------------------------------------
672 // wxMenu attaching/detaching to/from menu bar
673 // ----------------------------------------------------------------------------
674
675 wxMenuBar* wxMenuBase::GetMenuBar() const
676 {
677 if(GetParent())
678 return GetParent()->GetMenuBar();
679 return m_menuBar;
680 }
681
682 void wxMenuBase::Attach(wxMenuBarBase *menubar)
683 {
684 // use Detach() instead!
685 wxASSERT_MSG( menubar, wxT("menu can't be attached to NULL menubar") );
686
687 // use IsAttached() to prevent this from happening
688 wxASSERT_MSG( !m_menuBar, wxT("attaching menu twice?") );
689
690 m_menuBar = (wxMenuBar *)menubar;
691 }
692
693 void wxMenuBase::Detach()
694 {
695 // use IsAttached() to prevent this from happening
696 wxASSERT_MSG( m_menuBar, wxT("detaching unattached menu?") );
697
698 m_menuBar = NULL;
699 }
700
701 // ----------------------------------------------------------------------------
702 // wxMenu invoking window handling
703 // ----------------------------------------------------------------------------
704
705 void wxMenuBase::SetInvokingWindow(wxWindow *win)
706 {
707 wxASSERT_MSG( !GetParent(),
708 "should only be called for top level popup menus" );
709 wxASSERT_MSG( !IsAttached(),
710 "menus attached to menu bar can't have invoking window" );
711
712 m_invokingWindow = win;
713 }
714
715 wxWindow *wxMenuBase::GetWindow() const
716 {
717 // only the top level menus have non-NULL invoking window or a pointer to
718 // the menu bar so recurse upwards until we find it
719 const wxMenuBase *menu = this;
720 while ( menu->GetParent() )
721 {
722 menu = menu->GetParent();
723 }
724
725 return menu->GetMenuBar() ? menu->GetMenuBar()->GetFrame()
726 : menu->GetInvokingWindow();
727 }
728
729 // ----------------------------------------------------------------------------
730 // wxMenu functions forwarded to wxMenuItem
731 // ----------------------------------------------------------------------------
732
733 void wxMenuBase::Enable( int itemid, bool enable )
734 {
735 wxMenuItem *item = FindItem(itemid);
736
737 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
738
739 item->Enable(enable);
740 }
741
742 bool wxMenuBase::IsEnabled( int itemid ) const
743 {
744 wxMenuItem *item = FindItem(itemid);
745
746 wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") );
747
748 return item->IsEnabled();
749 }
750
751 void wxMenuBase::Check( int itemid, bool enable )
752 {
753 wxMenuItem *item = FindItem(itemid);
754
755 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
756
757 item->Check(enable);
758 }
759
760 bool wxMenuBase::IsChecked( int itemid ) const
761 {
762 wxMenuItem *item = FindItem(itemid);
763
764 wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") );
765
766 return item->IsChecked();
767 }
768
769 void wxMenuBase::SetLabel( int itemid, const wxString &label )
770 {
771 wxMenuItem *item = FindItem(itemid);
772
773 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
774
775 item->SetItemLabel(label);
776 }
777
778 wxString wxMenuBase::GetLabel( int itemid ) const
779 {
780 wxMenuItem *item = FindItem(itemid);
781
782 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") );
783
784 return item->GetItemLabel();
785 }
786
787 void wxMenuBase::SetHelpString( int itemid, const wxString& helpString )
788 {
789 wxMenuItem *item = FindItem(itemid);
790
791 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
792
793 item->SetHelp( helpString );
794 }
795
796 wxString wxMenuBase::GetHelpString( int itemid ) const
797 {
798 wxMenuItem *item = FindItem(itemid);
799
800 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") );
801
802 return item->GetHelp();
803 }
804
805 // ----------------------------------------------------------------------------
806 // wxMenuBarBase ctor and dtor
807 // ----------------------------------------------------------------------------
808
809 wxMenuBarBase::wxMenuBarBase()
810 {
811 // not attached yet
812 m_menuBarFrame = NULL;
813 }
814
815 wxMenuBarBase::~wxMenuBarBase()
816 {
817 WX_CLEAR_LIST(wxMenuList, m_menus);
818 }
819
820 // ----------------------------------------------------------------------------
821 // wxMenuBar item access: the base class versions manage m_menus list, the
822 // derived class should reflect the changes in the real menubar
823 // ----------------------------------------------------------------------------
824
825 wxMenu *wxMenuBarBase::GetMenu(size_t pos) const
826 {
827 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
828 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") );
829
830 return node->GetData();
831 }
832
833 bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title)
834 {
835 wxCHECK_MSG( menu, false, wxT("can't append NULL menu") );
836 wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") );
837
838 m_menus.Append(menu);
839 menu->Attach(this);
840
841 return true;
842 }
843
844 bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu,
845 const wxString& title)
846 {
847 if ( pos == m_menus.GetCount() )
848 {
849 return wxMenuBarBase::Append(menu, title);
850 }
851 else // not at the end
852 {
853 wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") );
854
855 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
856 wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") );
857
858 m_menus.Insert(node, menu);
859 menu->Attach(this);
860
861 return true;
862 }
863 }
864
865 wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu,
866 const wxString& WXUNUSED(title))
867 {
868 wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") );
869
870 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
871 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") );
872
873 wxMenu *menuOld = node->GetData();
874 node->SetData(menu);
875
876 menu->Attach(this);
877 menuOld->Detach();
878
879 return menuOld;
880 }
881
882 wxMenu *wxMenuBarBase::Remove(size_t pos)
883 {
884 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
885 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") );
886
887 wxMenu *menu = node->GetData();
888 m_menus.Erase(node);
889 menu->Detach();
890
891 return menu;
892 }
893
894 int wxMenuBarBase::FindMenu(const wxString& title) const
895 {
896 wxString label = wxMenuItem::GetLabelText(title);
897
898 size_t count = GetMenuCount();
899 for ( size_t i = 0; i < count; i++ )
900 {
901 wxString title2 = GetMenuLabel(i);
902 if ( (title2 == title) ||
903 (wxMenuItem::GetLabelText(title2) == label) )
904 {
905 // found
906 return (int)i;
907 }
908 }
909
910 return wxNOT_FOUND;
911
912 }
913
914 // ----------------------------------------------------------------------------
915 // wxMenuBar attaching/detaching to/from the frame
916 // ----------------------------------------------------------------------------
917
918 void wxMenuBarBase::Attach(wxFrame *frame)
919 {
920 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
921
922 m_menuBarFrame = frame;
923 }
924
925 void wxMenuBarBase::Detach()
926 {
927 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
928
929 m_menuBarFrame = NULL;
930 }
931
932 // ----------------------------------------------------------------------------
933 // wxMenuBar searching for items
934 // ----------------------------------------------------------------------------
935
936 wxMenuItem *wxMenuBarBase::FindItem(int itemid, wxMenu **menu) const
937 {
938 if ( menu )
939 *menu = NULL;
940
941 wxMenuItem *item = NULL;
942 size_t count = GetMenuCount(), i;
943 wxMenuList::const_iterator it;
944 for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ )
945 {
946 item = (*it)->FindItem(itemid, menu);
947 }
948
949 return item;
950 }
951
952 int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const
953 {
954 wxString label = wxMenuItem::GetLabelText(menu);
955
956 int i = 0;
957 wxMenuList::compatibility_iterator node;
958 for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ )
959 {
960 if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) )
961 return node->GetData()->FindItem(item);
962 }
963
964 return wxNOT_FOUND;
965 }
966
967 // ---------------------------------------------------------------------------
968 // wxMenuBar functions forwarded to wxMenuItem
969 // ---------------------------------------------------------------------------
970
971 void wxMenuBarBase::Enable(int itemid, bool enable)
972 {
973 wxMenuItem *item = FindItem(itemid);
974
975 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
976
977 item->Enable(enable);
978 }
979
980 void wxMenuBarBase::Check(int itemid, bool check)
981 {
982 wxMenuItem *item = FindItem(itemid);
983
984 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
985 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
986
987 item->Check(check);
988 }
989
990 bool wxMenuBarBase::IsChecked(int itemid) const
991 {
992 wxMenuItem *item = FindItem(itemid);
993
994 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") );
995
996 return item->IsChecked();
997 }
998
999 bool wxMenuBarBase::IsEnabled(int itemid) const
1000 {
1001 wxMenuItem *item = FindItem(itemid);
1002
1003 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") );
1004
1005 return item->IsEnabled();
1006 }
1007
1008 void wxMenuBarBase::SetLabel(int itemid, const wxString& label)
1009 {
1010 wxMenuItem *item = FindItem(itemid);
1011
1012 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
1013
1014 item->SetItemLabel(label);
1015 }
1016
1017 wxString wxMenuBarBase::GetLabel(int itemid) const
1018 {
1019 wxMenuItem *item = FindItem(itemid);
1020
1021 wxCHECK_MSG( item, wxEmptyString,
1022 wxT("wxMenuBar::GetLabel(): no such item") );
1023
1024 return item->GetItemLabel();
1025 }
1026
1027 void wxMenuBarBase::SetHelpString(int itemid, const wxString& helpString)
1028 {
1029 wxMenuItem *item = FindItem(itemid);
1030
1031 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
1032
1033 item->SetHelp(helpString);
1034 }
1035
1036 wxString wxMenuBarBase::GetHelpString(int itemid) const
1037 {
1038 wxMenuItem *item = FindItem(itemid);
1039
1040 wxCHECK_MSG( item, wxEmptyString,
1041 wxT("wxMenuBar::GetHelpString(): no such item") );
1042
1043 return item->GetHelp();
1044 }
1045
1046 void wxMenuBarBase::UpdateMenus()
1047 {
1048 wxMenu* menu;
1049 int nCount = GetMenuCount();
1050 for (int n = 0; n < nCount; n++)
1051 {
1052 menu = GetMenu( n );
1053 if (menu != NULL)
1054 menu->UpdateUI( NULL );
1055 }
1056 }
1057
1058 #if WXWIN_COMPATIBILITY_2_8
1059 // get or change the label of the menu at given position
1060 void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label)
1061 {
1062 SetMenuLabel(pos, label);
1063 }
1064
1065 wxString wxMenuBarBase::GetLabelTop(size_t pos) const
1066 {
1067 return GetMenuLabelText(pos);
1068 }
1069 #endif
1070
1071 #endif // wxUSE_MENUS