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"
21 #include "wx/bitmap.h"
27 #include "wx/gtk/private.h"
29 #include <gdk/gdkkeysyms.h>
31 // FIXME: is this right? somehow I don't think so (VZ)
33 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
34 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
35 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
37 #define ACCEL_OBJECT GtkWindow
38 #define ACCEL_OBJECTS(a) (a)->acceleratables
39 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
41 // we use normal item but with a special id for the menu title
42 static const int wxGTK_TITLE_ID
= -3;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
49 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 static wxString
wxReplaceUnderscore( const wxString
& title
)
60 // GTK 1.2 wants to have "_" instead of "&" for accelerators
63 while (*pc
!= wxT('\0'))
65 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
67 // "&" is doubled to indicate "&" instead of accelerator
71 else if (*pc
== wxT('&'))
77 if ( *pc
== wxT('_') )
79 // underscores must be doubled to prevent them from being
80 // interpreted as accelerator character prefix by GTK
89 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
94 //-----------------------------------------------------------------------------
95 // activate message from GTK
96 //-----------------------------------------------------------------------------
98 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
101 wxapp_install_idle_handler();
103 event
.SetEventObject( menu
);
105 wxEvtHandler
* handler
= menu
->GetEventHandler();
106 if (handler
&& handler
->ProcessEvent(event
))
109 wxWindow
*win
= menu
->GetInvokingWindow();
111 win
->GetEventHandler()->ProcessEvent( event
);
116 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
118 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
120 DoCommonMenuCallbackCode(menu
, event
);
123 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
125 if ( !menubar
->GetMenuCount() )
127 // if menubar is empty we can't call GetMenu(0) below
131 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
133 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
144 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
146 // the parent window is known after wxFrame::SetMenu()
147 m_needParent
= false;
149 m_invokingWindow
= (wxWindow
*) NULL
;
151 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
152 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
154 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
158 m_menubar
= gtk_menu_bar_new();
160 if (style
& wxMB_DOCKABLE
)
162 m_widget
= gtk_handle_box_new();
163 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
164 gtk_widget_show( GTK_WIDGET(m_menubar
) );
168 m_widget
= GTK_WIDGET(m_menubar
);
175 for (size_t i
= 0; i
< n
; ++i
)
176 Append(menus
[i
], titles
[i
]);
178 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
179 // don't get it when the menu is dismissed by clicking outside the
180 // toolbar) so we connect to the global one, even if it means that we
181 // can't pass the menu which was closed in wxMenuEvent object
182 g_signal_connect (m_menubar
, "deactivate",
183 G_CALLBACK (gtk_menu_close_callback
), this);
187 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
189 Init(n
, menus
, titles
, style
);
192 wxMenuBar::wxMenuBar(long style
)
194 Init(0, NULL
, NULL
, style
);
197 wxMenuBar::wxMenuBar()
199 Init(0, NULL
, NULL
, 0);
202 wxMenuBar::~wxMenuBar()
206 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
208 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
210 wxWindow
*top_frame
= win
;
211 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
212 top_frame
= top_frame
->GetParent();
214 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
217 wxMenuItem
*menuitem
= node
->GetData();
218 if (menuitem
->IsSubMenu())
219 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
220 node
= node
->GetNext();
224 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
226 menu
->SetInvokingWindow( win
);
228 wxWindow
*top_frame
= win
;
229 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
230 top_frame
= top_frame
->GetParent();
232 // support for native hot keys
233 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
234 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
235 gtk_accel_group_attach( menu
->m_accel
, obj
);
237 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
240 wxMenuItem
*menuitem
= node
->GetData();
241 if (menuitem
->IsSubMenu())
242 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
243 node
= node
->GetNext();
247 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
249 m_invokingWindow
= win
;
250 wxWindow
*top_frame
= win
;
251 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
252 top_frame
= top_frame
->GetParent();
254 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
257 wxMenu
*menu
= node
->GetData();
258 wxMenubarSetInvokingWindow( menu
, win
);
259 node
= node
->GetNext();
263 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
265 m_invokingWindow
= (wxWindow
*) NULL
;
266 wxWindow
*top_frame
= win
;
267 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
268 top_frame
= top_frame
->GetParent();
270 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
273 wxMenu
*menu
= node
->GetData();
274 wxMenubarUnsetInvokingWindow( menu
, win
);
275 node
= node
->GetNext();
279 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
281 if ( !wxMenuBarBase::Append( menu
, title
) )
284 return GtkAppend(menu
, title
);
287 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
289 wxString
str( wxReplaceUnderscore( title
) );
291 // This doesn't have much effect right now.
292 menu
->SetTitle( str
);
294 // The "m_owner" is the "menu item"
295 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
297 gtk_widget_show( menu
->m_owner
);
299 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
302 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
304 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
306 g_signal_connect (menu
->m_owner
, "activate",
307 G_CALLBACK (gtk_menu_open_callback
),
310 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
311 // addings menu later on.
312 if (m_invokingWindow
)
314 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
316 // OPTIMISE ME: we should probably cache this, or pass it
317 // directly, but for now this is a minimal
318 // change to validate the new dynamic sizing.
319 // see (and refactor :) similar code in Remove
322 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
325 frame
->UpdateMenuBarSize();
331 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
333 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
338 if ( !GtkAppend(menu
, title
, (int)pos
) )
344 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
346 // remove the old item and insert a new one
347 wxMenu
*menuOld
= Remove(pos
);
348 if ( menuOld
&& !Insert(pos
, menu
, title
) )
350 return (wxMenu
*) NULL
;
353 // either Insert() succeeded or Remove() failed and menuOld is NULL
357 wxMenu
*wxMenuBar::Remove(size_t pos
)
359 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
361 return (wxMenu
*) NULL
;
363 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
364 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
366 gtk_widget_destroy( menu
->m_owner
);
367 menu
->m_owner
= NULL
;
369 if (m_invokingWindow
)
371 // OPTIMISE ME: see comment in GtkAppend
372 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
375 frame
->UpdateMenuBarSize();
381 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
383 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
385 int res
= menu
->FindItem( itemString
);
386 if (res
!= wxNOT_FOUND
)
390 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
393 wxMenuItem
*item
= node
->GetData();
394 if (item
->IsSubMenu())
395 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
397 node
= node
->GetNext();
403 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
405 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
408 wxMenu
*menu
= node
->GetData();
409 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
412 node
= node
->GetNext();
418 // Find a wxMenuItem using its id. Recurses down into sub-menus
419 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
421 wxMenuItem
* result
= menu
->FindChildItem(id
);
423 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
424 while ( node
&& result
== NULL
)
426 wxMenuItem
*item
= node
->GetData();
427 if (item
->IsSubMenu())
429 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
431 node
= node
->GetNext();
437 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
439 wxMenuItem
* result
= 0;
440 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
441 while (node
&& result
== 0)
443 wxMenu
*menu
= node
->GetData();
444 result
= FindMenuItemByIdRecursive( menu
, id
);
445 node
= node
->GetNext();
450 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
456 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
458 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
460 wxCHECK_RET( node
, wxT("menu not found") );
462 wxMenu
* menu
= node
->GetData();
465 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
468 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
470 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
472 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
474 wxMenu
* menu
= node
->GetData();
477 wxString
text( menu
->GetTitle() );
478 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
480 if ( *pc
== wxT('_') )
482 // '_' is the escape character for GTK+
486 // don't remove ampersands '&' since if we have them in the menu title
487 // it means that they were doubled to indicate "&" instead of accelerator
495 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
497 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
499 wxCHECK_RET( node
, wxT("menu not found") );
501 wxMenu
* menu
= node
->GetData();
503 const wxString
str( wxReplaceUnderscore( label
) );
505 menu
->SetTitle( str
);
508 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
511 //-----------------------------------------------------------------------------
513 //-----------------------------------------------------------------------------
516 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
519 wxapp_install_idle_handler();
521 int id
= menu
->FindMenuIdByMenuItem(widget
);
523 /* should find it for normal (not popup) menu */
524 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
525 _T("menu item not found in gtk_menu_clicked_callback") );
527 if (!menu
->IsEnabled(id
))
530 wxMenuItem
* item
= menu
->FindChildItem( id
);
531 wxCHECK_RET( item
, wxT("error in menu item callback") );
533 if ( item
->GetId() == wxGTK_TITLE_ID
)
535 // ignore events from the menu title
539 if (item
->IsCheckable())
541 bool isReallyChecked
= item
->IsChecked(),
542 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
544 // ensure that the internal state is always consistent with what is
545 // shown on the screen
546 item
->wxMenuItemBase::Check(isReallyChecked
);
548 // we must not report the events for the radio button going up nor the
549 // events resulting from the calls to wxMenuItem::Check()
550 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
551 (isInternallyChecked
== isReallyChecked
) )
558 // Is this menu on a menubar? (possibly nested)
559 wxFrame
* frame
= NULL
;
560 if(menu
->IsAttached())
561 frame
= menu
->GetMenuBar()->GetFrame();
563 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
564 // normally wxMenu::SendEvent() should be enough, if it doesn't work
565 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
566 // should be fixed instead of working around it here...
569 // If it is attached then let the frame send the event.
570 // Don't call frame->ProcessCommand(id) because it toggles
571 // checkable items and we've already done that above.
572 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
573 commandEvent
.SetEventObject(frame
);
574 if (item
->IsCheckable())
575 commandEvent
.SetInt(item
->IsChecked());
576 commandEvent
.SetEventObject(menu
);
578 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
582 // otherwise let the menu have it
583 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
588 //-----------------------------------------------------------------------------
590 //-----------------------------------------------------------------------------
593 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
595 if (g_isIdle
) wxapp_install_idle_handler();
597 int id
= menu
->FindMenuIdByMenuItem(widget
);
599 wxASSERT( id
!= -1 ); // should find it!
601 if (!menu
->IsEnabled(id
))
604 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
605 event
.SetEventObject( menu
);
607 wxEvtHandler
* handler
= menu
->GetEventHandler();
608 if (handler
&& handler
->ProcessEvent(event
))
611 wxWindow
*win
= menu
->GetInvokingWindow();
612 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
616 //-----------------------------------------------------------------------------
618 //-----------------------------------------------------------------------------
621 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
623 if (g_isIdle
) wxapp_install_idle_handler();
625 int id
= menu
->FindMenuIdByMenuItem(widget
);
627 wxASSERT( id
!= -1 ); // should find it!
629 if (!menu
->IsEnabled(id
))
632 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
633 event
.SetEventObject( menu
);
635 wxEvtHandler
* handler
= menu
->GetEventHandler();
636 if (handler
&& handler
->ProcessEvent(event
))
639 wxWindow
*win
= menu
->GetInvokingWindow();
641 win
->GetEventHandler()->ProcessEvent( event
);
645 //-----------------------------------------------------------------------------
647 //-----------------------------------------------------------------------------
649 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
651 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
653 const wxString
& name
,
654 const wxString
& help
,
658 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
661 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
663 const wxString
& text
,
664 const wxString
& help
,
667 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
672 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
674 const wxString
& text
,
675 const wxString
& help
,
678 : wxMenuItemBase(parentMenu
, id
, text
, help
,
679 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
684 void wxMenuItem::Init(const wxString
& text
)
686 m_labelWidget
= (GtkWidget
*) NULL
;
687 m_menuItem
= (GtkWidget
*) NULL
;
692 wxMenuItem::~wxMenuItem()
694 // don't delete menu items, the menus take care of that
697 // return the menu item text without any menu accels
699 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
703 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
705 if ( *pc
== wxT('\t'))
708 if ( *pc
== wxT('_') )
710 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
716 if ( *pc
== wxT('\\') )
718 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
724 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
727 // "&" is doubled to indicate "&" instead of accelerator
734 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
739 void wxMenuItem::SetText( const wxString
& str
)
741 // Some optimization to avoid flicker
742 wxString oldLabel
= m_text
;
743 oldLabel
= wxStripMenuCodes(oldLabel
);
744 oldLabel
.Replace(wxT("_"), wxT(""));
745 wxString label1
= wxStripMenuCodes(str
);
746 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
747 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
751 if (oldLabel
== label1
&&
752 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
759 label
= (GtkLabel
*) m_labelWidget
;
761 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
763 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(m_text
) );
767 GdkModifierType accel_mods
;
768 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
771 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
772 m_parentMenu
->m_accel
,
777 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
778 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
781 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
783 m_parentMenu
->m_accel
,
790 // it's valid for this function to be called even if m_menuItem == NULL
791 void wxMenuItem::DoSetText( const wxString
& str
)
793 // '\t' is the deliminator indicating a hot key
795 const wxChar
*pc
= str
;
796 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
798 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
800 // "&" is doubled to indicate "&" instead of accelerator
804 else if (*pc
== wxT('&'))
808 else if ( *pc
== wxT('_') ) // escape underscores
827 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
832 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
837 return (wxAcceleratorEntry
*)NULL
;
840 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
842 label
<< wxT('\t') << GetHotKey();
844 return wxGetAccelFromString(label
);
847 #endif // wxUSE_ACCEL
849 void wxMenuItem::Check( bool check
)
851 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
853 if (check
== m_isChecked
)
856 wxMenuItemBase::Check( check
);
862 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
866 wxFAIL_MSG( _T("can't check this item") );
870 void wxMenuItem::Enable( bool enable
)
872 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
874 gtk_widget_set_sensitive( m_menuItem
, enable
);
875 wxMenuItemBase::Enable( enable
);
878 bool wxMenuItem::IsChecked() const
880 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
882 wxCHECK_MSG( IsCheckable(), false,
883 wxT("can't get state of uncheckable item!") );
885 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
888 //-----------------------------------------------------------------------------
890 //-----------------------------------------------------------------------------
892 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
896 m_accel
= gtk_accel_group_new();
897 m_menu
= gtk_menu_new();
898 // NB: keep reference to the menu so that it is not destroyed behind
899 // our back by GTK+ e.g. when it is removed from menubar:
900 gtk_widget_ref(m_menu
);
902 m_owner
= (GtkWidget
*) NULL
;
904 // Tearoffs are entries, just like separators. So if we want this
905 // menu to be a tear-off one, we just append a tearoff entry
907 if ( m_style
& wxMENU_TEAROFF
)
909 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
911 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
916 // append the title as the very first entry if we have it
917 if ( !m_title
.empty() )
919 Append(wxGTK_TITLE_ID
, m_title
);
926 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
928 if ( GTK_IS_WIDGET( m_menu
))
931 gtk_widget_unref( m_menu
);
932 // if the menu is inserted in another menu at this time, there was
933 // one more reference to it:
935 gtk_widget_destroy( m_menu
);
939 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
945 if ( mitem
->IsSeparator() )
947 menuItem
= gtk_separator_menu_item_new();
949 else if (mitem
->GetBitmap().Ok())
951 text
= mitem
->GetText();
952 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
954 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
957 if (bitmap
->HasPixbuf())
959 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
963 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
964 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
965 bitmap
->GetMask()->GetBitmap() :
967 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
970 gtk_widget_show(image
);
972 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
976 else // a normal item
978 // text has "_" instead of "&" after mitem->SetText() so don't use it
979 text
= mitem
->GetText() ;
981 switch ( mitem
->GetKind() )
985 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
992 GSList
*group
= NULL
;
993 if ( m_prevRadio
== NULL
)
995 // start of a new radio group
996 m_prevRadio
= menuItem
=
997 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
999 else // continue the radio group
1001 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1002 m_prevRadio
= menuItem
=
1003 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1009 wxFAIL_MSG( _T("unexpected menu item kind") );
1014 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1023 GdkModifierType accel_mods
;
1024 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1026 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1027 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1030 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1039 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1041 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1043 gtk_widget_show( menuItem
);
1045 if ( !mitem
->IsSeparator() )
1047 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1049 g_signal_connect (menuItem
, "select",
1050 G_CALLBACK (gtk_menu_hilight_callback
), this);
1051 g_signal_connect (menuItem
, "deselect",
1052 G_CALLBACK (gtk_menu_nolight_callback
), this);
1054 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1056 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1058 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1060 // if adding a submenu to a menu already existing in the menu bar, we
1061 // must set invoking window to allow processing events from this
1063 if ( m_invokingWindow
)
1064 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1068 g_signal_connect (menuItem
, "activate",
1069 G_CALLBACK (gtk_menu_clicked_callback
),
1074 mitem
->SetMenuItem(menuItem
);
1078 // This doesn't even exist!
1079 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1085 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1087 if (!GtkAppend(mitem
))
1090 return wxMenuBase::DoAppend(mitem
);
1093 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1095 if ( !wxMenuBase::DoInsert(pos
, item
) )
1099 if ( !GtkAppend(item
, (int)pos
) )
1105 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1107 if ( !wxMenuBase::DoRemove(item
) )
1108 return (wxMenuItem
*)NULL
;
1110 // TODO: this code doesn't delete the item factory item and this seems
1111 // impossible as of GTK 1.2.6.
1112 gtk_widget_destroy( item
->GetMenuItem() );
1117 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1119 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1122 wxMenuItem
*item
= node
->GetData();
1123 if (item
->GetMenuItem() == menuItem
)
1124 return item
->GetId();
1125 node
= node
->GetNext();
1131 // ----------------------------------------------------------------------------
1133 // ----------------------------------------------------------------------------
1137 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1141 wxAcceleratorEntry
*accel
= item
.GetAccel();
1144 int flags
= accel
->GetFlags();
1145 if ( flags
& wxACCEL_ALT
)
1146 hotkey
+= wxT("<alt>");
1147 if ( flags
& wxACCEL_CTRL
)
1148 hotkey
+= wxT("<control>");
1149 if ( flags
& wxACCEL_SHIFT
)
1150 hotkey
+= wxT("<shift>");
1152 int code
= accel
->GetKeyCode();
1179 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1182 // TODO: we should use gdk_keyval_name() (a.k.a.
1183 // XKeysymToString) here as well as hardcoding the keysym
1184 // names this might be not portable
1186 hotkey
<< wxT("Insert" );
1189 hotkey
<< wxT("Delete" );
1192 hotkey
<< wxT("Up" );
1195 hotkey
<< wxT("Down" );
1198 hotkey
<< wxT("PgUp" );
1201 hotkey
<< wxT("PgDn" );
1204 hotkey
<< wxT("Left" );
1207 hotkey
<< wxT("Right" );
1210 hotkey
<< wxT("Home" );
1213 hotkey
<< wxT("End" );
1216 hotkey
<< wxT("Return" );
1219 hotkey
<< wxT("BackSpace" );
1222 hotkey
<< wxT("Tab" );
1225 hotkey
<< wxT("Esc" );
1228 hotkey
<< wxT("space" );
1231 hotkey
<< wxT("Multiply" );
1234 hotkey
<< wxT("Add" );
1237 hotkey
<< wxT("Separator" );
1240 hotkey
<< wxT("Subtract" );
1243 hotkey
<< wxT("Decimal" );
1246 hotkey
<< wxT("Divide" );
1249 hotkey
<< wxT("Cancel" );
1252 hotkey
<< wxT("Clear" );
1255 hotkey
<< wxT("Menu" );
1258 hotkey
<< wxT("Pause" );
1261 hotkey
<< wxT("Capital" );
1264 hotkey
<< wxT("Select" );
1267 hotkey
<< wxT("Print" );
1270 hotkey
<< wxT("Execute" );
1273 hotkey
<< wxT("Snapshot" );
1276 hotkey
<< wxT("Help" );
1279 hotkey
<< wxT("Num_Lock" );
1282 hotkey
<< wxT("Scroll_Lock" );
1284 case WXK_NUMPAD_INSERT
:
1285 hotkey
<< wxT("KP_Insert" );
1287 case WXK_NUMPAD_DELETE
:
1288 hotkey
<< wxT("KP_Delete" );
1290 case WXK_NUMPAD_SPACE
:
1291 hotkey
<< wxT("KP_Space" );
1293 case WXK_NUMPAD_TAB
:
1294 hotkey
<< wxT("KP_Tab" );
1296 case WXK_NUMPAD_ENTER
:
1297 hotkey
<< wxT("KP_Enter" );
1299 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1301 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1303 case WXK_NUMPAD_HOME
:
1304 hotkey
<< wxT("KP_Home" );
1306 case WXK_NUMPAD_LEFT
:
1307 hotkey
<< wxT("KP_Left" );
1310 hotkey
<< wxT("KP_Up" );
1312 case WXK_NUMPAD_RIGHT
:
1313 hotkey
<< wxT("KP_Right" );
1315 case WXK_NUMPAD_DOWN
:
1316 hotkey
<< wxT("KP_Down" );
1318 case WXK_NUMPAD_PAGEUP
:
1319 hotkey
<< wxT("KP_PgUp" );
1321 case WXK_NUMPAD_PAGEDOWN
:
1322 hotkey
<< wxT("KP_PgDn" );
1324 case WXK_NUMPAD_END
:
1325 hotkey
<< wxT("KP_End" );
1327 case WXK_NUMPAD_BEGIN
:
1328 hotkey
<< wxT("KP_Begin" );
1330 case WXK_NUMPAD_EQUAL
:
1331 hotkey
<< wxT("KP_Equal" );
1333 case WXK_NUMPAD_MULTIPLY
:
1334 hotkey
<< wxT("KP_Multiply" );
1336 case WXK_NUMPAD_ADD
:
1337 hotkey
<< wxT("KP_Add" );
1339 case WXK_NUMPAD_SEPARATOR
:
1340 hotkey
<< wxT("KP_Separator" );
1342 case WXK_NUMPAD_SUBTRACT
:
1343 hotkey
<< wxT("KP_Subtract" );
1345 case WXK_NUMPAD_DECIMAL
:
1346 hotkey
<< wxT("KP_Decimal" );
1348 case WXK_NUMPAD_DIVIDE
:
1349 hotkey
<< wxT("KP_Divide" );
1351 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1352 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1353 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1354 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1356 case WXK_WINDOWS_LEFT
:
1357 hotkey
<< wxT("Super_L" );
1359 case WXK_WINDOWS_RIGHT
:
1360 hotkey
<< wxT("Super_R" );
1362 case WXK_WINDOWS_MENU
:
1363 hotkey
<< wxT("Menu" );
1366 hotkey
<< wxT("Command" );
1368 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1369 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1370 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1371 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1372 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1373 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1374 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1377 // if there are any other keys wxGetAccelFromString() may
1378 // return, we should process them here
1383 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1391 wxFAIL_MSG( wxT("unknown keyboard accel") );
1400 #endif // wxUSE_ACCEL
1402 // ----------------------------------------------------------------------------
1403 // Pop-up menu stuff
1404 // ----------------------------------------------------------------------------
1406 #if wxUSE_MENUS_NATIVE
1408 extern "C" WXDLLIMPEXP_CORE
1409 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1411 *is_waiting
= false;
1414 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1416 menu
->SetInvokingWindow( win
);
1418 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1421 wxMenuItem
*menuitem
= node
->GetData();
1422 if (menuitem
->IsSubMenu())
1424 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1427 node
= node
->GetNext();
1431 extern "C" WXDLLIMPEXP_CORE
1432 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1434 gboolean
* WXUNUSED(whatever
),
1435 gpointer user_data
)
1437 // ensure that the menu appears entirely on screen
1439 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1441 wxSize sizeScreen
= wxGetDisplaySize();
1442 wxPoint
*pos
= (wxPoint
*)user_data
;
1444 gint xmax
= sizeScreen
.x
- req
.width
,
1445 ymax
= sizeScreen
.y
- req
.height
;
1447 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1448 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1451 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1453 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1455 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1457 // NOTE: if you change this code, you need to update
1458 // the same code in taskbar.cpp as well. This
1459 // is ugly code duplication, I know.
1461 SetInvokingWindow( menu
, this );
1465 bool is_waiting
= true;
1467 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1468 G_CALLBACK (gtk_pop_hide_callback
),
1473 GtkMenuPositionFunc posfunc
;
1474 if ( x
== -1 && y
== -1 )
1476 // use GTK's default positioning algorithm
1482 pos
= ClientToScreen(wxPoint(x
, y
));
1484 posfunc
= wxPopupMenuPositionCallback
;
1487 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1488 DoCommonMenuCallbackCode(menu
, eventOpen
);
1491 GTK_MENU(menu
->m_menu
),
1492 (GtkWidget
*) NULL
, // parent menu shell
1493 (GtkWidget
*) NULL
, // parent menu item
1494 posfunc
, // function to position it
1495 userdata
, // client data
1496 0, // button used to activate it
1497 gtk_get_current_event_time()
1502 gtk_main_iteration();
1505 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1507 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1508 DoCommonMenuCallbackCode(menu
, eventClose
);
1513 #endif // wxUSE_MENUS_NATIVE