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 using Gtkmm2ext::Keyboard;
30 KeyEditor::KeyEditor ()
31 : ArdourDialog (_("Key Bindings"), false)
32 , unbind_button (_("Remove shortcut"))
33 , unbind_box (BUTTONBOX_END)
39 model = TreeStore::create(columns);
41 view.set_model (model);
42 view.append_column (_("Action"), columns.action);
43 view.append_column (_("Shortcut"), columns.binding);
44 view.set_headers_visible (true);
45 view.get_selection()->set_mode (SELECTION_SINGLE);
46 view.set_reorderable (false);
47 view.set_size_request (500,300);
48 view.set_enable_search (false);
49 view.set_rules_hint (true);
50 view.set_name (X_("KeyEditorTree"));
52 view.get_selection()->signal_changed().connect (mem_fun (*this, &KeyEditor::action_selected));
55 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
58 get_vbox()->set_spacing (6);
59 get_vbox()->pack_start (scroller);
61 if (!ARDOUR::Profile->get_sae()) {
63 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
65 unbind_box.set_spacing (6);
66 unbind_box.pack_start (*hint, false, true);
67 unbind_box.pack_start (unbind_button, false, false);
68 unbind_button.signal_clicked().connect (mem_fun (*this, &KeyEditor::unbind));
70 get_vbox()->pack_start (unbind_box, false, false);
72 unbind_button.show ();
76 get_vbox()->set_border_width (12);
81 unbind_button.set_sensitive (false);
87 TreeModel::iterator i = view.get_selection()->get_selected();
89 unbind_button.set_sensitive (false);
91 cerr << "trying to unbind\n";
93 if (i != model->children().end()) {
94 string path = (*i)[columns.path];
96 if (!(*i)[columns.bindable]) {
100 bool result = AccelMap::change_entry (path,
105 (*i)[columns.binding] = string ();
111 KeyEditor::on_show ()
114 view.get_selection()->unselect_all ();
115 ArdourDialog::on_show ();
119 KeyEditor::on_unmap ()
121 ArdourDialog::on_unmap ();
125 KeyEditor::action_selected ()
127 if (view.get_selection()->count_selected_rows() == 0) {
131 TreeModel::iterator i = view.get_selection()->get_selected();
133 unbind_button.set_sensitive (false);
135 if (i != model->children().end()) {
137 string path = (*i)[columns.path];
139 if (!(*i)[columns.bindable]) {
143 string binding = (*i)[columns.binding];
145 if (!binding.empty()) {
146 unbind_button.set_sensitive (true);
152 KeyEditor::on_key_press_event (GdkEventKey* ev)
155 last_state = ev->state;
160 KeyEditor::on_key_release_event (GdkEventKey* ev)
162 if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
166 TreeModel::iterator i = view.get_selection()->get_selected();
168 if (i != model->children().end()) {
169 string path = (*i)[columns.path];
171 if (!(*i)[columns.bindable]) {
175 cerr << "real lkeyval: " << ev->keyval << endl;
176 possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
177 cerr << "using keyval = " << ev->keyval << endl;
180 bool result = AccelMap::change_entry (path,
182 ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
185 cerr << "New binding to " << ev->keyval << " worked: " << result << endl;
189 (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
199 KeyEditor::populate ()
201 vector<string> paths;
202 vector<string> labels;
204 vector<AccelKey> bindings;
205 typedef std::map<string,TreeIter> NodeMap;
209 ActionManager::get_all_actions (labels, paths, keys, bindings);
211 vector<string>::iterator k;
212 vector<string>::iterator p;
213 vector<string>::iterator l;
217 for (l = labels.begin(), k = keys.begin(), p = paths.begin(); l != labels.end(); ++k, ++p, ++l) {
220 vector<string> parts;
224 split (*p, parts, '/');
230 if ((r = nodes.find (parts[1])) == nodes.end()) {
232 /* top level is missing */
235 TreeModel::Row parent;
236 rowp = model->append();
237 nodes[parts[1]] = rowp;
239 parent[columns.action] = parts[1];
240 parent[columns.bindable] = false;
242 row = *(model->append (parent.children()));
246 row = *(model->append ((*r->second)->children()));
250 /* add this action */
252 row[columns.action] = (*l);
253 row[columns.path] = (*p);
254 row[columns.bindable] = true;
256 if (*k == ActionManager::unbound_string) {
257 row[columns.binding] = string();
264 NSAlternate/NSOption key to Mod1
265 NSCommand key to Meta
268 replace_all (label, "<Meta>", _("Command-"));
269 replace_all (label, "<Alt>", _("Option-"));
270 replace_all (label, "<Shift>", _("Shift-"));
271 replace_all (label, "<Control>", _("Control-"));
272 row[columns.binding] = label;
274 row[columns.binding] = (*k);