Copy local state in AudioRegionView copy constructor. Fixes #4047.
[ardour2.git] / libs / gtkmm2ext / actions.cc
blob2bbb1520f1321f3cf38169aa584dc6f59b14d034
1 /*
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.
20 #include <cstring>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <stdint.h>
26 #include <gtk/gtkaccelmap.h>
27 #include <gtk/gtkuimanager.h>
28 #include <gtk/gtkactiongroup.h>
30 #include <gtkmm/accelmap.h>
31 #include <gtkmm/uimanager.h>
33 #include "pbd/error.h"
35 #include "gtkmm2ext/actions.h"
36 #include "gtkmm2ext/utils.h"
38 #include "i18n.h"
40 using namespace std;
41 using namespace Gtk;
42 using namespace Glib;
43 using namespace sigc;
44 using namespace PBD;
45 using namespace Gtkmm2ext;
47 RefPtr<UIManager> ActionManager::ui_manager;
48 string ActionManager::unbound_string = "--";
51 RefPtr<Action>
52 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
54 RefPtr<Action> act;
56 act = Action::create (name, label);
57 group->add (act, sl);
59 return act;
62 RefPtr<Action>
63 ActionManager::register_action (RefPtr<ActionGroup> group, const char * name, const char * label)
65 RefPtr<Action> act;
67 act = Action::create (name, label);
68 group->add (act);
70 return act;
74 RefPtr<Action>
75 ActionManager::register_radio_action (RefPtr<ActionGroup> group, RadioAction::Group& rgroup, const char * name, const char * label, slot<void> sl)
77 RefPtr<Action> act;
79 act = RadioAction::create (rgroup, name, label);
80 group->add (act, sl);
82 return act;
85 RefPtr<Action>
86 ActionManager::register_radio_action (
87 RefPtr<ActionGroup> group, RadioAction::Group& rgroup, string const & name, string const & label, string const & tooltip, slot<void> sl
90 RefPtr<Action> act;
92 act = RadioAction::create (rgroup, name, label, tooltip);
93 group->add (act, sl);
95 return act;
98 RefPtr<Action>
99 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, const char * name, const char * label, slot<void> sl)
101 RefPtr<Action> act;
103 act = ToggleAction::create (name, label);
104 group->add (act, sl);
106 return act;
109 RefPtr<Action>
110 ActionManager::register_toggle_action (RefPtr<ActionGroup> group, string const & name, string const & label, string const & tooltip, slot<void> sl)
112 RefPtr<Action> act;
114 act = ToggleAction::create (name, label, tooltip);
115 group->add (act, sl);
117 return act;
120 bool
121 ActionManager::lookup_entry (const ustring accel_path, Gtk::AccelKey& key)
123 GtkAccelKey gkey;
124 bool known = gtk_accel_map_lookup_entry (accel_path.c_str(), &gkey);
126 if (known) {
127 key = AccelKey (gkey.accel_key, Gdk::ModifierType (gkey.accel_mods));
128 } else {
129 key = AccelKey (GDK_VoidSymbol, Gdk::ModifierType (0));
132 return known;
135 struct SortActionsByLabel {
136 bool operator() (Glib::RefPtr<Gtk::Action> a, Glib::RefPtr<Gtk::Action> b) {
137 ustring astr = a->get_accel_path();
138 ustring bstr = b->get_accel_path();
139 return astr < bstr;
143 void
144 ActionManager::get_all_actions (vector<string>& groups, vector<string>& names, vector<string>& tooltips, vector<AccelKey>& bindings)
146 /* the C++ API for functions used here appears to be broken in
147 gtkmm2.6, so we fall back to the C level.
150 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
151 GList* node;
152 GList* acts;
154 for (node = list; node; node = g_list_next (node)) {
156 GtkActionGroup* group = (GtkActionGroup*) node->data;
158 /* first pass: collect them all */
160 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
161 action_list the_acts;
163 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
164 GtkAction* action = (GtkAction*) acts->data;
165 the_acts.push_back (Glib::wrap (action, true));
168 /* now sort by label */
170 SortActionsByLabel cmp;
171 the_acts.sort (cmp);
173 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
175 string accel_path = (*a)->get_accel_path ();
177 groups.push_back (gtk_action_group_get_name(group));
178 names.push_back (accel_path.substr (accel_path.find_last_of ('/') + 1));
179 tooltips.push_back ((*a)->get_tooltip ());
181 AccelKey key;
182 lookup_entry (accel_path, key);
183 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
188 void
189 ActionManager::get_all_actions (vector<string>& names, vector<string>& paths, vector<string>& tooltips, vector<string>& keys, vector<AccelKey>& bindings)
191 /* the C++ API for functions used here appears to be broken in
192 gtkmm2.6, so we fall back to the C level.
195 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
196 GList* node;
197 GList* acts;
199 for (node = list; node; node = g_list_next (node)) {
201 GtkActionGroup* group = (GtkActionGroup*) node->data;
203 /* first pass: collect them all */
205 typedef std::list<Glib::RefPtr<Gtk::Action> > action_list;
206 action_list the_acts;
208 for (acts = gtk_action_group_list_actions (group); acts; acts = g_list_next (acts)) {
209 GtkAction* action = (GtkAction*) acts->data;
210 the_acts.push_back (Glib::wrap (action, true));
213 /* now sort by label */
215 SortActionsByLabel cmp;
216 the_acts.sort (cmp);
218 for (action_list::iterator a = the_acts.begin(); a != the_acts.end(); ++a) {
220 ustring const label = (*a)->property_label ();
221 string const accel_path = (*a)->get_accel_path ();
223 names.push_back (label);
224 paths.push_back (accel_path);
225 tooltips.push_back ((*a)->get_tooltip ());
227 AccelKey key;
228 keys.push_back (get_key_representation (accel_path, key));
229 bindings.push_back (AccelKey (key.get_key(), Gdk::ModifierType (key.get_mod())));
234 void
235 ActionManager::add_action_group (RefPtr<ActionGroup> grp)
237 ui_manager->insert_action_group (grp);
240 Widget*
241 ActionManager::get_widget (const char * name)
243 return ui_manager->get_widget (name);
246 RefPtr<Action>
247 ActionManager::get_action (const char* path)
249 GtkAction* _act;
250 RefPtr<Action> act;
252 if ((_act = gtk_ui_manager_get_action (ui_manager->gobj(), path)) != 0) {
253 return Glib::wrap (_act, true);
256 return act;
259 RefPtr<Action>
260 ActionManager::get_action (const char* group_name, const char* action_name)
262 /* the C++ API for functions used here appears to be broken in
263 gtkmm2.6, so we fall back to the C level.
266 if (ui_manager == 0) {
267 return RefPtr<Action> ();
270 GList* list = gtk_ui_manager_get_action_groups (ui_manager->gobj());
271 GList* node;
272 RefPtr<Action> act;
274 for (node = list; node; node = g_list_next (node)) {
276 GtkActionGroup* _ag = (GtkActionGroup*) node->data;
278 if (strcmp (group_name, gtk_action_group_get_name (_ag)) == 0) {
280 GtkAction* _act;
282 if ((_act = gtk_action_group_get_action (_ag, action_name)) != 0) {
283 act = Glib::wrap (_act, true);
284 break;
289 return act;
292 void
293 ActionManager::set_sensitive (vector<RefPtr<Action> >& actions, bool state)
295 for (vector<RefPtr<Action> >::iterator i = actions.begin(); i != actions.end(); ++i) {
296 (*i)->set_sensitive (state);
300 void
301 ActionManager::uncheck_toggleaction (string n)
303 char const * name = n.c_str ();
305 const char *last_slash = strrchr (name, '/');
307 if (last_slash == 0) {
308 fatal << string_compose ("programmer error: %1 %2", "illegal toggle action name", name) << endmsg;
309 /*NOTREACHED*/
310 return;
313 /* 10 = strlen ("<Actions>/") */
314 size_t len = last_slash - (name + 10);
316 char* group_name = new char[len+1];
317 memcpy (group_name, name + 10, len);
318 group_name[len] = '\0';
320 const char* action_name = last_slash + 1;
322 RefPtr<Action> act = get_action (group_name, action_name);
323 if (act) {
324 RefPtr<ToggleAction> tact = RefPtr<ToggleAction>::cast_dynamic(act);
325 tact->set_active (false);
326 } else {
327 error << string_compose (_("Unknown action name: %1"), name) << endmsg;
330 delete [] group_name;
333 string
334 ActionManager::get_key_representation (const string& accel_path, AccelKey& key)
336 bool known = lookup_entry (accel_path, key);
338 if (known) {
339 uint32_t k = possibly_translate_legal_accelerator_to_real_key (key.get_key());
340 key = AccelKey (k, Gdk::ModifierType (key.get_mod()));
341 return ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
344 return unbound_string;