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 = "--";
58 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
62 act = Action::create (name, label);
69 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
73 act = Action::create (name, label);
81 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
85 act = RadioAction::create (rgroup, name, label);
92 ActionManager::register_radio_action (
93 RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
98 act = RadioAction::create (rgroup, name, label, tooltip);
105 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
109 act = ToggleAction::create (name, label);
110 group->add (act, sl);
116 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
120 act = ToggleAction::create (name, label, tooltip);
121 group->add (act, sl);
127 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
130 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
133 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
135 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
141 struct SortActionsByLabel {
142 bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
143 ustring astr = a->get_accel_path();
144 ustring bstr = b->get_accel_path();
150 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
152 /* the C++ API for functions used here appears to be broken in
153 gtkmm2.6, so we fall back to the C level.
156 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
160 for (node = list; node; node = g_list_next (node)) {
162 GtkActionGroup* group = (GtkActionGroup*) node->data;
164 /* first pass: collect them all */
166 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
167 action_list the_acts;
169 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
170 GtkAction* action = (GtkAction*) acts->data;
171 the_acts.push_back (Glib::wrap (action, true));
174 /* now sort by label */
176 SortActionsByLabel cmp;
179 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
181 string accel_path = (*a)->get_accel_path ();
183 groups.push_back (gtk_action_group_get_name(group));
184 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
185 tooltips.push_back ((*a)->get_tooltip ());
188 lookup_entry (accel_path, key);
189 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
195 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
197 /* the C++ API for functions used here appears to be broken in
198 gtkmm2.6, so we fall back to the C level.
201 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
205 for (node = list; node; node = g_list_next (node)) {
207 GtkActionGroup* group = (GtkActionGroup*) node->data;
209 /* first pass: collect them all */
211 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
212 action_list the_acts;
214 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
215 GtkAction* action = (GtkAction*) acts->data;
216 the_acts.push_back (Glib::wrap (action, true));
219 /* now sort by label */
221 SortActionsByLabel cmp;
224 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
226 ustring const label = (*a)->property_label ();
227 string const accel_path = (*a)->get_accel_path ();
229 names.push_back (label);
230 paths.push_back (accel_path);
231 tooltips.push_back ((*a)->get_tooltip ());
234 keys.push_back (get_key_representation (accel_path, key));
235 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
241 ActionManager::enable_accelerators ()
243 /* the C++ API for functions used here appears to be broken in
244 gtkmm2.6, so we fall back to the C level.
247 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
250 string ui_string = "<ui>";
252 /* get all actions, build a string describing them all as <accelerator
256 for (node = list; node; node = g_list_next (node)) {
258 GtkActionGroup* group = (GtkActionGroup*) node->data;
260 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
261 ui_string += "<accelerator action=\"";
263 /* OK, this is pretty stupid ... there is the full
264 * accel path returned by gtk_action_get_accel_path ()
265 * but of course the UIManager doesn't use that, but
266 * just a name, which is the last component of the
267 * path. What a totally ridiculous design.
270 string fullpath = gtk_action_get_accel_path ((GtkAction*) acts->data);
272 ui_string += Glib::path_get_basename (fullpath);
277 ui_string += "</ui>";
281 ui_manager->add_ui_from_string (ui_string);
287 ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {}
290 typedef std::vector<ActionState> ActionStates;
292 static ActionStates action_states_to_restore;
293 static bool actions_disabled = false;
296 ActionManager::save_action_states ()
298 /* the C++ API for functions used here appears to be broken in
299 gtkmm2.6, so we fall back to the C level.
301 GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj());
305 for (node = list; node; node = g_list_next (node)) {
307 GtkActionGroup* group = (GtkActionGroup*) node->data;
309 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
310 GtkAction* action = (GtkAction*) acts->data;
311 action_states_to_restore.push_back (ActionState (action, gtk_action_get_sensitive (action)));
317 ActionManager::enable_active_actions ()
319 if (!actions_disabled) {
323 for (ActionStates::iterator i = action_states_to_restore.begin(); i != action_states_to_restore.end(); ++i) {
324 if ((*i).action && (*i).sensitive) {
325 gtk_action_set_sensitive ((*i).action, true);
329 action_states_to_restore.clear ();
330 actions_disabled = false;
334 ActionManager::disable_active_actions ()
336 if (actions_disabled == true ) {
339 // save all action's states to action_states_to_restore
340 save_action_states ();
342 // set all action's states disabled
343 for (ActionStates::iterator i = action_states_to_restore.begin(); i != action_states_to_restore.end(); ++i) {
344 if ((*i).sensitive) {
345 gtk_action_set_sensitive ((*i).action, false);
348 actions_disabled = true;
352 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
354 ui_manager->insert_action_group (grp);
358 ActionManager::get_widget (const char * name)
360 return ui_manager->get_widget (name);
364 ActionManager::get_action (const char* path)
367 return RefPtr<Action>();
370 /* Skip <Actions>/ in path */
372 int len = strlen (path);
375 /* shortest possible path: "a/b" */
376 return RefPtr<Action>();
379 if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
381 } else if (path[0] == '/') {
385 vector<char> copy(len+1);
386 strcpy (©[0], path);
387 char* slash = strchr (©[0], '/');
389 return RefPtr<Action> ();
393 return get_action (©[0], ++slash);
398 ActionManager::get_action (const char* group_name, const char* action_name)
400 /* the C++ API for functions used here appears to be broken in
401 gtkmm2.6, so we fall back to the C level.
404 if (ui_manager == 0) {
405 return RefPtr<Action> ();
408 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
412 for (node = list; node; node = g_list_next (node)) {
414 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
416 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
420 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
421 act = Glib::wrap (_act, true);
431 ActionManager::get_action_from_name (const char* name)
433 /* the C++ API for functions used here appears to be broken in
434 gtkmm2.6, so we fall back to the C level.
437 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
441 for (node = list; node; node = g_list_next (node)) {
443 GtkActionGroup* group = (GtkActionGroup*) node->data;
445 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
446 GtkAction* action = (GtkAction*) acts->data;
447 if (!strcmp (gtk_action_get_name (action), name)) {
448 return Glib::wrap (action, true);
453 return RefPtr<Action>();
457 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
459 // if actions weren't disabled
460 if (!actions_disabled) {
461 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
462 (*i)->set_sensitive (state);
466 // actions were disabled
467 // so we should just set necessary action's states in action_states_to_restore
468 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
469 // go through action_states_to_restore and set state of actions
470 for (ActionStates::iterator j = action_states_to_restore.begin(); j != action_states_to_restore.end(); ++j) {
471 // all actions should have their individual name, so we can use it for comparison
472 if (gtk_action_get_name ((*j).action) == (*i)->get_name ()) {
473 (*j).sensitive = state;
481 ActionManager::check_toggleaction (string n)
483 set_toggleaction_state (n, true);
487 ActionManager::uncheck_toggleaction (string n)
489 set_toggleaction_state (n, false);
493 ActionManager::set_toggleaction_state (string n, bool s)
495 char const * name = n.c_str ();
497 const char *last_slash = strrchr (name, '/');
499 if (last_slash == 0) {
500 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
501 abort(); /*NOTREACHED*/
505 /* 10 = strlen ("<Actions>/") */
506 size_t len = last_slash - (name + 10);
508 char* group_name = new char[len+1];
509 memcpy (group_name, name + 10, len);
510 group_name[len] = '\0';
512 const char* action_name = last_slash + 1;
514 RefPtr<Action> act = get_action (group_name, action_name);
516 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
517 tact->set_active (s);
519 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
522 delete [] group_name;
526 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
528 bool known = lookup_entry (accel_path, key);
531 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
532 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
533 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
536 return unbound_string;
540 ActionManager::do_action (const char* group, const char*action)
542 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
549 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
551 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
553 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
555 tact->set_active (yn);