1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
3 // Purpose: implementation of wxMenuBar and wxMenu classes for wxGTK
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"
21 #include "wx/bitmap.h"
26 #include "wx/stockitem.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
30 // we use normal item but with a special id for the menu title
31 static const int wxGTK_TITLE_ID
= -3;
33 // forward declare it as it's used by wxMenuBar too when using Hildon
36 static void gtk_menu_clicked_callback(GtkWidget
*widget
, wxMenu
*menu
);
40 static bool wxGetStockGtkAccelerator(const char *id
, GdkModifierType
*mod
, guint
*key
);
41 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
44 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
46 event
.SetEventObject( menu
);
48 wxEvtHandler
* handler
= menu
->GetEventHandler();
49 if (handler
&& handler
->SafelyProcessEvent(event
))
52 wxWindow
*win
= menu
->GetInvokingWindow();
54 win
->HandleWindowEvent( event
);
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
63 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
65 m_invokingWindow
= NULL
;
68 // Hildon window uses a single menu instead of a menu bar, so wxMenuBar is
69 // the same as menu in this case
71 m_menubar
= gtk_menu_new();
72 #else // !wxUSE_LIBHILDON
73 if (!PreCreation( NULL
, wxDefaultPosition
, wxDefaultSize
) ||
74 !CreateBase( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
76 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
80 m_menubar
= gtk_menu_bar_new();
82 if (style
& wxMB_DOCKABLE
)
84 m_widget
= gtk_handle_box_new();
85 gtk_container_add(GTK_CONTAINER(m_widget
), m_menubar
);
86 gtk_widget_show(m_menubar
);
96 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
98 for (size_t i
= 0; i
< n
; ++i
)
99 Append(menus
[i
], titles
[i
]);
102 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
104 Init(n
, menus
, titles
, style
);
107 wxMenuBar::wxMenuBar(long style
)
109 Init(0, NULL
, NULL
, style
);
112 wxMenuBar::wxMenuBar()
114 Init(0, NULL
, NULL
, 0);
117 wxMenuBar::~wxMenuBar()
122 wxMenubarUnsetInvokingWindow(wxMenu
* menu
, wxWindow
* win
, GtkWindow
* tlw
= NULL
)
124 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
126 // support for native hot keys
130 tlw
= GTK_WINDOW(wxGetTopLevelParent(win
)->m_widget
);
131 if (g_slist_find(menu
->m_accel
->acceleratables
, tlw
))
132 gtk_window_remove_accel_group(tlw
, menu
->m_accel
);
135 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
138 wxMenuItem
*menuitem
= node
->GetData();
139 if (menuitem
->IsSubMenu())
140 wxMenubarUnsetInvokingWindow(menuitem
->GetSubMenu(), win
, tlw
);
141 node
= node
->GetNext();
146 wxMenubarSetInvokingWindow(wxMenu
* menu
, wxWindow
* win
, GtkWindow
* tlw
= NULL
)
148 menu
->SetInvokingWindow( win
);
150 // support for native hot keys
154 tlw
= GTK_WINDOW(wxGetTopLevelParent(win
)->m_widget
);
155 if (!g_slist_find(menu
->m_accel
->acceleratables
, tlw
))
156 gtk_window_add_accel_group(tlw
, menu
->m_accel
);
159 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
162 wxMenuItem
*menuitem
= node
->GetData();
163 if (menuitem
->IsSubMenu())
164 wxMenubarSetInvokingWindow(menuitem
->GetSubMenu(), win
, tlw
);
165 node
= node
->GetNext();
169 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
171 m_invokingWindow
= win
;
173 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
176 wxMenu
*menu
= node
->GetData();
177 wxMenubarSetInvokingWindow( menu
, win
);
178 node
= node
->GetNext();
182 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir
)
184 if ( dir
== wxLayout_Default
)
186 const wxWindow
*const frame
= GetFrame();
189 // inherit layout from frame.
190 dir
= frame
->GetLayoutDirection();
192 else // use global layout
194 dir
= wxTheApp
->GetLayoutDirection();
198 if ( dir
== wxLayout_Default
)
201 GTKSetLayout(m_menubar
, dir
);
203 // also set the layout of all menus we already have (new ones will inherit
204 // the current layout)
205 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
207 node
= node
->GetNext() )
209 wxMenu
*const menu
= node
->GetData();
210 menu
->SetLayoutDirection(dir
);
214 wxLayoutDirection
wxMenuBar::GetLayoutDirection() const
216 return GTKGetLayout(m_menubar
);
219 void wxMenuBar::Attach(wxFrame
*frame
)
221 wxMenuBarBase::Attach(frame
);
223 SetLayoutDirection(wxLayout_Default
);
226 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
228 m_invokingWindow
= (wxWindow
*) NULL
;
230 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
233 wxMenu
*menu
= node
->GetData();
234 wxMenubarUnsetInvokingWindow( menu
, win
);
235 node
= node
->GetNext();
239 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
241 if ( !wxMenuBarBase::Append( menu
, title
) )
244 return GtkAppend(menu
, title
);
247 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
249 menu
->SetLayoutDirection(GetLayoutDirection());
252 // if the menu has only one item, append it directly to the top level menu
253 // instead of inserting a useless submenu
254 if ( menu
->GetMenuItemCount() == 1 )
256 wxMenuItem
* const item
= menu
->FindItemByPosition(0);
258 // remove both mnemonics and accelerator: neither is useful under Maemo
259 const wxString
str(wxStripMenuCodes(item
->GetItemLabel()));
261 if ( item
->IsSubMenu() )
262 return GtkAppend(item
->GetSubMenu(), str
, pos
);
264 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
266 g_signal_connect(menu
->m_owner
, "activate",
267 G_CALLBACK(gtk_menu_clicked_callback
), menu
);
268 item
->SetMenuItem(menu
->m_owner
);
271 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
273 const wxString
str(wxConvertMnemonicsToGTK(title
));
275 // This doesn't have much effect right now.
276 menu
->SetTitle( str
);
278 // The "m_owner" is the "menu item"
279 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
281 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
284 gtk_widget_show( menu
->m_owner
);
287 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
289 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
291 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
292 // addings menu later on.
293 if (m_invokingWindow
)
294 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
299 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
301 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
306 if ( !GtkAppend(menu
, title
, (int)pos
) )
312 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
314 // remove the old item and insert a new one
315 wxMenu
*menuOld
= Remove(pos
);
316 if ( menuOld
&& !Insert(pos
, menu
, title
) )
318 return (wxMenu
*) NULL
;
321 // either Insert() succeeded or Remove() failed and menuOld is NULL
325 wxMenu
*wxMenuBar::Remove(size_t pos
)
327 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
329 return (wxMenu
*) NULL
;
331 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu
->m_owner
), NULL
);
332 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
334 gtk_widget_destroy( menu
->m_owner
);
335 menu
->m_owner
= NULL
;
337 if (m_invokingWindow
)
338 wxMenubarUnsetInvokingWindow( menu
, m_invokingWindow
);
343 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
345 if (wxMenuItem::GetLabelText(wxConvertMnemonicsFromGTK(menu
->GetTitle())) == wxMenuItem::GetLabelText(menuString
))
347 int res
= menu
->FindItem( itemString
);
348 if (res
!= wxNOT_FOUND
)
352 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
355 wxMenuItem
*item
= node
->GetData();
356 if (item
->IsSubMenu())
357 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
359 node
= node
->GetNext();
365 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
367 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
370 wxMenu
*menu
= node
->GetData();
371 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
374 node
= node
->GetNext();
380 // Find a wxMenuItem using its id. Recurses down into sub-menus
381 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
383 wxMenuItem
* result
= menu
->FindChildItem(id
);
385 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
386 while ( node
&& result
== NULL
)
388 wxMenuItem
*item
= node
->GetData();
389 if (item
->IsSubMenu())
391 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
393 node
= node
->GetNext();
399 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
401 wxMenuItem
* result
= 0;
402 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
403 while (node
&& result
== 0)
405 wxMenu
*menu
= node
->GetData();
406 result
= FindMenuItemByIdRecursive( menu
, id
);
407 node
= node
->GetNext();
412 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
418 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
420 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
422 wxCHECK_RET( node
, wxT("menu not found") );
424 wxMenu
* menu
= node
->GetData();
427 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
430 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
432 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
434 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
436 wxMenu
* menu
= node
->GetData();
438 return wxConvertMnemonicsFromGTK(menu
->GetTitle());
441 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
443 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
445 wxCHECK_RET( node
, wxT("menu not found") );
447 wxMenu
* menu
= node
->GetData();
449 const wxString
str(wxConvertMnemonicsToGTK(label
));
451 menu
->SetTitle( str
);
454 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
457 //-----------------------------------------------------------------------------
459 //-----------------------------------------------------------------------------
462 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
464 int id
= menu
->FindMenuIdByMenuItem(widget
);
466 /* should find it for normal (not popup) menu */
467 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
468 _T("menu item not found in gtk_menu_clicked_callback") );
470 if (!menu
->IsEnabled(id
))
473 wxMenuItem
* item
= menu
->FindChildItem( id
);
474 wxCHECK_RET( item
, wxT("error in menu item callback") );
476 if ( item
->GetId() == wxGTK_TITLE_ID
)
478 // ignore events from the menu title
482 if (item
->IsCheckable())
484 bool isReallyChecked
= item
->IsChecked(),
485 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
487 // ensure that the internal state is always consistent with what is
488 // shown on the screen
489 item
->wxMenuItemBase::Check(isReallyChecked
);
491 // we must not report the events for the radio button going up nor the
492 // events resulting from the calls to wxMenuItem::Check()
493 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
494 (isInternallyChecked
== isReallyChecked
) )
501 // Is this menu on a menubar? (possibly nested)
502 wxFrame
* frame
= NULL
;
503 if(menu
->IsAttached())
504 frame
= menu
->GetMenuBar()->GetFrame();
506 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
507 // normally wxMenu::SendEvent() should be enough, if it doesn't work
508 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
509 // should be fixed instead of working around it here...
512 // If it is attached then let the frame send the event.
513 // Don't call frame->ProcessCommand(id) because it toggles
514 // checkable items and we've already done that above.
515 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
516 commandEvent
.SetEventObject(frame
);
517 if (item
->IsCheckable())
518 commandEvent
.SetInt(item
->IsChecked());
520 frame
->HandleWindowEvent(commandEvent
);
524 // otherwise let the menu have it
525 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
530 //-----------------------------------------------------------------------------
532 //-----------------------------------------------------------------------------
535 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
537 int id
= menu
->FindMenuIdByMenuItem(widget
);
539 wxASSERT( id
!= -1 ); // should find it!
541 if (!menu
->IsEnabled(id
))
544 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
545 event
.SetEventObject( menu
);
547 wxEvtHandler
* handler
= menu
->GetEventHandler();
548 if (handler
&& handler
->SafelyProcessEvent(event
))
551 wxWindow
*win
= menu
->GetInvokingWindow();
552 if (win
) win
->HandleWindowEvent( event
);
556 //-----------------------------------------------------------------------------
558 //-----------------------------------------------------------------------------
561 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
563 int id
= menu
->FindMenuIdByMenuItem(widget
);
565 wxASSERT( id
!= -1 ); // should find it!
567 if (!menu
->IsEnabled(id
))
570 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
571 event
.SetEventObject( menu
);
573 wxEvtHandler
* handler
= menu
->GetEventHandler();
574 if (handler
&& handler
->SafelyProcessEvent(event
))
577 wxWindow
*win
= menu
->GetInvokingWindow();
579 win
->HandleWindowEvent( event
);
583 //-----------------------------------------------------------------------------
585 //-----------------------------------------------------------------------------
587 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
589 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
591 const wxString
& name
,
592 const wxString
& help
,
596 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
599 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
601 const wxString
& text
,
602 const wxString
& help
,
605 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
610 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
612 const wxString
& text
,
613 const wxString
& help
,
616 : wxMenuItemBase(parentMenu
, id
, text
, help
,
617 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
622 void wxMenuItem::Init(const wxString
& text
)
624 m_labelWidget
= (GtkWidget
*) NULL
;
625 m_menuItem
= (GtkWidget
*) NULL
;
630 wxMenuItem::~wxMenuItem()
632 // don't delete menu items, the menus take care of that
635 // return the menu item text without any menu accels
638 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
640 // The argument to this function will now always be in wxWidgets standard label
641 // format, not GTK+ format, so we do what the other ports do.
643 return wxStripMenuCodes(text
);
648 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
650 if ( *pc
== wxT('\t'))
653 if ( *pc
== wxT('_') )
655 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
661 if ( *pc
== wxT('\\') )
663 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
669 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
672 // "&" is doubled to indicate "&" instead of accelerator
679 // wxPrintf( wxT("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() );
685 wxString
wxMenuItem::GetItemLabel() const
687 wxString label
= wxConvertMnemonicsFromGTK(m_text
);
688 if (!m_hotKey
.IsEmpty())
689 label
<< "\t" << m_hotKey
;
693 void wxMenuItem::SetItemLabel( const wxString
& str
)
695 // cache some data which must be used later
696 bool isstock
= wxIsStockID(GetId());
697 const char *stockid
= NULL
;
699 stockid
= wxGetStockGtkID(GetId());
701 // Some optimization to avoid flicker
702 wxString oldLabel
= m_text
;
703 oldLabel
= wxStripMenuCodes(oldLabel
);
704 oldLabel
.Replace(wxT("_"), wxT(""));
705 wxString label1
= wxStripMenuCodes(str
);
707 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
708 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
709 #endif // wxUSE_ACCEL
714 if (oldLabel
== label1
&&
715 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
722 label
= (GtkLabel
*) m_labelWidget
;
724 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
726 // stock menu items can have empty labels:
727 wxString text
= m_text
;
728 if (text
.IsEmpty() && !IsSeparator())
730 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
731 text
= wxGetStockLabel(GetId());
733 // need & => _ conversion
734 text
= GTKProcessMenuItemLabel(text
, NULL
);
737 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(text
) );
740 // remove old accelerator from our parent's accelerator group, if present
742 GdkModifierType accel_mods
;
743 if (oldbuf
[(size_t)0] != '\0')
745 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
748 gtk_widget_remove_accelerator(m_menuItem
,
749 m_parentMenu
->m_accel
,
756 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
757 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
758 gtk_widget_remove_accelerator( m_menuItem
,
759 m_parentMenu
->m_accel
,
764 // add new accelerator to our parent's accelerator group
765 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
766 if (buf
[(size_t)0] != '\0')
768 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
771 gtk_widget_add_accelerator( m_menuItem
,
773 m_parentMenu
->m_accel
,
781 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
782 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
783 gtk_widget_remove_accelerator( m_menuItem
,
784 m_parentMenu
->m_accel
,
788 #endif // wxUSE_ACCEL
791 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
792 // implemented in control.cpp and from wxMenuItemBase::GetLabelText...
793 // so there's no real code duplication
794 wxString
wxMenuItem::GTKProcessMenuItemLabel(const wxString
& str
, wxString
*hotKey
)
798 // '\t' is the deliminator indicating a hot key
799 wxString::const_iterator pc
= str
.begin();
800 while ( pc
!= str
.end() && *pc
!= wxT('\t') )
804 wxString::const_iterator next
= pc
+ 1;
805 if (next
!= str
.end() && *next
== wxT('&'))
807 // "&" is doubled to indicate "&" instead of accelerator
816 else if ( *pc
== wxT('_') ) // escape underscores
833 hotKey
->assign(pc
, str
.end());
840 // it's valid for this function to be called even if m_menuItem == NULL
841 void wxMenuItem::DoSetText( const wxString
& str
)
844 m_text
= GTKProcessMenuItemLabel(str
, &m_hotKey
);
849 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
857 // accelerator parsing code looks for them after a TAB, so insert a dummy
860 label
<< wxT('\t') << GetHotKey();
862 return wxAcceleratorEntry::Create(label
);
865 #endif // wxUSE_ACCEL
867 void wxMenuItem::Check( bool check
)
869 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
871 if (check
== m_isChecked
)
874 wxMenuItemBase::Check( check
);
880 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
884 wxFAIL_MSG( _T("can't check this item") );
888 void wxMenuItem::Enable( bool enable
)
890 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
892 gtk_widget_set_sensitive( m_menuItem
, enable
);
893 wxMenuItemBase::Enable( enable
);
896 bool wxMenuItem::IsChecked() const
898 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
900 wxCHECK_MSG( IsCheckable(), false,
901 wxT("can't get state of uncheckable item!") );
903 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
906 //-----------------------------------------------------------------------------
908 //-----------------------------------------------------------------------------
912 static void menu_map(GtkWidget
*, wxMenu
* menu
)
914 wxMenuEvent
event(wxEVT_MENU_OPEN
, menu
->m_popupShown
? -1 : 0, menu
);
915 DoCommonMenuCallbackCode(menu
, event
);
918 // "hide" from m_menu
919 static void menu_hide(GtkWidget
*, wxMenu
* menu
)
921 wxMenuEvent
event(wxEVT_MENU_CLOSE
, menu
->m_popupShown
? -1 : 0, menu
);
922 menu
->m_popupShown
= false;
923 DoCommonMenuCallbackCode(menu
, event
);
927 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
931 m_popupShown
= false;
933 m_accel
= gtk_accel_group_new();
934 m_menu
= gtk_menu_new();
935 // NB: keep reference to the menu so that it is not destroyed behind
936 // our back by GTK+ e.g. when it is removed from menubar:
937 g_object_ref(m_menu
);
938 gtk_object_sink(GTK_OBJECT(m_menu
));
940 m_owner
= (GtkWidget
*) NULL
;
942 // Tearoffs are entries, just like separators. So if we want this
943 // menu to be a tear-off one, we just append a tearoff entry
945 if ( m_style
& wxMENU_TEAROFF
)
947 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
949 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
954 // append the title as the very first entry if we have it
955 if ( !m_title
.empty() )
957 Append(wxGTK_TITLE_ID
, m_title
);
961 // "show" occurs for sub-menus which are not showing, so use "map" instead
962 g_signal_connect(m_menu
, "map", G_CALLBACK(menu_map
), this);
963 g_signal_connect(m_menu
, "hide", G_CALLBACK(menu_hide
), this);
969 g_object_unref(m_menu
);
971 // if the menu is inserted in another menu at this time, there was
972 // one more reference to it:
974 gtk_widget_destroy(m_menu
);
976 g_object_unref(m_accel
);
979 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir
)
982 wxWindow::GTKSetLayout(m_owner
, dir
);
983 //else: will be called later by wxMenuBar again
986 wxLayoutDirection
wxMenu::GetLayoutDirection() const
988 return wxWindow::GTKGetLayout(m_owner
);
991 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
995 // cache some data used later
996 wxString text
= mitem
->wxMenuItemBase::GetItemLabel();
997 int id
= mitem
->GetId();
998 bool isstock
= wxIsStockID(id
);
999 const char *stockid
= NULL
;
1001 stockid
= wxGetStockGtkID(mitem
->GetId());
1003 // stock menu items can have an empty label
1004 if (text
.IsEmpty() && !mitem
->IsSeparator())
1006 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
1007 text
= wxGetStockLabel(id
);
1009 // need & => _ conversion
1010 text
= wxMenuItem::GTKProcessMenuItemLabel(text
, NULL
);
1013 if ( mitem
->IsSeparator() )
1015 menuItem
= gtk_separator_menu_item_new();
1018 else if ( mitem
->GetBitmap().Ok() ||
1019 (mitem
->GetKind() == wxITEM_NORMAL
&& isstock
) )
1021 wxBitmap
bitmap(mitem
->GetBitmap());
1023 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1028 // use stock bitmap for this item if available on the assumption
1029 // that it never hurts to follow GTK+ conventions more closely
1030 image
= stockid
? gtk_image_new_from_stock(stockid
, GTK_ICON_SIZE_MENU
)
1033 else // we have a custom bitmap
1035 wxASSERT_MSG( mitem
->GetKind() == wxITEM_NORMAL
,
1036 _T("only normal menu items can have bitmaps") );
1038 if ( bitmap
.HasPixbuf() )
1040 image
= gtk_image_new_from_pixbuf(bitmap
.GetPixbuf());
1044 GdkPixmap
*gdk_pixmap
= bitmap
.GetPixmap();
1045 GdkBitmap
*gdk_bitmap
= bitmap
.GetMask() ?
1046 bitmap
.GetMask()->GetBitmap() :
1048 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1054 gtk_widget_show(image
);
1056 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1061 else // a normal item
1063 // NB: 'text' variable has "_" instead of "&" after mitem->SetItemLabel()
1066 switch ( mitem
->GetKind() )
1070 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1077 GSList
*group
= NULL
;
1078 if ( m_prevRadio
== NULL
)
1080 // start of a new radio group
1081 m_prevRadio
= menuItem
=
1082 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1084 else // continue the radio group
1086 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1087 m_prevRadio
= menuItem
=
1088 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1094 wxFAIL_MSG( _T("unexpected menu item kind") );
1099 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1109 GdkModifierType accel_mods
;
1110 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1112 if (buf
[(size_t)0] != '\0')
1114 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1117 gtk_widget_add_accelerator (menuItem
,
1127 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1128 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
1129 gtk_widget_add_accelerator( menuItem
,
1136 #endif // wxUSE_ACCEL
1139 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1141 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1143 gtk_widget_show( menuItem
);
1145 if ( !mitem
->IsSeparator() )
1147 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1149 g_signal_connect (menuItem
, "select",
1150 G_CALLBACK (gtk_menu_hilight_callback
), this);
1151 g_signal_connect (menuItem
, "deselect",
1152 G_CALLBACK (gtk_menu_nolight_callback
), this);
1154 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1156 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1158 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1160 // if adding a submenu to a menu already existing in the menu bar, we
1161 // must set invoking window to allow processing events from this
1163 if ( m_invokingWindow
)
1164 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1168 g_signal_connect (menuItem
, "activate",
1169 G_CALLBACK (gtk_menu_clicked_callback
),
1174 mitem
->SetMenuItem(menuItem
);
1178 // This doesn't even exist!
1179 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1185 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1187 if (!GtkAppend(mitem
))
1190 return wxMenuBase::DoAppend(mitem
);
1193 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1195 if ( !wxMenuBase::DoInsert(pos
, item
) )
1199 if ( !GtkAppend(item
, (int)pos
) )
1205 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1207 if ( !wxMenuBase::DoRemove(item
) )
1210 GtkWidget
* const mitem
= item
->GetMenuItem();
1211 if ( m_prevRadio
== mitem
)
1213 // deleting an item starts a new radio group (has to as we shouldn't
1214 // keep a deleted pointer anyhow)
1218 // TODO: this code doesn't delete the item factory item and this seems
1219 // impossible as of GTK 1.2.6.
1220 gtk_widget_destroy( mitem
);
1225 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1227 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1230 wxMenuItem
*item
= node
->GetData();
1231 if (item
->GetMenuItem() == menuItem
)
1232 return item
->GetId();
1233 node
= node
->GetNext();
1239 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1241 wxMenuBase::Attach(menubar
);
1243 // inherit layout direction from menubar.
1244 SetLayoutDirection(menubar
->GetLayoutDirection());
1247 // ----------------------------------------------------------------------------
1249 // ----------------------------------------------------------------------------
1253 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1257 wxAcceleratorEntry
*accel
= item
.GetAccel();
1260 int flags
= accel
->GetFlags();
1261 if ( flags
& wxACCEL_ALT
)
1262 hotkey
+= wxT("<alt>");
1263 if ( flags
& wxACCEL_CTRL
)
1264 hotkey
+= wxT("<control>");
1265 if ( flags
& wxACCEL_SHIFT
)
1266 hotkey
+= wxT("<shift>");
1268 int code
= accel
->GetKeyCode();
1295 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1298 // TODO: we should use gdk_keyval_name() (a.k.a.
1299 // XKeysymToString) here as well as hardcoding the keysym
1300 // names this might be not portable
1302 hotkey
<< wxT("Insert" );
1305 hotkey
<< wxT("Delete" );
1308 hotkey
<< wxT("Up" );
1311 hotkey
<< wxT("Down" );
1314 hotkey
<< wxT("Page_Up" );
1317 hotkey
<< wxT("Page_Down" );
1320 hotkey
<< wxT("Left" );
1323 hotkey
<< wxT("Right" );
1326 hotkey
<< wxT("Home" );
1329 hotkey
<< wxT("End" );
1332 hotkey
<< wxT("Return" );
1335 hotkey
<< wxT("BackSpace" );
1338 hotkey
<< wxT("Tab" );
1341 hotkey
<< wxT("Esc" );
1344 hotkey
<< wxT("space" );
1347 hotkey
<< wxT("Multiply" );
1350 hotkey
<< wxT("Add" );
1353 hotkey
<< wxT("Separator" );
1356 hotkey
<< wxT("Subtract" );
1359 hotkey
<< wxT("Decimal" );
1362 hotkey
<< wxT("Divide" );
1365 hotkey
<< wxT("Cancel" );
1368 hotkey
<< wxT("Clear" );
1371 hotkey
<< wxT("Menu" );
1374 hotkey
<< wxT("Pause" );
1377 hotkey
<< wxT("Capital" );
1380 hotkey
<< wxT("Select" );
1383 hotkey
<< wxT("Print" );
1386 hotkey
<< wxT("Execute" );
1389 hotkey
<< wxT("Snapshot" );
1392 hotkey
<< wxT("Help" );
1395 hotkey
<< wxT("Num_Lock" );
1398 hotkey
<< wxT("Scroll_Lock" );
1400 case WXK_NUMPAD_INSERT
:
1401 hotkey
<< wxT("KP_Insert" );
1403 case WXK_NUMPAD_DELETE
:
1404 hotkey
<< wxT("KP_Delete" );
1406 case WXK_NUMPAD_SPACE
:
1407 hotkey
<< wxT("KP_Space" );
1409 case WXK_NUMPAD_TAB
:
1410 hotkey
<< wxT("KP_Tab" );
1412 case WXK_NUMPAD_ENTER
:
1413 hotkey
<< wxT("KP_Enter" );
1415 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1417 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1419 case WXK_NUMPAD_HOME
:
1420 hotkey
<< wxT("KP_Home" );
1422 case WXK_NUMPAD_LEFT
:
1423 hotkey
<< wxT("KP_Left" );
1426 hotkey
<< wxT("KP_Up" );
1428 case WXK_NUMPAD_RIGHT
:
1429 hotkey
<< wxT("KP_Right" );
1431 case WXK_NUMPAD_DOWN
:
1432 hotkey
<< wxT("KP_Down" );
1434 case WXK_NUMPAD_PAGEUP
:
1435 hotkey
<< wxT("KP_Page_Up" );
1437 case WXK_NUMPAD_PAGEDOWN
:
1438 hotkey
<< wxT("KP_Page_Down" );
1440 case WXK_NUMPAD_END
:
1441 hotkey
<< wxT("KP_End" );
1443 case WXK_NUMPAD_BEGIN
:
1444 hotkey
<< wxT("KP_Begin" );
1446 case WXK_NUMPAD_EQUAL
:
1447 hotkey
<< wxT("KP_Equal" );
1449 case WXK_NUMPAD_MULTIPLY
:
1450 hotkey
<< wxT("KP_Multiply" );
1452 case WXK_NUMPAD_ADD
:
1453 hotkey
<< wxT("KP_Add" );
1455 case WXK_NUMPAD_SEPARATOR
:
1456 hotkey
<< wxT("KP_Separator" );
1458 case WXK_NUMPAD_SUBTRACT
:
1459 hotkey
<< wxT("KP_Subtract" );
1461 case WXK_NUMPAD_DECIMAL
:
1462 hotkey
<< wxT("KP_Decimal" );
1464 case WXK_NUMPAD_DIVIDE
:
1465 hotkey
<< wxT("KP_Divide" );
1467 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1468 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1469 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1470 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1472 case WXK_WINDOWS_LEFT
:
1473 hotkey
<< wxT("Super_L" );
1475 case WXK_WINDOWS_RIGHT
:
1476 hotkey
<< wxT("Super_R" );
1478 case WXK_WINDOWS_MENU
:
1479 hotkey
<< wxT("Menu" );
1482 hotkey
<< wxT("Command" );
1484 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1485 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1486 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1487 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1488 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1489 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1490 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1493 // if there are any other keys wxAcceleratorEntry::Create() may
1494 // return, we should process them here
1500 name
= wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint
)code
));
1501 if ( !name
.empty() )
1508 wxFAIL_MSG( wxT("unknown keyboard accel") );
1517 #endif // wxUSE_ACCEL
1519 const char *wxGetStockGtkID(wxWindowID id
)
1521 #define STOCKITEM(wx,gtk) \
1525 #define STOCKITEM_MISSING(wx) \
1529 #if GTK_CHECK_VERSION(2,4,0)
1530 #define STOCKITEM_24(wx,gtk) STOCKITEM(wx,gtk)
1532 #define STOCKITEM_24(wx,gtk) STOCKITEM_MISSING(wx)
1535 #if GTK_CHECK_VERSION(2,6,0)
1536 #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk)
1538 #define STOCKITEM_26(wx,gtk) STOCKITEM_MISSING(wx)
1541 #if GTK_CHECK_VERSION(2,10,0)
1542 #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk)
1544 #define STOCKITEM_210(wx,gtk) STOCKITEM_MISSING(wx)
1550 STOCKITEM_26(wxID_ABOUT
, GTK_STOCK_ABOUT
)
1551 STOCKITEM(wxID_ADD
, GTK_STOCK_ADD
)
1552 STOCKITEM(wxID_APPLY
, GTK_STOCK_APPLY
)
1553 STOCKITEM(wxID_BOLD
, GTK_STOCK_BOLD
)
1554 STOCKITEM(wxID_CANCEL
, GTK_STOCK_CANCEL
)
1555 STOCKITEM(wxID_CLEAR
, GTK_STOCK_CLEAR
)
1556 STOCKITEM(wxID_CLOSE
, GTK_STOCK_CLOSE
)
1557 STOCKITEM(wxID_COPY
, GTK_STOCK_COPY
)
1558 STOCKITEM(wxID_CUT
, GTK_STOCK_CUT
)
1559 STOCKITEM(wxID_DELETE
, GTK_STOCK_DELETE
)
1560 STOCKITEM_26(wxID_EDIT
, GTK_STOCK_EDIT
)
1561 STOCKITEM(wxID_FIND
, GTK_STOCK_FIND
)
1562 STOCKITEM_26(wxID_FILE
, GTK_STOCK_FILE
)
1563 STOCKITEM(wxID_REPLACE
, GTK_STOCK_FIND_AND_REPLACE
)
1564 STOCKITEM(wxID_BACKWARD
, GTK_STOCK_GO_BACK
)
1565 STOCKITEM(wxID_DOWN
, GTK_STOCK_GO_DOWN
)
1566 STOCKITEM(wxID_FORWARD
, GTK_STOCK_GO_FORWARD
)
1567 STOCKITEM(wxID_UP
, GTK_STOCK_GO_UP
)
1568 STOCKITEM(wxID_HELP
, GTK_STOCK_HELP
)
1569 STOCKITEM(wxID_HOME
, GTK_STOCK_HOME
)
1570 STOCKITEM_24(wxID_INDENT
, GTK_STOCK_INDENT
)
1571 STOCKITEM(wxID_INDEX
, GTK_STOCK_INDEX
)
1572 STOCKITEM(wxID_ITALIC
, GTK_STOCK_ITALIC
)
1573 STOCKITEM(wxID_JUSTIFY_CENTER
, GTK_STOCK_JUSTIFY_CENTER
)
1574 STOCKITEM(wxID_JUSTIFY_FILL
, GTK_STOCK_JUSTIFY_FILL
)
1575 STOCKITEM(wxID_JUSTIFY_LEFT
, GTK_STOCK_JUSTIFY_LEFT
)
1576 STOCKITEM(wxID_JUSTIFY_RIGHT
, GTK_STOCK_JUSTIFY_RIGHT
)
1577 STOCKITEM(wxID_NEW
, GTK_STOCK_NEW
)
1578 STOCKITEM(wxID_NO
, GTK_STOCK_NO
)
1579 STOCKITEM(wxID_OK
, GTK_STOCK_OK
)
1580 STOCKITEM(wxID_OPEN
, GTK_STOCK_OPEN
)
1581 STOCKITEM(wxID_PASTE
, GTK_STOCK_PASTE
)
1582 STOCKITEM(wxID_PREFERENCES
, GTK_STOCK_PREFERENCES
)
1583 STOCKITEM(wxID_PRINT
, GTK_STOCK_PRINT
)
1584 STOCKITEM(wxID_PREVIEW
, GTK_STOCK_PRINT_PREVIEW
)
1585 STOCKITEM(wxID_PROPERTIES
, GTK_STOCK_PROPERTIES
)
1586 STOCKITEM(wxID_EXIT
, GTK_STOCK_QUIT
)
1587 STOCKITEM(wxID_REDO
, GTK_STOCK_REDO
)
1588 STOCKITEM(wxID_REFRESH
, GTK_STOCK_REFRESH
)
1589 STOCKITEM(wxID_REMOVE
, GTK_STOCK_REMOVE
)
1590 STOCKITEM(wxID_REVERT_TO_SAVED
, GTK_STOCK_REVERT_TO_SAVED
)
1591 STOCKITEM(wxID_SAVE
, GTK_STOCK_SAVE
)
1592 STOCKITEM(wxID_SAVEAS
, GTK_STOCK_SAVE_AS
)
1593 STOCKITEM_210(wxID_SELECTALL
, GTK_STOCK_SELECT_ALL
)
1594 STOCKITEM(wxID_STOP
, GTK_STOCK_STOP
)
1595 STOCKITEM(wxID_UNDELETE
, GTK_STOCK_UNDELETE
)
1596 STOCKITEM(wxID_UNDERLINE
, GTK_STOCK_UNDERLINE
)
1597 STOCKITEM(wxID_UNDO
, GTK_STOCK_UNDO
)
1598 STOCKITEM_24(wxID_UNINDENT
, GTK_STOCK_UNINDENT
)
1599 STOCKITEM(wxID_YES
, GTK_STOCK_YES
)
1600 STOCKITEM(wxID_ZOOM_100
, GTK_STOCK_ZOOM_100
)
1601 STOCKITEM(wxID_ZOOM_FIT
, GTK_STOCK_ZOOM_FIT
)
1602 STOCKITEM(wxID_ZOOM_IN
, GTK_STOCK_ZOOM_IN
)
1603 STOCKITEM(wxID_ZOOM_OUT
, GTK_STOCK_ZOOM_OUT
)
1606 wxFAIL_MSG( _T("invalid stock item ID") );
1617 bool wxGetStockGtkAccelerator(const char *id
, GdkModifierType
*mod
, guint
*key
)
1622 GtkStockItem stock_item
;
1623 if (gtk_stock_lookup (id
, &stock_item
))
1625 if (key
) *key
= stock_item
.keyval
;
1626 if (mod
) *mod
= stock_item
.modifier
;
1628 // some GTK stock items have zero values for the keyval;
1629 // it means that they do not have an accelerator...
1630 if (stock_item
.keyval
)
1636 #endif // wxUSE_ACCEL
1638 #endif // wxUSE_MENUS