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