part 2 of the OMG-make-radio-buttons-and-actions-work-properly commit for the step...
[ardour2.git] / libs / gtkmm2ext / bindings.cc
blobaf709b49b6c737d026be25c1aa3f6aaef9d83ae0
1 #include <iostream>
2 #include "pbd/xml++.h"
3 #include "gtkmm2ext/bindings.h"
4 #include "gtkmm2ext/keyboard.h"
6 #include "i18n.h"
8 using namespace std;
9 using namespace Glib;
10 using namespace Gtk;
11 using namespace Gtkmm2ext;
13 string
14 KeyboardKey::name () const
16 int s = state();
18 string str;
20 if (s & Keyboard::PrimaryModifier) {
21 str += "Primary";
23 if (s & Keyboard::SecondaryModifier) {
24 if (!str.empty()) {
25 str += '-';
27 str += "Secondary";
29 if (s & Keyboard::TertiaryModifier) {
30 if (!str.empty()) {
31 str += '-';
33 str += "Tertiary";
35 if (s & Keyboard::Level4Modifier) {
36 if (!str.empty()) {
37 str += '-';
39 str += "Level4";
42 if (!str.empty()) {
43 str += '-';
46 str += gdk_keyval_name (key());
48 return str;
51 bool
52 KeyboardKey::make_key (const string& str, KeyboardKey& k)
54 int s = 0;
56 if (str.find ("Primary") != string::npos) {
57 s |= Keyboard::PrimaryModifier;
60 if (str.find ("Secondary") != string::npos) {
61 s |= Keyboard::SecondaryModifier;
64 if (str.find ("Tertiary") != string::npos) {
65 s |= Keyboard::TertiaryModifier;
68 if (str.find ("Level4") != string::npos) {
69 s |= Keyboard::Level4Modifier;
72 string::size_type lastmod = str.find_last_of ('-');
73 guint keyval;
75 if (lastmod == string::npos) {
76 keyval = gdk_keyval_from_name (str.c_str());
77 } else {
78 keyval = gdk_keyval_from_name (str.substr (lastmod+1).c_str());
81 if (keyval == GDK_VoidSymbol) {
82 return false;
85 k = KeyboardKey (s, keyval);
86 return true;
89 Bindings::Bindings ()
90 : action_map (0)
94 Bindings::~Bindings()
98 void
99 Bindings::set_action_map (ActionMap& am)
101 action_map = &am;
102 press_bindings.clear ();
103 release_bindings.clear ();
106 bool
107 Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op)
109 KeybindingMap* kbm;
111 switch (op) {
112 case KeyboardKey::Press:
113 kbm = &press_bindings;
114 break;
115 case KeyboardKey::Release:
116 kbm = &release_bindings;
117 break;
120 KeybindingMap::iterator k = kbm->find (kb);
122 if (k == kbm->end()) {
123 /* no entry for this key in the state map */
124 return false;
127 /* lets do it ... */
129 cerr << "Firing up " << k->second->get_name() << endl;
130 k->second->activate ();
131 return true;
134 void
135 Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr<Action> what)
137 KeybindingMap* kbm;
139 switch (op) {
140 case KeyboardKey::Press:
141 kbm = &press_bindings;
142 break;
143 case KeyboardKey::Release:
144 kbm = &release_bindings;
145 break;
148 KeybindingMap::iterator k = kbm->find (kb);
150 if (k == kbm->end()) {
151 pair<KeyboardKey,RefPtr<Action> > newpair (kb, what);
152 kbm->insert (newpair);
153 } else {
154 k->second = what;
158 void
159 Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op)
161 KeybindingMap* kbm;
163 switch (op) {
164 case KeyboardKey::Press:
165 kbm = &press_bindings;
166 break;
167 case KeyboardKey::Release:
168 kbm = &release_bindings;
169 break;
172 KeybindingMap::iterator k = kbm->find (kb);
174 if (k != kbm->end()) {
175 kbm->erase (k);
179 bool
180 Bindings::save (const string& path)
182 XMLTree tree;
183 XMLNode* root = new XMLNode (X_("Bindings"));
184 tree.set_root (root);
186 XMLNode* presses = new XMLNode (X_("Press"));
187 root->add_child_nocopy (*presses);
189 for (KeybindingMap::iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
190 XMLNode* child;
191 child = new XMLNode (X_("Binding"));
192 child->add_property (X_("key"), k->first.name());
193 child->add_property (X_("action"), k->second->get_name());
194 presses->add_child_nocopy (*child);
197 if (!tree.write (path)) {
198 ::unlink (path.c_str());
199 return false;
202 return true;
205 bool
206 Bindings::load (const string& path)
208 XMLTree tree;
210 if (!action_map) {
211 return false;
214 if (!tree.read (path)) {
215 return false;
218 press_bindings.clear ();
219 release_bindings.clear ();
221 XMLNode& root (*tree.root());
222 const XMLNodeList& children (root.children());
224 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
226 if ((*i)->name() == X_("Press") || (*i)->name() == X_("Release")) {
228 KeyboardKey::Operation op;
230 if ((*i)->name() == X_("Press")) {
231 op = KeyboardKey::Press;
232 } else {
233 op = KeyboardKey::Release;
236 const XMLNodeList& gchildren ((*i)->children());
238 for (XMLNodeList::const_iterator p = gchildren.begin(); p != gchildren.end(); ++p) {
240 XMLProperty* ap;
241 XMLProperty* kp;
243 ap = (*p)->property ("action");
244 kp = (*p)->property ("key");
246 if (!ap || !kp) {
247 continue;
250 RefPtr<Action> act = action_map->find_action (ap->value());
252 if (!act) {
253 continue;
256 KeyboardKey k;
258 if (!KeyboardKey::make_key (kp->value(), k)) {
259 continue;
262 add (k, op, act);
267 return true;
270 RefPtr<Action>
271 ActionMap::find_action (const string& name)
273 _ActionMap::iterator a = actions.find (name);
275 if (a != actions.end()) {
276 return a->second;
279 return RefPtr<Action>();
282 RefPtr<Action>
283 ActionMap::register_action (const char* path,
284 const char* name, const char* label, sigc::slot<void> sl)
286 string fullpath;
288 RefPtr<Action> act = Action::create (name, label);
290 act->signal_activate().connect (sl);
292 fullpath = path;
293 fullpath += '/';
294 fullpath += name;
296 actions.insert (_ActionMap::value_type (fullpath, act));
297 return act;
300 RefPtr<Action>
301 ActionMap::register_radio_action (const char* path, Gtk::RadioAction::Group& rgroup,
302 const char* name, const char* label,
303 sigc::slot<void,GtkAction*> sl,
304 int value)
306 string fullpath;
308 RefPtr<Action> act = RadioAction::create (rgroup, name, label);
309 RefPtr<RadioAction> ract = RefPtr<RadioAction>::cast_dynamic(act);
310 ract->property_value() = value;
312 act->signal_activate().connect (sigc::bind (sl, act->gobj()));
314 fullpath = path;
315 fullpath += '/';
316 fullpath += name;
318 actions.insert (_ActionMap::value_type (fullpath, act));
319 return act;
322 RefPtr<Action>
323 ActionMap::register_toggle_action (const char* path,
324 const char* name, const char* label, sigc::slot<void> sl)
326 string fullpath;
328 RefPtr<Action> act = ToggleAction::create (name, label);
330 act->signal_activate().connect (sl);
332 fullpath = path;
333 fullpath += '/';
334 fullpath += name;
336 actions.insert (_ActionMap::value_type (fullpath, act));
337 return act;