Make scroll-wheel modifier keys consistent in main editor window.
[ardour.git] / libs / gtkmm2ext / keyboard.cc
1 /*
2     Copyright (C) 2001 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 <vector>
21
22 #include <algorithm>
23 #include <fstream>
24 #include <iostream>
25
26 #include <ctype.h>
27
28 #include <gtkmm/widget.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/accelmap.h>
31 #include <gdk/gdkkeysyms.h>
32
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"
38
39 #include "gtkmm2ext/keyboard.h"
40 #include "gtkmm2ext/actions.h"
41 #include "gtkmm2ext/debug.h"
42
43 #include "i18n.h"
44
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48 using namespace std;
49
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;
57
58 #ifdef GTKOSX
59
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;
67
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"); }
74
75 #else
76
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 */
84
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"); }
91
92 #endif
93
94 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
95 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
96
97 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
98 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
99 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
100
101
102 Keyboard*    Keyboard::_the_keyboard = 0;
103 Gtk::Window* Keyboard::current_window = 0;
104 bool         Keyboard::_some_magic_widget_has_focus = false;
105
106 std::string Keyboard::user_keybindings_path;
107 bool Keyboard::can_save_keybindings = false;
108 bool Keyboard::bindings_changed_after_save_became_legal = false;
109 map<string,string> Keyboard::binding_files;
110 string Keyboard::_current_binding_name;
111 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
112
113 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
114
115 GdkModifierType Keyboard::RelevantModifierKeyMask;
116
117 void
118 Keyboard::magic_widget_grab_focus ()
119 {
120         _some_magic_widget_has_focus = true;
121 }
122
123 void
124 Keyboard::magic_widget_drop_focus ()
125 {
126         _some_magic_widget_has_focus = false;
127 }
128
129 bool
130 Keyboard::some_magic_widget_has_focus ()
131 {
132         return _some_magic_widget_has_focus;
133 }
134
135 Keyboard::Keyboard ()
136 {
137         if (_the_keyboard == 0) {
138                 _the_keyboard = this;
139                 _current_binding_name = _("Unknown");
140         }
141
142         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
143
144         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
145         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
146         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
147         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
148         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
149         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
150
151         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
152
153         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
154 }
155
156 Keyboard::~Keyboard ()
157 {
158         gtk_key_snooper_remove (snooper_id);
159 }
160
161 XMLNode&
162 Keyboard::get_state (void)
163 {
164         XMLNode* node = new XMLNode ("Keyboard");
165         char buf[32];
166
167         snprintf (buf, sizeof (buf), "%d", edit_but);
168         node->add_property ("edit-button", buf);
169         snprintf (buf, sizeof (buf), "%d", edit_mod);
170         node->add_property ("edit-modifier", buf);
171         snprintf (buf, sizeof (buf), "%d", delete_but);
172         node->add_property ("delete-button", buf);
173         snprintf (buf, sizeof (buf), "%d", delete_mod);
174         node->add_property ("delete-modifier", buf);
175         snprintf (buf, sizeof (buf), "%d", snap_mod);
176         node->add_property ("snap-modifier", buf);
177         snprintf (buf, sizeof (buf), "%d", insert_note_but);
178         node->add_property ("insert-note-button", buf);
179         snprintf (buf, sizeof (buf), "%d", insert_note_mod);
180         node->add_property ("insert-note-modifier", buf);
181
182         return *node;
183 }
184
185 int
186 Keyboard::set_state (const XMLNode& node, int /*version*/)
187 {
188         const XMLProperty* prop;
189
190         if ((prop = node.property ("edit-button")) != 0) {
191                 sscanf (prop->value().c_str(), "%d", &edit_but);
192         }
193
194         if ((prop = node.property ("edit-modifier")) != 0) {
195                 sscanf (prop->value().c_str(), "%d", &edit_mod);
196         }
197
198         if ((prop = node.property ("delete-button")) != 0) {
199                 sscanf (prop->value().c_str(), "%d", &delete_but);
200         }
201
202         if ((prop = node.property ("delete-modifier")) != 0) {
203                 sscanf (prop->value().c_str(), "%d", &delete_mod);
204         }
205
206         if ((prop = node.property ("snap-modifier")) != 0) {
207                 sscanf (prop->value().c_str(), "%d", &snap_mod);
208         }
209
210         if ((prop = node.property ("insert-note-button")) != 0) {
211                 sscanf (prop->value().c_str(), "%d", &insert_note_but);
212         }
213
214         if ((prop = node.property ("insert-note-modifier")) != 0) {
215                 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
216         }
217
218         return 0;
219 }
220
221 gint
222 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
223 {
224         return ((Keyboard *) data)->snooper (widget, event);
225 }
226
227 gint
228 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
229 {
230         uint32_t keyval;
231         bool ret = false;
232
233         DEBUG_TRACE (
234                 DEBUG::Keyboard,
235                 string_compose (
236                         "Snoop widget %1 key %2 type %3 state %4 magic %5\n",
237                         widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus
238                         )
239                 );
240
241         if (event->keyval == GDK_Shift_R) {
242                 keyval = GDK_Shift_L;
243
244         } else if (event->keyval == GDK_Control_R) {
245                 keyval = GDK_Control_L;
246
247         } else {
248                 keyval = event->keyval;
249         }
250
251         if (keyval == GDK_Shift_L) {
252                 /* There is a special and rather hacky situation in Editor which makes
253                    it useful to know when a shift key has been released, so emit a signal
254                    here (see Editor::_stepping_axis_view)
255                 */
256                 ShiftReleased (); /* EMIT SIGNAL */
257         }
258
259         if (event->type == GDK_KEY_PRESS) {
260
261                 if (find (state.begin(), state.end(), keyval) == state.end()) {
262                         state.push_back (keyval);
263                         sort (state.begin(), state.end());
264
265                 } else {
266
267                         /* key is already down. if its also used for release,
268                            prevent auto-repeat events.
269                         */
270
271                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
272
273                                 const AccelKey& ak (k->first);
274
275                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
276                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
277                                         ret = true;
278                                         break;
279                                 }
280                         }
281                 }
282
283         } else if (event->type == GDK_KEY_RELEASE) {
284
285                 State::iterator i;
286
287                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
288                         state.erase (i);
289                         sort (state.begin(), state.end());
290                 }
291
292                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
293
294                         const AccelKey& ak (k->first);
295                         two_strings ts (k->second);
296
297                         if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
298                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
299                                 if (act) {
300                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
301                                         act->activate();
302                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
303                                         ret = true;
304                                 }
305                                 break;
306                         }
307                 }
308         }
309
310         /* Special keys that we want to handle in
311            any dialog, no matter whether it uses
312            the regular set of accelerators or not
313         */
314
315         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
316                 switch (event->keyval) {
317                 case GDK_w:
318                         close_current_dialog ();
319                         ret = true;
320                         break;
321                 }
322         }
323
324         return ret;
325 }
326
327 void
328 Keyboard::close_current_dialog ()
329 {
330         if (current_window) {
331                 current_window->hide ();
332                 current_window = 0;
333         }
334 }
335
336 bool
337 Keyboard::key_is_down (uint32_t keyval)
338 {
339         return find (state.begin(), state.end(), keyval) != state.end();
340 }
341
342 bool
343 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
344 {
345         current_window = win;
346         return false;
347 }
348
349 bool
350 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
351 {
352         if (ev) {
353                 switch (ev->detail) {
354                 case GDK_NOTIFY_INFERIOR:
355                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
356                         break;
357
358                 case GDK_NOTIFY_VIRTUAL:
359                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
360                         /* fallthru */
361
362                 default:
363                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
364                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
365                         state.clear ();
366                         current_window = 0;
367                 }
368         } else {
369                 current_window = 0;
370         }
371
372         return false;
373 }
374
375 void
376 Keyboard::set_edit_button (guint but)
377 {
378         edit_but = but;
379 }
380
381 void
382 Keyboard::set_edit_modifier (guint mod)
383 {
384         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
385         edit_mod = mod;
386         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
387 }
388
389 void
390 Keyboard::set_delete_button (guint but)
391 {
392         delete_but = but;
393 }
394
395 void
396 Keyboard::set_delete_modifier (guint mod)
397 {
398         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
399         delete_mod = mod;
400         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
401 }
402
403 void
404 Keyboard::set_insert_note_button (guint but)
405 {
406         insert_note_but = but;
407 }
408
409 void
410 Keyboard::set_insert_note_modifier (guint mod)
411 {
412         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
413         insert_note_mod = mod;
414         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
415 }
416
417
418 void
419 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
420 {
421         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
422         var = newval;
423         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
424 }
425
426 void
427 Keyboard::set_snap_modifier (guint mod)
428 {
429         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
430         snap_mod = mod;
431         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
432 }
433
434 bool
435 Keyboard::is_edit_event (GdkEventButton *ev)
436 {
437         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
438                 (ev->button == Keyboard::edit_button()) &&
439                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
440 }
441
442 bool
443 Keyboard::is_insert_note_event (GdkEventButton *ev)
444 {
445         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
446                 (ev->button == Keyboard::insert_note_button()) &&
447                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
448 }
449
450 bool
451 Keyboard::is_button2_event (GdkEventButton* ev)
452 {
453 #ifdef GTKOSX
454         return (ev->button == 2) ||
455                 ((ev->button == 1) &&
456                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
457 #else
458         return ev->button == 2;
459 #endif
460 }
461
462 bool
463 Keyboard::is_delete_event (GdkEventButton *ev)
464 {
465         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
466                 (ev->button == Keyboard::delete_button()) &&
467                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
468 }
469
470 bool
471 Keyboard::is_context_menu_event (GdkEventButton *ev)
472 {
473         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
474                 (ev->button == 3) &&
475                 ((ev->state & RelevantModifierKeyMask) == 0);
476 }
477
478 bool
479 Keyboard::no_modifiers_active (guint state)
480 {
481         return (state & RelevantModifierKeyMask) == 0;
482 }
483
484 bool
485 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
486 {
487         return (state & mask) == (guint) mask;
488 }
489
490 bool
491 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
492 {
493         return (state & RelevantModifierKeyMask) == (guint) mask;
494 }
495
496 void
497 Keyboard::keybindings_changed ()
498 {
499         if (Keyboard::can_save_keybindings) {
500                 Keyboard::bindings_changed_after_save_became_legal = true;
501         }
502
503         Keyboard::save_keybindings ();
504 }
505
506 void
507 Keyboard::set_can_save_keybindings (bool yn)
508 {
509         can_save_keybindings = yn;
510 }
511
512 void
513 Keyboard::save_keybindings ()
514 {
515         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
516                 Gtk::AccelMap::save (user_keybindings_path);
517         }
518 }
519
520 bool
521 Keyboard::load_keybindings (string path)
522 {
523         try {
524                 info << "Loading bindings from " << path << endl;
525
526                 Gtk::AccelMap::load (path);
527
528                 _current_binding_name = _("Unknown");
529
530                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
531                         if (path == x->second) {
532                                 _current_binding_name = x->first;
533                                 break;
534                         }
535                 }
536
537
538         } catch (...) {
539                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
540                       << endmsg;
541                 return false;
542         }
543
544         /* now find all release-driven bindings */
545
546         vector<string> groups;
547         vector<string> names;
548         vector<string> tooltips;
549         vector<AccelKey> bindings;
550
551         ActionManager::get_all_actions (groups, names, tooltips, bindings);
552
553         vector<string>::iterator g;
554         vector<AccelKey>::iterator b;
555         vector<string>::iterator n;
556
557         release_keys.clear ();
558
559         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
560                 stringstream s;
561                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
562
563                 if ((*b).get_key() != GDK_VoidSymbol) {
564                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
565                 } else {
566                         s << "unbound\n";
567                 }
568
569                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
570         }
571
572         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
573                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
574                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
575                 }
576         }
577
578         return true;
579 }
580