]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
(ugh, I need to get a faster computer)
[wxWidgets.git] / src / gtk / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
13 #endif
14
15 #include "wx/menu.h"
16 #include "wx/log.h"
17 #include "wx/intl.h"
18 #include "wx/app.h"
19
20 #include "gdk/gdk.h"
21 #include "gtk/gtk.h"
22
23 //-----------------------------------------------------------------------------
24 // wxMenuBar
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
28
29 wxMenuBar::wxMenuBar( long style )
30 {
31 m_needParent = FALSE; // hmmm
32
33 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, style, "menu" );
34
35 m_menus.DeleteContents( TRUE );
36
37 m_menubar = gtk_menu_bar_new();
38
39 if (style & wxMB_DOCKABLE)
40 {
41 m_widget = gtk_handle_box_new();
42 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
43 gtk_widget_show( GTK_WIDGET(m_menubar) );
44 }
45 else
46 {
47 m_widget = GTK_WIDGET(m_menubar);
48 }
49
50 PostCreation();
51
52 Show( TRUE );
53 }
54
55 wxMenuBar::wxMenuBar()
56 {
57 m_needParent = FALSE; // hmmm
58
59 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
60
61 m_menus.DeleteContents( TRUE );
62
63 m_menubar = gtk_menu_bar_new();
64
65 m_widget = GTK_WIDGET(m_menubar);
66
67 PostCreation();
68
69 Show( TRUE );
70 }
71
72 void wxMenuBar::Append( wxMenu *menu, const wxString &title )
73 {
74 m_menus.Append( menu );
75
76 wxString s = _T("");
77 for ( const wxChar *pc = title; *pc != _T('\0'); pc++ )
78 {
79 if (*pc == _T('&'))
80 {
81 pc++; /* skip it */
82 #if (GTK_MINOR_VERSION > 0)
83 // s << '_'; not yet
84 #endif
85 }
86 s << *pc;
87 }
88
89 menu->SetTitle(s);
90 menu->m_owner = gtk_menu_item_new_with_label( MBSTRINGCAST s.mbc_str() );
91 gtk_widget_show( menu->m_owner );
92 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
93
94 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
95 }
96
97 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
98 {
99 if (menu->GetTitle() == menuString)
100 {
101 int res = menu->FindItem( itemString );
102 if (res != wxNOT_FOUND)
103 return res;
104 }
105
106 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
107 while (node)
108 {
109 wxMenuItem *item = (wxMenuItem*)node->Data();
110 if (item->IsSubMenu())
111 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
112
113 node = node->Next();
114 }
115
116 return wxNOT_FOUND;
117 }
118
119 wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem = NULL) const
120 {
121 if ( menuForItem )
122 {
123 // TODO return the pointer to the menu
124
125 *menuForItem = NULL;
126 }
127
128 return FindItem(itemId);
129 }
130
131 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
132 {
133 wxNode *node = m_menus.First();
134 while (node)
135 {
136 wxMenu *menu = (wxMenu*)node->Data();
137 int res = FindMenuItemRecursive( menu, menuString, itemString);
138 if (res != -1) return res;
139 node = node->Next();
140 }
141 return -1;
142 }
143
144 // Find a wxMenuItem using its id. Recurses down into sub-menus
145 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
146 {
147 wxMenuItem* result = menu->FindItem(id);
148
149 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
150 while ( node && result == NULL )
151 {
152 wxMenuItem *item = (wxMenuItem*)node->Data();
153 if (item->IsSubMenu())
154 {
155 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
156 }
157 node = node->Next();
158 }
159
160 return result;
161 }
162
163 wxMenuItem* wxMenuBar::FindItem( int id ) const
164 {
165 wxMenuItem* result = 0;
166 wxNode *node = m_menus.First();
167 while (node && result == 0)
168 {
169 wxMenu *menu = (wxMenu*)node->Data();
170 result = FindMenuItemByIdRecursive( menu, id );
171 node = node->Next();
172 }
173
174 return result;
175 }
176
177 void wxMenuBar::Check( int id, bool check )
178 {
179 wxMenuItem* item = FindMenuItemById( id );
180
181 wxCHECK_RET( item, _T("wxMenuBar::Check: no such item") );
182
183 item->Check(check);
184 }
185
186 bool wxMenuBar::IsChecked( int id ) const
187 {
188 wxMenuItem* item = FindMenuItemById( id );
189
190 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked: no such item") );
191
192 return item->IsChecked();
193 }
194
195 void wxMenuBar::Enable( int id, bool enable )
196 {
197 wxMenuItem* item = FindMenuItemById( id );
198
199 wxCHECK_RET( item, _T("wxMenuBar::Enable: no such item") );
200
201 item->Enable(enable);
202 }
203
204 bool wxMenuBar::IsEnabled( int id ) const
205 {
206 wxMenuItem* item = FindMenuItemById( id );
207
208 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled: no such item") );
209
210 return item->IsEnabled();
211 }
212
213 wxString wxMenuBar::GetLabel( int id ) const
214 {
215 wxMenuItem* item = FindMenuItemById( id );
216
217 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel: no such item") );
218
219 return item->GetText();
220 }
221
222 void wxMenuBar::SetLabel( int id, const wxString &label )
223 {
224 wxMenuItem* item = FindMenuItemById( id );
225
226 wxCHECK_RET( item, _T("wxMenuBar::SetLabel: no such item") );
227
228 item->SetText( label );
229 }
230
231 void wxMenuBar::EnableTop( int pos, bool flag )
232 {
233 wxNode *node = m_menus.Nth( pos );
234
235 wxCHECK_RET( node, _T("menu not found") );
236
237 wxMenu* menu = (wxMenu*)node->Data();
238
239 if (menu->m_owner)
240 gtk_widget_set_sensitive( menu->m_owner, flag );
241 }
242
243 wxString wxMenuBar::GetLabelTop( int pos ) const
244 {
245 wxNode *node = m_menus.Nth( pos );
246
247 wxCHECK_MSG( node, _T("invalid"), _T("menu not found") );
248
249 wxMenu* menu = (wxMenu*)node->Data();
250
251 return menu->GetTitle();
252 }
253
254 void wxMenuBar::SetLabelTop( int pos, const wxString& label )
255 {
256 wxNode *node = m_menus.Nth( pos );
257
258 wxCHECK_RET( node, _T("menu not found") );
259
260 wxMenu* menu = (wxMenu*)node->Data();
261
262 menu->SetTitle( label );
263 }
264
265 void wxMenuBar::SetHelpString( int id, const wxString& helpString )
266 {
267 wxMenuItem* item = FindMenuItemById( id );
268
269 wxCHECK_RET( item, _T("wxMenuBar::SetHelpString: no such item") );
270
271 item->SetHelp( helpString );
272 }
273
274 wxString wxMenuBar::GetHelpString( int id ) const
275 {
276 wxMenuItem* item = FindMenuItemById( id );
277
278 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
279
280 return item->GetHelp();
281 }
282
283 //-----------------------------------------------------------------------------
284 // "activate"
285 //-----------------------------------------------------------------------------
286
287 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
288 {
289 int id = menu->FindMenuIdByMenuItem(widget);
290
291 /* should find it for normal (not popup) menu */
292 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
293
294 if (!menu->IsEnabled(id))
295 return;
296
297 wxMenuItem* item = menu->FindItem( id );
298 wxCHECK_RET( item, _T("error in menu item callback") );
299
300 if (item->IsCheckable())
301 {
302 if (item->GetCheckedFlag() == item->IsChecked())
303 {
304 /* the menu item has been checked by calling wxMenuItem->Check() */
305 return;
306 }
307 else
308 {
309 /* the user pressed on the menu item -> report */
310 item->SetCheckedFlag(item->IsChecked()); /* make consistent again */
311 }
312 }
313
314 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
315 event.SetEventObject( menu );
316 event.SetInt(id );
317
318 if (menu->GetCallback())
319 {
320 (void) (*(menu->GetCallback())) (*menu, event);
321 return;
322 }
323
324 if (menu->GetEventHandler()->ProcessEvent(event))
325 return;
326
327 wxWindow *win = menu->GetInvokingWindow();
328 if (win)
329 win->GetEventHandler()->ProcessEvent( event );
330 }
331
332 //-----------------------------------------------------------------------------
333 // "select"
334 //-----------------------------------------------------------------------------
335
336 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
337 {
338 int id = menu->FindMenuIdByMenuItem(widget);
339
340 wxASSERT( id != -1 ); // should find it!
341
342 if (!menu->IsEnabled(id))
343 return;
344
345 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
346 event.SetEventObject( menu );
347
348 /* wxMSW doesn't call callback here either
349
350 if (menu->m_callback)
351 {
352 (void) (*(menu->m_callback)) (*menu, event);
353 return;
354 }
355 */
356
357 if (menu->GetEventHandler()->ProcessEvent(event))
358 return;
359
360 wxWindow *win = menu->GetInvokingWindow();
361 if (win) win->GetEventHandler()->ProcessEvent( event );
362 }
363
364 //-----------------------------------------------------------------------------
365 // "deselect"
366 //-----------------------------------------------------------------------------
367
368 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
369 {
370 int id = menu->FindMenuIdByMenuItem(widget);
371
372 wxASSERT( id != -1 ); // should find it!
373
374 if (!menu->IsEnabled(id))
375 return;
376
377 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
378 event.SetEventObject( menu );
379
380 if (menu->GetEventHandler()->ProcessEvent(event))
381 return;
382
383 wxWindow *win = menu->GetInvokingWindow();
384 if (win)
385 win->GetEventHandler()->ProcessEvent( event );
386 }
387
388 //-----------------------------------------------------------------------------
389 // wxMenuItem
390 //-----------------------------------------------------------------------------
391
392 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
393
394 wxMenuItem::wxMenuItem()
395 {
396 m_id = ID_SEPARATOR;
397 m_isCheckMenu = FALSE;
398 m_isChecked = FALSE;
399 m_isEnabled = TRUE;
400 m_subMenu = (wxMenu *) NULL;
401 m_menuItem = (GtkWidget *) NULL;
402 }
403
404 // it's valid for this function to be called even if m_menuItem == NULL
405 void wxMenuItem::SetName( const wxString& str )
406 {
407 m_text = _T("");
408 for ( const wxChar *pc = str; *pc != _T('\0'); pc++ )
409 {
410 if (*pc == _T('&'))
411 {
412 pc++; /* skip it */
413 #if (GTK_MINOR_VERSION > 0)
414 m_text << _T('_');
415 #endif
416 }
417 m_text << *pc;
418 }
419
420 if (m_menuItem)
421 {
422 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
423 gtk_label_set( label, m_text.mbc_str());
424 }
425 }
426
427 void wxMenuItem::Check( bool check )
428 {
429 wxCHECK_RET( m_menuItem, _T("invalid menu item") );
430
431 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
432
433 if (check == m_isChecked) return;
434
435 m_isChecked = check;
436 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
437 }
438
439 void wxMenuItem::Enable( bool enable )
440 {
441 wxCHECK_RET( m_menuItem, _T("invalid menu item") );
442
443 gtk_widget_set_sensitive( m_menuItem, enable );
444 m_isEnabled = enable;
445 }
446
447 bool wxMenuItem::IsChecked() const
448 {
449 wxCHECK_MSG( m_menuItem, FALSE, _T("invalid menu item") );
450
451 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
452
453 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
454
455 return bIsChecked;
456 }
457
458 //-----------------------------------------------------------------------------
459 // wxMenu
460 //-----------------------------------------------------------------------------
461
462 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
463
464 wxMenu::wxMenu( const wxString& title, const wxFunction func )
465 {
466 m_title = title;
467 m_items.DeleteContents( TRUE );
468 m_invokingWindow = (wxWindow *) NULL;
469
470 #if (GTK_MINOR_VERSION > 0)
471 m_accel = gtk_accel_group_new();
472 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
473 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
474 #else
475 m_menu = gtk_menu_new(); // Do not show!
476 #endif
477
478 m_callback = func;
479 m_eventHandler = this;
480 m_clientData = (void*) NULL;
481
482 if (m_title.IsNull()) m_title = _T("");
483 if (m_title != _T(""))
484 {
485 Append(-2, m_title);
486 AppendSeparator();
487 }
488
489 m_owner = (GtkWidget*) NULL;
490 }
491
492 wxMenu::~wxMenu()
493 {
494 /* how do we delete an item-factory ? */
495 }
496
497 void wxMenu::SetTitle( const wxString& title )
498 {
499 // TODO Waiting for something better
500 m_title = title;
501 }
502
503 const wxString wxMenu::GetTitle() const
504 {
505 return m_title;
506 }
507
508 void wxMenu::AppendSeparator()
509 {
510 wxMenuItem *mitem = new wxMenuItem();
511 mitem->SetId(ID_SEPARATOR);
512
513 GtkWidget *menuItem = gtk_menu_item_new();
514 gtk_menu_append( GTK_MENU(m_menu), menuItem );
515 gtk_widget_show( menuItem );
516
517 mitem->SetMenuItem(menuItem);
518 m_items.Append( mitem );
519 }
520
521 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
522 {
523 wxMenuItem *mitem = new wxMenuItem();
524 mitem->SetId(id);
525 mitem->SetText(item);
526 mitem->SetHelp(helpStr);
527 mitem->SetCheckable(checkable);
528 const wxChar *text = mitem->GetText();
529
530 #if (GTK_MINOR_VERSION > 0)
531 wxChar buf[100];
532 wxStrcpy( buf, _T("/") );
533 wxStrcat( buf, text );
534
535 const wxWX2MBbuf pbuf = wxConv_current->cWX2MB(buf);
536 GtkItemFactoryEntry entry;
537 entry.path = MBSTRINGCAST pbuf;
538 entry.accelerator = (gchar*) NULL;
539 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
540 entry.callback_action = 0;
541 if (checkable)
542 entry.item_type = "<CheckItem>";
543 else
544 entry.item_type = "<Item>";
545
546 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
547
548 /* in order to get the pointer to the item we need the item text _without_ underscores */
549 wxString s = _T("<main>/");
550 for ( const wxChar *pc = text; *pc != _T('\0'); pc++ )
551 {
552 if (*pc == _T('_')) pc++; /* skip it */
553 s << *pc;
554 }
555
556 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mbc_str() );
557
558 #else
559
560 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label(text)
561 : gtk_menu_item_new_with_label(text);
562
563 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
564 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
565 (gpointer)this );
566
567 gtk_menu_append( GTK_MENU(m_menu), menuItem );
568 gtk_widget_show( menuItem );
569
570 #endif
571
572 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
573 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
574 (gpointer)this );
575
576 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
577 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
578 (gpointer)this );
579
580 mitem->SetMenuItem(menuItem);
581
582 m_items.Append( mitem );
583 }
584
585 void wxMenu::Append( int id, const wxString &text, wxMenu *subMenu, const wxString &helpStr )
586 {
587 wxMenuItem *mitem = new wxMenuItem();
588 mitem->SetId(id);
589 mitem->SetText(text);
590 mitem->SetHelp(helpStr);
591
592 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
593 mitem->SetMenuItem(menuItem);
594 mitem->SetSubMenu(subMenu);
595
596 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
597 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
598 (gpointer*)this );
599
600 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
601 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
602 (gpointer*)this );
603
604 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
605 gtk_menu_append( GTK_MENU(m_menu), menuItem );
606 gtk_widget_show( menuItem );
607 m_items.Append( mitem );
608 }
609
610 void wxMenu::Append( wxMenuItem *item )
611 {
612 m_items.Append( item );
613
614 GtkWidget *menuItem = (GtkWidget*) NULL;
615
616 if (item->IsSeparator())
617 menuItem = gtk_menu_item_new();
618 else if (item->IsSubMenu())
619 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
620 else
621 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
622 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
623
624 if (!item->IsSeparator())
625 {
626 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
627 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
628 (gpointer*)this );
629
630 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
631 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
632 (gpointer*)this );
633
634 if (!item->IsSubMenu())
635 {
636 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
637 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
638 (gpointer*)this );
639 }
640 }
641
642 gtk_menu_append( GTK_MENU(m_menu), menuItem );
643 gtk_widget_show( menuItem );
644 item->SetMenuItem(menuItem);
645 }
646
647 int wxMenu::FindItem( const wxString itemString ) const
648 {
649 wxString s = _T("");
650 for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ )
651 {
652 if (*pc == _T('&'))
653 {
654 pc++; /* skip it */
655 #if (GTK_MINOR_VERSION > 0)
656 s << _T('_');
657 #endif
658 }
659 s << *pc;
660 }
661
662 wxNode *node = m_items.First();
663 while (node)
664 {
665 wxMenuItem *item = (wxMenuItem*)node->Data();
666 if (item->GetText() == s)
667 {
668 return item->GetId();
669 }
670 node = node->Next();
671 }
672
673 return wxNOT_FOUND;
674 }
675
676 void wxMenu::Enable( int id, bool enable )
677 {
678 wxMenuItem *item = FindItem(id);
679
680 wxCHECK_RET( item, _T("wxMenu::Enable: no such item") );
681
682 item->Enable(enable);
683 }
684
685 bool wxMenu::IsEnabled( int id ) const
686 {
687 wxMenuItem *item = FindItem(id);
688
689 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") );
690
691 return item->IsEnabled();
692 }
693
694 void wxMenu::Check( int id, bool enable )
695 {
696 wxMenuItem *item = FindItem(id);
697
698 wxCHECK_RET( item, _T("wxMenu::Check: no such item") );
699
700 item->Check(enable);
701 }
702
703 bool wxMenu::IsChecked( int id ) const
704 {
705 wxMenuItem *item = FindItem(id);
706
707 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") );
708
709 return item->IsChecked();
710 }
711
712 void wxMenu::SetLabel( int id, const wxString &label )
713 {
714 wxMenuItem *item = FindItem(id);
715
716 wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") );
717
718 item->SetText(label);
719 }
720
721 wxString wxMenu::GetLabel( int id ) const
722 {
723 wxMenuItem *item = FindItem(id);
724
725 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") );
726
727 return item->GetText();
728 }
729
730 void wxMenu::SetHelpString( int id, const wxString& helpString )
731 {
732 wxMenuItem *item = FindItem(id);
733
734 wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") );
735
736 item->SetHelp( helpString );
737 }
738
739 wxString wxMenu::GetHelpString( int id ) const
740 {
741 wxMenuItem *item = FindItem(id);
742
743 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") );
744
745 return item->GetHelp();
746 }
747
748 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
749 {
750 wxNode *node = m_items.First();
751 while (node)
752 {
753 wxMenuItem *item = (wxMenuItem*)node->Data();
754 if (item->GetMenuItem() == menuItem)
755 return item->GetId();
756 node = node->Next();
757 }
758
759 return wxNOT_FOUND;
760 }
761
762 wxMenuItem *wxMenu::FindItem(int id) const
763 {
764 wxNode *node = m_items.First();
765 while (node)
766 {
767 wxMenuItem *item = (wxMenuItem*)node->Data();
768 if (item->GetId() == id)
769 {
770 return item;
771 }
772 node = node->Next();
773 }
774
775 /* Not finding anything here can be correct
776 * when search the entire menu system for
777 * an entry -> no error message. */
778
779 return (wxMenuItem *) NULL;
780 }
781
782 void wxMenu::SetInvokingWindow( wxWindow *win )
783 {
784 m_invokingWindow = win;
785 }
786
787 wxWindow *wxMenu::GetInvokingWindow()
788 {
789 return m_invokingWindow;
790 }
791
792 // Update a menu and all submenus recursively. source is the object that has
793 // the update event handlers defined for it. If NULL, the menu or associated
794 // window will be used.
795 void wxMenu::UpdateUI(wxEvtHandler* source)
796 {
797 if (!source && GetInvokingWindow())
798 source = GetInvokingWindow()->GetEventHandler();
799 if (!source)
800 source = GetEventHandler();
801 if (!source)
802 source = this;
803
804 wxNode* node = GetItems().First();
805 while (node)
806 {
807 wxMenuItem* item = (wxMenuItem*) node->Data();
808 if ( !item->IsSeparator() )
809 {
810 wxWindowID id = item->GetId();
811 wxUpdateUIEvent event(id);
812 event.SetEventObject( source );
813
814 if (source->ProcessEvent(event))
815 {
816 if (event.GetSetText())
817 SetLabel(id, event.GetText());
818 if (event.GetSetChecked())
819 Check(id, event.GetChecked());
820 if (event.GetSetEnabled())
821 Enable(id, event.GetEnabled());
822 }
823
824 if (item->GetSubMenu())
825 item->GetSubMenu()->UpdateUI(source);
826 }
827 node = node->Next();
828 }
829 }
830