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