Ensure RelevantModifierKeyMask is updated on each modifier change.
[ardour.git] / gtk2_ardour / 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 "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/file_utils.h"
23 #include "pbd/basename.h"
24
25 #include "ardour/filesystem_paths.h"
26
27 #include "ardour_ui.h"
28 #include "public_editor.h"
29 #include "keyboard.h"
30 #include "opts.h"
31 #include "ui_config.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace Gtk;
37 using namespace PBD;
38 using namespace ARDOUR;
39 using Gtkmm2ext::Keyboard;
40
41 #ifdef __APPLE__
42 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
43 #else
44 guint ArdourKeyboard::constraint_mod = Keyboard::SecondaryModifier;
45 #endif
46 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
47 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
48 guint ArdourKeyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
49 guint ArdourKeyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
50 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier;
51 guint ArdourKeyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
52
53 ArdourKeyboard::ArdourKeyboard (ARDOUR_UI& ardour_ui) : ui (ardour_ui)
54 {
55         Keyboard::RelevantModifierKeysChanged.connect (sigc::mem_fun (*this, &ArdourKeyboard::reset_relevant_modifier_key_mask));
56 }
57
58 void
59 ArdourKeyboard::find_bindings_files (map<string,string>& files)
60 {
61         vector<std::string> found;
62         Searchpath spath = ardour_config_search_path();
63
64         find_files_matching_pattern (found, spath, string_compose ("*%1", Keyboard::binding_filename_suffix));
65
66         if (found.empty()) {
67                 return;
68         }
69
70         for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
71                 std::string path(*x);
72                 pair<string,string> namepath;
73                 namepath.second = path;
74                 namepath.first = PBD::basename_nosuffix (path);
75                 files.insert (namepath);
76         }
77 }
78
79 void
80 ArdourKeyboard::setup_keybindings ()
81 {
82         using namespace ARDOUR_COMMAND_LINE;
83         string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
84         vector<string> strs;
85
86         binding_files.clear ();
87
88         find_bindings_files (binding_files);
89
90         /* set up the per-user bindings path */
91
92         string lowercase_program_name = downcase (string(PROGRAM_NAME));
93
94         user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
95
96         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
97                 std::pair<string,string> newpair;
98                 newpair.first = _("your own");
99                 newpair.second = user_keybindings_path;
100                 binding_files.insert (newpair);
101         }
102
103         /* check to see if they gave a style name ("ergonomic") or an actual filename (*.bindings)
104         */
105
106         if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
107
108                 // just a style name - allow user to
109                 // specify the layout type.
110
111                 char* layout;
112
113                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
114
115                         /* user-specified keyboard layout */
116
117                         keybindings_path += '-';
118                         keybindings_path += layout;
119
120                 } else {
121
122                         /* default to US/ANSI - we have to pick something */
123
124                         keybindings_path += "-us";
125                 }
126
127                 keybindings_path += binding_filename_suffix;
128         }
129
130         if (keybindings_path.empty()) {
131
132                 /* no path or binding name given: check the user one first */
133
134                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
135
136                         keybindings_path = "";
137
138                 } else {
139
140                         keybindings_path = user_keybindings_path;
141                 }
142         }
143
144         /* if we still don't have a path at this point, use the default */
145
146         if (keybindings_path.empty()) {
147                 keybindings_path = default_bindings;
148         }
149
150         cerr << "KP is " << keybindings_path << endl;
151
152         while (true) {
153
154                 if (!Glib::path_is_absolute (keybindings_path)) {
155
156                         /* not absolute - look in the usual places */
157                         std::string keybindings_file;
158
159                         if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
160
161                                 if (keybindings_path == default_bindings) {
162                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
163                                         return;
164                                 } else {
165                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
166                                                                    keybindings_path)
167                                                 << endmsg;
168                                         keybindings_path = default_bindings;
169                                 }
170
171                         } else {
172
173                                 /* use it */
174
175                                 keybindings_path = keybindings_file;
176                                 break;
177
178                         }
179
180                 } else {
181
182                         /* path is absolute already */
183
184                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
185                                 if (keybindings_path == default_bindings) {
186                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
187                                         return;
188                                 } else {
189                                         keybindings_path = default_bindings;
190                                 }
191
192                         } else {
193                                 break;
194                         }
195                 }
196         }
197
198         info << string_compose (_("Loading keybindings from %1"), keybindings_path) << endmsg;
199
200         load_keybindings (keybindings_path);
201
202         /* catch changes made via some GTK mechanism */
203
204         // GtkAccelMap* accelmap = gtk_accel_map_get();
205         // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
206 }
207
208 XMLNode&
209 ArdourKeyboard::get_state (void)
210 {
211         XMLNode* node = &Keyboard::get_state ();
212         char buf[32];
213
214         snprintf (buf, sizeof (buf), "%d", constraint_mod);
215         node->add_property ("constraint-modifier", buf);
216         snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
217         node->add_property ("trim-contents-modifier", buf);
218         snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
219         node->add_property ("trim-overlap-modifier", buf);
220         snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
221         node->add_property ("trim-anchored-modifier", buf);
222         snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
223         node->add_property ("fine-adjust-modifier", buf);
224         snprintf (buf, sizeof (buf), "%d", push_points_mod);
225         node->add_property ("push-points-modifier", buf);
226         snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
227         node->add_property ("note-size-relative-modifier", buf);
228
229         return *node;
230 }
231
232 int
233 ArdourKeyboard::set_state (const XMLNode& node, int version)
234 {
235         XMLProperty const * prop;
236
237         if ((prop = node.property ("constraint-modifier")) != 0) {
238                 sscanf (prop->value().c_str(), "%d", &constraint_mod);
239         }
240
241         if ((prop = node.property ("trim-contents-modifier")) != 0) {
242                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
243         }
244
245         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
246                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
247         }
248
249         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
250                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
251         }
252
253         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
254                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
255         }
256
257         if ((prop = node.property ("push-points-modifier")) != 0) {
258                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
259         }
260
261         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
262                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
263         }
264
265         return Keyboard::set_state (node, version);
266 }
267
268 void
269 ArdourKeyboard::reset_relevant_modifier_key_mask ()
270 {
271         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
272         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
273         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
274         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
275         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
276         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
277         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
278 }
279
280 /* Snap and snap delta modifiers may contain each other, so we use the
281  * following two methods to sort that out:
282  */
283 bool
284 ArdourKeyboard::indicates_snap (guint state)
285 {
286         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
287         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
288         const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
289
290         return  (contains_s && ((contains_d && s_contains_d) || !contains_d));
291 }
292
293 bool
294 ArdourKeyboard::indicates_snap_delta (guint state)
295 {
296         const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
297         const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
298         const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
299
300         return (contains_d && ((contains_s && d_contains_s) || !contains_s));
301 }
302
303 void
304 ArdourKeyboard::set_constraint_modifier (guint mod)
305 {
306         constraint_mod = mod;
307         the_keyboard().reset_relevant_modifier_key_mask();
308 }
309
310 void
311 ArdourKeyboard::set_trim_contents_modifier (guint mod)
312 {
313         trim_contents_mod = mod;
314         the_keyboard().reset_relevant_modifier_key_mask();
315 }
316
317 void
318 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
319 {
320         trim_overlap_mod = mod;
321         the_keyboard().reset_relevant_modifier_key_mask();
322 }
323
324 void
325 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
326 {
327         trim_anchored_mod = mod;
328         the_keyboard().reset_relevant_modifier_key_mask();
329 }
330
331 void
332 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
333 {
334         fine_adjust_mod = mod;
335         the_keyboard().reset_relevant_modifier_key_mask();
336 }
337
338 void
339 ArdourKeyboard::set_push_points_modifier (guint mod)
340 {
341         push_points_mod = mod;
342         the_keyboard().reset_relevant_modifier_key_mask();
343 }
344
345 void
346 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
347 {
348         note_size_relative_mod = mod;
349         the_keyboard().reset_relevant_modifier_key_mask();
350 }
351
352 Selection::Operation
353 ArdourKeyboard::selection_type (guint state)
354 {
355         /* note that there is no modifier for "Add" */
356
357         if (modifier_state_equals (state, RangeSelectModifier)) {
358                 return Selection::Extend;
359         } else if (modifier_state_equals (state, PrimaryModifier)) {
360                 return Selection::Toggle;
361         } else {
362                 return Selection::Set;
363         }
364 }