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