1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #include "wx/bitmap.h"
23 #include "wx/gtk/private.h"
25 #include <gdk/gdkkeysyms.h>
27 // FIXME: is this right? somehow I don't think so (VZ)
29 #include <glib-object.h>
31 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
32 #define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
33 #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
35 #define ACCEL_OBJECT GtkWindow
36 #define ACCEL_OBJECTS(a) (a)->acceleratables
37 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
39 #define ACCEL_OBJECT GtkObject
40 #define ACCEL_OBJECTS(a) (a)->attach_objects
41 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
44 // we use normal item but with a special id for the menu title
45 static const int wxGTK_TITLE_ID
= -3;
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 extern void wxapp_install_idle_handler();
55 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 static wxString
wxReplaceUnderscore( const wxString
& title
)
66 // GTK 1.2 wants to have "_" instead of "&" for accelerators
69 while (*pc
!= wxT('\0'))
71 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
73 // "&" is doubled to indicate "&" instead of accelerator
77 else if (*pc
== wxT('&'))
83 if ( *pc
== wxT('_') )
85 // underscores must be doubled to prevent them from being
86 // interpreted as accelerator character prefix by GTK
95 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
100 //-----------------------------------------------------------------------------
101 // activate message from GTK
102 //-----------------------------------------------------------------------------
105 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
107 if (g_isIdle
) wxapp_install_idle_handler();
109 wxMenuEvent
event( wxEVT_MENU_OPEN
, -1, menu
);
110 event
.SetEventObject( menu
);
112 wxEvtHandler
* handler
= menu
->GetEventHandler();
113 if (handler
&& handler
->ProcessEvent(event
))
116 wxWindow
*win
= menu
->GetInvokingWindow();
117 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
125 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
127 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
129 // the parent window is known after wxFrame::SetMenu()
130 m_needParent
= FALSE
;
132 m_invokingWindow
= (wxWindow
*) NULL
;
134 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
135 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
137 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
141 m_menubar
= gtk_menu_bar_new();
143 m_accel
= gtk_accel_group_new();
146 if (style
& wxMB_DOCKABLE
)
148 m_widget
= gtk_handle_box_new();
149 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
150 gtk_widget_show( GTK_WIDGET(m_menubar
) );
154 m_widget
= GTK_WIDGET(m_menubar
);
161 for (size_t i
= 0; i
< n
; ++i
)
162 Append(menus
[i
], titles
[i
]);
165 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
167 Init(n
, menus
, titles
, style
);
170 wxMenuBar::wxMenuBar(long style
)
172 Init(0, NULL
, NULL
, style
);
175 wxMenuBar::wxMenuBar()
177 Init(0, NULL
, NULL
, 0);
180 wxMenuBar::~wxMenuBar()
184 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
186 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
188 wxWindow
*top_frame
= win
;
189 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
190 top_frame
= top_frame
->GetParent();
193 // support for native hot keys
194 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
197 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
200 wxMenuItem
*menuitem
= node
->GetData();
201 if (menuitem
->IsSubMenu())
202 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
203 node
= node
->GetNext();
207 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
209 menu
->SetInvokingWindow( win
);
211 wxWindow
*top_frame
= win
;
212 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
213 top_frame
= top_frame
->GetParent();
215 // support for native hot keys
216 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
217 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
218 gtk_accel_group_attach( menu
->m_accel
, obj
);
220 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
223 wxMenuItem
*menuitem
= node
->GetData();
224 if (menuitem
->IsSubMenu())
225 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
226 node
= node
->GetNext();
230 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
232 m_invokingWindow
= win
;
233 wxWindow
*top_frame
= win
;
234 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
235 top_frame
= top_frame
->GetParent();
238 // support for native key accelerators indicated by underscroes
239 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
240 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
241 gtk_accel_group_attach( m_accel
, obj
);
244 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
247 wxMenu
*menu
= node
->GetData();
248 wxMenubarSetInvokingWindow( menu
, win
);
249 node
= node
->GetNext();
253 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
255 m_invokingWindow
= (wxWindow
*) NULL
;
256 wxWindow
*top_frame
= win
;
257 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
258 top_frame
= top_frame
->GetParent();
261 // support for native key accelerators indicated by underscroes
262 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
265 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
268 wxMenu
*menu
= node
->GetData();
269 wxMenubarUnsetInvokingWindow( menu
, win
);
270 node
= node
->GetNext();
274 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
276 if ( !wxMenuBarBase::Append( menu
, title
) )
279 return GtkAppend(menu
, title
);
282 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
284 wxString
str( wxReplaceUnderscore( title
) );
286 // This doesn't have much effect right now.
287 menu
->SetTitle( str
);
289 // The "m_owner" is the "menu item"
291 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
293 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
294 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
296 gtk_label_set_text( label
, wxGTK_CONV( str
) );
298 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
299 if (accel_key
!= GDK_VoidSymbol
)
301 gtk_widget_add_accelerator (menu
->m_owner
,
303 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
310 gtk_widget_show( menu
->m_owner
);
312 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
315 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
317 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
319 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
320 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
323 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
324 // addings menu later on.
325 if (m_invokingWindow
)
327 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
329 // OPTIMISE ME: we should probably cache this, or pass it
330 // directly, but for now this is a minimal
331 // change to validate the new dynamic sizing.
332 // see (and refactor :) similar code in Remove
335 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
338 frame
->UpdateMenuBarSize();
344 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
346 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
351 if ( !GtkAppend(menu
, title
, (int)pos
) )
357 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
359 // remove the old item and insert a new one
360 wxMenu
*menuOld
= Remove(pos
);
361 if ( menuOld
&& !Insert(pos
, menu
, title
) )
363 return (wxMenu
*) NULL
;
366 // either Insert() succeeded or Remove() failed and menuOld is NULL
370 wxMenu
*wxMenuBar::Remove(size_t pos
)
372 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
374 return (wxMenu
*) NULL
;
376 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
377 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
379 gtk_widget_destroy( menu
->m_owner
);
380 menu
->m_owner
= NULL
;
382 if (m_invokingWindow
)
384 // OPTIMISE ME: see comment in GtkAppend
385 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
388 frame
->UpdateMenuBarSize();
394 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
396 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
398 int res
= menu
->FindItem( itemString
);
399 if (res
!= wxNOT_FOUND
)
403 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
406 wxMenuItem
*item
= node
->GetData();
407 if (item
->IsSubMenu())
408 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
410 node
= node
->GetNext();
416 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
418 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
421 wxMenu
*menu
= node
->GetData();
422 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
425 node
= node
->GetNext();
431 // Find a wxMenuItem using its id. Recurses down into sub-menus
432 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
434 wxMenuItem
* result
= menu
->FindChildItem(id
);
436 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
437 while ( node
&& result
== NULL
)
439 wxMenuItem
*item
= node
->GetData();
440 if (item
->IsSubMenu())
442 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
444 node
= node
->GetNext();
450 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
452 wxMenuItem
* result
= 0;
453 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
454 while (node
&& result
== 0)
456 wxMenu
*menu
= node
->GetData();
457 result
= FindMenuItemByIdRecursive( menu
, id
);
458 node
= node
->GetNext();
463 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
469 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
471 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
473 wxCHECK_RET( node
, wxT("menu not found") );
475 wxMenu
* menu
= node
->GetData();
478 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
481 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
483 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
485 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
487 wxMenu
* menu
= node
->GetData();
490 wxString
text( menu
->GetTitle() );
491 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
493 if ( *pc
== wxT('_') )
495 // '_' is the escape character for GTK+
499 // don't remove ampersands '&' since if we have them in the menu title
500 // it means that they were doubled to indicate "&" instead of accelerator
508 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
510 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
512 wxCHECK_RET( node
, wxT("menu not found") );
514 wxMenu
* menu
= node
->GetData();
516 const wxString
str( wxReplaceUnderscore( label
) );
518 menu
->SetTitle( str
);
522 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
525 gtk_label_set( glabel
, wxGTK_CONV( str
) );
527 /* reparse key accel */
528 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
529 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
534 //-----------------------------------------------------------------------------
536 //-----------------------------------------------------------------------------
539 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
542 wxapp_install_idle_handler();
544 int id
= menu
->FindMenuIdByMenuItem(widget
);
546 /* should find it for normal (not popup) menu */
547 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
548 _T("menu item not found in gtk_menu_clicked_callback") );
550 if (!menu
->IsEnabled(id
))
553 wxMenuItem
* item
= menu
->FindChildItem( id
);
554 wxCHECK_RET( item
, wxT("error in menu item callback") );
556 if ( item
->GetId() == wxGTK_TITLE_ID
)
558 // ignore events from the menu title
562 if (item
->IsCheckable())
564 bool isReallyChecked
= item
->IsChecked(),
565 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
567 // ensure that the internal state is always consistent with what is
568 // shown on the screen
569 item
->wxMenuItemBase::Check(isReallyChecked
);
571 // we must not report the events for the radio button going up nor the
572 // events resulting from the calls to wxMenuItem::Check()
573 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
574 (isInternallyChecked
== isReallyChecked
) )
581 // Is this menu on a menubar? (possibly nested)
582 wxFrame
* frame
= NULL
;
583 if(menu
->IsAttached())
584 frame
= menu
->GetMenuBar()->GetFrame();
586 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
587 // normally wxMenu::SendEvent() should be enough, if it doesn't work
588 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
589 // should be fixed instead of working around it here...
592 // If it is attached then let the frame send the event.
593 // Don't call frame->ProcessCommand(id) because it toggles
594 // checkable items and we've already done that above.
595 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
596 commandEvent
.SetEventObject(frame
);
597 if (item
->IsCheckable())
598 commandEvent
.SetInt(item
->IsChecked());
599 commandEvent
.SetEventObject(menu
);
601 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
605 // otherwise let the menu have it
606 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
611 //-----------------------------------------------------------------------------
613 //-----------------------------------------------------------------------------
616 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
618 if (g_isIdle
) wxapp_install_idle_handler();
620 int id
= menu
->FindMenuIdByMenuItem(widget
);
622 wxASSERT( id
!= -1 ); // should find it!
624 if (!menu
->IsEnabled(id
))
627 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
628 event
.SetEventObject( menu
);
630 wxEvtHandler
* handler
= menu
->GetEventHandler();
631 if (handler
&& handler
->ProcessEvent(event
))
634 wxWindow
*win
= menu
->GetInvokingWindow();
635 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
639 //-----------------------------------------------------------------------------
641 //-----------------------------------------------------------------------------
644 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
646 if (g_isIdle
) wxapp_install_idle_handler();
648 int id
= menu
->FindMenuIdByMenuItem(widget
);
650 wxASSERT( id
!= -1 ); // should find it!
652 if (!menu
->IsEnabled(id
))
655 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
656 event
.SetEventObject( menu
);
658 wxEvtHandler
* handler
= menu
->GetEventHandler();
659 if (handler
&& handler
->ProcessEvent(event
))
662 wxWindow
*win
= menu
->GetInvokingWindow();
664 win
->GetEventHandler()->ProcessEvent( event
);
668 //-----------------------------------------------------------------------------
670 //-----------------------------------------------------------------------------
672 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
674 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
676 const wxString
& name
,
677 const wxString
& help
,
681 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
684 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
686 const wxString
& text
,
687 const wxString
& help
,
690 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
695 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
697 const wxString
& text
,
698 const wxString
& help
,
701 : wxMenuItemBase(parentMenu
, id
, text
, help
,
702 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
707 void wxMenuItem::Init(const wxString
& text
)
709 m_labelWidget
= (GtkWidget
*) NULL
;
710 m_menuItem
= (GtkWidget
*) NULL
;
715 wxMenuItem::~wxMenuItem()
717 // don't delete menu items, the menus take care of that
720 // return the menu item text without any menu accels
722 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
726 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
728 if ( *pc
== wxT('\t'))
731 if ( *pc
== wxT('_') )
733 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
740 if ( *pc
== wxT('\\') )
742 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
749 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
752 // "&" is doubled to indicate "&" instead of accelerator
759 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
764 void wxMenuItem::SetText( const wxString
& str
)
766 // Some optimization to avoid flicker
767 wxString oldLabel
= m_text
;
768 oldLabel
= wxStripMenuCodes(oldLabel
);
769 oldLabel
.Replace(wxT("_"), wxT(""));
770 wxString label1
= wxStripMenuCodes(str
);
771 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
772 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
776 if (oldLabel
== label1
&&
777 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
784 label
= (GtkLabel
*) m_labelWidget
;
786 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
789 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
792 gtk_label_set( label
, wxGTK_CONV( m_text
) );
795 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
796 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
801 GdkModifierType accel_mods
;
802 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
805 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
806 m_parentMenu
->m_accel
,
811 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
812 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
815 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
817 m_parentMenu
->m_accel
,
824 // it's valid for this function to be called even if m_menuItem == NULL
825 void wxMenuItem::DoSetText( const wxString
& str
)
827 // '\t' is the deliminator indicating a hot key
829 const wxChar
*pc
= str
;
830 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
832 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
834 // "&" is doubled to indicate "&" instead of accelerator
838 else if (*pc
== wxT('&'))
842 else if ( *pc
== wxT('_') ) // escape underscores
861 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
866 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
871 return (wxAcceleratorEntry
*)NULL
;
874 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
876 label
<< wxT('\t') << GetHotKey();
878 return wxGetAccelFromString(label
);
881 #endif // wxUSE_ACCEL
883 void wxMenuItem::Check( bool check
)
885 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
887 if (check
== m_isChecked
)
890 wxMenuItemBase::Check( check
);
896 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
900 wxFAIL_MSG( _T("can't check this item") );
904 void wxMenuItem::Enable( bool enable
)
906 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
908 gtk_widget_set_sensitive( m_menuItem
, enable
);
909 wxMenuItemBase::Enable( enable
);
912 bool wxMenuItem::IsChecked() const
914 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
916 wxCHECK_MSG( IsCheckable(), FALSE
,
917 wxT("can't get state of uncheckable item!") );
919 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
922 //-----------------------------------------------------------------------------
924 //-----------------------------------------------------------------------------
926 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
930 m_accel
= gtk_accel_group_new();
931 m_menu
= gtk_menu_new();
932 // NB: keep reference to the menu so that it is not destroyed behind
933 // our back by GTK+ e.g. when it is removed from menubar:
934 gtk_widget_ref(m_menu
);
936 m_owner
= (GtkWidget
*) NULL
;
938 // Tearoffs are entries, just like separators. So if we want this
939 // menu to be a tear-off one, we just append a tearoff entry
941 if ( m_style
& wxMENU_TEAROFF
)
943 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
945 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
950 // append the title as the very first entry if we have it
951 if ( !m_title
.empty() )
953 Append(wxGTK_TITLE_ID
, m_title
);
960 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
962 if ( GTK_IS_WIDGET( m_menu
))
965 gtk_widget_unref( m_menu
);
966 // if the menu is inserted in another menu at this time, there was
967 // one more reference to it:
969 gtk_widget_destroy( m_menu
);
973 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
982 if ( mitem
->IsSeparator() )
985 menuItem
= gtk_separator_menu_item_new();
988 menuItem
= gtk_menu_item_new();
991 else if (mitem
->GetBitmap().Ok())
993 text
= mitem
->GetText();
994 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
997 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1000 if (bitmap
->HasPixbuf())
1002 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
1006 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
1007 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
1008 bitmap
->GetMask()->GetBitmap() :
1010 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1013 gtk_widget_show(image
);
1015 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1019 wxUnusedVar(bitmap
);
1020 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1021 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1026 else // a normal item
1028 // text has "_" instead of "&" after mitem->SetText() so don't use it
1029 text
= mitem
->GetText() ;
1031 switch ( mitem
->GetKind() )
1036 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1038 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1039 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1041 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1049 GSList
*group
= NULL
;
1050 if ( m_prevRadio
== NULL
)
1052 // start of a new radio group
1054 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1056 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1057 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1059 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1062 else // continue the radio group
1065 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1066 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1068 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1069 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1070 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1077 wxFAIL_MSG( _T("unexpected menu item kind") );
1083 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1085 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1086 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1096 GdkModifierType accel_mods
;
1097 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1099 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1100 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1103 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1112 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1114 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1116 gtk_widget_show( menuItem
);
1118 if ( !mitem
->IsSeparator() )
1120 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1122 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1123 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1126 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1127 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1130 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1132 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1134 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1136 // if adding a submenu to a menu already existing in the menu bar, we
1137 // must set invoking window to allow processing events from this
1139 if ( m_invokingWindow
)
1140 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1144 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1145 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1150 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1151 if (accel_key
!= GDK_VoidSymbol
)
1153 gtk_widget_add_accelerator (menuItem
,
1155 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1163 mitem
->SetMenuItem(menuItem
);
1167 // This doesn't even exist!
1168 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1174 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1176 if (!GtkAppend(mitem
))
1179 return wxMenuBase::DoAppend(mitem
);
1182 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1184 if ( !wxMenuBase::DoInsert(pos
, item
) )
1188 if ( !GtkAppend(item
, (int)pos
) )
1194 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1196 if ( !wxMenuBase::DoRemove(item
) )
1197 return (wxMenuItem
*)NULL
;
1199 // TODO: this code doesn't delete the item factory item and this seems
1200 // impossible as of GTK 1.2.6.
1201 gtk_widget_destroy( item
->GetMenuItem() );
1206 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1208 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1211 wxMenuItem
*item
= node
->GetData();
1212 if (item
->GetMenuItem() == menuItem
)
1213 return item
->GetId();
1214 node
= node
->GetNext();
1220 // ----------------------------------------------------------------------------
1222 // ----------------------------------------------------------------------------
1226 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1230 wxAcceleratorEntry
*accel
= item
.GetAccel();
1233 int flags
= accel
->GetFlags();
1234 if ( flags
& wxACCEL_ALT
)
1235 hotkey
+= wxT("<alt>");
1236 if ( flags
& wxACCEL_CTRL
)
1237 hotkey
+= wxT("<control>");
1238 if ( flags
& wxACCEL_SHIFT
)
1239 hotkey
+= wxT("<shift>");
1241 int code
= accel
->GetKeyCode();
1268 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1271 // TODO: we should use gdk_keyval_name() (a.k.a.
1272 // XKeysymToString) here as well as hardcoding the keysym
1273 // names this might be not portable
1275 hotkey
<< wxT("Insert" );
1278 hotkey
<< wxT("Delete" );
1281 hotkey
<< wxT("Up" );
1284 hotkey
<< wxT("Down" );
1288 hotkey
<< wxT("Prior" );
1292 hotkey
<< wxT("Next" );
1295 hotkey
<< wxT("Left" );
1298 hotkey
<< wxT("Right" );
1301 hotkey
<< wxT("Home" );
1304 hotkey
<< wxT("End" );
1307 hotkey
<< wxT("Return" );
1310 hotkey
<< wxT("BackSpace" );
1313 hotkey
<< wxT("Tab" );
1316 hotkey
<< wxT("Esc" );
1319 hotkey
<< wxT("space" );
1322 hotkey
<< wxT("Multiply" );
1325 hotkey
<< wxT("Add" );
1328 hotkey
<< wxT("Separator" );
1331 hotkey
<< wxT("Subtract" );
1334 hotkey
<< wxT("Decimal" );
1337 hotkey
<< wxT("Divide" );
1340 hotkey
<< wxT("Cancel" );
1343 hotkey
<< wxT("Clear" );
1346 hotkey
<< wxT("Menu" );
1349 hotkey
<< wxT("Pause" );
1352 hotkey
<< wxT("Capital" );
1355 hotkey
<< wxT("Select" );
1358 hotkey
<< wxT("Print" );
1361 hotkey
<< wxT("Execute" );
1364 hotkey
<< wxT("Snapshot" );
1367 hotkey
<< wxT("Help" );
1370 hotkey
<< wxT("Num_Lock" );
1373 hotkey
<< wxT("Scroll_Lock" );
1375 case WXK_NUMPAD_INSERT
:
1376 hotkey
<< wxT("KP_Insert" );
1378 case WXK_NUMPAD_DELETE
:
1379 hotkey
<< wxT("KP_Delete" );
1381 case WXK_NUMPAD_SPACE
:
1382 hotkey
<< wxT("KP_Space" );
1384 case WXK_NUMPAD_TAB
:
1385 hotkey
<< wxT("KP_Tab" );
1387 case WXK_NUMPAD_ENTER
:
1388 hotkey
<< wxT("KP_Enter" );
1390 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1392 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1394 case WXK_NUMPAD_HOME
:
1395 hotkey
<< wxT("KP_Home" );
1397 case WXK_NUMPAD_LEFT
:
1398 hotkey
<< wxT("KP_Left" );
1401 hotkey
<< wxT("KP_Up" );
1403 case WXK_NUMPAD_RIGHT
:
1404 hotkey
<< wxT("KP_Right" );
1406 case WXK_NUMPAD_DOWN
:
1407 hotkey
<< wxT("KP_Down" );
1409 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1410 hotkey
<< wxT("KP_Prior" );
1412 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1413 hotkey
<< wxT("KP_Next" );
1415 case WXK_NUMPAD_END
:
1416 hotkey
<< wxT("KP_End" );
1418 case WXK_NUMPAD_BEGIN
:
1419 hotkey
<< wxT("KP_Begin" );
1421 case WXK_NUMPAD_EQUAL
:
1422 hotkey
<< wxT("KP_Equal" );
1424 case WXK_NUMPAD_MULTIPLY
:
1425 hotkey
<< wxT("KP_Multiply" );
1427 case WXK_NUMPAD_ADD
:
1428 hotkey
<< wxT("KP_Add" );
1430 case WXK_NUMPAD_SEPARATOR
:
1431 hotkey
<< wxT("KP_Separator" );
1433 case WXK_NUMPAD_SUBTRACT
:
1434 hotkey
<< wxT("KP_Subtract" );
1436 case WXK_NUMPAD_DECIMAL
:
1437 hotkey
<< wxT("KP_Decimal" );
1439 case WXK_NUMPAD_DIVIDE
:
1440 hotkey
<< wxT("KP_Divide" );
1442 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1443 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1444 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1445 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1447 case WXK_WINDOWS_LEFT
:
1448 hotkey
<< wxT("Super_L" );
1450 case WXK_WINDOWS_RIGHT
:
1451 hotkey
<< wxT("Super_R" );
1453 case WXK_WINDOWS_MENU
:
1454 hotkey
<< wxT("Menu" );
1457 hotkey
<< wxT("Command" );
1459 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1460 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1461 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1462 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1463 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1464 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1465 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1468 // if there are any other keys wxGetAccelFromString() may
1469 // return, we should process them here
1474 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1482 wxFAIL_MSG( wxT("unknown keyboard accel") );
1491 #endif // wxUSE_ACCEL