]> git.saurik.com Git - wxWidgets.git/blame - src/common/menucmn.cpp
Don't use size_t for plural forms parameter.
[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
51// ----------------------------------------------------------------------------
c36d4774 52// wxMenuItemBase
ee0a94cf
RR
53// ----------------------------------------------------------------------------
54
55wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu,
56 int id,
57 const wxString& text,
58 const wxString& help,
59 wxItemKind kind,
60 wxMenu *subMenu)
ee0a94cf 61{
9cd28f48
VZ
62 // notice that parentMenu can be NULL: the item can be attached to the menu
63 // later with SetMenu()
ee0a94cf
RR
64
65 m_parentMenu = parentMenu;
66 m_subMenu = subMenu;
67 m_isEnabled = true;
68 m_isChecked = false;
69 m_id = id;
70 m_kind = kind;
71 if (m_id == wxID_ANY)
0edeeb6d 72 m_id = wxWindow::NewControlId();
ee0a94cf
RR
73 if (m_id == wxID_SEPARATOR)
74 m_kind = wxITEM_SEPARATOR;
345319d6 75
52af3158 76 SetItemLabel(text);
345319d6 77 SetHelp(help);
ee0a94cf
RR
78}
79
80wxMenuItemBase::~wxMenuItemBase()
81{
82 delete m_subMenu;
83}
84
85#if wxUSE_ACCEL
86
1e6feb95
VZ
87wxAcceleratorEntry *wxMenuItemBase::GetAccel() const
88{
52af3158 89 return wxAcceleratorEntry::Create(GetItemLabel());
1e6feb95
VZ
90}
91
717a57c2
VZ
92void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel)
93{
94 wxString text = m_text.BeforeFirst(wxT('\t'));
95 if ( accel )
96 {
97 text += wxT('\t');
ee0a94cf 98 text += accel->ToString();
717a57c2
VZ
99 }
100
52af3158 101 SetItemLabel(text);
717a57c2
VZ
102}
103
104#endif // wxUSE_ACCEL
105
52af3158 106void wxMenuItemBase::SetItemLabel(const wxString& str)
345319d6
VZ
107{
108 m_text = str;
109
110 if ( m_text.empty() && !IsSeparator() )
111 {
112 wxASSERT_MSG( wxIsStockID(GetId()),
113 wxT("A non-stock menu item with an empty label?") );
114 m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
115 wxSTOCK_WITH_MNEMONIC);
116 }
117}
118
119void wxMenuItemBase::SetHelp(const wxString& str)
120{
121 m_help = str;
122
123 if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) )
124 {
125 // get a stock help string
126 m_help = wxGetStockHelpString(GetId());
127 }
128}
129
cabb286d
PC
130#ifndef __WXPM__
131wxString wxMenuItemBase::GetLabelText(const wxString& text)
132{
133 return wxStripMenuCodes(text);
134}
135#endif
136
52af3158
JS
137#if WXWIN_COMPATIBILITY_2_8
138wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
139{
140 return GetLabelText(text);
141}
142#endif
143
6d971354
RR
144bool wxMenuBase::ms_locked = true;
145
717a57c2
VZ
146// ----------------------------------------------------------------------------
147// wxMenu ctor and dtor
148// ----------------------------------------------------------------------------
149
150void wxMenuBase::Init(long style)
151{
d3b9f782
VZ
152 m_menuBar = NULL;
153 m_menuParent = NULL;
717a57c2 154
d3b9f782 155 m_invokingWindow = NULL;
717a57c2 156 m_style = style;
d3b9f782 157 m_clientData = NULL;
717a57c2
VZ
158 m_eventHandler = this;
159}
160
161wxMenuBase::~wxMenuBase()
162{
222ed1d6 163 WX_CLEAR_LIST(wxMenuItemList, m_items);
717a57c2
VZ
164}
165
166// ----------------------------------------------------------------------------
167// wxMenu item adding/removing
168// ----------------------------------------------------------------------------
169
1e6feb95
VZ
170void wxMenuBase::AddSubMenu(wxMenu *submenu)
171{
9a83f860 172 wxCHECK_RET( submenu, wxT("can't add a NULL submenu") );
1e6feb95 173
1e6feb95
VZ
174 submenu->SetParent((wxMenu *)this);
175}
176
9add9367 177wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item)
717a57c2 178{
9add9367 179 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") );
717a57c2
VZ
180
181 m_items.Append(item);
1e93ca17 182 item->SetMenu((wxMenu*)this);
1e6feb95
VZ
183 if ( item->IsSubMenu() )
184 {
185 AddSubMenu(item->GetSubMenu());
186 }
717a57c2 187
9add9367 188 return item;
717a57c2
VZ
189}
190
9add9367 191wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item)
717a57c2 192{
9add9367 193 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") );
717a57c2 194
32db328c
VZ
195 if ( pos == GetMenuItemCount() )
196 {
197 return DoAppend(item);
198 }
199 else
200 {
4e32eea1 201 wxCHECK_MSG( pos < GetMenuItemCount(), NULL,
32db328c
VZ
202 wxT("invalid index in wxMenu::Insert") );
203
204 return DoInsert(pos, item);
205 }
717a57c2
VZ
206}
207
9add9367 208wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item)
717a57c2 209{
9add9367 210 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") );
717a57c2 211
222ed1d6 212 wxMenuItemList::compatibility_iterator node = m_items.Item(pos);
4e32eea1 213 wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") );
717a57c2
VZ
214
215 m_items.Insert(node, item);
1e93ca17 216 item->SetMenu((wxMenu*)this);
1e6feb95
VZ
217 if ( item->IsSubMenu() )
218 {
219 AddSubMenu(item->GetSubMenu());
220 }
717a57c2 221
9add9367 222 return item;
717a57c2
VZ
223}
224
225wxMenuItem *wxMenuBase::Remove(wxMenuItem *item)
226{
227 wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") );
228
229 return DoRemove(item);
230}
231
232wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item)
233{
222ed1d6 234 wxMenuItemList::compatibility_iterator node = m_items.Find(item);
717a57c2
VZ
235
236 // if we get here, the item is valid or one of Remove() functions is broken
237 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
238
239 // we detach the item, but we do delete the list node (i.e. don't call
240 // DetachNode() here!)
222ed1d6 241 m_items.Erase(node);
717a57c2
VZ
242
243 // item isn't attached to anything any more
d3b9f782 244 item->SetMenu(NULL);
717a57c2
VZ
245 wxMenu *submenu = item->GetSubMenu();
246 if ( submenu )
247 {
d3b9f782 248 submenu->SetParent(NULL);
082006f3
VZ
249 if ( submenu->IsAttached() )
250 submenu->Detach();
717a57c2
VZ
251 }
252
253 return item;
254}
255
256bool wxMenuBase::Delete(wxMenuItem *item)
257{
4e32eea1 258 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") );
717a57c2
VZ
259
260 return DoDelete(item);
261}
262
263bool wxMenuBase::DoDelete(wxMenuItem *item)
264{
265 wxMenuItem *item2 = DoRemove(item);
4e32eea1 266 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
717a57c2
VZ
267
268 // don't delete the submenu
d3b9f782 269 item2->SetSubMenu(NULL);
717a57c2
VZ
270
271 delete item2;
272
4e32eea1 273 return true;
717a57c2
VZ
274}
275
276bool wxMenuBase::Destroy(wxMenuItem *item)
277{
4e32eea1 278 wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") );
717a57c2
VZ
279
280 return DoDestroy(item);
281}
282
283bool wxMenuBase::DoDestroy(wxMenuItem *item)
284{
285 wxMenuItem *item2 = DoRemove(item);
4e32eea1 286 wxCHECK_MSG( item2, false, wxT("failed to delete menu item") );
717a57c2
VZ
287
288 delete item2;
289
4e32eea1 290 return true;
717a57c2
VZ
291}
292
293// ----------------------------------------------------------------------------
294// wxMenu searching for items
295// ----------------------------------------------------------------------------
296
4e32eea1 297// Finds the item id matching the given string, wxNOT_FOUND if not found.
717a57c2
VZ
298int wxMenuBase::FindItem(const wxString& text) const
299{
52af3158 300 wxString label = wxMenuItem::GetLabelText(text);
222ed1d6 301 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
717a57c2
VZ
302 node;
303 node = node->GetNext() )
304 {
305 wxMenuItem *item = node->GetData();
306 if ( item->IsSubMenu() )
307 {
308 int rc = item->GetSubMenu()->FindItem(label);
309 if ( rc != wxNOT_FOUND )
310 return rc;
311 }
adb21613
VZ
312
313 // we execute this code for submenus as well to alllow finding them by
314 // name just like the ordinary items
315 if ( !item->IsSeparator() )
717a57c2 316 {
68bc148c 317 if ( item->GetItemLabelText() == label )
717a57c2
VZ
318 return item->GetId();
319 }
320 }
321
322 return wxNOT_FOUND;
323}
324
325// recursive search for item by id
326wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const
327{
328 if ( itemMenu )
329 *itemMenu = NULL;
330
331 wxMenuItem *item = NULL;
222ed1d6 332 for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
717a57c2
VZ
333 node && !item;
334 node = node->GetNext() )
335 {
336 item = node->GetData();
337
338 if ( item->GetId() == itemId )
339 {
340 if ( itemMenu )
341 *itemMenu = (wxMenu *)this;
342 }
343 else if ( item->IsSubMenu() )
344 {
345 item = item->GetSubMenu()->FindItem(itemId, itemMenu);
346 }
347 else
348 {
349 // don't exit the loop
350 item = NULL;
351 }
352 }
353
354 return item;
355}
356
357// non recursive search
358wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const
359{
d3b9f782 360 wxMenuItem *item = NULL;
222ed1d6 361 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2
VZ
362
363 size_t pos;
364 for ( pos = 0; node; pos++ )
365 {
1dddf838
VZ
366 if ( node->GetData()->GetId() == id )
367 {
368 item = node->GetData();
369
717a57c2 370 break;
1dddf838 371 }
717a57c2
VZ
372
373 node = node->GetNext();
374 }
375
376 if ( ppos )
377 {
1987af7e 378 *ppos = item ? pos : (size_t)wxNOT_FOUND;
717a57c2
VZ
379 }
380
381 return item;
382}
383
01ebf752
JS
384// find by position
385wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const
386{
20aed026 387 wxCHECK_MSG( position < m_items.GetCount(), NULL,
9a83f860 388 wxT("wxMenu::FindItemByPosition(): invalid menu index") );
20aed026
VZ
389
390 return m_items.Item( position )->GetData();
01ebf752
JS
391}
392
717a57c2 393// ----------------------------------------------------------------------------
1e6feb95 394// wxMenu helpers used by derived classes
717a57c2
VZ
395// ----------------------------------------------------------------------------
396
397// Update a menu and all submenus recursively. source is the object that has
398// the update event handlers defined for it. If NULL, the menu or associated
399// window will be used.
400void wxMenuBase::UpdateUI(wxEvtHandler* source)
401{
5ce61d9f
RR
402 if (GetInvokingWindow())
403 {
404 // Don't update menus if the parent
405 // frame is about to get deleted
406 wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() );
407 if (tlw && wxPendingDelete.Member(tlw))
408 return;
409 }
410
717a57c2
VZ
411 if ( !source && GetInvokingWindow() )
412 source = GetInvokingWindow()->GetEventHandler();
413 if ( !source )
414 source = GetEventHandler();
415 if ( !source )
416 source = this;
417
222ed1d6 418 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2
VZ
419 while ( node )
420 {
421 wxMenuItem* item = node->GetData();
422 if ( !item->IsSeparator() )
423 {
424 wxWindowID id = item->GetId();
425 wxUpdateUIEvent event(id);
426 event.SetEventObject( source );
427
428 if ( source->ProcessEvent(event) )
429 {
18afa2ac 430 // if anything changed, update the changed attribute
717a57c2
VZ
431 if (event.GetSetText())
432 SetLabel(id, event.GetText());
433 if (event.GetSetChecked())
434 Check(id, event.GetChecked());
435 if (event.GetSetEnabled())
436 Enable(id, event.GetEnabled());
437 }
438
439 // recurse to the submenus
440 if ( item->GetSubMenu() )
441 item->GetSubMenu()->UpdateUI(source);
442 }
18afa2ac 443 //else: item is a separator (which doesn't process update UI events)
717a57c2
VZ
444
445 node = node->GetNext();
446 }
447}
448
1e6feb95
VZ
449bool wxMenuBase::SendEvent(int id, int checked)
450{
451 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id);
452 event.SetEventObject(this);
453 event.SetInt(checked);
454
4e32eea1 455 bool processed = false;
1e6feb95 456
7739f9c9
VZ
457 // Try the menu's event handler first
458 wxEvtHandler *handler = GetEventHandler();
459 if ( handler )
460 processed = handler->SafelyProcessEvent(event);
1e6feb95 461
7739f9c9 462 // Try the window the menu was popped up from or its menu bar belongs to
1e6feb95
VZ
463 if ( !processed )
464 {
7739f9c9
VZ
465 wxWindow * const win = GetWindow();
466 if ( win )
467 processed = win->HandleWindowEvent(event);
1e6feb95
VZ
468 }
469
470 return processed;
471}
472
473// ----------------------------------------------------------------------------
474// wxMenu attaching/detaching to/from menu bar
475// ----------------------------------------------------------------------------
476
dbdf9a17
DE
477wxMenuBar* wxMenuBase::GetMenuBar() const
478{
479 if(GetParent())
480 return GetParent()->GetMenuBar();
481 return m_menuBar;
482}
483
1e6feb95
VZ
484void wxMenuBase::Attach(wxMenuBarBase *menubar)
485{
486 // use Detach() instead!
9a83f860 487 wxASSERT_MSG( menubar, wxT("menu can't be attached to NULL menubar") );
1e6feb95
VZ
488
489 // use IsAttached() to prevent this from happening
9a83f860 490 wxASSERT_MSG( !m_menuBar, wxT("attaching menu twice?") );
1e6feb95
VZ
491
492 m_menuBar = (wxMenuBar *)menubar;
493}
494
495void wxMenuBase::Detach()
496{
497 // use IsAttached() to prevent this from happening
9a83f860 498 wxASSERT_MSG( m_menuBar, wxT("detaching unattached menu?") );
1e6feb95
VZ
499
500 m_menuBar = NULL;
501}
502
e3f5caa2
VZ
503// ----------------------------------------------------------------------------
504// wxMenu invoking window handling
505// ----------------------------------------------------------------------------
506
507void wxMenuBase::SetInvokingWindow(wxWindow *win)
508{
509 wxASSERT_MSG( !GetParent(),
510 "should only be called for top level popup menus" );
511 wxASSERT_MSG( !IsAttached(),
512 "menus attached to menu bar can't have invoking window" );
513
514 m_invokingWindow = win;
515}
516
394cfde3 517wxWindow *wxMenuBase::GetWindow() const
e3f5caa2 518{
394cfde3
VZ
519 // only the top level menus have non-NULL invoking window or a pointer to
520 // the menu bar so recurse upwards until we find it
e3f5caa2
VZ
521 const wxMenuBase *menu = this;
522 while ( menu->GetParent() )
523 {
524 menu = menu->GetParent();
525 }
526
394cfde3
VZ
527 return menu->GetMenuBar() ? menu->GetMenuBar()->GetFrame()
528 : menu->GetInvokingWindow();
7118e711
VZ
529}
530
717a57c2
VZ
531// ----------------------------------------------------------------------------
532// wxMenu functions forwarded to wxMenuItem
533// ----------------------------------------------------------------------------
534
535void wxMenuBase::Enable( int id, bool enable )
536{
537 wxMenuItem *item = FindItem(id);
538
539 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
540
541 item->Enable(enable);
542}
543
544bool wxMenuBase::IsEnabled( int id ) const
545{
546 wxMenuItem *item = FindItem(id);
547
4e32eea1 548 wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") );
717a57c2
VZ
549
550 return item->IsEnabled();
551}
552
553void wxMenuBase::Check( int id, bool enable )
554{
555 wxMenuItem *item = FindItem(id);
556
557 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
558
559 item->Check(enable);
560}
561
562bool wxMenuBase::IsChecked( int id ) const
563{
564 wxMenuItem *item = FindItem(id);
565
4e32eea1 566 wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") );
717a57c2
VZ
567
568 return item->IsChecked();
569}
570
571void wxMenuBase::SetLabel( int id, const wxString &label )
572{
573 wxMenuItem *item = FindItem(id);
574
575 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
576
52af3158 577 item->SetItemLabel(label);
717a57c2
VZ
578}
579
580wxString wxMenuBase::GetLabel( int id ) const
581{
582 wxMenuItem *item = FindItem(id);
583
525d8583 584 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") );
717a57c2 585
52af3158 586 return item->GetItemLabel();
717a57c2
VZ
587}
588
589void wxMenuBase::SetHelpString( int id, const wxString& helpString )
590{
591 wxMenuItem *item = FindItem(id);
592
593 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
594
595 item->SetHelp( helpString );
596}
597
598wxString wxMenuBase::GetHelpString( int id ) const
599{
600 wxMenuItem *item = FindItem(id);
601
525d8583 602 wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") );
717a57c2
VZ
603
604 return item->GetHelp();
605}
606
607// ----------------------------------------------------------------------------
608// wxMenuBarBase ctor and dtor
3dfac970
VZ
609// ----------------------------------------------------------------------------
610
611wxMenuBarBase::wxMenuBarBase()
612{
1e6feb95
VZ
613 // not attached yet
614 m_menuBarFrame = NULL;
3dfac970
VZ
615}
616
617wxMenuBarBase::~wxMenuBarBase()
618{
222ed1d6 619 WX_CLEAR_LIST(wxMenuList, m_menus);
3dfac970
VZ
620}
621
622// ----------------------------------------------------------------------------
623// wxMenuBar item access: the base class versions manage m_menus list, the
624// derived class should reflect the changes in the real menubar
625// ----------------------------------------------------------------------------
626
627wxMenu *wxMenuBarBase::GetMenu(size_t pos) const
628{
222ed1d6 629 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
3dfac970
VZ
630 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") );
631
632 return node->GetData();
633}
634
b871bb95 635bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title)
3dfac970 636{
4e32eea1 637 wxCHECK_MSG( menu, false, wxT("can't append NULL menu") );
b871bb95 638 wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") );
3dfac970
VZ
639
640 m_menus.Append(menu);
1e6feb95 641 menu->Attach(this);
3dfac970 642
4e32eea1 643 return true;
3dfac970
VZ
644}
645
646bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu,
32db328c 647 const wxString& title)
3dfac970 648{
32db328c
VZ
649 if ( pos == m_menus.GetCount() )
650 {
186baeb2 651 return wxMenuBarBase::Append(menu, title);
32db328c 652 }
1e6feb95 653 else // not at the end
32db328c 654 {
4e32eea1 655 wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") );
3dfac970 656
222ed1d6 657 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
4e32eea1 658 wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") );
3dfac970 659
32db328c 660 m_menus.Insert(node, menu);
1e6feb95 661 menu->Attach(this);
3dfac970 662
4e32eea1 663 return true;
32db328c 664 }
3dfac970
VZ
665}
666
667wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu,
668 const wxString& WXUNUSED(title))
669{
670 wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") );
671
222ed1d6 672 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
3dfac970
VZ
673 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") );
674
675 wxMenu *menuOld = node->GetData();
676 node->SetData(menu);
677
1e6feb95
VZ
678 menu->Attach(this);
679 menuOld->Detach();
680
3dfac970
VZ
681 return menuOld;
682}
683
684wxMenu *wxMenuBarBase::Remove(size_t pos)
685{
222ed1d6 686 wxMenuList::compatibility_iterator node = m_menus.Item(pos);
3dfac970
VZ
687 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") );
688
3dfac970 689 wxMenu *menu = node->GetData();
222ed1d6 690 m_menus.Erase(node);
1e6feb95 691 menu->Detach();
3dfac970 692
3dfac970
VZ
693 return menu;
694}
695
270e8b6a 696int wxMenuBarBase::FindMenu(const wxString& title) const
52130557 697{
52af3158 698 wxString label = wxMenuItem::GetLabelText(title);
52130557
VZ
699
700 size_t count = GetMenuCount();
701 for ( size_t i = 0; i < count; i++ )
702 {
52af3158 703 wxString title2 = GetMenuLabel(i);
52130557 704 if ( (title2 == title) ||
52af3158 705 (wxMenuItem::GetLabelText(title2) == label) )
52130557
VZ
706 {
707 // found
2b5f62a0 708 return (int)i;
52130557
VZ
709 }
710 }
711
712 return wxNOT_FOUND;
713
714}
715
1e6feb95
VZ
716// ----------------------------------------------------------------------------
717// wxMenuBar attaching/detaching to/from the frame
718// ----------------------------------------------------------------------------
719
720void wxMenuBarBase::Attach(wxFrame *frame)
721{
722 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
723
724 m_menuBarFrame = frame;
725}
726
727void wxMenuBarBase::Detach()
728{
729 wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") );
730
731 m_menuBarFrame = NULL;
732}
733
734// ----------------------------------------------------------------------------
735// wxMenuBar searching for items
736// ----------------------------------------------------------------------------
737
738wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const
739{
740 if ( menu )
741 *menu = NULL;
742
743 wxMenuItem *item = NULL;
222ed1d6
MB
744 size_t count = GetMenuCount(), i;
745 wxMenuList::const_iterator it;
746 for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ )
1e6feb95 747 {
222ed1d6 748 item = (*it)->FindItem(id, menu);
1e6feb95
VZ
749 }
750
751 return item;
752}
753
754int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const
755{
52af3158 756 wxString label = wxMenuItem::GetLabelText(menu);
1e6feb95
VZ
757
758 int i = 0;
222ed1d6 759 wxMenuList::compatibility_iterator node;
1e6feb95
VZ
760 for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ )
761 {
52af3158 762 if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) )
1e6feb95
VZ
763 return node->GetData()->FindItem(item);
764 }
765
766 return wxNOT_FOUND;
767}
768
3dfac970
VZ
769// ---------------------------------------------------------------------------
770// wxMenuBar functions forwarded to wxMenuItem
771// ---------------------------------------------------------------------------
772
773void wxMenuBarBase::Enable(int id, bool enable)
774{
775 wxMenuItem *item = FindItem(id);
776
777 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
778
779 item->Enable(enable);
780}
781
782void wxMenuBarBase::Check(int id, bool check)
783{
784 wxMenuItem *item = FindItem(id);
785
786 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
787 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
788
789 item->Check(check);
790}
791
792bool wxMenuBarBase::IsChecked(int id) const
793{
794 wxMenuItem *item = FindItem(id);
795
4e32eea1 796 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") );
3dfac970
VZ
797
798 return item->IsChecked();
799}
800
801bool wxMenuBarBase::IsEnabled(int id) const
802{
803 wxMenuItem *item = FindItem(id);
804
4e32eea1 805 wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") );
3dfac970
VZ
806
807 return item->IsEnabled();
808}
809
810void wxMenuBarBase::SetLabel(int id, const wxString& label)
811{
812 wxMenuItem *item = FindItem(id);
813
814 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
815
52af3158 816 item->SetItemLabel(label);
3dfac970
VZ
817}
818
819wxString wxMenuBarBase::GetLabel(int id) const
820{
821 wxMenuItem *item = FindItem(id);
822
823 wxCHECK_MSG( item, wxEmptyString,
824 wxT("wxMenuBar::GetLabel(): no such item") );
825
52af3158 826 return item->GetItemLabel();
3dfac970
VZ
827}
828
829void wxMenuBarBase::SetHelpString(int id, const wxString& helpString)
830{
831 wxMenuItem *item = FindItem(id);
832
833 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
834
835 item->SetHelp(helpString);
836}
837
838wxString wxMenuBarBase::GetHelpString(int id) const
839{
840 wxMenuItem *item = FindItem(id);
841
842 wxCHECK_MSG( item, wxEmptyString,
843 wxT("wxMenuBar::GetHelpString(): no such item") );
844
845 return item->GetHelp();
846}
847
9cd28f48 848void wxMenuBarBase::UpdateMenus()
4d538595
DS
849{
850 wxEvtHandler* source;
851 wxMenu* menu;
852 int nCount = GetMenuCount();
853 for (int n = 0; n < nCount; n++)
854 {
855 menu = GetMenu( n );
856 if (menu != NULL)
857 {
858 source = menu->GetEventHandler();
859 if (source != NULL)
860 menu->UpdateUI( source );
861 }
862 }
863}
864
68bc148c
JS
865#if WXWIN_COMPATIBILITY_2_8
866// get or change the label of the menu at given position
867void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label)
868{
869 SetMenuLabel(pos, label);
870}
871
872wxString wxMenuBarBase::GetLabelTop(size_t pos) const
873{
c4e43bea 874 return GetMenuLabelText(pos);
68bc148c
JS
875}
876#endif
877
1e6feb95 878#endif // wxUSE_MENUS