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 /*========================================== HELPER =========================================*/
55 void bindings_collision_dialog (Gtk::Window& parent)
57 ArdourDialog dialog (parent, _("Colliding keybindings"), true);
58 Label label (_("The key sequence is already bound. Please remove the other binding first."));
60 dialog.get_vbox()->pack_start (label, true, true);
61 dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
66 /*======================================== KEYEDITOR =========================================*/
67 KeyEditor::KeyEditor ()
68 : ArdourWindow (_("Key Bindings"))
69 , unbind_button (_("Remove shortcut"))
70 , unbind_box (BUTTONBOX_END)
72 , sort_type(Gtk::SORT_ASCENDING)
76 notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
78 vpacker.pack_start (notebook, true, true);
80 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
82 unbind_box.set_spacing (6);
83 unbind_box.pack_start (*hint, false, true);
84 unbind_box.pack_start (unbind_button, false, false);
85 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
87 vpacker.pack_start (unbind_box, false, false);
89 unbind_button.show ();
91 reset_button.add (reset_label);
92 reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
94 reset_box.pack_start (reset_button, true, false);
98 reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
99 vpacker.pack_start (reset_box, false, false);
103 unbind_button.set_sensitive (false);
107 KeyEditor::add_tab (string const & name, Bindings& bindings)
109 Tab* t = new Tab (*this, name, &bindings);
112 notebook.append_page (*t, name);
118 current_tab()->unbind ();
122 KeyEditor::page_change (GtkNotebookPage*, guint)
124 current_tab()->view.get_selection()->unselect_all ();
125 unbind_button.set_sensitive (false);
129 KeyEditor::on_key_press_event (GdkEventKey* ev)
131 if (!ev->is_modifier) {
132 last_keyval = ev->keyval;
135 /* Don't let anything else handle the key press, because navigation
136 * keys will be used by GTK to change the selection/treeview cursor
144 KeyEditor::on_key_release_event (GdkEventKey* ev)
146 if (last_keyval == 0) {
150 current_tab()->bind (ev, last_keyval);
156 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
161 model = TreeStore::create(columns);
163 view.set_model (model);
164 view.append_column (_("Action"), columns.name);
165 view.append_column (_("Shortcut"), columns.binding);
166 view.set_headers_visible (true);
167 view.set_headers_clickable (true);
168 view.get_selection()->set_mode (SELECTION_SINGLE);
169 view.set_reorderable (false);
170 view.set_size_request (500,300);
171 view.set_enable_search (false);
172 view.set_rules_hint (true);
173 view.set_name (X_("KeyEditorTree"));
175 view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &Tab::action_selected));
177 view.get_column(0)->set_sort_column (columns.name);
178 view.get_column(1)->set_sort_column (columns.binding);
179 model->set_sort_column (owner.sort_column, owner.sort_type);
180 model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &Tab::sort_column_changed));
182 signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
185 scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
188 set_border_width (12);
189 pack_start (scroller);
193 KeyEditor::Tab::action_selected ()
195 if (view.get_selection()->count_selected_rows() == 0) {
199 TreeModel::iterator i = view.get_selection()->get_selected();
201 owner.unbind_button.set_sensitive (false);
203 if (i != model->children().end()) {
205 string path = (*i)[columns.path];
207 if (!(*i)[columns.bindable]) {
211 string binding = (*i)[columns.binding];
213 if (!binding.empty()) {
214 owner.unbind_button.set_sensitive (true);
220 KeyEditor::Tab::unbind ()
222 TreeModel::iterator i = view.get_selection()->get_selected();
224 owner.unbind_button.set_sensitive (false);
226 if (i != model->children().end()) {
227 if (!(*i)[columns.bindable]) {
231 const std::string& action_path = (*i)[columns.path];
233 bindings->remove (Gtkmm2ext::Bindings::Press, action_path , true);
234 (*i)[columns.binding] = string ();
239 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
241 TreeModel::iterator i = view.get_selection()->get_selected();
243 if (i == model->children().end()) {
247 string action_path = (*i)[columns.path];
249 if (!(*i)[columns.bindable]) {
253 GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
254 Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
256 if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
257 bindings_collision_dialog (owner);
261 bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
264 (*i)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
265 owner.unbind_button.set_sensitive (true);
270 KeyEditor::Tab::populate ()
272 vector<string> paths;
273 vector<string> labels;
274 vector<string> tooltips;
276 vector<Glib::RefPtr<Action> > actions;
277 typedef std::map<string,TreeIter> NodeMap;
281 bindings->get_all_actions (paths, labels, tooltips, keys, actions);
283 vector<string>::iterator k;
284 vector<string>::iterator p;
285 vector<string>::iterator t;
286 vector<string>::iterator l;
287 vector<Glib::RefPtr<Action> >::iterator a;
291 for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
294 vector<string> parts;
296 split (*p, parts, '/');
298 string category = parts[1];
299 string action_name = parts[2];
301 if (action_name.empty()) {
305 //kinda kludgy way to avoid displaying menu items as mappable
306 if ((action_name.find ("Menu") == action_name.length() - 4) ||
307 (action_name.find ("menu") == action_name.length() - 4) ||
308 (action_name == _("RegionList"))) {
312 if ((r = nodes.find (category)) == nodes.end()) {
314 /* category/group is missing, so add it first */
317 TreeModel::Row parent;
318 rowp = model->append();
319 nodes[category] = rowp;
321 parent[columns.name] = category;
322 parent[columns.bindable] = false;
323 parent[columns.action] = *a;
325 /* now set up the child row that we're about to fill
326 * out with information
329 row = *(model->append (parent.children()));
333 /* category/group is present, so just add the child row */
335 row = *(model->append ((*r->second)->children()));
339 /* add this action */
341 /* use the "visible label" as the action name */
344 /* no label, try using the tooltip instead */
345 row[columns.name] = *t;
347 row[columns.name] = *l;
349 row[columns.path] = string_compose ("%1/%2", category, action_name);
350 row[columns.bindable] = true;
352 if (*k == ActionManager::unbound_string) {
353 row[columns.binding] = string();
355 row[columns.binding] = *k;
357 row[columns.action] = *a;
362 KeyEditor::Tab::sort_column_changed ()
366 if (model->get_sort_column_id (column, type)) {
367 owner.sort_column = column;
368 owner.sort_type = type;
373 KeyEditor::Tab::tab_mapped ()
375 model->set_sort_column (owner.sort_column, owner.sort_type);
381 Keyboard::the_keyboard().reset_bindings ();
383 for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
384 (*t)->view.get_selection()->unselect_all ();
390 KeyEditor::current_tab ()
392 return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));