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