]>
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 | // notice that parentMenu can be NULL: the item can be attached to the menu | |
62 | // later with SetMenu() | |
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) | |
71 | m_id = wxWindow::NewControlId(); | |
72 | if (m_id == wxID_SEPARATOR) | |
73 | m_kind = wxITEM_SEPARATOR; | |
74 | ||
75 | SetItemLabel(text); | |
76 | SetHelp(help); | |
77 | } | |
78 | ||
79 | wxMenuItemBase::~wxMenuItemBase() | |
80 | { | |
81 | delete m_subMenu; | |
82 | } | |
83 | ||
84 | #if wxUSE_ACCEL | |
85 | ||
86 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const | |
87 | { | |
88 | return wxAcceleratorEntry::Create(GetItemLabel()); | |
89 | } | |
90 | ||
91 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) | |
92 | { | |
93 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
94 | if ( accel ) | |
95 | { | |
96 | text += wxT('\t'); | |
97 | text += accel->ToString(); | |
98 | } | |
99 | ||
100 | SetItemLabel(text); | |
101 | } | |
102 | ||
103 | #endif // wxUSE_ACCEL | |
104 | ||
105 | void wxMenuItemBase::SetItemLabel(const wxString& str) | |
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 | ||
129 | #ifndef __WXPM__ | |
130 | wxString wxMenuItemBase::GetLabelText(const wxString& text) | |
131 | { | |
132 | return wxStripMenuCodes(text); | |
133 | } | |
134 | #endif | |
135 | ||
136 | #if WXWIN_COMPATIBILITY_2_8 | |
137 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
138 | { | |
139 | return GetLabelText(text); | |
140 | } | |
141 | #endif | |
142 | ||
143 | bool wxMenuBase::ms_locked = true; | |
144 | ||
145 | // ---------------------------------------------------------------------------- | |
146 | // wxMenu ctor and dtor | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
149 | void wxMenuBase::Init(long style) | |
150 | { | |
151 | m_menuBar = (wxMenuBar *)NULL; | |
152 | m_menuParent = (wxMenu *)NULL; | |
153 | ||
154 | m_invokingWindow = (wxWindow *)NULL; | |
155 | m_style = style; | |
156 | m_clientData = (void *)NULL; | |
157 | m_eventHandler = this; | |
158 | } | |
159 | ||
160 | wxMenuBase::~wxMenuBase() | |
161 | { | |
162 | WX_CLEAR_LIST(wxMenuItemList, m_items); | |
163 | } | |
164 | ||
165 | // ---------------------------------------------------------------------------- | |
166 | // wxMenu item adding/removing | |
167 | // ---------------------------------------------------------------------------- | |
168 | ||
169 | void wxMenuBase::AddSubMenu(wxMenu *submenu) | |
170 | { | |
171 | wxCHECK_RET( submenu, _T("can't add a NULL submenu") ); | |
172 | ||
173 | submenu->SetParent((wxMenu *)this); | |
174 | } | |
175 | ||
176 | wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item) | |
177 | { | |
178 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") ); | |
179 | ||
180 | m_items.Append(item); | |
181 | item->SetMenu((wxMenu*)this); | |
182 | if ( item->IsSubMenu() ) | |
183 | { | |
184 | AddSubMenu(item->GetSubMenu()); | |
185 | } | |
186 | ||
187 | return item; | |
188 | } | |
189 | ||
190 | wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item) | |
191 | { | |
192 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") ); | |
193 | ||
194 | if ( pos == GetMenuItemCount() ) | |
195 | { | |
196 | return DoAppend(item); | |
197 | } | |
198 | else | |
199 | { | |
200 | wxCHECK_MSG( pos < GetMenuItemCount(), NULL, | |
201 | wxT("invalid index in wxMenu::Insert") ); | |
202 | ||
203 | return DoInsert(pos, item); | |
204 | } | |
205 | } | |
206 | ||
207 | wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) | |
208 | { | |
209 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") ); | |
210 | ||
211 | wxMenuItemList::compatibility_iterator node = m_items.Item(pos); | |
212 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") ); | |
213 | ||
214 | m_items.Insert(node, item); | |
215 | item->SetMenu((wxMenu*)this); | |
216 | if ( item->IsSubMenu() ) | |
217 | { | |
218 | AddSubMenu(item->GetSubMenu()); | |
219 | } | |
220 | ||
221 | return item; | |
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 | { | |
233 | wxMenuItemList::compatibility_iterator node = m_items.Find(item); | |
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!) | |
240 | m_items.Erase(node); | |
241 | ||
242 | // item isn't attached to anything any more | |
243 | item->SetMenu((wxMenu *)NULL); | |
244 | wxMenu *submenu = item->GetSubMenu(); | |
245 | if ( submenu ) | |
246 | { | |
247 | submenu->SetParent((wxMenu *)NULL); | |
248 | if ( submenu->IsAttached() ) | |
249 | submenu->Detach(); | |
250 | } | |
251 | ||
252 | return item; | |
253 | } | |
254 | ||
255 | bool wxMenuBase::Delete(wxMenuItem *item) | |
256 | { | |
257 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") ); | |
258 | ||
259 | return DoDelete(item); | |
260 | } | |
261 | ||
262 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
263 | { | |
264 | wxMenuItem *item2 = DoRemove(item); | |
265 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); | |
266 | ||
267 | // don't delete the submenu | |
268 | item2->SetSubMenu((wxMenu *)NULL); | |
269 | ||
270 | delete item2; | |
271 | ||
272 | return true; | |
273 | } | |
274 | ||
275 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
276 | { | |
277 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") ); | |
278 | ||
279 | return DoDestroy(item); | |
280 | } | |
281 | ||
282 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
283 | { | |
284 | wxMenuItem *item2 = DoRemove(item); | |
285 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); | |
286 | ||
287 | delete item2; | |
288 | ||
289 | return true; | |
290 | } | |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // wxMenu searching for items | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
296 | // Finds the item id matching the given string, wxNOT_FOUND if not found. | |
297 | int wxMenuBase::FindItem(const wxString& text) const | |
298 | { | |
299 | wxString label = wxMenuItem::GetLabelText(text); | |
300 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
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 | } | |
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() ) | |
315 | { | |
316 | if ( item->GetItemLabelText() == label ) | |
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; | |
331 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
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 | { | |
359 | wxMenuItem *item = (wxMenuItem *)NULL; | |
360 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
361 | ||
362 | size_t pos; | |
363 | for ( pos = 0; node; pos++ ) | |
364 | { | |
365 | if ( node->GetData()->GetId() == id ) | |
366 | { | |
367 | item = node->GetData(); | |
368 | ||
369 | break; | |
370 | } | |
371 | ||
372 | node = node->GetNext(); | |
373 | } | |
374 | ||
375 | if ( ppos ) | |
376 | { | |
377 | *ppos = item ? pos : (size_t)wxNOT_FOUND; | |
378 | } | |
379 | ||
380 | return item; | |
381 | } | |
382 | ||
383 | // find by position | |
384 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
385 | { | |
386 | wxCHECK_MSG( position < m_items.GetCount(), NULL, | |
387 | _T("wxMenu::FindItemByPosition(): invalid menu index") ); | |
388 | ||
389 | return m_items.Item( position )->GetData(); | |
390 | } | |
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // wxMenu helpers used by derived classes | |
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 | { | |
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 | ||
410 | if ( !source && GetInvokingWindow() ) | |
411 | source = GetInvokingWindow()->GetEventHandler(); | |
412 | if ( !source ) | |
413 | source = GetEventHandler(); | |
414 | if ( !source ) | |
415 | source = this; | |
416 | ||
417 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
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 | { | |
429 | // if anything changed, update the changed attribute | |
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 | } | |
442 | //else: item is a separator (which doesn't process update UI events) | |
443 | ||
444 | node = node->GetNext(); | |
445 | } | |
446 | } | |
447 | ||
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 | ||
454 | bool processed = false; | |
455 | ||
456 | // Try the menu's event handler | |
457 | // if ( !processed ) | |
458 | { | |
459 | wxEvtHandler *handler = GetEventHandler(); | |
460 | if ( handler ) | |
461 | processed = handler->SafelyProcessEvent(event); | |
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 | { | |
474 | processed = win->HandleWindowEvent(event); | |
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 | ||
489 | wxMenuBar* wxMenuBase::GetMenuBar() const | |
490 | { | |
491 | if(GetParent()) | |
492 | return GetParent()->GetMenuBar(); | |
493 | return m_menuBar; | |
494 | } | |
495 | ||
496 | void wxMenuBase::Attach(wxMenuBarBase *menubar) | |
497 | { | |
498 | // use Detach() instead! | |
499 | wxASSERT_MSG( menubar, _T("menu can't be attached to NULL menubar") ); | |
500 | ||
501 | // use IsAttached() to prevent this from happening | |
502 | wxASSERT_MSG( !m_menuBar, _T("attaching menu twice?") ); | |
503 | ||
504 | m_menuBar = (wxMenuBar *)menubar; | |
505 | } | |
506 | ||
507 | void wxMenuBase::Detach() | |
508 | { | |
509 | // use IsAttached() to prevent this from happening | |
510 | wxASSERT_MSG( m_menuBar, _T("detaching unattached menu?") ); | |
511 | ||
512 | m_menuBar = NULL; | |
513 | } | |
514 | ||
515 | // ---------------------------------------------------------------------------- | |
516 | // wxMenu functions forwarded to wxMenuItem | |
517 | // ---------------------------------------------------------------------------- | |
518 | ||
519 | void wxMenuBase::Enable( int id, bool enable ) | |
520 | { | |
521 | wxMenuItem *item = FindItem(id); | |
522 | ||
523 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
524 | ||
525 | item->Enable(enable); | |
526 | } | |
527 | ||
528 | bool wxMenuBase::IsEnabled( int id ) const | |
529 | { | |
530 | wxMenuItem *item = FindItem(id); | |
531 | ||
532 | wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") ); | |
533 | ||
534 | return item->IsEnabled(); | |
535 | } | |
536 | ||
537 | void wxMenuBase::Check( int id, bool enable ) | |
538 | { | |
539 | wxMenuItem *item = FindItem(id); | |
540 | ||
541 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
542 | ||
543 | item->Check(enable); | |
544 | } | |
545 | ||
546 | bool wxMenuBase::IsChecked( int id ) const | |
547 | { | |
548 | wxMenuItem *item = FindItem(id); | |
549 | ||
550 | wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") ); | |
551 | ||
552 | return item->IsChecked(); | |
553 | } | |
554 | ||
555 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
556 | { | |
557 | wxMenuItem *item = FindItem(id); | |
558 | ||
559 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
560 | ||
561 | item->SetItemLabel(label); | |
562 | } | |
563 | ||
564 | wxString wxMenuBase::GetLabel( int id ) const | |
565 | { | |
566 | wxMenuItem *item = FindItem(id); | |
567 | ||
568 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") ); | |
569 | ||
570 | return item->GetItemLabel(); | |
571 | } | |
572 | ||
573 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
574 | { | |
575 | wxMenuItem *item = FindItem(id); | |
576 | ||
577 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
578 | ||
579 | item->SetHelp( helpString ); | |
580 | } | |
581 | ||
582 | wxString wxMenuBase::GetHelpString( int id ) const | |
583 | { | |
584 | wxMenuItem *item = FindItem(id); | |
585 | ||
586 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") ); | |
587 | ||
588 | return item->GetHelp(); | |
589 | } | |
590 | ||
591 | // ---------------------------------------------------------------------------- | |
592 | // wxMenuBarBase ctor and dtor | |
593 | // ---------------------------------------------------------------------------- | |
594 | ||
595 | wxMenuBarBase::wxMenuBarBase() | |
596 | { | |
597 | // not attached yet | |
598 | m_menuBarFrame = NULL; | |
599 | } | |
600 | ||
601 | wxMenuBarBase::~wxMenuBarBase() | |
602 | { | |
603 | WX_CLEAR_LIST(wxMenuList, m_menus); | |
604 | } | |
605 | ||
606 | // ---------------------------------------------------------------------------- | |
607 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
608 | // derived class should reflect the changes in the real menubar | |
609 | // ---------------------------------------------------------------------------- | |
610 | ||
611 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
612 | { | |
613 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
614 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); | |
615 | ||
616 | return node->GetData(); | |
617 | } | |
618 | ||
619 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
620 | { | |
621 | wxCHECK_MSG( menu, false, wxT("can't append NULL menu") ); | |
622 | ||
623 | m_menus.Append(menu); | |
624 | menu->Attach(this); | |
625 | ||
626 | return true; | |
627 | } | |
628 | ||
629 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
630 | const wxString& title) | |
631 | { | |
632 | if ( pos == m_menus.GetCount() ) | |
633 | { | |
634 | return wxMenuBarBase::Append(menu, title); | |
635 | } | |
636 | else // not at the end | |
637 | { | |
638 | wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") ); | |
639 | ||
640 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
641 | wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") ); | |
642 | ||
643 | m_menus.Insert(node, menu); | |
644 | menu->Attach(this); | |
645 | ||
646 | return true; | |
647 | } | |
648 | } | |
649 | ||
650 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
651 | const wxString& WXUNUSED(title)) | |
652 | { | |
653 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
654 | ||
655 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
656 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); | |
657 | ||
658 | wxMenu *menuOld = node->GetData(); | |
659 | node->SetData(menu); | |
660 | ||
661 | menu->Attach(this); | |
662 | menuOld->Detach(); | |
663 | ||
664 | return menuOld; | |
665 | } | |
666 | ||
667 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
668 | { | |
669 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); | |
670 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); | |
671 | ||
672 | wxMenu *menu = node->GetData(); | |
673 | m_menus.Erase(node); | |
674 | menu->Detach(); | |
675 | ||
676 | return menu; | |
677 | } | |
678 | ||
679 | int wxMenuBarBase::FindMenu(const wxString& title) const | |
680 | { | |
681 | wxString label = wxMenuItem::GetLabelText(title); | |
682 | ||
683 | size_t count = GetMenuCount(); | |
684 | for ( size_t i = 0; i < count; i++ ) | |
685 | { | |
686 | wxString title2 = GetMenuLabel(i); | |
687 | if ( (title2 == title) || | |
688 | (wxMenuItem::GetLabelText(title2) == label) ) | |
689 | { | |
690 | // found | |
691 | return (int)i; | |
692 | } | |
693 | } | |
694 | ||
695 | return wxNOT_FOUND; | |
696 | ||
697 | } | |
698 | ||
699 | // ---------------------------------------------------------------------------- | |
700 | // wxMenuBar attaching/detaching to/from the frame | |
701 | // ---------------------------------------------------------------------------- | |
702 | ||
703 | void wxMenuBarBase::Attach(wxFrame *frame) | |
704 | { | |
705 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
706 | ||
707 | m_menuBarFrame = frame; | |
708 | } | |
709 | ||
710 | void wxMenuBarBase::Detach() | |
711 | { | |
712 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
713 | ||
714 | m_menuBarFrame = NULL; | |
715 | } | |
716 | ||
717 | // ---------------------------------------------------------------------------- | |
718 | // wxMenuBar searching for items | |
719 | // ---------------------------------------------------------------------------- | |
720 | ||
721 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
722 | { | |
723 | if ( menu ) | |
724 | *menu = NULL; | |
725 | ||
726 | wxMenuItem *item = NULL; | |
727 | size_t count = GetMenuCount(), i; | |
728 | wxMenuList::const_iterator it; | |
729 | for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ ) | |
730 | { | |
731 | item = (*it)->FindItem(id, menu); | |
732 | } | |
733 | ||
734 | return item; | |
735 | } | |
736 | ||
737 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
738 | { | |
739 | wxString label = wxMenuItem::GetLabelText(menu); | |
740 | ||
741 | int i = 0; | |
742 | wxMenuList::compatibility_iterator node; | |
743 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) | |
744 | { | |
745 | if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) ) | |
746 | return node->GetData()->FindItem(item); | |
747 | } | |
748 | ||
749 | return wxNOT_FOUND; | |
750 | } | |
751 | ||
752 | // --------------------------------------------------------------------------- | |
753 | // wxMenuBar functions forwarded to wxMenuItem | |
754 | // --------------------------------------------------------------------------- | |
755 | ||
756 | void wxMenuBarBase::Enable(int id, bool enable) | |
757 | { | |
758 | wxMenuItem *item = FindItem(id); | |
759 | ||
760 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
761 | ||
762 | item->Enable(enable); | |
763 | } | |
764 | ||
765 | void wxMenuBarBase::Check(int id, bool check) | |
766 | { | |
767 | wxMenuItem *item = FindItem(id); | |
768 | ||
769 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
770 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
771 | ||
772 | item->Check(check); | |
773 | } | |
774 | ||
775 | bool wxMenuBarBase::IsChecked(int id) const | |
776 | { | |
777 | wxMenuItem *item = FindItem(id); | |
778 | ||
779 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") ); | |
780 | ||
781 | return item->IsChecked(); | |
782 | } | |
783 | ||
784 | bool wxMenuBarBase::IsEnabled(int id) const | |
785 | { | |
786 | wxMenuItem *item = FindItem(id); | |
787 | ||
788 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") ); | |
789 | ||
790 | return item->IsEnabled(); | |
791 | } | |
792 | ||
793 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
794 | { | |
795 | wxMenuItem *item = FindItem(id); | |
796 | ||
797 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
798 | ||
799 | item->SetItemLabel(label); | |
800 | } | |
801 | ||
802 | wxString wxMenuBarBase::GetLabel(int id) const | |
803 | { | |
804 | wxMenuItem *item = FindItem(id); | |
805 | ||
806 | wxCHECK_MSG( item, wxEmptyString, | |
807 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
808 | ||
809 | return item->GetItemLabel(); | |
810 | } | |
811 | ||
812 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
813 | { | |
814 | wxMenuItem *item = FindItem(id); | |
815 | ||
816 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
817 | ||
818 | item->SetHelp(helpString); | |
819 | } | |
820 | ||
821 | wxString wxMenuBarBase::GetHelpString(int id) const | |
822 | { | |
823 | wxMenuItem *item = FindItem(id); | |
824 | ||
825 | wxCHECK_MSG( item, wxEmptyString, | |
826 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
827 | ||
828 | return item->GetHelp(); | |
829 | } | |
830 | ||
831 | void wxMenuBarBase::UpdateMenus() | |
832 | { | |
833 | wxEvtHandler* source; | |
834 | wxMenu* menu; | |
835 | int nCount = GetMenuCount(); | |
836 | for (int n = 0; n < nCount; n++) | |
837 | { | |
838 | menu = GetMenu( n ); | |
839 | if (menu != NULL) | |
840 | { | |
841 | source = menu->GetEventHandler(); | |
842 | if (source != NULL) | |
843 | menu->UpdateUI( source ); | |
844 | } | |
845 | } | |
846 | } | |
847 | ||
848 | #if WXWIN_COMPATIBILITY_2_8 | |
849 | // get or change the label of the menu at given position | |
850 | void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label) | |
851 | { | |
852 | SetMenuLabel(pos, label); | |
853 | } | |
854 | ||
855 | wxString wxMenuBarBase::GetLabelTop(size_t pos) const | |
856 | { | |
857 | return GetMenuLabelText(pos); | |
858 | } | |
859 | #endif | |
860 | ||
861 | #endif // wxUSE_MENUS |