2 Copyright (C) 2002 Paul Davis
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.
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.
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.
21 #include "gtk2ardour-config.h"
26 #include <gtkmm/stock.h>
27 #include <gtkmm/label.h>
28 #include <gtkmm/accelkey.h>
29 #include <gtkmm/accelmap.h>
30 #include <gtkmm/uimanager.h>
32 #include "gtkmm2ext/utils.h"
34 #include "pbd/strsplit.h"
36 #include "ardour/filesystem_paths.h"
37 #include "ardour/profile.h"
41 #include "keyeditor.h"
50 using Gtkmm2ext::Keyboard;
52 KeyEditor::KeyEditor ()
53 : ArdourWindow (_("Key Bindings"))
54 , unbind_button (_("Remove shortcut"))
55 , unbind_box (BUTTONBOX_END)
60 model = TreeStore::create(columns);
62 view.set_model (model);
63 view.append_column (_("Action"), columns.action);
64 view.append_column (_("Shortcut"), columns.binding);
65 view.set_headers_visible (true);
66 view.get_selection()->set_mode (SELECTION_SINGLE);
67 view.set_reorderable (false);
68 view.set_size_request (500,300);
69 view.set_enable_search (false);
70 view.set_rules_hint (true);
71 view.set_name (X_("KeyEditorTree"));
73 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &KeyEditor::action_selected));
76 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
78 vpacker.set_spacing (6);
79 vpacker.set_border_width (12);
80 vpacker.pack_start (scroller);
82 if (!ARDOUR::Profile->get_sae()) {
84 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
86 unbind_box.set_spacing (6);
87 unbind_box.pack_start (*hint, false, true);
88 unbind_box.pack_start (unbind_button, false, false);
89 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
91 vpacker.pack_start (unbind_box, false, false);
93 unbind_button.show ();
97 reset_button.add (reset_label);
98 reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
100 reset_box.pack_start (reset_button, true, false);
102 reset_button.show ();
104 reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
105 vpacker.pack_start (reset_box, false, false);
113 unbind_button.set_sensitive (false);
119 TreeModel::iterator i = view.get_selection()->get_selected();
121 unbind_button.set_sensitive (false);
123 if (i != model->children().end()) {
124 string path = (*i)[columns.path];
126 if (!(*i)[columns.bindable]) {
130 bool result = AccelMap::change_entry (path,
135 (*i)[columns.binding] = string ();
141 KeyEditor::on_show ()
144 view.get_selection()->unselect_all ();
145 ArdourWindow::on_show ();
149 KeyEditor::on_unmap ()
151 ArdourWindow::on_unmap ();
155 KeyEditor::action_selected ()
157 if (view.get_selection()->count_selected_rows() == 0) {
161 TreeModel::iterator i = view.get_selection()->get_selected();
163 unbind_button.set_sensitive (false);
165 if (i != model->children().end()) {
167 string path = (*i)[columns.path];
169 if (!(*i)[columns.bindable]) {
173 string binding = (*i)[columns.binding];
175 if (!binding.empty()) {
176 unbind_button.set_sensitive (true);
182 KeyEditor::on_key_press_event (GdkEventKey* ev)
184 if (!ev->is_modifier) {
185 last_keyval = ev->keyval;
187 return ArdourWindow::on_key_press_event (ev);
191 KeyEditor::on_key_release_event (GdkEventKey* ev)
193 if (ARDOUR::Profile->get_sae() || last_keyval == 0) {
197 TreeModel::iterator i = view.get_selection()->get_selected();
199 if (i != model->children().end()) {
200 string path = (*i)[columns.path];
202 if (!(*i)[columns.bindable]) {
206 GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & ev->state);
208 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
209 Gtkmm2ext::possibly_translate_mod_to_make_legal_accelerator (mod);
211 bool result = AccelMap::change_entry (path,
213 Gdk::ModifierType(mod),
218 (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
219 unbind_button.set_sensitive (true);
229 KeyEditor::populate ()
231 vector<string> paths;
232 vector<string> labels;
233 vector<string> tooltips;
235 vector<AccelKey> bindings;
236 typedef std::map<string,TreeIter> NodeMap;
240 ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
242 vector<string>::iterator k;
243 vector<string>::iterator p;
244 vector<string>::iterator t;
245 vector<string>::iterator l;
249 for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
252 vector<string> parts;
256 split (*p, parts, '/');
262 //kinda kludgy way to avoid displaying menu items as mappable
263 if ( parts[1] == _("Main_menu") )
265 if ( parts[1] == _("redirectmenu") )
267 if ( parts[1] == _("Editor_menus") )
269 if ( parts[1] == _("RegionList") )
271 if ( parts[1] == _("ProcessorMenu") )
274 if ((r = nodes.find (parts[1])) == nodes.end()) {
276 /* top level is missing */
279 TreeModel::Row parent;
280 rowp = model->append();
281 nodes[parts[1]] = rowp;
283 parent[columns.action] = parts[1];
284 parent[columns.bindable] = false;
286 row = *(model->append (parent.children()));
290 row = *(model->append ((*r->second)->children()));
294 /* add this action */
297 row[columns.action] = *t;
299 row[columns.action] = *l;
301 row[columns.path] = (*p);
302 row[columns.bindable] = true;
304 if (*k == ActionManager::unbound_string) {
305 row[columns.binding] = string();
307 row[columns.binding] = (*k);
315 Keyboard::the_keyboard().reset_bindings ();
317 view.get_selection()->unselect_all ();