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 "pbd/error.h"
39 #include "gtkmm2ext/actions.h"
40 #include "gtkmm2ext/utils.h"
49 using namespace Gtkmm2ext;
51 RefPtr<UIManager> ActionManager::ui_manager;
52 string ActionManager::unbound_string = "--";
56 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
60 act = Action::create (name, label);
67 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
71 act = Action::create (name, label);
79 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
83 act = RadioAction::create (rgroup, name, label);
90 ActionManager::register_radio_action (
91 RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
96 act = RadioAction::create (rgroup, name, label, tooltip);
103 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
107 act = ToggleAction::create (name, label);
108 group->add (act, sl);
114 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
118 act = ToggleAction::create (name, label, tooltip);
119 group->add (act, sl);
125 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
128 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
131 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
133 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
139 struct SortActionsByLabel {
140 bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
141 ustring astr = a->get_accel_path();
142 ustring bstr = b->get_accel_path();
148 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
150 /* the C++ API for functions used here appears to be broken in
151 gtkmm2.6, so we fall back to the C level.
154 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
158 for (node = list; node; node = g_list_next (node)) {
160 GtkActionGroup* group = (GtkActionGroup*) node->data;
162 /* first pass: collect them all */
164 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
165 action_list the_acts;
167 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
168 GtkAction* action = (GtkAction*) acts->data;
169 the_acts.push_back (Glib::wrap (action, true));
172 /* now sort by label */
174 SortActionsByLabel cmp;
177 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
179 string accel_path = (*a)->get_accel_path ();
181 groups.push_back (gtk_action_group_get_name(group));
182 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
183 tooltips.push_back ((*a)->get_tooltip ());
186 lookup_entry (accel_path, key);
187 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
193 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
195 /* the C++ API for functions used here appears to be broken in
196 gtkmm2.6, so we fall back to the C level.
199 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
203 for (node = list; node; node = g_list_next (node)) {
205 GtkActionGroup* group = (GtkActionGroup*) node->data;
207 /* first pass: collect them all */
209 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
210 action_list the_acts;
212 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
213 GtkAction* action = (GtkAction*) acts->data;
214 the_acts.push_back (Glib::wrap (action, true));
217 /* now sort by label */
219 SortActionsByLabel cmp;
222 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
224 ustring const label = (*a)->property_label ();
225 string const accel_path = (*a)->get_accel_path ();
227 names.push_back (label);
228 paths.push_back (accel_path);
229 tooltips.push_back ((*a)->get_tooltip ());
232 keys.push_back (get_key_representation (accel_path, key));
233 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
241 ActionState (GtkAction* a, bool s) : action (a), sensitive (s) {}
244 typedef std::vector<ActionState> ActionStates;
246 static std::stack<boost::shared_ptr<ActionStates> > state_stack;
248 static boost::shared_ptr<ActionStates>
251 boost::shared_ptr<ActionStates> state = boost::shared_ptr<ActionStates>(new ActionStates);
253 /* the C++ API for functions used here appears to be broken in
254 gtkmm2.6, so we fall back to the C level.
257 GList* list = gtk_ui_manager_get_action_groups (ActionManager::ui_manager->gobj());
261 for (node = list; node; node = g_list_next (node)) {
263 GtkActionGroup* group = (GtkActionGroup*) node->data;
265 /* first pass: collect them all */
267 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
268 action_list the_acts;
270 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
271 GtkAction* action = (GtkAction*) acts->data;
273 state->push_back (ActionState (action, gtk_action_get_sensitive (action)));
281 ActionManager::push_action_state ()
283 state_stack.push (get_action_state());
287 ActionManager::pop_action_state ()
289 if (state_stack.empty()) {
290 warning << string_compose (_("programming error: %1"), X_("ActionManager::pop_action_state called with empty stack")) << endmsg;
294 boost::shared_ptr<ActionStates> as = state_stack.top ();
297 for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
298 gtk_action_set_sensitive ((*i).action, (*i).sensitive);
303 ActionManager::disable_all_actions ()
305 push_action_state ();
306 boost::shared_ptr<ActionStates> as = state_stack.top ();
308 for (ActionStates::iterator i = as->begin(); i != as->end(); ++i) {
309 gtk_action_set_sensitive ((*i).action, false);
314 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
316 ui_manager->insert_action_group (grp);
320 ActionManager::get_widget (const char * name)
322 return ui_manager->get_widget (name);
326 ActionManager::get_action (const char* path)
329 return RefPtr<Action>();
332 /* Skip <Actions>/ in path */
334 int len = strlen (path);
337 /* shortest possible path: "a/b" */
338 return RefPtr<Action>();
341 if (len > 10 && !strncmp (path, "<Actions>/", 10 )) {
343 } else if (path[0] == '/') {
347 vector<char> copy(len+1);
348 strcpy (©[0], path);
349 char* slash = strchr (©[0], '/');
351 return RefPtr<Action> ();
355 return get_action (©[0], ++slash);
360 ActionManager::get_action (const char* group_name, const char* action_name)
362 /* the C++ API for functions used here appears to be broken in
363 gtkmm2.6, so we fall back to the C level.
366 if (ui_manager == 0) {
367 return RefPtr<Action> ();
370 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
374 for (node = list; node; node = g_list_next (node)) {
376 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
378 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
382 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
383 act = Glib::wrap (_act, true);
393 ActionManager::get_action_from_name (const char* name)
395 /* the C++ API for functions used here appears to be broken in
396 gtkmm2.6, so we fall back to the C level.
399 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
403 for (node = list; node; node = g_list_next (node)) {
405 GtkActionGroup* group = (GtkActionGroup*) node->data;
407 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
408 GtkAction* action = (GtkAction*) acts->data;
409 if (!strcmp (gtk_action_get_name (action), name)) {
410 return Glib::wrap (action, true);
415 return RefPtr<Action>();
419 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
421 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
422 (*i)->set_sensitive (state);
427 ActionManager::check_toggleaction (string n)
429 set_toggleaction_state (n, true);
433 ActionManager::uncheck_toggleaction (string n)
435 set_toggleaction_state (n, false);
439 ActionManager::set_toggleaction_state (string n, bool s)
441 char const * name = n.c_str ();
443 const char *last_slash = strrchr (name, '/');
445 if (last_slash == 0) {
446 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
451 /* 10 = strlen ("<Actions>/") */
452 size_t len = last_slash - (name + 10);
454 char* group_name = new char[len+1];
455 memcpy (group_name, name + 10, len);
456 group_name[len] = '\0';
458 const char* action_name = last_slash + 1;
460 RefPtr<Action> act = get_action (group_name, action_name);
462 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
463 tact->set_active (s);
465 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
468 delete [] group_name;
472 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
474 bool known = lookup_entry (accel_path, key);
477 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
478 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
479 return ui_manager->get_accel_group()->get_label (key.get_key(), Gdk::ModifierType (key.get_mod()));
482 return unbound_string;
486 ActionManager::do_action (const char* group, const char*action)
488 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
495 ActionManager::set_toggle_action (const char* group, const char*action, bool yn)
497 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (group, action);
499 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
501 tact->set_active (yn);