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