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 g_signal_connect (m_menubar
, "deactivate",
189 G_CALLBACK (gtk_menu_close_callback
), this);
193 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
195 Init(n
, menus
, titles
, style
);
198 wxMenuBar::wxMenuBar(long style
)
200 Init(0, NULL
, NULL
, style
);
203 wxMenuBar::wxMenuBar()
205 Init(0, NULL
, NULL
, 0);
208 wxMenuBar::~wxMenuBar()
212 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
214 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
216 wxWindow
*top_frame
= win
;
217 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
218 top_frame
= top_frame
->GetParent();
220 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
223 wxMenuItem
*menuitem
= node
->GetData();
224 if (menuitem
->IsSubMenu())
225 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
226 node
= node
->GetNext();
230 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
232 menu
->SetInvokingWindow( win
);
234 wxWindow
*top_frame
= win
;
235 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
236 top_frame
= top_frame
->GetParent();
238 // support for native hot keys
239 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
240 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
241 gtk_accel_group_attach( menu
->m_accel
, obj
);
243 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
246 wxMenuItem
*menuitem
= node
->GetData();
247 if (menuitem
->IsSubMenu())
248 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
249 node
= node
->GetNext();
253 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
255 m_invokingWindow
= win
;
256 wxWindow
*top_frame
= win
;
257 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
258 top_frame
= top_frame
->GetParent();
260 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
263 wxMenu
*menu
= node
->GetData();
264 wxMenubarSetInvokingWindow( menu
, win
);
265 node
= node
->GetNext();
269 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
271 m_invokingWindow
= (wxWindow
*) NULL
;
272 wxWindow
*top_frame
= win
;
273 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
274 top_frame
= top_frame
->GetParent();
276 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
279 wxMenu
*menu
= node
->GetData();
280 wxMenubarUnsetInvokingWindow( menu
, win
);
281 node
= node
->GetNext();
285 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
287 if ( !wxMenuBarBase::Append( menu
, title
) )
290 return GtkAppend(menu
, title
);
293 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
295 wxString
str( wxReplaceUnderscore( title
) );
297 // This doesn't have much effect right now.
298 menu
->SetTitle( str
);
300 // The "m_owner" is the "menu item"
301 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
303 gtk_widget_show( menu
->m_owner
);
305 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
308 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
310 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
312 g_signal_connect (menu
->m_owner
, "activate",
313 G_CALLBACK (gtk_menu_open_callback
),
316 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
317 // addings menu later on.
318 if (m_invokingWindow
)
320 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
322 // OPTIMISE ME: we should probably cache this, or pass it
323 // directly, but for now this is a minimal
324 // change to validate the new dynamic sizing.
325 // see (and refactor :) similar code in Remove
328 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
331 frame
->UpdateMenuBarSize();
337 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
339 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
344 if ( !GtkAppend(menu
, title
, (int)pos
) )
350 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
352 // remove the old item and insert a new one
353 wxMenu
*menuOld
= Remove(pos
);
354 if ( menuOld
&& !Insert(pos
, menu
, title
) )
356 return (wxMenu
*) NULL
;
359 // either Insert() succeeded or Remove() failed and menuOld is NULL
363 wxMenu
*wxMenuBar::Remove(size_t pos
)
365 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
367 return (wxMenu
*) NULL
;
369 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
370 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
372 gtk_widget_destroy( menu
->m_owner
);
373 menu
->m_owner
= NULL
;
375 if (m_invokingWindow
)
377 // OPTIMISE ME: see comment in GtkAppend
378 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
381 frame
->UpdateMenuBarSize();
387 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
389 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
391 int res
= menu
->FindItem( itemString
);
392 if (res
!= wxNOT_FOUND
)
396 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
399 wxMenuItem
*item
= node
->GetData();
400 if (item
->IsSubMenu())
401 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
403 node
= node
->GetNext();
409 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
411 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
414 wxMenu
*menu
= node
->GetData();
415 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
418 node
= node
->GetNext();
424 // Find a wxMenuItem using its id. Recurses down into sub-menus
425 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
427 wxMenuItem
* result
= menu
->FindChildItem(id
);
429 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
430 while ( node
&& result
== NULL
)
432 wxMenuItem
*item
= node
->GetData();
433 if (item
->IsSubMenu())
435 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
437 node
= node
->GetNext();
443 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
445 wxMenuItem
* result
= 0;
446 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
447 while (node
&& result
== 0)
449 wxMenu
*menu
= node
->GetData();
450 result
= FindMenuItemByIdRecursive( menu
, id
);
451 node
= node
->GetNext();
456 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
462 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
464 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
466 wxCHECK_RET( node
, wxT("menu not found") );
468 wxMenu
* menu
= node
->GetData();
471 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
474 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
476 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
478 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
480 wxMenu
* menu
= node
->GetData();
483 wxString
text( menu
->GetTitle() );
484 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
486 if ( *pc
== wxT('_') )
488 // '_' is the escape character for GTK+
492 // don't remove ampersands '&' since if we have them in the menu title
493 // it means that they were doubled to indicate "&" instead of accelerator
501 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
503 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
505 wxCHECK_RET( node
, wxT("menu not found") );
507 wxMenu
* menu
= node
->GetData();
509 const wxString
str( wxReplaceUnderscore( label
) );
511 menu
->SetTitle( str
);
514 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
517 //-----------------------------------------------------------------------------
519 //-----------------------------------------------------------------------------
522 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
525 wxapp_install_idle_handler();
527 int id
= menu
->FindMenuIdByMenuItem(widget
);
529 /* should find it for normal (not popup) menu */
530 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
531 _T("menu item not found in gtk_menu_clicked_callback") );
533 if (!menu
->IsEnabled(id
))
536 wxMenuItem
* item
= menu
->FindChildItem( id
);
537 wxCHECK_RET( item
, wxT("error in menu item callback") );
539 if ( item
->GetId() == wxGTK_TITLE_ID
)
541 // ignore events from the menu title
545 if (item
->IsCheckable())
547 bool isReallyChecked
= item
->IsChecked(),
548 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
550 // ensure that the internal state is always consistent with what is
551 // shown on the screen
552 item
->wxMenuItemBase::Check(isReallyChecked
);
554 // we must not report the events for the radio button going up nor the
555 // events resulting from the calls to wxMenuItem::Check()
556 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
557 (isInternallyChecked
== isReallyChecked
) )
564 // Is this menu on a menubar? (possibly nested)
565 wxFrame
* frame
= NULL
;
566 if(menu
->IsAttached())
567 frame
= menu
->GetMenuBar()->GetFrame();
569 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
570 // normally wxMenu::SendEvent() should be enough, if it doesn't work
571 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
572 // should be fixed instead of working around it here...
575 // If it is attached then let the frame send the event.
576 // Don't call frame->ProcessCommand(id) because it toggles
577 // checkable items and we've already done that above.
578 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
579 commandEvent
.SetEventObject(frame
);
580 if (item
->IsCheckable())
581 commandEvent
.SetInt(item
->IsChecked());
582 commandEvent
.SetEventObject(menu
);
584 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
588 // otherwise let the menu have it
589 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
594 //-----------------------------------------------------------------------------
596 //-----------------------------------------------------------------------------
599 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
601 if (g_isIdle
) wxapp_install_idle_handler();
603 int id
= menu
->FindMenuIdByMenuItem(widget
);
605 wxASSERT( id
!= -1 ); // should find it!
607 if (!menu
->IsEnabled(id
))
610 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
611 event
.SetEventObject( menu
);
613 wxEvtHandler
* handler
= menu
->GetEventHandler();
614 if (handler
&& handler
->ProcessEvent(event
))
617 wxWindow
*win
= menu
->GetInvokingWindow();
618 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
622 //-----------------------------------------------------------------------------
624 //-----------------------------------------------------------------------------
627 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
629 if (g_isIdle
) wxapp_install_idle_handler();
631 int id
= menu
->FindMenuIdByMenuItem(widget
);
633 wxASSERT( id
!= -1 ); // should find it!
635 if (!menu
->IsEnabled(id
))
638 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
639 event
.SetEventObject( menu
);
641 wxEvtHandler
* handler
= menu
->GetEventHandler();
642 if (handler
&& handler
->ProcessEvent(event
))
645 wxWindow
*win
= menu
->GetInvokingWindow();
647 win
->GetEventHandler()->ProcessEvent( event
);
651 //-----------------------------------------------------------------------------
653 //-----------------------------------------------------------------------------
655 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
657 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
659 const wxString
& name
,
660 const wxString
& help
,
664 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
667 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
669 const wxString
& text
,
670 const wxString
& help
,
673 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
678 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
680 const wxString
& text
,
681 const wxString
& help
,
684 : wxMenuItemBase(parentMenu
, id
, text
, help
,
685 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
690 void wxMenuItem::Init(const wxString
& text
)
692 m_labelWidget
= (GtkWidget
*) NULL
;
693 m_menuItem
= (GtkWidget
*) NULL
;
698 wxMenuItem::~wxMenuItem()
700 // don't delete menu items, the menus take care of that
703 // return the menu item text without any menu accels
705 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
709 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
711 if ( *pc
== wxT('\t'))
714 if ( *pc
== wxT('_') )
716 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
722 if ( *pc
== wxT('\\') )
724 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
730 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
733 // "&" is doubled to indicate "&" instead of accelerator
740 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
745 void wxMenuItem::SetText( const wxString
& str
)
747 // Some optimization to avoid flicker
748 wxString oldLabel
= m_text
;
749 oldLabel
= wxStripMenuCodes(oldLabel
);
750 oldLabel
.Replace(wxT("_"), wxT(""));
751 wxString label1
= wxStripMenuCodes(str
);
752 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
753 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
757 if (oldLabel
== label1
&&
758 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
765 label
= (GtkLabel
*) m_labelWidget
;
767 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
769 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV(m_text
) );
773 GdkModifierType accel_mods
;
774 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
777 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
778 m_parentMenu
->m_accel
,
783 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
784 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
787 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
789 m_parentMenu
->m_accel
,
796 // it's valid for this function to be called even if m_menuItem == NULL
797 void wxMenuItem::DoSetText( const wxString
& str
)
799 // '\t' is the deliminator indicating a hot key
801 const wxChar
*pc
= str
;
802 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
804 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
806 // "&" is doubled to indicate "&" instead of accelerator
810 else if (*pc
== wxT('&'))
814 else if ( *pc
== wxT('_') ) // escape underscores
833 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
838 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
843 return (wxAcceleratorEntry
*)NULL
;
846 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
848 label
<< wxT('\t') << GetHotKey();
850 return wxGetAccelFromString(label
);
853 #endif // wxUSE_ACCEL
855 void wxMenuItem::Check( bool check
)
857 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
859 if (check
== m_isChecked
)
862 wxMenuItemBase::Check( check
);
868 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
872 wxFAIL_MSG( _T("can't check this item") );
876 void wxMenuItem::Enable( bool enable
)
878 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
880 gtk_widget_set_sensitive( m_menuItem
, enable
);
881 wxMenuItemBase::Enable( enable
);
884 bool wxMenuItem::IsChecked() const
886 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
888 wxCHECK_MSG( IsCheckable(), FALSE
,
889 wxT("can't get state of uncheckable item!") );
891 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
894 //-----------------------------------------------------------------------------
896 //-----------------------------------------------------------------------------
898 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
902 m_accel
= gtk_accel_group_new();
903 m_menu
= gtk_menu_new();
904 // NB: keep reference to the menu so that it is not destroyed behind
905 // our back by GTK+ e.g. when it is removed from menubar:
906 gtk_widget_ref(m_menu
);
908 m_owner
= (GtkWidget
*) NULL
;
910 // Tearoffs are entries, just like separators. So if we want this
911 // menu to be a tear-off one, we just append a tearoff entry
913 if ( m_style
& wxMENU_TEAROFF
)
915 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
917 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
922 // append the title as the very first entry if we have it
923 if ( !m_title
.empty() )
925 Append(wxGTK_TITLE_ID
, m_title
);
932 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
934 if ( GTK_IS_WIDGET( m_menu
))
937 gtk_widget_unref( m_menu
);
938 // if the menu is inserted in another menu at this time, there was
939 // one more reference to it:
941 gtk_widget_destroy( m_menu
);
945 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
951 if ( mitem
->IsSeparator() )
953 menuItem
= gtk_separator_menu_item_new();
955 else if (mitem
->GetBitmap().Ok())
957 text
= mitem
->GetText();
958 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
960 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
963 if (bitmap
->HasPixbuf())
965 image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
969 GdkPixmap
*gdk_pixmap
= bitmap
->GetPixmap();
970 GdkBitmap
*gdk_bitmap
= bitmap
->GetMask() ?
971 bitmap
->GetMask()->GetBitmap() :
973 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
976 gtk_widget_show(image
);
978 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
982 else // a normal item
984 // text has "_" instead of "&" after mitem->SetText() so don't use it
985 text
= mitem
->GetText() ;
987 switch ( mitem
->GetKind() )
991 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
998 GSList
*group
= NULL
;
999 if ( m_prevRadio
== NULL
)
1001 // start of a new radio group
1002 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1004 else // continue the radio group
1006 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1007 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV( text
) );
1013 wxFAIL_MSG( _T("unexpected menu item kind") );
1018 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text
) );
1027 GdkModifierType accel_mods
;
1028 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1030 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1031 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1034 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1043 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1045 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1047 gtk_widget_show( menuItem
);
1049 if ( !mitem
->IsSeparator() )
1051 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1053 g_signal_connect (menuItem
, "select",
1054 G_CALLBACK (gtk_menu_hilight_callback
), this);
1055 g_signal_connect (menuItem
, "deselect",
1056 G_CALLBACK (gtk_menu_nolight_callback
), this);
1058 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1060 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1062 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1064 // if adding a submenu to a menu already existing in the menu bar, we
1065 // must set invoking window to allow processing events from this
1067 if ( m_invokingWindow
)
1068 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1072 g_signal_connect (menuItem
, "activate",
1073 G_CALLBACK (gtk_menu_clicked_callback
),
1078 mitem
->SetMenuItem(menuItem
);
1082 // This doesn't even exist!
1083 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1089 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1091 if (!GtkAppend(mitem
))
1094 return wxMenuBase::DoAppend(mitem
);
1097 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1099 if ( !wxMenuBase::DoInsert(pos
, item
) )
1103 if ( !GtkAppend(item
, (int)pos
) )
1109 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1111 if ( !wxMenuBase::DoRemove(item
) )
1112 return (wxMenuItem
*)NULL
;
1114 // TODO: this code doesn't delete the item factory item and this seems
1115 // impossible as of GTK 1.2.6.
1116 gtk_widget_destroy( item
->GetMenuItem() );
1121 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1123 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1126 wxMenuItem
*item
= node
->GetData();
1127 if (item
->GetMenuItem() == menuItem
)
1128 return item
->GetId();
1129 node
= node
->GetNext();
1135 // ----------------------------------------------------------------------------
1137 // ----------------------------------------------------------------------------
1141 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1145 wxAcceleratorEntry
*accel
= item
.GetAccel();
1148 int flags
= accel
->GetFlags();
1149 if ( flags
& wxACCEL_ALT
)
1150 hotkey
+= wxT("<alt>");
1151 if ( flags
& wxACCEL_CTRL
)
1152 hotkey
+= wxT("<control>");
1153 if ( flags
& wxACCEL_SHIFT
)
1154 hotkey
+= wxT("<shift>");
1156 int code
= accel
->GetKeyCode();
1183 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1186 // TODO: we should use gdk_keyval_name() (a.k.a.
1187 // XKeysymToString) here as well as hardcoding the keysym
1188 // names this might be not portable
1190 hotkey
<< wxT("Insert" );
1193 hotkey
<< wxT("Delete" );
1196 hotkey
<< wxT("Up" );
1199 hotkey
<< wxT("Down" );
1203 hotkey
<< wxT("Prior" );
1207 hotkey
<< wxT("Next" );
1210 hotkey
<< wxT("Left" );
1213 hotkey
<< wxT("Right" );
1216 hotkey
<< wxT("Home" );
1219 hotkey
<< wxT("End" );
1222 hotkey
<< wxT("Return" );
1225 hotkey
<< wxT("BackSpace" );
1228 hotkey
<< wxT("Tab" );
1231 hotkey
<< wxT("Esc" );
1234 hotkey
<< wxT("space" );
1237 hotkey
<< wxT("Multiply" );
1240 hotkey
<< wxT("Add" );
1243 hotkey
<< wxT("Separator" );
1246 hotkey
<< wxT("Subtract" );
1249 hotkey
<< wxT("Decimal" );
1252 hotkey
<< wxT("Divide" );
1255 hotkey
<< wxT("Cancel" );
1258 hotkey
<< wxT("Clear" );
1261 hotkey
<< wxT("Menu" );
1264 hotkey
<< wxT("Pause" );
1267 hotkey
<< wxT("Capital" );
1270 hotkey
<< wxT("Select" );
1273 hotkey
<< wxT("Print" );
1276 hotkey
<< wxT("Execute" );
1279 hotkey
<< wxT("Snapshot" );
1282 hotkey
<< wxT("Help" );
1285 hotkey
<< wxT("Num_Lock" );
1288 hotkey
<< wxT("Scroll_Lock" );
1290 case WXK_NUMPAD_INSERT
:
1291 hotkey
<< wxT("KP_Insert" );
1293 case WXK_NUMPAD_DELETE
:
1294 hotkey
<< wxT("KP_Delete" );
1296 case WXK_NUMPAD_SPACE
:
1297 hotkey
<< wxT("KP_Space" );
1299 case WXK_NUMPAD_TAB
:
1300 hotkey
<< wxT("KP_Tab" );
1302 case WXK_NUMPAD_ENTER
:
1303 hotkey
<< wxT("KP_Enter" );
1305 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1307 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1309 case WXK_NUMPAD_HOME
:
1310 hotkey
<< wxT("KP_Home" );
1312 case WXK_NUMPAD_LEFT
:
1313 hotkey
<< wxT("KP_Left" );
1316 hotkey
<< wxT("KP_Up" );
1318 case WXK_NUMPAD_RIGHT
:
1319 hotkey
<< wxT("KP_Right" );
1321 case WXK_NUMPAD_DOWN
:
1322 hotkey
<< wxT("KP_Down" );
1324 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1325 hotkey
<< wxT("KP_Prior" );
1327 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1328 hotkey
<< wxT("KP_Next" );
1330 case WXK_NUMPAD_END
:
1331 hotkey
<< wxT("KP_End" );
1333 case WXK_NUMPAD_BEGIN
:
1334 hotkey
<< wxT("KP_Begin" );
1336 case WXK_NUMPAD_EQUAL
:
1337 hotkey
<< wxT("KP_Equal" );
1339 case WXK_NUMPAD_MULTIPLY
:
1340 hotkey
<< wxT("KP_Multiply" );
1342 case WXK_NUMPAD_ADD
:
1343 hotkey
<< wxT("KP_Add" );
1345 case WXK_NUMPAD_SEPARATOR
:
1346 hotkey
<< wxT("KP_Separator" );
1348 case WXK_NUMPAD_SUBTRACT
:
1349 hotkey
<< wxT("KP_Subtract" );
1351 case WXK_NUMPAD_DECIMAL
:
1352 hotkey
<< wxT("KP_Decimal" );
1354 case WXK_NUMPAD_DIVIDE
:
1355 hotkey
<< wxT("KP_Divide" );
1357 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1358 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1359 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1360 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1362 case WXK_WINDOWS_LEFT
:
1363 hotkey
<< wxT("Super_L" );
1365 case WXK_WINDOWS_RIGHT
:
1366 hotkey
<< wxT("Super_R" );
1368 case WXK_WINDOWS_MENU
:
1369 hotkey
<< wxT("Menu" );
1372 hotkey
<< wxT("Command" );
1374 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1375 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1376 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1377 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1378 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1379 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1380 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1383 // if there are any other keys wxGetAccelFromString() may
1384 // return, we should process them here
1389 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1397 wxFAIL_MSG( wxT("unknown keyboard accel") );
1406 #endif // wxUSE_ACCEL
1408 // ----------------------------------------------------------------------------
1409 // Pop-up menu stuff
1410 // ----------------------------------------------------------------------------
1412 #if wxUSE_MENUS_NATIVE
1414 extern "C" WXDLLIMPEXP_CORE
1415 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1417 *is_waiting
= FALSE
;
1420 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1422 menu
->SetInvokingWindow( win
);
1424 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1427 wxMenuItem
*menuitem
= node
->GetData();
1428 if (menuitem
->IsSubMenu())
1430 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1433 node
= node
->GetNext();
1437 extern "C" WXDLLIMPEXP_CORE
1438 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1440 gboolean
* WXUNUSED(whatever
),
1441 gpointer user_data
)
1443 // ensure that the menu appears entirely on screen
1445 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1447 wxSize sizeScreen
= wxGetDisplaySize();
1448 wxPoint
*pos
= (wxPoint
*)user_data
;
1450 gint xmax
= sizeScreen
.x
- req
.width
,
1451 ymax
= sizeScreen
.y
- req
.height
;
1453 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1454 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1457 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1459 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1461 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1463 // NOTE: if you change this code, you need to update
1464 // the same code in taskbar.cpp as well. This
1465 // is ugly code duplication, I know.
1467 SetInvokingWindow( menu
, this );
1471 bool is_waiting
= true;
1473 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1474 G_CALLBACK (gtk_pop_hide_callback
),
1479 GtkMenuPositionFunc posfunc
;
1480 if ( x
== -1 && y
== -1 )
1482 // use GTK's default positioning algorithm
1488 pos
= ClientToScreen(wxPoint(x
, y
));
1490 posfunc
= wxPopupMenuPositionCallback
;
1493 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1494 DoCommonMenuCallbackCode(menu
, eventOpen
);
1497 GTK_MENU(menu
->m_menu
),
1498 (GtkWidget
*) NULL
, // parent menu shell
1499 (GtkWidget
*) NULL
, // parent menu item
1500 posfunc
, // function to position it
1501 userdata
, // client data
1502 0, // button used to activate it
1503 gtk_get_current_event_time()
1508 gtk_main_iteration();
1511 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1513 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1514 DoCommonMenuCallbackCode(menu
, eventClose
);
1519 #endif // wxUSE_MENUS_NATIVE