remove some uncharacteristic comments in code
[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         if (i == model->children().end()) {
242                 return;
243         }
244
245         string action_path = (*i)[columns.path];
246
247         if (!(*i)[columns.bindable]) {
248                 return;
249         }
250
251         GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
252         Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
253
254         if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
255                 bindings_collision_dialog (owner);
256                 return;
257         }
258
259         bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
260
261         if (result) {
262                 (*i)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
263                 owner.unbind_button.set_sensitive (true);
264         }
265 }
266
267 void
268 KeyEditor::Tab::populate ()
269 {
270         vector<string> paths;
271         vector<string> labels;
272         vector<string> tooltips;
273         vector<string> keys;
274         vector<Glib::RefPtr<Action> > actions;
275         typedef std::map<string,TreeIter> NodeMap;
276         NodeMap nodes;
277         NodeMap::iterator r;
278
279         bindings->get_all_actions (paths, labels, tooltips, keys, actions);
280
281         vector<string>::iterator k;
282         vector<string>::iterator p;
283         vector<string>::iterator t;
284         vector<string>::iterator l;
285         vector<Glib::RefPtr<Action> >::iterator a;
286
287         model->clear ();
288
289         for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
290
291                 TreeModel::Row row;
292                 vector<string> parts;
293
294                 split (*p, parts, '/');
295
296                 string category = parts[1];
297                 string action_name = parts[2];
298
299                 if (action_name.empty()) {
300                         continue;
301                 }
302
303                 //kinda kludgy way to avoid displaying menu items as mappable
304                 if ((action_name.find ("Menu") == action_name.length() - 4) ||
305                     (action_name.find ("menu") == action_name.length() - 4) ||
306                     (action_name == _("RegionList"))) {
307                         continue;
308                 }
309
310                 if ((r = nodes.find (category)) == nodes.end()) {
311
312                         /* category/group is missing, so add it first */
313
314                         TreeIter rowp;
315                         TreeModel::Row parent;
316                         rowp = model->append();
317                         nodes[category] = rowp;
318                         parent = *(rowp);
319                         parent[columns.name] = category;
320                         parent[columns.bindable] = false;
321                         parent[columns.action] = *a;
322
323                         /* now set up the child row that we're about to fill
324                          * out with information
325                          */
326
327                         row = *(model->append (parent.children()));
328
329                 } else {
330
331                         /* category/group is present, so just add the child row */
332
333                         row = *(model->append ((*r->second)->children()));
334
335                 }
336
337                 /* add this action */
338
339                 /* use the "visible label" as the action name */
340
341                 if (l->empty ()) {
342                         /* no label, try using the tooltip instead */
343                         row[columns.name] = *t;
344                 } else {
345                         row[columns.name] = *l;
346                 }
347                 row[columns.path] = string_compose ("%1/%2", category, action_name);
348                 row[columns.bindable] = true;
349
350                 if (*k == ActionManager::unbound_string) {
351                         row[columns.binding] = string();
352                 } else {
353                         row[columns.binding] = *k;
354                 }
355                 row[columns.action] = *a;
356         }
357 }
358
359 void
360 KeyEditor::Tab::sort_column_changed ()
361 {
362         int column;
363         SortType type;
364         if (model->get_sort_column_id (column, type)) {
365                 owner.sort_column = column;
366                 owner.sort_type = type;
367         }
368 }
369
370 void
371 KeyEditor::Tab::tab_mapped ()
372 {
373         model->set_sort_column (owner.sort_column,  owner.sort_type);
374 }
375
376 void
377 KeyEditor::reset ()
378 {
379         Keyboard::the_keyboard().reset_bindings ();
380
381         for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
382                 (*t)->view.get_selection()->unselect_all ();
383                 (*t)->populate ();
384         }
385 }
386
387 KeyEditor::Tab*
388 KeyEditor::current_tab ()
389 {
390         return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));
391 }