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 if (i == model->children().end()) {
245 string action_path = (*i)[columns.path];
247 if (!(*i)[columns.bindable]) {
251 GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
252 Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
254 if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
255 bindings_collision_dialog (owner);
259 bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
262 (*i)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
263 owner.unbind_button.set_sensitive (true);
268 KeyEditor::Tab::populate ()
270 vector<string> paths;
271 vector<string> labels;
272 vector<string> tooltips;
274 vector<Glib::RefPtr<Action> > actions;
275 typedef std::map<string,TreeIter> NodeMap;
279 bindings->get_all_actions (paths, labels, tooltips, keys, actions);
281 vector<string>::iterator k;
282 vector<string>::iterator p;
283 vector<string>::iterator t;
284 vector<string>::iterator l;
285 vector<Glib::RefPtr<Action> >::iterator a;
289 for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
292 vector<string> parts;
294 split (*p, parts, '/');
296 string category = parts[1];
297 string action_name = parts[2];
299 if (action_name.empty()) {
303 //kinda kludgy way to avoid displaying menu items as mappable
304 if ((action_name.find ("Menu") == action_name.length() - 4) ||
305 (action_name.find ("menu") == action_name.length() - 4) ||
306 (action_name == _("RegionList"))) {
310 if ((r = nodes.find (category)) == nodes.end()) {
312 /* category/group is missing, so add it first */
315 TreeModel::Row parent;
316 rowp = model->append();
317 nodes[category] = rowp;
319 parent[columns.name] = category;
320 parent[columns.bindable] = false;
321 parent[columns.action] = *a;
323 /* now set up the child row that we're about to fill
324 * out with information
327 row = *(model->append (parent.children()));
331 /* category/group is present, so just add the child row */
333 row = *(model->append ((*r->second)->children()));
337 /* add this action */
339 /* use the "visible label" as the action name */
342 /* no label, try using the tooltip instead */
343 row[columns.name] = *t;
345 row[columns.name] = *l;
347 row[columns.path] = string_compose ("%1/%2", category, action_name);
348 row[columns.bindable] = true;
350 if (*k == ActionManager::unbound_string) {
351 row[columns.binding] = string();
353 row[columns.binding] = *k;
355 row[columns.action] = *a;
360 KeyEditor::Tab::sort_column_changed ()
364 if (model->get_sort_column_id (column, type)) {
365 owner.sort_column = column;
366 owner.sort_type = type;
371 KeyEditor::Tab::tab_mapped ()
373 model->set_sort_column (owner.sort_column, owner.sort_type);
379 Keyboard::the_keyboard().reset_bindings ();
381 for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
382 (*t)->view.get_selection()->unselect_all ();
388 KeyEditor::current_tab ()
390 return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));