X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fbindings.cc;h=11b55932a4d1336f13c5b05f05bff4173a53646b;hb=c05cfe332868c1aca477aedcecdfb78948e6d559;hp=168e2cd24cc8f702b9377edef9ed76a8d9505071;hpb=add6c7b48021b10db45aa31d22034a718439eaa0;p=ardour.git diff --git a/libs/gtkmm2ext/bindings.cc b/libs/gtkmm2ext/bindings.cc index 168e2cd24c..11b55932a4 100644 --- a/libs/gtkmm2ext/bindings.cc +++ b/libs/gtkmm2ext/bindings.cc @@ -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; @@ -46,8 +47,6 @@ list Bindings::bindings; /* global. Gulp */ list ActionMap::action_maps; /* global. Gulp */ PBD::Signal1 Bindings::BindingsChanged; - -/*============================ ActionNameRegistered ===========================*/ template struct ActionNameRegistered { @@ -61,7 +60,6 @@ struct ActionNameRegistered std::string const& action_name; }; -/*================================ MouseButton ================================*/ MouseButton::MouseButton (uint32_t state, uint32_t keycode) { uint32_t ignore = ~Keyboard::RelevantModifierKeyMask; @@ -179,16 +177,6 @@ KeyboardKey::display_label () const uint32_t mod = state(); -#ifdef __APPLE__ - /* We use both bits (MOD2|META) for Primary on OS X, - * but we don't want MOD2 showing up in listings. - */ - - if (mod & GDK_MOD2_MASK) { - mod &= ~GDK_MOD2_MASK; - } -#endif - return gtk_accelerator_get_label (key(), (GdkModifierType) mod); } @@ -237,6 +225,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) { @@ -267,19 +345,20 @@ KeyboardKey::make_key (const string& str, KeyboardKey& k) string actual; - if (str.size() == 1) { - actual = PBD::downcase (str); - } else { + string::size_type lastmod = str.find_last_of ('-'); + + if (lastmod != string::npos) { + actual = str.substr (lastmod+1); + } + else { actual = str; } - string::size_type lastmod = actual.find_last_of ('-'); - guint keyval; - - if (lastmod != string::npos) { - actual = PBD::downcase (str.substr (lastmod+1)); + if (actual.size() == 1) { + actual = PBD::downcase (actual); } + guint keyval; keyval = gdk_keyval_from_name (actual.c_str()); if (keyval == GDK_VoidSymbol || keyval == 0) { @@ -508,7 +587,10 @@ Bindings::push_to_gtk (KeyboardKey kb, RefPtr what) * happens. */ - Gtk::AccelMap::add_entry (what->get_accel_path(), kb.key(), (Gdk::ModifierType) kb.state()); + + int mod = kb.state(); + + Gtk::AccelMap::add_entry (what->get_accel_path(), kb.key(), (Gdk::ModifierType) mod); } } @@ -520,23 +602,33 @@ 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)) { + if (is_registered (op, action_name)) { return false; } KeybindingMap& kbm = get_keymap (op); + if (group) { + KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name, group->value())); + (void) kbm.insert (new_pair).first; + } else { + KeybindingMap::value_type new_pair = make_pair (kb, ActionInfo (action_name)); + (void) kbm.insert (new_pair).first; + } - KeybindingMap::value_type new_pair (kb, ActionInfo (action_name)); - kbm.insert (new_pair).first; + DEBUG_TRACE (DEBUG::Bindings, string_compose ("add binding between %1 and %2, group [%3]\n", + kb, action_name, (group ? group->value() : string()))); if (can_save) { Keyboard::keybindings_changed (); @@ -606,7 +698,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); @@ -638,16 +730,16 @@ Bindings::save (XMLNode& root) } child = new XMLNode (X_("Binding")); - child->add_property (X_("key"), k->first.name()); - child->add_property (X_("action"), k->second.action_name); + child->set_property (X_("key"), k->first.name()); + child->set_property (X_("action"), k->second.action_name); presses->add_child_nocopy (*child); } for (MouseButtonBindingMap::iterator k = button_press_bindings.begin(); k != button_press_bindings.end(); ++k) { XMLNode* child; child = new XMLNode (X_("Binding")); - child->add_property (X_("button"), k->first.name()); - child->add_property (X_("action"), k->second.action_name); + child->set_property (X_("button"), k->first.name()); + child->set_property (X_("action"), k->second.action_name); presses->add_child_nocopy (*child); } @@ -661,16 +753,16 @@ Bindings::save (XMLNode& root) } child = new XMLNode (X_("Binding")); - child->add_property (X_("key"), k->first.name()); - child->add_property (X_("action"), k->second.action_name); + child->set_property (X_("key"), k->first.name()); + child->set_property (X_("action"), k->second.action_name); releases->add_child_nocopy (*child); } for (MouseButtonBindingMap::iterator k = button_release_bindings.begin(); k != button_release_bindings.end(); ++k) { XMLNode* child; child = new XMLNode (X_("Binding")); - child->add_property (X_("button"), k->first.name()); - child->add_property (X_("action"), k->second.action_name); + child->set_property (X_("button"), k->first.name()); + child->set_property (X_("action"), k->second.action_name); releases->add_child_nocopy (*child); } @@ -678,6 +770,191 @@ Bindings::save (XMLNode& root) root.add_child_nocopy (*releases); } +void +Bindings::save_all_bindings_as_html (ostream& ostr) +{ + if (bindings.empty()) { + return; + } + + + ostr << "\n\n"; + ostr << PROGRAM_NAME; + ostr << "\n"; + + ostr << "\n\n"; + + ostr << "\n\n"; + ostr << "\n\n"; + + /* first column: separate by group */ + ostr << "\n\n"; + + //second column + ostr << "\n\n"; + + + ostr << "\n\n"; + ostr << "
\n\n"; + for (list::const_iterator b = bindings.begin(); b != bindings.end(); ++b) { + (*b)->save_as_html (ostr, true); + } + ostr << "\n\n"; + for (list::const_iterator b = bindings.begin(); b != bindings.end(); ++b) { + (*b)->save_as_html (ostr, false); + } + ostr << "
\n\n"; + + ostr << "

\n\n"; + ostr << "\n\n"; + ostr << "\n\n"; + ostr << "\n\n"; + ostr << "\n\n"; + ostr << "
\n\n"; + ostr << "

Partial List of Available Actions { => with current shortcut, where applicable }

\n\n"; + { + vector paths; + vector labels; + vector tooltips; + vector keys; + vector > actions; + + Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions); + + vector::iterator k; + vector::iterator p; + vector::iterator l; + + for (p = paths.begin(), k = keys.begin(), l = labels.begin(); p != paths.end(); ++k, ++p, ++l) { + + string print_path = *p; + /* strip / from the start */ + print_path = print_path.substr (10); + + if ((*k).empty()) { + ostr << print_path << " ( " << *l << " ) " << "
" << endl; + } else { + ostr << print_path << " ( " << *l << " ) " << " => " << *k << "
" << endl; + } + } + } + ostr << "
\n\n"; + + ostr << "\n"; + ostr << "\n"; +} + +void +Bindings::save_as_html (ostream& ostr, bool categorize) const +{ + + if (!press_bindings.empty()) { + + ostr << "

"; + if (categorize) + ostr << _("Window") << ": " << name() << _(" (Categorized)"); + else + ostr << _("Window") << ": " << name() << _(" (Alphabetical)"); + ostr << "

\n\n"; + + typedef std::map > GroupMap; + GroupMap group_map; + + for (KeybindingMap::const_iterator k = press_bindings.begin(); k != press_bindings.end(); ++k) { + + if (k->first.name().empty()) { + continue; + } + + string group_name; + if (categorize && !k->second.group_name.empty()) { + group_name = k->second.group_name; + } else { + group_name = _("Uncategorized"); + } + + GroupMap::iterator gm = group_map.find (group_name); + if (gm == group_map.end()) { + std::vector li; + li.push_back (k); + group_map.insert (make_pair (group_name,li)); + } else { + gm->second.push_back (k); + } + } + + + for (GroupMap::const_iterator gm = group_map.begin(); gm != group_map.end(); ++gm) { + + if (categorize) { + ostr << "

" << gm->first << "

\n"; + } + + for (vector::const_iterator k = gm->second.begin(); k != gm->second.end(); ++k) { + + if ((*k)->first.name().empty()) { + continue; + } + + RefPtr action; + + if ((*k)->second.action) { + action = (*k)->second.action; + } else { + if (_action_map) { + action = _action_map->find_action ((*k)->second.action_name); + } + } + + if (!action) { + continue; + } + + string key_name = (*k)->first.native_short_name (); + replace_all (key_name, X_("KP_"), X_("Numpad ")); + replace_all (key_name, X_("nabla"), X_("Tab")); + + 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]); + } + } + + key_name.append(" "); + + while (key_name.length()<28) + key_name.append("-"); + + ostr << "" << key_name; + ostr << "" << action->get_label() << "
\n"; + } + ostr << "\n\n"; + + } + + ostr << "\n"; + } +} + bool Bindings::load (XMLNode const& node) { @@ -711,13 +988,16 @@ Bindings::load_operation (XMLNode const& node) for (XMLNodeList::const_iterator p = children.begin(); p != children.end(); ++p) { - XMLProperty* ap; - XMLProperty* kp; - XMLProperty* bp; + XMLProperty const * ap; + XMLProperty const * kp; + XMLProperty const * bp; + XMLProperty const * gp; + XMLNode const * child = *p; - ap = (*p)->property ("action"); - kp = (*p)->property ("key"); - bp = (*p)->property ("button"); + ap = child->property ("action"); + kp = child->property ("key"); + bp = child->property ("button"); + gp = child->property ("group"); if (!ap || (!kp && !bp)) { continue; @@ -728,13 +1008,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); } } } @@ -811,6 +1091,17 @@ Bindings::is_bound (KeyboardKey const& kb, Operation op) const return km.find(kb) != km.end(); } +std::string +Bindings::bound_name (KeyboardKey const& kb, Operation op) const +{ + const KeybindingMap& km = get_keymap(op); + KeybindingMap::const_iterator b = km.find(kb); + if (b == km.end()) { + return ""; + } + return b->second.action_name; +} + bool Bindings::is_registered (Operation op, std::string const& action_name) const { @@ -897,6 +1188,13 @@ ActionMap::find_action (const string& name) RefPtr ActionMap::create_action_group (const string& name) { + Glib::ListHandle > agl = ActionManager::ui_manager->get_action_groups (); + for (Glib::ListHandle >::iterator i = agl.begin (); i != agl.end (); ++i) { + if ((*i)->get_name () == name) { + return *i; + } + } + RefPtr g = ActionGroup::create (name); /* this is one of the places where our own Action management code @@ -1070,5 +1368,6 @@ ActionMap::get_all_actions (std::vector& paths, std::ostream& operator<<(std::ostream& out, Gtkmm2ext::KeyboardKey const & k) { char const *gdk_name = gdk_keyval_name (k.key()); - return out << "Key " << k.key() << " (" << (gdk_name ? gdk_name : "no-key") << ") state " << hex << k.state() << dec; + return out << "Key " << k.key() << " (" << (gdk_name ? gdk_name : "no-key") << ") state " + << hex << k.state() << dec << ' ' << show_gdk_event_state (k.state()); }