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.
25 #include <gtk/gtkaccelmap.h>
26 #include <gtk/gtkuimanager.h>
27 #include <gtk/gtkactiongroup.h>
29 #include <gtkmm/accelmap.h>
30 #include <gtkmm/uimanager.h>
32 #include "pbd/error.h"
34 #include "gtkmm2ext/actions.h"
44 RefPtr<UIManager> ActionManager::ui_manager;
45 string ActionManager::unbound_string = "--";
49 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
53 act = Action::create (name, label);
60 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
64 act = Action::create (name, label);
72 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
76 act = RadioAction::create (rgroup, name, label);
83 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
87 act = ToggleAction::create (name, label);
94 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
97 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
100 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
102 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
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();
117 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<AccelKey>& bindings)
119 /* the C++ API for functions used here appears to be broken in
120 gtkmm2.6, so we fall back to the C level.
123 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
127 for (node = list; node; node = g_list_next (node)) {
129 GtkActionGroup* group = (GtkActionGroup*) node->data;
131 /* first pass: collect them all */
133 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
134 action_list the_acts;
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));
141 /* now sort by label */
143 SortActionsByLabel cmp;
146 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
148 string accel_path = (*a)->get_accel_path ();
150 groups.push_back (gtk_action_group_get_name(group));
151 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
154 lookup_entry (accel_path, key);
155 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
161 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& keys, vector<AccelKey>& bindings)
163 /* the C++ API for functions used here appears to be broken in
164 gtkmm2.6, so we fall back to the C level.
167 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
171 for (node = list; node; node = g_list_next (node)) {
173 GtkActionGroup* group = (GtkActionGroup*) node->data;
175 /* first pass: collect them all */
177 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
178 action_list the_acts;
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));
185 /* now sort by label */
187 SortActionsByLabel cmp;
190 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
192 string accel_path = (*a)->get_accel_path ();
193 ustring label = (*a)->property_label();
195 names.push_back (label);
196 paths.push_back (accel_path);
199 keys.push_back (get_key_representation (accel_path, key));
200 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
206 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
208 ui_manager->insert_action_group (grp);
212 ActionManager::get_widget (const char * name)
214 return ui_manager->get_widget (name);
218 ActionManager::get_action (const char* path)
223 if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
224 return Glib::wrap (_act, true);
231 ActionManager::get_action (const char* group_name, const char* action_name)
233 /* the C++ API for functions used here appears to be broken in
234 gtkmm2.6, so we fall back to the C level.
237 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
241 for (node = list; node; node = g_list_next (node)) {
243 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
245 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
249 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
250 act = Glib::wrap (_act, true);
260 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
262 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
263 (*i)->set_sensitive (state);
268 ActionManager::uncheck_toggleaction (const char * name)
270 const char *last_slash = strrchr (name, '/');
272 if (last_slash == 0) {
273 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
278 /* 10 = strlen ("<Actions>/") */
279 size_t len = last_slash - (name + 10);
281 char* group_name = new char[len+1];
282 memcpy (group_name, name + 10, len);
283 group_name[len] = '\0';
285 const char* action_name = last_slash + 1;
287 RefPtr<Action> act = get_action (group_name, action_name);
289 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
290 tact->set_active (false);
292 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
295 delete [] group_name;