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