Merge branch 'master' into cairocanvas
[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 name: [%6] key %2 type %3 state %4 magic %5\n",
237                         widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
238                         gtk_widget_get_name (widget)
239                         )
240                 );
241
242         if (event->keyval == GDK_Shift_R) {
243                 keyval = GDK_Shift_L;
244
245         } else if (event->keyval == GDK_Control_R) {
246                 keyval = GDK_Control_L;
247
248         } else {
249                 keyval = event->keyval;
250         }
251
252         if (event->state & ScrollZoomVerticalModifier) {
253                 /* There is a special and rather hacky situation in Editor which makes
254                    it useful to know when the modifier key for vertical zoom has been
255                    released, so emit a signal here (see Editor::_stepping_axis_view).
256                    Note that the state bit for the modifier key is set for the key-up
257                    event when the modifier is released, but not the key-down when it
258                    is pressed, so we get here on key-up, which is what we want.
259                 */
260                 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
261         }
262
263         if (event->type == GDK_KEY_PRESS) {
264
265                 if (find (state.begin(), state.end(), keyval) == state.end()) {
266                         state.push_back (keyval);
267                         sort (state.begin(), state.end());
268
269                 } else {
270
271                         /* key is already down. if its also used for release,
272                            prevent auto-repeat events.
273                         */
274
275                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
276
277                                 const AccelKey& ak (k->first);
278
279                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
280                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
281                                         ret = true;
282                                         break;
283                                 }
284                         }
285                 }
286
287         } else if (event->type == GDK_KEY_RELEASE) {
288
289                 State::iterator i;
290
291                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
292                         state.erase (i);
293                         sort (state.begin(), state.end());
294                 }
295
296                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
297
298                         const AccelKey& ak (k->first);
299                         two_strings ts (k->second);
300
301                         if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
302                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
303                                 if (act) {
304                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
305                                         act->activate();
306                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
307                                         ret = true;
308                                 }
309                                 break;
310                         }
311                 }
312         }
313
314         /* Special keys that we want to handle in
315            any dialog, no matter whether it uses
316            the regular set of accelerators or not
317         */
318
319         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
320                 switch (event->keyval) {
321                 case GDK_w:
322                         close_current_dialog ();
323                         ret = true;
324                         break;
325                 }
326         }
327
328         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
329
330         return ret;
331 }
332
333 void
334 Keyboard::close_current_dialog ()
335 {
336         if (current_window) {
337                 current_window->hide ();
338                 current_window = 0;
339         }
340 }
341
342 bool
343 Keyboard::key_is_down (uint32_t keyval)
344 {
345         return find (state.begin(), state.end(), keyval) != state.end();
346 }
347
348 bool
349 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
350 {
351         current_window = win;
352         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
353         return false;
354 }
355
356 bool
357 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
358 {
359         if (ev) {
360                 switch (ev->detail) {
361                 case GDK_NOTIFY_INFERIOR:
362                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
363                         break;
364
365                 case GDK_NOTIFY_VIRTUAL:
366                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
367                         /* fallthru */
368
369                 default:
370                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
371                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
372                         state.clear ();
373                         current_window = 0;
374                 }
375         } else {
376                 current_window = 0;
377         }
378
379         return false;
380 }
381
382 void
383 Keyboard::set_edit_button (guint but)
384 {
385         edit_but = but;
386 }
387
388 void
389 Keyboard::set_edit_modifier (guint mod)
390 {
391         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
392         edit_mod = mod;
393         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
394 }
395
396 void
397 Keyboard::set_delete_button (guint but)
398 {
399         delete_but = but;
400 }
401
402 void
403 Keyboard::set_delete_modifier (guint mod)
404 {
405         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
406         delete_mod = mod;
407         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
408 }
409
410 void
411 Keyboard::set_insert_note_button (guint but)
412 {
413         insert_note_but = but;
414 }
415
416 void
417 Keyboard::set_insert_note_modifier (guint mod)
418 {
419         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
420         insert_note_mod = mod;
421         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
422 }
423
424
425 void
426 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
427 {
428         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
429         var = newval;
430         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
431 }
432
433 void
434 Keyboard::set_snap_modifier (guint mod)
435 {
436         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
437         snap_mod = mod;
438         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
439 }
440
441 bool
442 Keyboard::is_edit_event (GdkEventButton *ev)
443 {
444         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
445                 (ev->button == Keyboard::edit_button()) &&
446                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
447 }
448
449 bool
450 Keyboard::is_insert_note_event (GdkEventButton *ev)
451 {
452         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
453                 (ev->button == Keyboard::insert_note_button()) &&
454                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
455 }
456
457 bool
458 Keyboard::is_button2_event (GdkEventButton* ev)
459 {
460 #ifdef GTKOSX
461         return (ev->button == 2) ||
462                 ((ev->button == 1) &&
463                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
464 #else
465         return ev->button == 2;
466 #endif
467 }
468
469 bool
470 Keyboard::is_delete_event (GdkEventButton *ev)
471 {
472         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
473                 (ev->button == Keyboard::delete_button()) &&
474                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
475 }
476
477 bool
478 Keyboard::is_context_menu_event (GdkEventButton *ev)
479 {
480         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
481                 (ev->button == 3) &&
482                 ((ev->state & RelevantModifierKeyMask) == 0);
483 }
484
485 bool
486 Keyboard::no_modifiers_active (guint state)
487 {
488         return (state & RelevantModifierKeyMask) == 0;
489 }
490
491 bool
492 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
493 {
494         return (state & mask) == (guint) mask;
495 }
496
497 bool
498 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
499 {
500         return (state & RelevantModifierKeyMask) == (guint) mask;
501 }
502
503 void
504 Keyboard::keybindings_changed ()
505 {
506         if (Keyboard::can_save_keybindings) {
507                 Keyboard::bindings_changed_after_save_became_legal = true;
508         }
509
510         Keyboard::save_keybindings ();
511 }
512
513 void
514 Keyboard::set_can_save_keybindings (bool yn)
515 {
516         can_save_keybindings = yn;
517 }
518
519 void
520 Keyboard::save_keybindings ()
521 {
522         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
523                 Gtk::AccelMap::save (user_keybindings_path);
524         }
525 }
526
527 bool
528 Keyboard::load_keybindings (string path)
529 {
530         try {
531                 info << "Loading bindings from " << path << endl;
532
533                 Gtk::AccelMap::load (path);
534
535                 _current_binding_name = _("Unknown");
536
537                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
538                         if (path == x->second) {
539                                 _current_binding_name = x->first;
540                                 break;
541                         }
542                 }
543
544
545         } catch (...) {
546                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
547                       << endmsg;
548                 return false;
549         }
550
551         /* now find all release-driven bindings */
552
553         vector<string> groups;
554         vector<string> names;
555         vector<string> tooltips;
556         vector<AccelKey> bindings;
557
558         ActionManager::get_all_actions (groups, names, tooltips, bindings);
559
560         vector<string>::iterator g;
561         vector<AccelKey>::iterator b;
562         vector<string>::iterator n;
563
564         release_keys.clear ();
565
566         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
567                 stringstream s;
568                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
569
570                 if ((*b).get_key() != GDK_VoidSymbol) {
571                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
572                 } else {
573                         s << "unbound\n";
574                 }
575
576                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
577         }
578
579         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
580                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
581                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
582                 }
583         }
584
585         return true;
586 }
587