extend API of key binding editor to allow for tab removal
[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 <boost/algorithm/string.hpp>
27
28 #include <gtkmm/stock.h>
29 #include <gtkmm/label.h>
30 #include <gtkmm/accelkey.h>
31 #include <gtkmm/accelmap.h>
32 #include <gtkmm/uimanager.h>
33
34 #include "gtkmm2ext/bindings.h"
35 #include "gtkmm2ext/utils.h"
36
37 #include "pbd/strsplit.h"
38
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/profile.h"
41
42 #include "actions.h"
43 #include "keyboard.h"
44 #include "keyeditor.h"
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace Gtk;
50 using namespace Gdk;
51 using namespace PBD;
52
53 using Gtkmm2ext::Keyboard;
54 using Gtkmm2ext::Bindings;
55
56 void bindings_collision_dialog (Gtk::Window& parent)
57 {
58         ArdourDialog dialog (parent, _("Colliding keybindings"), true);
59         Label label (_("The key sequence is already bound. Please remove the other binding first."));
60
61         dialog.get_vbox()->pack_start (label, true, true);
62         dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
63         dialog.show_all ();
64         dialog.run();
65 }
66
67 KeyEditor::KeyEditor ()
68         : ArdourWindow (_("Key Bindings"))
69         , unbind_button (_("Remove shortcut"))
70         , unbind_box (BUTTONBOX_END)
71         , filter_entry (_("Search..."), true)
72         , filter_string("")
73         , sort_column(0)
74         , sort_type(Gtk::SORT_ASCENDING)
75 {
76
77         notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
78
79         vpacker.pack_start (notebook, true, true);
80
81         Glib::RefPtr<Gdk::Pixbuf> icon = ARDOUR_UI_UTILS::get_icon ("search");
82         filter_entry.set_icon_from_pixbuf (icon);
83         filter_entry.set_icon_tooltip_text (_("Click to reset search string"));
84         filter_entry.signal_search_string_updated ().connect (sigc::mem_fun (*this, &KeyEditor::search_string_updated));
85         vpacker.pack_start (filter_entry, false, false);
86
87         Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
88         hint->show ();
89         unbind_box.set_spacing (6);
90         unbind_box.pack_start (*hint, false, true);
91         unbind_box.pack_start (unbind_button, false, false);
92         unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
93
94         vpacker.pack_start (unbind_box, false, false);
95         unbind_box.show ();
96         unbind_button.show ();
97
98         reset_button.add (reset_label);
99         reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
100
101         reset_box.pack_start (reset_button, true, false);
102         reset_box.show ();
103         reset_button.show ();
104         reset_label.show ();
105         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
106         vpacker.pack_start (reset_box, false, false);
107
108         add (vpacker);
109
110         unbind_button.set_sensitive (false);
111 }
112
113 void
114 KeyEditor::add_tab (string const & name, Bindings& bindings)
115 {
116         Tab* t = new Tab (*this, name, &bindings);
117         t->populate ();
118         t->show_all ();
119         notebook.append_page (*t, name);
120 }
121
122
123 void
124 KeyEditor::remove_tab (string const &name)
125 {
126         guint npages = notebook.get_n_pages ();
127
128         for (guint n = 0; n < npages; ++n) {
129                 Widget* w = notebook.get_nth_page (n);
130                 Tab* tab = dynamic_cast<Tab*> (w);
131                 if (tab) {
132                         if (tab->name == name) {
133                                 notebook.remove_page (*w);
134                                 return;
135                         }
136                 }
137         }
138 }
139
140 void
141 KeyEditor::unbind ()
142 {
143         current_tab()->unbind ();
144 }
145
146 void
147 KeyEditor::page_change (GtkNotebookPage*, guint)
148 {
149         current_tab()->view.get_selection()->unselect_all ();
150         unbind_button.set_sensitive (false);
151 }
152
153 bool
154 KeyEditor::Tab::on_key_press_event (GdkEventKey* ev)
155 {
156         if (!ev->is_modifier) {
157                 last_keyval = ev->keyval;
158         }
159
160         /* Don't let anything else handle the key press, because navigation
161          * keys will be used by GTK to change the selection/treeview cursor
162          * position
163          */
164
165         return true;
166 }
167
168 bool
169 KeyEditor::Tab::on_key_release_event (GdkEventKey* ev)
170 {
171         if (last_keyval == 0) {
172                 return false;
173         }
174
175         owner.current_tab()->bind (ev, last_keyval);
176
177         last_keyval = 0;
178         return true;
179 }
180
181 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
182         : owner (ke)
183         , name (str)
184         , bindings (b)
185         , last_keyval (0)
186 {
187         data_model = TreeStore::create(columns);
188         populate ();
189         
190         filter = TreeModelFilter::create(data_model);
191         filter->set_visible_func (sigc::mem_fun (*this, &Tab::visible_func));
192
193         sorted_filter = TreeModelSort::create(filter);
194
195         view.set_model (sorted_filter);
196         view.append_column (_("Action"), columns.name);
197         view.append_column (_("Shortcut"), columns.binding);
198         view.set_headers_visible (true);
199         view.set_headers_clickable (true);
200         view.get_selection()->set_mode (SELECTION_SINGLE);
201         view.set_reorderable (false);
202         view.set_size_request (500,300);
203         view.set_enable_search (false);
204         view.set_rules_hint (true);
205         view.set_name (X_("KeyEditorTree"));
206
207         view.signal_cursor_changed().connect (sigc::mem_fun (*this, &Tab::action_selected));
208
209         view.get_column(0)->set_sort_column (columns.name);
210         view.get_column(1)->set_sort_column (columns.binding);
211         data_model->set_sort_column (owner.sort_column,  owner.sort_type);
212         data_model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &Tab::sort_column_changed));
213
214         signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
215
216         scroller.add (view);
217         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
218
219         set_spacing (6);
220         set_border_width (12);
221         pack_start (scroller);
222 }
223
224 void
225 KeyEditor::Tab::action_selected ()
226 {
227         if (view.get_selection()->count_selected_rows() == 0) {
228                 return;
229         }
230
231         TreeModel::const_iterator it = view.get_selection()->get_selected();
232
233         if (!it) {
234                 return;
235         }
236
237         if (!(*it)[columns.bindable]) {
238                 owner.unbind_button.set_sensitive (false);
239                 return;
240         }
241
242         const string& binding = (*it)[columns.binding];
243
244         if (!binding.empty()) {
245                 owner.unbind_button.set_sensitive (true);
246         }
247 }
248
249 void
250 KeyEditor::Tab::unbind ()
251 {
252         const std::string& action_path = (*view.get_selection()->get_selected())[columns.path];
253
254         TreeModel::iterator it = find_action_path (data_model->children().begin(), data_model->children().end(),  action_path);
255
256         if (!it || !(*it)[columns.bindable]) {
257                 return;
258         }
259
260         bindings->remove (Gtkmm2ext::Bindings::Press,  action_path , true);
261         (*it)[columns.binding] = string ();
262
263         owner.unbind_button.set_sensitive (false);
264 }
265
266 void
267 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
268 {
269         const std::string& action_path = (*view.get_selection()->get_selected())[columns.path];
270         TreeModel::iterator it = find_action_path (data_model->children().begin(), data_model->children().end(),  action_path);
271
272         /* pressed key could be upper case if Shift was used. We want all
273            single keys stored as their lower-case version, so ensure this
274         */
275
276         pressed_key = gdk_keyval_to_lower (pressed_key);
277
278         if (!it || !(*it)[columns.bindable]) {
279                 return;
280         }
281
282         GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
283         Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
284
285         if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
286                 bindings_collision_dialog (owner);
287                 return;
288         }
289
290         bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
291
292         if (result) {
293                 (*it)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
294                 owner.unbind_button.set_sensitive (true);
295         }
296 }
297
298 void
299 KeyEditor::Tab::populate ()
300 {
301         vector<string> paths;
302         vector<string> labels;
303         vector<string> tooltips;
304         vector<string> keys;
305         vector<Glib::RefPtr<Action> > actions;
306         typedef std::map<string,TreeIter> NodeMap;
307         NodeMap nodes;
308         NodeMap::iterator r;
309
310         bindings->get_all_actions (paths, labels, tooltips, keys, actions);
311
312         vector<string>::iterator k;
313         vector<string>::iterator p;
314         vector<string>::iterator t;
315         vector<string>::iterator l;
316         vector<Glib::RefPtr<Action> >::iterator a;
317
318         data_model->clear ();
319
320         for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
321
322                 TreeModel::Row row;
323                 vector<string> parts;
324
325                 split (*p, parts, '/');
326
327                 string category = parts[1];
328                 string action_name = parts[2];
329
330                 if (action_name.empty()) {
331                         continue;
332                 }
333
334                 //kinda kludgy way to avoid displaying menu items as mappable
335                 if ((action_name.find ("Menu") == action_name.length() - 4) ||
336                     (action_name.find ("menu") == action_name.length() - 4) ||
337                     (action_name == _("RegionList"))) {
338                         continue;
339                 }
340
341                 if ((r = nodes.find (category)) == nodes.end()) {
342
343                         /* category/group is missing, so add it first */
344
345                         TreeIter rowp;
346                         TreeModel::Row parent;
347                         rowp = data_model->append();
348                         nodes[category] = rowp;
349                         parent = *(rowp);
350                         parent[columns.name] = category;
351                         parent[columns.bindable] = false;
352                         parent[columns.action] = *a;
353
354                         /* now set up the child row that we're about to fill
355                          * out with information
356                          */
357
358                         row = *(data_model->append (parent.children()));
359
360                 } else {
361
362                         /* category/group is present, so just add the child row */
363
364                         row = *(data_model->append ((*r->second)->children()));
365
366                 }
367
368                 /* add this action */
369
370                 /* use the "visible label" as the action name */
371
372                 if (l->empty ()) {
373                         /* no label, try using the tooltip instead */
374                         row[columns.name] = *t;
375                 } else {
376                         row[columns.name] = *l;
377                 }
378                 row[columns.path] = string_compose ("%1/%2", category, action_name);
379                 row[columns.bindable] = true;
380
381                 if (*k == ActionManager::unbound_string) {
382                         row[columns.binding] = string();
383                 } else {
384                         row[columns.binding] = *k;
385                 }
386                 row[columns.action] = *a;
387         }
388 }
389
390 void
391 KeyEditor::Tab::sort_column_changed ()
392 {
393         int column;
394         SortType type;
395         if (data_model->get_sort_column_id (column, type)) {
396                 owner.sort_column = column;
397                 owner.sort_type = type;
398         }
399 }
400
401 void
402 KeyEditor::Tab::tab_mapped ()
403 {
404         data_model->set_sort_column (owner.sort_column,  owner.sort_type);
405         filter->refilter ();
406 }
407
408 bool
409 KeyEditor::Tab::visible_func(const Gtk::TreeModel::const_iterator& iter) const
410 {
411         if (!iter) {
412                 return false;
413         }
414
415         // never filter when search string is empty or item is a category
416         if (owner.filter_string.empty () || !(*iter)[columns.bindable]) {
417                 return true;
418         }
419
420         // search name
421         std::string name = (*iter)[columns.name];
422         boost::to_lower (name);
423         if (name.find (owner.filter_string) != std::string::npos) {
424                 return true;
425         }
426
427         // search binding
428         std::string binding = (*iter)[columns.binding];
429         boost::to_lower (binding);
430         if (binding.find (owner.filter_string) != std::string::npos) {
431                 return true;
432         }
433
434         return false;
435 }
436
437 TreeModel::iterator
438 KeyEditor::Tab::find_action_path (TreeModel::const_iterator begin, TreeModel::const_iterator end, const std::string& action_path) const
439 {
440         if (!begin) {
441                 return end;
442         }
443
444         for (TreeModel::iterator it = begin; it != end; ++it) {
445                 if (it->children()) {
446                         TreeModel::iterator jt = find_action_path (it->children().begin(), it->children().end(), action_path);
447                         if (jt != it->children().end()) {
448                                 return jt;
449                         }
450                 }
451                 const std::string& path = (*it)[columns.path];
452                 if (action_path.compare(path) == 0) {
453                         return it;
454                 }
455         }
456         return end;
457 }
458
459 void
460 KeyEditor::reset ()
461 {
462         Keyboard::the_keyboard().reset_bindings ();
463
464         for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
465                 (*t)->view.get_selection()->unselect_all ();
466                 (*t)->populate ();
467         }
468 }
469
470 KeyEditor::Tab*
471 KeyEditor::current_tab ()
472 {
473         return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));
474 }
475
476 void
477 KeyEditor::search_string_updated (const std::string& filter)
478 {
479         filter_string = boost::to_lower_copy(filter);
480         current_tab ()->filter->refilter ();
481 }
482