Fix port matrix menu checkbox when the window is closed. Make the port matrix origin...
[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 #include <stdint.h>
25
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
29
30 #include <gtkmm/accelmap.h>
31 #include <gtkmm/uimanager.h>
32
33 #include "pbd/error.h"
34
35 #include "gtkmm2ext/actions.h"
36 #include "gtkmm2ext/utils.h"
37
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 Gtkmm2ext;
46
47 RefPtr<UIManager> ActionManager::ui_manager;
48 string ActionManager::unbound_string = "--";
49
50
51 RefPtr<Action>
52 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
53 {
54         RefPtr<Action> act;
55
56         act = Action::create (name, label);
57         group->add (act, sl);
58
59         return act;
60 }
61
62 RefPtr<Action>
63 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
64 {
65         RefPtr<Action> act;
66
67         act = Action::create (name, label);
68         group->add (act);
69
70         return act;
71 }
72
73
74 RefPtr<Action>
75 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
76 {
77         RefPtr<Action> act;
78
79         act = RadioAction::create (rgroup, name, label);
80         group->add (act, sl);
81
82         return act;
83 }
84
85 RefPtr<Action>
86 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
87 {
88         RefPtr<Action> act;
89
90         act = ToggleAction::create (name, label);
91         group->add (act, sl);
92
93         return act;
94 }
95
96 bool
97 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
98 {
99         GtkAccelKey gkey;
100         bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
101
102         if (known) {
103                 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
104         } else {
105                 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
106         }
107
108         return known;
109 }
110
111 struct SortActionsByLabel {
112     bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
113             ustring astr = a->get_accel_path();
114             ustring bstr = b->get_accel_path();
115             return astr < bstr;
116     }
117 };
118
119 void
120 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<AccelKey>& bindings)
121 {
122         /* the C++ API for functions used here appears to be broken in
123            gtkmm2.6, so we fall back to the C level.
124         */
125
126         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
127         GList* node;
128         GList* acts;
129
130         for (node = list; node; node = g_list_next (node)) {
131
132                 GtkActionGroup* group = (GtkActionGroup*) node->data;
133
134                 /* first pass: collect them all */
135
136                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
137                 action_list the_acts;
138
139                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
140                         GtkAction* action = (GtkAction*) acts->data;
141                         the_acts.push_back (Glib::wrap (action, true));
142                 }
143
144                 /* now sort by label */
145
146                 SortActionsByLabel cmp;
147                 the_acts.sort (cmp);
148
149                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
150
151                         string accel_path = (*a)->get_accel_path ();
152
153                         groups.push_back (gtk_action_group_get_name(group));
154                         names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
155
156                         AccelKey key;
157                         lookup_entry (accel_path, key);
158                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
159                 }
160         }
161 }
162
163 void
164 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
165 {
166         /* the C++ API for functions used here appears to be broken in
167            gtkmm2.6, so we fall back to the C level.
168         */
169
170         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
171         GList* node;
172         GList* acts;
173
174         for (node = list; node; node = g_list_next (node)) {
175
176                 GtkActionGroup* group = (GtkActionGroup*) node->data;
177
178                 /* first pass: collect them all */
179
180                 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
181                 action_list the_acts;
182
183                 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
184                         GtkAction* action = (GtkAction*) acts->data;
185                         the_acts.push_back (Glib::wrap (action, true));
186                 }
187
188                 /* now sort by label */
189
190                 SortActionsByLabel cmp;
191                 the_acts.sort (cmp);
192
193                 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
194
195                         string accel_path = (*a)->get_accel_path ();
196                         ustring label = (*a)->property_label();
197
198                         names.push_back (label);
199                         paths.push_back (accel_path);
200
201                         AccelKey key;
202                         keys.push_back (get_key_representation (accel_path, key));
203                         bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
204                 }
205         }
206 }
207
208 void
209 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
210 {
211         ui_manager->insert_action_group (grp);
212 }
213
214 Widget*
215 ActionManager::get_widget (const char * name)
216 {
217         return ui_manager->get_widget (name);
218 }
219
220 RefPtr<Action>
221 ActionManager::get_action (const char* path)
222 {
223         GtkAction* _act;
224         RefPtr<Action> act;
225
226         if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
227                 return Glib::wrap (_act, true);
228         }
229
230         return act;
231 }
232
233 RefPtr<Action>
234 ActionManager::get_action (const char* group_name, const char* action_name)
235 {
236         /* the C++ API for functions used here appears to be broken in
237            gtkmm2.6, so we fall back to the C level.
238         */
239
240         GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
241         GList* node;
242         RefPtr<Action> act;
243
244         for (node = list; node; node = g_list_next (node)) {
245
246                 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
247
248                 if (strcmp (group_name,  gtk_action_group_get_name (_ag)) == 0) {
249
250                         GtkAction* _act;
251
252                         if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
253                                 act = Glib::wrap (_act, true);
254                                 break;
255                         }
256                 }
257         }
258
259         return act;
260 }
261
262 void
263 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
264 {
265         for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
266                 (*i)->set_sensitive (state);
267         }
268 }
269
270 void
271 ActionManager::uncheck_toggleaction (string n)
272 {
273         char const * name = n.c_str ();
274         
275         const char *last_slash = strrchr (name, '/');
276
277         if (last_slash == 0) {
278                 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
279                 /*NOTREACHED*/
280                 return;
281         }
282
283         /* 10 = strlen ("<Actions>/") */
284         size_t len = last_slash - (name + 10);
285
286         char* group_name = new char[len+1];
287         memcpy (group_name, name + 10, len);
288         group_name[len] = '\0';
289
290         const char* action_name = last_slash + 1;
291
292         RefPtr<Action> act = get_action (group_name, action_name);
293         if (act) {
294                 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
295                 tact->set_active (false);
296         } else {
297                 error << string_compose (_("Unknown action name: %1"),  name) << endmsg;
298         }
299
300         delete [] group_name;
301 }
302
303 string
304 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
305 {
306         bool known = lookup_entry (accel_path, key);
307         
308         if (known) {
309                 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
310                 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
311                 return ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
312         } 
313         
314         return unbound_string;
315 }