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 else if ( *pc
== _T('_') ) // escape underscores
602 else if (*pc
== _T('/')) /* we have to filter out slashes ... */
604 m_text
<< _T('\\'); /* ... and replace them with back slashes */
611 /* only GTK 1.2 knows about hot keys */
613 #if (GTK_MINOR_VERSION > 0)
623 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
624 gtk_label_set( label
, m_text
.mb_str());
628 void wxMenuItem::Check( bool check
)
630 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
632 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
634 if (check
== m_isChecked
) return;
637 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
640 void wxMenuItem::Enable( bool enable
)
642 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
644 gtk_widget_set_sensitive( m_menuItem
, enable
);
645 m_isEnabled
= enable
;
648 bool wxMenuItem::IsChecked() const
650 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
652 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
654 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
659 //-----------------------------------------------------------------------------
661 //-----------------------------------------------------------------------------
663 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
666 wxMenu::Init( const wxString
& title
,
668 const wxFunction func
672 m_items
.DeleteContents( TRUE
);
673 m_invokingWindow
= (wxWindow
*) NULL
;
676 #if (GTK_MINOR_VERSION > 0)
677 m_accel
= gtk_accel_group_new();
678 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
679 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
681 m_menu
= gtk_menu_new(); // Do not show!
686 m_eventHandler
= this;
687 m_clientData
= (void*) NULL
;
689 if (m_title
.IsNull()) m_title
= _T("");
690 if (m_title
!= _T(""))
696 m_owner
= (GtkWidget
*) NULL
;
698 #if (GTK_MINOR_VERSION > 0)
699 /* Tearoffs are entries, just like separators. So if we want this
700 menu to be a tear-off one, we just append a tearoff entry
702 if(m_style
& wxMENU_TEAROFF
)
704 GtkItemFactoryEntry entry
;
705 entry
.path
= "/tearoff";
706 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
707 entry
.callback_action
= 0;
708 entry
.item_type
= "<Tearoff>";
709 entry
.accelerator
= (gchar
*) NULL
;
710 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
711 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
718 /* how do we delete an item-factory ? */
719 gtk_widget_destroy( m_menu
);
723 void wxMenu::SetTitle( const wxString
& title
)
725 // TODO Waiting for something better
729 const wxString
wxMenu::GetTitle() const
734 void wxMenu::AppendSeparator()
736 wxMenuItem
*mitem
= new wxMenuItem();
737 mitem
->SetId(ID_SEPARATOR
);
739 #if (GTK_MINOR_VERSION > 0)
740 GtkItemFactoryEntry entry
;
742 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
743 entry
.callback_action
= 0;
744 entry
.item_type
= "<Separator>";
745 entry
.accelerator
= (gchar
*) NULL
;
747 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
749 /* this will be wrong for more than one separator. do we care? */
750 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
752 GtkWidget
*menuItem
= gtk_menu_item_new();
753 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
754 gtk_widget_show( menuItem
);
757 mitem
->SetMenuItem(menuItem
);
758 m_items
.Append( mitem
);
761 #if (GTK_MINOR_VERSION > 0)
762 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
764 if (hotkey
.IsEmpty()) return (char*) NULL
;
768 case _T('a'): /* Alt */
770 case _T('m'): /* Meta */
773 strcpy( hotbuf
, "<alt>" );
774 wxString last
= hotkey
.Right(1);
775 strcat( hotbuf
, last
.mb_str() );
778 case _T('c'): /* Ctrl */
780 case _T('s'): /* Strg, yeah man, I'm German */
783 strcpy( hotbuf
, "<control>" );
784 wxString last
= hotkey
.Right(1);
785 strcat( hotbuf
, last
.mb_str() );
788 case _T('F'): /* function keys */
790 strcpy( hotbuf
, hotkey
.mb_str() );
801 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
803 wxMenuItem
*mitem
= new wxMenuItem();
805 mitem
->SetText(item
);
806 mitem
->SetHelp(helpStr
);
807 mitem
->SetCheckable(checkable
);
809 #if (GTK_MINOR_VERSION > 0)
810 /* text has "_" instead of "&" after mitem->SetText() */
811 wxString
text( mitem
->GetText() );
813 /* local buffer in multibyte form */
816 strcat( buf
, text
.mb_str() );
818 GtkItemFactoryEntry entry
;
820 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
821 entry
.callback_action
= 0;
823 entry
.item_type
= "<CheckItem>";
825 entry
.item_type
= "<Item>";
828 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
830 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
832 /* in order to get the pointer to the item we need the item text _without_ underscores */
833 wxString s
= _T("<main>/");
834 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
836 while (*pc
== _T('_')) pc
++; /* skip it */
840 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
844 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
845 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
847 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
848 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
851 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
852 gtk_widget_show( menuItem
);
856 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
857 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
860 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
861 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
864 mitem
->SetMenuItem(menuItem
);
866 m_items
.Append( mitem
);
869 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
871 wxMenuItem
*mitem
= new wxMenuItem();
873 mitem
->SetText(item
);
874 mitem
->SetHelp(helpStr
);
876 #if (GTK_MINOR_VERSION > 0)
877 /* text has "_" instead of "&" after mitem->SetText() */
878 wxString
text( mitem
->GetText() );
880 /* local buffer in multibyte form */
883 strcat( buf
, text
.mb_str() );
885 GtkItemFactoryEntry entry
;
887 entry
.callback
= (GtkItemFactoryCallback
) 0;
888 entry
.callback_action
= 0;
889 entry
.item_type
= "<Branch>";
891 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
893 /* in order to get the pointer to the item we need the item text _without_ underscores */
894 wxString s
= _T("<main>/");
895 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
897 if (*pc
== _T('_')) pc
++; /* skip it */
901 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
905 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
907 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
908 gtk_widget_show( menuItem
);
912 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
913 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
916 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
917 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
920 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
922 mitem
->SetMenuItem(menuItem
);
923 mitem
->SetSubMenu(subMenu
);
925 m_items
.Append( mitem
);
928 void wxMenu::Append( wxMenuItem
*item
)
930 m_items
.Append( item
);
932 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
934 if (item
->IsSeparator())
935 menuItem
= gtk_menu_item_new();
936 else if (item
->IsSubMenu())
937 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
939 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
940 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
942 if (!item
->IsSeparator())
944 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
945 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
948 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
949 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
952 if (!item
->IsSubMenu())
954 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
955 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
960 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
961 gtk_widget_show( menuItem
);
962 item
->SetMenuItem(menuItem
);
965 int wxMenu::FindItem( const wxString itemString
) const
968 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
973 #if (GTK_MINOR_VERSION > 0)
980 wxNode
*node
= m_items
.First();
983 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
984 if (item
->GetText() == s
)
986 return item
->GetId();
994 void wxMenu::Enable( int id
, bool enable
)
996 wxMenuItem
*item
= FindItem(id
);
998 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
1000 item
->Enable(enable
);
1003 bool wxMenu::IsEnabled( int id
) const
1005 wxMenuItem
*item
= FindItem(id
);
1007 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
1009 return item
->IsEnabled();
1012 void wxMenu::Check( int id
, bool enable
)
1014 wxMenuItem
*item
= FindItem(id
);
1016 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
1018 item
->Check(enable
);
1021 bool wxMenu::IsChecked( int id
) const
1023 wxMenuItem
*item
= FindItem(id
);
1025 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
1027 return item
->IsChecked();
1030 void wxMenu::SetLabel( int id
, const wxString
&label
)
1032 wxMenuItem
*item
= FindItem(id
);
1034 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
1036 item
->SetText(label
);
1039 wxString
wxMenu::GetLabel( int id
) const
1041 wxMenuItem
*item
= FindItem(id
);
1043 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1045 return item
->GetText();
1048 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1050 wxMenuItem
*item
= FindItem(id
);
1052 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1054 item
->SetHelp( helpString
);
1057 wxString
wxMenu::GetHelpString( int id
) const
1059 wxMenuItem
*item
= FindItem(id
);
1061 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1063 return item
->GetHelp();
1066 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1068 wxNode
*node
= m_items
.First();
1071 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1072 if (item
->GetMenuItem() == menuItem
)
1073 return item
->GetId();
1074 node
= node
->Next();
1080 wxMenuItem
*wxMenu::FindItem(int id
) const
1082 wxNode
*node
= m_items
.First();
1085 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1086 if (item
->GetId() == id
)
1090 node
= node
->Next();
1093 /* Not finding anything here can be correct
1094 * when search the entire menu system for
1095 * an entry -> no error message. */
1097 return (wxMenuItem
*) NULL
;
1100 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1102 m_invokingWindow
= win
;
1105 wxWindow
*wxMenu::GetInvokingWindow()
1107 return m_invokingWindow
;
1110 // Update a menu and all submenus recursively. source is the object that has
1111 // the update event handlers defined for it. If NULL, the menu or associated
1112 // window will be used.
1113 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1115 if (!source
&& GetInvokingWindow())
1116 source
= GetInvokingWindow()->GetEventHandler();
1118 source
= GetEventHandler();
1122 wxNode
* node
= GetItems().First();
1125 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1126 if ( !item
->IsSeparator() )
1128 wxWindowID id
= item
->GetId();
1129 wxUpdateUIEvent
event(id
);
1130 event
.SetEventObject( source
);
1132 if (source
->ProcessEvent(event
))
1134 if (event
.GetSetText())
1135 SetLabel(id
, event
.GetText());
1136 if (event
.GetSetChecked())
1137 Check(id
, event
.GetChecked());
1138 if (event
.GetSetEnabled())
1139 Enable(id
, event
.GetEnabled());
1142 if (item
->GetSubMenu())
1143 item
->GetSubMenu()->UpdateUI(source
);
1145 node
= node
->Next();