3 #include "ardour/profile.h"
5 #include <gtkmm/stock.h>
6 #include <gtkmm/label.h>
7 #include <gtkmm/accelkey.h>
8 #include <gtkmm/accelmap.h>
9 #include <gtkmm/uimanager.h>
11 #include "pbd/strsplit.h"
12 #include "pbd/replace_all.h"
14 #include "ardour/profile.h"
18 #include "keyeditor.h"
28 KeyEditor::KeyEditor ()
29 : ArdourDialog (_("Key Bindings"), false)
30 , unbind_button (_("Remove shortcut"))
31 , unbind_box (BUTTONBOX_END)
37 model = TreeStore::create(columns);
39 view.set_model (model);
40 view.append_column (_("Action"), columns.action);
41 view.append_column (_("Shortcut"), columns.binding);
42 view.set_headers_visible (true);
43 view.get_selection()->set_mode (SELECTION_SINGLE);
44 view.set_reorderable (false);
45 view.set_size_request (500,300);
46 view.set_enable_search (false);
47 view.set_rules_hint (true);
48 view.set_name (X_("KeyEditorTree"));
50 view.get_selection()->signal_changed().connect (mem_fun (*this, &KeyEditor::action_selected));
53 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
56 get_vbox()->set_spacing (6);
57 get_vbox()->pack_start (scroller);
59 if (!ARDOUR::Profile->get_sae()) {
61 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
63 unbind_box.set_spacing (6);
64 unbind_box.pack_start (*hint, false, true);
65 unbind_box.pack_start (unbind_button, false, false);
66 unbind_button.signal_clicked().connect (mem_fun (*this, &KeyEditor::unbind));
68 get_vbox()->pack_start (unbind_box, false, false);
70 unbind_button.show ();
74 get_vbox()->set_border_width (12);
79 unbind_button.set_sensitive (false);
85 TreeModel::iterator i = view.get_selection()->get_selected();
87 unbind_button.set_sensitive (false);
89 cerr << "trying to unbind\n";
91 if (i != model->children().end()) {
92 string path = (*i)[columns.path];
94 if (!(*i)[columns.bindable]) {
98 bool result = AccelMap::change_entry (path,
103 (*i)[columns.binding] = string ();
109 KeyEditor::on_show ()
112 view.get_selection()->unselect_all ();
113 ArdourDialog::on_show ();
117 KeyEditor::on_unmap ()
119 ArdourDialog::on_unmap ();
123 KeyEditor::action_selected ()
125 if (view.get_selection()->count_selected_rows() == 0) {
129 TreeModel::iterator i = view.get_selection()->get_selected();
131 unbind_button.set_sensitive (false);
133 if (i != model->children().end()) {
135 string path = (*i)[columns.path];
137 if (!(*i)[columns.bindable]) {
141 string binding = (*i)[columns.binding];
143 if (!binding.empty()) {
144 unbind_button.set_sensitive (true);
150 KeyEditor::on_key_press_event (GdkEventKey* ev)
153 last_state = ev->state;
158 KeyEditor::on_key_release_event (GdkEventKey* ev)
160 if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
164 TreeModel::iterator i = view.get_selection()->get_selected();
166 if (i != model->children().end()) {
167 string path = (*i)[columns.path];
169 if (!(*i)[columns.bindable]) {
173 cerr << "real lkeyval: " << ev->keyval << endl;
174 possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
175 cerr << "using keyval = " << ev->keyval << endl;
178 bool result = AccelMap::change_entry (path,
180 ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
183 cerr << "New binding to " << ev->keyval << " worked: " << result << endl;
187 (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
197 KeyEditor::populate ()
199 vector<string> paths;
200 vector<string> labels;
202 vector<AccelKey> bindings;
203 typedef std::map<string,TreeIter> NodeMap;
207 ActionManager::get_all_actions (labels, paths, keys, bindings);
209 vector<string>::iterator k;
210 vector<string>::iterator p;
211 vector<string>::iterator l;
215 for (l = labels.begin(), k = keys.begin(), p = paths.begin(); l != labels.end(); ++k, ++p, ++l) {
218 vector<string> parts;
222 split (*p, parts, '/');
228 if ((r = nodes.find (parts[1])) == nodes.end()) {
230 /* top level is missing */
233 TreeModel::Row parent;
234 rowp = model->append();
235 nodes[parts[1]] = rowp;
237 parent[columns.action] = parts[1];
238 parent[columns.bindable] = false;
240 row = *(model->append (parent.children()));
244 row = *(model->append ((*r->second)->children()));
248 /* add this action */
250 row[columns.action] = (*l);
251 row[columns.path] = (*p);
252 row[columns.bindable] = true;
254 if (*k == ActionManager::unbound_string) {
255 row[columns.binding] = string();
262 NSAlternate/NSOption key to Mod1
263 NSCommand key to Meta
266 replace_all (label, "<Meta>", _("Command-"));
267 replace_all (label, "<Alt>", _("Option-"));
268 replace_all (label, "<Shift>", _("Shift-"));
269 replace_all (label, "<Control>", _("Control-"));
270 row[columns.binding] = label;
272 row[columns.binding] = (*k);