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