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 if (style
& wxMB_DOCKABLE
)
168 m_widget
= gtk_handle_box_new();
169 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
170 gtk_widget_show( GTK_WIDGET(m_menubar
) );
174 m_widget
= GTK_WIDGET(m_menubar
);
181 for (size_t i
= 0; i
< n
; ++i
)
182 Append(menus
[i
], titles
[i
]);
184 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
185 // don't get it when the menu is dismissed by clicking outside the
186 // toolbar) so we connect to the global one, even if it means that we
187 // can't pass the menu which was closed in wxMenuEvent object
188 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
190 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
195 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
197 Init(n
, menus
, titles
, style
);
200 wxMenuBar::wxMenuBar(long style
)
202 Init(0, NULL
, NULL
, style
);
205 wxMenuBar::wxMenuBar()
207 Init(0, NULL
, NULL
, 0);
210 wxMenuBar::~wxMenuBar()
214 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
216 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
218 wxWindow
*top_frame
= win
;
219 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
220 top_frame
= top_frame
->GetParent();
222 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
225 wxMenuItem
*menuitem
= node
->GetData();
226 if (menuitem
->IsSubMenu())
227 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
228 node
= node
->GetNext();
232 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
234 menu
->SetInvokingWindow( win
);
236 wxWindow
*top_frame
= win
;
237 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
238 top_frame
= top_frame
->GetParent();
240 // support for native hot keys
241 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
242 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
243 gtk_accel_group_attach( menu
->m_accel
, obj
);
245 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
248 wxMenuItem
*menuitem
= node
->GetData();
249 if (menuitem
->IsSubMenu())
250 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
251 node
= node
->GetNext();
255 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
257 m_invokingWindow
= win
;
258 wxWindow
*top_frame
= win
;
259 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
260 top_frame
= top_frame
->GetParent();
262 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
265 wxMenu
*menu
= node
->GetData();
266 wxMenubarSetInvokingWindow( menu
, win
);
267 node
= node
->GetNext();
271 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
273 m_invokingWindow
= (wxWindow
*) NULL
;
274 wxWindow
*top_frame
= win
;
275 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
276 top_frame
= top_frame
->GetParent();
278 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
281 wxMenu
*menu
= node
->GetData();
282 wxMenubarUnsetInvokingWindow( menu
, win
);
283 node
= node
->GetNext();
287 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
289 if ( !wxMenuBarBase::Append( menu
, title
) )
292 return GtkAppend(menu
, title
);
295 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
297 wxString
str( wxReplaceUnderscore( title
) );
299 // This doesn't have much effect right now.
300 menu
->SetTitle( str
);
302 // The "m_owner" is the "menu item"
303 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
305 gtk_widget_show( menu
->m_owner
);
307 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
310 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
312 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
314 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
315 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
318 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
319 // addings menu later on.
320 if (m_invokingWindow
)
322 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
324 // OPTIMISE ME: we should probably cache this, or pass it
325 // directly, but for now this is a minimal
326 // change to validate the new dynamic sizing.
327 // see (and refactor :) similar code in Remove
330 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
333 frame
->UpdateMenuBarSize();
339 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
341 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
346 if ( !GtkAppend(menu
, title
, (int)pos
) )
352 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
354 // remove the old item and insert a new one
355 wxMenu
*menuOld
= Remove(pos
);
356 if ( menuOld
&& !Insert(pos
, menu
, title
) )
358 return (wxMenu
*) NULL
;
361 // either Insert() succeeded or Remove() failed and menuOld is NULL
365 wxMenu
*wxMenuBar::Remove(size_t pos
)
367 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
369 return (wxMenu
*) NULL
;
371 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
372 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
374 gtk_widget_destroy( menu
->m_owner
);
375 menu
->m_owner
= NULL
;
377 if (m_invokingWindow
)
379 // OPTIMISE ME: see comment in GtkAppend
380 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
383 frame
->UpdateMenuBarSize();
389 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
391 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
393 int res
= menu
->FindItem( itemString
);
394 if (res
!= wxNOT_FOUND
)
398 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
401 wxMenuItem
*item
= node
->GetData();
402 if (item
->IsSubMenu())
403 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
405 node
= node
->GetNext();
411 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
413 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
416 wxMenu
*menu
= node
->GetData();
417 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
420 node
= node
->GetNext();
426 // Find a wxMenuItem using its id. Recurses down into sub-menus
427 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
429 wxMenuItem
* result
= menu
->FindChildItem(id
);
431 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
432 while ( node
&& result
== NULL
)
434 wxMenuItem
*item
= node
->GetData();
435 if (item
->IsSubMenu())
437 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
439 node
= node
->GetNext();
445 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
447 wxMenuItem
* result
= 0;
448 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
449 while (node
&& result
== 0)
451 wxMenu
*menu
= node
->GetData();
452 result
= FindMenuItemByIdRecursive( menu
, id
);
453 node
= node
->GetNext();
458 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
464 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
466 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
468 wxCHECK_RET( node
, wxT("menu not found") );
470 wxMenu
* menu
= node
->GetData();
473 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
476 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
478 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
480 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
482 wxMenu
* menu
= node
->GetData();
485 wxString
text( menu
->GetTitle() );
486 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
488 if ( *pc
== wxT('_') )
490 // '_' is the escape character for GTK+
494 // don't remove ampersands '&' since if we have them in the menu title
495 // it means that they were doubled to indicate "&" instead of accelerator
503 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
505 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
507 wxCHECK_RET( node
, wxT("menu not found") );
509 wxMenu
* menu
= node
->GetData();
511 const wxString
str( wxReplaceUnderscore( label
) );
513 menu
->SetTitle( str
);
517 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
520 gtk_label_set( glabel
, wxGTK_CONV( str
) );
522 /* reparse key accel */
523 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
524 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
529 //-----------------------------------------------------------------------------
531 //-----------------------------------------------------------------------------
534 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
537 wxapp_install_idle_handler();
539 int id
= menu
->FindMenuIdByMenuItem(widget
);
541 /* should find it for normal (not popup) menu */
542 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
543 _T("menu item not found in gtk_menu_clicked_callback") );
545 if (!menu
->IsEnabled(id
))
548 wxMenuItem
* item
= menu
->FindChildItem( id
);
549 wxCHECK_RET( item
, wxT("error in menu item callback") );
551 if ( item
->GetId() == wxGTK_TITLE_ID
)
553 // ignore events from the menu title
557 if (item
->IsCheckable())
559 bool isReallyChecked
= item
->IsChecked(),
560 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
562 // ensure that the internal state is always consistent with what is
563 // shown on the screen
564 item
->wxMenuItemBase::Check(isReallyChecked
);
566 // we must not report the events for the radio button going up nor the
567 // events resulting from the calls to wxMenuItem::Check()
568 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
569 (isInternallyChecked
== isReallyChecked
) )
576 // Is this menu on a menubar? (possibly nested)
577 wxFrame
* frame
= NULL
;
578 if(menu
->IsAttached())
579 frame
= menu
->GetMenuBar()->GetFrame();
581 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
582 // normally wxMenu::SendEvent() should be enough, if it doesn't work
583 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
584 // should be fixed instead of working around it here...
587 // If it is attached then let the frame send the event.
588 // Don't call frame->ProcessCommand(id) because it toggles
589 // checkable items and we've already done that above.
590 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
591 commandEvent
.SetEventObject(frame
);
592 if (item
->IsCheckable())
593 commandEvent
.SetInt(item
->IsChecked());
594 commandEvent
.SetEventObject(menu
);
596 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
600 // otherwise let the menu have it
601 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
606 //-----------------------------------------------------------------------------
608 //-----------------------------------------------------------------------------
611 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
613 if (g_isIdle
) wxapp_install_idle_handler();
615 int id
= menu
->FindMenuIdByMenuItem(widget
);
617 wxASSERT( id
!= -1 ); // should find it!
619 if (!menu
->IsEnabled(id
))
622 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
623 event
.SetEventObject( menu
);
625 wxEvtHandler
* handler
= menu
->GetEventHandler();
626 if (handler
&& handler
->ProcessEvent(event
))
629 wxWindow
*win
= menu
->GetInvokingWindow();
630 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
634 //-----------------------------------------------------------------------------
636 //-----------------------------------------------------------------------------
639 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
641 if (g_isIdle
) wxapp_install_idle_handler();
643 int id
= menu
->FindMenuIdByMenuItem(widget
);
645 wxASSERT( id
!= -1 ); // should find it!
647 if (!menu
->IsEnabled(id
))
650 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
651 event
.SetEventObject( menu
);
653 wxEvtHandler
* handler
= menu
->GetEventHandler();
654 if (handler
&& handler
->ProcessEvent(event
))
657 wxWindow
*win
= menu
->GetInvokingWindow();
659 win
->GetEventHandler()->ProcessEvent( event
);
663 //-----------------------------------------------------------------------------
665 //-----------------------------------------------------------------------------
667 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
669 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
671 const wxString
& name
,
672 const wxString
& help
,
676 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
679 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
681 const wxString
& text
,
682 const wxString
& help
,
685 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
690 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
692 const wxString
& text
,
693 const wxString
& help
,
696 : wxMenuItemBase(parentMenu
, id
, text
, help
,
697 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
702 void wxMenuItem::Init(const wxString
& text
)
704 m_labelWidget
= (GtkWidget
*) NULL
;
705 m_menuItem
= (GtkWidget
*) NULL
;
710 wxMenuItem::~wxMenuItem()
712 // don't delete menu items, the menus take care of that
715 // return the menu item text without any menu accels
717 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
721 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
723 if ( *pc
== wxT('\t'))
726 if ( *pc
== wxT('_') )
728 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
734 if ( *pc
== wxT('\\') )
736 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
742 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
745 // "&" is doubled to indicate "&" instead of accelerator
752 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
757 void wxMenuItem::SetText( const wxString
& str
)
759 // Some optimization to avoid flicker
760 wxString oldLabel
= m_text
;
761 oldLabel
= wxStripMenuCodes(oldLabel
);
762 oldLabel
.Replace(wxT("_"), wxT(""));
763 wxString label1
= wxStripMenuCodes(str
);
764 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
765 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
769 if (oldLabel
== label1
&&
770 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
777 label
= (GtkLabel
*) m_labelWidget
;
779 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
781 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
785 GdkModifierType accel_mods
;
786 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
789 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
790 m_parentMenu
->m_accel
,
795 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
796 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
799 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
801 m_parentMenu
->m_accel
,
808 // it's valid for this function to be called even if m_menuItem == NULL
809 void wxMenuItem::DoSetText( const wxString
& str
)
811 // '\t' is the deliminator indicating a hot key
813 const wxChar
*pc
= str
;
814 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
816 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
818 // "&" is doubled to indicate "&" instead of accelerator
822 else if (*pc
== wxT('&'))
826 else if ( *pc
== wxT('_') ) // escape underscores
845 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
850 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
855 return (wxAcceleratorEntry
*)NULL
;
858 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
860 label
<< wxT('\t') << GetHotKey();
862 return wxGetAccelFromString(label
);
865 #endif // wxUSE_ACCEL
867 void wxMenuItem::Check( bool check
)
869 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
871 if (check
== m_isChecked
)
874 wxMenuItemBase::Check( check
);
880 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
884 wxFAIL_MSG( _T("can't check this item") );
888 void wxMenuItem::Enable( bool enable
)
890 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
892 gtk_widget_set_sensitive( m_menuItem
, enable
);
893 wxMenuItemBase::Enable( enable
);
896 bool wxMenuItem::IsChecked() const
898 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
900 wxCHECK_MSG( IsCheckable(), FALSE
,
901 wxT("can't get state of uncheckable item!") );
903 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
906 //-----------------------------------------------------------------------------
908 //-----------------------------------------------------------------------------
910 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
914 m_accel
= gtk_accel_group_new();
915 m_menu
= gtk_menu_new();
916 // NB: keep reference to the menu so that it is not destroyed behind
917 // our back by GTK+ e.g. when it is removed from menubar:
918 gtk_widget_ref(m_menu
);
920 m_owner
= (GtkWidget
*) NULL
;
922 // Tearoffs are entries, just like separators. So if we want this
923 // menu to be a tear-off one, we just append a tearoff entry
925 if ( m_style
& wxMENU_TEAROFF
)
927 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
929 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
934 // append the title as the very first entry if we have it
935 if ( !m_title
.empty() )
937 Append(wxGTK_TITLE_ID
, m_title
);
944 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
946 if ( GTK_IS_WIDGET( m_menu
))
949 gtk_widget_unref( m_menu
);
950 // if the menu is inserted in another menu at this time, there was
951 // one more reference to it:
953 gtk_widget_destroy( m_menu
);
957 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
963 if ( mitem
->IsSeparator() )
965 menuItem
= gtk_separator_menu_item_new();
967 else if (mitem
->GetBitmap().Ok())
969 text
= mitem
->GetText();
970 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
972 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
975 if (bitmap
->HasPixbuf())
977 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
981 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
982 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
983 bitmap
->GetMask()->GetBitmap() :
985 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
988 gtk_widget_show(image
);
990 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
994 else // a normal item
996 // text has "_" instead of "&" after mitem->SetText() so don't use it
997 text
= mitem
->GetText() ;
999 switch ( mitem
->GetKind() )
1003 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1010 GSList
*group
= NULL
;
1011 if ( m_prevRadio
== NULL
)
1013 // start of a new radio group
1014 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1016 else // continue the radio group
1018 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1019 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1025 wxFAIL_MSG( _T("unexpected menu item kind") );
1030 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1039 GdkModifierType accel_mods
;
1040 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1042 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1043 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1046 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1055 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1057 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1059 gtk_widget_show( menuItem
);
1061 if ( !mitem
->IsSeparator() )
1063 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1065 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1066 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1069 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1070 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1073 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1075 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1077 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1079 // if adding a submenu to a menu already existing in the menu bar, we
1080 // must set invoking window to allow processing events from this
1082 if ( m_invokingWindow
)
1083 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1087 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1088 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1093 mitem
->SetMenuItem(menuItem
);
1097 // This doesn't even exist!
1098 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1104 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1106 if (!GtkAppend(mitem
))
1109 return wxMenuBase::DoAppend(mitem
);
1112 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1114 if ( !wxMenuBase::DoInsert(pos
, item
) )
1118 if ( !GtkAppend(item
, (int)pos
) )
1124 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1126 if ( !wxMenuBase::DoRemove(item
) )
1127 return (wxMenuItem
*)NULL
;
1129 // TODO: this code doesn't delete the item factory item and this seems
1130 // impossible as of GTK 1.2.6.
1131 gtk_widget_destroy( item
->GetMenuItem() );
1136 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1138 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1141 wxMenuItem
*item
= node
->GetData();
1142 if (item
->GetMenuItem() == menuItem
)
1143 return item
->GetId();
1144 node
= node
->GetNext();
1150 // ----------------------------------------------------------------------------
1152 // ----------------------------------------------------------------------------
1156 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1160 wxAcceleratorEntry
*accel
= item
.GetAccel();
1163 int flags
= accel
->GetFlags();
1164 if ( flags
& wxACCEL_ALT
)
1165 hotkey
+= wxT("<alt>");
1166 if ( flags
& wxACCEL_CTRL
)
1167 hotkey
+= wxT("<control>");
1168 if ( flags
& wxACCEL_SHIFT
)
1169 hotkey
+= wxT("<shift>");
1171 int code
= accel
->GetKeyCode();
1198 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1201 // TODO: we should use gdk_keyval_name() (a.k.a.
1202 // XKeysymToString) here as well as hardcoding the keysym
1203 // names this might be not portable
1205 hotkey
<< wxT("Insert" );
1208 hotkey
<< wxT("Delete" );
1211 hotkey
<< wxT("Up" );
1214 hotkey
<< wxT("Down" );
1218 hotkey
<< wxT("Prior" );
1222 hotkey
<< wxT("Next" );
1225 hotkey
<< wxT("Left" );
1228 hotkey
<< wxT("Right" );
1231 hotkey
<< wxT("Home" );
1234 hotkey
<< wxT("End" );
1237 hotkey
<< wxT("Return" );
1240 hotkey
<< wxT("BackSpace" );
1243 hotkey
<< wxT("Tab" );
1246 hotkey
<< wxT("Esc" );
1249 hotkey
<< wxT("space" );
1252 hotkey
<< wxT("Multiply" );
1255 hotkey
<< wxT("Add" );
1258 hotkey
<< wxT("Separator" );
1261 hotkey
<< wxT("Subtract" );
1264 hotkey
<< wxT("Decimal" );
1267 hotkey
<< wxT("Divide" );
1270 hotkey
<< wxT("Cancel" );
1273 hotkey
<< wxT("Clear" );
1276 hotkey
<< wxT("Menu" );
1279 hotkey
<< wxT("Pause" );
1282 hotkey
<< wxT("Capital" );
1285 hotkey
<< wxT("Select" );
1288 hotkey
<< wxT("Print" );
1291 hotkey
<< wxT("Execute" );
1294 hotkey
<< wxT("Snapshot" );
1297 hotkey
<< wxT("Help" );
1300 hotkey
<< wxT("Num_Lock" );
1303 hotkey
<< wxT("Scroll_Lock" );
1305 case WXK_NUMPAD_INSERT
:
1306 hotkey
<< wxT("KP_Insert" );
1308 case WXK_NUMPAD_DELETE
:
1309 hotkey
<< wxT("KP_Delete" );
1311 case WXK_NUMPAD_SPACE
:
1312 hotkey
<< wxT("KP_Space" );
1314 case WXK_NUMPAD_TAB
:
1315 hotkey
<< wxT("KP_Tab" );
1317 case WXK_NUMPAD_ENTER
:
1318 hotkey
<< wxT("KP_Enter" );
1320 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1322 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1324 case WXK_NUMPAD_HOME
:
1325 hotkey
<< wxT("KP_Home" );
1327 case WXK_NUMPAD_LEFT
:
1328 hotkey
<< wxT("KP_Left" );
1331 hotkey
<< wxT("KP_Up" );
1333 case WXK_NUMPAD_RIGHT
:
1334 hotkey
<< wxT("KP_Right" );
1336 case WXK_NUMPAD_DOWN
:
1337 hotkey
<< wxT("KP_Down" );
1339 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1340 hotkey
<< wxT("KP_Prior" );
1342 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1343 hotkey
<< wxT("KP_Next" );
1345 case WXK_NUMPAD_END
:
1346 hotkey
<< wxT("KP_End" );
1348 case WXK_NUMPAD_BEGIN
:
1349 hotkey
<< wxT("KP_Begin" );
1351 case WXK_NUMPAD_EQUAL
:
1352 hotkey
<< wxT("KP_Equal" );
1354 case WXK_NUMPAD_MULTIPLY
:
1355 hotkey
<< wxT("KP_Multiply" );
1357 case WXK_NUMPAD_ADD
:
1358 hotkey
<< wxT("KP_Add" );
1360 case WXK_NUMPAD_SEPARATOR
:
1361 hotkey
<< wxT("KP_Separator" );
1363 case WXK_NUMPAD_SUBTRACT
:
1364 hotkey
<< wxT("KP_Subtract" );
1366 case WXK_NUMPAD_DECIMAL
:
1367 hotkey
<< wxT("KP_Decimal" );
1369 case WXK_NUMPAD_DIVIDE
:
1370 hotkey
<< wxT("KP_Divide" );
1372 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1373 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1374 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1375 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1377 case WXK_WINDOWS_LEFT
:
1378 hotkey
<< wxT("Super_L" );
1380 case WXK_WINDOWS_RIGHT
:
1381 hotkey
<< wxT("Super_R" );
1383 case WXK_WINDOWS_MENU
:
1384 hotkey
<< wxT("Menu" );
1387 hotkey
<< wxT("Command" );
1389 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1390 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1391 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1392 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1393 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1394 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1395 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1398 // if there are any other keys wxGetAccelFromString() may
1399 // return, we should process them here
1404 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1412 wxFAIL_MSG( wxT("unknown keyboard accel") );
1421 #endif // wxUSE_ACCEL
1423 // ----------------------------------------------------------------------------
1424 // Pop-up menu stuff
1425 // ----------------------------------------------------------------------------
1427 #if wxUSE_MENUS_NATIVE
1430 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1432 *is_waiting
= FALSE
;
1435 void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1437 menu
->SetInvokingWindow( win
);
1439 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1442 wxMenuItem
*menuitem
= node
->GetData();
1443 if (menuitem
->IsSubMenu())
1445 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1448 node
= node
->GetNext();
1453 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1455 gboolean
* WXUNUSED(whatever
),
1456 gpointer user_data
)
1458 // ensure that the menu appears entirely on screen
1460 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1462 wxSize sizeScreen
= wxGetDisplaySize();
1463 wxPoint
*pos
= (wxPoint
*)user_data
;
1465 gint xmax
= sizeScreen
.x
- req
.width
,
1466 ymax
= sizeScreen
.y
- req
.height
;
1468 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1469 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1472 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1474 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1476 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1478 // NOTE: if you change this code, you need to update
1479 // the same code in taskbar.cpp as well. This
1480 // is ugly code duplication, I know.
1482 SetInvokingWindow( menu
, this );
1486 bool is_waiting
= true;
1488 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1490 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1491 (gpointer
)&is_waiting
);
1495 GtkMenuPositionFunc posfunc
;
1496 if ( x
== -1 && y
== -1 )
1498 // use GTK's default positioning algorithm
1504 pos
= ClientToScreen(wxPoint(x
, y
));
1506 posfunc
= wxPopupMenuPositionCallback
;
1509 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1510 DoCommonMenuCallbackCode(menu
, eventOpen
);
1513 GTK_MENU(menu
->m_menu
),
1514 (GtkWidget
*) NULL
, // parent menu shell
1515 (GtkWidget
*) NULL
, // parent menu item
1516 posfunc
, // function to position it
1517 userdata
, // client data
1518 0, // button used to activate it
1519 gtk_get_current_event_time()
1524 gtk_main_iteration();
1527 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1529 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1530 DoCommonMenuCallbackCode(menu
, eventClose
);
1535 #endif // wxUSE_MENUS_NATIVE