replace final(?) incidences of program name (ardour, Ardour) with PROGRAM_NAME (thank...
[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 Keyboard*    Keyboard::_the_keyboard = 0;
98 Gtk::Window* Keyboard::current_window = 0;
99 bool         Keyboard::_some_magic_widget_has_focus = false;
100
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;
107
108 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
109
110 GdkModifierType Keyboard::RelevantModifierKeyMask;
111
112 void
113 Keyboard::magic_widget_grab_focus ()
114 {
115         _some_magic_widget_has_focus = true;
116 }
117
118 void
119 Keyboard::magic_widget_drop_focus ()
120 {
121         _some_magic_widget_has_focus = false;
122 }
123
124 bool
125 Keyboard::some_magic_widget_has_focus ()
126 {
127         return _some_magic_widget_has_focus;
128 }
129
130 Keyboard::Keyboard ()
131 {
132         if (_the_keyboard == 0) {
133                 _the_keyboard = this;
134                 _current_binding_name = _("Unknown");
135         }
136
137         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
138
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);
145
146         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
147
148         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
149 }
150
151 Keyboard::~Keyboard ()
152 {
153         gtk_key_snooper_remove (snooper_id);
154 }
155
156 XMLNode&
157 Keyboard::get_state (void)
158 {
159         XMLNode* node = new XMLNode ("Keyboard");
160         char buf[32];
161
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);
176
177         return *node;
178 }
179
180 int
181 Keyboard::set_state (const XMLNode& node, int /*version*/)
182 {
183         const XMLProperty* prop;
184
185         if ((prop = node.property ("edit-button")) != 0) {
186                 sscanf (prop->value().c_str(), "%d", &edit_but);
187         }
188
189         if ((prop = node.property ("edit-modifier")) != 0) {
190                 sscanf (prop->value().c_str(), "%d", &edit_mod);
191         }
192
193         if ((prop = node.property ("delete-button")) != 0) {
194                 sscanf (prop->value().c_str(), "%d", &delete_but);
195         }
196
197         if ((prop = node.property ("delete-modifier")) != 0) {
198                 sscanf (prop->value().c_str(), "%d", &delete_mod);
199         }
200
201         if ((prop = node.property ("snap-modifier")) != 0) {
202                 sscanf (prop->value().c_str(), "%d", &snap_mod);
203         }
204
205         if ((prop = node.property ("insert-note-button")) != 0) {
206                 sscanf (prop->value().c_str(), "%d", &insert_note_but);
207         }
208
209         if ((prop = node.property ("insert-note-modifier")) != 0) {
210                 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
211         }
212
213         return 0;
214 }
215
216 gint
217 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
218 {
219         return ((Keyboard *) data)->snooper (widget, event);
220 }
221
222 gint
223 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
224 {
225         uint32_t keyval;
226         bool ret = false;
227
228         DEBUG_TRACE (
229                 DEBUG::Keyboard,
230                 string_compose (
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
233                         )
234                 );
235
236         if (event->keyval == GDK_Shift_R) {
237                 keyval = GDK_Shift_L;
238
239         } else if (event->keyval == GDK_Control_R) {
240                 keyval = GDK_Control_L;
241
242         } else {
243                 keyval = event->keyval;
244         }
245
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)
250                 */
251                 ShiftReleased (); /* EMIT SIGNAL */
252         }
253
254         if (event->type == GDK_KEY_PRESS) {
255
256                 if (find (state.begin(), state.end(), keyval) == state.end()) {
257                         state.push_back (keyval);
258                         sort (state.begin(), state.end());
259
260                 } else {
261
262                         /* key is already down. if its also used for release,
263                            prevent auto-repeat events.
264                         */
265
266                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
267
268                                 const AccelKey& ak (k->first);
269
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");
272                                         ret = true;
273                                         break;
274                                 }
275                         }
276                 }
277
278         } else if (event->type == GDK_KEY_RELEASE) {
279
280                 State::iterator i;
281
282                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
283                         state.erase (i);
284                         sort (state.begin(), state.end());
285                 }
286
287                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
288
289                         const AccelKey& ak (k->first);
290                         two_strings ts (k->second);
291
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());
294                                 if (act) {
295                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
296                                         act->activate();
297                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
298                                         ret = true;
299                                 }
300                                 break;
301                         }
302                 }
303         }
304
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
308         */
309
310         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
311                 switch (event->keyval) {
312                 case GDK_w:
313                         close_current_dialog ();
314                         ret = true;
315                         break;
316                 }
317         }
318
319         return ret;
320 }
321
322 void
323 Keyboard::close_current_dialog ()
324 {
325         if (current_window) {
326                 current_window->hide ();
327                 current_window = 0;
328         }
329 }
330
331 bool
332 Keyboard::key_is_down (uint32_t keyval)
333 {
334         return find (state.begin(), state.end(), keyval) != state.end();
335 }
336
337 bool
338 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
339 {
340         current_window = win;
341         return false;
342 }
343
344 bool
345 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
346 {
347         if (ev) {
348                 switch (ev->detail) {
349                 case GDK_NOTIFY_INFERIOR:
350                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
351                         break;
352
353                 case GDK_NOTIFY_VIRTUAL:
354                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
355                         /* fallthru */
356
357                 default:
358                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
359                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
360                         state.clear ();
361                         current_window = 0;
362                 }
363         } else {
364                 current_window = 0;
365         }
366
367         return false;
368 }
369
370 void
371 Keyboard::set_edit_button (guint but)
372 {
373         edit_but = but;
374 }
375
376 void
377 Keyboard::set_edit_modifier (guint mod)
378 {
379         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
380         edit_mod = mod;
381         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
382 }
383
384 void
385 Keyboard::set_delete_button (guint but)
386 {
387         delete_but = but;
388 }
389
390 void
391 Keyboard::set_delete_modifier (guint mod)
392 {
393         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
394         delete_mod = mod;
395         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
396 }
397
398 void
399 Keyboard::set_insert_note_button (guint but)
400 {
401         insert_note_but = but;
402 }
403
404 void
405 Keyboard::set_insert_note_modifier (guint mod)
406 {
407         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
408         insert_note_mod = mod;
409         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
410 }
411
412
413 void
414 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
415 {
416         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
417         var = newval;
418         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
419 }
420
421 void
422 Keyboard::set_snap_modifier (guint mod)
423 {
424         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
425         snap_mod = mod;
426         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
427 }
428
429 bool
430 Keyboard::is_edit_event (GdkEventButton *ev)
431 {
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());
435 }
436
437 bool
438 Keyboard::is_insert_note_event (GdkEventButton *ev)
439 {
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());
443 }
444
445 bool
446 Keyboard::is_button2_event (GdkEventButton* ev)
447 {
448 #ifdef GTKOSX
449         return (ev->button == 2) ||
450                 ((ev->button == 1) &&
451                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
452 #else
453         return ev->button == 2;
454 #endif
455 }
456
457 bool
458 Keyboard::is_delete_event (GdkEventButton *ev)
459 {
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());
463 }
464
465 bool
466 Keyboard::is_context_menu_event (GdkEventButton *ev)
467 {
468         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
469                 (ev->button == 3) &&
470                 ((ev->state & RelevantModifierKeyMask) == 0);
471 }
472
473 bool
474 Keyboard::no_modifiers_active (guint state)
475 {
476         return (state & RelevantModifierKeyMask) == 0;
477 }
478
479 bool
480 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
481 {
482         return (state & mask) == (guint) mask;
483 }
484
485 bool
486 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
487 {
488         return (state & RelevantModifierKeyMask) == (guint) mask;
489 }
490
491 void
492 Keyboard::keybindings_changed ()
493 {
494         if (Keyboard::can_save_keybindings) {
495                 Keyboard::bindings_changed_after_save_became_legal = true;
496         }
497
498         Keyboard::save_keybindings ();
499 }
500
501 void
502 Keyboard::set_can_save_keybindings (bool yn)
503 {
504         can_save_keybindings = yn;
505 }
506
507 void
508 Keyboard::save_keybindings ()
509 {
510         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
511                 Gtk::AccelMap::save (user_keybindings_path);
512         }
513 }
514
515 bool
516 Keyboard::load_keybindings (string path)
517 {
518         try {
519                 info << "Loading bindings from " << path << endl;
520
521                 Gtk::AccelMap::load (path);
522
523                 _current_binding_name = _("Unknown");
524
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;
528                                 break;
529                         }
530                 }
531
532
533         } catch (...) {
534                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
535                       << endmsg;
536                 return false;
537         }
538
539         /* now find all release-driven bindings */
540
541         vector<string> groups;
542         vector<string> names;
543         vector<string> tooltips;
544         vector<AccelKey> bindings;
545
546         ActionManager::get_all_actions (groups, names, tooltips, bindings);
547
548         vector<string>::iterator g;
549         vector<AccelKey>::iterator b;
550         vector<string>::iterator n;
551
552         release_keys.clear ();
553
554         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
555                 stringstream s;
556                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
557
558                 if ((*b).get_key() != GDK_VoidSymbol) {
559                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
560                 } else {
561                         s << "unbound\n";
562                 }
563
564                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
565         }
566
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)));
570                 }
571         }
572
573         return true;
574 }
575