OSC make strippable null on drop references
[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 #else
99         string name = "faderport8-small.png";
100 #endif
101         Searchpath spath(ARDOUR::ardour_data_search_path());
102         spath.add_subdirectory_to_paths ("icons");
103         find_file (spath, name, data_file_path);
104         if (!data_file_path.empty()) {
105                 image.set (data_file_path);
106                 hpacker.pack_start (image, false, false);
107         }
108
109         Gtk::Label* l;
110         int row = 0;
111
112         input_combo.pack_start (midi_port_columns.short_name);
113         output_combo.pack_start (midi_port_columns.short_name);
114
115         build_prefs_combos ();
116         update_prefs_combos ();
117
118         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &input_combo, true));
119         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &output_combo, false));
120
121         clock_combo.signal_changed().connect (sigc::mem_fun (*this, &FP8GUI::clock_mode_changed));
122         scribble_combo.signal_changed().connect (sigc::mem_fun (*this, &FP8GUI::scribble_mode_changed));
123         two_line_text_cb.signal_toggled().connect(sigc::mem_fun (*this, &FP8GUI::twolinetext_toggled));
124         auto_pluginui_cb.signal_toggled().connect(sigc::mem_fun (*this, &FP8GUI::auto_pluginui_toggled));
125
126         l = manage (new Gtk::Label);
127         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
128         l->set_alignment (1.0, 0.5);
129         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
130         table.attach (input_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
131         row++;
132
133         l = manage (new Gtk::Label);
134         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
135         l->set_alignment (1.0, 0.5);
136         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
137         table.attach (output_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
138         row++;
139
140         Gtk::HSeparator *hsep = manage(new Gtk::HSeparator);
141         table.attach (*hsep, 0, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 6);
142         row++;
143
144         hpacker.pack_start (table, true, true);
145         pack_start (hpacker, false, false);
146
147         /* actions */
148         build_available_action_menu ();
149
150         int action_row = 0;
151         int action_col = 0;
152         Gtk::Alignment* align;
153
154         for (FP8Controls::UserButtonMap::const_iterator i = fp.control().user_buttons ().begin ();
155                         i != fp.control().user_buttons ().end (); ++i) {
156                 Gtk::ComboBox* user_combo = manage (new Gtk::ComboBox);
157                 build_action_combo (*user_combo, i->first);
158                 l = manage (new Gtk::Label);
159                 l->set_markup (string_compose ("<span weight=\"bold\">%1:</span>", i->second));
160                 l->set_alignment (1.0, 0.5);
161                 table.attach (*l, 3 * action_col, 3 * action_col + 1, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
162                 align = manage (new Alignment);
163                 align->set (0.0, 0.5);
164                 align->add (*user_combo);
165                 table.attach (*align, 3 * action_col + 1, 3 * action_col + 2, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
166
167                 if (++action_row == 4) {
168                         action_row = 0;
169                         ++action_col;
170                 }
171         }
172
173         for (int c = 0; c < 2; ++c) {
174                 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
175                 table.attach (*vsep, 3 * c + 2, 3 * c + 3, row, row + 4, AttachOptions(0), AttachOptions(FILL), 6, 0);
176         }
177
178         row += 4;
179
180         hsep = manage(new Gtk::HSeparator);
181         table.attach (*hsep, 0, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 6);
182         row++;
183
184         l = manage (new Gtk::Label);
185         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock:")));
186         l->set_alignment (1.0, 0.5);
187         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
188         table.attach (clock_combo, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
189
190         table.attach (two_line_text_cb, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
191         row++;
192
193         l = manage (new Gtk::Label);
194         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Display:")));
195         l->set_alignment (1.0, 0.5);
196         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
197         table.attach (scribble_combo, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
198
199         table.attach (auto_pluginui_cb, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
200         row++;
201
202         /* update the port connection combos */
203         update_port_combos ();
204
205         /* catch future changes to connection state */
206         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FP8GUI::connection_handler, this), gui_context());
207 }
208
209 FP8GUI::~FP8GUI ()
210 {
211 }
212
213 void
214 FP8GUI::connection_handler ()
215 {
216         PBD::Unwinder<bool> ici (ignore_active_change, true);
217         update_port_combos ();
218 }
219
220 void
221 FP8GUI::update_port_combos ()
222 {
223         vector<string> midi_inputs;
224         vector<string> midi_outputs;
225
226         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
227         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
228
229         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
230         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
231         bool input_found = false;
232         bool output_found = false;
233         int n;
234
235         input_combo.set_model (input);
236         output_combo.set_model (output);
237
238         Gtk::TreeModel::Children children = input->children();
239         Gtk::TreeModel::Children::iterator i;
240         i = children.begin();
241         ++i; /* skip "Disconnected" */
242
243         for (n = 1;  i != children.end(); ++i, ++n) {
244                 string port_name = (*i)[midi_port_columns.full_name];
245                 if (fp.input_port()->connected_to (port_name)) {
246                         input_combo.set_active (n);
247                         input_found = true;
248                         break;
249                 }
250         }
251
252         if (!input_found) {
253                 input_combo.set_active (0); /* disconnected */
254         }
255
256         children = output->children();
257         i = children.begin();
258         ++i; /* skip "Disconnected" */
259
260         for (n = 1;  i != children.end(); ++i, ++n) {
261                 string port_name = (*i)[midi_port_columns.full_name];
262                 if (fp.output_port()->connected_to (port_name)) {
263                         output_combo.set_active (n);
264                         output_found = true;
265                         break;
266                 }
267         }
268
269         if (!output_found) {
270                 output_combo.set_active (0); /* disconnected */
271         }
272 }
273
274
275 Glib::RefPtr<Gtk::ListStore>
276 FP8GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
277 {
278         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
279         TreeModel::Row row;
280
281         row = *store->append ();
282         row[midi_port_columns.full_name] = string();
283         row[midi_port_columns.short_name] = _("Disconnected");
284
285         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
286                 row = *store->append ();
287                 row[midi_port_columns.full_name] = *p;
288                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
289                 if (pn.empty ()) {
290                         pn = (*p).substr ((*p).find (':') + 1);
291                 }
292                 row[midi_port_columns.short_name] = pn;
293         }
294
295         return store;
296 }
297
298 void
299 FP8GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
300 {
301         if (ignore_active_change) {
302                 return;
303         }
304
305         TreeModel::iterator active = combo->get_active ();
306         string new_port = (*active)[midi_port_columns.full_name];
307
308         if (new_port.empty()) {
309                 if (for_input) {
310                         fp.input_port()->disconnect_all ();
311                 } else {
312                         fp.output_port()->disconnect_all ();
313                 }
314
315                 return;
316         }
317
318         if (for_input) {
319                 if (!fp.input_port()->connected_to (new_port)) {
320                         fp.input_port()->disconnect_all ();
321                         fp.input_port()->connect (new_port);
322                 }
323         } else {
324                 if (!fp.output_port()->connected_to (new_port)) {
325                         fp.output_port()->disconnect_all ();
326                         fp.output_port()->connect (new_port);
327                 }
328         }
329 }
330
331
332
333 void
334 FP8GUI::build_available_action_menu ()
335 {
336         /* build a model of all available actions (needs to be tree structured
337          * more)
338          */
339
340         available_action_model = TreeStore::create (action_columns);
341
342         vector<string> paths;
343         vector<string> labels;
344         vector<string> tooltips;
345         vector<string> keys;
346         vector<Glib::RefPtr<Gtk::Action> > actions;
347
348         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
349
350         typedef std::map<string,TreeIter> NodeMap;
351         NodeMap nodes;
352         NodeMap::iterator r;
353
354
355         vector<string>::iterator k;
356         vector<string>::iterator p;
357         vector<string>::iterator t;
358         vector<string>::iterator l;
359
360         available_action_model->clear ();
361
362         TreeIter rowp;
363         TreeModel::Row parent;
364
365         /* Disabled item (row 0) */
366
367         rowp = available_action_model->append();
368         parent = *(rowp);
369         parent[action_columns.name] = _("Disabled");
370
371         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
372
373                 TreeModel::Row row;
374                 vector<string> parts;
375
376                 parts.clear ();
377
378                 split (*p, parts, '/');
379
380                 if (parts.empty()) {
381                         continue;
382                 }
383
384                 //kinda kludgy way to avoid displaying menu items as mappable
385                 if ( parts[1] == _("Main_menu") )
386                         continue;
387                 if ( parts[1] == _("JACK") )
388                         continue;
389                 if ( parts[1] == _("redirectmenu") )
390                         continue;
391                 if ( parts[1] == _("Editor_menus") )
392                         continue;
393                 if ( parts[1] == _("RegionList") )
394                         continue;
395                 if ( parts[1] == _("ProcessorMenu") )
396                         continue;
397
398                 if ((r = nodes.find (parts[1])) == nodes.end()) {
399
400                         /* top level is missing */
401
402                         TreeIter rowp;
403                         TreeModel::Row parent;
404                         rowp = available_action_model->append();
405                         nodes[parts[1]] = rowp;
406                         parent = *(rowp);
407                         parent[action_columns.name] = parts[1];
408
409                         row = *(available_action_model->append (parent.children()));
410
411                 } else {
412
413                         row = *(available_action_model->append ((*r->second)->children()));
414
415                 }
416
417                 /* add this action */
418
419                 if (l->empty ()) {
420                         row[action_columns.name] = *t;
421                         action_map[*t] = *p;
422                 } else {
423                         row[action_columns.name] = *l;
424                         action_map[*l] = *p;
425                 }
426
427                 string path = (*p);
428                 /* ControlProtocol::access_action() is not interested in the
429                    legacy "<Actions>/" prefix part of a path.
430                 */
431                 path = path.substr (strlen ("<Actions>/"));
432
433                 row[action_columns.path] = path;
434         }
435 }
436
437 bool
438 FP8GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const& action_path, TreeModel::iterator* found)
439 {
440         TreeModel::Row row = *iter;
441         string path = row[action_columns.path];
442
443         if (path == action_path) {
444                 *found = iter;
445                 return true;
446         }
447
448         return false;
449 }
450
451 void
452 FP8GUI::build_action_combo (Gtk::ComboBox& cb, FP8Controls::ButtonId id)
453 {
454         cb.set_model (available_action_model);
455         cb.pack_start (action_columns.name);
456
457         /* set the active "row" to the right value for the current button binding */
458         string current_action = fp.get_button_action (id, false); /* lookup release action */
459
460         if (current_action.empty()) {
461                 cb.set_active (0); /* "disabled" */
462         } else {
463                 TreeModel::iterator iter = available_action_model->children().end();
464
465                 available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FP8GUI::find_action_in_model), current_action, &iter));
466
467                 if (iter != available_action_model->children().end()) {
468                         cb.set_active (iter);
469                 } else {
470                         cb.set_active (0);
471                 }
472         }
473         /* bind signal _after_ setting the current value */
474         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::action_changed), &cb, id));
475 }
476
477 void
478 FP8GUI::action_changed (Gtk::ComboBox* cb, FP8Controls::ButtonId id)
479 {
480         TreeModel::const_iterator row = cb->get_active ();
481         string action_path = (*row)[action_columns.path];
482         fp.set_button_action (id, false, action_path);
483 }
484
485
486 void
487 FP8GUI::build_prefs_combos ()
488 {
489         vector<string> clock_strings;
490         vector<string> scribble_strings;
491
492         //clock_strings.push_back (_("Off"));
493         clock_strings.push_back (_("Timecode"));
494         clock_strings.push_back (_("BBT"));
495         clock_strings.push_back (_("Timecode + BBT"));
496
497         scribble_strings.push_back (_("Off"));
498         scribble_strings.push_back (_("Meter"));
499         scribble_strings.push_back (_("Pan"));
500         scribble_strings.push_back (_("Meter + Pan"));
501
502         set_popdown_strings (clock_combo, clock_strings);
503         set_popdown_strings (scribble_combo, scribble_strings);
504 }
505
506 void
507 FP8GUI::update_prefs_combos ()
508 {
509         switch (fp.clock_mode()) {
510                 default:
511                         clock_combo.set_active_text (_("Off"));
512                         break;
513                 case 1:
514                         clock_combo.set_active_text (_("Timecode"));
515                         break;
516                 case 2:
517                         clock_combo.set_active_text (_("BBT"));
518                         break;
519                 case 3:
520                         clock_combo.set_active_text (_("Timecode + BBT"));
521                         break;
522         }
523
524         switch (fp.scribble_mode()) {
525                 default:
526                         scribble_combo.set_active_text (_("Off"));
527                         break;
528                 case 1:
529                         scribble_combo.set_active_text (_("Meter"));
530                         break;
531                 case 2:
532                         scribble_combo.set_active_text (_("Pan"));
533                         break;
534                 case 3:
535                         scribble_combo.set_active_text (_("Meter + Pan"));
536                         break;
537         }
538         two_line_text_cb.set_active (fp.twolinetext ());
539         auto_pluginui_cb.set_active (fp.auto_pluginui ());
540 }
541
542 void
543 FP8GUI::clock_mode_changed ()
544 {
545         string str = clock_combo.get_active_text();
546         if (str == _("BBT")) {
547                 fp.set_clock_mode (2);
548         } else if (str == _("Timecode + BBT")) {
549                 fp.set_clock_mode (3);
550         } else {
551                 fp.set_clock_mode (1);
552         }
553 }
554
555 void
556 FP8GUI::scribble_mode_changed ()
557 {
558         string str = scribble_combo.get_active_text();
559         if (str == _("Off")) {
560                 fp.set_scribble_mode (0);
561         } else if (str == _("Meter")) {
562                 fp.set_scribble_mode (1);
563         } else if (str == _("Pan")) {
564                 fp.set_scribble_mode (2);
565         } else {
566                 fp.set_scribble_mode (3);
567         }
568 }
569
570 void
571 FP8GUI::twolinetext_toggled ()
572 {
573         fp.set_two_line_text (two_line_text_cb.get_active ());
574 }
575
576
577 void
578 FP8GUI::auto_pluginui_toggled ()
579 {
580         fp.set_auto_pluginui (auto_pluginui_cb.get_active ());
581 }