]>
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()
60 void wxMenuItemBase::SetAccel(wxAcceleratorEntry
*accel
)
62 wxString text
= m_text
.BeforeFirst(wxT('\t'));
67 int flags
= accel
->GetFlags();
68 if ( flags
& wxACCEL_ALT
)
70 if ( flags
& wxACCEL_CTRL
)
72 if ( flags
& wxACCEL_SHIFT
)
73 text
+= wxT("Shift-");
75 int code
= accel
->GetKeyCode();
90 text
<< wxT('F') << code
- WXK_F1
+ 1;
93 // if there are any other keys wxGetAccelFromString() may return,
94 // we should process them here
97 if ( wxIsalnum(code
) )
104 wxFAIL_MSG( wxT("unknown keyboard accel") );
111 #endif // wxUSE_ACCEL
113 // ----------------------------------------------------------------------------
114 // wxMenu ctor and dtor
115 // ----------------------------------------------------------------------------
117 void wxMenuBase::Init(long style
)
119 m_items
.DeleteContents(TRUE
);
121 m_menuBar
= (wxMenuBar
*)NULL
;
122 m_menuParent
= (wxMenu
*)NULL
;
124 m_invokingWindow
= (wxWindow
*)NULL
;
126 m_clientData
= (void *)NULL
;
127 m_eventHandler
= this;
130 wxMenuBase::~wxMenuBase()
132 // nothing to do, wxMenuItemList dtor will delete the menu items.
133 // Actually, in GTK, the submenus have to get deleted first.
136 // ----------------------------------------------------------------------------
137 // wxMenu item adding/removing
138 // ----------------------------------------------------------------------------
140 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
142 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
144 m_items
.Append(item
);
149 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
151 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
152 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
153 wxT("invalid index in wxMenu::Insert") );
155 return DoInsert(pos
, item
);
158 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
160 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
162 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
163 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
165 m_items
.Insert(node
, item
);
170 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
172 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
174 return DoRemove(item
);
177 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
179 wxMenuItemList::Node
*node
= m_items
.Find(item
);
181 // if we get here, the item is valid or one of Remove() functions is broken
182 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
184 // we detach the item, but we do delete the list node (i.e. don't call
185 // DetachNode() here!)
186 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
187 m_items
.DeleteNode(node
);
189 // item isn't attached to anything any more
190 wxMenu
*submenu
= item
->GetSubMenu();
193 submenu
->SetParent((wxMenu
*)NULL
);
199 bool wxMenuBase::Delete(wxMenuItem
*item
)
201 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Delete") );
203 return DoDelete(item
);
206 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
208 wxMenuItem
*item2
= DoRemove(item
);
209 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
211 // don't delete the submenu
212 item2
->SetSubMenu((wxMenu
*)NULL
);
219 bool wxMenuBase::Destroy(wxMenuItem
*item
)
221 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Destroy") );
223 return DoDestroy(item
);
226 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
228 wxMenuItem
*item2
= DoRemove(item
);
229 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
236 // ----------------------------------------------------------------------------
237 // wxMenu searching for items
238 // ----------------------------------------------------------------------------
240 // Finds the item id matching the given string, -1 if not found.
241 int wxMenuBase::FindItem(const wxString
& text
) const
243 wxString label
= wxMenuItem(NULL
, wxID_SEPARATOR
, text
).GetLabel();
244 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
246 node
= node
->GetNext() )
248 wxMenuItem
*item
= node
->GetData();
249 if ( item
->IsSubMenu() )
251 int rc
= item
->GetSubMenu()->FindItem(label
);
252 if ( rc
!= wxNOT_FOUND
)
255 else if ( !item
->IsSeparator() )
257 if ( item
->GetLabel() == label
)
258 return item
->GetId();
265 // recursive search for item by id
266 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
271 wxMenuItem
*item
= NULL
;
272 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
274 node
= node
->GetNext() )
276 item
= node
->GetData();
278 if ( item
->GetId() == itemId
)
281 *itemMenu
= (wxMenu
*)this;
283 else if ( item
->IsSubMenu() )
285 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
289 // don't exit the loop
297 // non recursive search
298 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
300 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
301 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
304 for ( pos
= 0; node
; pos
++ )
306 if ( node
->GetData()->GetId() == id
)
308 item
= node
->GetData();
313 node
= node
->GetNext();
318 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 // Update a menu and all submenus recursively. source is the object that has
329 // the update event handlers defined for it. If NULL, the menu or associated
330 // window will be used.
331 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
333 if ( !source
&& GetInvokingWindow() )
334 source
= GetInvokingWindow()->GetEventHandler();
336 source
= GetEventHandler();
340 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
343 wxMenuItem
* item
= node
->GetData();
344 if ( !item
->IsSeparator() )
346 wxWindowID id
= item
->GetId();
347 wxUpdateUIEvent
event(id
);
348 event
.SetEventObject( source
);
350 if ( source
->ProcessEvent(event
) )
352 // if anything changed, update the chanegd attribute
353 if (event
.GetSetText())
354 SetLabel(id
, event
.GetText());
355 if (event
.GetSetChecked())
356 Check(id
, event
.GetChecked());
357 if (event
.GetSetEnabled())
358 Enable(id
, event
.GetEnabled());
361 // recurse to the submenus
362 if ( item
->GetSubMenu() )
363 item
->GetSubMenu()->UpdateUI(source
);
365 //else: item is a separator (which don't process update UI events)
367 node
= node
->GetNext();
371 // ----------------------------------------------------------------------------
372 // wxMenu functions forwarded to wxMenuItem
373 // ----------------------------------------------------------------------------
375 void wxMenuBase::Enable( int id
, bool enable
)
377 wxMenuItem
*item
= FindItem(id
);
379 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
381 item
->Enable(enable
);
384 bool wxMenuBase::IsEnabled( int id
) const
386 wxMenuItem
*item
= FindItem(id
);
388 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
390 return item
->IsEnabled();
393 void wxMenuBase::Check( int id
, bool enable
)
395 wxMenuItem
*item
= FindItem(id
);
397 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
402 bool wxMenuBase::IsChecked( int id
) const
404 wxMenuItem
*item
= FindItem(id
);
406 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
408 return item
->IsChecked();
411 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
413 wxMenuItem
*item
= FindItem(id
);
415 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
417 item
->SetText(label
);
420 wxString
wxMenuBase::GetLabel( int id
) const
422 wxMenuItem
*item
= FindItem(id
);
424 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
426 return item
->GetText();
429 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
431 wxMenuItem
*item
= FindItem(id
);
433 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
435 item
->SetHelp( helpString
);
438 wxString
wxMenuBase::GetHelpString( int id
) const
440 wxMenuItem
*item
= FindItem(id
);
442 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
444 return item
->GetHelp();
447 // ----------------------------------------------------------------------------
448 // wxMenuBarBase ctor and dtor
449 // ----------------------------------------------------------------------------
451 wxMenuBarBase::wxMenuBarBase()
453 // we own the menus when we get them
454 m_menus
.DeleteContents(TRUE
);
457 wxMenuBarBase::~wxMenuBarBase()
459 // nothing to do, the list will delete the menus because of the call to
460 // DeleteContents() above
463 // ----------------------------------------------------------------------------
464 // wxMenuBar item access: the base class versions manage m_menus list, the
465 // derived class should reflect the changes in the real menubar
466 // ----------------------------------------------------------------------------
468 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
470 wxMenuList::Node
*node
= m_menus
.Item(pos
);
471 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
473 return node
->GetData();
476 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
478 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
480 m_menus
.Append(menu
);
485 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
486 const wxString
& WXUNUSED(title
))
488 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
490 wxMenuList::Node
*node
= m_menus
.Item(pos
);
491 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
493 m_menus
.Insert(node
, menu
);
498 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
499 const wxString
& WXUNUSED(title
))
501 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
503 wxMenuList::Node
*node
= m_menus
.Item(pos
);
504 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
506 wxMenu
*menuOld
= node
->GetData();
512 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
514 wxMenuList::Node
*node
= m_menus
.Item(pos
);
515 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
517 node
= m_menus
.DetachNode(node
);
518 wxCHECK( node
, NULL
); // unexpected
519 wxMenu
*menu
= node
->GetData();
526 // ---------------------------------------------------------------------------
527 // wxMenuBar functions forwarded to wxMenuItem
528 // ---------------------------------------------------------------------------
530 void wxMenuBarBase::Enable(int id
, bool enable
)
532 wxMenuItem
*item
= FindItem(id
);
534 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
536 item
->Enable(enable
);
539 void wxMenuBarBase::Check(int id
, bool check
)
541 wxMenuItem
*item
= FindItem(id
);
543 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
544 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
549 bool wxMenuBarBase::IsChecked(int id
) const
551 wxMenuItem
*item
= FindItem(id
);
553 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
555 return item
->IsChecked();
558 bool wxMenuBarBase::IsEnabled(int id
) const
560 wxMenuItem
*item
= FindItem(id
);
562 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
564 return item
->IsEnabled();
567 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
569 wxMenuItem
*item
= FindItem(id
);
571 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
573 item
->SetText(label
);
576 wxString
wxMenuBarBase::GetLabel(int id
) const
578 wxMenuItem
*item
= FindItem(id
);
580 wxCHECK_MSG( item
, wxEmptyString
,
581 wxT("wxMenuBar::GetLabel(): no such item") );
583 return item
->GetText();
586 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
588 wxMenuItem
*item
= FindItem(id
);
590 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
592 item
->SetHelp(helpString
);
595 wxString
wxMenuBarBase::GetHelpString(int id
) const
597 wxMenuItem
*item
= FindItem(id
);
599 wxCHECK_MSG( item
, wxEmptyString
,
600 wxT("wxMenuBar::GetHelpString(): no such item") );
602 return item
->GetHelp();