Add a global state localeguard (and one for route templates)
[ardour.git] / gtk2_ardour / keyeditor.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <map>
25
26 #include <boost/algorithm/string.hpp>
27
28 #include <gtkmm/stock.h>
29 #include <gtkmm/label.h>
30 #include <gtkmm/accelkey.h>
31 #include <gtkmm/accelmap.h>
32 #include <gtkmm/uimanager.h>
33
34 #include "gtkmm2ext/bindings.h"
35 #include "gtkmm2ext/utils.h"
36
37 #include "pbd/strsplit.h"
38
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/profile.h"
41
42 #include "actions.h"
43 #include "keyboard.h"
44 #include "keyeditor.h"
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace Gtk;
50 using namespace Gdk;
51 using namespace PBD;
52
53 using Gtkmm2ext::Keyboard;
54 using Gtkmm2ext::Bindings;
55
56 sigc::signal<void> KeyEditor::UpdateBindings;
57
58 void bindings_collision_dialog (Gtk::Window& parent)
59 {
60         ArdourDialog dialog (parent, _("Colliding keybindings"), true);
61         Label label (_("The key sequence is already bound. Please remove the other binding first."));
62
63         dialog.get_vbox()->pack_start (label, true, true);
64         dialog.add_button (_("Ok"), Gtk::RESPONSE_ACCEPT);
65         dialog.show_all ();
66         dialog.run();
67 }
68
69 KeyEditor::KeyEditor ()
70         : ArdourWindow (_("Key Bindings"))
71         , unbind_button (_("Remove shortcut"))
72         , unbind_box (BUTTONBOX_END)
73         , filter_entry (_("Search..."), true)
74         , filter_string("")
75         , sort_column(0)
76         , sort_type(Gtk::SORT_ASCENDING)
77 {
78
79         notebook.signal_switch_page ().connect (sigc::mem_fun (*this, &KeyEditor::page_change));
80
81         vpacker.pack_start (notebook, true, true);
82
83         Glib::RefPtr<Gdk::Pixbuf> icon = ARDOUR_UI_UTILS::get_icon ("search");
84         filter_entry.set_icon_from_pixbuf (icon);
85         filter_entry.set_icon_tooltip_text (_("Click to reset search string"));
86         filter_entry.signal_search_string_updated ().connect (sigc::mem_fun (*this, &KeyEditor::search_string_updated));
87         vpacker.pack_start (filter_entry, false, false);
88
89         Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
90         hint->show ();
91         unbind_box.set_spacing (6);
92         unbind_box.pack_start (*hint, false, true);
93         unbind_box.pack_start (unbind_button, false, false);
94         unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
95
96         vpacker.pack_start (unbind_box, false, false);
97         unbind_box.show ();
98         unbind_button.show ();
99
100         reset_button.add (reset_label);
101         reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
102
103         reset_box.pack_start (reset_button, true, false);
104         reset_box.show ();
105         reset_button.show ();
106         reset_label.show ();
107         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
108         vpacker.pack_start (reset_box, false, false);
109
110         add (vpacker);
111
112         unbind_button.set_sensitive (false);
113         UpdateBindings.connect (sigc::mem_fun (*this, &KeyEditor::refresh));
114 }
115
116 void
117 KeyEditor::add_tab (string const & name, Bindings& bindings)
118 {
119         Tab* t = new Tab (*this, name, &bindings);
120
121         if (t->populate () == 0) {
122                 /* no bindings */
123                 delete t;
124                 return;
125         }
126
127         tabs.push_back (t);
128         t->show_all ();
129         notebook.append_page (*t, name);
130 }
131
132
133 void
134 KeyEditor::remove_tab (string const &name)
135 {
136         guint npages = notebook.get_n_pages ();
137
138         for (guint n = 0; n < npages; ++n) {
139                 Widget* w = notebook.get_nth_page (n);
140                 Tab* tab = dynamic_cast<Tab*> (w);
141                 if (tab) {
142                         if (tab->name == name) {
143                                 notebook.remove_page (*w);
144                                 return;
145                         }
146                 }
147         }
148         cerr << "Removed " << name << endl;
149 }
150
151 void
152 KeyEditor::unbind ()
153 {
154         current_tab()->unbind ();
155 }
156
157 void
158 KeyEditor::page_change (GtkNotebookPage*, guint)
159 {
160         current_tab()->view.get_selection()->unselect_all ();
161         unbind_button.set_sensitive (false);
162 }
163
164 bool
165 KeyEditor::Tab::key_press_event (GdkEventKey* ev)
166 {
167         if (view.get_selection()->count_selected_rows() != 1) {
168                 return false;
169         }
170
171         if (!ev->is_modifier) {
172                 last_keyval = ev->keyval;
173         }
174
175         /* Don't let anything else handle the key press, because navigation
176          * keys will be used by GTK to change the selection/treeview cursor
177          * position
178          */
179
180         return true;
181 }
182
183 bool
184 KeyEditor::Tab::key_release_event (GdkEventKey* ev)
185 {
186         if (view.get_selection()->count_selected_rows() != 1) {
187                 return false;
188         }
189
190         if (last_keyval == 0) {
191                 return false;
192         }
193
194         owner.current_tab()->bind (ev, last_keyval);
195
196         last_keyval = 0;
197         return true;
198 }
199
200 KeyEditor::Tab::Tab (KeyEditor& ke, string const & str, Bindings* b)
201         : owner (ke)
202         , name (str)
203         , bindings (b)
204         , last_keyval (0)
205 {
206         data_model = TreeStore::create(columns);
207         populate ();
208
209         filter = TreeModelFilter::create(data_model);
210         filter->set_visible_func (sigc::mem_fun (*this, &Tab::visible_func));
211
212         sorted_filter = TreeModelSort::create(filter);
213
214         view.set_model (sorted_filter);
215         view.append_column (_("Action"), columns.name);
216         view.append_column (_("Shortcut"), columns.binding);
217         view.set_headers_visible (true);
218         view.set_headers_clickable (true);
219         view.get_selection()->set_mode (SELECTION_SINGLE);
220         view.set_reorderable (false);
221         view.set_size_request (500,300);
222         view.set_enable_search (false);
223         view.set_rules_hint (true);
224         view.set_name (X_("KeyEditorTree"));
225
226         view.signal_cursor_changed().connect (sigc::mem_fun (*this, &Tab::action_selected));
227         view.signal_key_press_event().connect (sigc::mem_fun (*this, &Tab::key_press_event), false);
228         view.signal_key_release_event().connect (sigc::mem_fun (*this, &Tab::key_release_event), false);
229
230         view.get_column(0)->set_sort_column (columns.name);
231         view.get_column(1)->set_sort_column (columns.binding);
232         data_model->set_sort_column (owner.sort_column,  owner.sort_type);
233         data_model->signal_sort_column_changed().connect (sigc::mem_fun (*this, &Tab::sort_column_changed));
234
235         signal_map().connect (sigc::mem_fun (*this, &Tab::tab_mapped));
236
237         scroller.add (view);
238         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
239
240         set_spacing (6);
241         set_border_width (12);
242         pack_start (scroller);
243 }
244
245 void
246 KeyEditor::Tab::action_selected ()
247 {
248         if (view.get_selection()->count_selected_rows() == 0) {
249                 return;
250         }
251
252         TreeModel::const_iterator it = view.get_selection()->get_selected();
253
254         if (!it) {
255                 return;
256         }
257
258         if (!(*it)[columns.bindable]) {
259                 owner.unbind_button.set_sensitive (false);
260                 return;
261         }
262
263         const string& binding = (*it)[columns.binding];
264
265         if (!binding.empty()) {
266                 owner.unbind_button.set_sensitive (true);
267         }
268 }
269
270 void
271 KeyEditor::Tab::unbind ()
272 {
273         const std::string& action_path = (*view.get_selection()->get_selected())[columns.path];
274
275         TreeModel::iterator it = find_action_path (data_model->children().begin(), data_model->children().end(),  action_path);
276
277         if (!it || !(*it)[columns.bindable]) {
278                 return;
279         }
280
281         bindings->remove (Gtkmm2ext::Bindings::Press,  action_path , true);
282         (*it)[columns.binding] = string ();
283
284         owner.unbind_button.set_sensitive (false);
285 }
286
287 void
288 KeyEditor::Tab::bind (GdkEventKey* release_event, guint pressed_key)
289 {
290         const std::string& action_path = (*view.get_selection()->get_selected())[columns.path];
291         TreeModel::iterator it = find_action_path (data_model->children().begin(), data_model->children().end(),  action_path);
292
293         /* pressed key could be upper case if Shift was used. We want all
294            single keys stored as their lower-case version, so ensure this
295         */
296
297         pressed_key = gdk_keyval_to_lower (pressed_key);
298
299         if (!it || !(*it)[columns.bindable]) {
300                 return;
301         }
302
303         GdkModifierType mod = (GdkModifierType)(Keyboard::RelevantModifierKeyMask & release_event->state);
304         Gtkmm2ext::KeyboardKey new_binding (mod, pressed_key);
305
306         if (bindings->is_bound (new_binding, Gtkmm2ext::Bindings::Press)) {
307                 bindings_collision_dialog (owner);
308                 return;
309         }
310
311         bool result = bindings->replace (new_binding, Gtkmm2ext::Bindings::Press, action_path);
312
313         if (result) {
314                 (*it)[columns.binding] = gtk_accelerator_get_label (new_binding.key(), (GdkModifierType) new_binding.state());
315                 owner.unbind_button.set_sensitive (true);
316         }
317 }
318
319 uint32_t
320 KeyEditor::Tab::populate ()
321 {
322         vector<string> paths;
323         vector<string> labels;
324         vector<string> tooltips;
325         vector<string> keys;
326         vector<Glib::RefPtr<Action> > actions;
327         typedef std::map<string,TreeIter> NodeMap;
328         NodeMap nodes;
329         NodeMap::iterator r;
330
331         bindings->get_all_actions (paths, labels, tooltips, keys, actions);
332
333         vector<string>::iterator k;
334         vector<string>::iterator p;
335         vector<string>::iterator t;
336         vector<string>::iterator l;
337         vector<Glib::RefPtr<Action> >::iterator a;
338
339         data_model->clear ();
340
341         for (a = actions.begin(), l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l, ++a) {
342
343                 TreeModel::Row row;
344                 vector<string> parts;
345
346                 split (*p, parts, '/');
347
348                 string category = parts[1];
349                 string action_name = parts[2];
350
351                 if (action_name.empty()) {
352                         continue;
353                 }
354
355                 //kinda kludgy way to avoid displaying menu items as mappable
356                 if ((action_name.find ("Menu") == action_name.length() - 4) ||
357                     (action_name.find ("menu") == action_name.length() - 4) ||
358                     (action_name == _("RegionList"))) {
359                         continue;
360                 }
361
362                 if ((r = nodes.find (category)) == nodes.end()) {
363
364                         /* category/group is missing, so add it first */
365
366                         TreeIter rowp;
367                         TreeModel::Row parent;
368                         rowp = data_model->append();
369                         nodes[category] = rowp;
370                         parent = *(rowp);
371                         parent[columns.name] = category;
372                         parent[columns.bindable] = false;
373                         parent[columns.action] = *a;
374
375                         /* now set up the child row that we're about to fill
376                          * out with information
377                          */
378
379                         row = *(data_model->append (parent.children()));
380
381                 } else {
382
383                         /* category/group is present, so just add the child row */
384
385                         row = *(data_model->append ((*r->second)->children()));
386
387                 }
388
389                 /* add this action */
390
391                 /* use the "visible label" as the action name */
392
393                 if (l->empty ()) {
394                         /* no label, try using the tooltip instead */
395                         row[columns.name] = *t;
396                 } else {
397                         row[columns.name] = *l;
398                 }
399                 row[columns.path] = string_compose ("%1/%2", category, action_name);
400                 row[columns.bindable] = true;
401
402                 if (*k == ActionManager::unbound_string) {
403                         row[columns.binding] = string();
404                 } else {
405                         row[columns.binding] = *k;
406                 }
407                 row[columns.action] = *a;
408         }
409
410         return data_model->children().size();
411 }
412
413 void
414 KeyEditor::Tab::sort_column_changed ()
415 {
416         int column;
417         SortType type;
418         if (data_model->get_sort_column_id (column, type)) {
419                 owner.sort_column = column;
420                 owner.sort_type = type;
421         }
422 }
423
424 void
425 KeyEditor::Tab::tab_mapped ()
426 {
427         data_model->set_sort_column (owner.sort_column,  owner.sort_type);
428         filter->refilter ();
429 }
430
431 bool
432 KeyEditor::Tab::visible_func(const Gtk::TreeModel::const_iterator& iter) const
433 {
434         if (!iter) {
435                 return false;
436         }
437
438         // never filter when search string is empty or item is a category
439         if (owner.filter_string.empty () || !(*iter)[columns.bindable]) {
440                 return true;
441         }
442
443         // search name
444         std::string name = (*iter)[columns.name];
445         boost::to_lower (name);
446         if (name.find (owner.filter_string) != std::string::npos) {
447                 return true;
448         }
449
450         // search binding
451         std::string binding = (*iter)[columns.binding];
452         boost::to_lower (binding);
453         if (binding.find (owner.filter_string) != std::string::npos) {
454                 return true;
455         }
456
457         return false;
458 }
459
460 TreeModel::iterator
461 KeyEditor::Tab::find_action_path (TreeModel::const_iterator begin, TreeModel::const_iterator end, const std::string& action_path) const
462 {
463         if (!begin) {
464                 return end;
465         }
466
467         for (TreeModel::iterator it = begin; it != end; ++it) {
468                 if (it->children()) {
469                         TreeModel::iterator jt = find_action_path (it->children().begin(), it->children().end(), action_path);
470                         if (jt != it->children().end()) {
471                                 return jt;
472                         }
473                 }
474                 const std::string& path = (*it)[columns.path];
475                 if (action_path.compare(path) == 0) {
476                         return it;
477                 }
478         }
479         return end;
480 }
481
482 void
483 KeyEditor::reset ()
484 {
485         Keyboard::the_keyboard().reset_bindings ();
486         refresh ();
487 }
488
489 void
490 KeyEditor::refresh ()
491 {
492         for (Tabs::iterator t = tabs.begin(); t != tabs.end(); ++t) {
493                 (*t)->view.get_selection()->unselect_all ();
494                 (*t)->populate ();
495         }
496 }
497
498 KeyEditor::Tab*
499 KeyEditor::current_tab ()
500 {
501         return dynamic_cast<Tab*> (notebook.get_nth_page (notebook.get_current_page()));
502 }
503
504 void
505 KeyEditor::search_string_updated (const std::string& filter)
506 {
507         filter_string = boost::to_lower_copy(filter);
508         KeyEditor::Tab* tab = current_tab ();
509         if (tab) {
510                 tab->filter->refilter ();
511         }
512 }