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