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;
221 entry
.item_type
= (m_style
& wxMB_TEAROFF
|| menu
->GetStyle() &
223 "<Tearoff>" : "<Branch>";
225 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
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
++ )
231 if (*pc
== _T('_')) pc
++; /* skip it */
235 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
237 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
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
);
245 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
250 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
252 if (menu
->GetTitle() == menuString
)
254 int res
= menu
->FindItem( itemString
);
255 if (res
!= wxNOT_FOUND
)
259 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
262 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
263 if (item
->IsSubMenu())
264 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
272 wxMenuItem
*wxMenuBar::FindItemForId(int itemId
, wxMenu
**menuForItem
) const
276 // TODO return the pointer to the menu
281 return FindItem(itemId
);
284 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
286 wxNode
*node
= m_menus
.First();
289 wxMenu
*menu
= (wxMenu
*)node
->Data();
290 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
291 if (res
!= -1) return res
;
297 // Find a wxMenuItem using its id. Recurses down into sub-menus
298 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
300 wxMenuItem
* result
= menu
->FindItem(id
);
302 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
303 while ( node
&& result
== NULL
)
305 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
306 if (item
->IsSubMenu())
308 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
316 wxMenuItem
* wxMenuBar::FindItem( int id
) const
318 wxMenuItem
* result
= 0;
319 wxNode
*node
= m_menus
.First();
320 while (node
&& result
== 0)
322 wxMenu
*menu
= (wxMenu
*)node
->Data();
323 result
= FindMenuItemByIdRecursive( menu
, id
);
330 void wxMenuBar::Check( int id
, bool check
)
332 wxMenuItem
* item
= FindMenuItemById( id
);
334 wxCHECK_RET( item
, _T("wxMenuBar::Check: no such item") );
339 bool wxMenuBar::IsChecked( int id
) const
341 wxMenuItem
* item
= FindMenuItemById( id
);
343 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked: no such item") );
345 return item
->IsChecked();
348 void wxMenuBar::Enable( int id
, bool enable
)
350 wxMenuItem
* item
= FindMenuItemById( id
);
352 wxCHECK_RET( item
, _T("wxMenuBar::Enable: no such item") );
354 item
->Enable(enable
);
357 bool wxMenuBar::IsEnabled( int id
) const
359 wxMenuItem
* item
= FindMenuItemById( id
);
361 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled: no such item") );
363 return item
->IsEnabled();
366 wxString
wxMenuBar::GetLabel( int id
) const
368 wxMenuItem
* item
= FindMenuItemById( id
);
370 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel: no such item") );
372 return item
->GetText();
375 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
377 wxMenuItem
* item
= FindMenuItemById( id
);
379 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel: no such item") );
381 item
->SetText( label
);
384 void wxMenuBar::EnableTop( int pos
, bool flag
)
386 wxNode
*node
= m_menus
.Nth( pos
);
388 wxCHECK_RET( node
, _T("menu not found") );
390 wxMenu
* menu
= (wxMenu
*)node
->Data();
393 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
396 wxString
wxMenuBar::GetLabelTop( int pos
) const
398 wxNode
*node
= m_menus
.Nth( pos
);
400 wxCHECK_MSG( node
, _T("invalid"), _T("menu not found") );
402 wxMenu
* menu
= (wxMenu
*)node
->Data();
404 return menu
->GetTitle();
407 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
409 wxNode
*node
= m_menus
.Nth( pos
);
411 wxCHECK_RET( node
, _T("menu not found") );
413 wxMenu
* menu
= (wxMenu
*)node
->Data();
415 menu
->SetTitle( label
);
418 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
420 wxMenuItem
* item
= FindMenuItemById( id
);
422 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString: no such item") );
424 item
->SetHelp( helpString
);
427 wxString
wxMenuBar::GetHelpString( int id
) const
429 wxMenuItem
* item
= FindMenuItemById( id
);
431 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
433 return item
->GetHelp();
436 //-----------------------------------------------------------------------------
438 //-----------------------------------------------------------------------------
440 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
442 if (g_isIdle
) wxapp_install_idle_handler();
444 int id
= menu
->FindMenuIdByMenuItem(widget
);
446 /* should find it for normal (not popup) menu */
447 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
449 if (!menu
->IsEnabled(id
))
452 wxMenuItem
* item
= menu
->FindItem( id
);
453 wxCHECK_RET( item
, _T("error in menu item callback") );
455 if (item
->IsCheckable())
457 if (item
->GetCheckedFlag() == item
->IsChecked())
459 /* the menu item has been checked by calling wxMenuItem->Check() */
464 /* the user pressed on the menu item -> report */
465 item
->SetCheckedFlag(item
->IsChecked()); /* make consistent again */
469 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
470 event
.SetEventObject( menu
);
473 if (menu
->GetCallback())
475 (void) (*(menu
->GetCallback())) (*menu
, event
);
479 if (menu
->GetEventHandler()->ProcessEvent(event
))
482 wxWindow
*win
= menu
->GetInvokingWindow();
484 win
->GetEventHandler()->ProcessEvent( event
);
487 //-----------------------------------------------------------------------------
489 //-----------------------------------------------------------------------------
491 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
493 if (g_isIdle
) wxapp_install_idle_handler();
495 int id
= menu
->FindMenuIdByMenuItem(widget
);
497 wxASSERT( id
!= -1 ); // should find it!
499 if (!menu
->IsEnabled(id
))
502 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
503 event
.SetEventObject( menu
);
505 /* wxMSW doesn't call callback here either
507 if (menu->m_callback)
509 (void) (*(menu->m_callback)) (*menu, event);
514 if (menu
->GetEventHandler()->ProcessEvent(event
))
517 wxWindow
*win
= menu
->GetInvokingWindow();
518 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
521 //-----------------------------------------------------------------------------
523 //-----------------------------------------------------------------------------
525 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
527 if (g_isIdle
) wxapp_install_idle_handler();
529 int id
= menu
->FindMenuIdByMenuItem(widget
);
531 wxASSERT( id
!= -1 ); // should find it!
533 if (!menu
->IsEnabled(id
))
536 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
537 event
.SetEventObject( menu
);
539 if (menu
->GetEventHandler()->ProcessEvent(event
))
542 wxWindow
*win
= menu
->GetInvokingWindow();
544 win
->GetEventHandler()->ProcessEvent( event
);
547 //-----------------------------------------------------------------------------
549 //-----------------------------------------------------------------------------
551 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
553 wxMenuItem::wxMenuItem()
556 m_isCheckMenu
= FALSE
;
559 m_subMenu
= (wxMenu
*) NULL
;
560 m_menuItem
= (GtkWidget
*) NULL
;
563 // it's valid for this function to be called even if m_menuItem == NULL
564 void wxMenuItem::SetName( const wxString
& str
)
566 /* '\t' is the deliminator indicating a hot key */
568 const wxChar
*pc
= str
;
569 for (; (*pc
!= _T('\0')) && (*pc
!= _T('\t')); pc
++ )
573 #if (GTK_MINOR_VERSION > 0)
576 if (*pc
== _T('/')) /* we have to filter out slashes ... */
578 m_text
<< _T('\\'); /* ... and replace them with back slashes */
585 /* only GTK 1.2 knows about hot keys */
587 #if (GTK_MINOR_VERSION > 0)
597 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
598 gtk_label_set( label
, m_text
.mb_str());
602 void wxMenuItem::Check( bool check
)
604 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
606 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
608 if (check
== m_isChecked
) return;
611 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
614 void wxMenuItem::Enable( bool enable
)
616 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
618 gtk_widget_set_sensitive( m_menuItem
, enable
);
619 m_isEnabled
= enable
;
622 bool wxMenuItem::IsChecked() const
624 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
626 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
628 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
633 //-----------------------------------------------------------------------------
635 //-----------------------------------------------------------------------------
637 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
639 wxMenu::wxMenu( const wxString
& title
, const wxFunction func
, long style
)
641 Init(title
, func
, style
);
644 wxMenu::wxMenu(long style
)
646 Init(wxEmptyString
, (wxFunction
) NULL
, style
);
650 wxMenu::Init( const wxString
& title
, const wxFunction func
, long style
)
653 m_items
.DeleteContents( TRUE
);
654 m_invokingWindow
= (wxWindow
*) NULL
;
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>" );
662 m_menu
= gtk_menu_new(); // Do not show!
666 m_eventHandler
= this;
667 m_clientData
= (void*) NULL
;
669 if (m_title
.IsNull()) m_title
= _T("");
670 if (m_title
!= _T(""))
676 m_owner
= (GtkWidget
*) NULL
;
681 /* how do we delete an item-factory ? */
684 void wxMenu::SetTitle( const wxString
& title
)
686 // TODO Waiting for something better
690 const wxString
wxMenu::GetTitle() const
695 void wxMenu::AppendSeparator()
697 wxMenuItem
*mitem
= new wxMenuItem();
698 mitem
->SetId(ID_SEPARATOR
);
700 #if (GTK_MINOR_VERSION > 0)
701 GtkItemFactoryEntry entry
;
703 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
704 entry
.callback_action
= 0;
705 entry
.item_type
= "<Separator>";
706 entry
.accelerator
= (gchar
*) NULL
;
708 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
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" );
713 GtkWidget
*menuItem
= gtk_menu_item_new();
714 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
715 gtk_widget_show( menuItem
);
718 mitem
->SetMenuItem(menuItem
);
719 m_items
.Append( mitem
);
722 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
724 if (hotkey
.IsEmpty()) return (char*) NULL
;
728 case _T('a'): /* Alt */
730 case _T('m'): /* Meta */
733 strcpy( hotbuf
, "<alt>" );
734 wxString last
= hotkey
.Right(1);
735 strcat( hotbuf
, last
.mb_str() );
738 case _T('c'): /* Ctrl */
740 case _T('s'): /* Strg, yeah man, I'm German */
743 strcpy( hotbuf
, "<control>" );
744 wxString last
= hotkey
.Right(1);
745 strcat( hotbuf
, last
.mb_str() );
748 case _T('F'): /* function keys */
750 strcpy( hotbuf
, hotkey
.mb_str() );
760 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
762 wxMenuItem
*mitem
= new wxMenuItem();
764 mitem
->SetText(item
);
765 mitem
->SetHelp(helpStr
);
766 mitem
->SetCheckable(checkable
);
768 #if (GTK_MINOR_VERSION > 0)
769 /* text has "_" instead of "&" after mitem->SetText() */
770 wxString
text( mitem
->GetText() );
772 /* local buffer in multibyte form */
775 strcat( buf
, text
.mb_str() );
777 GtkItemFactoryEntry entry
;
779 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
780 entry
.callback_action
= 0;
782 entry
.item_type
= "<CheckItem>";
784 entry
.item_type
= "<Item>";
787 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
789 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
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
++ )
795 if (*pc
== _T('_')) pc
++; /* skip it */
799 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
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() );
806 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
807 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
810 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
811 gtk_widget_show( menuItem
);
815 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
816 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
819 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
820 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
823 mitem
->SetMenuItem(menuItem
);
825 m_items
.Append( mitem
);
828 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
830 wxMenuItem
*mitem
= new wxMenuItem();
832 mitem
->SetText(item
);
833 mitem
->SetHelp(helpStr
);
835 #if (GTK_MINOR_VERSION > 0)
836 /* text has "_" instead of "&" after mitem->SetText() */
837 wxString
text( mitem
->GetText() );
839 /* local buffer in multibyte form */
842 strcat( buf
, text
.mb_str() );
844 GtkItemFactoryEntry entry
;
846 entry
.callback
= (GtkItemFactoryCallback
) 0;
847 entry
.callback_action
= 0;
848 entry
.item_type
= (m_style
& wxMENU_TEAROFF
) ? "<Tearoff>" : "<Branch>";
850 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
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
++ )
856 if (*pc
== _T('_')) pc
++; /* skip it */
860 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
864 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
866 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
867 gtk_widget_show( menuItem
);
871 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
872 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
875 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
876 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
879 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
881 mitem
->SetMenuItem(menuItem
);
882 mitem
->SetSubMenu(subMenu
);
884 m_items
.Append( mitem
);
887 void wxMenu::Append( wxMenuItem
*item
)
889 m_items
.Append( item
);
891 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
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());
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());
901 if (!item
->IsSeparator())
903 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
904 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
907 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
908 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
911 if (!item
->IsSubMenu())
913 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
914 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
919 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
920 gtk_widget_show( menuItem
);
921 item
->SetMenuItem(menuItem
);
924 int wxMenu::FindItem( const wxString itemString
) const
927 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
932 #if (GTK_MINOR_VERSION > 0)
939 wxNode
*node
= m_items
.First();
942 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
943 if (item
->GetText() == s
)
945 return item
->GetId();
953 void wxMenu::Enable( int id
, bool enable
)
955 wxMenuItem
*item
= FindItem(id
);
957 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
959 item
->Enable(enable
);
962 bool wxMenu::IsEnabled( int id
) const
964 wxMenuItem
*item
= FindItem(id
);
966 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
968 return item
->IsEnabled();
971 void wxMenu::Check( int id
, bool enable
)
973 wxMenuItem
*item
= FindItem(id
);
975 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
980 bool wxMenu::IsChecked( int id
) const
982 wxMenuItem
*item
= FindItem(id
);
984 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
986 return item
->IsChecked();
989 void wxMenu::SetLabel( int id
, const wxString
&label
)
991 wxMenuItem
*item
= FindItem(id
);
993 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
995 item
->SetText(label
);
998 wxString
wxMenu::GetLabel( int id
) const
1000 wxMenuItem
*item
= FindItem(id
);
1002 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1004 return item
->GetText();
1007 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1009 wxMenuItem
*item
= FindItem(id
);
1011 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1013 item
->SetHelp( helpString
);
1016 wxString
wxMenu::GetHelpString( int id
) const
1018 wxMenuItem
*item
= FindItem(id
);
1020 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1022 return item
->GetHelp();
1025 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1027 wxNode
*node
= m_items
.First();
1030 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1031 if (item
->GetMenuItem() == menuItem
)
1032 return item
->GetId();
1033 node
= node
->Next();
1039 wxMenuItem
*wxMenu::FindItem(int id
) const
1041 wxNode
*node
= m_items
.First();
1044 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1045 if (item
->GetId() == id
)
1049 node
= node
->Next();
1052 /* Not finding anything here can be correct
1053 * when search the entire menu system for
1054 * an entry -> no error message. */
1056 return (wxMenuItem
*) NULL
;
1059 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1061 m_invokingWindow
= win
;
1064 wxWindow
*wxMenu::GetInvokingWindow()
1066 return m_invokingWindow
;
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.
1072 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1074 if (!source
&& GetInvokingWindow())
1075 source
= GetInvokingWindow()->GetEventHandler();
1077 source
= GetEventHandler();
1081 wxNode
* node
= GetItems().First();
1084 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1085 if ( !item
->IsSeparator() )
1087 wxWindowID id
= item
->GetId();
1088 wxUpdateUIEvent
event(id
);
1089 event
.SetEventObject( source
);
1091 if (source
->ProcessEvent(event
))
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());
1101 if (item
->GetSubMenu())
1102 item
->GetSubMenu()->UpdateUI(source
);
1104 node
= node
->Next();