1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/menu.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
13 #include "wx/stockitem.h"
19 #include "wx/bitmap.h"
26 #include "wx/gtk1/private.h"
27 #include "wx/gtk1/private/mnemonics.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 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
179 // the parent window is known after wxFrame::SetMenu()
180 m_needParent
= false;
183 if (!PreCreation( NULL
, wxDefaultPosition
, wxDefaultSize
) ||
184 !CreateBase( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
186 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
190 m_menubar
= gtk_menu_bar_new();
191 m_accel
= gtk_accel_group_new();
193 if (style
& wxMB_DOCKABLE
)
195 m_widget
= gtk_handle_box_new();
196 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
197 gtk_widget_show( GTK_WIDGET(m_menubar
) );
201 m_widget
= GTK_WIDGET(m_menubar
);
208 for (size_t i
= 0; i
< n
; ++i
)
209 Append(menus
[i
], titles
[i
]);
211 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
212 // don't get it when the menu is dismissed by clicking outside the
213 // toolbar) so we connect to the global one, even if it means that we
214 // can't pass the menu which was closed in wxMenuEvent object
215 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
217 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
222 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
224 Init(n
, menus
, titles
, style
);
227 wxMenuBar::wxMenuBar(long style
)
229 Init(0, NULL
, NULL
, style
);
232 wxMenuBar::wxMenuBar()
234 Init(0, NULL
, NULL
, 0);
237 wxMenuBar::~wxMenuBar()
241 static void DetachFromFrame( wxMenu
*menu
, wxWindow
*win
)
243 wxWindow
*top_frame
= win
;
244 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
245 top_frame
= top_frame
->GetParent();
247 // support for native hot keys
248 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
250 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
253 wxMenuItem
*menuitem
= node
->GetData();
254 if (menuitem
->IsSubMenu())
255 DetachFromFrame( menuitem
->GetSubMenu(), win
);
256 node
= node
->GetNext();
260 static void AttachToFrame( wxMenu
*menu
, wxWindow
*win
)
262 wxWindow
*top_frame
= win
;
263 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
264 top_frame
= top_frame
->GetParent();
266 // support for native hot keys
267 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
268 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
269 gtk_accel_group_attach( menu
->m_accel
, obj
);
271 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
274 wxMenuItem
*menuitem
= node
->GetData();
275 if (menuitem
->IsSubMenu())
276 AttachToFrame( menuitem
->GetSubMenu(), win
);
277 node
= node
->GetNext();
281 void wxMenuBar::Attach( wxFrame
*win
)
283 wxMenuBarBase::Attach(win
);
285 wxWindow
*top_frame
= win
;
286 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
287 top_frame
= top_frame
->GetParent();
289 // support for native key accelerators indicated by underscroes
290 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
291 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
292 gtk_accel_group_attach( m_accel
, obj
);
294 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
297 wxMenu
*menu
= node
->GetData();
298 AttachToFrame( menu
, win
);
299 node
= node
->GetNext();
303 void wxMenuBar::Detach()
305 wxWindow
*top_frame
= m_menuBarFrame
;
306 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
307 top_frame
= top_frame
->GetParent();
309 // support for native key accelerators indicated by underscroes
310 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
312 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
315 wxMenu
*menu
= node
->GetData();
316 DetachFromFrame( menu
, top_frame
);
317 node
= node
->GetNext();
320 wxMenuBarBase::Detach();
323 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
325 if ( !wxMenuBarBase::Append( menu
, title
) )
328 return GtkAppend(menu
, title
);
331 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
333 wxString
str( wxReplaceUnderscore( title
) );
335 // This doesn't have much effect right now.
336 menu
->SetTitle( str
);
338 // The "m_owner" is the "menu item"
339 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
340 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
342 gtk_label_set_text( label
, wxGTK_CONV( str
) );
344 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
345 if (accel_key
!= GDK_VoidSymbol
)
347 gtk_widget_add_accelerator (menu
->m_owner
,
349 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
355 gtk_widget_show( menu
->m_owner
);
357 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
360 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
362 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
364 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
365 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
370 AttachToFrame( menu
, m_menuBarFrame
);
372 // OPTIMISE ME: we should probably cache this, or pass it
373 // directly, but for now this is a minimal
374 // change to validate the new dynamic sizing.
375 // see (and refactor :) similar code in Remove
378 m_menuBarFrame
->UpdateMenuBarSize();
384 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
386 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
391 if ( !GtkAppend(menu
, title
, (int)pos
) )
397 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
399 // remove the old item and insert a new one
400 wxMenu
*menuOld
= Remove(pos
);
401 if ( menuOld
&& !Insert(pos
, menu
, title
) )
406 // either Insert() succeeded or Remove() failed and menuOld is NULL
410 wxMenu
*wxMenuBar::Remove(size_t pos
)
412 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
416 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
417 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
419 gtk_widget_destroy( menu
->m_owner
);
420 menu
->m_owner
= NULL
;
424 // OPTIMISE ME: see comment in GtkAppend
425 m_menuBarFrame
->UpdateMenuBarSize();
431 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
433 if (wxMenuItem::GetLabelText(menu
->GetTitle()) == wxMenuItem::GetLabelText(menuString
))
435 int res
= menu
->FindItem( itemString
);
436 if (res
!= wxNOT_FOUND
)
440 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
443 wxMenuItem
*item
= node
->GetData();
444 if (item
->IsSubMenu())
445 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
447 node
= node
->GetNext();
453 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
455 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
458 wxMenu
*menu
= node
->GetData();
459 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
462 node
= node
->GetNext();
468 // Find a wxMenuItem using its id. Recurses down into sub-menus
469 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
471 wxMenuItem
* result
= menu
->FindChildItem(id
);
473 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
474 while ( node
&& result
== NULL
)
476 wxMenuItem
*item
= node
->GetData();
477 if (item
->IsSubMenu())
479 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
481 node
= node
->GetNext();
487 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
489 wxMenuItem
* result
= 0;
490 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
491 while (node
&& result
== 0)
493 wxMenu
*menu
= node
->GetData();
494 result
= FindMenuItemByIdRecursive( menu
, id
);
495 node
= node
->GetNext();
500 *menuForItem
= result
? result
->GetMenu() : NULL
;
506 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
508 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
510 wxCHECK_RET( node
, wxT("menu not found") );
512 wxMenu
* menu
= node
->GetData();
515 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
518 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
520 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
522 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
524 wxMenu
* menu
= node
->GetData();
526 return menu
->GetTitle();
529 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
531 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
533 wxCHECK_RET( node
, wxT("menu not found") );
535 wxMenu
* menu
= node
->GetData();
537 const wxString
str( wxReplaceUnderscore( label
) );
539 menu
->SetTitle( str
);
543 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
546 gtk_label_set( glabel
, wxGTK_CONV( str
) );
548 /* reparse key accel */
549 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
550 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
555 //-----------------------------------------------------------------------------
557 //-----------------------------------------------------------------------------
560 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
563 wxapp_install_idle_handler();
565 int id
= menu
->FindMenuIdByMenuItem(widget
);
567 /* should find it for normal (not popup) menu */
568 wxASSERT_MSG( (id
!= -1) || (menu
->GetWindow() != NULL
),
569 wxT("menu item not found in gtk_menu_clicked_callback") );
571 if (!menu
->IsEnabled(id
))
574 wxMenuItem
* item
= menu
->FindChildItem( id
);
575 wxCHECK_RET( item
, wxT("error in menu item callback") );
577 if ( item
->GetId() == wxGTK_TITLE_ID
)
579 // ignore events from the menu title
583 if (item
->IsCheckable())
585 bool isReallyChecked
= item
->IsChecked(),
586 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
588 // ensure that the internal state is always consistent with what is
589 // shown on the screen
590 item
->wxMenuItemBase::Check(isReallyChecked
);
592 // we must not report the events for the radio button going up nor the
593 // events resulting from the calls to wxMenuItem::Check()
594 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
595 (isInternallyChecked
== isReallyChecked
) )
602 // Is this menu on a menubar? (possibly nested)
603 wxFrame
* frame
= NULL
;
604 if(menu
->IsAttached())
605 frame
= menu
->GetMenuBar()->GetFrame();
607 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
608 // normally wxMenu::SendEvent() should be enough, if it doesn't work
609 // in wxGTK then we have a bug in wxMenu::GetWindow() which
610 // should be fixed instead of working around it here...
613 // If it is attached then let the frame send the event.
614 // Don't call frame->ProcessCommand(id) because it toggles
615 // checkable items and we've already done that above.
616 wxCommandEvent
commandEvent(wxEVT_MENU
, id
);
617 commandEvent
.SetEventObject(frame
);
618 if (item
->IsCheckable())
619 commandEvent
.SetInt(item
->IsChecked());
620 commandEvent
.SetEventObject(menu
);
622 frame
->HandleWindowEvent(commandEvent
);
626 // otherwise let the menu have it
627 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
632 //-----------------------------------------------------------------------------
634 //-----------------------------------------------------------------------------
637 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
639 if (g_isIdle
) wxapp_install_idle_handler();
641 int id
= menu
->FindMenuIdByMenuItem(widget
);
643 wxASSERT( id
!= -1 ); // should find it!
645 if (!menu
->IsEnabled(id
))
648 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
649 event
.SetEventObject( menu
);
651 wxEvtHandler
* handler
= menu
->GetEventHandler();
652 if (handler
&& handler
->ProcessEvent(event
))
655 wxWindow
*win
= menu
->GetWindow();
656 if (win
) win
->HandleWindowEvent( event
);
660 //-----------------------------------------------------------------------------
662 //-----------------------------------------------------------------------------
665 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
667 if (g_isIdle
) wxapp_install_idle_handler();
669 int id
= menu
->FindMenuIdByMenuItem(widget
);
671 wxASSERT( id
!= -1 ); // should find it!
673 if (!menu
->IsEnabled(id
))
676 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
677 event
.SetEventObject( menu
);
679 wxEvtHandler
* handler
= menu
->GetEventHandler();
680 if (handler
&& handler
->ProcessEvent(event
))
683 wxWindow
*win
= menu
->GetWindow();
685 win
->HandleWindowEvent( event
);
689 //-----------------------------------------------------------------------------
691 //-----------------------------------------------------------------------------
693 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
695 const wxString
& name
,
696 const wxString
& help
,
700 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
703 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
705 const wxString
& text
,
706 const wxString
& help
,
709 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
714 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
716 const wxString
& text
,
717 const wxString
& help
,
720 : wxMenuItemBase(parentMenu
, id
, text
, help
,
721 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
726 void wxMenuItem::Init()
728 m_labelWidget
= NULL
;
734 wxMenuItem::~wxMenuItem()
736 // don't delete menu items, the menus take care of that
739 wxString
wxMenuItem::GetItemLabel() const
741 wxString label
= wxConvertFromGTKToWXLabel(m_text
);
742 if (!m_hotKey
.IsEmpty())
743 label
= label
+ wxT("\t") + m_hotKey
;
747 void wxMenuItem::SetItemLabel( const wxString
& string
)
749 wxString str
= string
;
750 if ( str
.empty() && !IsSeparator() )
752 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
753 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
754 wxSTOCK_WITH_MNEMONIC
);
757 // Some optimization to avoid flicker
758 wxString oldLabel
= m_text
;
759 oldLabel
= wxStripMenuCodes(oldLabel
);
760 oldLabel
.Replace(wxT("_"), wxEmptyString
);
761 wxString label1
= wxStripMenuCodes(str
);
762 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
763 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
767 if (oldLabel
== label1
&&
768 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
775 label
= (GtkLabel
*) m_labelWidget
;
777 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
780 gtk_label_set( label
, wxGTK_CONV( m_text
) );
783 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
784 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
788 GdkModifierType accel_mods
;
789 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
792 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
793 m_parentMenu
->m_accel
,
798 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
799 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
802 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
804 m_parentMenu
->m_accel
,
811 // it's valid for this function to be called even if m_menuItem == NULL
812 void wxMenuItem::DoSetText( const wxString
& str
)
814 // '\t' is the deliminator indicating a hot key
816 text
.reserve(str
.length());
818 const wxChar
*pc
= str
;
819 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
821 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
823 // "&" is doubled to indicate "&" instead of accelerator
827 else if (*pc
== wxT('&'))
831 else if ( *pc
== wxT('_') ) // escape underscores
842 m_hotKey
= wxEmptyString
;
844 if ( *pc
== wxT('\t') )
855 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
863 // accelerator parsing code looks for them after a TAB, so insert a dummy
866 label
<< wxT('\t') << GetHotKey();
868 return wxAcceleratorEntry::Create(label
);
871 #endif // wxUSE_ACCEL
873 void wxMenuItem::Check( bool check
)
875 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
877 if (check
== m_isChecked
)
880 wxMenuItemBase::Check( check
);
886 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
890 wxFAIL_MSG( wxT("can't check this item") );
894 void wxMenuItem::Enable( bool enable
)
896 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
898 gtk_widget_set_sensitive( m_menuItem
, enable
);
899 wxMenuItemBase::Enable( enable
);
902 bool wxMenuItem::IsChecked() const
904 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
906 wxCHECK_MSG( IsCheckable(), false,
907 wxT("can't get state of uncheckable item!") );
909 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
912 //-----------------------------------------------------------------------------
914 //-----------------------------------------------------------------------------
918 m_accel
= gtk_accel_group_new();
919 m_menu
= gtk_menu_new();
920 // NB: keep reference to the menu so that it is not destroyed behind
921 // our back by GTK+ e.g. when it is removed from menubar:
922 gtk_widget_ref(m_menu
);
926 // Tearoffs are entries, just like separators. So if we want this
927 // menu to be a tear-off one, we just append a tearoff entry
929 if ( m_style
& wxMENU_TEAROFF
)
931 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
933 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
938 // append the title as the very first entry if we have it
939 if ( !m_title
.empty() )
941 Append(wxGTK_TITLE_ID
, m_title
);
948 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
950 if ( GTK_IS_WIDGET( m_menu
))
953 gtk_widget_unref( m_menu
);
954 // if the menu is inserted in another menu at this time, there was
955 // one more reference to it:
957 gtk_widget_destroy( m_menu
);
961 wxString
wxMenu::GetTitle() const
963 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
966 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
971 GtkLabel
* label
= NULL
;
973 if ( mitem
->IsSeparator() )
976 menuItem
= gtk_menu_item_new();
978 else if (mitem
->GetBitmap().IsOk())
980 text
= mitem
->wxMenuItemBase::GetItemLabel();
981 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
985 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
986 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
990 else // a normal item
992 // text has "_" instead of "&" after mitem->SetItemLabel() so don't use it
993 text
= mitem
->wxMenuItemBase::GetItemLabel() ;
995 switch ( mitem
->GetKind() )
999 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1000 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1002 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1009 GSList
*group
= NULL
;
1010 if ( m_prevRadio
== NULL
)
1012 // start of a new radio group
1013 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1014 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1016 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1018 else // continue the radio group
1020 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1021 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1022 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1028 wxFAIL_MSG( wxT("unexpected menu item kind") );
1033 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1034 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1043 GdkModifierType accel_mods
;
1044 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1046 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetItemLabel().c_str(), GetGtkHotKey(*mitem).c_str() );
1047 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1050 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1059 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1061 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1063 gtk_widget_show( menuItem
);
1065 if ( !mitem
->IsSeparator() )
1067 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1069 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1070 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1073 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1074 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1077 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1079 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1081 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1085 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1086 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1090 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1091 if (accel_key
!= GDK_VoidSymbol
)
1093 gtk_widget_add_accelerator (menuItem
,
1095 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1102 mitem
->SetMenuItem(menuItem
);
1106 // This doesn't even exist!
1107 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1113 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1115 if (!GtkAppend(mitem
))
1118 return wxMenuBase::DoAppend(mitem
);
1121 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1123 if ( !wxMenuBase::DoInsert(pos
, item
) )
1127 if ( !GtkAppend(item
, (int)pos
) )
1133 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1135 if ( !wxMenuBase::DoRemove(item
) )
1138 // TODO: this code doesn't delete the item factory item and this seems
1139 // impossible as of GTK 1.2.6.
1140 gtk_widget_destroy( item
->GetMenuItem() );
1145 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1147 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1150 wxMenuItem
*item
= node
->GetData();
1151 if (item
->GetMenuItem() == menuItem
)
1152 return item
->GetId();
1153 node
= node
->GetNext();
1159 // ----------------------------------------------------------------------------
1161 // ----------------------------------------------------------------------------
1165 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1169 wxAcceleratorEntry
*accel
= item
.GetAccel();
1172 int flags
= accel
->GetFlags();
1173 if ( flags
& wxACCEL_ALT
)
1174 hotkey
+= wxT("<alt>");
1175 if ( flags
& wxACCEL_CTRL
)
1176 hotkey
+= wxT("<control>");
1177 if ( flags
& wxACCEL_SHIFT
)
1178 hotkey
+= wxT("<shift>");
1180 int code
= accel
->GetKeyCode();
1207 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1210 // TODO: we should use gdk_keyval_name() (a.k.a.
1211 // XKeysymToString) here as well as hardcoding the keysym
1212 // names this might be not portable
1214 hotkey
<< wxT("Insert" );
1217 hotkey
<< wxT("Delete" );
1220 hotkey
<< wxT("Up" );
1223 hotkey
<< wxT("Down" );
1226 hotkey
<< wxT("Page_Up" );
1229 hotkey
<< wxT("Page_Down" );
1232 hotkey
<< wxT("Left" );
1235 hotkey
<< wxT("Right" );
1238 hotkey
<< wxT("Home" );
1241 hotkey
<< wxT("End" );
1244 hotkey
<< wxT("Return" );
1247 hotkey
<< wxT("BackSpace" );
1250 hotkey
<< wxT("Tab" );
1253 hotkey
<< wxT("Esc" );
1256 hotkey
<< wxT("space" );
1259 hotkey
<< wxT("Multiply" );
1262 hotkey
<< wxT("Add" );
1265 hotkey
<< wxT("Separator" );
1268 hotkey
<< wxT("Subtract" );
1271 hotkey
<< wxT("Decimal" );
1274 hotkey
<< wxT("Divide" );
1277 hotkey
<< wxT("Cancel" );
1280 hotkey
<< wxT("Clear" );
1283 hotkey
<< wxT("Menu" );
1286 hotkey
<< wxT("Pause" );
1289 hotkey
<< wxT("Capital" );
1292 hotkey
<< wxT("Select" );
1295 hotkey
<< wxT("Print" );
1298 hotkey
<< wxT("Execute" );
1301 hotkey
<< wxT("Snapshot" );
1304 hotkey
<< wxT("Help" );
1307 hotkey
<< wxT("Num_Lock" );
1310 hotkey
<< wxT("Scroll_Lock" );
1312 case WXK_NUMPAD_INSERT
:
1313 hotkey
<< wxT("KP_Insert" );
1315 case WXK_NUMPAD_DELETE
:
1316 hotkey
<< wxT("KP_Delete" );
1318 case WXK_NUMPAD_SPACE
:
1319 hotkey
<< wxT("KP_Space" );
1321 case WXK_NUMPAD_TAB
:
1322 hotkey
<< wxT("KP_Tab" );
1324 case WXK_NUMPAD_ENTER
:
1325 hotkey
<< wxT("KP_Enter" );
1327 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1329 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1331 case WXK_NUMPAD_HOME
:
1332 hotkey
<< wxT("KP_Home" );
1334 case WXK_NUMPAD_LEFT
:
1335 hotkey
<< wxT("KP_Left" );
1338 hotkey
<< wxT("KP_Up" );
1340 case WXK_NUMPAD_RIGHT
:
1341 hotkey
<< wxT("KP_Right" );
1343 case WXK_NUMPAD_DOWN
:
1344 hotkey
<< wxT("KP_Down" );
1346 case WXK_NUMPAD_PAGEUP
:
1347 hotkey
<< wxT("KP_Page_Up" );
1349 case WXK_NUMPAD_PAGEDOWN
:
1350 hotkey
<< wxT("KP_Page_Down" );
1352 case WXK_NUMPAD_END
:
1353 hotkey
<< wxT("KP_End" );
1355 case WXK_NUMPAD_BEGIN
:
1356 hotkey
<< wxT("KP_Begin" );
1358 case WXK_NUMPAD_EQUAL
:
1359 hotkey
<< wxT("KP_Equal" );
1361 case WXK_NUMPAD_MULTIPLY
:
1362 hotkey
<< wxT("KP_Multiply" );
1364 case WXK_NUMPAD_ADD
:
1365 hotkey
<< wxT("KP_Add" );
1367 case WXK_NUMPAD_SEPARATOR
:
1368 hotkey
<< wxT("KP_Separator" );
1370 case WXK_NUMPAD_SUBTRACT
:
1371 hotkey
<< wxT("KP_Subtract" );
1373 case WXK_NUMPAD_DECIMAL
:
1374 hotkey
<< wxT("KP_Decimal" );
1376 case WXK_NUMPAD_DIVIDE
:
1377 hotkey
<< wxT("KP_Divide" );
1379 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1380 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1381 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1382 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1384 case WXK_WINDOWS_LEFT
:
1385 hotkey
<< wxT("Super_L" );
1387 case WXK_WINDOWS_RIGHT
:
1388 hotkey
<< wxT("Super_R" );
1390 case WXK_WINDOWS_MENU
:
1391 hotkey
<< wxT("Menu" );
1394 hotkey
<< wxT("Command" );
1396 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1397 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1398 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1399 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1400 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1401 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1402 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1405 // if there are any other keys wxAcceleratorEntry::Create() may
1406 // return, we should process them here
1411 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1412 if ( !name
.empty() )
1419 wxFAIL_MSG( wxT("unknown keyboard accel") );
1428 #endif // wxUSE_ACCEL
1430 // ----------------------------------------------------------------------------
1431 // Pop-up menu stuff
1432 // ----------------------------------------------------------------------------
1434 #if wxUSE_MENUS_NATIVE
1436 extern "C" WXDLLIMPEXP_CORE
1437 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1439 *is_waiting
= false;
1442 extern "C" WXDLLIMPEXP_CORE
1443 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1445 gpointer user_data
)
1447 // ensure that the menu appears entirely on screen
1449 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1451 wxSize sizeScreen
= wxGetDisplaySize();
1452 wxPoint
*pos
= (wxPoint
*)user_data
;
1454 gint xmax
= sizeScreen
.x
- req
.width
,
1455 ymax
= sizeScreen
.y
- req
.height
;
1457 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1458 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1461 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1463 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1465 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1467 // NOTE: if you change this code, you need to update
1468 // the same code in taskbar.cpp as well. This
1469 // is ugly code duplication, I know.
1473 bool is_waiting
= true;
1475 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1477 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1478 (gpointer
)&is_waiting
);
1482 GtkMenuPositionFunc posfunc
;
1483 if ( x
== -1 && y
== -1 )
1485 // use GTK's default positioning algorithm
1491 pos
= ClientToScreen(wxPoint(x
, y
));
1493 posfunc
= wxPopupMenuPositionCallback
;
1496 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1497 DoCommonMenuCallbackCode(menu
, eventOpen
);
1500 GTK_MENU(menu
->m_menu
),
1501 NULL
, // parent menu shell
1502 NULL
, // parent menu item
1503 posfunc
, // function to position it
1504 userdata
, // client data
1505 0, // button used to activate it
1506 wxGtkTimeLastClick
// the time of activation
1511 gtk_main_iteration();
1514 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1516 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1517 DoCommonMenuCallbackCode(menu
, eventClose
);
1522 #endif // wxUSE_MENUS_NATIVE