Move ui-centric modifiers into ArdourKeyboard, set some bad defaults.
[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
24 #include "ardour/filesystem_paths.h"
25
26 #include "ardour_ui.h"
27 #include "public_editor.h"
28 #include "keyboard.h"
29 #include "opts.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace Gtk;
35 using namespace PBD;
36 using namespace ARDOUR;
37 using Gtkmm2ext::Keyboard;
38
39 static void
40 accel_map_changed (GtkAccelMap* /*map*/,
41                    gchar* /*path*/,
42                    guint /*key*/,
43                    GdkModifierType /*mod*/,
44                    gpointer keyboard)
45 {
46         ArdourKeyboard* me = (ArdourKeyboard*)keyboard;
47         Keyboard::keybindings_changed ();
48         me->ui.setup_tooltips ();
49 }
50
51 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
52 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
53 guint ArdourKeyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
54 guint ArdourKeyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
55 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier;
56 guint ArdourKeyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
57
58 void
59 ArdourKeyboard::setup_keybindings ()
60 {
61         using namespace ARDOUR_COMMAND_LINE;
62         string default_bindings = "mnemonic-us.bindings";
63         vector<string> strs;
64
65         binding_files.clear ();
66
67         ARDOUR::find_bindings_files (binding_files);
68
69         /* set up the per-user bindings path */
70
71         string lowercase_program_name = downcase (PROGRAM_NAME);
72
73         user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + ".bindings");
74
75         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
76                 std::pair<string,string> newpair;
77                 newpair.first = _("your own");
78                 newpair.second = user_keybindings_path;
79                 binding_files.insert (newpair);
80         }
81
82         /* check to see if they gave a style name ("SAE", "ergonomic") or
83            an actual filename (*.bindings)
84         */
85
86         if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
87
88                 // just a style name - allow user to
89                 // specify the layout type.
90
91                 char* layout;
92
93                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
94
95                         /* user-specified keyboard layout */
96
97                         keybindings_path += '-';
98                         keybindings_path += layout;
99
100                 } else {
101
102                         /* default to US/ANSI - we have to pick something */
103
104                         keybindings_path += "-us";
105                 }
106
107                 keybindings_path += ".bindings";
108         }
109
110         if (keybindings_path.empty()) {
111
112                 /* no path or binding name given: check the user one first */
113
114                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
115
116                         keybindings_path = "";
117
118                 } else {
119
120                         keybindings_path = user_keybindings_path;
121                 }
122         }
123
124         /* if we still don't have a path at this point, use the default */
125
126         if (keybindings_path.empty()) {
127                 keybindings_path = default_bindings;
128         }
129
130         while (true) {
131
132                 if (!Glib::path_is_absolute (keybindings_path)) {
133
134                         /* not absolute - look in the usual places */
135                         std::string keybindings_file;
136
137                         if ( ! find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
138
139                                 if (keybindings_path == default_bindings) {
140                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
141                                         return;
142                                 } else {
143                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
144                                                                    keybindings_path)
145                                                 << endmsg;
146                                         keybindings_path = default_bindings;
147                                 }
148
149                         } else {
150
151                                 /* use it */
152
153                                 keybindings_path = keybindings_file;
154                                 break;
155
156                         }
157
158                 } else {
159
160                         /* path is absolute already */
161
162                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
163                                 if (keybindings_path == default_bindings) {
164                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
165                                         return;
166                                 } else {
167                                         keybindings_path = default_bindings;
168                                 }
169
170                         } else {
171                                 break;
172                         }
173                 }
174         }
175
176         load_keybindings (keybindings_path);
177
178         /* catch changes */
179
180         GtkAccelMap* accelmap = gtk_accel_map_get();
181         g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
182 }
183
184 XMLNode&
185 ArdourKeyboard::get_state (void)
186 {
187         XMLNode* node = &Keyboard::get_state ();
188         char buf[32];
189
190         snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
191         node->add_property ("trim-contents-modifier", buf);
192         snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
193         node->add_property ("trim-overlap-modifier", buf);
194         snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
195         node->add_property ("trim-anchored-modifier", buf);
196         snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
197         node->add_property ("fine-adjust-modifier", buf);
198         snprintf (buf, sizeof (buf), "%d", push_points_mod);
199         node->add_property ("push-points-modifier", buf);
200         snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
201         node->add_property ("note-size-relative-modifier", buf);
202
203         return *node;
204 }
205
206 int
207 ArdourKeyboard::set_state (const XMLNode& node, int version)
208 {
209         const XMLProperty* prop;
210
211         if ((prop = node.property ("trim-contents-modifier")) != 0) {
212                 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
213         }
214
215         if ((prop = node.property ("trim-overlap-modifier")) != 0) {
216                 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
217         }
218
219         if ((prop = node.property ("trim-anchored-modifier")) != 0) {
220                 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
221         }
222
223         if ((prop = node.property ("fine-adjust-modifier")) != 0) {
224                 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
225         }
226
227         if ((prop = node.property ("push-points-modifier")) != 0) {
228                 sscanf (prop->value().c_str(), "%d", &push_points_mod);
229         }
230
231         if ((prop = node.property ("note-size-relative-modifier")) != 0) {
232                 sscanf (prop->value().c_str(), "%d", &note_size_relative_mod);
233         }
234
235         return Keyboard::set_state (node, version);
236 }
237
238 void
239 ArdourKeyboard::set_trim_contents_modifier (guint mod)
240 {
241         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
242         trim_contents_mod = mod;
243         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
244 }
245
246 void
247 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
248 {
249         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
250         trim_overlap_mod = mod;
251         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
252 }
253
254 void
255 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
256 {
257         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
258         trim_anchored_mod = mod;
259         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
260 }
261
262 void
263 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
264 {
265         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
266         fine_adjust_mod = mod;
267         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
268 }
269
270 void
271 ArdourKeyboard::set_push_points_modifier (guint mod)
272 {
273         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
274         push_points_mod = mod;
275         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
276 }
277
278 void
279 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
280 {
281         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
282         note_size_relative_mod = mod;
283         RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
284 }
285
286 Selection::Operation
287 ArdourKeyboard::selection_type (guint state)
288 {
289         /* note that there is no modifier for "Add" */
290
291         if (modifier_state_equals (state, RangeSelectModifier)) {
292                 return Selection::Extend;
293         } else if (modifier_state_equals (state, PrimaryModifier)) {
294                 return Selection::Toggle;
295         } else {
296                 return Selection::Set;
297         }
298 }
299
300