Remove ancient, unmaintained xcode project files
[ardour.git] / libs / gtkmm2ext / bindings.cc
1 /*
2   Copyright (C) 2012 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <iostream>
21
22 #include "pbd/gstdio_compat.h"
23 #include <gtkmm/accelmap.h>
24 #include <gtkmm/uimanager.h>
25
26 #include "pbd/convert.h"
27 #include "pbd/debug.h"
28 #include "pbd/error.h"
29 #include "pbd/replace_all.h"
30 #include "pbd/xml++.h"
31
32 #include "gtkmm2ext/actions.h"
33 #include "gtkmm2ext/bindings.h"
34 #include "gtkmm2ext/debug.h"
35 #include "gtkmm2ext/keyboard.h"
36 #include "gtkmm2ext/utils.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace std;
41 using namespace Glib;
42 using namespace Gtk;
43 using namespace Gtkmm2ext;
44 using namespace PBD;
45
46 list<Bindings*> Bindings::bindings; /* global. Gulp */
47 PBD::Signal1<void,Bindings*> Bindings::BindingsChanged;
48
49 template <typename IteratorValueType>
50 struct ActionNameRegistered
51 {
52         ActionNameRegistered(std::string const& name)
53                 : action_name(name)
54         {}
55
56         bool operator()(IteratorValueType elem) const {
57                 return elem.second.action_name == action_name;
58         }
59         std::string const& action_name;
60 };
61
62 MouseButton::MouseButton (uint32_t state, uint32_t keycode)
63 {
64         uint32_t ignore = ~Keyboard::RelevantModifierKeyMask;
65
66         /* this is a slightly wierd test that relies on
67          * gdk_keyval_is_{upper,lower}() returning true for keys that have no
68          * case-sensitivity. This covers mostly non-alphanumeric keys.
69          */
70
71         if (gdk_keyval_is_upper (keycode) && gdk_keyval_is_lower (keycode)) {
72                 /* key is not subject to case, so ignore SHIFT
73                  */
74                 ignore |= GDK_SHIFT_MASK;
75         }
76
77         _val = (state & ~ignore);
78         _val <<= 32;
79         _val |= keycode;
80 };
81
82 bool
83 MouseButton::make_button (const string& str, MouseButton& b)
84 {
85         int s = 0;
86
87         if (str.find ("Primary") != string::npos) {
88                 s |= Keyboard::PrimaryModifier;
89         }
90
91         if (str.find ("Secondary") != string::npos) {
92                 s |= Keyboard::SecondaryModifier;
93         }
94
95         if (str.find ("Tertiary") != string::npos) {
96                 s |= Keyboard::TertiaryModifier;
97         }
98
99         if (str.find ("Level4") != string::npos) {
100                 s |= Keyboard::Level4Modifier;
101         }
102
103         string::size_type lastmod = str.find_last_of ('-');
104         uint32_t button_number;
105
106         if (lastmod == string::npos) {
107                 button_number = PBD::atoi (str);
108         } else {
109                 button_number = PBD::atoi (str.substr (lastmod+1));
110         }
111
112         b = MouseButton (s, button_number);
113         return true;
114 }
115
116 string
117 MouseButton::name () const
118 {
119         int s = state();
120
121         string str;
122
123         if (s & Keyboard::PrimaryModifier) {
124                 str += "Primary";
125         }
126         if (s & Keyboard::SecondaryModifier) {
127                 if (!str.empty()) {
128                         str += '-';
129                 }
130                 str += "Secondary";
131         }
132         if (s & Keyboard::TertiaryModifier) {
133                 if (!str.empty()) {
134                         str += '-';
135                 }
136                 str += "Tertiary";
137         }
138         if (s & Keyboard::Level4Modifier) {
139                 if (!str.empty()) {
140                         str += '-';
141                 }
142                 str += "Level4";
143         }
144
145         if (!str.empty()) {
146                 str += '-';
147         }
148
149         char buf[16];
150         snprintf (buf, sizeof (buf), "%u", button());
151         str += buf;
152
153         return str;
154 }
155
156 /*================================ KeyboardKey ================================*/
157 KeyboardKey::KeyboardKey (uint32_t state, uint32_t keycode)
158 {
159         uint32_t ignore = ~Keyboard::RelevantModifierKeyMask;
160
161         _val = (state & ~ignore);
162         _val <<= 32;
163         _val |= keycode;
164 }
165
166 string
167 KeyboardKey::display_label () const
168 {
169         if (key() == 0) {
170                 return string();
171         }
172
173         /* This magically returns a string that will display the right thing
174          *  on all platforms, notably the command key on OS X.
175          */
176
177         uint32_t mod = state();
178
179         return gtk_accelerator_get_label (key(), (GdkModifierType) mod);
180 }
181
182 string
183 KeyboardKey::name () const
184 {
185         int s = state();
186
187         string str;
188
189         if (s & Keyboard::PrimaryModifier) {
190                 str += "Primary";
191         }
192         if (s & Keyboard::SecondaryModifier) {
193                 if (!str.empty()) {
194                         str += '-';
195                 }
196                 str += "Secondary";
197         }
198         if (s & Keyboard::TertiaryModifier) {
199                 if (!str.empty()) {
200                         str += '-';
201                 }
202                 str += "Tertiary";
203         }
204         if (s & Keyboard::Level4Modifier) {
205                 if (!str.empty()) {
206                         str += '-';
207                 }
208                 str += "Level4";
209         }
210
211         if (!str.empty()) {
212                 str += '-';
213         }
214
215         char const *gdk_name = gdk_keyval_name (key());
216
217         if (gdk_name) {
218                 str += gdk_name;
219         } else {
220                 /* fail! */
221                 return string();
222         }
223
224         return str;
225 }
226
227 string
228 KeyboardKey::native_name () const
229 {
230         int s = state();
231
232         string str;
233
234         if (s & Keyboard::PrimaryModifier) {
235                 str += Keyboard::primary_modifier_name ();
236         }
237         if (s & Keyboard::SecondaryModifier) {
238                 if (!str.empty()) {
239                         str += '-';
240                 }
241                 str += Keyboard::secondary_modifier_name ();
242         }
243         if (s & Keyboard::TertiaryModifier) {
244                 if (!str.empty()) {
245                         str += '-';
246                 }
247                 str += Keyboard::tertiary_modifier_name ();
248         }
249         if (s & Keyboard::Level4Modifier) {
250                 if (!str.empty()) {
251                         str += '-';
252                 }
253                 str += Keyboard::level4_modifier_name ();
254         }
255
256         if (!str.empty()) {
257                 str += '-';
258         }
259
260         char const *gdk_name = gdk_keyval_name (key());
261
262         if (gdk_name) {
263                 str += gdk_name;
264         } else {
265                 /* fail! */
266                 return string();
267         }
268
269         return str;
270 }
271
272 string
273 KeyboardKey::native_short_name () const
274 {
275         int s = state();
276
277         string str;
278
279         if (s & Keyboard::PrimaryModifier) {
280                 str += Keyboard::primary_modifier_short_name ();
281         }
282         if (s & Keyboard::SecondaryModifier) {
283                 if (!str.empty()) {
284                         str += '-';
285                 }
286                 str += Keyboard::secondary_modifier_short_name ();
287         }
288         if (s & Keyboard::TertiaryModifier) {
289                 if (!str.empty()) {
290                         str += '-';
291                 }
292                 str += Keyboard::tertiary_modifier_short_name ();
293         }
294         if (s & Keyboard::Level4Modifier) {
295                 if (!str.empty()) {
296                         str += '-';
297                 }
298                 str += Keyboard::level4_modifier_short_name ();
299         }
300
301         if (!str.empty()) {
302                 str += '-';
303         }
304
305         char const *gdk_name = gdk_keyval_name (key());
306
307         if (gdk_name) {
308                 str += gdk_name;
309         } else {
310                 /* fail! */
311                 return string();
312         }
313
314         return str;
315 }
316
317 bool
318 KeyboardKey::make_key (const string& str, KeyboardKey& k)
319 {
320         int s = 0;
321
322         if (str.find ("Primary") != string::npos) {
323                 s |= Keyboard::PrimaryModifier;
324         }
325
326         if (str.find ("Secondary") != string::npos) {
327                 s |= Keyboard::SecondaryModifier;
328         }
329
330         if (str.find ("Tertiary") != string::npos) {
331                 s |= Keyboard::TertiaryModifier;
332         }
333
334         if (str.find ("Level4") != string::npos) {
335                 s |= Keyboard::Level4Modifier;
336         }
337
338         /* since all SINGLE key events keycodes are changed to lower case
339          * before looking them up, make sure we only store lower case here. The
340          * Shift part will be stored in the modifier part of the KeyboardKey.
341          *
342          * And yes Mildred, this doesn't cover CapsLock cases. Oh well.
343          */
344
345         string actual;
346
347         string::size_type lastmod = str.find_last_of ('-');
348
349         if (lastmod != string::npos) {
350                 actual = str.substr (lastmod+1);
351         }
352         else {
353                 actual = str;
354         }
355
356         if (actual.size() == 1) {
357                 actual = PBD::downcase (actual);
358         }
359
360         guint keyval;
361         keyval = gdk_keyval_from_name (actual.c_str());
362
363         if (keyval == GDK_VoidSymbol || keyval == 0) {
364                 return false;
365         }
366
367         k = KeyboardKey (s, keyval);
368
369         return true;
370 }
371
372 /*================================= Bindings =================================*/
373 Bindings::Bindings (std::string const& name)
374         : _name (name)
375 {
376         bindings.push_back (this);
377 }
378
379 Bindings::~Bindings()
380 {
381         bindings.remove (this);
382 }
383
384 string
385 Bindings::ardour_action_name (RefPtr<Action> action)
386 {
387         /* Skip "<Actions>/" */
388         return action->get_accel_path ().substr (10);
389 }
390
391 KeyboardKey
392 Bindings::get_binding_for_action (RefPtr<Action> action, Operation& op)
393 {
394         const string action_name = ardour_action_name (action);
395
396         for (KeybindingMap::iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
397
398                 /* option one: action has already been associated with the
399                  * binding
400                  */
401
402                 if (k->second.action == action) {
403                         return k->first;
404                 }
405
406                 /* option two: action name matches, so lookup the action,
407                  * setup the association while we're here, and return the binding.
408                  */
409
410                 if (k->second.action_name == action_name) {
411                         k->second.action = ActionManager::get_action (action_name, false);
412                         return k->first;
413                 }
414
415         }
416
417         for (KeybindingMap::iterator k = release_bindings.begin(); k != release_bindings.end(); ++k) {
418
419                 /* option one: action has already been associated with the
420                  * binding
421                  */
422
423                 if (k->second.action == action) {
424                         return k->first;
425                 }
426
427                 /* option two: action name matches, so lookup the action,
428                  * setup the association while we're here, and return the binding.
429                  */
430
431                 if (k->second.action_name == action_name) {
432                         k->second.action = ActionManager::get_action (action_name, false);
433                         return k->first;
434                 }
435
436         }
437
438         return KeyboardKey::null_key();
439 }
440
441 void
442 Bindings::reassociate ()
443 {
444         dissociate ();
445         associate ();
446 }
447
448 bool
449 Bindings::empty_keys() const
450 {
451         return press_bindings.empty() && release_bindings.empty();
452 }
453
454 bool
455 Bindings::empty_mouse () const
456 {
457         return button_press_bindings.empty() && button_release_bindings.empty();
458 }
459
460 bool
461 Bindings::empty() const
462 {
463         return empty_keys() && empty_mouse ();
464 }
465
466 bool
467 Bindings::activate (KeyboardKey kb, Operation op)
468 {
469         KeybindingMap& kbm = get_keymap (op);
470
471         /* if shift was pressed, GDK will send us (e.g) 'E' rather than 'e'.
472            Our bindings all use the lower case character/keyname, so switch
473            to the lower case before doing the lookup.
474         */
475
476         KeyboardKey unshifted (kb.state(), gdk_keyval_to_lower (kb.key()));
477
478         KeybindingMap::iterator k = kbm.find (unshifted);
479
480         if (k == kbm.end()) {
481                 /* no entry for this key in the state map */
482                 DEBUG_TRACE (DEBUG::Bindings, string_compose ("no binding for %1 (of %2)\n", unshifted, kbm.size()));
483                 return false;
484         }
485
486         RefPtr<Action> action;
487
488         if (k->second.action) {
489                 action = k->second.action;
490         } else {
491                 action = ActionManager::get_action (k->second.action_name, false);
492         }
493
494         if (action) {
495                 /* lets do it ... */
496                 DEBUG_TRACE (DEBUG::Bindings, string_compose ("binding for %1: %2\n", unshifted, k->second.action_name));
497                 action->activate ();
498         } else {
499                 DEBUG_TRACE (DEBUG::Bindings, string_compose ("binding for %1 is known but has no action\n", unshifted));
500         }
501         /* return true even if the action could not be found */
502
503         return true;
504 }
505
506 void
507 Bindings::associate ()
508 {
509         KeybindingMap::iterator k;
510
511         for (k = press_bindings.begin(); k != press_bindings.end(); ++k) {
512                 k->second.action = ActionManager::get_action (k->second.action_name, false);
513                 if (k->second.action) {
514                         push_to_gtk (k->first, k->second.action);
515                 }
516         }
517
518         for (k = release_bindings.begin(); k != release_bindings.end(); ++k) {
519                 k->second.action = ActionManager::get_action (k->second.action_name, false);
520                 /* no working support in GTK for release bindings */
521         }
522
523         MouseButtonBindingMap::iterator b;
524
525         for (b = button_press_bindings.begin(); b != button_press_bindings.end(); ++b) {
526                 b->second.action = ActionManager::get_action (b->second.action_name, false);
527         }
528
529         for (b = button_release_bindings.begin(); b != button_release_bindings.end(); ++b) {
530                 b->second.action = ActionManager::get_action (b->second.action_name, false);
531         }
532 }
533
534 void
535 Bindings::dissociate ()
536 {
537         KeybindingMap::iterator k;
538
539         for (k = press_bindings.begin(); k != press_bindings.end(); ++k) {
540                 k->second.action.clear ();
541         }
542         for (k = release_bindings.begin(); k != release_bindings.end(); ++k) {
543                 k->second.action.clear ();
544         }
545 }
546
547 void
548 Bindings::push_to_gtk (KeyboardKey kb, RefPtr<Action> what)
549 {
550         /* GTK has the useful feature of showing key bindings for actions in
551          * menus. As of August 2015, we have no interest in trying to
552          * reimplement this functionality, so we will use it even though we no
553          * longer use GTK accelerators for handling key events. To do this, we
554          * need to make sure that there is a fully populated GTK AccelMap set
555          * up with all bindings/actions.
556          */
557
558         Gtk::AccelKey gtk_key;
559         bool entry_exists = Gtk::AccelMap::lookup_entry (what->get_accel_path(), gtk_key);
560
561         if (!entry_exists) {
562
563                 /* there is a trick happening here. It turns out that
564                  * gtk_accel_map_add_entry() performs no validation checks on
565                  * the accelerator keyval. This means we can use it to define
566                  * ANY accelerator, even if they violate GTK's rules
567                  * (e.g. about not using navigation keys). This works ONLY when
568                  * the entry in the GTK accelerator map has not already been
569                  * added. The entries will be added by the GTK UIManager when
570                  * building menus, so this code must be called before that
571                  * happens.
572                  */
573
574
575                 int mod = kb.state();
576
577                 Gtk::AccelMap::add_entry (what->get_accel_path(), kb.key(), (Gdk::ModifierType) mod);
578         }
579 }
580
581 bool
582 Bindings::replace (KeyboardKey kb, Operation op, string const & action_name, bool can_save)
583 {
584         if (is_registered(op, action_name)) {
585                 remove (op, action_name, can_save);
586         }
587
588         /* XXX need a way to get the old group name */
589         add (kb, op, action_name, 0, can_save);
590
591         return true;
592 }
593
594 bool
595 Bindings::add (KeyboardKey kb, Operation op, string const& action_name, XMLProperty const* group, bool can_save)
596 {
597         if (is_registered (op, action_name)) {
598                 return false;
599         }
600
601         KeybindingMap& kbm = get_keymap (op);
602         if (group) {
603                 KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name, group->value()));
604                 (void) kbm.insert (new_pair).first;
605         } else {
606                 KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name));
607                 (void) kbm.insert (new_pair).first;
608         }
609
610         DEBUG_TRACE (DEBUG::Bindings, string_compose ("add binding between %1 and %2, group [%3]\n",
611                                                       kb, action_name, (group ? group->value() : string())));
612
613         if (can_save) {
614                 Keyboard::keybindings_changed ();
615         }
616
617         BindingsChanged (this); /* EMIT SIGNAL */
618         return true;
619 }
620
621 bool
622 Bindings::remove (Operation op, std::string const& action_name, bool can_save)
623 {
624         bool erased_action = false;
625         KeybindingMap& kbm = get_keymap (op);
626         for (KeybindingMap::iterator k = kbm.begin(); k != kbm.end(); ++k) {
627                 if (k->second.action_name == action_name) {
628                         kbm.erase (k);
629                         erased_action = true;
630                         break;
631                 }
632         }
633
634         if (!erased_action) {
635                 return erased_action;
636         }
637
638         if (can_save) {
639                 Keyboard::keybindings_changed ();
640         }
641
642         BindingsChanged (this); /* EMIT SIGNAL */
643         return erased_action;
644 }
645
646
647 bool
648 Bindings::activate (MouseButton bb, Operation op)
649 {
650         MouseButtonBindingMap& bbm = get_mousemap(op);
651
652         MouseButtonBindingMap::iterator b = bbm.find (bb);
653
654         if (b == bbm.end()) {
655                 /* no entry for this key in the state map */
656                 return false;
657         }
658
659         RefPtr<Action> action;
660
661         if (b->second.action) {
662                 action = b->second.action;
663         } else {
664                 action = ActionManager::get_action (b->second.action_name, false);
665         }
666
667         if (action) {
668                 /* lets do it ... */
669                 DEBUG_TRACE (DEBUG::Bindings, string_compose ("activating action %1\n", ardour_action_name (action)));
670                 action->activate ();
671         }
672
673         /* return true even if the action could not be found */
674
675         return true;
676 }
677
678 void
679 Bindings::add (MouseButton bb, Operation op, string const& action_name, XMLProperty const* /*group*/)
680 {
681         MouseButtonBindingMap& bbm = get_mousemap(op);
682
683         MouseButtonBindingMap::value_type newpair (bb, ActionInfo (action_name));
684         bbm.insert (newpair);
685 }
686
687 void
688 Bindings::remove (MouseButton bb, Operation op)
689 {
690         MouseButtonBindingMap& bbm = get_mousemap(op);
691         MouseButtonBindingMap::iterator b = bbm.find (bb);
692
693         if (b != bbm.end()) {
694                 bbm.erase (b);
695         }
696 }
697
698 void
699 Bindings::save (XMLNode& root)
700 {
701         XMLNode* presses = new XMLNode (X_("Press"));
702
703         for (KeybindingMap::iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
704                 XMLNode* child;
705
706                 if (k->first.name().empty()) {
707                         continue;
708                 }
709
710                 child = new XMLNode (X_("Binding"));
711                 child->set_property (X_("key"), k->first.name());
712                 child->set_property (X_("action"), k->second.action_name);
713                 presses->add_child_nocopy (*child);
714         }
715
716         for (MouseButtonBindingMap::iterator k = button_press_bindings.begin(); k != button_press_bindings.end(); ++k) {
717                 XMLNode* child;
718                 child = new XMLNode (X_("Binding"));
719                 child->set_property (X_("button"), k->first.name());
720                 child->set_property (X_("action"), k->second.action_name);
721                 presses->add_child_nocopy (*child);
722         }
723
724         XMLNode* releases = new XMLNode (X_("Release"));
725
726         for (KeybindingMap::iterator k = release_bindings.begin(); k != release_bindings.end(); ++k) {
727                 XMLNode* child;
728
729                 if (k->first.name().empty()) {
730                         continue;
731                 }
732
733                 child = new XMLNode (X_("Binding"));
734                 child->set_property (X_("key"), k->first.name());
735                 child->set_property (X_("action"), k->second.action_name);
736                 releases->add_child_nocopy (*child);
737         }
738
739         for (MouseButtonBindingMap::iterator k = button_release_bindings.begin(); k != button_release_bindings.end(); ++k) {
740                 XMLNode* child;
741                 child = new XMLNode (X_("Binding"));
742                 child->set_property (X_("button"), k->first.name());
743                 child->set_property (X_("action"), k->second.action_name);
744                 releases->add_child_nocopy (*child);
745         }
746
747         root.add_child_nocopy (*presses);
748         root.add_child_nocopy (*releases);
749 }
750
751 void
752 Bindings::save_all_bindings_as_html (ostream& ostr)
753 {
754         if (bindings.empty()) {
755                 return;
756         }
757
758
759         ostr << "<html>\n<head>\n<title>";
760         ostr << PROGRAM_NAME;
761         ostr << "</title>\n";
762         ostr << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
763
764         ostr << "</head>\n<body>\n";
765
766         ostr << "<table border=\"2\" cellpadding=\"6\"><tbody>\n\n";
767         ostr << "<tr>\n\n";
768
769         /* first column: separate by group */
770         ostr << "<td>\n\n";
771         for (list<Bindings*>::const_iterator b = bindings.begin(); b != bindings.end(); ++b) {
772                 (*b)->save_as_html (ostr, true);
773         }
774         ostr << "</td>\n\n";
775
776         //second column
777         ostr << "<td style=\"vertical-align:top\">\n\n";
778         for (list<Bindings*>::const_iterator b = bindings.begin(); b != bindings.end(); ++b) {
779                 (*b)->save_as_html (ostr, false);
780         }
781         ostr << "</td>\n\n";
782
783
784         ostr << "</tr>\n\n";
785         ostr << "</tbody></table>\n\n";
786
787         ostr << "</br></br>\n\n";
788         ostr << "<table border=\"2\" cellpadding=\"6\"><tbody>\n\n";
789         ostr << "<tr>\n\n";
790         ostr << "<td>\n\n";
791         ostr << "<h2><u> Partial List of Available Actions { => with current shortcut, where applicable } </u></h2>\n\n";
792         {
793                 vector<string> paths;
794                 vector<string> labels;
795                 vector<string> tooltips;
796                 vector<string> keys;
797                 vector<Glib::RefPtr<Gtk::Action> > actions;
798
799                 ActionManager::get_all_actions (paths, labels, tooltips, keys, actions);
800
801                 vector<string>::iterator k;
802                 vector<string>::iterator p;
803                 vector<string>::iterator l;
804
805                 for (p = paths.begin(), k = keys.begin(), l = labels.begin(); p != paths.end(); ++k, ++p, ++l) {
806
807                         string print_path = *p;
808                         /* strip <Actions>/ from the start */
809                         print_path = print_path.substr (10);
810
811                         if ((*k).empty()) {
812                                 ostr << print_path  << " ( " << *l << " ) "  << "</br>" << endl;
813                         } else {
814                                 ostr << print_path << " ( " << *l << " ) " << " => " << *k << "</br>" << endl;
815                         }
816                 }
817         }
818         ostr << "</td>\n\n";
819         ostr << "</tr>\n\n";
820         ostr << "</tbody></table>\n\n";
821
822         ostr << "</body>\n";
823         ostr << "</html>\n";
824 }
825
826 void
827 Bindings::save_as_html (ostream& ostr, bool categorize) const
828 {
829
830         if (!press_bindings.empty()) {
831
832                 ostr << "<h2><u>";
833                 if (categorize)
834                         ostr << _("Window") << ": " << name() << _(" (Categorized)");
835                 else
836                         ostr << _("Window") << ": " << name() << _(" (Alphabetical)");
837                 ostr << "</u></h2>\n\n";
838
839                 typedef std::map<std::string, std::vector<KeybindingMap::const_iterator> > GroupMap;
840                 GroupMap group_map;
841
842                 for (KeybindingMap::const_iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
843
844                         if (k->first.name().empty()) {
845                                 continue;
846                         }
847
848                         string group_name;
849                         if (categorize && !k->second.group_name.empty()) {
850                                 group_name = k->second.group_name;
851                         } else {
852                                 group_name = _("Uncategorized");
853                         }
854
855                         GroupMap::iterator gm = group_map.find (group_name);
856                         if (gm == group_map.end()) {
857                                 std::vector<KeybindingMap::const_iterator> li;
858                                 li.push_back (k);
859                                 group_map.insert (make_pair (group_name,li));
860                         } else {
861                                 gm->second.push_back (k);
862                         }
863                 }
864
865
866                 for (GroupMap::const_iterator gm = group_map.begin(); gm != group_map.end(); ++gm) {
867
868                         if (categorize) {
869                                 ostr << "<h3>" << gm->first << "</h3>\n";
870                         }
871
872                         for (vector<KeybindingMap::const_iterator>::const_iterator k = gm->second.begin(); k != gm->second.end(); ++k) {
873
874                                 if ((*k)->first.name().empty()) {
875                                         continue;
876                                 }
877
878                                 RefPtr<Action> action;
879
880                                 if ((*k)->second.action) {
881                                         action = (*k)->second.action;
882                                 } else {
883                                         action = ActionManager::get_action ((*k)->second.action_name, false);
884                                 }
885
886                                 if (!action) {
887                                         continue;
888                                 }
889
890                                 string key_name = (*k)->first.native_short_name ();
891                                 replace_all (key_name, X_("KP_"), X_("Numpad "));
892                                 replace_all (key_name, X_("nabla"), X_("Tab"));
893
894                                 string::size_type pos;
895
896                                 char const *targets[] = { X_("Separator"), X_("Add"), X_("Subtract"), X_("Decimal"), X_("Divide"),
897                                                           X_("grave"), X_("comma"), X_("period"), X_("asterisk"), X_("backslash"),
898                                                           X_("apostrophe"), X_("minus"), X_("plus"), X_("slash"), X_("semicolon"),
899                                                           X_("colon"), X_("equal"), X_("bracketleft"), X_("bracketright"),
900                                                           X_("ampersand"), X_("numbersign"), X_("parenleft"), X_("parenright"),
901                                                           X_("quoteright"), X_("quoteleft"), X_("exclam"), X_("quotedbl"),
902                                                           0
903                                 };
904
905                                 char const *replacements[] = { X_("-"), X_("+"), X_("-"), X_("."), X_("/"),
906                                                                X_("`"), X_(","), X_("."), X_("*"), X_("\\"),
907                                                                X_("'"), X_("-"), X_("+"), X_("/"), X_(";"),
908                                                                X_(":"), X_("="), X_("{"), X_("{"),
909                                                                X_("&"), X_("#"), X_("("), X_(")"),
910                                                                X_("`"), X_("'"), X_("!"), X_("\""),
911                                 };
912
913                                 for (size_t n = 0; targets[n]; ++n) {
914                                         if ((pos = key_name.find (targets[n])) != string::npos) {
915                                                 key_name.replace (pos, strlen (targets[n]), replacements[n]);
916                                         }
917                                 }
918
919                                 key_name.append(" ");
920
921                                 while (key_name.length()<28)
922                                         key_name.append("-");
923
924                                 ostr << "<span style=\"font-family:monospace;\">" << key_name;
925                                 ostr << "<i>" << action->get_label() << "</i></span></br>\n";
926                         }
927                         ostr << "\n\n";
928
929                 }
930
931                 ostr << "\n";
932         }
933 }
934
935 bool
936 Bindings::load (XMLNode const& node)
937 {
938         const XMLNodeList& children (node.children());
939
940         press_bindings.clear ();
941         release_bindings.clear ();
942
943         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
944                 /* each node could be Press or Release */
945                 load_operation (**i);
946         }
947
948         return true;
949 }
950
951 void
952 Bindings::load_operation (XMLNode const& node)
953 {
954         if (node.name() == X_("Press") || node.name() == X_("Release")) {
955
956                 Operation op;
957
958                 if (node.name() == X_("Press")) {
959                         op = Press;
960                 } else {
961                         op = Release;
962                 }
963
964                 const XMLNodeList& children (node.children());
965
966                 for (XMLNodeList::const_iterator p = children.begin(); p != children.end(); ++p) {
967
968                         XMLProperty const * ap;
969                         XMLProperty const * kp;
970                         XMLProperty const * bp;
971                         XMLProperty const * gp;
972                         XMLNode const * child = *p;
973
974                         ap = child->property ("action");
975                         kp = child->property ("key");
976                         bp = child->property ("button");
977                         gp = child->property ("group");
978
979                         if (!ap || (!kp && !bp)) {
980                                 continue;
981                         }
982
983                         if (kp) {
984                                 KeyboardKey k;
985                                 if (!KeyboardKey::make_key (kp->value(), k)) {
986                                         continue;
987                                 }
988                                 add (k, op, ap->value(), gp);
989                         } else {
990                                 MouseButton b;
991                                 if (!MouseButton::make_button (bp->value(), b)) {
992                                         continue;
993                                 }
994                                 add (b, op, ap->value(), gp);
995                         }
996                 }
997         }
998 }
999
1000 void
1001 Bindings::get_all_actions (std::vector<std::string>& paths,
1002                            std::vector<std::string>& labels,
1003                            std::vector<std::string>& tooltips,
1004                            std::vector<std::string>& keys,
1005                            std::vector<RefPtr<Action> >& actions)
1006 {
1007         /* build a reverse map from actions to bindings */
1008
1009         typedef map<Glib::RefPtr<Gtk::Action>,KeyboardKey> ReverseMap;
1010         ReverseMap rmap;
1011
1012         for (KeybindingMap::const_iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
1013                 rmap.insert (make_pair (k->second.action, k->first));
1014         }
1015
1016         /* get a list of all actions XXX relevant for these bindings */
1017
1018         std::vector<Glib::RefPtr<Action> > relevant_actions;
1019         ActionManager::get_actions (this, relevant_actions);
1020
1021         for (vector<Glib::RefPtr<Action> >::const_iterator act = relevant_actions.begin(); act != relevant_actions.end(); ++act) {
1022
1023                 paths.push_back ((*act)->get_accel_path());
1024                 labels.push_back ((*act)->get_label());
1025                 tooltips.push_back ((*act)->get_tooltip());
1026
1027                 ReverseMap::iterator r = rmap.find (*act);
1028
1029                 if (r != rmap.end()) {
1030                         keys.push_back (r->second.display_label());
1031                 } else {
1032                         keys.push_back (string());
1033                 }
1034
1035                 actions.push_back (*act);
1036         }
1037 }
1038
1039 Bindings*
1040 Bindings::get_bindings (string const& name)
1041 {
1042         for (list<Bindings*>::iterator b = bindings.begin(); b != bindings.end(); b++) {
1043                 if ((*b)->name() == name) {
1044                         return *b;
1045                 }
1046         }
1047
1048         return 0;
1049 }
1050
1051 void
1052 Bindings::associate_all ()
1053 {
1054         for (list<Bindings*>::iterator b = bindings.begin(); b != bindings.end(); b++) {
1055                 (*b)->associate ();
1056         }
1057 }
1058
1059 bool
1060 Bindings::is_bound (KeyboardKey const& kb, Operation op) const
1061 {
1062         const KeybindingMap& km = get_keymap(op);
1063         return km.find(kb) != km.end();
1064 }
1065
1066 std::string
1067 Bindings::bound_name (KeyboardKey const& kb, Operation op) const
1068 {
1069         const KeybindingMap& km = get_keymap(op);
1070         KeybindingMap::const_iterator b = km.find(kb);
1071         if (b == km.end()) {
1072                 return "";
1073         }
1074         return b->second.action_name;
1075 }
1076
1077 bool
1078 Bindings::is_registered (Operation op, std::string const& action_name) const
1079 {
1080         const KeybindingMap& km = get_keymap(op);
1081         return std::find_if(km.begin(),  km.end(),  ActionNameRegistered<KeybindingMap::const_iterator::value_type>(action_name)) != km.end();
1082 }
1083
1084 Bindings::KeybindingMap&
1085 Bindings::get_keymap (Operation op)
1086 {
1087         switch (op) {
1088         case Press:
1089                 return press_bindings;
1090         case Release:
1091         default:
1092                 return release_bindings;
1093         }
1094 }
1095
1096 const Bindings::KeybindingMap&
1097 Bindings::get_keymap (Operation op) const
1098 {
1099         switch (op) {
1100         case Press:
1101                 return press_bindings;
1102         case Release:
1103         default:
1104                 return release_bindings;
1105         }
1106 }
1107
1108 Bindings::MouseButtonBindingMap&
1109 Bindings::get_mousemap (Operation op)
1110 {
1111         switch (op) {
1112         case Press:
1113                 return button_press_bindings;
1114         case Release:
1115         default:
1116                 return button_release_bindings;
1117         }
1118 }
1119
1120 std::ostream& operator<<(std::ostream& out, Gtkmm2ext::KeyboardKey const & k) {
1121         char const *gdk_name = gdk_keyval_name (k.key());
1122         return out << "Key " << k.key() << " (" << (gdk_name ? gdk_name : "no-key") << ") state "
1123                    << hex << k.state() << dec << ' ' << show_gdk_event_state (k.state());
1124 }