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>
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.
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.
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.
24 #include "gtk2ardour-config.h"
31 #include <boost/algorithm/string.hpp>
34 #include <glib/gstdio.h>
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>
44 #include "gtkmm2ext/bindings.h"
45 #include "gtkmm2ext/utils.h"
47 #include "pbd/error.h"
48 #include "pbd/openuri.h"
49 #include "pbd/strsplit.h"
51 #include "ardour/filesystem_paths.h"
52 #include "ardour/profile.h"
56 #include "keyeditor.h"
65 using Gtkmm2ext::Keyboard;
66 using Gtkmm2ext::Bindings;
68 sigc::signal<void> KeyEditor::UpdateBindings;
70 static void bindings_collision_dialog (Gtk::Window& parent, const std::string& bound_name)
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));
76 dialog.get_vbox()->pack_start (label, true, true);
77 dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
82 KeyEditor::KeyEditor ()
83 : ArdourWindow (_("Keyboard Shortcuts"))
84 , unbind_button (_("Remove shortcut"))
85 , unbind_box (BUTTONBOX_END)
86 , filter_entry (_("Search..."), true)
89 , sort_type(Gtk::SORT_ASCENDING)
92 notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
94 vpacker.pack_start (notebook, true, true);
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);
102 Label* hint = manage (new Label (_("To remove a shortcut, select an action then press this: ")));
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));
108 vpacker.set_spacing (4);
109 vpacker.pack_start (unbind_box, false, false);
111 unbind_button.show ();
113 reset_button.add (reset_label);
114 reset_label.set_markup (string_compose (" <span size=\"large\" weight=\"bold\">%1</span> ", _("Reset Bindings to Defaults")));
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)")));
119 print_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::print));
121 reset_box.pack_start (reset_button, true, false);
122 reset_box.pack_start (print_button, true, false);
124 reset_button.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);
133 unbind_button.set_sensitive (false);
134 _refresh_connection = UpdateBindings.connect (sigc::mem_fun (*this, &KeyEditor::refresh));
138 KeyEditor::add_tab (string const & name, Bindings& bindings)
140 Tab* t = new Tab (*this, name, &bindings);
142 if (t->populate () == 0) {
150 notebook.append_page (*t, name);
155 KeyEditor::remove_tab (string const &name)
157 guint npages = notebook.get_n_pages ();
159 for (guint n = 0; n < npages; ++n) {
160 Widget* w = notebook.get_nth_page (n);
161 Tab* tab = dynamic_cast<Tab*> (w);
163 if (tab->name == name) {
164 notebook.remove_page (*w);
169 cerr << "Removed " << name << endl;
175 current_tab()->unbind ();
179 KeyEditor::page_change (GtkNotebookPage*, guint)
181 current_tab()->view.get_selection()->unselect_all ();
182 unbind_button.set_sensitive (false);
186 KeyEditor::Tab::key_press_event (GdkEventKey* ev)
188 if (view.get_selection()->count_selected_rows() != 1) {
192 if (!ev->is_modifier) {
193 last_keyval = ev->keyval;
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
205 KeyEditor::Tab::key_release_event (GdkEventKey* ev)
207 if (view.get_selection()->count_selected_rows() != 1) {
211 if (last_keyval == 0) {
215 owner.current_tab()->bind (ev, last_keyval);
221 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
227 data_model = TreeStore::create(columns);
230 filter = TreeModelFilter::create(data_model);
231 filter->set_visible_func (sigc::mem_fun (*this, &Tab::visible_func));
233 sorted_filter = TreeModelSort::create(filter);
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"));
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);
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));
256 signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
259 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
262 set_border_width (12);
263 pack_start (scroller);
267 KeyEditor::Tab::action_selected ()
269 if (view.get_selection()->count_selected_rows() == 0) {
273 TreeModel::const_iterator it = view.get_selection()->get_selected();
279 if (!(*it)[columns.bindable]) {
280 owner.unbind_button.set_sensitive (false);
284 const string& binding = (*it)[columns.binding];
286 if (!binding.empty()) {
287 owner.unbind_button.set_sensitive (true);
292 KeyEditor::Tab::unbind ()
294 const std::string& action_path = (*view.get_selection()->get_selected())[columns.path];
296 TreeModel::iterator it = find_action_path (data_model->children().begin(), data_model->children().end(), action_path);
298 if (!it || !(*it)[columns.bindable]) {
302 bindings->remove (Gtkmm2ext::Bindings::Press, action_path , true);
303 (*it)[columns.binding] = string ();
305 owner.unbind_button.set_sensitive (false);
309 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
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);
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
318 pressed_key = gdk_keyval_to_lower (pressed_key);
320 if (!it || !(*it)[columns.bindable]) {
324 GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
325 Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
327 if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
328 bindings_collision_dialog (owner, bindings->bound_name (new_binding, Gtkmm2ext::Bindings::Press));
332 bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
335 (*it)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
336 owner.unbind_button.set_sensitive (true);
341 KeyEditor::Tab::populate ()
343 vector<string> paths;
344 vector<string> labels;
345 vector<string> tooltips;
347 vector<Glib::RefPtr<Action> > actions;
348 typedef std::map<string,TreeIter> NodeMap;
352 bindings->get_all_actions (paths, labels, tooltips, keys, actions);
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;
360 data_model->clear ();
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) {
365 vector<string> parts;
367 split (*p, parts, '/');
369 string category = parts[1];
370 string action_name = parts[2];
372 if (action_name.empty()) {
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"))) {
385 if ((r = nodes.find (category)) == nodes.end()) {
387 /* category/group is missing, so add it first */
390 TreeModel::Row parent;
391 rowp = data_model->append();
392 nodes[category] = rowp;
394 parent[columns.name] = category;
395 parent[columns.bindable] = false;
396 parent[columns.action] = *a;
398 /* now set up the child row that we're about to fill
399 * out with information
402 row = *(data_model->append (parent.children()));
406 /* category/group is present, so just add the child row */
408 row = *(data_model->append ((*r->second)->children()));
412 /* add this action */
414 /* use the "visible label" as the action name */
417 /* no label, try using the tooltip instead */
418 row[columns.name] = *t;
420 row[columns.name] = *l;
422 row[columns.path] = string_compose ("%1/%2", category, action_name);
423 row[columns.bindable] = true;
425 if (*k == ActionManager::unbound_string) {
426 row[columns.binding] = string();
428 row[columns.binding] = *k;
430 row[columns.action] = *a;
433 return data_model->children().size();
437 KeyEditor::Tab::sort_column_changed ()
441 if (data_model->get_sort_column_id (column, type)) {
442 owner.sort_column = column;
443 owner.sort_type = type;
448 KeyEditor::Tab::tab_mapped ()
450 data_model->set_sort_column (owner.sort_column, owner.sort_type);
455 KeyEditor::Tab::visible_func(const Gtk::TreeModel::const_iterator& iter) const
461 // never filter when search string is empty or item is a category
462 if (owner.filter_string.empty () || !(*iter)[columns.bindable]) {
467 std::string name = (*iter)[columns.name];
468 boost::to_lower (name);
469 if (name.find (owner.filter_string) != std::string::npos) {
474 std::string binding = (*iter)[columns.binding];
475 boost::to_lower (binding);
476 if (binding.find (owner.filter_string) != std::string::npos) {
484 KeyEditor::Tab::find_action_path (TreeModel::const_iterator begin, TreeModel::const_iterator end, const std::string& action_path) const
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()) {
497 const std::string& path = (*it)[columns.path];
498 if (action_path.compare(path) == 0) {
508 Keyboard::the_keyboard().reset_bindings ();
513 KeyEditor::refresh ()
515 for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
516 (*t)->view.get_selection()->unselect_all ();
522 KeyEditor::current_tab ()
524 return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));
528 KeyEditor::search_string_updated (const std::string& filter)
530 filter_string = boost::to_lower_copy(filter);
531 KeyEditor::Tab* tab = current_tab ();
533 tab->filter->refilter ();
538 KeyEditor::print () const
541 Bindings::save_all_bindings_as_html (sstr);
543 if (sstr.str().empty()) {
552 if ((fd = g_file_open_tmp ("akprintXXXXXX.html", &file_name, &err)) < 0) {
554 error << string_compose (_("Could not open temporary file to print bindings (%1)"), err->message) << endmsg;
560 #ifdef PLATFORM_WINDOWS
566 if (!g_file_set_contents (file_name, sstr.str().c_str(), sstr.str().size(), &err)) {
567 #ifndef PLATFORM_WINDOWS
570 g_unlink (file_name);
572 error << string_compose (_("Could not save bindings to file (%1)"), err->message) << endmsg;
578 #ifndef PLATFORM_WINDOWS
582 PBD::open_uri (string_compose ("file:///%1", file_name));