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