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
, wxMenu
*menu
)
131 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, menu
);
133 DoCommonMenuCallbackCode(menu
, event
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
144 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
146 // the parent window is known after wxFrame::SetMenu()
147 m_needParent
= FALSE
;
149 m_invokingWindow
= (wxWindow
*) NULL
;
151 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
152 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
154 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
158 m_menubar
= gtk_menu_bar_new();
160 m_accel
= gtk_accel_group_new();
163 if (style
& wxMB_DOCKABLE
)
165 m_widget
= gtk_handle_box_new();
166 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
167 gtk_widget_show( GTK_WIDGET(m_menubar
) );
171 m_widget
= GTK_WIDGET(m_menubar
);
178 for (size_t i
= 0; i
< n
; ++i
)
179 Append(menus
[i
], titles
[i
]);
182 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
184 Init(n
, menus
, titles
, style
);
187 wxMenuBar::wxMenuBar(long style
)
189 Init(0, NULL
, NULL
, style
);
192 wxMenuBar::wxMenuBar()
194 Init(0, NULL
, NULL
, 0);
197 wxMenuBar::~wxMenuBar()
201 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
203 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
205 wxWindow
*top_frame
= win
;
206 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
207 top_frame
= top_frame
->GetParent();
210 // support for native hot keys
211 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
214 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
217 wxMenuItem
*menuitem
= node
->GetData();
218 if (menuitem
->IsSubMenu())
219 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
220 node
= node
->GetNext();
224 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
226 menu
->SetInvokingWindow( win
);
228 wxWindow
*top_frame
= win
;
229 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
230 top_frame
= top_frame
->GetParent();
232 // support for native hot keys
233 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
234 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
235 gtk_accel_group_attach( menu
->m_accel
, obj
);
237 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
240 wxMenuItem
*menuitem
= node
->GetData();
241 if (menuitem
->IsSubMenu())
242 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
243 node
= node
->GetNext();
247 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
249 m_invokingWindow
= win
;
250 wxWindow
*top_frame
= win
;
251 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
252 top_frame
= top_frame
->GetParent();
255 // support for native key accelerators indicated by underscroes
256 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
257 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
258 gtk_accel_group_attach( m_accel
, obj
);
261 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
264 wxMenu
*menu
= node
->GetData();
265 wxMenubarSetInvokingWindow( menu
, win
);
266 node
= node
->GetNext();
270 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
272 m_invokingWindow
= (wxWindow
*) NULL
;
273 wxWindow
*top_frame
= win
;
274 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
275 top_frame
= top_frame
->GetParent();
278 // support for native key accelerators indicated by underscroes
279 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
282 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
285 wxMenu
*menu
= node
->GetData();
286 wxMenubarUnsetInvokingWindow( menu
, win
);
287 node
= node
->GetNext();
291 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
293 if ( !wxMenuBarBase::Append( menu
, title
) )
296 return GtkAppend(menu
, title
);
299 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
301 wxString
str( wxReplaceUnderscore( title
) );
303 // This doesn't have much effect right now.
304 menu
->SetTitle( str
);
306 // The "m_owner" is the "menu item"
308 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
310 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
311 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
313 gtk_label_set_text( label
, wxGTK_CONV( str
) );
315 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
316 if (accel_key
!= GDK_VoidSymbol
)
318 gtk_widget_add_accelerator (menu
->m_owner
,
320 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
327 gtk_widget_show( menu
->m_owner
);
329 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
332 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
334 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
336 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
337 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
340 // TODO: this is not enough as we don't get the notification if the menu is
341 // dismissed without any selection (e.g. by clicking outside it), but
342 // I couldn't find any GTK+ signal which would be triggered by this:
343 // neither selection-done, nor deactivate, nor cancel are
344 GtkMenuShell
*menu_shell
= &(GTK_MENU(menu
->m_menu
)->menu_shell
);
345 gtk_signal_connect( GTK_OBJECT(menu_shell
), "selection-done",
346 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
349 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
350 // addings menu later on.
351 if (m_invokingWindow
)
353 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
355 // OPTIMISE ME: we should probably cache this, or pass it
356 // directly, but for now this is a minimal
357 // change to validate the new dynamic sizing.
358 // see (and refactor :) similar code in Remove
361 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
364 frame
->UpdateMenuBarSize();
370 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
372 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
377 if ( !GtkAppend(menu
, title
, (int)pos
) )
383 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
385 // remove the old item and insert a new one
386 wxMenu
*menuOld
= Remove(pos
);
387 if ( menuOld
&& !Insert(pos
, menu
, title
) )
389 return (wxMenu
*) NULL
;
392 // either Insert() succeeded or Remove() failed and menuOld is NULL
396 wxMenu
*wxMenuBar::Remove(size_t pos
)
398 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
400 return (wxMenu
*) NULL
;
402 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
403 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
405 gtk_widget_destroy( menu
->m_owner
);
406 menu
->m_owner
= NULL
;
408 if (m_invokingWindow
)
410 // OPTIMISE ME: see comment in GtkAppend
411 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
414 frame
->UpdateMenuBarSize();
420 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
422 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
424 int res
= menu
->FindItem( itemString
);
425 if (res
!= wxNOT_FOUND
)
429 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
432 wxMenuItem
*item
= node
->GetData();
433 if (item
->IsSubMenu())
434 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
436 node
= node
->GetNext();
442 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
444 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
447 wxMenu
*menu
= node
->GetData();
448 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
451 node
= node
->GetNext();
457 // Find a wxMenuItem using its id. Recurses down into sub-menus
458 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
460 wxMenuItem
* result
= menu
->FindChildItem(id
);
462 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
463 while ( node
&& result
== NULL
)
465 wxMenuItem
*item
= node
->GetData();
466 if (item
->IsSubMenu())
468 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
470 node
= node
->GetNext();
476 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
478 wxMenuItem
* result
= 0;
479 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
480 while (node
&& result
== 0)
482 wxMenu
*menu
= node
->GetData();
483 result
= FindMenuItemByIdRecursive( menu
, id
);
484 node
= node
->GetNext();
489 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
495 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
497 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
499 wxCHECK_RET( node
, wxT("menu not found") );
501 wxMenu
* menu
= node
->GetData();
504 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
507 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
509 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
511 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
513 wxMenu
* menu
= node
->GetData();
516 wxString
text( menu
->GetTitle() );
517 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
519 if ( *pc
== wxT('_') )
521 // '_' is the escape character for GTK+
525 // don't remove ampersands '&' since if we have them in the menu title
526 // it means that they were doubled to indicate "&" instead of accelerator
534 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
536 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
538 wxCHECK_RET( node
, wxT("menu not found") );
540 wxMenu
* menu
= node
->GetData();
542 const wxString
str( wxReplaceUnderscore( label
) );
544 menu
->SetTitle( str
);
548 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
551 gtk_label_set( glabel
, wxGTK_CONV( str
) );
553 /* reparse key accel */
554 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
555 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
560 //-----------------------------------------------------------------------------
562 //-----------------------------------------------------------------------------
565 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
568 wxapp_install_idle_handler();
570 int id
= menu
->FindMenuIdByMenuItem(widget
);
572 /* should find it for normal (not popup) menu */
573 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
574 _T("menu item not found in gtk_menu_clicked_callback") );
576 if (!menu
->IsEnabled(id
))
579 wxMenuItem
* item
= menu
->FindChildItem( id
);
580 wxCHECK_RET( item
, wxT("error in menu item callback") );
582 if ( item
->GetId() == wxGTK_TITLE_ID
)
584 // ignore events from the menu title
588 if (item
->IsCheckable())
590 bool isReallyChecked
= item
->IsChecked(),
591 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
593 // ensure that the internal state is always consistent with what is
594 // shown on the screen
595 item
->wxMenuItemBase::Check(isReallyChecked
);
597 // we must not report the events for the radio button going up nor the
598 // events resulting from the calls to wxMenuItem::Check()
599 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
600 (isInternallyChecked
== isReallyChecked
) )
607 // Is this menu on a menubar? (possibly nested)
608 wxFrame
* frame
= NULL
;
609 if(menu
->IsAttached())
610 frame
= menu
->GetMenuBar()->GetFrame();
612 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
613 // normally wxMenu::SendEvent() should be enough, if it doesn't work
614 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
615 // should be fixed instead of working around it here...
618 // If it is attached then let the frame send the event.
619 // Don't call frame->ProcessCommand(id) because it toggles
620 // checkable items and we've already done that above.
621 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
622 commandEvent
.SetEventObject(frame
);
623 if (item
->IsCheckable())
624 commandEvent
.SetInt(item
->IsChecked());
625 commandEvent
.SetEventObject(menu
);
627 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
631 // otherwise let the menu have it
632 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
637 //-----------------------------------------------------------------------------
639 //-----------------------------------------------------------------------------
642 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
644 if (g_isIdle
) wxapp_install_idle_handler();
646 int id
= menu
->FindMenuIdByMenuItem(widget
);
648 wxASSERT( id
!= -1 ); // should find it!
650 if (!menu
->IsEnabled(id
))
653 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
654 event
.SetEventObject( menu
);
656 wxEvtHandler
* handler
= menu
->GetEventHandler();
657 if (handler
&& handler
->ProcessEvent(event
))
660 wxWindow
*win
= menu
->GetInvokingWindow();
661 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
665 //-----------------------------------------------------------------------------
667 //-----------------------------------------------------------------------------
670 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
672 if (g_isIdle
) wxapp_install_idle_handler();
674 int id
= menu
->FindMenuIdByMenuItem(widget
);
676 wxASSERT( id
!= -1 ); // should find it!
678 if (!menu
->IsEnabled(id
))
681 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
682 event
.SetEventObject( menu
);
684 wxEvtHandler
* handler
= menu
->GetEventHandler();
685 if (handler
&& handler
->ProcessEvent(event
))
688 wxWindow
*win
= menu
->GetInvokingWindow();
690 win
->GetEventHandler()->ProcessEvent( event
);
694 //-----------------------------------------------------------------------------
696 //-----------------------------------------------------------------------------
698 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
700 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
702 const wxString
& name
,
703 const wxString
& help
,
707 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
710 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
712 const wxString
& text
,
713 const wxString
& help
,
716 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
721 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
723 const wxString
& text
,
724 const wxString
& help
,
727 : wxMenuItemBase(parentMenu
, id
, text
, help
,
728 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
733 void wxMenuItem::Init(const wxString
& text
)
735 m_labelWidget
= (GtkWidget
*) NULL
;
736 m_menuItem
= (GtkWidget
*) NULL
;
741 wxMenuItem::~wxMenuItem()
743 // don't delete menu items, the menus take care of that
746 // return the menu item text without any menu accels
748 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
752 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
754 if ( *pc
== wxT('\t'))
757 if ( *pc
== wxT('_') )
759 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
766 if ( *pc
== wxT('\\') )
768 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
775 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
778 // "&" is doubled to indicate "&" instead of accelerator
785 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
790 void wxMenuItem::SetText( const wxString
& str
)
792 // Some optimization to avoid flicker
793 wxString oldLabel
= m_text
;
794 oldLabel
= wxStripMenuCodes(oldLabel
);
795 oldLabel
.Replace(wxT("_"), wxT(""));
796 wxString label1
= wxStripMenuCodes(str
);
797 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
798 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
802 if (oldLabel
== label1
&&
803 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
810 label
= (GtkLabel
*) m_labelWidget
;
812 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
815 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
818 gtk_label_set( label
, wxGTK_CONV( m_text
) );
821 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
822 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
827 GdkModifierType accel_mods
;
828 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
831 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
832 m_parentMenu
->m_accel
,
837 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
838 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
841 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
843 m_parentMenu
->m_accel
,
850 // it's valid for this function to be called even if m_menuItem == NULL
851 void wxMenuItem::DoSetText( const wxString
& str
)
853 // '\t' is the deliminator indicating a hot key
855 const wxChar
*pc
= str
;
856 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
858 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
860 // "&" is doubled to indicate "&" instead of accelerator
864 else if (*pc
== wxT('&'))
868 else if ( *pc
== wxT('_') ) // escape underscores
887 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
892 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
897 return (wxAcceleratorEntry
*)NULL
;
900 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
902 label
<< wxT('\t') << GetHotKey();
904 return wxGetAccelFromString(label
);
907 #endif // wxUSE_ACCEL
909 void wxMenuItem::Check( bool check
)
911 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
913 if (check
== m_isChecked
)
916 wxMenuItemBase::Check( check
);
922 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
926 wxFAIL_MSG( _T("can't check this item") );
930 void wxMenuItem::Enable( bool enable
)
932 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
934 gtk_widget_set_sensitive( m_menuItem
, enable
);
935 wxMenuItemBase::Enable( enable
);
938 bool wxMenuItem::IsChecked() const
940 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
942 wxCHECK_MSG( IsCheckable(), FALSE
,
943 wxT("can't get state of uncheckable item!") );
945 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
948 //-----------------------------------------------------------------------------
950 //-----------------------------------------------------------------------------
952 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
956 m_accel
= gtk_accel_group_new();
957 m_menu
= gtk_menu_new();
958 // NB: keep reference to the menu so that it is not destroyed behind
959 // our back by GTK+ e.g. when it is removed from menubar:
960 gtk_widget_ref(m_menu
);
962 m_owner
= (GtkWidget
*) NULL
;
964 // Tearoffs are entries, just like separators. So if we want this
965 // menu to be a tear-off one, we just append a tearoff entry
967 if ( m_style
& wxMENU_TEAROFF
)
969 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
971 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
976 // append the title as the very first entry if we have it
977 if ( !m_title
.empty() )
979 Append(wxGTK_TITLE_ID
, m_title
);
986 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
988 if ( GTK_IS_WIDGET( m_menu
))
991 gtk_widget_unref( m_menu
);
992 // if the menu is inserted in another menu at this time, there was
993 // one more reference to it:
995 gtk_widget_destroy( m_menu
);
999 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
1001 GtkWidget
*menuItem
;
1008 if ( mitem
->IsSeparator() )
1011 menuItem
= gtk_separator_menu_item_new();
1014 menuItem
= gtk_menu_item_new();
1017 else if (mitem
->GetBitmap().Ok())
1019 text
= mitem
->GetText();
1020 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1023 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1026 if (bitmap
->HasPixbuf())
1028 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
1032 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
1033 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
1034 bitmap
->GetMask()->GetBitmap() :
1036 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1039 gtk_widget_show(image
);
1041 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1045 wxUnusedVar(bitmap
);
1046 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1047 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1052 else // a normal item
1054 // text has "_" instead of "&" after mitem->SetText() so don't use it
1055 text
= mitem
->GetText() ;
1057 switch ( mitem
->GetKind() )
1062 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1064 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1065 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1067 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1075 GSList
*group
= NULL
;
1076 if ( m_prevRadio
== NULL
)
1078 // start of a new radio group
1080 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1082 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1083 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1085 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1088 else // continue the radio group
1091 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1092 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1094 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1095 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1096 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1103 wxFAIL_MSG( _T("unexpected menu item kind") );
1109 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1111 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1112 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1122 GdkModifierType accel_mods
;
1123 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1125 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1126 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1129 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1138 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1140 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1142 gtk_widget_show( menuItem
);
1144 if ( !mitem
->IsSeparator() )
1146 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1148 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1149 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1152 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1153 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1156 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1158 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1160 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1162 // if adding a submenu to a menu already existing in the menu bar, we
1163 // must set invoking window to allow processing events from this
1165 if ( m_invokingWindow
)
1166 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1170 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1171 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1176 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1177 if (accel_key
!= GDK_VoidSymbol
)
1179 gtk_widget_add_accelerator (menuItem
,
1181 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1189 mitem
->SetMenuItem(menuItem
);
1193 // This doesn't even exist!
1194 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1200 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1202 if (!GtkAppend(mitem
))
1205 return wxMenuBase::DoAppend(mitem
);
1208 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1210 if ( !wxMenuBase::DoInsert(pos
, item
) )
1214 if ( !GtkAppend(item
, (int)pos
) )
1220 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1222 if ( !wxMenuBase::DoRemove(item
) )
1223 return (wxMenuItem
*)NULL
;
1225 // TODO: this code doesn't delete the item factory item and this seems
1226 // impossible as of GTK 1.2.6.
1227 gtk_widget_destroy( item
->GetMenuItem() );
1232 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1234 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1237 wxMenuItem
*item
= node
->GetData();
1238 if (item
->GetMenuItem() == menuItem
)
1239 return item
->GetId();
1240 node
= node
->GetNext();
1246 // ----------------------------------------------------------------------------
1248 // ----------------------------------------------------------------------------
1252 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1256 wxAcceleratorEntry
*accel
= item
.GetAccel();
1259 int flags
= accel
->GetFlags();
1260 if ( flags
& wxACCEL_ALT
)
1261 hotkey
+= wxT("<alt>");
1262 if ( flags
& wxACCEL_CTRL
)
1263 hotkey
+= wxT("<control>");
1264 if ( flags
& wxACCEL_SHIFT
)
1265 hotkey
+= wxT("<shift>");
1267 int code
= accel
->GetKeyCode();
1294 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1297 // TODO: we should use gdk_keyval_name() (a.k.a.
1298 // XKeysymToString) here as well as hardcoding the keysym
1299 // names this might be not portable
1301 hotkey
<< wxT("Insert" );
1304 hotkey
<< wxT("Delete" );
1307 hotkey
<< wxT("Up" );
1310 hotkey
<< wxT("Down" );
1314 hotkey
<< wxT("Prior" );
1318 hotkey
<< wxT("Next" );
1321 hotkey
<< wxT("Left" );
1324 hotkey
<< wxT("Right" );
1327 hotkey
<< wxT("Home" );
1330 hotkey
<< wxT("End" );
1333 hotkey
<< wxT("Return" );
1336 hotkey
<< wxT("BackSpace" );
1339 hotkey
<< wxT("Tab" );
1342 hotkey
<< wxT("Esc" );
1345 hotkey
<< wxT("space" );
1348 hotkey
<< wxT("Multiply" );
1351 hotkey
<< wxT("Add" );
1354 hotkey
<< wxT("Separator" );
1357 hotkey
<< wxT("Subtract" );
1360 hotkey
<< wxT("Decimal" );
1363 hotkey
<< wxT("Divide" );
1366 hotkey
<< wxT("Cancel" );
1369 hotkey
<< wxT("Clear" );
1372 hotkey
<< wxT("Menu" );
1375 hotkey
<< wxT("Pause" );
1378 hotkey
<< wxT("Capital" );
1381 hotkey
<< wxT("Select" );
1384 hotkey
<< wxT("Print" );
1387 hotkey
<< wxT("Execute" );
1390 hotkey
<< wxT("Snapshot" );
1393 hotkey
<< wxT("Help" );
1396 hotkey
<< wxT("Num_Lock" );
1399 hotkey
<< wxT("Scroll_Lock" );
1401 case WXK_NUMPAD_INSERT
:
1402 hotkey
<< wxT("KP_Insert" );
1404 case WXK_NUMPAD_DELETE
:
1405 hotkey
<< wxT("KP_Delete" );
1407 case WXK_NUMPAD_SPACE
:
1408 hotkey
<< wxT("KP_Space" );
1410 case WXK_NUMPAD_TAB
:
1411 hotkey
<< wxT("KP_Tab" );
1413 case WXK_NUMPAD_ENTER
:
1414 hotkey
<< wxT("KP_Enter" );
1416 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1418 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1420 case WXK_NUMPAD_HOME
:
1421 hotkey
<< wxT("KP_Home" );
1423 case WXK_NUMPAD_LEFT
:
1424 hotkey
<< wxT("KP_Left" );
1427 hotkey
<< wxT("KP_Up" );
1429 case WXK_NUMPAD_RIGHT
:
1430 hotkey
<< wxT("KP_Right" );
1432 case WXK_NUMPAD_DOWN
:
1433 hotkey
<< wxT("KP_Down" );
1435 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1436 hotkey
<< wxT("KP_Prior" );
1438 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1439 hotkey
<< wxT("KP_Next" );
1441 case WXK_NUMPAD_END
:
1442 hotkey
<< wxT("KP_End" );
1444 case WXK_NUMPAD_BEGIN
:
1445 hotkey
<< wxT("KP_Begin" );
1447 case WXK_NUMPAD_EQUAL
:
1448 hotkey
<< wxT("KP_Equal" );
1450 case WXK_NUMPAD_MULTIPLY
:
1451 hotkey
<< wxT("KP_Multiply" );
1453 case WXK_NUMPAD_ADD
:
1454 hotkey
<< wxT("KP_Add" );
1456 case WXK_NUMPAD_SEPARATOR
:
1457 hotkey
<< wxT("KP_Separator" );
1459 case WXK_NUMPAD_SUBTRACT
:
1460 hotkey
<< wxT("KP_Subtract" );
1462 case WXK_NUMPAD_DECIMAL
:
1463 hotkey
<< wxT("KP_Decimal" );
1465 case WXK_NUMPAD_DIVIDE
:
1466 hotkey
<< wxT("KP_Divide" );
1468 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1469 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1470 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1471 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1473 case WXK_WINDOWS_LEFT
:
1474 hotkey
<< wxT("Super_L" );
1476 case WXK_WINDOWS_RIGHT
:
1477 hotkey
<< wxT("Super_R" );
1479 case WXK_WINDOWS_MENU
:
1480 hotkey
<< wxT("Menu" );
1483 hotkey
<< wxT("Command" );
1485 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1486 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1487 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1488 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1489 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1490 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1491 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1494 // if there are any other keys wxGetAccelFromString() may
1495 // return, we should process them here
1500 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1508 wxFAIL_MSG( wxT("unknown keyboard accel") );
1517 #endif // wxUSE_ACCEL