come up and stay up
[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     $Id$
19 */
20
21 #include <vector>
22 #include <gtk/gtkaccelmap.h>
23 #include <gtkmm/accelmap.h>
24 #include <gtkmm/uimanager.h>
25
26 #include <pbd/error.h>
27
28 #include <ardour/ardour.h>
29
30 #include "actions.h"
31
32 using namespace std;
33 using namespace Gtk;
34 using namespace Glib;
35 using namespace sigc;
36
37 vector<RefPtr<Gtk::Action> > ActionManager::session_sensitive_actions;
38 vector<RefPtr<Gtk::Action> > ActionManager::region_list_selection_sensitive_actions;
39 vector<RefPtr<Gtk::Action> > ActionManager::region_selection_sensitive_actions;
40 vector<RefPtr<Gtk::Action> > ActionManager::track_selection_sensitive_actions;
41 vector<RefPtr<Gtk::Action> > ActionManager::plugin_selection_sensitive_actions;
42 vector<RefPtr<Gtk::Action> > ActionManager::range_sensitive_actions;
43 vector<RefPtr<Gtk::Action> > ActionManager::jack_sensitive_actions;
44 vector<RefPtr<Gtk::Action> > ActionManager::jack_opposite_sensitive_actions;
45 RefPtr<UIManager> ActionManager::ui_manager;
46 string ActionManager::unbound_string = "--";
47
48 void
49 ActionManager::init ()
50 {
51         ui_manager = UIManager::create ();
52
53         try {
54           ui_manager->add_ui_from_file (ARDOUR::find_config_file("ardour-menus.xml"));
55         } catch (Glib::MarkupError& err) {
56                 error << "badly formatted UI definition file" << endmsg;
57         } catch (...) {
58           std::cerr << "ardour action xml file not found" << endl;
59         }
60     
61 }
62
63 RefPtr<Action>
64 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
65 {
66         RefPtr<Action> act = register_action (group, name, label, sl);
67         AccelMap::add_entry (act->get_accel_path(), key, mods);
68
69         return act;
70 }
71
72 RefPtr<Action>
73 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
74 {
75         RefPtr<Action> act = register_action (group, name, label);
76         group->add (act, sl);
77
78         return act;
79 }
80
81 RefPtr<Action>
82 ActionManager::register_action (RefPtr<ActionGroup> group, string name, string label)
83 {
84         RefPtr<Action> act;
85
86         act = Action::create (name, label);
87         group->add (act);
88
89         return act;
90 }
91
92
93 RefPtr<Action>
94 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
95 {
96         RefPtr<Action> act = register_radio_action (group, rgroup, name, label, sl);
97         AccelMap::add_entry (act->get_accel_path(), key, mods);
98
99         return act;
100 }
101
102 RefPtr<Action>
103 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group rgroup, string name, string label, slot<void> sl)
104 {
105         RefPtr<Action> act;
106
107         act = RadioAction::create (rgroup, name, label);
108         group->add (act, sl);
109
110         return act;
111 }
112
113
114 RefPtr<Action>
115 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl, guint key, Gdk::ModifierType mods)
116 {
117         RefPtr<Action> act = register_toggle_action (group,name, label, sl);
118         AccelMap::add_entry (act->get_accel_path(), key, mods);
119
120         return act;
121 }
122
123 RefPtr<Action>
124 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string name, string label, slot<void> sl)
125 {
126         RefPtr<Action> act;
127
128         act = ToggleAction::create (name, label);
129         group->add (act, sl);
130
131         return act;
132 }
133
134 bool 
135 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
136 {
137         GtkAccelKey gkey;
138         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
139         
140         if (known) {
141                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
142         } else {
143                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
144         }
145
146         return known;
147 }
148
149 void
150 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
151 {
152         ListHandle<RefPtr<ActionGroup> > uim_groups = ui_manager->get_action_groups ();
153         
154         for (ListHandle<RefPtr<ActionGroup> >::iterator g = uim_groups.begin(); g != uim_groups.end(); ++g) {
155                 
156                 ListHandle<RefPtr<Action> > group_actions = (*g)->get_actions();
157                 
158                 for (ListHandle<RefPtr<Action> >::iterator a = group_actions.begin(); a != group_actions.end(); ++a) {
159                         
160                         ustring accel_path;
161                         
162                         accel_path = (*a)->get_accel_path();
163                         
164                         names.push_back ((*a)->get_name());
165                         paths.push_back (accel_path);
166                         
167                         AccelKey key;
168                         bool known = lookup_entry (accel_path, key);
169                         
170                         if (known) {
171                                 keys.push_back (ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod())));
172                         } else {
173                                 keys.push_back (unbound_string);
174                         }
175                         
176                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
177                 }
178         }
179 }
180
181 void
182 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
183 {
184         ui_manager->insert_action_group (grp);
185 }
186
187 Widget*
188 ActionManager::get_widget (ustring name)
189 {
190         return ui_manager->get_widget (name);
191 }
192
193 RefPtr<Action>
194 ActionManager::get_action (ustring name)
195 {
196         return ui_manager->get_action (name);
197 }
198
199 void 
200 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
201 {
202         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
203                 (*i)->set_sensitive (state);
204         }
205 }