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 std::stack<boost::shared_ptr<ActionStates> > state_stack;
294 static boost::shared_ptr<ActionStates>
297 boost::shared_ptr<ActionStates> state = boost::shared_ptr<ActionStates>(new ActionStates);
299 /* the C++ API for functions used here appears to be broken in
300 gtkmm2.6, so we fall back to the C level.
303 GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj());
307 for (node = list; node; node = g_list_next (node)) {
309 GtkActionGroup* group = (GtkActionGroup*) node->data;
311 /* first pass: collect them all */
313 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
314 action_list the_acts;
316 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
317 GtkAction* action = (GtkAction*) acts->data;
319 state->push_back (ActionState (action, gtk_action_get_sensitive (action)));
327 ActionManager::push_action_state ()
329 state_stack.push (get_action_state());
333 ActionManager::pop_action_state ()
335 if (state_stack.empty()) {
336 warning << string_compose (_("programming error: %1"), X_("ActionManager::pop_action_state called with empty stack")) << endmsg;
340 boost::shared_ptr<ActionStates> as = state_stack.top ();
343 for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
344 gtk_action_set_sensitive ((*i).action, (*i).sensitive);
349 ActionManager::disable_all_actions ()
351 push_action_state ();
352 boost::shared_ptr<ActionStates> as = state_stack.top ();
354 for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
355 gtk_action_set_sensitive ((*i).action, false);
360 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
362 ui_manager->insert_action_group (grp);
366 ActionManager::get_widget (const char * name)
368 return ui_manager->get_widget (name);
372 ActionManager::get_action (const char* path)
375 return RefPtr<Action>();
378 /* Skip <Actions>/ in path */
380 int len = strlen (path);
383 /* shortest possible path: "a/b" */
384 return RefPtr<Action>();
387 if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
389 } else if (path[0] == '/') {
393 vector<char> copy(len+1);
394 strcpy (©[0], path);
395 char* slash = strchr (©[0], '/');
397 return RefPtr<Action> ();
401 return get_action (©[0], ++slash);
406 ActionManager::get_action (const char* group_name, const char* action_name)
408 /* the C++ API for functions used here appears to be broken in
409 gtkmm2.6, so we fall back to the C level.
412 if (ui_manager == 0) {
413 return RefPtr<Action> ();
416 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
420 for (node = list; node; node = g_list_next (node)) {
422 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
424 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
428 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
429 act = Glib::wrap (_act, true);
439 ActionManager::get_action_from_name (const char* name)
441 /* the C++ API for functions used here appears to be broken in
442 gtkmm2.6, so we fall back to the C level.
445 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
449 for (node = list; node; node = g_list_next (node)) {
451 GtkActionGroup* group = (GtkActionGroup*) node->data;
453 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
454 GtkAction* action = (GtkAction*) acts->data;
455 if (!strcmp (gtk_action_get_name (action), name)) {
456 return Glib::wrap (action, true);
461 return RefPtr<Action>();
465 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
467 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
468 (*i)->set_sensitive (state);
473 ActionManager::check_toggleaction (string n)
475 set_toggleaction_state (n, true);
479 ActionManager::uncheck_toggleaction (string n)
481 set_toggleaction_state (n, false);
485 ActionManager::set_toggleaction_state (string n, bool s)
487 char const * name = n.c_str ();
489 const char *last_slash = strrchr (name, '/');
491 if (last_slash == 0) {
492 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
497 /* 10 = strlen ("<Actions>/") */
498 size_t len = last_slash - (name + 10);
500 char* group_name = new char[len+1];
501 memcpy (group_name, name + 10, len);
502 group_name[len] = '\0';
504 const char* action_name = last_slash + 1;
506 RefPtr<Action> act = get_action (group_name, action_name);
508 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
509 tact->set_active (s);
511 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
514 delete [] group_name;
518 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
520 bool known = lookup_entry (accel_path, key);
523 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
524 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
525 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
528 return unbound_string;
532 ActionManager::do_action (const char* group, const char*action)
534 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
541 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
543 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
545 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
547 tact->set_active (yn);