1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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"
19 #include "wx/bitmap.h"
23 #include "wx/stockitem.h"
24 #include "wx/gtk/private.h"
26 // FIXME: is this right? somehow I don't think so (VZ)
28 #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g))
29 //#define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g))
30 //#define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m)
32 #define ACCEL_OBJECT GtkWindow
33 #define ACCEL_OBJECTS(a) (a)->acceleratables
34 #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj)
36 // we use normal item but with a special id for the menu title
37 static const int wxGTK_TITLE_ID
= -3;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 static wxString
GetGtkHotKey( const wxMenuItem
& item
);
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 static wxString
wxReplaceUnderscore( const wxString
& title
)
55 // GTK 1.2 wants to have "_" instead of "&" for accelerators
58 while (*pc
!= wxT('\0'))
60 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
62 // "&" is doubled to indicate "&" instead of accelerator
66 else if (*pc
== wxT('&'))
72 if ( *pc
== wxT('_') )
74 // underscores must be doubled to prevent them from being
75 // interpreted as accelerator character prefix by GTK
84 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
89 //-----------------------------------------------------------------------------
90 // activate message from GTK
91 //-----------------------------------------------------------------------------
93 static void DoCommonMenuCallbackCode(wxMenu
*menu
, wxMenuEvent
& event
)
96 wxapp_install_idle_handler();
98 event
.SetEventObject( menu
);
100 wxEvtHandler
* handler
= menu
->GetEventHandler();
101 if (handler
&& handler
->ProcessEvent(event
))
104 wxWindow
*win
= menu
->GetInvokingWindow();
106 win
->GetEventHandler()->ProcessEvent( event
);
111 static void gtk_menu_open_callback( GtkWidget
*widget
, wxMenu
*menu
)
113 wxMenuEvent
event(wxEVT_MENU_OPEN
, -1, menu
);
115 DoCommonMenuCallbackCode(menu
, event
);
118 static void gtk_menu_close_callback( GtkWidget
*widget
, wxMenuBar
*menubar
)
120 if ( !menubar
->GetMenuCount() )
122 // if menubar is empty we can't call GetMenu(0) below
126 wxMenuEvent
event( wxEVT_MENU_CLOSE
, -1, NULL
);
128 DoCommonMenuCallbackCode(menubar
->GetMenu(0), event
);
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
,wxWindow
)
139 void wxMenuBar::Init(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
141 // the parent window is known after wxFrame::SetMenu()
142 m_needParent
= false;
144 m_invokingWindow
= (wxWindow
*) NULL
;
146 if (!PreCreation( (wxWindow
*) NULL
, wxDefaultPosition
, wxDefaultSize
) ||
147 !CreateBase( (wxWindow
*) NULL
, -1, wxDefaultPosition
, wxDefaultSize
, style
, wxDefaultValidator
, wxT("menubar") ))
149 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
153 m_menubar
= gtk_menu_bar_new();
155 if (style
& wxMB_DOCKABLE
)
157 m_widget
= gtk_handle_box_new();
158 gtk_container_add( GTK_CONTAINER(m_widget
), GTK_WIDGET(m_menubar
) );
159 gtk_widget_show( GTK_WIDGET(m_menubar
) );
163 m_widget
= GTK_WIDGET(m_menubar
);
170 for (size_t i
= 0; i
< n
; ++i
)
171 Append(menus
[i
], titles
[i
]);
173 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
174 // don't get it when the menu is dismissed by clicking outside the
175 // toolbar) so we connect to the global one, even if it means that we
176 // can't pass the menu which was closed in wxMenuEvent object
177 g_signal_connect (m_menubar
, "deactivate",
178 G_CALLBACK (gtk_menu_close_callback
), this);
182 wxMenuBar::wxMenuBar(size_t n
, wxMenu
*menus
[], const wxString titles
[], long style
)
184 Init(n
, menus
, titles
, style
);
187 wxMenuBar::wxMenuBar(long style
)
189 Init(0, NULL
, NULL
, style
);
192 wxMenuBar::wxMenuBar()
194 Init(0, NULL
, NULL
, 0);
197 wxMenuBar::~wxMenuBar()
201 static void wxMenubarUnsetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
203 menu
->SetInvokingWindow( (wxWindow
*) NULL
);
205 wxWindow
*top_frame
= win
;
206 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
207 top_frame
= top_frame
->GetParent();
209 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
212 wxMenuItem
*menuitem
= node
->GetData();
213 if (menuitem
->IsSubMenu())
214 wxMenubarUnsetInvokingWindow( menuitem
->GetSubMenu(), win
);
215 node
= node
->GetNext();
219 static void wxMenubarSetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
221 menu
->SetInvokingWindow( win
);
223 wxWindow
*top_frame
= win
;
224 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
225 top_frame
= top_frame
->GetParent();
227 // support for native hot keys
228 ACCEL_OBJECT
*obj
= ACCEL_OBJ_CAST(top_frame
->m_widget
);
229 if ( !g_slist_find( ACCEL_OBJECTS(menu
->m_accel
), obj
) )
230 gtk_accel_group_attach( menu
->m_accel
, obj
);
232 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
235 wxMenuItem
*menuitem
= node
->GetData();
236 if (menuitem
->IsSubMenu())
237 wxMenubarSetInvokingWindow( menuitem
->GetSubMenu(), win
);
238 node
= node
->GetNext();
242 void wxMenuBar::SetInvokingWindow( wxWindow
*win
)
244 m_invokingWindow
= win
;
245 wxWindow
*top_frame
= win
;
246 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
247 top_frame
= top_frame
->GetParent();
249 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
252 wxMenu
*menu
= node
->GetData();
253 wxMenubarSetInvokingWindow( menu
, win
);
254 node
= node
->GetNext();
258 void wxMenuBar::UnsetInvokingWindow( wxWindow
*win
)
260 m_invokingWindow
= (wxWindow
*) NULL
;
261 wxWindow
*top_frame
= win
;
262 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
263 top_frame
= top_frame
->GetParent();
265 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
268 wxMenu
*menu
= node
->GetData();
269 wxMenubarUnsetInvokingWindow( menu
, win
);
270 node
= node
->GetNext();
274 bool wxMenuBar::Append( wxMenu
*menu
, const wxString
&title
)
276 if ( !wxMenuBarBase::Append( menu
, title
) )
279 return GtkAppend(menu
, title
);
282 bool wxMenuBar::GtkAppend(wxMenu
*menu
, const wxString
& title
, int pos
)
284 wxString
str( wxReplaceUnderscore( title
) );
286 // This doesn't have much effect right now.
287 menu
->SetTitle( str
);
289 // The "m_owner" is the "menu item"
290 menu
->m_owner
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str
) );
292 gtk_widget_show( menu
->m_owner
);
294 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu
->m_owner
), menu
->m_menu
);
297 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
);
299 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar
), menu
->m_owner
, pos
);
301 g_signal_connect (menu
->m_owner
, "activate",
302 G_CALLBACK (gtk_menu_open_callback
),
305 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
306 // addings menu later on.
307 if (m_invokingWindow
)
309 wxMenubarSetInvokingWindow( menu
, m_invokingWindow
);
311 // OPTIMISE ME: we should probably cache this, or pass it
312 // directly, but for now this is a minimal
313 // change to validate the new dynamic sizing.
314 // see (and refactor :) similar code in Remove
317 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
320 frame
->UpdateMenuBarSize();
326 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
328 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
333 if ( !GtkAppend(menu
, title
, (int)pos
) )
339 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
341 // remove the old item and insert a new one
342 wxMenu
*menuOld
= Remove(pos
);
343 if ( menuOld
&& !Insert(pos
, menu
, title
) )
345 return (wxMenu
*) NULL
;
348 // either Insert() succeeded or Remove() failed and menuOld is NULL
352 wxMenu
*wxMenuBar::Remove(size_t pos
)
354 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
356 return (wxMenu
*) NULL
;
358 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu
->m_owner
) );
359 gtk_container_remove(GTK_CONTAINER(m_menubar
), menu
->m_owner
);
361 gtk_widget_destroy( menu
->m_owner
);
362 menu
->m_owner
= NULL
;
364 if (m_invokingWindow
)
366 // OPTIMISE ME: see comment in GtkAppend
367 wxFrame
*frame
= wxDynamicCast( m_invokingWindow
, wxFrame
);
370 frame
->UpdateMenuBarSize();
376 static int FindMenuItemRecursive( const wxMenu
*menu
, const wxString
&menuString
, const wxString
&itemString
)
378 if (wxMenuItem::GetLabelFromText(menu
->GetTitle()) == wxMenuItem::GetLabelFromText(menuString
))
380 int res
= menu
->FindItem( itemString
);
381 if (res
!= wxNOT_FOUND
)
385 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
388 wxMenuItem
*item
= node
->GetData();
389 if (item
->IsSubMenu())
390 return FindMenuItemRecursive(item
->GetSubMenu(), menuString
, itemString
);
392 node
= node
->GetNext();
398 int wxMenuBar::FindMenuItem( const wxString
&menuString
, const wxString
&itemString
) const
400 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
403 wxMenu
*menu
= node
->GetData();
404 int res
= FindMenuItemRecursive( menu
, menuString
, itemString
);
407 node
= node
->GetNext();
413 // Find a wxMenuItem using its id. Recurses down into sub-menus
414 static wxMenuItem
* FindMenuItemByIdRecursive(const wxMenu
* menu
, int id
)
416 wxMenuItem
* result
= menu
->FindChildItem(id
);
418 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
419 while ( node
&& result
== NULL
)
421 wxMenuItem
*item
= node
->GetData();
422 if (item
->IsSubMenu())
424 result
= FindMenuItemByIdRecursive( item
->GetSubMenu(), id
);
426 node
= node
->GetNext();
432 wxMenuItem
* wxMenuBar::FindItem( int id
, wxMenu
**menuForItem
) const
434 wxMenuItem
* result
= 0;
435 wxMenuList::compatibility_iterator node
= m_menus
.GetFirst();
436 while (node
&& result
== 0)
438 wxMenu
*menu
= node
->GetData();
439 result
= FindMenuItemByIdRecursive( menu
, id
);
440 node
= node
->GetNext();
445 *menuForItem
= result
? result
->GetMenu() : (wxMenu
*)NULL
;
451 void wxMenuBar::EnableTop( size_t pos
, bool flag
)
453 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
455 wxCHECK_RET( node
, wxT("menu not found") );
457 wxMenu
* menu
= node
->GetData();
460 gtk_widget_set_sensitive( menu
->m_owner
, flag
);
463 wxString
wxMenuBar::GetLabelTop( size_t pos
) const
465 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
467 wxCHECK_MSG( node
, wxT("invalid"), wxT("menu not found") );
469 wxMenu
* menu
= node
->GetData();
472 wxString
text( menu
->GetTitle() );
473 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
475 if ( *pc
== wxT('_') )
477 // '_' is the escape character for GTK+
481 // don't remove ampersands '&' since if we have them in the menu title
482 // it means that they were doubled to indicate "&" instead of accelerator
490 void wxMenuBar::SetLabelTop( size_t pos
, const wxString
& label
)
492 wxMenuList::compatibility_iterator node
= m_menus
.Item( pos
);
494 wxCHECK_RET( node
, wxT("menu not found") );
496 wxMenu
* menu
= node
->GetData();
498 const wxString
str( wxReplaceUnderscore( label
) );
500 menu
->SetTitle( str
);
503 gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu
->m_owner
)->child
), wxGTK_CONV(str
) );
506 //-----------------------------------------------------------------------------
508 //-----------------------------------------------------------------------------
511 static void gtk_menu_clicked_callback( GtkWidget
*widget
, wxMenu
*menu
)
514 wxapp_install_idle_handler();
516 int id
= menu
->FindMenuIdByMenuItem(widget
);
518 /* should find it for normal (not popup) menu */
519 wxASSERT_MSG( (id
!= -1) || (menu
->GetInvokingWindow() != NULL
),
520 _T("menu item not found in gtk_menu_clicked_callback") );
522 if (!menu
->IsEnabled(id
))
525 wxMenuItem
* item
= menu
->FindChildItem( id
);
526 wxCHECK_RET( item
, wxT("error in menu item callback") );
528 if ( item
->GetId() == wxGTK_TITLE_ID
)
530 // ignore events from the menu title
534 if (item
->IsCheckable())
536 bool isReallyChecked
= item
->IsChecked(),
537 isInternallyChecked
= item
->wxMenuItemBase::IsChecked();
539 // ensure that the internal state is always consistent with what is
540 // shown on the screen
541 item
->wxMenuItemBase::Check(isReallyChecked
);
543 // we must not report the events for the radio button going up nor the
544 // events resulting from the calls to wxMenuItem::Check()
545 if ( (item
->GetKind() == wxITEM_RADIO
&& !isReallyChecked
) ||
546 (isInternallyChecked
== isReallyChecked
) )
553 // Is this menu on a menubar? (possibly nested)
554 wxFrame
* frame
= NULL
;
555 if(menu
->IsAttached())
556 frame
= menu
->GetMenuBar()->GetFrame();
558 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
559 // normally wxMenu::SendEvent() should be enough, if it doesn't work
560 // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which
561 // should be fixed instead of working around it here...
564 // If it is attached then let the frame send the event.
565 // Don't call frame->ProcessCommand(id) because it toggles
566 // checkable items and we've already done that above.
567 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
568 commandEvent
.SetEventObject(frame
);
569 if (item
->IsCheckable())
570 commandEvent
.SetInt(item
->IsChecked());
571 commandEvent
.SetEventObject(menu
);
573 frame
->GetEventHandler()->ProcessEvent(commandEvent
);
577 // otherwise let the menu have it
578 menu
->SendEvent(id
, item
->IsCheckable() ? item
->IsChecked() : -1);
583 //-----------------------------------------------------------------------------
585 //-----------------------------------------------------------------------------
588 static void gtk_menu_hilight_callback( GtkWidget
*widget
, wxMenu
*menu
)
590 if (g_isIdle
) wxapp_install_idle_handler();
592 int id
= menu
->FindMenuIdByMenuItem(widget
);
594 wxASSERT( id
!= -1 ); // should find it!
596 if (!menu
->IsEnabled(id
))
599 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, id
);
600 event
.SetEventObject( menu
);
602 wxEvtHandler
* handler
= menu
->GetEventHandler();
603 if (handler
&& handler
->ProcessEvent(event
))
606 wxWindow
*win
= menu
->GetInvokingWindow();
607 if (win
) win
->GetEventHandler()->ProcessEvent( event
);
611 //-----------------------------------------------------------------------------
613 //-----------------------------------------------------------------------------
616 static void gtk_menu_nolight_callback( GtkWidget
*widget
, wxMenu
*menu
)
618 if (g_isIdle
) wxapp_install_idle_handler();
620 int id
= menu
->FindMenuIdByMenuItem(widget
);
622 wxASSERT( id
!= -1 ); // should find it!
624 if (!menu
->IsEnabled(id
))
627 wxMenuEvent
event( wxEVT_MENU_HIGHLIGHT
, -1 );
628 event
.SetEventObject( menu
);
630 wxEvtHandler
* handler
= menu
->GetEventHandler();
631 if (handler
&& handler
->ProcessEvent(event
))
634 wxWindow
*win
= menu
->GetInvokingWindow();
636 win
->GetEventHandler()->ProcessEvent( event
);
640 //-----------------------------------------------------------------------------
642 //-----------------------------------------------------------------------------
644 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem
, wxObject
)
646 wxMenuItem
*wxMenuItemBase::New(wxMenu
*parentMenu
,
648 const wxString
& name
,
649 const wxString
& help
,
653 return new wxMenuItem(parentMenu
, id
, name
, help
, kind
, subMenu
);
656 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
658 const wxString
& text
,
659 const wxString
& help
,
662 : wxMenuItemBase(parentMenu
, id
, text
, help
, kind
, subMenu
)
667 wxMenuItem::wxMenuItem(wxMenu
*parentMenu
,
669 const wxString
& text
,
670 const wxString
& help
,
673 : wxMenuItemBase(parentMenu
, id
, text
, help
,
674 isCheckable
? wxITEM_CHECK
: wxITEM_NORMAL
, subMenu
)
679 void wxMenuItem::Init(const wxString
& text
)
681 m_labelWidget
= (GtkWidget
*) NULL
;
682 m_menuItem
= (GtkWidget
*) NULL
;
687 wxMenuItem::~wxMenuItem()
689 // don't delete menu items, the menus take care of that
692 // return the menu item text without any menu accels
694 wxString
wxMenuItemBase::GetLabelFromText(const wxString
& text
)
698 for ( const wxChar
*pc
= text
.c_str(); *pc
; pc
++ )
700 if ( *pc
== wxT('\t'))
703 if ( *pc
== wxT('_') )
705 // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx"
711 if ( *pc
== wxT('\\') )
713 // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx"
719 if ( (*pc
== wxT('&')) && (*(pc
+1) != wxT('&')) )
722 // "&" is doubled to indicate "&" instead of accelerator
729 // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() );
734 void wxMenuItem::SetText( const wxString
& str
)
736 // cache some data which must be used later
737 bool isstock
= wxIsStockID(GetId());
738 const char *stockid
= NULL
;
740 stockid
= wxGetStockGtkID(GetId());
742 // Some optimization to avoid flicker
743 wxString oldLabel
= m_text
;
744 oldLabel
= wxStripMenuCodes(oldLabel
);
745 oldLabel
.Replace(wxT("_"), wxT(""));
746 wxString label1
= wxStripMenuCodes(str
);
747 wxString oldhotkey
= GetHotKey(); // Store the old hotkey in Ctrl-foo format
748 wxCharBuffer oldbuf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo
752 if (oldLabel
== label1
&&
753 oldhotkey
== GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
760 label
= (GtkLabel
*) m_labelWidget
;
762 label
= GTK_LABEL( GTK_BIN(m_menuItem
)->child
);
764 // stock menu items can have empty labels:
765 wxString text
= m_text
;
766 if (text
.IsEmpty() && !IsSeparator())
768 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
769 text
= wxGetStockLabel(GetId());
771 // need & => _ conversion
772 text
= GTKProcessMenuItemLabel(text
, NULL
);
775 gtk_label_set_text_with_mnemonic( GTK_LABEL(label
), wxGTK_CONV_SYS(text
) );
778 // remove old accelerator from our parent's accelerator group, if present
780 GdkModifierType accel_mods
;
781 if (oldbuf
[(size_t)0] != '\0')
783 gtk_accelerator_parse( (const char*) oldbuf
, &accel_key
, &accel_mods
);
786 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
787 m_parentMenu
->m_accel
,
794 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
795 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
796 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
797 m_parentMenu
->m_accel
,
802 // add new accelerator to our parent's accelerator group
803 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*this) );
804 if (buf
[(size_t)0] != '\0')
806 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
809 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem
),
811 m_parentMenu
->m_accel
,
819 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
820 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
821 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem
),
822 m_parentMenu
->m_accel
,
828 // NOTE: this function is different from the similar functions GTKProcessMnemonics()
829 // implemented in control.cpp and from wxMenuItemBase::GetLabelFromText...
830 // so there's no real code duplication
831 wxString
wxMenuItem::GTKProcessMenuItemLabel(const wxString
& str
, wxString
*hotKey
)
835 // '\t' is the deliminator indicating a hot key
836 const wxChar
*pc
= str
;
837 while ( (*pc
!= wxT('\0')) && (*pc
!= wxT('\t')) )
839 if ((*pc
== wxT('&')) && (*(pc
+1) == wxT('&')))
841 // "&" is doubled to indicate "&" instead of accelerator
845 else if (*pc
== wxT('&'))
849 else if ( *pc
== wxT('_') ) // escape underscores
873 // it's valid for this function to be called even if m_menuItem == NULL
874 void wxMenuItem::DoSetText( const wxString
& str
)
877 m_text
= GTKProcessMenuItemLabel(str
, &m_hotKey
);
878 // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() );
883 wxAcceleratorEntry
*wxMenuItem::GetAccel() const
888 return (wxAcceleratorEntry
*)NULL
;
891 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
893 label
<< wxT('\t') << GetHotKey();
895 return wxGetAccelFromString(label
);
898 #endif // wxUSE_ACCEL
900 void wxMenuItem::Check( bool check
)
902 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
904 if (check
== m_isChecked
)
907 wxMenuItemBase::Check( check
);
913 gtk_check_menu_item_set_active( (GtkCheckMenuItem
*)m_menuItem
, (gint
)check
);
917 wxFAIL_MSG( _T("can't check this item") );
921 void wxMenuItem::Enable( bool enable
)
923 wxCHECK_RET( m_menuItem
, wxT("invalid menu item") );
925 gtk_widget_set_sensitive( m_menuItem
, enable
);
926 wxMenuItemBase::Enable( enable
);
929 bool wxMenuItem::IsChecked() const
931 wxCHECK_MSG( m_menuItem
, false, wxT("invalid menu item") );
933 wxCHECK_MSG( IsCheckable(), false,
934 wxT("can't get state of uncheckable item!") );
936 return ((GtkCheckMenuItem
*)m_menuItem
)->active
!= 0;
939 //-----------------------------------------------------------------------------
941 //-----------------------------------------------------------------------------
943 IMPLEMENT_DYNAMIC_CLASS(wxMenu
,wxEvtHandler
)
947 m_accel
= gtk_accel_group_new();
948 m_menu
= gtk_menu_new();
949 // NB: keep reference to the menu so that it is not destroyed behind
950 // our back by GTK+ e.g. when it is removed from menubar:
951 gtk_widget_ref(m_menu
);
953 m_owner
= (GtkWidget
*) NULL
;
955 // Tearoffs are entries, just like separators. So if we want this
956 // menu to be a tear-off one, we just append a tearoff entry
958 if ( m_style
& wxMENU_TEAROFF
)
960 GtkWidget
*tearoff
= gtk_tearoff_menu_item_new();
962 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), tearoff
);
967 // append the title as the very first entry if we have it
968 if ( !m_title
.empty() )
970 Append(wxGTK_TITLE_ID
, m_title
);
977 WX_CLEAR_LIST(wxMenuItemList
, m_items
);
979 if ( GTK_IS_WIDGET( m_menu
))
982 gtk_widget_unref( m_menu
);
983 // if the menu is inserted in another menu at this time, there was
984 // one more reference to it:
986 gtk_widget_destroy( m_menu
);
990 bool wxMenu::GtkAppend(wxMenuItem
*mitem
, int pos
)
994 // cache some data used later
995 wxString text
= mitem
->GetText();
996 int id
= mitem
->GetId();
997 bool isstock
= wxIsStockID(id
);
998 const char *stockid
= NULL
;
1000 stockid
= wxGetStockGtkID(mitem
->GetId());
1002 // stock menu items can have an empty label
1003 if (text
.IsEmpty() && !mitem
->IsSeparator())
1005 wxASSERT_MSG(isstock
, wxT("A non-stock menu item with an empty label?"));
1006 text
= wxGetStockLabel(id
);
1008 // need & => _ conversion
1009 text
= wxMenuItem::GTKProcessMenuItemLabel(text
, NULL
);
1012 if ( mitem
->IsSeparator() )
1014 menuItem
= gtk_separator_menu_item_new();
1016 else if ( mitem
->GetBitmap().Ok() ||
1017 (mitem
->GetKind() == wxITEM_NORMAL
&& isstock
) )
1019 wxBitmap
bitmap(mitem
->GetBitmap());
1021 menuItem
= gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1026 // use stock bitmap for this item if available on the assumption
1027 // that it never hurts to follow GTK+ conventions more closely
1028 image
= stockid
? gtk_image_new_from_stock(stockid
, GTK_ICON_SIZE_MENU
)
1031 else // we have a custom bitmap
1033 wxASSERT_MSG( mitem
->GetKind() == wxITEM_NORMAL
,
1034 _T("only normal menu items can have bitmaps") );
1036 if ( bitmap
.HasPixbuf() )
1038 image
= gtk_image_new_from_pixbuf(bitmap
.GetPixbuf());
1042 GdkPixmap
*gdk_pixmap
= bitmap
.GetPixmap();
1043 GdkBitmap
*gdk_bitmap
= bitmap
.GetMask() ?
1044 bitmap
.GetMask()->GetBitmap() :
1046 image
= gtk_image_new_from_pixmap( gdk_pixmap
, gdk_bitmap
);
1052 gtk_widget_show(image
);
1054 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem
), image
);
1059 else // a normal item
1061 // NB: 'text' variable has "_" instead of "&" after mitem->SetText()
1064 switch ( mitem
->GetKind() )
1068 menuItem
= gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1075 GSList
*group
= NULL
;
1076 if ( m_prevRadio
== NULL
)
1078 // start of a new radio group
1079 m_prevRadio
= menuItem
=
1080 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1082 else // continue the radio group
1084 group
= gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio
));
1085 m_prevRadio
= menuItem
=
1086 gtk_radio_menu_item_new_with_mnemonic( group
, wxGTK_CONV_SYS( text
) );
1092 wxFAIL_MSG( _T("unexpected menu item kind") );
1097 menuItem
= gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text
) );
1106 GdkModifierType accel_mods
;
1107 wxCharBuffer buf
= wxGTK_CONV_SYS( GetGtkHotKey(*mitem
) );
1109 // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() );
1110 if (buf
[(size_t)0] != '\0')
1112 gtk_accelerator_parse( (const char*) buf
, &accel_key
, &accel_mods
);
1115 gtk_widget_add_accelerator (GTK_WIDGET(menuItem
),
1125 // if the accelerator was taken from a stock ID, just get it back from GTK+ stock
1126 if (wxGetStockGtkAccelerator(stockid
, &accel_mods
, &accel_key
))
1127 gtk_widget_add_accelerator( GTK_WIDGET(menuItem
),
1136 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu
), menuItem
);
1138 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu
), menuItem
, pos
);
1140 gtk_widget_show( menuItem
);
1142 if ( !mitem
->IsSeparator() )
1144 wxASSERT_MSG( menuItem
, wxT("invalid menuitem") );
1146 g_signal_connect (menuItem
, "select",
1147 G_CALLBACK (gtk_menu_hilight_callback
), this);
1148 g_signal_connect (menuItem
, "deselect",
1149 G_CALLBACK (gtk_menu_nolight_callback
), this);
1151 if ( mitem
->IsSubMenu() && mitem
->GetKind() != wxITEM_RADIO
&& mitem
->GetKind() != wxITEM_CHECK
)
1153 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem
), mitem
->GetSubMenu()->m_menu
);
1155 gtk_widget_show( mitem
->GetSubMenu()->m_menu
);
1157 // if adding a submenu to a menu already existing in the menu bar, we
1158 // must set invoking window to allow processing events from this
1160 if ( m_invokingWindow
)
1161 wxMenubarSetInvokingWindow(mitem
->GetSubMenu(), m_invokingWindow
);
1165 g_signal_connect (menuItem
, "activate",
1166 G_CALLBACK (gtk_menu_clicked_callback
),
1171 mitem
->SetMenuItem(menuItem
);
1175 // This doesn't even exist!
1176 // gtk_widget_lock_accelerators(mitem->GetMenuItem());
1182 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*mitem
)
1184 if (!GtkAppend(mitem
))
1187 return wxMenuBase::DoAppend(mitem
);
1190 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
1192 if ( !wxMenuBase::DoInsert(pos
, item
) )
1196 if ( !GtkAppend(item
, (int)pos
) )
1202 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
1204 if ( !wxMenuBase::DoRemove(item
) )
1205 return (wxMenuItem
*)NULL
;
1207 // TODO: this code doesn't delete the item factory item and this seems
1208 // impossible as of GTK 1.2.6.
1209 gtk_widget_destroy( item
->GetMenuItem() );
1214 int wxMenu::FindMenuIdByMenuItem( GtkWidget
*menuItem
) const
1216 wxMenuItemList::compatibility_iterator node
= m_items
.GetFirst();
1219 wxMenuItem
*item
= node
->GetData();
1220 if (item
->GetMenuItem() == menuItem
)
1221 return item
->GetId();
1222 node
= node
->GetNext();
1228 // ----------------------------------------------------------------------------
1230 // ----------------------------------------------------------------------------
1234 static wxString
GetGtkHotKey( const wxMenuItem
& item
)
1238 wxAcceleratorEntry
*accel
= item
.GetAccel();
1241 int flags
= accel
->GetFlags();
1242 if ( flags
& wxACCEL_ALT
)
1243 hotkey
+= wxT("<alt>");
1244 if ( flags
& wxACCEL_CTRL
)
1245 hotkey
+= wxT("<control>");
1246 if ( flags
& wxACCEL_SHIFT
)
1247 hotkey
+= wxT("<shift>");
1249 int code
= accel
->GetKeyCode();
1276 hotkey
+= wxString::Format(wxT("F%d"), code
- WXK_F1
+ 1);
1279 // TODO: we should use gdk_keyval_name() (a.k.a.
1280 // XKeysymToString) here as well as hardcoding the keysym
1281 // names this might be not portable
1283 hotkey
<< wxT("Insert" );
1286 hotkey
<< wxT("Delete" );
1289 hotkey
<< wxT("Up" );
1292 hotkey
<< wxT("Down" );
1295 hotkey
<< wxT("PgUp" );
1298 hotkey
<< wxT("PgDn" );
1301 hotkey
<< wxT("Left" );
1304 hotkey
<< wxT("Right" );
1307 hotkey
<< wxT("Home" );
1310 hotkey
<< wxT("End" );
1313 hotkey
<< wxT("Return" );
1316 hotkey
<< wxT("BackSpace" );
1319 hotkey
<< wxT("Tab" );
1322 hotkey
<< wxT("Esc" );
1325 hotkey
<< wxT("space" );
1328 hotkey
<< wxT("Multiply" );
1331 hotkey
<< wxT("Add" );
1334 hotkey
<< wxT("Separator" );
1337 hotkey
<< wxT("Subtract" );
1340 hotkey
<< wxT("Decimal" );
1343 hotkey
<< wxT("Divide" );
1346 hotkey
<< wxT("Cancel" );
1349 hotkey
<< wxT("Clear" );
1352 hotkey
<< wxT("Menu" );
1355 hotkey
<< wxT("Pause" );
1358 hotkey
<< wxT("Capital" );
1361 hotkey
<< wxT("Select" );
1364 hotkey
<< wxT("Print" );
1367 hotkey
<< wxT("Execute" );
1370 hotkey
<< wxT("Snapshot" );
1373 hotkey
<< wxT("Help" );
1376 hotkey
<< wxT("Num_Lock" );
1379 hotkey
<< wxT("Scroll_Lock" );
1381 case WXK_NUMPAD_INSERT
:
1382 hotkey
<< wxT("KP_Insert" );
1384 case WXK_NUMPAD_DELETE
:
1385 hotkey
<< wxT("KP_Delete" );
1387 case WXK_NUMPAD_SPACE
:
1388 hotkey
<< wxT("KP_Space" );
1390 case WXK_NUMPAD_TAB
:
1391 hotkey
<< wxT("KP_Tab" );
1393 case WXK_NUMPAD_ENTER
:
1394 hotkey
<< wxT("KP_Enter" );
1396 case WXK_NUMPAD_F1
: case WXK_NUMPAD_F2
: case WXK_NUMPAD_F3
:
1398 hotkey
+= wxString::Format(wxT("KP_F%d"), code
- WXK_NUMPAD_F1
+ 1);
1400 case WXK_NUMPAD_HOME
:
1401 hotkey
<< wxT("KP_Home" );
1403 case WXK_NUMPAD_LEFT
:
1404 hotkey
<< wxT("KP_Left" );
1407 hotkey
<< wxT("KP_Up" );
1409 case WXK_NUMPAD_RIGHT
:
1410 hotkey
<< wxT("KP_Right" );
1412 case WXK_NUMPAD_DOWN
:
1413 hotkey
<< wxT("KP_Down" );
1415 case WXK_NUMPAD_PAGEUP
:
1416 hotkey
<< wxT("KP_PgUp" );
1418 case WXK_NUMPAD_PAGEDOWN
:
1419 hotkey
<< wxT("KP_PgDn" );
1421 case WXK_NUMPAD_END
:
1422 hotkey
<< wxT("KP_End" );
1424 case WXK_NUMPAD_BEGIN
:
1425 hotkey
<< wxT("KP_Begin" );
1427 case WXK_NUMPAD_EQUAL
:
1428 hotkey
<< wxT("KP_Equal" );
1430 case WXK_NUMPAD_MULTIPLY
:
1431 hotkey
<< wxT("KP_Multiply" );
1433 case WXK_NUMPAD_ADD
:
1434 hotkey
<< wxT("KP_Add" );
1436 case WXK_NUMPAD_SEPARATOR
:
1437 hotkey
<< wxT("KP_Separator" );
1439 case WXK_NUMPAD_SUBTRACT
:
1440 hotkey
<< wxT("KP_Subtract" );
1442 case WXK_NUMPAD_DECIMAL
:
1443 hotkey
<< wxT("KP_Decimal" );
1445 case WXK_NUMPAD_DIVIDE
:
1446 hotkey
<< wxT("KP_Divide" );
1448 case WXK_NUMPAD0
: case WXK_NUMPAD1
: case WXK_NUMPAD2
:
1449 case WXK_NUMPAD3
: case WXK_NUMPAD4
: case WXK_NUMPAD5
:
1450 case WXK_NUMPAD6
: case WXK_NUMPAD7
: case WXK_NUMPAD8
: case WXK_NUMPAD9
:
1451 hotkey
+= wxString::Format(wxT("KP_%d"), code
- WXK_NUMPAD0
);
1453 case WXK_WINDOWS_LEFT
:
1454 hotkey
<< wxT("Super_L" );
1456 case WXK_WINDOWS_RIGHT
:
1457 hotkey
<< wxT("Super_R" );
1459 case WXK_WINDOWS_MENU
:
1460 hotkey
<< wxT("Menu" );
1463 hotkey
<< wxT("Command" );
1465 /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt
1466 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4:
1467 case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8:
1468 case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1469 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16:
1470 case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20:
1471 hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1);
1474 // if there are any other keys wxGetAccelFromString() may
1475 // return, we should process them here
1480 wxString name
= wxGTK_CONV_BACK( gdk_keyval_name((guint
)code
) );
1488 wxFAIL_MSG( wxT("unknown keyboard accel") );
1497 #endif // wxUSE_ACCEL
1499 // ----------------------------------------------------------------------------
1500 // Pop-up menu stuff
1501 // ----------------------------------------------------------------------------
1503 #if wxUSE_MENUS_NATIVE
1505 extern "C" WXDLLIMPEXP_CORE
1506 void gtk_pop_hide_callback( GtkWidget
*WXUNUSED(widget
), bool* is_waiting
)
1508 *is_waiting
= false;
1511 WXDLLIMPEXP_CORE
void SetInvokingWindow( wxMenu
*menu
, wxWindow
* win
)
1513 menu
->SetInvokingWindow( win
);
1515 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
1518 wxMenuItem
*menuitem
= node
->GetData();
1519 if (menuitem
->IsSubMenu())
1521 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
1524 node
= node
->GetNext();
1528 extern "C" WXDLLIMPEXP_CORE
1529 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
1531 gboolean
* WXUNUSED(whatever
),
1532 gpointer user_data
)
1534 // ensure that the menu appears entirely on screen
1536 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
1538 wxSize sizeScreen
= wxGetDisplaySize();
1539 wxPoint
*pos
= (wxPoint
*)user_data
;
1541 gint xmax
= sizeScreen
.x
- req
.width
,
1542 ymax
= sizeScreen
.y
- req
.height
;
1544 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
1545 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
1548 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
1550 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
1552 wxCHECK_MSG( menu
!= NULL
, false, wxT("invalid popup-menu") );
1554 // NOTE: if you change this code, you need to update
1555 // the same code in taskbar.cpp as well. This
1556 // is ugly code duplication, I know.
1558 SetInvokingWindow( menu
, this );
1562 bool is_waiting
= true;
1564 gulong handler
= g_signal_connect (menu
->m_menu
, "hide",
1565 G_CALLBACK (gtk_pop_hide_callback
),
1570 GtkMenuPositionFunc posfunc
;
1571 if ( x
== -1 && y
== -1 )
1573 // use GTK's default positioning algorithm
1579 pos
= ClientToScreen(wxPoint(x
, y
));
1581 posfunc
= wxPopupMenuPositionCallback
;
1584 wxMenuEvent
eventOpen(wxEVT_MENU_OPEN
, -1, menu
);
1585 DoCommonMenuCallbackCode(menu
, eventOpen
);
1588 GTK_MENU(menu
->m_menu
),
1589 (GtkWidget
*) NULL
, // parent menu shell
1590 (GtkWidget
*) NULL
, // parent menu item
1591 posfunc
, // function to position it
1592 userdata
, // client data
1593 0, // button used to activate it
1594 gtk_get_current_event_time()
1599 gtk_main_iteration();
1602 g_signal_handler_disconnect (menu
->m_menu
, handler
);
1604 wxMenuEvent
eventClose(wxEVT_MENU_CLOSE
, -1, menu
);
1605 DoCommonMenuCallbackCode(menu
, eventClose
);
1610 #endif // wxUSE_MENUS_NATIVE