1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/bitmap.h"
26 #include "wx/stockitem.h"
27 #include "wx/gtk/private.h"
29 #include <gdk/gdkkeysyms.h>
31 // FIXME: is this right? somehow I don't think so (VZ)
33 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
34 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
35 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
37 #define ACCEL_OBJECT GtkWindow
38 #define ACCEL_OBJECTS(a) (a)->acceleratables
39 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
41 // we use normal item but with a special id for the menu title
42 static const int wxGTK_TITLE_ID
= -3;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
49 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 static wxString
wxReplaceUnderscore( const wxString
& title
)
60 // GTK 1.2 wants to have "_" instead of "&" for accelerators
63 while (*pc
!= wxT('\0'))
65 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
67 // "&" is doubled to indicate "&" instead of accelerator
71 else if (*pc
== wxT('&'))
77 if ( *pc
== wxT('_') )
79 // underscores must be doubled to prevent them from being
80 // interpreted as accelerator character prefix by GTK
89 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
94 //-----------------------------------------------------------------------------
95 // activate message from GTK
96 //-----------------------------------------------------------------------------
98 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
101 wxapp_install_idle_handler();
103 event
.SetEventObject( menu
);
105 wxEvtHandler
* handler
= menu
->GetEventHandler();
106 if (handler
&& handler
->ProcessEvent(event
))
109 wxWindow
*win
= menu
->GetInvokingWindow();
111 win
->GetEventHandler()->ProcessEvent( event
);
116 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
118 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
120 DoCommonMenuCallbackCode(menu
, event
);
123 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
125 if ( !menubar
->GetMenuCount() )
127 // if menubar is empty we can't call GetMenu(0) below
131 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
133 DoCommonMenuCallbackCode(menubar
->GetMenu(0), 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 if (style
& wxMB_DOCKABLE
)
162 m_widget
= gtk_handle_box_new();
163 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
164 gtk_widget_show( GTK_WIDGET(m_menubar
) );
168 m_widget
= GTK_WIDGET(m_menubar
);
175 for (size_t i
= 0; i
< n
; ++i
)
176 Append(menus
[i
], titles
[i
]);
178 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
179 // don't get it when the menu is dismissed by clicking outside the
180 // toolbar) so we connect to the global one, even if it means that we
181 // can't pass the menu which was closed in wxMenuEvent object
182 g_signal_connect (m_menubar
, "deactivate",
183 G_CALLBACK (gtk_menu_close_callback
), this);
187 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
189 Init(n
, menus
, titles
, style
);
192 wxMenuBar::wxMenuBar(long style
)
194 Init(0, NULL
, NULL
, style
);
197 wxMenuBar::wxMenuBar()
199 Init(0, NULL
, NULL
, 0);
202 wxMenuBar::~wxMenuBar()
206 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
208 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
210 wxWindow
*top_frame
= win
;
211 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
212 top_frame
= top_frame
->GetParent();
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();
254 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
257 wxMenu
*menu
= node
->GetData();
258 wxMenubarSetInvokingWindow( menu
, win
);
259 node
= node
->GetNext();
263 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
265 m_invokingWindow
= (wxWindow
*) NULL
;
266 wxWindow
*top_frame
= win
;
267 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
268 top_frame
= top_frame
->GetParent();
270 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
273 wxMenu
*menu
= node
->GetData();
274 wxMenubarUnsetInvokingWindow( menu
, win
);
275 node
= node
->GetNext();
279 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
281 if ( !wxMenuBarBase::Append( menu
, title
) )
284 return GtkAppend(menu
, title
);
287 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
289 wxString
str( wxReplaceUnderscore( title
) );
291 // This doesn't have much effect right now.
292 menu
->SetTitle( str
);
294 // The "m_owner" is the "menu item"
295 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
297 gtk_widget_show( menu
->m_owner
);
299 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
302 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
304 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
306 g_signal_connect (menu
->m_owner
, "activate",
307 G_CALLBACK (gtk_menu_open_callback
),
310 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
311 // addings menu later on.
312 if (m_invokingWindow
)
314 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
316 // OPTIMISE ME: we should probably cache this, or pass it
317 // directly, but for now this is a minimal
318 // change to validate the new dynamic sizing.
319 // see (and refactor :) similar code in Remove
322 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
325 frame
->UpdateMenuBarSize();
331 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
333 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
338 if ( !GtkAppend(menu
, title
, (int)pos
) )
344 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
346 // remove the old item and insert a new one
347 wxMenu
*menuOld
= Remove(pos
);
348 if ( menuOld
&& !Insert(pos
, menu
, title
) )
350 return (wxMenu
*) NULL
;
353 // either Insert() succeeded or Remove() failed and menuOld is NULL
357 wxMenu
*wxMenuBar::Remove(size_t pos
)
359 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
361 return (wxMenu
*) NULL
;
363 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
364 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
366 gtk_widget_destroy( menu
->m_owner
);
367 menu
->m_owner
= NULL
;
369 if (m_invokingWindow
)
371 // OPTIMISE ME: see comment in GtkAppend
372 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
375 frame
->UpdateMenuBarSize();
381 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
383 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
385 int res
= menu
->FindItem( itemString
);
386 if (res
!= wxNOT_FOUND
)
390 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
393 wxMenuItem
*item
= node
->GetData();
394 if (item
->IsSubMenu())
395 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
397 node
= node
->GetNext();
403 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
405 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
408 wxMenu
*menu
= node
->GetData();
409 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
412 node
= node
->GetNext();
418 // Find a wxMenuItem using its id. Recurses down into sub-menus
419 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
421 wxMenuItem
* result
= menu
->FindChildItem(id
);
423 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
424 while ( node
&& result
== NULL
)
426 wxMenuItem
*item
= node
->GetData();
427 if (item
->IsSubMenu())
429 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
431 node
= node
->GetNext();
437 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
439 wxMenuItem
* result
= 0;
440 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
441 while (node
&& result
== 0)
443 wxMenu
*menu
= node
->GetData();
444 result
= FindMenuItemByIdRecursive( menu
, id
);
445 node
= node
->GetNext();
450 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
456 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
458 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
460 wxCHECK_RET( node
, wxT("menu not found") );
462 wxMenu
* menu
= node
->GetData();
465 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
468 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
470 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
472 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
474 wxMenu
* menu
= node
->GetData();
477 wxString
text( menu
->GetTitle() );
478 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
480 if ( *pc
== wxT('_') )
482 // '_' is the escape character for GTK+
486 // don't remove ampersands '&' since if we have them in the menu title
487 // it means that they were doubled to indicate "&" instead of accelerator
495 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
497 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
499 wxCHECK_RET( node
, wxT("menu not found") );
501 wxMenu
* menu
= node
->GetData();
503 const wxString
str( wxReplaceUnderscore( label
) );
505 menu
->SetTitle( str
);
508 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
511 //-----------------------------------------------------------------------------
513 //-----------------------------------------------------------------------------
516 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
519 wxapp_install_idle_handler();
521 int id
= menu
->FindMenuIdByMenuItem(widget
);
523 /* should find it for normal (not popup) menu */
524 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
525 _T("menu item not found in gtk_menu_clicked_callback") );
527 if (!menu
->IsEnabled(id
))
530 wxMenuItem
* item
= menu
->FindChildItem( id
);
531 wxCHECK_RET( item
, wxT("error in menu item callback") );
533 if ( item
->GetId() == wxGTK_TITLE_ID
)
535 // ignore events from the menu title
539 if (item
->IsCheckable())
541 bool isReallyChecked
= item
->IsChecked(),
542 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
544 // ensure that the internal state is always consistent with what is
545 // shown on the screen
546 item
->wxMenuItemBase::Check(isReallyChecked
);
548 // we must not report the events for the radio button going up nor the
549 // events resulting from the calls to wxMenuItem::Check()
550 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
551 (isInternallyChecked
== isReallyChecked
) )
558 // Is this menu on a menubar? (possibly nested)
559 wxFrame
* frame
= NULL
;
560 if(menu
->IsAttached())
561 frame
= menu
->GetMenuBar()->GetFrame();
563 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
564 // normally wxMenu::SendEvent() should be enough, if it doesn't work
565 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
566 // should be fixed instead of working around it here...
569 // If it is attached then let the frame send the event.
570 // Don't call frame->ProcessCommand(id) because it toggles
571 // checkable items and we've already done that above.
572 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
573 commandEvent
.SetEventObject(frame
);
574 if (item
->IsCheckable())
575 commandEvent
.SetInt(item
->IsChecked());
576 commandEvent
.SetEventObject(menu
);
578 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
582 // otherwise let the menu have it
583 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
588 //-----------------------------------------------------------------------------
590 //-----------------------------------------------------------------------------
593 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
595 if (g_isIdle
) wxapp_install_idle_handler();
597 int id
= menu
->FindMenuIdByMenuItem(widget
);
599 wxASSERT( id
!= -1 ); // should find it!
601 if (!menu
->IsEnabled(id
))
604 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
605 event
.SetEventObject( menu
);
607 wxEvtHandler
* handler
= menu
->GetEventHandler();
608 if (handler
&& handler
->ProcessEvent(event
))
611 wxWindow
*win
= menu
->GetInvokingWindow();
612 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
616 //-----------------------------------------------------------------------------
618 //-----------------------------------------------------------------------------
621 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
623 if (g_isIdle
) wxapp_install_idle_handler();
625 int id
= menu
->FindMenuIdByMenuItem(widget
);
627 wxASSERT( id
!= -1 ); // should find it!
629 if (!menu
->IsEnabled(id
))
632 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
633 event
.SetEventObject( menu
);
635 wxEvtHandler
* handler
= menu
->GetEventHandler();
636 if (handler
&& handler
->ProcessEvent(event
))
639 wxWindow
*win
= menu
->GetInvokingWindow();
641 win
->GetEventHandler()->ProcessEvent( event
);
645 //-----------------------------------------------------------------------------
647 //-----------------------------------------------------------------------------
649 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
651 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
653 const wxString
& name
,
654 const wxString
& help
,
658 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
661 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
663 const wxString
& text
,
664 const wxString
& help
,
667 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
672 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
674 const wxString
& text
,
675 const wxString
& help
,
678 : wxMenuItemBase(parentMenu
, id
, text
, help
,
679 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
684 void wxMenuItem::Init(const wxString
& text
)
686 m_labelWidget
= (GtkWidget
*) NULL
;
687 m_menuItem
= (GtkWidget
*) NULL
;
692 wxMenuItem::~wxMenuItem()
694 // don't delete menu items, the menus take care of that
697 // return the menu item text without any menu accels
699 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
703 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
705 if ( *pc
== wxT('\t'))
708 if ( *pc
== wxT('_') )
710 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
716 if ( *pc
== wxT('\\') )
718 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
724 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
727 // "&" is doubled to indicate "&" instead of accelerator
734 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
739 void wxMenuItem::SetText( const wxString
& str
)
741 // Some optimization to avoid flicker
742 wxString oldLabel
= m_text
;
743 oldLabel
= wxStripMenuCodes(oldLabel
);
744 oldLabel
.Replace(wxT("_"), wxT(""));
745 wxString label1
= wxStripMenuCodes(str
);
746 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
747 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
751 if (oldLabel
== label1
&&
752 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
759 label
= (GtkLabel
*) m_labelWidget
;
761 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
763 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(m_text
) );
767 GdkModifierType accel_mods
;
768 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
771 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
772 m_parentMenu
->m_accel
,
777 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
778 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
781 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
783 m_parentMenu
->m_accel
,
790 // it's valid for this function to be called even if m_menuItem == NULL
791 void wxMenuItem::DoSetText( const wxString
& str
)
793 // '\t' is the deliminator indicating a hot key
795 const wxChar
*pc
= str
;
796 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
798 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
800 // "&" is doubled to indicate "&" instead of accelerator
804 else if (*pc
== wxT('&'))
808 else if ( *pc
== wxT('_') ) // escape underscores
827 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
832 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
837 return (wxAcceleratorEntry
*)NULL
;
840 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
842 label
<< wxT('\t') << GetHotKey();
844 return wxGetAccelFromString(label
);
847 #endif // wxUSE_ACCEL
849 void wxMenuItem::Check( bool check
)
851 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
853 if (check
== m_isChecked
)
856 wxMenuItemBase::Check( check
);
862 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
866 wxFAIL_MSG( _T("can't check this item") );
870 void wxMenuItem::Enable( bool enable
)
872 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
874 gtk_widget_set_sensitive( m_menuItem
, enable
);
875 wxMenuItemBase::Enable( enable
);
878 bool wxMenuItem::IsChecked() const
880 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
882 wxCHECK_MSG( IsCheckable(), false,
883 wxT("can't get state of uncheckable item!") );
885 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
888 //-----------------------------------------------------------------------------
890 //-----------------------------------------------------------------------------
892 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
896 m_accel
= gtk_accel_group_new();
897 m_menu
= gtk_menu_new();
898 // NB: keep reference to the menu so that it is not destroyed behind
899 // our back by GTK+ e.g. when it is removed from menubar:
900 gtk_widget_ref(m_menu
);
902 m_owner
= (GtkWidget
*) NULL
;
904 // Tearoffs are entries, just like separators. So if we want this
905 // menu to be a tear-off one, we just append a tearoff entry
907 if ( m_style
& wxMENU_TEAROFF
)
909 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
911 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
916 // append the title as the very first entry if we have it
917 if ( !m_title
.empty() )
919 Append(wxGTK_TITLE_ID
, m_title
);
926 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
928 if ( GTK_IS_WIDGET( m_menu
))
931 gtk_widget_unref( m_menu
);
932 // if the menu is inserted in another menu at this time, there was
933 // one more reference to it:
935 gtk_widget_destroy( m_menu
);
939 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
945 if ( mitem
->IsSeparator() )
947 menuItem
= gtk_separator_menu_item_new();
949 else if ( mitem
->GetBitmap().Ok() ||
950 (mitem
->GetKind() == wxITEM_NORMAL
&&
951 wxIsStockID(mitem
->GetId())) )
953 text
= mitem
->GetText();
954 wxBitmap
bitmap(mitem
->GetBitmap());
956 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
961 // use stock bitmap for this item if available on the assumption
962 // that it never hurts to follow GTK+ conventions more closely
963 const char *stock
= wxGetStockGtkID(mitem
->GetId());
964 image
= stock
? gtk_image_new_from_stock(stock
, GTK_ICON_SIZE_MENU
)
967 else // we have a custom bitmap
969 wxASSERT_MSG( mitem
->GetKind() == wxITEM_NORMAL
,
970 _T("only normal menu items can have bitmaps") );
972 if ( bitmap
.HasPixbuf() )
974 image
= gtk_image_new_from_pixbuf(bitmap
.GetPixbuf());
978 GdkPixmap
*gdk_pixmap
= bitmap
.GetPixmap();
979 GdkBitmap
*gdk_bitmap
= bitmap
.GetMask() ?
980 bitmap
.GetMask()->GetBitmap() :
982 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
);
995 else // a normal item
997 // text has "_" instead of "&" after mitem->SetText() so don't use it
998 text
= mitem
->GetText() ;
1000 switch ( mitem
->GetKind() )
1004 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1011 GSList
*group
= NULL
;
1012 if ( m_prevRadio
== NULL
)
1014 // start of a new radio group
1015 m_prevRadio
= menuItem
=
1016 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1018 else // continue the radio group
1020 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1021 m_prevRadio
= menuItem
=
1022 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1028 wxFAIL_MSG( _T("unexpected menu item kind") );
1033 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1042 GdkModifierType accel_mods
;
1043 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1045 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1046 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1049 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1058 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1060 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1062 gtk_widget_show( menuItem
);
1064 if ( !mitem
->IsSeparator() )
1066 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1068 g_signal_connect (menuItem
, "select",
1069 G_CALLBACK (gtk_menu_hilight_callback
), this);
1070 g_signal_connect (menuItem
, "deselect",
1071 G_CALLBACK (gtk_menu_nolight_callback
), this);
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 g_signal_connect (menuItem
, "activate",
1088 G_CALLBACK (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" );
1217 hotkey
<< wxT("PgUp" );
1220 hotkey
<< wxT("PgDn" );
1223 hotkey
<< wxT("Left" );
1226 hotkey
<< wxT("Right" );
1229 hotkey
<< wxT("Home" );
1232 hotkey
<< wxT("End" );
1235 hotkey
<< wxT("Return" );
1238 hotkey
<< wxT("BackSpace" );
1241 hotkey
<< wxT("Tab" );
1244 hotkey
<< wxT("Esc" );
1247 hotkey
<< wxT("space" );
1250 hotkey
<< wxT("Multiply" );
1253 hotkey
<< wxT("Add" );
1256 hotkey
<< wxT("Separator" );
1259 hotkey
<< wxT("Subtract" );
1262 hotkey
<< wxT("Decimal" );
1265 hotkey
<< wxT("Divide" );
1268 hotkey
<< wxT("Cancel" );
1271 hotkey
<< wxT("Clear" );
1274 hotkey
<< wxT("Menu" );
1277 hotkey
<< wxT("Pause" );
1280 hotkey
<< wxT("Capital" );
1283 hotkey
<< wxT("Select" );
1286 hotkey
<< wxT("Print" );
1289 hotkey
<< wxT("Execute" );
1292 hotkey
<< wxT("Snapshot" );
1295 hotkey
<< wxT("Help" );
1298 hotkey
<< wxT("Num_Lock" );
1301 hotkey
<< wxT("Scroll_Lock" );
1303 case WXK_NUMPAD_INSERT
:
1304 hotkey
<< wxT("KP_Insert" );
1306 case WXK_NUMPAD_DELETE
:
1307 hotkey
<< wxT("KP_Delete" );
1309 case WXK_NUMPAD_SPACE
:
1310 hotkey
<< wxT("KP_Space" );
1312 case WXK_NUMPAD_TAB
:
1313 hotkey
<< wxT("KP_Tab" );
1315 case WXK_NUMPAD_ENTER
:
1316 hotkey
<< wxT("KP_Enter" );
1318 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1320 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1322 case WXK_NUMPAD_HOME
:
1323 hotkey
<< wxT("KP_Home" );
1325 case WXK_NUMPAD_LEFT
:
1326 hotkey
<< wxT("KP_Left" );
1329 hotkey
<< wxT("KP_Up" );
1331 case WXK_NUMPAD_RIGHT
:
1332 hotkey
<< wxT("KP_Right" );
1334 case WXK_NUMPAD_DOWN
:
1335 hotkey
<< wxT("KP_Down" );
1337 case WXK_NUMPAD_PAGEUP
:
1338 hotkey
<< wxT("KP_PgUp" );
1340 case WXK_NUMPAD_PAGEDOWN
:
1341 hotkey
<< wxT("KP_PgDn" );
1343 case WXK_NUMPAD_END
:
1344 hotkey
<< wxT("KP_End" );
1346 case WXK_NUMPAD_BEGIN
:
1347 hotkey
<< wxT("KP_Begin" );
1349 case WXK_NUMPAD_EQUAL
:
1350 hotkey
<< wxT("KP_Equal" );
1352 case WXK_NUMPAD_MULTIPLY
:
1353 hotkey
<< wxT("KP_Multiply" );
1355 case WXK_NUMPAD_ADD
:
1356 hotkey
<< wxT("KP_Add" );
1358 case WXK_NUMPAD_SEPARATOR
:
1359 hotkey
<< wxT("KP_Separator" );
1361 case WXK_NUMPAD_SUBTRACT
:
1362 hotkey
<< wxT("KP_Subtract" );
1364 case WXK_NUMPAD_DECIMAL
:
1365 hotkey
<< wxT("KP_Decimal" );
1367 case WXK_NUMPAD_DIVIDE
:
1368 hotkey
<< wxT("KP_Divide" );
1370 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1371 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1372 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1373 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1375 case WXK_WINDOWS_LEFT
:
1376 hotkey
<< wxT("Super_L" );
1378 case WXK_WINDOWS_RIGHT
:
1379 hotkey
<< wxT("Super_R" );
1381 case WXK_WINDOWS_MENU
:
1382 hotkey
<< wxT("Menu" );
1385 hotkey
<< wxT("Command" );
1387 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1388 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1389 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1390 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1391 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1392 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1393 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1396 // if there are any other keys wxGetAccelFromString() may
1397 // return, we should process them here
1402 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1410 wxFAIL_MSG( wxT("unknown keyboard accel") );
1419 #endif // wxUSE_ACCEL
1421 // ----------------------------------------------------------------------------
1422 // Pop-up menu stuff
1423 // ----------------------------------------------------------------------------
1425 #if wxUSE_MENUS_NATIVE
1427 extern "C" WXDLLIMPEXP_CORE
1428 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1430 *is_waiting
= false;
1433 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1435 menu
->SetInvokingWindow( win
);
1437 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1440 wxMenuItem
*menuitem
= node
->GetData();
1441 if (menuitem
->IsSubMenu())
1443 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1446 node
= node
->GetNext();
1450 extern "C" WXDLLIMPEXP_CORE
1451 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1453 gboolean
* WXUNUSED(whatever
),
1454 gpointer user_data
)
1456 // ensure that the menu appears entirely on screen
1458 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1460 wxSize sizeScreen
= wxGetDisplaySize();
1461 wxPoint
*pos
= (wxPoint
*)user_data
;
1463 gint xmax
= sizeScreen
.x
- req
.width
,
1464 ymax
= sizeScreen
.y
- req
.height
;
1466 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1467 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1470 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1472 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1474 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1476 // NOTE: if you change this code, you need to update
1477 // the same code in taskbar.cpp as well. This
1478 // is ugly code duplication, I know.
1480 SetInvokingWindow( menu
, this );
1484 bool is_waiting
= true;
1486 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1487 G_CALLBACK (gtk_pop_hide_callback
),
1492 GtkMenuPositionFunc posfunc
;
1493 if ( x
== -1 && y
== -1 )
1495 // use GTK's default positioning algorithm
1501 pos
= ClientToScreen(wxPoint(x
, y
));
1503 posfunc
= wxPopupMenuPositionCallback
;
1506 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1507 DoCommonMenuCallbackCode(menu
, eventOpen
);
1510 GTK_MENU(menu
->m_menu
),
1511 (GtkWidget
*) NULL
, // parent menu shell
1512 (GtkWidget
*) NULL
, // parent menu item
1513 posfunc
, // function to position it
1514 userdata
, // client data
1515 0, // button used to activate it
1516 gtk_get_current_event_time()
1521 gtk_main_iteration();
1524 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1526 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1527 DoCommonMenuCallbackCode(menu
, eventClose
);
1532 #endif // wxUSE_MENUS_NATIVE