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