Debian Codegraph @

gtk/src/actiongroup.ccg

raw
1 /* Copyright (C) 2003 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 <glibmm/vectorutils.h>
18
19 #include <gtk/gtk.h>
20 #include <gtkmm/accelmap.h>
21
22
23 namespace Gtk
24 {
25
26 _DEPRECATE_IFDEF_START
27 void ActionGroup::add(const Glib::RefPtr<Action>& action)
28 {
29 gtk_action_group_add_action_with_accel(gobj(), Glib::unwrap(action),
30 nullptr /* accelerator=nullptr means use the stock accelerator if this is a stock item */ );
31 }
32
33 void ActionGroup::add(const Glib::RefPtr<Action>& action, const AccelKey& accel_key)
34 {
35 // We need to half-duplicate the gtk_action_group_add_action_with_accel() implementation, because we want to
36 // use AccelKey, not just the accelerator string format that is _one_ of the ways to create an AccelKey. murrayc.
37
38 //The AccelKey might have been constructed from key+mod or from an accelerator string,
39 //but it the end that just produces a key+mod.
40 guint key = accel_key.get_key();
41 auto mod = accel_key.get_mod();
42 if(key)
43 {
44 // The AccelKey constructor can take an accel path, so I suppose we should not ignore it,
45 // but I can't imagine how it would be useful with the UIManager. murrayc.
46 auto accel_path = accel_key.get_path();
47 if(accel_path.empty())
48 {
49 //Copied from the gtk_action_group_add_action_with_accel() implementation:
50
51 gchar* action_name = nullptr;
52 g_object_get(G_OBJECT(Glib::unwrap(action)), "name", &action_name, nullptr);
53 if(action_name)
54 {
55 accel_path = "<Actions>/" + get_name() + '/' + action_name;
56 g_free(action_name);
57 }
58 }
59
60 AccelMap::add_entry(accel_path.raw(), key, mod);
61
62 action->set_accel_path(accel_path);
63 }
64
65 gtk_action_group_add_action(gobj(), action->gobj());
66 }
67
68 void ActionGroup::add(const Glib::RefPtr<Action>& action, const Action::SlotActivate& slot)
69 {
70 //This is meant to be similar to the gtk_action_group_add_actions() convenience method that is used from C.
71 //This also does the work of gtk_action_group_add_toggle_actions() and gtk_action_group_add_radio_actions_full(),
72 //because the extra stuff there is already done when the Action (or a derived type, such as ToggleAction) is create()ed.
73
74 action->signal_activate().connect(slot);
75
76 //We probably don't need to use translate_string(), because the label and tooltip will be regular translated strings in our C++ application code.
77 //But C needs it because gtk_action_group_add_actions() takes a static array of GtkActionEntrys whose fields
78 //can not be translated with gettext macros.
79 //But we should soon know if menus and toolbars are not translated in non-english locales. murrayc.
80
81 add(action);
82 }
83
84 void ActionGroup::add(const Glib::RefPtr<Action>& action, const AccelKey& accel_key, const Action::SlotActivate& slot)
85 {
86 //This is meant to be similar to the gtk_action_group_add_actions() convenience method that is used from C.
87 //This also does the work of gtk_action_group_add_toggle_actions() and gtk_action_group_add_radio_actions_full(),
88 //because the extra stuff there is already done when the Action (or a derived type, such as ToggleAction) is create()ed.
89
90 action->signal_activate().connect(slot);
91 add(action, accel_key);
92
93 /*
94 //Create the accel path (a kind of unique key):
95 Glib::ustring accel_path = "<Actions>/" + get_name() + "/" + action->get_name();
96
97 //Register the accel path:
98 Gtk::AccelMap::add_entry(accel_path, accel_key.get_key(), accel_key.get_mod());
99
100 //USe the accel path:
101 action->set_accel_path(accel_path);
102 */
103 }
104 _DEPRECATE_IFDEF_END
105
106 } // namespace Gtk