1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "menu.h"
12 #pragma implementation "menuitem.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
36 wxMenuBar::wxMenuBar( long style
)
38 /* the parent window is known after wxFrame::SetMenu() */
42 PreCreation( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, "menu" );
44 m_menus
.DeleteContents( TRUE
);
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>" );
52 m_menubar
= gtk_menu_bar_new();
55 if (style
& wxMB_DOCKABLE
)
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
) );
63 m_widget
= GTK_WIDGET(m_menubar
);
69 wxMenuBar::wxMenuBar()
71 /* the parent window is known after wxFrame::SetMenu() */
75 PreCreation( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, "menu" );
77 m_menus
.DeleteContents( TRUE
);
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>" );
85 m_menubar
= gtk_menu_bar_new();
88 m_widget
= GTK_WIDGET(m_menubar
);
93 wxMenuBar::~wxMenuBar()
95 // how to destroy a GtkItemFactory ?
98 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
100 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
102 #if (GTK_MINOR_VERSION > 0)
103 wxWindow
*top_frame
= win
;
104 while (top_frame
->GetParent()) top_frame
= top_frame
->GetParent();
106 /* support for native hot keys */
107 gtk_accel_group_detach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
110 wxNode
*node
= menu
->GetItems().First();
113 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
114 if (menuitem
->IsSubMenu())
115 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
120 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
122 menu
->SetInvokingWindow( win
);
124 #if (GTK_MINOR_VERSION > 0)
125 wxWindow
*top_frame
= win
;
126 while (top_frame
->GetParent())
127 top_frame
= top_frame
->GetParent();
129 /* support for native hot keys */
130 gtk_accel_group_attach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
133 wxNode
*node
= menu
->GetItems().First();
136 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
137 if (menuitem
->IsSubMenu())
138 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
143 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
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();
150 /* support for native key accelerators indicated by underscroes */
151 gtk_accel_group_attach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
154 wxNode
*node
= m_menus
.First();
157 wxMenu
*menu
= (wxMenu
*)node
->Data();
158 wxMenubarSetInvokingWindow( menu
, win
);
163 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
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();
170 /* support for native key accelerators indicated by underscroes */
171 gtk_accel_group_detach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
174 wxNode
*node
= m_menus
.First();
177 wxMenu
*menu
= (wxMenu
*)node
->Data();
178 wxMenubarUnsetInvokingWindow( menu
, win
);
183 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
185 m_menus
.Append( menu
);
187 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
189 for ( const wxChar
*pc
= title
; *pc
!= _T('\0'); pc
++ )
193 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
205 /* this doesn't have much effect right now */
206 menu
->SetTitle( str
);
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)
211 /* local buffer in multibyte form */
214 strcat( buf
, str
.mb_str() );
216 GtkItemFactoryEntry entry
;
218 entry
.accelerator
= (gchar
*) NULL
;
219 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
220 entry
.callback_action
= 0;
223 if ((m_style & wxMB_TEAROFF) || (menu->GetStyle() & wxMENU_TEAROFF))
224 entry.item_type = "<Tearoff>";
227 entry
.item_type
= "<Branch>";
229 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
231 /* in order to get the pointer to the item we need the item text _without_ underscores */
232 wxString tmp
= _T("<main>/");
233 for ( const wxChar
*pc
= str
; *pc
!= _T('\0'); pc
++ )
235 if (*pc
== _T('_')) pc
++; /* skip it */
239 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
241 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
244 if ((m_style & wxMB_TEAROFF) || (menu->GetStyle() & wxMENU_TEAROFF))
246 entry.item_type = "<Tearoff>";
248 tmp.Append( _T("/tearoff") );
249 strcpy( buf, tmp.mb_str() );
250 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 );
255 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
256 gtk_widget_show( menu
->m_owner
);
257 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
259 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
264 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
266 if (menu
->GetTitle() == menuString
)
268 int res
= menu
->FindItem( itemString
);
269 if (res
!= wxNOT_FOUND
)
273 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
276 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
277 if (item
->IsSubMenu())
278 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
286 wxMenuItem
*wxMenuBar::FindItemForId(int itemId
, wxMenu
**menuForItem
) const
290 // TODO return the pointer to the menu
295 return FindItem(itemId
);
298 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
300 wxNode
*node
= m_menus
.First();
303 wxMenu
*menu
= (wxMenu
*)node
->Data();
304 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
305 if (res
!= -1) return res
;
311 // Find a wxMenuItem using its id. Recurses down into sub-menus
312 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
314 wxMenuItem
* result
= menu
->FindItem(id
);
316 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
317 while ( node
&& result
== NULL
)
319 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
320 if (item
->IsSubMenu())
322 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
330 wxMenuItem
* wxMenuBar::FindItem( int id
) const
332 wxMenuItem
* result
= 0;
333 wxNode
*node
= m_menus
.First();
334 while (node
&& result
== 0)
336 wxMenu
*menu
= (wxMenu
*)node
->Data();
337 result
= FindMenuItemByIdRecursive( menu
, id
);
344 void wxMenuBar::Check( int id
, bool check
)
346 wxMenuItem
* item
= FindMenuItemById( id
);
348 wxCHECK_RET( item
, _T("wxMenuBar::Check: no such item") );
353 bool wxMenuBar::IsChecked( int id
) const
355 wxMenuItem
* item
= FindMenuItemById( id
);
357 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked: no such item") );
359 return item
->IsChecked();
362 void wxMenuBar::Enable( int id
, bool enable
)
364 wxMenuItem
* item
= FindMenuItemById( id
);
366 wxCHECK_RET( item
, _T("wxMenuBar::Enable: no such item") );
368 item
->Enable(enable
);
371 bool wxMenuBar::IsEnabled( int id
) const
373 wxMenuItem
* item
= FindMenuItemById( id
);
375 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled: no such item") );
377 return item
->IsEnabled();
380 wxString
wxMenuBar::GetLabel( int id
) const
382 wxMenuItem
* item
= FindMenuItemById( id
);
384 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel: no such item") );
386 return item
->GetText();
389 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
391 wxMenuItem
* item
= FindMenuItemById( id
);
393 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel: no such item") );
395 item
->SetText( label
);
398 void wxMenuBar::EnableTop( int pos
, bool flag
)
400 wxNode
*node
= m_menus
.Nth( pos
);
402 wxCHECK_RET( node
, _T("menu not found") );
404 wxMenu
* menu
= (wxMenu
*)node
->Data();
407 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
410 wxString
wxMenuBar::GetLabelTop( int pos
) const
412 wxNode
*node
= m_menus
.Nth( pos
);
414 wxCHECK_MSG( node
, _T("invalid"), _T("menu not found") );
416 wxMenu
* menu
= (wxMenu
*)node
->Data();
418 return menu
->GetTitle();
421 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
423 wxNode
*node
= m_menus
.Nth( pos
);
425 wxCHECK_RET( node
, _T("menu not found") );
427 wxMenu
* menu
= (wxMenu
*)node
->Data();
429 menu
->SetTitle( label
);
432 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
434 wxMenuItem
* item
= FindMenuItemById( id
);
436 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString: no such item") );
438 item
->SetHelp( helpString
);
441 wxString
wxMenuBar::GetHelpString( int id
) const
443 wxMenuItem
* item
= FindMenuItemById( id
);
445 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
447 return item
->GetHelp();
450 //-----------------------------------------------------------------------------
452 //-----------------------------------------------------------------------------
454 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
456 if (g_isIdle
) wxapp_install_idle_handler();
458 int id
= menu
->FindMenuIdByMenuItem(widget
);
460 /* should find it for normal (not popup) menu */
461 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
463 if (!menu
->IsEnabled(id
))
466 wxMenuItem
* item
= menu
->FindItem( id
);
467 wxCHECK_RET( item
, _T("error in menu item callback") );
469 if (item
->IsCheckable())
471 if (item
->GetCheckedFlag() == item
->IsChecked())
473 /* the menu item has been checked by calling wxMenuItem->Check() */
478 /* the user pressed on the menu item -> report */
479 item
->SetCheckedFlag(item
->IsChecked()); /* make consistent again */
483 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
484 event
.SetEventObject( menu
);
487 if (menu
->GetCallback())
489 (void) (*(menu
->GetCallback())) (*menu
, event
);
493 if (menu
->GetEventHandler()->ProcessEvent(event
))
496 wxWindow
*win
= menu
->GetInvokingWindow();
498 win
->GetEventHandler()->ProcessEvent( event
);
501 //-----------------------------------------------------------------------------
503 //-----------------------------------------------------------------------------
505 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
507 if (g_isIdle
) wxapp_install_idle_handler();
509 int id
= menu
->FindMenuIdByMenuItem(widget
);
511 wxASSERT( id
!= -1 ); // should find it!
513 if (!menu
->IsEnabled(id
))
516 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
517 event
.SetEventObject( menu
);
519 /* wxMSW doesn't call callback here either
521 if (menu->m_callback)
523 (void) (*(menu->m_callback)) (*menu, event);
528 if (menu
->GetEventHandler()->ProcessEvent(event
))
531 wxWindow
*win
= menu
->GetInvokingWindow();
532 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
535 //-----------------------------------------------------------------------------
537 //-----------------------------------------------------------------------------
539 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
541 if (g_isIdle
) wxapp_install_idle_handler();
543 int id
= menu
->FindMenuIdByMenuItem(widget
);
545 wxASSERT( id
!= -1 ); // should find it!
547 if (!menu
->IsEnabled(id
))
550 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
551 event
.SetEventObject( menu
);
553 if (menu
->GetEventHandler()->ProcessEvent(event
))
556 wxWindow
*win
= menu
->GetInvokingWindow();
558 win
->GetEventHandler()->ProcessEvent( event
);
561 //-----------------------------------------------------------------------------
563 //-----------------------------------------------------------------------------
565 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
567 wxMenuItem::wxMenuItem()
570 m_isCheckMenu
= FALSE
;
573 m_subMenu
= (wxMenu
*) NULL
;
574 m_menuItem
= (GtkWidget
*) NULL
;
577 // it's valid for this function to be called even if m_menuItem == NULL
578 void wxMenuItem::SetName( const wxString
& str
)
580 /* '\t' is the deliminator indicating a hot key */
582 const wxChar
*pc
= str
;
583 for (; (*pc
!= _T('\0')) && (*pc
!= _T('\t')); pc
++ )
587 #if (GTK_MINOR_VERSION > 0)
590 if (*pc
== _T('/')) /* we have to filter out slashes ... */
592 m_text
<< _T('\\'); /* ... and replace them with back slashes */
599 /* only GTK 1.2 knows about hot keys */
601 #if (GTK_MINOR_VERSION > 0)
611 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
612 gtk_label_set( label
, m_text
.mb_str());
616 void wxMenuItem::Check( bool check
)
618 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
620 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
622 if (check
== m_isChecked
) return;
625 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
628 void wxMenuItem::Enable( bool enable
)
630 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
632 gtk_widget_set_sensitive( m_menuItem
, enable
);
633 m_isEnabled
= enable
;
636 bool wxMenuItem::IsChecked() const
638 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
640 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
642 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
647 //-----------------------------------------------------------------------------
649 //-----------------------------------------------------------------------------
651 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
653 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
, long style
)
655 Init(title
, func
, style
);
658 wxMenu::wxMenu(long style
)
660 Init(wxEmptyString
, (wxFunction
) NULL
, style
);
664 wxMenu::Init( const wxString
& title
, const wxFunction func
, long style
)
667 m_items
.DeleteContents( TRUE
);
668 m_invokingWindow
= (wxWindow
*) NULL
;
671 #if (GTK_MINOR_VERSION > 0)
672 m_accel
= gtk_accel_group_new();
673 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
674 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
676 m_menu
= gtk_menu_new(); // Do not show!
680 m_eventHandler
= this;
681 m_clientData
= (void*) NULL
;
683 if (m_title
.IsNull()) m_title
= _T("");
684 if (m_title
!= _T(""))
690 m_owner
= (GtkWidget
*) NULL
;
695 /* how do we delete an item-factory ? */
698 void wxMenu::SetTitle( const wxString
& title
)
700 // TODO Waiting for something better
704 const wxString
wxMenu::GetTitle() const
709 void wxMenu::AppendSeparator()
711 wxMenuItem
*mitem
= new wxMenuItem();
712 mitem
->SetId(ID_SEPARATOR
);
714 #if (GTK_MINOR_VERSION > 0)
715 GtkItemFactoryEntry entry
;
717 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
718 entry
.callback_action
= 0;
719 entry
.item_type
= "<Separator>";
720 entry
.accelerator
= (gchar
*) NULL
;
722 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
724 /* this will be wrong for more than one separator. do we care? */
725 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
727 GtkWidget
*menuItem
= gtk_menu_item_new();
728 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
729 gtk_widget_show( menuItem
);
732 mitem
->SetMenuItem(menuItem
);
733 m_items
.Append( mitem
);
736 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
738 if (hotkey
.IsEmpty()) return (char*) NULL
;
742 case _T('a'): /* Alt */
744 case _T('m'): /* Meta */
747 strcpy( hotbuf
, "<alt>" );
748 wxString last
= hotkey
.Right(1);
749 strcat( hotbuf
, last
.mb_str() );
752 case _T('c'): /* Ctrl */
754 case _T('s'): /* Strg, yeah man, I'm German */
757 strcpy( hotbuf
, "<control>" );
758 wxString last
= hotkey
.Right(1);
759 strcat( hotbuf
, last
.mb_str() );
762 case _T('F'): /* function keys */
764 strcpy( hotbuf
, hotkey
.mb_str() );
774 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
776 wxMenuItem
*mitem
= new wxMenuItem();
778 mitem
->SetText(item
);
779 mitem
->SetHelp(helpStr
);
780 mitem
->SetCheckable(checkable
);
782 #if (GTK_MINOR_VERSION > 0)
783 /* text has "_" instead of "&" after mitem->SetText() */
784 wxString
text( mitem
->GetText() );
786 /* local buffer in multibyte form */
789 strcat( buf
, text
.mb_str() );
791 GtkItemFactoryEntry entry
;
793 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
794 entry
.callback_action
= 0;
796 entry
.item_type
= "<CheckItem>";
798 entry
.item_type
= "<Item>";
801 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
803 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
805 /* in order to get the pointer to the item we need the item text _without_ underscores */
806 wxString s
= _T("<main>/");
807 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
809 if (*pc
== _T('_')) pc
++; /* skip it */
813 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
817 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
818 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
820 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
821 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
824 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
825 gtk_widget_show( menuItem
);
829 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
830 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
833 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
834 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
837 mitem
->SetMenuItem(menuItem
);
839 m_items
.Append( mitem
);
842 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
844 wxMenuItem
*mitem
= new wxMenuItem();
846 mitem
->SetText(item
);
847 mitem
->SetHelp(helpStr
);
849 #if (GTK_MINOR_VERSION > 0)
850 /* text has "_" instead of "&" after mitem->SetText() */
851 wxString
text( mitem
->GetText() );
853 /* local buffer in multibyte form */
856 strcat( buf
, text
.mb_str() );
858 GtkItemFactoryEntry entry
;
860 entry
.callback
= (GtkItemFactoryCallback
) 0;
861 entry
.callback_action
= 0;
864 if (m_style & wxMENU_TEAROFF)
865 entry.item_type = "<Tearoff>";
868 entry
.item_type
= "<Branch>";
870 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
872 /* in order to get the pointer to the item we need the item text _without_ underscores */
873 wxString s
= _T("<main>/");
874 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
876 if (*pc
== _T('_')) pc
++; /* skip it */
880 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
884 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
886 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
887 gtk_widget_show( menuItem
);
891 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
892 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
895 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
896 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
899 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
901 mitem
->SetMenuItem(menuItem
);
902 mitem
->SetSubMenu(subMenu
);
904 m_items
.Append( mitem
);
907 void wxMenu::Append( wxMenuItem
*item
)
909 m_items
.Append( item
);
911 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
913 if (item
->IsSeparator())
914 menuItem
= gtk_menu_item_new();
915 else if (item
->IsSubMenu())
916 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
918 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
919 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
921 if (!item
->IsSeparator())
923 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
924 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
927 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
928 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
931 if (!item
->IsSubMenu())
933 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
934 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
939 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
940 gtk_widget_show( menuItem
);
941 item
->SetMenuItem(menuItem
);
944 int wxMenu::FindItem( const wxString itemString
) const
947 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
952 #if (GTK_MINOR_VERSION > 0)
959 wxNode
*node
= m_items
.First();
962 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
963 if (item
->GetText() == s
)
965 return item
->GetId();
973 void wxMenu::Enable( int id
, bool enable
)
975 wxMenuItem
*item
= FindItem(id
);
977 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
979 item
->Enable(enable
);
982 bool wxMenu::IsEnabled( int id
) const
984 wxMenuItem
*item
= FindItem(id
);
986 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
988 return item
->IsEnabled();
991 void wxMenu::Check( int id
, bool enable
)
993 wxMenuItem
*item
= FindItem(id
);
995 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
1000 bool wxMenu::IsChecked( int id
) const
1002 wxMenuItem
*item
= FindItem(id
);
1004 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
1006 return item
->IsChecked();
1009 void wxMenu::SetLabel( int id
, const wxString
&label
)
1011 wxMenuItem
*item
= FindItem(id
);
1013 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
1015 item
->SetText(label
);
1018 wxString
wxMenu::GetLabel( int id
) const
1020 wxMenuItem
*item
= FindItem(id
);
1022 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1024 return item
->GetText();
1027 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1029 wxMenuItem
*item
= FindItem(id
);
1031 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1033 item
->SetHelp( helpString
);
1036 wxString
wxMenu::GetHelpString( int id
) const
1038 wxMenuItem
*item
= FindItem(id
);
1040 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1042 return item
->GetHelp();
1045 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1047 wxNode
*node
= m_items
.First();
1050 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1051 if (item
->GetMenuItem() == menuItem
)
1052 return item
->GetId();
1053 node
= node
->Next();
1059 wxMenuItem
*wxMenu::FindItem(int id
) const
1061 wxNode
*node
= m_items
.First();
1064 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1065 if (item
->GetId() == id
)
1069 node
= node
->Next();
1072 /* Not finding anything here can be correct
1073 * when search the entire menu system for
1074 * an entry -> no error message. */
1076 return (wxMenuItem
*) NULL
;
1079 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1081 m_invokingWindow
= win
;
1084 wxWindow
*wxMenu::GetInvokingWindow()
1086 return m_invokingWindow
;
1089 // Update a menu and all submenus recursively. source is the object that has
1090 // the update event handlers defined for it. If NULL, the menu or associated
1091 // window will be used.
1092 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1094 if (!source
&& GetInvokingWindow())
1095 source
= GetInvokingWindow()->GetEventHandler();
1097 source
= GetEventHandler();
1101 wxNode
* node
= GetItems().First();
1104 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1105 if ( !item
->IsSeparator() )
1107 wxWindowID id
= item
->GetId();
1108 wxUpdateUIEvent
event(id
);
1109 event
.SetEventObject( source
);
1111 if (source
->ProcessEvent(event
))
1113 if (event
.GetSetText())
1114 SetLabel(id
, event
.GetText());
1115 if (event
.GetSetChecked())
1116 Check(id
, event
.GetChecked());
1117 if (event
.GetSetEnabled())
1118 Enable(id
, event
.GetEnabled());
1121 if (item
->GetSubMenu())
1122 item
->GetSubMenu()->UpdateUI(source
);
1124 node
= node
->Next();