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/bindings.h"
33 #include "gtkmm2ext/utils.h"
35 #include "pbd/strsplit.h"
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/profile.h"
42 #include "keyeditor.h"
51 using Gtkmm2ext::Keyboard;
52 using Gtkmm2ext::Bindings;
54 void bindings_collision_dialog (Gtk::Window& parent)
56 ArdourDialog dialog (parent, _("Colliding keybindings"), true);
57 Label label (_("The key sequence is already bound. Please remove the other binding first."));
59 dialog.get_vbox()->pack_start (label, true, true);
60 dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
65 KeyEditor::KeyEditor ()
66 : ArdourWindow (_("Key Bindings"))
67 , unbind_button (_("Remove shortcut"))
68 , unbind_box (BUTTONBOX_END)
70 , sort_type(Gtk::SORT_ASCENDING)
74 notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
76 vpacker.pack_start (notebook, true, true);
78 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
80 unbind_box.set_spacing (6);
81 unbind_box.pack_start (*hint, false, true);
82 unbind_box.pack_start (unbind_button, false, false);
83 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
85 vpacker.pack_start (unbind_box, false, false);
87 unbind_button.show ();
89 reset_button.add (reset_label);
90 reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
92 reset_box.pack_start (reset_button, true, false);
96 reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
97 vpacker.pack_start (reset_box, false, false);
101 unbind_button.set_sensitive (false);
105 KeyEditor::add_tab (string const & name, Bindings& bindings)
107 Tab* t = new Tab (*this, name, &bindings);
110 notebook.append_page (*t, name);
116 current_tab()->unbind ();
120 KeyEditor::page_change (GtkNotebookPage*, guint)
122 current_tab()->view.get_selection()->unselect_all ();
123 unbind_button.set_sensitive (false);
127 KeyEditor::on_key_press_event (GdkEventKey* ev)
129 if (!ev->is_modifier) {
130 last_keyval = ev->keyval;
133 /* Don't let anything else handle the key press, because navigation
134 * keys will be used by GTK to change the selection/treeview cursor
142 KeyEditor::on_key_release_event (GdkEventKey* ev)
144 if (last_keyval == 0) {
148 current_tab()->bind (ev, last_keyval);
154 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
159 model = TreeStore::create(columns);
161 view.set_model (model);
162 view.append_column (_("Action"), columns.name);
163 view.append_column (_("Shortcut"), columns.binding);
164 view.set_headers_visible (true);
165 view.set_headers_clickable (true);
166 view.get_selection()->set_mode (SELECTION_SINGLE);
167 view.set_reorderable (false);
168 view.set_size_request (500,300);
169 view.set_enable_search (false);
170 view.set_rules_hint (true);
171 view.set_name (X_("KeyEditorTree"));
173 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &Tab::action_selected));
175 view.get_column(0)->set_sort_column (columns.name);
176 view.get_column(1)->set_sort_column (columns.binding);
177 model->set_sort_column (owner.sort_column, owner.sort_type);
178 model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &Tab::sort_column_changed));
180 signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
183 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
186 set_border_width (12);
187 pack_start (scroller);
191 KeyEditor::Tab::action_selected ()
193 if (view.get_selection()->count_selected_rows() == 0) {
197 TreeModel::iterator i = view.get_selection()->get_selected();
199 owner.unbind_button.set_sensitive (false);
201 if (i != model->children().end()) {
203 string path = (*i)[columns.path];
205 if (!(*i)[columns.bindable]) {
209 string binding = (*i)[columns.binding];
211 if (!binding.empty()) {
212 owner.unbind_button.set_sensitive (true);
218 KeyEditor::Tab::unbind ()
220 TreeModel::iterator i = view.get_selection()->get_selected();
222 owner.unbind_button.set_sensitive (false);
224 if (i != model->children().end()) {
225 if (!(*i)[columns.bindable]) {
229 const std::string& action_path = (*i)[columns.path];
231 bindings->remove (Gtkmm2ext::Bindings::Press, action_path , true);
232 (*i)[columns.binding] = string ();
237 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
239 TreeModel::iterator i = view.get_selection()->get_selected();
241 /* pressed key could be upper case if Shift was used. We want all
242 single keys stored as their lower-case version, so ensure this
245 pressed_key = gdk_keyval_to_lower (pressed_key);
247 if (i == model->children().end()) {
251 string action_path = (*i)[columns.path];
253 if (!(*i)[columns.bindable]) {
257 GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
258 Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
260 if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
261 bindings_collision_dialog (owner);
265 bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
268 (*i)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
269 owner.unbind_button.set_sensitive (true);
274 KeyEditor::Tab::populate ()
276 vector<string> paths;
277 vector<string> labels;
278 vector<string> tooltips;
280 vector<Glib::RefPtr<Action> > actions;
281 typedef std::map<string,TreeIter> NodeMap;
285 bindings->get_all_actions (paths, labels, tooltips, keys, actions);
287 vector<string>::iterator k;
288 vector<string>::iterator p;
289 vector<string>::iterator t;
290 vector<string>::iterator l;
291 vector<Glib::RefPtr<Action> >::iterator a;
295 for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
298 vector<string> parts;
300 split (*p, parts, '/');
302 string category = parts[1];
303 string action_name = parts[2];
305 if (action_name.empty()) {
309 //kinda kludgy way to avoid displaying menu items as mappable
310 if ((action_name.find ("Menu") == action_name.length() - 4) ||
311 (action_name.find ("menu") == action_name.length() - 4) ||
312 (action_name == _("RegionList"))) {
316 if ((r = nodes.find (category)) == nodes.end()) {
318 /* category/group is missing, so add it first */
321 TreeModel::Row parent;
322 rowp = model->append();
323 nodes[category] = rowp;
325 parent[columns.name] = category;
326 parent[columns.bindable] = false;
327 parent[columns.action] = *a;
329 /* now set up the child row that we're about to fill
330 * out with information
333 row = *(model->append (parent.children()));
337 /* category/group is present, so just add the child row */
339 row = *(model->append ((*r->second)->children()));
343 /* add this action */
345 /* use the "visible label" as the action name */
348 /* no label, try using the tooltip instead */
349 row[columns.name] = *t;
351 row[columns.name] = *l;
353 row[columns.path] = string_compose ("%1/%2", category, action_name);
354 row[columns.bindable] = true;
356 if (*k == ActionManager::unbound_string) {
357 row[columns.binding] = string();
359 row[columns.binding] = *k;
361 row[columns.action] = *a;
366 KeyEditor::Tab::sort_column_changed ()
370 if (model->get_sort_column_id (column, type)) {
371 owner.sort_column = column;
372 owner.sort_type = type;
377 KeyEditor::Tab::tab_mapped ()
379 model->set_sort_column (owner.sort_column, owner.sort_type);
385 Keyboard::the_keyboard().reset_bindings ();
387 for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
388 (*t)->view.get_selection()->unselect_all ();
394 KeyEditor::current_tab ()
396 return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));