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