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
);
189 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
191 for ( pc
= title
; *pc
!= _T('\0'); pc
++ )
195 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
207 /* this doesn't have much effect right now */
208 menu
->SetTitle( str
);
210 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
211 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
213 /* local buffer in multibyte form */
215 buf
<< _T('/') << str
.c_str();
217 char *cbuf
= new char[buf
.Length()];
218 strcpy(cbuf
, buf
.mbc_str());
220 GtkItemFactoryEntry entry
;
221 entry
.path
= (gchar
*)cbuf
; // const_cast
222 entry
.accelerator
= (gchar
*) NULL
;
223 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
224 entry
.callback_action
= 0;
225 entry
.item_type
= "<Branch>";
227 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
228 /* in order to get the pointer to the item we need the item text _without_ underscores */
229 wxString tmp
= _T("<main>/");
230 for ( pc
= str
; *pc
!= _T('\0'); pc
++ )
232 if (*pc
== _T('_')) pc
++; /* skip it */
235 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
236 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
240 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
241 gtk_widget_show( menu
->m_owner
);
242 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
244 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
249 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
251 if (menu
->GetTitle() == menuString
)
253 int res
= menu
->FindItem( itemString
);
254 if (res
!= wxNOT_FOUND
)
258 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
261 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
262 if (item
->IsSubMenu())
263 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
271 wxMenuItem
*wxMenuBar::FindItemForId(int itemId
, wxMenu
**menuForItem
) const
275 // TODO return the pointer to the menu
280 return FindItem(itemId
);
283 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
285 wxNode
*node
= m_menus
.First();
288 wxMenu
*menu
= (wxMenu
*)node
->Data();
289 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
290 if (res
!= -1) return res
;
296 // Find a wxMenuItem using its id. Recurses down into sub-menus
297 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
299 wxMenuItem
* result
= menu
->FindItem(id
);
301 wxNode
*node
= ((wxMenu
*)menu
)->GetItems().First(); // const_cast
302 while ( node
&& result
== NULL
)
304 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
305 if (item
->IsSubMenu())
307 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
315 wxMenuItem
* wxMenuBar::FindItem( int id
) const
317 wxMenuItem
* result
= 0;
318 wxNode
*node
= m_menus
.First();
319 while (node
&& result
== 0)
321 wxMenu
*menu
= (wxMenu
*)node
->Data();
322 result
= FindMenuItemByIdRecursive( menu
, id
);
329 void wxMenuBar::Check( int id
, bool check
)
331 wxMenuItem
* item
= FindMenuItemById( id
);
333 wxCHECK_RET( item
, _T("wxMenuBar::Check: no such item") );
338 bool wxMenuBar::IsChecked( int id
) const
340 wxMenuItem
* item
= FindMenuItemById( id
);
342 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked: no such item") );
344 return item
->IsChecked();
347 void wxMenuBar::Enable( int id
, bool enable
)
349 wxMenuItem
* item
= FindMenuItemById( id
);
351 wxCHECK_RET( item
, _T("wxMenuBar::Enable: no such item") );
353 item
->Enable(enable
);
356 bool wxMenuBar::IsEnabled( int id
) const
358 wxMenuItem
* item
= FindMenuItemById( id
);
360 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled: no such item") );
362 return item
->IsEnabled();
365 wxString
wxMenuBar::GetLabel( int id
) const
367 wxMenuItem
* item
= FindMenuItemById( id
);
369 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel: no such item") );
371 return item
->GetText();
374 void wxMenuBar::SetLabel( int id
, const wxString
&label
)
376 wxMenuItem
* item
= FindMenuItemById( id
);
378 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel: no such item") );
380 item
->SetText( label
);
383 void wxMenuBar::EnableTop( int pos
, bool flag
)
385 wxNode
*node
= m_menus
.Nth( pos
);
387 wxCHECK_RET( node
, _T("menu not found") );
389 wxMenu
* menu
= (wxMenu
*)node
->Data();
392 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
395 wxString
wxMenuBar::GetLabelTop( int pos
) const
397 wxNode
*node
= m_menus
.Nth( pos
);
399 wxCHECK_MSG( node
, _T("invalid"), _T("menu not found") );
401 wxMenu
* menu
= (wxMenu
*)node
->Data();
403 return menu
->GetTitle();
406 void wxMenuBar::SetLabelTop( int pos
, const wxString
& label
)
408 wxNode
*node
= m_menus
.Nth( pos
);
410 wxCHECK_RET( node
, _T("menu not found") );
412 wxMenu
* menu
= (wxMenu
*)node
->Data();
414 menu
->SetTitle( label
);
417 void wxMenuBar::SetHelpString( int id
, const wxString
& helpString
)
419 wxMenuItem
* item
= FindMenuItemById( id
);
421 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString: no such item") );
423 item
->SetHelp( helpString
);
426 wxString
wxMenuBar::GetHelpString( int id
) const
428 wxMenuItem
* item
= FindMenuItemById( id
);
430 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
432 return item
->GetHelp();
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
439 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
441 if (g_isIdle
) wxapp_install_idle_handler();
443 int id
= menu
->FindMenuIdByMenuItem(widget
);
445 /* should find it for normal (not popup) menu */
446 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
448 if (!menu
->IsEnabled(id
))
451 wxMenuItem
* item
= menu
->FindItem( id
);
452 wxCHECK_RET( item
, _T("error in menu item callback") );
454 if (item
->IsCheckable())
456 if (item
->GetCheckedFlag() == item
->IsChecked())
458 /* the menu item has been checked by calling wxMenuItem->Check() */
463 /* the user pressed on the menu item -> report */
464 item
->SetCheckedFlag(item
->IsChecked()); /* make consistent again */
468 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
469 event
.SetEventObject( menu
);
472 if (menu
->GetCallback())
474 (void) (*(menu
->GetCallback())) (*menu
, event
);
478 if (menu
->GetEventHandler()->ProcessEvent(event
))
481 wxWindow
*win
= menu
->GetInvokingWindow();
483 win
->GetEventHandler()->ProcessEvent( event
);
486 //-----------------------------------------------------------------------------
488 //-----------------------------------------------------------------------------
490 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
492 if (g_isIdle
) wxapp_install_idle_handler();
494 int id
= menu
->FindMenuIdByMenuItem(widget
);
496 wxASSERT( id
!= -1 ); // should find it!
498 if (!menu
->IsEnabled(id
))
501 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
502 event
.SetEventObject( menu
);
504 if (menu
->GetEventHandler()->ProcessEvent(event
))
507 wxWindow
*win
= menu
->GetInvokingWindow();
508 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
511 //-----------------------------------------------------------------------------
513 //-----------------------------------------------------------------------------
515 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
517 if (g_isIdle
) wxapp_install_idle_handler();
519 int id
= menu
->FindMenuIdByMenuItem(widget
);
521 wxASSERT( id
!= -1 ); // should find it!
523 if (!menu
->IsEnabled(id
))
526 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
527 event
.SetEventObject( menu
);
529 if (menu
->GetEventHandler()->ProcessEvent(event
))
532 wxWindow
*win
= menu
->GetInvokingWindow();
534 win
->GetEventHandler()->ProcessEvent( event
);
537 //-----------------------------------------------------------------------------
539 //-----------------------------------------------------------------------------
541 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
,wxObject
)
543 wxMenuItem::wxMenuItem()
546 m_isCheckMenu
= FALSE
;
549 m_subMenu
= (wxMenu
*) NULL
;
550 m_menuItem
= (GtkWidget
*) NULL
;
553 // it's valid for this function to be called even if m_menuItem == NULL
554 void wxMenuItem::SetName( const wxString
& str
)
556 /* '\t' is the deliminator indicating a hot key */
558 const wxChar
*pc
= str
;
559 for (; (*pc
!= _T('\0')) && (*pc
!= _T('\t')); pc
++ )
563 #if (GTK_MINOR_VERSION > 0)
566 if (*pc
== _T('/')) /* we have to filter out slashes ... */
568 m_text
<< _T('\\'); /* ... and replace them with back slashes */
575 /* only GTK 1.2 knows about hot keys */
577 #if (GTK_MINOR_VERSION > 0)
587 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
588 gtk_label_set( label
, m_text
.mb_str());
592 void wxMenuItem::Check( bool check
)
594 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
596 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
598 if (check
== m_isChecked
) return;
601 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
604 void wxMenuItem::Enable( bool enable
)
606 wxCHECK_RET( m_menuItem
, _T("invalid menu item") );
608 gtk_widget_set_sensitive( m_menuItem
, enable
);
609 m_isEnabled
= enable
;
612 bool wxMenuItem::IsChecked() const
614 wxCHECK_MSG( m_menuItem
, FALSE
, _T("invalid menu item") );
616 wxCHECK( IsCheckable(), FALSE
); // can't get state of uncheckable item!
618 bool bIsChecked
= ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
623 //-----------------------------------------------------------------------------
625 //-----------------------------------------------------------------------------
627 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
630 wxMenu::Init( const wxString
& title
,
632 #ifdef WXWIN_COMPATIBILITY
633 , const wxFunction func
638 m_items
.DeleteContents( TRUE
);
639 m_invokingWindow
= (wxWindow
*) NULL
;
642 #if (GTK_MINOR_VERSION > 0)
643 m_accel
= gtk_accel_group_new();
644 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
645 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
647 m_menu
= gtk_menu_new(); // Do not show!
650 #ifdef WXWIN_COMPATIBILITY
654 m_eventHandler
= this;
655 m_clientData
= (void*) NULL
;
657 if (m_title
.IsNull()) m_title
= _T("");
658 if (m_title
!= _T(""))
664 m_owner
= (GtkWidget
*) NULL
;
666 #if (GTK_MINOR_VERSION > 0)
667 /* Tearoffs are entries, just like separators. So if we want this
668 menu to be a tear-off one, we just append a tearoff entry
670 if(m_style
& wxMENU_TEAROFF
)
672 GtkItemFactoryEntry entry
;
673 entry
.path
= "/tearoff";
674 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
675 entry
.callback_action
= 0;
676 entry
.item_type
= "<Tearoff>";
677 entry
.accelerator
= (gchar
*) NULL
;
678 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
679 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
686 /* how do we delete an item-factory ? */
687 gtk_widget_destroy( m_menu
);
691 void wxMenu::SetTitle( const wxString
& title
)
693 // TODO Waiting for something better
697 const wxString
wxMenu::GetTitle() const
702 void wxMenu::AppendSeparator()
704 wxMenuItem
*mitem
= new wxMenuItem();
705 mitem
->SetId(ID_SEPARATOR
);
707 #if (GTK_MINOR_VERSION > 0)
708 GtkItemFactoryEntry entry
;
710 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
711 entry
.callback_action
= 0;
712 entry
.item_type
= "<Separator>";
713 entry
.accelerator
= (gchar
*) NULL
;
715 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
717 /* this will be wrong for more than one separator. do we care? */
718 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
720 GtkWidget
*menuItem
= gtk_menu_item_new();
721 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
722 gtk_widget_show( menuItem
);
725 mitem
->SetMenuItem(menuItem
);
726 m_items
.Append( mitem
);
729 static char* GetHotKey( const wxString
&hotkey
, char *hotbuf
)
731 if (hotkey
.IsEmpty()) return (char*) NULL
;
735 case _T('a'): /* Alt */
737 case _T('m'): /* Meta */
740 strcpy( hotbuf
, "<alt>" );
741 wxString last
= hotkey
.Right(1);
742 strcat( hotbuf
, last
.mb_str() );
745 case _T('c'): /* Ctrl */
747 case _T('s'): /* Strg, yeah man, I'm German */
750 strcpy( hotbuf
, "<control>" );
751 wxString last
= hotkey
.Right(1);
752 strcat( hotbuf
, last
.mb_str() );
755 case _T('F'): /* function keys */
757 strcpy( hotbuf
, hotkey
.mb_str() );
767 void wxMenu::Append( int id
, const wxString
&item
, const wxString
&helpStr
, bool checkable
)
769 wxMenuItem
*mitem
= new wxMenuItem();
771 mitem
->SetText(item
);
772 mitem
->SetHelp(helpStr
);
773 mitem
->SetCheckable(checkable
);
775 #if (GTK_MINOR_VERSION > 0)
776 /* text has "_" instead of "&" after mitem->SetText() */
777 wxString
text( mitem
->GetText() );
779 /* local buffer in multibyte form */
782 strcat( buf
, text
.mb_str() );
784 GtkItemFactoryEntry entry
;
786 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
787 entry
.callback_action
= 0;
789 entry
.item_type
= "<CheckItem>";
791 entry
.item_type
= "<Item>";
794 entry
.accelerator
= GetHotKey( mitem
->GetHotKey(), hotbuf
);
796 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
798 /* in order to get the pointer to the item we need the item text _without_ underscores */
799 wxString s
= _T("<main>/");
800 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
802 if (*pc
== _T('_')) pc
++; /* skip it */
806 GtkWidget
*menuItem
= gtk_item_factory_get_widget( m_factory
, s
.mb_str() );
810 GtkWidget
*menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
811 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
813 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
814 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
817 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
818 gtk_widget_show( menuItem
);
822 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
823 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
826 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
827 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
830 mitem
->SetMenuItem(menuItem
);
832 m_items
.Append( mitem
);
835 void wxMenu::Append( int id
, const wxString
&item
, wxMenu
*subMenu
, const wxString
&helpStr
)
837 wxMenuItem
*mitem
= new wxMenuItem();
839 mitem
->SetText(item
);
840 mitem
->SetHelp(helpStr
);
842 #if (GTK_MINOR_VERSION > 0)
843 /* text has "_" instead of "&" after mitem->SetText() */
844 wxString
text( mitem
->GetText() );
846 /* local buffer in multibyte form */
849 strcat( buf
, text
.mb_str() );
851 GtkItemFactoryEntry entry
;
853 entry
.callback
= (GtkItemFactoryCallback
) 0;
854 entry
.callback_action
= 0;
855 entry
.item_type
= "<Branch>";
857 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
859 /* in order to get the pointer to the item we need the item text _without_ underscores */
860 wxString s
= _T("<main>/");
861 for ( const wxChar
*pc
= text
; *pc
!= _T('\0'); pc
++ )
863 if (*pc
== _T('_')) pc
++; /* skip it */
867 GtkWidget
*menuItem
= gtk_item_factory_get_item( m_factory
, s
.mb_str() );
871 GtkWidget
*menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
873 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
874 gtk_widget_show( menuItem
);
878 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
879 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
882 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
883 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
886 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), subMenu
->m_menu
);
888 mitem
->SetMenuItem(menuItem
);
889 mitem
->SetSubMenu(subMenu
);
891 m_items
.Append( mitem
);
894 void wxMenu::Append( wxMenuItem
*item
)
896 m_items
.Append( item
);
898 GtkWidget
*menuItem
= (GtkWidget
*) NULL
;
900 if (item
->IsSeparator())
901 menuItem
= gtk_menu_item_new();
902 else if (item
->IsSubMenu())
903 menuItem
= gtk_menu_item_new_with_label(item
->GetText().mbc_str());
905 menuItem
= item
->IsCheckable() ? gtk_check_menu_item_new_with_label(item
->GetText().mbc_str())
906 : gtk_menu_item_new_with_label(item
->GetText().mbc_str());
908 if (!item
->IsSeparator())
910 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
911 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
914 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
915 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
918 if (!item
->IsSubMenu())
920 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
921 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
926 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
927 gtk_widget_show( menuItem
);
928 item
->SetMenuItem(menuItem
);
931 int wxMenu::FindItem( const wxString itemString
) const
934 for ( const wxChar
*pc
= itemString
; *pc
!= _T('\0'); pc
++ )
939 #if (GTK_MINOR_VERSION > 0)
946 wxNode
*node
= m_items
.First();
949 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
950 if (item
->GetText() == s
)
952 return item
->GetId();
960 void wxMenu::Enable( int id
, bool enable
)
962 wxMenuItem
*item
= FindItem(id
);
964 wxCHECK_RET( item
, _T("wxMenu::Enable: no such item") );
966 item
->Enable(enable
);
969 bool wxMenu::IsEnabled( int id
) const
971 wxMenuItem
*item
= FindItem(id
);
973 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsEnabled: no such item") );
975 return item
->IsEnabled();
978 void wxMenu::Check( int id
, bool enable
)
980 wxMenuItem
*item
= FindItem(id
);
982 wxCHECK_RET( item
, _T("wxMenu::Check: no such item") );
987 bool wxMenu::IsChecked( int id
) const
989 wxMenuItem
*item
= FindItem(id
);
991 wxCHECK_MSG( item
, FALSE
, _T("wxMenu::IsChecked: no such item") );
993 return item
->IsChecked();
996 void wxMenu::SetLabel( int id
, const wxString
&label
)
998 wxMenuItem
*item
= FindItem(id
);
1000 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
1002 item
->SetText(label
);
1005 wxString
wxMenu::GetLabel( int id
) const
1007 wxMenuItem
*item
= FindItem(id
);
1009 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetLabel: no such item") );
1011 return item
->GetText();
1014 void wxMenu::SetHelpString( int id
, const wxString
& helpString
)
1016 wxMenuItem
*item
= FindItem(id
);
1018 wxCHECK_RET( item
, _T("wxMenu::SetHelpString: no such item") );
1020 item
->SetHelp( helpString
);
1023 wxString
wxMenu::GetHelpString( int id
) const
1025 wxMenuItem
*item
= FindItem(id
);
1027 wxCHECK_MSG( item
, _T(""), _T("wxMenu::GetHelpString: no such item") );
1029 return item
->GetHelp();
1032 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1034 wxNode
*node
= m_items
.First();
1037 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1038 if (item
->GetMenuItem() == menuItem
)
1039 return item
->GetId();
1040 node
= node
->Next();
1046 wxMenuItem
*wxMenu::FindItem(int id
) const
1048 wxNode
*node
= m_items
.First();
1051 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
1052 if (item
->GetId() == id
)
1056 node
= node
->Next();
1059 /* Not finding anything here can be correct
1060 * when search the entire menu system for
1061 * an entry -> no error message. */
1063 return (wxMenuItem
*) NULL
;
1066 void wxMenu::SetInvokingWindow( wxWindow
*win
)
1068 m_invokingWindow
= win
;
1071 wxWindow
*wxMenu::GetInvokingWindow()
1073 return m_invokingWindow
;
1076 // Update a menu and all submenus recursively. source is the object that has
1077 // the update event handlers defined for it. If NULL, the menu or associated
1078 // window will be used.
1079 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1081 if (!source
&& GetInvokingWindow())
1082 source
= GetInvokingWindow()->GetEventHandler();
1084 source
= GetEventHandler();
1088 wxNode
* node
= GetItems().First();
1091 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1092 if ( !item
->IsSeparator() )
1094 wxWindowID id
= item
->GetId();
1095 wxUpdateUIEvent
event(id
);
1096 event
.SetEventObject( source
);
1098 if (source
->ProcessEvent(event
))
1100 if (event
.GetSetText())
1101 SetLabel(id
, event
.GetText());
1102 if (event
.GetSetChecked())
1103 Check(id
, event
.GetChecked());
1104 if (event
.GetSetEnabled())
1105 Enable(id
, event
.GetEnabled());
1108 if (item
->GetSubMenu())
1109 item
->GetSubMenu()->UpdateUI(source
);
1111 node
= node
->Next();