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