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