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