3 #include "gtkmm2ext/bindings.h"
4 #include "gtkmm2ext/keyboard.h"
11 using namespace Gtkmm2ext;
14 KeyboardKey::name () const
20 if (s & Keyboard::PrimaryModifier) {
23 if (s & Keyboard::SecondaryModifier) {
29 if (s & Keyboard::TertiaryModifier) {
35 if (s & Keyboard::Level4Modifier) {
46 str += gdk_keyval_name (key());
52 KeyboardKey::make_key (const string& str, KeyboardKey& k)
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 ('-');
75 if (lastmod == string::npos) {
76 keyval = gdk_keyval_from_name (str.c_str());
78 keyval = gdk_keyval_from_name (str.substr (lastmod+1).c_str());
81 if (keyval == GDK_VoidSymbol) {
85 k = KeyboardKey (s, keyval);
99 Bindings::set_action_map (ActionMap& am)
102 press_bindings.clear ();
103 release_bindings.clear ();
107 Bindings::activate (KeyboardKey kb, KeyboardKey::Operation op)
112 case KeyboardKey::Press:
113 kbm = &press_bindings;
115 case KeyboardKey::Release:
116 kbm = &release_bindings;
120 KeybindingMap::iterator k = kbm->find (kb);
122 if (k == kbm->end()) {
123 /* no entry for this key in the state map */
129 cerr << "Firing up " << k->second->get_name() << endl;
130 k->second->activate ();
135 Bindings::add (KeyboardKey kb, KeyboardKey::Operation op, RefPtr<Action> what)
140 case KeyboardKey::Press:
141 kbm = &press_bindings;
143 case KeyboardKey::Release:
144 kbm = &release_bindings;
148 KeybindingMap::iterator k = kbm->find (kb);
150 if (k == kbm->end()) {
151 pair<KeyboardKey,RefPtr<Action> > newpair (kb, what);
152 kbm->insert (newpair);
159 Bindings::remove (KeyboardKey kb, KeyboardKey::Operation op)
164 case KeyboardKey::Press:
165 kbm = &press_bindings;
167 case KeyboardKey::Release:
168 kbm = &release_bindings;
172 KeybindingMap::iterator k = kbm->find (kb);
174 if (k != kbm->end()) {
180 Bindings::save (const string& path)
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) {
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());
206 Bindings::load (const string& path)
211 cerr << "No action map to load bindings with!\n";
215 if (!tree.read (path)) {
216 cerr << "Cannot load XML file @ " << path << endl;
220 press_bindings.clear ();
221 release_bindings.clear ();
223 XMLNode& root (*tree.root());
224 const XMLNodeList& children (root.children());
226 cerr << "check the " << children.size() << " children\n";
228 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
230 cerr << "child name: " << (*i)->name() << endl;
232 if ((*i)->name() == X_("Press") || (*i)->name() == X_("Release")) {
234 KeyboardKey::Operation op;
236 if ((*i)->name() == X_("Press")) {
237 op = KeyboardKey::Press;
239 op = KeyboardKey::Release;
242 const XMLNodeList& gchildren ((*i)->children());
244 for (XMLNodeList::const_iterator p = gchildren.begin(); p != gchildren.end(); ++p) {
249 ap = (*p)->property ("action");
250 kp = (*p)->property ("key");
256 cerr << "key = " << kp->value () << " action = " << ap->value() << endl;
258 RefPtr<Action> act = action_map->find_action (ap->value());
266 if (!KeyboardKey::make_key (kp->value(), k)) {
270 cerr << "binding " << act->get_name() << " to " << k.name() << endl;
281 ActionMap::find_action (const string& name)
283 _ActionMap::iterator a = actions.find (name);
285 if (a != actions.end()) {
289 return RefPtr<Action>();
293 ActionMap::register_action (const char* path,
294 const char* name, const char* label, sigc::slot<void> sl)
298 RefPtr<Action> act = Action::create (name, label);
300 act->signal_activate().connect (sl);
306 actions.insert (_ActionMap::value_type (fullpath, act));
307 cerr << "Registered action @ " << fullpath << endl;
312 ActionMap::register_radio_action (const char* path, Gtk::RadioAction::Group& rgroup,
313 const char* name, const char* label, sigc::slot<void> sl)
317 RefPtr<Action> act = RadioAction::create (rgroup, name, label);
319 act->signal_activate().connect (sl);
325 actions.insert (_ActionMap::value_type (fullpath, act));
326 cerr << "Registered action @ " << fullpath << endl;
332 ActionMap::register_toggle_action (const char*path,
333 const char* name, const char* label, sigc::slot<void> sl)
337 RefPtr<Action> act = ToggleAction::create (name, label);
339 act->signal_activate().connect (sl);
345 actions.insert (_ActionMap::value_type (fullpath, act));
346 cerr << "Registered action @ " << fullpath << endl;