2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/file_utils.h"
23 #include "pbd/basename.h"
25 #include "ardour/filesystem_paths.h"
26 #include "ardour/revision.h"
28 #include "ardour_ui.h"
29 #include "public_editor.h"
32 #include "ui_config.h"
39 using namespace ARDOUR;
40 using Gtkmm2ext::Keyboard;
43 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
45 guint ArdourKeyboard::constraint_mod = Keyboard::TertiaryModifier;
48 /* TrimDrag::start_grab() */
49 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
51 /* TrimDrag::motion() */
52 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
54 /* TrimDrag::start_grab() */
55 guint ArdourKeyboard::trim_anchored_mod = Keyboard::PrimaryModifier|Keyboard::TertiaryModifier;
57 /* ControlPointDrag::motion() && LineDrag::motion()*/
58 guint ArdourKeyboard::fine_adjust_mod = Keyboard::PrimaryModifier|Keyboard::SecondaryModifier; // XXX better just 2ndary
60 /* ControlPointDrag::start_grab() && MarkerDrag::motion() */
61 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier|Keyboard::Level4Modifier;
63 /* NoteResizeDrag::start_grab() */
64 guint ArdourKeyboard::note_size_relative_mod = Keyboard::TertiaryModifier; // XXX better: 2ndary
66 ArdourKeyboard::ArdourKeyboard (ARDOUR_UI& ardour_ui) : ui (ardour_ui)
68 Keyboard::RelevantModifierKeysChanged.connect (sigc::mem_fun (*this, &ArdourKeyboard::reset_relevant_modifier_key_mask));
72 ArdourKeyboard::find_bindings_files (map<string,string>& files)
74 vector<std::string> found;
75 Searchpath spath = ardour_config_search_path();
77 find_files_matching_pattern (found, spath, string_compose ("*%1", Keyboard::binding_filename_suffix));
83 for (vector<std::string>::iterator x = found.begin(); x != found.end(); ++x) {
85 pair<string,string> namepath;
86 namepath.second = path;
87 namepath.first = PBD::basename_nosuffix (path);
88 files.insert (namepath);
93 ArdourKeyboard::setup_keybindings ()
95 string keybindings_path = ARDOUR_COMMAND_LINE::keybindings_path;
96 string default_bindings = string_compose ("%1%2", UIConfiguration::instance().get_default_bindings(), Keyboard::binding_filename_suffix);
99 binding_files.clear ();
101 find_bindings_files (binding_files);
103 /* set up the per-user bindings path */
105 string lowercase_program_name = downcase (string(PROGRAM_NAME));
107 /* extract and append minor vesion */
108 std::string rev (revision);
109 std::size_t pos = rev.find_first_of("-");
110 if (pos != string::npos && pos > 0) {
111 lowercase_program_name += "-";
112 lowercase_program_name += rev.substr (0, pos);
115 user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + binding_filename_suffix);
117 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
118 std::pair<string,string> newpair;
119 newpair.first = _("your own");
120 newpair.second = user_keybindings_path;
121 binding_files.insert (newpair);
124 /* check to see if they gave a style name ("ergonomic") or an actual filename (*.bindings)
127 if (!keybindings_path.empty() && keybindings_path.find (binding_filename_suffix) == string::npos) {
129 // just a style name - allow user to
130 // specify the layout type.
134 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
136 /* user-specified keyboard layout */
138 keybindings_path += '-';
139 keybindings_path += layout;
143 /* default to US/ANSI - we have to pick something */
145 keybindings_path += "-us";
148 keybindings_path += binding_filename_suffix;
151 if (keybindings_path.empty()) {
153 /* no path or binding name given: check the user one first */
155 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
157 keybindings_path = "";
161 keybindings_path = user_keybindings_path;
165 /* if we still don't have a path at this point, use the default */
167 if (keybindings_path.empty()) {
168 keybindings_path = default_bindings;
173 if (!Glib::path_is_absolute (keybindings_path)) {
175 /* not absolute - look in the usual places */
176 std::string keybindings_file;
178 if (!find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
180 if (keybindings_path == default_bindings) {
181 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
184 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
187 keybindings_path = default_bindings;
194 keybindings_path = keybindings_file;
201 /* path is absolute already */
203 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
204 if (keybindings_path == default_bindings) {
205 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
208 keybindings_path = default_bindings;
217 load_keybindings (keybindings_path);
219 /* catch changes made via some GTK mechanism */
221 // GtkAccelMap* accelmap = gtk_accel_map_get();
222 // g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
226 ArdourKeyboard::get_state (void)
228 XMLNode* node = &Keyboard::get_state ();
230 node->set_property ("constraint-modifier", constraint_mod);
231 node->set_property ("trim-contents-modifier", trim_contents_mod);
232 node->set_property ("trim-overlap-modifier", trim_overlap_mod);
233 node->set_property ("trim-anchored-modifier", trim_anchored_mod);
234 node->set_property ("fine-adjust-modifier", fine_adjust_mod);
235 node->set_property ("push-points-modifier", push_points_mod);
236 node->set_property ("note-size-relative-modifier", note_size_relative_mod);
242 ArdourKeyboard::set_state (const XMLNode& node, int version)
244 node.get_property ("constraint-modifier", constraint_mod);
245 node.get_property ("trim-contents-modifier", trim_contents_mod);
246 node.get_property ("trim-overlap-modifier", trim_overlap_mod);
247 node.get_property ("trim-anchored-modifier", trim_anchored_mod);
248 node.get_property ("fine-adjust-modifier", fine_adjust_mod);
249 node.get_property ("push-points-modifier", push_points_mod);
250 node.get_property ("note-size-relative-modifier", note_size_relative_mod);
252 return Keyboard::set_state (node, version);
256 ArdourKeyboard::reset_relevant_modifier_key_mask ()
258 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
259 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
260 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
261 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
262 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
263 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
264 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
267 /* Snap and snap delta modifiers may contain each other, so we use the
268 * following two methods to sort that out:
271 ArdourKeyboard::indicates_snap (guint state)
273 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
274 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
275 const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
277 return (contains_s && ((contains_d && s_contains_d) || !contains_d));
281 ArdourKeyboard::indicates_snap_delta (guint state)
283 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
284 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
285 const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
287 return (contains_d && ((contains_s && d_contains_s) || !contains_s));
290 /* Constraint and copy modifiers are both in effect at the beginning of some drags, and may be set ambiguously */
292 ArdourKeyboard::indicates_copy (guint state)
294 const bool contains_c = Keyboard::modifier_state_contains (state, Keyboard::CopyModifier);
295 const bool equals_cs = Keyboard::modifier_state_equals (state, constraint_modifier ());
297 return contains_c && !equals_cs;
301 ArdourKeyboard::indicates_constraint (guint state)
303 const bool contains_cs = Keyboard::modifier_state_contains (state, constraint_modifier ());
304 const bool equals_c = Keyboard::modifier_state_equals (state, Keyboard::CopyModifier);
306 return contains_cs && !equals_c;
310 ArdourKeyboard::set_constraint_modifier (guint mod)
312 constraint_mod = mod;
313 the_keyboard().reset_relevant_modifier_key_mask();
317 ArdourKeyboard::set_trim_contents_modifier (guint mod)
319 trim_contents_mod = mod;
320 the_keyboard().reset_relevant_modifier_key_mask();
324 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
326 trim_overlap_mod = mod;
327 the_keyboard().reset_relevant_modifier_key_mask();
331 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
333 trim_anchored_mod = mod;
334 the_keyboard().reset_relevant_modifier_key_mask();
338 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
340 fine_adjust_mod = mod;
341 the_keyboard().reset_relevant_modifier_key_mask();
345 ArdourKeyboard::set_push_points_modifier (guint mod)
347 push_points_mod = mod;
348 the_keyboard().reset_relevant_modifier_key_mask();
352 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
354 note_size_relative_mod = mod;
355 the_keyboard().reset_relevant_modifier_key_mask();
359 ArdourKeyboard::selection_type (guint state)
361 /* note that there is no modifier for "Add" */
363 if (modifier_state_equals (state, RangeSelectModifier)) {
364 return Selection::Extend;
365 } else if (modifier_state_equals (state, PrimaryModifier)) {
366 return Selection::Toggle;
368 return Selection::Set;