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"
23 #include "wx/stockitem.h"
24 #include "wx/gtk/private.h"
26 // FIXME: is this right? somehow I don't think so (VZ)
28 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
29 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
30 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
32 #define ACCEL_OBJECT GtkWindow
33 #define ACCEL_OBJECTS(a) (a)->acceleratables
34 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
36 // we use normal item but with a special id for the menu title
37 static const int wxGTK_TITLE_ID
= -3;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 static wxString
wxReplaceUnderscore( const wxString
& title
)
55 // GTK 1.2 wants to have "_" instead of "&" for accelerators
58 while (*pc
!= wxT('\0'))
60 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
62 // "&" is doubled to indicate "&" instead of accelerator
66 else if (*pc
== wxT('&'))
72 if ( *pc
== wxT('_') )
74 // underscores must be doubled to prevent them from being
75 // interpreted as accelerator character prefix by GTK
84 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
89 //-----------------------------------------------------------------------------
90 // activate message from GTK
91 //-----------------------------------------------------------------------------
93 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
96 wxapp_install_idle_handler();
98 event
.SetEventObject( menu
);
100 wxEvtHandler
* handler
= menu
->GetEventHandler();
101 if (handler
&& handler
->ProcessEvent(event
))
104 wxWindow
*win
= menu
->GetInvokingWindow();
106 win
->GetEventHandler()->ProcessEvent( event
);
111 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
113 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
115 DoCommonMenuCallbackCode(menu
, event
);
118 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
120 if ( !menubar
->GetMenuCount() )
122 // if menubar is empty we can't call GetMenu(0) below
126 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
128 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
139 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
141 // the parent window is known after wxFrame::SetMenu()
142 m_needParent
= false;
144 m_invokingWindow
= (wxWindow
*) NULL
;
146 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
147 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
149 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
153 m_menubar
= gtk_menu_bar_new();
155 if (style
& wxMB_DOCKABLE
)
157 m_widget
= gtk_handle_box_new();
158 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
159 gtk_widget_show( GTK_WIDGET(m_menubar
) );
163 m_widget
= GTK_WIDGET(m_menubar
);
170 for (size_t i
= 0; i
< n
; ++i
)
171 Append(menus
[i
], titles
[i
]);
173 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
174 // don't get it when the menu is dismissed by clicking outside the
175 // toolbar) so we connect to the global one, even if it means that we
176 // can't pass the menu which was closed in wxMenuEvent object
177 g_signal_connect (m_menubar
, "deactivate",
178 G_CALLBACK (gtk_menu_close_callback
), this);
182 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
184 Init(n
, menus
, titles
, style
);
187 wxMenuBar::wxMenuBar(long style
)
189 Init(0, NULL
, NULL
, style
);
192 wxMenuBar::wxMenuBar()
194 Init(0, NULL
, NULL
, 0);
197 wxMenuBar::~wxMenuBar()
201 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
203 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
205 wxWindow
*top_frame
= win
;
206 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
207 top_frame
= top_frame
->GetParent();
209 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
212 wxMenuItem
*menuitem
= node
->GetData();
213 if (menuitem
->IsSubMenu())
214 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
215 node
= node
->GetNext();
219 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
221 menu
->SetInvokingWindow( win
);
223 wxWindow
*top_frame
= win
;
224 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
225 top_frame
= top_frame
->GetParent();
227 // support for native hot keys
228 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
229 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
230 gtk_accel_group_attach( menu
->m_accel
, obj
);
232 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
235 wxMenuItem
*menuitem
= node
->GetData();
236 if (menuitem
->IsSubMenu())
237 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
238 node
= node
->GetNext();
242 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
244 m_invokingWindow
= win
;
245 wxWindow
*top_frame
= win
;
246 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
247 top_frame
= top_frame
->GetParent();
249 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
252 wxMenu
*menu
= node
->GetData();
253 wxMenubarSetInvokingWindow( menu
, win
);
254 node
= node
->GetNext();
258 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
260 m_invokingWindow
= (wxWindow
*) NULL
;
261 wxWindow
*top_frame
= win
;
262 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
263 top_frame
= top_frame
->GetParent();
265 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
268 wxMenu
*menu
= node
->GetData();
269 wxMenubarUnsetInvokingWindow( menu
, win
);
270 node
= node
->GetNext();
274 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
276 if ( !wxMenuBarBase::Append( menu
, title
) )
279 return GtkAppend(menu
, title
);
282 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
284 wxString
str( wxReplaceUnderscore( title
) );
286 // This doesn't have much effect right now.
287 menu
->SetTitle( str
);
289 // The "m_owner" is the "menu item"
290 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
292 gtk_widget_show( menu
->m_owner
);
294 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
297 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
299 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
301 g_signal_connect (menu
->m_owner
, "activate",
302 G_CALLBACK (gtk_menu_open_callback
),
305 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
306 // addings menu later on.
307 if (m_invokingWindow
)
309 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
311 // OPTIMISE ME: we should probably cache this, or pass it
312 // directly, but for now this is a minimal
313 // change to validate the new dynamic sizing.
314 // see (and refactor :) similar code in Remove
317 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
320 frame
->UpdateMenuBarSize();
326 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
328 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
333 if ( !GtkAppend(menu
, title
, (int)pos
) )
339 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
341 // remove the old item and insert a new one
342 wxMenu
*menuOld
= Remove(pos
);
343 if ( menuOld
&& !Insert(pos
, menu
, title
) )
345 return (wxMenu
*) NULL
;
348 // either Insert() succeeded or Remove() failed and menuOld is NULL
352 wxMenu
*wxMenuBar::Remove(size_t pos
)
354 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
356 return (wxMenu
*) NULL
;
358 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
359 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
361 gtk_widget_destroy( menu
->m_owner
);
362 menu
->m_owner
= NULL
;
364 if (m_invokingWindow
)
366 // OPTIMISE ME: see comment in GtkAppend
367 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
370 frame
->UpdateMenuBarSize();
376 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
378 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
380 int res
= menu
->FindItem( itemString
);
381 if (res
!= wxNOT_FOUND
)
385 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
388 wxMenuItem
*item
= node
->GetData();
389 if (item
->IsSubMenu())
390 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
392 node
= node
->GetNext();
398 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
400 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
403 wxMenu
*menu
= node
->GetData();
404 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
407 node
= node
->GetNext();
413 // Find a wxMenuItem using its id. Recurses down into sub-menus
414 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
416 wxMenuItem
* result
= menu
->FindChildItem(id
);
418 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
419 while ( node
&& result
== NULL
)
421 wxMenuItem
*item
= node
->GetData();
422 if (item
->IsSubMenu())
424 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
426 node
= node
->GetNext();
432 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
434 wxMenuItem
* result
= 0;
435 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
436 while (node
&& result
== 0)
438 wxMenu
*menu
= node
->GetData();
439 result
= FindMenuItemByIdRecursive( menu
, id
);
440 node
= node
->GetNext();
445 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
451 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
453 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
455 wxCHECK_RET( node
, wxT("menu not found") );
457 wxMenu
* menu
= node
->GetData();
460 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
463 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
465 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
467 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
469 wxMenu
* menu
= node
->GetData();
472 wxString
text( menu
->GetTitle() );
473 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
475 if ( *pc
== wxT('_') )
477 // '_' is the escape character for GTK+
481 // don't remove ampersands '&' since if we have them in the menu title
482 // it means that they were doubled to indicate "&" instead of accelerator
490 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
492 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
494 wxCHECK_RET( node
, wxT("menu not found") );
496 wxMenu
* menu
= node
->GetData();
498 const wxString
str( wxReplaceUnderscore( label
) );
500 menu
->SetTitle( str
);
503 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
506 //-----------------------------------------------------------------------------
508 //-----------------------------------------------------------------------------
511 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
514 wxapp_install_idle_handler();
516 int id
= menu
->FindMenuIdByMenuItem(widget
);
518 /* should find it for normal (not popup) menu */
519 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
520 _T("menu item not found in gtk_menu_clicked_callback") );
522 if (!menu
->IsEnabled(id
))
525 wxMenuItem
* item
= menu
->FindChildItem( id
);
526 wxCHECK_RET( item
, wxT("error in menu item callback") );
528 if ( item
->GetId() == wxGTK_TITLE_ID
)
530 // ignore events from the menu title
534 if (item
->IsCheckable())
536 bool isReallyChecked
= item
->IsChecked(),
537 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
539 // ensure that the internal state is always consistent with what is
540 // shown on the screen
541 item
->wxMenuItemBase::Check(isReallyChecked
);
543 // we must not report the events for the radio button going up nor the
544 // events resulting from the calls to wxMenuItem::Check()
545 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
546 (isInternallyChecked
== isReallyChecked
) )
553 // Is this menu on a menubar? (possibly nested)
554 wxFrame
* frame
= NULL
;
555 if(menu
->IsAttached())
556 frame
= menu
->GetMenuBar()->GetFrame();
558 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
559 // normally wxMenu::SendEvent() should be enough, if it doesn't work
560 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
561 // should be fixed instead of working around it here...
564 // If it is attached then let the frame send the event.
565 // Don't call frame->ProcessCommand(id) because it toggles
566 // checkable items and we've already done that above.
567 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
568 commandEvent
.SetEventObject(frame
);
569 if (item
->IsCheckable())
570 commandEvent
.SetInt(item
->IsChecked());
571 commandEvent
.SetEventObject(menu
);
573 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
577 // otherwise let the menu have it
578 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
583 //-----------------------------------------------------------------------------
585 //-----------------------------------------------------------------------------
588 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
590 if (g_isIdle
) wxapp_install_idle_handler();
592 int id
= menu
->FindMenuIdByMenuItem(widget
);
594 wxASSERT( id
!= -1 ); // should find it!
596 if (!menu
->IsEnabled(id
))
599 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
600 event
.SetEventObject( menu
);
602 wxEvtHandler
* handler
= menu
->GetEventHandler();
603 if (handler
&& handler
->ProcessEvent(event
))
606 wxWindow
*win
= menu
->GetInvokingWindow();
607 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
611 //-----------------------------------------------------------------------------
613 //-----------------------------------------------------------------------------
616 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
618 if (g_isIdle
) wxapp_install_idle_handler();
620 int id
= menu
->FindMenuIdByMenuItem(widget
);
622 wxASSERT( id
!= -1 ); // should find it!
624 if (!menu
->IsEnabled(id
))
627 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
628 event
.SetEventObject( menu
);
630 wxEvtHandler
* handler
= menu
->GetEventHandler();
631 if (handler
&& handler
->ProcessEvent(event
))
634 wxWindow
*win
= menu
->GetInvokingWindow();
636 win
->GetEventHandler()->ProcessEvent( event
);
640 //-----------------------------------------------------------------------------
642 //-----------------------------------------------------------------------------
644 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
646 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
648 const wxString
& name
,
649 const wxString
& help
,
653 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
656 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
658 const wxString
& text
,
659 const wxString
& help
,
662 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
667 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
669 const wxString
& text
,
670 const wxString
& help
,
673 : wxMenuItemBase(parentMenu
, id
, text
, help
,
674 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
679 void wxMenuItem::Init(const wxString
& text
)
681 m_labelWidget
= (GtkWidget
*) NULL
;
682 m_menuItem
= (GtkWidget
*) NULL
;
687 wxMenuItem::~wxMenuItem()
689 // don't delete menu items, the menus take care of that
692 // return the menu item text without any menu accels
694 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
698 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
700 if ( *pc
== wxT('\t'))
703 if ( *pc
== wxT('_') )
705 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
711 if ( *pc
== wxT('\\') )
713 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
719 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
722 // "&" is doubled to indicate "&" instead of accelerator
729 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
734 void wxMenuItem::SetText( const wxString
& str
)
736 // Some optimization to avoid flicker
737 wxString oldLabel
= m_text
;
738 oldLabel
= wxStripMenuCodes(oldLabel
);
739 oldLabel
.Replace(wxT("_"), wxT(""));
740 wxString label1
= wxStripMenuCodes(str
);
741 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
742 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
746 if (oldLabel
== label1
&&
747 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
754 label
= (GtkLabel
*) m_labelWidget
;
756 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
758 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(m_text
) );
762 GdkModifierType accel_mods
;
763 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
766 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
767 m_parentMenu
->m_accel
,
772 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
773 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
776 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
778 m_parentMenu
->m_accel
,
785 // it's valid for this function to be called even if m_menuItem == NULL
786 void wxMenuItem::DoSetText( const wxString
& str
)
788 // '\t' is the deliminator indicating a hot key
790 const wxChar
*pc
= str
;
791 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
793 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
795 // "&" is doubled to indicate "&" instead of accelerator
799 else if (*pc
== wxT('&'))
803 else if ( *pc
== wxT('_') ) // escape underscores
822 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
827 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
832 return (wxAcceleratorEntry
*)NULL
;
835 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
837 label
<< wxT('\t') << GetHotKey();
839 return wxGetAccelFromString(label
);
842 #endif // wxUSE_ACCEL
844 void wxMenuItem::Check( bool check
)
846 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
848 if (check
== m_isChecked
)
851 wxMenuItemBase::Check( check
);
857 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
861 wxFAIL_MSG( _T("can't check this item") );
865 void wxMenuItem::Enable( bool enable
)
867 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
869 gtk_widget_set_sensitive( m_menuItem
, enable
);
870 wxMenuItemBase::Enable( enable
);
873 bool wxMenuItem::IsChecked() const
875 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
877 wxCHECK_MSG( IsCheckable(), false,
878 wxT("can't get state of uncheckable item!") );
880 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
883 //-----------------------------------------------------------------------------
885 //-----------------------------------------------------------------------------
887 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
891 m_accel
= gtk_accel_group_new();
892 m_menu
= gtk_menu_new();
893 // NB: keep reference to the menu so that it is not destroyed behind
894 // our back by GTK+ e.g. when it is removed from menubar:
895 gtk_widget_ref(m_menu
);
897 m_owner
= (GtkWidget
*) NULL
;
899 // Tearoffs are entries, just like separators. So if we want this
900 // menu to be a tear-off one, we just append a tearoff entry
902 if ( m_style
& wxMENU_TEAROFF
)
904 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
906 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
911 // append the title as the very first entry if we have it
912 if ( !m_title
.empty() )
914 Append(wxGTK_TITLE_ID
, m_title
);
921 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
923 if ( GTK_IS_WIDGET( m_menu
))
926 gtk_widget_unref( m_menu
);
927 // if the menu is inserted in another menu at this time, there was
928 // one more reference to it:
930 gtk_widget_destroy( m_menu
);
934 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
940 if ( mitem
->IsSeparator() )
942 menuItem
= gtk_separator_menu_item_new();
944 else if ( mitem
->GetBitmap().Ok() ||
945 (mitem
->GetKind() == wxITEM_NORMAL
&&
946 wxIsStockID(mitem
->GetId())) )
948 text
= mitem
->GetText();
949 wxBitmap
bitmap(mitem
->GetBitmap());
951 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
956 // use stock bitmap for this item if available on the assumption
957 // that it never hurts to follow GTK+ conventions more closely
958 const char *stock
= wxGetStockGtkID(mitem
->GetId());
959 image
= stock
? gtk_image_new_from_stock(stock
, GTK_ICON_SIZE_MENU
)
962 else // we have a custom bitmap
964 wxASSERT_MSG( mitem
->GetKind() == wxITEM_NORMAL
,
965 _T("only normal menu items can have bitmaps") );
967 if ( bitmap
.HasPixbuf() )
969 image
= gtk_image_new_from_pixbuf(bitmap
.GetPixbuf());
973 GdkPixmap
*gdk_pixmap
= bitmap
.GetPixmap();
974 GdkBitmap
*gdk_bitmap
= bitmap
.GetMask() ?
975 bitmap
.GetMask()->GetBitmap() :
977 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
983 gtk_widget_show(image
);
985 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
990 else // a normal item
992 // text has "_" instead of "&" after mitem->SetText() so don't use it
993 text
= mitem
->GetText() ;
995 switch ( mitem
->GetKind() )
999 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1006 GSList
*group
= NULL
;
1007 if ( m_prevRadio
== NULL
)
1009 // start of a new radio group
1010 m_prevRadio
= menuItem
=
1011 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1013 else // continue the radio group
1015 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1016 m_prevRadio
= menuItem
=
1017 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1023 wxFAIL_MSG( _T("unexpected menu item kind") );
1028 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1037 GdkModifierType accel_mods
;
1038 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1040 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1041 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1044 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1053 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1055 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1057 gtk_widget_show( menuItem
);
1059 if ( !mitem
->IsSeparator() )
1061 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1063 g_signal_connect (menuItem
, "select",
1064 G_CALLBACK (gtk_menu_hilight_callback
), this);
1065 g_signal_connect (menuItem
, "deselect",
1066 G_CALLBACK (gtk_menu_nolight_callback
), this);
1068 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1070 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1072 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1074 // if adding a submenu to a menu already existing in the menu bar, we
1075 // must set invoking window to allow processing events from this
1077 if ( m_invokingWindow
)
1078 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1082 g_signal_connect (menuItem
, "activate",
1083 G_CALLBACK (gtk_menu_clicked_callback
),
1088 mitem
->SetMenuItem(menuItem
);
1092 // This doesn't even exist!
1093 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1099 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1101 if (!GtkAppend(mitem
))
1104 return wxMenuBase::DoAppend(mitem
);
1107 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1109 if ( !wxMenuBase::DoInsert(pos
, item
) )
1113 if ( !GtkAppend(item
, (int)pos
) )
1119 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1121 if ( !wxMenuBase::DoRemove(item
) )
1122 return (wxMenuItem
*)NULL
;
1124 // TODO: this code doesn't delete the item factory item and this seems
1125 // impossible as of GTK 1.2.6.
1126 gtk_widget_destroy( item
->GetMenuItem() );
1131 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1133 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1136 wxMenuItem
*item
= node
->GetData();
1137 if (item
->GetMenuItem() == menuItem
)
1138 return item
->GetId();
1139 node
= node
->GetNext();
1145 // ----------------------------------------------------------------------------
1147 // ----------------------------------------------------------------------------
1151 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1155 wxAcceleratorEntry
*accel
= item
.GetAccel();
1158 int flags
= accel
->GetFlags();
1159 if ( flags
& wxACCEL_ALT
)
1160 hotkey
+= wxT("<alt>");
1161 if ( flags
& wxACCEL_CTRL
)
1162 hotkey
+= wxT("<control>");
1163 if ( flags
& wxACCEL_SHIFT
)
1164 hotkey
+= wxT("<shift>");
1166 int code
= accel
->GetKeyCode();
1193 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1196 // TODO: we should use gdk_keyval_name() (a.k.a.
1197 // XKeysymToString) here as well as hardcoding the keysym
1198 // names this might be not portable
1200 hotkey
<< wxT("Insert" );
1203 hotkey
<< wxT("Delete" );
1206 hotkey
<< wxT("Up" );
1209 hotkey
<< wxT("Down" );
1212 hotkey
<< wxT("PgUp" );
1215 hotkey
<< wxT("PgDn" );
1218 hotkey
<< wxT("Left" );
1221 hotkey
<< wxT("Right" );
1224 hotkey
<< wxT("Home" );
1227 hotkey
<< wxT("End" );
1230 hotkey
<< wxT("Return" );
1233 hotkey
<< wxT("BackSpace" );
1236 hotkey
<< wxT("Tab" );
1239 hotkey
<< wxT("Esc" );
1242 hotkey
<< wxT("space" );
1245 hotkey
<< wxT("Multiply" );
1248 hotkey
<< wxT("Add" );
1251 hotkey
<< wxT("Separator" );
1254 hotkey
<< wxT("Subtract" );
1257 hotkey
<< wxT("Decimal" );
1260 hotkey
<< wxT("Divide" );
1263 hotkey
<< wxT("Cancel" );
1266 hotkey
<< wxT("Clear" );
1269 hotkey
<< wxT("Menu" );
1272 hotkey
<< wxT("Pause" );
1275 hotkey
<< wxT("Capital" );
1278 hotkey
<< wxT("Select" );
1281 hotkey
<< wxT("Print" );
1284 hotkey
<< wxT("Execute" );
1287 hotkey
<< wxT("Snapshot" );
1290 hotkey
<< wxT("Help" );
1293 hotkey
<< wxT("Num_Lock" );
1296 hotkey
<< wxT("Scroll_Lock" );
1298 case WXK_NUMPAD_INSERT
:
1299 hotkey
<< wxT("KP_Insert" );
1301 case WXK_NUMPAD_DELETE
:
1302 hotkey
<< wxT("KP_Delete" );
1304 case WXK_NUMPAD_SPACE
:
1305 hotkey
<< wxT("KP_Space" );
1307 case WXK_NUMPAD_TAB
:
1308 hotkey
<< wxT("KP_Tab" );
1310 case WXK_NUMPAD_ENTER
:
1311 hotkey
<< wxT("KP_Enter" );
1313 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1315 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1317 case WXK_NUMPAD_HOME
:
1318 hotkey
<< wxT("KP_Home" );
1320 case WXK_NUMPAD_LEFT
:
1321 hotkey
<< wxT("KP_Left" );
1324 hotkey
<< wxT("KP_Up" );
1326 case WXK_NUMPAD_RIGHT
:
1327 hotkey
<< wxT("KP_Right" );
1329 case WXK_NUMPAD_DOWN
:
1330 hotkey
<< wxT("KP_Down" );
1332 case WXK_NUMPAD_PAGEUP
:
1333 hotkey
<< wxT("KP_PgUp" );
1335 case WXK_NUMPAD_PAGEDOWN
:
1336 hotkey
<< wxT("KP_PgDn" );
1338 case WXK_NUMPAD_END
:
1339 hotkey
<< wxT("KP_End" );
1341 case WXK_NUMPAD_BEGIN
:
1342 hotkey
<< wxT("KP_Begin" );
1344 case WXK_NUMPAD_EQUAL
:
1345 hotkey
<< wxT("KP_Equal" );
1347 case WXK_NUMPAD_MULTIPLY
:
1348 hotkey
<< wxT("KP_Multiply" );
1350 case WXK_NUMPAD_ADD
:
1351 hotkey
<< wxT("KP_Add" );
1353 case WXK_NUMPAD_SEPARATOR
:
1354 hotkey
<< wxT("KP_Separator" );
1356 case WXK_NUMPAD_SUBTRACT
:
1357 hotkey
<< wxT("KP_Subtract" );
1359 case WXK_NUMPAD_DECIMAL
:
1360 hotkey
<< wxT("KP_Decimal" );
1362 case WXK_NUMPAD_DIVIDE
:
1363 hotkey
<< wxT("KP_Divide" );
1365 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1366 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1367 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1368 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1370 case WXK_WINDOWS_LEFT
:
1371 hotkey
<< wxT("Super_L" );
1373 case WXK_WINDOWS_RIGHT
:
1374 hotkey
<< wxT("Super_R" );
1376 case WXK_WINDOWS_MENU
:
1377 hotkey
<< wxT("Menu" );
1380 hotkey
<< wxT("Command" );
1382 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1383 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1384 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1385 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1386 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1387 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1388 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1391 // if there are any other keys wxGetAccelFromString() may
1392 // return, we should process them here
1397 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1405 wxFAIL_MSG( wxT("unknown keyboard accel") );
1414 #endif // wxUSE_ACCEL
1416 // ----------------------------------------------------------------------------
1417 // Pop-up menu stuff
1418 // ----------------------------------------------------------------------------
1420 #if wxUSE_MENUS_NATIVE
1422 extern "C" WXDLLIMPEXP_CORE
1423 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1425 *is_waiting
= false;
1428 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1430 menu
->SetInvokingWindow( win
);
1432 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1435 wxMenuItem
*menuitem
= node
->GetData();
1436 if (menuitem
->IsSubMenu())
1438 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1441 node
= node
->GetNext();
1445 extern "C" WXDLLIMPEXP_CORE
1446 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1448 gboolean
* WXUNUSED(whatever
),
1449 gpointer user_data
)
1451 // ensure that the menu appears entirely on screen
1453 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1455 wxSize sizeScreen
= wxGetDisplaySize();
1456 wxPoint
*pos
= (wxPoint
*)user_data
;
1458 gint xmax
= sizeScreen
.x
- req
.width
,
1459 ymax
= sizeScreen
.y
- req
.height
;
1461 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1462 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1465 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1467 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1469 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1471 // NOTE: if you change this code, you need to update
1472 // the same code in taskbar.cpp as well. This
1473 // is ugly code duplication, I know.
1475 SetInvokingWindow( menu
, this );
1479 bool is_waiting
= true;
1481 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1482 G_CALLBACK (gtk_pop_hide_callback
),
1487 GtkMenuPositionFunc posfunc
;
1488 if ( x
== -1 && y
== -1 )
1490 // use GTK's default positioning algorithm
1496 pos
= ClientToScreen(wxPoint(x
, y
));
1498 posfunc
= wxPopupMenuPositionCallback
;
1501 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1502 DoCommonMenuCallbackCode(menu
, eventOpen
);
1505 GTK_MENU(menu
->m_menu
),
1506 (GtkWidget
*) NULL
, // parent menu shell
1507 (GtkWidget
*) NULL
, // parent menu item
1508 posfunc
, // function to position it
1509 userdata
, // client data
1510 0, // button used to activate it
1511 gtk_get_current_event_time()
1516 gtk_main_iteration();
1519 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1521 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1522 DoCommonMenuCallbackCode(menu
, eventClose
);
1527 #endif // wxUSE_MENUS_NATIVE