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(wxConvertFromGTKToWXLabel(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 wxConvertFromGTKToWXLabel(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 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
972 GtkLabel
* label
= NULL
;
974 if ( mitem
->IsSeparator() )
977 menuItem
= gtk_menu_item_new();
979 else if (mitem
->GetBitmap().Ok())
981 text
= mitem
->wxMenuItemBase::GetItemLabel();
982 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
986 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
987 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
991 else // a normal item
993 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
994 text
= mitem
->wxMenuItemBase::GetItemLabel() ;
996 switch ( mitem
->GetKind() )
1000 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1001 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1003 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1010 GSList
*group
= NULL
;
1011 if ( m_prevRadio
== NULL
)
1013 // start of a new radio group
1014 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1015 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1017 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1019 else // continue the radio group
1021 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1022 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1023 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1029 wxFAIL_MSG( wxT("unexpected menu item kind") );
1034 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1035 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1044 GdkModifierType accel_mods
;
1045 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1047 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1048 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1051 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1060 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1062 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1064 gtk_widget_show( menuItem
);
1066 if ( !mitem
->IsSeparator() )
1068 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1070 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1071 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1074 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1075 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1078 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1080 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1082 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1086 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1087 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1091 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1092 if (accel_key
!= GDK_VoidSymbol
)
1094 gtk_widget_add_accelerator (menuItem
,
1096 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1103 mitem
->SetMenuItem(menuItem
);
1107 // This doesn't even exist!
1108 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1114 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1116 if (!GtkAppend(mitem
))
1119 return wxMenuBase::DoAppend(mitem
);
1122 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1124 if ( !wxMenuBase::DoInsert(pos
, item
) )
1128 if ( !GtkAppend(item
, (int)pos
) )
1134 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1136 if ( !wxMenuBase::DoRemove(item
) )
1139 // TODO: this code doesn't delete the item factory item and this seems
1140 // impossible as of GTK 1.2.6.
1141 gtk_widget_destroy( item
->GetMenuItem() );
1146 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1148 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1151 wxMenuItem
*item
= node
->GetData();
1152 if (item
->GetMenuItem() == menuItem
)
1153 return item
->GetId();
1154 node
= node
->GetNext();
1160 // ----------------------------------------------------------------------------
1162 // ----------------------------------------------------------------------------
1166 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1170 wxAcceleratorEntry
*accel
= item
.GetAccel();
1173 int flags
= accel
->GetFlags();
1174 if ( flags
& wxACCEL_ALT
)
1175 hotkey
+= wxT("<alt>");
1176 if ( flags
& wxACCEL_CTRL
)
1177 hotkey
+= wxT("<control>");
1178 if ( flags
& wxACCEL_SHIFT
)
1179 hotkey
+= wxT("<shift>");
1181 int code
= accel
->GetKeyCode();
1208 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1211 // TODO: we should use gdk_keyval_name() (a.k.a.
1212 // XKeysymToString) here as well as hardcoding the keysym
1213 // names this might be not portable
1215 hotkey
<< wxT("Insert" );
1218 hotkey
<< wxT("Delete" );
1221 hotkey
<< wxT("Up" );
1224 hotkey
<< wxT("Down" );
1227 hotkey
<< wxT("Page_Up" );
1230 hotkey
<< wxT("Page_Down" );
1233 hotkey
<< wxT("Left" );
1236 hotkey
<< wxT("Right" );
1239 hotkey
<< wxT("Home" );
1242 hotkey
<< wxT("End" );
1245 hotkey
<< wxT("Return" );
1248 hotkey
<< wxT("BackSpace" );
1251 hotkey
<< wxT("Tab" );
1254 hotkey
<< wxT("Esc" );
1257 hotkey
<< wxT("space" );
1260 hotkey
<< wxT("Multiply" );
1263 hotkey
<< wxT("Add" );
1266 hotkey
<< wxT("Separator" );
1269 hotkey
<< wxT("Subtract" );
1272 hotkey
<< wxT("Decimal" );
1275 hotkey
<< wxT("Divide" );
1278 hotkey
<< wxT("Cancel" );
1281 hotkey
<< wxT("Clear" );
1284 hotkey
<< wxT("Menu" );
1287 hotkey
<< wxT("Pause" );
1290 hotkey
<< wxT("Capital" );
1293 hotkey
<< wxT("Select" );
1296 hotkey
<< wxT("Print" );
1299 hotkey
<< wxT("Execute" );
1302 hotkey
<< wxT("Snapshot" );
1305 hotkey
<< wxT("Help" );
1308 hotkey
<< wxT("Num_Lock" );
1311 hotkey
<< wxT("Scroll_Lock" );
1313 case WXK_NUMPAD_INSERT
:
1314 hotkey
<< wxT("KP_Insert" );
1316 case WXK_NUMPAD_DELETE
:
1317 hotkey
<< wxT("KP_Delete" );
1319 case WXK_NUMPAD_SPACE
:
1320 hotkey
<< wxT("KP_Space" );
1322 case WXK_NUMPAD_TAB
:
1323 hotkey
<< wxT("KP_Tab" );
1325 case WXK_NUMPAD_ENTER
:
1326 hotkey
<< wxT("KP_Enter" );
1328 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1330 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1332 case WXK_NUMPAD_HOME
:
1333 hotkey
<< wxT("KP_Home" );
1335 case WXK_NUMPAD_LEFT
:
1336 hotkey
<< wxT("KP_Left" );
1339 hotkey
<< wxT("KP_Up" );
1341 case WXK_NUMPAD_RIGHT
:
1342 hotkey
<< wxT("KP_Right" );
1344 case WXK_NUMPAD_DOWN
:
1345 hotkey
<< wxT("KP_Down" );
1347 case WXK_NUMPAD_PAGEUP
:
1348 hotkey
<< wxT("KP_Page_Up" );
1350 case WXK_NUMPAD_PAGEDOWN
:
1351 hotkey
<< wxT("KP_Page_Down" );
1353 case WXK_NUMPAD_END
:
1354 hotkey
<< wxT("KP_End" );
1356 case WXK_NUMPAD_BEGIN
:
1357 hotkey
<< wxT("KP_Begin" );
1359 case WXK_NUMPAD_EQUAL
:
1360 hotkey
<< wxT("KP_Equal" );
1362 case WXK_NUMPAD_MULTIPLY
:
1363 hotkey
<< wxT("KP_Multiply" );
1365 case WXK_NUMPAD_ADD
:
1366 hotkey
<< wxT("KP_Add" );
1368 case WXK_NUMPAD_SEPARATOR
:
1369 hotkey
<< wxT("KP_Separator" );
1371 case WXK_NUMPAD_SUBTRACT
:
1372 hotkey
<< wxT("KP_Subtract" );
1374 case WXK_NUMPAD_DECIMAL
:
1375 hotkey
<< wxT("KP_Decimal" );
1377 case WXK_NUMPAD_DIVIDE
:
1378 hotkey
<< wxT("KP_Divide" );
1380 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1381 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1382 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1383 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1385 case WXK_WINDOWS_LEFT
:
1386 hotkey
<< wxT("Super_L" );
1388 case WXK_WINDOWS_RIGHT
:
1389 hotkey
<< wxT("Super_R" );
1391 case WXK_WINDOWS_MENU
:
1392 hotkey
<< wxT("Menu" );
1395 hotkey
<< wxT("Command" );
1397 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1398 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1399 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1400 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1401 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1402 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1403 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1406 // if there are any other keys wxAcceleratorEntry::Create() may
1407 // return, we should process them here
1412 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1413 if ( !name
.empty() )
1420 wxFAIL_MSG( wxT("unknown keyboard accel") );
1429 #endif // wxUSE_ACCEL
1431 // ----------------------------------------------------------------------------
1432 // Pop-up menu stuff
1433 // ----------------------------------------------------------------------------
1435 #if wxUSE_MENUS_NATIVE
1437 extern "C" WXDLLIMPEXP_CORE
1438 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1440 *is_waiting
= false;
1443 extern "C" WXDLLIMPEXP_CORE
1444 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1446 gpointer user_data
)
1448 // ensure that the menu appears entirely on screen
1450 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1452 wxSize sizeScreen
= wxGetDisplaySize();
1453 wxPoint
*pos
= (wxPoint
*)user_data
;
1455 gint xmax
= sizeScreen
.x
- req
.width
,
1456 ymax
= sizeScreen
.y
- req
.height
;
1458 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1459 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1462 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1464 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1466 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1468 // NOTE: if you change this code, you need to update
1469 // the same code in taskbar.cpp as well. This
1470 // is ugly code duplication, I know.
1474 bool is_waiting
= true;
1476 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1478 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1479 (gpointer
)&is_waiting
);
1483 GtkMenuPositionFunc posfunc
;
1484 if ( x
== -1 && y
== -1 )
1486 // use GTK's default positioning algorithm
1492 pos
= ClientToScreen(wxPoint(x
, y
));
1494 posfunc
= wxPopupMenuPositionCallback
;
1497 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1498 DoCommonMenuCallbackCode(menu
, eventOpen
);
1501 GTK_MENU(menu
->m_menu
),
1502 NULL
, // parent menu shell
1503 NULL
, // parent menu item
1504 posfunc
, // function to position it
1505 userdata
, // client data
1506 0, // button used to activate it
1507 wxGtkTimeLastClick
// the time of activation
1512 gtk_main_iteration();
1515 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1517 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1518 DoCommonMenuCallbackCode(menu
, eventClose
);
1523 #endif // wxUSE_MENUS_NATIVE