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 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 extern void wxapp_install_idle_handler();
55 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 static wxString
wxReplaceUnderscore( const wxString
& title
)
66 // GTK 1.2 wants to have "_" instead of "&" for accelerators
69 while (*pc
!= wxT('\0'))
71 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
73 // "&" is doubled to indicate "&" instead of accelerator
77 else if (*pc
== wxT('&'))
83 if ( *pc
== wxT('_') )
85 // underscores must be doubled to prevent them from being
86 // interpreted as accelerator character prefix by GTK
95 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
100 //-----------------------------------------------------------------------------
101 // activate message from GTK
102 //-----------------------------------------------------------------------------
104 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
107 wxapp_install_idle_handler();
109 event
.SetEventObject( menu
);
111 wxEvtHandler
* handler
= menu
->GetEventHandler();
112 if (handler
&& handler
->ProcessEvent(event
))
115 wxWindow
*win
= menu
->GetInvokingWindow();
117 win
->GetEventHandler()->ProcessEvent( event
);
122 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
124 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
126 DoCommonMenuCallbackCode(menu
, event
);
129 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
131 if ( !menubar
->GetMenuCount() )
133 // if menubar is empty we can't call GetMenu(0) below
137 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
139 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
144 //-----------------------------------------------------------------------------
146 //-----------------------------------------------------------------------------
148 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
150 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
152 // the parent window is known after wxFrame::SetMenu()
153 m_needParent
= FALSE
;
155 m_invokingWindow
= (wxWindow
*) NULL
;
157 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
158 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
160 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
164 m_menubar
= gtk_menu_bar_new();
166 m_accel
= gtk_accel_group_new();
169 if (style
& wxMB_DOCKABLE
)
171 m_widget
= gtk_handle_box_new();
172 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
173 gtk_widget_show( GTK_WIDGET(m_menubar
) );
177 m_widget
= GTK_WIDGET(m_menubar
);
184 for (size_t i
= 0; i
< n
; ++i
)
185 Append(menus
[i
], titles
[i
]);
187 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
188 // don't get it when the menu is dismissed by clicking outside the
189 // toolbar) so we connect to the global one, even if it means that we
190 // can't pass the menu which was closed in wxMenuEvent object
191 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
193 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
198 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
200 Init(n
, menus
, titles
, style
);
203 wxMenuBar::wxMenuBar(long style
)
205 Init(0, NULL
, NULL
, style
);
208 wxMenuBar::wxMenuBar()
210 Init(0, NULL
, NULL
, 0);
213 wxMenuBar::~wxMenuBar()
217 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
219 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
221 wxWindow
*top_frame
= win
;
222 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
223 top_frame
= top_frame
->GetParent();
226 // support for native hot keys
227 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
230 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
233 wxMenuItem
*menuitem
= node
->GetData();
234 if (menuitem
->IsSubMenu())
235 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
236 node
= node
->GetNext();
240 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
242 menu
->SetInvokingWindow( win
);
244 wxWindow
*top_frame
= win
;
245 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
246 top_frame
= top_frame
->GetParent();
248 // support for native hot keys
249 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
250 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
251 gtk_accel_group_attach( menu
->m_accel
, obj
);
253 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
256 wxMenuItem
*menuitem
= node
->GetData();
257 if (menuitem
->IsSubMenu())
258 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
259 node
= node
->GetNext();
263 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
265 m_invokingWindow
= win
;
266 wxWindow
*top_frame
= win
;
267 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
268 top_frame
= top_frame
->GetParent();
271 // support for native key accelerators indicated by underscroes
272 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
273 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
274 gtk_accel_group_attach( m_accel
, obj
);
277 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
280 wxMenu
*menu
= node
->GetData();
281 wxMenubarSetInvokingWindow( menu
, win
);
282 node
= node
->GetNext();
286 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
288 m_invokingWindow
= (wxWindow
*) NULL
;
289 wxWindow
*top_frame
= win
;
290 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
291 top_frame
= top_frame
->GetParent();
294 // support for native key accelerators indicated by underscroes
295 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
298 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
301 wxMenu
*menu
= node
->GetData();
302 wxMenubarUnsetInvokingWindow( menu
, win
);
303 node
= node
->GetNext();
307 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
309 if ( !wxMenuBarBase::Append( menu
, title
) )
312 return GtkAppend(menu
, title
);
315 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
317 wxString
str( wxReplaceUnderscore( title
) );
319 // This doesn't have much effect right now.
320 menu
->SetTitle( str
);
322 // The "m_owner" is the "menu item"
324 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
326 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
327 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
329 gtk_label_set_text( label
, wxGTK_CONV( str
) );
331 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
332 if (accel_key
!= GDK_VoidSymbol
)
334 gtk_widget_add_accelerator (menu
->m_owner
,
336 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
343 gtk_widget_show( menu
->m_owner
);
345 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
348 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
350 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
352 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
353 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
356 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
357 // addings menu later on.
358 if (m_invokingWindow
)
360 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
362 // OPTIMISE ME: we should probably cache this, or pass it
363 // directly, but for now this is a minimal
364 // change to validate the new dynamic sizing.
365 // see (and refactor :) similar code in Remove
368 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
371 frame
->UpdateMenuBarSize();
377 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
379 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
384 if ( !GtkAppend(menu
, title
, (int)pos
) )
390 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
392 // remove the old item and insert a new one
393 wxMenu
*menuOld
= Remove(pos
);
394 if ( menuOld
&& !Insert(pos
, menu
, title
) )
396 return (wxMenu
*) NULL
;
399 // either Insert() succeeded or Remove() failed and menuOld is NULL
403 wxMenu
*wxMenuBar::Remove(size_t pos
)
405 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
407 return (wxMenu
*) NULL
;
409 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
410 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
412 gtk_widget_destroy( menu
->m_owner
);
413 menu
->m_owner
= NULL
;
415 if (m_invokingWindow
)
417 // OPTIMISE ME: see comment in GtkAppend
418 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
421 frame
->UpdateMenuBarSize();
427 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
429 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
431 int res
= menu
->FindItem( itemString
);
432 if (res
!= wxNOT_FOUND
)
436 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
439 wxMenuItem
*item
= node
->GetData();
440 if (item
->IsSubMenu())
441 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
443 node
= node
->GetNext();
449 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
451 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
454 wxMenu
*menu
= node
->GetData();
455 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
458 node
= node
->GetNext();
464 // Find a wxMenuItem using its id. Recurses down into sub-menus
465 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
467 wxMenuItem
* result
= menu
->FindChildItem(id
);
469 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
470 while ( node
&& result
== NULL
)
472 wxMenuItem
*item
= node
->GetData();
473 if (item
->IsSubMenu())
475 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
477 node
= node
->GetNext();
483 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
485 wxMenuItem
* result
= 0;
486 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
487 while (node
&& result
== 0)
489 wxMenu
*menu
= node
->GetData();
490 result
= FindMenuItemByIdRecursive( menu
, id
);
491 node
= node
->GetNext();
496 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
502 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
504 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
506 wxCHECK_RET( node
, wxT("menu not found") );
508 wxMenu
* menu
= node
->GetData();
511 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
514 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
516 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
518 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
520 wxMenu
* menu
= node
->GetData();
523 wxString
text( menu
->GetTitle() );
524 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
526 if ( *pc
== wxT('_') )
528 // '_' is the escape character for GTK+
532 // don't remove ampersands '&' since if we have them in the menu title
533 // it means that they were doubled to indicate "&" instead of accelerator
541 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
543 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
545 wxCHECK_RET( node
, wxT("menu not found") );
547 wxMenu
* menu
= node
->GetData();
549 const wxString
str( wxReplaceUnderscore( label
) );
551 menu
->SetTitle( str
);
555 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
558 gtk_label_set( glabel
, wxGTK_CONV( str
) );
560 /* reparse key accel */
561 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
562 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
567 //-----------------------------------------------------------------------------
569 //-----------------------------------------------------------------------------
572 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
575 wxapp_install_idle_handler();
577 int id
= menu
->FindMenuIdByMenuItem(widget
);
579 /* should find it for normal (not popup) menu */
580 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
581 _T("menu item not found in gtk_menu_clicked_callback") );
583 if (!menu
->IsEnabled(id
))
586 wxMenuItem
* item
= menu
->FindChildItem( id
);
587 wxCHECK_RET( item
, wxT("error in menu item callback") );
589 if ( item
->GetId() == wxGTK_TITLE_ID
)
591 // ignore events from the menu title
595 if (item
->IsCheckable())
597 bool isReallyChecked
= item
->IsChecked(),
598 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
600 // ensure that the internal state is always consistent with what is
601 // shown on the screen
602 item
->wxMenuItemBase::Check(isReallyChecked
);
604 // we must not report the events for the radio button going up nor the
605 // events resulting from the calls to wxMenuItem::Check()
606 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
607 (isInternallyChecked
== isReallyChecked
) )
614 // Is this menu on a menubar? (possibly nested)
615 wxFrame
* frame
= NULL
;
616 if(menu
->IsAttached())
617 frame
= menu
->GetMenuBar()->GetFrame();
619 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
620 // normally wxMenu::SendEvent() should be enough, if it doesn't work
621 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
622 // should be fixed instead of working around it here...
625 // If it is attached then let the frame send the event.
626 // Don't call frame->ProcessCommand(id) because it toggles
627 // checkable items and we've already done that above.
628 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
629 commandEvent
.SetEventObject(frame
);
630 if (item
->IsCheckable())
631 commandEvent
.SetInt(item
->IsChecked());
632 commandEvent
.SetEventObject(menu
);
634 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
638 // otherwise let the menu have it
639 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
644 //-----------------------------------------------------------------------------
646 //-----------------------------------------------------------------------------
649 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
651 if (g_isIdle
) wxapp_install_idle_handler();
653 int id
= menu
->FindMenuIdByMenuItem(widget
);
655 wxASSERT( id
!= -1 ); // should find it!
657 if (!menu
->IsEnabled(id
))
660 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
661 event
.SetEventObject( menu
);
663 wxEvtHandler
* handler
= menu
->GetEventHandler();
664 if (handler
&& handler
->ProcessEvent(event
))
667 wxWindow
*win
= menu
->GetInvokingWindow();
668 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
672 //-----------------------------------------------------------------------------
674 //-----------------------------------------------------------------------------
677 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
679 if (g_isIdle
) wxapp_install_idle_handler();
681 int id
= menu
->FindMenuIdByMenuItem(widget
);
683 wxASSERT( id
!= -1 ); // should find it!
685 if (!menu
->IsEnabled(id
))
688 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
689 event
.SetEventObject( menu
);
691 wxEvtHandler
* handler
= menu
->GetEventHandler();
692 if (handler
&& handler
->ProcessEvent(event
))
695 wxWindow
*win
= menu
->GetInvokingWindow();
697 win
->GetEventHandler()->ProcessEvent( event
);
701 //-----------------------------------------------------------------------------
703 //-----------------------------------------------------------------------------
705 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
707 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
709 const wxString
& name
,
710 const wxString
& help
,
714 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
717 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
719 const wxString
& text
,
720 const wxString
& help
,
723 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
728 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
730 const wxString
& text
,
731 const wxString
& help
,
734 : wxMenuItemBase(parentMenu
, id
, text
, help
,
735 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
740 void wxMenuItem::Init(const wxString
& text
)
742 m_labelWidget
= (GtkWidget
*) NULL
;
743 m_menuItem
= (GtkWidget
*) NULL
;
748 wxMenuItem::~wxMenuItem()
750 // don't delete menu items, the menus take care of that
753 // return the menu item text without any menu accels
755 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
759 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
761 if ( *pc
== wxT('\t'))
764 if ( *pc
== wxT('_') )
766 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
773 if ( *pc
== wxT('\\') )
775 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
782 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
785 // "&" is doubled to indicate "&" instead of accelerator
792 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
797 void wxMenuItem::SetText( const wxString
& str
)
799 // Some optimization to avoid flicker
800 wxString oldLabel
= m_text
;
801 oldLabel
= wxStripMenuCodes(oldLabel
);
802 oldLabel
.Replace(wxT("_"), wxT(""));
803 wxString label1
= wxStripMenuCodes(str
);
804 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
805 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
809 if (oldLabel
== label1
&&
810 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
817 label
= (GtkLabel
*) m_labelWidget
;
819 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
822 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
825 gtk_label_set( label
, wxGTK_CONV( m_text
) );
828 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
829 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
834 GdkModifierType accel_mods
;
835 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
838 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
839 m_parentMenu
->m_accel
,
844 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
845 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
848 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
850 m_parentMenu
->m_accel
,
857 // it's valid for this function to be called even if m_menuItem == NULL
858 void wxMenuItem::DoSetText( const wxString
& str
)
860 // '\t' is the deliminator indicating a hot key
862 const wxChar
*pc
= str
;
863 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
865 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
867 // "&" is doubled to indicate "&" instead of accelerator
871 else if (*pc
== wxT('&'))
875 else if ( *pc
== wxT('_') ) // escape underscores
894 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
899 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
904 return (wxAcceleratorEntry
*)NULL
;
907 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
909 label
<< wxT('\t') << GetHotKey();
911 return wxGetAccelFromString(label
);
914 #endif // wxUSE_ACCEL
916 void wxMenuItem::Check( bool check
)
918 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
920 if (check
== m_isChecked
)
923 wxMenuItemBase::Check( check
);
929 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
933 wxFAIL_MSG( _T("can't check this item") );
937 void wxMenuItem::Enable( bool enable
)
939 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
941 gtk_widget_set_sensitive( m_menuItem
, enable
);
942 wxMenuItemBase::Enable( enable
);
945 bool wxMenuItem::IsChecked() const
947 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
949 wxCHECK_MSG( IsCheckable(), FALSE
,
950 wxT("can't get state of uncheckable item!") );
952 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
955 //-----------------------------------------------------------------------------
957 //-----------------------------------------------------------------------------
959 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
963 m_accel
= gtk_accel_group_new();
964 m_menu
= gtk_menu_new();
965 // NB: keep reference to the menu so that it is not destroyed behind
966 // our back by GTK+ e.g. when it is removed from menubar:
967 gtk_widget_ref(m_menu
);
969 m_owner
= (GtkWidget
*) NULL
;
971 // Tearoffs are entries, just like separators. So if we want this
972 // menu to be a tear-off one, we just append a tearoff entry
974 if ( m_style
& wxMENU_TEAROFF
)
976 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
978 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
983 // append the title as the very first entry if we have it
984 if ( !m_title
.empty() )
986 Append(wxGTK_TITLE_ID
, m_title
);
993 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
995 if ( GTK_IS_WIDGET( m_menu
))
998 gtk_widget_unref( m_menu
);
999 // if the menu is inserted in another menu at this time, there was
1000 // one more reference to it:
1002 gtk_widget_destroy( m_menu
);
1006 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
1008 GtkWidget
*menuItem
;
1015 if ( mitem
->IsSeparator() )
1018 menuItem
= gtk_separator_menu_item_new();
1021 menuItem
= gtk_menu_item_new();
1024 else if (mitem
->GetBitmap().Ok())
1026 text
= mitem
->GetText();
1027 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1030 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1033 if (bitmap
->HasPixbuf())
1035 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
1039 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
1040 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
1041 bitmap
->GetMask()->GetBitmap() :
1043 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1046 gtk_widget_show(image
);
1048 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1052 wxUnusedVar(bitmap
);
1053 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1054 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1059 else // a normal item
1061 // text has "_" instead of "&" after mitem->SetText() so don't use it
1062 text
= mitem
->GetText() ;
1064 switch ( mitem
->GetKind() )
1069 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1071 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1072 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1074 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1082 GSList
*group
= NULL
;
1083 if ( m_prevRadio
== NULL
)
1085 // start of a new radio group
1087 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1089 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1090 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1092 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1095 else // continue the radio group
1098 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1099 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1101 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1102 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1103 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1110 wxFAIL_MSG( _T("unexpected menu item kind") );
1116 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1118 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1119 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1129 GdkModifierType accel_mods
;
1130 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1132 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1133 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1136 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1145 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1147 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1149 gtk_widget_show( menuItem
);
1151 if ( !mitem
->IsSeparator() )
1153 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1155 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1156 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1159 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1160 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1163 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1165 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1167 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1169 // if adding a submenu to a menu already existing in the menu bar, we
1170 // must set invoking window to allow processing events from this
1172 if ( m_invokingWindow
)
1173 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1177 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1178 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1183 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1184 if (accel_key
!= GDK_VoidSymbol
)
1186 gtk_widget_add_accelerator (menuItem
,
1188 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1196 mitem
->SetMenuItem(menuItem
);
1200 // This doesn't even exist!
1201 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1207 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1209 if (!GtkAppend(mitem
))
1212 return wxMenuBase::DoAppend(mitem
);
1215 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1217 if ( !wxMenuBase::DoInsert(pos
, item
) )
1221 if ( !GtkAppend(item
, (int)pos
) )
1227 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1229 if ( !wxMenuBase::DoRemove(item
) )
1230 return (wxMenuItem
*)NULL
;
1232 // TODO: this code doesn't delete the item factory item and this seems
1233 // impossible as of GTK 1.2.6.
1234 gtk_widget_destroy( item
->GetMenuItem() );
1239 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1241 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1244 wxMenuItem
*item
= node
->GetData();
1245 if (item
->GetMenuItem() == menuItem
)
1246 return item
->GetId();
1247 node
= node
->GetNext();
1253 // ----------------------------------------------------------------------------
1255 // ----------------------------------------------------------------------------
1259 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1263 wxAcceleratorEntry
*accel
= item
.GetAccel();
1266 int flags
= accel
->GetFlags();
1267 if ( flags
& wxACCEL_ALT
)
1268 hotkey
+= wxT("<alt>");
1269 if ( flags
& wxACCEL_CTRL
)
1270 hotkey
+= wxT("<control>");
1271 if ( flags
& wxACCEL_SHIFT
)
1272 hotkey
+= wxT("<shift>");
1274 int code
= accel
->GetKeyCode();
1301 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1304 // TODO: we should use gdk_keyval_name() (a.k.a.
1305 // XKeysymToString) here as well as hardcoding the keysym
1306 // names this might be not portable
1308 hotkey
<< wxT("Insert" );
1311 hotkey
<< wxT("Delete" );
1314 hotkey
<< wxT("Up" );
1317 hotkey
<< wxT("Down" );
1321 hotkey
<< wxT("Prior" );
1325 hotkey
<< wxT("Next" );
1328 hotkey
<< wxT("Left" );
1331 hotkey
<< wxT("Right" );
1334 hotkey
<< wxT("Home" );
1337 hotkey
<< wxT("End" );
1340 hotkey
<< wxT("Return" );
1343 hotkey
<< wxT("BackSpace" );
1346 hotkey
<< wxT("Tab" );
1349 hotkey
<< wxT("Esc" );
1352 hotkey
<< wxT("space" );
1355 hotkey
<< wxT("Multiply" );
1358 hotkey
<< wxT("Add" );
1361 hotkey
<< wxT("Separator" );
1364 hotkey
<< wxT("Subtract" );
1367 hotkey
<< wxT("Decimal" );
1370 hotkey
<< wxT("Divide" );
1373 hotkey
<< wxT("Cancel" );
1376 hotkey
<< wxT("Clear" );
1379 hotkey
<< wxT("Menu" );
1382 hotkey
<< wxT("Pause" );
1385 hotkey
<< wxT("Capital" );
1388 hotkey
<< wxT("Select" );
1391 hotkey
<< wxT("Print" );
1394 hotkey
<< wxT("Execute" );
1397 hotkey
<< wxT("Snapshot" );
1400 hotkey
<< wxT("Help" );
1403 hotkey
<< wxT("Num_Lock" );
1406 hotkey
<< wxT("Scroll_Lock" );
1408 case WXK_NUMPAD_INSERT
:
1409 hotkey
<< wxT("KP_Insert" );
1411 case WXK_NUMPAD_DELETE
:
1412 hotkey
<< wxT("KP_Delete" );
1414 case WXK_NUMPAD_SPACE
:
1415 hotkey
<< wxT("KP_Space" );
1417 case WXK_NUMPAD_TAB
:
1418 hotkey
<< wxT("KP_Tab" );
1420 case WXK_NUMPAD_ENTER
:
1421 hotkey
<< wxT("KP_Enter" );
1423 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1425 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1427 case WXK_NUMPAD_HOME
:
1428 hotkey
<< wxT("KP_Home" );
1430 case WXK_NUMPAD_LEFT
:
1431 hotkey
<< wxT("KP_Left" );
1434 hotkey
<< wxT("KP_Up" );
1436 case WXK_NUMPAD_RIGHT
:
1437 hotkey
<< wxT("KP_Right" );
1439 case WXK_NUMPAD_DOWN
:
1440 hotkey
<< wxT("KP_Down" );
1442 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1443 hotkey
<< wxT("KP_Prior" );
1445 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1446 hotkey
<< wxT("KP_Next" );
1448 case WXK_NUMPAD_END
:
1449 hotkey
<< wxT("KP_End" );
1451 case WXK_NUMPAD_BEGIN
:
1452 hotkey
<< wxT("KP_Begin" );
1454 case WXK_NUMPAD_EQUAL
:
1455 hotkey
<< wxT("KP_Equal" );
1457 case WXK_NUMPAD_MULTIPLY
:
1458 hotkey
<< wxT("KP_Multiply" );
1460 case WXK_NUMPAD_ADD
:
1461 hotkey
<< wxT("KP_Add" );
1463 case WXK_NUMPAD_SEPARATOR
:
1464 hotkey
<< wxT("KP_Separator" );
1466 case WXK_NUMPAD_SUBTRACT
:
1467 hotkey
<< wxT("KP_Subtract" );
1469 case WXK_NUMPAD_DECIMAL
:
1470 hotkey
<< wxT("KP_Decimal" );
1472 case WXK_NUMPAD_DIVIDE
:
1473 hotkey
<< wxT("KP_Divide" );
1475 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1476 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1477 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1478 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1480 case WXK_WINDOWS_LEFT
:
1481 hotkey
<< wxT("Super_L" );
1483 case WXK_WINDOWS_RIGHT
:
1484 hotkey
<< wxT("Super_R" );
1486 case WXK_WINDOWS_MENU
:
1487 hotkey
<< wxT("Menu" );
1490 hotkey
<< wxT("Command" );
1492 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1493 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1494 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1495 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1496 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1497 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1498 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1501 // if there are any other keys wxGetAccelFromString() may
1502 // return, we should process them here
1507 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1515 wxFAIL_MSG( wxT("unknown keyboard accel") );
1524 #endif // wxUSE_ACCEL