]>
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;
129 #if wxUSE_MENU_CALLBACK
130 m_callback
= (wxFunction
) NULL
;
131 #endif // wxUSE_MENU_CALLBACK
134 wxMenuBase::~wxMenuBase()
136 // nothing to do, wxMenuItemList dtor will delete the menu items.
137 // Actually, in GTK, the submenus have to get deleted first.
140 // ----------------------------------------------------------------------------
141 // wxMenu item adding/removing
142 // ----------------------------------------------------------------------------
144 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
146 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
148 m_items
.Append(item
);
153 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
155 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
156 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
157 wxT("invalid index in wxMenu::Insert") );
159 return DoInsert(pos
, item
);
162 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
164 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
166 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
167 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
169 m_items
.Insert(node
, item
);
174 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
176 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
178 return DoRemove(item
);
181 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
183 wxMenuItemList::Node
*node
= m_items
.Find(item
);
185 // if we get here, the item is valid or one of Remove() functions is broken
186 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
188 // we detach the item, but we do delete the list node (i.e. don't call
189 // DetachNode() here!)
190 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
191 m_items
.DeleteNode(node
);
193 // item isn't attached to anything any more
194 wxMenu
*submenu
= item
->GetSubMenu();
197 submenu
->SetParent((wxMenu
*)NULL
);
203 bool wxMenuBase::Delete(wxMenuItem
*item
)
205 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Delete") );
207 return DoDelete(item
);
210 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
212 wxMenuItem
*item2
= DoRemove(item
);
213 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
215 // don't delete the submenu
216 item2
->SetSubMenu((wxMenu
*)NULL
);
223 bool wxMenuBase::Destroy(wxMenuItem
*item
)
225 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Destroy") );
227 return DoDestroy(item
);
230 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
232 wxMenuItem
*item2
= DoRemove(item
);
233 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
240 // ----------------------------------------------------------------------------
241 // wxMenu searching for items
242 // ----------------------------------------------------------------------------
244 // Finds the item id matching the given string, -1 if not found.
245 int wxMenuBase::FindItem(const wxString
& text
) const
247 wxString label
= wxMenuItem(NULL
, wxID_SEPARATOR
, text
).GetLabel();
248 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
250 node
= node
->GetNext() )
252 wxMenuItem
*item
= node
->GetData();
253 if ( item
->IsSubMenu() )
255 int rc
= item
->GetSubMenu()->FindItem(label
);
256 if ( rc
!= wxNOT_FOUND
)
259 else if ( !item
->IsSeparator() )
261 if ( item
->GetLabel() == label
)
262 return item
->GetId();
269 // recursive search for item by id
270 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
275 wxMenuItem
*item
= NULL
;
276 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
278 node
= node
->GetNext() )
280 item
= node
->GetData();
282 if ( item
->GetId() == itemId
)
285 *itemMenu
= (wxMenu
*)this;
287 else if ( item
->IsSubMenu() )
289 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
293 // don't exit the loop
301 // non recursive search
302 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
304 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
305 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
308 for ( pos
= 0; node
; pos
++ )
310 if ( node
->GetData()->GetId() == id
)
312 item
= node
->GetData();
317 node
= node
->GetNext();
322 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 // Update a menu and all submenus recursively. source is the object that has
333 // the update event handlers defined for it. If NULL, the menu or associated
334 // window will be used.
335 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
337 if ( !source
&& GetInvokingWindow() )
338 source
= GetInvokingWindow()->GetEventHandler();
340 source
= GetEventHandler();
344 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
347 wxMenuItem
* item
= node
->GetData();
348 if ( !item
->IsSeparator() )
350 wxWindowID id
= item
->GetId();
351 wxUpdateUIEvent
event(id
);
352 event
.SetEventObject( source
);
354 if ( source
->ProcessEvent(event
) )
356 // if anything changed, update the chanegd attribute
357 if (event
.GetSetText())
358 SetLabel(id
, event
.GetText());
359 if (event
.GetSetChecked())
360 Check(id
, event
.GetChecked());
361 if (event
.GetSetEnabled())
362 Enable(id
, event
.GetEnabled());
365 // recurse to the submenus
366 if ( item
->GetSubMenu() )
367 item
->GetSubMenu()->UpdateUI(source
);
369 //else: item is a separator (which don't process update UI events)
371 node
= node
->GetNext();
375 // ----------------------------------------------------------------------------
376 // wxMenu functions forwarded to wxMenuItem
377 // ----------------------------------------------------------------------------
379 void wxMenuBase::Enable( int id
, bool enable
)
381 wxMenuItem
*item
= FindItem(id
);
383 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
385 item
->Enable(enable
);
388 bool wxMenuBase::IsEnabled( int id
) const
390 wxMenuItem
*item
= FindItem(id
);
392 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
394 return item
->IsEnabled();
397 void wxMenuBase::Check( int id
, bool enable
)
399 wxMenuItem
*item
= FindItem(id
);
401 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
406 bool wxMenuBase::IsChecked( int id
) const
408 wxMenuItem
*item
= FindItem(id
);
410 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
412 return item
->IsChecked();
415 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
417 wxMenuItem
*item
= FindItem(id
);
419 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
421 item
->SetText(label
);
424 wxString
wxMenuBase::GetLabel( int id
) const
426 wxMenuItem
*item
= FindItem(id
);
428 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
430 return item
->GetText();
433 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
435 wxMenuItem
*item
= FindItem(id
);
437 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
439 item
->SetHelp( helpString
);
442 wxString
wxMenuBase::GetHelpString( int id
) const
444 wxMenuItem
*item
= FindItem(id
);
446 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
448 return item
->GetHelp();
451 // ----------------------------------------------------------------------------
452 // wxMenuBarBase ctor and dtor
453 // ----------------------------------------------------------------------------
455 wxMenuBarBase::wxMenuBarBase()
457 // we own the menus when we get them
458 m_menus
.DeleteContents(TRUE
);
461 wxMenuBarBase::~wxMenuBarBase()
463 // nothing to do, the list will delete the menus because of the call to
464 // DeleteContents() above
467 // ----------------------------------------------------------------------------
468 // wxMenuBar item access: the base class versions manage m_menus list, the
469 // derived class should reflect the changes in the real menubar
470 // ----------------------------------------------------------------------------
472 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
474 wxMenuList::Node
*node
= m_menus
.Item(pos
);
475 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
477 return node
->GetData();
480 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
482 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
484 m_menus
.Append(menu
);
489 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
490 const wxString
& WXUNUSED(title
))
492 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
494 wxMenuList::Node
*node
= m_menus
.Item(pos
);
495 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
497 m_menus
.Insert(node
, menu
);
502 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
503 const wxString
& WXUNUSED(title
))
505 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
507 wxMenuList::Node
*node
= m_menus
.Item(pos
);
508 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
510 wxMenu
*menuOld
= node
->GetData();
516 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
518 wxMenuList::Node
*node
= m_menus
.Item(pos
);
519 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
521 node
= m_menus
.DetachNode(node
);
522 wxCHECK( node
, NULL
); // unexpected
523 wxMenu
*menu
= node
->GetData();
530 // ---------------------------------------------------------------------------
531 // wxMenuBar functions forwarded to wxMenuItem
532 // ---------------------------------------------------------------------------
534 void wxMenuBarBase::Enable(int id
, bool enable
)
536 wxMenuItem
*item
= FindItem(id
);
538 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
540 item
->Enable(enable
);
543 void wxMenuBarBase::Check(int id
, bool check
)
545 wxMenuItem
*item
= FindItem(id
);
547 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
548 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
553 bool wxMenuBarBase::IsChecked(int id
) const
555 wxMenuItem
*item
= FindItem(id
);
557 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
559 return item
->IsChecked();
562 bool wxMenuBarBase::IsEnabled(int id
) const
564 wxMenuItem
*item
= FindItem(id
);
566 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
568 return item
->IsEnabled();
571 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
573 wxMenuItem
*item
= FindItem(id
);
575 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
577 item
->SetText(label
);
580 wxString
wxMenuBarBase::GetLabel(int id
) const
582 wxMenuItem
*item
= FindItem(id
);
584 wxCHECK_MSG( item
, wxEmptyString
,
585 wxT("wxMenuBar::GetLabel(): no such item") );
587 return item
->GetText();
590 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
592 wxMenuItem
*item
= FindItem(id
);
594 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
596 item
->SetHelp(helpString
);
599 wxString
wxMenuBarBase::GetHelpString(int id
) const
601 wxMenuItem
*item
= FindItem(id
);
603 wxCHECK_MSG( item
, wxEmptyString
,
604 wxT("wxMenuBar::GetHelpString(): no such item") );
606 return item
->GetHelp();