Small refactoring of keyboard bindings (first part)
[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/bindings.h"
33 #include "gtkmm2ext/utils.h"
34
35 #include "pbd/strsplit.h"
36
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/profile.h"
39
40 #include "actions.h"
41 #include "keyboard.h"
42 #include "keyeditor.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace Gdk;
49 using namespace PBD;
50
51 using Gtkmm2ext::Keyboard;
52 using Gtkmm2ext::Bindings;
53
54 /*========================================== HELPER =========================================*/
55 void bindings_collision_dialog (Gtk::Window& parent)
56 {
57         ArdourDialog dialog (parent, _("Colliding keybindings"), true);
58         Label label (_("The key sequence is already bound. Please remove the other binding first."));
59
60         dialog.get_vbox()->pack_start (label, true, true);
61         dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
62         dialog.show_all ();
63         dialog.run();
64 }
65
66 /*======================================== KEYEDITOR =========================================*/
67 KeyEditor::KeyEditor ()
68         : ArdourWindow (_("Key Bindings"))
69         , unbind_button (_("Remove shortcut"))
70         , unbind_box (BUTTONBOX_END)
71         , sort_column(0)
72         , sort_type(Gtk::SORT_ASCENDING)
73 {
74         last_keyval = 0;
75
76         notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
77
78         vpacker.pack_start (notebook, true, true);
79
80         Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
81         hint->show ();
82         unbind_box.set_spacing (6);
83         unbind_box.pack_start (*hint, false, true);
84         unbind_box.pack_start (unbind_button, false, false);
85         unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
86
87         vpacker.pack_start (unbind_box, false, false);
88         unbind_box.show ();
89         unbind_button.show ();
90
91         reset_button.add (reset_label);
92         reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
93
94         reset_box.pack_start (reset_button, true, false);
95         reset_box.show ();
96         reset_button.show ();
97         reset_label.show ();
98         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
99         vpacker.pack_start (reset_box, false, false);
100
101         add (vpacker);
102
103         unbind_button.set_sensitive (false);
104 }
105
106 void
107 KeyEditor::add_tab (string const & name, Bindings& bindings)
108 {
109         Tab* t = new Tab (*this, name, &bindings);
110         t->populate ();
111         t->show_all ();
112         notebook.append_page (*t, name);
113 }
114
115 void
116 KeyEditor::unbind ()
117 {
118         current_tab()->unbind ();
119 }
120
121 void
122 KeyEditor::page_change (GtkNotebookPage*, guint)
123 {
124         current_tab()->view.get_selection()->unselect_all ();
125         unbind_button.set_sensitive (false);
126 }
127
128 bool
129 KeyEditor::on_key_press_event (GdkEventKey* ev)
130 {
131         if (!ev->is_modifier) {
132                 last_keyval = ev->keyval;
133         }
134
135         /* Don't let anything else handle the key press, because navigation
136          * keys will be used by GTK to change the selection/treeview cursor
137          * position
138          */
139
140         return true;
141 }
142
143 bool
144 KeyEditor::on_key_release_event (GdkEventKey* ev)
145 {
146         if (last_keyval == 0) {
147                 return false;
148         }
149
150         current_tab()->bind (ev, last_keyval);
151
152         last_keyval = 0;
153         return true;
154 }
155
156 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
157         : owner (ke)
158         , name (str)
159         , bindings (b)
160 {
161         model = TreeStore::create(columns);
162
163         view.set_model (model);
164         view.append_column (_("Action"), columns.name);
165         view.append_column (_("Shortcut"), columns.binding);
166         view.set_headers_visible (true);
167         view.set_headers_clickable (true);
168         view.get_selection()->set_mode (SELECTION_SINGLE);
169         view.set_reorderable (false);
170         view.set_size_request (500,300);
171         view.set_enable_search (false);
172         view.set_rules_hint (true);
173         view.set_name (X_("KeyEditorTree"));
174
175         view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &Tab::action_selected));
176
177         view.get_column(0)->set_sort_column (columns.name);
178         view.get_column(1)->set_sort_column (columns.binding);
179         model->set_sort_column (owner.sort_column,  owner.sort_type);
180         model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &Tab::sort_column_changed));
181
182         signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
183
184         scroller.add (view);
185         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
186
187         set_spacing (6);
188         set_border_width (12);
189         pack_start (scroller);
190 }
191
192 void
193 KeyEditor::Tab::action_selected ()
194 {
195         if (view.get_selection()->count_selected_rows() == 0) {
196                 return;
197         }
198
199         TreeModel::iterator i = view.get_selection()->get_selected();
200
201         owner.unbind_button.set_sensitive (false);
202
203         if (i != model->children().end()) {
204
205                 string path = (*i)[columns.path];
206
207                 if (!(*i)[columns.bindable]) {
208                         return;
209                 }
210
211                 string binding = (*i)[columns.binding];
212
213                 if (!binding.empty()) {
214                         owner.unbind_button.set_sensitive (true);
215                 }
216         }
217 }
218
219 void
220 KeyEditor::Tab::unbind ()
221 {
222         TreeModel::iterator i = view.get_selection()->get_selected();
223
224         owner.unbind_button.set_sensitive (false);
225
226         if (i != model->children().end()) {
227                 if (!(*i)[columns.bindable]) {
228                         return;
229                 }
230
231                 const std::string& action_path = (*i)[columns.path];
232
233                 bindings->remove (Gtkmm2ext::Bindings::Press,  action_path , true);
234                 (*i)[columns.binding] = string ();
235         }
236 }
237
238 void
239 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
240 {
241         TreeModel::iterator i = view.get_selection()->get_selected();
242
243         if (i == model->children().end()) {
244                 return;
245         }
246
247         string action_path = (*i)[columns.path];
248
249         if (!(*i)[columns.bindable]) {
250                 return;
251         }
252
253         GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
254         Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
255
256         if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
257                 bindings_collision_dialog (owner);
258                 return;
259         }
260
261         bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
262
263         if (result) {
264                 (*i)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
265                 owner.unbind_button.set_sensitive (true);
266         }
267 }
268
269 void
270 KeyEditor::Tab::populate ()
271 {
272         vector<string> paths;
273         vector<string> labels;
274         vector<string> tooltips;
275         vector<string> keys;
276         vector<Glib::RefPtr<Action> > actions;
277         typedef std::map<string,TreeIter> NodeMap;
278         NodeMap nodes;
279         NodeMap::iterator r;
280
281         bindings->get_all_actions (paths, labels, tooltips, keys, actions);
282
283         vector<string>::iterator k;
284         vector<string>::iterator p;
285         vector<string>::iterator t;
286         vector<string>::iterator l;
287         vector<Glib::RefPtr<Action> >::iterator a;
288
289         model->clear ();
290
291         for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
292
293                 TreeModel::Row row;
294                 vector<string> parts;
295
296                 split (*p, parts, '/');
297
298                 string category = parts[1];
299                 string action_name = parts[2];
300
301                 if (action_name.empty()) {
302                         continue;
303                 }
304
305                 //kinda kludgy way to avoid displaying menu items as mappable
306                 if ((action_name.find ("Menu") == action_name.length() - 4) ||
307                     (action_name.find ("menu") == action_name.length() - 4) ||
308                     (action_name == _("RegionList"))) {
309                         continue;
310                 }
311
312                 if ((r = nodes.find (category)) == nodes.end()) {
313
314                         /* category/group is missing, so add it first */
315
316                         TreeIter rowp;
317                         TreeModel::Row parent;
318                         rowp = model->append();
319                         nodes[category] = rowp;
320                         parent = *(rowp);
321                         parent[columns.name] = category;
322                         parent[columns.bindable] = false;
323                         parent[columns.action] = *a;
324
325                         /* now set up the child row that we're about to fill
326                          * out with information
327                          */
328
329                         row = *(model->append (parent.children()));
330
331                 } else {
332
333                         /* category/group is present, so just add the child row */
334
335                         row = *(model->append ((*r->second)->children()));
336
337                 }
338
339                 /* add this action */
340
341                 /* use the "visible label" as the action name */
342
343                 if (l->empty ()) {
344                         /* no label, try using the tooltip instead */
345                         row[columns.name] = *t;
346                 } else {
347                         row[columns.name] = *l;
348                 }
349                 row[columns.path] = string_compose ("%1/%2", category, action_name);
350                 row[columns.bindable] = true;
351
352                 if (*k == ActionManager::unbound_string) {
353                         row[columns.binding] = string();
354                 } else {
355                         row[columns.binding] = *k;
356                 }
357                 row[columns.action] = *a;
358         }
359 }
360
361 void
362 KeyEditor::Tab::sort_column_changed ()
363 {
364         int column;
365         SortType type;
366         if (model->get_sort_column_id (column, type)) {
367                 owner.sort_column = column;
368                 owner.sort_type = type;
369         }
370 }
371
372 void
373 KeyEditor::Tab::tab_mapped ()
374 {
375         model->set_sort_column (owner.sort_column,  owner.sort_type);
376 }
377
378 void
379 KeyEditor::reset ()
380 {
381         Keyboard::the_keyboard().reset_bindings ();
382
383         for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
384                 (*t)->view.get_selection()->unselect_all ();
385                 (*t)->populate ();
386         }
387 }
388
389 KeyEditor::Tab*
390 KeyEditor::current_tab ()
391 {
392         return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));
393 }