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 "ardour/profile.h"
28 #include <gtkmm/stock.h>
29 #include <gtkmm/label.h>
30 #include <gtkmm/accelkey.h>
31 #include <gtkmm/accelmap.h>
32 #include <gtkmm/uimanager.h>
34 #include "gtkmm2ext/utils.h"
36 #include "pbd/strsplit.h"
37 #include "pbd/replace_all.h"
39 #include "ardour/profile.h"
43 #include "keyeditor.h"
53 using Gtkmm2ext::Keyboard;
55 KeyEditor::KeyEditor ()
56 : ArdourWindow (_("Key Bindings"))
57 , unbind_button (_("Remove shortcut"))
58 , unbind_box (BUTTONBOX_END)
64 model = TreeStore::create(columns);
66 view.set_model (model);
67 view.append_column (_("Action"), columns.action);
68 view.append_column (_("Shortcut"), columns.binding);
69 view.set_headers_visible (true);
70 view.get_selection()->set_mode (SELECTION_SINGLE);
71 view.set_reorderable (false);
72 view.set_size_request (500,300);
73 view.set_enable_search (false);
74 view.set_rules_hint (true);
75 view.set_name (X_("KeyEditorTree"));
77 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &KeyEditor::action_selected));
80 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
84 if (!ARDOUR::Profile->get_sae()) {
86 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
88 unbind_box.set_spacing (6);
89 unbind_box.pack_start (*hint, false, true);
90 unbind_box.pack_start (unbind_button, false, false);
91 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
95 unbind_button.show ();
102 unbind_button.set_sensitive (false);
108 TreeModel::iterator i = view.get_selection()->get_selected();
110 unbind_button.set_sensitive (false);
112 cerr << "trying to unbind\n";
114 if (i != model->children().end()) {
115 string path = (*i)[columns.path];
117 if (!(*i)[columns.bindable]) {
121 bool result = AccelMap::change_entry (path,
126 (*i)[columns.binding] = string ();
132 KeyEditor::on_show ()
135 view.get_selection()->unselect_all ();
136 ArdourWindow::on_show ();
140 KeyEditor::on_unmap ()
142 ArdourWindow::on_unmap ();
146 KeyEditor::action_selected ()
148 if (view.get_selection()->count_selected_rows() == 0) {
152 TreeModel::iterator i = view.get_selection()->get_selected();
154 unbind_button.set_sensitive (false);
156 if (i != model->children().end()) {
158 string path = (*i)[columns.path];
160 if (!(*i)[columns.bindable]) {
164 string binding = (*i)[columns.binding];
166 if (!binding.empty()) {
167 unbind_button.set_sensitive (true);
173 KeyEditor::on_key_press_event (GdkEventKey* ev)
176 last_state = ev->state;
181 KeyEditor::on_key_release_event (GdkEventKey* ev)
183 if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
187 TreeModel::iterator i = view.get_selection()->get_selected();
189 if (i != model->children().end()) {
190 string path = (*i)[columns.path];
192 if (!(*i)[columns.bindable]) {
196 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
199 bool result = AccelMap::change_entry (path,
201 ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
206 (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
216 KeyEditor::populate ()
218 vector<string> paths;
219 vector<string> labels;
220 vector<string> tooltips;
222 vector<AccelKey> bindings;
223 typedef std::map<string,TreeIter> NodeMap;
227 ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
229 vector<string>::iterator k;
230 vector<string>::iterator p;
231 vector<string>::iterator t;
232 vector<string>::iterator l;
236 for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
239 vector<string> parts;
243 split (*p, parts, '/');
249 //kinda kludgy way to avoid displaying menu items as mappable
250 if ( parts[1] == _("Main_menu") )
252 if ( parts[1] == _("JACK") )
254 if ( parts[1] == _("redirectmenu") )
256 if ( parts[1] == _("Editor_menus") )
258 if ( parts[1] == _("RegionList") )
260 if ( parts[1] == _("ProcessorMenu") )
263 if ((r = nodes.find (parts[1])) == nodes.end()) {
265 /* top level is missing */
268 TreeModel::Row parent;
269 rowp = model->append();
270 nodes[parts[1]] = rowp;
272 parent[columns.action] = parts[1];
273 parent[columns.bindable] = false;
275 row = *(model->append (parent.children()));
279 row = *(model->append ((*r->second)->children()));
283 /* add this action */
286 row[columns.action] = *t;
288 row[columns.action] = *l;
290 row[columns.path] = (*p);
291 row[columns.bindable] = true;
293 if (*k == ActionManager::unbound_string) {
294 row[columns.binding] = string();
296 row[columns.binding] = (*k);