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.
27 #include <boost/shared_ptr.hpp>
29 #include <gtk/gtkaccelmap.h>
30 #include <gtk/gtkuimanager.h>
31 #include <gtk/gtkactiongroup.h>
34 #include <gtkmm/accelmap.h>
35 #include <gtkmm/uimanager.h>
37 #include <glibmm/miscutils.h>
39 #include "pbd/error.h"
41 #include "gtkmm2ext/actions.h"
42 #include "gtkmm2ext/utils.h"
51 using namespace Gtkmm2ext;
53 RefPtr<UIManager> ActionManager::ui_manager;
54 string ActionManager::unbound_string = "--";
57 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
60 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
63 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
65 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
71 struct SortActionsByLabel {
72 bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
73 ustring astr = a->get_accel_path();
74 ustring bstr = b->get_accel_path();
80 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
82 /* the C++ API for functions used here appears to be broken in
83 gtkmm2.6, so we fall back to the C level.
86 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
90 for (node = list; node; node = g_list_next (node)) {
92 GtkActionGroup* group = (GtkActionGroup*) node->data;
94 /* first pass: collect them all */
96 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
99 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
100 GtkAction* action = (GtkAction*) acts->data;
101 the_acts.push_back (Glib::wrap (action, true));
104 /* now sort by label */
106 SortActionsByLabel cmp;
109 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
111 string accel_path = (*a)->get_accel_path ();
113 groups.push_back (gtk_action_group_get_name(group));
114 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
115 tooltips.push_back ((*a)->get_tooltip ());
118 lookup_entry (accel_path, key);
119 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
125 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
127 /* the C++ API for functions used here appears to be broken in
128 gtkmm2.6, so we fall back to the C level.
131 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
135 for (node = list; node; node = g_list_next (node)) {
137 GtkActionGroup* group = (GtkActionGroup*) node->data;
139 /* first pass: collect them all */
141 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
142 action_list the_acts;
144 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
145 GtkAction* action = (GtkAction*) acts->data;
146 the_acts.push_back (Glib::wrap (action, true));
149 /* now sort by label */
151 SortActionsByLabel cmp;
154 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
156 ustring const label = (*a)->property_label ();
157 string const accel_path = (*a)->get_accel_path ();
159 names.push_back (label);
160 paths.push_back (accel_path);
161 tooltips.push_back ((*a)->get_tooltip ());
164 keys.push_back (get_key_representation (accel_path, key));
165 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
171 ActionManager::enable_accelerators ()
173 /* the C++ API for functions used here appears to be broken in
174 gtkmm2.6, so we fall back to the C level.
177 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
180 string ui_string = "<ui>";
182 /* get all actions, build a string describing them all as <accelerator
186 for (node = list; node; node = g_list_next (node)) {
188 GtkActionGroup* group = (GtkActionGroup*) node->data;
190 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
191 ui_string += "<accelerator action=\"";
193 /* OK, this is pretty stupid ... there is the full
194 * accel path returned by gtk_action_get_accel_path ()
195 * but of course the UIManager doesn't use that, but
196 * just a name, which is the last component of the
197 * path. What a totally ridiculous design.
200 string fullpath = gtk_action_get_accel_path ((GtkAction*) acts->data);
202 ui_string += Glib::path_get_basename (fullpath);
207 ui_string += "</ui>";
211 ui_manager->add_ui_from_string (ui_string);
217 ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {}
220 typedef std::vector<ActionState> ActionStates;
222 static ActionStates action_states_to_restore;
223 static bool actions_disabled = false;
226 ActionManager::save_action_states ()
228 /* the C++ API for functions used here appears to be broken in
229 gtkmm2.6, so we fall back to the C level.
231 GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj());
235 for (node = list; node; node = g_list_next (node)) {
237 GtkActionGroup* group = (GtkActionGroup*) node->data;
239 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
240 GtkAction* action = (GtkAction*) acts->data;
241 action_states_to_restore.push_back (ActionState (action, gtk_action_get_sensitive (action)));
247 ActionManager::enable_active_actions ()
249 if (!actions_disabled) {
253 for (ActionStates::iterator i = action_states_to_restore.begin(); i != action_states_to_restore.end(); ++i) {
254 if ((*i).action && (*i).sensitive) {
255 gtk_action_set_sensitive ((*i).action, true);
259 action_states_to_restore.clear ();
260 actions_disabled = false;
264 ActionManager::disable_active_actions ()
266 if (actions_disabled == true ) {
269 // save all action's states to action_states_to_restore
270 save_action_states ();
272 // set all action's states disabled
273 for (ActionStates::iterator i = action_states_to_restore.begin(); i != action_states_to_restore.end(); ++i) {
274 if ((*i).sensitive) {
275 gtk_action_set_sensitive ((*i).action, false);
278 actions_disabled = true;
282 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
284 ui_manager->insert_action_group (grp);
288 ActionManager::get_widget (const char * name)
290 return ui_manager->get_widget (name);
294 ActionManager::get_action (const char* path)
297 return RefPtr<Action>();
300 /* Skip <Actions>/ in path */
302 int len = strlen (path);
305 /* shortest possible path: "a/b" */
306 return RefPtr<Action>();
309 if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
311 } else if (path[0] == '/') {
315 vector<char> copy(len+1);
316 strcpy (©[0], path);
317 char* slash = strchr (©[0], '/');
319 return RefPtr<Action> ();
323 return get_action (©[0], ++slash);
328 ActionManager::get_action (const char* group_name, const char* action_name)
330 /* the C++ API for functions used here appears to be broken in
331 gtkmm2.6, so we fall back to the C level.
334 if (ui_manager == 0) {
335 return RefPtr<Action> ();
338 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
342 for (node = list; node; node = g_list_next (node)) {
344 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
346 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
350 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
351 act = Glib::wrap (_act, true);
361 ActionManager::get_action_from_name (const char* name)
363 /* the C++ API for functions used here appears to be broken in
364 gtkmm2.6, so we fall back to the C level.
367 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
371 for (node = list; node; node = g_list_next (node)) {
373 GtkActionGroup* group = (GtkActionGroup*) node->data;
375 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
376 GtkAction* action = (GtkAction*) acts->data;
377 if (!strcmp (gtk_action_get_name (action), name)) {
378 return Glib::wrap (action, true);
383 return RefPtr<Action>();
387 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
389 // if actions weren't disabled
390 if (!actions_disabled) {
391 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
392 (*i)->set_sensitive (state);
396 // actions were disabled
397 // so we should just set necessary action's states in action_states_to_restore
398 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
399 // go through action_states_to_restore and set state of actions
400 for (ActionStates::iterator j = action_states_to_restore.begin(); j != action_states_to_restore.end(); ++j) {
401 // all actions should have their individual name, so we can use it for comparison
402 if (gtk_action_get_name ((*j).action) == (*i)->get_name ()) {
403 (*j).sensitive = state;
411 ActionManager::check_toggleaction (string n)
413 set_toggleaction_state (n, true);
417 ActionManager::uncheck_toggleaction (string n)
419 set_toggleaction_state (n, false);
423 ActionManager::set_toggleaction_state (string n, bool s)
425 char const * name = n.c_str ();
427 const char *last_slash = strrchr (name, '/');
429 if (last_slash == 0) {
430 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
431 abort(); /*NOTREACHED*/
435 /* 10 = strlen ("<Actions>/") */
436 size_t len = last_slash - (name + 10);
438 char* group_name = new char[len+1];
439 memcpy (group_name, name + 10, len);
440 group_name[len] = '\0';
442 const char* action_name = last_slash + 1;
444 RefPtr<Action> act = get_action (group_name, action_name);
446 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
447 tact->set_active (s);
449 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
452 delete [] group_name;
456 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
458 bool known = lookup_entry (accel_path, key);
461 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
462 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
463 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
466 return unbound_string;
470 ActionManager::do_action (const char* group, const char*action)
472 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
479 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
481 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
483 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
485 tact->set_active (yn);