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