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