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