faderport: GUI tweaks
[ardour.git] / libs / surfaces / faderport / gui.cc
1 /*
2     Copyright (C) 2015 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
27
28 #include "gtkmm2ext/actions.h"
29 #include "gtkmm2ext/gtk_ui.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/utils.h"
32
33 #include "ardour/audioengine.h"
34 #include "ardour/filesystem_paths.h"
35
36 #include "faderport.h"
37 #include "gui.h"
38
39 #include "i18n.h"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
44 using namespace std;
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47
48 void*
49 FaderPort::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<FaderPort*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 FaderPort::tear_down_gui ()
60 {
61         if (gui) {
62                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
63                 if (w) {
64                         w->hide();
65                         delete w;
66                 }
67         }
68         delete static_cast<FPGUI*> (gui);
69         gui = 0;
70 }
71
72 void
73 FaderPort::build_gui ()
74 {
75         gui = (void*) new FPGUI (*this);
76 }
77
78 /*--------------------*/
79
80 FPGUI::FPGUI (FaderPort& p)
81         : fp (p)
82         , table (2, 5)
83         , action_table (4, 4)
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 = "faderport-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         Gtk::Alignment* align;
105         int row = 0;
106
107         input_combo.pack_start (midi_port_columns.short_name);
108         output_combo.pack_start (midi_port_columns.short_name);
109
110         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &input_combo, true));
111         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &output_combo, false));
112
113         l = manage (new Gtk::Label);
114         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
115         l->set_alignment (1.0, 0.5);
116         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
117         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
118         row++;
119
120         l = manage (new Gtk::Label);
121         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
122         l->set_alignment (1.0, 0.5);
123         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
124         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
125         row++;
126
127         build_mix_action_combo (mix_combo[0], FaderPort::ButtonState(0));
128         build_mix_action_combo (mix_combo[1], FaderPort::ShiftDown);
129         build_mix_action_combo (mix_combo[2], FaderPort::LongPress);
130
131         build_proj_action_combo (proj_combo[0], FaderPort::ButtonState(0));
132         build_proj_action_combo (proj_combo[1], FaderPort::ShiftDown);
133         build_proj_action_combo (proj_combo[2], FaderPort::LongPress);
134
135         build_trns_action_combo (trns_combo[0], FaderPort::ButtonState(0));
136         build_trns_action_combo (trns_combo[1], FaderPort::ShiftDown);
137         build_trns_action_combo (trns_combo[2], FaderPort::LongPress);
138
139         action_table.set_row_spacings (4);
140         action_table.set_col_spacings (6);
141         action_table.set_border_width (12);
142         action_table.set_homogeneous (false);
143
144         int action_row = 0;
145
146         l = manage (new Gtk::Label);
147         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Press Action")));
148         l->set_alignment (0.5, 0.5);
149         action_table.attach (*l, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
150         l = manage (new Gtk::Label);
151         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Shift-Press Action")));
152         l->set_alignment (0.5, 0.5);
153         action_table.attach (*l, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
154         l = manage (new Gtk::Label);
155         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Long Press Action")));
156         l->set_alignment (0.5, 0.5);
157         action_table.attach (*l, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
158         action_row++;
159
160         l = manage (new Gtk::Label);
161         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Mix")));
162         l->set_alignment (1.0, 0.5);
163         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
164         align = manage (new Alignment);
165         align->set (0.0, 0.5);
166         align->add (mix_combo[0]);
167         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
168         align = manage (new Alignment);
169         align->set (0.0, 0.5);
170         align->add (mix_combo[1]);
171         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
172         align = manage (new Alignment);
173         align->set (0.0, 0.5);
174         align->add (mix_combo[2]);
175         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
176         action_row++;
177
178         l = manage (new Gtk::Label);
179         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Proj")));
180         l->set_alignment (1.0, 0.5);
181         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
182         align = manage (new Alignment);
183         align->set (0.0, 0.5);
184         align->add (proj_combo[0]);
185         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
186         align = manage (new Alignment);
187         align->set (0.0, 0.5);
188         align->add (proj_combo[1]);
189         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
190         align = manage (new Alignment);
191         align->set (0.0, 0.5);
192         align->add (proj_combo[2]);
193         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
194         action_row++;
195
196         l = manage (new Gtk::Label);
197         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Trns")));
198         l->set_alignment (1.0, 0.5);
199         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
200         align = manage (new Alignment);
201         align->set (0.0, 0.5);
202         align->add (trns_combo[0]);
203         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
204         align = manage (new Alignment);
205         align->set (0.0, 0.5);
206         align->add (trns_combo[1]);
207         action_table.attach (*align, 2, 3, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
208         align = manage (new Alignment);
209         align->set (0.0, 0.5);
210         align->add (trns_combo[2]);
211         action_table.attach (*align, 3, 4, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
212         action_row++;
213
214         table.attach (action_table, 0, 5, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
215         row++;
216
217         hpacker.pack_start (table, true, true);
218         pack_start (hpacker, false, false);
219
220         /* update the port connection combos */
221
222         update_port_combos ();
223
224         /* catch future changes to connection state */
225
226         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FPGUI::connection_handler, this), gui_context());
227 }
228
229 FPGUI::~FPGUI ()
230 {
231 }
232
233 void
234 FPGUI::connection_handler ()
235 {
236         /* ignore all changes to combobox active strings here, because we're
237            updating them to match a new ("external") reality - we were called
238            because port connections have changed.
239         */
240
241         PBD::Unwinder<bool> ici (ignore_active_change, true);
242
243         update_port_combos ();
244 }
245
246 void
247 FPGUI::update_port_combos ()
248 {
249         vector<string> midi_inputs;
250         vector<string> midi_outputs;
251
252         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
253         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
254
255         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
256         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
257         bool input_found = false;
258         bool output_found = false;
259         int n;
260
261         input_combo.set_model (input);
262         output_combo.set_model (output);
263
264         Gtk::TreeModel::Children children = input->children();
265         Gtk::TreeModel::Children::iterator i;
266         i = children.begin();
267         ++i; /* skip "Disconnected" */
268
269
270         for (n = 1;  i != children.end(); ++i, ++n) {
271                 string port_name = (*i)[midi_port_columns.full_name];
272                 if (fp.input_port()->connected_to (port_name)) {
273                         input_combo.set_active (n);
274                         input_found = true;
275                         break;
276                 }
277         }
278
279         if (!input_found) {
280                 input_combo.set_active (0); /* disconnected */
281         }
282
283         children = output->children();
284         i = children.begin();
285         ++i; /* skip "Disconnected" */
286
287         for (n = 1;  i != children.end(); ++i, ++n) {
288                 string port_name = (*i)[midi_port_columns.full_name];
289                 if (fp.output_port()->connected_to (port_name)) {
290                         output_combo.set_active (n);
291                         output_found = true;
292                         break;
293                 }
294         }
295
296         if (!output_found) {
297                 output_combo.set_active (0); /* disconnected */
298         }
299 }
300
301 void
302 FPGUI::build_available_action_menu ()
303 {
304         /* build a model of all available actions (needs to be tree structured
305          * more)
306          */
307
308         available_action_model = TreeStore::create (action_columns);
309
310         vector<string> paths;
311         vector<string> labels;
312         vector<string> tooltips;
313         vector<string> keys;
314         vector<AccelKey> bindings;
315         typedef std::map<string,TreeIter> NodeMap;
316         NodeMap nodes;
317         NodeMap::iterator r;
318
319         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
320
321         vector<string>::iterator k;
322         vector<string>::iterator p;
323         vector<string>::iterator t;
324         vector<string>::iterator l;
325
326         available_action_model->clear ();
327
328         /* Because there are button bindings built in that are not
329         in the key binding map, there needs to be a way to undo
330         a profile edit. */
331         TreeIter rowp;
332         TreeModel::Row parent;
333         rowp = available_action_model->append();
334         parent = *(rowp);
335         parent[action_columns.name] = _("Remove Binding");
336
337         /* Key aliasing */
338
339         rowp = available_action_model->append();
340         parent = *(rowp);
341         parent[action_columns.name] = _("Shift");
342         rowp = available_action_model->append();
343         parent = *(rowp);
344         parent[action_columns.name] = _("Control");
345         rowp = available_action_model->append();
346         parent = *(rowp);
347         parent[action_columns.name] = _("Option");
348         rowp = available_action_model->append();
349         parent = *(rowp);
350         parent[action_columns.name] = _("CmdAlt");
351
352
353         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
354
355                 TreeModel::Row row;
356                 vector<string> parts;
357
358                 parts.clear ();
359
360                 split (*p, parts, '/');
361
362                 if (parts.empty()) {
363                         continue;
364                 }
365
366                 //kinda kludgy way to avoid displaying menu items as mappable
367                 if ( parts[1] == _("Main_menu") )
368                         continue;
369                 if ( parts[1] == _("JACK") )
370                         continue;
371                 if ( parts[1] == _("redirectmenu") )
372                         continue;
373                 if ( parts[1] == _("Editor_menus") )
374                         continue;
375                 if ( parts[1] == _("RegionList") )
376                         continue;
377                 if ( parts[1] == _("ProcessorMenu") )
378                         continue;
379
380                 if ((r = nodes.find (parts[1])) == nodes.end()) {
381
382                         /* top level is missing */
383
384                         TreeIter rowp;
385                         TreeModel::Row parent;
386                         rowp = available_action_model->append();
387                         nodes[parts[1]] = rowp;
388                         parent = *(rowp);
389                         parent[action_columns.name] = parts[1];
390
391                         row = *(available_action_model->append (parent.children()));
392
393                 } else {
394
395                         row = *(available_action_model->append ((*r->second)->children()));
396
397                 }
398
399                 /* add this action */
400
401                 if (l->empty ()) {
402                         row[action_columns.name] = *t;
403                         action_map[*t] = *p;
404                 } else {
405                         row[action_columns.name] = *l;
406                         action_map[*l] = *p;
407                 }
408
409                 row[action_columns.path] = (*p);
410         }
411 }
412
413 void
414 FPGUI::action_changed (Gtk::ComboBox* cb, FaderPort::ButtonID id, FaderPort::ButtonState bs)
415 {
416         TreeModel::const_iterator row = cb->get_active ();
417         string action_path = (*row)[action_columns.path];
418
419         /* release binding */
420         fp.set_action (id, action_path, false, bs);
421 }
422
423 void
424 FPGUI::build_action_combo (Gtk::ComboBox& cb, vector<pair<string,string> > const & actions, FaderPort::ButtonID id, FaderPort::ButtonState bs)
425 {
426         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (action_columns));
427         TreeIter rowp;
428         TreeModel::Row row;
429         string current_action = fp.get_action (id, false, bs); /* lookup release action */
430         int active_row = -1;
431         int n;
432         vector<pair<string,string> >::const_iterator i;
433
434         rowp = model->append();
435         row = *(rowp);
436         row[action_columns.name] = _("Disabled");
437         row[action_columns.path] = string();
438
439         if (current_action.empty()) {
440                 active_row = 0;
441         }
442
443         for (i = actions.begin(), n = 0; i != actions.end(); ++i, ++n) {
444                 rowp = model->append();
445                 row = *(rowp);
446                 row[action_columns.name] = i->first;
447                 row[action_columns.path] = i->second;
448                 if (current_action == i->second) {
449                         active_row = n+1;
450                 }
451         }
452
453         cb.set_model (model);
454         cb.pack_start (action_columns.name);
455
456         if (active_row >= 0) {
457                 cb.set_active (active_row);
458         }
459
460         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, id, bs));
461 }
462
463 void
464 FPGUI::build_mix_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
465 {
466         vector<pair<string,string> > actions;
467
468         actions.push_back (make_pair (string (_("Toggle Editor & Mixer Windows")), string (X_("Common/toggle-editor-mixer"))));
469         actions.push_back (make_pair (string (_("Show/Hide Editor mixer strip")), string (X_("Editor/show-editor-mixer"))));
470
471         build_action_combo (cb, actions, FaderPort::Mix, bs);
472 }
473
474 void
475 FPGUI::build_proj_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
476 {
477         vector<pair<string,string> > actions;
478
479         actions.push_back (make_pair (string("Toggle Meterbridge"), string(X_("Common/toggle-meterbridge"))));
480         actions.push_back (make_pair (string("Toggle Summary"), string(X_("Editor/ToggleSummary"))));
481         actions.push_back (make_pair (string("Toggle Editor Lists"), string(X_("Editor/show-editor-list"))));
482         actions.push_back (make_pair (string (_("Zoom to Session")), string (X_("Editor/zoom-to-session"))));
483         actions.push_back (make_pair (string (_("Zoom In")), string (X_("Editor/temporal-zoom-in"))));
484         actions.push_back (make_pair (string (_("Zoom Out")), string (X_("Editor/temporal-zoom-out"))));
485
486         build_action_combo (cb, actions, FaderPort::Proj, bs);
487 }
488
489 void
490 FPGUI::build_trns_action_combo (Gtk::ComboBox& cb, FaderPort::ButtonState bs)
491 {
492         vector<pair<string,string> > actions;
493
494         actions.push_back (make_pair (string("Toggle Locations"), string(X_("Window/toggle-locations"))));
495         actions.push_back (make_pair (string("Toggle Metronome"), string(X_("Transport/ToggleClick"))));
496         actions.push_back (make_pair (string("Toggle Sync"), string(X_("Transport/ToggleExternalSync"))));
497         actions.push_back (make_pair (string("Set Playhead @pointer"), string(X_("Editor/set-playhead"))));
498
499         build_action_combo (cb, actions, FaderPort::Trns, bs);
500 }
501
502 Glib::RefPtr<Gtk::ListStore>
503 FPGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
504 {
505         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
506         TreeModel::Row row;
507
508         row = *store->append ();
509         row[midi_port_columns.full_name] = string();
510         row[midi_port_columns.short_name] = _("Disconnected");
511
512         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
513                 row = *store->append ();
514                 row[midi_port_columns.full_name] = *p;
515                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
516                 if (pn.empty ()) {
517                         pn = (*p).substr ((*p).find (':') + 1);
518                 }
519                 row[midi_port_columns.short_name] = pn;
520         }
521
522         return store;
523 }
524
525 void
526 FPGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
527 {
528         if (ignore_active_change) {
529                 return;
530         }
531
532         TreeModel::iterator active = combo->get_active ();
533         string new_port = (*active)[midi_port_columns.full_name];
534
535         if (new_port.empty()) {
536                 if (for_input) {
537                         fp.input_port()->disconnect_all ();
538                 } else {
539                         fp.output_port()->disconnect_all ();
540                 }
541
542                 return;
543         }
544
545         if (for_input) {
546                 if (!fp.input_port()->connected_to (new_port)) {
547                         fp.input_port()->disconnect_all ();
548                         fp.input_port()->connect (new_port);
549                 }
550         } else {
551                 if (!fp.output_port()->connected_to (new_port)) {
552                         fp.output_port()->disconnect_all ();
553                         fp.output_port()->connect (new_port);
554                 }
555         }
556 }