1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/bitmap.h"
23 #include "wx/gtk/private.h"
25 #include <gdk/gdkkeysyms.h>
27 // FIXME: is this right? somehow I don't think so (VZ)
29 #include <glib-object.h>
31 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
32 #define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
33 #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
35 #define ACCEL_OBJECT GtkWindow
36 #define ACCEL_OBJECTS(a) (a)->acceleratables
37 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
39 #define ACCEL_OBJECT GtkObject
40 #define ACCEL_OBJECTS(a) (a)->attach_objects
41 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
44 // we use normal item but with a special id for the menu title
45 static const int wxGTK_TITLE_ID
= -3;
47 // defined in window.cpp
49 extern guint32 wxGtkTimeLastClick
;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 extern void wxapp_install_idle_handler();
60 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 static wxString
wxReplaceUnderscore( const wxString
& title
)
71 // GTK 1.2 wants to have "_" instead of "&" for accelerators
74 while (*pc
!= wxT('\0'))
76 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
78 // "&" is doubled to indicate "&" instead of accelerator
82 else if (*pc
== wxT('&'))
88 if ( *pc
== wxT('_') )
90 // underscores must be doubled to prevent them from being
91 // interpreted as accelerator character prefix by GTK
100 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
105 //-----------------------------------------------------------------------------
106 // activate message from GTK
107 //-----------------------------------------------------------------------------
109 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
112 wxapp_install_idle_handler();
114 event
.SetEventObject( menu
);
116 wxEvtHandler
* handler
= menu
->GetEventHandler();
117 if (handler
&& handler
->ProcessEvent(event
))
120 wxWindow
*win
= menu
->GetInvokingWindow();
122 win
->GetEventHandler()->ProcessEvent( event
);
127 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
129 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
131 DoCommonMenuCallbackCode(menu
, event
);
134 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
136 if ( !menubar
->GetMenuCount() )
138 // if menubar is empty we can't call GetMenu(0) below
142 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
144 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
155 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
157 // the parent window is known after wxFrame::SetMenu()
158 m_needParent
= FALSE
;
160 m_invokingWindow
= (wxWindow
*) NULL
;
162 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
163 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
165 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
169 m_menubar
= gtk_menu_bar_new();
171 m_accel
= gtk_accel_group_new();
174 if (style
& wxMB_DOCKABLE
)
176 m_widget
= gtk_handle_box_new();
177 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
178 gtk_widget_show( GTK_WIDGET(m_menubar
) );
182 m_widget
= GTK_WIDGET(m_menubar
);
189 for (size_t i
= 0; i
< n
; ++i
)
190 Append(menus
[i
], titles
[i
]);
192 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
193 // don't get it when the menu is dismissed by clicking outside the
194 // toolbar) so we connect to the global one, even if it means that we
195 // can't pass the menu which was closed in wxMenuEvent object
196 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
198 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
203 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
205 Init(n
, menus
, titles
, style
);
208 wxMenuBar::wxMenuBar(long style
)
210 Init(0, NULL
, NULL
, style
);
213 wxMenuBar::wxMenuBar()
215 Init(0, NULL
, NULL
, 0);
218 wxMenuBar::~wxMenuBar()
222 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
224 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
226 wxWindow
*top_frame
= win
;
227 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
228 top_frame
= top_frame
->GetParent();
231 // support for native hot keys
232 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
235 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
238 wxMenuItem
*menuitem
= node
->GetData();
239 if (menuitem
->IsSubMenu())
240 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
241 node
= node
->GetNext();
245 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
247 menu
->SetInvokingWindow( win
);
249 wxWindow
*top_frame
= win
;
250 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
251 top_frame
= top_frame
->GetParent();
253 // support for native hot keys
254 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
255 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
256 gtk_accel_group_attach( menu
->m_accel
, obj
);
258 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
261 wxMenuItem
*menuitem
= node
->GetData();
262 if (menuitem
->IsSubMenu())
263 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
264 node
= node
->GetNext();
268 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
270 m_invokingWindow
= win
;
271 wxWindow
*top_frame
= win
;
272 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
273 top_frame
= top_frame
->GetParent();
276 // support for native key accelerators indicated by underscroes
277 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
278 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
279 gtk_accel_group_attach( m_accel
, obj
);
282 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
285 wxMenu
*menu
= node
->GetData();
286 wxMenubarSetInvokingWindow( menu
, win
);
287 node
= node
->GetNext();
291 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
293 m_invokingWindow
= (wxWindow
*) NULL
;
294 wxWindow
*top_frame
= win
;
295 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
296 top_frame
= top_frame
->GetParent();
299 // support for native key accelerators indicated by underscroes
300 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
303 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
306 wxMenu
*menu
= node
->GetData();
307 wxMenubarUnsetInvokingWindow( menu
, win
);
308 node
= node
->GetNext();
312 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
314 if ( !wxMenuBarBase::Append( menu
, title
) )
317 return GtkAppend(menu
, title
);
320 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
322 wxString
str( wxReplaceUnderscore( title
) );
324 // This doesn't have much effect right now.
325 menu
->SetTitle( str
);
327 // The "m_owner" is the "menu item"
329 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
331 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
332 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
334 gtk_label_set_text( label
, wxGTK_CONV( str
) );
336 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
337 if (accel_key
!= GDK_VoidSymbol
)
339 gtk_widget_add_accelerator (menu
->m_owner
,
341 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
348 gtk_widget_show( menu
->m_owner
);
350 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
353 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
355 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
357 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
358 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
361 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
362 // addings menu later on.
363 if (m_invokingWindow
)
365 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
367 // OPTIMISE ME: we should probably cache this, or pass it
368 // directly, but for now this is a minimal
369 // change to validate the new dynamic sizing.
370 // see (and refactor :) similar code in Remove
373 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
376 frame
->UpdateMenuBarSize();
382 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
384 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
389 if ( !GtkAppend(menu
, title
, (int)pos
) )
395 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
397 // remove the old item and insert a new one
398 wxMenu
*menuOld
= Remove(pos
);
399 if ( menuOld
&& !Insert(pos
, menu
, title
) )
401 return (wxMenu
*) NULL
;
404 // either Insert() succeeded or Remove() failed and menuOld is NULL
408 wxMenu
*wxMenuBar::Remove(size_t pos
)
410 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
412 return (wxMenu
*) NULL
;
414 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
415 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
417 gtk_widget_destroy( menu
->m_owner
);
418 menu
->m_owner
= NULL
;
420 if (m_invokingWindow
)
422 // OPTIMISE ME: see comment in GtkAppend
423 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
426 frame
->UpdateMenuBarSize();
432 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
434 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
436 int res
= menu
->FindItem( itemString
);
437 if (res
!= wxNOT_FOUND
)
441 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
444 wxMenuItem
*item
= node
->GetData();
445 if (item
->IsSubMenu())
446 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
448 node
= node
->GetNext();
454 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
456 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
459 wxMenu
*menu
= node
->GetData();
460 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
463 node
= node
->GetNext();
469 // Find a wxMenuItem using its id. Recurses down into sub-menus
470 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
472 wxMenuItem
* result
= menu
->FindChildItem(id
);
474 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
475 while ( node
&& result
== NULL
)
477 wxMenuItem
*item
= node
->GetData();
478 if (item
->IsSubMenu())
480 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
482 node
= node
->GetNext();
488 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
490 wxMenuItem
* result
= 0;
491 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
492 while (node
&& result
== 0)
494 wxMenu
*menu
= node
->GetData();
495 result
= FindMenuItemByIdRecursive( menu
, id
);
496 node
= node
->GetNext();
501 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
507 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
509 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
511 wxCHECK_RET( node
, wxT("menu not found") );
513 wxMenu
* menu
= node
->GetData();
516 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
519 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
521 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
523 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
525 wxMenu
* menu
= node
->GetData();
528 wxString
text( menu
->GetTitle() );
529 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
531 if ( *pc
== wxT('_') )
533 // '_' is the escape character for GTK+
537 // don't remove ampersands '&' since if we have them in the menu title
538 // it means that they were doubled to indicate "&" instead of accelerator
546 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
548 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
550 wxCHECK_RET( node
, wxT("menu not found") );
552 wxMenu
* menu
= node
->GetData();
554 const wxString
str( wxReplaceUnderscore( label
) );
556 menu
->SetTitle( str
);
560 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
563 gtk_label_set( glabel
, wxGTK_CONV( str
) );
565 /* reparse key accel */
566 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
567 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
572 //-----------------------------------------------------------------------------
574 //-----------------------------------------------------------------------------
577 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
580 wxapp_install_idle_handler();
582 int id
= menu
->FindMenuIdByMenuItem(widget
);
584 /* should find it for normal (not popup) menu */
585 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
586 _T("menu item not found in gtk_menu_clicked_callback") );
588 if (!menu
->IsEnabled(id
))
591 wxMenuItem
* item
= menu
->FindChildItem( id
);
592 wxCHECK_RET( item
, wxT("error in menu item callback") );
594 if ( item
->GetId() == wxGTK_TITLE_ID
)
596 // ignore events from the menu title
600 if (item
->IsCheckable())
602 bool isReallyChecked
= item
->IsChecked(),
603 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
605 // ensure that the internal state is always consistent with what is
606 // shown on the screen
607 item
->wxMenuItemBase::Check(isReallyChecked
);
609 // we must not report the events for the radio button going up nor the
610 // events resulting from the calls to wxMenuItem::Check()
611 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
612 (isInternallyChecked
== isReallyChecked
) )
619 // Is this menu on a menubar? (possibly nested)
620 wxFrame
* frame
= NULL
;
621 if(menu
->IsAttached())
622 frame
= menu
->GetMenuBar()->GetFrame();
624 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
625 // normally wxMenu::SendEvent() should be enough, if it doesn't work
626 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
627 // should be fixed instead of working around it here...
630 // If it is attached then let the frame send the event.
631 // Don't call frame->ProcessCommand(id) because it toggles
632 // checkable items and we've already done that above.
633 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
634 commandEvent
.SetEventObject(frame
);
635 if (item
->IsCheckable())
636 commandEvent
.SetInt(item
->IsChecked());
637 commandEvent
.SetEventObject(menu
);
639 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
643 // otherwise let the menu have it
644 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
649 //-----------------------------------------------------------------------------
651 //-----------------------------------------------------------------------------
654 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
656 if (g_isIdle
) wxapp_install_idle_handler();
658 int id
= menu
->FindMenuIdByMenuItem(widget
);
660 wxASSERT( id
!= -1 ); // should find it!
662 if (!menu
->IsEnabled(id
))
665 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
666 event
.SetEventObject( menu
);
668 wxEvtHandler
* handler
= menu
->GetEventHandler();
669 if (handler
&& handler
->ProcessEvent(event
))
672 wxWindow
*win
= menu
->GetInvokingWindow();
673 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
677 //-----------------------------------------------------------------------------
679 //-----------------------------------------------------------------------------
682 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
684 if (g_isIdle
) wxapp_install_idle_handler();
686 int id
= menu
->FindMenuIdByMenuItem(widget
);
688 wxASSERT( id
!= -1 ); // should find it!
690 if (!menu
->IsEnabled(id
))
693 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
694 event
.SetEventObject( menu
);
696 wxEvtHandler
* handler
= menu
->GetEventHandler();
697 if (handler
&& handler
->ProcessEvent(event
))
700 wxWindow
*win
= menu
->GetInvokingWindow();
702 win
->GetEventHandler()->ProcessEvent( event
);
706 //-----------------------------------------------------------------------------
708 //-----------------------------------------------------------------------------
710 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
712 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
714 const wxString
& name
,
715 const wxString
& help
,
719 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
722 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
724 const wxString
& text
,
725 const wxString
& help
,
728 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
733 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
735 const wxString
& text
,
736 const wxString
& help
,
739 : wxMenuItemBase(parentMenu
, id
, text
, help
,
740 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
745 void wxMenuItem::Init(const wxString
& text
)
747 m_labelWidget
= (GtkWidget
*) NULL
;
748 m_menuItem
= (GtkWidget
*) NULL
;
753 wxMenuItem::~wxMenuItem()
755 // don't delete menu items, the menus take care of that
758 // return the menu item text without any menu accels
760 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
764 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
766 if ( *pc
== wxT('\t'))
769 if ( *pc
== wxT('_') )
771 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
778 if ( *pc
== wxT('\\') )
780 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
787 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
790 // "&" is doubled to indicate "&" instead of accelerator
797 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
802 void wxMenuItem::SetText( const wxString
& str
)
804 // Some optimization to avoid flicker
805 wxString oldLabel
= m_text
;
806 oldLabel
= wxStripMenuCodes(oldLabel
);
807 oldLabel
.Replace(wxT("_"), wxT(""));
808 wxString label1
= wxStripMenuCodes(str
);
809 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
810 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
814 if (oldLabel
== label1
&&
815 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
822 label
= (GtkLabel
*) m_labelWidget
;
824 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
827 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
830 gtk_label_set( label
, wxGTK_CONV( m_text
) );
833 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
834 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
839 GdkModifierType accel_mods
;
840 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
843 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
844 m_parentMenu
->m_accel
,
849 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
850 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
853 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
855 m_parentMenu
->m_accel
,
862 // it's valid for this function to be called even if m_menuItem == NULL
863 void wxMenuItem::DoSetText( const wxString
& str
)
865 // '\t' is the deliminator indicating a hot key
867 const wxChar
*pc
= str
;
868 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
870 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
872 // "&" is doubled to indicate "&" instead of accelerator
876 else if (*pc
== wxT('&'))
880 else if ( *pc
== wxT('_') ) // escape underscores
899 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
904 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
909 return (wxAcceleratorEntry
*)NULL
;
912 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
914 label
<< wxT('\t') << GetHotKey();
916 return wxGetAccelFromString(label
);
919 #endif // wxUSE_ACCEL
921 void wxMenuItem::Check( bool check
)
923 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
925 if (check
== m_isChecked
)
928 wxMenuItemBase::Check( check
);
934 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
938 wxFAIL_MSG( _T("can't check this item") );
942 void wxMenuItem::Enable( bool enable
)
944 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
946 gtk_widget_set_sensitive( m_menuItem
, enable
);
947 wxMenuItemBase::Enable( enable
);
950 bool wxMenuItem::IsChecked() const
952 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
954 wxCHECK_MSG( IsCheckable(), FALSE
,
955 wxT("can't get state of uncheckable item!") );
957 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
960 //-----------------------------------------------------------------------------
962 //-----------------------------------------------------------------------------
964 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
968 m_accel
= gtk_accel_group_new();
969 m_menu
= gtk_menu_new();
970 // NB: keep reference to the menu so that it is not destroyed behind
971 // our back by GTK+ e.g. when it is removed from menubar:
972 gtk_widget_ref(m_menu
);
974 m_owner
= (GtkWidget
*) NULL
;
976 // Tearoffs are entries, just like separators. So if we want this
977 // menu to be a tear-off one, we just append a tearoff entry
979 if ( m_style
& wxMENU_TEAROFF
)
981 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
983 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
988 // append the title as the very first entry if we have it
989 if ( !m_title
.empty() )
991 Append(wxGTK_TITLE_ID
, m_title
);
998 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
1000 if ( GTK_IS_WIDGET( m_menu
))
1003 gtk_widget_unref( m_menu
);
1004 // if the menu is inserted in another menu at this time, there was
1005 // one more reference to it:
1007 gtk_widget_destroy( m_menu
);
1011 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
1013 GtkWidget
*menuItem
;
1020 if ( mitem
->IsSeparator() )
1023 menuItem
= gtk_separator_menu_item_new();
1026 menuItem
= gtk_menu_item_new();
1029 else if (mitem
->GetBitmap().Ok())
1031 text
= mitem
->GetText();
1032 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1035 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1038 if (bitmap
->HasPixbuf())
1040 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
1044 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
1045 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
1046 bitmap
->GetMask()->GetBitmap() :
1048 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1051 gtk_widget_show(image
);
1053 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1057 wxUnusedVar(bitmap
);
1058 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1059 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1064 else // a normal item
1066 // text has "_" instead of "&" after mitem->SetText() so don't use it
1067 text
= mitem
->GetText() ;
1069 switch ( mitem
->GetKind() )
1074 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1076 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1077 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1079 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1087 GSList
*group
= NULL
;
1088 if ( m_prevRadio
== NULL
)
1090 // start of a new radio group
1092 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1094 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1095 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1097 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1100 else // continue the radio group
1103 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1104 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1106 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1107 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1108 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1115 wxFAIL_MSG( _T("unexpected menu item kind") );
1121 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1123 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1124 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1134 GdkModifierType accel_mods
;
1135 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1137 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1138 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1141 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1150 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1152 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1154 gtk_widget_show( menuItem
);
1156 if ( !mitem
->IsSeparator() )
1158 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1160 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1161 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1164 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1165 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1168 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1170 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1172 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1174 // if adding a submenu to a menu already existing in the menu bar, we
1175 // must set invoking window to allow processing events from this
1177 if ( m_invokingWindow
)
1178 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1182 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1183 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1188 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1189 if (accel_key
!= GDK_VoidSymbol
)
1191 gtk_widget_add_accelerator (menuItem
,
1193 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1201 mitem
->SetMenuItem(menuItem
);
1205 // This doesn't even exist!
1206 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1212 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1214 if (!GtkAppend(mitem
))
1217 return wxMenuBase::DoAppend(mitem
);
1220 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1222 if ( !wxMenuBase::DoInsert(pos
, item
) )
1226 if ( !GtkAppend(item
, (int)pos
) )
1232 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1234 if ( !wxMenuBase::DoRemove(item
) )
1235 return (wxMenuItem
*)NULL
;
1237 // TODO: this code doesn't delete the item factory item and this seems
1238 // impossible as of GTK 1.2.6.
1239 gtk_widget_destroy( item
->GetMenuItem() );
1244 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1246 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1249 wxMenuItem
*item
= node
->GetData();
1250 if (item
->GetMenuItem() == menuItem
)
1251 return item
->GetId();
1252 node
= node
->GetNext();
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1264 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1268 wxAcceleratorEntry
*accel
= item
.GetAccel();
1271 int flags
= accel
->GetFlags();
1272 if ( flags
& wxACCEL_ALT
)
1273 hotkey
+= wxT("<alt>");
1274 if ( flags
& wxACCEL_CTRL
)
1275 hotkey
+= wxT("<control>");
1276 if ( flags
& wxACCEL_SHIFT
)
1277 hotkey
+= wxT("<shift>");
1279 int code
= accel
->GetKeyCode();
1306 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1309 // TODO: we should use gdk_keyval_name() (a.k.a.
1310 // XKeysymToString) here as well as hardcoding the keysym
1311 // names this might be not portable
1313 hotkey
<< wxT("Insert" );
1316 hotkey
<< wxT("Delete" );
1319 hotkey
<< wxT("Up" );
1322 hotkey
<< wxT("Down" );
1326 hotkey
<< wxT("Prior" );
1330 hotkey
<< wxT("Next" );
1333 hotkey
<< wxT("Left" );
1336 hotkey
<< wxT("Right" );
1339 hotkey
<< wxT("Home" );
1342 hotkey
<< wxT("End" );
1345 hotkey
<< wxT("Return" );
1348 hotkey
<< wxT("BackSpace" );
1351 hotkey
<< wxT("Tab" );
1354 hotkey
<< wxT("Esc" );
1357 hotkey
<< wxT("space" );
1360 hotkey
<< wxT("Multiply" );
1363 hotkey
<< wxT("Add" );
1366 hotkey
<< wxT("Separator" );
1369 hotkey
<< wxT("Subtract" );
1372 hotkey
<< wxT("Decimal" );
1375 hotkey
<< wxT("Divide" );
1378 hotkey
<< wxT("Cancel" );
1381 hotkey
<< wxT("Clear" );
1384 hotkey
<< wxT("Menu" );
1387 hotkey
<< wxT("Pause" );
1390 hotkey
<< wxT("Capital" );
1393 hotkey
<< wxT("Select" );
1396 hotkey
<< wxT("Print" );
1399 hotkey
<< wxT("Execute" );
1402 hotkey
<< wxT("Snapshot" );
1405 hotkey
<< wxT("Help" );
1408 hotkey
<< wxT("Num_Lock" );
1411 hotkey
<< wxT("Scroll_Lock" );
1413 case WXK_NUMPAD_INSERT
:
1414 hotkey
<< wxT("KP_Insert" );
1416 case WXK_NUMPAD_DELETE
:
1417 hotkey
<< wxT("KP_Delete" );
1419 case WXK_NUMPAD_SPACE
:
1420 hotkey
<< wxT("KP_Space" );
1422 case WXK_NUMPAD_TAB
:
1423 hotkey
<< wxT("KP_Tab" );
1425 case WXK_NUMPAD_ENTER
:
1426 hotkey
<< wxT("KP_Enter" );
1428 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1430 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1432 case WXK_NUMPAD_HOME
:
1433 hotkey
<< wxT("KP_Home" );
1435 case WXK_NUMPAD_LEFT
:
1436 hotkey
<< wxT("KP_Left" );
1439 hotkey
<< wxT("KP_Up" );
1441 case WXK_NUMPAD_RIGHT
:
1442 hotkey
<< wxT("KP_Right" );
1444 case WXK_NUMPAD_DOWN
:
1445 hotkey
<< wxT("KP_Down" );
1447 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1448 hotkey
<< wxT("KP_Prior" );
1450 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1451 hotkey
<< wxT("KP_Next" );
1453 case WXK_NUMPAD_END
:
1454 hotkey
<< wxT("KP_End" );
1456 case WXK_NUMPAD_BEGIN
:
1457 hotkey
<< wxT("KP_Begin" );
1459 case WXK_NUMPAD_EQUAL
:
1460 hotkey
<< wxT("KP_Equal" );
1462 case WXK_NUMPAD_MULTIPLY
:
1463 hotkey
<< wxT("KP_Multiply" );
1465 case WXK_NUMPAD_ADD
:
1466 hotkey
<< wxT("KP_Add" );
1468 case WXK_NUMPAD_SEPARATOR
:
1469 hotkey
<< wxT("KP_Separator" );
1471 case WXK_NUMPAD_SUBTRACT
:
1472 hotkey
<< wxT("KP_Subtract" );
1474 case WXK_NUMPAD_DECIMAL
:
1475 hotkey
<< wxT("KP_Decimal" );
1477 case WXK_NUMPAD_DIVIDE
:
1478 hotkey
<< wxT("KP_Divide" );
1480 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1481 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1482 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1483 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1485 case WXK_WINDOWS_LEFT
:
1486 hotkey
<< wxT("Super_L" );
1488 case WXK_WINDOWS_RIGHT
:
1489 hotkey
<< wxT("Super_R" );
1491 case WXK_WINDOWS_MENU
:
1492 hotkey
<< wxT("Menu" );
1495 hotkey
<< wxT("Command" );
1497 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1498 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1499 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1500 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1501 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1502 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1503 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1506 // if there are any other keys wxGetAccelFromString() may
1507 // return, we should process them here
1512 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1520 wxFAIL_MSG( wxT("unknown keyboard accel") );
1529 #endif // wxUSE_ACCEL
1531 // ----------------------------------------------------------------------------
1532 // Pop-up menu stuff
1533 // ----------------------------------------------------------------------------
1535 #if wxUSE_MENUS_NATIVE
1538 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1540 *is_waiting
= FALSE
;
1543 void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1545 menu
->SetInvokingWindow( win
);
1547 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1550 wxMenuItem
*menuitem
= node
->GetData();
1551 if (menuitem
->IsSubMenu())
1553 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1556 node
= node
->GetNext();
1561 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1564 gboolean
* WXUNUSED(whatever
),
1566 gpointer user_data
)
1568 // ensure that the menu appears entirely on screen
1570 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1572 wxSize sizeScreen
= wxGetDisplaySize();
1573 wxPoint
*pos
= (wxPoint
*)user_data
;
1575 gint xmax
= sizeScreen
.x
- req
.width
,
1576 ymax
= sizeScreen
.y
- req
.height
;
1578 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1579 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1582 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1584 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1586 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1588 // NOTE: if you change this code, you need to update
1589 // the same code in taskbar.cpp as well. This
1590 // is ugly code duplication, I know.
1592 SetInvokingWindow( menu
, this );
1596 bool is_waiting
= true;
1598 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1600 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1601 (gpointer
)&is_waiting
);
1605 GtkMenuPositionFunc posfunc
;
1606 if ( x
== -1 && y
== -1 )
1608 // use GTK's default positioning algorithm
1614 pos
= ClientToScreen(wxPoint(x
, y
));
1616 posfunc
= wxPopupMenuPositionCallback
;
1619 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1620 DoCommonMenuCallbackCode(menu
, eventOpen
);
1623 GTK_MENU(menu
->m_menu
),
1624 (GtkWidget
*) NULL
, // parent menu shell
1625 (GtkWidget
*) NULL
, // parent menu item
1626 posfunc
, // function to position it
1627 userdata
, // client data
1628 0, // button used to activate it
1630 gtk_get_current_event_time()
1632 wxGtkTimeLastClick
// the time of activation
1638 gtk_main_iteration();
1641 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1643 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1644 DoCommonMenuCallbackCode(menu
, eventClose
);
1649 #endif // wxUSE_MENUS_NATIVE