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"
29 #include <gdk/gdkkeysyms.h>
31 #define ACCEL_OBJECT GtkObject
32 #define ACCEL_OBJECTS(a) (a)->attach_objects
33 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
35 // we use normal item but with a special id for the menu title
36 static const int wxGTK_TITLE_ID
= -3;
38 // defined in window.cpp
39 extern guint32 wxGtkTimeLastClick
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern void wxapp_install_idle_handler();
49 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 static wxString
wxReplaceUnderscore( const wxString
& title
)
60 // GTK 1.2 wants to have "_" instead of "&" for accelerators
63 while (*pc
!= wxT('\0'))
65 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
67 // "&" is doubled to indicate "&" instead of accelerator
71 else if (*pc
== wxT('&'))
77 if ( *pc
== wxT('_') )
79 // underscores must be doubled to prevent them from being
80 // interpreted as accelerator character prefix by GTK
89 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
94 static wxString
wxConvertFromGTKToWXLabel(const wxString
& gtkLabel
)
97 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
99 // '_' is the escape character for GTK+.
101 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
103 // An underscore was escaped.
107 else if ( *pc
== wxT('_') )
109 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
112 else if ( *pc
== wxT('&') )
114 // Double the ampersand to escape it as far as wxWidgets is concerned
119 // don't remove ampersands '&' since if we have them in the menu title
120 // it means that they were doubled to indicate "&" instead of accelerator
129 //-----------------------------------------------------------------------------
130 // activate message from GTK
131 //-----------------------------------------------------------------------------
133 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
136 wxapp_install_idle_handler();
138 event
.SetEventObject( menu
);
140 wxEvtHandler
* handler
= menu
->GetEventHandler();
141 if (handler
&& handler
->ProcessEvent(event
))
144 wxWindow
*win
= menu
->GetInvokingWindow();
146 win
->GetEventHandler()->ProcessEvent( event
);
151 static void gtk_menu_open_callback( GtkWidget
*WXUNUSED(widget
), wxMenu
*menu
)
153 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
155 DoCommonMenuCallbackCode(menu
, event
);
158 static void gtk_menu_close_callback( GtkWidget
*WXUNUSED(widget
), wxMenuBar
*menubar
)
160 if ( !menubar
->GetMenuCount() )
162 // if menubar is empty we can't call GetMenu(0) below
166 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
168 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
179 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
181 // the parent window is known after wxFrame::SetMenu()
182 m_needParent
= false;
184 m_invokingWindow
= (wxWindow
*) NULL
;
186 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
187 !CreateBase( (wxWindow
*) 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 wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
246 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
248 wxWindow
*top_frame
= win
;
249 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
250 top_frame
= top_frame
->GetParent();
252 // support for native hot keys
253 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
255 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
258 wxMenuItem
*menuitem
= node
->GetData();
259 if (menuitem
->IsSubMenu())
260 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
261 node
= node
->GetNext();
265 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
267 menu
->SetInvokingWindow( win
);
269 wxWindow
*top_frame
= win
;
270 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
271 top_frame
= top_frame
->GetParent();
273 // support for native hot keys
274 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
275 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
276 gtk_accel_group_attach( menu
->m_accel
, obj
);
278 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
281 wxMenuItem
*menuitem
= node
->GetData();
282 if (menuitem
->IsSubMenu())
283 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
284 node
= node
->GetNext();
288 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
290 m_invokingWindow
= win
;
291 wxWindow
*top_frame
= win
;
292 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
293 top_frame
= top_frame
->GetParent();
295 // support for native key accelerators indicated by underscroes
296 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
297 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
298 gtk_accel_group_attach( m_accel
, obj
);
300 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
303 wxMenu
*menu
= node
->GetData();
304 wxMenubarSetInvokingWindow( menu
, win
);
305 node
= node
->GetNext();
309 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
311 m_invokingWindow
= (wxWindow
*) NULL
;
312 wxWindow
*top_frame
= win
;
313 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
314 top_frame
= top_frame
->GetParent();
316 // support for native key accelerators indicated by underscroes
317 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
319 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
322 wxMenu
*menu
= node
->GetData();
323 wxMenubarUnsetInvokingWindow( menu
, win
);
324 node
= node
->GetNext();
328 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
330 if ( !wxMenuBarBase::Append( menu
, title
) )
333 return GtkAppend(menu
, title
);
336 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
338 wxString
str( wxReplaceUnderscore( title
) );
340 // This doesn't have much effect right now.
341 menu
->SetTitle( str
);
343 // The "m_owner" is the "menu item"
344 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
345 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
347 gtk_label_set_text( label
, wxGTK_CONV( str
) );
349 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
350 if (accel_key
!= GDK_VoidSymbol
)
352 gtk_widget_add_accelerator (menu
->m_owner
,
354 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
360 gtk_widget_show( menu
->m_owner
);
362 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
365 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
367 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
369 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
370 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
373 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
374 // addings menu later on.
375 if (m_invokingWindow
)
377 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
379 // OPTIMISE ME: we should probably cache this, or pass it
380 // directly, but for now this is a minimal
381 // change to validate the new dynamic sizing.
382 // see (and refactor :) similar code in Remove
385 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
388 frame
->UpdateMenuBarSize();
394 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
396 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
401 if ( !GtkAppend(menu
, title
, (int)pos
) )
407 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
409 // remove the old item and insert a new one
410 wxMenu
*menuOld
= Remove(pos
);
411 if ( menuOld
&& !Insert(pos
, menu
, title
) )
413 return (wxMenu
*) NULL
;
416 // either Insert() succeeded or Remove() failed and menuOld is NULL
420 wxMenu
*wxMenuBar::Remove(size_t pos
)
422 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
424 return (wxMenu
*) NULL
;
426 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
427 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
429 gtk_widget_destroy( menu
->m_owner
);
430 menu
->m_owner
= NULL
;
432 if (m_invokingWindow
)
434 // OPTIMISE ME: see comment in GtkAppend
435 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
438 frame
->UpdateMenuBarSize();
444 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
446 if (wxMenuItem::GetLabelText(wxConvertFromGTKToWXLabel(menu
->GetTitle())) == wxMenuItem::GetLabelText(menuString
))
448 int res
= menu
->FindItem( itemString
);
449 if (res
!= wxNOT_FOUND
)
453 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
456 wxMenuItem
*item
= node
->GetData();
457 if (item
->IsSubMenu())
458 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
460 node
= node
->GetNext();
466 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
468 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
471 wxMenu
*menu
= node
->GetData();
472 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
475 node
= node
->GetNext();
481 // Find a wxMenuItem using its id. Recurses down into sub-menus
482 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
484 wxMenuItem
* result
= menu
->FindChildItem(id
);
486 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
487 while ( node
&& result
== NULL
)
489 wxMenuItem
*item
= node
->GetData();
490 if (item
->IsSubMenu())
492 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
494 node
= node
->GetNext();
500 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
502 wxMenuItem
* result
= 0;
503 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
504 while (node
&& result
== 0)
506 wxMenu
*menu
= node
->GetData();
507 result
= FindMenuItemByIdRecursive( menu
, id
);
508 node
= node
->GetNext();
513 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
519 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
521 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
523 wxCHECK_RET( node
, wxT("menu not found") );
525 wxMenu
* menu
= node
->GetData();
528 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
531 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
533 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
535 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
537 wxMenu
* menu
= node
->GetData();
539 return wxConvertFromGTKToWXLabel(menu
->GetTitle());
542 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
544 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
546 wxCHECK_RET( node
, wxT("menu not found") );
548 wxMenu
* menu
= node
->GetData();
550 const wxString
str( wxReplaceUnderscore( label
) );
552 menu
->SetTitle( str
);
556 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
559 gtk_label_set( glabel
, wxGTK_CONV( str
) );
561 /* reparse key accel */
562 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
563 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
568 //-----------------------------------------------------------------------------
570 //-----------------------------------------------------------------------------
573 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
576 wxapp_install_idle_handler();
578 int id
= menu
->FindMenuIdByMenuItem(widget
);
580 /* should find it for normal (not popup) menu */
581 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
582 _T("menu item not found in gtk_menu_clicked_callback") );
584 if (!menu
->IsEnabled(id
))
587 wxMenuItem
* item
= menu
->FindChildItem( id
);
588 wxCHECK_RET( item
, wxT("error in menu item callback") );
590 if ( item
->GetId() == wxGTK_TITLE_ID
)
592 // ignore events from the menu title
596 if (item
->IsCheckable())
598 bool isReallyChecked
= item
->IsChecked(),
599 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
601 // ensure that the internal state is always consistent with what is
602 // shown on the screen
603 item
->wxMenuItemBase::Check(isReallyChecked
);
605 // we must not report the events for the radio button going up nor the
606 // events resulting from the calls to wxMenuItem::Check()
607 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
608 (isInternallyChecked
== isReallyChecked
) )
615 // Is this menu on a menubar? (possibly nested)
616 wxFrame
* frame
= NULL
;
617 if(menu
->IsAttached())
618 frame
= menu
->GetMenuBar()->GetFrame();
620 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
621 // normally wxMenu::SendEvent() should be enough, if it doesn't work
622 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
623 // should be fixed instead of working around it here...
626 // If it is attached then let the frame send the event.
627 // Don't call frame->ProcessCommand(id) because it toggles
628 // checkable items and we've already done that above.
629 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
630 commandEvent
.SetEventObject(frame
);
631 if (item
->IsCheckable())
632 commandEvent
.SetInt(item
->IsChecked());
633 commandEvent
.SetEventObject(menu
);
635 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
639 // otherwise let the menu have it
640 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
645 //-----------------------------------------------------------------------------
647 //-----------------------------------------------------------------------------
650 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
652 if (g_isIdle
) wxapp_install_idle_handler();
654 int id
= menu
->FindMenuIdByMenuItem(widget
);
656 wxASSERT( id
!= -1 ); // should find it!
658 if (!menu
->IsEnabled(id
))
661 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
662 event
.SetEventObject( menu
);
664 wxEvtHandler
* handler
= menu
->GetEventHandler();
665 if (handler
&& handler
->ProcessEvent(event
))
668 wxWindow
*win
= menu
->GetInvokingWindow();
669 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
673 //-----------------------------------------------------------------------------
675 //-----------------------------------------------------------------------------
678 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
680 if (g_isIdle
) wxapp_install_idle_handler();
682 int id
= menu
->FindMenuIdByMenuItem(widget
);
684 wxASSERT( id
!= -1 ); // should find it!
686 if (!menu
->IsEnabled(id
))
689 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
690 event
.SetEventObject( menu
);
692 wxEvtHandler
* handler
= menu
->GetEventHandler();
693 if (handler
&& handler
->ProcessEvent(event
))
696 wxWindow
*win
= menu
->GetInvokingWindow();
698 win
->GetEventHandler()->ProcessEvent( event
);
702 //-----------------------------------------------------------------------------
704 //-----------------------------------------------------------------------------
706 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
708 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
710 const wxString
& name
,
711 const wxString
& help
,
715 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
718 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
720 const wxString
& text
,
721 const wxString
& help
,
724 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
729 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
731 const wxString
& text
,
732 const wxString
& help
,
735 : wxMenuItemBase(parentMenu
, id
, text
, help
,
736 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
741 void wxMenuItem::Init()
743 m_labelWidget
= (GtkWidget
*) NULL
;
744 m_menuItem
= (GtkWidget
*) NULL
;
749 wxMenuItem::~wxMenuItem()
751 // don't delete menu items, the menus take care of that
754 // return the menu item text without any menu accels
756 wxString
wxMenuItemBase::GetLabelText(const wxString
& text
)
758 // The argument to this function will now always be in wxWidgets standard label
759 // format, not GTK+ format, so we do what the other ports do.
761 return wxStripMenuCodes(text
);
766 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
768 if ( *pc
== wxT('\t'))
771 if ( *pc
== wxT('_') )
773 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
779 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
782 // "&" is doubled to indicate "&" instead of accelerator
789 // wxPrintf( wxT("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() );
795 wxString
wxMenuItem::GetItemLabel() const
797 wxString label
= wxConvertFromGTKToWXLabel(m_text
);
798 if (!m_hotKey
.IsEmpty())
799 label
= label
+ wxT("\t") + m_hotKey
;
803 void wxMenuItem::SetItemLabel( const wxString
& string
)
805 wxString str
= string
;
806 if ( str
.empty() && !IsSeparator() )
808 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
809 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
810 wxSTOCK_WITH_MNEMONIC
);
813 // Some optimization to avoid flicker
814 wxString oldLabel
= m_text
;
815 oldLabel
= wxStripMenuCodes(oldLabel
);
816 oldLabel
.Replace(wxT("_"), wxEmptyString
);
817 wxString label1
= wxStripMenuCodes(str
);
818 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
819 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
823 if (oldLabel
== label1
&&
824 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
831 label
= (GtkLabel
*) m_labelWidget
;
833 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
836 gtk_label_set( label
, wxGTK_CONV( m_text
) );
839 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
840 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
844 GdkModifierType accel_mods
;
845 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
848 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
849 m_parentMenu
->m_accel
,
854 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
855 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
858 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
860 m_parentMenu
->m_accel
,
867 // it's valid for this function to be called even if m_menuItem == NULL
868 void wxMenuItem::DoSetText( const wxString
& str
)
870 // '\t' is the deliminator indicating a hot key
872 text
.reserve(str
.length());
874 const wxChar
*pc
= str
;
875 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
877 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
879 // "&" is doubled to indicate "&" instead of accelerator
883 else if (*pc
== wxT('&'))
887 else if ( *pc
== wxT('_') ) // escape underscores
898 m_hotKey
= wxEmptyString
;
900 if ( *pc
== wxT('\t') )
911 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
916 return (wxAcceleratorEntry
*)NULL
;
919 // accelerator parsing code looks for them after a TAB, so insert a dummy
922 label
<< wxT('\t') << GetHotKey();
924 return wxAcceleratorEntry::Create(label
);
927 #endif // wxUSE_ACCEL
929 void wxMenuItem::Check( bool check
)
931 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
933 if (check
== m_isChecked
)
936 wxMenuItemBase::Check( check
);
942 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
946 wxFAIL_MSG( _T("can't check this item") );
950 void wxMenuItem::Enable( bool enable
)
952 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
954 gtk_widget_set_sensitive( m_menuItem
, enable
);
955 wxMenuItemBase::Enable( enable
);
958 bool wxMenuItem::IsChecked() const
960 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
962 wxCHECK_MSG( IsCheckable(), false,
963 wxT("can't get state of uncheckable item!") );
965 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
968 //-----------------------------------------------------------------------------
970 //-----------------------------------------------------------------------------
972 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
976 m_accel
= gtk_accel_group_new();
977 m_menu
= gtk_menu_new();
978 // NB: keep reference to the menu so that it is not destroyed behind
979 // our back by GTK+ e.g. when it is removed from menubar:
980 gtk_widget_ref(m_menu
);
982 m_owner
= (GtkWidget
*) NULL
;
984 // Tearoffs are entries, just like separators. So if we want this
985 // menu to be a tear-off one, we just append a tearoff entry
987 if ( m_style
& wxMENU_TEAROFF
)
989 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
991 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
996 // append the title as the very first entry if we have it
997 if ( !m_title
.empty() )
999 Append(wxGTK_TITLE_ID
, m_title
);
1006 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
1008 if ( GTK_IS_WIDGET( m_menu
))
1011 gtk_widget_unref( m_menu
);
1012 // if the menu is inserted in another menu at this time, there was
1013 // one more reference to it:
1015 gtk_widget_destroy( m_menu
);
1019 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
1021 GtkWidget
*menuItem
;
1024 GtkLabel
* label
= NULL
;
1026 if ( mitem
->IsSeparator() )
1029 menuItem
= gtk_menu_item_new();
1031 else if (mitem
->GetBitmap().Ok())
1033 text
= mitem
->wxMenuItemBase::GetItemLabel();
1034 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1037 wxUnusedVar(bitmap
);
1038 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1039 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1043 else // a normal item
1045 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
1046 text
= mitem
->wxMenuItemBase::GetItemLabel() ;
1048 switch ( mitem
->GetKind() )
1052 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1053 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1055 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1062 GSList
*group
= NULL
;
1063 if ( m_prevRadio
== NULL
)
1065 // start of a new radio group
1066 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1067 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1069 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1071 else // continue the radio group
1073 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1074 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1075 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1081 wxFAIL_MSG( _T("unexpected menu item kind") );
1086 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1087 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1096 GdkModifierType accel_mods
;
1097 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1099 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1100 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1103 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1112 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1114 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1116 gtk_widget_show( menuItem
);
1118 if ( !mitem
->IsSeparator() )
1120 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1122 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1123 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1126 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1127 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1130 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1132 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1134 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1136 // if adding a submenu to a menu already existing in the menu bar, we
1137 // must set invoking window to allow processing events from this
1139 if ( m_invokingWindow
)
1140 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1144 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1145 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1149 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1150 if (accel_key
!= GDK_VoidSymbol
)
1152 gtk_widget_add_accelerator (menuItem
,
1154 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1161 mitem
->SetMenuItem(menuItem
);
1165 // This doesn't even exist!
1166 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1172 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1174 if (!GtkAppend(mitem
))
1177 return wxMenuBase::DoAppend(mitem
);
1180 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1182 if ( !wxMenuBase::DoInsert(pos
, item
) )
1186 if ( !GtkAppend(item
, (int)pos
) )
1192 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1194 if ( !wxMenuBase::DoRemove(item
) )
1195 return (wxMenuItem
*)NULL
;
1197 // TODO: this code doesn't delete the item factory item and this seems
1198 // impossible as of GTK 1.2.6.
1199 gtk_widget_destroy( item
->GetMenuItem() );
1204 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1206 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1209 wxMenuItem
*item
= node
->GetData();
1210 if (item
->GetMenuItem() == menuItem
)
1211 return item
->GetId();
1212 node
= node
->GetNext();
1218 // ----------------------------------------------------------------------------
1220 // ----------------------------------------------------------------------------
1224 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1228 wxAcceleratorEntry
*accel
= item
.GetAccel();
1231 int flags
= accel
->GetFlags();
1232 if ( flags
& wxACCEL_ALT
)
1233 hotkey
+= wxT("<alt>");
1234 if ( flags
& wxACCEL_CTRL
)
1235 hotkey
+= wxT("<control>");
1236 if ( flags
& wxACCEL_SHIFT
)
1237 hotkey
+= wxT("<shift>");
1239 int code
= accel
->GetKeyCode();
1266 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1269 // TODO: we should use gdk_keyval_name() (a.k.a.
1270 // XKeysymToString) here as well as hardcoding the keysym
1271 // names this might be not portable
1273 hotkey
<< wxT("Insert" );
1276 hotkey
<< wxT("Delete" );
1279 hotkey
<< wxT("Up" );
1282 hotkey
<< wxT("Down" );
1285 hotkey
<< wxT("Page_Up" );
1288 hotkey
<< wxT("Page_Down" );
1291 hotkey
<< wxT("Left" );
1294 hotkey
<< wxT("Right" );
1297 hotkey
<< wxT("Home" );
1300 hotkey
<< wxT("End" );
1303 hotkey
<< wxT("Return" );
1306 hotkey
<< wxT("BackSpace" );
1309 hotkey
<< wxT("Tab" );
1312 hotkey
<< wxT("Esc" );
1315 hotkey
<< wxT("space" );
1318 hotkey
<< wxT("Multiply" );
1321 hotkey
<< wxT("Add" );
1324 hotkey
<< wxT("Separator" );
1327 hotkey
<< wxT("Subtract" );
1330 hotkey
<< wxT("Decimal" );
1333 hotkey
<< wxT("Divide" );
1336 hotkey
<< wxT("Cancel" );
1339 hotkey
<< wxT("Clear" );
1342 hotkey
<< wxT("Menu" );
1345 hotkey
<< wxT("Pause" );
1348 hotkey
<< wxT("Capital" );
1351 hotkey
<< wxT("Select" );
1354 hotkey
<< wxT("Print" );
1357 hotkey
<< wxT("Execute" );
1360 hotkey
<< wxT("Snapshot" );
1363 hotkey
<< wxT("Help" );
1366 hotkey
<< wxT("Num_Lock" );
1369 hotkey
<< wxT("Scroll_Lock" );
1371 case WXK_NUMPAD_INSERT
:
1372 hotkey
<< wxT("KP_Insert" );
1374 case WXK_NUMPAD_DELETE
:
1375 hotkey
<< wxT("KP_Delete" );
1377 case WXK_NUMPAD_SPACE
:
1378 hotkey
<< wxT("KP_Space" );
1380 case WXK_NUMPAD_TAB
:
1381 hotkey
<< wxT("KP_Tab" );
1383 case WXK_NUMPAD_ENTER
:
1384 hotkey
<< wxT("KP_Enter" );
1386 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1388 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1390 case WXK_NUMPAD_HOME
:
1391 hotkey
<< wxT("KP_Home" );
1393 case WXK_NUMPAD_LEFT
:
1394 hotkey
<< wxT("KP_Left" );
1397 hotkey
<< wxT("KP_Up" );
1399 case WXK_NUMPAD_RIGHT
:
1400 hotkey
<< wxT("KP_Right" );
1402 case WXK_NUMPAD_DOWN
:
1403 hotkey
<< wxT("KP_Down" );
1405 case WXK_NUMPAD_PAGEUP
:
1406 hotkey
<< wxT("KP_Page_Up" );
1408 case WXK_NUMPAD_PAGEDOWN
:
1409 hotkey
<< wxT("KP_Page_Down" );
1411 case WXK_NUMPAD_END
:
1412 hotkey
<< wxT("KP_End" );
1414 case WXK_NUMPAD_BEGIN
:
1415 hotkey
<< wxT("KP_Begin" );
1417 case WXK_NUMPAD_EQUAL
:
1418 hotkey
<< wxT("KP_Equal" );
1420 case WXK_NUMPAD_MULTIPLY
:
1421 hotkey
<< wxT("KP_Multiply" );
1423 case WXK_NUMPAD_ADD
:
1424 hotkey
<< wxT("KP_Add" );
1426 case WXK_NUMPAD_SEPARATOR
:
1427 hotkey
<< wxT("KP_Separator" );
1429 case WXK_NUMPAD_SUBTRACT
:
1430 hotkey
<< wxT("KP_Subtract" );
1432 case WXK_NUMPAD_DECIMAL
:
1433 hotkey
<< wxT("KP_Decimal" );
1435 case WXK_NUMPAD_DIVIDE
:
1436 hotkey
<< wxT("KP_Divide" );
1438 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1439 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1440 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1441 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1443 case WXK_WINDOWS_LEFT
:
1444 hotkey
<< wxT("Super_L" );
1446 case WXK_WINDOWS_RIGHT
:
1447 hotkey
<< wxT("Super_R" );
1449 case WXK_WINDOWS_MENU
:
1450 hotkey
<< wxT("Menu" );
1453 hotkey
<< wxT("Command" );
1455 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1456 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1457 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1458 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1459 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1460 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1461 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1464 // if there are any other keys wxAcceleratorEntry::Create() may
1465 // return, we should process them here
1470 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1471 if ( !name
.empty() )
1478 wxFAIL_MSG( wxT("unknown keyboard accel") );
1487 #endif // wxUSE_ACCEL
1489 // ----------------------------------------------------------------------------
1490 // Pop-up menu stuff
1491 // ----------------------------------------------------------------------------
1493 #if wxUSE_MENUS_NATIVE
1495 extern "C" WXDLLIMPEXP_CORE
1496 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1498 *is_waiting
= false;
1501 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1503 menu
->SetInvokingWindow( win
);
1505 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1508 wxMenuItem
*menuitem
= node
->GetData();
1509 if (menuitem
->IsSubMenu())
1511 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1514 node
= node
->GetNext();
1518 extern "C" WXDLLIMPEXP_CORE
1519 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1521 gpointer user_data
)
1523 // ensure that the menu appears entirely on screen
1525 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1527 wxSize sizeScreen
= wxGetDisplaySize();
1528 wxPoint
*pos
= (wxPoint
*)user_data
;
1530 gint xmax
= sizeScreen
.x
- req
.width
,
1531 ymax
= sizeScreen
.y
- req
.height
;
1533 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1534 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1537 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1539 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1541 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1543 // NOTE: if you change this code, you need to update
1544 // the same code in taskbar.cpp as well. This
1545 // is ugly code duplication, I know.
1547 SetInvokingWindow( menu
, this );
1551 bool is_waiting
= true;
1553 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1555 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1556 (gpointer
)&is_waiting
);
1560 GtkMenuPositionFunc posfunc
;
1561 if ( x
== -1 && y
== -1 )
1563 // use GTK's default positioning algorithm
1569 pos
= ClientToScreen(wxPoint(x
, y
));
1571 posfunc
= wxPopupMenuPositionCallback
;
1574 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1575 DoCommonMenuCallbackCode(menu
, eventOpen
);
1578 GTK_MENU(menu
->m_menu
),
1579 (GtkWidget
*) NULL
, // parent menu shell
1580 (GtkWidget
*) NULL
, // parent menu item
1581 posfunc
, // function to position it
1582 userdata
, // client data
1583 0, // button used to activate it
1584 wxGtkTimeLastClick
// the time of activation
1589 gtk_main_iteration();
1592 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1594 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1595 DoCommonMenuCallbackCode(menu
, eventClose
);
1600 #endif // wxUSE_MENUS_NATIVE