make ardour3 build and link on OS X (tiger, at least)
[ardour.git] / gtk2_ardour / keyeditor.cc
1 /*
2     Copyright (C) 2002 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <map>
25
26 #include "ardour/profile.h"
27
28 #include <gtkmm/stock.h>
29 #include <gtkmm/label.h>
30 #include <gtkmm/accelkey.h>
31 #include <gtkmm/accelmap.h>
32 #include <gtkmm/uimanager.h>
33
34 #include "gtkmm2ext/utils.h"
35
36 #include "pbd/strsplit.h"
37 #include "pbd/replace_all.h"
38
39 #include "ardour/profile.h"
40
41 #include "actions.h"
42 #include "keyboard.h"
43 #include "keyeditor.h"
44 #include "utils.h"
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace Gtk;
50 using namespace Gdk;
51 using namespace PBD;
52
53 using Gtkmm2ext::Keyboard;
54
55 KeyEditor::KeyEditor ()
56         : ArdourDialog (_("Key Bindings"), false)
57         , unbind_button (_("Remove shortcut"))
58         , unbind_box (BUTTONBOX_END)
59
60 {
61         can_bind = false;
62         last_state = 0;
63
64         model = TreeStore::create(columns);
65
66         view.set_model (model);
67         view.append_column (_("Action"), columns.action);
68         view.append_column (_("Shortcut"), columns.binding);
69         view.set_headers_visible (true);
70         view.get_selection()->set_mode (SELECTION_SINGLE);
71         view.set_reorderable (false);
72         view.set_size_request (500,300);
73         view.set_enable_search (false);
74         view.set_rules_hint (true);
75         view.set_name (X_("KeyEditorTree"));
76
77         view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &KeyEditor::action_selected));
78
79         scroller.add (view);
80         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
81
82
83         get_vbox()->set_spacing (6);
84         get_vbox()->pack_start (scroller);
85
86         if (!ARDOUR::Profile->get_sae()) {
87
88                 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
89                 hint->show ();
90                 unbind_box.set_spacing (6);
91                 unbind_box.pack_start (*hint, false, true);
92                 unbind_box.pack_start (unbind_button, false, false);
93                 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
94
95                 get_vbox()->pack_start (unbind_box, false, false);
96                 unbind_box.show ();
97                 unbind_button.show ();
98
99         }
100
101         get_vbox()->set_border_width (12);
102
103         view.show ();
104         scroller.show ();
105
106         unbind_button.set_sensitive (false);
107 }
108
109 void
110 KeyEditor::unbind ()
111 {
112         TreeModel::iterator i = view.get_selection()->get_selected();
113
114         unbind_button.set_sensitive (false);
115
116         cerr << "trying to unbind\n";
117
118         if (i != model->children().end()) {
119                 string path = (*i)[columns.path];
120
121                 if (!(*i)[columns.bindable]) {
122                         return;
123                 }
124
125                 bool result = AccelMap::change_entry (path,
126                                                       0,
127                                                       (ModifierType) 0,
128                                                       true);
129                 if (result) {
130                         (*i)[columns.binding] = string ();
131                 }
132         }
133 }
134
135 void
136 KeyEditor::on_show ()
137 {
138         populate ();
139         view.get_selection()->unselect_all ();
140         ArdourDialog::on_show ();
141 }
142
143 void
144 KeyEditor::on_unmap ()
145 {
146         ArdourDialog::on_unmap ();
147 }
148
149 void
150 KeyEditor::action_selected ()
151 {
152         if (view.get_selection()->count_selected_rows() == 0) {
153                 return;
154         }
155
156         TreeModel::iterator i = view.get_selection()->get_selected();
157
158         unbind_button.set_sensitive (false);
159
160         if (i != model->children().end()) {
161
162                 string path = (*i)[columns.path];
163
164                 if (!(*i)[columns.bindable]) {
165                         return;
166                 }
167
168                 string binding = (*i)[columns.binding];
169
170                 if (!binding.empty()) {
171                         unbind_button.set_sensitive (true);
172                 }
173         }
174 }
175
176 bool
177 KeyEditor::on_key_press_event (GdkEventKey* ev)
178 {
179         can_bind = true;
180         last_state = ev->state;
181         return false;
182 }
183
184 bool
185 KeyEditor::on_key_release_event (GdkEventKey* ev)
186 {
187         if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
188                 return false;
189         }
190
191         TreeModel::iterator i = view.get_selection()->get_selected();
192
193         if (i != model->children().end()) {
194                 string path = (*i)[columns.path];
195
196                 if (!(*i)[columns.bindable]) {
197                         goto out;
198                 }
199
200                 cerr << "real lkeyval: " << ev->keyval << endl;
201                 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
202                 cerr << "using keyval = " << ev->keyval << endl;
203
204
205                 bool result = AccelMap::change_entry (path,
206                                                       ev->keyval,
207                                                       ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
208                                                       true);
209
210                 cerr << "New binding to " << ev->keyval << " worked: " << result << endl;
211
212                 if (result) {
213                         AccelKey key;
214                         (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
215                 }
216         }
217
218   out:
219         can_bind = false;
220         return true;
221 }
222
223 void
224 KeyEditor::populate ()
225 {
226         vector<string> paths;
227         vector<string> labels;
228         vector<string> keys;
229         vector<AccelKey> bindings;
230         typedef std::map<string,TreeIter> NodeMap;
231         NodeMap nodes;
232         NodeMap::iterator r;
233
234         ActionManager::get_all_actions (labels, paths, keys, bindings);
235
236         vector<string>::iterator k;
237         vector<string>::iterator p;
238         vector<string>::iterator l;
239
240         model->clear ();
241
242         for (l = labels.begin(), k = keys.begin(), p = paths.begin(); l != labels.end(); ++k, ++p, ++l) {
243
244                 TreeModel::Row row;
245                 vector<string> parts;
246
247                 parts.clear ();
248
249                 split (*p, parts, '/');
250
251                 if (parts.empty()) {
252                         continue;
253                 }
254
255                 if ((r = nodes.find (parts[1])) == nodes.end()) {
256
257                         /* top level is missing */
258
259                         TreeIter rowp;
260                         TreeModel::Row parent;
261                         rowp = model->append();
262                         nodes[parts[1]] = rowp;
263                         parent = *(rowp);
264                         parent[columns.action] = parts[1];
265                         parent[columns.bindable] = false;
266
267                         row = *(model->append (parent.children()));
268
269                 } else {
270
271                         row = *(model->append ((*r->second)->children()));
272
273                 }
274
275                 /* add this action */
276
277                 row[columns.action] = (*l);
278                 row[columns.path] = (*p);
279                 row[columns.bindable] = true;
280
281                 if (*k == ActionManager::unbound_string) {
282                         row[columns.binding] = string();
283                 } else {
284
285 #ifdef GTKOSX
286                         string label = (*k);
287
288                         /* Gtk/Quartz maps:
289                            NSAlternate/NSOption key to Mod1
290                            NSCommand key to Meta
291                         */
292
293                         replace_all (label, "<Meta>", _("Command-"));
294                         replace_all (label, "<Alt>", _("Option-"));
295                         replace_all (label, "<Shift>", _("Shift-"));
296                         replace_all (label, "<Control>", _("Control-"));
297                         row[columns.binding] = label;
298 #else
299                         row[columns.binding] = (*k);
300 #endif
301                 }
302         }
303 }