allow use of Return, KP_Enter and more in key binding editor; better display of such...
[ardour.git] / gtk2_ardour / 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
25 #include <gtk/gtkaccelmap.h>
26 #include <gtk/gtkuimanager.h>
27 #include <gtk/gtkactiongroup.h>
28
29 #include <gtkmm/accelmap.h>
30 #include <gtkmm/uimanager.h>
31
32 #include "pbd/error.h"
33 #include "pbd/file_utils.h"
34
35 #include "ardour/ardour.h"
36 #include "ardour/filesystem_paths.h"
37 #include "ardour/rc_configuration.h"
38
39 #include "utils.h"
40 #include "actions.h"
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace Gtk;
45 using namespace Glib;
46 using namespace sigc;
47 using namespace PBD;
48 using namespace ARDOUR;
49
50 vector<RefPtr<Gtk::Action> > ActionManager::session_sensitive_actions;
51 vector<RefPtr<Gtk::Action> > ActionManager::write_sensitive_actions;
52 vector<RefPtr<Gtk::Action> > ActionManager::region_list_selection_sensitive_actions;
53 vector<RefPtr<Gtk::Action> > ActionManager::plugin_selection_sensitive_actions;
54 vector<RefPtr<Gtk::Action> > ActionManager::region_selection_sensitive_actions;
55 vector<RefPtr<Gtk::Action> > ActionManager::track_selection_sensitive_actions;
56 vector<RefPtr<Gtk::Action> > ActionManager::point_selection_sensitive_actions;
57 vector<RefPtr<Gtk::Action> > ActionManager::time_selection_sensitive_actions;
58 vector<RefPtr<Gtk::Action> > ActionManager::line_selection_sensitive_actions;
59 vector<RefPtr<Gtk::Action> > ActionManager::playlist_selection_sensitive_actions;
60 vector<RefPtr<Gtk::Action> > ActionManager::mouse_edit_point_requires_canvas_actions;
61
62 vector<RefPtr<Gtk::Action> > ActionManager::range_sensitive_actions;
63 vector<RefPtr<Gtk::Action> > ActionManager::jack_sensitive_actions;
64 vector<RefPtr<Gtk::Action> > ActionManager::jack_opposite_sensitive_actions;
65 vector<RefPtr<Gtk::Action> > ActionManager::transport_sensitive_actions;
66 vector<RefPtr<Gtk::Action> > ActionManager::edit_point_in_region_sensitive_actions;
67
68 RefPtr<UIManager> ActionManager::ui_manager;
69 string ActionManager::unbound_string = "--";
70
71 void
72 ActionManager::init ()
73 {
74         ui_manager = UIManager::create ();
75
76         sys::path ui_file;
77
78         SearchPath spath = ardour_search_path() + user_config_directory() + system_config_search_path();
79
80         find_file_in_search_path (spath, "ardour.menus", ui_file);
81
82         bool loaded = false;
83
84         try {
85                 ui_manager->add_ui_from_file (ui_file.to_string());
86                 loaded = true;
87         } catch (Glib::MarkupError& err) {
88                 error << string_compose (_("badly formatted UI definition file: %1"), err.what()) << endmsg;
89                 cerr << string_compose (_("badly formatted UI definition file: %1"), err.what()) << endl;
90         } catch (...) {
91                 error << _("Ardour menu definition file not found") << endmsg;
92         }
93
94         if (!loaded) {
95                 cerr << _("ardour will not work without a valid ardour.menus file") << endl;
96                 error << _("ardour will not work without a valid ardour.menus file") << endmsg;
97                 exit(1);
98         }
99 }
100
101 RefPtr<Action>
102 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
103 {
104         RefPtr<Action> act;
105
106         act = Action::create (name, label);
107         group->add (act, sl);
108
109         return act;
110 }
111
112 RefPtr<Action>
113 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
114 {
115         RefPtr<Action> act;
116
117         act = Action::create (name, label);
118         group->add (act);
119
120         return act;
121 }
122
123
124 RefPtr<Action>
125 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
126 {
127         RefPtr<Action> act;
128
129         act = RadioAction::create (rgroup, name, label);
130         group->add (act, sl);
131
132         return act;
133 }
134
135 RefPtr<Action>
136 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
137 {
138         RefPtr<Action> act;
139
140         act = ToggleAction::create (name, label);
141         group->add (act, sl);
142
143         return act;
144 }
145
146 bool
147 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
148 {
149         GtkAccelKey gkey;
150         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
151
152         if (known) {
153                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
154         } else {
155                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
156         }
157
158         return known;
159 }
160
161 struct SortActionsByLabel {
162     bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
163             ustring astr = a->get_accel_path();
164             ustring bstr = b->get_accel_path();
165             return astr < bstr;
166     }
167 };
168
169 void
170 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<AccelKey>& bindings)
171 {
172         /* the C++ API for functions used here appears to be broken in
173            gtkmm2.6, so we fall back to the C level.
174         */
175
176         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
177         GList* node;
178         GList* acts;
179
180         for (node = list; node; node = g_list_next (node)) {
181
182                 GtkActionGroup* group = (GtkActionGroup*) node->data;
183
184                 /* first pass: collect them all */
185
186                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
187                 action_list the_acts;
188
189                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
190                         GtkAction* action = (GtkAction*) acts->data;
191                         the_acts.push_back (Glib::wrap (action, true));
192                 }
193
194                 /* now sort by label */
195
196                 SortActionsByLabel cmp;
197                 the_acts.sort (cmp);
198
199                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
200
201                         string accel_path = (*a)->get_accel_path ();
202
203                         groups.push_back (gtk_action_group_get_name(group));
204                         names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
205
206                         AccelKey key;
207                         lookup_entry (accel_path, key);
208                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
209                 }
210         }
211 }
212
213 void
214 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
215 {
216         /* the C++ API for functions used here appears to be broken in
217            gtkmm2.6, so we fall back to the C level.
218         */
219
220         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
221         GList* node;
222         GList* acts;
223
224         for (node = list; node; node = g_list_next (node)) {
225
226                 GtkActionGroup* group = (GtkActionGroup*) node->data;
227
228                 /* first pass: collect them all */
229
230                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
231                 action_list the_acts;
232
233                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
234                         GtkAction* action = (GtkAction*) acts->data;
235                         the_acts.push_back (Glib::wrap (action, true));
236                 }
237
238                 /* now sort by label */
239
240                 SortActionsByLabel cmp;
241                 the_acts.sort (cmp);
242
243                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
244
245                         string accel_path = (*a)->get_accel_path ();
246                         ustring label = (*a)->property_label();
247
248                         names.push_back (label);
249                         paths.push_back (accel_path);
250
251                         AccelKey key;
252                         keys.push_back (get_key_representation (accel_path, key));
253                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
254                 }
255         }
256 }
257
258 void
259 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
260 {
261         ui_manager->insert_action_group (grp);
262 }
263
264 Widget*
265 ActionManager::get_widget (const char * name)
266 {
267         return ui_manager->get_widget (name);
268 }
269
270 RefPtr<Action>
271 ActionManager::get_action (const char* path)
272 {
273         GtkAction* _act;
274         RefPtr<Action> act;
275
276         if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
277                 return Glib::wrap (_act, true);
278         }
279
280         return act;
281 }
282
283 RefPtr<Action>
284 ActionManager::get_action (const char* group_name, const char* action_name)
285 {
286         /* the C++ API for functions used here appears to be broken in
287            gtkmm2.6, so we fall back to the C level.
288         */
289
290         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
291         GList* node;
292         RefPtr<Action> act;
293
294         for (node = list; node; node = g_list_next (node)) {
295
296                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
297
298                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
299
300                         GtkAction* _act;
301
302                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
303                                 act = Glib::wrap (_act, true);
304                                 break;
305                         }
306                 }
307         }
308
309         return act;
310 }
311
312 void
313 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
314 {
315         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
316                 (*i)->set_sensitive (state);
317         }
318 }
319
320 void
321 ActionManager::uncheck_toggleaction (const char * name)
322 {
323         const char *last_slash = strrchr (name, '/');
324
325         if (last_slash == 0) {
326                 fatal << string_compose (_("programmer error: %1 %2"), X_("illegal toggle action name"), name) << endmsg;
327                 /*NOTREACHED*/
328                 return;
329         }
330
331         /* 10 = strlen ("<Actions>/") */
332         size_t len = last_slash - (name + 10);
333
334         char* group_name = new char[len+1];
335         memcpy (group_name, name + 10, len);
336         group_name[len] = '\0';
337
338         const char* action_name = last_slash + 1;
339
340         RefPtr<Action> act = get_action (group_name, action_name);
341         if (act) {
342                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
343                 tact->set_active (false);
344         } else {
345                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
346         }
347
348         delete [] group_name;
349 }
350
351 /** Examine the state of a Configuration setting and a toggle action, and toggle the Configuration
352  * setting if its state doesn't match the toggle action.
353  * @param group Action group.
354  * @param action Action name.
355  * @param Method to set the state of the Configuration setting.
356  * @param Method to get the state of the Configuration setting.
357  */
358 void
359 ActionManager::toggle_config_state (const char* group, const char* action, bool (RCConfiguration::*set)(bool), bool (RCConfiguration::*get)(void) const)
360 {
361         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
362
363         if (act) {
364                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
365
366                 if (tact) {
367                         bool x = (Config->*get)();
368
369                         if (x != tact->get_active()) {
370                                 (Config->*set) (!x);
371                         }
372                 }
373         }
374 }
375
376 void
377 ActionManager::toggle_config_state_foo (const char* group, const char* action, sigc::slot<bool, bool> set, sigc::slot<bool> get)
378 {
379         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
380
381         if (act) {
382                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
383
384                 if (tact) {
385                         bool const x = get ();
386
387                         if (x != tact->get_active ()) {
388                                 set (!x);
389                         }
390                 }
391         }
392 }
393
394
395 /** Set the state of a ToggleAction using a particular Configuration get() method
396  * @param group Action group.
397  * @param action Action name.
398  * @param get Method to obtain the state that the ToggleAction should have.
399  */
400 void
401 ActionManager::map_some_state (const char* group, const char* action, bool (RCConfiguration::*get)() const)
402 {
403         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
404         if (act) {
405                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
406
407                 if (tact) {
408
409                         bool x = (Config->*get)();
410
411                         if (tact->get_active() != x) {
412                                 tact->set_active (x);
413                         }
414                 } else {
415                         cerr << group << ':' << action << " is not a toggle\n";
416                 }
417         } else {
418                 cerr << group << ':' << action << " not an action\n";
419         }
420 }
421
422 void
423 ActionManager::map_some_state (const char* group, const char* action, sigc::slot<bool> get)
424 {
425         Glib::RefPtr<Action> act = ActionManager::get_action (group, action);
426         if (act) {
427                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
428
429                 if (tact) {
430
431                         bool const x = get ();
432
433                         if (tact->get_active() != x) {
434                                 tact->set_active (x);
435                         }
436                 }
437         }
438 }
439
440 string
441 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
442 {
443         bool known = lookup_entry (accel_path, key);
444         
445         if (known) {
446                 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
447                 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
448                 return ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
449         } 
450         
451         return unbound_string;
452 }