Use Stripable::Sorter in libs
[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 {
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 = "faderport8-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         int row = 0;
105
106         input_combo.pack_start (midi_port_columns.short_name);
107         output_combo.pack_start (midi_port_columns.short_name);
108
109         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &input_combo, true));
110         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &output_combo, false));
111
112         l = manage (new Gtk::Label);
113         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
114         l->set_alignment (1.0, 0.5);
115         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
116         table.attach (input_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
117         row++;
118
119         l = manage (new Gtk::Label);
120         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
121         l->set_alignment (1.0, 0.5);
122         table.attach (*l, 1, 4, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
123         table.attach (output_combo, 4, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
124         row++;
125
126         Gtk::HSeparator *hsep = manage(new Gtk::HSeparator);
127         table.attach (*hsep, 0, 8, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 6);
128         row++;
129
130         hpacker.pack_start (table, true, true);
131         pack_start (hpacker, false, false);
132
133         /* actions */
134         build_available_action_menu ();
135
136         int action_row = 0;
137         int action_col = 0;
138         Gtk::Alignment* align;
139
140         for (FP8Controls::UserButtonMap::const_iterator i = fp.control().user_buttons ().begin ();
141                         i != fp.control().user_buttons ().end (); ++i) {
142                 Gtk::ComboBox* user_combo = manage (new Gtk::ComboBox);
143                 build_action_combo (*user_combo, i->first);
144                 l = manage (new Gtk::Label);
145                 l->set_markup (string_compose ("<span weight=\"bold\">%1:</span>", i->second));
146                 l->set_alignment (1.0, 0.5);
147                 table.attach (*l, 3 * action_col, 3 * action_col + 1, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
148                 align = manage (new Alignment);
149                 align->set (0.0, 0.5);
150                 align->add (*user_combo);
151                 table.attach (*align, 3 * action_col + 1, 3 * action_col + 2, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
152
153                 if (++action_row == 4) {
154                         action_row = 0;
155                         ++action_col;
156                 }
157         }
158
159         for (int c = 0; c < 2; ++c) {
160                 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
161                 table.attach (*vsep, 3 * c + 2, 3 * c + 3, row, row + 4, AttachOptions(0), AttachOptions(FILL), 6, 0);
162         }
163
164         /* update the port connection combos */
165         update_port_combos ();
166
167         /* catch future changes to connection state */
168         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FP8GUI::connection_handler, this), gui_context());
169 }
170
171 FP8GUI::~FP8GUI ()
172 {
173 }
174
175 void
176 FP8GUI::connection_handler ()
177 {
178         PBD::Unwinder<bool> ici (ignore_active_change, true);
179         update_port_combos ();
180 }
181
182 void
183 FP8GUI::update_port_combos ()
184 {
185         vector<string> midi_inputs;
186         vector<string> midi_outputs;
187
188         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
189         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
190
191         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
192         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
193         bool input_found = false;
194         bool output_found = false;
195         int n;
196
197         input_combo.set_model (input);
198         output_combo.set_model (output);
199
200         Gtk::TreeModel::Children children = input->children();
201         Gtk::TreeModel::Children::iterator i;
202         i = children.begin();
203         ++i; /* skip "Disconnected" */
204
205         for (n = 1;  i != children.end(); ++i, ++n) {
206                 string port_name = (*i)[midi_port_columns.full_name];
207                 if (fp.input_port()->connected_to (port_name)) {
208                         input_combo.set_active (n);
209                         input_found = true;
210                         break;
211                 }
212         }
213
214         if (!input_found) {
215                 input_combo.set_active (0); /* disconnected */
216         }
217
218         children = output->children();
219         i = children.begin();
220         ++i; /* skip "Disconnected" */
221
222         for (n = 1;  i != children.end(); ++i, ++n) {
223                 string port_name = (*i)[midi_port_columns.full_name];
224                 if (fp.output_port()->connected_to (port_name)) {
225                         output_combo.set_active (n);
226                         output_found = true;
227                         break;
228                 }
229         }
230
231         if (!output_found) {
232                 output_combo.set_active (0); /* disconnected */
233         }
234 }
235
236
237 Glib::RefPtr<Gtk::ListStore>
238 FP8GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
239 {
240         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
241         TreeModel::Row row;
242
243         row = *store->append ();
244         row[midi_port_columns.full_name] = string();
245         row[midi_port_columns.short_name] = _("Disconnected");
246
247         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
248                 row = *store->append ();
249                 row[midi_port_columns.full_name] = *p;
250                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
251                 if (pn.empty ()) {
252                         pn = (*p).substr ((*p).find (':') + 1);
253                 }
254                 row[midi_port_columns.short_name] = pn;
255         }
256
257         return store;
258 }
259
260 void
261 FP8GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
262 {
263         if (ignore_active_change) {
264                 return;
265         }
266
267         TreeModel::iterator active = combo->get_active ();
268         string new_port = (*active)[midi_port_columns.full_name];
269
270         if (new_port.empty()) {
271                 if (for_input) {
272                         fp.input_port()->disconnect_all ();
273                 } else {
274                         fp.output_port()->disconnect_all ();
275                 }
276
277                 return;
278         }
279
280         if (for_input) {
281                 if (!fp.input_port()->connected_to (new_port)) {
282                         fp.input_port()->disconnect_all ();
283                         fp.input_port()->connect (new_port);
284                 }
285         } else {
286                 if (!fp.output_port()->connected_to (new_port)) {
287                         fp.output_port()->disconnect_all ();
288                         fp.output_port()->connect (new_port);
289                 }
290         }
291 }
292
293
294
295 void
296 FP8GUI::build_available_action_menu ()
297 {
298         /* build a model of all available actions (needs to be tree structured
299          * more)
300          */
301
302         available_action_model = TreeStore::create (action_columns);
303
304         vector<string> paths;
305         vector<string> labels;
306         vector<string> tooltips;
307         vector<string> keys;
308         vector<Glib::RefPtr<Gtk::Action> > actions;
309
310         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
311
312         typedef std::map<string,TreeIter> NodeMap;
313         NodeMap nodes;
314         NodeMap::iterator r;
315
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         TreeIter rowp;
325         TreeModel::Row parent;
326
327         /* Disabled item (row 0) */
328
329         rowp = available_action_model->append();
330         parent = *(rowp);
331         parent[action_columns.name] = _("Disabled");
332
333         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
334
335                 TreeModel::Row row;
336                 vector<string> parts;
337
338                 parts.clear ();
339
340                 split (*p, parts, '/');
341
342                 if (parts.empty()) {
343                         continue;
344                 }
345
346                 //kinda kludgy way to avoid displaying menu items as mappable
347                 if ( parts[1] == _("Main_menu") )
348                         continue;
349                 if ( parts[1] == _("JACK") )
350                         continue;
351                 if ( parts[1] == _("redirectmenu") )
352                         continue;
353                 if ( parts[1] == _("Editor_menus") )
354                         continue;
355                 if ( parts[1] == _("RegionList") )
356                         continue;
357                 if ( parts[1] == _("ProcessorMenu") )
358                         continue;
359
360                 if ((r = nodes.find (parts[1])) == nodes.end()) {
361
362                         /* top level is missing */
363
364                         TreeIter rowp;
365                         TreeModel::Row parent;
366                         rowp = available_action_model->append();
367                         nodes[parts[1]] = rowp;
368                         parent = *(rowp);
369                         parent[action_columns.name] = parts[1];
370
371                         row = *(available_action_model->append (parent.children()));
372
373                 } else {
374
375                         row = *(available_action_model->append ((*r->second)->children()));
376
377                 }
378
379                 /* add this action */
380
381                 if (l->empty ()) {
382                         row[action_columns.name] = *t;
383                         action_map[*t] = *p;
384                 } else {
385                         row[action_columns.name] = *l;
386                         action_map[*l] = *p;
387                 }
388
389                 string path = (*p);
390                 /* ControlProtocol::access_action() is not interested in the
391                    legacy "<Actions>/" prefix part of a path.
392                 */
393                 path = path.substr (strlen ("<Actions>/"));
394
395                 row[action_columns.path] = path;
396         }
397 }
398
399 bool
400 FP8GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const& action_path, TreeModel::iterator* found)
401 {
402         TreeModel::Row row = *iter;
403         string path = row[action_columns.path];
404
405         if (path == action_path) {
406                 *found = iter;
407                 return true;
408         }
409
410         return false;
411 }
412
413 void
414 FP8GUI::build_action_combo (Gtk::ComboBox& cb, FP8Controls::ButtonId id)
415 {
416         cb.set_model (available_action_model);
417         cb.pack_start (action_columns.name);
418
419         /* set the active "row" to the right value for the current button binding */
420         string current_action = fp.get_button_action (id, false); /* lookup release action */
421
422         if (current_action.empty()) {
423                 cb.set_active (0); /* "disabled" */
424         } else {
425                 TreeModel::iterator iter = available_action_model->children().end();
426
427                 available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FP8GUI::find_action_in_model), current_action, &iter));
428
429                 if (iter != available_action_model->children().end()) {
430                         cb.set_active (iter);
431                 } else {
432                         cb.set_active (0);
433                 }
434         }
435         /* bind signal _after_ setting the current value */
436         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::action_changed), &cb, id));
437 }
438
439 void
440 FP8GUI::action_changed (Gtk::ComboBox* cb, FP8Controls::ButtonId id)
441 {
442         TreeModel::const_iterator row = cb->get_active ();
443         string action_path = (*row)[action_columns.path];
444         fp.set_button_action (id, false, action_path);
445 }