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"
35 #include "pbd/replace_all.h"
37 #include "ardour/profile.h"
41 #include "keyeditor.h"
51 using Gtkmm2ext::Keyboard;
53 KeyEditor::KeyEditor ()
54 : ArdourWindow (_("Key Bindings"))
55 , unbind_button (_("Remove shortcut"))
56 , unbind_box (BUTTONBOX_END)
62 model = TreeStore::create(columns);
64 view.set_model (model);
65 view.append_column (_("Action"), columns.action);
66 view.append_column (_("Shortcut"), columns.binding);
67 view.set_headers_visible (true);
68 view.get_selection()->set_mode (SELECTION_SINGLE);
69 view.set_reorderable (false);
70 view.set_size_request (500,300);
71 view.set_enable_search (false);
72 view.set_rules_hint (true);
73 view.set_name (X_("KeyEditorTree"));
75 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &KeyEditor::action_selected));
78 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
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));
93 unbind_button.show ();
100 unbind_button.set_sensitive (false);
106 TreeModel::iterator i = view.get_selection()->get_selected();
108 unbind_button.set_sensitive (false);
110 if (i != model->children().end()) {
111 string path = (*i)[columns.path];
113 if (!(*i)[columns.bindable]) {
117 bool result = AccelMap::change_entry (path,
122 (*i)[columns.binding] = string ();
128 KeyEditor::on_show ()
131 view.get_selection()->unselect_all ();
132 ArdourWindow::on_show ();
136 KeyEditor::on_unmap ()
138 ArdourWindow::on_unmap ();
142 KeyEditor::action_selected ()
144 if (view.get_selection()->count_selected_rows() == 0) {
148 TreeModel::iterator i = view.get_selection()->get_selected();
150 unbind_button.set_sensitive (false);
152 if (i != model->children().end()) {
154 string path = (*i)[columns.path];
156 if (!(*i)[columns.bindable]) {
160 string binding = (*i)[columns.binding];
162 if (!binding.empty()) {
163 unbind_button.set_sensitive (true);
169 KeyEditor::on_key_press_event (GdkEventKey* ev)
172 last_state = ev->state;
177 KeyEditor::on_key_release_event (GdkEventKey* ev)
179 if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
183 TreeModel::iterator i = view.get_selection()->get_selected();
185 if (i != model->children().end()) {
186 string path = (*i)[columns.path];
188 if (!(*i)[columns.bindable]) {
192 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
195 bool result = AccelMap::change_entry (path,
197 ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
202 (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
212 KeyEditor::populate ()
214 vector<string> paths;
215 vector<string> labels;
216 vector<string> tooltips;
218 vector<AccelKey> bindings;
219 typedef std::map<string,TreeIter> NodeMap;
223 ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
225 vector<string>::iterator k;
226 vector<string>::iterator p;
227 vector<string>::iterator t;
228 vector<string>::iterator l;
232 for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
235 vector<string> parts;
239 split (*p, parts, '/');
245 //kinda kludgy way to avoid displaying menu items as mappable
246 if ( parts[1] == _("Main_menu") )
248 if ( parts[1] == _("JACK") )
250 if ( parts[1] == _("redirectmenu") )
252 if ( parts[1] == _("Editor_menus") )
254 if ( parts[1] == _("RegionList") )
256 if ( parts[1] == _("ProcessorMenu") )
259 if ((r = nodes.find (parts[1])) == nodes.end()) {
261 /* top level is missing */
264 TreeModel::Row parent;
265 rowp = model->append();
266 nodes[parts[1]] = rowp;
268 parent[columns.action] = parts[1];
269 parent[columns.bindable] = false;
271 row = *(model->append (parent.children()));
275 row = *(model->append ((*r->second)->children()));
279 /* add this action */
282 row[columns.action] = *t;
284 row[columns.action] = *l;
286 row[columns.path] = (*p);
287 row[columns.bindable] = true;
289 if (*k == ActionManager::unbound_string) {
290 row[columns.binding] = string();
292 row[columns.binding] = (*k);