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