1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
14 #include "wx/stockitem.h"
20 #include "wx/bitmap.h"
27 #include "wx/gtk1/private.h"
28 #include "wx/gtk1/private/mnemonics.h"
30 #include <gdk/gdkkeysyms.h>
32 #define ACCEL_OBJECT GtkObject
33 #define ACCEL_OBJECTS(a) (a)->attach_objects
34 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
36 // we use normal item but with a special id for the menu title
37 static const int wxGTK_TITLE_ID
= -3;
39 // defined in window.cpp
40 extern guint32 wxGtkTimeLastClick
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern void wxapp_install_idle_handler();
50 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 static wxString
wxReplaceUnderscore( const wxString
& title
)
61 // GTK 1.2 wants to have "_" instead of "&" for accelerators
64 while (*pc
!= wxT('\0'))
66 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
68 // "&" is doubled to indicate "&" instead of accelerator
72 else if (*pc
== wxT('&'))
78 if ( *pc
== wxT('_') )
80 // underscores must be doubled to prevent them from being
81 // interpreted as accelerator character prefix by GTK
90 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
95 static wxString
wxConvertFromGTKToWXLabel(const wxString
& gtkLabel
)
98 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
100 // '_' is the escape character for GTK+.
102 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
104 // An underscore was escaped.
108 else if ( *pc
== wxT('_') )
110 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
113 else if ( *pc
== wxT('&') )
115 // Double the ampersand to escape it as far as wxWidgets is concerned
120 // don't remove ampersands '&' since if we have them in the menu title
121 // it means that they were doubled to indicate "&" instead of accelerator
130 //-----------------------------------------------------------------------------
131 // activate message from GTK
132 //-----------------------------------------------------------------------------
134 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
137 wxapp_install_idle_handler();
139 event
.SetEventObject( menu
);
141 wxEvtHandler
* handler
= menu
->GetEventHandler();
142 if (handler
&& handler
->ProcessEvent(event
))
145 wxWindow
*win
= menu
->GetWindow();
147 win
->HandleWindowEvent( event
);
152 static void gtk_menu_open_callback( GtkWidget
*WXUNUSED(widget
), wxMenu
*menu
)
154 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
156 DoCommonMenuCallbackCode(menu
, event
);
159 static void gtk_menu_close_callback( GtkWidget
*WXUNUSED(widget
), wxMenuBar
*menubar
)
161 if ( !menubar
->GetMenuCount() )
163 // if menubar is empty we can't call GetMenu(0) below
167 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
169 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
180 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
182 // the parent window is known after wxFrame::SetMenu()
183 m_needParent
= false;
186 if (!PreCreation( NULL
, wxDefaultPosition
, wxDefaultSize
) ||
187 !CreateBase( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
189 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
193 m_menubar
= gtk_menu_bar_new();
194 m_accel
= gtk_accel_group_new();
196 if (style
& wxMB_DOCKABLE
)
198 m_widget
= gtk_handle_box_new();
199 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
200 gtk_widget_show( GTK_WIDGET(m_menubar
) );
204 m_widget
= GTK_WIDGET(m_menubar
);
211 for (size_t i
= 0; i
< n
; ++i
)
212 Append(menus
[i
], titles
[i
]);
214 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
215 // don't get it when the menu is dismissed by clicking outside the
216 // toolbar) so we connect to the global one, even if it means that we
217 // can't pass the menu which was closed in wxMenuEvent object
218 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
220 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
225 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
227 Init(n
, menus
, titles
, style
);
230 wxMenuBar::wxMenuBar(long style
)
232 Init(0, NULL
, NULL
, style
);
235 wxMenuBar::wxMenuBar()
237 Init(0, NULL
, NULL
, 0);
240 wxMenuBar::~wxMenuBar()
244 static void DetachFromFrame( wxMenu
*menu
, wxWindow
*win
)
246 wxWindow
*top_frame
= win
;
247 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
248 top_frame
= top_frame
->GetParent();
250 // support for native hot keys
251 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
253 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
256 wxMenuItem
*menuitem
= node
->GetData();
257 if (menuitem
->IsSubMenu())
258 DetachFromFrame( menuitem
->GetSubMenu(), win
);
259 node
= node
->GetNext();
263 static void AttachToFrame( wxMenu
*menu
, wxWindow
*win
)
265 wxWindow
*top_frame
= win
;
266 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
267 top_frame
= top_frame
->GetParent();
269 // support for native hot keys
270 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
271 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
272 gtk_accel_group_attach( menu
->m_accel
, obj
);
274 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
277 wxMenuItem
*menuitem
= node
->GetData();
278 if (menuitem
->IsSubMenu())
279 AttachToFrame( menuitem
->GetSubMenu(), win
);
280 node
= node
->GetNext();
284 void wxMenuBar::Attach( wxFrame
*win
)
286 wxMenuBarBase::Attach(win
);
288 wxWindow
*top_frame
= win
;
289 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
290 top_frame
= top_frame
->GetParent();
292 // support for native key accelerators indicated by underscroes
293 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
294 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
295 gtk_accel_group_attach( m_accel
, obj
);
297 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
300 wxMenu
*menu
= node
->GetData();
301 AttachToFrame( menu
, win
);
302 node
= node
->GetNext();
306 void wxMenuBar::Detach()
308 wxWindow
*top_frame
= m_menuBarFrame
;
309 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
310 top_frame
= top_frame
->GetParent();
312 // support for native key accelerators indicated by underscroes
313 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
315 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
318 wxMenu
*menu
= node
->GetData();
319 DetachFromFrame( menu
, top_frame
);
320 node
= node
->GetNext();
323 wxMenuBarBase::Detach();
326 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
328 if ( !wxMenuBarBase::Append( menu
, title
) )
331 return GtkAppend(menu
, title
);
334 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
336 wxString
str( wxReplaceUnderscore( title
) );
338 // This doesn't have much effect right now.
339 menu
->SetTitle( str
);
341 // The "m_owner" is the "menu item"
342 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
343 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
345 gtk_label_set_text( label
, wxGTK_CONV( str
) );
347 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
348 if (accel_key
!= GDK_VoidSymbol
)
350 gtk_widget_add_accelerator (menu
->m_owner
,
352 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
358 gtk_widget_show( menu
->m_owner
);
360 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
363 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
365 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
367 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
368 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
373 AttachToFrame( menu
, m_menuBarFrame
);
375 // OPTIMISE ME: we should probably cache this, or pass it
376 // directly, but for now this is a minimal
377 // change to validate the new dynamic sizing.
378 // see (and refactor :) similar code in Remove
381 m_menuBarFrame
->UpdateMenuBarSize();
387 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
389 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
394 if ( !GtkAppend(menu
, title
, (int)pos
) )
400 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
402 // remove the old item and insert a new one
403 wxMenu
*menuOld
= Remove(pos
);
404 if ( menuOld
&& !Insert(pos
, menu
, title
) )
409 // either Insert() succeeded or Remove() failed and menuOld is NULL
413 wxMenu
*wxMenuBar::Remove(size_t pos
)
415 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
419 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
420 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
422 gtk_widget_destroy( menu
->m_owner
);
423 menu
->m_owner
= NULL
;
427 // OPTIMISE ME: see comment in GtkAppend
428 m_menuBarFrame
->UpdateMenuBarSize();
434 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
436 if (wxMenuItem::GetLabelText(menu
->GetTitle()) == wxMenuItem::GetLabelText(menuString
))
438 int res
= menu
->FindItem( itemString
);
439 if (res
!= wxNOT_FOUND
)
443 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
446 wxMenuItem
*item
= node
->GetData();
447 if (item
->IsSubMenu())
448 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
450 node
= node
->GetNext();
456 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
458 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
461 wxMenu
*menu
= node
->GetData();
462 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
465 node
= node
->GetNext();
471 // Find a wxMenuItem using its id. Recurses down into sub-menus
472 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
474 wxMenuItem
* result
= menu
->FindChildItem(id
);
476 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
477 while ( node
&& result
== NULL
)
479 wxMenuItem
*item
= node
->GetData();
480 if (item
->IsSubMenu())
482 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
484 node
= node
->GetNext();
490 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
492 wxMenuItem
* result
= 0;
493 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
494 while (node
&& result
== 0)
496 wxMenu
*menu
= node
->GetData();
497 result
= FindMenuItemByIdRecursive( menu
, id
);
498 node
= node
->GetNext();
503 *menuForItem
= result
? result
->GetMenu() : NULL
;
509 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
511 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
513 wxCHECK_RET( node
, wxT("menu not found") );
515 wxMenu
* menu
= node
->GetData();
518 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
521 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
523 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
525 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
527 wxMenu
* menu
= node
->GetData();
529 return menu
->GetTitle();
532 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
534 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
536 wxCHECK_RET( node
, wxT("menu not found") );
538 wxMenu
* menu
= node
->GetData();
540 const wxString
str( wxReplaceUnderscore( label
) );
542 menu
->SetTitle( str
);
546 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
549 gtk_label_set( glabel
, wxGTK_CONV( str
) );
551 /* reparse key accel */
552 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
553 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
558 //-----------------------------------------------------------------------------
560 //-----------------------------------------------------------------------------
563 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
566 wxapp_install_idle_handler();
568 int id
= menu
->FindMenuIdByMenuItem(widget
);
570 /* should find it for normal (not popup) menu */
571 wxASSERT_MSG( (id
!= -1) || (menu
->GetWindow() != NULL
),
572 wxT("menu item not found in gtk_menu_clicked_callback") );
574 if (!menu
->IsEnabled(id
))
577 wxMenuItem
* item
= menu
->FindChildItem( id
);
578 wxCHECK_RET( item
, wxT("error in menu item callback") );
580 if ( item
->GetId() == wxGTK_TITLE_ID
)
582 // ignore events from the menu title
586 if (item
->IsCheckable())
588 bool isReallyChecked
= item
->IsChecked(),
589 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
591 // ensure that the internal state is always consistent with what is
592 // shown on the screen
593 item
->wxMenuItemBase::Check(isReallyChecked
);
595 // we must not report the events for the radio button going up nor the
596 // events resulting from the calls to wxMenuItem::Check()
597 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
598 (isInternallyChecked
== isReallyChecked
) )
605 // Is this menu on a menubar? (possibly nested)
606 wxFrame
* frame
= NULL
;
607 if(menu
->IsAttached())
608 frame
= menu
->GetMenuBar()->GetFrame();
610 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
611 // normally wxMenu::SendEvent() should be enough, if it doesn't work
612 // in wxGTK then we have a bug in wxMenu::GetWindow() which
613 // should be fixed instead of working around it here...
616 // If it is attached then let the frame send the event.
617 // Don't call frame->ProcessCommand(id) because it toggles
618 // checkable items and we've already done that above.
619 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
620 commandEvent
.SetEventObject(frame
);
621 if (item
->IsCheckable())
622 commandEvent
.SetInt(item
->IsChecked());
623 commandEvent
.SetEventObject(menu
);
625 frame
->HandleWindowEvent(commandEvent
);
629 // otherwise let the menu have it
630 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
635 //-----------------------------------------------------------------------------
637 //-----------------------------------------------------------------------------
640 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
642 if (g_isIdle
) wxapp_install_idle_handler();
644 int id
= menu
->FindMenuIdByMenuItem(widget
);
646 wxASSERT( id
!= -1 ); // should find it!
648 if (!menu
->IsEnabled(id
))
651 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
652 event
.SetEventObject( menu
);
654 wxEvtHandler
* handler
= menu
->GetEventHandler();
655 if (handler
&& handler
->ProcessEvent(event
))
658 wxWindow
*win
= menu
->GetWindow();
659 if (win
) win
->HandleWindowEvent( event
);
663 //-----------------------------------------------------------------------------
665 //-----------------------------------------------------------------------------
668 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
670 if (g_isIdle
) wxapp_install_idle_handler();
672 int id
= menu
->FindMenuIdByMenuItem(widget
);
674 wxASSERT( id
!= -1 ); // should find it!
676 if (!menu
->IsEnabled(id
))
679 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
680 event
.SetEventObject( menu
);
682 wxEvtHandler
* handler
= menu
->GetEventHandler();
683 if (handler
&& handler
->ProcessEvent(event
))
686 wxWindow
*win
= menu
->GetWindow();
688 win
->HandleWindowEvent( event
);
692 //-----------------------------------------------------------------------------
694 //-----------------------------------------------------------------------------
696 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
698 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
700 const wxString
& name
,
701 const wxString
& help
,
705 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
708 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
710 const wxString
& text
,
711 const wxString
& help
,
714 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
719 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
721 const wxString
& text
,
722 const wxString
& help
,
725 : wxMenuItemBase(parentMenu
, id
, text
, help
,
726 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
731 void wxMenuItem::Init()
733 m_labelWidget
= NULL
;
739 wxMenuItem::~wxMenuItem()
741 // don't delete menu items, the menus take care of that
744 wxString
wxMenuItem::GetItemLabel() const
746 wxString label
= wxConvertFromGTKToWXLabel(m_text
);
747 if (!m_hotKey
.IsEmpty())
748 label
= label
+ wxT("\t") + m_hotKey
;
752 void wxMenuItem::SetItemLabel( const wxString
& string
)
754 wxString str
= string
;
755 if ( str
.empty() && !IsSeparator() )
757 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
758 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
759 wxSTOCK_WITH_MNEMONIC
);
762 // Some optimization to avoid flicker
763 wxString oldLabel
= m_text
;
764 oldLabel
= wxStripMenuCodes(oldLabel
);
765 oldLabel
.Replace(wxT("_"), wxEmptyString
);
766 wxString label1
= wxStripMenuCodes(str
);
767 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
768 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
772 if (oldLabel
== label1
&&
773 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
780 label
= (GtkLabel
*) m_labelWidget
;
782 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
785 gtk_label_set( label
, wxGTK_CONV( m_text
) );
788 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
789 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
793 GdkModifierType accel_mods
;
794 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
797 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
798 m_parentMenu
->m_accel
,
803 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
804 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
807 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
809 m_parentMenu
->m_accel
,
816 // it's valid for this function to be called even if m_menuItem == NULL
817 void wxMenuItem::DoSetText( const wxString
& str
)
819 // '\t' is the deliminator indicating a hot key
821 text
.reserve(str
.length());
823 const wxChar
*pc
= str
;
824 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
826 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
828 // "&" is doubled to indicate "&" instead of accelerator
832 else if (*pc
== wxT('&'))
836 else if ( *pc
== wxT('_') ) // escape underscores
847 m_hotKey
= wxEmptyString
;
849 if ( *pc
== wxT('\t') )
860 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
868 // accelerator parsing code looks for them after a TAB, so insert a dummy
871 label
<< wxT('\t') << GetHotKey();
873 return wxAcceleratorEntry::Create(label
);
876 #endif // wxUSE_ACCEL
878 void wxMenuItem::Check( bool check
)
880 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
882 if (check
== m_isChecked
)
885 wxMenuItemBase::Check( check
);
891 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
895 wxFAIL_MSG( wxT("can't check this item") );
899 void wxMenuItem::Enable( bool enable
)
901 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
903 gtk_widget_set_sensitive( m_menuItem
, enable
);
904 wxMenuItemBase::Enable( enable
);
907 bool wxMenuItem::IsChecked() const
909 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
911 wxCHECK_MSG( IsCheckable(), false,
912 wxT("can't get state of uncheckable item!") );
914 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
917 //-----------------------------------------------------------------------------
919 //-----------------------------------------------------------------------------
921 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
925 m_accel
= gtk_accel_group_new();
926 m_menu
= gtk_menu_new();
927 // NB: keep reference to the menu so that it is not destroyed behind
928 // our back by GTK+ e.g. when it is removed from menubar:
929 gtk_widget_ref(m_menu
);
933 // Tearoffs are entries, just like separators. So if we want this
934 // menu to be a tear-off one, we just append a tearoff entry
936 if ( m_style
& wxMENU_TEAROFF
)
938 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
940 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
945 // append the title as the very first entry if we have it
946 if ( !m_title
.empty() )
948 Append(wxGTK_TITLE_ID
, m_title
);
955 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
957 if ( GTK_IS_WIDGET( m_menu
))
960 gtk_widget_unref( m_menu
);
961 // if the menu is inserted in another menu at this time, there was
962 // one more reference to it:
964 gtk_widget_destroy( m_menu
);
968 wxString
wxMenu::GetTitle() const
970 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
973 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
978 GtkLabel
* label
= NULL
;
980 if ( mitem
->IsSeparator() )
983 menuItem
= gtk_menu_item_new();
985 else if (mitem
->GetBitmap().Ok())
987 text
= mitem
->wxMenuItemBase::GetItemLabel();
988 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
992 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
993 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
997 else // a normal item
999 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
1000 text
= mitem
->wxMenuItemBase::GetItemLabel() ;
1002 switch ( mitem
->GetKind() )
1006 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1007 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1009 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1016 GSList
*group
= NULL
;
1017 if ( m_prevRadio
== NULL
)
1019 // start of a new radio group
1020 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1021 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1023 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1025 else // continue the radio group
1027 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1028 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1029 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1035 wxFAIL_MSG( wxT("unexpected menu item kind") );
1040 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1041 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1050 GdkModifierType accel_mods
;
1051 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1053 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1054 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1057 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1066 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1068 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1070 gtk_widget_show( menuItem
);
1072 if ( !mitem
->IsSeparator() )
1074 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1076 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1077 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1080 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1081 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1084 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1086 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1088 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1092 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1093 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1097 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1098 if (accel_key
!= GDK_VoidSymbol
)
1100 gtk_widget_add_accelerator (menuItem
,
1102 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1109 mitem
->SetMenuItem(menuItem
);
1113 // This doesn't even exist!
1114 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1120 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1122 if (!GtkAppend(mitem
))
1125 return wxMenuBase::DoAppend(mitem
);
1128 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1130 if ( !wxMenuBase::DoInsert(pos
, item
) )
1134 if ( !GtkAppend(item
, (int)pos
) )
1140 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1142 if ( !wxMenuBase::DoRemove(item
) )
1145 // TODO: this code doesn't delete the item factory item and this seems
1146 // impossible as of GTK 1.2.6.
1147 gtk_widget_destroy( item
->GetMenuItem() );
1152 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1154 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1157 wxMenuItem
*item
= node
->GetData();
1158 if (item
->GetMenuItem() == menuItem
)
1159 return item
->GetId();
1160 node
= node
->GetNext();
1166 // ----------------------------------------------------------------------------
1168 // ----------------------------------------------------------------------------
1172 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1176 wxAcceleratorEntry
*accel
= item
.GetAccel();
1179 int flags
= accel
->GetFlags();
1180 if ( flags
& wxACCEL_ALT
)
1181 hotkey
+= wxT("<alt>");
1182 if ( flags
& wxACCEL_CTRL
)
1183 hotkey
+= wxT("<control>");
1184 if ( flags
& wxACCEL_SHIFT
)
1185 hotkey
+= wxT("<shift>");
1187 int code
= accel
->GetKeyCode();
1214 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1217 // TODO: we should use gdk_keyval_name() (a.k.a.
1218 // XKeysymToString) here as well as hardcoding the keysym
1219 // names this might be not portable
1221 hotkey
<< wxT("Insert" );
1224 hotkey
<< wxT("Delete" );
1227 hotkey
<< wxT("Up" );
1230 hotkey
<< wxT("Down" );
1233 hotkey
<< wxT("Page_Up" );
1236 hotkey
<< wxT("Page_Down" );
1239 hotkey
<< wxT("Left" );
1242 hotkey
<< wxT("Right" );
1245 hotkey
<< wxT("Home" );
1248 hotkey
<< wxT("End" );
1251 hotkey
<< wxT("Return" );
1254 hotkey
<< wxT("BackSpace" );
1257 hotkey
<< wxT("Tab" );
1260 hotkey
<< wxT("Esc" );
1263 hotkey
<< wxT("space" );
1266 hotkey
<< wxT("Multiply" );
1269 hotkey
<< wxT("Add" );
1272 hotkey
<< wxT("Separator" );
1275 hotkey
<< wxT("Subtract" );
1278 hotkey
<< wxT("Decimal" );
1281 hotkey
<< wxT("Divide" );
1284 hotkey
<< wxT("Cancel" );
1287 hotkey
<< wxT("Clear" );
1290 hotkey
<< wxT("Menu" );
1293 hotkey
<< wxT("Pause" );
1296 hotkey
<< wxT("Capital" );
1299 hotkey
<< wxT("Select" );
1302 hotkey
<< wxT("Print" );
1305 hotkey
<< wxT("Execute" );
1308 hotkey
<< wxT("Snapshot" );
1311 hotkey
<< wxT("Help" );
1314 hotkey
<< wxT("Num_Lock" );
1317 hotkey
<< wxT("Scroll_Lock" );
1319 case WXK_NUMPAD_INSERT
:
1320 hotkey
<< wxT("KP_Insert" );
1322 case WXK_NUMPAD_DELETE
:
1323 hotkey
<< wxT("KP_Delete" );
1325 case WXK_NUMPAD_SPACE
:
1326 hotkey
<< wxT("KP_Space" );
1328 case WXK_NUMPAD_TAB
:
1329 hotkey
<< wxT("KP_Tab" );
1331 case WXK_NUMPAD_ENTER
:
1332 hotkey
<< wxT("KP_Enter" );
1334 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1336 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1338 case WXK_NUMPAD_HOME
:
1339 hotkey
<< wxT("KP_Home" );
1341 case WXK_NUMPAD_LEFT
:
1342 hotkey
<< wxT("KP_Left" );
1345 hotkey
<< wxT("KP_Up" );
1347 case WXK_NUMPAD_RIGHT
:
1348 hotkey
<< wxT("KP_Right" );
1350 case WXK_NUMPAD_DOWN
:
1351 hotkey
<< wxT("KP_Down" );
1353 case WXK_NUMPAD_PAGEUP
:
1354 hotkey
<< wxT("KP_Page_Up" );
1356 case WXK_NUMPAD_PAGEDOWN
:
1357 hotkey
<< wxT("KP_Page_Down" );
1359 case WXK_NUMPAD_END
:
1360 hotkey
<< wxT("KP_End" );
1362 case WXK_NUMPAD_BEGIN
:
1363 hotkey
<< wxT("KP_Begin" );
1365 case WXK_NUMPAD_EQUAL
:
1366 hotkey
<< wxT("KP_Equal" );
1368 case WXK_NUMPAD_MULTIPLY
:
1369 hotkey
<< wxT("KP_Multiply" );
1371 case WXK_NUMPAD_ADD
:
1372 hotkey
<< wxT("KP_Add" );
1374 case WXK_NUMPAD_SEPARATOR
:
1375 hotkey
<< wxT("KP_Separator" );
1377 case WXK_NUMPAD_SUBTRACT
:
1378 hotkey
<< wxT("KP_Subtract" );
1380 case WXK_NUMPAD_DECIMAL
:
1381 hotkey
<< wxT("KP_Decimal" );
1383 case WXK_NUMPAD_DIVIDE
:
1384 hotkey
<< wxT("KP_Divide" );
1386 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1387 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1388 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1389 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1391 case WXK_WINDOWS_LEFT
:
1392 hotkey
<< wxT("Super_L" );
1394 case WXK_WINDOWS_RIGHT
:
1395 hotkey
<< wxT("Super_R" );
1397 case WXK_WINDOWS_MENU
:
1398 hotkey
<< wxT("Menu" );
1401 hotkey
<< wxT("Command" );
1403 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1404 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1405 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1406 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1407 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1408 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1409 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1412 // if there are any other keys wxAcceleratorEntry::Create() may
1413 // return, we should process them here
1418 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1419 if ( !name
.empty() )
1426 wxFAIL_MSG( wxT("unknown keyboard accel") );
1435 #endif // wxUSE_ACCEL
1437 // ----------------------------------------------------------------------------
1438 // Pop-up menu stuff
1439 // ----------------------------------------------------------------------------
1441 #if wxUSE_MENUS_NATIVE
1443 extern "C" WXDLLIMPEXP_CORE
1444 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1446 *is_waiting
= false;
1449 extern "C" WXDLLIMPEXP_CORE
1450 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1452 gpointer user_data
)
1454 // ensure that the menu appears entirely on screen
1456 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1458 wxSize sizeScreen
= wxGetDisplaySize();
1459 wxPoint
*pos
= (wxPoint
*)user_data
;
1461 gint xmax
= sizeScreen
.x
- req
.width
,
1462 ymax
= sizeScreen
.y
- req
.height
;
1464 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1465 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1468 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1470 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1472 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1474 // NOTE: if you change this code, you need to update
1475 // the same code in taskbar.cpp as well. This
1476 // is ugly code duplication, I know.
1480 bool is_waiting
= true;
1482 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1484 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1485 (gpointer
)&is_waiting
);
1489 GtkMenuPositionFunc posfunc
;
1490 if ( x
== -1 && y
== -1 )
1492 // use GTK's default positioning algorithm
1498 pos
= ClientToScreen(wxPoint(x
, y
));
1500 posfunc
= wxPopupMenuPositionCallback
;
1503 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1504 DoCommonMenuCallbackCode(menu
, eventOpen
);
1507 GTK_MENU(menu
->m_menu
),
1508 NULL
, // parent menu shell
1509 NULL
, // parent menu item
1510 posfunc
, // function to position it
1511 userdata
, // client data
1512 0, // button used to activate it
1513 wxGtkTimeLastClick
// the time of activation
1518 gtk_main_iteration();
1521 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1523 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1524 DoCommonMenuCallbackCode(menu
, eventClose
);
1529 #endif // wxUSE_MENUS_NATIVE