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