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