]>
git.saurik.com Git - wxWidgets.git/blob - src/common/menucmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/menucmn.cpp
3 // Purpose: wxMenu and wxMenuBar methods common to all ports
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "menubase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxMenuList
);
44 WX_DEFINE_LIST(wxMenuItemList
);
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 wxMenuItemBase::~wxMenuItemBase()
62 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
64 wxString text
= m_text
.BeforeFirst(wxT('\t'));
69 int flags
= accel
->GetFlags();
70 if ( flags
& wxACCEL_ALT
)
72 if ( flags
& wxACCEL_CTRL
)
74 if ( flags
& wxACCEL_SHIFT
)
75 text
+= wxT("Shift-");
77 int code
= accel
->GetKeyCode();
92 text
<< wxT('F') << code
- WXK_F1
+ 1;
95 // if there are any other keys wxGetAccelFromString() may return,
96 // we should process them here
99 if ( wxIsalnum(code
) )
101 text
<< (wxChar
)code
;
106 wxFAIL_MSG( wxT("unknown keyboard accel") );
113 #endif // wxUSE_ACCEL
115 // ----------------------------------------------------------------------------
116 // wxMenu ctor and dtor
117 // ----------------------------------------------------------------------------
119 void wxMenuBase::Init(long style
)
121 m_items
.DeleteContents(TRUE
);
123 m_menuBar
= (wxMenuBar
*)NULL
;
124 m_menuParent
= (wxMenu
*)NULL
;
126 m_invokingWindow
= (wxWindow
*)NULL
;
128 m_clientData
= (void *)NULL
;
129 m_eventHandler
= this;
131 #if wxUSE_MENU_CALLBACK
132 m_callback
= (wxFunction
) NULL
;
133 #endif // wxUSE_MENU_CALLBACK
136 wxMenuBase::~wxMenuBase()
138 // nothing to do, wxMenuItemList dtor will delete the menu items.
139 // Actually, in GTK, the submenus have to get deleted first.
142 // ----------------------------------------------------------------------------
143 // wxMenu item adding/removing
144 // ----------------------------------------------------------------------------
146 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
148 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
150 m_items
.Append(item
);
155 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
157 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
159 if ( pos
== GetMenuItemCount() )
161 return DoAppend(item
);
165 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
166 wxT("invalid index in wxMenu::Insert") );
168 return DoInsert(pos
, item
);
172 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
174 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
176 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
177 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
179 m_items
.Insert(node
, item
);
184 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
186 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
188 return DoRemove(item
);
191 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
193 wxMenuItemList::Node
*node
= m_items
.Find(item
);
195 // if we get here, the item is valid or one of Remove() functions is broken
196 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
198 // we detach the item, but we do delete the list node (i.e. don't call
199 // DetachNode() here!)
200 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
201 m_items
.DeleteNode(node
);
203 // item isn't attached to anything any more
204 wxMenu
*submenu
= item
->GetSubMenu();
207 submenu
->SetParent((wxMenu
*)NULL
);
213 bool wxMenuBase::Delete(wxMenuItem
*item
)
215 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Delete") );
217 return DoDelete(item
);
220 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
222 wxMenuItem
*item2
= DoRemove(item
);
223 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
225 // don't delete the submenu
226 item2
->SetSubMenu((wxMenu
*)NULL
);
233 bool wxMenuBase::Destroy(wxMenuItem
*item
)
235 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Destroy") );
237 return DoDestroy(item
);
240 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
242 wxMenuItem
*item2
= DoRemove(item
);
243 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
250 // ----------------------------------------------------------------------------
251 // wxMenu searching for items
252 // ----------------------------------------------------------------------------
254 // Finds the item id matching the given string, -1 if not found.
255 int wxMenuBase::FindItem(const wxString
& text
) const
257 wxString label
= wxMenuItem::GetLabelFromText(text
);
258 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
260 node
= node
->GetNext() )
262 wxMenuItem
*item
= node
->GetData();
263 if ( item
->IsSubMenu() )
265 int rc
= item
->GetSubMenu()->FindItem(label
);
266 if ( rc
!= wxNOT_FOUND
)
270 // we execute this code for submenus as well to alllow finding them by
271 // name just like the ordinary items
272 if ( !item
->IsSeparator() )
274 if ( item
->GetLabel() == label
)
275 return item
->GetId();
282 // recursive search for item by id
283 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
288 wxMenuItem
*item
= NULL
;
289 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
291 node
= node
->GetNext() )
293 item
= node
->GetData();
295 if ( item
->GetId() == itemId
)
298 *itemMenu
= (wxMenu
*)this;
300 else if ( item
->IsSubMenu() )
302 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
306 // don't exit the loop
314 // non recursive search
315 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
317 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
318 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
321 for ( pos
= 0; node
; pos
++ )
323 if ( node
->GetData()->GetId() == id
)
325 item
= node
->GetData();
330 node
= node
->GetNext();
335 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 // Update a menu and all submenus recursively. source is the object that has
346 // the update event handlers defined for it. If NULL, the menu or associated
347 // window will be used.
348 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
350 if ( !source
&& GetInvokingWindow() )
351 source
= GetInvokingWindow()->GetEventHandler();
353 source
= GetEventHandler();
357 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
360 wxMenuItem
* item
= node
->GetData();
361 if ( !item
->IsSeparator() )
363 wxWindowID id
= item
->GetId();
364 wxUpdateUIEvent
event(id
);
365 event
.SetEventObject( source
);
367 if ( source
->ProcessEvent(event
) )
369 // if anything changed, update the chanegd attribute
370 if (event
.GetSetText())
371 SetLabel(id
, event
.GetText());
372 if (event
.GetSetChecked())
373 Check(id
, event
.GetChecked());
374 if (event
.GetSetEnabled())
375 Enable(id
, event
.GetEnabled());
378 // recurse to the submenus
379 if ( item
->GetSubMenu() )
380 item
->GetSubMenu()->UpdateUI(source
);
382 //else: item is a separator (which don't process update UI events)
384 node
= node
->GetNext();
388 // ----------------------------------------------------------------------------
389 // wxMenu functions forwarded to wxMenuItem
390 // ----------------------------------------------------------------------------
392 void wxMenuBase::Enable( int id
, bool enable
)
394 wxMenuItem
*item
= FindItem(id
);
396 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
398 item
->Enable(enable
);
401 bool wxMenuBase::IsEnabled( int id
) const
403 wxMenuItem
*item
= FindItem(id
);
405 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
407 return item
->IsEnabled();
410 void wxMenuBase::Check( int id
, bool enable
)
412 wxMenuItem
*item
= FindItem(id
);
414 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
419 bool wxMenuBase::IsChecked( int id
) const
421 wxMenuItem
*item
= FindItem(id
);
423 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
425 return item
->IsChecked();
428 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
430 wxMenuItem
*item
= FindItem(id
);
432 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
434 item
->SetText(label
);
437 wxString
wxMenuBase::GetLabel( int id
) const
439 wxMenuItem
*item
= FindItem(id
);
441 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
443 return item
->GetText();
446 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
448 wxMenuItem
*item
= FindItem(id
);
450 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
452 item
->SetHelp( helpString
);
455 wxString
wxMenuBase::GetHelpString( int id
) const
457 wxMenuItem
*item
= FindItem(id
);
459 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
461 return item
->GetHelp();
464 // ----------------------------------------------------------------------------
465 // wxMenuBarBase ctor and dtor
466 // ----------------------------------------------------------------------------
468 wxMenuBarBase::wxMenuBarBase()
470 // we own the menus when we get them
471 m_menus
.DeleteContents(TRUE
);
474 wxMenuBarBase::~wxMenuBarBase()
476 // nothing to do, the list will delete the menus because of the call to
477 // DeleteContents() above
480 // ----------------------------------------------------------------------------
481 // wxMenuBar item access: the base class versions manage m_menus list, the
482 // derived class should reflect the changes in the real menubar
483 // ----------------------------------------------------------------------------
485 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
487 wxMenuList::Node
*node
= m_menus
.Item(pos
);
488 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
490 return node
->GetData();
493 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
495 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
497 m_menus
.Append(menu
);
502 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
503 const wxString
& title
)
505 if ( pos
== m_menus
.GetCount() )
507 return wxMenuBarBase::Append(menu
, title
);
511 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
513 wxMenuList::Node
*node
= m_menus
.Item(pos
);
514 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
516 m_menus
.Insert(node
, menu
);
522 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
523 const wxString
& WXUNUSED(title
))
525 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
527 wxMenuList::Node
*node
= m_menus
.Item(pos
);
528 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
530 wxMenu
*menuOld
= node
->GetData();
536 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
538 wxMenuList::Node
*node
= m_menus
.Item(pos
);
539 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
541 node
= m_menus
.DetachNode(node
);
542 wxCHECK( node
, NULL
); // unexpected
543 wxMenu
*menu
= node
->GetData();
550 int wxMenuBarBase::FindMenu(const wxString
& title
) const
552 wxString label
= wxMenuItem::GetLabelFromText(title
);
554 size_t count
= GetMenuCount();
555 for ( size_t i
= 0; i
< count
; i
++ )
557 wxString title2
= GetLabelTop(i
);
558 if ( (title2
== title
) ||
559 (wxMenuItem::GetLabelFromText(title2
) == label
) )
570 // ---------------------------------------------------------------------------
571 // wxMenuBar functions forwarded to wxMenuItem
572 // ---------------------------------------------------------------------------
574 void wxMenuBarBase::Enable(int id
, bool enable
)
576 wxMenuItem
*item
= FindItem(id
);
578 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
580 item
->Enable(enable
);
583 void wxMenuBarBase::Check(int id
, bool check
)
585 wxMenuItem
*item
= FindItem(id
);
587 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
588 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
593 bool wxMenuBarBase::IsChecked(int id
) const
595 wxMenuItem
*item
= FindItem(id
);
597 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
599 return item
->IsChecked();
602 bool wxMenuBarBase::IsEnabled(int id
) const
604 wxMenuItem
*item
= FindItem(id
);
606 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
608 return item
->IsEnabled();
611 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
613 wxMenuItem
*item
= FindItem(id
);
615 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
617 item
->SetText(label
);
620 wxString
wxMenuBarBase::GetLabel(int id
) const
622 wxMenuItem
*item
= FindItem(id
);
624 wxCHECK_MSG( item
, wxEmptyString
,
625 wxT("wxMenuBar::GetLabel(): no such item") );
627 return item
->GetText();
630 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
632 wxMenuItem
*item
= FindItem(id
);
634 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
636 item
->SetHelp(helpString
);
639 wxString
wxMenuBarBase::GetHelpString(int id
) const
641 wxMenuItem
*item
= FindItem(id
);
643 wxCHECK_MSG( item
, wxEmptyString
,
644 wxT("wxMenuBar::GetHelpString(): no such item") );
646 return item
->GetHelp();