add ability to save current action sensitivities and restore them, and to disable...
[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 <stack>
25 #include <stdint.h>
26
27 #include <boost/shared_ptr.hpp>
28
29 #include <gtk/gtkaccelmap.h>
30 #include <gtk/gtkuimanager.h>
31 #include <gtk/gtkactiongroup.h>
32
33 #include <gtkmm.h>
34 #include <gtkmm/accelmap.h>
35 #include <gtkmm/uimanager.h>
36
37 #include "pbd/error.h"
38
39 #include "gtkmm2ext/actions.h"
40 #include "gtkmm2ext/utils.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace Gtk;
46 using namespace Glib;
47 using namespace sigc;
48 using namespace PBD;
49 using namespace Gtkmm2ext;
50
51 RefPtr<UIManager> ActionManager::ui_manager;
52 string ActionManager::unbound_string = "--";
53
54
55 RefPtr<Action>
56 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
57 {
58         RefPtr<Action> act;
59
60         act = Action::create (name, label);
61         group->add (act, sl);
62
63         return act;
64 }
65
66 RefPtr<Action>
67 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
68 {
69         RefPtr<Action> act;
70
71         act = Action::create (name, label);
72         group->add (act);
73
74         return act;
75 }
76
77
78 RefPtr<Action>
79 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
80 {
81         RefPtr<Action> act;
82
83         act = RadioAction::create (rgroup, name, label);
84         group->add (act, sl);
85
86         return act;
87 }
88
89 RefPtr<Action>
90 ActionManager::register_radio_action (
91         RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
92         )
93 {
94         RefPtr<Action> act;
95
96         act = RadioAction::create (rgroup, name, label, tooltip);
97         group->add (act, sl);
98
99         return act;
100 }
101
102 RefPtr<Action>
103 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
104 {
105         RefPtr<Action> act;
106
107         act = ToggleAction::create (name, label);
108         group->add (act, sl);
109
110         return act;
111 }
112
113 RefPtr<Action>
114 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
115 {
116         RefPtr<Action> act;
117
118         act = ToggleAction::create (name, label, tooltip);
119         group->add (act, sl);
120
121         return act;
122 }
123
124 bool
125 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
126 {
127         GtkAccelKey gkey;
128         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
129
130         if (known) {
131                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
132         } else {
133                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
134         }
135
136         return known;
137 }
138
139 struct SortActionsByLabel {
140     bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
141             ustring astr = a->get_accel_path();
142             ustring bstr = b->get_accel_path();
143             return astr < bstr;
144     }
145 };
146
147 void
148 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
149 {
150         /* the C++ API for functions used here appears to be broken in
151            gtkmm2.6, so we fall back to the C level.
152         */
153
154         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
155         GList* node;
156         GList* acts;
157
158         for (node = list; node; node = g_list_next (node)) {
159
160                 GtkActionGroup* group = (GtkActionGroup*) node->data;
161
162                 /* first pass: collect them all */
163
164                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
165                 action_list the_acts;
166
167                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
168                         GtkAction* action = (GtkAction*) acts->data;
169                         the_acts.push_back (Glib::wrap (action, true));
170                 }
171
172                 /* now sort by label */
173
174                 SortActionsByLabel cmp;
175                 the_acts.sort (cmp);
176
177                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
178
179                         string accel_path = (*a)->get_accel_path ();
180
181                         groups.push_back (gtk_action_group_get_name(group));
182                         names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
183                         tooltips.push_back ((*a)->get_tooltip ());
184
185                         AccelKey key;
186                         lookup_entry (accel_path, key);
187                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
188                 }
189         }
190 }
191
192 void
193 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
194 {
195         /* the C++ API for functions used here appears to be broken in
196            gtkmm2.6, so we fall back to the C level.
197         */
198
199         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
200         GList* node;
201         GList* acts;
202
203         for (node = list; node; node = g_list_next (node)) {
204
205                 GtkActionGroup* group = (GtkActionGroup*) node->data;
206
207                 /* first pass: collect them all */
208
209                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
210                 action_list the_acts;
211
212                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
213                         GtkAction* action = (GtkAction*) acts->data;
214                         the_acts.push_back (Glib::wrap (action, true));
215                 }
216
217                 /* now sort by label */
218
219                 SortActionsByLabel cmp;
220                 the_acts.sort (cmp);
221
222                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
223
224                         ustring const label = (*a)->property_label ();
225                         string const accel_path = (*a)->get_accel_path ();
226
227                         names.push_back (label);
228                         paths.push_back (accel_path);
229                         tooltips.push_back ((*a)->get_tooltip ());
230
231                         AccelKey key;
232                         keys.push_back (get_key_representation (accel_path, key));
233                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
234                 }
235         }
236 }
237
238 struct ActionState {
239         GtkAction* action;
240         bool       sensitive;
241         ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {}
242 };
243
244 typedef std::vector<ActionState> ActionStates;
245
246 static std::stack<boost::shared_ptr<ActionStates> > state_stack;
247
248 static boost::shared_ptr<ActionStates>
249 get_action_state ()
250 {
251         boost::shared_ptr<ActionStates> state = boost::shared_ptr<ActionStates>(new ActionStates);
252         
253         /* the C++ API for functions used here appears to be broken in
254            gtkmm2.6, so we fall back to the C level.
255         */
256
257         GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj());
258         GList* node;
259         GList* acts;
260
261         for (node = list; node; node = g_list_next (node)) {
262
263                 GtkActionGroup* group = (GtkActionGroup*) node->data;
264
265                 /* first pass: collect them all */
266
267                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
268                 action_list the_acts;
269
270                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
271                         GtkAction* action = (GtkAction*) acts->data;
272
273                         state->push_back (ActionState (action, gtk_action_get_sensitive (action)));
274                 }
275         }
276         
277         return state;
278 }
279
280 void
281 ActionManager::push_action_state ()
282 {
283         state_stack.push (get_action_state());
284 }
285
286 void
287 ActionManager::pop_action_state ()
288 {
289         if (state_stack.empty()) {
290                 warning << string_compose (_("programming error: %1"), X_("ActionManager::pop_action_state called with empty stack")) << endmsg;
291                 return;
292         }
293
294         boost::shared_ptr<ActionStates> as = state_stack.top ();
295         state_stack.pop ();
296         
297         for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
298                 gtk_action_set_sensitive ((*i).action, (*i).sensitive);
299         }
300 }
301
302 void
303 ActionManager::disable_all_actions ()
304 {
305         push_action_state ();
306         boost::shared_ptr<ActionStates> as = state_stack.top ();
307         
308         for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
309                 gtk_action_set_sensitive ((*i).action, false);
310         }
311 }
312
313 void
314 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
315 {
316         ui_manager->insert_action_group (grp);
317 }
318
319 Widget*
320 ActionManager::get_widget (const char * name)
321 {
322         return ui_manager->get_widget (name);
323 }
324
325 RefPtr<Action>
326 ActionManager::get_action (const char* path)
327 {
328         if (!path) {
329                 return RefPtr<Action>();
330         }
331
332         /* Skip <Actions>/ in path */
333
334         int len = strlen (path);
335
336         if (len < 3) {
337                 /* shortest possible path: "a/b" */
338                 return RefPtr<Action>();
339         }
340
341         if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
342                 path = path+10;
343         } else if (path[0] == '/') {
344                 path++;
345         }
346
347         vector<char> copy(len+1);
348         strcpy (&copy[0], path);
349         char* slash = strchr (&copy[0], '/');
350         if (!slash) {
351                 return RefPtr<Action> ();
352         }
353         *slash = '\0';
354
355         return get_action (&copy[0], ++slash);
356         
357 }
358
359 RefPtr<Action>
360 ActionManager::get_action (const char* group_name, const char* action_name)
361 {
362         /* the C++ API for functions used here appears to be broken in
363            gtkmm2.6, so we fall back to the C level.
364         */
365
366         if (ui_manager == 0) {
367                 return RefPtr<Action> ();
368         }
369
370         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
371         GList* node;
372         RefPtr<Action> act;
373
374         for (node = list; node; node = g_list_next (node)) {
375
376                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
377
378                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
379
380                         GtkAction* _act;
381
382                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
383                                 act = Glib::wrap (_act, true);
384                                 break;
385                         }
386                 }
387         }
388
389         return act;
390 }
391
392 RefPtr<Action>
393 ActionManager::get_action_from_name (const char* name)
394 {
395         /* the C++ API for functions used here appears to be broken in
396            gtkmm2.6, so we fall back to the C level.
397         */
398
399         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
400         GList* node;
401         GList* acts;
402
403         for (node = list; node; node = g_list_next (node)) {
404
405                 GtkActionGroup* group = (GtkActionGroup*) node->data;
406
407                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
408                         GtkAction* action = (GtkAction*) acts->data;
409                         if (!strcmp (gtk_action_get_name (action), name)) {
410                                 return Glib::wrap (action, true);
411                         }
412                 }
413         }
414
415         return RefPtr<Action>();
416 }
417
418 void
419 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
420 {
421         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
422                 (*i)->set_sensitive (state);
423         }
424 }
425
426 void
427 ActionManager::check_toggleaction (string n)
428 {
429         set_toggleaction_state (n, true);
430 }
431
432 void
433 ActionManager::uncheck_toggleaction (string n)
434 {
435         set_toggleaction_state (n, false);
436 }
437
438 void
439 ActionManager::set_toggleaction_state (string n, bool s)
440 {
441         char const * name = n.c_str ();
442         
443         const char *last_slash = strrchr (name, '/');
444
445         if (last_slash == 0) {
446                 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
447                 /*NOTREACHED*/
448                 return;
449         }
450
451         /* 10 = strlen ("<Actions>/") */
452         size_t len = last_slash - (name + 10);
453
454         char* group_name = new char[len+1];
455         memcpy (group_name, name + 10, len);
456         group_name[len] = '\0';
457
458         const char* action_name = last_slash + 1;
459
460         RefPtr<Action> act = get_action (group_name, action_name);
461         if (act) {
462                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
463                 tact->set_active (s);
464         } else {
465                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
466         }
467
468         delete [] group_name;
469 }
470
471 string
472 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
473 {
474         bool known = lookup_entry (accel_path, key);
475         
476         if (known) {
477                 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
478                 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
479                 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
480         } 
481         
482         return unbound_string;
483 }
484
485 void
486 ActionManager::do_action (const char* group, const char*action)
487 {
488         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
489         if (act) {
490                 act->activate ();
491         }
492 }
493
494 void
495 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
496 {
497         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
498         if (act) {
499                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
500                 if (tact) {
501                         tact->set_active (yn);
502                 }
503         }
504 }
505