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.
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
31 #include <gtkmm/accelmap.h>
32 #include <gtkmm/uimanager.h>
34 #include "pbd/error.h"
36 #include "gtkmm2ext/actions.h"
37 #include "gtkmm2ext/utils.h"
46 using namespace Gtkmm2ext;
48 RefPtr<UIManager> ActionManager::ui_manager;
49 string ActionManager::unbound_string = "--";
53 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
57 act = Action::create (name, label);
64 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
68 act = Action::create (name, label);
76 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
80 act = RadioAction::create (rgroup, name, label);
87 ActionManager::register_radio_action (
88 RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
93 act = RadioAction::create (rgroup, name, label, tooltip);
100 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
104 act = ToggleAction::create (name, label);
105 group->add (act, sl);
111 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
115 act = ToggleAction::create (name, label, tooltip);
116 group->add (act, sl);
122 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
125 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
128 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
130 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
136 struct SortActionsByLabel {
137 bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
138 ustring astr = a->get_accel_path();
139 ustring bstr = b->get_accel_path();
145 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
147 /* the C++ API for functions used here appears to be broken in
148 gtkmm2.6, so we fall back to the C level.
151 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
155 for (node = list; node; node = g_list_next (node)) {
157 GtkActionGroup* group = (GtkActionGroup*) node->data;
159 /* first pass: collect them all */
161 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
162 action_list the_acts;
164 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
165 GtkAction* action = (GtkAction*) acts->data;
166 the_acts.push_back (Glib::wrap (action, true));
169 /* now sort by label */
171 SortActionsByLabel cmp;
174 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
176 string accel_path = (*a)->get_accel_path ();
178 groups.push_back (gtk_action_group_get_name(group));
179 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
180 tooltips.push_back ((*a)->get_tooltip ());
183 lookup_entry (accel_path, key);
184 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
190 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
192 /* the C++ API for functions used here appears to be broken in
193 gtkmm2.6, so we fall back to the C level.
196 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
200 for (node = list; node; node = g_list_next (node)) {
202 GtkActionGroup* group = (GtkActionGroup*) node->data;
204 /* first pass: collect them all */
206 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
207 action_list the_acts;
209 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
210 GtkAction* action = (GtkAction*) acts->data;
211 the_acts.push_back (Glib::wrap (action, true));
214 /* now sort by label */
216 SortActionsByLabel cmp;
219 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
221 ustring const label = (*a)->property_label ();
222 string const accel_path = (*a)->get_accel_path ();
224 names.push_back (label);
225 paths.push_back (accel_path);
226 tooltips.push_back ((*a)->get_tooltip ());
229 keys.push_back (get_key_representation (accel_path, key));
230 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
236 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
238 ui_manager->insert_action_group (grp);
242 ActionManager::get_widget (const char * name)
244 return ui_manager->get_widget (name);
248 ActionManager::get_action (const char* path)
251 return RefPtr<Action>();
254 /* Skip <Actions>/ in path */
256 int len = strlen (path);
259 /* shortest possible path: "a/b" */
260 return RefPtr<Action>();
263 if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
265 } else if (path[0] == '/') {
269 vector<char> copy(len+1);
270 strcpy (©[0], path);
271 char* slash = strchr (©[0], '/');
273 return RefPtr<Action> ();
277 return get_action (©[0], ++slash);
282 ActionManager::get_action (const char* group_name, const char* action_name)
284 /* the C++ API for functions used here appears to be broken in
285 gtkmm2.6, so we fall back to the C level.
288 if (ui_manager == 0) {
289 return RefPtr<Action> ();
292 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
296 for (node = list; node; node = g_list_next (node)) {
298 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
300 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
304 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
305 act = Glib::wrap (_act, true);
315 ActionManager::get_action_from_name (const char* name)
317 /* the C++ API for functions used here appears to be broken in
318 gtkmm2.6, so we fall back to the C level.
321 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
325 for (node = list; node; node = g_list_next (node)) {
327 GtkActionGroup* group = (GtkActionGroup*) node->data;
329 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
330 GtkAction* action = (GtkAction*) acts->data;
331 if (!strcmp (gtk_action_get_name (action), name)) {
332 return Glib::wrap (action, true);
337 return RefPtr<Action>();
341 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
343 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
344 (*i)->set_sensitive (state);
349 ActionManager::check_toggleaction (string n)
351 set_toggleaction_state (n, true);
355 ActionManager::uncheck_toggleaction (string n)
357 set_toggleaction_state (n, false);
361 ActionManager::set_toggleaction_state (string n, bool s)
363 char const * name = n.c_str ();
365 const char *last_slash = strrchr (name, '/');
367 if (last_slash == 0) {
368 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
373 /* 10 = strlen ("<Actions>/") */
374 size_t len = last_slash - (name + 10);
376 char* group_name = new char[len+1];
377 memcpy (group_name, name + 10, len);
378 group_name[len] = '\0';
380 const char* action_name = last_slash + 1;
382 RefPtr<Action> act = get_action (group_name, action_name);
384 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
385 tact->set_active (s);
387 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
390 delete [] group_name;
394 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
396 bool known = lookup_entry (accel_path, key);
399 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
400 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
401 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
404 return unbound_string;
408 ActionManager::do_action (const char* group, const char*action)
410 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
417 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
419 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
421 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
423 tact->set_active (yn);