1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
14 #include "wx/stockitem.h"
20 #include "wx/bitmap.h"
27 #include "wx/gtk1/private.h"
29 #include <gdk/gdkkeysyms.h>
31 #define ACCEL_OBJECT GtkObject
32 #define ACCEL_OBJECTS(a) (a)->attach_objects
33 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
35 // we use normal item but with a special id for the menu title
36 static const int wxGTK_TITLE_ID
= -3;
38 // defined in window.cpp
39 extern guint32 wxGtkTimeLastClick
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern void wxapp_install_idle_handler();
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 static wxString
wxConvertFromGTKToWXLabel(const wxString
& gtkLabel
)
97 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
99 // '_' is the escape character for GTK+.
101 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
103 // An underscore was escaped.
107 else if ( *pc
== wxT('_') )
109 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
112 else if ( *pc
== wxT('&') )
114 // Double the ampersand to escape it as far as wxWidgets is concerned
119 // don't remove ampersands '&' since if we have them in the menu title
120 // it means that they were doubled to indicate "&" instead of accelerator
129 //-----------------------------------------------------------------------------
130 // activate message from GTK
131 //-----------------------------------------------------------------------------
133 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
136 wxapp_install_idle_handler();
138 event
.SetEventObject( menu
);
140 wxEvtHandler
* handler
= menu
->GetEventHandler();
141 if (handler
&& handler
->ProcessEvent(event
))
144 wxWindow
*win
= menu
->GetWindow();
146 win
->HandleWindowEvent( event
);
151 static void gtk_menu_open_callback( GtkWidget
*WXUNUSED(widget
), wxMenu
*menu
)
153 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
155 DoCommonMenuCallbackCode(menu
, event
);
158 static void gtk_menu_close_callback( GtkWidget
*WXUNUSED(widget
), wxMenuBar
*menubar
)
160 if ( !menubar
->GetMenuCount() )
162 // if menubar is empty we can't call GetMenu(0) below
166 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
168 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
179 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
181 // the parent window is known after wxFrame::SetMenu()
182 m_needParent
= false;
185 if (!PreCreation( NULL
, wxDefaultPosition
, wxDefaultSize
) ||
186 !CreateBase( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
188 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
192 m_menubar
= gtk_menu_bar_new();
193 m_accel
= gtk_accel_group_new();
195 if (style
& wxMB_DOCKABLE
)
197 m_widget
= gtk_handle_box_new();
198 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
199 gtk_widget_show( GTK_WIDGET(m_menubar
) );
203 m_widget
= GTK_WIDGET(m_menubar
);
210 for (size_t i
= 0; i
< n
; ++i
)
211 Append(menus
[i
], titles
[i
]);
213 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
214 // don't get it when the menu is dismissed by clicking outside the
215 // toolbar) so we connect to the global one, even if it means that we
216 // can't pass the menu which was closed in wxMenuEvent object
217 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
219 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
224 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
226 Init(n
, menus
, titles
, style
);
229 wxMenuBar::wxMenuBar(long style
)
231 Init(0, NULL
, NULL
, style
);
234 wxMenuBar::wxMenuBar()
236 Init(0, NULL
, NULL
, 0);
239 wxMenuBar::~wxMenuBar()
243 static void DetachFromFrame( wxMenu
*menu
, wxWindow
*win
)
245 wxWindow
*top_frame
= win
;
246 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
247 top_frame
= top_frame
->GetParent();
249 // support for native hot keys
250 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
252 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
255 wxMenuItem
*menuitem
= node
->GetData();
256 if (menuitem
->IsSubMenu())
257 DetachFromFrame( menuitem
->GetSubMenu(), win
);
258 node
= node
->GetNext();
262 static void AttachToFrame( wxMenu
*menu
, wxWindow
*win
)
264 wxWindow
*top_frame
= win
;
265 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
266 top_frame
= top_frame
->GetParent();
268 // support for native hot keys
269 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
270 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
271 gtk_accel_group_attach( menu
->m_accel
, obj
);
273 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
276 wxMenuItem
*menuitem
= node
->GetData();
277 if (menuitem
->IsSubMenu())
278 AttachToFrame( menuitem
->GetSubMenu(), win
);
279 node
= node
->GetNext();
283 void wxMenuBar::Attach( wxFrame
*win
)
285 wxMenuBarBase::Attach(win
);
287 wxWindow
*top_frame
= win
;
288 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
289 top_frame
= top_frame
->GetParent();
291 // support for native key accelerators indicated by underscroes
292 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
293 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
294 gtk_accel_group_attach( m_accel
, obj
);
296 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
299 wxMenu
*menu
= node
->GetData();
300 AttachToFrame( menu
, win
);
301 node
= node
->GetNext();
305 void wxMenuBar::Detach()
307 wxWindow
*top_frame
= m_menuBarFrame
;
308 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
309 top_frame
= top_frame
->GetParent();
311 // support for native key accelerators indicated by underscroes
312 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
314 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
317 wxMenu
*menu
= node
->GetData();
318 DetachFromFrame( menu
, top_frame
);
319 node
= node
->GetNext();
322 wxMenuBarBase::Detach();
325 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
327 if ( !wxMenuBarBase::Append( menu
, title
) )
330 return GtkAppend(menu
, title
);
333 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
335 wxString
str( wxReplaceUnderscore( title
) );
337 // This doesn't have much effect right now.
338 menu
->SetTitle( str
);
340 // The "m_owner" is the "menu item"
341 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
342 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
344 gtk_label_set_text( label
, wxGTK_CONV( str
) );
346 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
347 if (accel_key
!= GDK_VoidSymbol
)
349 gtk_widget_add_accelerator (menu
->m_owner
,
351 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
357 gtk_widget_show( menu
->m_owner
);
359 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
362 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
364 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
366 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
367 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
372 AttachToFrame( menu
, m_menuBarFrame
);
374 // OPTIMISE ME: we should probably cache this, or pass it
375 // directly, but for now this is a minimal
376 // change to validate the new dynamic sizing.
377 // see (and refactor :) similar code in Remove
380 m_menuBarFrame
->UpdateMenuBarSize();
386 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
388 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
393 if ( !GtkAppend(menu
, title
, (int)pos
) )
399 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
401 // remove the old item and insert a new one
402 wxMenu
*menuOld
= Remove(pos
);
403 if ( menuOld
&& !Insert(pos
, menu
, title
) )
408 // either Insert() succeeded or Remove() failed and menuOld is NULL
412 wxMenu
*wxMenuBar::Remove(size_t pos
)
414 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
418 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
419 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
421 gtk_widget_destroy( menu
->m_owner
);
422 menu
->m_owner
= NULL
;
426 // OPTIMISE ME: see comment in GtkAppend
427 m_menuBarFrame
->UpdateMenuBarSize();
433 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
435 if (wxMenuItem::GetLabelText(menu
->GetTitle()) == wxMenuItem::GetLabelText(menuString
))
437 int res
= menu
->FindItem( itemString
);
438 if (res
!= wxNOT_FOUND
)
442 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
445 wxMenuItem
*item
= node
->GetData();
446 if (item
->IsSubMenu())
447 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
449 node
= node
->GetNext();
455 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
457 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
460 wxMenu
*menu
= node
->GetData();
461 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
464 node
= node
->GetNext();
470 // Find a wxMenuItem using its id. Recurses down into sub-menus
471 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
473 wxMenuItem
* result
= menu
->FindChildItem(id
);
475 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
476 while ( node
&& result
== NULL
)
478 wxMenuItem
*item
= node
->GetData();
479 if (item
->IsSubMenu())
481 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
483 node
= node
->GetNext();
489 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
491 wxMenuItem
* result
= 0;
492 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
493 while (node
&& result
== 0)
495 wxMenu
*menu
= node
->GetData();
496 result
= FindMenuItemByIdRecursive( menu
, id
);
497 node
= node
->GetNext();
502 *menuForItem
= result
? result
->GetMenu() : NULL
;
508 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
510 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
512 wxCHECK_RET( node
, wxT("menu not found") );
514 wxMenu
* menu
= node
->GetData();
517 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
520 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
522 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
524 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
526 wxMenu
* menu
= node
->GetData();
528 return menu
->GetTitle();
531 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
533 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
535 wxCHECK_RET( node
, wxT("menu not found") );
537 wxMenu
* menu
= node
->GetData();
539 const wxString
str( wxReplaceUnderscore( label
) );
541 menu
->SetTitle( str
);
545 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
548 gtk_label_set( glabel
, wxGTK_CONV( str
) );
550 /* reparse key accel */
551 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
552 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
557 //-----------------------------------------------------------------------------
559 //-----------------------------------------------------------------------------
562 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
565 wxapp_install_idle_handler();
567 int id
= menu
->FindMenuIdByMenuItem(widget
);
569 /* should find it for normal (not popup) menu */
570 wxASSERT_MSG( (id
!= -1) || (menu
->GetWindow() != NULL
),
571 wxT("menu item not found in gtk_menu_clicked_callback") );
573 if (!menu
->IsEnabled(id
))
576 wxMenuItem
* item
= menu
->FindChildItem( id
);
577 wxCHECK_RET( item
, wxT("error in menu item callback") );
579 if ( item
->GetId() == wxGTK_TITLE_ID
)
581 // ignore events from the menu title
585 if (item
->IsCheckable())
587 bool isReallyChecked
= item
->IsChecked(),
588 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
590 // ensure that the internal state is always consistent with what is
591 // shown on the screen
592 item
->wxMenuItemBase::Check(isReallyChecked
);
594 // we must not report the events for the radio button going up nor the
595 // events resulting from the calls to wxMenuItem::Check()
596 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
597 (isInternallyChecked
== isReallyChecked
) )
604 // Is this menu on a menubar? (possibly nested)
605 wxFrame
* frame
= NULL
;
606 if(menu
->IsAttached())
607 frame
= menu
->GetMenuBar()->GetFrame();
609 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
610 // normally wxMenu::SendEvent() should be enough, if it doesn't work
611 // in wxGTK then we have a bug in wxMenu::GetWindow() which
612 // should be fixed instead of working around it here...
615 // If it is attached then let the frame send the event.
616 // Don't call frame->ProcessCommand(id) because it toggles
617 // checkable items and we've already done that above.
618 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
619 commandEvent
.SetEventObject(frame
);
620 if (item
->IsCheckable())
621 commandEvent
.SetInt(item
->IsChecked());
622 commandEvent
.SetEventObject(menu
);
624 frame
->HandleWindowEvent(commandEvent
);
628 // otherwise let the menu have it
629 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
634 //-----------------------------------------------------------------------------
636 //-----------------------------------------------------------------------------
639 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
641 if (g_isIdle
) wxapp_install_idle_handler();
643 int id
= menu
->FindMenuIdByMenuItem(widget
);
645 wxASSERT( id
!= -1 ); // should find it!
647 if (!menu
->IsEnabled(id
))
650 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
651 event
.SetEventObject( menu
);
653 wxEvtHandler
* handler
= menu
->GetEventHandler();
654 if (handler
&& handler
->ProcessEvent(event
))
657 wxWindow
*win
= menu
->GetWindow();
658 if (win
) win
->HandleWindowEvent( event
);
662 //-----------------------------------------------------------------------------
664 //-----------------------------------------------------------------------------
667 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
669 if (g_isIdle
) wxapp_install_idle_handler();
671 int id
= menu
->FindMenuIdByMenuItem(widget
);
673 wxASSERT( id
!= -1 ); // should find it!
675 if (!menu
->IsEnabled(id
))
678 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
679 event
.SetEventObject( menu
);
681 wxEvtHandler
* handler
= menu
->GetEventHandler();
682 if (handler
&& handler
->ProcessEvent(event
))
685 wxWindow
*win
= menu
->GetWindow();
687 win
->HandleWindowEvent( event
);
691 //-----------------------------------------------------------------------------
693 //-----------------------------------------------------------------------------
695 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
697 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
699 const wxString
& name
,
700 const wxString
& help
,
704 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
707 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
709 const wxString
& text
,
710 const wxString
& help
,
713 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
718 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
720 const wxString
& text
,
721 const wxString
& help
,
724 : wxMenuItemBase(parentMenu
, id
, text
, help
,
725 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
730 void wxMenuItem::Init()
732 m_labelWidget
= NULL
;
738 wxMenuItem::~wxMenuItem()
740 // don't delete menu items, the menus take care of that
743 wxString
wxMenuItem::GetItemLabel() const
745 wxString label
= wxConvertFromGTKToWXLabel(m_text
);
746 if (!m_hotKey
.IsEmpty())
747 label
= label
+ wxT("\t") + m_hotKey
;
751 void wxMenuItem::SetItemLabel( const wxString
& string
)
753 wxString str
= string
;
754 if ( str
.empty() && !IsSeparator() )
756 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
757 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
758 wxSTOCK_WITH_MNEMONIC
);
761 // Some optimization to avoid flicker
762 wxString oldLabel
= m_text
;
763 oldLabel
= wxStripMenuCodes(oldLabel
);
764 oldLabel
.Replace(wxT("_"), wxEmptyString
);
765 wxString label1
= wxStripMenuCodes(str
);
766 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
767 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
771 if (oldLabel
== label1
&&
772 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
779 label
= (GtkLabel
*) m_labelWidget
;
781 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
784 gtk_label_set( label
, wxGTK_CONV( m_text
) );
787 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
788 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
792 GdkModifierType accel_mods
;
793 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
796 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
797 m_parentMenu
->m_accel
,
802 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
803 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
806 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
808 m_parentMenu
->m_accel
,
815 // it's valid for this function to be called even if m_menuItem == NULL
816 void wxMenuItem::DoSetText( const wxString
& str
)
818 // '\t' is the deliminator indicating a hot key
820 text
.reserve(str
.length());
822 const wxChar
*pc
= str
;
823 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
825 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
827 // "&" is doubled to indicate "&" instead of accelerator
831 else if (*pc
== wxT('&'))
835 else if ( *pc
== wxT('_') ) // escape underscores
846 m_hotKey
= wxEmptyString
;
848 if ( *pc
== wxT('\t') )
859 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
867 // accelerator parsing code looks for them after a TAB, so insert a dummy
870 label
<< wxT('\t') << GetHotKey();
872 return wxAcceleratorEntry::Create(label
);
875 #endif // wxUSE_ACCEL
877 void wxMenuItem::Check( bool check
)
879 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
881 if (check
== m_isChecked
)
884 wxMenuItemBase::Check( check
);
890 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
894 wxFAIL_MSG( wxT("can't check this item") );
898 void wxMenuItem::Enable( bool enable
)
900 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
902 gtk_widget_set_sensitive( m_menuItem
, enable
);
903 wxMenuItemBase::Enable( enable
);
906 bool wxMenuItem::IsChecked() const
908 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
910 wxCHECK_MSG( IsCheckable(), false,
911 wxT("can't get state of uncheckable item!") );
913 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
916 //-----------------------------------------------------------------------------
918 //-----------------------------------------------------------------------------
920 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
924 m_accel
= gtk_accel_group_new();
925 m_menu
= gtk_menu_new();
926 // NB: keep reference to the menu so that it is not destroyed behind
927 // our back by GTK+ e.g. when it is removed from menubar:
928 gtk_widget_ref(m_menu
);
932 // Tearoffs are entries, just like separators. So if we want this
933 // menu to be a tear-off one, we just append a tearoff entry
935 if ( m_style
& wxMENU_TEAROFF
)
937 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
939 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
944 // append the title as the very first entry if we have it
945 if ( !m_title
.empty() )
947 Append(wxGTK_TITLE_ID
, m_title
);
954 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
956 if ( GTK_IS_WIDGET( m_menu
))
959 gtk_widget_unref( m_menu
);
960 // if the menu is inserted in another menu at this time, there was
961 // one more reference to it:
963 gtk_widget_destroy( m_menu
);
967 const wxString
wxMenu::GetTitle() const
969 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
972 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
977 GtkLabel
* label
= NULL
;
979 if ( mitem
->IsSeparator() )
982 menuItem
= gtk_menu_item_new();
984 else if (mitem
->GetBitmap().Ok())
986 text
= mitem
->wxMenuItemBase::GetItemLabel();
987 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
991 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
992 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
996 else // a normal item
998 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
999 text
= mitem
->wxMenuItemBase::GetItemLabel() ;
1001 switch ( mitem
->GetKind() )
1005 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1006 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1008 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1015 GSList
*group
= NULL
;
1016 if ( m_prevRadio
== NULL
)
1018 // start of a new radio group
1019 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1020 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1022 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1024 else // continue the radio group
1026 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1027 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1028 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1034 wxFAIL_MSG( wxT("unexpected menu item kind") );
1039 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1040 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1049 GdkModifierType accel_mods
;
1050 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1052 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1053 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1056 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1065 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1067 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1069 gtk_widget_show( menuItem
);
1071 if ( !mitem
->IsSeparator() )
1073 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1075 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1076 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1079 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1080 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1083 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1085 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1087 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1091 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1092 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1096 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1097 if (accel_key
!= GDK_VoidSymbol
)
1099 gtk_widget_add_accelerator (menuItem
,
1101 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1108 mitem
->SetMenuItem(menuItem
);
1112 // This doesn't even exist!
1113 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1119 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1121 if (!GtkAppend(mitem
))
1124 return wxMenuBase::DoAppend(mitem
);
1127 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1129 if ( !wxMenuBase::DoInsert(pos
, item
) )
1133 if ( !GtkAppend(item
, (int)pos
) )
1139 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1141 if ( !wxMenuBase::DoRemove(item
) )
1144 // TODO: this code doesn't delete the item factory item and this seems
1145 // impossible as of GTK 1.2.6.
1146 gtk_widget_destroy( item
->GetMenuItem() );
1151 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1153 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1156 wxMenuItem
*item
= node
->GetData();
1157 if (item
->GetMenuItem() == menuItem
)
1158 return item
->GetId();
1159 node
= node
->GetNext();
1165 // ----------------------------------------------------------------------------
1167 // ----------------------------------------------------------------------------
1171 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1175 wxAcceleratorEntry
*accel
= item
.GetAccel();
1178 int flags
= accel
->GetFlags();
1179 if ( flags
& wxACCEL_ALT
)
1180 hotkey
+= wxT("<alt>");
1181 if ( flags
& wxACCEL_CTRL
)
1182 hotkey
+= wxT("<control>");
1183 if ( flags
& wxACCEL_SHIFT
)
1184 hotkey
+= wxT("<shift>");
1186 int code
= accel
->GetKeyCode();
1213 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1216 // TODO: we should use gdk_keyval_name() (a.k.a.
1217 // XKeysymToString) here as well as hardcoding the keysym
1218 // names this might be not portable
1220 hotkey
<< wxT("Insert" );
1223 hotkey
<< wxT("Delete" );
1226 hotkey
<< wxT("Up" );
1229 hotkey
<< wxT("Down" );
1232 hotkey
<< wxT("Page_Up" );
1235 hotkey
<< wxT("Page_Down" );
1238 hotkey
<< wxT("Left" );
1241 hotkey
<< wxT("Right" );
1244 hotkey
<< wxT("Home" );
1247 hotkey
<< wxT("End" );
1250 hotkey
<< wxT("Return" );
1253 hotkey
<< wxT("BackSpace" );
1256 hotkey
<< wxT("Tab" );
1259 hotkey
<< wxT("Esc" );
1262 hotkey
<< wxT("space" );
1265 hotkey
<< wxT("Multiply" );
1268 hotkey
<< wxT("Add" );
1271 hotkey
<< wxT("Separator" );
1274 hotkey
<< wxT("Subtract" );
1277 hotkey
<< wxT("Decimal" );
1280 hotkey
<< wxT("Divide" );
1283 hotkey
<< wxT("Cancel" );
1286 hotkey
<< wxT("Clear" );
1289 hotkey
<< wxT("Menu" );
1292 hotkey
<< wxT("Pause" );
1295 hotkey
<< wxT("Capital" );
1298 hotkey
<< wxT("Select" );
1301 hotkey
<< wxT("Print" );
1304 hotkey
<< wxT("Execute" );
1307 hotkey
<< wxT("Snapshot" );
1310 hotkey
<< wxT("Help" );
1313 hotkey
<< wxT("Num_Lock" );
1316 hotkey
<< wxT("Scroll_Lock" );
1318 case WXK_NUMPAD_INSERT
:
1319 hotkey
<< wxT("KP_Insert" );
1321 case WXK_NUMPAD_DELETE
:
1322 hotkey
<< wxT("KP_Delete" );
1324 case WXK_NUMPAD_SPACE
:
1325 hotkey
<< wxT("KP_Space" );
1327 case WXK_NUMPAD_TAB
:
1328 hotkey
<< wxT("KP_Tab" );
1330 case WXK_NUMPAD_ENTER
:
1331 hotkey
<< wxT("KP_Enter" );
1333 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1335 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1337 case WXK_NUMPAD_HOME
:
1338 hotkey
<< wxT("KP_Home" );
1340 case WXK_NUMPAD_LEFT
:
1341 hotkey
<< wxT("KP_Left" );
1344 hotkey
<< wxT("KP_Up" );
1346 case WXK_NUMPAD_RIGHT
:
1347 hotkey
<< wxT("KP_Right" );
1349 case WXK_NUMPAD_DOWN
:
1350 hotkey
<< wxT("KP_Down" );
1352 case WXK_NUMPAD_PAGEUP
:
1353 hotkey
<< wxT("KP_Page_Up" );
1355 case WXK_NUMPAD_PAGEDOWN
:
1356 hotkey
<< wxT("KP_Page_Down" );
1358 case WXK_NUMPAD_END
:
1359 hotkey
<< wxT("KP_End" );
1361 case WXK_NUMPAD_BEGIN
:
1362 hotkey
<< wxT("KP_Begin" );
1364 case WXK_NUMPAD_EQUAL
:
1365 hotkey
<< wxT("KP_Equal" );
1367 case WXK_NUMPAD_MULTIPLY
:
1368 hotkey
<< wxT("KP_Multiply" );
1370 case WXK_NUMPAD_ADD
:
1371 hotkey
<< wxT("KP_Add" );
1373 case WXK_NUMPAD_SEPARATOR
:
1374 hotkey
<< wxT("KP_Separator" );
1376 case WXK_NUMPAD_SUBTRACT
:
1377 hotkey
<< wxT("KP_Subtract" );
1379 case WXK_NUMPAD_DECIMAL
:
1380 hotkey
<< wxT("KP_Decimal" );
1382 case WXK_NUMPAD_DIVIDE
:
1383 hotkey
<< wxT("KP_Divide" );
1385 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1386 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1387 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1388 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1390 case WXK_WINDOWS_LEFT
:
1391 hotkey
<< wxT("Super_L" );
1393 case WXK_WINDOWS_RIGHT
:
1394 hotkey
<< wxT("Super_R" );
1396 case WXK_WINDOWS_MENU
:
1397 hotkey
<< wxT("Menu" );
1400 hotkey
<< wxT("Command" );
1402 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1403 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1404 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1405 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1406 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1407 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1408 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1411 // if there are any other keys wxAcceleratorEntry::Create() may
1412 // return, we should process them here
1417 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1418 if ( !name
.empty() )
1425 wxFAIL_MSG( wxT("unknown keyboard accel") );
1434 #endif // wxUSE_ACCEL
1436 // ----------------------------------------------------------------------------
1437 // Pop-up menu stuff
1438 // ----------------------------------------------------------------------------
1440 #if wxUSE_MENUS_NATIVE
1442 extern "C" WXDLLIMPEXP_CORE
1443 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1445 *is_waiting
= false;
1448 extern "C" WXDLLIMPEXP_CORE
1449 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1451 gpointer user_data
)
1453 // ensure that the menu appears entirely on screen
1455 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1457 wxSize sizeScreen
= wxGetDisplaySize();
1458 wxPoint
*pos
= (wxPoint
*)user_data
;
1460 gint xmax
= sizeScreen
.x
- req
.width
,
1461 ymax
= sizeScreen
.y
- req
.height
;
1463 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1464 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1467 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1469 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1471 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1473 // NOTE: if you change this code, you need to update
1474 // the same code in taskbar.cpp as well. This
1475 // is ugly code duplication, I know.
1479 bool is_waiting
= true;
1481 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1483 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1484 (gpointer
)&is_waiting
);
1488 GtkMenuPositionFunc posfunc
;
1489 if ( x
== -1 && y
== -1 )
1491 // use GTK's default positioning algorithm
1497 pos
= ClientToScreen(wxPoint(x
, y
));
1499 posfunc
= wxPopupMenuPositionCallback
;
1502 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1503 DoCommonMenuCallbackCode(menu
, eventOpen
);
1506 GTK_MENU(menu
->m_menu
),
1507 NULL
, // parent menu shell
1508 NULL
, // parent menu item
1509 posfunc
, // function to position it
1510 userdata
, // client data
1511 0, // button used to activate it
1512 wxGtkTimeLastClick
// the time of activation
1517 gtk_main_iteration();
1520 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1522 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1523 DoCommonMenuCallbackCode(menu
, eventClose
);
1528 #endif // wxUSE_MENUS_NATIVE