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() */
41 m_invokingWindow
= (wxWindow
*) NULL
;
43 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
44 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, _T("menubar") ))
46 wxFAIL_MSG( _T("wxMenuBar creation failed") );
50 m_menus
.DeleteContents( TRUE
);
52 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
53 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
54 m_accel
= gtk_accel_group_new();
55 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
56 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
58 m_menubar
= gtk_menu_bar_new();
61 if (style
& wxMB_DOCKABLE
)
63 m_widget
= gtk_handle_box_new();
64 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
65 gtk_widget_show( GTK_WIDGET(m_menubar
) );
69 m_widget
= GTK_WIDGET(m_menubar
);
75 wxMenuBar::wxMenuBar()
77 /* the parent window is known after wxFrame::SetMenu() */
80 m_invokingWindow
= (wxWindow
*) NULL
;
82 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
83 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, wxDefaultValidator
, _T("menubar") ))
85 wxFAIL_MSG( _T("wxMenuBar creation failed") );
89 m_menus
.DeleteContents( TRUE
);
91 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
92 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
93 m_accel
= gtk_accel_group_new();
94 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
95 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
97 m_menubar
= gtk_menu_bar_new();
100 m_widget
= GTK_WIDGET(m_menubar
);
105 wxMenuBar::~wxMenuBar()
107 // how to destroy a GtkItemFactory ?
110 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
112 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
114 #if (GTK_MINOR_VERSION > 0)
115 wxWindow
*top_frame
= win
;
116 while (top_frame
->GetParent()) top_frame
= top_frame
->GetParent();
118 /* support for native hot keys */
119 gtk_accel_group_detach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
122 wxNode
*node
= menu
->GetItems().First();
125 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
126 if (menuitem
->IsSubMenu())
127 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
132 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
134 menu
->SetInvokingWindow( win
);
136 #if (GTK_MINOR_VERSION > 0)
137 wxWindow
*top_frame
= win
;
138 while (top_frame
->GetParent())
139 top_frame
= top_frame
->GetParent();
141 /* support for native hot keys */
142 gtk_accel_group_attach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
145 wxNode
*node
= menu
->GetItems().First();
148 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
149 if (menuitem
->IsSubMenu())
150 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
155 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
157 m_invokingWindow
= win
;
158 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
159 wxWindow
*top_frame
= win
;
160 while (top_frame
->GetParent())
161 top_frame
= top_frame
->GetParent();
163 /* support for native key accelerators indicated by underscroes */
164 gtk_accel_group_attach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
167 wxNode
*node
= m_menus
.First();
170 wxMenu
*menu
= (wxMenu
*)node
->Data();
171 wxMenubarSetInvokingWindow( menu
, win
);
176 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
178 m_invokingWindow
= (wxWindow
*) NULL
;
179 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
180 wxWindow
*top_frame
= win
;
181 while (top_frame
->GetParent())
182 top_frame
= top_frame
->GetParent();
184 /* support for native key accelerators indicated by underscroes */
185 gtk_accel_group_detach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
188 wxNode
*node
= m_menus
.First();
191 wxMenu
*menu
= (wxMenu
*)node
->Data();
192 wxMenubarUnsetInvokingWindow( menu
, win
);
197 void wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
199 m_menus
.Append( menu
);
203 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
205 for ( pc
= title
; *pc
!= _T('\0'); pc
++ )
209 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
212 else if (*pc
== _T('/'))
220 if ( *pc
== _T('_') )
222 // underscores must be doubled to prevent them from being
223 // interpreted as accelerator character prefix by GTK
232 /* this doesn't have much effect right now */
233 menu
->SetTitle( str
);
235 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
236 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
238 /* local buffer in multibyte form */
240 buf
<< _T('/') << str
.c_str();
242 char *cbuf
= new char[buf
.Length()+1];
243 strcpy(cbuf
, buf
.mbc_str());
245 GtkItemFactoryEntry entry
;
246 entry
.path
= (gchar
*)cbuf
; // const_cast
247 entry
.accelerator
= (gchar
*) NULL
;
248 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
249 entry
.callback_action
= 0;
250 entry
.item_type
= "<Branch>";
252 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
253 /* in order to get the pointer to the item we need the item text _without_ underscores */
254 wxString tmp
= _T("<main>/");
255 for ( pc
= str
; *pc
!= _T('\0'); pc
++ )
257 // contrary to the common sense, we must throw out _all_ underscores,
258 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
259 // might naively think). IMHO it's a bug in GTK+ (VZ)
260 while (*pc
== _T('_'))
264 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
265 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
269 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
270 gtk_widget_show( menu
->m_owner
);
271 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
273 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
277 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
278 // adding menu later on.
279 if (m_invokingWindow
)
280 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
283 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
285 if (menu
->GetTitle() == menuString
)
287 int res
= menu
->FindItem( itemString
);
288 if (res
!= wxNOT_FOUND
)
292 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
295 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
296 if (item
->IsSubMenu())
297 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
305 wxMenuItem
*wxMenuBar::FindItemForId(int itemId
, wxMenu
**menuForItem
) const
309 // TODO return the pointer to the menu
314 return FindItem(itemId
);
317 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
319 wxNode
*node
= m_menus
.First();
322 wxMenu
*menu
= (wxMenu
*)node
->Data();
323 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
324 if (res
!= -1) return res
;
330 // Find a wxMenuItem using its id. Recurses down into sub-menus
331 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
333 wxMenuItem
* result
= menu
->FindItem(id
);
335 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
336 while ( node
&& result
== NULL
)
338 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
339 if (item
->IsSubMenu())
341 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
349 wxMenuItem
* wxMenuBar::FindItem( int id
) const
351 wxMenuItem
* result
= 0;
352 wxNode
*node
= m_menus
.First();
353 while (node
&& result
== 0)
355 wxMenu
*menu
= (wxMenu
*)node
->Data();
356 result
= FindMenuItemByIdRecursive( menu
, id
);
363 void wxMenuBar::Check( int id
, bool check
)
365 wxMenuItem
* item
= FindMenuItemById( id
);
367 wxCHECK_RET( item
, _T("wxMenuBar::Check: no such item") );
372 bool wxMenuBar::IsChecked( int id
) const
374 wxMenuItem
* item
= FindMenuItemById( id
);
376 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked: no such item") );
378 return item
->IsChecked();
381 void wxMenuBar::Enable( int id
, bool enable
)
383 wxMenuItem
* item
= FindMenuItemById( id
);
385 wxCHECK_RET( item
, _T("wxMenuBar::Enable: no such item") );
387 item
->Enable(enable
);
390 bool wxMenuBar::IsEnabled( int id
) const
392 wxMenuItem
* item
= FindMenuItemById( id
);
394 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled: no such item") );
396 return item
->IsEnabled();
399 wxString
wxMenuBar::GetLabel( int id
) const
401 wxMenuItem
* item
= FindMenuItemById( id
);
403 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel: no such item") );
405 return item
->GetText();
408 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
410 wxMenuItem
* item
= FindMenuItemById( id
);
412 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel: no such item") );
414 item
->SetText( label
);
417 void wxMenuBar::EnableTop( int pos
, bool flag
)
419 wxNode
*node
= m_menus
.Nth( pos
);
421 wxCHECK_RET( node
, _T("menu not found") );
423 wxMenu
* menu
= (wxMenu
*)node
->Data();
426 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
429 wxString
wxMenuBar::GetLabelTop( int pos
) const
431 wxNode
*node
= m_menus
.Nth( pos
);
433 wxCHECK_MSG( node
, _T("invalid"), _T("menu not found") );
435 wxMenu
* menu
= (wxMenu
*)node
->Data();
437 return menu
->GetTitle();
440 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
442 wxNode
*node
= m_menus
.Nth( pos
);
444 wxCHECK_RET( node
, _T("menu not found") );
446 wxMenu
* menu
= (wxMenu
*)node
->Data();
448 menu
->SetTitle( label
);
451 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
453 wxMenuItem
* item
= FindMenuItemById( id
);
455 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString: no such item") );
457 item
->SetHelp( helpString
);
460 wxString
wxMenuBar::GetHelpString( int id
) const
462 wxMenuItem
* item
= FindMenuItemById( id
);
464 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
466 return item
->GetHelp();
469 //-----------------------------------------------------------------------------
471 //-----------------------------------------------------------------------------
473 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
475 if (g_isIdle
) wxapp_install_idle_handler();
477 int id
= menu
->FindMenuIdByMenuItem(widget
);
479 /* should find it for normal (not popup) menu */
480 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
482 if (!menu
->IsEnabled(id
))
485 wxMenuItem
* item
= menu
->FindItem( id
);
486 wxCHECK_RET( item
, _T("error in menu item callback") );
488 if (item
->IsCheckable())
490 if (item
->GetCheckedFlag() == item
->IsChecked())
492 /* the menu item has been checked by calling wxMenuItem->Check() */
497 /* the user pressed on the menu item -> report */
498 item
->SetCheckedFlag(item
->IsChecked()); /* make consistent again */
502 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
503 event
.SetEventObject( menu
);
506 if (menu
->GetCallback())
508 (void) (*(menu
->GetCallback())) (*menu
, event
);
512 if (menu
->GetEventHandler()->ProcessEvent(event
))
515 wxWindow
*win
= menu
->GetInvokingWindow();
517 win
->GetEventHandler()->ProcessEvent( event
);
520 //-----------------------------------------------------------------------------
522 //-----------------------------------------------------------------------------
524 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
526 if (g_isIdle
) wxapp_install_idle_handler();
528 int id
= menu
->FindMenuIdByMenuItem(widget
);
530 wxASSERT( id
!= -1 ); // should find it!
532 if (!menu
->IsEnabled(id
))
535 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
536 event
.SetEventObject( menu
);
538 if (menu
->GetEventHandler()->ProcessEvent(event
))
541 wxWindow
*win
= menu
->GetInvokingWindow();
542 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
545 //-----------------------------------------------------------------------------
547 //-----------------------------------------------------------------------------
549 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
551 if (g_isIdle
) wxapp_install_idle_handler();
553 int id
= menu
->FindMenuIdByMenuItem(widget
);
555 wxASSERT( id
!= -1 ); // should find it!
557 if (!menu
->IsEnabled(id
))
560 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
561 event
.SetEventObject( menu
);
563 if (menu
->GetEventHandler()->ProcessEvent(event
))
566 wxWindow
*win
= menu
->GetInvokingWindow();
568 win
->GetEventHandler()->ProcessEvent( event
);
571 //-----------------------------------------------------------------------------
573 //-----------------------------------------------------------------------------
575 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
577 wxMenuItem::wxMenuItem()
580 m_isCheckMenu
= FALSE
;
583 m_subMenu
= (wxMenu
*) NULL
;
584 m_menuItem
= (GtkWidget
*) NULL
;
587 // it's valid for this function to be called even if m_menuItem == NULL
588 void wxMenuItem::SetName( const wxString
& str
)
590 /* '\t' is the deliminator indicating a hot key */
592 const wxChar
*pc
= str
;
593 for (; (*pc
!= _T('\0')) && (*pc
!= _T('\t')); pc
++ )
597 #if (GTK_MINOR_VERSION > 0)
600 else if ( *pc
== _T('_') ) // escape underscores
604 else if (*pc
== _T('/')) /* we have to filter out slashes ... */
606 m_text
<< _T('\\'); /* ... and replace them with back slashes */
613 /* only GTK 1.2 knows about hot keys */
615 #if (GTK_MINOR_VERSION > 0)
625 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
626 gtk_label_set( label
, m_text
.mb_str());
630 void wxMenuItem::Check( bool check
)
632 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
634 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
636 if (check
== m_isChecked
) return;
639 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
642 void wxMenuItem::Enable( bool enable
)
644 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
646 gtk_widget_set_sensitive( m_menuItem
, enable
);
647 m_isEnabled
= enable
;
650 bool wxMenuItem::IsChecked() const
652 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
654 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
656 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
661 //-----------------------------------------------------------------------------
663 //-----------------------------------------------------------------------------
665 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
668 wxMenu::Init( const wxString
& title
,
670 const wxFunction func
674 m_items
.DeleteContents( TRUE
);
675 m_invokingWindow
= (wxWindow
*) NULL
;
678 #if (GTK_MINOR_VERSION > 0)
679 m_accel
= gtk_accel_group_new();
680 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
681 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
683 m_menu
= gtk_menu_new(); // Do not show!
688 m_eventHandler
= this;
689 m_clientData
= (void*) NULL
;
691 if (m_title
.IsNull()) m_title
= _T("");
692 if (m_title
!= _T(""))
698 m_owner
= (GtkWidget
*) NULL
;
700 #if (GTK_MINOR_VERSION > 0)
701 /* Tearoffs are entries, just like separators. So if we want this
702 menu to be a tear-off one, we just append a tearoff entry
704 if(m_style
& wxMENU_TEAROFF
)
706 GtkItemFactoryEntry entry
;
707 entry
.path
= "/tearoff";
708 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
709 entry
.callback_action
= 0;
710 entry
.item_type
= "<Tearoff>";
711 entry
.accelerator
= (gchar
*) NULL
;
712 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
713 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
720 /* how do we delete an item-factory ? */
721 gtk_widget_destroy( m_menu
);
725 void wxMenu::SetTitle( const wxString
& title
)
727 // TODO Waiting for something better
731 const wxString
wxMenu::GetTitle() const
736 void wxMenu::AppendSeparator()
738 wxMenuItem
*mitem
= new wxMenuItem();
739 mitem
->SetId(ID_SEPARATOR
);
741 #if (GTK_MINOR_VERSION > 0)
742 GtkItemFactoryEntry entry
;
744 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
745 entry
.callback_action
= 0;
746 entry
.item_type
= "<Separator>";
747 entry
.accelerator
= (gchar
*) NULL
;
749 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
751 /* this will be wrong for more than one separator. do we care? */
752 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
754 GtkWidget
*menuItem
= gtk_menu_item_new();
755 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
756 gtk_widget_show( menuItem
);
759 mitem
->SetMenuItem(menuItem
);
760 m_items
.Append( mitem
);
763 #if (GTK_MINOR_VERSION > 0)
764 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
766 if (hotkey
.IsEmpty()) return (char*) NULL
;
770 case _T('a'): /* Alt */
772 case _T('m'): /* Meta */
775 strcpy( hotbuf
, "<alt>" );
776 wxString last
= hotkey
.Right(1);
777 strcat( hotbuf
, last
.mb_str() );
780 case _T('c'): /* Ctrl */
782 case _T('s'): /* Strg, yeah man, I'm German */
785 strcpy( hotbuf
, "<control>" );
786 wxString last
= hotkey
.Right(1);
787 strcat( hotbuf
, last
.mb_str() );
790 case _T('F'): /* function keys */
792 strcpy( hotbuf
, hotkey
.mb_str() );
803 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
805 wxMenuItem
*mitem
= new wxMenuItem();
807 mitem
->SetText(item
);
808 mitem
->SetHelp(helpStr
);
809 mitem
->SetCheckable(checkable
);
811 #if (GTK_MINOR_VERSION > 0)
812 /* text has "_" instead of "&" after mitem->SetText() */
813 wxString
text( mitem
->GetText() );
815 /* local buffer in multibyte form */
818 strcat( buf
, text
.mb_str() );
820 GtkItemFactoryEntry entry
;
822 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
823 entry
.callback_action
= 0;
825 entry
.item_type
= "<CheckItem>";
827 entry
.item_type
= "<Item>";
830 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
832 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
834 /* in order to get the pointer to the item we need the item text _without_ underscores */
835 wxString s
= _T("<main>/");
836 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
838 while (*pc
== _T('_')) pc
++; /* skip it */
842 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
846 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
847 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
849 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
850 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
853 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
854 gtk_widget_show( menuItem
);
858 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
859 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
862 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
863 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
866 mitem
->SetMenuItem(menuItem
);
868 m_items
.Append( mitem
);
871 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
873 wxMenuItem
*mitem
= new wxMenuItem();
875 mitem
->SetText(item
);
876 mitem
->SetHelp(helpStr
);
878 #if (GTK_MINOR_VERSION > 0)
879 /* text has "_" instead of "&" after mitem->SetText() */
880 wxString
text( mitem
->GetText() );
882 /* local buffer in multibyte form */
885 strcat( buf
, text
.mb_str() );
887 GtkItemFactoryEntry entry
;
889 entry
.callback
= (GtkItemFactoryCallback
) 0;
890 entry
.callback_action
= 0;
891 entry
.item_type
= "<Branch>";
893 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
895 /* in order to get the pointer to the item we need the item text _without_ underscores */
896 wxString s
= _T("<main>/");
897 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
899 if (*pc
== _T('_')) pc
++; /* skip it */
903 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
907 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
909 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
910 gtk_widget_show( menuItem
);
914 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
915 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
918 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
919 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
922 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
924 mitem
->SetMenuItem(menuItem
);
925 mitem
->SetSubMenu(subMenu
);
927 m_items
.Append( mitem
);
930 void wxMenu::Append( wxMenuItem
*item
)
932 m_items
.Append( item
);
934 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
936 if (item
->IsSeparator())
937 menuItem
= gtk_menu_item_new();
938 else if (item
->IsSubMenu())
939 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
941 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
942 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
944 if (!item
->IsSeparator())
946 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
947 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
950 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
951 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
954 if (!item
->IsSubMenu())
956 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
957 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
962 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
963 gtk_widget_show( menuItem
);
964 item
->SetMenuItem(menuItem
);
967 int wxMenu::FindItem( const wxString itemString
) const
970 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
975 #if (GTK_MINOR_VERSION > 0)
982 wxNode
*node
= m_items
.First();
985 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
986 if (item
->GetText() == s
)
988 return item
->GetId();
996 void wxMenu::Enable( int id
, bool enable
)
998 wxMenuItem
*item
= FindItem(id
);
1000 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
1002 item
->Enable(enable
);
1005 bool wxMenu::IsEnabled( int id
) const
1007 wxMenuItem
*item
= FindItem(id
);
1009 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
1011 return item
->IsEnabled();
1014 void wxMenu::Check( int id
, bool enable
)
1016 wxMenuItem
*item
= FindItem(id
);
1018 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
1020 item
->Check(enable
);
1023 bool wxMenu::IsChecked( int id
) const
1025 wxMenuItem
*item
= FindItem(id
);
1027 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
1029 return item
->IsChecked();
1032 void wxMenu::SetLabel( int id
, const wxString
&label
)
1034 wxMenuItem
*item
= FindItem(id
);
1036 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
1038 item
->SetText(label
);
1041 wxString
wxMenu::GetLabel( int id
) const
1043 wxMenuItem
*item
= FindItem(id
);
1045 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1047 return item
->GetText();
1050 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1052 wxMenuItem
*item
= FindItem(id
);
1054 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1056 item
->SetHelp( helpString
);
1059 wxString
wxMenu::GetHelpString( int id
) const
1061 wxMenuItem
*item
= FindItem(id
);
1063 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1065 return item
->GetHelp();
1068 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1070 wxNode
*node
= m_items
.First();
1073 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1074 if (item
->GetMenuItem() == menuItem
)
1075 return item
->GetId();
1076 node
= node
->Next();
1082 wxMenuItem
*wxMenu::FindItem(int id
) const
1084 wxNode
*node
= m_items
.First();
1087 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1088 if (item
->GetId() == id
)
1092 node
= node
->Next();
1095 /* Not finding anything here can be correct
1096 * when search the entire menu system for
1097 * an entry -> no error message. */
1099 return (wxMenuItem
*) NULL
;
1102 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1104 m_invokingWindow
= win
;
1107 wxWindow
*wxMenu::GetInvokingWindow()
1109 return m_invokingWindow
;
1112 // Update a menu and all submenus recursively. source is the object that has
1113 // the update event handlers defined for it. If NULL, the menu or associated
1114 // window will be used.
1115 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1117 if (!source
&& GetInvokingWindow())
1118 source
= GetInvokingWindow()->GetEventHandler();
1120 source
= GetEventHandler();
1124 wxNode
* node
= GetItems().First();
1127 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1128 if ( !item
->IsSeparator() )
1130 wxWindowID id
= item
->GetId();
1131 wxUpdateUIEvent
event(id
);
1132 event
.SetEventObject( source
);
1134 if (source
->ProcessEvent(event
))
1136 if (event
.GetSetText())
1137 SetLabel(id
, event
.GetText());
1138 if (event
.GetSetChecked())
1139 Check(id
, event
.GetChecked());
1140 if (event
.GetSetEnabled())
1141 Enable(id
, event
.GetEnabled());
1144 if (item
->GetSubMenu())
1145 item
->GetSubMenu()->UpdateUI(source
);
1147 node
= node
->Next();