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"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
35 static wxString
GetHotKey( const wxMenuItem
& item
);
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
44 wxMenuBar::wxMenuBar( long style
)
46 /* the parent window is known after wxFrame::SetMenu() */
49 m_invokingWindow
= (wxWindow
*) NULL
;
51 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
52 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
54 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
58 m_menus
.DeleteContents( TRUE
);
60 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
61 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
62 m_accel
= gtk_accel_group_new();
63 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
64 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
66 m_menubar
= gtk_menu_bar_new();
69 if (style
& wxMB_DOCKABLE
)
71 m_widget
= gtk_handle_box_new();
72 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
73 gtk_widget_show( GTK_WIDGET(m_menubar
) );
77 m_widget
= GTK_WIDGET(m_menubar
);
85 wxMenuBar::wxMenuBar()
87 /* the parent window is known after wxFrame::SetMenu() */
90 m_invokingWindow
= (wxWindow
*) NULL
;
92 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
93 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, wxDefaultValidator
, wxT("menubar") ))
95 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
99 m_menus
.DeleteContents( TRUE
);
101 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
102 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
103 m_accel
= gtk_accel_group_new();
104 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
105 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
107 m_menubar
= gtk_menu_bar_new();
110 m_widget
= GTK_WIDGET(m_menubar
);
117 wxMenuBar::~wxMenuBar()
119 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
122 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
124 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
126 #if (GTK_MINOR_VERSION > 0)
127 wxWindow
*top_frame
= win
;
128 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
129 top_frame
= top_frame
->GetParent();
131 /* support for native hot keys */
132 gtk_accel_group_detach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
135 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
138 wxMenuItem
*menuitem
= node
->GetData();
139 if (menuitem
->IsSubMenu())
140 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
141 node
= node
->GetNext();
145 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
147 menu
->SetInvokingWindow( win
);
149 #if (GTK_MINOR_VERSION > 0)
150 wxWindow
*top_frame
= win
;
151 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
152 top_frame
= top_frame
->GetParent();
154 /* support for native hot keys */
155 gtk_accel_group_attach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
158 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
161 wxMenuItem
*menuitem
= node
->GetData();
162 if (menuitem
->IsSubMenu())
163 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
164 node
= node
->GetNext();
168 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
170 m_invokingWindow
= win
;
171 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
172 wxWindow
*top_frame
= win
;
173 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
174 top_frame
= top_frame
->GetParent();
176 /* support for native key accelerators indicated by underscroes */
177 gtk_accel_group_attach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
180 wxMenuList::Node
*node
= m_menus
.GetFirst();
183 wxMenu
*menu
= node
->GetData();
184 wxMenubarSetInvokingWindow( menu
, win
);
185 node
= node
->GetNext();
189 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
191 m_invokingWindow
= (wxWindow
*) NULL
;
192 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
193 wxWindow
*top_frame
= win
;
194 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
195 top_frame
= top_frame
->GetParent();
197 /* support for native key accelerators indicated by underscroes */
198 gtk_accel_group_detach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
201 wxMenuList::Node
*node
= m_menus
.GetFirst();
204 wxMenu
*menu
= node
->GetData();
205 wxMenubarUnsetInvokingWindow( menu
, win
);
206 node
= node
->GetNext();
210 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
212 if ( !wxMenuBarBase::Append( menu
, title
) )
215 return GtkAppend(menu
, title
);
218 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
)
222 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
224 for ( pc
= title
; *pc
!= wxT('\0'); pc
++ )
228 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
231 else if (*pc
== wxT('/'))
239 if ( *pc
== wxT('_') )
241 // underscores must be doubled to prevent them from being
242 // interpreted as accelerator character prefix by GTK
251 /* this doesn't have much effect right now */
252 menu
->SetTitle( str
);
254 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
255 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
257 /* local buffer in multibyte form */
259 buf
<< wxT('/') << str
.c_str();
261 char *cbuf
= new char[buf
.Length()+1];
262 strcpy(cbuf
, buf
.mbc_str());
264 GtkItemFactoryEntry entry
;
265 entry
.path
= (gchar
*)cbuf
; // const_cast
266 entry
.accelerator
= (gchar
*) NULL
;
267 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
268 entry
.callback_action
= 0;
269 entry
.item_type
= "<Branch>";
271 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
272 /* in order to get the pointer to the item we need the item text _without_ underscores */
273 wxString tmp
= wxT("<main>/");
274 for ( pc
= str
; *pc
!= wxT('\0'); pc
++ )
276 // contrary to the common sense, we must throw out _all_ underscores,
277 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
278 // might naively think). IMHO it's a bug in GTK+ (VZ)
279 while (*pc
== wxT('_'))
283 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
284 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
288 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
289 gtk_widget_show( menu
->m_owner
);
290 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
292 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
296 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
297 // adding menu later on.
298 if (m_invokingWindow
)
299 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
304 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
306 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
310 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
311 // of version 1.2.6), so we first append the item and then change its
313 if ( !GtkAppend(menu
, title
) )
316 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
317 gpointer data
= g_list_last(menu_shell
->children
)->data
;
318 menu_shell
->children
= g_list_remove(menu_shell
->children
, data
);
319 menu_shell
->children
= g_list_insert(menu_shell
->children
, data
, pos
);
323 // this should be easy to do with GTK 1.0 - can use standard functions for
324 // this and don't need any hacks like above, but as I don't have GTK 1.0
325 // any more I can't do it
326 wxFAIL_MSG( wxT("TODO") );
329 #endif // GTK 1.2/1.0
332 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
334 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
335 return (wxMenu
*) NULL
;
337 // remove the old item and insert a new one
338 wxMenu
*menuOld
= Remove(pos
);
339 if ( menuOld
&& !Insert(pos
, menu
, title
) )
341 return (wxMenu
*) NULL
;
344 // either Insert() succeeded or Remove() failed and menuOld is NULL
348 wxMenu
*wxMenuBar::Remove(size_t pos
)
350 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
352 return (wxMenu
*) NULL
;
355 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
357 printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
358 printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
361 // unparent calls unref() and that would delete the widget so we raise
362 // the ref count to 2 artificially before invoking unparent.
363 gtk_widget_ref( menu
->m_menu
);
364 gtk_widget_unparent( menu
->m_menu
);
366 gtk_widget_destroy( menu
->m_owner
);
369 printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
370 printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
376 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
378 if (menu
->GetTitle() == menuString
)
380 int res
= menu
->FindItem( itemString
);
381 if (res
!= wxNOT_FOUND
)
385 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
388 wxMenuItem
*item
= node
->GetData();
389 if (item
->IsSubMenu())
390 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
392 node
= node
->GetNext();
398 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
400 wxMenuList::Node
*node
= m_menus
.GetFirst();
403 wxMenu
*menu
= node
->GetData();
404 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
407 node
= node
->GetNext();
413 // Find a wxMenuItem using its id. Recurses down into sub-menus
414 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
416 wxMenuItem
* result
= menu
->FindChildItem(id
);
418 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
419 while ( node
&& result
== NULL
)
421 wxMenuItem
*item
= node
->GetData();
422 if (item
->IsSubMenu())
424 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
426 node
= node
->GetNext();
432 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
434 wxMenuItem
* result
= 0;
435 wxMenuList::Node
*node
= m_menus
.GetFirst();
436 while (node
&& result
== 0)
438 wxMenu
*menu
= node
->GetData();
439 result
= FindMenuItemByIdRecursive( menu
, id
);
440 node
= node
->GetNext();
445 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
451 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
453 wxMenuList::Node
*node
= m_menus
.Item( pos
);
455 wxCHECK_RET( node
, wxT("menu not found") );
457 wxMenu
* menu
= node
->GetData();
460 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
463 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
465 wxMenuList::Node
*node
= m_menus
.Item( pos
);
467 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
469 wxMenu
* menu
= node
->GetData();
471 return menu
->GetTitle();
474 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
476 wxMenuList::Node
*node
= m_menus
.Item( pos
);
478 wxCHECK_RET( node
, wxT("menu not found") );
480 wxMenu
* menu
= node
->GetData();
482 menu
->SetTitle( label
);
485 //-----------------------------------------------------------------------------
487 //-----------------------------------------------------------------------------
489 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
491 if (g_isIdle
) wxapp_install_idle_handler();
493 int id
= menu
->FindMenuIdByMenuItem(widget
);
495 /* should find it for normal (not popup) menu */
496 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
498 if (!menu
->IsEnabled(id
))
501 wxMenuItem
* item
= menu
->FindChildItem( id
);
502 wxCHECK_RET( item
, wxT("error in menu item callback") );
504 if (item
->IsCheckable())
506 bool isReallyChecked
= item
->IsChecked();
507 if ( item
->wxMenuItemBase::IsChecked() == isReallyChecked
)
509 /* the menu item has been checked by calling wxMenuItem->Check() */
514 /* the user pressed on the menu item -> report and make consistent
516 item
->wxMenuItemBase::Check(isReallyChecked
);
520 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
521 event
.SetEventObject( menu
);
524 #if wxUSE_MENU_CALLBACK
525 if (menu
->GetCallback())
527 (void) (*(menu
->GetCallback())) (*menu
, event
);
530 #endif // wxUSE_MENU_CALLBACK
532 if (menu
->GetEventHandler()->ProcessEvent(event
))
535 wxWindow
*win
= menu
->GetInvokingWindow();
537 win
->GetEventHandler()->ProcessEvent( event
);
540 //-----------------------------------------------------------------------------
542 //-----------------------------------------------------------------------------
544 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
546 if (g_isIdle
) wxapp_install_idle_handler();
548 int id
= menu
->FindMenuIdByMenuItem(widget
);
550 wxASSERT( id
!= -1 ); // should find it!
552 if (!menu
->IsEnabled(id
))
555 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
556 event
.SetEventObject( menu
);
558 if (menu
->GetEventHandler()->ProcessEvent(event
))
561 wxWindow
*win
= menu
->GetInvokingWindow();
562 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
565 //-----------------------------------------------------------------------------
567 //-----------------------------------------------------------------------------
569 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
571 if (g_isIdle
) wxapp_install_idle_handler();
573 int id
= menu
->FindMenuIdByMenuItem(widget
);
575 wxASSERT( id
!= -1 ); // should find it!
577 if (!menu
->IsEnabled(id
))
580 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
581 event
.SetEventObject( menu
);
583 if (menu
->GetEventHandler()->ProcessEvent(event
))
586 wxWindow
*win
= menu
->GetInvokingWindow();
588 win
->GetEventHandler()->ProcessEvent( event
);
591 //-----------------------------------------------------------------------------
593 //-----------------------------------------------------------------------------
595 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
597 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
599 const wxString
& name
,
600 const wxString
& help
,
604 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
607 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
609 const wxString
& text
,
610 const wxString
& help
,
615 m_isCheckable
= isCheckable
;
619 m_parentMenu
= parentMenu
;
622 m_menuItem
= (GtkWidget
*) NULL
;
627 wxMenuItem::~wxMenuItem()
629 // don't delete menu items, the menus take care of that
632 // return the menu item text without any menu accels
634 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
637 #if (GTK_MINOR_VERSION > 0)
638 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
640 if ( *pc
== wxT('_') )
642 // this is the escape character for GTK+ - skip it
650 #endif // GTK+ 1.2/1.0
655 void wxMenuItem::SetText( const wxString
& str
)
661 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
664 gtk_label_set( label
, m_text
.mb_str());
666 /* reparse key accel */
667 (void)gtk_label_parse_uline (GTK_LABEL(label
), m_text
.mb_str() );
668 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
672 // it's valid for this function to be called even if m_menuItem == NULL
673 void wxMenuItem::DoSetText( const wxString
& str
)
675 /* '\t' is the deliminator indicating a hot key */
677 const wxChar
*pc
= str
;
678 for (; (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')); pc
++ )
682 #if (GTK_MINOR_VERSION > 0)
685 else if ( *pc
== wxT('_') ) // escape underscores
689 else if (*pc
== wxT('/')) /* we have to filter out slashes ... */
691 m_text
<< wxT('\\'); /* ... and replace them with back slashes */
698 /* only GTK 1.2 knows about hot keys */
700 #if (GTK_MINOR_VERSION > 0)
711 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
716 return (wxAcceleratorEntry
*)NULL
;
719 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
721 label
<< wxT('\t') << GetHotKey();
723 return wxGetAccelFromString(label
);
726 #endif // wxUSE_ACCEL
728 void wxMenuItem::Check( bool check
)
730 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
732 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
734 if (check
== m_isChecked
)
737 wxMenuItemBase::Check( check
);
738 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
741 void wxMenuItem::Enable( bool enable
)
743 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
745 gtk_widget_set_sensitive( m_menuItem
, enable
);
746 wxMenuItemBase::Enable( enable
);
749 bool wxMenuItem::IsChecked() const
751 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
753 wxCHECK_MSG( IsCheckable(), FALSE
,
754 wxT("can't get state of uncheckable item!") );
756 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
759 wxString
wxMenuItem::GetFactoryPath() const
761 /* in order to get the pointer to the item we need the item text _without_
763 wxString
path( wxT("<main>/") );
769 //-----------------------------------------------------------------------------
771 //-----------------------------------------------------------------------------
773 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
777 #if (GTK_MINOR_VERSION > 0)
778 m_accel
= gtk_accel_group_new();
779 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
780 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
782 m_menu
= gtk_menu_new(); // Do not show!
785 m_owner
= (GtkWidget
*) NULL
;
787 #if (GTK_MINOR_VERSION > 0)
788 /* Tearoffs are entries, just like separators. So if we want this
789 menu to be a tear-off one, we just append a tearoff entry
791 if(m_style
& wxMENU_TEAROFF
)
793 GtkItemFactoryEntry entry
;
794 entry
.path
= "/tearoff";
795 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
796 entry
.callback_action
= 0;
797 entry
.item_type
= "<Tearoff>";
798 entry
.accelerator
= (gchar
*) NULL
;
799 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
800 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
804 // append the title as the very first entry if we have it
816 gtk_widget_destroy( m_menu
);
818 gtk_object_unref( GTK_OBJECT(m_factory
) );
821 bool wxMenu::GtkAppend(wxMenuItem
*mitem
)
825 if ( mitem
->IsSeparator() )
827 #if (GTK_MINOR_VERSION > 0)
828 GtkItemFactoryEntry entry
;
830 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
831 entry
.callback_action
= 0;
832 entry
.item_type
= "<Separator>";
833 entry
.accelerator
= (gchar
*) NULL
;
835 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
837 /* this will be wrong for more than one separator. do we care? */
838 menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
840 menuItem
= gtk_menu_item_new();
841 #endif // GTK 1.2/1.0
843 else if ( mitem
->IsSubMenu() )
845 #if (GTK_MINOR_VERSION > 0)
846 /* text has "_" instead of "&" after mitem->SetText() */
847 wxString
text( mitem
->GetText() );
849 /* local buffer in multibyte form */
852 strcat( buf
, text
.mb_str() );
854 GtkItemFactoryEntry entry
;
856 entry
.callback
= (GtkItemFactoryCallback
) 0;
857 entry
.callback_action
= 0;
858 entry
.item_type
= "<Branch>";
859 entry
.accelerator
= (gchar
*) NULL
;
861 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
863 wxString
path( mitem
->GetFactoryPath() );
864 menuItem
= gtk_item_factory_get_item( m_factory
, path
.mb_str() );
866 menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
867 #endif // GTK 1.2/1.0
869 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
871 else // a normal item
873 #if (GTK_MINOR_VERSION > 0)
874 /* text has "_" instead of "&" after mitem->SetText() */
875 wxString
text( mitem
->GetText() );
877 /* local buffer in multibyte form */
880 strcat( buf
, text
.mb_str() );
882 GtkItemFactoryEntry entry
;
884 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
885 entry
.callback_action
= 0;
886 if ( mitem
->IsCheckable() )
887 entry
.item_type
= "<CheckItem>";
889 entry
.item_type
= "<Item>";
890 entry
.accelerator
= (gchar
*) NULL
;
893 // due to an apparent bug in GTK+, we have to use a static buffer here -
894 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
896 static char s_accel
[32]; // must be big enough for <control><alt><shift>F12
897 strncpy(s_accel
, GetHotKey(*mitem
).mb_str(), WXSIZEOF(s_accel
));
898 entry
.accelerator
= s_accel
;
899 #else // !wxUSE_ACCEL
900 entry
.accelerator
= (char*) NULL
;
901 #endif // wxUSE_ACCEL/!wxUSE_ACCEL
903 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
905 wxString
path( mitem
->GetFactoryPath() );
906 menuItem
= gtk_item_factory_get_widget( m_factory
, path
.mb_str() );
908 menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
909 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
911 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
912 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
914 #endif // GTK+ 1.2/1.0
917 if ( !mitem
->IsSeparator() )
919 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
920 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
923 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
924 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
928 #if GTK_MINOR_VERSION == 0
929 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
930 gtk_widget_show( menuItem
);
933 mitem
->SetMenuItem(menuItem
);
938 bool wxMenu::DoAppend(wxMenuItem
*mitem
)
940 return GtkAppend(mitem
) && wxMenuBase::DoAppend(mitem
);
943 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
945 if ( !wxMenuBase::DoInsert(pos
, item
) )
949 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
950 // of version 1.2.6), so we first append the item and then change its
952 if ( !GtkAppend(item
) )
955 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
956 gpointer data
= g_list_last(menu_shell
->children
)->data
;
957 menu_shell
->children
= g_list_remove(menu_shell
->children
, data
);
958 menu_shell
->children
= g_list_insert(menu_shell
->children
, data
, pos
);
962 // this should be easy to do...
963 wxFAIL_MSG( wxT("not implemented") );
966 #endif // GTK 1.2/1.0
969 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
971 if ( !wxMenuBase::DoRemove(item
) )
972 return (wxMenuItem
*)NULL
;
974 // TODO: this code doesn't delete the item factory item and this seems
975 // impossible as of GTK 1.2.6.
976 gtk_widget_destroy( item
->GetMenuItem() );
981 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
983 wxNode
*node
= m_items
.First();
986 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
987 if (item
->GetMenuItem() == menuItem
)
988 return item
->GetId();
995 // ----------------------------------------------------------------------------
997 // ----------------------------------------------------------------------------
999 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
1000 static wxString
GetHotKey( const wxMenuItem
& item
)
1004 wxAcceleratorEntry
*accel
= item
.GetAccel();
1007 int flags
= accel
->GetFlags();
1008 if ( flags
& wxACCEL_ALT
)
1009 hotkey
+= wxT("<alt>");
1010 if ( flags
& wxACCEL_CTRL
)
1011 hotkey
+= wxT("<control>");
1012 if ( flags
& wxACCEL_SHIFT
)
1013 hotkey
+= wxT("<shift>");
1015 int code
= accel
->GetKeyCode();
1030 hotkey
<< wxT('F') << code
- WXK_F1
+ 1;
1033 // if there are any other keys wxGetAccelFromString() may return,
1034 // we should process them here
1037 if ( wxIsalnum(code
) )
1039 hotkey
<< (wxChar
)code
;
1044 wxFAIL_MSG( wxT("unknown keyboard accel") );
1052 #endif // wxUSE_ACCEL