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"
19 #include "wx/bitmap.h"
26 #include "wx/gtk1/private.h"
28 #include <gdk/gdkkeysyms.h>
30 #define ACCEL_OBJECT GtkObject
31 #define ACCEL_OBJECTS(a) (a)->attach_objects
32 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
34 // we use normal item but with a special id for the menu title
35 static const int wxGTK_TITLE_ID
= -3;
37 // defined in window.cpp
38 extern guint32 wxGtkTimeLastClick
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern void wxapp_install_idle_handler();
48 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 static wxString
wxReplaceUnderscore( const wxString
& title
)
59 // GTK 1.2 wants to have "_" instead of "&" for accelerators
62 while (*pc
!= wxT('\0'))
64 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
66 // "&" is doubled to indicate "&" instead of accelerator
70 else if (*pc
== wxT('&'))
76 if ( *pc
== wxT('_') )
78 // underscores must be doubled to prevent them from being
79 // interpreted as accelerator character prefix by GTK
88 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
93 //-----------------------------------------------------------------------------
94 // activate message from GTK
95 //-----------------------------------------------------------------------------
97 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
100 wxapp_install_idle_handler();
102 event
.SetEventObject( menu
);
104 wxEvtHandler
* handler
= menu
->GetEventHandler();
105 if (handler
&& handler
->ProcessEvent(event
))
108 wxWindow
*win
= menu
->GetInvokingWindow();
110 win
->GetEventHandler()->ProcessEvent( event
);
115 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
117 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
119 DoCommonMenuCallbackCode(menu
, event
);
122 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
124 if ( !menubar
->GetMenuCount() )
126 // if menubar is empty we can't call GetMenu(0) below
130 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
132 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
143 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
145 // the parent window is known after wxFrame::SetMenu()
146 m_needParent
= false;
148 m_invokingWindow
= (wxWindow
*) NULL
;
150 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
151 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
153 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
157 m_menubar
= gtk_menu_bar_new();
158 m_accel
= gtk_accel_group_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 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
184 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
189 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
191 Init(n
, menus
, titles
, style
);
194 wxMenuBar::wxMenuBar(long style
)
196 Init(0, NULL
, NULL
, style
);
199 wxMenuBar::wxMenuBar()
201 Init(0, NULL
, NULL
, 0);
204 wxMenuBar::~wxMenuBar()
208 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
210 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
212 wxWindow
*top_frame
= win
;
213 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
214 top_frame
= top_frame
->GetParent();
216 // support for native hot keys
217 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
219 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
222 wxMenuItem
*menuitem
= node
->GetData();
223 if (menuitem
->IsSubMenu())
224 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
225 node
= node
->GetNext();
229 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
231 menu
->SetInvokingWindow( win
);
233 wxWindow
*top_frame
= win
;
234 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
235 top_frame
= top_frame
->GetParent();
237 // support for native hot keys
238 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
239 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
240 gtk_accel_group_attach( menu
->m_accel
, obj
);
242 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
245 wxMenuItem
*menuitem
= node
->GetData();
246 if (menuitem
->IsSubMenu())
247 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
248 node
= node
->GetNext();
252 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
254 m_invokingWindow
= win
;
255 wxWindow
*top_frame
= win
;
256 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
257 top_frame
= top_frame
->GetParent();
259 // support for native key accelerators indicated by underscroes
260 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
261 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
262 gtk_accel_group_attach( m_accel
, obj
);
264 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
267 wxMenu
*menu
= node
->GetData();
268 wxMenubarSetInvokingWindow( menu
, win
);
269 node
= node
->GetNext();
273 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
275 m_invokingWindow
= (wxWindow
*) NULL
;
276 wxWindow
*top_frame
= win
;
277 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
278 top_frame
= top_frame
->GetParent();
280 // support for native key accelerators indicated by underscroes
281 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
283 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
286 wxMenu
*menu
= node
->GetData();
287 wxMenubarUnsetInvokingWindow( menu
, win
);
288 node
= node
->GetNext();
292 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
294 if ( !wxMenuBarBase::Append( menu
, title
) )
297 return GtkAppend(menu
, title
);
300 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
302 wxString
str( wxReplaceUnderscore( title
) );
304 // This doesn't have much effect right now.
305 menu
->SetTitle( str
);
307 // The "m_owner" is the "menu item"
308 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
309 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
311 gtk_label_set_text( label
, wxGTK_CONV( str
) );
313 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
314 if (accel_key
!= GDK_VoidSymbol
)
316 gtk_widget_add_accelerator (menu
->m_owner
,
318 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
324 gtk_widget_show( menu
->m_owner
);
326 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
329 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
331 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
333 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
334 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
337 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
338 // addings menu later on.
339 if (m_invokingWindow
)
341 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
343 // OPTIMISE ME: we should probably cache this, or pass it
344 // directly, but for now this is a minimal
345 // change to validate the new dynamic sizing.
346 // see (and refactor :) similar code in Remove
349 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
352 frame
->UpdateMenuBarSize();
358 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
360 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
365 if ( !GtkAppend(menu
, title
, (int)pos
) )
371 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
373 // remove the old item and insert a new one
374 wxMenu
*menuOld
= Remove(pos
);
375 if ( menuOld
&& !Insert(pos
, menu
, title
) )
377 return (wxMenu
*) NULL
;
380 // either Insert() succeeded or Remove() failed and menuOld is NULL
384 wxMenu
*wxMenuBar::Remove(size_t pos
)
386 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
388 return (wxMenu
*) NULL
;
390 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
391 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
393 gtk_widget_destroy( menu
->m_owner
);
394 menu
->m_owner
= NULL
;
396 if (m_invokingWindow
)
398 // OPTIMISE ME: see comment in GtkAppend
399 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
402 frame
->UpdateMenuBarSize();
408 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
410 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
412 int res
= menu
->FindItem( itemString
);
413 if (res
!= wxNOT_FOUND
)
417 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
420 wxMenuItem
*item
= node
->GetData();
421 if (item
->IsSubMenu())
422 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
424 node
= node
->GetNext();
430 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
432 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
435 wxMenu
*menu
= node
->GetData();
436 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
439 node
= node
->GetNext();
445 // Find a wxMenuItem using its id. Recurses down into sub-menus
446 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
448 wxMenuItem
* result
= menu
->FindChildItem(id
);
450 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
451 while ( node
&& result
== NULL
)
453 wxMenuItem
*item
= node
->GetData();
454 if (item
->IsSubMenu())
456 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
458 node
= node
->GetNext();
464 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
466 wxMenuItem
* result
= 0;
467 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
468 while (node
&& result
== 0)
470 wxMenu
*menu
= node
->GetData();
471 result
= FindMenuItemByIdRecursive( menu
, id
);
472 node
= node
->GetNext();
477 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
483 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
485 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
487 wxCHECK_RET( node
, wxT("menu not found") );
489 wxMenu
* menu
= node
->GetData();
492 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
495 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
497 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
499 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
501 wxMenu
* menu
= node
->GetData();
504 wxString
text( menu
->GetTitle() );
505 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
507 if ( *pc
== wxT('_') )
509 // '_' is the escape character for GTK+
513 // don't remove ampersands '&' since if we have them in the menu title
514 // it means that they were doubled to indicate "&" instead of accelerator
522 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
524 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
526 wxCHECK_RET( node
, wxT("menu not found") );
528 wxMenu
* menu
= node
->GetData();
530 const wxString
str( wxReplaceUnderscore( label
) );
532 menu
->SetTitle( str
);
536 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
539 gtk_label_set( glabel
, wxGTK_CONV( str
) );
541 /* reparse key accel */
542 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
543 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
548 //-----------------------------------------------------------------------------
550 //-----------------------------------------------------------------------------
553 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
556 wxapp_install_idle_handler();
558 int id
= menu
->FindMenuIdByMenuItem(widget
);
560 /* should find it for normal (not popup) menu */
561 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
562 _T("menu item not found in gtk_menu_clicked_callback") );
564 if (!menu
->IsEnabled(id
))
567 wxMenuItem
* item
= menu
->FindChildItem( id
);
568 wxCHECK_RET( item
, wxT("error in menu item callback") );
570 if ( item
->GetId() == wxGTK_TITLE_ID
)
572 // ignore events from the menu title
576 if (item
->IsCheckable())
578 bool isReallyChecked
= item
->IsChecked(),
579 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
581 // ensure that the internal state is always consistent with what is
582 // shown on the screen
583 item
->wxMenuItemBase::Check(isReallyChecked
);
585 // we must not report the events for the radio button going up nor the
586 // events resulting from the calls to wxMenuItem::Check()
587 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
588 (isInternallyChecked
== isReallyChecked
) )
595 // Is this menu on a menubar? (possibly nested)
596 wxFrame
* frame
= NULL
;
597 if(menu
->IsAttached())
598 frame
= menu
->GetMenuBar()->GetFrame();
600 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
601 // normally wxMenu::SendEvent() should be enough, if it doesn't work
602 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
603 // should be fixed instead of working around it here...
606 // If it is attached then let the frame send the event.
607 // Don't call frame->ProcessCommand(id) because it toggles
608 // checkable items and we've already done that above.
609 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
610 commandEvent
.SetEventObject(frame
);
611 if (item
->IsCheckable())
612 commandEvent
.SetInt(item
->IsChecked());
613 commandEvent
.SetEventObject(menu
);
615 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
619 // otherwise let the menu have it
620 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
625 //-----------------------------------------------------------------------------
627 //-----------------------------------------------------------------------------
630 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
632 if (g_isIdle
) wxapp_install_idle_handler();
634 int id
= menu
->FindMenuIdByMenuItem(widget
);
636 wxASSERT( id
!= -1 ); // should find it!
638 if (!menu
->IsEnabled(id
))
641 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
642 event
.SetEventObject( menu
);
644 wxEvtHandler
* handler
= menu
->GetEventHandler();
645 if (handler
&& handler
->ProcessEvent(event
))
648 wxWindow
*win
= menu
->GetInvokingWindow();
649 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
653 //-----------------------------------------------------------------------------
655 //-----------------------------------------------------------------------------
658 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
660 if (g_isIdle
) wxapp_install_idle_handler();
662 int id
= menu
->FindMenuIdByMenuItem(widget
);
664 wxASSERT( id
!= -1 ); // should find it!
666 if (!menu
->IsEnabled(id
))
669 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
670 event
.SetEventObject( menu
);
672 wxEvtHandler
* handler
= menu
->GetEventHandler();
673 if (handler
&& handler
->ProcessEvent(event
))
676 wxWindow
*win
= menu
->GetInvokingWindow();
678 win
->GetEventHandler()->ProcessEvent( event
);
682 //-----------------------------------------------------------------------------
684 //-----------------------------------------------------------------------------
686 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
688 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
690 const wxString
& name
,
691 const wxString
& help
,
695 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
698 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
700 const wxString
& text
,
701 const wxString
& help
,
704 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
709 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
711 const wxString
& text
,
712 const wxString
& help
,
715 : wxMenuItemBase(parentMenu
, id
, text
, help
,
716 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
721 void wxMenuItem::Init(const wxString
& text
)
723 m_labelWidget
= (GtkWidget
*) NULL
;
724 m_menuItem
= (GtkWidget
*) NULL
;
729 wxMenuItem::~wxMenuItem()
731 // don't delete menu items, the menus take care of that
734 // return the menu item text without any menu accels
736 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
740 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
742 if ( *pc
== wxT('\t'))
745 if ( *pc
== wxT('_') )
747 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
753 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
756 // "&" is doubled to indicate "&" instead of accelerator
763 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
768 void wxMenuItem::SetText( const wxString
& str
)
770 // Some optimization to avoid flicker
771 wxString oldLabel
= m_text
;
772 oldLabel
= wxStripMenuCodes(oldLabel
);
773 oldLabel
.Replace(wxT("_"), wxEmptyString
);
774 wxString label1
= wxStripMenuCodes(str
);
775 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
776 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
780 if (oldLabel
== label1
&&
781 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
788 label
= (GtkLabel
*) m_labelWidget
;
790 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
793 gtk_label_set( label
, wxGTK_CONV( m_text
) );
796 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
797 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
801 GdkModifierType accel_mods
;
802 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
805 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
806 m_parentMenu
->m_accel
,
811 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
812 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
815 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
817 m_parentMenu
->m_accel
,
824 // it's valid for this function to be called even if m_menuItem == NULL
825 void wxMenuItem::DoSetText( const wxString
& str
)
827 // '\t' is the deliminator indicating a hot key
829 const wxChar
*pc
= str
;
830 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
832 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
834 // "&" is doubled to indicate "&" instead of accelerator
838 else if (*pc
== wxT('&'))
842 else if ( *pc
== wxT('_') ) // escape underscores
853 m_hotKey
= wxEmptyString
;
861 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
866 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
871 return (wxAcceleratorEntry
*)NULL
;
874 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
876 label
<< wxT('\t') << GetHotKey();
878 return wxGetAccelFromString(label
);
881 #endif // wxUSE_ACCEL
883 void wxMenuItem::Check( bool check
)
885 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
887 if (check
== m_isChecked
)
890 wxMenuItemBase::Check( check
);
896 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
900 wxFAIL_MSG( _T("can't check this item") );
904 void wxMenuItem::Enable( bool enable
)
906 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
908 gtk_widget_set_sensitive( m_menuItem
, enable
);
909 wxMenuItemBase::Enable( enable
);
912 bool wxMenuItem::IsChecked() const
914 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
916 wxCHECK_MSG( IsCheckable(), false,
917 wxT("can't get state of uncheckable item!") );
919 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
922 //-----------------------------------------------------------------------------
924 //-----------------------------------------------------------------------------
926 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
930 m_accel
= gtk_accel_group_new();
931 m_menu
= gtk_menu_new();
932 // NB: keep reference to the menu so that it is not destroyed behind
933 // our back by GTK+ e.g. when it is removed from menubar:
934 gtk_widget_ref(m_menu
);
936 m_owner
= (GtkWidget
*) NULL
;
938 // Tearoffs are entries, just like separators. So if we want this
939 // menu to be a tear-off one, we just append a tearoff entry
941 if ( m_style
& wxMENU_TEAROFF
)
943 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
945 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
950 // append the title as the very first entry if we have it
951 if ( !m_title
.empty() )
953 Append(wxGTK_TITLE_ID
, m_title
);
960 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
962 if ( GTK_IS_WIDGET( m_menu
))
965 gtk_widget_unref( m_menu
);
966 // if the menu is inserted in another menu at this time, there was
967 // one more reference to it:
969 gtk_widget_destroy( m_menu
);
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
->GetText();
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->SetText() so don't use it
1000 text
= mitem
->GetText() ;
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( _T("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->GetText().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
);
1090 // if adding a submenu to a menu already existing in the menu bar, we
1091 // must set invoking window to allow processing events from this
1093 if ( m_invokingWindow
)
1094 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1098 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1099 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1103 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1104 if (accel_key
!= GDK_VoidSymbol
)
1106 gtk_widget_add_accelerator (menuItem
,
1108 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1115 mitem
->SetMenuItem(menuItem
);
1119 // This doesn't even exist!
1120 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1126 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1128 if (!GtkAppend(mitem
))
1131 return wxMenuBase::DoAppend(mitem
);
1134 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1136 if ( !wxMenuBase::DoInsert(pos
, item
) )
1140 if ( !GtkAppend(item
, (int)pos
) )
1146 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1148 if ( !wxMenuBase::DoRemove(item
) )
1149 return (wxMenuItem
*)NULL
;
1151 // TODO: this code doesn't delete the item factory item and this seems
1152 // impossible as of GTK 1.2.6.
1153 gtk_widget_destroy( item
->GetMenuItem() );
1158 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1160 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1163 wxMenuItem
*item
= node
->GetData();
1164 if (item
->GetMenuItem() == menuItem
)
1165 return item
->GetId();
1166 node
= node
->GetNext();
1172 // ----------------------------------------------------------------------------
1174 // ----------------------------------------------------------------------------
1178 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1182 wxAcceleratorEntry
*accel
= item
.GetAccel();
1185 int flags
= accel
->GetFlags();
1186 if ( flags
& wxACCEL_ALT
)
1187 hotkey
+= wxT("<alt>");
1188 if ( flags
& wxACCEL_CTRL
)
1189 hotkey
+= wxT("<control>");
1190 if ( flags
& wxACCEL_SHIFT
)
1191 hotkey
+= wxT("<shift>");
1193 int code
= accel
->GetKeyCode();
1220 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1223 // TODO: we should use gdk_keyval_name() (a.k.a.
1224 // XKeysymToString) here as well as hardcoding the keysym
1225 // names this might be not portable
1227 hotkey
<< wxT("Insert" );
1230 hotkey
<< wxT("Delete" );
1233 hotkey
<< wxT("Up" );
1236 hotkey
<< wxT("Down" );
1239 hotkey
<< wxT("PgUp" );
1242 hotkey
<< wxT("PgDn" );
1245 hotkey
<< wxT("Left" );
1248 hotkey
<< wxT("Right" );
1251 hotkey
<< wxT("Home" );
1254 hotkey
<< wxT("End" );
1257 hotkey
<< wxT("Return" );
1260 hotkey
<< wxT("BackSpace" );
1263 hotkey
<< wxT("Tab" );
1266 hotkey
<< wxT("Esc" );
1269 hotkey
<< wxT("space" );
1272 hotkey
<< wxT("Multiply" );
1275 hotkey
<< wxT("Add" );
1278 hotkey
<< wxT("Separator" );
1281 hotkey
<< wxT("Subtract" );
1284 hotkey
<< wxT("Decimal" );
1287 hotkey
<< wxT("Divide" );
1290 hotkey
<< wxT("Cancel" );
1293 hotkey
<< wxT("Clear" );
1296 hotkey
<< wxT("Menu" );
1299 hotkey
<< wxT("Pause" );
1302 hotkey
<< wxT("Capital" );
1305 hotkey
<< wxT("Select" );
1308 hotkey
<< wxT("Print" );
1311 hotkey
<< wxT("Execute" );
1314 hotkey
<< wxT("Snapshot" );
1317 hotkey
<< wxT("Help" );
1320 hotkey
<< wxT("Num_Lock" );
1323 hotkey
<< wxT("Scroll_Lock" );
1325 case WXK_NUMPAD_INSERT
:
1326 hotkey
<< wxT("KP_Insert" );
1328 case WXK_NUMPAD_DELETE
:
1329 hotkey
<< wxT("KP_Delete" );
1331 case WXK_NUMPAD_SPACE
:
1332 hotkey
<< wxT("KP_Space" );
1334 case WXK_NUMPAD_TAB
:
1335 hotkey
<< wxT("KP_Tab" );
1337 case WXK_NUMPAD_ENTER
:
1338 hotkey
<< wxT("KP_Enter" );
1340 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1342 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1344 case WXK_NUMPAD_HOME
:
1345 hotkey
<< wxT("KP_Home" );
1347 case WXK_NUMPAD_LEFT
:
1348 hotkey
<< wxT("KP_Left" );
1351 hotkey
<< wxT("KP_Up" );
1353 case WXK_NUMPAD_RIGHT
:
1354 hotkey
<< wxT("KP_Right" );
1356 case WXK_NUMPAD_DOWN
:
1357 hotkey
<< wxT("KP_Down" );
1359 case WXK_NUMPAD_PAGEUP
:
1360 hotkey
<< wxT("KP_PgUp" );
1362 case WXK_NUMPAD_PAGEDOWN
:
1363 hotkey
<< wxT("KP_PgDn" );
1365 case WXK_NUMPAD_END
:
1366 hotkey
<< wxT("KP_End" );
1368 case WXK_NUMPAD_BEGIN
:
1369 hotkey
<< wxT("KP_Begin" );
1371 case WXK_NUMPAD_EQUAL
:
1372 hotkey
<< wxT("KP_Equal" );
1374 case WXK_NUMPAD_MULTIPLY
:
1375 hotkey
<< wxT("KP_Multiply" );
1377 case WXK_NUMPAD_ADD
:
1378 hotkey
<< wxT("KP_Add" );
1380 case WXK_NUMPAD_SEPARATOR
:
1381 hotkey
<< wxT("KP_Separator" );
1383 case WXK_NUMPAD_SUBTRACT
:
1384 hotkey
<< wxT("KP_Subtract" );
1386 case WXK_NUMPAD_DECIMAL
:
1387 hotkey
<< wxT("KP_Decimal" );
1389 case WXK_NUMPAD_DIVIDE
:
1390 hotkey
<< wxT("KP_Divide" );
1392 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1393 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1394 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1395 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1397 case WXK_WINDOWS_LEFT
:
1398 hotkey
<< wxT("Super_L" );
1400 case WXK_WINDOWS_RIGHT
:
1401 hotkey
<< wxT("Super_R" );
1403 case WXK_WINDOWS_MENU
:
1404 hotkey
<< wxT("Menu" );
1407 hotkey
<< wxT("Command" );
1409 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1410 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1411 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1412 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1413 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1414 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1415 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1418 // if there are any other keys wxGetAccelFromString() may
1419 // return, we should process them here
1424 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1432 wxFAIL_MSG( wxT("unknown keyboard accel") );
1441 #endif // wxUSE_ACCEL
1443 // ----------------------------------------------------------------------------
1444 // Pop-up menu stuff
1445 // ----------------------------------------------------------------------------
1447 #if wxUSE_MENUS_NATIVE
1449 extern "C" WXDLLIMPEXP_CORE
1450 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1452 *is_waiting
= false;
1455 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1457 menu
->SetInvokingWindow( win
);
1459 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1462 wxMenuItem
*menuitem
= node
->GetData();
1463 if (menuitem
->IsSubMenu())
1465 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1468 node
= node
->GetNext();
1472 extern "C" WXDLLIMPEXP_CORE
1473 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1475 gpointer user_data
)
1477 // ensure that the menu appears entirely on screen
1479 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1481 wxSize sizeScreen
= wxGetDisplaySize();
1482 wxPoint
*pos
= (wxPoint
*)user_data
;
1484 gint xmax
= sizeScreen
.x
- req
.width
,
1485 ymax
= sizeScreen
.y
- req
.height
;
1487 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1488 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1491 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1493 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1495 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1497 // NOTE: if you change this code, you need to update
1498 // the same code in taskbar.cpp as well. This
1499 // is ugly code duplication, I know.
1501 SetInvokingWindow( menu
, this );
1505 bool is_waiting
= true;
1507 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1509 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1510 (gpointer
)&is_waiting
);
1514 GtkMenuPositionFunc posfunc
;
1515 if ( x
== -1 && y
== -1 )
1517 // use GTK's default positioning algorithm
1523 pos
= ClientToScreen(wxPoint(x
, y
));
1525 posfunc
= wxPopupMenuPositionCallback
;
1528 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1529 DoCommonMenuCallbackCode(menu
, eventOpen
);
1532 GTK_MENU(menu
->m_menu
),
1533 (GtkWidget
*) NULL
, // parent menu shell
1534 (GtkWidget
*) NULL
, // parent menu item
1535 posfunc
, // function to position it
1536 userdata
, // client data
1537 0, // button used to activate it
1538 wxGtkTimeLastClick
// the time of activation
1543 gtk_main_iteration();
1546 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1548 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1549 DoCommonMenuCallbackCode(menu
, eventClose
);
1554 #endif // wxUSE_MENUS_NATIVE