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