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
);
83 wxMenuBar::wxMenuBar()
85 /* the parent window is known after wxFrame::SetMenu() */
88 m_invokingWindow
= (wxWindow
*) NULL
;
90 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
91 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, 0, wxDefaultValidator
, wxT("menubar") ))
93 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
97 m_menus
.DeleteContents( TRUE
);
99 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
100 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
101 m_accel
= gtk_accel_group_new();
102 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU_BAR
, "<main>", m_accel
);
103 m_menubar
= gtk_item_factory_get_widget( m_factory
, "<main>" );
105 m_menubar
= gtk_menu_bar_new();
108 m_widget
= GTK_WIDGET(m_menubar
);
113 wxMenuBar::~wxMenuBar()
115 // gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
118 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
120 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
122 #if (GTK_MINOR_VERSION > 0)
123 wxWindow
*top_frame
= win
;
124 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
125 top_frame
= top_frame
->GetParent();
127 /* support for native hot keys */
128 gtk_accel_group_detach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
131 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
134 wxMenuItem
*menuitem
= node
->GetData();
135 if (menuitem
->IsSubMenu())
136 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
137 node
= node
->GetNext();
141 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
143 menu
->SetInvokingWindow( win
);
145 #if (GTK_MINOR_VERSION > 0)
146 wxWindow
*top_frame
= win
;
147 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
148 top_frame
= top_frame
->GetParent();
150 /* support for native hot keys */
151 gtk_accel_group_attach( menu
->m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
154 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
157 wxMenuItem
*menuitem
= node
->GetData();
158 if (menuitem
->IsSubMenu())
159 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
160 node
= node
->GetNext();
164 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
166 m_invokingWindow
= win
;
167 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
168 wxWindow
*top_frame
= win
;
169 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
170 top_frame
= top_frame
->GetParent();
172 /* support for native key accelerators indicated by underscroes */
173 gtk_accel_group_attach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
176 wxMenuList::Node
*node
= m_menus
.GetFirst();
179 wxMenu
*menu
= node
->GetData();
180 wxMenubarSetInvokingWindow( menu
, win
);
181 node
= node
->GetNext();
185 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
187 m_invokingWindow
= (wxWindow
*) NULL
;
188 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
189 wxWindow
*top_frame
= win
;
190 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
191 top_frame
= top_frame
->GetParent();
193 /* support for native key accelerators indicated by underscroes */
194 gtk_accel_group_detach( m_accel
, GTK_OBJECT(top_frame
->m_widget
) );
197 wxMenuList::Node
*node
= m_menus
.GetFirst();
200 wxMenu
*menu
= node
->GetData();
201 wxMenubarUnsetInvokingWindow( menu
, win
);
202 node
= node
->GetNext();
206 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
208 if ( !wxMenuBarBase::Append( menu
, title
) )
211 return GtkAppend(menu
, title
);
214 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
)
218 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
220 for ( pc
= title
; *pc
!= wxT('\0'); pc
++ )
224 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
227 else if (*pc
== wxT('/'))
235 if ( *pc
== wxT('_') )
237 // underscores must be doubled to prevent them from being
238 // interpreted as accelerator character prefix by GTK
247 /* this doesn't have much effect right now */
248 menu
->SetTitle( str
);
250 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
251 #if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
253 /* local buffer in multibyte form */
255 buf
<< wxT('/') << str
.c_str();
257 char *cbuf
= new char[buf
.Length()+1];
258 strcpy(cbuf
, buf
.mbc_str());
260 GtkItemFactoryEntry entry
;
261 entry
.path
= (gchar
*)cbuf
; // const_cast
262 entry
.accelerator
= (gchar
*) NULL
;
263 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
264 entry
.callback_action
= 0;
265 entry
.item_type
= "<Branch>";
267 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
268 /* in order to get the pointer to the item we need the item text _without_ underscores */
269 wxString tmp
= wxT("<main>/");
270 for ( pc
= str
; *pc
!= wxT('\0'); pc
++ )
272 // contrary to the common sense, we must throw out _all_ underscores,
273 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
274 // might naively think). IMHO it's a bug in GTK+ (VZ)
275 while (*pc
== wxT('_'))
279 menu
->m_owner
= gtk_item_factory_get_item( m_factory
, tmp
.mb_str() );
280 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
284 menu
->m_owner
= gtk_menu_item_new_with_label( str
.mb_str() );
285 gtk_widget_show( menu
->m_owner
);
286 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
288 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar
), menu
->m_owner
);
292 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
293 // adding menu later on.
294 if (m_invokingWindow
)
295 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
300 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
302 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
306 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
307 // of version 1.2.6), so we first append the item and then change its
309 if ( !GtkAppend(menu
, title
) )
312 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
313 gpointer data
= g_list_last(menu_shell
->children
)->data
;
314 menu_shell
->children
= g_list_remove(menu_shell
->children
, data
);
315 menu_shell
->children
= g_list_insert(menu_shell
->children
, data
, pos
);
319 // this should be easy to do with GTK 1.0 - can use standard functions for
320 // this and don't need any hacks like above, but as I don't have GTK 1.0
321 // any more I can't do it
322 wxFAIL_MSG( wxT("TODO") );
325 #endif // GTK 1.2/1.0
328 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
330 if ( !wxMenuBarBase::Replace(pos
, menu
, title
) )
331 return (wxMenu
*) NULL
;
333 // remove the old item and insert a new one
334 wxMenu
*menuOld
= Remove(pos
);
335 if ( menuOld
&& !Insert(pos
, menu
, title
) )
337 return (wxMenu
*) NULL
;
340 // either Insert() succeeded or Remove() failed and menuOld is NULL
344 wxMenu
*wxMenuBar::Remove(size_t pos
)
346 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
348 return (wxMenu
*) NULL
;
350 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
351 printf( "factory entries before %d\n", (int)g_slist_length(m_factory
->items
) );
352 printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell
->children
) );
354 // unparent calls unref() and that would delete the widget so we raise
355 // the ref count to 2 artificially before invoking unparent.
356 gtk_widget_ref( menu
->m_menu
);
357 gtk_widget_unparent( menu
->m_menu
);
359 gtk_widget_destroy( menu
->m_owner
);
361 printf( "factory entries after %d\n", (int)g_slist_length(m_factory
->items
) );
362 printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell
->children
) );
367 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
369 if (menu
->GetTitle() == menuString
)
371 int res
= menu
->FindItem( itemString
);
372 if (res
!= wxNOT_FOUND
)
376 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
379 wxMenuItem
*item
= node
->GetData();
380 if (item
->IsSubMenu())
381 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
383 node
= node
->GetNext();
389 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
391 wxMenuList::Node
*node
= m_menus
.GetFirst();
394 wxMenu
*menu
= node
->GetData();
395 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
398 node
= node
->GetNext();
404 // Find a wxMenuItem using its id. Recurses down into sub-menus
405 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
407 wxMenuItem
* result
= menu
->FindChildItem(id
);
409 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetFirst();
410 while ( node
&& result
== NULL
)
412 wxMenuItem
*item
= node
->GetData();
413 if (item
->IsSubMenu())
415 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
417 node
= node
->GetNext();
423 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
425 wxMenuItem
* result
= 0;
426 wxMenuList::Node
*node
= m_menus
.GetFirst();
427 while (node
&& result
== 0)
429 wxMenu
*menu
= node
->GetData();
430 result
= FindMenuItemByIdRecursive( menu
, id
);
431 node
= node
->GetNext();
436 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
442 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
444 wxMenuList::Node
*node
= m_menus
.Item( pos
);
446 wxCHECK_RET( node
, wxT("menu not found") );
448 wxMenu
* menu
= node
->GetData();
451 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
454 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
456 wxMenuList::Node
*node
= m_menus
.Item( pos
);
458 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
460 wxMenu
* menu
= node
->GetData();
462 return menu
->GetTitle();
465 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
467 wxMenuList::Node
*node
= m_menus
.Item( pos
);
469 wxCHECK_RET( node
, wxT("menu not found") );
471 wxMenu
* menu
= node
->GetData();
473 menu
->SetTitle( label
);
476 //-----------------------------------------------------------------------------
478 //-----------------------------------------------------------------------------
480 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
482 if (g_isIdle
) wxapp_install_idle_handler();
484 int id
= menu
->FindMenuIdByMenuItem(widget
);
486 /* should find it for normal (not popup) menu */
487 wxASSERT( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
) );
489 if (!menu
->IsEnabled(id
))
492 wxMenuItem
* item
= menu
->FindChildItem( id
);
493 wxCHECK_RET( item
, wxT("error in menu item callback") );
495 if (item
->IsCheckable())
497 bool isReallyChecked
= item
->IsChecked();
498 if ( item
->wxMenuItemBase::IsChecked() == isReallyChecked
)
500 /* the menu item has been checked by calling wxMenuItem->Check() */
505 /* the user pressed on the menu item -> report and make consistent
507 item
->wxMenuItemBase::Check(isReallyChecked
);
511 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, id
);
512 event
.SetEventObject( menu
);
515 #if wxUSE_MENU_CALLBACK
516 if (menu
->GetCallback())
518 (void) (*(menu
->GetCallback())) (*menu
, event
);
521 #endif // wxUSE_MENU_CALLBACK
523 if (menu
->GetEventHandler()->ProcessEvent(event
))
526 wxWindow
*win
= menu
->GetInvokingWindow();
528 win
->GetEventHandler()->ProcessEvent( event
);
531 //-----------------------------------------------------------------------------
533 //-----------------------------------------------------------------------------
535 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
537 if (g_isIdle
) wxapp_install_idle_handler();
539 int id
= menu
->FindMenuIdByMenuItem(widget
);
541 wxASSERT( id
!= -1 ); // should find it!
543 if (!menu
->IsEnabled(id
))
546 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
547 event
.SetEventObject( menu
);
549 if (menu
->GetEventHandler()->ProcessEvent(event
))
552 wxWindow
*win
= menu
->GetInvokingWindow();
553 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
556 //-----------------------------------------------------------------------------
558 //-----------------------------------------------------------------------------
560 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
562 if (g_isIdle
) wxapp_install_idle_handler();
564 int id
= menu
->FindMenuIdByMenuItem(widget
);
566 wxASSERT( id
!= -1 ); // should find it!
568 if (!menu
->IsEnabled(id
))
571 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
572 event
.SetEventObject( menu
);
574 if (menu
->GetEventHandler()->ProcessEvent(event
))
577 wxWindow
*win
= menu
->GetInvokingWindow();
579 win
->GetEventHandler()->ProcessEvent( event
);
582 //-----------------------------------------------------------------------------
584 //-----------------------------------------------------------------------------
586 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxMenuItemBase
)
588 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
590 const wxString
& name
,
591 const wxString
& help
,
595 return new wxMenuItem(parentMenu
, id
, name
, help
, isCheckable
, subMenu
);
598 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
600 const wxString
& text
,
601 const wxString
& help
,
606 m_isCheckable
= isCheckable
;
610 m_parentMenu
= parentMenu
;
613 m_menuItem
= (GtkWidget
*) NULL
;
618 wxMenuItem::~wxMenuItem()
620 // don't delete menu items, the menus take care of that
623 // return the menu item text without any menu accels
624 wxString
wxMenuItem::GetLabel() const
627 #if (GTK_MINOR_VERSION > 0)
628 for ( const wxChar
*pc
= m_text
.c_str(); *pc
; pc
++ )
630 if ( *pc
== wxT('_') )
632 // this is the escape character for GTK+ - skip it
640 #endif // GTK+ 1.2/1.0
645 void wxMenuItem::SetText( const wxString
& str
)
651 GtkLabel
*label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
654 gtk_label_set( label
, m_text
.mb_str());
656 /* reparse key accel */
657 (void)gtk_label_parse_uline (GTK_LABEL(label
), m_text
.mb_str() );
658 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
662 // it's valid for this function to be called even if m_menuItem == NULL
663 void wxMenuItem::DoSetText( const wxString
& str
)
665 /* '\t' is the deliminator indicating a hot key */
667 const wxChar
*pc
= str
;
668 for (; (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')); pc
++ )
672 #if (GTK_MINOR_VERSION > 0)
675 else if ( *pc
== wxT('_') ) // escape underscores
679 else if (*pc
== wxT('/')) /* we have to filter out slashes ... */
681 m_text
<< wxT('\\'); /* ... and replace them with back slashes */
688 /* only GTK 1.2 knows about hot keys */
690 #if (GTK_MINOR_VERSION > 0)
701 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
706 return (wxAcceleratorEntry
*)NULL
;
709 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
711 label
<< wxT('\t') << GetHotKey();
713 return wxGetAccelFromString(label
);
716 #endif // wxUSE_ACCEL
718 void wxMenuItem::Check( bool check
)
720 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
722 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
724 if (check
== m_isChecked
)
727 wxMenuItemBase::Check( check
);
728 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
731 void wxMenuItem::Enable( bool enable
)
733 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
735 gtk_widget_set_sensitive( m_menuItem
, enable
);
736 wxMenuItemBase::Enable( enable
);
739 bool wxMenuItem::IsChecked() const
741 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
743 wxCHECK_MSG( IsCheckable(), FALSE
,
744 wxT("can't get state of uncheckable item!") );
746 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
749 wxString
wxMenuItem::GetFactoryPath() const
751 /* in order to get the pointer to the item we need the item text _without_
753 wxString
path( wxT("<main>/") );
759 //-----------------------------------------------------------------------------
761 //-----------------------------------------------------------------------------
763 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
767 #if (GTK_MINOR_VERSION > 0)
768 m_accel
= gtk_accel_group_new();
769 m_factory
= gtk_item_factory_new( GTK_TYPE_MENU
, "<main>", m_accel
);
770 m_menu
= gtk_item_factory_get_widget( m_factory
, "<main>" );
772 m_menu
= gtk_menu_new(); // Do not show!
775 m_owner
= (GtkWidget
*) NULL
;
777 #if (GTK_MINOR_VERSION > 0)
778 /* Tearoffs are entries, just like separators. So if we want this
779 menu to be a tear-off one, we just append a tearoff entry
781 if(m_style
& wxMENU_TEAROFF
)
783 GtkItemFactoryEntry entry
;
784 entry
.path
= "/tearoff";
785 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
786 entry
.callback_action
= 0;
787 entry
.item_type
= "<Tearoff>";
788 entry
.accelerator
= (gchar
*) NULL
;
789 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
790 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
794 // append the title as the very first entry if we have it
806 gtk_widget_destroy( m_menu
);
808 gtk_object_unref( GTK_OBJECT(m_factory
) );
811 bool wxMenu::GtkAppend(wxMenuItem
*mitem
)
815 if ( mitem
->IsSeparator() )
817 #if (GTK_MINOR_VERSION > 0)
818 GtkItemFactoryEntry entry
;
820 entry
.callback
= (GtkItemFactoryCallback
) NULL
;
821 entry
.callback_action
= 0;
822 entry
.item_type
= "<Separator>";
823 entry
.accelerator
= (gchar
*) NULL
;
825 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
827 /* this will be wrong for more than one separator. do we care? */
828 menuItem
= gtk_item_factory_get_widget( m_factory
, "<main>/sep" );
830 menuItem
= gtk_menu_item_new();
831 #endif // GTK 1.2/1.0
833 else if ( mitem
->IsSubMenu() )
835 #if (GTK_MINOR_VERSION > 0)
836 /* text has "_" instead of "&" after mitem->SetText() */
837 wxString
text( mitem
->GetText() );
839 /* local buffer in multibyte form */
842 strcat( buf
, text
.mb_str() );
844 GtkItemFactoryEntry entry
;
846 entry
.callback
= (GtkItemFactoryCallback
) 0;
847 entry
.callback_action
= 0;
848 entry
.item_type
= "<Branch>";
849 entry
.accelerator
= (gchar
*) NULL
;
851 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
853 wxString
path( mitem
->GetFactoryPath() );
854 menuItem
= gtk_item_factory_get_item( m_factory
, path
.mb_str() );
856 menuItem
= gtk_menu_item_new_with_label(mitem
->GetText().mbc_str());
857 #endif // GTK 1.2/1.0
859 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
861 else // a normal item
863 #if (GTK_MINOR_VERSION > 0)
864 /* text has "_" instead of "&" after mitem->SetText() */
865 wxString
text( mitem
->GetText() );
867 /* local buffer in multibyte form */
870 strcat( buf
, text
.mb_str() );
872 GtkItemFactoryEntry entry
;
874 entry
.callback
= (GtkItemFactoryCallback
) gtk_menu_clicked_callback
;
875 entry
.callback_action
= 0;
876 if ( mitem
->IsCheckable() )
877 entry
.item_type
= "<CheckItem>";
879 entry
.item_type
= "<Item>";
880 entry
.accelerator
= (gchar
*) NULL
;
883 // due to an apparent bug in GTK+, we have to use a static buffer here -
884 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
886 static char s_accel
[32]; // must be big enough for <control><alt><shift>F12
887 strncpy(s_accel
, GetHotKey(*mitem
).mb_str(), WXSIZEOF(s_accel
));
888 entry
.accelerator
= s_accel
;
889 #else // !wxUSE_ACCEL
890 entry
.accelerator
= (char*) NULL
;
891 #endif // wxUSE_ACCEL/!wxUSE_ACCEL
893 gtk_item_factory_create_item( m_factory
, &entry
, (gpointer
) this, 2 ); /* what is 2 ? */
895 wxString
path( mitem
->GetFactoryPath() );
896 menuItem
= gtk_item_factory_get_widget( m_factory
, path
.mb_str() );
898 menuItem
= checkable
? gtk_check_menu_item_new_with_label( mitem
->GetText().mb_str() )
899 : gtk_menu_item_new_with_label( mitem
->GetText().mb_str() );
901 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
902 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
904 #endif // GTK+ 1.2/1.0
907 if ( !mitem
->IsSeparator() )
909 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
910 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
913 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
914 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
918 #if GTK_MINOR_VERSION == 0
919 gtk_menu_append( GTK_MENU(m_menu
), menuItem
);
920 gtk_widget_show( menuItem
);
923 mitem
->SetMenuItem(menuItem
);
928 bool wxMenu::DoAppend(wxMenuItem
*mitem
)
930 return GtkAppend(mitem
) && wxMenuBase::DoAppend(mitem
);
933 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
935 if ( !wxMenuBase::DoInsert(pos
, item
) )
939 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
940 // of version 1.2.6), so we first append the item and then change its
942 if ( !GtkAppend(item
) )
945 GtkMenuShell
*menu_shell
= GTK_MENU_SHELL(m_factory
->widget
);
946 gpointer data
= g_list_last(menu_shell
->children
)->data
;
947 menu_shell
->children
= g_list_remove(menu_shell
->children
, data
);
948 menu_shell
->children
= g_list_insert(menu_shell
->children
, data
, pos
);
952 // this should be easy to do...
953 wxFAIL_MSG( wxT("not implemented") );
954 #endif // GTK 1.2/1.0
959 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
961 if ( !wxMenuBase::DoRemove(item
) )
962 return (wxMenuItem
*)NULL
;
964 // TODO: this code doesn't delete the item factory item and this seems
965 // impossible as of GTK 1.2.6.
966 gtk_widget_destroy( item
->GetMenuItem() );
971 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
973 wxNode
*node
= m_items
.First();
976 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
977 if (item
->GetMenuItem() == menuItem
)
978 return item
->GetId();
985 // ----------------------------------------------------------------------------
987 // ----------------------------------------------------------------------------
989 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
990 static wxString
GetHotKey( const wxMenuItem
& item
)
994 wxAcceleratorEntry
*accel
= item
.GetAccel();
997 int flags
= accel
->GetFlags();
998 if ( flags
& wxACCEL_ALT
)
999 hotkey
+= wxT("<alt>");
1000 if ( flags
& wxACCEL_CTRL
)
1001 hotkey
+= wxT("<control>");
1002 if ( flags
& wxACCEL_SHIFT
)
1003 hotkey
+= wxT("<shift>");
1005 int code
= accel
->GetKeyCode();
1020 hotkey
<< wxT('F') << code
- WXK_F1
+ 1;
1023 // if there are any other keys wxGetAccelFromString() may return,
1024 // we should process them here
1027 if ( wxIsalnum(code
) )
1029 hotkey
<< (wxChar
)code
;
1034 wxFAIL_MSG( wxT("unknown keyboard accel") );
1042 #endif // wxUSE_ACCEL