faderport: allow User button to be bound to ANY possible action.
[ardour.git] / libs / surfaces / faderport / gui.cc
1 /*
2     Copyright (C) 2015 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 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
27
28 #include "gtkmm2ext/actions.h"
29 #include "gtkmm2ext/gtk_ui.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/utils.h"
32
33 #include "ardour/audioengine.h"
34 #include "ardour/filesystem_paths.h"
35
36 #include "faderport.h"
37 #include "gui.h"
38
39 #include "i18n.h"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
44 using namespace std;
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47
48 void*
49 FaderPort::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<FaderPort*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 FaderPort::tear_down_gui ()
60 {
61         if (gui) {
62                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
63                 if (w) {
64                         w->hide();
65                         delete w;
66                 }
67         }
68         delete static_cast<FPGUI*> (gui);
69         gui = 0;
70 }
71
72 void
73 FaderPort::build_gui ()
74 {
75         gui = (void*) new FPGUI (*this);
76 }
77
78 /*--------------------*/
79
80 FPGUI::FPGUI (FaderPort& p)
81         : fp (p)
82         , table (2, 5)
83         , action_table (4, 4)
84         , ignore_active_change (false)
85 {
86         set_border_width (12);
87
88         table.set_row_spacings (4);
89         table.set_col_spacings (6);
90         table.set_border_width (12);
91         table.set_homogeneous (false);
92
93         std::string data_file_path;
94         string name = "faderport-small.png";
95         Searchpath spath(ARDOUR::ardour_data_search_path());
96         spath.add_subdirectory_to_paths ("icons");
97         find_file (spath, name, data_file_path);
98         if (!data_file_path.empty()) {
99                 image.set (data_file_path);
100                 hpacker.pack_start (image, false, false);
101         }
102
103         Gtk::Label* l;
104         Gtk::Alignment* align;
105         int row = 0;
106
107         input_combo.pack_start (midi_port_columns.short_name);
108         output_combo.pack_start (midi_port_columns.short_name);
109
110         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &input_combo, true));
111         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &output_combo, false));
112
113         l = manage (new Gtk::Label);
114         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
115         l->set_alignment (1.0, 0.5);
116         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
117         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
118         row++;
119
120         l = manage (new Gtk::Label);
121         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
122         l->set_alignment (1.0, 0.5);
123         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
124         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
125         row++;
126
127         build_mix_action_combo (mix_combo[0], FaderPort::ButtonState(0));
128         build_mix_action_combo (mix_combo[1], FaderPort::ShiftDown);
129         build_mix_action_combo (mix_combo[2], FaderPort::LongPress);
130
131         build_proj_action_combo (proj_combo[0], FaderPort::ButtonState(0));
132         build_proj_action_combo (proj_combo[1], FaderPort::ShiftDown);
133         build_proj_action_combo (proj_combo[2], FaderPort::LongPress);
134
135         build_trns_action_combo (trns_combo[0], FaderPort::ButtonState(0));
136         build_trns_action_combo (trns_combo[1], FaderPort::ShiftDown);
137         build_trns_action_combo (trns_combo[2], FaderPort::LongPress);
138
139         build_available_action_menu ();
140
141         build_user_action_combo (user_combo[0], FaderPort::ButtonState(0));
142         build_user_action_combo (user_combo[1], FaderPort::ShiftDown);
143         build_user_action_combo (user_combo[2], FaderPort::LongPress);
144
145         action_table.set_row_spacings (4);
146         action_table.set_col_spacings (6);
147         action_table.set_border_width (12);
148         action_table.set_homogeneous (false);
149
150         int action_row = 0;
151
152         l = manage (new Gtk::Label);
153         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Press Action")));
154         l->set_alignment (0.5, 0.5);
155         action_table.attach (*l, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
156         l = manage (new Gtk::Label);
157         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Shift-Press Action")));
158         l->set_alignment (0.5, 0.5);
159         action_table.attach (*l, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
160         l = manage (new Gtk::Label);
161         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Long Press Action")));
162         l->set_alignment (0.5, 0.5);
163         action_table.attach (*l, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
164         action_row++;
165
166         l = manage (new Gtk::Label);
167         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Mix")));
168         l->set_alignment (1.0, 0.5);
169         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
170         align = manage (new Alignment);
171         align->set (0.0, 0.5);
172         align->add (mix_combo[0]);
173         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
174         align = manage (new Alignment);
175         align->set (0.0, 0.5);
176         align->add (mix_combo[1]);
177         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
178         align = manage (new Alignment);
179         align->set (0.0, 0.5);
180         align->add (mix_combo[2]);
181         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
182         action_row++;
183
184         l = manage (new Gtk::Label);
185         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Proj")));
186         l->set_alignment (1.0, 0.5);
187         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
188         align = manage (new Alignment);
189         align->set (0.0, 0.5);
190         align->add (proj_combo[0]);
191         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
192         align = manage (new Alignment);
193         align->set (0.0, 0.5);
194         align->add (proj_combo[1]);
195         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
196         align = manage (new Alignment);
197         align->set (0.0, 0.5);
198         align->add (proj_combo[2]);
199         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
200         action_row++;
201
202         l = manage (new Gtk::Label);
203         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Trns")));
204         l->set_alignment (1.0, 0.5);
205         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
206         align = manage (new Alignment);
207         align->set (0.0, 0.5);
208         align->add (trns_combo[0]);
209         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
210         align = manage (new Alignment);
211         align->set (0.0, 0.5);
212         align->add (trns_combo[1]);
213         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
214         align = manage (new Alignment);
215         align->set (0.0, 0.5);
216         align->add (trns_combo[2]);
217         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
218         action_row++;
219
220         l = manage (new Gtk::Label);
221         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("User")));
222         l->set_alignment (1.0, 0.5);
223         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
224         align = manage (new Alignment);
225         align->set (0.0, 0.5);
226         align->add (user_combo[0]);
227         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
228         align = manage (new Alignment);
229         align->set (0.0, 0.5);
230         align->add (user_combo[1]);
231         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
232         align = manage (new Alignment);
233         align->set (0.0, 0.5);
234         align->add (user_combo[2]);
235         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
236         action_row++;
237
238         table.attach (action_table, 0, 5, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
239         row++;
240
241         hpacker.pack_start (table, true, true);
242         pack_start (hpacker, false, false);
243
244         /* update the port connection combos */
245
246         update_port_combos ();
247
248         /* catch future changes to connection state */
249
250         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FPGUI::connection_handler, this), gui_context());
251 }
252
253 FPGUI::~FPGUI ()
254 {
255 }
256
257 void
258 FPGUI::connection_handler ()
259 {
260         /* ignore all changes to combobox active strings here, because we're
261            updating them to match a new ("external") reality - we were called
262            because port connections have changed.
263         */
264
265         PBD::Unwinder<bool> ici (ignore_active_change, true);
266
267         update_port_combos ();
268 }
269
270 void
271 FPGUI::update_port_combos ()
272 {
273         vector<string> midi_inputs;
274         vector<string> midi_outputs;
275
276         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
277         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
278
279         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
280         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
281         bool input_found = false;
282         bool output_found = false;
283         int n;
284
285         input_combo.set_model (input);
286         output_combo.set_model (output);
287
288         Gtk::TreeModel::Children children = input->children();
289         Gtk::TreeModel::Children::iterator i;
290         i = children.begin();
291         ++i; /* skip "Disconnected" */
292
293
294         for (n = 1;  i != children.end(); ++i, ++n) {
295                 string port_name = (*i)[midi_port_columns.full_name];
296                 if (fp.input_port()->connected_to (port_name)) {
297                         input_combo.set_active (n);
298                         input_found = true;
299                         break;
300                 }
301         }
302
303         if (!input_found) {
304                 input_combo.set_active (0); /* disconnected */
305         }
306
307         children = output->children();
308         i = children.begin();
309         ++i; /* skip "Disconnected" */
310
311         for (n = 1;  i != children.end(); ++i, ++n) {
312                 string port_name = (*i)[midi_port_columns.full_name];
313                 if (fp.output_port()->connected_to (port_name)) {
314                         output_combo.set_active (n);
315                         output_found = true;
316                         break;
317                 }
318         }
319
320         if (!output_found) {
321                 output_combo.set_active (0); /* disconnected */
322         }
323 }
324
325 void
326 FPGUI::build_available_action_menu ()
327 {
328         /* build a model of all available actions (needs to be tree structured
329          * more)
330          */
331
332         available_action_model = TreeStore::create (action_columns);
333
334         vector<string> paths;
335         vector<string> labels;
336         vector<string> tooltips;
337         vector<string> keys;
338         vector<AccelKey> bindings;
339         typedef std::map<string,TreeIter> NodeMap;
340         NodeMap nodes;
341         NodeMap::iterator r;
342
343         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
344
345         vector<string>::iterator k;
346         vector<string>::iterator p;
347         vector<string>::iterator t;
348         vector<string>::iterator l;
349
350         available_action_model->clear ();
351
352         /* Because there are button bindings built in that are not
353         in the key binding map, there needs to be a way to undo
354         a profile edit. */
355         TreeIter rowp;
356         TreeModel::Row parent;
357         rowp = available_action_model->append();
358         parent = *(rowp);
359         parent[action_columns.name] = _("Remove Binding");
360
361         /* Key aliasing */
362
363         rowp = available_action_model->append();
364         parent = *(rowp);
365         parent[action_columns.name] = _("Shift");
366         rowp = available_action_model->append();
367         parent = *(rowp);
368         parent[action_columns.name] = _("Control");
369         rowp = available_action_model->append();
370         parent = *(rowp);
371         parent[action_columns.name] = _("Option");
372         rowp = available_action_model->append();
373         parent = *(rowp);
374         parent[action_columns.name] = _("CmdAlt");
375
376
377         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
378
379                 TreeModel::Row row;
380                 vector<string> parts;
381
382                 parts.clear ();
383
384                 split (*p, parts, '/');
385
386                 if (parts.empty()) {
387                         continue;
388                 }
389
390                 //kinda kludgy way to avoid displaying menu items as mappable
391                 if ( parts[1] == _("Main_menu") )
392                         continue;
393                 if ( parts[1] == _("JACK") )
394                         continue;
395                 if ( parts[1] == _("redirectmenu") )
396                         continue;
397                 if ( parts[1] == _("Editor_menus") )
398                         continue;
399                 if ( parts[1] == _("RegionList") )
400                         continue;
401                 if ( parts[1] == _("ProcessorMenu") )
402                         continue;
403
404                 if ((r = nodes.find (parts[1])) == nodes.end()) {
405
406                         /* top level is missing */
407
408                         TreeIter rowp;
409                         TreeModel::Row parent;
410                         rowp = available_action_model->append();
411                         nodes[parts[1]] = rowp;
412                         parent = *(rowp);
413                         parent[action_columns.name] = parts[1];
414
415                         row = *(available_action_model->append (parent.children()));
416
417                 } else {
418
419                         row = *(available_action_model->append ((*r->second)->children()));
420
421                 }
422
423                 /* add this action */
424
425                 if (l->empty ()) {
426                         row[action_columns.name] = *t;
427                         action_map[*t] = *p;
428                 } else {
429                         row[action_columns.name] = *l;
430                         action_map[*l] = *p;
431                 }
432
433                 string path = (*p);
434                 /* ControlProtocol::access_action() is not interested in the
435                    legacy "<Actions>/" prefix part of a path.
436                 */
437                 path = path.substr (strlen ("<Actions>/"));
438
439                 row[action_columns.path] = path;
440         }
441 }
442
443 void
444 FPGUI::action_changed (Gtk::ComboBox* cb, FaderPort::ButtonID id, FaderPort::ButtonState bs)
445 {
446         TreeModel::const_iterator row = cb->get_active ();
447         string action_path = (*row)[action_columns.path];
448
449         /* release binding */
450         fp.set_action (id, action_path, false, bs);
451 }
452
453 void
454 FPGUI::build_action_combo (Gtk::ComboBox& cb, vector<pair<string,string> > const & actions, FaderPort::ButtonID id, FaderPort::ButtonState bs)
455 {
456         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (action_columns));
457         TreeIter rowp;
458         TreeModel::Row row;
459         string current_action = fp.get_action (id, false, bs); /* lookup release action */
460         int active_row = -1;
461         int n;
462         vector<pair<string,string> >::const_iterator i;
463
464         rowp = model->append();
465         row = *(rowp);
466         row[action_columns.name] = _("Disabled");
467         row[action_columns.path] = string();
468
469         if (current_action.empty()) {
470                 active_row = 0;
471         }
472
473         for (i = actions.begin(), n = 0; i != actions.end(); ++i, ++n) {
474                 rowp = model->append();
475                 row = *(rowp);
476                 row[action_columns.name] = i->first;
477                 row[action_columns.path] = i->second;
478                 if (current_action == i->second) {
479                         active_row = n+1;
480                 }
481         }
482
483         cb.set_model (model);
484         cb.pack_start (action_columns.name);
485
486         if (active_row >= 0) {
487                 cb.set_active (active_row);
488         }
489
490         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, id, bs));
491 }
492
493 void
494 FPGUI::build_mix_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
495 {
496         vector<pair<string,string> > actions;
497
498         actions.push_back (make_pair (string (_("Toggle Editor & Mixer Windows")), string (X_("Common/toggle-editor-mixer"))));
499         actions.push_back (make_pair (string (_("Show/Hide Editor mixer strip")), string (X_("Editor/show-editor-mixer"))));
500
501         build_action_combo (cb, actions, FaderPort::Mix, bs);
502 }
503
504 void
505 FPGUI::build_proj_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
506 {
507         vector<pair<string,string> > actions;
508
509         actions.push_back (make_pair (string("Toggle Meterbridge"), string(X_("Common/toggle-meterbridge"))));
510         actions.push_back (make_pair (string("Toggle Summary"), string(X_("Editor/ToggleSummary"))));
511         actions.push_back (make_pair (string("Toggle Editor Lists"), string(X_("Editor/show-editor-list"))));
512         actions.push_back (make_pair (string (_("Zoom to Session")), string (X_("Editor/zoom-to-session"))));
513         actions.push_back (make_pair (string (_("Zoom In")), string (X_("Editor/temporal-zoom-in"))));
514         actions.push_back (make_pair (string (_("Zoom Out")), string (X_("Editor/temporal-zoom-out"))));
515
516         build_action_combo (cb, actions, FaderPort::Proj, bs);
517 }
518
519 void
520 FPGUI::build_trns_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
521 {
522         vector<pair<string,string> > actions;
523
524         actions.push_back (make_pair (string("Toggle Locations"), string(X_("Window/toggle-locations"))));
525         actions.push_back (make_pair (string("Toggle Metronome"), string(X_("Transport/ToggleClick"))));
526         actions.push_back (make_pair (string("Toggle Sync"), string(X_("Transport/ToggleExternalSync"))));
527         actions.push_back (make_pair (string("Set Playhead @pointer"), string(X_("Editor/set-playhead"))));
528
529         build_action_combo (cb, actions, FaderPort::Trns, bs);
530 }
531
532 void
533 FPGUI::build_user_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
534 {
535         cb.set_model (available_action_model);
536         cb.pack_start (action_columns.name);
537         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, FaderPort::User, bs));
538 }
539
540 Glib::RefPtr<Gtk::ListStore>
541 FPGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
542 {
543         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
544         TreeModel::Row row;
545
546         row = *store->append ();
547         row[midi_port_columns.full_name] = string();
548         row[midi_port_columns.short_name] = _("Disconnected");
549
550         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
551                 row = *store->append ();
552                 row[midi_port_columns.full_name] = *p;
553                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
554                 if (pn.empty ()) {
555                         pn = (*p).substr ((*p).find (':') + 1);
556                 }
557                 row[midi_port_columns.short_name] = pn;
558         }
559
560         return store;
561 }
562
563 void
564 FPGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
565 {
566         if (ignore_active_change) {
567                 return;
568         }
569
570         TreeModel::iterator active = combo->get_active ();
571         string new_port = (*active)[midi_port_columns.full_name];
572
573         if (new_port.empty()) {
574                 if (for_input) {
575                         fp.input_port()->disconnect_all ();
576                 } else {
577                         fp.output_port()->disconnect_all ();
578                 }
579
580                 return;
581         }
582
583         if (for_input) {
584                 if (!fp.input_port()->connected_to (new_port)) {
585                         fp.input_port()->disconnect_all ();
586                         fp.input_port()->connect (new_port);
587                 }
588         } else {
589                 if (!fp.output_port()->connected_to (new_port)) {
590                         fp.output_port()->disconnect_all ();
591                         fp.output_port()->connect (new_port);
592                 }
593         }
594 }