Debian Codegraph @

gtk/src/menuitem.ccg

raw
1 /* Copyright (C) 1998-2002 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <gtk/gtk.h>
18 #include <gtkmm/label.h>
19 #include <gtkmm/misc.h>
20 #include <gtkmm/menu.h>
21 #include <gtkmm/box.h>
22 #include <gtkmm/window.h>
23 #include <gtkmm/menubar.h>
24 #include <gtkmm/accelgroup.h>
25 #include <gtkmm/accelmap.h>
26
27 // gtk_menu_item_(de)select are just a wrapper for gtk_item_(de)select
28 // and defined in Gtk_Item, so they do not be redefined here
29
30 namespace Gtk
31 {
32
33
34 MenuItem::MenuItem(Widget& widget)
35 :
36 _CONSTRUCT()
37 {
38 add(widget);
39 }
40
41 MenuItem::MenuItem(const Glib::ustring& label, bool mnemonic)
42 :
43 _CONSTRUCT()
44 {
45 add_accel_label(label, mnemonic); /* Left-aligned label */
46 }
47
48 void MenuItem::add_accel_label(const Glib::ustring& label, bool mnemonic)
49 {
50 set_label(label);
51 set_use_underline(mnemonic);
52 }
53
54 void MenuItem::set_accel_key(const AccelKey& accel_key)
55 {
56 accel_key_ = accel_key;
57 }
58
59 bool MenuItem::has_submenu() const
60 {
61 return get_submenu() != nullptr;
62 }
63
64 void MenuItem::accelerate(Window& window)
65 {
66 if(!accel_key_.is_null())
67 {
68 _DEPRECATE_IFDEF_START
69 auto accel_group = window.get_accel_group();
70 #else
71 // Gtk::Window::get_accel_group() does not exist if gtkmm is built
72 // without deprecated API.
73 Glib::RefPtr<AccelGroup> accel_group;
74 auto accel_groups = window.get_accel_groups();
75 if (accel_groups.empty())
76 {
77 // Create an AccelGroup for window, if there is none.
78 accel_group = AccelGroup::create();
79 window.add_accel_group(accel_group);
80 }
81 else
82 accel_group = accel_groups[0];
83 _DEPRECATE_IFDEF_END
84
85 if(accel_key_.get_path().empty())
86 {
87 add_accelerator("activate", accel_group,
88 accel_key_.get_key(), accel_key_.get_mod(), ACCEL_VISIBLE);
89 }
90 else
91 {
92 AccelMap::add_entry(accel_key_.get_path().raw(),
93 accel_key_.get_key(),
94 accel_key_.get_mod());
95
96 Bin::set_accel_path(accel_key_.get_path(), accel_group);
97 }
98 }
99
100 if(has_submenu())
101 get_submenu()->accelerate(window);
102 }
103
104 void MenuItem::unset_accel_path()
105 {
106 gtk_menu_item_set_accel_path(gobj(), nullptr);
107 }
108
109 void MenuItem::unset_submenu()
110 {
111 gtk_menu_item_set_submenu(gobj(), nullptr);
112 }
113
114 } // namespace Gtk