1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/bitmap.h"
23 #include "wx/gtk1/private.h"
25 #include <gdk/gdkkeysyms.h>
27 #define ACCEL_OBJECT GtkObject
28 #define ACCEL_OBJECTS(a) (a)->attach_objects
29 #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
31 // we use normal item but with a special id for the menu title
32 static const int wxGTK_TITLE_ID
= -3;
34 // defined in window.cpp
35 extern guint32 wxGtkTimeLastClick
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
45 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 static wxString
wxReplaceUnderscore( const wxString
& title
)
56 // GTK 1.2 wants to have "_" instead of "&" for accelerators
59 while (*pc
!= wxT('\0'))
61 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
63 // "&" is doubled to indicate "&" instead of accelerator
67 else if (*pc
== wxT('&'))
73 if ( *pc
== wxT('_') )
75 // underscores must be doubled to prevent them from being
76 // interpreted as accelerator character prefix by GTK
85 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
90 //-----------------------------------------------------------------------------
91 // activate message from GTK
92 //-----------------------------------------------------------------------------
94 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
97 wxapp_install_idle_handler();
99 event
.SetEventObject( menu
);
101 wxEvtHandler
* handler
= menu
->GetEventHandler();
102 if (handler
&& handler
->ProcessEvent(event
))
105 wxWindow
*win
= menu
->GetInvokingWindow();
107 win
->GetEventHandler()->ProcessEvent( event
);
112 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
114 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
116 DoCommonMenuCallbackCode(menu
, event
);
119 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
121 if ( !menubar
->GetMenuCount() )
123 // if menubar is empty we can't call GetMenu(0) below
127 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
129 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
140 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
142 // the parent window is known after wxFrame::SetMenu()
143 m_needParent
= FALSE
;
145 m_invokingWindow
= (wxWindow
*) NULL
;
147 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
148 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
150 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
154 m_menubar
= gtk_menu_bar_new();
155 m_accel
= gtk_accel_group_new();
157 if (style
& wxMB_DOCKABLE
)
159 m_widget
= gtk_handle_box_new();
160 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
161 gtk_widget_show( GTK_WIDGET(m_menubar
) );
165 m_widget
= GTK_WIDGET(m_menubar
);
172 for (size_t i
= 0; i
< n
; ++i
)
173 Append(menus
[i
], titles
[i
]);
175 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
176 // don't get it when the menu is dismissed by clicking outside the
177 // toolbar) so we connect to the global one, even if it means that we
178 // can't pass the menu which was closed in wxMenuEvent object
179 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar
)),
181 GTK_SIGNAL_FUNC(gtk_menu_close_callback
),
186 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
188 Init(n
, menus
, titles
, style
);
191 wxMenuBar::wxMenuBar(long style
)
193 Init(0, NULL
, NULL
, style
);
196 wxMenuBar::wxMenuBar()
198 Init(0, NULL
, NULL
, 0);
201 wxMenuBar::~wxMenuBar()
205 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
207 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
209 wxWindow
*top_frame
= win
;
210 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
211 top_frame
= top_frame
->GetParent();
213 // support for native hot keys
214 gtk_accel_group_detach( menu
->m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
216 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
219 wxMenuItem
*menuitem
= node
->GetData();
220 if (menuitem
->IsSubMenu())
221 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
222 node
= node
->GetNext();
226 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
228 menu
->SetInvokingWindow( win
);
230 wxWindow
*top_frame
= win
;
231 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
232 top_frame
= top_frame
->GetParent();
234 // support for native hot keys
235 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
236 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
237 gtk_accel_group_attach( menu
->m_accel
, obj
);
239 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
242 wxMenuItem
*menuitem
= node
->GetData();
243 if (menuitem
->IsSubMenu())
244 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
245 node
= node
->GetNext();
249 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
251 m_invokingWindow
= win
;
252 wxWindow
*top_frame
= win
;
253 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
254 top_frame
= top_frame
->GetParent();
256 // support for native key accelerators indicated by underscroes
257 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
258 if ( !g_slist_find( ACCEL_OBJECTS(m_accel
), obj
) )
259 gtk_accel_group_attach( m_accel
, obj
);
261 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
264 wxMenu
*menu
= node
->GetData();
265 wxMenubarSetInvokingWindow( menu
, win
);
266 node
= node
->GetNext();
270 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
272 m_invokingWindow
= (wxWindow
*) NULL
;
273 wxWindow
*top_frame
= win
;
274 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
275 top_frame
= top_frame
->GetParent();
277 // support for native key accelerators indicated by underscroes
278 gtk_accel_group_detach( m_accel
, ACCEL_OBJ_CAST(top_frame
->m_widget
) );
280 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
283 wxMenu
*menu
= node
->GetData();
284 wxMenubarUnsetInvokingWindow( menu
, win
);
285 node
= node
->GetNext();
289 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
291 if ( !wxMenuBarBase::Append( menu
, title
) )
294 return GtkAppend(menu
, title
);
297 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
299 wxString
str( wxReplaceUnderscore( title
) );
301 // This doesn't have much effect right now.
302 menu
->SetTitle( str
);
304 // The "m_owner" is the "menu item"
305 menu
->m_owner
= gtk_menu_item_new_with_label( wxGTK_CONV( str
) );
306 GtkLabel
*label
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
308 gtk_label_set_text( label
, wxGTK_CONV( str
) );
310 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( str
) );
311 if (accel_key
!= GDK_VoidSymbol
)
313 gtk_widget_add_accelerator (menu
->m_owner
,
315 m_accel
, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
321 gtk_widget_show( menu
->m_owner
);
323 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
326 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
328 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
330 gtk_signal_connect( GTK_OBJECT(menu
->m_owner
), "activate",
331 GTK_SIGNAL_FUNC(gtk_menu_open_callback
),
334 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
335 // addings menu later on.
336 if (m_invokingWindow
)
338 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
340 // OPTIMISE ME: we should probably cache this, or pass it
341 // directly, but for now this is a minimal
342 // change to validate the new dynamic sizing.
343 // see (and refactor :) similar code in Remove
346 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
349 frame
->UpdateMenuBarSize();
355 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
357 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
362 if ( !GtkAppend(menu
, title
, (int)pos
) )
368 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
370 // remove the old item and insert a new one
371 wxMenu
*menuOld
= Remove(pos
);
372 if ( menuOld
&& !Insert(pos
, menu
, title
) )
374 return (wxMenu
*) NULL
;
377 // either Insert() succeeded or Remove() failed and menuOld is NULL
381 wxMenu
*wxMenuBar::Remove(size_t pos
)
383 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
385 return (wxMenu
*) NULL
;
387 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
388 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
390 gtk_widget_destroy( menu
->m_owner
);
391 menu
->m_owner
= NULL
;
393 if (m_invokingWindow
)
395 // OPTIMISE ME: see comment in GtkAppend
396 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
399 frame
->UpdateMenuBarSize();
405 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
407 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
409 int res
= menu
->FindItem( itemString
);
410 if (res
!= wxNOT_FOUND
)
414 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
417 wxMenuItem
*item
= node
->GetData();
418 if (item
->IsSubMenu())
419 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
421 node
= node
->GetNext();
427 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
429 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
432 wxMenu
*menu
= node
->GetData();
433 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
436 node
= node
->GetNext();
442 // Find a wxMenuItem using its id. Recurses down into sub-menus
443 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
445 wxMenuItem
* result
= menu
->FindChildItem(id
);
447 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
448 while ( node
&& result
== NULL
)
450 wxMenuItem
*item
= node
->GetData();
451 if (item
->IsSubMenu())
453 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
455 node
= node
->GetNext();
461 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
463 wxMenuItem
* result
= 0;
464 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
465 while (node
&& result
== 0)
467 wxMenu
*menu
= node
->GetData();
468 result
= FindMenuItemByIdRecursive( menu
, id
);
469 node
= node
->GetNext();
474 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
480 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
482 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
484 wxCHECK_RET( node
, wxT("menu not found") );
486 wxMenu
* menu
= node
->GetData();
489 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
492 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
494 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
496 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
498 wxMenu
* menu
= node
->GetData();
501 wxString
text( menu
->GetTitle() );
502 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
504 if ( *pc
== wxT('_') )
506 // '_' is the escape character for GTK+
510 // don't remove ampersands '&' since if we have them in the menu title
511 // it means that they were doubled to indicate "&" instead of accelerator
519 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
521 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
523 wxCHECK_RET( node
, wxT("menu not found") );
525 wxMenu
* menu
= node
->GetData();
527 const wxString
str( wxReplaceUnderscore( label
) );
529 menu
->SetTitle( str
);
533 GtkLabel
*glabel
= GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
);
536 gtk_label_set( glabel
, wxGTK_CONV( str
) );
538 /* reparse key accel */
539 (void)gtk_label_parse_uline (GTK_LABEL(glabel
), wxGTK_CONV( str
) );
540 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel
) );
545 //-----------------------------------------------------------------------------
547 //-----------------------------------------------------------------------------
550 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
553 wxapp_install_idle_handler();
555 int id
= menu
->FindMenuIdByMenuItem(widget
);
557 /* should find it for normal (not popup) menu */
558 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
559 _T("menu item not found in gtk_menu_clicked_callback") );
561 if (!menu
->IsEnabled(id
))
564 wxMenuItem
* item
= menu
->FindChildItem( id
);
565 wxCHECK_RET( item
, wxT("error in menu item callback") );
567 if ( item
->GetId() == wxGTK_TITLE_ID
)
569 // ignore events from the menu title
573 if (item
->IsCheckable())
575 bool isReallyChecked
= item
->IsChecked(),
576 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
578 // ensure that the internal state is always consistent with what is
579 // shown on the screen
580 item
->wxMenuItemBase::Check(isReallyChecked
);
582 // we must not report the events for the radio button going up nor the
583 // events resulting from the calls to wxMenuItem::Check()
584 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
585 (isInternallyChecked
== isReallyChecked
) )
592 // Is this menu on a menubar? (possibly nested)
593 wxFrame
* frame
= NULL
;
594 if(menu
->IsAttached())
595 frame
= menu
->GetMenuBar()->GetFrame();
597 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
598 // normally wxMenu::SendEvent() should be enough, if it doesn't work
599 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
600 // should be fixed instead of working around it here...
603 // If it is attached then let the frame send the event.
604 // Don't call frame->ProcessCommand(id) because it toggles
605 // checkable items and we've already done that above.
606 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
607 commandEvent
.SetEventObject(frame
);
608 if (item
->IsCheckable())
609 commandEvent
.SetInt(item
->IsChecked());
610 commandEvent
.SetEventObject(menu
);
612 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
616 // otherwise let the menu have it
617 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
622 //-----------------------------------------------------------------------------
624 //-----------------------------------------------------------------------------
627 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
629 if (g_isIdle
) wxapp_install_idle_handler();
631 int id
= menu
->FindMenuIdByMenuItem(widget
);
633 wxASSERT( id
!= -1 ); // should find it!
635 if (!menu
->IsEnabled(id
))
638 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
639 event
.SetEventObject( menu
);
641 wxEvtHandler
* handler
= menu
->GetEventHandler();
642 if (handler
&& handler
->ProcessEvent(event
))
645 wxWindow
*win
= menu
->GetInvokingWindow();
646 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
650 //-----------------------------------------------------------------------------
652 //-----------------------------------------------------------------------------
655 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
657 if (g_isIdle
) wxapp_install_idle_handler();
659 int id
= menu
->FindMenuIdByMenuItem(widget
);
661 wxASSERT( id
!= -1 ); // should find it!
663 if (!menu
->IsEnabled(id
))
666 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
667 event
.SetEventObject( menu
);
669 wxEvtHandler
* handler
= menu
->GetEventHandler();
670 if (handler
&& handler
->ProcessEvent(event
))
673 wxWindow
*win
= menu
->GetInvokingWindow();
675 win
->GetEventHandler()->ProcessEvent( event
);
679 //-----------------------------------------------------------------------------
681 //-----------------------------------------------------------------------------
683 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
685 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
687 const wxString
& name
,
688 const wxString
& help
,
692 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
695 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
697 const wxString
& text
,
698 const wxString
& help
,
701 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
706 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
708 const wxString
& text
,
709 const wxString
& help
,
712 : wxMenuItemBase(parentMenu
, id
, text
, help
,
713 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
718 void wxMenuItem::Init(const wxString
& text
)
720 m_labelWidget
= (GtkWidget
*) NULL
;
721 m_menuItem
= (GtkWidget
*) NULL
;
726 wxMenuItem::~wxMenuItem()
728 // don't delete menu items, the menus take care of that
731 // return the menu item text without any menu accels
733 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
737 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
739 if ( *pc
== wxT('\t'))
742 if ( *pc
== wxT('_') )
744 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
750 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
753 // "&" is doubled to indicate "&" instead of accelerator
760 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
765 void wxMenuItem::SetText( const wxString
& str
)
767 // Some optimization to avoid flicker
768 wxString oldLabel
= m_text
;
769 oldLabel
= wxStripMenuCodes(oldLabel
);
770 oldLabel
.Replace(wxT("_"), wxT(""));
771 wxString label1
= wxStripMenuCodes(str
);
772 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
773 wxCharBuffer oldbuf
= wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
777 if (oldLabel
== label1
&&
778 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
785 label
= (GtkLabel
*) m_labelWidget
;
787 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
790 gtk_label_set( label
, wxGTK_CONV( m_text
) );
793 (void)gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV(m_text
) );
794 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label
) );
798 GdkModifierType accel_mods
;
799 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
802 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
803 m_parentMenu
->m_accel
,
808 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*this) );
809 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
812 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
814 m_parentMenu
->m_accel
,
821 // it's valid for this function to be called even if m_menuItem == NULL
822 void wxMenuItem::DoSetText( const wxString
& str
)
824 // '\t' is the deliminator indicating a hot key
826 const wxChar
*pc
= str
;
827 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
829 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
831 // "&" is doubled to indicate "&" instead of accelerator
835 else if (*pc
== wxT('&'))
839 else if ( *pc
== wxT('_') ) // escape underscores
858 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
863 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
868 return (wxAcceleratorEntry
*)NULL
;
871 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
873 label
<< wxT('\t') << GetHotKey();
875 return wxGetAccelFromString(label
);
878 #endif // wxUSE_ACCEL
880 void wxMenuItem::Check( bool check
)
882 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
884 if (check
== m_isChecked
)
887 wxMenuItemBase::Check( check
);
893 gtk_check_menu_item_set_state( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
897 wxFAIL_MSG( _T("can't check this item") );
901 void wxMenuItem::Enable( bool enable
)
903 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
905 gtk_widget_set_sensitive( m_menuItem
, enable
);
906 wxMenuItemBase::Enable( enable
);
909 bool wxMenuItem::IsChecked() const
911 wxCHECK_MSG( m_menuItem
, FALSE
, wxT("invalid menu item") );
913 wxCHECK_MSG( IsCheckable(), FALSE
,
914 wxT("can't get state of uncheckable item!") );
916 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
919 //-----------------------------------------------------------------------------
921 //-----------------------------------------------------------------------------
923 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
927 m_accel
= gtk_accel_group_new();
928 m_menu
= gtk_menu_new();
929 // NB: keep reference to the menu so that it is not destroyed behind
930 // our back by GTK+ e.g. when it is removed from menubar:
931 gtk_widget_ref(m_menu
);
933 m_owner
= (GtkWidget
*) NULL
;
935 // Tearoffs are entries, just like separators. So if we want this
936 // menu to be a tear-off one, we just append a tearoff entry
938 if ( m_style
& wxMENU_TEAROFF
)
940 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
942 gtk_menu_append(GTK_MENU(m_menu
), tearoff
);
947 // append the title as the very first entry if we have it
948 if ( !m_title
.empty() )
950 Append(wxGTK_TITLE_ID
, m_title
);
957 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
959 if ( GTK_IS_WIDGET( m_menu
))
962 gtk_widget_unref( m_menu
);
963 // if the menu is inserted in another menu at this time, there was
964 // one more reference to it:
966 gtk_widget_destroy( m_menu
);
970 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
977 if ( mitem
->IsSeparator() )
980 menuItem
= gtk_menu_item_new();
982 else if (mitem
->GetBitmap().Ok())
984 text
= mitem
->GetText();
985 const wxBitmap
*bitmap
= &mitem
->GetBitmap();
989 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
990 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
994 else // a normal item
996 // text has "_" instead of "&" after mitem->SetText() so don't use it
997 text
= mitem
->GetText() ;
999 switch ( mitem
->GetKind() )
1003 menuItem
= gtk_check_menu_item_new_with_label( wxGTK_CONV( text
) );
1004 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1006 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1013 GSList
*group
= NULL
;
1014 if ( m_prevRadio
== NULL
)
1016 // start of a new radio group
1017 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1018 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1020 gtk_label_set_text( label
, wxGTK_CONV( text
) );
1022 else // continue the radio group
1024 group
= gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1025 m_prevRadio
= menuItem
= gtk_radio_menu_item_new_with_label( group
, wxGTK_CONV( text
) );
1026 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1032 wxFAIL_MSG( _T("unexpected menu item kind") );
1037 menuItem
= gtk_menu_item_new_with_label( wxGTK_CONV( text
) );
1038 label
= GTK_LABEL( GTK_BIN(menuItem
)->child
);
1047 GdkModifierType accel_mods
;
1048 wxCharBuffer buf
= wxGTK_CONV( GetGtkHotKey(*mitem
) );
1050 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1051 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1054 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1063 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1065 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1067 gtk_widget_show( menuItem
);
1069 if ( !mitem
->IsSeparator() )
1071 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1073 gtk_signal_connect( GTK_OBJECT(menuItem
), "select",
1074 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback
),
1077 gtk_signal_connect( GTK_OBJECT(menuItem
), "deselect",
1078 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback
),
1081 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1083 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1085 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1087 // if adding a submenu to a menu already existing in the menu bar, we
1088 // must set invoking window to allow processing events from this
1090 if ( m_invokingWindow
)
1091 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1095 gtk_signal_connect( GTK_OBJECT(menuItem
), "activate",
1096 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback
),
1100 guint accel_key
= gtk_label_parse_uline (GTK_LABEL(label
), wxGTK_CONV( text
) );
1101 if (accel_key
!= GDK_VoidSymbol
)
1103 gtk_widget_add_accelerator (menuItem
,
1105 gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu
)),
1112 mitem
->SetMenuItem(menuItem
);
1116 // This doesn't even exist!
1117 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1123 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1125 if (!GtkAppend(mitem
))
1128 return wxMenuBase::DoAppend(mitem
);
1131 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1133 if ( !wxMenuBase::DoInsert(pos
, item
) )
1137 if ( !GtkAppend(item
, (int)pos
) )
1143 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1145 if ( !wxMenuBase::DoRemove(item
) )
1146 return (wxMenuItem
*)NULL
;
1148 // TODO: this code doesn't delete the item factory item and this seems
1149 // impossible as of GTK 1.2.6.
1150 gtk_widget_destroy( item
->GetMenuItem() );
1155 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1157 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1160 wxMenuItem
*item
= node
->GetData();
1161 if (item
->GetMenuItem() == menuItem
)
1162 return item
->GetId();
1163 node
= node
->GetNext();
1169 // ----------------------------------------------------------------------------
1171 // ----------------------------------------------------------------------------
1175 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1179 wxAcceleratorEntry
*accel
= item
.GetAccel();
1182 int flags
= accel
->GetFlags();
1183 if ( flags
& wxACCEL_ALT
)
1184 hotkey
+= wxT("<alt>");
1185 if ( flags
& wxACCEL_CTRL
)
1186 hotkey
+= wxT("<control>");
1187 if ( flags
& wxACCEL_SHIFT
)
1188 hotkey
+= wxT("<shift>");
1190 int code
= accel
->GetKeyCode();
1217 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1220 // TODO: we should use gdk_keyval_name() (a.k.a.
1221 // XKeysymToString) here as well as hardcoding the keysym
1222 // names this might be not portable
1224 hotkey
<< wxT("Insert" );
1227 hotkey
<< wxT("Delete" );
1230 hotkey
<< wxT("Up" );
1233 hotkey
<< wxT("Down" );
1237 hotkey
<< wxT("Prior" );
1241 hotkey
<< wxT("Next" );
1244 hotkey
<< wxT("Left" );
1247 hotkey
<< wxT("Right" );
1250 hotkey
<< wxT("Home" );
1253 hotkey
<< wxT("End" );
1256 hotkey
<< wxT("Return" );
1259 hotkey
<< wxT("BackSpace" );
1262 hotkey
<< wxT("Tab" );
1265 hotkey
<< wxT("Esc" );
1268 hotkey
<< wxT("space" );
1271 hotkey
<< wxT("Multiply" );
1274 hotkey
<< wxT("Add" );
1277 hotkey
<< wxT("Separator" );
1280 hotkey
<< wxT("Subtract" );
1283 hotkey
<< wxT("Decimal" );
1286 hotkey
<< wxT("Divide" );
1289 hotkey
<< wxT("Cancel" );
1292 hotkey
<< wxT("Clear" );
1295 hotkey
<< wxT("Menu" );
1298 hotkey
<< wxT("Pause" );
1301 hotkey
<< wxT("Capital" );
1304 hotkey
<< wxT("Select" );
1307 hotkey
<< wxT("Print" );
1310 hotkey
<< wxT("Execute" );
1313 hotkey
<< wxT("Snapshot" );
1316 hotkey
<< wxT("Help" );
1319 hotkey
<< wxT("Num_Lock" );
1322 hotkey
<< wxT("Scroll_Lock" );
1324 case WXK_NUMPAD_INSERT
:
1325 hotkey
<< wxT("KP_Insert" );
1327 case WXK_NUMPAD_DELETE
:
1328 hotkey
<< wxT("KP_Delete" );
1330 case WXK_NUMPAD_SPACE
:
1331 hotkey
<< wxT("KP_Space" );
1333 case WXK_NUMPAD_TAB
:
1334 hotkey
<< wxT("KP_Tab" );
1336 case WXK_NUMPAD_ENTER
:
1337 hotkey
<< wxT("KP_Enter" );
1339 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1341 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1343 case WXK_NUMPAD_HOME
:
1344 hotkey
<< wxT("KP_Home" );
1346 case WXK_NUMPAD_LEFT
:
1347 hotkey
<< wxT("KP_Left" );
1350 hotkey
<< wxT("KP_Up" );
1352 case WXK_NUMPAD_RIGHT
:
1353 hotkey
<< wxT("KP_Right" );
1355 case WXK_NUMPAD_DOWN
:
1356 hotkey
<< wxT("KP_Down" );
1358 case WXK_NUMPAD_PRIOR
: case WXK_NUMPAD_PAGEUP
:
1359 hotkey
<< wxT("KP_Prior" );
1361 case WXK_NUMPAD_NEXT
: case WXK_NUMPAD_PAGEDOWN
:
1362 hotkey
<< wxT("KP_Next" );
1364 case WXK_NUMPAD_END
:
1365 hotkey
<< wxT("KP_End" );
1367 case WXK_NUMPAD_BEGIN
:
1368 hotkey
<< wxT("KP_Begin" );
1370 case WXK_NUMPAD_EQUAL
:
1371 hotkey
<< wxT("KP_Equal" );
1373 case WXK_NUMPAD_MULTIPLY
:
1374 hotkey
<< wxT("KP_Multiply" );
1376 case WXK_NUMPAD_ADD
:
1377 hotkey
<< wxT("KP_Add" );
1379 case WXK_NUMPAD_SEPARATOR
:
1380 hotkey
<< wxT("KP_Separator" );
1382 case WXK_NUMPAD_SUBTRACT
:
1383 hotkey
<< wxT("KP_Subtract" );
1385 case WXK_NUMPAD_DECIMAL
:
1386 hotkey
<< wxT("KP_Decimal" );
1388 case WXK_NUMPAD_DIVIDE
:
1389 hotkey
<< wxT("KP_Divide" );
1391 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1392 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1393 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1394 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1396 case WXK_WINDOWS_LEFT
:
1397 hotkey
<< wxT("Super_L" );
1399 case WXK_WINDOWS_RIGHT
:
1400 hotkey
<< wxT("Super_R" );
1402 case WXK_WINDOWS_MENU
:
1403 hotkey
<< wxT("Menu" );
1406 hotkey
<< wxT("Command" );
1408 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1409 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1410 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1411 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1412 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1413 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1414 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1417 // if there are any other keys wxGetAccelFromString() may
1418 // return, we should process them here
1423 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1431 wxFAIL_MSG( wxT("unknown keyboard accel") );
1440 #endif // wxUSE_ACCEL
1442 // ----------------------------------------------------------------------------
1443 // Pop-up menu stuff
1444 // ----------------------------------------------------------------------------
1446 #if wxUSE_MENUS_NATIVE
1448 extern "C" WXDLLIMPEXP_CORE
1449 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1451 *is_waiting
= FALSE
;
1454 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1456 menu
->SetInvokingWindow( win
);
1458 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1461 wxMenuItem
*menuitem
= node
->GetData();
1462 if (menuitem
->IsSubMenu())
1464 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1467 node
= node
->GetNext();
1471 extern "C" WXDLLIMPEXP_CORE
1472 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1474 gpointer user_data
)
1476 // ensure that the menu appears entirely on screen
1478 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1480 wxSize sizeScreen
= wxGetDisplaySize();
1481 wxPoint
*pos
= (wxPoint
*)user_data
;
1483 gint xmax
= sizeScreen
.x
- req
.width
,
1484 ymax
= sizeScreen
.y
- req
.height
;
1486 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1487 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1490 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1492 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1494 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1496 // NOTE: if you change this code, you need to update
1497 // the same code in taskbar.cpp as well. This
1498 // is ugly code duplication, I know.
1500 SetInvokingWindow( menu
, this );
1504 bool is_waiting
= true;
1506 gulong handler
= gtk_signal_connect( GTK_OBJECT(menu
->m_menu
),
1508 GTK_SIGNAL_FUNC(gtk_pop_hide_callback
),
1509 (gpointer
)&is_waiting
);
1513 GtkMenuPositionFunc posfunc
;
1514 if ( x
== -1 && y
== -1 )
1516 // use GTK's default positioning algorithm
1522 pos
= ClientToScreen(wxPoint(x
, y
));
1524 posfunc
= wxPopupMenuPositionCallback
;
1527 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1528 DoCommonMenuCallbackCode(menu
, eventOpen
);
1531 GTK_MENU(menu
->m_menu
),
1532 (GtkWidget
*) NULL
, // parent menu shell
1533 (GtkWidget
*) NULL
, // parent menu item
1534 posfunc
, // function to position it
1535 userdata
, // client data
1536 0, // button used to activate it
1537 wxGtkTimeLastClick
// the time of activation
1542 gtk_main_iteration();
1545 gtk_signal_disconnect(GTK_OBJECT(menu
->m_menu
), handler
);
1547 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1548 DoCommonMenuCallbackCode(menu
, eventClose
);
1553 #endif // wxUSE_MENUS_NATIVE