remove unusued ActionManager::get_action() variant
[ardour.git] / libs / gtkmm2ext / actions.cc
1 /*
2     Copyright (C) 2005 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstring>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <stdint.h>
25
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
29
30 #include <gtkmm/accelmap.h>
31 #include <gtkmm/uimanager.h>
32
33 #include "pbd/error.h"
34
35 #include "gtkmm2ext/actions.h"
36 #include "gtkmm2ext/utils.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace Gtk;
42 using namespace Glib;
43 using namespace sigc;
44 using namespace PBD;
45 using namespace Gtkmm2ext;
46
47 RefPtr<UIManager> ActionManager::ui_manager;
48 string ActionManager::unbound_string = "--";
49
50
51 RefPtr<Action>
52 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
53 {
54         RefPtr<Action> act;
55
56         act = Action::create (name, label);
57         group->add (act, sl);
58
59         return act;
60 }
61
62 RefPtr<Action>
63 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
64 {
65         RefPtr<Action> act;
66
67         act = Action::create (name, label);
68         group->add (act);
69
70         return act;
71 }
72
73
74 RefPtr<Action>
75 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
76 {
77         RefPtr<Action> act;
78
79         act = RadioAction::create (rgroup, name, label);
80         group->add (act, sl);
81
82         return act;
83 }
84
85 RefPtr<Action>
86 ActionManager::register_radio_action (
87         RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
88         )
89 {
90         RefPtr<Action> act;
91
92         act = RadioAction::create (rgroup, name, label, tooltip);
93         group->add (act, sl);
94
95         return act;
96 }
97
98 RefPtr<Action>
99 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
100 {
101         RefPtr<Action> act;
102
103         act = ToggleAction::create (name, label);
104         group->add (act, sl);
105
106         return act;
107 }
108
109 RefPtr<Action>
110 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
111 {
112         RefPtr<Action> act;
113
114         act = ToggleAction::create (name, label, tooltip);
115         group->add (act, sl);
116
117         return act;
118 }
119
120 bool
121 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
122 {
123         GtkAccelKey gkey;
124         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
125
126         if (known) {
127                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
128         } else {
129                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
130         }
131
132         return known;
133 }
134
135 struct SortActionsByLabel {
136     bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
137             ustring astr = a->get_accel_path();
138             ustring bstr = b->get_accel_path();
139             return astr < bstr;
140     }
141 };
142
143 void
144 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
145 {
146         /* the C++ API for functions used here appears to be broken in
147            gtkmm2.6, so we fall back to the C level.
148         */
149
150         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
151         GList* node;
152         GList* acts;
153
154         for (node = list; node; node = g_list_next (node)) {
155
156                 GtkActionGroup* group = (GtkActionGroup*) node->data;
157
158                 /* first pass: collect them all */
159
160                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
161                 action_list the_acts;
162
163                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
164                         GtkAction* action = (GtkAction*) acts->data;
165                         the_acts.push_back (Glib::wrap (action, true));
166                 }
167
168                 /* now sort by label */
169
170                 SortActionsByLabel cmp;
171                 the_acts.sort (cmp);
172
173                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
174
175                         string accel_path = (*a)->get_accel_path ();
176
177                         groups.push_back (gtk_action_group_get_name(group));
178                         names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
179                         tooltips.push_back ((*a)->get_tooltip ());
180
181                         AccelKey key;
182                         lookup_entry (accel_path, key);
183                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
184                 }
185         }
186 }
187
188 void
189 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
190 {
191         /* the C++ API for functions used here appears to be broken in
192            gtkmm2.6, so we fall back to the C level.
193         */
194
195         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
196         GList* node;
197         GList* acts;
198
199         for (node = list; node; node = g_list_next (node)) {
200
201                 GtkActionGroup* group = (GtkActionGroup*) node->data;
202
203                 /* first pass: collect them all */
204
205                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
206                 action_list the_acts;
207
208                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
209                         GtkAction* action = (GtkAction*) acts->data;
210                         the_acts.push_back (Glib::wrap (action, true));
211                 }
212
213                 /* now sort by label */
214
215                 SortActionsByLabel cmp;
216                 the_acts.sort (cmp);
217
218                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
219
220                         ustring const label = (*a)->property_label ();
221                         string const accel_path = (*a)->get_accel_path ();
222
223                         names.push_back (label);
224                         paths.push_back (accel_path);
225                         tooltips.push_back ((*a)->get_tooltip ());
226
227                         AccelKey key;
228                         keys.push_back (get_key_representation (accel_path, key));
229                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
230                 }
231         }
232 }
233
234 void
235 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
236 {
237         ui_manager->insert_action_group (grp);
238 }
239
240 Widget*
241 ActionManager::get_widget (const char * name)
242 {
243         return ui_manager->get_widget (name);
244 }
245
246 RefPtr<Action>
247 ActionManager::get_action (const char* group_name, const char* action_name)
248 {
249         /* the C++ API for functions used here appears to be broken in
250            gtkmm2.6, so we fall back to the C level.
251         */
252
253         if (ui_manager == 0) {
254                 return RefPtr<Action> ();
255         }
256
257         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
258         GList* node;
259         RefPtr<Action> act;
260
261         for (node = list; node; node = g_list_next (node)) {
262
263                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
264
265                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
266
267                         GtkAction* _act;
268
269                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
270                                 act = Glib::wrap (_act, true);
271                                 break;
272                         }
273                 }
274         }
275
276         return act;
277 }
278
279 void
280 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
281 {
282         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
283                 (*i)->set_sensitive (state);
284         }
285 }
286
287 void
288 ActionManager::uncheck_toggleaction (string n)
289 {
290         char const * name = n.c_str ();
291         
292         const char *last_slash = strrchr (name, '/');
293
294         if (last_slash == 0) {
295                 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
296                 /*NOTREACHED*/
297                 return;
298         }
299
300         /* 10 = strlen ("<Actions>/") */
301         size_t len = last_slash - (name + 10);
302
303         char* group_name = new char[len+1];
304         memcpy (group_name, name + 10, len);
305         group_name[len] = '\0';
306
307         const char* action_name = last_slash + 1;
308
309         RefPtr<Action> act = get_action (group_name, action_name);
310         if (act) {
311                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
312                 tact->set_active (false);
313         } else {
314                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
315         }
316
317         delete [] group_name;
318 }
319
320 string
321 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
322 {
323         bool known = lookup_entry (accel_path, key);
324         
325         if (known) {
326                 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
327                 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
328                 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
329         } 
330         
331         return unbound_string;
332 }
333
334 void
335 ActionManager::do_action (const char* group, const char*action)
336 {
337         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
338         if (act) {
339                 act->activate ();
340         }
341 }
342
343 void
344 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
345 {
346         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
347         if (act) {
348                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
349                 if (tact) {
350                         tact->set_active (yn);
351                 }
352         }
353 }
354