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