Slightly grubby hack to fix up the state of the Window->Mixer menu item when the...
[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* path)
248 {
249         if (!path) {
250                 return RefPtr<Action>();
251         }
252
253         /* Skip <Actions>/ in path */
254
255         int len = strlen (path);
256
257         if (len < 3) {
258                 /* shortest possible path: "a/b" */
259                 return RefPtr<Action>();
260         }
261
262         if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
263                 path = path+10;
264         } else if (path[0] == '/') {
265                 path++;
266         }
267
268         char copy[len+1];
269         strcpy (copy, path);
270         char* slash = strchr (copy, '/');
271         if (!slash) {
272                 return RefPtr<Action> ();
273         }
274         *slash = '\0';
275
276         return get_action (copy, ++slash);
277         
278 }
279
280 RefPtr<Action>
281 ActionManager::get_action (const char* group_name, const char* action_name)
282 {
283         /* the C++ API for functions used here appears to be broken in
284            gtkmm2.6, so we fall back to the C level.
285         */
286
287         if (ui_manager == 0) {
288                 return RefPtr<Action> ();
289         }
290
291         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
292         GList* node;
293         RefPtr<Action> act;
294
295         for (node = list; node; node = g_list_next (node)) {
296
297                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
298
299                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
300
301                         GtkAction* _act;
302
303                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
304                                 act = Glib::wrap (_act, true);
305                                 break;
306                         }
307                 }
308         }
309
310         return act;
311 }
312
313 RefPtr<Action>
314 ActionManager::get_action_from_name (const char* name)
315 {
316         /* the C++ API for functions used here appears to be broken in
317            gtkmm2.6, so we fall back to the C level.
318         */
319
320         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
321         GList* node;
322         GList* acts;
323
324         for (node = list; node; node = g_list_next (node)) {
325
326                 GtkActionGroup* group = (GtkActionGroup*) node->data;
327
328                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
329                         GtkAction* action = (GtkAction*) acts->data;
330                         if (!strcmp (gtk_action_get_name (action), name)) {
331                                 return Glib::wrap (action, true);
332                         }
333                 }
334         }
335
336         return RefPtr<Action>();
337 }
338
339 void
340 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
341 {
342         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
343                 (*i)->set_sensitive (state);
344         }
345 }
346
347 void
348 ActionManager::check_toggleaction (string n)
349 {
350         set_toggleaction_state (n, true);
351 }
352
353 void
354 ActionManager::uncheck_toggleaction (string n)
355 {
356         set_toggleaction_state (n, false);
357 }
358
359 void
360 ActionManager::set_toggleaction_state (string n, bool s)
361 {
362         char const * name = n.c_str ();
363         
364         const char *last_slash = strrchr (name, '/');
365
366         if (last_slash == 0) {
367                 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
368                 /*NOTREACHED*/
369                 return;
370         }
371
372         /* 10 = strlen ("<Actions>/") */
373         size_t len = last_slash - (name + 10);
374
375         char* group_name = new char[len+1];
376         memcpy (group_name, name + 10, len);
377         group_name[len] = '\0';
378
379         const char* action_name = last_slash + 1;
380
381         RefPtr<Action> act = get_action (group_name, action_name);
382         if (act) {
383                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
384                 tact->set_active (s);
385         } else {
386                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
387         }
388
389         delete [] group_name;
390 }
391
392 string
393 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
394 {
395         bool known = lookup_entry (accel_path, key);
396         
397         if (known) {
398                 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
399                 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
400                 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
401         } 
402         
403         return unbound_string;
404 }
405
406 void
407 ActionManager::do_action (const char* group, const char*action)
408 {
409         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
410         if (act) {
411                 act->activate ();
412         }
413 }
414
415 void
416 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
417 {
418         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
419         if (act) {
420                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
421                 if (tact) {
422                         tact->set_active (yn);
423                 }
424         }
425 }
426