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