]>
git.saurik.com Git - wxWidgets.git/blob - src/common/menucmn.cpp
65459efdd33d93bdf0db9ce1d210bf965ce9a6a4
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;
128 m_callback
= (wxFunction
) NULL
;
131 wxMenuBase::~wxMenuBase()
133 // nothing to do, wxMenuItemList dtor will delete the menu items.
134 // Actually, in GTK, the submenus have to get deleted first.
137 // ----------------------------------------------------------------------------
138 // wxMenu item adding/removing
139 // ----------------------------------------------------------------------------
141 bool wxMenuBase::DoAppend(wxMenuItem
*item
)
143 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Append()") );
145 m_items
.Append(item
);
150 bool wxMenuBase::Insert(size_t pos
, wxMenuItem
*item
)
152 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert") );
153 wxCHECK_MSG( pos
< GetMenuItemCount(), FALSE
,
154 wxT("invalid index in wxMenu::Insert") );
156 return DoInsert(pos
, item
);
159 bool wxMenuBase::DoInsert(size_t pos
, wxMenuItem
*item
)
161 wxCHECK_MSG( item
, FALSE
, wxT("invalid item in wxMenu::Insert()") );
163 wxMenuItemList::Node
*node
= m_items
.Item(pos
);
164 wxCHECK_MSG( node
, FALSE
, wxT("invalid index in wxMenu::Insert()") );
166 m_items
.Insert(node
, item
);
171 wxMenuItem
*wxMenuBase::Remove(wxMenuItem
*item
)
173 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Remove") );
175 return DoRemove(item
);
178 wxMenuItem
*wxMenuBase::DoRemove(wxMenuItem
*item
)
180 wxMenuItemList::Node
*node
= m_items
.Find(item
);
182 // if we get here, the item is valid or one of Remove() functions is broken
183 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
185 // we detach the item, but we do delete the list node (i.e. don't call
186 // DetachNode() here!)
187 node
->SetData((wxMenuItem
*)NULL
); // to prevent it from deleting the item
188 m_items
.DeleteNode(node
);
190 // item isn't attached to anything any more
191 wxMenu
*submenu
= item
->GetSubMenu();
194 submenu
->SetParent((wxMenu
*)NULL
);
200 bool wxMenuBase::Delete(wxMenuItem
*item
)
202 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Delete") );
204 return DoDelete(item
);
207 bool wxMenuBase::DoDelete(wxMenuItem
*item
)
209 wxMenuItem
*item2
= DoRemove(item
);
210 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
212 // don't delete the submenu
213 item2
->SetSubMenu((wxMenu
*)NULL
);
220 bool wxMenuBase::Destroy(wxMenuItem
*item
)
222 wxCHECK_MSG( item
, NULL
, wxT("invalid item in wxMenu::Destroy") );
224 return DoDestroy(item
);
227 bool wxMenuBase::DoDestroy(wxMenuItem
*item
)
229 wxMenuItem
*item2
= DoRemove(item
);
230 wxCHECK_MSG( item2
, FALSE
, wxT("failed to delete menu item") );
237 // ----------------------------------------------------------------------------
238 // wxMenu searching for items
239 // ----------------------------------------------------------------------------
241 // Finds the item id matching the given string, -1 if not found.
242 int wxMenuBase::FindItem(const wxString
& text
) const
244 wxString label
= wxMenuItem(NULL
, wxID_SEPARATOR
, text
).GetLabel();
245 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
247 node
= node
->GetNext() )
249 wxMenuItem
*item
= node
->GetData();
250 if ( item
->IsSubMenu() )
252 int rc
= item
->GetSubMenu()->FindItem(label
);
253 if ( rc
!= wxNOT_FOUND
)
256 else if ( !item
->IsSeparator() )
258 if ( item
->GetLabel() == label
)
259 return item
->GetId();
266 // recursive search for item by id
267 wxMenuItem
*wxMenuBase::FindItem(int itemId
, wxMenu
**itemMenu
) const
272 wxMenuItem
*item
= NULL
;
273 for ( wxMenuItemList::Node
*node
= m_items
.GetFirst();
275 node
= node
->GetNext() )
277 item
= node
->GetData();
279 if ( item
->GetId() == itemId
)
282 *itemMenu
= (wxMenu
*)this;
284 else if ( item
->IsSubMenu() )
286 item
= item
->GetSubMenu()->FindItem(itemId
, itemMenu
);
290 // don't exit the loop
298 // non recursive search
299 wxMenuItem
*wxMenuBase::FindChildItem(int id
, size_t *ppos
) const
301 wxMenuItem
*item
= (wxMenuItem
*)NULL
;
302 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
305 for ( pos
= 0; node
; pos
++ )
307 if ( node
->GetData()->GetId() == id
)
309 item
= node
->GetData();
314 node
= node
->GetNext();
319 *ppos
= item
? pos
: (size_t)wxNOT_FOUND
;
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 // Update a menu and all submenus recursively. source is the object that has
330 // the update event handlers defined for it. If NULL, the menu or associated
331 // window will be used.
332 void wxMenuBase::UpdateUI(wxEvtHandler
* source
)
334 if ( !source
&& GetInvokingWindow() )
335 source
= GetInvokingWindow()->GetEventHandler();
337 source
= GetEventHandler();
341 wxMenuItemList::Node
* node
= GetMenuItems().GetFirst();
344 wxMenuItem
* item
= node
->GetData();
345 if ( !item
->IsSeparator() )
347 wxWindowID id
= item
->GetId();
348 wxUpdateUIEvent
event(id
);
349 event
.SetEventObject( source
);
351 if ( source
->ProcessEvent(event
) )
353 // if anything changed, update the chanegd attribute
354 if (event
.GetSetText())
355 SetLabel(id
, event
.GetText());
356 if (event
.GetSetChecked())
357 Check(id
, event
.GetChecked());
358 if (event
.GetSetEnabled())
359 Enable(id
, event
.GetEnabled());
362 // recurse to the submenus
363 if ( item
->GetSubMenu() )
364 item
->GetSubMenu()->UpdateUI(source
);
366 //else: item is a separator (which don't process update UI events)
368 node
= node
->GetNext();
372 // ----------------------------------------------------------------------------
373 // wxMenu functions forwarded to wxMenuItem
374 // ----------------------------------------------------------------------------
376 void wxMenuBase::Enable( int id
, bool enable
)
378 wxMenuItem
*item
= FindItem(id
);
380 wxCHECK_RET( item
, wxT("wxMenu::Enable: no such item") );
382 item
->Enable(enable
);
385 bool wxMenuBase::IsEnabled( int id
) const
387 wxMenuItem
*item
= FindItem(id
);
389 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsEnabled: no such item") );
391 return item
->IsEnabled();
394 void wxMenuBase::Check( int id
, bool enable
)
396 wxMenuItem
*item
= FindItem(id
);
398 wxCHECK_RET( item
, wxT("wxMenu::Check: no such item") );
403 bool wxMenuBase::IsChecked( int id
) const
405 wxMenuItem
*item
= FindItem(id
);
407 wxCHECK_MSG( item
, FALSE
, wxT("wxMenu::IsChecked: no such item") );
409 return item
->IsChecked();
412 void wxMenuBase::SetLabel( int id
, const wxString
&label
)
414 wxMenuItem
*item
= FindItem(id
);
416 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
418 item
->SetText(label
);
421 wxString
wxMenuBase::GetLabel( int id
) const
423 wxMenuItem
*item
= FindItem(id
);
425 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetLabel: no such item") );
427 return item
->GetText();
430 void wxMenuBase::SetHelpString( int id
, const wxString
& helpString
)
432 wxMenuItem
*item
= FindItem(id
);
434 wxCHECK_RET( item
, wxT("wxMenu::SetHelpString: no such item") );
436 item
->SetHelp( helpString
);
439 wxString
wxMenuBase::GetHelpString( int id
) const
441 wxMenuItem
*item
= FindItem(id
);
443 wxCHECK_MSG( item
, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
445 return item
->GetHelp();
448 // ----------------------------------------------------------------------------
449 // wxMenuBarBase ctor and dtor
450 // ----------------------------------------------------------------------------
452 wxMenuBarBase::wxMenuBarBase()
454 // we own the menus when we get them
455 m_menus
.DeleteContents(TRUE
);
458 wxMenuBarBase::~wxMenuBarBase()
460 // nothing to do, the list will delete the menus because of the call to
461 // DeleteContents() above
464 // ----------------------------------------------------------------------------
465 // wxMenuBar item access: the base class versions manage m_menus list, the
466 // derived class should reflect the changes in the real menubar
467 // ----------------------------------------------------------------------------
469 wxMenu
*wxMenuBarBase::GetMenu(size_t pos
) const
471 wxMenuList::Node
*node
= m_menus
.Item(pos
);
472 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::GetMenu()") );
474 return node
->GetData();
477 bool wxMenuBarBase::Append(wxMenu
*menu
, const wxString
& WXUNUSED(title
))
479 wxCHECK_MSG( menu
, FALSE
, wxT("can't append NULL menu") );
481 m_menus
.Append(menu
);
486 bool wxMenuBarBase::Insert(size_t pos
, wxMenu
*menu
,
487 const wxString
& WXUNUSED(title
))
489 wxCHECK_MSG( menu
, FALSE
, wxT("can't insert NULL menu") );
491 wxMenuList::Node
*node
= m_menus
.Item(pos
);
492 wxCHECK_MSG( node
, FALSE
, wxT("bad index in wxMenuBar::Insert()") );
494 m_menus
.Insert(node
, menu
);
499 wxMenu
*wxMenuBarBase::Replace(size_t pos
, wxMenu
*menu
,
500 const wxString
& WXUNUSED(title
))
502 wxCHECK_MSG( menu
, NULL
, wxT("can't insert NULL menu") );
504 wxMenuList::Node
*node
= m_menus
.Item(pos
);
505 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Replace()") );
507 wxMenu
*menuOld
= node
->GetData();
513 wxMenu
*wxMenuBarBase::Remove(size_t pos
)
515 wxMenuList::Node
*node
= m_menus
.Item(pos
);
516 wxCHECK_MSG( node
, NULL
, wxT("bad index in wxMenuBar::Remove()") );
518 node
= m_menus
.DetachNode(node
);
519 wxCHECK( node
, NULL
); // unexpected
520 wxMenu
*menu
= node
->GetData();
527 // ---------------------------------------------------------------------------
528 // wxMenuBar functions forwarded to wxMenuItem
529 // ---------------------------------------------------------------------------
531 void wxMenuBarBase::Enable(int id
, bool enable
)
533 wxMenuItem
*item
= FindItem(id
);
535 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
537 item
->Enable(enable
);
540 void wxMenuBarBase::Check(int id
, bool check
)
542 wxMenuItem
*item
= FindItem(id
);
544 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
545 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
550 bool wxMenuBarBase::IsChecked(int id
) const
552 wxMenuItem
*item
= FindItem(id
);
554 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
556 return item
->IsChecked();
559 bool wxMenuBarBase::IsEnabled(int id
) const
561 wxMenuItem
*item
= FindItem(id
);
563 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
565 return item
->IsEnabled();
568 void wxMenuBarBase::SetLabel(int id
, const wxString
& label
)
570 wxMenuItem
*item
= FindItem(id
);
572 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
574 item
->SetText(label
);
577 wxString
wxMenuBarBase::GetLabel(int id
) const
579 wxMenuItem
*item
= FindItem(id
);
581 wxCHECK_MSG( item
, wxEmptyString
,
582 wxT("wxMenuBar::GetLabel(): no such item") );
584 return item
->GetText();
587 void wxMenuBarBase::SetHelpString(int id
, const wxString
& helpString
)
589 wxMenuItem
*item
= FindItem(id
);
591 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
593 item
->SetHelp(helpString
);
596 wxString
wxMenuBarBase::GetHelpString(int id
) const
598 wxMenuItem
*item
= FindItem(id
);
600 wxCHECK_MSG( item
, wxEmptyString
,
601 wxT("wxMenuBar::GetHelpString(): no such item") );
603 return item
->GetHelp();