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