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