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"
28 #include "wx/gtk1/private/mnemonics.h"
30 #include <gdk/gdkkeysyms.h>
32 #define ACCEL_OBJECT GtkObject
33 #define ACCEL_OBJECTS(a) (a)->attach_objects
34 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
36 // we use normal item but with a special id for the menu title
37 static const int wxGTK_TITLE_ID
= -3;
39 // defined in window.cpp
40 extern guint32 wxGtkTimeLastClick
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern void wxapp_install_idle_handler();
50 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 static wxString
wxReplaceUnderscore( const wxString
& title
)
61 // GTK 1.2 wants to have "_" instead of "&" for accelerators
64 while (*pc
!= wxT('\0'))
66 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
68 // "&" is doubled to indicate "&" instead of accelerator
72 else if (*pc
== wxT('&'))
78 if ( *pc
== wxT('_') )
80 // underscores must be doubled to prevent them from being
81 // interpreted as accelerator character prefix by GTK
90 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
95 static wxString
wxConvertFromGTKToWXLabel(const wxString
& gtkLabel
)
98 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
100 // '_' is the escape character for GTK+.
102 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
104 // An underscore was escaped.
108 else if ( *pc
== wxT('_') )
110 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
113 else if ( *pc
== wxT('&') )
115 // Double the ampersand to escape it as far as wxWidgets is concerned
120 // don't remove ampersands '&' since if we have them in the menu title
121 // it means that they were doubled to indicate "&" instead of accelerator
130 //-----------------------------------------------------------------------------
131 // activate message from GTK
132 //-----------------------------------------------------------------------------
134 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
137 wxapp_install_idle_handler();
139 event
.SetEventObject( menu
);
141 wxEvtHandler
* handler
= menu
->GetEventHandler();
142 if (handler
&& handler
->ProcessEvent(event
))
145 wxWindow
*win
= menu
->GetWindow();
147 win
->HandleWindowEvent( event
);
152 static void gtk_menu_open_callback( GtkWidget
*WXUNUSED(widget
), wxMenu
*menu
)
154 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
156 DoCommonMenuCallbackCode(menu
, event
);
159 static void gtk_menu_close_callback( GtkWidget
*WXUNUSED(widget
), wxMenuBar
*menubar
)
161 if ( !menubar
->GetMenuCount() )
163 // if menubar is empty we can't call GetMenu(0) below
167 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
169 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
180 // the parent window is known after wxFrame::SetMenu()
181 m_needParent
= false;
184 if (!PreCreation( NULL
, wxDefaultPosition
, wxDefaultSize
) ||
185 !CreateBase( NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
187 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
191 m_menubar
= gtk_menu_bar_new();
192 m_accel
= gtk_accel_group_new();
194 if (style
& wxMB_DOCKABLE
)
196 m_widget
= gtk_handle_box_new();
197 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
198 gtk_widget_show( GTK_WIDGET(m_menubar
) );
202 m_widget
= GTK_WIDGET(m_menubar
);
209 for (size_t i
= 0; i
< n
; ++i
)
210 Append(menus
[i
], titles
[i
]);
212 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
213 // don't get it when the menu is dismissed by clicking outside the
214 // toolbar) so we connect to the global one, even if it means that we
215 // can't pass the menu which was closed in wxMenuEvent object
216 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
218 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
223 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
225 Init(n
, menus
, titles
, style
);
228 wxMenuBar::wxMenuBar(long style
)
230 Init(0, NULL
, NULL
, style
);
233 wxMenuBar::wxMenuBar()
235 Init(0, NULL
, NULL
, 0);
238 wxMenuBar::~wxMenuBar()
242 static void DetachFromFrame( wxMenu
*menu
, wxWindow
*win
)
244 wxWindow
*top_frame
= win
;
245 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
246 top_frame
= top_frame
->GetParent();
248 // support for native hot keys
249 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
251 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
254 wxMenuItem
*menuitem
= node
->GetData();
255 if (menuitem
->IsSubMenu())
256 DetachFromFrame( menuitem
->GetSubMenu(), win
);
257 node
= node
->GetNext();
261 static void AttachToFrame( wxMenu
*menu
, wxWindow
*win
)
263 wxWindow
*top_frame
= win
;
264 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
265 top_frame
= top_frame
->GetParent();
267 // support for native hot keys
268 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
269 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
270 gtk_accel_group_attach( menu
->m_accel
, obj
);
272 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
275 wxMenuItem
*menuitem
= node
->GetData();
276 if (menuitem
->IsSubMenu())
277 AttachToFrame( menuitem
->GetSubMenu(), win
);
278 node
= node
->GetNext();
282 void wxMenuBar::Attach( wxFrame
*win
)
284 wxMenuBarBase::Attach(win
);
286 wxWindow
*top_frame
= win
;
287 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
288 top_frame
= top_frame
->GetParent();
290 // support for native key accelerators indicated by underscroes
291 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
292 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
293 gtk_accel_group_attach( m_accel
, obj
);
295 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
298 wxMenu
*menu
= node
->GetData();
299 AttachToFrame( menu
, win
);
300 node
= node
->GetNext();
304 void wxMenuBar::Detach()
306 wxWindow
*top_frame
= m_menuBarFrame
;
307 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
308 top_frame
= top_frame
->GetParent();
310 // support for native key accelerators indicated by underscroes
311 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
313 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
316 wxMenu
*menu
= node
->GetData();
317 DetachFromFrame( menu
, top_frame
);
318 node
= node
->GetNext();
321 wxMenuBarBase::Detach();
324 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
326 if ( !wxMenuBarBase::Append( menu
, title
) )
329 return GtkAppend(menu
, title
);
332 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
334 wxString
str( wxReplaceUnderscore( title
) );
336 // This doesn't have much effect right now.
337 menu
->SetTitle( str
);
339 // The "m_owner" is the "menu item"
340 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
341 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
343 gtk_label_set_text( label
, wxGTK_CONV( str
) );
345 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
346 if (accel_key
!= GDK_VoidSymbol
)
348 gtk_widget_add_accelerator (menu
->m_owner
,
350 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
356 gtk_widget_show( menu
->m_owner
);
358 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
361 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
363 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
365 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
366 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
371 AttachToFrame( menu
, m_menuBarFrame
);
373 // OPTIMISE ME: we should probably cache this, or pass it
374 // directly, but for now this is a minimal
375 // change to validate the new dynamic sizing.
376 // see (and refactor :) similar code in Remove
379 m_menuBarFrame
->UpdateMenuBarSize();
385 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
387 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
392 if ( !GtkAppend(menu
, title
, (int)pos
) )
398 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
400 // remove the old item and insert a new one
401 wxMenu
*menuOld
= Remove(pos
);
402 if ( menuOld
&& !Insert(pos
, menu
, title
) )
407 // either Insert() succeeded or Remove() failed and menuOld is NULL
411 wxMenu
*wxMenuBar::Remove(size_t pos
)
413 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
417 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
418 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
420 gtk_widget_destroy( menu
->m_owner
);
421 menu
->m_owner
= NULL
;
425 // OPTIMISE ME: see comment in GtkAppend
426 m_menuBarFrame
->UpdateMenuBarSize();
432 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
434 if (wxMenuItem::GetLabelText(menu
->GetTitle()) == wxMenuItem::GetLabelText(menuString
))
436 int res
= menu
->FindItem( itemString
);
437 if (res
!= wxNOT_FOUND
)
441 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
444 wxMenuItem
*item
= node
->GetData();
445 if (item
->IsSubMenu())
446 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
448 node
= node
->GetNext();
454 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
456 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
459 wxMenu
*menu
= node
->GetData();
460 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
463 node
= node
->GetNext();
469 // Find a wxMenuItem using its id. Recurses down into sub-menus
470 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
472 wxMenuItem
* result
= menu
->FindChildItem(id
);
474 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
475 while ( node
&& result
== NULL
)
477 wxMenuItem
*item
= node
->GetData();
478 if (item
->IsSubMenu())
480 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
482 node
= node
->GetNext();
488 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
490 wxMenuItem
* result
= 0;
491 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
492 while (node
&& result
== 0)
494 wxMenu
*menu
= node
->GetData();
495 result
= FindMenuItemByIdRecursive( menu
, id
);
496 node
= node
->GetNext();
501 *menuForItem
= result
? result
->GetMenu() : NULL
;
507 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
509 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
511 wxCHECK_RET( node
, wxT("menu not found") );
513 wxMenu
* menu
= node
->GetData();
516 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
519 wxString
wxMenuBar::GetMenuLabel( size_t pos
) const
521 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
523 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
525 wxMenu
* menu
= node
->GetData();
527 return menu
->GetTitle();
530 void wxMenuBar::SetMenuLabel( size_t pos
, const wxString
& label
)
532 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
534 wxCHECK_RET( node
, wxT("menu not found") );
536 wxMenu
* menu
= node
->GetData();
538 const wxString
str( wxReplaceUnderscore( label
) );
540 menu
->SetTitle( str
);
544 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
547 gtk_label_set( glabel
, wxGTK_CONV( str
) );
549 /* reparse key accel */
550 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
551 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
556 //-----------------------------------------------------------------------------
558 //-----------------------------------------------------------------------------
561 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
564 wxapp_install_idle_handler();
566 int id
= menu
->FindMenuIdByMenuItem(widget
);
568 /* should find it for normal (not popup) menu */
569 wxASSERT_MSG( (id
!= -1) || (menu
->GetWindow() != NULL
),
570 wxT("menu item not found in gtk_menu_clicked_callback") );
572 if (!menu
->IsEnabled(id
))
575 wxMenuItem
* item
= menu
->FindChildItem( id
);
576 wxCHECK_RET( item
, wxT("error in menu item callback") );
578 if ( item
->GetId() == wxGTK_TITLE_ID
)
580 // ignore events from the menu title
584 if (item
->IsCheckable())
586 bool isReallyChecked
= item
->IsChecked(),
587 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
589 // ensure that the internal state is always consistent with what is
590 // shown on the screen
591 item
->wxMenuItemBase::Check(isReallyChecked
);
593 // we must not report the events for the radio button going up nor the
594 // events resulting from the calls to wxMenuItem::Check()
595 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
596 (isInternallyChecked
== isReallyChecked
) )
603 // Is this menu on a menubar? (possibly nested)
604 wxFrame
* frame
= NULL
;
605 if(menu
->IsAttached())
606 frame
= menu
->GetMenuBar()->GetFrame();
608 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
609 // normally wxMenu::SendEvent() should be enough, if it doesn't work
610 // in wxGTK then we have a bug in wxMenu::GetWindow() which
611 // should be fixed instead of working around it here...
614 // If it is attached then let the frame send the event.
615 // Don't call frame->ProcessCommand(id) because it toggles
616 // checkable items and we've already done that above.
617 wxCommandEvent
commandEvent(wxEVT_MENU
, id
);
618 commandEvent
.SetEventObject(frame
);
619 if (item
->IsCheckable())
620 commandEvent
.SetInt(item
->IsChecked());
621 commandEvent
.SetEventObject(menu
);
623 frame
->HandleWindowEvent(commandEvent
);
627 // otherwise let the menu have it
628 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
633 //-----------------------------------------------------------------------------
635 //-----------------------------------------------------------------------------
638 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
640 if (g_isIdle
) wxapp_install_idle_handler();
642 int id
= menu
->FindMenuIdByMenuItem(widget
);
644 wxASSERT( id
!= -1 ); // should find it!
646 if (!menu
->IsEnabled(id
))
649 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
650 event
.SetEventObject( menu
);
652 wxEvtHandler
* handler
= menu
->GetEventHandler();
653 if (handler
&& handler
->ProcessEvent(event
))
656 wxWindow
*win
= menu
->GetWindow();
657 if (win
) win
->HandleWindowEvent( event
);
661 //-----------------------------------------------------------------------------
663 //-----------------------------------------------------------------------------
666 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
668 if (g_isIdle
) wxapp_install_idle_handler();
670 int id
= menu
->FindMenuIdByMenuItem(widget
);
672 wxASSERT( id
!= -1 ); // should find it!
674 if (!menu
->IsEnabled(id
))
677 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
678 event
.SetEventObject( menu
);
680 wxEvtHandler
* handler
= menu
->GetEventHandler();
681 if (handler
&& handler
->ProcessEvent(event
))
684 wxWindow
*win
= menu
->GetWindow();
686 win
->HandleWindowEvent( event
);
690 //-----------------------------------------------------------------------------
692 //-----------------------------------------------------------------------------
694 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
696 const wxString
& name
,
697 const wxString
& help
,
701 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
704 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
706 const wxString
& text
,
707 const wxString
& help
,
710 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
715 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
717 const wxString
& text
,
718 const wxString
& help
,
721 : wxMenuItemBase(parentMenu
, id
, text
, help
,
722 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
727 void wxMenuItem::Init()
729 m_labelWidget
= NULL
;
735 wxMenuItem::~wxMenuItem()
737 // don't delete menu items, the menus take care of that
740 wxString
wxMenuItem::GetItemLabel() const
742 wxString label
= wxConvertFromGTKToWXLabel(m_text
);
743 if (!m_hotKey
.IsEmpty())
744 label
= label
+ wxT("\t") + m_hotKey
;
748 void wxMenuItem::SetItemLabel( const wxString
& string
)
750 wxString str
= string
;
751 if ( str
.empty() && !IsSeparator() )
753 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
754 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
755 wxSTOCK_WITH_MNEMONIC
);
758 // Some optimization to avoid flicker
759 wxString oldLabel
= m_text
;
760 oldLabel
= wxStripMenuCodes(oldLabel
);
761 oldLabel
.Replace(wxT("_"), wxEmptyString
);
762 wxString label1
= wxStripMenuCodes(str
);
763 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
764 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
768 if (oldLabel
== label1
&&
769 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
776 label
= (GtkLabel
*) m_labelWidget
;
778 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
781 gtk_label_set( label
, wxGTK_CONV( m_text
) );
784 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
785 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
789 GdkModifierType accel_mods
;
790 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
793 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
794 m_parentMenu
->m_accel
,
799 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
800 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
803 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
805 m_parentMenu
->m_accel
,
812 // it's valid for this function to be called even if m_menuItem == NULL
813 void wxMenuItem::DoSetText( const wxString
& str
)
815 // '\t' is the deliminator indicating a hot key
817 text
.reserve(str
.length());
819 const wxChar
*pc
= str
;
820 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
822 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
824 // "&" is doubled to indicate "&" instead of accelerator
828 else if (*pc
== wxT('&'))
832 else if ( *pc
== wxT('_') ) // escape underscores
843 m_hotKey
= wxEmptyString
;
845 if ( *pc
== wxT('\t') )
856 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
864 // accelerator parsing code looks for them after a TAB, so insert a dummy
867 label
<< wxT('\t') << GetHotKey();
869 return wxAcceleratorEntry::Create(label
);
872 #endif // wxUSE_ACCEL
874 void wxMenuItem::Check( bool check
)
876 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
878 if (check
== m_isChecked
)
881 wxMenuItemBase::Check( check
);
887 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
891 wxFAIL_MSG( wxT("can't check this item") );
895 void wxMenuItem::Enable( bool enable
)
897 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
899 gtk_widget_set_sensitive( m_menuItem
, enable
);
900 wxMenuItemBase::Enable( enable
);
903 bool wxMenuItem::IsChecked() const
905 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
907 wxCHECK_MSG( IsCheckable(), false,
908 wxT("can't get state of uncheckable item!") );
910 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
913 //-----------------------------------------------------------------------------
915 //-----------------------------------------------------------------------------
919 m_accel
= gtk_accel_group_new();
920 m_menu
= gtk_menu_new();
921 // NB: keep reference to the menu so that it is not destroyed behind
922 // our back by GTK+ e.g. when it is removed from menubar:
923 gtk_widget_ref(m_menu
);
927 // Tearoffs are entries, just like separators. So if we want this
928 // menu to be a tear-off one, we just append a tearoff entry
930 if ( m_style
& wxMENU_TEAROFF
)
932 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
934 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
939 // append the title as the very first entry if we have it
940 if ( !m_title
.empty() )
942 Append(wxGTK_TITLE_ID
, m_title
);
949 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
951 if ( GTK_IS_WIDGET( m_menu
))
954 gtk_widget_unref( m_menu
);
955 // if the menu is inserted in another menu at this time, there was
956 // one more reference to it:
958 gtk_widget_destroy( m_menu
);
962 wxString
wxMenu::GetTitle() const
964 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
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().IsOk())
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