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