3a537de028fffc01417ce6d51f0f6d196d1beb26
[ardour.git] / libs / surfaces / faderport8 / gui.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2015 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23 #include <gtkmm/separator.h>
24
25 #include "pbd/unwind.h"
26 #include "pbd/strsplit.h"
27 #include "pbd/file_utils.h"
28
29 #include "gtkmm2ext/bindings.h"
30 #include "gtkmm2ext/gtk_ui.h"
31 #include "gtkmm2ext/gui_thread.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "ardour/audioengine.h"
35 #include "ardour/filesystem_paths.h"
36
37 #include "faderport8.h"
38 #include "gui.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace PBD;
43 using namespace ARDOUR;
44 using namespace std;
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47 using namespace ArdourSurface::FP_NAMESPACE;
48
49 void*
50 FaderPort8::get_gui () const
51 {
52         if (!gui) {
53                 const_cast<FaderPort8*>(this)->build_gui ();
54         }
55         static_cast<Gtk::VBox*>(gui)->show_all();
56         return gui;
57 }
58
59 void
60 FaderPort8::tear_down_gui ()
61 {
62         if (gui) {
63                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
64                 if (w) {
65                         w->hide();
66                         delete w;
67                 }
68         }
69         delete static_cast<FP8GUI*> (gui);
70         gui = 0;
71 }
72
73 void
74 FaderPort8::build_gui ()
75 {
76         gui = (void*) new FP8GUI (*this);
77 }
78
79 /* ****************************************************************************/
80
81 FP8GUI::FP8GUI (FaderPort8& p)
82         : fp (p)
83         , table (2, 3)
84         , ignore_active_change (false)
85         , two_line_text_cb (_("Two Line Trackname"))
86         , auto_pluginui_cb (_("Auto Show/Hide Plugin GUIs"))
87 {
88         set_border_width (12);
89
90         table.set_row_spacings (4);
91         table.set_col_spacings (6);
92         table.set_border_width (12);
93         table.set_homogeneous (false);
94
95         std::string data_file_path;
96 #ifdef FADERPORT16
97         string name = "faderport16-small.png";
98 #elif defined FADERPORT2
99         string name = "faderport2018-small.png";
100 #else
101         string name = "faderport8-small.png";
102 #endif
103         Searchpath spath(ARDOUR::ardour_data_search_path());
104         spath.add_subdirectory_to_paths ("icons");
105         find_file (spath, name, data_file_path);
106         if (!data_file_path.empty()) {
107                 image.set (data_file_path);
108                 hpacker.pack_start (image, false, false);
109         }
110
111         Gtk::Label* l;
112         int row = 0;
113
114         input_combo.pack_start (midi_port_columns.short_name);
115         output_combo.pack_start (midi_port_columns.short_name);
116
117         build_prefs_combos ();
118         update_prefs_combos ();
119
120         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &input_combo, true));
121         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &output_combo, false));
122
123         clock_combo.signal_changed().connect (sigc::mem_fun (*this, &FP8GUI::clock_mode_changed));
124         scribble_combo.signal_changed().connect (sigc::mem_fun (*this, &FP8GUI::scribble_mode_changed));
125         two_line_text_cb.signal_toggled().connect(sigc::mem_fun (*this, &FP8GUI::twolinetext_toggled));
126         auto_pluginui_cb.signal_toggled().connect(sigc::mem_fun (*this, &FP8GUI::auto_pluginui_toggled));
127
128         l = manage (new Gtk::Label);
129         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
130         l->set_alignment (1.0, 0.5);
131         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
132         table.attach (input_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
133         row++;
134
135         l = manage (new Gtk::Label);
136         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
137         l->set_alignment (1.0, 0.5);
138         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
139         table.attach (output_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
140         row++;
141
142         Gtk::HSeparator *hsep = manage(new Gtk::HSeparator);
143         table.attach (*hsep, 0, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 6);
144         row++;
145
146         hpacker.pack_start (table, true, true);
147         pack_start (hpacker, false, false);
148
149         /* actions */
150         build_available_action_menu ();
151
152         int action_row = 0;
153         int action_col = 0;
154         Gtk::Alignment* align;
155
156         for (FP8Controls::UserButtonMap::const_iterator i = fp.control().user_buttons ().begin ();
157                         i != fp.control().user_buttons ().end (); ++i) {
158                 Gtk::ComboBox* user_combo = manage (new Gtk::ComboBox);
159                 build_action_combo (*user_combo, i->first);
160                 l = manage (new Gtk::Label);
161                 l->set_markup (string_compose ("<span weight=\"bold\">%1:</span>", i->second));
162                 l->set_alignment (1.0, 0.5);
163                 table.attach (*l, 3 * action_col, 3 * action_col + 1, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
164                 align = manage (new Alignment);
165                 align->set (0.0, 0.5);
166                 align->add (*user_combo);
167                 table.attach (*align, 3 * action_col + 1, 3 * action_col + 2, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
168
169 #ifdef FADERPORT2
170                 if (++action_row == 2)
171 #else
172                 if (++action_row == 4)
173 #endif
174                 {
175                         action_row = 0;
176                         ++action_col;
177                 }
178         }
179
180         for (int c = 0; c < 2; ++c) {
181                 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
182                 table.attach (*vsep, 3 * c + 2, 3 * c + 3, row, row + 4, AttachOptions(0), AttachOptions(FILL), 6, 0);
183         }
184
185         row += 4;
186
187 #ifndef FADERPORT2
188         hsep = manage(new Gtk::HSeparator);
189         table.attach (*hsep, 0, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 6);
190         row++;
191
192         l = manage (new Gtk::Label);
193         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock:")));
194         l->set_alignment (1.0, 0.5);
195         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
196         table.attach (clock_combo, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
197
198         table.attach (two_line_text_cb, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
199         row++;
200
201         l = manage (new Gtk::Label);
202         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Display:")));
203         l->set_alignment (1.0, 0.5);
204         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
205         table.attach (scribble_combo, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
206
207         table.attach (auto_pluginui_cb, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
208         row++;
209 #endif
210
211         /* update the port connection combos */
212         update_port_combos ();
213
214         /* catch future changes to connection state */
215         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FP8GUI::connection_handler, this), gui_context());
216 }
217
218 FP8GUI::~FP8GUI ()
219 {
220 }
221
222 void
223 FP8GUI::connection_handler ()
224 {
225         PBD::Unwinder<bool> ici (ignore_active_change, true);
226         update_port_combos ();
227 }
228
229 void
230 FP8GUI::update_port_combos ()
231 {
232         vector<string> midi_inputs;
233         vector<string> midi_outputs;
234
235         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
236         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
237
238         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
239         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
240         bool input_found = false;
241         bool output_found = false;
242         int n;
243
244         input_combo.set_model (input);
245         output_combo.set_model (output);
246
247         Gtk::TreeModel::Children children = input->children();
248         Gtk::TreeModel::Children::iterator i;
249         i = children.begin();
250         ++i; /* skip "Disconnected" */
251
252         for (n = 1;  i != children.end(); ++i, ++n) {
253                 string port_name = (*i)[midi_port_columns.full_name];
254                 if (fp.input_port()->connected_to (port_name)) {
255                         input_combo.set_active (n);
256                         input_found = true;
257                         break;
258                 }
259         }
260
261         if (!input_found) {
262                 input_combo.set_active (0); /* disconnected */
263         }
264
265         children = output->children();
266         i = children.begin();
267         ++i; /* skip "Disconnected" */
268
269         for (n = 1;  i != children.end(); ++i, ++n) {
270                 string port_name = (*i)[midi_port_columns.full_name];
271                 if (fp.output_port()->connected_to (port_name)) {
272                         output_combo.set_active (n);
273                         output_found = true;
274                         break;
275                 }
276         }
277
278         if (!output_found) {
279                 output_combo.set_active (0); /* disconnected */
280         }
281 }
282
283
284 Glib::RefPtr<Gtk::ListStore>
285 FP8GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
286 {
287         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
288         TreeModel::Row row;
289
290         row = *store->append ();
291         row[midi_port_columns.full_name] = string();
292         row[midi_port_columns.short_name] = _("Disconnected");
293
294         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
295                 row = *store->append ();
296                 row[midi_port_columns.full_name] = *p;
297                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
298                 if (pn.empty ()) {
299                         pn = (*p).substr ((*p).find (':') + 1);
300                 }
301                 row[midi_port_columns.short_name] = pn;
302         }
303
304         return store;
305 }
306
307 void
308 FP8GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
309 {
310         if (ignore_active_change) {
311                 return;
312         }
313
314         TreeModel::iterator active = combo->get_active ();
315         string new_port = (*active)[midi_port_columns.full_name];
316
317         if (new_port.empty()) {
318                 if (for_input) {
319                         fp.input_port()->disconnect_all ();
320                 } else {
321                         fp.output_port()->disconnect_all ();
322                 }
323
324                 return;
325         }
326
327         if (for_input) {
328                 if (!fp.input_port()->connected_to (new_port)) {
329                         fp.input_port()->disconnect_all ();
330                         fp.input_port()->connect (new_port);
331                 }
332         } else {
333                 if (!fp.output_port()->connected_to (new_port)) {
334                         fp.output_port()->disconnect_all ();
335                         fp.output_port()->connect (new_port);
336                 }
337         }
338 }
339
340
341
342 void
343 FP8GUI::build_available_action_menu ()
344 {
345         /* build a model of all available actions (needs to be tree structured
346          * more)
347          */
348
349         available_action_model = TreeStore::create (action_columns);
350
351         vector<string> paths;
352         vector<string> labels;
353         vector<string> tooltips;
354         vector<string> keys;
355         vector<Glib::RefPtr<Gtk::Action> > actions;
356
357         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
358
359         typedef std::map<string,TreeIter> NodeMap;
360         NodeMap nodes;
361         NodeMap::iterator r;
362
363
364         vector<string>::iterator k;
365         vector<string>::iterator p;
366         vector<string>::iterator t;
367         vector<string>::iterator l;
368
369         available_action_model->clear ();
370
371         TreeIter rowp;
372         TreeModel::Row parent;
373
374         /* Disabled item (row 0) */
375
376         rowp = available_action_model->append();
377         parent = *(rowp);
378         parent[action_columns.name] = _("Disabled");
379
380         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
381
382                 TreeModel::Row row;
383                 vector<string> parts;
384
385                 parts.clear ();
386
387                 split (*p, parts, '/');
388
389                 if (parts.empty()) {
390                         continue;
391                 }
392
393                 //kinda kludgy way to avoid displaying menu items as mappable
394                 if ( parts[1] == _("Main_menu") )
395                         continue;
396                 if ( parts[1] == _("JACK") )
397                         continue;
398                 if ( parts[1] == _("redirectmenu") )
399                         continue;
400                 if ( parts[1] == _("Editor_menus") )
401                         continue;
402                 if ( parts[1] == _("RegionList") )
403                         continue;
404                 if ( parts[1] == _("ProcessorMenu") )
405                         continue;
406
407                 if ((r = nodes.find (parts[1])) == nodes.end()) {
408
409                         /* top level is missing */
410
411                         TreeIter rowp;
412                         TreeModel::Row parent;
413                         rowp = available_action_model->append();
414                         nodes[parts[1]] = rowp;
415                         parent = *(rowp);
416                         parent[action_columns.name] = parts[1];
417
418                         row = *(available_action_model->append (parent.children()));
419
420                 } else {
421
422                         row = *(available_action_model->append ((*r->second)->children()));
423
424                 }
425
426                 /* add this action */
427
428                 if (l->empty ()) {
429                         row[action_columns.name] = *t;
430                         action_map[*t] = *p;
431                 } else {
432                         row[action_columns.name] = *l;
433                         action_map[*l] = *p;
434                 }
435
436                 string path = (*p);
437                 /* ControlProtocol::access_action() is not interested in the
438                    legacy "<Actions>/" prefix part of a path.
439                 */
440                 path = path.substr (strlen ("<Actions>/"));
441
442                 row[action_columns.path] = path;
443         }
444 }
445
446 bool
447 FP8GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const& action_path, TreeModel::iterator* found)
448 {
449         TreeModel::Row row = *iter;
450         string path = row[action_columns.path];
451
452         if (path == action_path) {
453                 *found = iter;
454                 return true;
455         }
456
457         return false;
458 }
459
460 void
461 FP8GUI::build_action_combo (Gtk::ComboBox& cb, FP8Controls::ButtonId id)
462 {
463         cb.set_model (available_action_model);
464         cb.pack_start (action_columns.name);
465
466         /* set the active "row" to the right value for the current button binding */
467         string current_action = fp.get_button_action (id, false); /* lookup release action */
468
469         if (current_action.empty()) {
470                 cb.set_active (0); /* "disabled" */
471         } else {
472                 TreeModel::iterator iter = available_action_model->children().end();
473
474                 available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FP8GUI::find_action_in_model), current_action, &iter));
475
476                 if (iter != available_action_model->children().end()) {
477                         cb.set_active (iter);
478                 } else {
479                         cb.set_active (0);
480                 }
481         }
482         /* bind signal _after_ setting the current value */
483         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::action_changed), &cb, id));
484 }
485
486 void
487 FP8GUI::action_changed (Gtk::ComboBox* cb, FP8Controls::ButtonId id)
488 {
489         TreeModel::const_iterator row = cb->get_active ();
490         string action_path = (*row)[action_columns.path];
491         fp.set_button_action (id, false, action_path);
492 }
493
494
495 void
496 FP8GUI::build_prefs_combos ()
497 {
498         vector<string> clock_strings;
499         vector<string> scribble_strings;
500
501         //clock_strings.push_back (_("Off"));
502         clock_strings.push_back (_("Timecode"));
503         clock_strings.push_back (_("BBT"));
504         clock_strings.push_back (_("Timecode + BBT"));
505
506         scribble_strings.push_back (_("Off"));
507         scribble_strings.push_back (_("Meter"));
508         scribble_strings.push_back (_("Pan"));
509         scribble_strings.push_back (_("Meter + Pan"));
510
511         set_popdown_strings (clock_combo, clock_strings);
512         set_popdown_strings (scribble_combo, scribble_strings);
513 }
514
515 void
516 FP8GUI::update_prefs_combos ()
517 {
518         switch (fp.clock_mode()) {
519                 default:
520                         clock_combo.set_active_text (_("Off"));
521                         break;
522                 case 1:
523                         clock_combo.set_active_text (_("Timecode"));
524                         break;
525                 case 2:
526                         clock_combo.set_active_text (_("BBT"));
527                         break;
528                 case 3:
529                         clock_combo.set_active_text (_("Timecode + BBT"));
530                         break;
531         }
532
533         switch (fp.scribble_mode()) {
534                 default:
535                         scribble_combo.set_active_text (_("Off"));
536                         break;
537                 case 1:
538                         scribble_combo.set_active_text (_("Meter"));
539                         break;
540                 case 2:
541                         scribble_combo.set_active_text (_("Pan"));
542                         break;
543                 case 3:
544                         scribble_combo.set_active_text (_("Meter + Pan"));
545                         break;
546         }
547         two_line_text_cb.set_active (fp.twolinetext ());
548         auto_pluginui_cb.set_active (fp.auto_pluginui ());
549 }
550
551 void
552 FP8GUI::clock_mode_changed ()
553 {
554         string str = clock_combo.get_active_text();
555         if (str == _("BBT")) {
556                 fp.set_clock_mode (2);
557         } else if (str == _("Timecode + BBT")) {
558                 fp.set_clock_mode (3);
559         } else {
560                 fp.set_clock_mode (1);
561         }
562 }
563
564 void
565 FP8GUI::scribble_mode_changed ()
566 {
567         string str = scribble_combo.get_active_text();
568         if (str == _("Off")) {
569                 fp.set_scribble_mode (0);
570         } else if (str == _("Meter")) {
571                 fp.set_scribble_mode (1);
572         } else if (str == _("Pan")) {
573                 fp.set_scribble_mode (2);
574         } else {
575                 fp.set_scribble_mode (3);
576         }
577 }
578
579 void
580 FP8GUI::twolinetext_toggled ()
581 {
582         fp.set_two_line_text (two_line_text_cb.get_active ());
583 }
584
585
586 void
587 FP8GUI::auto_pluginui_toggled ()
588 {
589         fp.set_auto_pluginui (auto_pluginui_cb.get_active ());
590 }