2 Copyright (C) 2001 Paul Davis
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.
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.
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.
28 #include <gtkmm/widget.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/accelmap.h>
31 #include <gdk/gdkkeysyms.h>
33 #include "pbd/error.h"
34 #include "pbd/file_utils.h"
35 #include "pbd/search_path.h"
36 #include "pbd/xml++.h"
37 #include "pbd/debug.h"
39 #include "gtkmm2ext/keyboard.h"
40 #include "gtkmm2ext/actions.h"
41 #include "gtkmm2ext/debug.h"
47 using namespace Gtkmm2ext;
50 guint Keyboard::edit_but = 3;
51 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
52 guint Keyboard::delete_but = 3;
53 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
54 guint Keyboard::insert_note_but = 1;
55 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
56 guint Keyboard::snap_mod = GDK_MOD3_MASK;
60 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK; // Command
61 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
62 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
63 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
64 guint Keyboard::CopyModifier = GDK_CONTROL_MASK; // Control
65 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
66 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
68 const char* Keyboard::primary_modifier_name() { return _("Command"); }
69 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
70 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
71 const char* Keyboard::level4_modifier_name() { return _("Option"); }
72 const char* Keyboard::copy_modifier_name() { return _("Control"); }
73 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
77 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
78 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK; // Alt/Option
79 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
80 guint Keyboard::Level4Modifier = GDK_MOD4_MASK; // Mod4/Windows
81 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
82 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
83 guint Keyboard::button2_modifiers = 0; /* not used */
85 const char* Keyboard::primary_modifier_name() { return _("Control"); }
86 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
87 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
88 const char* Keyboard::level4_modifier_name() { return _("Meta"); }
89 const char* Keyboard::copy_modifier_name() { return _("Control"); }
90 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
94 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
95 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
97 Keyboard* Keyboard::_the_keyboard = 0;
98 Gtk::Window* Keyboard::current_window = 0;
99 bool Keyboard::_some_magic_widget_has_focus = false;
101 std::string Keyboard::user_keybindings_path;
102 bool Keyboard::can_save_keybindings = false;
103 bool Keyboard::bindings_changed_after_save_became_legal = false;
104 map<string,string> Keyboard::binding_files;
105 string Keyboard::_current_binding_name;
106 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
108 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
110 GdkModifierType Keyboard::RelevantModifierKeyMask;
113 Keyboard::magic_widget_grab_focus ()
115 _some_magic_widget_has_focus = true;
119 Keyboard::magic_widget_drop_focus ()
121 _some_magic_widget_has_focus = false;
125 Keyboard::some_magic_widget_has_focus ()
127 return _some_magic_widget_has_focus;
130 Keyboard::Keyboard ()
132 if (_the_keyboard == 0) {
133 _the_keyboard = this;
134 _current_binding_name = _("Unknown");
137 RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
139 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
140 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
141 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
142 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
143 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
144 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
146 gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
148 snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
151 Keyboard::~Keyboard ()
153 gtk_key_snooper_remove (snooper_id);
157 Keyboard::get_state (void)
159 XMLNode* node = new XMLNode ("Keyboard");
162 snprintf (buf, sizeof (buf), "%d", edit_but);
163 node->add_property ("edit-button", buf);
164 snprintf (buf, sizeof (buf), "%d", edit_mod);
165 node->add_property ("edit-modifier", buf);
166 snprintf (buf, sizeof (buf), "%d", delete_but);
167 node->add_property ("delete-button", buf);
168 snprintf (buf, sizeof (buf), "%d", delete_mod);
169 node->add_property ("delete-modifier", buf);
170 snprintf (buf, sizeof (buf), "%d", snap_mod);
171 node->add_property ("snap-modifier", buf);
172 snprintf (buf, sizeof (buf), "%d", insert_note_but);
173 node->add_property ("insert-note-button", buf);
174 snprintf (buf, sizeof (buf), "%d", insert_note_mod);
175 node->add_property ("insert-note-modifier", buf);
181 Keyboard::set_state (const XMLNode& node, int /*version*/)
183 const XMLProperty* prop;
185 if ((prop = node.property ("edit-button")) != 0) {
186 sscanf (prop->value().c_str(), "%d", &edit_but);
189 if ((prop = node.property ("edit-modifier")) != 0) {
190 sscanf (prop->value().c_str(), "%d", &edit_mod);
193 if ((prop = node.property ("delete-button")) != 0) {
194 sscanf (prop->value().c_str(), "%d", &delete_but);
197 if ((prop = node.property ("delete-modifier")) != 0) {
198 sscanf (prop->value().c_str(), "%d", &delete_mod);
201 if ((prop = node.property ("snap-modifier")) != 0) {
202 sscanf (prop->value().c_str(), "%d", &snap_mod);
205 if ((prop = node.property ("insert-note-button")) != 0) {
206 sscanf (prop->value().c_str(), "%d", &insert_note_but);
209 if ((prop = node.property ("insert-note-modifier")) != 0) {
210 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
217 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
219 return ((Keyboard *) data)->snooper (widget, event);
223 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
231 "Snoop widget %1 key %2 type %3 state %4 magic %5\n",
232 widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus
236 if (event->keyval == GDK_Shift_R) {
237 keyval = GDK_Shift_L;
239 } else if (event->keyval == GDK_Control_R) {
240 keyval = GDK_Control_L;
243 keyval = event->keyval;
246 if (keyval == GDK_Shift_L) {
247 /* There is a special and rather hacky situation in Editor which makes
248 it useful to know when a shift key has been released, so emit a signal
249 here (see Editor::_stepping_axis_view)
251 ShiftReleased (); /* EMIT SIGNAL */
254 if (event->type == GDK_KEY_PRESS) {
256 if (find (state.begin(), state.end(), keyval) == state.end()) {
257 state.push_back (keyval);
258 sort (state.begin(), state.end());
262 /* key is already down. if its also used for release,
263 prevent auto-repeat events.
266 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
268 const AccelKey& ak (k->first);
270 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
271 DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
278 } else if (event->type == GDK_KEY_RELEASE) {
282 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
284 sort (state.begin(), state.end());
287 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
289 const AccelKey& ak (k->first);
290 two_strings ts (k->second);
292 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
293 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
295 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
297 DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
305 /* Special keys that we want to handle in
306 any dialog, no matter whether it uses
307 the regular set of accelerators or not
310 if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
311 switch (event->keyval) {
313 close_current_dialog ();
323 Keyboard::close_current_dialog ()
325 if (current_window) {
326 current_window->hide ();
332 Keyboard::key_is_down (uint32_t keyval)
334 return find (state.begin(), state.end(), keyval) != state.end();
338 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
340 current_window = win;
345 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
348 switch (ev->detail) {
349 case GDK_NOTIFY_INFERIOR:
350 DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
353 case GDK_NOTIFY_VIRTUAL:
354 DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
358 DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
359 DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
371 Keyboard::set_edit_button (guint but)
377 Keyboard::set_edit_modifier (guint mod)
379 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
381 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
385 Keyboard::set_delete_button (guint but)
391 Keyboard::set_delete_modifier (guint mod)
393 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
395 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
399 Keyboard::set_insert_note_button (guint but)
401 insert_note_but = but;
405 Keyboard::set_insert_note_modifier (guint mod)
407 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
408 insert_note_mod = mod;
409 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
414 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
416 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
418 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
422 Keyboard::set_snap_modifier (guint mod)
424 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
426 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
430 Keyboard::is_edit_event (GdkEventButton *ev)
432 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
433 (ev->button == Keyboard::edit_button()) &&
434 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
438 Keyboard::is_insert_note_event (GdkEventButton *ev)
440 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
441 (ev->button == Keyboard::insert_note_button()) &&
442 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
446 Keyboard::is_button2_event (GdkEventButton* ev)
449 return (ev->button == 2) ||
450 ((ev->button == 1) &&
451 ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
453 return ev->button == 2;
458 Keyboard::is_delete_event (GdkEventButton *ev)
460 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
461 (ev->button == Keyboard::delete_button()) &&
462 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
466 Keyboard::is_context_menu_event (GdkEventButton *ev)
468 return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
470 ((ev->state & RelevantModifierKeyMask) == 0);
474 Keyboard::no_modifiers_active (guint state)
476 return (state & RelevantModifierKeyMask) == 0;
480 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
482 return (state & mask) == (guint) mask;
486 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
488 return (state & RelevantModifierKeyMask) == (guint) mask;
492 Keyboard::keybindings_changed ()
494 if (Keyboard::can_save_keybindings) {
495 Keyboard::bindings_changed_after_save_became_legal = true;
498 Keyboard::save_keybindings ();
502 Keyboard::set_can_save_keybindings (bool yn)
504 can_save_keybindings = yn;
508 Keyboard::save_keybindings ()
510 if (can_save_keybindings && bindings_changed_after_save_became_legal) {
511 Gtk::AccelMap::save (user_keybindings_path);
516 Keyboard::load_keybindings (string path)
519 info << "Loading bindings from " << path << endl;
521 Gtk::AccelMap::load (path);
523 _current_binding_name = _("Unknown");
525 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
526 if (path == x->second) {
527 _current_binding_name = x->first;
534 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
539 /* now find all release-driven bindings */
541 vector<string> groups;
542 vector<string> names;
543 vector<string> tooltips;
544 vector<AccelKey> bindings;
546 ActionManager::get_all_actions (groups, names, tooltips, bindings);
548 vector<string>::iterator g;
549 vector<AccelKey>::iterator b;
550 vector<string>::iterator n;
552 release_keys.clear ();
554 for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
556 s << "Action: " << *n << " Group: " << *g << " Binding: ";
558 if ((*b).get_key() != GDK_VoidSymbol) {
559 s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
564 DEBUG_TRACE (DEBUG::Bindings, s.str ());
567 for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
568 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
569 release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));