1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
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"
19 #include "wx/bitmap.h"
24 #include "wx/stockitem.h"
25 #include "wx/gtk/private.h"
27 // FIXME: is this right? somehow I don't think so (VZ)
29 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
30 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
31 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
33 #define ACCEL_OBJECT GtkWindow
34 #define ACCEL_OBJECTS(a) (a)->acceleratables
35 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
37 // we use normal item but with a special id for the menu title
38 static const int wxGTK_TITLE_ID
= -3;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 static wxString
wxReplaceUnderscore( const wxString
& title
)
56 // GTK 1.2 wants to have "_" instead of "&" for accelerators
59 while (*pc
!= wxT('\0'))
61 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
63 // "&" is doubled to indicate "&" instead of accelerator
67 else if (*pc
== wxT('&'))
73 if ( *pc
== wxT('_') )
75 // underscores must be doubled to prevent them from being
76 // interpreted as accelerator character prefix by GTK
85 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
90 //-----------------------------------------------------------------------------
91 // activate message from GTK
92 //-----------------------------------------------------------------------------
94 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
97 wxapp_install_idle_handler();
99 event
.SetEventObject( menu
);
101 wxEvtHandler
* handler
= menu
->GetEventHandler();
102 if (handler
&& handler
->ProcessEvent(event
))
105 wxWindow
*win
= menu
->GetInvokingWindow();
107 win
->GetEventHandler()->ProcessEvent( event
);
112 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
114 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
116 DoCommonMenuCallbackCode(menu
, event
);
119 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
121 if ( !menubar
->GetMenuCount() )
123 // if menubar is empty we can't call GetMenu(0) below
127 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
129 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
140 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
142 // the parent window is known after wxFrame::SetMenu()
143 m_needParent
= false;
145 m_invokingWindow
= (wxWindow
*) NULL
;
147 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
148 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
150 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
154 m_menubar
= gtk_menu_bar_new();
156 if (style
& wxMB_DOCKABLE
)
158 m_widget
= gtk_handle_box_new();
159 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
160 gtk_widget_show( GTK_WIDGET(m_menubar
) );
164 m_widget
= GTK_WIDGET(m_menubar
);
171 for (size_t i
= 0; i
< n
; ++i
)
172 Append(menus
[i
], titles
[i
]);
174 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
175 // don't get it when the menu is dismissed by clicking outside the
176 // toolbar) so we connect to the global one, even if it means that we
177 // can't pass the menu which was closed in wxMenuEvent object
178 g_signal_connect (m_menubar
, "deactivate",
179 G_CALLBACK (gtk_menu_close_callback
), this);
183 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
185 Init(n
, menus
, titles
, style
);
188 wxMenuBar::wxMenuBar(long style
)
190 Init(0, NULL
, NULL
, style
);
193 wxMenuBar::wxMenuBar()
195 Init(0, NULL
, NULL
, 0);
198 wxMenuBar::~wxMenuBar()
202 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
204 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
206 wxWindow
*top_frame
= win
;
207 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
208 top_frame
= top_frame
->GetParent();
210 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
213 wxMenuItem
*menuitem
= node
->GetData();
214 if (menuitem
->IsSubMenu())
215 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
216 node
= node
->GetNext();
220 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
222 menu
->SetInvokingWindow( win
);
224 wxWindow
*top_frame
= win
;
225 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
226 top_frame
= top_frame
->GetParent();
228 // support for native hot keys
229 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
230 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
231 gtk_accel_group_attach( menu
->m_accel
, obj
);
233 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
236 wxMenuItem
*menuitem
= node
->GetData();
237 if (menuitem
->IsSubMenu())
238 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
239 node
= node
->GetNext();
243 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
245 m_invokingWindow
= win
;
246 wxWindow
*top_frame
= win
;
247 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
248 top_frame
= top_frame
->GetParent();
250 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
253 wxMenu
*menu
= node
->GetData();
254 wxMenubarSetInvokingWindow( menu
, win
);
255 node
= node
->GetNext();
259 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir
)
261 if ( dir
== wxLayout_Default
)
263 const wxWindow
*const frame
= GetFrame();
266 // inherit layout from frame.
267 dir
= frame
->GetLayoutDirection();
269 else // use global layout
271 dir
= wxTheApp
->GetLayoutDirection();
275 if ( dir
== wxLayout_Default
)
278 GTKSetLayout(m_menubar
, dir
);
280 // also set the layout of all menus we already have (new ones will inherit
281 // the current layout)
282 for ( wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
284 node
= node
->GetNext() )
286 wxMenu
*const menu
= node
->GetData();
287 menu
->SetLayoutDirection(dir
);
291 wxLayoutDirection
wxMenuBar::GetLayoutDirection() const
293 return GTKGetLayout(m_menubar
);
296 void wxMenuBar::Attach(wxFrame
*frame
)
298 wxMenuBarBase::Attach(frame
);
300 SetLayoutDirection(wxLayout_Default
);
303 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
305 m_invokingWindow
= (wxWindow
*) NULL
;
306 wxWindow
*top_frame
= win
;
307 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
308 top_frame
= top_frame
->GetParent();
310 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
313 wxMenu
*menu
= node
->GetData();
314 wxMenubarUnsetInvokingWindow( menu
, win
);
315 node
= node
->GetNext();
319 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
321 if ( !wxMenuBarBase::Append( menu
, title
) )
324 return GtkAppend(menu
, title
);
327 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
329 wxString
str( wxReplaceUnderscore( title
) );
331 // This doesn't have much effect right now.
332 menu
->SetTitle( str
);
334 // The "m_owner" is the "menu item"
335 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
336 menu
->SetLayoutDirection(GetLayoutDirection());
338 gtk_widget_show( menu
->m_owner
);
340 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
343 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
345 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
347 g_signal_connect (menu
->m_owner
, "activate",
348 G_CALLBACK (gtk_menu_open_callback
),
351 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
352 // addings menu later on.
353 if (m_invokingWindow
)
355 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
357 // OPTIMISE ME: we should probably cache this, or pass it
358 // directly, but for now this is a minimal
359 // change to validate the new dynamic sizing.
360 // see (and refactor :) similar code in Remove
363 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
366 frame
->UpdateMenuBarSize();
372 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
374 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
379 if ( !GtkAppend(menu
, title
, (int)pos
) )
385 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
387 // remove the old item and insert a new one
388 wxMenu
*menuOld
= Remove(pos
);
389 if ( menuOld
&& !Insert(pos
, menu
, title
) )
391 return (wxMenu
*) NULL
;
394 // either Insert() succeeded or Remove() failed and menuOld is NULL
398 wxMenu
*wxMenuBar::Remove(size_t pos
)
400 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
402 return (wxMenu
*) NULL
;
404 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
405 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
407 gtk_widget_destroy( menu
->m_owner
);
408 menu
->m_owner
= NULL
;
410 if (m_invokingWindow
)
412 // OPTIMISE ME: see comment in GtkAppend
413 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
416 frame
->UpdateMenuBarSize();
422 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
424 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
426 int res
= menu
->FindItem( itemString
);
427 if (res
!= wxNOT_FOUND
)
431 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
434 wxMenuItem
*item
= node
->GetData();
435 if (item
->IsSubMenu())
436 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
438 node
= node
->GetNext();
444 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
446 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
449 wxMenu
*menu
= node
->GetData();
450 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
453 node
= node
->GetNext();
459 // Find a wxMenuItem using its id. Recurses down into sub-menus
460 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
462 wxMenuItem
* result
= menu
->FindChildItem(id
);
464 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
465 while ( node
&& result
== NULL
)
467 wxMenuItem
*item
= node
->GetData();
468 if (item
->IsSubMenu())
470 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
472 node
= node
->GetNext();
478 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
480 wxMenuItem
* result
= 0;
481 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
482 while (node
&& result
== 0)
484 wxMenu
*menu
= node
->GetData();
485 result
= FindMenuItemByIdRecursive( menu
, id
);
486 node
= node
->GetNext();
491 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
497 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
499 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
501 wxCHECK_RET( node
, wxT("menu not found") );
503 wxMenu
* menu
= node
->GetData();
506 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
509 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
511 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
513 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
515 wxMenu
* menu
= node
->GetData();
518 wxString
text( menu
->GetTitle() );
519 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
521 if ( *pc
== wxT('_') )
523 // '_' is the escape character for GTK+
527 // don't remove ampersands '&' since if we have them in the menu title
528 // it means that they were doubled to indicate "&" instead of accelerator
536 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
538 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
540 wxCHECK_RET( node
, wxT("menu not found") );
542 wxMenu
* menu
= node
->GetData();
544 const wxString
str( wxReplaceUnderscore( label
) );
546 menu
->SetTitle( str
);
549 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
552 //-----------------------------------------------------------------------------
554 //-----------------------------------------------------------------------------
557 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
560 wxapp_install_idle_handler();
562 int id
= menu
->FindMenuIdByMenuItem(widget
);
564 /* should find it for normal (not popup) menu */
565 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
566 _T("menu item not found in gtk_menu_clicked_callback") );
568 if (!menu
->IsEnabled(id
))
571 wxMenuItem
* item
= menu
->FindChildItem( id
);
572 wxCHECK_RET( item
, wxT("error in menu item callback") );
574 if ( item
->GetId() == wxGTK_TITLE_ID
)
576 // ignore events from the menu title
580 if (item
->IsCheckable())
582 bool isReallyChecked
= item
->IsChecked(),
583 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
585 // ensure that the internal state is always consistent with what is
586 // shown on the screen
587 item
->wxMenuItemBase::Check(isReallyChecked
);
589 // we must not report the events for the radio button going up nor the
590 // events resulting from the calls to wxMenuItem::Check()
591 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
592 (isInternallyChecked
== isReallyChecked
) )
599 // Is this menu on a menubar? (possibly nested)
600 wxFrame
* frame
= NULL
;
601 if(menu
->IsAttached())
602 frame
= menu
->GetMenuBar()->GetFrame();
604 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
605 // normally wxMenu::SendEvent() should be enough, if it doesn't work
606 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
607 // should be fixed instead of working around it here...
610 // If it is attached then let the frame send the event.
611 // Don't call frame->ProcessCommand(id) because it toggles
612 // checkable items and we've already done that above.
613 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
614 commandEvent
.SetEventObject(frame
);
615 if (item
->IsCheckable())
616 commandEvent
.SetInt(item
->IsChecked());
617 commandEvent
.SetEventObject(menu
);
619 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
623 // otherwise let the menu have it
624 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
629 //-----------------------------------------------------------------------------
631 //-----------------------------------------------------------------------------
634 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
636 if (g_isIdle
) wxapp_install_idle_handler();
638 int id
= menu
->FindMenuIdByMenuItem(widget
);
640 wxASSERT( id
!= -1 ); // should find it!
642 if (!menu
->IsEnabled(id
))
645 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
646 event
.SetEventObject( menu
);
648 wxEvtHandler
* handler
= menu
->GetEventHandler();
649 if (handler
&& handler
->ProcessEvent(event
))
652 wxWindow
*win
= menu
->GetInvokingWindow();
653 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
657 //-----------------------------------------------------------------------------
659 //-----------------------------------------------------------------------------
662 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
664 if (g_isIdle
) wxapp_install_idle_handler();
666 int id
= menu
->FindMenuIdByMenuItem(widget
);
668 wxASSERT( id
!= -1 ); // should find it!
670 if (!menu
->IsEnabled(id
))
673 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
674 event
.SetEventObject( menu
);
676 wxEvtHandler
* handler
= menu
->GetEventHandler();
677 if (handler
&& handler
->ProcessEvent(event
))
680 wxWindow
*win
= menu
->GetInvokingWindow();
682 win
->GetEventHandler()->ProcessEvent( event
);
686 //-----------------------------------------------------------------------------
688 //-----------------------------------------------------------------------------
690 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
692 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
694 const wxString
& name
,
695 const wxString
& help
,
699 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
702 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
704 const wxString
& text
,
705 const wxString
& help
,
708 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
713 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
715 const wxString
& text
,
716 const wxString
& help
,
719 : wxMenuItemBase(parentMenu
, id
, text
, help
,
720 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
725 void wxMenuItem::Init(const wxString
& text
)
727 m_labelWidget
= (GtkWidget
*) NULL
;
728 m_menuItem
= (GtkWidget
*) NULL
;
733 wxMenuItem::~wxMenuItem()
735 // don't delete menu items, the menus take care of that
738 // return the menu item text without any menu accels
740 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
744 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
746 if ( *pc
== wxT('\t'))
749 if ( *pc
== wxT('_') )
751 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
757 if ( *pc
== wxT('\\') )
759 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
765 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
768 // "&" is doubled to indicate "&" instead of accelerator
775 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
780 void wxMenuItem::SetText( const wxString
& str
)
782 // cache some data which must be used later
783 bool isstock
= wxIsStockID(GetId());
784 const char *stockid
= NULL
;
786 stockid
= wxGetStockGtkID(GetId());
788 // Some optimization to avoid flicker
789 wxString oldLabel
= m_text
;
790 oldLabel
= wxStripMenuCodes(oldLabel
);
791 oldLabel
.Replace(wxT("_"), wxT(""));
792 wxString label1
= wxStripMenuCodes(str
);
793 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
794 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
798 if (oldLabel
== label1
&&
799 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
806 label
= (GtkLabel
*) m_labelWidget
;
808 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
810 // stock menu items can have empty labels:
811 wxString text
= m_text
;
812 if (text
.IsEmpty() && !IsSeparator())
814 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
815 text
= wxGetStockLabel(GetId());
817 // need & => _ conversion
818 text
= GTKProcessMenuItemLabel(text
, NULL
);
821 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(text
) );
824 // remove old accelerator from our parent's accelerator group, if present
826 GdkModifierType accel_mods
;
827 if (oldbuf
[(size_t)0] != '\0')
829 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
832 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
833 m_parentMenu
->m_accel
,
840 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
841 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
842 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
843 m_parentMenu
->m_accel
,
848 // add new accelerator to our parent's accelerator group
849 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
850 if (buf
[(size_t)0] != '\0')
852 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
855 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
857 m_parentMenu
->m_accel
,
865 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
866 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
867 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
868 m_parentMenu
->m_accel
,
874 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
875 // implemented in control.cpp and from wxMenuItemBase::GetLabelFromText...
876 // so there's no real code duplication
877 wxString
wxMenuItem::GTKProcessMenuItemLabel(const wxString
& str
, wxString
*hotKey
)
881 // '\t' is the deliminator indicating a hot key
882 const wxChar
*pc
= str
;
883 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
885 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
887 // "&" is doubled to indicate "&" instead of accelerator
891 else if (*pc
== wxT('&'))
895 else if ( *pc
== wxT('_') ) // escape underscores
919 // it's valid for this function to be called even if m_menuItem == NULL
920 void wxMenuItem::DoSetText( const wxString
& str
)
923 m_text
= GTKProcessMenuItemLabel(str
, &m_hotKey
);
928 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
936 // accelerator parsing code looks for them after a TAB, so insert a dummy
939 label
<< wxT('\t') << GetHotKey();
941 return wxAcceleratorEntry::Create(label
);
944 #endif // wxUSE_ACCEL
946 void wxMenuItem::Check( bool check
)
948 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
950 if (check
== m_isChecked
)
953 wxMenuItemBase::Check( check
);
959 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
963 wxFAIL_MSG( _T("can't check this item") );
967 void wxMenuItem::Enable( bool enable
)
969 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
971 gtk_widget_set_sensitive( m_menuItem
, enable
);
972 wxMenuItemBase::Enable( enable
);
975 bool wxMenuItem::IsChecked() const
977 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
979 wxCHECK_MSG( IsCheckable(), false,
980 wxT("can't get state of uncheckable item!") );
982 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
985 //-----------------------------------------------------------------------------
987 //-----------------------------------------------------------------------------
989 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
993 m_accel
= gtk_accel_group_new();
994 m_menu
= gtk_menu_new();
995 // NB: keep reference to the menu so that it is not destroyed behind
996 // our back by GTK+ e.g. when it is removed from menubar:
997 gtk_widget_ref(m_menu
);
999 m_owner
= (GtkWidget
*) NULL
;
1001 // Tearoffs are entries, just like separators. So if we want this
1002 // menu to be a tear-off one, we just append a tearoff entry
1004 if ( m_style
& wxMENU_TEAROFF
)
1006 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
1008 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
1013 // append the title as the very first entry if we have it
1014 if ( !m_title
.empty() )
1016 Append(wxGTK_TITLE_ID
, m_title
);
1023 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
1025 if ( GTK_IS_WIDGET( m_menu
))
1028 gtk_widget_unref( m_menu
);
1029 // if the menu is inserted in another menu at this time, there was
1030 // one more reference to it:
1032 gtk_widget_destroy( m_menu
);
1036 void wxMenu::SetLayoutDirection(const wxLayoutDirection dir
)
1039 wxWindow::GTKSetLayout(m_owner
, dir
);
1040 //else: will be called later by wxMenuBar again
1043 wxLayoutDirection
wxMenu::GetLayoutDirection() const
1045 return wxWindow::GTKGetLayout(m_owner
);
1048 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
1050 GtkWidget
*menuItem
;
1052 // cache some data used later
1053 wxString text
= mitem
->GetText();
1054 int id
= mitem
->GetId();
1055 bool isstock
= wxIsStockID(id
);
1056 const char *stockid
= NULL
;
1058 stockid
= wxGetStockGtkID(mitem
->GetId());
1060 // stock menu items can have an empty label
1061 if (text
.IsEmpty() && !mitem
->IsSeparator())
1063 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
1064 text
= wxGetStockLabel(id
);
1066 // need & => _ conversion
1067 text
= wxMenuItem::GTKProcessMenuItemLabel(text
, NULL
);
1070 if ( mitem
->IsSeparator() )
1072 menuItem
= gtk_separator_menu_item_new();
1074 else if ( mitem
->GetBitmap().Ok() ||
1075 (mitem
->GetKind() == wxITEM_NORMAL
&& isstock
) )
1077 wxBitmap
bitmap(mitem
->GetBitmap());
1079 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1084 // use stock bitmap for this item if available on the assumption
1085 // that it never hurts to follow GTK+ conventions more closely
1086 image
= stockid
? gtk_image_new_from_stock(stockid
, GTK_ICON_SIZE_MENU
)
1089 else // we have a custom bitmap
1091 wxASSERT_MSG( mitem
->GetKind() == wxITEM_NORMAL
,
1092 _T("only normal menu items can have bitmaps") );
1094 if ( bitmap
.HasPixbuf() )
1096 image
= gtk_image_new_from_pixbuf(bitmap
.GetPixbuf());
1100 GdkPixmap
*gdk_pixmap
= bitmap
.GetPixmap();
1101 GdkBitmap
*gdk_bitmap
= bitmap
.GetMask() ?
1102 bitmap
.GetMask()->GetBitmap() :
1104 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1110 gtk_widget_show(image
);
1112 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1117 else // a normal item
1119 // NB: 'text' variable has "_" instead of "&" after mitem->SetText()
1122 switch ( mitem
->GetKind() )
1126 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1133 GSList
*group
= NULL
;
1134 if ( m_prevRadio
== NULL
)
1136 // start of a new radio group
1137 m_prevRadio
= menuItem
=
1138 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1140 else // continue the radio group
1142 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1143 m_prevRadio
= menuItem
=
1144 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1150 wxFAIL_MSG( _T("unexpected menu item kind") );
1155 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1164 GdkModifierType accel_mods
;
1165 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1167 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1168 if (buf
[(size_t)0] != '\0')
1170 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1173 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1183 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1184 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
1185 gtk_widget_add_accelerator( GTK_WIDGET(menuItem
),
1194 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1196 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1198 gtk_widget_show( menuItem
);
1200 if ( !mitem
->IsSeparator() )
1202 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1204 g_signal_connect (menuItem
, "select",
1205 G_CALLBACK (gtk_menu_hilight_callback
), this);
1206 g_signal_connect (menuItem
, "deselect",
1207 G_CALLBACK (gtk_menu_nolight_callback
), this);
1209 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1211 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1213 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1215 // if adding a submenu to a menu already existing in the menu bar, we
1216 // must set invoking window to allow processing events from this
1218 if ( m_invokingWindow
)
1219 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1223 g_signal_connect (menuItem
, "activate",
1224 G_CALLBACK (gtk_menu_clicked_callback
),
1229 mitem
->SetMenuItem(menuItem
);
1233 // This doesn't even exist!
1234 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1240 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1242 if (!GtkAppend(mitem
))
1245 return wxMenuBase::DoAppend(mitem
);
1248 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1250 if ( !wxMenuBase::DoInsert(pos
, item
) )
1254 if ( !GtkAppend(item
, (int)pos
) )
1260 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1262 if ( !wxMenuBase::DoRemove(item
) )
1263 return (wxMenuItem
*)NULL
;
1265 // TODO: this code doesn't delete the item factory item and this seems
1266 // impossible as of GTK 1.2.6.
1267 gtk_widget_destroy( item
->GetMenuItem() );
1272 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1274 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1277 wxMenuItem
*item
= node
->GetData();
1278 if (item
->GetMenuItem() == menuItem
)
1279 return item
->GetId();
1280 node
= node
->GetNext();
1286 void wxMenu::Attach(wxMenuBarBase
*menubar
)
1288 wxMenuBase::Attach(menubar
);
1290 // inherit layout direction from menubar.
1291 SetLayoutDirection(menubar
->GetLayoutDirection());
1294 // ----------------------------------------------------------------------------
1296 // ----------------------------------------------------------------------------
1300 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1304 wxAcceleratorEntry
*accel
= item
.GetAccel();
1307 int flags
= accel
->GetFlags();
1308 if ( flags
& wxACCEL_ALT
)
1309 hotkey
+= wxT("<alt>");
1310 if ( flags
& wxACCEL_CTRL
)
1311 hotkey
+= wxT("<control>");
1312 if ( flags
& wxACCEL_SHIFT
)
1313 hotkey
+= wxT("<shift>");
1315 int code
= accel
->GetKeyCode();
1342 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1345 // TODO: we should use gdk_keyval_name() (a.k.a.
1346 // XKeysymToString) here as well as hardcoding the keysym
1347 // names this might be not portable
1349 hotkey
<< wxT("Insert" );
1352 hotkey
<< wxT("Delete" );
1355 hotkey
<< wxT("Up" );
1358 hotkey
<< wxT("Down" );
1361 hotkey
<< wxT("PgUp" );
1364 hotkey
<< wxT("PgDn" );
1367 hotkey
<< wxT("Left" );
1370 hotkey
<< wxT("Right" );
1373 hotkey
<< wxT("Home" );
1376 hotkey
<< wxT("End" );
1379 hotkey
<< wxT("Return" );
1382 hotkey
<< wxT("BackSpace" );
1385 hotkey
<< wxT("Tab" );
1388 hotkey
<< wxT("Esc" );
1391 hotkey
<< wxT("space" );
1394 hotkey
<< wxT("Multiply" );
1397 hotkey
<< wxT("Add" );
1400 hotkey
<< wxT("Separator" );
1403 hotkey
<< wxT("Subtract" );
1406 hotkey
<< wxT("Decimal" );
1409 hotkey
<< wxT("Divide" );
1412 hotkey
<< wxT("Cancel" );
1415 hotkey
<< wxT("Clear" );
1418 hotkey
<< wxT("Menu" );
1421 hotkey
<< wxT("Pause" );
1424 hotkey
<< wxT("Capital" );
1427 hotkey
<< wxT("Select" );
1430 hotkey
<< wxT("Print" );
1433 hotkey
<< wxT("Execute" );
1436 hotkey
<< wxT("Snapshot" );
1439 hotkey
<< wxT("Help" );
1442 hotkey
<< wxT("Num_Lock" );
1445 hotkey
<< wxT("Scroll_Lock" );
1447 case WXK_NUMPAD_INSERT
:
1448 hotkey
<< wxT("KP_Insert" );
1450 case WXK_NUMPAD_DELETE
:
1451 hotkey
<< wxT("KP_Delete" );
1453 case WXK_NUMPAD_SPACE
:
1454 hotkey
<< wxT("KP_Space" );
1456 case WXK_NUMPAD_TAB
:
1457 hotkey
<< wxT("KP_Tab" );
1459 case WXK_NUMPAD_ENTER
:
1460 hotkey
<< wxT("KP_Enter" );
1462 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1464 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1466 case WXK_NUMPAD_HOME
:
1467 hotkey
<< wxT("KP_Home" );
1469 case WXK_NUMPAD_LEFT
:
1470 hotkey
<< wxT("KP_Left" );
1473 hotkey
<< wxT("KP_Up" );
1475 case WXK_NUMPAD_RIGHT
:
1476 hotkey
<< wxT("KP_Right" );
1478 case WXK_NUMPAD_DOWN
:
1479 hotkey
<< wxT("KP_Down" );
1481 case WXK_NUMPAD_PAGEUP
:
1482 hotkey
<< wxT("KP_PgUp" );
1484 case WXK_NUMPAD_PAGEDOWN
:
1485 hotkey
<< wxT("KP_PgDn" );
1487 case WXK_NUMPAD_END
:
1488 hotkey
<< wxT("KP_End" );
1490 case WXK_NUMPAD_BEGIN
:
1491 hotkey
<< wxT("KP_Begin" );
1493 case WXK_NUMPAD_EQUAL
:
1494 hotkey
<< wxT("KP_Equal" );
1496 case WXK_NUMPAD_MULTIPLY
:
1497 hotkey
<< wxT("KP_Multiply" );
1499 case WXK_NUMPAD_ADD
:
1500 hotkey
<< wxT("KP_Add" );
1502 case WXK_NUMPAD_SEPARATOR
:
1503 hotkey
<< wxT("KP_Separator" );
1505 case WXK_NUMPAD_SUBTRACT
:
1506 hotkey
<< wxT("KP_Subtract" );
1508 case WXK_NUMPAD_DECIMAL
:
1509 hotkey
<< wxT("KP_Decimal" );
1511 case WXK_NUMPAD_DIVIDE
:
1512 hotkey
<< wxT("KP_Divide" );
1514 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1515 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1516 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1517 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1519 case WXK_WINDOWS_LEFT
:
1520 hotkey
<< wxT("Super_L" );
1522 case WXK_WINDOWS_RIGHT
:
1523 hotkey
<< wxT("Super_R" );
1525 case WXK_WINDOWS_MENU
:
1526 hotkey
<< wxT("Menu" );
1529 hotkey
<< wxT("Command" );
1531 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1532 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1533 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1534 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1535 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1536 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1537 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1540 // if there are any other keys wxAcceleratorEntry::Create() may
1541 // return, we should process them here
1546 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1554 wxFAIL_MSG( wxT("unknown keyboard accel") );
1563 #endif // wxUSE_ACCEL
1565 // ----------------------------------------------------------------------------
1566 // Pop-up menu stuff
1567 // ----------------------------------------------------------------------------
1569 #if wxUSE_MENUS_NATIVE
1571 extern "C" WXDLLIMPEXP_CORE
1572 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1574 *is_waiting
= false;
1577 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1579 menu
->SetInvokingWindow( win
);
1581 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1584 wxMenuItem
*menuitem
= node
->GetData();
1585 if (menuitem
->IsSubMenu())
1587 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1590 node
= node
->GetNext();
1594 extern "C" WXDLLIMPEXP_CORE
1595 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1597 gboolean
* WXUNUSED(whatever
),
1598 gpointer user_data
)
1600 // ensure that the menu appears entirely on screen
1602 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1604 wxSize sizeScreen
= wxGetDisplaySize();
1605 wxPoint
*pos
= (wxPoint
*)user_data
;
1607 gint xmax
= sizeScreen
.x
- req
.width
,
1608 ymax
= sizeScreen
.y
- req
.height
;
1610 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1611 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1614 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1616 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1618 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1620 // NOTE: if you change this code, you need to update
1621 // the same code in taskbar.cpp as well. This
1622 // is ugly code duplication, I know.
1624 SetInvokingWindow( menu
, this );
1628 bool is_waiting
= true;
1630 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1631 G_CALLBACK (gtk_pop_hide_callback
),
1636 GtkMenuPositionFunc posfunc
;
1637 if ( x
== -1 && y
== -1 )
1639 // use GTK's default positioning algorithm
1645 pos
= ClientToScreen(wxPoint(x
, y
));
1647 posfunc
= wxPopupMenuPositionCallback
;
1650 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1651 DoCommonMenuCallbackCode(menu
, eventOpen
);
1654 GTK_MENU(menu
->m_menu
),
1655 (GtkWidget
*) NULL
, // parent menu shell
1656 (GtkWidget
*) NULL
, // parent menu item
1657 posfunc
, // function to position it
1658 userdata
, // client data
1659 0, // button used to activate it
1660 gtk_get_current_event_time()
1665 gtk_main_iteration();
1668 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1670 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1671 DoCommonMenuCallbackCode(menu
, eventClose
);
1676 #endif // wxUSE_MENUS_NATIVE