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