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"
24 #include "ardour/filesystem_paths.h"
26 #include "ardour_ui.h"
27 #include "public_editor.h"
36 using namespace ARDOUR;
37 using Gtkmm2ext::Keyboard;
40 accel_map_changed (GtkAccelMap* /*map*/,
43 GdkModifierType /*mod*/,
46 ArdourKeyboard* me = (ArdourKeyboard*)keyboard;
47 Keyboard::keybindings_changed ();
48 me->ui.setup_tooltips ();
52 guint ArdourKeyboard::constraint_mod = Keyboard::PrimaryModifier;
54 guint ArdourKeyboard::constraint_mod = Keyboard::SecondaryModifier;
56 guint ArdourKeyboard::trim_contents_mod = Keyboard::PrimaryModifier;
57 guint ArdourKeyboard::trim_overlap_mod = Keyboard::TertiaryModifier;
58 guint ArdourKeyboard::trim_anchored_mod = Keyboard::TertiaryModifier;
59 guint ArdourKeyboard::fine_adjust_mod = Keyboard::SecondaryModifier;
60 guint ArdourKeyboard::push_points_mod = Keyboard::PrimaryModifier;
61 guint ArdourKeyboard::note_size_relative_mod = Keyboard::PrimaryModifier;
64 ArdourKeyboard::setup_keybindings ()
66 using namespace ARDOUR_COMMAND_LINE;
67 string default_bindings = "mnemonic-us.bindings";
70 binding_files.clear ();
72 ARDOUR::find_bindings_files (binding_files);
74 /* set up the per-user bindings path */
76 string lowercase_program_name = downcase (string(PROGRAM_NAME));
78 user_keybindings_path = Glib::build_filename (user_config_directory(), lowercase_program_name + ".bindings");
80 if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
81 std::pair<string,string> newpair;
82 newpair.first = _("your own");
83 newpair.second = user_keybindings_path;
84 binding_files.insert (newpair);
87 /* check to see if they gave a style name ("SAE", "ergonomic") or
88 an actual filename (*.bindings)
91 if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
93 // just a style name - allow user to
94 // specify the layout type.
98 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
100 /* user-specified keyboard layout */
102 keybindings_path += '-';
103 keybindings_path += layout;
107 /* default to US/ANSI - we have to pick something */
109 keybindings_path += "-us";
112 keybindings_path += ".bindings";
115 if (keybindings_path.empty()) {
117 /* no path or binding name given: check the user one first */
119 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
121 keybindings_path = "";
125 keybindings_path = user_keybindings_path;
129 /* if we still don't have a path at this point, use the default */
131 if (keybindings_path.empty()) {
132 keybindings_path = default_bindings;
137 if (!Glib::path_is_absolute (keybindings_path)) {
139 /* not absolute - look in the usual places */
140 std::string keybindings_file;
142 if ( ! find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
144 if (keybindings_path == default_bindings) {
145 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
148 warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
151 keybindings_path = default_bindings;
158 keybindings_path = keybindings_file;
165 /* path is absolute already */
167 if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
168 if (keybindings_path == default_bindings) {
169 error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
172 keybindings_path = default_bindings;
181 load_keybindings (keybindings_path);
185 GtkAccelMap* accelmap = gtk_accel_map_get();
186 g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
190 ArdourKeyboard::get_state (void)
192 XMLNode* node = &Keyboard::get_state ();
195 snprintf (buf, sizeof (buf), "%d", constraint_mod);
196 node->add_property ("constraint-modifier", buf);
197 snprintf (buf, sizeof (buf), "%d", trim_contents_mod);
198 node->add_property ("trim-contents-modifier", buf);
199 snprintf (buf, sizeof (buf), "%d", trim_overlap_mod);
200 node->add_property ("trim-overlap-modifier", buf);
201 snprintf (buf, sizeof (buf), "%d", trim_anchored_mod);
202 node->add_property ("trim-anchored-modifier", buf);
203 snprintf (buf, sizeof (buf), "%d", fine_adjust_mod);
204 node->add_property ("fine-adjust-modifier", buf);
205 snprintf (buf, sizeof (buf), "%d", push_points_mod);
206 node->add_property ("push-points-modifier", buf);
207 snprintf (buf, sizeof (buf), "%d", note_size_relative_mod);
208 node->add_property ("note-size-relative-modifier", buf);
214 ArdourKeyboard::set_state (const XMLNode& node, int version)
216 const XMLProperty* prop;
218 if ((prop = node.property ("constraint-modifier")) != 0) {
219 sscanf (prop->value().c_str(), "%d", &constraint_mod);
222 if ((prop = node.property ("trim-contents-modifier")) != 0) {
223 sscanf (prop->value().c_str(), "%d", &trim_contents_mod);
226 if ((prop = node.property ("trim-overlap-modifier")) != 0) {
227 sscanf (prop->value().c_str(), "%d", &trim_overlap_mod);
230 if ((prop = node.property ("trim-anchored-modifier")) != 0) {
231 sscanf (prop->value().c_str(), "%d", &trim_anchored_mod);
234 if ((prop = node.property ("fine-adjust-modifier")) != 0) {
235 sscanf (prop->value().c_str(), "%d", &fine_adjust_mod);
238 if ((prop = node.property ("push-points-modifier")) != 0) {
239 sscanf (prop->value().c_str(), "%d", &push_points_mod);
242 if ((prop = node.property ("note-size-relative-modifier")) != 0) {
243 sscanf (prop->value().c_str(), "%d", ¬e_size_relative_mod);
246 return Keyboard::set_state (node, version);
249 /* Snap and snap delta modifiers may contain each other, so we use the
250 * following two methods to sort that out:
253 ArdourKeyboard::indicates_snap (guint state)
255 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
256 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
257 const bool s_contains_d = Keyboard::modifier_state_contains (Keyboard::snap_modifier (), Keyboard::snap_delta_modifier ());
259 return (contains_s && ((contains_d && s_contains_d) || !contains_d));
263 ArdourKeyboard::indicates_snap_delta (guint state)
265 const bool contains_d = Keyboard::modifier_state_contains (state, Keyboard::snap_delta_modifier ());
266 const bool contains_s = Keyboard::modifier_state_contains (state, Keyboard::snap_modifier ());
267 const bool d_contains_s = Keyboard::modifier_state_contains (Keyboard::snap_delta_modifier (), Keyboard::snap_modifier ());
269 return (contains_d && ((contains_s && d_contains_s) || !contains_s));
273 ArdourKeyboard::set_constraint_modifier (guint mod)
275 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~constraint_mod);
276 constraint_mod = mod;
277 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | constraint_mod);
281 ArdourKeyboard::set_trim_contents_modifier (guint mod)
283 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_contents_mod);
284 trim_contents_mod = mod;
285 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_contents_mod);
289 ArdourKeyboard::set_trim_overlap_modifier (guint mod)
291 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_overlap_mod);
292 trim_overlap_mod = mod;
293 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_overlap_mod);
297 ArdourKeyboard::set_trim_anchored_modifier (guint mod)
299 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~trim_anchored_mod);
300 trim_anchored_mod = mod;
301 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | trim_anchored_mod);
305 ArdourKeyboard::set_fine_adjust_modifier (guint mod)
307 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~fine_adjust_mod);
308 fine_adjust_mod = mod;
309 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | fine_adjust_mod);
313 ArdourKeyboard::set_push_points_modifier (guint mod)
315 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~push_points_mod);
316 push_points_mod = mod;
317 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | push_points_mod);
321 ArdourKeyboard::set_note_size_relative_modifier (guint mod)
323 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask & ~note_size_relative_mod);
324 note_size_relative_mod = mod;
325 RelevantModifierKeyMask = GdkModifierType (RelevantModifierKeyMask | note_size_relative_mod);
329 ArdourKeyboard::selection_type (guint state)
331 /* note that there is no modifier for "Add" */
333 if (modifier_state_equals (state, RangeSelectModifier)) {
334 return Selection::Extend;
335 } else if (modifier_state_equals (state, PrimaryModifier)) {
336 return Selection::Toggle;
338 return Selection::Set;