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 //-----------------------------------------------------------------------------
95 // activate message from GTK
96 //-----------------------------------------------------------------------------
98 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
101 wxapp_install_idle_handler();
103 event
.SetEventObject( menu
);
105 wxEvtHandler
* handler
= menu
->GetEventHandler();
106 if (handler
&& handler
->ProcessEvent(event
))
109 wxWindow
*win
= menu
->GetInvokingWindow();
111 win
->GetEventHandler()->ProcessEvent( event
);
116 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
118 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
120 DoCommonMenuCallbackCode(menu
, event
);
123 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
125 if ( !menubar
->GetMenuCount() )
127 // if menubar is empty we can't call GetMenu(0) below
131 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
133 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
144 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
146 // the parent window is known after wxFrame::SetMenu()
147 m_needParent
= false;
149 m_invokingWindow
= (wxWindow
*) NULL
;
151 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
152 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
154 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
158 m_menubar
= gtk_menu_bar_new();
159 m_accel
= gtk_accel_group_new();
161 if (style
& wxMB_DOCKABLE
)
163 m_widget
= gtk_handle_box_new();
164 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
165 gtk_widget_show( GTK_WIDGET(m_menubar
) );
169 m_widget
= GTK_WIDGET(m_menubar
);
176 for (size_t i
= 0; i
< n
; ++i
)
177 Append(menus
[i
], titles
[i
]);
179 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
180 // don't get it when the menu is dismissed by clicking outside the
181 // toolbar) so we connect to the global one, even if it means that we
182 // can't pass the menu which was closed in wxMenuEvent object
183 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
185 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
190 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
192 Init(n
, menus
, titles
, style
);
195 wxMenuBar::wxMenuBar(long style
)
197 Init(0, NULL
, NULL
, style
);
200 wxMenuBar::wxMenuBar()
202 Init(0, NULL
, NULL
, 0);
205 wxMenuBar::~wxMenuBar()
209 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
211 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
213 wxWindow
*top_frame
= win
;
214 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
215 top_frame
= top_frame
->GetParent();
217 // support for native hot keys
218 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
220 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
223 wxMenuItem
*menuitem
= node
->GetData();
224 if (menuitem
->IsSubMenu())
225 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
226 node
= node
->GetNext();
230 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
232 menu
->SetInvokingWindow( win
);
234 wxWindow
*top_frame
= win
;
235 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
236 top_frame
= top_frame
->GetParent();
238 // support for native hot keys
239 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
240 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
241 gtk_accel_group_attach( menu
->m_accel
, obj
);
243 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
246 wxMenuItem
*menuitem
= node
->GetData();
247 if (menuitem
->IsSubMenu())
248 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
249 node
= node
->GetNext();
253 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
255 m_invokingWindow
= win
;
256 wxWindow
*top_frame
= win
;
257 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
258 top_frame
= top_frame
->GetParent();
260 // support for native key accelerators indicated by underscroes
261 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
262 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
263 gtk_accel_group_attach( m_accel
, obj
);
265 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
268 wxMenu
*menu
= node
->GetData();
269 wxMenubarSetInvokingWindow( menu
, win
);
270 node
= node
->GetNext();
274 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
276 m_invokingWindow
= (wxWindow
*) NULL
;
277 wxWindow
*top_frame
= win
;
278 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
279 top_frame
= top_frame
->GetParent();
281 // support for native key accelerators indicated by underscroes
282 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
284 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
287 wxMenu
*menu
= node
->GetData();
288 wxMenubarUnsetInvokingWindow( menu
, win
);
289 node
= node
->GetNext();
293 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
295 if ( !wxMenuBarBase::Append( menu
, title
) )
298 return GtkAppend(menu
, title
);
301 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
303 wxString
str( wxReplaceUnderscore( title
) );
305 // This doesn't have much effect right now.
306 menu
->SetTitle( str
);
308 // The "m_owner" is the "menu item"
309 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
310 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
312 gtk_label_set_text( label
, wxGTK_CONV( str
) );
314 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
315 if (accel_key
!= GDK_VoidSymbol
)
317 gtk_widget_add_accelerator (menu
->m_owner
,
319 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
325 gtk_widget_show( menu
->m_owner
);
327 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
330 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
332 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
334 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
335 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
338 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
339 // addings menu later on.
340 if (m_invokingWindow
)
342 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
344 // OPTIMISE ME: we should probably cache this, or pass it
345 // directly, but for now this is a minimal
346 // change to validate the new dynamic sizing.
347 // see (and refactor :) similar code in Remove
350 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
353 frame
->UpdateMenuBarSize();
359 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
361 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
366 if ( !GtkAppend(menu
, title
, (int)pos
) )
372 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
374 // remove the old item and insert a new one
375 wxMenu
*menuOld
= Remove(pos
);
376 if ( menuOld
&& !Insert(pos
, menu
, title
) )
378 return (wxMenu
*) NULL
;
381 // either Insert() succeeded or Remove() failed and menuOld is NULL
385 wxMenu
*wxMenuBar::Remove(size_t pos
)
387 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
389 return (wxMenu
*) NULL
;
391 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
392 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
394 gtk_widget_destroy( menu
->m_owner
);
395 menu
->m_owner
= NULL
;
397 if (m_invokingWindow
)
399 // OPTIMISE ME: see comment in GtkAppend
400 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
403 frame
->UpdateMenuBarSize();
409 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
411 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
413 int res
= menu
->FindItem( itemString
);
414 if (res
!= wxNOT_FOUND
)
418 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
421 wxMenuItem
*item
= node
->GetData();
422 if (item
->IsSubMenu())
423 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
425 node
= node
->GetNext();
431 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
433 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
436 wxMenu
*menu
= node
->GetData();
437 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
440 node
= node
->GetNext();
446 // Find a wxMenuItem using its id. Recurses down into sub-menus
447 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
449 wxMenuItem
* result
= menu
->FindChildItem(id
);
451 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
452 while ( node
&& result
== NULL
)
454 wxMenuItem
*item
= node
->GetData();
455 if (item
->IsSubMenu())
457 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
459 node
= node
->GetNext();
465 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
467 wxMenuItem
* result
= 0;
468 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
469 while (node
&& result
== 0)
471 wxMenu
*menu
= node
->GetData();
472 result
= FindMenuItemByIdRecursive( menu
, id
);
473 node
= node
->GetNext();
478 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
484 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
486 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
488 wxCHECK_RET( node
, wxT("menu not found") );
490 wxMenu
* menu
= node
->GetData();
493 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
496 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
498 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
500 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
502 wxMenu
* menu
= node
->GetData();
505 wxString
text( menu
->GetTitle() );
506 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
508 if ( *pc
== wxT('_') )
510 // '_' is the escape character for GTK+
514 // don't remove ampersands '&' since if we have them in the menu title
515 // it means that they were doubled to indicate "&" instead of accelerator
523 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
525 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
527 wxCHECK_RET( node
, wxT("menu not found") );
529 wxMenu
* menu
= node
->GetData();
531 const wxString
str( wxReplaceUnderscore( label
) );
533 menu
->SetTitle( str
);
537 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
540 gtk_label_set( glabel
, wxGTK_CONV( str
) );
542 /* reparse key accel */
543 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
544 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
549 //-----------------------------------------------------------------------------
551 //-----------------------------------------------------------------------------
554 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
557 wxapp_install_idle_handler();
559 int id
= menu
->FindMenuIdByMenuItem(widget
);
561 /* should find it for normal (not popup) menu */
562 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
563 _T("menu item not found in gtk_menu_clicked_callback") );
565 if (!menu
->IsEnabled(id
))
568 wxMenuItem
* item
= menu
->FindChildItem( id
);
569 wxCHECK_RET( item
, wxT("error in menu item callback") );
571 if ( item
->GetId() == wxGTK_TITLE_ID
)
573 // ignore events from the menu title
577 if (item
->IsCheckable())
579 bool isReallyChecked
= item
->IsChecked(),
580 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
582 // ensure that the internal state is always consistent with what is
583 // shown on the screen
584 item
->wxMenuItemBase::Check(isReallyChecked
);
586 // we must not report the events for the radio button going up nor the
587 // events resulting from the calls to wxMenuItem::Check()
588 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
589 (isInternallyChecked
== isReallyChecked
) )
596 // Is this menu on a menubar? (possibly nested)
597 wxFrame
* frame
= NULL
;
598 if(menu
->IsAttached())
599 frame
= menu
->GetMenuBar()->GetFrame();
601 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
602 // normally wxMenu::SendEvent() should be enough, if it doesn't work
603 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
604 // should be fixed instead of working around it here...
607 // If it is attached then let the frame send the event.
608 // Don't call frame->ProcessCommand(id) because it toggles
609 // checkable items and we've already done that above.
610 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
611 commandEvent
.SetEventObject(frame
);
612 if (item
->IsCheckable())
613 commandEvent
.SetInt(item
->IsChecked());
614 commandEvent
.SetEventObject(menu
);
616 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
620 // otherwise let the menu have it
621 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
626 //-----------------------------------------------------------------------------
628 //-----------------------------------------------------------------------------
631 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
633 if (g_isIdle
) wxapp_install_idle_handler();
635 int id
= menu
->FindMenuIdByMenuItem(widget
);
637 wxASSERT( id
!= -1 ); // should find it!
639 if (!menu
->IsEnabled(id
))
642 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
643 event
.SetEventObject( menu
);
645 wxEvtHandler
* handler
= menu
->GetEventHandler();
646 if (handler
&& handler
->ProcessEvent(event
))
649 wxWindow
*win
= menu
->GetInvokingWindow();
650 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
654 //-----------------------------------------------------------------------------
656 //-----------------------------------------------------------------------------
659 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
661 if (g_isIdle
) wxapp_install_idle_handler();
663 int id
= menu
->FindMenuIdByMenuItem(widget
);
665 wxASSERT( id
!= -1 ); // should find it!
667 if (!menu
->IsEnabled(id
))
670 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
671 event
.SetEventObject( menu
);
673 wxEvtHandler
* handler
= menu
->GetEventHandler();
674 if (handler
&& handler
->ProcessEvent(event
))
677 wxWindow
*win
= menu
->GetInvokingWindow();
679 win
->GetEventHandler()->ProcessEvent( event
);
683 //-----------------------------------------------------------------------------
685 //-----------------------------------------------------------------------------
687 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
689 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
691 const wxString
& name
,
692 const wxString
& help
,
696 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
699 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
701 const wxString
& text
,
702 const wxString
& help
,
705 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
710 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
712 const wxString
& text
,
713 const wxString
& help
,
716 : wxMenuItemBase(parentMenu
, id
, text
, help
,
717 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
722 void wxMenuItem::Init()
724 m_labelWidget
= (GtkWidget
*) NULL
;
725 m_menuItem
= (GtkWidget
*) NULL
;
730 wxMenuItem::~wxMenuItem()
732 // don't delete menu items, the menus take care of that
735 // return the menu item text without any menu accels
737 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
741 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
743 if ( *pc
== wxT('\t'))
746 if ( *pc
== wxT('_') )
748 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
754 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
757 // "&" is doubled to indicate "&" instead of accelerator
764 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
769 void wxMenuItem::SetText( const wxString
& string
)
771 wxString str
= string
;
772 if ( str
.empty() && !IsSeparator() )
774 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
775 str
= wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR
|
776 wxSTOCK_WITH_MNEMONIC
);
779 // Some optimization to avoid flicker
780 wxString oldLabel
= m_text
;
781 oldLabel
= wxStripMenuCodes(oldLabel
);
782 oldLabel
.Replace(wxT("_"), wxEmptyString
);
783 wxString label1
= wxStripMenuCodes(str
);
784 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
785 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
789 if (oldLabel
== label1
&&
790 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
797 label
= (GtkLabel
*) m_labelWidget
;
799 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
802 gtk_label_set( label
, wxGTK_CONV( m_text
) );
805 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
806 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
810 GdkModifierType accel_mods
;
811 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
814 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
815 m_parentMenu
->m_accel
,
820 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
821 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
824 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
826 m_parentMenu
->m_accel
,
833 // it's valid for this function to be called even if m_menuItem == NULL
834 void wxMenuItem::DoSetText( const wxString
& str
)
836 // '\t' is the deliminator indicating a hot key
838 text
.reserve(str
.length());
840 const wxChar
*pc
= str
;
841 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
843 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
845 // "&" is doubled to indicate "&" instead of accelerator
849 else if (*pc
== wxT('&'))
853 else if ( *pc
== wxT('_') ) // escape underscores
864 m_hotKey
= wxEmptyString
;
866 if ( *pc
== wxT('\t') )
877 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
882 return (wxAcceleratorEntry
*)NULL
;
885 // accelerator parsing code looks for them after a TAB, so insert a dummy
888 label
<< wxT('\t') << GetHotKey();
890 return wxAcceleratorEntry::Create(label
);
893 #endif // wxUSE_ACCEL
895 void wxMenuItem::Check( bool check
)
897 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
899 if (check
== m_isChecked
)
902 wxMenuItemBase::Check( check
);
908 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
912 wxFAIL_MSG( _T("can't check this item") );
916 void wxMenuItem::Enable( bool enable
)
918 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
920 gtk_widget_set_sensitive( m_menuItem
, enable
);
921 wxMenuItemBase::Enable( enable
);
924 bool wxMenuItem::IsChecked() const
926 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
928 wxCHECK_MSG( IsCheckable(), false,
929 wxT("can't get state of uncheckable item!") );
931 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
934 //-----------------------------------------------------------------------------
936 //-----------------------------------------------------------------------------
938 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
942 m_accel
= gtk_accel_group_new();
943 m_menu
= gtk_menu_new();
944 // NB: keep reference to the menu so that it is not destroyed behind
945 // our back by GTK+ e.g. when it is removed from menubar:
946 gtk_widget_ref(m_menu
);
948 m_owner
= (GtkWidget
*) NULL
;
950 // Tearoffs are entries, just like separators. So if we want this
951 // menu to be a tear-off one, we just append a tearoff entry
953 if ( m_style
& wxMENU_TEAROFF
)
955 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
957 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
962 // append the title as the very first entry if we have it
963 if ( !m_title
.empty() )
965 Append(wxGTK_TITLE_ID
, m_title
);
972 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
974 if ( GTK_IS_WIDGET( m_menu
))
977 gtk_widget_unref( m_menu
);
978 // if the menu is inserted in another menu at this time, there was
979 // one more reference to it:
981 gtk_widget_destroy( m_menu
);
985 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
990 GtkLabel
* label
= NULL
;
992 if ( mitem
->IsSeparator() )
995 menuItem
= gtk_menu_item_new();
997 else if (mitem
->GetBitmap().Ok())
999 text
= mitem
->GetText();
1000 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
1003 wxUnusedVar(bitmap
);
1004 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1005 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1009 else // a normal item
1011 // text has "_" instead of "&" after mitem->SetText() so don't use it
1012 text
= mitem
->GetText() ;
1014 switch ( mitem
->GetKind() )
1018 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1019 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1021 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1028 GSList
*group
= NULL
;
1029 if ( m_prevRadio
== NULL
)
1031 // start of a new radio group
1032 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1033 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1035 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1037 else // continue the radio group
1039 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1040 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1041 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1047 wxFAIL_MSG( _T("unexpected menu item kind") );
1052 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1053 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1062 GdkModifierType accel_mods
;
1063 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1065 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1066 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1069 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1078 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1080 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1082 gtk_widget_show( menuItem
);
1084 if ( !mitem
->IsSeparator() )
1086 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1088 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1089 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1092 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1093 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1096 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1098 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1100 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1102 // if adding a submenu to a menu already existing in the menu bar, we
1103 // must set invoking window to allow processing events from this
1105 if ( m_invokingWindow
)
1106 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1110 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1111 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1115 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1116 if (accel_key
!= GDK_VoidSymbol
)
1118 gtk_widget_add_accelerator (menuItem
,
1120 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1127 mitem
->SetMenuItem(menuItem
);
1131 // This doesn't even exist!
1132 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1138 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1140 if (!GtkAppend(mitem
))
1143 return wxMenuBase::DoAppend(mitem
);
1146 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1148 if ( !wxMenuBase::DoInsert(pos
, item
) )
1152 if ( !GtkAppend(item
, (int)pos
) )
1158 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1160 if ( !wxMenuBase::DoRemove(item
) )
1161 return (wxMenuItem
*)NULL
;
1163 // TODO: this code doesn't delete the item factory item and this seems
1164 // impossible as of GTK 1.2.6.
1165 gtk_widget_destroy( item
->GetMenuItem() );
1170 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1172 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1175 wxMenuItem
*item
= node
->GetData();
1176 if (item
->GetMenuItem() == menuItem
)
1177 return item
->GetId();
1178 node
= node
->GetNext();
1184 // ----------------------------------------------------------------------------
1186 // ----------------------------------------------------------------------------
1190 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1194 wxAcceleratorEntry
*accel
= item
.GetAccel();
1197 int flags
= accel
->GetFlags();
1198 if ( flags
& wxACCEL_ALT
)
1199 hotkey
+= wxT("<alt>");
1200 if ( flags
& wxACCEL_CTRL
)
1201 hotkey
+= wxT("<control>");
1202 if ( flags
& wxACCEL_SHIFT
)
1203 hotkey
+= wxT("<shift>");
1205 int code
= accel
->GetKeyCode();
1232 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1235 // TODO: we should use gdk_keyval_name() (a.k.a.
1236 // XKeysymToString) here as well as hardcoding the keysym
1237 // names this might be not portable
1239 hotkey
<< wxT("Insert" );
1242 hotkey
<< wxT("Delete" );
1245 hotkey
<< wxT("Up" );
1248 hotkey
<< wxT("Down" );
1251 hotkey
<< wxT("Page_Up" );
1254 hotkey
<< wxT("Page_Down" );
1257 hotkey
<< wxT("Left" );
1260 hotkey
<< wxT("Right" );
1263 hotkey
<< wxT("Home" );
1266 hotkey
<< wxT("End" );
1269 hotkey
<< wxT("Return" );
1272 hotkey
<< wxT("BackSpace" );
1275 hotkey
<< wxT("Tab" );
1278 hotkey
<< wxT("Esc" );
1281 hotkey
<< wxT("space" );
1284 hotkey
<< wxT("Multiply" );
1287 hotkey
<< wxT("Add" );
1290 hotkey
<< wxT("Separator" );
1293 hotkey
<< wxT("Subtract" );
1296 hotkey
<< wxT("Decimal" );
1299 hotkey
<< wxT("Divide" );
1302 hotkey
<< wxT("Cancel" );
1305 hotkey
<< wxT("Clear" );
1308 hotkey
<< wxT("Menu" );
1311 hotkey
<< wxT("Pause" );
1314 hotkey
<< wxT("Capital" );
1317 hotkey
<< wxT("Select" );
1320 hotkey
<< wxT("Print" );
1323 hotkey
<< wxT("Execute" );
1326 hotkey
<< wxT("Snapshot" );
1329 hotkey
<< wxT("Help" );
1332 hotkey
<< wxT("Num_Lock" );
1335 hotkey
<< wxT("Scroll_Lock" );
1337 case WXK_NUMPAD_INSERT
:
1338 hotkey
<< wxT("KP_Insert" );
1340 case WXK_NUMPAD_DELETE
:
1341 hotkey
<< wxT("KP_Delete" );
1343 case WXK_NUMPAD_SPACE
:
1344 hotkey
<< wxT("KP_Space" );
1346 case WXK_NUMPAD_TAB
:
1347 hotkey
<< wxT("KP_Tab" );
1349 case WXK_NUMPAD_ENTER
:
1350 hotkey
<< wxT("KP_Enter" );
1352 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1354 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1356 case WXK_NUMPAD_HOME
:
1357 hotkey
<< wxT("KP_Home" );
1359 case WXK_NUMPAD_LEFT
:
1360 hotkey
<< wxT("KP_Left" );
1363 hotkey
<< wxT("KP_Up" );
1365 case WXK_NUMPAD_RIGHT
:
1366 hotkey
<< wxT("KP_Right" );
1368 case WXK_NUMPAD_DOWN
:
1369 hotkey
<< wxT("KP_Down" );
1371 case WXK_NUMPAD_PAGEUP
:
1372 hotkey
<< wxT("KP_Page_Up" );
1374 case WXK_NUMPAD_PAGEDOWN
:
1375 hotkey
<< wxT("KP_Page_Down" );
1377 case WXK_NUMPAD_END
:
1378 hotkey
<< wxT("KP_End" );
1380 case WXK_NUMPAD_BEGIN
:
1381 hotkey
<< wxT("KP_Begin" );
1383 case WXK_NUMPAD_EQUAL
:
1384 hotkey
<< wxT("KP_Equal" );
1386 case WXK_NUMPAD_MULTIPLY
:
1387 hotkey
<< wxT("KP_Multiply" );
1389 case WXK_NUMPAD_ADD
:
1390 hotkey
<< wxT("KP_Add" );
1392 case WXK_NUMPAD_SEPARATOR
:
1393 hotkey
<< wxT("KP_Separator" );
1395 case WXK_NUMPAD_SUBTRACT
:
1396 hotkey
<< wxT("KP_Subtract" );
1398 case WXK_NUMPAD_DECIMAL
:
1399 hotkey
<< wxT("KP_Decimal" );
1401 case WXK_NUMPAD_DIVIDE
:
1402 hotkey
<< wxT("KP_Divide" );
1404 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1405 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1406 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1407 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1409 case WXK_WINDOWS_LEFT
:
1410 hotkey
<< wxT("Super_L" );
1412 case WXK_WINDOWS_RIGHT
:
1413 hotkey
<< wxT("Super_R" );
1415 case WXK_WINDOWS_MENU
:
1416 hotkey
<< wxT("Menu" );
1419 hotkey
<< wxT("Command" );
1421 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1422 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1423 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1424 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1425 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1426 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1427 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1430 // if there are any other keys wxAcceleratorEntry::Create() may
1431 // return, we should process them here
1436 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1437 if ( !name
.empty() )
1444 wxFAIL_MSG( wxT("unknown keyboard accel") );
1453 #endif // wxUSE_ACCEL
1455 // ----------------------------------------------------------------------------
1456 // Pop-up menu stuff
1457 // ----------------------------------------------------------------------------
1459 #if wxUSE_MENUS_NATIVE
1461 extern "C" WXDLLIMPEXP_CORE
1462 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1464 *is_waiting
= false;
1467 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1469 menu
->SetInvokingWindow( win
);
1471 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1474 wxMenuItem
*menuitem
= node
->GetData();
1475 if (menuitem
->IsSubMenu())
1477 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1480 node
= node
->GetNext();
1484 extern "C" WXDLLIMPEXP_CORE
1485 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1487 gpointer user_data
)
1489 // ensure that the menu appears entirely on screen
1491 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1493 wxSize sizeScreen
= wxGetDisplaySize();
1494 wxPoint
*pos
= (wxPoint
*)user_data
;
1496 gint xmax
= sizeScreen
.x
- req
.width
,
1497 ymax
= sizeScreen
.y
- req
.height
;
1499 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1500 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1503 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1505 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1507 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1509 // NOTE: if you change this code, you need to update
1510 // the same code in taskbar.cpp as well. This
1511 // is ugly code duplication, I know.
1513 SetInvokingWindow( menu
, this );
1517 bool is_waiting
= true;
1519 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1521 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1522 (gpointer
)&is_waiting
);
1526 GtkMenuPositionFunc posfunc
;
1527 if ( x
== -1 && y
== -1 )
1529 // use GTK's default positioning algorithm
1535 pos
= ClientToScreen(wxPoint(x
, y
));
1537 posfunc
= wxPopupMenuPositionCallback
;
1540 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1541 DoCommonMenuCallbackCode(menu
, eventOpen
);
1544 GTK_MENU(menu
->m_menu
),
1545 (GtkWidget
*) NULL
, // parent menu shell
1546 (GtkWidget
*) NULL
, // parent menu item
1547 posfunc
, // function to position it
1548 userdata
, // client data
1549 0, // button used to activate it
1550 wxGtkTimeLastClick
// the time of activation
1555 gtk_main_iteration();
1558 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1560 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1561 DoCommonMenuCallbackCode(menu
, eventClose
);
1566 #endif // wxUSE_MENUS_NATIVE