]> git.saurik.com Git - wxWidgets.git/blob - src/common/menucmn.cpp
1. wxMenu changes: wxMenuBase appears, several new functions for dynamic menu
[wxWidgets.git] / src / common / menucmn.cpp
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 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 {
304 item = node->GetData();
305 if ( item->GetId() == id )
306 break;
307
308 node = node->GetNext();
309 }
310
311 if ( ppos )
312 {
313 *ppos = item ? pos : wxNOT_FOUND;
314 }
315
316 return item;
317 }
318
319 // ----------------------------------------------------------------------------
320 // wxMenu helpers
321 // ----------------------------------------------------------------------------
322
323 // Update a menu and all submenus recursively. source is the object that has
324 // the update event handlers defined for it. If NULL, the menu or associated
325 // window will be used.
326 void wxMenuBase::UpdateUI(wxEvtHandler* source)
327 {
328 if ( !source && GetInvokingWindow() )
329 source = GetInvokingWindow()->GetEventHandler();
330 if ( !source )
331 source = GetEventHandler();
332 if ( !source )
333 source = this;
334
335 wxMenuItemList::Node* node = GetMenuItems().GetFirst();
336 while ( node )
337 {
338 wxMenuItem* item = node->GetData();
339 if ( !item->IsSeparator() )
340 {
341 wxWindowID id = item->GetId();
342 wxUpdateUIEvent event(id);
343 event.SetEventObject( source );
344
345 if ( source->ProcessEvent(event) )
346 {
347 // if anything changed, update the chanegd attribute
348 if (event.GetSetText())
349 SetLabel(id, event.GetText());
350 if (event.GetSetChecked())
351 Check(id, event.GetChecked());
352 if (event.GetSetEnabled())
353 Enable(id, event.GetEnabled());
354 }
355
356 // recurse to the submenus
357 if ( item->GetSubMenu() )
358 item->GetSubMenu()->UpdateUI(source);
359 }
360 //else: item is a separator (which don't process update UI events)
361
362 node = node->GetNext();
363 }
364 }
365
366 // ----------------------------------------------------------------------------
367 // wxMenu functions forwarded to wxMenuItem
368 // ----------------------------------------------------------------------------
369
370 void wxMenuBase::Enable( int id, bool enable )
371 {
372 wxMenuItem *item = FindItem(id);
373
374 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
375
376 item->Enable(enable);
377 }
378
379 bool wxMenuBase::IsEnabled( int id ) const
380 {
381 wxMenuItem *item = FindItem(id);
382
383 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") );
384
385 return item->IsEnabled();
386 }
387
388 void wxMenuBase::Check( int id, bool enable )
389 {
390 wxMenuItem *item = FindItem(id);
391
392 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
393
394 item->Check(enable);
395 }
396
397 bool wxMenuBase::IsChecked( int id ) const
398 {
399 wxMenuItem *item = FindItem(id);
400
401 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") );
402
403 return item->IsChecked();
404 }
405
406 void wxMenuBase::SetLabel( int id, const wxString &label )
407 {
408 wxMenuItem *item = FindItem(id);
409
410 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
411
412 item->SetText(label);
413 }
414
415 wxString wxMenuBase::GetLabel( int id ) const
416 {
417 wxMenuItem *item = FindItem(id);
418
419 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") );
420
421 return item->GetText();
422 }
423
424 void wxMenuBase::SetHelpString( int id, const wxString& helpString )
425 {
426 wxMenuItem *item = FindItem(id);
427
428 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
429
430 item->SetHelp( helpString );
431 }
432
433 wxString wxMenuBase::GetHelpString( int id ) const
434 {
435 wxMenuItem *item = FindItem(id);
436
437 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
438
439 return item->GetHelp();
440 }
441
442 // ----------------------------------------------------------------------------
443 // wxMenuBarBase ctor and dtor
444 // ----------------------------------------------------------------------------
445
446 wxMenuBarBase::wxMenuBarBase()
447 {
448 // we own the menus when we get them
449 m_menus.DeleteContents(TRUE);
450 }
451
452 wxMenuBarBase::~wxMenuBarBase()
453 {
454 // nothing to do, the list will delete the menus because of the call to
455 // DeleteContents() above
456 }
457
458 // ----------------------------------------------------------------------------
459 // wxMenuBar item access: the base class versions manage m_menus list, the
460 // derived class should reflect the changes in the real menubar
461 // ----------------------------------------------------------------------------
462
463 wxMenu *wxMenuBarBase::GetMenu(size_t pos) const
464 {
465 wxMenuList::Node *node = m_menus.Item(pos);
466 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") );
467
468 return node->GetData();
469 }
470
471 bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title))
472 {
473 wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") );
474
475 m_menus.Append(menu);
476
477 return TRUE;
478 }
479
480 bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu,
481 const wxString& WXUNUSED(title))
482 {
483 wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") );
484
485 wxMenuList::Node *node = m_menus.Item(pos);
486 wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") );
487
488 m_menus.Insert(node, menu);
489
490 return TRUE;
491 }
492
493 wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu,
494 const wxString& WXUNUSED(title))
495 {
496 wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") );
497
498 wxMenuList::Node *node = m_menus.Item(pos);
499 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") );
500
501 wxMenu *menuOld = node->GetData();
502 node->SetData(menu);
503
504 return menuOld;
505 }
506
507 wxMenu *wxMenuBarBase::Remove(size_t pos)
508 {
509 wxMenuList::Node *node = m_menus.Item(pos);
510 wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") );
511
512 node = m_menus.DetachNode(node);
513 wxCHECK( node, NULL ); // unexpected
514 wxMenu *menu = node->GetData();
515
516 delete node;
517
518 return menu;
519 }
520
521 // ---------------------------------------------------------------------------
522 // wxMenuBar functions forwarded to wxMenuItem
523 // ---------------------------------------------------------------------------
524
525 void wxMenuBarBase::Enable(int id, bool enable)
526 {
527 wxMenuItem *item = FindItem(id);
528
529 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
530
531 item->Enable(enable);
532 }
533
534 void wxMenuBarBase::Check(int id, bool check)
535 {
536 wxMenuItem *item = FindItem(id);
537
538 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
539 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
540
541 item->Check(check);
542 }
543
544 bool wxMenuBarBase::IsChecked(int id) const
545 {
546 wxMenuItem *item = FindItem(id);
547
548 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") );
549
550 return item->IsChecked();
551 }
552
553 bool wxMenuBarBase::IsEnabled(int id) const
554 {
555 wxMenuItem *item = FindItem(id);
556
557 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") );
558
559 return item->IsEnabled();
560 }
561
562 void wxMenuBarBase::SetLabel(int id, const wxString& label)
563 {
564 wxMenuItem *item = FindItem(id);
565
566 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
567
568 item->SetText(label);
569 }
570
571 wxString wxMenuBarBase::GetLabel(int id) const
572 {
573 wxMenuItem *item = FindItem(id);
574
575 wxCHECK_MSG( item, wxEmptyString,
576 wxT("wxMenuBar::GetLabel(): no such item") );
577
578 return item->GetText();
579 }
580
581 void wxMenuBarBase::SetHelpString(int id, const wxString& helpString)
582 {
583 wxMenuItem *item = FindItem(id);
584
585 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
586
587 item->SetHelp(helpString);
588 }
589
590 wxString wxMenuBarBase::GetHelpString(int id) const
591 {
592 wxMenuItem *item = FindItem(id);
593
594 wxCHECK_MSG( item, wxEmptyString,
595 wxT("wxMenuBar::GetHelpString(): no such item") );
596
597 return item->GetHelp();
598 }
599