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