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