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