]>
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
, NULL
, 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
, NULL
, 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
)
269 else if ( !item
->IsSeparator() )
271 if ( item
->GetLabel() == label
)
272 return item
->GetId();
279 // recursive search for item by id
280 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
285 wxMenuItem
*item
= NULL
;
286 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
288 node
= node
->GetNext() )
290 item
= node
->GetData();
292 if ( item
->GetId() == itemId
)
295 *itemMenu
= (wxMenu
*)this;
297 else if ( item
->IsSubMenu() )
299 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
303 // don't exit the loop
311 // non recursive search
312 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
314 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
315 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
318 for ( pos
= 0; node
; pos
++ )
320 if ( node
->GetData()->GetId() == id
)
322 item
= node
->GetData();
327 node
= node
->GetNext();
332 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 // Update a menu and all submenus recursively. source is the object that has
343 // the update event handlers defined for it. If NULL, the menu or associated
344 // window will be used.
345 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
347 if ( !source
&& GetInvokingWindow() )
348 source
= GetInvokingWindow()->GetEventHandler();
350 source
= GetEventHandler();
354 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
357 wxMenuItem
* item
= node
->GetData();
358 if ( !item
->IsSeparator() )
360 wxWindowID id
= item
->GetId();
361 wxUpdateUIEvent
event(id
);
362 event
.SetEventObject( source
);
364 if ( source
->ProcessEvent(event
) )
366 // if anything changed, update the chanegd attribute
367 if (event
.GetSetText())
368 SetLabel(id
, event
.GetText());
369 if (event
.GetSetChecked())
370 Check(id
, event
.GetChecked());
371 if (event
.GetSetEnabled())
372 Enable(id
, event
.GetEnabled());
375 // recurse to the submenus
376 if ( item
->GetSubMenu() )
377 item
->GetSubMenu()->UpdateUI(source
);
379 //else: item is a separator (which don't process update UI events)
381 node
= node
->GetNext();
385 // ----------------------------------------------------------------------------
386 // wxMenu functions forwarded to wxMenuItem
387 // ----------------------------------------------------------------------------
389 void wxMenuBase::Enable( int id
, bool enable
)
391 wxMenuItem
*item
= FindItem(id
);
393 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
395 item
->Enable(enable
);
398 bool wxMenuBase::IsEnabled( int id
) const
400 wxMenuItem
*item
= FindItem(id
);
402 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
404 return item
->IsEnabled();
407 void wxMenuBase::Check( int id
, bool enable
)
409 wxMenuItem
*item
= FindItem(id
);
411 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
416 bool wxMenuBase::IsChecked( int id
) const
418 wxMenuItem
*item
= FindItem(id
);
420 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
422 return item
->IsChecked();
425 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
427 wxMenuItem
*item
= FindItem(id
);
429 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
431 item
->SetText(label
);
434 wxString
wxMenuBase::GetLabel( int id
) const
436 wxMenuItem
*item
= FindItem(id
);
438 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
440 return item
->GetText();
443 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
445 wxMenuItem
*item
= FindItem(id
);
447 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
449 item
->SetHelp( helpString
);
452 wxString
wxMenuBase::GetHelpString( int id
) const
454 wxMenuItem
*item
= FindItem(id
);
456 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
458 return item
->GetHelp();
461 // ----------------------------------------------------------------------------
462 // wxMenuBarBase ctor and dtor
463 // ----------------------------------------------------------------------------
465 wxMenuBarBase::wxMenuBarBase()
467 // we own the menus when we get them
468 m_menus
.DeleteContents(TRUE
);
471 wxMenuBarBase::~wxMenuBarBase()
473 // nothing to do, the list will delete the menus because of the call to
474 // DeleteContents() above
477 // ----------------------------------------------------------------------------
478 // wxMenuBar item access: the base class versions manage m_menus list, the
479 // derived class should reflect the changes in the real menubar
480 // ----------------------------------------------------------------------------
482 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
484 wxMenuList::Node
*node
= m_menus
.Item(pos
);
485 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
487 return node
->GetData();
490 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
492 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
494 m_menus
.Append(menu
);
499 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
500 const wxString
& title
)
502 if ( pos
== m_menus
.GetCount() )
504 return Append(menu
, title
);
508 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
510 wxMenuList::Node
*node
= m_menus
.Item(pos
);
511 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
513 m_menus
.Insert(node
, menu
);
519 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
520 const wxString
& WXUNUSED(title
))
522 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
524 wxMenuList::Node
*node
= m_menus
.Item(pos
);
525 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
527 wxMenu
*menuOld
= node
->GetData();
533 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
535 wxMenuList::Node
*node
= m_menus
.Item(pos
);
536 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
538 node
= m_menus
.DetachNode(node
);
539 wxCHECK( node
, NULL
); // unexpected
540 wxMenu
*menu
= node
->GetData();
547 int wxMenuBarBase::FindMenu(const wxString
& title
) const
549 wxString label
= wxMenuItem::GetLabelFromText(title
);
551 size_t count
= GetMenuCount();
552 for ( size_t i
= 0; i
< count
; i
++ )
554 wxString title2
= GetLabelTop(i
);
555 if ( (title2
== title
) ||
556 (wxMenuItem::GetLabelFromText(title2
) == label
) )
567 // ---------------------------------------------------------------------------
568 // wxMenuBar functions forwarded to wxMenuItem
569 // ---------------------------------------------------------------------------
571 void wxMenuBarBase::Enable(int id
, bool enable
)
573 wxMenuItem
*item
= FindItem(id
);
575 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
577 item
->Enable(enable
);
580 void wxMenuBarBase::Check(int id
, bool check
)
582 wxMenuItem
*item
= FindItem(id
);
584 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
585 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
590 bool wxMenuBarBase::IsChecked(int id
) const
592 wxMenuItem
*item
= FindItem(id
);
594 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
596 return item
->IsChecked();
599 bool wxMenuBarBase::IsEnabled(int id
) const
601 wxMenuItem
*item
= FindItem(id
);
603 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
605 return item
->IsEnabled();
608 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
610 wxMenuItem
*item
= FindItem(id
);
612 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
614 item
->SetText(label
);
617 wxString
wxMenuBarBase::GetLabel(int id
) const
619 wxMenuItem
*item
= FindItem(id
);
621 wxCHECK_MSG( item
, wxEmptyString
,
622 wxT("wxMenuBar::GetLabel(): no such item") );
624 return item
->GetText();
627 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
629 wxMenuItem
*item
= FindItem(id
);
631 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
633 item
->SetHelp(helpString
);
636 wxString
wxMenuBarBase::GetHelpString(int id
) const
638 wxMenuItem
*item
= FindItem(id
);
640 wxCHECK_MSG( item
, wxEmptyString
,
641 wxT("wxMenuBar::GetHelpString(): no such item") );
643 return item
->GetHelp();