fix up file renaming code a little bit
[ArdourMidi.git] / libs / gtkmm2ext / actions.cc
blobbff718e9b74aeed5a9d660aacad164bb56879324
1 /*
2 Copyright (C) 2005 Paul Davis
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.
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.
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.
20 #include <cstring>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <stdint.h>
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
30 #include <gtkmm/accelmap.h>
31 #include <gtkmm/uimanager.h>
33 #include "pbd/error.h"
35 #include "gtkmm2ext/actions.h"
36 #include "gtkmm2ext/utils.h"
38 #include "i18n.h"
40 using namespace std;
41 using namespace Gtk;
42 using namespace Glib;
43 using namespace sigc;
44 using namespace PBD;
45 using namespace Gtkmm2ext;
47 RefPtr<UIManager> ActionManager::ui_manager;
48 string ActionManager::unbound_string = "--";
51 RefPtr<Action>
52 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
54 RefPtr<Action> act;
56 act = Action::create (name, label);
57 group->add (act, sl);
59 return act;
62 RefPtr<Action>
63 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
65 RefPtr<Action> act;
67 act = Action::create (name, label);
68 group->add (act);
70 return act;
74 RefPtr<Action>
75 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
77 RefPtr<Action> act;
79 act = RadioAction::create (rgroup, name, label);
80 group->add (act, sl);
82 return act;
85 RefPtr<Action>
86 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
88 RefPtr<Action> act;
90 act = ToggleAction::create (name, label);
91 group->add (act, sl);
93 return act;
96 bool
97 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
99 GtkAccelKey gkey;
100 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
102 if (known) {
103 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
104 } else {
105 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
108 return known;
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;
119 void
120 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<AccelKey>& bindings)
122 /* the C++ API for functions used here appears to be broken in
123 gtkmm2.6, so we fall back to the C level.
126 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
127 GList* node;
128 GList* acts;
130 for (node = list; node; node = g_list_next (node)) {
132 GtkActionGroup* group = (GtkActionGroup*) node->data;
134 /* first pass: collect them all */
136 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
137 action_list the_acts;
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));
144 /* now sort by label */
146 SortActionsByLabel cmp;
147 the_acts.sort (cmp);
149 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
151 string accel_path = (*a)->get_accel_path ();
153 groups.push_back (gtk_action_group_get_name(group));
154 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
156 AccelKey key;
157 lookup_entry (accel_path, key);
158 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
163 void
164 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
166 /* the C++ API for functions used here appears to be broken in
167 gtkmm2.6, so we fall back to the C level.
170 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
171 GList* node;
172 GList* acts;
174 for (node = list; node; node = g_list_next (node)) {
176 GtkActionGroup* group = (GtkActionGroup*) node->data;
178 /* first pass: collect them all */
180 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
181 action_list the_acts;
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));
188 /* now sort by label */
190 SortActionsByLabel cmp;
191 the_acts.sort (cmp);
193 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
195 string accel_path = (*a)->get_accel_path ();
196 ustring label = (*a)->property_label();
198 names.push_back (label);
199 paths.push_back (accel_path);
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())));
208 void
209 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
211 ui_manager->insert_action_group (grp);
214 Widget*
215 ActionManager::get_widget (const char * name)
217 return ui_manager->get_widget (name);
220 RefPtr<Action>
221 ActionManager::get_action (const char* path)
223 GtkAction* _act;
224 RefPtr<Action> act;
226 if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
227 return Glib::wrap (_act, true);
230 return act;
233 RefPtr<Action>
234 ActionManager::get_action (const char* group_name, const char* action_name)
236 /* the C++ API for functions used here appears to be broken in
237 gtkmm2.6, so we fall back to the C level.
240 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
241 GList* node;
242 RefPtr<Action> act;
244 for (node = list; node; node = g_list_next (node)) {
246 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
248 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
250 GtkAction* _act;
252 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
253 act = Glib::wrap (_act, true);
254 break;
259 return act;
262 void
263 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
265 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
266 (*i)->set_sensitive (state);
270 void
271 ActionManager::uncheck_toggleaction (const char * name)
273 const char *last_slash = strrchr (name, '/');
275 if (last_slash == 0) {
276 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
277 /*NOTREACHED*/
278 return;
281 /* 10 = strlen ("<Actions>/") */
282 size_t len = last_slash - (name + 10);
284 char* group_name = new char[len+1];
285 memcpy (group_name, name + 10, len);
286 group_name[len] = '\0';
288 const char* action_name = last_slash + 1;
290 RefPtr<Action> act = get_action (group_name, action_name);
291 if (act) {
292 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
293 tact->set_active (false);
294 } else {
295 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
298 delete [] group_name;
301 string
302 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
304 bool known = lookup_entry (accel_path, key);
306 if (known) {
307 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
308 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
309 return ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
312 return unbound_string;