]>
Commit | Line | Data |
---|---|---|
3dfac970 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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) wxWindows team | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "menubase.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/menu.h" | |
33 | #endif | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // template lists | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | #include "wx/listimpl.cpp" | |
717a57c2 | 40 | |
3dfac970 | 41 | WX_DEFINE_LIST(wxMenuList); |
717a57c2 | 42 | WX_DEFINE_LIST(wxMenuItemList); |
3dfac970 VZ |
43 | |
44 | // ============================================================================ | |
45 | // implementation | |
46 | // ============================================================================ | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
717a57c2 VZ |
49 | // wxMenuItem |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | wxMenuItemBase::~wxMenuItemBase() | |
53 | { | |
a583e623 RR |
54 | if (m_subMenu) |
55 | delete m_subMenu; | |
717a57c2 VZ |
56 | } |
57 | ||
58 | #if wxUSE_ACCEL | |
59 | ||
60 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) | |
61 | { | |
62 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
63 | if ( accel ) | |
64 | { | |
65 | text += wxT('\t'); | |
66 | ||
67 | int flags = accel->GetFlags(); | |
68 | if ( flags & wxACCEL_ALT ) | |
69 | text += wxT("Alt-"); | |
70 | if ( flags & wxACCEL_CTRL ) | |
71 | text += wxT("Ctrl-"); | |
72 | if ( flags & wxACCEL_SHIFT ) | |
73 | text += wxT("Shift-"); | |
74 | ||
75 | int code = accel->GetKeyCode(); | |
76 | switch ( code ) | |
77 | { | |
78 | case WXK_F1: | |
79 | case WXK_F2: | |
80 | case WXK_F3: | |
81 | case WXK_F4: | |
82 | case WXK_F5: | |
83 | case WXK_F6: | |
84 | case WXK_F7: | |
85 | case WXK_F8: | |
86 | case WXK_F9: | |
87 | case WXK_F10: | |
88 | case WXK_F11: | |
89 | case WXK_F12: | |
90 | text << wxT('F') << code - WXK_F1 + 1; | |
91 | break; | |
92 | ||
93 | // if there are any other keys wxGetAccelFromString() may return, | |
94 | // we should process them here | |
95 | ||
96 | default: | |
97 | if ( wxIsalnum(code) ) | |
98 | { | |
99 | text << (wxChar)code; | |
100 | ||
101 | break; | |
102 | } | |
103 | ||
104 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
105 | } | |
106 | } | |
107 | ||
108 | SetText(text); | |
109 | } | |
110 | ||
111 | #endif // wxUSE_ACCEL | |
112 | ||
113 | // ---------------------------------------------------------------------------- | |
114 | // wxMenu ctor and dtor | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
117 | void wxMenuBase::Init(long style) | |
118 | { | |
119 | m_items.DeleteContents(TRUE); | |
120 | ||
121 | m_menuBar = (wxMenuBar *)NULL; | |
122 | m_menuParent = (wxMenu *)NULL; | |
123 | ||
124 | m_invokingWindow = (wxWindow *)NULL; | |
125 | m_style = style; | |
126 | m_clientData = (void *)NULL; | |
127 | m_eventHandler = this; | |
5b0d48b3 | 128 | m_callback = (wxFunction) NULL; |
717a57c2 VZ |
129 | } |
130 | ||
131 | wxMenuBase::~wxMenuBase() | |
132 | { | |
a583e623 RR |
133 | // nothing to do, wxMenuItemList dtor will delete the menu items. |
134 | // Actually, in GTK, the submenus have to get deleted first. | |
717a57c2 VZ |
135 | } |
136 | ||
137 | // ---------------------------------------------------------------------------- | |
138 | // wxMenu item adding/removing | |
139 | // ---------------------------------------------------------------------------- | |
140 | ||
141 | bool wxMenuBase::DoAppend(wxMenuItem *item) | |
142 | { | |
143 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Append()") ); | |
144 | ||
145 | m_items.Append(item); | |
146 | ||
147 | return TRUE; | |
148 | } | |
149 | ||
150 | bool wxMenuBase::Insert(size_t pos, wxMenuItem *item) | |
151 | { | |
152 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert") ); | |
153 | wxCHECK_MSG( pos < GetMenuItemCount(), FALSE, | |
154 | wxT("invalid index in wxMenu::Insert") ); | |
155 | ||
156 | return DoInsert(pos, item); | |
157 | } | |
158 | ||
159 | bool wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) | |
160 | { | |
161 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert()") ); | |
162 | ||
163 | wxMenuItemList::Node *node = m_items.Item(pos); | |
164 | wxCHECK_MSG( node, FALSE, wxT("invalid index in wxMenu::Insert()") ); | |
165 | ||
166 | m_items.Insert(node, item); | |
167 | ||
168 | return TRUE; | |
169 | } | |
170 | ||
171 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
172 | { | |
173 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
174 | ||
175 | return DoRemove(item); | |
176 | } | |
177 | ||
178 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
179 | { | |
180 | wxMenuItemList::Node *node = m_items.Find(item); | |
181 | ||
182 | // if we get here, the item is valid or one of Remove() functions is broken | |
183 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
184 | ||
185 | // we detach the item, but we do delete the list node (i.e. don't call | |
186 | // DetachNode() here!) | |
187 | node->SetData((wxMenuItem *)NULL); // to prevent it from deleting the item | |
188 | m_items.DeleteNode(node); | |
189 | ||
190 | // item isn't attached to anything any more | |
191 | wxMenu *submenu = item->GetSubMenu(); | |
192 | if ( submenu ) | |
193 | { | |
194 | submenu->SetParent((wxMenu *)NULL); | |
195 | } | |
196 | ||
197 | return item; | |
198 | } | |
199 | ||
200 | bool wxMenuBase::Delete(wxMenuItem *item) | |
201 | { | |
202 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Delete") ); | |
203 | ||
204 | return DoDelete(item); | |
205 | } | |
206 | ||
207 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
208 | { | |
209 | wxMenuItem *item2 = DoRemove(item); | |
210 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
211 | ||
212 | // don't delete the submenu | |
213 | item2->SetSubMenu((wxMenu *)NULL); | |
214 | ||
215 | delete item2; | |
216 | ||
217 | return TRUE; | |
218 | } | |
219 | ||
220 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
221 | { | |
222 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Destroy") ); | |
223 | ||
224 | return DoDestroy(item); | |
225 | } | |
226 | ||
227 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
228 | { | |
229 | wxMenuItem *item2 = DoRemove(item); | |
230 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
231 | ||
232 | delete item2; | |
233 | ||
234 | return TRUE; | |
235 | } | |
236 | ||
237 | // ---------------------------------------------------------------------------- | |
238 | // wxMenu searching for items | |
239 | // ---------------------------------------------------------------------------- | |
240 | ||
241 | // Finds the item id matching the given string, -1 if not found. | |
242 | int wxMenuBase::FindItem(const wxString& text) const | |
243 | { | |
244 | wxString label = wxMenuItem(NULL, wxID_SEPARATOR, text).GetLabel(); | |
245 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
246 | node; | |
247 | node = node->GetNext() ) | |
248 | { | |
249 | wxMenuItem *item = node->GetData(); | |
250 | if ( item->IsSubMenu() ) | |
251 | { | |
252 | int rc = item->GetSubMenu()->FindItem(label); | |
253 | if ( rc != wxNOT_FOUND ) | |
254 | return rc; | |
255 | } | |
256 | else if ( !item->IsSeparator() ) | |
257 | { | |
258 | if ( item->GetLabel() == label ) | |
259 | return item->GetId(); | |
260 | } | |
261 | } | |
262 | ||
263 | return wxNOT_FOUND; | |
264 | } | |
265 | ||
266 | // recursive search for item by id | |
267 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
268 | { | |
269 | if ( itemMenu ) | |
270 | *itemMenu = NULL; | |
271 | ||
272 | wxMenuItem *item = NULL; | |
273 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
274 | node && !item; | |
275 | node = node->GetNext() ) | |
276 | { | |
277 | item = node->GetData(); | |
278 | ||
279 | if ( item->GetId() == itemId ) | |
280 | { | |
281 | if ( itemMenu ) | |
282 | *itemMenu = (wxMenu *)this; | |
283 | } | |
284 | else if ( item->IsSubMenu() ) | |
285 | { | |
286 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
287 | } | |
288 | else | |
289 | { | |
290 | // don't exit the loop | |
291 | item = NULL; | |
292 | } | |
293 | } | |
294 | ||
295 | return item; | |
296 | } | |
297 | ||
298 | // non recursive search | |
299 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
300 | { | |
301 | wxMenuItem *item = (wxMenuItem *)NULL; | |
302 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
303 | ||
304 | size_t pos; | |
305 | for ( pos = 0; node; pos++ ) | |
306 | { | |
1dddf838 VZ |
307 | if ( node->GetData()->GetId() == id ) |
308 | { | |
309 | item = node->GetData(); | |
310 | ||
717a57c2 | 311 | break; |
1dddf838 | 312 | } |
717a57c2 VZ |
313 | |
314 | node = node->GetNext(); | |
315 | } | |
316 | ||
317 | if ( ppos ) | |
318 | { | |
1987af7e | 319 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
717a57c2 VZ |
320 | } |
321 | ||
322 | return item; | |
323 | } | |
324 | ||
325 | // ---------------------------------------------------------------------------- | |
326 | // wxMenu helpers | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
329 | // Update a menu and all submenus recursively. source is the object that has | |
330 | // the update event handlers defined for it. If NULL, the menu or associated | |
331 | // window will be used. | |
332 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
333 | { | |
334 | if ( !source && GetInvokingWindow() ) | |
335 | source = GetInvokingWindow()->GetEventHandler(); | |
336 | if ( !source ) | |
337 | source = GetEventHandler(); | |
338 | if ( !source ) | |
339 | source = this; | |
340 | ||
341 | wxMenuItemList::Node* node = GetMenuItems().GetFirst(); | |
342 | while ( node ) | |
343 | { | |
344 | wxMenuItem* item = node->GetData(); | |
345 | if ( !item->IsSeparator() ) | |
346 | { | |
347 | wxWindowID id = item->GetId(); | |
348 | wxUpdateUIEvent event(id); | |
349 | event.SetEventObject( source ); | |
350 | ||
351 | if ( source->ProcessEvent(event) ) | |
352 | { | |
353 | // if anything changed, update the chanegd attribute | |
354 | if (event.GetSetText()) | |
355 | SetLabel(id, event.GetText()); | |
356 | if (event.GetSetChecked()) | |
357 | Check(id, event.GetChecked()); | |
358 | if (event.GetSetEnabled()) | |
359 | Enable(id, event.GetEnabled()); | |
360 | } | |
361 | ||
362 | // recurse to the submenus | |
363 | if ( item->GetSubMenu() ) | |
364 | item->GetSubMenu()->UpdateUI(source); | |
365 | } | |
366 | //else: item is a separator (which don't process update UI events) | |
367 | ||
368 | node = node->GetNext(); | |
369 | } | |
370 | } | |
371 | ||
372 | // ---------------------------------------------------------------------------- | |
373 | // wxMenu functions forwarded to wxMenuItem | |
374 | // ---------------------------------------------------------------------------- | |
375 | ||
376 | void wxMenuBase::Enable( int id, bool enable ) | |
377 | { | |
378 | wxMenuItem *item = FindItem(id); | |
379 | ||
380 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
381 | ||
382 | item->Enable(enable); | |
383 | } | |
384 | ||
385 | bool wxMenuBase::IsEnabled( int id ) const | |
386 | { | |
387 | wxMenuItem *item = FindItem(id); | |
388 | ||
389 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") ); | |
390 | ||
391 | return item->IsEnabled(); | |
392 | } | |
393 | ||
394 | void wxMenuBase::Check( int id, bool enable ) | |
395 | { | |
396 | wxMenuItem *item = FindItem(id); | |
397 | ||
398 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
399 | ||
400 | item->Check(enable); | |
401 | } | |
402 | ||
403 | bool wxMenuBase::IsChecked( int id ) const | |
404 | { | |
405 | wxMenuItem *item = FindItem(id); | |
406 | ||
407 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") ); | |
408 | ||
409 | return item->IsChecked(); | |
410 | } | |
411 | ||
412 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
413 | { | |
414 | wxMenuItem *item = FindItem(id); | |
415 | ||
416 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
417 | ||
418 | item->SetText(label); | |
419 | } | |
420 | ||
421 | wxString wxMenuBase::GetLabel( int id ) const | |
422 | { | |
423 | wxMenuItem *item = FindItem(id); | |
424 | ||
425 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") ); | |
426 | ||
427 | return item->GetText(); | |
428 | } | |
429 | ||
430 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
431 | { | |
432 | wxMenuItem *item = FindItem(id); | |
433 | ||
434 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
435 | ||
436 | item->SetHelp( helpString ); | |
437 | } | |
438 | ||
439 | wxString wxMenuBase::GetHelpString( int id ) const | |
440 | { | |
441 | wxMenuItem *item = FindItem(id); | |
442 | ||
443 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") ); | |
444 | ||
445 | return item->GetHelp(); | |
446 | } | |
447 | ||
448 | // ---------------------------------------------------------------------------- | |
449 | // wxMenuBarBase ctor and dtor | |
3dfac970 VZ |
450 | // ---------------------------------------------------------------------------- |
451 | ||
452 | wxMenuBarBase::wxMenuBarBase() | |
453 | { | |
454 | // we own the menus when we get them | |
455 | m_menus.DeleteContents(TRUE); | |
456 | } | |
457 | ||
458 | wxMenuBarBase::~wxMenuBarBase() | |
459 | { | |
460 | // nothing to do, the list will delete the menus because of the call to | |
461 | // DeleteContents() above | |
462 | } | |
463 | ||
464 | // ---------------------------------------------------------------------------- | |
465 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
466 | // derived class should reflect the changes in the real menubar | |
467 | // ---------------------------------------------------------------------------- | |
468 | ||
469 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
470 | { | |
471 | wxMenuList::Node *node = m_menus.Item(pos); | |
472 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); | |
473 | ||
474 | return node->GetData(); | |
475 | } | |
476 | ||
477 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
478 | { | |
479 | wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") ); | |
480 | ||
481 | m_menus.Append(menu); | |
482 | ||
483 | return TRUE; | |
484 | } | |
485 | ||
486 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
487 | const wxString& WXUNUSED(title)) | |
488 | { | |
489 | wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") ); | |
490 | ||
491 | wxMenuList::Node *node = m_menus.Item(pos); | |
492 | wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") ); | |
493 | ||
494 | m_menus.Insert(node, menu); | |
495 | ||
496 | return TRUE; | |
497 | } | |
498 | ||
499 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
500 | const wxString& WXUNUSED(title)) | |
501 | { | |
502 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
503 | ||
504 | wxMenuList::Node *node = m_menus.Item(pos); | |
505 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); | |
506 | ||
507 | wxMenu *menuOld = node->GetData(); | |
508 | node->SetData(menu); | |
509 | ||
510 | return menuOld; | |
511 | } | |
512 | ||
513 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
514 | { | |
515 | wxMenuList::Node *node = m_menus.Item(pos); | |
516 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); | |
517 | ||
518 | node = m_menus.DetachNode(node); | |
519 | wxCHECK( node, NULL ); // unexpected | |
520 | wxMenu *menu = node->GetData(); | |
521 | ||
522 | delete node; | |
523 | ||
524 | return menu; | |
525 | } | |
526 | ||
527 | // --------------------------------------------------------------------------- | |
528 | // wxMenuBar functions forwarded to wxMenuItem | |
529 | // --------------------------------------------------------------------------- | |
530 | ||
531 | void wxMenuBarBase::Enable(int id, bool enable) | |
532 | { | |
533 | wxMenuItem *item = FindItem(id); | |
534 | ||
535 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
536 | ||
537 | item->Enable(enable); | |
538 | } | |
539 | ||
540 | void wxMenuBarBase::Check(int id, bool check) | |
541 | { | |
542 | wxMenuItem *item = FindItem(id); | |
543 | ||
544 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
545 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
546 | ||
547 | item->Check(check); | |
548 | } | |
549 | ||
550 | bool wxMenuBarBase::IsChecked(int id) const | |
551 | { | |
552 | wxMenuItem *item = FindItem(id); | |
553 | ||
554 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") ); | |
555 | ||
556 | return item->IsChecked(); | |
557 | } | |
558 | ||
559 | bool wxMenuBarBase::IsEnabled(int id) const | |
560 | { | |
561 | wxMenuItem *item = FindItem(id); | |
562 | ||
563 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") ); | |
564 | ||
565 | return item->IsEnabled(); | |
566 | } | |
567 | ||
568 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
569 | { | |
570 | wxMenuItem *item = FindItem(id); | |
571 | ||
572 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
573 | ||
574 | item->SetText(label); | |
575 | } | |
576 | ||
577 | wxString wxMenuBarBase::GetLabel(int id) const | |
578 | { | |
579 | wxMenuItem *item = FindItem(id); | |
580 | ||
581 | wxCHECK_MSG( item, wxEmptyString, | |
582 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
583 | ||
584 | return item->GetText(); | |
585 | } | |
586 | ||
587 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
588 | { | |
589 | wxMenuItem *item = FindItem(id); | |
590 | ||
591 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
592 | ||
593 | item->SetHelp(helpString); | |
594 | } | |
595 | ||
596 | wxString wxMenuBarBase::GetHelpString(int id) const | |
597 | { | |
598 | wxMenuItem *item = FindItem(id); | |
599 | ||
600 | wxCHECK_MSG( item, wxEmptyString, | |
601 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
602 | ||
603 | return item->GetHelp(); | |
604 | } | |
605 |