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