before pushing keybindings to GTK for menu display, make sure that we add back the...
[ardour.git] / libs / gtkmm2ext / bindings.cc
index d06a3a28c1ed3d97ba6ca55f9e2456a6a66cd82b..7d7d4ed0a6ee722026db4668da8b39b57ca12bfa 100644 (file)
@@ -26,6 +26,7 @@
 #include "pbd/convert.h"
 #include "pbd/debug.h"
 #include "pbd/error.h"
+#include "pbd/replace_all.h"
 #include "pbd/xml++.h"
 
 #include "gtkmm2ext/actions.h"
@@ -34,7 +35,7 @@
 #include "gtkmm2ext/keyboard.h"
 #include "gtkmm2ext/utils.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace Glib;
@@ -234,6 +235,96 @@ KeyboardKey::name () const
        return str;
 }
 
+string
+KeyboardKey::native_name () const
+{
+       int s = state();
+
+       string str;
+
+       if (s & Keyboard::PrimaryModifier) {
+               str += Keyboard::primary_modifier_name ();
+       }
+       if (s & Keyboard::SecondaryModifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::secondary_modifier_name ();
+       }
+       if (s & Keyboard::TertiaryModifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::tertiary_modifier_name ();
+       }
+       if (s & Keyboard::Level4Modifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::level4_modifier_name ();
+       }
+
+       if (!str.empty()) {
+               str += '-';
+       }
+
+       char const *gdk_name = gdk_keyval_name (key());
+
+       if (gdk_name) {
+               str += gdk_name;
+       } else {
+               /* fail! */
+               return string();
+       }
+
+       return str;
+}
+
+string
+KeyboardKey::native_short_name () const
+{
+       int s = state();
+
+       string str;
+
+       if (s & Keyboard::PrimaryModifier) {
+               str += Keyboard::primary_modifier_short_name ();
+       }
+       if (s & Keyboard::SecondaryModifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::secondary_modifier_short_name ();
+       }
+       if (s & Keyboard::TertiaryModifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::tertiary_modifier_short_name ();
+       }
+       if (s & Keyboard::Level4Modifier) {
+               if (!str.empty()) {
+                       str += '-';
+               }
+               str += Keyboard::level4_modifier_short_name ();
+       }
+
+       if (!str.empty()) {
+               str += '-';
+       }
+
+       char const *gdk_name = gdk_keyval_name (key());
+
+       if (gdk_name) {
+               str += gdk_name;
+       } else {
+               /* fail! */
+               return string();
+       }
+
+       return str;
+}
+
 bool
 KeyboardKey::make_key (const string& str, KeyboardKey& k)
 {
@@ -506,7 +597,20 @@ Bindings::push_to_gtk (KeyboardKey kb, RefPtr<Action> what)
                 * happens.
                 */
 
-               Gtk::AccelMap::add_entry (what->get_accel_path(), kb.key(), (Gdk::ModifierType) kb.state());
+
+               int mod = kb.state();
+#ifdef __APPLE__
+               /* See comments in Keyboard::Keyboard about GTK handling of MOD2, META and the Command key.
+                *
+                * If we do not do this, GTK+ won't show the correct text for shortcuts in menus.
+                */
+
+               if (mod & GDK_MOD2_MASK) {
+                       mod =  mod | GDK_META_MASK;
+               }
+#endif
+
+               Gtk::AccelMap::add_entry (what->get_accel_path(), kb.key(), (Gdk::ModifierType) mod);
        }
 }
 
@@ -518,23 +622,30 @@ Bindings::replace (KeyboardKey kb, Operation op, string const & action_name, boo
        }
 
        if (is_registered(op, action_name)) {
-               remove(op, action_name, can_save);
+               remove (op, action_name, can_save);
        }
-       add (kb, op, action_name, can_save);
+
+       /* XXX need a way to get the old group name */
+       add (kb, op, action_name, 0, can_save);
+
        return true;
 }
 
 bool
-Bindings::add (KeyboardKey kb, Operation op, string const& action_name, bool can_save)
+Bindings::add (KeyboardKey kb, Operation op, string const& action_name, XMLProperty const* group, bool can_save)
 {
        if (is_registered (op, action_name)) {
                return false;
        }
 
        KeybindingMap& kbm = get_keymap (op);
-
-       KeybindingMap::value_type new_pair (kb, ActionInfo (action_name));
-       kbm.insert (new_pair).first;
+       if (group) {
+               KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name, group->value()));
+               kbm.insert (new_pair).first;
+       } else {
+               KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name));
+               kbm.insert (new_pair).first;
+       }
 
        if (can_save) {
                Keyboard::keybindings_changed ();
@@ -604,7 +715,7 @@ Bindings::activate (MouseButton bb, Operation op)
 }
 
 void
-Bindings::add (MouseButton bb, Operation op, string const& action_name)
+Bindings::add (MouseButton bb, Operation op, string const& action_name, XMLProperty const* /*group*/)
 {
        MouseButtonBindingMap& bbm = get_mousemap(op);
 
@@ -683,84 +794,95 @@ Bindings::save_all_bindings_as_html (ostream& ostr)
                return;
        }
 
-       ostr << "<http>\n<head>\n<title>";
+
+       ostr << "<html>\n<head>\n<title>";
        ostr << PROGRAM_NAME;
-       ostr << "</title>\n</head>\n<body>\n";
+       ostr << "</title>\n";
+
+
+       ostr << "<style>\n";
+       ostr << "\n\
+.key-name-even, .key-name-odd\n\
+{\n\
+    font-weight: bold;\n\
+}\n\
+\n\
+.key-action-odd, .key-action-even\n\
+{\n\
+    font-weight: normal;\n\
+    font-style: italic;\n\
+}";
+       ostr << "</style>\n";
+
+       ostr << "</head>\n<body>\n";
+
+       ostr << "<div class=\"container\">\n";
 
        for (list<Bindings*>::const_iterator b = bindings.begin(); b != bindings.end(); ++b) {
                (*b)->save_as_html (ostr);
        }
 
+       ostr << "</div>\n";
        ostr << "</body>\n";
-       ostr << "</http>\n";
+       ostr << "</html>\n";
 }
 
 void
 Bindings::save_as_html (ostream& ostr) const
 {
-       ostr << "<h1 class=\"binding-set-name\">";
-       ostr << name();
-       ostr << "</h1>\n";
-
-       if (!press_bindings.empty() || !button_press_bindings.empty()) {
 
-               ostr << "<h2 class=\"action-title\">";
-               ostr << _("Press");
-               ostr << "</h2>\n";
+       if (!press_bindings.empty()) {
 
-               if (!press_bindings.empty()) {
+               ostr << "<div class=\"binding-set\">\n";
+               ostr << "<h1>";
+               ostr << name();
+               ostr << "</h1>\n\n";
 
-                       ostr << "<dl class=\"key-binding\">\n";
+               /* first pass: separate by group */
 
-                       for (KeybindingMap::const_iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
-                               if (k->first.name().empty()) {
-                                       continue;
-                               }
-
-                               RefPtr<Action> action;
-
-                               if (k->second.action) {
-                                       action = k->second.action;
-                               } else {
-                                       if (_action_map) {
-                                               action = _action_map->find_action (k->second.action_name);
-                                       }
-                               }
+               typedef std::map<std::string, std::vector<KeybindingMap::const_iterator> > GroupMap;
+               GroupMap group_map;
 
-                               if (!action) {
-                                       continue;
-                               }
+               for (KeybindingMap::const_iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) {
+                       if (k->first.name().empty()) {
+                               continue;
+                       }
 
-                               ostr << "<dt class=\"key-name\">" << k->first.name() << "</dt>\n";
-                               ostr << "<dd class=\"key-action\">" << action->get_label() << "</dd>\n";
+                       string group_name;
+                       if (!k->second.group_name.empty()) {
+                               group_name = k->second.group_name;
+                       } else {
+                               group_name = X_("nogroup");
                        }
 
-                       ostr << "</dl>\n";
+                       GroupMap::iterator gm = group_map.find (group_name);
+                       if (gm == group_map.end()) {
+                               std::vector<KeybindingMap::const_iterator> li;
+                               li.push_back (k);
+                               group_map.insert (make_pair (group_name,li));
+                       } else {
+                               gm->second.push_back (k);
+                       }
                }
-       }
-
-       if (!release_bindings.empty() || !release_bindings.empty()) {
 
-               ostr << "<h2 class=\"action-title\">";
-               ostr << _("Release");
-               ostr << "</h2>\n";
+               for (GroupMap::const_iterator gm = group_map.begin(); gm != group_map.end(); ++gm) {
 
-               if (!release_bindings.empty()) {
-                       ostr << "<dl class=\"key-binding\">\n";
+                       ostr << "<div class=\"group\">\n";
+                       ostr << "<div class=\"group-name\">" << gm->first << "</div>\n";
 
-                       for (KeybindingMap::const_iterator k = release_bindings.begin(); k != release_bindings.end(); ++k) {
+                       for (vector<KeybindingMap::const_iterator>::const_iterator k = gm->second.begin(); k != gm->second.end(); ++k) {
 
-                               if (k->first.name().empty()) {
+                               if ((*k)->first.name().empty()) {
                                        continue;
                                }
 
                                RefPtr<Action> action;
 
-                               if (k->second.action) {
-                                       action = k->second.action;
+                               if ((*k)->second.action) {
+                                       action = (*k)->second.action;
                                } else {
                                        if (_action_map) {
-                                               action = _action_map->find_action (k->second.action_name);
+                                               action = _action_map->find_action ((*k)->second.action_name);
                                        }
                                }
 
@@ -768,12 +890,41 @@ Bindings::save_as_html (ostream& ostr) const
                                        continue;
                                }
 
-                               ostr << "<dt class=\"key-name\">" << k->first.name() << "</dt>\n";
-                               ostr << "<dd class=\"key-action\">" << action->get_label() << "</dd>\n";
-                       }
+                               string key_name = (*k)->first.native_short_name ();
+                               replace_all (key_name, X_("KP_"), X_("Numpad "));
+
+                               string::size_type pos;
+
+                               char const *targets[] = { X_("Separator"), X_("Add"), X_("Subtract"), X_("Decimal"), X_("Divide"),
+                                                         X_("grave"), X_("comma"), X_("period"), X_("asterisk"), X_("backslash"),
+                                                         X_("apostrophe"), X_("minus"), X_("plus"), X_("slash"), X_("semicolon"),
+                                                         X_("colon"), X_("equal"), X_("bracketleft"), X_("bracketright"),
+                                                         X_("ampersand"), X_("numbersign"), X_("parenleft"), X_("parenright"),
+                                                         X_("quoteright"), X_("quoteleft"), X_("exclam"), X_("quotedbl"),
+                                                         0
+                               };
+
+                               char const *replacements[] = { X_("-"), X_("+"), X_("-"), X_("."), X_("/"),
+                                                              X_("`"), X_(","), X_("."), X_("*"), X_("\\"),
+                                                              X_("'"), X_("-"), X_("+"), X_("/"), X_(";"),
+                                                              X_(":"), X_("="), X_("{"), X_("{"),
+                                                              X_("&"), X_("#"), X_("("), X_(")"),
+                                                              X_("`"), X_("'"), X_("!"), X_("\""),
+                               };
+
+                               for (size_t n = 0; targets[n]; ++n) {
+                                       if ((pos = key_name.find (targets[n])) != string::npos) {
+                                               key_name.replace (pos, strlen (targets[n]), replacements[n]);
+                                       }
+                               }
 
-                       ostr << "</dl>\n";
+                               ostr << "<div class=\"key\">" << key_name << "</div>";
+                               ostr << "<div class=\"action\">" << action->get_label() << "</div>\n";
+                       }
+                       ostr << "</div>\n\n";
                }
+
+               ostr << "</div>\n";
        }
 }
 
@@ -813,11 +964,13 @@ Bindings::load_operation (XMLNode const& node)
                        XMLProperty const * ap;
                        XMLProperty const * kp;
                        XMLProperty const * bp;
+                       XMLProperty const * gp;
                        XMLNode const * child = *p;
 
                        ap = child->property ("action");
                        kp = child->property ("key");
                        bp = child->property ("button");
+                       gp = child->property ("group");
 
                        if (!ap || (!kp && !bp)) {
                                continue;
@@ -828,13 +981,13 @@ Bindings::load_operation (XMLNode const& node)
                                if (!KeyboardKey::make_key (kp->value(), k)) {
                                        continue;
                                }
-                               add (k, op, ap->value());
+                               add (k, op, ap->value(), gp);
                        } else {
                                MouseButton b;
                                if (!MouseButton::make_button (bp->value(), b)) {
                                        continue;
                                }
-                               add (b, op, ap->value());
+                               add (b, op, ap->value(), gp);
                        }
                }
        }