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