Allow all drag -related modifiers to be set, re-instate "jump after trim"
[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 <cerrno>
27 #include <ctype.h>
28
29 #include <glib/gstdio.h>
30
31 #include <gtkmm/widget.h>
32 #include <gtkmm/window.h>
33 #include <gtkmm/accelmap.h>
34 #include <gdk/gdkkeysyms.h>
35
36 #include "pbd/error.h"
37 #include "pbd/convert.h"
38 #include "pbd/file_utils.h"
39 #include "pbd/search_path.h"
40 #include "pbd/xml++.h"
41 #include "pbd/debug.h"
42 #include "pbd/unwind.h"
43
44 #include "gtkmm2ext/keyboard.h"
45 #include "gtkmm2ext/actions.h"
46 #include "gtkmm2ext/debug.h"
47
48 #include "i18n.h"
49
50 using namespace PBD;
51 using namespace Gtk;
52 using namespace Gtkmm2ext;
53 using namespace std;
54
55 guint Keyboard::edit_but = 3;
56 guint Keyboard::edit_mod = GDK_CONTROL_MASK;
57 guint Keyboard::delete_but = 3;
58 guint Keyboard::delete_mod = GDK_SHIFT_MASK;
59 guint Keyboard::insert_note_but = 1;
60 guint Keyboard::insert_note_mod = GDK_CONTROL_MASK;
61 guint Keyboard::snap_mod = GDK_MOD3_MASK;
62 guint Keyboard::snap_delta_mod = 0;
63
64 #ifdef GTKOSX
65
66 uint Keyboard::PrimaryModifier = GDK_MOD2_MASK;   // Command
67 guint Keyboard::SecondaryModifier = GDK_CONTROL_MASK; // Control
68 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK; // Shift
69 guint Keyboard::Level4Modifier = GDK_MOD1_MASK; // Alt/Option
70 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;      // Control
71 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
72 guint Keyboard::button2_modifiers = Keyboard::SecondaryModifier|Keyboard::Level4Modifier;
73
74 const char* Keyboard::primary_modifier_name() { return _("Command"); }
75 const char* Keyboard::secondary_modifier_name() { return _("Control"); }
76 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
77 const char* Keyboard::level4_modifier_name() { return _("Option"); }
78 const char* Keyboard::copy_modifier_name() { return _("Control"); }
79 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
80
81 #else
82
83 guint Keyboard::PrimaryModifier = GDK_CONTROL_MASK; // Control
84 guint Keyboard::SecondaryModifier = GDK_MOD1_MASK;  // Alt/Option
85 guint Keyboard::TertiaryModifier = GDK_SHIFT_MASK;  // Shift
86 guint Keyboard::Level4Modifier = GDK_MOD4_MASK;     // Mod4/Windows
87 guint Keyboard::CopyModifier = GDK_CONTROL_MASK;
88 guint Keyboard::RangeSelectModifier = GDK_SHIFT_MASK;
89 guint Keyboard::button2_modifiers = 0; /* not used */
90
91 const char* Keyboard::primary_modifier_name() { return _("Control"); }
92 const char* Keyboard::secondary_modifier_name() { return _("Alt"); }
93 const char* Keyboard::tertiary_modifier_name() { return S_("Key|Shift"); }
94 const char* Keyboard::level4_modifier_name() { return _("Meta"); }
95 const char* Keyboard::copy_modifier_name() { return _("Control"); }
96 const char* Keyboard::rangeselect_modifier_name() { return S_("Key|Shift"); }
97
98 #endif
99
100 guint Keyboard::GainFineScaleModifier = Keyboard::PrimaryModifier;
101 guint Keyboard::GainExtraFineScaleModifier = Keyboard::SecondaryModifier;
102
103 guint Keyboard::ScrollZoomVerticalModifier = Keyboard::SecondaryModifier;
104 guint Keyboard::ScrollZoomHorizontalModifier = Keyboard::PrimaryModifier;
105 guint Keyboard::ScrollHorizontalModifier = Keyboard::TertiaryModifier;
106
107 guint Keyboard::trim_contents_mod = Keyboard::PrimaryModifier;
108 guint Keyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
109 guint Keyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
110 guint Keyboard::trim_jump_mod = Keyboard::TertiaryModifier;
111 guint Keyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
112 guint Keyboard::push_points_mod = Keyboard::PrimaryModifier;
113 guint Keyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
114
115 Keyboard*    Keyboard::_the_keyboard = 0;
116 Gtk::Window* Keyboard::current_window = 0;
117 bool         Keyboard::_some_magic_widget_has_focus = false;
118
119 std::string Keyboard::user_keybindings_path;
120 bool Keyboard::can_save_keybindings = false;
121 bool Keyboard::bindings_changed_after_save_became_legal = false;
122 map<string,string> Keyboard::binding_files;
123 string Keyboard::_current_binding_name;
124 map<AccelKey,pair<string,string>,Keyboard::AccelKeyLess> Keyboard::release_keys;
125
126 /* set this to initially contain the modifiers we care about, then track changes in ::set_edit_modifier() etc. */
127
128 GdkModifierType Keyboard::RelevantModifierKeyMask;
129
130 void
131 Keyboard::magic_widget_grab_focus ()
132 {
133         _some_magic_widget_has_focus = true;
134 }
135
136 void
137 Keyboard::magic_widget_drop_focus ()
138 {
139         _some_magic_widget_has_focus = false;
140 }
141
142 bool
143 Keyboard::some_magic_widget_has_focus ()
144 {
145         return _some_magic_widget_has_focus;
146 }
147
148 Keyboard::Keyboard ()
149 {
150         if (_the_keyboard == 0) {
151                 _the_keyboard = this;
152                 _current_binding_name = _("Unknown");
153         }
154
155         RelevantModifierKeyMask = (GdkModifierType) gtk_accelerator_get_default_mod_mask ();
156
157         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | PrimaryModifier);
158         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | SecondaryModifier);
159         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | TertiaryModifier);
160         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | Level4Modifier);
161         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | CopyModifier);
162         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | RangeSelectModifier);
163
164         gtk_accelerator_set_default_mod_mask (RelevantModifierKeyMask);
165
166         snooper_id = gtk_key_snooper_install (_snooper, (gpointer) this);
167 }
168
169 Keyboard::~Keyboard ()
170 {
171         gtk_key_snooper_remove (snooper_id);
172 }
173
174 XMLNode&
175 Keyboard::get_state (void)
176 {
177         XMLNode* node = new XMLNode ("Keyboard");
178         char buf[32];
179
180         snprintf (buf, sizeof (buf), "%d", edit_but);
181         node->add_property ("edit-button", buf);
182         snprintf (buf, sizeof (buf), "%d", edit_mod);
183         node->add_property ("edit-modifier", buf);
184         snprintf (buf, sizeof (buf), "%d", delete_but);
185         node->add_property ("delete-button", buf);
186         snprintf (buf, sizeof (buf), "%d", delete_mod);
187         node->add_property ("delete-modifier", buf);
188         snprintf (buf, sizeof (buf), "%d", snap_mod);
189         node->add_property ("snap-modifier", buf);
190         snprintf (buf, sizeof (buf), "%d", snap_delta_mod);
191         node->add_property ("snap-delta-modifier", buf);
192         snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
193         node->add_property ("trim-contents-modifier", buf);
194         snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
195         node->add_property ("trim-overlap-modifier", buf);
196         snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
197         node->add_property ("trim-anchored-modifier", buf);
198         snprintf (buf, sizeof (buf), "%d", trim_jump_mod);
199         node->add_property ("trim-jump-modifier", buf);
200         snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
201         node->add_property ("fine-adjust-modifier", buf);
202         snprintf (buf, sizeof (buf), "%d", push_points_mod);
203         node->add_property ("push-points-modifier", buf);
204         snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
205         node->add_property ("note-size-relative-modifier", buf);
206         snprintf (buf, sizeof (buf), "%d", insert_note_but);
207         node->add_property ("insert-note-button", buf);
208         snprintf (buf, sizeof (buf), "%d", insert_note_mod);
209         node->add_property ("insert-note-modifier", buf);
210
211         return *node;
212 }
213
214 int
215 Keyboard::set_state (const XMLNode& node, int /*version*/)
216 {
217         const XMLProperty* prop;
218
219         if ((prop = node.property ("edit-button")) != 0) {
220                 sscanf (prop->value().c_str(), "%d", &edit_but);
221         }
222
223         if ((prop = node.property ("edit-modifier")) != 0) {
224                 sscanf (prop->value().c_str(), "%d", &edit_mod);
225         }
226
227         if ((prop = node.property ("delete-button")) != 0) {
228                 sscanf (prop->value().c_str(), "%d", &delete_but);
229         }
230
231         if ((prop = node.property ("delete-modifier")) != 0) {
232                 sscanf (prop->value().c_str(), "%d", &delete_mod);
233         }
234
235         if ((prop = node.property ("snap-modifier")) != 0) {
236                 sscanf (prop->value().c_str(), "%d", &snap_mod);
237         }
238
239         if ((prop = node.property ("snap-delta-modifier")) != 0) {
240                 sscanf (prop->value().c_str(), "%d", &snap_delta_mod);
241         }
242
243         if ((prop = node.property ("trim-contents-modifier")) != 0) {
244                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
245         }
246
247         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
248                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
249         }
250
251         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
252                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
253         }
254
255         if ((prop = node.property ("trim-jump-modifier")) != 0) {
256                 sscanf (prop->value().c_str(), "%d", &trim_jump_mod);
257         }
258
259         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
260                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
261         }
262
263         if ((prop = node.property ("push-points-modifier")) != 0) {
264                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
265         }
266
267         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
268                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
269         }
270
271         if ((prop = node.property ("insert-note-button")) != 0) {
272                 sscanf (prop->value().c_str(), "%d", &insert_note_but);
273         }
274
275         if ((prop = node.property ("insert-note-modifier")) != 0) {
276                 sscanf (prop->value().c_str(), "%d", &insert_note_mod);
277         }
278
279         return 0;
280 }
281
282 gint
283 Keyboard::_snooper (GtkWidget *widget, GdkEventKey *event, gpointer data)
284 {
285         return ((Keyboard *) data)->snooper (widget, event);
286 }
287
288 gint
289 Keyboard::snooper (GtkWidget *widget, GdkEventKey *event)
290 {
291         uint32_t keyval;
292         bool ret = false;
293
294         DEBUG_TRACE (
295                 DEBUG::Keyboard,
296                 string_compose (
297                         "Snoop widget %1 name: [%6] key %2 type %3 state %4 magic %5\n",
298                         widget, event->keyval, event->type, event->state, _some_magic_widget_has_focus,
299                         gtk_widget_get_name (widget)
300                         )
301                 );
302
303         if (event->keyval == GDK_Shift_R) {
304                 keyval = GDK_Shift_L;
305
306         } else if (event->keyval == GDK_Control_R) {
307                 keyval = GDK_Control_L;
308
309         } else {
310                 keyval = event->keyval;
311         }
312
313         if (event->state & ScrollZoomVerticalModifier) {
314                 /* There is a special and rather hacky situation in Editor which makes
315                    it useful to know when the modifier key for vertical zoom has been
316                    released, so emit a signal here (see Editor::_stepping_axis_view).
317                    Note that the state bit for the modifier key is set for the key-up
318                    event when the modifier is released, but not the key-down when it
319                    is pressed, so we get here on key-up, which is what we want.
320                 */
321                 ZoomVerticalModifierReleased (); /* EMIT SIGNAL */
322         }
323
324         if (event->type == GDK_KEY_PRESS) {
325
326                 if (find (state.begin(), state.end(), keyval) == state.end()) {
327                         state.push_back (keyval);
328                         sort (state.begin(), state.end());
329
330                 } else {
331
332                         /* key is already down. if its also used for release,
333                            prevent auto-repeat events.
334                         */
335
336                         for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
337
338                                 const AccelKey& ak (k->first);
339
340                                 if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
341                                         DEBUG_TRACE (DEBUG::Keyboard, "Suppress auto repeat\n");
342                                         ret = true;
343                                         break;
344                                 }
345                         }
346                 }
347
348         } else if (event->type == GDK_KEY_RELEASE) {
349
350                 State::iterator i;
351
352                 if ((i = find (state.begin(), state.end(), keyval)) != state.end()) {
353                         state.erase (i);
354                         sort (state.begin(), state.end());
355                 }
356
357                 for (map<AccelKey,two_strings,AccelKeyLess>::iterator k = release_keys.begin(); k != release_keys.end(); ++k) {
358
359                         const AccelKey& ak (k->first);
360                         two_strings ts (k->second);
361
362                         if (keyval == ak.get_key() && (Gdk::ModifierType)((event->state & Keyboard::RelevantModifierKeyMask) | Gdk::RELEASE_MASK) == ak.get_mod()) {
363                                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (ts.first.c_str(), ts.second.c_str());
364                                 if (act) {
365                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Activate %1 %2\n", ts.first, ts.second));
366                                         act->activate();
367                                         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Use repeat, suppress other\n", ts.first, ts.second));
368                                         ret = true;
369                                 }
370                                 break;
371                         }
372                 }
373         }
374
375         /* Special keys that we want to handle in
376            any dialog, no matter whether it uses
377            the regular set of accelerators or not
378         */
379
380         if (event->type == GDK_KEY_RELEASE && modifier_state_equals (event->state, PrimaryModifier)) {
381                 switch (event->keyval) {
382                 case GDK_w:
383                         close_current_dialog ();
384                         ret = true;
385                         break;
386                 }
387         }
388
389         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("snooper returns %1\n", ret));
390
391         return ret;
392 }
393
394 void
395 Keyboard::close_current_dialog ()
396 {
397         if (current_window) {
398                 current_window->hide ();
399                 current_window = 0;
400         }
401 }
402
403 bool
404 Keyboard::key_is_down (uint32_t keyval)
405 {
406         return find (state.begin(), state.end(), keyval) != state.end();
407 }
408
409 bool
410 Keyboard::enter_window (GdkEventCrossing *, Gtk::Window* win)
411 {
412         current_window = win;
413         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Entering window, title = %1\n", win->get_title()));
414         return false;
415 }
416
417 bool
418 Keyboard::leave_window (GdkEventCrossing *ev, Gtk::Window* /*win*/)
419 {
420         if (ev) {
421                 switch (ev->detail) {
422                 case GDK_NOTIFY_INFERIOR:
423                         DEBUG_TRACE (DEBUG::Keyboard, "INFERIOR crossing ... out\n");
424                         break;
425
426                 case GDK_NOTIFY_VIRTUAL:
427                         DEBUG_TRACE (DEBUG::Keyboard, "VIRTUAL crossing ... out\n");
428                         /* fallthru */
429
430                 default:
431                         DEBUG_TRACE (DEBUG::Keyboard, "REAL crossing ... out\n");
432                         DEBUG_TRACE (DEBUG::Keyboard, "Clearing current target\n");
433                         state.clear ();
434                         current_window = 0;
435                 }
436         } else {
437                 current_window = 0;
438         }
439
440         return false;
441 }
442
443 bool
444 Keyboard::focus_in_window (GdkEventFocus *, Gtk::Window* win)
445 {
446         current_window = win;
447         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Focusing in window, title = %1\n", win->get_title()));
448         return false;
449 }
450
451 bool
452 Keyboard::focus_out_window (GdkEventFocus * ev, Gtk::Window* win)
453 {
454         if (ev) {
455                 state.clear ();
456                 current_window = 0;
457         }  else {
458                 current_window = 0;
459         }
460
461         DEBUG_TRACE (DEBUG::Keyboard, string_compose ("Foucusing out window, title = %1\n", win->get_title()));
462
463         return false;
464 }
465
466 void
467 Keyboard::set_edit_button (guint but)
468 {
469         edit_but = but;
470 }
471
472 void
473 Keyboard::set_edit_modifier (guint mod)
474 {
475         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~edit_mod);
476         edit_mod = mod;
477         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | edit_mod);
478 }
479
480 void
481 Keyboard::set_delete_button (guint but)
482 {
483         delete_but = but;
484 }
485
486 void
487 Keyboard::set_delete_modifier (guint mod)
488 {
489         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~delete_mod);
490         delete_mod = mod;
491         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | delete_mod);
492 }
493
494 void
495 Keyboard::set_insert_note_button (guint but)
496 {
497         insert_note_but = but;
498 }
499
500 void
501 Keyboard::set_insert_note_modifier (guint mod)
502 {
503         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~insert_note_mod);
504         insert_note_mod = mod;
505         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | insert_note_mod);
506 }
507
508
509 void
510 Keyboard::set_modifier (uint32_t newval, uint32_t& var)
511 {
512         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~var);
513         var = newval;
514         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | var);
515 }
516
517 void
518 Keyboard::set_snap_modifier (guint mod)
519 {
520         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_mod);
521         snap_mod = mod;
522         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_mod);
523 }
524
525 void
526 Keyboard::set_snap_delta_modifier (guint mod)
527 {
528         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~snap_delta_mod);
529         snap_delta_mod = mod;
530         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | snap_delta_mod);
531 }
532
533 void
534 Keyboard::set_trim_contents_modifier (guint mod)
535 {
536         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
537         trim_contents_mod = mod;
538         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
539 }
540
541 void
542 Keyboard::set_trim_overlap_modifier (guint mod)
543 {
544         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
545         trim_overlap_mod = mod;
546         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
547 }
548
549 void
550 Keyboard::set_trim_anchored_modifier (guint mod)
551 {
552         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
553         trim_anchored_mod = mod;
554         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
555 }
556
557 void
558 Keyboard::set_trim_jump_modifier (guint mod)
559 {
560         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_jump_mod);
561         trim_jump_mod = mod;
562         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_jump_mod);
563 }
564
565 void
566 Keyboard::set_fine_adjust_modifier (guint mod)
567 {
568         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
569         fine_adjust_mod = mod;
570         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
571 }
572
573 void
574 Keyboard::set_push_points_modifier (guint mod)
575 {
576         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
577         push_points_mod = mod;
578         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
579 }
580
581 void
582 Keyboard::set_note_size_relative_modifier (guint mod)
583 {
584         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
585         note_size_relative_mod = mod;
586         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
587 }
588
589 bool
590 Keyboard::is_edit_event (GdkEventButton *ev)
591 {
592         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
593                 (ev->button == Keyboard::edit_button()) &&
594                 ((ev->state & RelevantModifierKeyMask) == Keyboard::edit_modifier());
595 }
596
597 bool
598 Keyboard::is_insert_note_event (GdkEventButton *ev)
599 {
600         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
601                 (ev->button == Keyboard::insert_note_button()) &&
602                 ((ev->state & RelevantModifierKeyMask) == Keyboard::insert_note_modifier());
603 }
604
605 bool
606 Keyboard::is_button2_event (GdkEventButton* ev)
607 {
608 #ifdef GTKOSX
609         return (ev->button == 2) ||
610                 ((ev->button == 1) &&
611                  ((ev->state & Keyboard::button2_modifiers) == Keyboard::button2_modifiers));
612 #else
613         return ev->button == 2;
614 #endif
615 }
616
617 bool
618 Keyboard::is_delete_event (GdkEventButton *ev)
619 {
620         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
621                 (ev->button == Keyboard::delete_button()) &&
622                 ((ev->state & RelevantModifierKeyMask) == Keyboard::delete_modifier());
623 }
624
625 bool
626 Keyboard::is_context_menu_event (GdkEventButton *ev)
627 {
628         return (ev->type == GDK_BUTTON_PRESS || ev->type == GDK_BUTTON_RELEASE) &&
629                 (ev->button == 3) &&
630                 ((ev->state & RelevantModifierKeyMask) == 0);
631 }
632
633 bool
634 Keyboard::no_modifiers_active (guint state)
635 {
636         return (state & RelevantModifierKeyMask) == 0;
637 }
638
639 bool
640 Keyboard::modifier_state_contains (guint state, ModifierMask mask)
641 {
642         return (state & mask) == (guint) mask;
643 }
644
645 bool
646 Keyboard::modifier_state_equals (guint state, ModifierMask mask)
647 {
648         return (state & RelevantModifierKeyMask) == (guint) mask;
649 }
650
651 void
652 Keyboard::keybindings_changed ()
653 {
654         if (Keyboard::can_save_keybindings) {
655                 Keyboard::bindings_changed_after_save_became_legal = true;
656         }
657
658         Keyboard::save_keybindings ();
659 }
660
661 void
662 Keyboard::set_can_save_keybindings (bool yn)
663 {
664         can_save_keybindings = yn;
665 }
666
667 void
668 Keyboard::save_keybindings ()
669 {
670         if (can_save_keybindings && bindings_changed_after_save_became_legal) {
671                 Gtk::AccelMap::save (user_keybindings_path);
672         }
673 }
674
675 bool
676 Keyboard::load_keybindings (string path)
677 {
678         try {
679                 info << "Loading bindings from " << path << endl;
680
681                 Gtk::AccelMap::load (path);
682
683                 _current_binding_name = _("Unknown");
684
685                 for (map<string,string>::iterator x = binding_files.begin(); x != binding_files.end(); ++x) {
686                         if (path == x->second) {
687                                 _current_binding_name = x->first;
688                                 break;
689                         }
690                 }
691
692
693         } catch (...) {
694                 error << string_compose (_("key bindings file not found at \"%2\" or contains errors."), path)
695                       << endmsg;
696                 return false;
697         }
698
699         /* now find all release-driven bindings */
700
701         vector<string> groups;
702         vector<string> names;
703         vector<string> tooltips;
704         vector<AccelKey> bindings;
705
706         ActionManager::get_all_actions (groups, names, tooltips, bindings);
707
708         vector<string>::iterator g;
709         vector<AccelKey>::iterator b;
710         vector<string>::iterator n;
711
712         release_keys.clear ();
713
714         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
715                 stringstream s;
716                 s << "Action: " << *n << " Group: " << *g << " Binding: ";
717
718                 if ((*b).get_key() != GDK_VoidSymbol) {
719                         s << b->get_key() << " w/mod " << hex << b->get_mod() << dec << " = " << b->get_abbrev () << "\n";
720                 } else {
721                         s << "unbound\n";
722                 }
723
724                 DEBUG_TRACE (DEBUG::Bindings, s.str ());
725         }
726
727         for (n = names.begin(), b = bindings.begin(), g = groups.begin(); n != names.end(); ++n, ++b, ++g) {
728                 if ((*b).get_mod() & Gdk::RELEASE_MASK) {
729                         release_keys.insert (pair<AccelKey,two_strings> (*b, two_strings (*g, *n)));
730                 }
731         }
732
733         return true;
734 }
735
736 int
737 Keyboard::reset_bindings ()
738 {
739         if (Glib::file_test (user_keybindings_path,  Glib::FILE_TEST_EXISTS)) {
740
741                 string new_path = user_keybindings_path;
742                 new_path += ".old";
743                 
744                 if (::g_rename (user_keybindings_path.c_str(), new_path.c_str())) {
745                         error << string_compose (_("Cannot rename your own keybinding file (%1)"), strerror (errno)) << endmsg;
746                         return -1;
747                 } 
748         }
749
750         {
751                 PBD::Unwinder<bool> uw (can_save_keybindings, false);
752                 setup_keybindings ();
753         }
754
755         return 0;
756 }