missing() on call
[ardour2.git] / libs / gtkmm2ext / bindings.cc
blob64bdd5203ae0ac71e24446abcea6952fb81398e5
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 uint32_t KeyboardKey::_ignored_state = 0;
15 KeyboardKey::KeyboardKey (uint32_t state, uint32_t keycode)
17 uint32_t ignore = _ignored_state;
19 if (gdk_keyval_is_upper (keycode) && gdk_keyval_is_lower (keycode)) {
20 /* key is not subject to case, so ignore SHIFT
22 ignore |= GDK_SHIFT_MASK;
25 _val = (state & ~ignore);
26 _val <<= 32;
27 _val |= keycode;
31 string
32 KeyboardKey::name () const
34 int s = state();
36 string str;
38 if (s & Keyboard::PrimaryModifier) {
39 str += "Primary";
41 if (s & Keyboard::SecondaryModifier) {
42 if (!str.empty()) {
43 str += '-';
45 str += "Secondary";
47 if (s & Keyboard::TertiaryModifier) {
48 if (!str.empty()) {
49 str += '-';
51 str += "Tertiary";
53 if (s & Keyboard::Level4Modifier) {
54 if (!str.empty()) {
55 str += '-';
57 str += "Level4";
60 if (!str.empty()) {
61 str += '-';
64 str += gdk_keyval_name (key());
66 return str;
69 bool
70 KeyboardKey::make_key (const string& str, KeyboardKey& k)
72 int s = 0;
74 if (str.find ("Primary") != string::npos) {
75 s |= Keyboard::PrimaryModifier;
78 if (str.find ("Secondary") != string::npos) {
79 s |= Keyboard::SecondaryModifier;
82 if (str.find ("Tertiary") != string::npos) {
83 s |= Keyboard::TertiaryModifier;
86 if (str.find ("Level4") != string::npos) {
87 s |= Keyboard::Level4Modifier;
90 string::size_type lastmod = str.find_last_of ('-');
91 guint keyval;
93 if (lastmod == string::npos) {
94 keyval = gdk_keyval_from_name (str.c_str());
95 } else {
96 keyval = gdk_keyval_from_name (str.substr (lastmod+1).c_str());
99 if (keyval == GDK_VoidSymbol) {
100 return false;
103 k = KeyboardKey (s, keyval);
104 return true;
107 Bindings::Bindings ()
108 : action_map (0)
112 Bindings::~Bindings()
116 void
117 Bindings::set_action_map (ActionMap& am)
119 action_map = &am;
120 press_bindings.clear ();
121 release_bindings.clear ();
124 bool
125 Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op)
127 KeybindingMap* kbm;
129 switch (op) {
130 case KeyboardKey::Press:
131 kbm = &press_bindings;
132 break;
133 case KeyboardKey::Release:
134 kbm = &release_bindings;
135 break;
138 KeybindingMap::iterator k = kbm->find (kb);
140 if (k == kbm->end()) {
141 /* no entry for this key in the state map */
142 return false;
145 /* lets do it ... */
147 k->second->activate ();
148 return true;
151 void
152 Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr<Action> what)
154 KeybindingMap* kbm;
156 switch (op) {
157 case KeyboardKey::Press:
158 kbm = &press_bindings;
159 break;
160 case KeyboardKey::Release:
161 kbm = &release_bindings;
162 break;
165 KeybindingMap::iterator k = kbm->find (kb);
167 if (k == kbm->end()) {
168 pair<KeyboardKey,RefPtr<Action> > newpair (kb, what);
169 kbm->insert (newpair);
170 cerr << "Bindings added " << kb.key() << " w/ " << kb.state() << endl;
171 } else {
172 k->second = what;
176 void
177 Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op)
179 KeybindingMap* kbm;
181 switch (op) {
182 case KeyboardKey::Press:
183 kbm = &press_bindings;
184 break;
185 case KeyboardKey::Release:
186 kbm = &release_bindings;
187 break;
190 KeybindingMap::iterator k = kbm->find (kb);
192 if (k != kbm->end()) {
193 kbm->erase (k);
197 bool
198 Bindings::save (const string& path)
200 XMLTree tree;
201 XMLNode* root = new XMLNode (X_("Bindings"));
202 tree.set_root (root);
204 XMLNode* presses = new XMLNode (X_("Press"));
205 root->add_child_nocopy (*presses);
207 for (KeybindingMap::iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
208 XMLNode* child;
209 child = new XMLNode (X_("Binding"));
210 child->add_property (X_("key"), k->first.name());
211 child->add_property (X_("action"), k->second->get_name());
212 presses->add_child_nocopy (*child);
215 XMLNode* releases = new XMLNode (X_("Release"));
216 root->add_child_nocopy (*releases);
218 for (KeybindingMap::iterator k = release_bindings.begin(); k != release_bindings.end(); ++k) {
219 XMLNode* child;
220 child = new XMLNode (X_("Binding"));
221 child->add_property (X_("key"), k->first.name());
222 child->add_property (X_("action"), k->second->get_name());
223 releases->add_child_nocopy (*child);
226 if (!tree.write (path)) {
227 ::unlink (path.c_str());
228 return false;
231 return true;
234 bool
235 Bindings::load (const string& path)
237 XMLTree tree;
239 if (!action_map) {
240 return false;
243 if (!tree.read (path)) {
244 return false;
247 press_bindings.clear ();
248 release_bindings.clear ();
250 XMLNode& root (*tree.root());
251 const XMLNodeList& children (root.children());
253 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
255 if ((*i)->name() == X_("Press") || (*i)->name() == X_("Release")) {
257 KeyboardKey::Operation op;
259 if ((*i)->name() == X_("Press")) {
260 op = KeyboardKey::Press;
261 } else {
262 op = KeyboardKey::Release;
265 const XMLNodeList& gchildren ((*i)->children());
267 for (XMLNodeList::const_iterator p = gchildren.begin(); p != gchildren.end(); ++p) {
269 XMLProperty* ap;
270 XMLProperty* kp;
272 ap = (*p)->property ("action");
273 kp = (*p)->property ("key");
275 if (!ap || !kp) {
276 continue;
279 RefPtr<Action> act = action_map->find_action (ap->value());
281 if (!act) {
282 continue;
285 KeyboardKey k;
287 if (!KeyboardKey::make_key (kp->value(), k)) {
288 continue;
291 add (k, op, act);
296 return true;
299 RefPtr<Action>
300 ActionMap::find_action (const string& name)
302 _ActionMap::iterator a = actions.find (name);
304 if (a != actions.end()) {
305 return a->second;
308 return RefPtr<Action>();
311 RefPtr<Action>
312 ActionMap::register_action (const char* path,
313 const char* name, const char* label, sigc::slot<void> sl)
315 string fullpath;
317 RefPtr<Action> act = Action::create (name, label);
319 act->signal_activate().connect (sl);
321 fullpath = path;
322 fullpath += '/';
323 fullpath += name;
325 actions.insert (_ActionMap::value_type (fullpath, act));
326 return act;
329 RefPtr<Action>
330 ActionMap::register_radio_action (const char* path, Gtk::RadioAction::Group& rgroup,
331 const char* name, const char* label,
332 sigc::slot<void,GtkAction*> sl,
333 int value)
335 string fullpath;
337 RefPtr<Action> act = RadioAction::create (rgroup, name, label);
338 RefPtr<RadioAction> ract = RefPtr<RadioAction>::cast_dynamic(act);
339 ract->property_value() = value;
341 act->signal_activate().connect (sigc::bind (sl, act->gobj()));
343 fullpath = path;
344 fullpath += '/';
345 fullpath += name;
347 actions.insert (_ActionMap::value_type (fullpath, act));
348 return act;
351 RefPtr<Action>
352 ActionMap::register_toggle_action (const char* path,
353 const char* name, const char* label, sigc::slot<void> sl)
355 string fullpath;
357 RefPtr<Action> act = ToggleAction::create (name, label);
359 act->signal_activate().connect (sl);
361 fullpath = path;
362 fullpath += '/';
363 fullpath += name;
365 actions.insert (_ActionMap::value_type (fullpath, act));
366 return act;