]>
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"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 #include "wx/listimpl.cpp"
41 WX_DEFINE_LIST(wxMenuList
);
42 WX_DEFINE_LIST(wxMenuItemList
);
44 // ============================================================================
46 // ============================================================================
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 wxMenuItemBase::~wxMenuItemBase()
59 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
61 wxString text
= m_text
.BeforeFirst(wxT('\t'));
66 int flags
= accel
->GetFlags();
67 if ( flags
& wxACCEL_ALT
)
69 if ( flags
& wxACCEL_CTRL
)
71 if ( flags
& wxACCEL_SHIFT
)
72 text
+= wxT("Shift-");
74 int code
= accel
->GetKeyCode();
89 text
<< wxT('F') << code
- WXK_F1
+ 1;
92 // if there are any other keys wxGetAccelFromString() may return,
93 // we should process them here
96 if ( wxIsalnum(code
) )
103 wxFAIL_MSG( wxT("unknown keyboard accel") );
110 #endif // wxUSE_ACCEL
112 // ----------------------------------------------------------------------------
113 // wxMenu ctor and dtor
114 // ----------------------------------------------------------------------------
116 void wxMenuBase::Init(long style
)
118 m_items
.DeleteContents(TRUE
);
120 m_menuBar
= (wxMenuBar
*)NULL
;
121 m_menuParent
= (wxMenu
*)NULL
;
123 m_invokingWindow
= (wxWindow
*)NULL
;
125 m_clientData
= (void *)NULL
;
126 m_eventHandler
= this;
129 wxMenuBase::~wxMenuBase()
131 // nothing to do, wxMenuItemList dtor will delete the menu items
134 // ----------------------------------------------------------------------------
135 // wxMenu item adding/removing
136 // ----------------------------------------------------------------------------
138 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
140 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
142 m_items
.Append(item
);
147 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
149 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
150 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
151 wxT("invalid index in wxMenu::Insert") );
153 return DoInsert(pos
, item
);
156 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
158 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
160 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
161 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
163 m_items
.Insert(node
, item
);
168 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
170 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
172 return DoRemove(item
);
175 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
177 wxMenuItemList::Node
*node
= m_items
.Find(item
);
179 // if we get here, the item is valid or one of Remove() functions is broken
180 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
182 // we detach the item, but we do delete the list node (i.e. don't call
183 // DetachNode() here!)
184 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
185 m_items
.DeleteNode(node
);
187 // item isn't attached to anything any more
188 wxMenu
*submenu
= item
->GetSubMenu();
191 submenu
->SetParent((wxMenu
*)NULL
);
197 bool wxMenuBase::Delete(wxMenuItem
*item
)
199 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Delete") );
201 return DoDelete(item
);
204 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
206 wxMenuItem
*item2
= DoRemove(item
);
207 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
209 // don't delete the submenu
210 item2
->SetSubMenu((wxMenu
*)NULL
);
217 bool wxMenuBase::Destroy(wxMenuItem
*item
)
219 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Destroy") );
221 return DoDestroy(item
);
224 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
226 wxMenuItem
*item2
= DoRemove(item
);
227 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
234 // ----------------------------------------------------------------------------
235 // wxMenu searching for items
236 // ----------------------------------------------------------------------------
238 // Finds the item id matching the given string, -1 if not found.
239 int wxMenuBase::FindItem(const wxString
& text
) const
241 wxString label
= wxMenuItem(NULL
, wxID_SEPARATOR
, text
).GetLabel();
242 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
244 node
= node
->GetNext() )
246 wxMenuItem
*item
= node
->GetData();
247 if ( item
->IsSubMenu() )
249 int rc
= item
->GetSubMenu()->FindItem(label
);
250 if ( rc
!= wxNOT_FOUND
)
253 else if ( !item
->IsSeparator() )
255 if ( item
->GetLabel() == label
)
256 return item
->GetId();
263 // recursive search for item by id
264 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
269 wxMenuItem
*item
= NULL
;
270 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
272 node
= node
->GetNext() )
274 item
= node
->GetData();
276 if ( item
->GetId() == itemId
)
279 *itemMenu
= (wxMenu
*)this;
281 else if ( item
->IsSubMenu() )
283 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
287 // don't exit the loop
295 // non recursive search
296 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
298 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
299 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
302 for ( pos
= 0; node
; pos
++ )
304 item
= node
->GetData();
305 if ( item
->GetId() == id
)
308 node
= node
->GetNext();
313 *ppos
= item
? pos
: wxNOT_FOUND
;
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 // Update a menu and all submenus recursively. source is the object that has
324 // the update event handlers defined for it. If NULL, the menu or associated
325 // window will be used.
326 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
328 if ( !source
&& GetInvokingWindow() )
329 source
= GetInvokingWindow()->GetEventHandler();
331 source
= GetEventHandler();
335 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
338 wxMenuItem
* item
= node
->GetData();
339 if ( !item
->IsSeparator() )
341 wxWindowID id
= item
->GetId();
342 wxUpdateUIEvent
event(id
);
343 event
.SetEventObject( source
);
345 if ( source
->ProcessEvent(event
) )
347 // if anything changed, update the chanegd attribute
348 if (event
.GetSetText())
349 SetLabel(id
, event
.GetText());
350 if (event
.GetSetChecked())
351 Check(id
, event
.GetChecked());
352 if (event
.GetSetEnabled())
353 Enable(id
, event
.GetEnabled());
356 // recurse to the submenus
357 if ( item
->GetSubMenu() )
358 item
->GetSubMenu()->UpdateUI(source
);
360 //else: item is a separator (which don't process update UI events)
362 node
= node
->GetNext();
366 // ----------------------------------------------------------------------------
367 // wxMenu functions forwarded to wxMenuItem
368 // ----------------------------------------------------------------------------
370 void wxMenuBase::Enable( int id
, bool enable
)
372 wxMenuItem
*item
= FindItem(id
);
374 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
376 item
->Enable(enable
);
379 bool wxMenuBase::IsEnabled( int id
) const
381 wxMenuItem
*item
= FindItem(id
);
383 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
385 return item
->IsEnabled();
388 void wxMenuBase::Check( int id
, bool enable
)
390 wxMenuItem
*item
= FindItem(id
);
392 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
397 bool wxMenuBase::IsChecked( int id
) const
399 wxMenuItem
*item
= FindItem(id
);
401 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
403 return item
->IsChecked();
406 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
408 wxMenuItem
*item
= FindItem(id
);
410 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
412 item
->SetText(label
);
415 wxString
wxMenuBase::GetLabel( int id
) const
417 wxMenuItem
*item
= FindItem(id
);
419 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
421 return item
->GetText();
424 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
426 wxMenuItem
*item
= FindItem(id
);
428 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
430 item
->SetHelp( helpString
);
433 wxString
wxMenuBase::GetHelpString( int id
) const
435 wxMenuItem
*item
= FindItem(id
);
437 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
439 return item
->GetHelp();
442 // ----------------------------------------------------------------------------
443 // wxMenuBarBase ctor and dtor
444 // ----------------------------------------------------------------------------
446 wxMenuBarBase::wxMenuBarBase()
448 // we own the menus when we get them
449 m_menus
.DeleteContents(TRUE
);
452 wxMenuBarBase::~wxMenuBarBase()
454 // nothing to do, the list will delete the menus because of the call to
455 // DeleteContents() above
458 // ----------------------------------------------------------------------------
459 // wxMenuBar item access: the base class versions manage m_menus list, the
460 // derived class should reflect the changes in the real menubar
461 // ----------------------------------------------------------------------------
463 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
465 wxMenuList::Node
*node
= m_menus
.Item(pos
);
466 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
468 return node
->GetData();
471 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
473 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
475 m_menus
.Append(menu
);
480 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
481 const wxString
& WXUNUSED(title
))
483 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
485 wxMenuList::Node
*node
= m_menus
.Item(pos
);
486 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
488 m_menus
.Insert(node
, menu
);
493 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
494 const wxString
& WXUNUSED(title
))
496 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
498 wxMenuList::Node
*node
= m_menus
.Item(pos
);
499 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
501 wxMenu
*menuOld
= node
->GetData();
507 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
509 wxMenuList::Node
*node
= m_menus
.Item(pos
);
510 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
512 node
= m_menus
.DetachNode(node
);
513 wxCHECK( node
, NULL
); // unexpected
514 wxMenu
*menu
= node
->GetData();
521 // ---------------------------------------------------------------------------
522 // wxMenuBar functions forwarded to wxMenuItem
523 // ---------------------------------------------------------------------------
525 void wxMenuBarBase::Enable(int id
, bool enable
)
527 wxMenuItem
*item
= FindItem(id
);
529 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
531 item
->Enable(enable
);
534 void wxMenuBarBase::Check(int id
, bool check
)
536 wxMenuItem
*item
= FindItem(id
);
538 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
539 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
544 bool wxMenuBarBase::IsChecked(int id
) const
546 wxMenuItem
*item
= FindItem(id
);
548 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
550 return item
->IsChecked();
553 bool wxMenuBarBase::IsEnabled(int id
) const
555 wxMenuItem
*item
= FindItem(id
);
557 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
559 return item
->IsEnabled();
562 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
564 wxMenuItem
*item
= FindItem(id
);
566 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
568 item
->SetText(label
);
571 wxString
wxMenuBarBase::GetLabel(int id
) const
573 wxMenuItem
*item
= FindItem(id
);
575 wxCHECK_MSG( item
, wxEmptyString
,
576 wxT("wxMenuBar::GetLabel(): no such item") );
578 return item
->GetText();
581 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
583 wxMenuItem
*item
= FindItem(id
);
585 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
587 item
->SetHelp(helpString
);
590 wxString
wxMenuBarBase::GetHelpString(int id
) const
592 wxMenuItem
*item
= FindItem(id
);
594 wxCHECK_MSG( item
, wxEmptyString
,
595 wxT("wxMenuBar::GetHelpString(): no such item") );
597 return item
->GetHelp();