lets see what gtk/osx displays for key bindings without us editing them
[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         : ArdourWindow (_("Key Bindings"))
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         add (scroller);
83
84         if (!ARDOUR::Profile->get_sae()) {
85
86                 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
87                 hint->show ();
88                 unbind_box.set_spacing (6);
89                 unbind_box.pack_start (*hint, false, true);
90                 unbind_box.pack_start (unbind_button, false, false);
91                 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
92
93                 add (unbind_box);
94                 unbind_box.show ();
95                 unbind_button.show ();
96
97         }
98
99         view.show ();
100         scroller.show ();
101
102         unbind_button.set_sensitive (false);
103 }
104
105 void
106 KeyEditor::unbind ()
107 {
108         TreeModel::iterator i = view.get_selection()->get_selected();
109
110         unbind_button.set_sensitive (false);
111
112         cerr << "trying to unbind\n";
113
114         if (i != model->children().end()) {
115                 string path = (*i)[columns.path];
116
117                 if (!(*i)[columns.bindable]) {
118                         return;
119                 }
120
121                 bool result = AccelMap::change_entry (path,
122                                                       0,
123                                                       (ModifierType) 0,
124                                                       true);
125                 if (result) {
126                         (*i)[columns.binding] = string ();
127                 }
128         }
129 }
130
131 void
132 KeyEditor::on_show ()
133 {
134         populate ();
135         view.get_selection()->unselect_all ();
136         ArdourWindow::on_show ();
137 }
138
139 void
140 KeyEditor::on_unmap ()
141 {
142         ArdourWindow::on_unmap ();
143 }
144
145 void
146 KeyEditor::action_selected ()
147 {
148         if (view.get_selection()->count_selected_rows() == 0) {
149                 return;
150         }
151
152         TreeModel::iterator i = view.get_selection()->get_selected();
153
154         unbind_button.set_sensitive (false);
155
156         if (i != model->children().end()) {
157
158                 string path = (*i)[columns.path];
159
160                 if (!(*i)[columns.bindable]) {
161                         return;
162                 }
163
164                 string binding = (*i)[columns.binding];
165
166                 if (!binding.empty()) {
167                         unbind_button.set_sensitive (true);
168                 }
169         }
170 }
171
172 bool
173 KeyEditor::on_key_press_event (GdkEventKey* ev)
174 {
175         can_bind = true;
176         last_state = ev->state;
177         return false;
178 }
179
180 bool
181 KeyEditor::on_key_release_event (GdkEventKey* ev)
182 {
183         if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
184                 return false;
185         }
186
187         TreeModel::iterator i = view.get_selection()->get_selected();
188
189         if (i != model->children().end()) {
190                 string path = (*i)[columns.path];
191
192                 if (!(*i)[columns.bindable]) {
193                         goto out;
194                 }
195
196                 cerr << "real lkeyval: " << ev->keyval << endl;
197                 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
198                 cerr << "using keyval = " << ev->keyval << endl;
199
200
201                 bool result = AccelMap::change_entry (path,
202                                                       ev->keyval,
203                                                       ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
204                                                       true);
205
206                 cerr << "New binding to " << ev->keyval << " worked: " << result << endl;
207
208                 if (result) {
209                         AccelKey key;
210                         (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
211                 }
212         }
213
214   out:
215         can_bind = false;
216         return true;
217 }
218
219 void
220 KeyEditor::populate ()
221 {
222         vector<string> paths;
223         vector<string> labels;
224         vector<string> tooltips;
225         vector<string> keys;
226         vector<AccelKey> bindings;
227         typedef std::map<string,TreeIter> NodeMap;
228         NodeMap nodes;
229         NodeMap::iterator r;
230
231         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
232
233         vector<string>::iterator k;
234         vector<string>::iterator p;
235         vector<string>::iterator t;
236         vector<string>::iterator l;
237
238         model->clear ();
239
240         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
241
242                 TreeModel::Row row;
243                 vector<string> parts;
244
245                 parts.clear ();
246
247                 split (*p, parts, '/');
248
249                 if (parts.empty()) {
250                         continue;
251                 }
252
253                 if ((r = nodes.find (parts[1])) == nodes.end()) {
254
255                         /* top level is missing */
256
257                         TreeIter rowp;
258                         TreeModel::Row parent;
259                         rowp = model->append();
260                         nodes[parts[1]] = rowp;
261                         parent = *(rowp);
262                         parent[columns.action] = parts[1];
263                         parent[columns.bindable] = false;
264
265                         row = *(model->append (parent.children()));
266
267                 } else {
268
269                         row = *(model->append ((*r->second)->children()));
270
271                 }
272
273                 /* add this action */
274
275                 if (l->empty ()) {
276                         row[columns.action] = *t;
277                 } else {
278                         row[columns.action] = *l;
279                 }
280                 row[columns.path] = (*p);
281                 row[columns.bindable] = true;
282
283                 if (*k == ActionManager::unbound_string) {
284                         row[columns.binding] = string();
285                 } else {
286
287 #ifdef GTKOSX
288                         string label = (*k);
289                         
290                         /* Gtk/Quartz maps:
291                            NSAlternate/NSOption key to Mod1
292                            NSCommand key to Mod2
293                         */
294
295 //                      replace_all (label, "<Primary>", _("Command-"));
296 //                      replace_all (label, "<Alt>", _("Option-"));
297 //                      replace_all (label, "<Shift>", _("Shift-"));
298 //                      replace_all (label, "<Control>", _("Control-"));
299
300                         row[columns.binding] = label;
301 #else
302                         row[columns.binding] = (*k);
303 #endif
304                 }
305         }
306 }