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