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