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