]>
Commit | Line | Data |
---|---|---|
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" | |
40 | ||
41 | WX_DEFINE_LIST(wxMenuList); | |
42 | WX_DEFINE_LIST(wxMenuItemList); | |
43 | ||
44 | // ============================================================================ | |
45 | // implementation | |
46 | // ============================================================================ | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // wxMenuItem | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | wxMenuItemBase::~wxMenuItemBase() | |
53 | { | |
54 | if (m_subMenu) | |
55 | delete m_subMenu; | |
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; | |
128 | } | |
129 | ||
130 | wxMenuBase::~wxMenuBase() | |
131 | { | |
132 | // nothing to do, wxMenuItemList dtor will delete the menu items. | |
133 | // Actually, in GTK, the submenus have to get deleted first. | |
134 | } | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | // wxMenu item adding/removing | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | bool wxMenuBase::DoAppend(wxMenuItem *item) | |
141 | { | |
142 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Append()") ); | |
143 | ||
144 | m_items.Append(item); | |
145 | ||
146 | return TRUE; | |
147 | } | |
148 | ||
149 | bool wxMenuBase::Insert(size_t pos, wxMenuItem *item) | |
150 | { | |
151 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert") ); | |
152 | wxCHECK_MSG( pos < GetMenuItemCount(), FALSE, | |
153 | wxT("invalid index in wxMenu::Insert") ); | |
154 | ||
155 | return DoInsert(pos, item); | |
156 | } | |
157 | ||
158 | bool wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) | |
159 | { | |
160 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert()") ); | |
161 | ||
162 | wxMenuItemList::Node *node = m_items.Item(pos); | |
163 | wxCHECK_MSG( node, FALSE, wxT("invalid index in wxMenu::Insert()") ); | |
164 | ||
165 | m_items.Insert(node, item); | |
166 | ||
167 | return TRUE; | |
168 | } | |
169 | ||
170 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
171 | { | |
172 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
173 | ||
174 | return DoRemove(item); | |
175 | } | |
176 | ||
177 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
178 | { | |
179 | wxMenuItemList::Node *node = m_items.Find(item); | |
180 | ||
181 | // if we get here, the item is valid or one of Remove() functions is broken | |
182 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
183 | ||
184 | // we detach the item, but we do delete the list node (i.e. don't call | |
185 | // DetachNode() here!) | |
186 | node->SetData((wxMenuItem *)NULL); // to prevent it from deleting the item | |
187 | m_items.DeleteNode(node); | |
188 | ||
189 | // item isn't attached to anything any more | |
190 | wxMenu *submenu = item->GetSubMenu(); | |
191 | if ( submenu ) | |
192 | { | |
193 | submenu->SetParent((wxMenu *)NULL); | |
194 | } | |
195 | ||
196 | return item; | |
197 | } | |
198 | ||
199 | bool wxMenuBase::Delete(wxMenuItem *item) | |
200 | { | |
201 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Delete") ); | |
202 | ||
203 | return DoDelete(item); | |
204 | } | |
205 | ||
206 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
207 | { | |
208 | wxMenuItem *item2 = DoRemove(item); | |
209 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
210 | ||
211 | // don't delete the submenu | |
212 | item2->SetSubMenu((wxMenu *)NULL); | |
213 | ||
214 | delete item2; | |
215 | ||
216 | return TRUE; | |
217 | } | |
218 | ||
219 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
220 | { | |
221 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Destroy") ); | |
222 | ||
223 | return DoDestroy(item); | |
224 | } | |
225 | ||
226 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
227 | { | |
228 | wxMenuItem *item2 = DoRemove(item); | |
229 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); | |
230 | ||
231 | delete item2; | |
232 | ||
233 | return TRUE; | |
234 | } | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // wxMenu searching for items | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | // Finds the item id matching the given string, -1 if not found. | |
241 | int wxMenuBase::FindItem(const wxString& text) const | |
242 | { | |
243 | wxString label = wxMenuItem(NULL, wxID_SEPARATOR, text).GetLabel(); | |
244 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
245 | node; | |
246 | node = node->GetNext() ) | |
247 | { | |
248 | wxMenuItem *item = node->GetData(); | |
249 | if ( item->IsSubMenu() ) | |
250 | { | |
251 | int rc = item->GetSubMenu()->FindItem(label); | |
252 | if ( rc != wxNOT_FOUND ) | |
253 | return rc; | |
254 | } | |
255 | else if ( !item->IsSeparator() ) | |
256 | { | |
257 | if ( item->GetLabel() == label ) | |
258 | return item->GetId(); | |
259 | } | |
260 | } | |
261 | ||
262 | return wxNOT_FOUND; | |
263 | } | |
264 | ||
265 | // recursive search for item by id | |
266 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
267 | { | |
268 | if ( itemMenu ) | |
269 | *itemMenu = NULL; | |
270 | ||
271 | wxMenuItem *item = NULL; | |
272 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); | |
273 | node && !item; | |
274 | node = node->GetNext() ) | |
275 | { | |
276 | item = node->GetData(); | |
277 | ||
278 | if ( item->GetId() == itemId ) | |
279 | { | |
280 | if ( itemMenu ) | |
281 | *itemMenu = (wxMenu *)this; | |
282 | } | |
283 | else if ( item->IsSubMenu() ) | |
284 | { | |
285 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
286 | } | |
287 | else | |
288 | { | |
289 | // don't exit the loop | |
290 | item = NULL; | |
291 | } | |
292 | } | |
293 | ||
294 | return item; | |
295 | } | |
296 | ||
297 | // non recursive search | |
298 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
299 | { | |
300 | wxMenuItem *item = (wxMenuItem *)NULL; | |
301 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
302 | ||
303 | size_t pos; | |
304 | for ( pos = 0; node; pos++ ) | |
305 | { | |
306 | if ( node->GetData()->GetId() == id ) | |
307 | { | |
308 | item = node->GetData(); | |
309 | ||
310 | break; | |
311 | } | |
312 | ||
313 | node = node->GetNext(); | |
314 | } | |
315 | ||
316 | if ( ppos ) | |
317 | { | |
318 | *ppos = item ? pos : (size_t)wxNOT_FOUND; | |
319 | } | |
320 | ||
321 | return item; | |
322 | } | |
323 | ||
324 | // ---------------------------------------------------------------------------- | |
325 | // wxMenu helpers | |
326 | // ---------------------------------------------------------------------------- | |
327 | ||
328 | // Update a menu and all submenus recursively. source is the object that has | |
329 | // the update event handlers defined for it. If NULL, the menu or associated | |
330 | // window will be used. | |
331 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
332 | { | |
333 | if ( !source && GetInvokingWindow() ) | |
334 | source = GetInvokingWindow()->GetEventHandler(); | |
335 | if ( !source ) | |
336 | source = GetEventHandler(); | |
337 | if ( !source ) | |
338 | source = this; | |
339 | ||
340 | wxMenuItemList::Node* node = GetMenuItems().GetFirst(); | |
341 | while ( node ) | |
342 | { | |
343 | wxMenuItem* item = node->GetData(); | |
344 | if ( !item->IsSeparator() ) | |
345 | { | |
346 | wxWindowID id = item->GetId(); | |
347 | wxUpdateUIEvent event(id); | |
348 | event.SetEventObject( source ); | |
349 | ||
350 | if ( source->ProcessEvent(event) ) | |
351 | { | |
352 | // if anything changed, update the chanegd attribute | |
353 | if (event.GetSetText()) | |
354 | SetLabel(id, event.GetText()); | |
355 | if (event.GetSetChecked()) | |
356 | Check(id, event.GetChecked()); | |
357 | if (event.GetSetEnabled()) | |
358 | Enable(id, event.GetEnabled()); | |
359 | } | |
360 | ||
361 | // recurse to the submenus | |
362 | if ( item->GetSubMenu() ) | |
363 | item->GetSubMenu()->UpdateUI(source); | |
364 | } | |
365 | //else: item is a separator (which don't process update UI events) | |
366 | ||
367 | node = node->GetNext(); | |
368 | } | |
369 | } | |
370 | ||
371 | // ---------------------------------------------------------------------------- | |
372 | // wxMenu functions forwarded to wxMenuItem | |
373 | // ---------------------------------------------------------------------------- | |
374 | ||
375 | void wxMenuBase::Enable( int id, bool enable ) | |
376 | { | |
377 | wxMenuItem *item = FindItem(id); | |
378 | ||
379 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
380 | ||
381 | item->Enable(enable); | |
382 | } | |
383 | ||
384 | bool wxMenuBase::IsEnabled( int id ) const | |
385 | { | |
386 | wxMenuItem *item = FindItem(id); | |
387 | ||
388 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") ); | |
389 | ||
390 | return item->IsEnabled(); | |
391 | } | |
392 | ||
393 | void wxMenuBase::Check( int id, bool enable ) | |
394 | { | |
395 | wxMenuItem *item = FindItem(id); | |
396 | ||
397 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
398 | ||
399 | item->Check(enable); | |
400 | } | |
401 | ||
402 | bool wxMenuBase::IsChecked( int id ) const | |
403 | { | |
404 | wxMenuItem *item = FindItem(id); | |
405 | ||
406 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") ); | |
407 | ||
408 | return item->IsChecked(); | |
409 | } | |
410 | ||
411 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
412 | { | |
413 | wxMenuItem *item = FindItem(id); | |
414 | ||
415 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
416 | ||
417 | item->SetText(label); | |
418 | } | |
419 | ||
420 | wxString wxMenuBase::GetLabel( int id ) const | |
421 | { | |
422 | wxMenuItem *item = FindItem(id); | |
423 | ||
424 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") ); | |
425 | ||
426 | return item->GetText(); | |
427 | } | |
428 | ||
429 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
430 | { | |
431 | wxMenuItem *item = FindItem(id); | |
432 | ||
433 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
434 | ||
435 | item->SetHelp( helpString ); | |
436 | } | |
437 | ||
438 | wxString wxMenuBase::GetHelpString( int id ) const | |
439 | { | |
440 | wxMenuItem *item = FindItem(id); | |
441 | ||
442 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") ); | |
443 | ||
444 | return item->GetHelp(); | |
445 | } | |
446 | ||
447 | // ---------------------------------------------------------------------------- | |
448 | // wxMenuBarBase ctor and dtor | |
449 | // ---------------------------------------------------------------------------- | |
450 | ||
451 | wxMenuBarBase::wxMenuBarBase() | |
452 | { | |
453 | // we own the menus when we get them | |
454 | m_menus.DeleteContents(TRUE); | |
455 | } | |
456 | ||
457 | wxMenuBarBase::~wxMenuBarBase() | |
458 | { | |
459 | // nothing to do, the list will delete the menus because of the call to | |
460 | // DeleteContents() above | |
461 | } | |
462 | ||
463 | // ---------------------------------------------------------------------------- | |
464 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
465 | // derived class should reflect the changes in the real menubar | |
466 | // ---------------------------------------------------------------------------- | |
467 | ||
468 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
469 | { | |
470 | wxMenuList::Node *node = m_menus.Item(pos); | |
471 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); | |
472 | ||
473 | return node->GetData(); | |
474 | } | |
475 | ||
476 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
477 | { | |
478 | wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") ); | |
479 | ||
480 | m_menus.Append(menu); | |
481 | ||
482 | return TRUE; | |
483 | } | |
484 | ||
485 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
486 | const wxString& WXUNUSED(title)) | |
487 | { | |
488 | wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") ); | |
489 | ||
490 | wxMenuList::Node *node = m_menus.Item(pos); | |
491 | wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") ); | |
492 | ||
493 | m_menus.Insert(node, menu); | |
494 | ||
495 | return TRUE; | |
496 | } | |
497 | ||
498 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
499 | const wxString& WXUNUSED(title)) | |
500 | { | |
501 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
502 | ||
503 | wxMenuList::Node *node = m_menus.Item(pos); | |
504 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); | |
505 | ||
506 | wxMenu *menuOld = node->GetData(); | |
507 | node->SetData(menu); | |
508 | ||
509 | return menuOld; | |
510 | } | |
511 | ||
512 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
513 | { | |
514 | wxMenuList::Node *node = m_menus.Item(pos); | |
515 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); | |
516 | ||
517 | node = m_menus.DetachNode(node); | |
518 | wxCHECK( node, NULL ); // unexpected | |
519 | wxMenu *menu = node->GetData(); | |
520 | ||
521 | delete node; | |
522 | ||
523 | return menu; | |
524 | } | |
525 | ||
526 | // --------------------------------------------------------------------------- | |
527 | // wxMenuBar functions forwarded to wxMenuItem | |
528 | // --------------------------------------------------------------------------- | |
529 | ||
530 | void wxMenuBarBase::Enable(int id, bool enable) | |
531 | { | |
532 | wxMenuItem *item = FindItem(id); | |
533 | ||
534 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
535 | ||
536 | item->Enable(enable); | |
537 | } | |
538 | ||
539 | void wxMenuBarBase::Check(int id, bool check) | |
540 | { | |
541 | wxMenuItem *item = FindItem(id); | |
542 | ||
543 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
544 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
545 | ||
546 | item->Check(check); | |
547 | } | |
548 | ||
549 | bool wxMenuBarBase::IsChecked(int id) const | |
550 | { | |
551 | wxMenuItem *item = FindItem(id); | |
552 | ||
553 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") ); | |
554 | ||
555 | return item->IsChecked(); | |
556 | } | |
557 | ||
558 | bool wxMenuBarBase::IsEnabled(int id) const | |
559 | { | |
560 | wxMenuItem *item = FindItem(id); | |
561 | ||
562 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") ); | |
563 | ||
564 | return item->IsEnabled(); | |
565 | } | |
566 | ||
567 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
568 | { | |
569 | wxMenuItem *item = FindItem(id); | |
570 | ||
571 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
572 | ||
573 | item->SetText(label); | |
574 | } | |
575 | ||
576 | wxString wxMenuBarBase::GetLabel(int id) const | |
577 | { | |
578 | wxMenuItem *item = FindItem(id); | |
579 | ||
580 | wxCHECK_MSG( item, wxEmptyString, | |
581 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
582 | ||
583 | return item->GetText(); | |
584 | } | |
585 | ||
586 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
587 | { | |
588 | wxMenuItem *item = FindItem(id); | |
589 | ||
590 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
591 | ||
592 | item->SetHelp(helpString); | |
593 | } | |
594 | ||
595 | wxString wxMenuBarBase::GetHelpString(int id) const | |
596 | { | |
597 | wxMenuItem *item = FindItem(id); | |
598 | ||
599 | wxCHECK_MSG( item, wxEmptyString, | |
600 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
601 | ||
602 | return item->GetHelp(); | |
603 | } | |
604 |