switch ActionManager to a namespace; move generic part into libgtkmm2ext
[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
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 "gtkmm2ext/actions.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace Gtk;
40 using namespace Glib;
41 using namespace sigc;
42 using namespace PBD;
43
44 RefPtr<UIManager> ActionManager::ui_manager;
45 string ActionManager::unbound_string = "--";
46
47
48 RefPtr<Action>
49 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
50 {
51         RefPtr<Action> act;
52
53         act = Action::create (name, label);
54         group->add (act, sl);
55
56         return act;
57 }
58
59 RefPtr<Action>
60 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
61 {
62         RefPtr<Action> act;
63
64         act = Action::create (name, label);
65         group->add (act);
66
67         return act;
68 }
69
70
71 RefPtr<Action>
72 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
73 {
74         RefPtr<Action> act;
75
76         act = RadioAction::create (rgroup, name, label);
77         group->add (act, sl);
78
79         return act;
80 }
81
82 RefPtr<Action>
83 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
84 {
85         RefPtr<Action> act;
86
87         act = ToggleAction::create (name, label);
88         group->add (act, sl);
89
90         return act;
91 }
92
93 bool
94 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
95 {
96         GtkAccelKey gkey;
97         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
98
99         if (known) {
100                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
101         } else {
102                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
103         }
104
105         return known;
106 }
107
108 struct SortActionsByLabel {
109     bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
110             ustring astr = a->get_accel_path();
111             ustring bstr = b->get_accel_path();
112             return astr < bstr;
113     }
114 };
115
116 void
117 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<AccelKey>& bindings)
118 {
119         /* the C++ API for functions used here appears to be broken in
120            gtkmm2.6, so we fall back to the C level.
121         */
122
123         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
124         GList* node;
125         GList* acts;
126
127         for (node = list; node; node = g_list_next (node)) {
128
129                 GtkActionGroup* group = (GtkActionGroup*) node->data;
130
131                 /* first pass: collect them all */
132
133                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
134                 action_list the_acts;
135
136                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
137                         GtkAction* action = (GtkAction*) acts->data;
138                         the_acts.push_back (Glib::wrap (action, true));
139                 }
140
141                 /* now sort by label */
142
143                 SortActionsByLabel cmp;
144                 the_acts.sort (cmp);
145
146                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
147
148                         string accel_path = (*a)->get_accel_path ();
149
150                         groups.push_back (gtk_action_group_get_name(group));
151                         names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
152
153                         AccelKey key;
154                         lookup_entry (accel_path, key);
155                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
156                 }
157         }
158 }
159
160 void
161 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, 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                         ustring label = (*a)->property_label();
194
195                         names.push_back (label);
196                         paths.push_back (accel_path);
197
198                         AccelKey key;
199                         keys.push_back (get_key_representation (accel_path, key));
200                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
201                 }
202         }
203 }
204
205 void
206 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
207 {
208         ui_manager->insert_action_group (grp);
209 }
210
211 Widget*
212 ActionManager::get_widget (const char * name)
213 {
214         return ui_manager->get_widget (name);
215 }
216
217 RefPtr<Action>
218 ActionManager::get_action (const char* path)
219 {
220         GtkAction* _act;
221         RefPtr<Action> act;
222
223         if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
224                 return Glib::wrap (_act, true);
225         }
226
227         return act;
228 }
229
230 RefPtr<Action>
231 ActionManager::get_action (const char* group_name, const char* action_name)
232 {
233         /* the C++ API for functions used here appears to be broken in
234            gtkmm2.6, so we fall back to the C level.
235         */
236
237         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
238         GList* node;
239         RefPtr<Action> act;
240
241         for (node = list; node; node = g_list_next (node)) {
242
243                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
244
245                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
246
247                         GtkAction* _act;
248
249                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
250                                 act = Glib::wrap (_act, true);
251                                 break;
252                         }
253                 }
254         }
255
256         return act;
257 }
258
259 void
260 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
261 {
262         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
263                 (*i)->set_sensitive (state);
264         }
265 }
266
267 void
268 ActionManager::uncheck_toggleaction (const char * name)
269 {
270         const char *last_slash = strrchr (name, '/');
271
272         if (last_slash == 0) {
273                 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
274                 /*NOTREACHED*/
275                 return;
276         }
277
278         /* 10 = strlen ("<Actions>/") */
279         size_t len = last_slash - (name + 10);
280
281         char* group_name = new char[len+1];
282         memcpy (group_name, name + 10, len);
283         group_name[len] = '\0';
284
285         const char* action_name = last_slash + 1;
286
287         RefPtr<Action> act = get_action (group_name, action_name);
288         if (act) {
289                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
290                 tact->set_active (false);
291         } else {
292                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
293         }
294
295         delete [] group_name;
296 }