1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/menu.cpp
3 // Purpose: implementation of wxMenuBar and wxMenu classes for wxGTK
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if wxUSE_MENUS
13
14 #include "wx/menu.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #include "wx/dialog.h"
20 #include "wx/frame.h"
21 #include "wx/bitmap.h"
22 #include "wx/app.h"
23 #endif
24
25 #include "wx/accel.h"
26 #include "wx/stockitem.h"
27
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/image.h"
30 #include "wx/gtk/private/mnemonics.h"
31
32 // Number of currently open modal dialogs, defined in src/gtk/toplevel.cpp.
33 extern int wxOpenModalDialogsCount;
34
35 // we use normal item but with a special id for the menu title
36 static const int wxGTK_TITLE_ID = -3;
37
38 #if wxUSE_ACCEL
39 static bool wxGetGtkAccel(const wxMenuItem*, guint*, GdkModifierType*);
40 #endif
41
42 // Unity hack: under Ubuntu Unity the global menu bar is not affected by a
43 // modal dialog being shown, so the user can select a menu item before hiding
44 // the dialog and, in particular, a new instance of the same dialog can be
45 // shown again, breaking a lot of programs not expecting this.
46 //
47 // So explicitly ignore any menu events generated while any modal dialogs
48 // are opened except for the events generated by a context menu within the
49 // modal dialog itself that should have a dialog as their invoking window.
IsMenuEventAllowed(wxMenu * menu)50 static bool IsMenuEventAllowed(wxMenu* menu)
51 {
52 if ( wxOpenModalDialogsCount )
53 {
54 wxWindow* tlw = wxGetTopLevelParent(menu->GetWindow());
55 if ( !tlw || !wxDynamicCast(tlw, wxDialog) )
56 {
57 // This must be an event from a menu bar of one of the frames.
58 return false;
59 }
60 }
61
62 return true;
63 }
64
DoCommonMenuCallbackCode(wxMenu * menu,wxMenuEvent & event)65 static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
66 {
67 if ( !IsMenuEventAllowed(menu) )
68 return;
69
70 wxMenu::ProcessMenuEvent(menu, event, menu->GetWindow());
71 }
72
73 // Return the top level menu containing this menu (possibly this menu itself).
GetRootParentMenu(wxMenu * menu)74 static wxMenu* GetRootParentMenu(wxMenu* menu)
75 {
76 while ( menu->GetParent() )
77 menu = menu->GetParent();
78
79 return menu;
80 }
81
82 // Call SetGtkLabel() to update the labels of all the items in this items sub
83 // menu, recursively.
UpdateSubMenuItemLabels(wxMenuItem * itemMenu)84 static void UpdateSubMenuItemLabels(wxMenuItem* itemMenu)
85 {
86 wxMenu* const menu = itemMenu->GetSubMenu();
87 wxCHECK_RET( menu, "should only be called for sub menus" );
88
89 const wxMenuItemList& items = menu->GetMenuItems();
90 for ( wxMenuItemList::const_iterator
91 it = items.begin(); it != items.end(); ++it )
92 {
93 wxMenuItem* const item = *it;
94 if ( !item->IsSeparator() )
95 {
96 item->SetGtkLabel();
97 if ( item->IsSubMenu() )
98 UpdateSubMenuItemLabels(item);
99 }
100 }
101 }
102
103 //-----------------------------------------------------------------------------
104 // wxMenuBar
105 //-----------------------------------------------------------------------------
106
~wxMenuBar()107 wxMenuBar::~wxMenuBar()
108 {
109 }
110
Init(size_t n,wxMenu * menus[],const wxString titles[],long style)111 void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
112 {
113 if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
114 !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
115 {
116 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
117 return;
118 }
119
120 m_menubar = gtk_menu_bar_new();
121
122 #ifndef __WXGTK4__
123 if ((style & wxMB_DOCKABLE)
124 #ifdef __WXGTK3__
125 // using GtkHandleBox prevents menubar from drawing with GTK+ >= 3.19.7
126 && gtk_check_version(3,19,7)
127 #endif
128 )
129 {
130 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
131 m_widget = gtk_handle_box_new();
132 wxGCC_WARNING_RESTORE()
133 gtk_container_add(GTK_CONTAINER(m_widget), m_menubar);
134 gtk_widget_show(m_menubar);
135 }
136 else
137 #endif
138 {
139 m_widget = m_menubar;
140 }
141
142 PostCreation();
143
144 g_object_ref_sink(m_widget);
145
146 for (size_t i = 0; i < n; ++i )
147 Append(menus[i], titles[i]);
148 }
149
wxMenuBar(size_t n,wxMenu * menus[],const wxString titles[],long style)150 wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
151 {
152 Init(n, menus, titles, style);
153 }
154
wxMenuBar(long style)155 wxMenuBar::wxMenuBar(long style)
156 {
157 Init(0, NULL, NULL, style);
158 }
159
wxMenuBar()160 wxMenuBar::wxMenuBar()
161 {
162 Init(0, NULL, NULL, 0);
163 }
164
165 // recursive helpers for wxMenuBar::Attach() and Detach(): they are called to
166 // associate the menus with the frame they belong to or dissociate them from it
167 namespace
168 {
169
170 // This should be called when detaching menus to ensure that they don't keep
171 // focus grab, because if they do, they continue getting all GTK+ messages
172 // which they can't process any more in their (soon to be) unrealized state.
173 void
EnsureNoGrab(GtkWidget * widget)174 EnsureNoGrab(GtkWidget* widget)
175 {
176 gtk_widget_hide(widget);
177 gtk_grab_remove(widget);
178 }
179
180 void
DetachFromFrame(wxMenu * menu,wxFrame * frame)181 DetachFromFrame(wxMenu* menu, wxFrame* frame)
182 {
183 // support for native hot keys
184 if (menu->m_accel)
185 {
186 // Note that wxGetTopLevelParent() is really needed because this frame
187 // can be an MDI child frame which is a fake frame and not a TLW at all
188 GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget);
189 if (g_slist_find(gtk_accel_groups_from_object(G_OBJECT(tlw)), menu->m_accel))
190 gtk_window_remove_accel_group(tlw, menu->m_accel);
191 }
192
193 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
194 while (node)
195 {
196 wxMenuItem *menuitem = node->GetData();
197 if (menuitem->IsSubMenu())
198 DetachFromFrame(menuitem->GetSubMenu(), frame);
199 node = node->GetNext();
200 }
201
202 EnsureNoGrab(menu->m_menu);
203 }
204
205 void
AttachToFrame(wxMenu * menu,wxFrame * frame)206 AttachToFrame(wxMenu* menu, wxFrame* frame)
207 {
208 // support for native hot keys
209 if (menu->m_accel)
210 {
211 GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget);
212 if (!g_slist_find(gtk_accel_groups_from_object(G_OBJECT(tlw)), menu->m_accel))
213 gtk_window_add_accel_group(tlw, menu->m_accel);
214 }
215
216 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
217 while (node)
218 {
219 wxMenuItem *menuitem = node->GetData();
220 if (menuitem->IsSubMenu())
221 AttachToFrame(menuitem->GetSubMenu(), frame);
222 node = node->GetNext();
223 }
224 }
225
226 } // anonymous namespace
227
SetLayoutDirection(wxLayoutDirection dir)228 void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir)
229 {
230 if ( dir == wxLayout_Default )
231 {
232 const wxWindow *const frame = GetFrame();
233 if ( frame )
234 {
235 // inherit layout from frame.
236 dir = frame->GetLayoutDirection();
237 }
238 else // use global layout
239 {
240 dir = wxTheApp->GetLayoutDirection();
241 }
242 }
243
244 if ( dir == wxLayout_Default )
245 return;
246
247 GTKSetLayout(m_menubar, dir);
248
249 // also set the layout of all menus we already have (new ones will inherit
250 // the current layout)
251 for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst();
252 node;
253 node = node->GetNext() )
254 {
255 wxMenu *const menu = node->GetData();
256 menu->SetLayoutDirection(dir);
257 }
258 }
259
GetLayoutDirection() const260 wxLayoutDirection wxMenuBar::GetLayoutDirection() const
261 {
262 return GTKGetLayout(m_menubar);
263 }
264
Attach(wxFrame * frame)265 void wxMenuBar::Attach(wxFrame *frame)
266 {
267 wxMenuBarBase::Attach(frame);
268
269 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
270 while (node)
271 {
272 wxMenu *menu = node->GetData();
273 AttachToFrame( menu, frame );
274 node = node->GetNext();
275 }
276
277 SetLayoutDirection(wxLayout_Default);
278 }
279
Detach()280 void wxMenuBar::Detach()
281 {
282 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
283 while (node)
284 {
285 wxMenu *menu = node->GetData();
286 DetachFromFrame( menu, m_menuBarFrame );
287 node = node->GetNext();
288 }
289
290 EnsureNoGrab(m_widget);
291
292 wxMenuBarBase::Detach();
293 }
294
Append(wxMenu * menu,const wxString & title)295 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
296 {
297 if (wxMenuBarBase::Append(menu, title))
298 {
299 GtkAppend(menu, title);
300 return true;
301 }
302 return false;
303 }
304
GtkAppend(wxMenu * menu,const wxString & title,int pos)305 void wxMenuBar::GtkAppend(wxMenu* menu, const wxString& title, int pos)
306 {
307 menu->SetLayoutDirection(GetLayoutDirection());
308
309 {
310 // This doesn't have much effect right now.
311 menu->SetTitle( title );
312
313 const wxString str(wxConvertMnemonicsToGTK(title));
314 // The "m_owner" is the "menu item"
315 menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) );
316
317 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
318 }
319 g_object_ref(menu->m_owner);
320
321 gtk_widget_show( menu->m_owner );
322
323 if (pos == -1)
324 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
325 else
326 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
327
328 if ( m_menuBarFrame )
329 AttachToFrame( menu, m_menuBarFrame );
330 }
331
Insert(size_t pos,wxMenu * menu,const wxString & title)332 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
333 {
334 if (wxMenuBarBase::Insert(pos, menu, title))
335 {
336 GtkAppend(menu, title, int(pos));
337 return true;
338 }
339 return false;
340 }
341
Replace(size_t pos,wxMenu * menu,const wxString & title)342 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
343 {
344 // remove the old item and insert a new one
345 wxMenu *menuOld = Remove(pos);
346 if ( menuOld && !Insert(pos, menu, title) )
347 {
348 return NULL;
349 }
350
351 // either Insert() succeeded or Remove() failed and menuOld is NULL
352 return menuOld;
353 }
354
Remove(size_t pos)355 wxMenu *wxMenuBar::Remove(size_t pos)
356 {
357 wxMenu *menu = wxMenuBarBase::Remove(pos);
358 if ( !menu )
359 return NULL;
360
361 // remove item from menubar before destroying item to avoid spurious
362 // warnings from Ubuntu libdbusmenu
363 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
364 // remove submenu to avoid destroying it when item is destroyed
365 #ifdef __WXGTK3__
366 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->m_owner), NULL);
367 #else
368 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(menu->m_owner));
369 #endif
370
371 gtk_widget_destroy( menu->m_owner );
372 g_object_unref(menu->m_owner);
373 menu->m_owner = NULL;
374
375 if ( m_menuBarFrame )
376 DetachFromFrame( menu, m_menuBarFrame );
377
378 return menu;
379 }
380
FindMenuItemRecursive(const wxMenu * menu,const wxString & menuString,const wxString & itemString)381 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
382 {
383 if (wxMenuItem::GetLabelText(menu->GetTitle()) == wxMenuItem::GetLabelText(menuString))
384 {
385 int res = menu->FindItem( itemString );
386 if (res != wxNOT_FOUND)
387 return res;
388 }
389
390 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
391 while (node)
392 {
393 wxMenuItem *item = node->GetData();
394 if (item->IsSubMenu())
395 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
396
397 node = node->GetNext();
398 }
399
400 return wxNOT_FOUND;
401 }
402
FindMenuItem(const wxString & menuString,const wxString & itemString) const403 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
404 {
405 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
406 while (node)
407 {
408 wxMenu *menu = node->GetData();
409 int res = FindMenuItemRecursive( menu, menuString, itemString);
410 if (res != -1)
411 return res;
412 node = node->GetNext();
413 }
414
415 return wxNOT_FOUND;
416 }
417
418 // Find a wxMenuItem using its id. Recurses down into sub-menus
FindMenuItemByIdRecursive(const wxMenu * menu,int id)419 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
420 {
421 wxMenuItem* result = menu->FindChildItem(id);
422
423 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
424 while ( node && result == NULL )
425 {
426 wxMenuItem *item = node->GetData();
427 if (item->IsSubMenu())
428 {
429 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
430 }
431 node = node->GetNext();
432 }
433
434 return result;
435 }
436
FindItem(int id,wxMenu ** menuForItem) const437 wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
438 {
439 wxMenuItem* result = 0;
440 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
441 while (node && result == 0)
442 {
443 wxMenu *menu = node->GetData();
444 result = FindMenuItemByIdRecursive( menu, id );
445 node = node->GetNext();
446 }
447
448 if ( menuForItem )
449 {
450 *menuForItem = result ? result->GetMenu() : NULL;
451 }
452
453 return result;
454 }
455
EnableTop(size_t pos,bool flag)456 void wxMenuBar::EnableTop( size_t pos, bool flag )
457 {
458 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
459
460 wxCHECK_RET( node, wxT("menu not found") );
461
462 wxMenu* menu = node->GetData();
463
464 if (menu->m_owner)
465 gtk_widget_set_sensitive( menu->m_owner, flag );
466 }
467
IsEnabledTop(size_t pos) const468 bool wxMenuBar::IsEnabledTop(size_t pos) const
469 {
470 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
471 wxCHECK_MSG( node, false, wxS("invalid index in IsEnabledTop") );
472 wxMenu* const menu = node->GetData();
473 wxCHECK_MSG( menu->m_owner, true, wxS("no menu owner?") );
474 return gtk_widget_get_sensitive( menu->m_owner ) != 0;
475 }
476
GetMenuLabel(size_t pos) const477 wxString wxMenuBar::GetMenuLabel( size_t pos ) const
478 {
479 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
480
481 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
482
483 wxMenu* menu = node->GetData();
484
485 return menu->GetTitle();
486 }
487
SetMenuLabel(size_t pos,const wxString & label)488 void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
489 {
490 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
491
492 wxCHECK_RET( node, wxT("menu not found") );
493
494 wxMenu* menu = node->GetData();
495
496 menu->SetTitle( label );
497
498 const wxString str(wxConvertMnemonicsToGTK(label));
499 if (menu->m_owner)
500 gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menu->m_owner))), wxGTK_CONV(str));
501 }
502
503 //-----------------------------------------------------------------------------
504 // "activate"
505 //-----------------------------------------------------------------------------
506
507 extern "C" {
menuitem_activate(GtkWidget *,wxMenuItem * item)508 static void menuitem_activate(GtkWidget*, wxMenuItem* item)
509 {
510 if (!item->IsEnabled())
511 return;
512
513 if ( !IsMenuEventAllowed(item->GetMenu()) )
514 return;
515
516 int id = item->GetId();
517 if (id == wxGTK_TITLE_ID)
518 {
519 // ignore events from the menu title
520 return;
521 }
522
523 if (item->IsCheckable())
524 {
525 bool isReallyChecked = item->IsChecked(),
526 isInternallyChecked = item->wxMenuItemBase::IsChecked();
527
528 // ensure that the internal state is always consistent with what is
529 // shown on the screen
530 item->wxMenuItemBase::Check(isReallyChecked);
531
532 // we must not report the events for the radio button going up nor the
533 // events resulting from the calls to wxMenuItem::Check()
534 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
535 (isInternallyChecked == isReallyChecked) )
536 {
537 return;
538 }
539 }
540
541 wxMenu* menu = item->GetMenu();
542 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
543
544 // A lot of existing code, including any program that closes its main
545 // window from a menu handler and expects the program to exit -- as our own
546 // minimal sample -- relies on getting an idle event after a menu event.
547 // But when using Ubuntu Unity detached menus, we get called from a DBUS
548 // handler called from g_timeout_dispatch() and Glib doesn't send us any
549 // idle events after it. So ask explicitly for an idle event to get one.
550 wxWakeUpIdle();
551 }
552 }
553
554 //-----------------------------------------------------------------------------
555 // "select"
556 //-----------------------------------------------------------------------------
557
558 extern "C" {
menuitem_select(GtkWidget *,wxMenuItem * item)559 static void menuitem_select(GtkWidget*, wxMenuItem* item)
560 {
561 if (!item->IsEnabled())
562 return;
563
564 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item->GetId(), item->GetMenu());
565 DoCommonMenuCallbackCode(item->GetMenu(), event);
566 }
567 }
568
569 //-----------------------------------------------------------------------------
570 // "deselect"
571 //-----------------------------------------------------------------------------
572
573 extern "C" {
menuitem_deselect(GtkWidget *,wxMenuItem * item)574 static void menuitem_deselect(GtkWidget*, wxMenuItem* item)
575 {
576 if (!item->IsEnabled())
577 return;
578
579 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, wxID_NONE, item->GetMenu());
580 DoCommonMenuCallbackCode(item->GetMenu(), event);
581 }
582 }
583
584 //-----------------------------------------------------------------------------
585 // wxMenuItem
586 //-----------------------------------------------------------------------------
587
New(wxMenu * parentMenu,int id,const wxString & name,const wxString & help,wxItemKind kind,wxMenu * subMenu)588 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
589 int id,
590 const wxString& name,
591 const wxString& help,
592 wxItemKind kind,
593 wxMenu *subMenu)
594 {
595 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
596 }
597
wxMenuItem(wxMenu * parentMenu,int id,const wxString & text,const wxString & help,wxItemKind kind,wxMenu * subMenu)598 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
599 int id,
600 const wxString& text,
601 const wxString& help,
602 wxItemKind kind,
603 wxMenu *subMenu)
604 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
605 {
606 m_menuItem = NULL;
607 }
608
609 #if WXWIN_COMPATIBILITY_2_8
wxMenuItem(wxMenu * parentMenu,int id,const wxString & text,const wxString & help,bool isCheckable,wxMenu * subMenu)610 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
611 int id,
612 const wxString& text,
613 const wxString& help,
614 bool isCheckable,
615 wxMenu *subMenu)
616 : wxMenuItemBase(parentMenu, id, text, help,
617 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
618 {
619 m_menuItem = NULL;
620 }
621 #endif
622
~wxMenuItem()623 wxMenuItem::~wxMenuItem()
624 {
625 if (m_menuItem)
626 g_object_unref(m_menuItem);
627 // don't delete menu items, the menus take care of that
628 }
629
SetMenuItem(GtkWidget * menuItem)630 void wxMenuItem::SetMenuItem(GtkWidget* menuItem)
631 {
632 if (m_menuItem)
633 g_object_unref(m_menuItem);
634 m_menuItem = menuItem;
635 if (menuItem)
636 g_object_ref(menuItem);
637 }
638
SetItemLabel(const wxString & str)639 void wxMenuItem::SetItemLabel( const wxString& str )
640 {
641 #if wxUSE_ACCEL
642 if (m_menuItem)
643 {
644 // remove old accelerator
645 guint accel_key;
646 GdkModifierType accel_mods;
647 if ( wxGetGtkAccel(this, &accel_key, &accel_mods) )
648 {
649 gtk_widget_remove_accelerator(
650 m_menuItem, GetRootParentMenu(m_parentMenu)->m_accel, accel_key, accel_mods);
651 }
652 }
653 #endif // wxUSE_ACCEL
654 wxMenuItemBase::SetItemLabel(str);
655 if (m_menuItem)
656 SetGtkLabel();
657 }
658
SetGtkLabel()659 void wxMenuItem::SetGtkLabel()
660 {
661 const wxString text = wxConvertMnemonicsToGTK(m_text.BeforeFirst('\t'));
662 GtkLabel* label = GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_menuItem)));
663 gtk_label_set_text_with_mnemonic(label, wxGTK_CONV_SYS(text));
664 #if wxUSE_ACCEL
665 guint accel_key;
666 GdkModifierType accel_mods;
667 if ( wxGetGtkAccel(this, &accel_key, &accel_mods) )
668 {
669 gtk_widget_add_accelerator(
670 m_menuItem, "activate", GetRootParentMenu(m_parentMenu)->m_accel,
671 accel_key, accel_mods, GTK_ACCEL_VISIBLE);
672 }
673 else
674 {
675 // Remove the accelerator since it couldn't be made or is invalid
676 m_text = m_text.BeforeFirst( wxS( '\t' ) );
677 }
678 #endif // wxUSE_ACCEL
679 }
680
SetBitmap(const wxBitmap & bitmap)681 void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
682 {
683 if (m_kind == wxITEM_NORMAL)
684 m_bitmap = bitmap;
685 else
686 {
687 wxFAIL_MSG("only normal menu items can have bitmaps");
688 }
689 }
690
Check(bool check)691 void wxMenuItem::Check( bool check )
692 {
693 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
694
695 if (check == m_isChecked)
696 return;
697
698 switch ( GetKind() )
699 {
700 case wxITEM_RADIO:
701 // It doesn't make sense to uncheck a radio item.
702 if ( !check )
703 return;
704
705 wxFALLTHROUGH;
706 case wxITEM_CHECK:
707 wxMenuItemBase::Check( check );
708 gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check );
709 break;
710
711 default:
712 wxFAIL_MSG( wxT("can't check this item") );
713 }
714 }
715
Enable(bool enable)716 void wxMenuItem::Enable( bool enable )
717 {
718 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
719
720 gtk_widget_set_sensitive( m_menuItem, enable );
721 wxMenuItemBase::Enable( enable );
722 }
723
IsChecked() const724 bool wxMenuItem::IsChecked() const
725 {
726 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
727
728 wxCHECK_MSG( IsCheckable(), false,
729 wxT("can't get state of uncheckable item!") );
730
731 return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(m_menuItem)) != 0;
732 }
733
734 //-----------------------------------------------------------------------------
735 // wxMenu
736 //-----------------------------------------------------------------------------
737
738 extern "C" {
739 // "map" from m_menu
menu_map(GtkWidget *,wxMenu * menu)740 static void menu_map(GtkWidget*, wxMenu* menu)
741 {
742 wxMenuEvent event(wxEVT_MENU_OPEN, menu->m_popupShown ? -1 : 0, menu);
743 DoCommonMenuCallbackCode(menu, event);
744 }
745
746 // "hide" from m_menu
menu_hide(GtkWidget *,wxMenu * menu)747 static void menu_hide(GtkWidget*, wxMenu* menu)
748 {
749 // When using Ubuntu Unity desktop environment we get "hide" signal even
750 // when the window is not shown yet because Unity hides all the menus to
751 // show them only in the global menu bar. Just ignore this even instead of
752 // crashing in DoCommonMenuCallbackCode().
753 if ( !menu->GetWindow() )
754 return;
755
756 wxMenuEvent event(wxEVT_MENU_CLOSE, menu->m_popupShown ? -1 : 0, menu);
757 menu->m_popupShown = false;
758 DoCommonMenuCallbackCode(menu, event);
759 }
760 }
761
762 // "can_activate_accel" from menu item
763 extern "C" {
can_activate_accel(GtkWidget *,guint,wxMenu * menu)764 static gboolean can_activate_accel(GtkWidget*, guint, wxMenu* menu)
765 {
766 menu->UpdateUI();
767 // always allow our "activate" handler to be called
768 return true;
769 }
770 }
771
Init()772 void wxMenu::Init()
773 {
774 m_popupShown = false;
775
776 m_accel = gtk_accel_group_new();
777 m_menu = gtk_menu_new();
778 g_object_ref_sink(m_menu);
779
780 m_owner = NULL;
781
782 #ifndef __WXGTK4__
783 // Tearoffs are entries, just like separators. So if we want this
784 // menu to be a tear-off one, we just append a tearoff entry
785 // immediately.
786 if ( m_style & wxMENU_TEAROFF )
787 {
788 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
789 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
790 wxGCC_WARNING_RESTORE()
791
792 gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff);
793 }
794 #endif
795
796 // append the title as the very first entry if we have it
797 if ( !m_title.empty() )
798 {
799 Append(wxGTK_TITLE_ID, m_title);
800 AppendSeparator();
801 }
802
803 // "show" occurs for sub-menus which are not showing, so use "map" instead
804 g_signal_connect(m_menu, "map", G_CALLBACK(menu_map), this);
805 g_signal_connect(m_menu, "hide", G_CALLBACK(menu_hide), this);
806 }
807
~wxMenu()808 wxMenu::~wxMenu()
809 {
810 // Destroying a menu generates a "hide" signal even if it's not shown
811 // currently, so disconnect it to avoid dummy wxEVT_MENU_CLOSE events
812 // generation.
813 g_signal_handlers_disconnect_by_data(m_menu, this);
814
815 if (m_owner)
816 {
817 gtk_widget_destroy(m_owner);
818 g_object_unref(m_owner);
819 }
820 else
821 gtk_widget_destroy(m_menu);
822
823 g_object_unref(m_menu);
824 g_object_unref(m_accel);
825 }
826
SetLayoutDirection(wxLayoutDirection dir)827 void wxMenu::SetLayoutDirection(wxLayoutDirection dir)
828 {
829 if ( m_owner )
830 {
831 wxWindow::GTKSetLayout(m_owner, dir);
832
833 wxMenuItemList::compatibility_iterator node = m_items.GetFirst();
834 for (; node; node = node->GetNext())
835 {
836 wxMenuItem* item = node->GetData();
837 if (wxMenu* subMenu = item->GetSubMenu())
838 subMenu->SetLayoutDirection(dir);
839 else if (GtkWidget* widget = item->GetMenuItem())
840 {
841 wxWindow::GTKSetLayout(widget, dir);
842 widget = gtk_bin_get_child(GTK_BIN(widget));
843 if (widget)
844 wxWindow::GTKSetLayout(widget, dir);
845 }
846 }
847 }
848 //else: will be called later by wxMenuBar again
849 }
850
GetLayoutDirection() const851 wxLayoutDirection wxMenu::GetLayoutDirection() const
852 {
853 return wxWindow::GTKGetLayout(m_owner);
854 }
855
GetTitle() const856 wxString wxMenu::GetTitle() const
857 {
858 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
859 }
860
SetTitle(const wxString & title)861 void wxMenu::SetTitle(const wxString& title)
862 {
863 wxMenuBase::SetTitle(wxConvertMnemonicsToGTK(title));
864 }
865
GtkAppend(wxMenuItem * mitem,int pos)866 void wxMenu::GtkAppend(wxMenuItem* mitem, int pos)
867 {
868 GtkWidget *menuItem;
869 switch (mitem->GetKind())
870 {
871 case wxITEM_SEPARATOR:
872 menuItem = gtk_separator_menu_item_new();
873 break;
874 case wxITEM_CHECK:
875 menuItem = gtk_check_menu_item_new_with_label("");
876 break;
877 case wxITEM_RADIO:
878 {
879 // See if we need to create a new radio group for this item or
880 // add it to an existing one.
881 wxMenuItem* radioGroupItem = NULL;
882
883 const size_t numItems = GetMenuItemCount();
884 const size_t n = pos == -1 ? numItems - 1 : size_t(pos);
885
886 if (n != 0)
887 {
888 wxMenuItem* const itemPrev = FindItemByPosition(n - 1);
889 if ( itemPrev->GetKind() == wxITEM_RADIO )
890 {
891 // Appending an item after an existing radio item puts
892 // it into the same radio group.
893 radioGroupItem = itemPrev;
894 }
895 }
896
897 if (radioGroupItem == NULL && n != numItems - 1)
898 {
899 wxMenuItem* const itemNext = FindItemByPosition(n + 1);
900 if ( itemNext->GetKind() == wxITEM_RADIO )
901 {
902 // Inserting an item before an existing radio item
903 // also puts it into the existing radio group.
904 radioGroupItem = itemNext;
905 }
906 }
907
908 GSList* group = NULL;
909 if ( radioGroupItem )
910 {
911 group = gtk_radio_menu_item_get_group(
912 GTK_RADIO_MENU_ITEM(radioGroupItem->GetMenuItem())
913 );
914 }
915
916 menuItem = gtk_radio_menu_item_new_with_label(group, "");
917 }
918 break;
919 default:
920 wxFAIL_MSG("unexpected menu item kind");
921 wxFALLTHROUGH;
922 case wxITEM_NORMAL:
923 #ifdef __WXGTK4__
924 //TODO GtkImageMenuItem is gone, have to implement it ourselves with
925 // GtkMenuItem GtkBox GtkAccelLabel GtkImage
926 menuItem = gtk_menu_item_new_with_label("");
927 #else
928 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
929 const wxBitmap& bitmap = mitem->GetBitmap();
930 if (bitmap.IsOk())
931 {
932 GtkWidget* image = wxGtkImage::New();
933 WX_GTK_IMAGE(image)->Set(bitmap);
934 menuItem = gtk_image_menu_item_new_with_label("");
935 gtk_widget_show(image);
936 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuItem), image);
937 }
938 else
939 {
940 const char* stockid = wxGetStockGtkID(mitem->GetId());
941 if (stockid)
942 // use stock bitmap for this item if available on the assumption
943 // that it never hurts to follow GTK+ conventions more closely
944 menuItem = gtk_image_menu_item_new_from_stock(stockid, NULL);
945 else
946 menuItem = gtk_menu_item_new_with_label("");
947 }
948 wxGCC_WARNING_RESTORE()
949 #endif
950 break;
951 }
952 mitem->SetMenuItem(menuItem);
953
954 gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos);
955
956 gtk_widget_show( menuItem );
957
958 if ( !mitem->IsSeparator() )
959 {
960 mitem->SetGtkLabel();
961 if ( mitem->IsSubMenu() )
962 UpdateSubMenuItemLabels(mitem);
963
964 g_signal_connect (menuItem, "select",
965 G_CALLBACK(menuitem_select), mitem);
966 g_signal_connect (menuItem, "deselect",
967 G_CALLBACK(menuitem_deselect), mitem);
968
969 if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK )
970 {
971 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
972
973 gtk_widget_show( mitem->GetSubMenu()->m_menu );
974 }
975 else
976 {
977 g_signal_connect(menuItem, "can_activate_accel",
978 G_CALLBACK(can_activate_accel), this);
979 g_signal_connect (menuItem, "activate",
980 G_CALLBACK(menuitem_activate),
981 mitem);
982 }
983 }
984 }
985
DoAppend(wxMenuItem * mitem)986 wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem)
987 {
988 if (wxMenuBase::DoAppend(mitem))
989 {
990 GtkAppend(mitem);
991 return mitem;
992 }
993 return NULL;
994 }
995
DoInsert(size_t pos,wxMenuItem * item)996 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
997 {
998 if (wxMenuBase::DoInsert(pos, item))
999 {
1000 GtkAppend(item, int(pos));
1001 return item;
1002 }
1003 return NULL;
1004 }
1005
DoRemove(wxMenuItem * item)1006 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
1007 {
1008 if ( !wxMenuBase::DoRemove(item) )
1009 return NULL;
1010
1011 GtkWidget * const mitem = item->GetMenuItem();
1012
1013 g_signal_handlers_disconnect_by_data(mitem, item);
1014
1015 #ifdef __WXGTK3__
1016 gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL);
1017 #else
1018 gtk_menu_item_remove_submenu(GTK_MENU_ITEM(mitem));
1019 #endif
1020
1021 gtk_widget_destroy(mitem);
1022 item->SetMenuItem(NULL);
1023
1024 return item;
1025 }
1026
Attach(wxMenuBarBase * menubar)1027 void wxMenu::Attach(wxMenuBarBase *menubar)
1028 {
1029 wxMenuBase::Attach(menubar);
1030
1031 // inherit layout direction from menubar.
1032 SetLayoutDirection(menubar->GetLayoutDirection());
1033 }
1034
1035 // ----------------------------------------------------------------------------
1036 // helpers
1037 // ----------------------------------------------------------------------------
1038
1039 #if wxUSE_ACCEL
1040
GetGtkHotKey(const wxMenuItem & item)1041 static wxString GetGtkHotKey( const wxMenuItem& item )
1042 {
1043 wxString hotkey;
1044
1045 wxAcceleratorEntry *accel = item.GetAccel();
1046 if ( accel )
1047 {
1048 int flags = accel->GetFlags();
1049 if ( flags & wxACCEL_ALT )
1050 hotkey += wxT("<alt>");
1051 if ( flags & wxACCEL_CTRL )
1052 hotkey += wxT("<control>");
1053 if ( flags & wxACCEL_SHIFT )
1054 hotkey += wxT("<shift>");
1055
1056 // Accelerator can be invalid for 2 different reasons from GTK point of
1057 // view: either the key just can't be used as an accelerator at all
1058 // (e.g. TAB), or it can only be used as an accelerator with modifiers.
1059 // This variable can be set to one of the matching values in the switch
1060 // below and is used to give the most fitting error message later.
1061 enum Validity
1062 {
1063 Valid_Always,
1064 Valid_Never,
1065 Valid_WithModifiers
1066 } validity = Valid_Always;
1067
1068 int code = accel->GetKeyCode();
1069 switch ( code )
1070 {
1071 case WXK_F1: case WXK_F2: case WXK_F3: case WXK_F4: case WXK_F5:
1072 case WXK_F6: case WXK_F7: case WXK_F8: case WXK_F9: case WXK_F10:
1073 case WXK_F11: case WXK_F12: case WXK_F13: case WXK_F14: case WXK_F15:
1074 case WXK_F16: case WXK_F17: case WXK_F18: case WXK_F19: case WXK_F20:
1075 case WXK_F21: case WXK_F22: case WXK_F23: case WXK_F24:
1076 hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1);
1077 break;
1078
1079 // TODO: we should use gdk_keyval_name() (a.k.a.
1080 // XKeysymToString) here as well as hardcoding the keysym
1081 // names this might be not portable
1082 case WXK_INSERT:
1083 hotkey << wxT("Insert" );
1084 break;
1085 case WXK_DELETE:
1086 hotkey << wxT("Delete" );
1087 break;
1088 case WXK_UP:
1089 validity = Valid_WithModifiers;
1090 hotkey << wxT("Up" );
1091 break;
1092 case WXK_DOWN:
1093 validity = Valid_WithModifiers;
1094 hotkey << wxT("Down" );
1095 break;
1096 case WXK_LEFT:
1097 validity = Valid_WithModifiers;
1098 hotkey << wxT("Left" );
1099 break;
1100 case WXK_RIGHT:
1101 validity = Valid_WithModifiers;
1102 hotkey << wxT("Right" );
1103 break;
1104 case WXK_PAGEUP:
1105 hotkey << wxT("Page_Up" );
1106 break;
1107 case WXK_PAGEDOWN:
1108 hotkey << wxT("Page_Down" );
1109 break;
1110 case WXK_HOME:
1111 hotkey << wxT("Home" );
1112 break;
1113 case WXK_END:
1114 hotkey << wxT("End" );
1115 break;
1116 case WXK_RETURN:
1117 hotkey << wxT("Return" );
1118 break;
1119 case WXK_BACK:
1120 hotkey << wxT("BackSpace" );
1121 break;
1122 case WXK_ESCAPE:
1123 hotkey << wxT("Escape" );
1124 break;
1125 case WXK_SPACE:
1126 hotkey << wxT("space" );
1127 break;
1128 case WXK_MULTIPLY:
1129 hotkey << wxT("multiply" );
1130 break;
1131 case WXK_CANCEL:
1132 hotkey << wxT("Cancel" );
1133 break;
1134 case WXK_CLEAR:
1135 hotkey << wxT("Clear" );
1136 break;
1137 case WXK_MENU:
1138 hotkey << wxT("Menu" );
1139 break;
1140 case WXK_PAUSE:
1141 hotkey << wxT("Pause" );
1142 break;
1143 case WXK_SELECT:
1144 hotkey << wxT("Select" );
1145 break;
1146 case WXK_PRINT:
1147 hotkey << wxT("Print" );
1148 break;
1149 case WXK_EXECUTE:
1150 hotkey << wxT("Execute" );
1151 break;
1152 case WXK_HELP:
1153 hotkey << wxT("Help" );
1154 break;
1155 case WXK_NUMPAD_INSERT:
1156 hotkey << wxT("KP_Insert" );
1157 break;
1158 case WXK_NUMPAD_DELETE:
1159 hotkey << wxT("KP_Delete" );
1160 break;
1161 case WXK_NUMPAD_SPACE:
1162 hotkey << wxT("KP_Space" );
1163 break;
1164 case WXK_NUMPAD_ENTER:
1165 hotkey << wxT("KP_Enter" );
1166 break;
1167 case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3:
1168 case WXK_NUMPAD_F4:
1169 hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1);
1170 break;
1171 case WXK_NUMPAD_HOME:
1172 hotkey << wxT("KP_Home" );
1173 break;
1174 case WXK_NUMPAD_UP:
1175 validity = Valid_WithModifiers;
1176 hotkey << wxT("KP_Up" );
1177 break;
1178 case WXK_NUMPAD_DOWN:
1179 validity = Valid_WithModifiers;
1180 hotkey << wxT("KP_Down" );
1181 break;
1182 case WXK_NUMPAD_LEFT:
1183 validity = Valid_WithModifiers;
1184 hotkey << wxT("KP_Left" );
1185 break;
1186 case WXK_NUMPAD_RIGHT:
1187 validity = Valid_WithModifiers;
1188 hotkey << wxT("KP_Right" );
1189 break;
1190 case WXK_NUMPAD_PAGEUP:
1191 hotkey << wxT("KP_Page_Up" );
1192 break;
1193 case WXK_NUMPAD_PAGEDOWN:
1194 hotkey << wxT("KP_Page_Down" );
1195 break;
1196 case WXK_NUMPAD_END:
1197 hotkey << wxT("KP_End" );
1198 break;
1199 case WXK_NUMPAD_BEGIN:
1200 hotkey << wxT("KP_Begin" );
1201 break;
1202 case WXK_NUMPAD_EQUAL:
1203 hotkey << wxT("KP_Equal" );
1204 break;
1205 case WXK_NUMPAD_MULTIPLY:
1206 hotkey << wxT("KP_Multiply" );
1207 break;
1208 case WXK_NUMPAD_ADD:
1209 hotkey << wxT("KP_Add" );
1210 break;
1211 case WXK_NUMPAD_SEPARATOR:
1212 hotkey << wxT("KP_Separator" );
1213 break;
1214 case WXK_NUMPAD_SUBTRACT:
1215 hotkey << wxT("KP_Subtract" );
1216 break;
1217 case WXK_NUMPAD_DECIMAL:
1218 hotkey << wxT("KP_Decimal" );
1219 break;
1220 case WXK_NUMPAD_DIVIDE:
1221 hotkey << wxT("KP_Divide" );
1222 break;
1223 case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2:
1224 case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5:
1225 case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8:
1226 case WXK_NUMPAD9:
1227 hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0);
1228 break;
1229 case WXK_WINDOWS_MENU:
1230 hotkey << wxT("Menu" );
1231 break;
1232
1233 /*
1234 The following keycodes have been shown not to work as accelerator
1235 keys on GTK (see https://trac.wxwidgets.org/ticket/10049)
1236 and are not valid
1237 (see gtkaccelgroup.c inside gtk_accelerator_valid())
1238 */
1239 case WXK_COMMAND: // Same as WXK_CONTROL
1240 case WXK_SHIFT:
1241 case WXK_ALT:
1242 case WXK_SCROLL: // Scroll lock
1243 case WXK_CAPITAL: // Caps lock
1244 case WXK_NUMLOCK:
1245 case WXK_NUMPAD_TAB:
1246 case WXK_TAB:
1247 case WXK_WINDOWS_LEFT:
1248 case WXK_WINDOWS_RIGHT:
1249
1250 /*
1251 The following keycodes do not map clearly into a GTK keycode,
1252 so they are not included in the accelerator mapping:
1253 */
1254 case WXK_ADD:
1255 case WXK_SEPARATOR:
1256 case WXK_SUBTRACT:
1257 case WXK_DECIMAL:
1258 case WXK_DIVIDE:
1259 case WXK_SNAPSHOT:
1260
1261 /*
1262 The following special codes do not map into GTK keycodes,
1263 see gdk/keynames.txt
1264 */
1265 case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3:
1266 case WXK_SPECIAL4: case WXK_SPECIAL5: case WXK_SPECIAL6:
1267 case WXK_SPECIAL7: case WXK_SPECIAL8: case WXK_SPECIAL9:
1268 case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12:
1269 case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15:
1270 case WXK_SPECIAL16: case WXK_SPECIAL17: case WXK_SPECIAL18:
1271 case WXK_SPECIAL19: case WXK_SPECIAL20:
1272 validity = Valid_Never;
1273 break;
1274
1275 // if there are any other keys wxAcceleratorEntry::Create() may
1276 // return, we should process them here
1277
1278 default:
1279 if ( code < 127 )
1280 {
1281 const wxString
1282 name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code));
1283 if ( !name.empty() )
1284 {
1285 hotkey << name;
1286 break;
1287 }
1288 }
1289
1290 wxLogDebug( "Unknown keyboard accelerator key code: %i", code );
1291 hotkey.clear();
1292 }
1293
1294 switch ( validity )
1295 {
1296 case Valid_Always:
1297 break;
1298
1299 case Valid_Never:
1300 wxLogDebug("\"%s\" is not supported as "
1301 "a keyboard accelerator with GTK",
1302 accel->ToString());
1303 hotkey.clear();
1304 break;
1305
1306 case Valid_WithModifiers:
1307 if ( !flags )
1308 {
1309 wxLogDebug("\"%s\" must use modifiers to be used as "
1310 "a keyboard accelerator with GTK",
1311 accel->ToString());
1312 hotkey.clear();
1313 }
1314 break;
1315 }
1316
1317 delete accel;
1318 }
1319
1320 return hotkey;
1321 }
1322
1323 static bool
wxGetGtkAccel(const wxMenuItem * item,guint * accel_key,GdkModifierType * accel_mods)1324 wxGetGtkAccel(const wxMenuItem* item, guint* accel_key, GdkModifierType* accel_mods)
1325 {
1326 const wxString string = GetGtkHotKey(*item);
1327 if (!string.empty())
1328 {
1329 gtk_accelerator_parse(wxGTK_CONV_SYS(string), accel_key, accel_mods);
1330
1331 // Normally, we detect all the keys considered invalid by GTK in
1332 // GetGtkHotKey(), but just in case GTK decides to add more invalid
1333 // keys in the future versions, recheck once again using its function.
1334 if ( gtk_accelerator_valid(*accel_key, *accel_mods) )
1335 return true;
1336
1337 wxLogDebug("\"%s\" is not a valid keyboard accelerator "
1338 "for this GTK version",
1339 string);
1340 }
1341 #ifndef __WXGTK4__
1342 else
1343 {
1344 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
1345
1346 // Check if this is a stock item for which GTK defines a default
1347 // accelerator.
1348 GtkStockItem stock_item;
1349 const char* stockid = wxGetStockGtkID(item->GetId());
1350 if ( stockid &&
1351 gtk_stock_lookup(stockid, &stock_item) &&
1352 stock_item.keyval )
1353 {
1354 *accel_key = stock_item.keyval;
1355 *accel_mods = stock_item.modifier;
1356
1357 return true;
1358 }
1359 wxGCC_WARNING_RESTORE()
1360 }
1361 #endif
1362
1363 return false;
1364 }
1365 #endif // wxUSE_ACCEL
1366
1367 #ifndef __WXGTK4__
1368 wxGCC_WARNING_SUPPRESS(deprecated-declarations)
1369 wxGCC_WARNING_SUPPRESS(cast-qual)
wxGetStockGtkID(wxWindowID id)1370 const char *wxGetStockGtkID(wxWindowID id)
1371 {
1372 #define STOCKITEM(wx,gtk) \
1373 case wx: \
1374 return gtk;
1375
1376 #if GTK_CHECK_VERSION(2,8,0)
1377 #define STOCKITEM_28(wx,gtk) STOCKITEM(wx,gtk)
1378 #else
1379 #define STOCKITEM_28(wx,gtk)
1380 #endif
1381
1382 #if GTK_CHECK_VERSION(2,10,0)
1383 #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk)
1384 #else
1385 #define STOCKITEM_210(wx,gtk)
1386 #endif
1387
1388
1389 switch (id)
1390 {
1391 STOCKITEM(wxID_ABOUT, GTK_STOCK_ABOUT)
1392 STOCKITEM(wxID_ADD, GTK_STOCK_ADD)
1393 STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY)
1394 STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK)
1395 STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD)
1396 STOCKITEM(wxID_BOTTOM, GTK_STOCK_GOTO_BOTTOM)
1397 STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL)
1398 STOCKITEM(wxID_CDROM, GTK_STOCK_CDROM)
1399 STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR)
1400 STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE)
1401 STOCKITEM(wxID_CONVERT, GTK_STOCK_CONVERT)
1402 STOCKITEM(wxID_COPY, GTK_STOCK_COPY)
1403 STOCKITEM(wxID_CUT, GTK_STOCK_CUT)
1404 STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE)
1405 STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN)
1406 STOCKITEM(wxID_EDIT, GTK_STOCK_EDIT)
1407 STOCKITEM(wxID_EXECUTE, GTK_STOCK_EXECUTE)
1408 STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT)
1409 STOCKITEM(wxID_FILE, GTK_STOCK_FILE)
1410 STOCKITEM(wxID_FIND, GTK_STOCK_FIND)
1411 STOCKITEM(wxID_FIRST, GTK_STOCK_GOTO_FIRST)
1412 STOCKITEM(wxID_FLOPPY, GTK_STOCK_FLOPPY)
1413 STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD)
1414 STOCKITEM(wxID_HARDDISK, GTK_STOCK_HARDDISK)
1415 STOCKITEM(wxID_HELP, GTK_STOCK_HELP)
1416 STOCKITEM(wxID_HOME, GTK_STOCK_HOME)
1417 STOCKITEM(wxID_INDENT, GTK_STOCK_INDENT)
1418 STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX)
1419 STOCKITEM_28(wxID_INFO, GTK_STOCK_INFO)
1420 STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC)
1421 STOCKITEM(wxID_JUMP_TO, GTK_STOCK_JUMP_TO)
1422 STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER)
1423 STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL)
1424 STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT)
1425 STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT)
1426 STOCKITEM(wxID_LAST, GTK_STOCK_GOTO_LAST)
1427 STOCKITEM(wxID_NETWORK, GTK_STOCK_NETWORK)
1428 STOCKITEM(wxID_NEW, GTK_STOCK_NEW)
1429 STOCKITEM(wxID_NO, GTK_STOCK_NO)
1430 STOCKITEM(wxID_OK, GTK_STOCK_OK)
1431 STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN)
1432 STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE)
1433 STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES)
1434 STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW)
1435 STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT)
1436 STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES)
1437 STOCKITEM(wxID_REDO, GTK_STOCK_REDO)
1438 STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH)
1439 STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE)
1440 STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE)
1441 STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED)
1442 STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE)
1443 STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS)
1444 STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL)
1445 STOCKITEM(wxID_SELECT_COLOR, GTK_STOCK_SELECT_COLOR)
1446 STOCKITEM(wxID_SELECT_FONT, GTK_STOCK_SELECT_FONT)
1447 STOCKITEM(wxID_SORT_ASCENDING, GTK_STOCK_SORT_ASCENDING)
1448 STOCKITEM(wxID_SORT_DESCENDING, GTK_STOCK_SORT_DESCENDING)
1449 STOCKITEM(wxID_SPELL_CHECK, GTK_STOCK_SPELL_CHECK)
1450 STOCKITEM(wxID_STOP, GTK_STOCK_STOP)
1451 STOCKITEM(wxID_STRIKETHROUGH, GTK_STOCK_STRIKETHROUGH)
1452 STOCKITEM(wxID_TOP, GTK_STOCK_GOTO_TOP)
1453 STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE)
1454 STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE)
1455 STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO)
1456 STOCKITEM(wxID_UNINDENT, GTK_STOCK_UNINDENT)
1457 STOCKITEM(wxID_UP, GTK_STOCK_GO_UP)
1458 STOCKITEM(wxID_YES, GTK_STOCK_YES)
1459 STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100)
1460 STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT)
1461 STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN)
1462 STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT)
1463
1464 default:
1465 break;
1466 }
1467
1468 #undef STOCKITEM
1469
1470 return NULL;
1471 }
1472 wxGCC_WARNING_RESTORE(cast-qual)
1473 wxGCC_WARNING_RESTORE()
1474 #endif // !__WXGTK4__
1475
1476 #endif // wxUSE_MENUS
1477