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 #include <glib-object.h>
35 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
36 #define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
37 #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
39 #define ACCEL_OBJECT GtkWindow
40 #define ACCEL_OBJECTS(a) (a)->acceleratables
41 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
43 #define ACCEL_OBJECT GtkObject
44 #define ACCEL_OBJECTS(a) (a)->attach_objects
45 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
48 // we use normal item but with a special id for the menu title
49 static const int wxGTK_TITLE_ID
= -3;
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
56 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 static wxString
wxReplaceUnderscore( const wxString
& title
)
67 // GTK 1.2 wants to have "_" instead of "&" for accelerators
70 while (*pc
!= wxT('\0'))
72 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
74 // "&" is doubled to indicate "&" instead of accelerator
78 else if (*pc
== wxT('&'))
84 if ( *pc
== wxT('_') )
86 // underscores must be doubled to prevent them from being
87 // interpreted as accelerator character prefix by GTK
96 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
101 //-----------------------------------------------------------------------------
102 // activate message from GTK
103 //-----------------------------------------------------------------------------
105 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
108 wxapp_install_idle_handler();
110 event
.SetEventObject( menu
);
112 wxEvtHandler
* handler
= menu
->GetEventHandler();
113 if (handler
&& handler
->ProcessEvent(event
))
116 wxWindow
*win
= menu
->GetInvokingWindow();
118 win
->GetEventHandler()->ProcessEvent( event
);
123 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
125 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
127 DoCommonMenuCallbackCode(menu
, event
);
130 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
132 if ( !menubar
->GetMenuCount() )
134 // if menubar is empty we can't call GetMenu(0) below
138 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
140 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
145 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
151 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
153 // the parent window is known after wxFrame::SetMenu()
154 m_needParent
= false;
156 m_invokingWindow
= (wxWindow
*) NULL
;
158 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
159 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
161 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
165 m_menubar
= gtk_menu_bar_new();
167 if (style
& wxMB_DOCKABLE
)
169 m_widget
= gtk_handle_box_new();
170 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
171 gtk_widget_show( GTK_WIDGET(m_menubar
) );
175 m_widget
= GTK_WIDGET(m_menubar
);
182 for (size_t i
= 0; i
< n
; ++i
)
183 Append(menus
[i
], titles
[i
]);
185 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
186 // don't get it when the menu is dismissed by clicking outside the
187 // toolbar) so we connect to the global one, even if it means that we
188 // can't pass the menu which was closed in wxMenuEvent object
189 g_signal_connect (m_menubar
, "deactivate",
190 G_CALLBACK (gtk_menu_close_callback
), this);
194 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
196 Init(n
, menus
, titles
, style
);
199 wxMenuBar::wxMenuBar(long style
)
201 Init(0, NULL
, NULL
, style
);
204 wxMenuBar::wxMenuBar()
206 Init(0, NULL
, NULL
, 0);
209 wxMenuBar::~wxMenuBar()
213 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
215 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
217 wxWindow
*top_frame
= win
;
218 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
219 top_frame
= top_frame
->GetParent();
221 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
224 wxMenuItem
*menuitem
= node
->GetData();
225 if (menuitem
->IsSubMenu())
226 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
227 node
= node
->GetNext();
231 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
233 menu
->SetInvokingWindow( win
);
235 wxWindow
*top_frame
= win
;
236 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
237 top_frame
= top_frame
->GetParent();
239 // support for native hot keys
240 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
241 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
242 gtk_accel_group_attach( menu
->m_accel
, obj
);
244 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
247 wxMenuItem
*menuitem
= node
->GetData();
248 if (menuitem
->IsSubMenu())
249 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
250 node
= node
->GetNext();
254 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
256 m_invokingWindow
= win
;
257 wxWindow
*top_frame
= win
;
258 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
259 top_frame
= top_frame
->GetParent();
261 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
264 wxMenu
*menu
= node
->GetData();
265 wxMenubarSetInvokingWindow( menu
, win
);
266 node
= node
->GetNext();
270 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
272 m_invokingWindow
= (wxWindow
*) NULL
;
273 wxWindow
*top_frame
= win
;
274 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
275 top_frame
= top_frame
->GetParent();
277 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
280 wxMenu
*menu
= node
->GetData();
281 wxMenubarUnsetInvokingWindow( menu
, win
);
282 node
= node
->GetNext();
286 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
288 if ( !wxMenuBarBase::Append( menu
, title
) )
291 return GtkAppend(menu
, title
);
294 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
296 wxString
str( wxReplaceUnderscore( title
) );
298 // This doesn't have much effect right now.
299 menu
->SetTitle( str
);
301 // The "m_owner" is the "menu item"
302 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
304 gtk_widget_show( menu
->m_owner
);
306 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
309 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
311 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
313 g_signal_connect (menu
->m_owner
, "activate",
314 G_CALLBACK (gtk_menu_open_callback
),
317 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
318 // addings menu later on.
319 if (m_invokingWindow
)
321 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
323 // OPTIMISE ME: we should probably cache this, or pass it
324 // directly, but for now this is a minimal
325 // change to validate the new dynamic sizing.
326 // see (and refactor :) similar code in Remove
329 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
332 frame
->UpdateMenuBarSize();
338 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
340 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
345 if ( !GtkAppend(menu
, title
, (int)pos
) )
351 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
353 // remove the old item and insert a new one
354 wxMenu
*menuOld
= Remove(pos
);
355 if ( menuOld
&& !Insert(pos
, menu
, title
) )
357 return (wxMenu
*) NULL
;
360 // either Insert() succeeded or Remove() failed and menuOld is NULL
364 wxMenu
*wxMenuBar::Remove(size_t pos
)
366 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
368 return (wxMenu
*) NULL
;
370 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
371 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
373 gtk_widget_destroy( menu
->m_owner
);
374 menu
->m_owner
= NULL
;
376 if (m_invokingWindow
)
378 // OPTIMISE ME: see comment in GtkAppend
379 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
382 frame
->UpdateMenuBarSize();
388 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
390 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
392 int res
= menu
->FindItem( itemString
);
393 if (res
!= wxNOT_FOUND
)
397 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
400 wxMenuItem
*item
= node
->GetData();
401 if (item
->IsSubMenu())
402 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
404 node
= node
->GetNext();
410 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
412 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
415 wxMenu
*menu
= node
->GetData();
416 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
419 node
= node
->GetNext();
425 // Find a wxMenuItem using its id. Recurses down into sub-menus
426 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
428 wxMenuItem
* result
= menu
->FindChildItem(id
);
430 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
431 while ( node
&& result
== NULL
)
433 wxMenuItem
*item
= node
->GetData();
434 if (item
->IsSubMenu())
436 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
438 node
= node
->GetNext();
444 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
446 wxMenuItem
* result
= 0;
447 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
448 while (node
&& result
== 0)
450 wxMenu
*menu
= node
->GetData();
451 result
= FindMenuItemByIdRecursive( menu
, id
);
452 node
= node
->GetNext();
457 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
463 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
465 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
467 wxCHECK_RET( node
, wxT("menu not found") );
469 wxMenu
* menu
= node
->GetData();
472 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
475 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
477 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
479 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
481 wxMenu
* menu
= node
->GetData();
484 wxString
text( menu
->GetTitle() );
485 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
487 if ( *pc
== wxT('_') )
489 // '_' is the escape character for GTK+
493 // don't remove ampersands '&' since if we have them in the menu title
494 // it means that they were doubled to indicate "&" instead of accelerator
502 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
504 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
506 wxCHECK_RET( node
, wxT("menu not found") );
508 wxMenu
* menu
= node
->GetData();
510 const wxString
str( wxReplaceUnderscore( label
) );
512 menu
->SetTitle( str
);
515 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
518 //-----------------------------------------------------------------------------
520 //-----------------------------------------------------------------------------
523 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
526 wxapp_install_idle_handler();
528 int id
= menu
->FindMenuIdByMenuItem(widget
);
530 /* should find it for normal (not popup) menu */
531 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
532 _T("menu item not found in gtk_menu_clicked_callback") );
534 if (!menu
->IsEnabled(id
))
537 wxMenuItem
* item
= menu
->FindChildItem( id
);
538 wxCHECK_RET( item
, wxT("error in menu item callback") );
540 if ( item
->GetId() == wxGTK_TITLE_ID
)
542 // ignore events from the menu title
546 if (item
->IsCheckable())
548 bool isReallyChecked
= item
->IsChecked(),
549 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
551 // ensure that the internal state is always consistent with what is
552 // shown on the screen
553 item
->wxMenuItemBase::Check(isReallyChecked
);
555 // we must not report the events for the radio button going up nor the
556 // events resulting from the calls to wxMenuItem::Check()
557 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
558 (isInternallyChecked
== isReallyChecked
) )
565 // Is this menu on a menubar? (possibly nested)
566 wxFrame
* frame
= NULL
;
567 if(menu
->IsAttached())
568 frame
= menu
->GetMenuBar()->GetFrame();
570 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
571 // normally wxMenu::SendEvent() should be enough, if it doesn't work
572 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
573 // should be fixed instead of working around it here...
576 // If it is attached then let the frame send the event.
577 // Don't call frame->ProcessCommand(id) because it toggles
578 // checkable items and we've already done that above.
579 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
580 commandEvent
.SetEventObject(frame
);
581 if (item
->IsCheckable())
582 commandEvent
.SetInt(item
->IsChecked());
583 commandEvent
.SetEventObject(menu
);
585 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
589 // otherwise let the menu have it
590 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
595 //-----------------------------------------------------------------------------
597 //-----------------------------------------------------------------------------
600 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
602 if (g_isIdle
) wxapp_install_idle_handler();
604 int id
= menu
->FindMenuIdByMenuItem(widget
);
606 wxASSERT( id
!= -1 ); // should find it!
608 if (!menu
->IsEnabled(id
))
611 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
612 event
.SetEventObject( menu
);
614 wxEvtHandler
* handler
= menu
->GetEventHandler();
615 if (handler
&& handler
->ProcessEvent(event
))
618 wxWindow
*win
= menu
->GetInvokingWindow();
619 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
623 //-----------------------------------------------------------------------------
625 //-----------------------------------------------------------------------------
628 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
630 if (g_isIdle
) wxapp_install_idle_handler();
632 int id
= menu
->FindMenuIdByMenuItem(widget
);
634 wxASSERT( id
!= -1 ); // should find it!
636 if (!menu
->IsEnabled(id
))
639 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
640 event
.SetEventObject( menu
);
642 wxEvtHandler
* handler
= menu
->GetEventHandler();
643 if (handler
&& handler
->ProcessEvent(event
))
646 wxWindow
*win
= menu
->GetInvokingWindow();
648 win
->GetEventHandler()->ProcessEvent( event
);
652 //-----------------------------------------------------------------------------
654 //-----------------------------------------------------------------------------
656 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
658 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
660 const wxString
& name
,
661 const wxString
& help
,
665 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
668 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
670 const wxString
& text
,
671 const wxString
& help
,
674 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
679 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
681 const wxString
& text
,
682 const wxString
& help
,
685 : wxMenuItemBase(parentMenu
, id
, text
, help
,
686 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
691 void wxMenuItem::Init(const wxString
& text
)
693 m_labelWidget
= (GtkWidget
*) NULL
;
694 m_menuItem
= (GtkWidget
*) NULL
;
699 wxMenuItem::~wxMenuItem()
701 // don't delete menu items, the menus take care of that
704 // return the menu item text without any menu accels
706 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
710 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
712 if ( *pc
== wxT('\t'))
715 if ( *pc
== wxT('_') )
717 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
723 if ( *pc
== wxT('\\') )
725 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
731 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
734 // "&" is doubled to indicate "&" instead of accelerator
741 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
746 void wxMenuItem::SetText( const wxString
& str
)
748 // Some optimization to avoid flicker
749 wxString oldLabel
= m_text
;
750 oldLabel
= wxStripMenuCodes(oldLabel
);
751 oldLabel
.Replace(wxT("_"), wxT(""));
752 wxString label1
= wxStripMenuCodes(str
);
753 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
754 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
758 if (oldLabel
== label1
&&
759 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
766 label
= (GtkLabel
*) m_labelWidget
;
768 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
770 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(m_text
) );
774 GdkModifierType accel_mods
;
775 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
778 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
779 m_parentMenu
->m_accel
,
784 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
785 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
788 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
790 m_parentMenu
->m_accel
,
797 // it's valid for this function to be called even if m_menuItem == NULL
798 void wxMenuItem::DoSetText( const wxString
& str
)
800 // '\t' is the deliminator indicating a hot key
802 const wxChar
*pc
= str
;
803 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
805 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
807 // "&" is doubled to indicate "&" instead of accelerator
811 else if (*pc
== wxT('&'))
815 else if ( *pc
== wxT('_') ) // escape underscores
834 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
839 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
844 return (wxAcceleratorEntry
*)NULL
;
847 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
849 label
<< wxT('\t') << GetHotKey();
851 return wxGetAccelFromString(label
);
854 #endif // wxUSE_ACCEL
856 void wxMenuItem::Check( bool check
)
858 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
860 if (check
== m_isChecked
)
863 wxMenuItemBase::Check( check
);
869 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
873 wxFAIL_MSG( _T("can't check this item") );
877 void wxMenuItem::Enable( bool enable
)
879 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
881 gtk_widget_set_sensitive( m_menuItem
, enable
);
882 wxMenuItemBase::Enable( enable
);
885 bool wxMenuItem::IsChecked() const
887 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
889 wxCHECK_MSG( IsCheckable(), false,
890 wxT("can't get state of uncheckable item!") );
892 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
895 //-----------------------------------------------------------------------------
897 //-----------------------------------------------------------------------------
899 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
903 m_accel
= gtk_accel_group_new();
904 m_menu
= gtk_menu_new();
905 // NB: keep reference to the menu so that it is not destroyed behind
906 // our back by GTK+ e.g. when it is removed from menubar:
907 gtk_widget_ref(m_menu
);
909 m_owner
= (GtkWidget
*) NULL
;
911 // Tearoffs are entries, just like separators. So if we want this
912 // menu to be a tear-off one, we just append a tearoff entry
914 if ( m_style
& wxMENU_TEAROFF
)
916 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
918 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
923 // append the title as the very first entry if we have it
924 if ( !m_title
.empty() )
926 Append(wxGTK_TITLE_ID
, m_title
);
933 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
935 if ( GTK_IS_WIDGET( m_menu
))
938 gtk_widget_unref( m_menu
);
939 // if the menu is inserted in another menu at this time, there was
940 // one more reference to it:
942 gtk_widget_destroy( m_menu
);
946 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
952 if ( mitem
->IsSeparator() )
954 menuItem
= gtk_separator_menu_item_new();
956 else if (mitem
->GetBitmap().Ok())
958 text
= mitem
->GetText();
959 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
961 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
964 if (bitmap
->HasPixbuf())
966 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
970 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
971 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
972 bitmap
->GetMask()->GetBitmap() :
974 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
977 gtk_widget_show(image
);
979 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
983 else // a normal item
985 // text has "_" instead of "&" after mitem->SetText() so don't use it
986 text
= mitem
->GetText() ;
988 switch ( mitem
->GetKind() )
992 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
999 GSList
*group
= NULL
;
1000 if ( m_prevRadio
== NULL
)
1002 // start of a new radio group
1003 m_prevRadio
= menuItem
=
1004 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1006 else // continue the radio group
1008 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1009 m_prevRadio
= menuItem
=
1010 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1016 wxFAIL_MSG( _T("unexpected menu item kind") );
1021 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1030 GdkModifierType accel_mods
;
1031 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1033 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1034 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1037 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1046 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1048 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1050 gtk_widget_show( menuItem
);
1052 if ( !mitem
->IsSeparator() )
1054 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1056 g_signal_connect (menuItem
, "select",
1057 G_CALLBACK (gtk_menu_hilight_callback
), this);
1058 g_signal_connect (menuItem
, "deselect",
1059 G_CALLBACK (gtk_menu_nolight_callback
), this);
1061 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1063 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1065 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1067 // if adding a submenu to a menu already existing in the menu bar, we
1068 // must set invoking window to allow processing events from this
1070 if ( m_invokingWindow
)
1071 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1075 g_signal_connect (menuItem
, "activate",
1076 G_CALLBACK (gtk_menu_clicked_callback
),
1081 mitem
->SetMenuItem(menuItem
);
1085 // This doesn't even exist!
1086 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1092 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1094 if (!GtkAppend(mitem
))
1097 return wxMenuBase::DoAppend(mitem
);
1100 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1102 if ( !wxMenuBase::DoInsert(pos
, item
) )
1106 if ( !GtkAppend(item
, (int)pos
) )
1112 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1114 if ( !wxMenuBase::DoRemove(item
) )
1115 return (wxMenuItem
*)NULL
;
1117 // TODO: this code doesn't delete the item factory item and this seems
1118 // impossible as of GTK 1.2.6.
1119 gtk_widget_destroy( item
->GetMenuItem() );
1124 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1126 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1129 wxMenuItem
*item
= node
->GetData();
1130 if (item
->GetMenuItem() == menuItem
)
1131 return item
->GetId();
1132 node
= node
->GetNext();
1138 // ----------------------------------------------------------------------------
1140 // ----------------------------------------------------------------------------
1144 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1148 wxAcceleratorEntry
*accel
= item
.GetAccel();
1151 int flags
= accel
->GetFlags();
1152 if ( flags
& wxACCEL_ALT
)
1153 hotkey
+= wxT("<alt>");
1154 if ( flags
& wxACCEL_CTRL
)
1155 hotkey
+= wxT("<control>");
1156 if ( flags
& wxACCEL_SHIFT
)
1157 hotkey
+= wxT("<shift>");
1159 int code
= accel
->GetKeyCode();
1186 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1189 // TODO: we should use gdk_keyval_name() (a.k.a.
1190 // XKeysymToString) here as well as hardcoding the keysym
1191 // names this might be not portable
1193 hotkey
<< wxT("Insert" );
1196 hotkey
<< wxT("Delete" );
1199 hotkey
<< wxT("Up" );
1202 hotkey
<< wxT("Down" );
1205 hotkey
<< wxT("PgUp" );
1208 hotkey
<< wxT("PgDn" );
1211 hotkey
<< wxT("Left" );
1214 hotkey
<< wxT("Right" );
1217 hotkey
<< wxT("Home" );
1220 hotkey
<< wxT("End" );
1223 hotkey
<< wxT("Return" );
1226 hotkey
<< wxT("BackSpace" );
1229 hotkey
<< wxT("Tab" );
1232 hotkey
<< wxT("Esc" );
1235 hotkey
<< wxT("space" );
1238 hotkey
<< wxT("Multiply" );
1241 hotkey
<< wxT("Add" );
1244 hotkey
<< wxT("Separator" );
1247 hotkey
<< wxT("Subtract" );
1250 hotkey
<< wxT("Decimal" );
1253 hotkey
<< wxT("Divide" );
1256 hotkey
<< wxT("Cancel" );
1259 hotkey
<< wxT("Clear" );
1262 hotkey
<< wxT("Menu" );
1265 hotkey
<< wxT("Pause" );
1268 hotkey
<< wxT("Capital" );
1271 hotkey
<< wxT("Select" );
1274 hotkey
<< wxT("Print" );
1277 hotkey
<< wxT("Execute" );
1280 hotkey
<< wxT("Snapshot" );
1283 hotkey
<< wxT("Help" );
1286 hotkey
<< wxT("Num_Lock" );
1289 hotkey
<< wxT("Scroll_Lock" );
1291 case WXK_NUMPAD_INSERT
:
1292 hotkey
<< wxT("KP_Insert" );
1294 case WXK_NUMPAD_DELETE
:
1295 hotkey
<< wxT("KP_Delete" );
1297 case WXK_NUMPAD_SPACE
:
1298 hotkey
<< wxT("KP_Space" );
1300 case WXK_NUMPAD_TAB
:
1301 hotkey
<< wxT("KP_Tab" );
1303 case WXK_NUMPAD_ENTER
:
1304 hotkey
<< wxT("KP_Enter" );
1306 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1308 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1310 case WXK_NUMPAD_HOME
:
1311 hotkey
<< wxT("KP_Home" );
1313 case WXK_NUMPAD_LEFT
:
1314 hotkey
<< wxT("KP_Left" );
1317 hotkey
<< wxT("KP_Up" );
1319 case WXK_NUMPAD_RIGHT
:
1320 hotkey
<< wxT("KP_Right" );
1322 case WXK_NUMPAD_DOWN
:
1323 hotkey
<< wxT("KP_Down" );
1325 case WXK_NUMPAD_PAGEUP
:
1326 hotkey
<< wxT("KP_PgUp" );
1328 case WXK_NUMPAD_PAGEDOWN
:
1329 hotkey
<< wxT("KP_PgDn" );
1331 case WXK_NUMPAD_END
:
1332 hotkey
<< wxT("KP_End" );
1334 case WXK_NUMPAD_BEGIN
:
1335 hotkey
<< wxT("KP_Begin" );
1337 case WXK_NUMPAD_EQUAL
:
1338 hotkey
<< wxT("KP_Equal" );
1340 case WXK_NUMPAD_MULTIPLY
:
1341 hotkey
<< wxT("KP_Multiply" );
1343 case WXK_NUMPAD_ADD
:
1344 hotkey
<< wxT("KP_Add" );
1346 case WXK_NUMPAD_SEPARATOR
:
1347 hotkey
<< wxT("KP_Separator" );
1349 case WXK_NUMPAD_SUBTRACT
:
1350 hotkey
<< wxT("KP_Subtract" );
1352 case WXK_NUMPAD_DECIMAL
:
1353 hotkey
<< wxT("KP_Decimal" );
1355 case WXK_NUMPAD_DIVIDE
:
1356 hotkey
<< wxT("KP_Divide" );
1358 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1359 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1360 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1361 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1363 case WXK_WINDOWS_LEFT
:
1364 hotkey
<< wxT("Super_L" );
1366 case WXK_WINDOWS_RIGHT
:
1367 hotkey
<< wxT("Super_R" );
1369 case WXK_WINDOWS_MENU
:
1370 hotkey
<< wxT("Menu" );
1373 hotkey
<< wxT("Command" );
1375 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1376 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1377 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1378 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1379 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1380 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1381 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1384 // if there are any other keys wxGetAccelFromString() may
1385 // return, we should process them here
1390 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1398 wxFAIL_MSG( wxT("unknown keyboard accel") );
1407 #endif // wxUSE_ACCEL
1409 // ----------------------------------------------------------------------------
1410 // Pop-up menu stuff
1411 // ----------------------------------------------------------------------------
1413 #if wxUSE_MENUS_NATIVE
1415 extern "C" WXDLLIMPEXP_CORE
1416 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1418 *is_waiting
= false;
1421 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1423 menu
->SetInvokingWindow( win
);
1425 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1428 wxMenuItem
*menuitem
= node
->GetData();
1429 if (menuitem
->IsSubMenu())
1431 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1434 node
= node
->GetNext();
1438 extern "C" WXDLLIMPEXP_CORE
1439 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1441 gboolean
* WXUNUSED(whatever
),
1442 gpointer user_data
)
1444 // ensure that the menu appears entirely on screen
1446 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1448 wxSize sizeScreen
= wxGetDisplaySize();
1449 wxPoint
*pos
= (wxPoint
*)user_data
;
1451 gint xmax
= sizeScreen
.x
- req
.width
,
1452 ymax
= sizeScreen
.y
- req
.height
;
1454 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1455 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1458 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1460 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1462 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1464 // NOTE: if you change this code, you need to update
1465 // the same code in taskbar.cpp as well. This
1466 // is ugly code duplication, I know.
1468 SetInvokingWindow( menu
, this );
1472 bool is_waiting
= true;
1474 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1475 G_CALLBACK (gtk_pop_hide_callback
),
1480 GtkMenuPositionFunc posfunc
;
1481 if ( x
== -1 && y
== -1 )
1483 // use GTK's default positioning algorithm
1489 pos
= ClientToScreen(wxPoint(x
, y
));
1491 posfunc
= wxPopupMenuPositionCallback
;
1494 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1495 DoCommonMenuCallbackCode(menu
, eventOpen
);
1498 GTK_MENU(menu
->m_menu
),
1499 (GtkWidget
*) NULL
, // parent menu shell
1500 (GtkWidget
*) NULL
, // parent menu item
1501 posfunc
, // function to position it
1502 userdata
, // client data
1503 0, // button used to activate it
1504 gtk_get_current_event_time()
1509 gtk_main_iteration();
1512 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1514 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1515 DoCommonMenuCallbackCode(menu
, eventClose
);
1520 #endif // wxUSE_MENUS_NATIVE