]>
Commit | Line | Data |
---|---|---|
3dfac970 | 1 | /////////////////////////////////////////////////////////////////////////////// |
0ad966ee | 2 | // Name: src/common/menucmn.cpp |
3dfac970 VZ |
3 | // Purpose: wxMenu and wxMenuBar methods common to all ports |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 26.10.99 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
3dfac970 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
3dfac970 VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_MENUS |
28 | ||
3dfac970 | 29 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
3dfac970 VZ |
32 | #include "wx/menu.h" |
33 | #endif | |
34 | ||
345319d6 VZ |
35 | #include "wx/stockitem.h" |
36 | ||
3dfac970 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // template lists | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | #include "wx/listimpl.cpp" | |
717a57c2 | 42 | |
259c43f6 VZ |
43 | WX_DEFINE_LIST(wxMenuList) |
44 | WX_DEFINE_LIST(wxMenuItemList) | |
3dfac970 VZ |
45 | |
46 | // ============================================================================ | |
47 | // implementation | |
48 | // ============================================================================ | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
c36d4774 | 51 | // wxMenuItemBase |
ee0a94cf RR |
52 | // ---------------------------------------------------------------------------- |
53 | ||
54 | wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, | |
55 | int id, | |
56 | const wxString& text, | |
57 | const wxString& help, | |
58 | wxItemKind kind, | |
59 | wxMenu *subMenu) | |
ee0a94cf | 60 | { |
9cd28f48 VZ |
61 | // notice that parentMenu can be NULL: the item can be attached to the menu |
62 | // later with SetMenu() | |
ee0a94cf RR |
63 | |
64 | m_parentMenu = parentMenu; | |
65 | m_subMenu = subMenu; | |
66 | m_isEnabled = true; | |
67 | m_isChecked = false; | |
68 | m_id = id; | |
69 | m_kind = kind; | |
70 | if (m_id == wxID_ANY) | |
0edeeb6d | 71 | m_id = wxWindow::NewControlId(); |
ee0a94cf RR |
72 | if (m_id == wxID_SEPARATOR) |
73 | m_kind = wxITEM_SEPARATOR; | |
345319d6 | 74 | |
52af3158 | 75 | SetItemLabel(text); |
345319d6 | 76 | SetHelp(help); |
ee0a94cf RR |
77 | } |
78 | ||
79 | wxMenuItemBase::~wxMenuItemBase() | |
80 | { | |
81 | delete m_subMenu; | |
82 | } | |
83 | ||
84 | #if wxUSE_ACCEL | |
85 | ||
1e6feb95 VZ |
86 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const |
87 | { | |
52af3158 | 88 | return wxAcceleratorEntry::Create(GetItemLabel()); |
1e6feb95 VZ |
89 | } |
90 | ||
717a57c2 VZ |
91 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) |
92 | { | |
93 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
94 | if ( accel ) | |
95 | { | |
96 | text += wxT('\t'); | |
ee0a94cf | 97 | text += accel->ToString(); |
717a57c2 VZ |
98 | } |
99 | ||
52af3158 | 100 | SetItemLabel(text); |
717a57c2 VZ |
101 | } |
102 | ||
103 | #endif // wxUSE_ACCEL | |
104 | ||
52af3158 | 105 | void wxMenuItemBase::SetItemLabel(const wxString& str) |
345319d6 VZ |
106 | { |
107 | m_text = str; | |
108 | ||
109 | if ( m_text.empty() && !IsSeparator() ) | |
110 | { | |
111 | wxASSERT_MSG( wxIsStockID(GetId()), | |
112 | wxT("A non-stock menu item with an empty label?") ); | |
113 | m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR | | |
114 | wxSTOCK_WITH_MNEMONIC); | |
115 | } | |
116 | } | |
117 | ||
118 | void wxMenuItemBase::SetHelp(const wxString& str) | |
119 | { | |
120 | m_help = str; | |
121 | ||
122 | if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) ) | |
123 | { | |
124 | // get a stock help string | |
125 | m_help = wxGetStockHelpString(GetId()); | |
126 | } | |
127 | } | |
128 | ||
cabb286d PC |
129 | #ifndef __WXPM__ |
130 | wxString wxMenuItemBase::GetLabelText(const wxString& text) | |
131 | { | |
132 | return wxStripMenuCodes(text); | |
133 | } | |
134 | #endif | |
135 | ||
52af3158 JS |
136 | #if WXWIN_COMPATIBILITY_2_8 |
137 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
138 | { | |
139 | return GetLabelText(text); | |
140 | } | |
141 | #endif | |
142 | ||
6d971354 RR |
143 | bool wxMenuBase::ms_locked = true; |
144 | ||
717a57c2 VZ |
145 | // ---------------------------------------------------------------------------- |
146 | // wxMenu ctor and dtor | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
149 | void wxMenuBase::Init(long style) | |
150 | { | |
d3b9f782 VZ |
151 | m_menuBar = NULL; |
152 | m_menuParent = NULL; | |
717a57c2 | 153 | |
d3b9f782 | 154 | m_invokingWindow = NULL; |
717a57c2 | 155 | m_style = style; |
d3b9f782 | 156 | m_clientData = NULL; |
717a57c2 VZ |
157 | m_eventHandler = this; |
158 | } | |
159 | ||
160 | wxMenuBase::~wxMenuBase() | |
161 | { | |
222ed1d6 | 162 | WX_CLEAR_LIST(wxMenuItemList, m_items); |
717a57c2 VZ |
163 | } |
164 | ||
165 | // ---------------------------------------------------------------------------- | |
166 | // wxMenu item adding/removing | |
167 | // ---------------------------------------------------------------------------- | |
168 | ||
1e6feb95 VZ |
169 | void wxMenuBase::AddSubMenu(wxMenu *submenu) |
170 | { | |
9a83f860 | 171 | wxCHECK_RET( submenu, wxT("can't add a NULL submenu") ); |
1e6feb95 | 172 | |
1e6feb95 VZ |
173 | submenu->SetParent((wxMenu *)this); |
174 | } | |
175 | ||
9add9367 | 176 | wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item) |
717a57c2 | 177 | { |
9add9367 | 178 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") ); |
717a57c2 VZ |
179 | |
180 | m_items.Append(item); | |
1e93ca17 | 181 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
182 | if ( item->IsSubMenu() ) |
183 | { | |
184 | AddSubMenu(item->GetSubMenu()); | |
185 | } | |
717a57c2 | 186 | |
9add9367 | 187 | return item; |
717a57c2 VZ |
188 | } |
189 | ||
9add9367 | 190 | wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item) |
717a57c2 | 191 | { |
9add9367 | 192 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") ); |
717a57c2 | 193 | |
32db328c VZ |
194 | if ( pos == GetMenuItemCount() ) |
195 | { | |
196 | return DoAppend(item); | |
197 | } | |
198 | else | |
199 | { | |
4e32eea1 | 200 | wxCHECK_MSG( pos < GetMenuItemCount(), NULL, |
32db328c VZ |
201 | wxT("invalid index in wxMenu::Insert") ); |
202 | ||
203 | return DoInsert(pos, item); | |
204 | } | |
717a57c2 VZ |
205 | } |
206 | ||
9add9367 | 207 | wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) |
717a57c2 | 208 | { |
9add9367 | 209 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") ); |
717a57c2 | 210 | |
222ed1d6 | 211 | wxMenuItemList::compatibility_iterator node = m_items.Item(pos); |
4e32eea1 | 212 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") ); |
717a57c2 VZ |
213 | |
214 | m_items.Insert(node, item); | |
1e93ca17 | 215 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
216 | if ( item->IsSubMenu() ) |
217 | { | |
218 | AddSubMenu(item->GetSubMenu()); | |
219 | } | |
717a57c2 | 220 | |
9add9367 | 221 | return item; |
717a57c2 VZ |
222 | } |
223 | ||
224 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
225 | { | |
226 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
227 | ||
228 | return DoRemove(item); | |
229 | } | |
230 | ||
231 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
232 | { | |
222ed1d6 | 233 | wxMenuItemList::compatibility_iterator node = m_items.Find(item); |
717a57c2 VZ |
234 | |
235 | // if we get here, the item is valid or one of Remove() functions is broken | |
236 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
237 | ||
238 | // we detach the item, but we do delete the list node (i.e. don't call | |
239 | // DetachNode() here!) | |
222ed1d6 | 240 | m_items.Erase(node); |
717a57c2 VZ |
241 | |
242 | // item isn't attached to anything any more | |
d3b9f782 | 243 | item->SetMenu(NULL); |
717a57c2 VZ |
244 | wxMenu *submenu = item->GetSubMenu(); |
245 | if ( submenu ) | |
246 | { | |
d3b9f782 | 247 | submenu->SetParent(NULL); |
082006f3 VZ |
248 | if ( submenu->IsAttached() ) |
249 | submenu->Detach(); | |
717a57c2 VZ |
250 | } |
251 | ||
252 | return item; | |
253 | } | |
254 | ||
255 | bool wxMenuBase::Delete(wxMenuItem *item) | |
256 | { | |
4e32eea1 | 257 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") ); |
717a57c2 VZ |
258 | |
259 | return DoDelete(item); | |
260 | } | |
261 | ||
262 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
263 | { | |
264 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 265 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
266 | |
267 | // don't delete the submenu | |
d3b9f782 | 268 | item2->SetSubMenu(NULL); |
717a57c2 VZ |
269 | |
270 | delete item2; | |
271 | ||
4e32eea1 | 272 | return true; |
717a57c2 VZ |
273 | } |
274 | ||
275 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
276 | { | |
4e32eea1 | 277 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") ); |
717a57c2 VZ |
278 | |
279 | return DoDestroy(item); | |
280 | } | |
281 | ||
282 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
283 | { | |
284 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 285 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
286 | |
287 | delete item2; | |
288 | ||
4e32eea1 | 289 | return true; |
717a57c2 VZ |
290 | } |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // wxMenu searching for items | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
4e32eea1 | 296 | // Finds the item id matching the given string, wxNOT_FOUND if not found. |
717a57c2 VZ |
297 | int wxMenuBase::FindItem(const wxString& text) const |
298 | { | |
52af3158 | 299 | wxString label = wxMenuItem::GetLabelText(text); |
222ed1d6 | 300 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
301 | node; |
302 | node = node->GetNext() ) | |
303 | { | |
304 | wxMenuItem *item = node->GetData(); | |
305 | if ( item->IsSubMenu() ) | |
306 | { | |
307 | int rc = item->GetSubMenu()->FindItem(label); | |
308 | if ( rc != wxNOT_FOUND ) | |
309 | return rc; | |
310 | } | |
adb21613 VZ |
311 | |
312 | // we execute this code for submenus as well to alllow finding them by | |
313 | // name just like the ordinary items | |
314 | if ( !item->IsSeparator() ) | |
717a57c2 | 315 | { |
68bc148c | 316 | if ( item->GetItemLabelText() == label ) |
717a57c2 VZ |
317 | return item->GetId(); |
318 | } | |
319 | } | |
320 | ||
321 | return wxNOT_FOUND; | |
322 | } | |
323 | ||
324 | // recursive search for item by id | |
325 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
326 | { | |
327 | if ( itemMenu ) | |
328 | *itemMenu = NULL; | |
329 | ||
330 | wxMenuItem *item = NULL; | |
222ed1d6 | 331 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
332 | node && !item; |
333 | node = node->GetNext() ) | |
334 | { | |
335 | item = node->GetData(); | |
336 | ||
337 | if ( item->GetId() == itemId ) | |
338 | { | |
339 | if ( itemMenu ) | |
340 | *itemMenu = (wxMenu *)this; | |
341 | } | |
342 | else if ( item->IsSubMenu() ) | |
343 | { | |
344 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
345 | } | |
346 | else | |
347 | { | |
348 | // don't exit the loop | |
349 | item = NULL; | |
350 | } | |
351 | } | |
352 | ||
353 | return item; | |
354 | } | |
355 | ||
356 | // non recursive search | |
357 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
358 | { | |
d3b9f782 | 359 | wxMenuItem *item = NULL; |
222ed1d6 | 360 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
361 | |
362 | size_t pos; | |
363 | for ( pos = 0; node; pos++ ) | |
364 | { | |
1dddf838 VZ |
365 | if ( node->GetData()->GetId() == id ) |
366 | { | |
367 | item = node->GetData(); | |
368 | ||
717a57c2 | 369 | break; |
1dddf838 | 370 | } |
717a57c2 VZ |
371 | |
372 | node = node->GetNext(); | |
373 | } | |
374 | ||
375 | if ( ppos ) | |
376 | { | |
1987af7e | 377 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
717a57c2 VZ |
378 | } |
379 | ||
380 | return item; | |
381 | } | |
382 | ||
01ebf752 JS |
383 | // find by position |
384 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
385 | { | |
20aed026 | 386 | wxCHECK_MSG( position < m_items.GetCount(), NULL, |
9a83f860 | 387 | wxT("wxMenu::FindItemByPosition(): invalid menu index") ); |
20aed026 VZ |
388 | |
389 | return m_items.Item( position )->GetData(); | |
01ebf752 JS |
390 | } |
391 | ||
717a57c2 | 392 | // ---------------------------------------------------------------------------- |
1e6feb95 | 393 | // wxMenu helpers used by derived classes |
717a57c2 VZ |
394 | // ---------------------------------------------------------------------------- |
395 | ||
396 | // Update a menu and all submenus recursively. source is the object that has | |
397 | // the update event handlers defined for it. If NULL, the menu or associated | |
398 | // window will be used. | |
399 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
400 | { | |
5ce61d9f RR |
401 | if (GetInvokingWindow()) |
402 | { | |
403 | // Don't update menus if the parent | |
404 | // frame is about to get deleted | |
405 | wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() ); | |
406 | if (tlw && wxPendingDelete.Member(tlw)) | |
407 | return; | |
408 | } | |
409 | ||
717a57c2 VZ |
410 | if ( !source && GetInvokingWindow() ) |
411 | source = GetInvokingWindow()->GetEventHandler(); | |
412 | if ( !source ) | |
413 | source = GetEventHandler(); | |
414 | if ( !source ) | |
415 | source = this; | |
416 | ||
222ed1d6 | 417 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
418 | while ( node ) |
419 | { | |
420 | wxMenuItem* item = node->GetData(); | |
421 | if ( !item->IsSeparator() ) | |
422 | { | |
423 | wxWindowID id = item->GetId(); | |
424 | wxUpdateUIEvent event(id); | |
425 | event.SetEventObject( source ); | |
426 | ||
427 | if ( source->ProcessEvent(event) ) | |
428 | { | |
18afa2ac | 429 | // if anything changed, update the changed attribute |
717a57c2 VZ |
430 | if (event.GetSetText()) |
431 | SetLabel(id, event.GetText()); | |
432 | if (event.GetSetChecked()) | |
433 | Check(id, event.GetChecked()); | |
434 | if (event.GetSetEnabled()) | |
435 | Enable(id, event.GetEnabled()); | |
436 | } | |
437 | ||
438 | // recurse to the submenus | |
439 | if ( item->GetSubMenu() ) | |
440 | item->GetSubMenu()->UpdateUI(source); | |
441 | } | |
18afa2ac | 442 | //else: item is a separator (which doesn't process update UI events) |
717a57c2 VZ |
443 | |
444 | node = node->GetNext(); | |
445 | } | |
446 | } | |
447 | ||
1e6feb95 VZ |
448 | bool wxMenuBase::SendEvent(int id, int checked) |
449 | { | |
450 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); | |
451 | event.SetEventObject(this); | |
452 | event.SetInt(checked); | |
453 | ||
4e32eea1 | 454 | bool processed = false; |
1e6feb95 | 455 | |
1e6feb95 | 456 | // Try the menu's event handler |
902725ee | 457 | // if ( !processed ) |
1e6feb95 VZ |
458 | { |
459 | wxEvtHandler *handler = GetEventHandler(); | |
460 | if ( handler ) | |
147e8ebc | 461 | processed = handler->SafelyProcessEvent(event); |
1e6feb95 VZ |
462 | } |
463 | ||
464 | // Try the window the menu was popped up from (and up through the | |
465 | // hierarchy) | |
466 | if ( !processed ) | |
467 | { | |
468 | const wxMenuBase *menu = this; | |
469 | while ( menu ) | |
470 | { | |
471 | wxWindow *win = menu->GetInvokingWindow(); | |
472 | if ( win ) | |
473 | { | |
d9df421a | 474 | processed = win->HandleWindowEvent(event); |
1e6feb95 VZ |
475 | break; |
476 | } | |
477 | ||
478 | menu = menu->GetParent(); | |
479 | } | |
480 | } | |
481 | ||
482 | return processed; | |
483 | } | |
484 | ||
485 | // ---------------------------------------------------------------------------- | |
486 | // wxMenu attaching/detaching to/from menu bar | |
487 | // ---------------------------------------------------------------------------- | |
488 | ||
dbdf9a17 DE |
489 | wxMenuBar* wxMenuBase::GetMenuBar() const |
490 | { | |
491 | if(GetParent()) | |
492 | return GetParent()->GetMenuBar(); | |
493 | return m_menuBar; | |
494 | } | |
495 | ||
1e6feb95 VZ |
496 | void wxMenuBase::Attach(wxMenuBarBase *menubar) |
497 | { | |
498 | // use Detach() instead! | |
9a83f860 | 499 | wxASSERT_MSG( menubar, wxT("menu can't be attached to NULL menubar") ); |
1e6feb95 VZ |
500 | |
501 | // use IsAttached() to prevent this from happening | |
9a83f860 | 502 | wxASSERT_MSG( !m_menuBar, wxT("attaching menu twice?") ); |
1e6feb95 VZ |
503 | |
504 | m_menuBar = (wxMenuBar *)menubar; | |
505 | } | |
506 | ||
507 | void wxMenuBase::Detach() | |
508 | { | |
509 | // use IsAttached() to prevent this from happening | |
9a83f860 | 510 | wxASSERT_MSG( m_menuBar, wxT("detaching unattached menu?") ); |
1e6feb95 VZ |
511 | |
512 | m_menuBar = NULL; | |
513 | } | |
514 | ||
e3f5caa2 VZ |
515 | // ---------------------------------------------------------------------------- |
516 | // wxMenu invoking window handling | |
517 | // ---------------------------------------------------------------------------- | |
518 | ||
519 | void wxMenuBase::SetInvokingWindow(wxWindow *win) | |
520 | { | |
521 | wxASSERT_MSG( !GetParent(), | |
522 | "should only be called for top level popup menus" ); | |
523 | wxASSERT_MSG( !IsAttached(), | |
524 | "menus attached to menu bar can't have invoking window" ); | |
525 | ||
526 | m_invokingWindow = win; | |
527 | } | |
528 | ||
529 | wxWindow *wxMenuBase::GetInvokingWindow() const | |
530 | { | |
531 | // only the popup menu itself has a non-NULL invoking window so recurse | |
532 | // upwards until we find it | |
533 | const wxMenuBase *menu = this; | |
534 | while ( menu->GetParent() ) | |
535 | { | |
536 | menu = menu->GetParent(); | |
537 | } | |
538 | ||
539 | // menu is a top level menu here | |
540 | return menu->m_invokingWindow; | |
541 | } | |
542 | ||
717a57c2 VZ |
543 | // ---------------------------------------------------------------------------- |
544 | // wxMenu functions forwarded to wxMenuItem | |
545 | // ---------------------------------------------------------------------------- | |
546 | ||
547 | void wxMenuBase::Enable( int id, bool enable ) | |
548 | { | |
549 | wxMenuItem *item = FindItem(id); | |
550 | ||
551 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
552 | ||
553 | item->Enable(enable); | |
554 | } | |
555 | ||
556 | bool wxMenuBase::IsEnabled( int id ) const | |
557 | { | |
558 | wxMenuItem *item = FindItem(id); | |
559 | ||
4e32eea1 | 560 | wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") ); |
717a57c2 VZ |
561 | |
562 | return item->IsEnabled(); | |
563 | } | |
564 | ||
565 | void wxMenuBase::Check( int id, bool enable ) | |
566 | { | |
567 | wxMenuItem *item = FindItem(id); | |
568 | ||
569 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
570 | ||
571 | item->Check(enable); | |
572 | } | |
573 | ||
574 | bool wxMenuBase::IsChecked( int id ) const | |
575 | { | |
576 | wxMenuItem *item = FindItem(id); | |
577 | ||
4e32eea1 | 578 | wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") ); |
717a57c2 VZ |
579 | |
580 | return item->IsChecked(); | |
581 | } | |
582 | ||
583 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
584 | { | |
585 | wxMenuItem *item = FindItem(id); | |
586 | ||
587 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
588 | ||
52af3158 | 589 | item->SetItemLabel(label); |
717a57c2 VZ |
590 | } |
591 | ||
592 | wxString wxMenuBase::GetLabel( int id ) const | |
593 | { | |
594 | wxMenuItem *item = FindItem(id); | |
595 | ||
525d8583 | 596 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") ); |
717a57c2 | 597 | |
52af3158 | 598 | return item->GetItemLabel(); |
717a57c2 VZ |
599 | } |
600 | ||
601 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
602 | { | |
603 | wxMenuItem *item = FindItem(id); | |
604 | ||
605 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
606 | ||
607 | item->SetHelp( helpString ); | |
608 | } | |
609 | ||
610 | wxString wxMenuBase::GetHelpString( int id ) const | |
611 | { | |
612 | wxMenuItem *item = FindItem(id); | |
613 | ||
525d8583 | 614 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") ); |
717a57c2 VZ |
615 | |
616 | return item->GetHelp(); | |
617 | } | |
618 | ||
619 | // ---------------------------------------------------------------------------- | |
620 | // wxMenuBarBase ctor and dtor | |
3dfac970 VZ |
621 | // ---------------------------------------------------------------------------- |
622 | ||
623 | wxMenuBarBase::wxMenuBarBase() | |
624 | { | |
1e6feb95 VZ |
625 | // not attached yet |
626 | m_menuBarFrame = NULL; | |
3dfac970 VZ |
627 | } |
628 | ||
629 | wxMenuBarBase::~wxMenuBarBase() | |
630 | { | |
222ed1d6 | 631 | WX_CLEAR_LIST(wxMenuList, m_menus); |
3dfac970 VZ |
632 | } |
633 | ||
634 | // ---------------------------------------------------------------------------- | |
635 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
636 | // derived class should reflect the changes in the real menubar | |
637 | // ---------------------------------------------------------------------------- | |
638 | ||
639 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
640 | { | |
222ed1d6 | 641 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
642 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); |
643 | ||
644 | return node->GetData(); | |
645 | } | |
646 | ||
b871bb95 | 647 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title) |
3dfac970 | 648 | { |
4e32eea1 | 649 | wxCHECK_MSG( menu, false, wxT("can't append NULL menu") ); |
b871bb95 | 650 | wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") ); |
3dfac970 VZ |
651 | |
652 | m_menus.Append(menu); | |
1e6feb95 | 653 | menu->Attach(this); |
3dfac970 | 654 | |
4e32eea1 | 655 | return true; |
3dfac970 VZ |
656 | } |
657 | ||
658 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
32db328c | 659 | const wxString& title) |
3dfac970 | 660 | { |
32db328c VZ |
661 | if ( pos == m_menus.GetCount() ) |
662 | { | |
186baeb2 | 663 | return wxMenuBarBase::Append(menu, title); |
32db328c | 664 | } |
1e6feb95 | 665 | else // not at the end |
32db328c | 666 | { |
4e32eea1 | 667 | wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") ); |
3dfac970 | 668 | |
222ed1d6 | 669 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
4e32eea1 | 670 | wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") ); |
3dfac970 | 671 | |
32db328c | 672 | m_menus.Insert(node, menu); |
1e6feb95 | 673 | menu->Attach(this); |
3dfac970 | 674 | |
4e32eea1 | 675 | return true; |
32db328c | 676 | } |
3dfac970 VZ |
677 | } |
678 | ||
679 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
680 | const wxString& WXUNUSED(title)) | |
681 | { | |
682 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
683 | ||
222ed1d6 | 684 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
685 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); |
686 | ||
687 | wxMenu *menuOld = node->GetData(); | |
688 | node->SetData(menu); | |
689 | ||
1e6feb95 VZ |
690 | menu->Attach(this); |
691 | menuOld->Detach(); | |
692 | ||
3dfac970 VZ |
693 | return menuOld; |
694 | } | |
695 | ||
696 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
697 | { | |
222ed1d6 | 698 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
699 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); |
700 | ||
3dfac970 | 701 | wxMenu *menu = node->GetData(); |
222ed1d6 | 702 | m_menus.Erase(node); |
1e6feb95 | 703 | menu->Detach(); |
3dfac970 | 704 | |
3dfac970 VZ |
705 | return menu; |
706 | } | |
707 | ||
270e8b6a | 708 | int wxMenuBarBase::FindMenu(const wxString& title) const |
52130557 | 709 | { |
52af3158 | 710 | wxString label = wxMenuItem::GetLabelText(title); |
52130557 VZ |
711 | |
712 | size_t count = GetMenuCount(); | |
713 | for ( size_t i = 0; i < count; i++ ) | |
714 | { | |
52af3158 | 715 | wxString title2 = GetMenuLabel(i); |
52130557 | 716 | if ( (title2 == title) || |
52af3158 | 717 | (wxMenuItem::GetLabelText(title2) == label) ) |
52130557 VZ |
718 | { |
719 | // found | |
2b5f62a0 | 720 | return (int)i; |
52130557 VZ |
721 | } |
722 | } | |
723 | ||
724 | return wxNOT_FOUND; | |
725 | ||
726 | } | |
727 | ||
1e6feb95 VZ |
728 | // ---------------------------------------------------------------------------- |
729 | // wxMenuBar attaching/detaching to/from the frame | |
730 | // ---------------------------------------------------------------------------- | |
731 | ||
732 | void wxMenuBarBase::Attach(wxFrame *frame) | |
733 | { | |
734 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
735 | ||
736 | m_menuBarFrame = frame; | |
737 | } | |
738 | ||
739 | void wxMenuBarBase::Detach() | |
740 | { | |
741 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
742 | ||
743 | m_menuBarFrame = NULL; | |
744 | } | |
745 | ||
746 | // ---------------------------------------------------------------------------- | |
747 | // wxMenuBar searching for items | |
748 | // ---------------------------------------------------------------------------- | |
749 | ||
750 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
751 | { | |
752 | if ( menu ) | |
753 | *menu = NULL; | |
754 | ||
755 | wxMenuItem *item = NULL; | |
222ed1d6 MB |
756 | size_t count = GetMenuCount(), i; |
757 | wxMenuList::const_iterator it; | |
758 | for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ ) | |
1e6feb95 | 759 | { |
222ed1d6 | 760 | item = (*it)->FindItem(id, menu); |
1e6feb95 VZ |
761 | } |
762 | ||
763 | return item; | |
764 | } | |
765 | ||
766 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
767 | { | |
52af3158 | 768 | wxString label = wxMenuItem::GetLabelText(menu); |
1e6feb95 VZ |
769 | |
770 | int i = 0; | |
222ed1d6 | 771 | wxMenuList::compatibility_iterator node; |
1e6feb95 VZ |
772 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) |
773 | { | |
52af3158 | 774 | if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) ) |
1e6feb95 VZ |
775 | return node->GetData()->FindItem(item); |
776 | } | |
777 | ||
778 | return wxNOT_FOUND; | |
779 | } | |
780 | ||
3dfac970 VZ |
781 | // --------------------------------------------------------------------------- |
782 | // wxMenuBar functions forwarded to wxMenuItem | |
783 | // --------------------------------------------------------------------------- | |
784 | ||
785 | void wxMenuBarBase::Enable(int id, bool enable) | |
786 | { | |
787 | wxMenuItem *item = FindItem(id); | |
788 | ||
789 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
790 | ||
791 | item->Enable(enable); | |
792 | } | |
793 | ||
794 | void wxMenuBarBase::Check(int id, bool check) | |
795 | { | |
796 | wxMenuItem *item = FindItem(id); | |
797 | ||
798 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
799 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
800 | ||
801 | item->Check(check); | |
802 | } | |
803 | ||
804 | bool wxMenuBarBase::IsChecked(int id) const | |
805 | { | |
806 | wxMenuItem *item = FindItem(id); | |
807 | ||
4e32eea1 | 808 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") ); |
3dfac970 VZ |
809 | |
810 | return item->IsChecked(); | |
811 | } | |
812 | ||
813 | bool wxMenuBarBase::IsEnabled(int id) const | |
814 | { | |
815 | wxMenuItem *item = FindItem(id); | |
816 | ||
4e32eea1 | 817 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") ); |
3dfac970 VZ |
818 | |
819 | return item->IsEnabled(); | |
820 | } | |
821 | ||
822 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
823 | { | |
824 | wxMenuItem *item = FindItem(id); | |
825 | ||
826 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
827 | ||
52af3158 | 828 | item->SetItemLabel(label); |
3dfac970 VZ |
829 | } |
830 | ||
831 | wxString wxMenuBarBase::GetLabel(int id) const | |
832 | { | |
833 | wxMenuItem *item = FindItem(id); | |
834 | ||
835 | wxCHECK_MSG( item, wxEmptyString, | |
836 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
837 | ||
52af3158 | 838 | return item->GetItemLabel(); |
3dfac970 VZ |
839 | } |
840 | ||
841 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
842 | { | |
843 | wxMenuItem *item = FindItem(id); | |
844 | ||
845 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
846 | ||
847 | item->SetHelp(helpString); | |
848 | } | |
849 | ||
850 | wxString wxMenuBarBase::GetHelpString(int id) const | |
851 | { | |
852 | wxMenuItem *item = FindItem(id); | |
853 | ||
854 | wxCHECK_MSG( item, wxEmptyString, | |
855 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
856 | ||
857 | return item->GetHelp(); | |
858 | } | |
859 | ||
9cd28f48 | 860 | void wxMenuBarBase::UpdateMenus() |
4d538595 DS |
861 | { |
862 | wxEvtHandler* source; | |
863 | wxMenu* menu; | |
864 | int nCount = GetMenuCount(); | |
865 | for (int n = 0; n < nCount; n++) | |
866 | { | |
867 | menu = GetMenu( n ); | |
868 | if (menu != NULL) | |
869 | { | |
870 | source = menu->GetEventHandler(); | |
871 | if (source != NULL) | |
872 | menu->UpdateUI( source ); | |
873 | } | |
874 | } | |
875 | } | |
876 | ||
68bc148c JS |
877 | #if WXWIN_COMPATIBILITY_2_8 |
878 | // get or change the label of the menu at given position | |
879 | void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label) | |
880 | { | |
881 | SetMenuLabel(pos, label); | |
882 | } | |
883 | ||
884 | wxString wxMenuBarBase::GetLabelTop(size_t pos) const | |
885 | { | |
c4e43bea | 886 | return GetMenuLabelText(pos); |
68bc148c JS |
887 | } |
888 | #endif | |
889 | ||
1e6feb95 | 890 | #endif // wxUSE_MENUS |