Rename PBD::find_file_in_search_path to just PBD::find_file
[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/error.h"
21 #include "pbd/file_utils.h"
22
23 #include "ardour/filesystem_paths.h"
24
25 #include "ardour_ui.h"
26 #include "keyboard.h"
27 #include "opts.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace Gtk;
33 using namespace PBD;
34 using namespace ARDOUR;
35 using Gtkmm2ext::Keyboard;
36
37 static void
38 accel_map_changed (GtkAccelMap* /*map*/,
39                    gchar* /*path*/,
40                    guint /*key*/,
41                    GdkModifierType /*mod*/,
42                    gpointer keyboard)
43 {
44         ArdourKeyboard* me = (ArdourKeyboard*)keyboard;
45         Keyboard::keybindings_changed ();
46         me->ui.setup_tooltips ();
47 }
48
49 void
50 ArdourKeyboard::setup_keybindings ()
51 {
52         using namespace ARDOUR_COMMAND_LINE;
53         string default_bindings = "mnemonic-us.bindings";
54         vector<string> strs;
55
56         binding_files.clear ();
57
58         ARDOUR::find_bindings_files (binding_files);
59
60         /* set up the per-user bindings path */
61
62         user_keybindings_path = Glib::build_filename (user_config_directory(), "ardour.bindings");
63
64         if (Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
65                 std::pair<string,string> newpair;
66                 newpair.first = _("your own");
67                 newpair.second = user_keybindings_path;
68                 binding_files.insert (newpair);
69         }
70
71         /* check to see if they gave a style name ("SAE", "ergonomic") or
72            an actual filename (*.bindings)
73         */
74
75         if (!keybindings_path.empty() && keybindings_path.find (".bindings") == string::npos) {
76
77                 // just a style name - allow user to
78                 // specify the layout type.
79
80                 char* layout;
81
82                 if ((layout = getenv ("ARDOUR_KEYBOARD_LAYOUT")) != 0 && layout[0] != '\0') {
83
84                         /* user-specified keyboard layout */
85
86                         keybindings_path += '-';
87                         keybindings_path += layout;
88
89                 } else {
90
91                         /* default to US/ANSI - we have to pick something */
92
93                         keybindings_path += "-us";
94                 }
95
96                 keybindings_path += ".bindings";
97         }
98
99         if (keybindings_path.empty()) {
100
101                 /* no path or binding name given: check the user one first */
102
103                 if (!Glib::file_test (user_keybindings_path, Glib::FILE_TEST_EXISTS)) {
104
105                         keybindings_path = "";
106
107                 } else {
108
109                         keybindings_path = user_keybindings_path;
110                 }
111         }
112
113         /* if we still don't have a path at this point, use the default */
114
115         if (keybindings_path.empty()) {
116                 keybindings_path = default_bindings;
117         }
118
119         while (true) {
120
121                 if (!Glib::path_is_absolute (keybindings_path)) {
122
123                         /* not absolute - look in the usual places */
124                         std::string keybindings_file;
125
126                         if ( ! find_file (ardour_config_search_path(), keybindings_path, keybindings_file)) {
127
128                                 if (keybindings_path == default_bindings) {
129                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
130                                         return;
131                                 } else {
132                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
133                                                                    keybindings_path)
134                                                 << endmsg;
135                                         keybindings_path = default_bindings;
136                                 }
137
138                         } else {
139
140                                 /* use it */
141
142                                 keybindings_path = keybindings_file;
143                                 break;
144
145                         }
146
147                 } else {
148
149                         /* path is absolute already */
150
151                         if (!Glib::file_test (keybindings_path, Glib::FILE_TEST_EXISTS)) {
152                                 if (keybindings_path == default_bindings) {
153                                         error << string_compose (_("Default keybindings not found - %1 will be hard to use!"), PROGRAM_NAME) << endmsg;
154                                         return;
155                                 } else {
156                                         warning << string_compose (_("Key bindings file \"%1\" not found. Default bindings used instead"),
157                                                                    keybindings_path)
158                                                 << endmsg;
159                                         keybindings_path = default_bindings;
160                                 }
161
162                         } else {
163                                 break;
164                         }
165                 }
166         }
167
168         load_keybindings (keybindings_path);
169
170         /* catch changes */
171
172         GtkAccelMap* accelmap = gtk_accel_map_get();
173         g_signal_connect (accelmap, "changed", (GCallback) accel_map_changed, this);
174 }
175
176 Selection::Operation
177 ArdourKeyboard::selection_type (guint state)
178 {
179         /* note that there is no modifier for "Add" */
180
181         if (modifier_state_equals (state, RangeSelectModifier)) {
182                 return Selection::Extend;
183         } else if (modifier_state_equals (state, PrimaryModifier)) {
184                 return Selection::Toggle;
185         } else {
186                 return Selection::Set;
187         }
188 }
189
190