]>
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 if ( node
->GetData()->GetId() == id
)
306 item
= node
->GetData();
311 node
= node
->GetNext();
316 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 // Update a menu and all submenus recursively. source is the object that has
327 // the update event handlers defined for it. If NULL, the menu or associated
328 // window will be used.
329 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
331 if ( !source
&& GetInvokingWindow() )
332 source
= GetInvokingWindow()->GetEventHandler();
334 source
= GetEventHandler();
338 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
341 wxMenuItem
* item
= node
->GetData();
342 if ( !item
->IsSeparator() )
344 wxWindowID id
= item
->GetId();
345 wxUpdateUIEvent
event(id
);
346 event
.SetEventObject( source
);
348 if ( source
->ProcessEvent(event
) )
350 // if anything changed, update the chanegd attribute
351 if (event
.GetSetText())
352 SetLabel(id
, event
.GetText());
353 if (event
.GetSetChecked())
354 Check(id
, event
.GetChecked());
355 if (event
.GetSetEnabled())
356 Enable(id
, event
.GetEnabled());
359 // recurse to the submenus
360 if ( item
->GetSubMenu() )
361 item
->GetSubMenu()->UpdateUI(source
);
363 //else: item is a separator (which don't process update UI events)
365 node
= node
->GetNext();
369 // ----------------------------------------------------------------------------
370 // wxMenu functions forwarded to wxMenuItem
371 // ----------------------------------------------------------------------------
373 void wxMenuBase::Enable( int id
, bool enable
)
375 wxMenuItem
*item
= FindItem(id
);
377 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
379 item
->Enable(enable
);
382 bool wxMenuBase::IsEnabled( int id
) const
384 wxMenuItem
*item
= FindItem(id
);
386 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
388 return item
->IsEnabled();
391 void wxMenuBase::Check( int id
, bool enable
)
393 wxMenuItem
*item
= FindItem(id
);
395 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
400 bool wxMenuBase::IsChecked( int id
) const
402 wxMenuItem
*item
= FindItem(id
);
404 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
406 return item
->IsChecked();
409 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
411 wxMenuItem
*item
= FindItem(id
);
413 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
415 item
->SetText(label
);
418 wxString
wxMenuBase::GetLabel( int id
) const
420 wxMenuItem
*item
= FindItem(id
);
422 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
424 return item
->GetText();
427 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
429 wxMenuItem
*item
= FindItem(id
);
431 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
433 item
->SetHelp( helpString
);
436 wxString
wxMenuBase::GetHelpString( int id
) const
438 wxMenuItem
*item
= FindItem(id
);
440 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
442 return item
->GetHelp();
445 // ----------------------------------------------------------------------------
446 // wxMenuBarBase ctor and dtor
447 // ----------------------------------------------------------------------------
449 wxMenuBarBase::wxMenuBarBase()
451 // we own the menus when we get them
452 m_menus
.DeleteContents(TRUE
);
455 wxMenuBarBase::~wxMenuBarBase()
457 // nothing to do, the list will delete the menus because of the call to
458 // DeleteContents() above
461 // ----------------------------------------------------------------------------
462 // wxMenuBar item access: the base class versions manage m_menus list, the
463 // derived class should reflect the changes in the real menubar
464 // ----------------------------------------------------------------------------
466 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
468 wxMenuList::Node
*node
= m_menus
.Item(pos
);
469 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
471 return node
->GetData();
474 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
476 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
478 m_menus
.Append(menu
);
483 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
484 const wxString
& WXUNUSED(title
))
486 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
488 wxMenuList::Node
*node
= m_menus
.Item(pos
);
489 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
491 m_menus
.Insert(node
, menu
);
496 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
497 const wxString
& WXUNUSED(title
))
499 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
501 wxMenuList::Node
*node
= m_menus
.Item(pos
);
502 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
504 wxMenu
*menuOld
= node
->GetData();
510 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
512 wxMenuList::Node
*node
= m_menus
.Item(pos
);
513 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
515 node
= m_menus
.DetachNode(node
);
516 wxCHECK( node
, NULL
); // unexpected
517 wxMenu
*menu
= node
->GetData();
524 // ---------------------------------------------------------------------------
525 // wxMenuBar functions forwarded to wxMenuItem
526 // ---------------------------------------------------------------------------
528 void wxMenuBarBase::Enable(int id
, bool enable
)
530 wxMenuItem
*item
= FindItem(id
);
532 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
534 item
->Enable(enable
);
537 void wxMenuBarBase::Check(int id
, bool check
)
539 wxMenuItem
*item
= FindItem(id
);
541 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
542 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
547 bool wxMenuBarBase::IsChecked(int id
) const
549 wxMenuItem
*item
= FindItem(id
);
551 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
553 return item
->IsChecked();
556 bool wxMenuBarBase::IsEnabled(int id
) const
558 wxMenuItem
*item
= FindItem(id
);
560 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
562 return item
->IsEnabled();
565 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
567 wxMenuItem
*item
= FindItem(id
);
569 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
571 item
->SetText(label
);
574 wxString
wxMenuBarBase::GetLabel(int id
) const
576 wxMenuItem
*item
= FindItem(id
);
578 wxCHECK_MSG( item
, wxEmptyString
,
579 wxT("wxMenuBar::GetLabel(): no such item") );
581 return item
->GetText();
584 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
586 wxMenuItem
*item
= FindItem(id
);
588 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
590 item
->SetHelp(helpString
);
593 wxString
wxMenuBarBase::GetHelpString(int id
) const
595 wxMenuItem
*item
= FindItem(id
);
597 wxCHECK_MSG( item
, wxEmptyString
,
598 wxT("wxMenuBar::GetHelpString(): no such item") );
600 return item
->GetHelp();