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('/'))
219 if ( *pc
== _T('_') )
221 // underscores must be doubled to prevent them from being
222 // interpreted as accelerator character prefix by GTK
230 /* this doesn't have much effect right now */
231 menu
->SetTitle( str
);
233 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
234 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
236 /* local buffer in multibyte form */
238 buf
<< _T('/') << str
.c_str();
240 char *cbuf
= new char[buf
.Length()+1];
241 strcpy(cbuf
, buf
.mbc_str());
243 GtkItemFactoryEntry entry
;
244 entry
.path
= (gchar
*)cbuf
; // const_cast
245 entry
.accelerator
= (gchar
*) NULL
;
246 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
247 entry
.callback_action
= 0;
248 entry
.item_type
= "<Branch>";
250 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
251 /* in order to get the pointer to the item we need the item text _without_ underscores */
252 wxString tmp
= _T("<main>/");
253 for ( pc
= str
; *pc
!= _T('\0'); pc
++ )
255 // contrary to the common sense, we must throw out _all_ underscores,
256 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
257 // might naively think). IMHO it's a bug in GTK+ (VZ)
258 while (*pc
== _T('_'))
262 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
263 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
267 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
268 gtk_widget_show( menu
->m_owner
);
269 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
271 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
275 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
276 // adding menu later on.
277 if (m_invokingWindow
)
278 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
281 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
283 if (menu
->GetTitle() == menuString
)
285 int res
= menu
->FindItem( itemString
);
286 if (res
!= wxNOT_FOUND
)
290 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
293 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
294 if (item
->IsSubMenu())
295 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
303 wxMenuItem
*wxMenuBar::FindItemForId(int itemId
, wxMenu
**menuForItem
) const
307 // TODO return the pointer to the menu
312 return FindItem(itemId
);
315 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
317 wxNode
*node
= m_menus
.First();
320 wxMenu
*menu
= (wxMenu
*)node
->Data();
321 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
322 if (res
!= -1) return res
;
328 // Find a wxMenuItem using its id. Recurses down into sub-menus
329 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
331 wxMenuItem
* result
= menu
->FindItem(id
);
333 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
334 while ( node
&& result
== NULL
)
336 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
337 if (item
->IsSubMenu())
339 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
347 wxMenuItem
* wxMenuBar::FindItem( int id
) const
349 wxMenuItem
* result
= 0;
350 wxNode
*node
= m_menus
.First();
351 while (node
&& result
== 0)
353 wxMenu
*menu
= (wxMenu
*)node
->Data();
354 result
= FindMenuItemByIdRecursive( menu
, id
);
361 void wxMenuBar::Check( int id
, bool check
)
363 wxMenuItem
* item
= FindMenuItemById( id
);
365 wxCHECK_RET( item
, _T("wxMenuBar::Check: no such item") );
370 bool wxMenuBar::IsChecked( int id
) const
372 wxMenuItem
* item
= FindMenuItemById( id
);
374 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked: no such item") );
376 return item
->IsChecked();
379 void wxMenuBar::Enable( int id
, bool enable
)
381 wxMenuItem
* item
= FindMenuItemById( id
);
383 wxCHECK_RET( item
, _T("wxMenuBar::Enable: no such item") );
385 item
->Enable(enable
);
388 bool wxMenuBar::IsEnabled( int id
) const
390 wxMenuItem
* item
= FindMenuItemById( id
);
392 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled: no such item") );
394 return item
->IsEnabled();
397 wxString
wxMenuBar::GetLabel( int id
) const
399 wxMenuItem
* item
= FindMenuItemById( id
);
401 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel: no such item") );
403 return item
->GetText();
406 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
408 wxMenuItem
* item
= FindMenuItemById( id
);
410 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel: no such item") );
412 item
->SetText( label
);
415 void wxMenuBar::EnableTop( int pos
, bool flag
)
417 wxNode
*node
= m_menus
.Nth( pos
);
419 wxCHECK_RET( node
, _T("menu not found") );
421 wxMenu
* menu
= (wxMenu
*)node
->Data();
424 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
427 wxString
wxMenuBar::GetLabelTop( int pos
) const
429 wxNode
*node
= m_menus
.Nth( pos
);
431 wxCHECK_MSG( node
, _T("invalid"), _T("menu not found") );
433 wxMenu
* menu
= (wxMenu
*)node
->Data();
435 return menu
->GetTitle();
438 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
440 wxNode
*node
= m_menus
.Nth( pos
);
442 wxCHECK_RET( node
, _T("menu not found") );
444 wxMenu
* menu
= (wxMenu
*)node
->Data();
446 menu
->SetTitle( label
);
449 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
451 wxMenuItem
* item
= FindMenuItemById( id
);
453 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString: no such item") );
455 item
->SetHelp( helpString
);
458 wxString
wxMenuBar::GetHelpString( int id
) const
460 wxMenuItem
* item
= FindMenuItemById( id
);
462 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
464 return item
->GetHelp();
467 //-----------------------------------------------------------------------------
469 //-----------------------------------------------------------------------------
471 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
473 if (g_isIdle
) wxapp_install_idle_handler();
475 int id
= menu
->FindMenuIdByMenuItem(widget
);
477 /* should find it for normal (not popup) menu */
478 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
480 if (!menu
->IsEnabled(id
))
483 wxMenuItem
* item
= menu
->FindItem( id
);
484 wxCHECK_RET( item
, _T("error in menu item callback") );
486 if (item
->IsCheckable())
488 if (item
->GetCheckedFlag() == item
->IsChecked())
490 /* the menu item has been checked by calling wxMenuItem->Check() */
495 /* the user pressed on the menu item -> report */
496 item
->SetCheckedFlag(item
->IsChecked()); /* make consistent again */
500 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
501 event
.SetEventObject( menu
);
504 if (menu
->GetCallback())
506 (void) (*(menu
->GetCallback())) (*menu
, event
);
510 if (menu
->GetEventHandler()->ProcessEvent(event
))
513 wxWindow
*win
= menu
->GetInvokingWindow();
515 win
->GetEventHandler()->ProcessEvent( event
);
518 //-----------------------------------------------------------------------------
520 //-----------------------------------------------------------------------------
522 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
524 if (g_isIdle
) wxapp_install_idle_handler();
526 int id
= menu
->FindMenuIdByMenuItem(widget
);
528 wxASSERT( id
!= -1 ); // should find it!
530 if (!menu
->IsEnabled(id
))
533 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
534 event
.SetEventObject( menu
);
536 if (menu
->GetEventHandler()->ProcessEvent(event
))
539 wxWindow
*win
= menu
->GetInvokingWindow();
540 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
543 //-----------------------------------------------------------------------------
545 //-----------------------------------------------------------------------------
547 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
549 if (g_isIdle
) wxapp_install_idle_handler();
551 int id
= menu
->FindMenuIdByMenuItem(widget
);
553 wxASSERT( id
!= -1 ); // should find it!
555 if (!menu
->IsEnabled(id
))
558 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
559 event
.SetEventObject( menu
);
561 if (menu
->GetEventHandler()->ProcessEvent(event
))
564 wxWindow
*win
= menu
->GetInvokingWindow();
566 win
->GetEventHandler()->ProcessEvent( event
);
569 //-----------------------------------------------------------------------------
571 //-----------------------------------------------------------------------------
573 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
575 wxMenuItem::wxMenuItem()
578 m_isCheckMenu
= FALSE
;
581 m_subMenu
= (wxMenu
*) NULL
;
582 m_menuItem
= (GtkWidget
*) NULL
;
585 // it's valid for this function to be called even if m_menuItem == NULL
586 void wxMenuItem::SetName( const wxString
& str
)
588 /* '\t' is the deliminator indicating a hot key */
590 const wxChar
*pc
= str
;
591 for (; (*pc
!= _T('\0')) && (*pc
!= _T('\t')); pc
++ )
595 #if (GTK_MINOR_VERSION > 0)
598 if (*pc
== _T('/')) /* we have to filter out slashes ... */
600 m_text
<< _T('\\'); /* ... and replace them with back slashes */
607 /* only GTK 1.2 knows about hot keys */
609 #if (GTK_MINOR_VERSION > 0)
619 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
620 gtk_label_set( label
, m_text
.mb_str());
624 void wxMenuItem::Check( bool check
)
626 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
628 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
630 if (check
== m_isChecked
) return;
633 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
636 void wxMenuItem::Enable( bool enable
)
638 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
640 gtk_widget_set_sensitive( m_menuItem
, enable
);
641 m_isEnabled
= enable
;
644 bool wxMenuItem::IsChecked() const
646 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
648 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
650 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
655 //-----------------------------------------------------------------------------
657 //-----------------------------------------------------------------------------
659 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
662 wxMenu::Init( const wxString
& title
,
664 const wxFunction func
668 m_items
.DeleteContents( TRUE
);
669 m_invokingWindow
= (wxWindow
*) NULL
;
672 #if (GTK_MINOR_VERSION > 0)
673 m_accel
= gtk_accel_group_new();
674 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
675 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
677 m_menu
= gtk_menu_new(); // Do not show!
682 m_eventHandler
= this;
683 m_clientData
= (void*) NULL
;
685 if (m_title
.IsNull()) m_title
= _T("");
686 if (m_title
!= _T(""))
692 m_owner
= (GtkWidget
*) NULL
;
694 #if (GTK_MINOR_VERSION > 0)
695 /* Tearoffs are entries, just like separators. So if we want this
696 menu to be a tear-off one, we just append a tearoff entry
698 if(m_style
& wxMENU_TEAROFF
)
700 GtkItemFactoryEntry entry
;
701 entry
.path
= "/tearoff";
702 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
703 entry
.callback_action
= 0;
704 entry
.item_type
= "<Tearoff>";
705 entry
.accelerator
= (gchar
*) NULL
;
706 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
707 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
714 /* how do we delete an item-factory ? */
715 gtk_widget_destroy( m_menu
);
719 void wxMenu::SetTitle( const wxString
& title
)
721 // TODO Waiting for something better
725 const wxString
wxMenu::GetTitle() const
730 void wxMenu::AppendSeparator()
732 wxMenuItem
*mitem
= new wxMenuItem();
733 mitem
->SetId(ID_SEPARATOR
);
735 #if (GTK_MINOR_VERSION > 0)
736 GtkItemFactoryEntry entry
;
738 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
739 entry
.callback_action
= 0;
740 entry
.item_type
= "<Separator>";
741 entry
.accelerator
= (gchar
*) NULL
;
743 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
745 /* this will be wrong for more than one separator. do we care? */
746 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
748 GtkWidget
*menuItem
= gtk_menu_item_new();
749 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
750 gtk_widget_show( menuItem
);
753 mitem
->SetMenuItem(menuItem
);
754 m_items
.Append( mitem
);
757 #if (GTK_MINOR_VERSION > 0)
758 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
760 if (hotkey
.IsEmpty()) return (char*) NULL
;
764 case _T('a'): /* Alt */
766 case _T('m'): /* Meta */
769 strcpy( hotbuf
, "<alt>" );
770 wxString last
= hotkey
.Right(1);
771 strcat( hotbuf
, last
.mb_str() );
774 case _T('c'): /* Ctrl */
776 case _T('s'): /* Strg, yeah man, I'm German */
779 strcpy( hotbuf
, "<control>" );
780 wxString last
= hotkey
.Right(1);
781 strcat( hotbuf
, last
.mb_str() );
784 case _T('F'): /* function keys */
786 strcpy( hotbuf
, hotkey
.mb_str() );
797 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
799 wxMenuItem
*mitem
= new wxMenuItem();
801 mitem
->SetText(item
);
802 mitem
->SetHelp(helpStr
);
803 mitem
->SetCheckable(checkable
);
805 #if (GTK_MINOR_VERSION > 0)
806 /* text has "_" instead of "&" after mitem->SetText() */
807 wxString
text( mitem
->GetText() );
809 /* local buffer in multibyte form */
812 strcat( buf
, text
.mb_str() );
814 GtkItemFactoryEntry entry
;
816 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
817 entry
.callback_action
= 0;
819 entry
.item_type
= "<CheckItem>";
821 entry
.item_type
= "<Item>";
824 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
826 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
828 /* in order to get the pointer to the item we need the item text _without_ underscores */
829 wxString s
= _T("<main>/");
830 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
832 if (*pc
== _T('_')) pc
++; /* skip it */
836 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
840 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
841 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
843 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
844 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
847 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
848 gtk_widget_show( menuItem
);
852 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
853 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
856 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
857 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
860 mitem
->SetMenuItem(menuItem
);
862 m_items
.Append( mitem
);
865 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
867 wxMenuItem
*mitem
= new wxMenuItem();
869 mitem
->SetText(item
);
870 mitem
->SetHelp(helpStr
);
872 #if (GTK_MINOR_VERSION > 0)
873 /* text has "_" instead of "&" after mitem->SetText() */
874 wxString
text( mitem
->GetText() );
876 /* local buffer in multibyte form */
879 strcat( buf
, text
.mb_str() );
881 GtkItemFactoryEntry entry
;
883 entry
.callback
= (GtkItemFactoryCallback
) 0;
884 entry
.callback_action
= 0;
885 entry
.item_type
= "<Branch>";
887 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
889 /* in order to get the pointer to the item we need the item text _without_ underscores */
890 wxString s
= _T("<main>/");
891 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
893 if (*pc
== _T('_')) pc
++; /* skip it */
897 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
901 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
903 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
904 gtk_widget_show( menuItem
);
908 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
909 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
912 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
913 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
916 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
918 mitem
->SetMenuItem(menuItem
);
919 mitem
->SetSubMenu(subMenu
);
921 m_items
.Append( mitem
);
924 void wxMenu::Append( wxMenuItem
*item
)
926 m_items
.Append( item
);
928 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
930 if (item
->IsSeparator())
931 menuItem
= gtk_menu_item_new();
932 else if (item
->IsSubMenu())
933 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
935 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
936 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
938 if (!item
->IsSeparator())
940 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
941 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
944 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
945 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
948 if (!item
->IsSubMenu())
950 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
951 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
956 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
957 gtk_widget_show( menuItem
);
958 item
->SetMenuItem(menuItem
);
961 int wxMenu::FindItem( const wxString itemString
) const
964 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
969 #if (GTK_MINOR_VERSION > 0)
976 wxNode
*node
= m_items
.First();
979 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
980 if (item
->GetText() == s
)
982 return item
->GetId();
990 void wxMenu::Enable( int id
, bool enable
)
992 wxMenuItem
*item
= FindItem(id
);
994 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
996 item
->Enable(enable
);
999 bool wxMenu::IsEnabled( int id
) const
1001 wxMenuItem
*item
= FindItem(id
);
1003 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
1005 return item
->IsEnabled();
1008 void wxMenu::Check( int id
, bool enable
)
1010 wxMenuItem
*item
= FindItem(id
);
1012 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
1014 item
->Check(enable
);
1017 bool wxMenu::IsChecked( int id
) const
1019 wxMenuItem
*item
= FindItem(id
);
1021 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
1023 return item
->IsChecked();
1026 void wxMenu::SetLabel( int id
, const wxString
&label
)
1028 wxMenuItem
*item
= FindItem(id
);
1030 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
1032 item
->SetText(label
);
1035 wxString
wxMenu::GetLabel( int id
) const
1037 wxMenuItem
*item
= FindItem(id
);
1039 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1041 return item
->GetText();
1044 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1046 wxMenuItem
*item
= FindItem(id
);
1048 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1050 item
->SetHelp( helpString
);
1053 wxString
wxMenu::GetHelpString( int id
) const
1055 wxMenuItem
*item
= FindItem(id
);
1057 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1059 return item
->GetHelp();
1062 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1064 wxNode
*node
= m_items
.First();
1067 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1068 if (item
->GetMenuItem() == menuItem
)
1069 return item
->GetId();
1070 node
= node
->Next();
1076 wxMenuItem
*wxMenu::FindItem(int id
) const
1078 wxNode
*node
= m_items
.First();
1081 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1082 if (item
->GetId() == id
)
1086 node
= node
->Next();
1089 /* Not finding anything here can be correct
1090 * when search the entire menu system for
1091 * an entry -> no error message. */
1093 return (wxMenuItem
*) NULL
;
1096 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1098 m_invokingWindow
= win
;
1101 wxWindow
*wxMenu::GetInvokingWindow()
1103 return m_invokingWindow
;
1106 // Update a menu and all submenus recursively. source is the object that has
1107 // the update event handlers defined for it. If NULL, the menu or associated
1108 // window will be used.
1109 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1111 if (!source
&& GetInvokingWindow())
1112 source
= GetInvokingWindow()->GetEventHandler();
1114 source
= GetEventHandler();
1118 wxNode
* node
= GetItems().First();
1121 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1122 if ( !item
->IsSeparator() )
1124 wxWindowID id
= item
->GetId();
1125 wxUpdateUIEvent
event(id
);
1126 event
.SetEventObject( source
);
1128 if (source
->ProcessEvent(event
))
1130 if (event
.GetSetText())
1131 SetLabel(id
, event
.GetText());
1132 if (event
.GetSetChecked())
1133 Check(id
, event
.GetChecked());
1134 if (event
.GetSetEnabled())
1135 Enable(id
, event
.GetEnabled());
1138 if (item
->GetSubMenu())
1139 item
->GetSubMenu()->UpdateUI(source
);
1141 node
= node
->Next();