faderport: add two functioning actions for the mix button
[ardour.git] / libs / surfaces / faderport / gui.cc
1 /*
2     Copyright (C) 2009-2012 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 <iostream>
21 #include <list>
22 #include <vector>
23 #include <string>
24
25 #include <gtkmm/adjustment.h>
26 #include <gtkmm/alignment.h>
27 #include <gtkmm/box.h>
28 #include <gtkmm/combobox.h>
29 #include <gtkmm/liststore.h>
30 #include <gtkmm/label.h>
31 #include <gtkmm/spinbutton.h>
32 #include <gtkmm/table.h>
33 #include <gtkmm/treestore.h>
34
35 namespace Gtk {
36         class CellRendererCombo;
37 }
38
39 #include "pbd/unwind.h"
40 #include "pbd/strsplit.h"
41
42 #include "gtkmm2ext/actions.h"
43 #include "gtkmm2ext/gtk_ui.h"
44 #include "gtkmm2ext/gui_thread.h"
45 #include "gtkmm2ext/utils.h"
46
47 #include "ardour/audioengine.h"
48
49 #include "faderport.h"
50
51 #include "i18n.h"
52
53 namespace ArdourSurface {
54
55 class FPGUI : public Gtk::VBox
56 {
57 public:
58         FPGUI (FaderPort&);
59         ~FPGUI ();
60
61 private:
62         FaderPort& fp;
63         Gtk::Table table;
64         Gtk::ComboBox input_combo;
65         Gtk::ComboBox output_combo;
66         Gtk::ComboBox mix_combo;
67         Gtk::ComboBox proj_combo;
68         Gtk::ComboBox trns_combo;
69
70         void update_port_combos (std::vector<std::string> const&, std::vector<std::string> const&);
71         PBD::ScopedConnection connection_change_connection;
72         void connection_handler ();
73
74         struct MidiPortColumns : public Gtk::TreeModel::ColumnRecord {
75                 MidiPortColumns() {
76                         add (short_name);
77                         add (full_name);
78                 }
79                 Gtk::TreeModelColumn<std::string> short_name;
80                 Gtk::TreeModelColumn<std::string> full_name;
81         };
82
83         MidiPortColumns midi_port_columns;
84         bool ignore_active_change;
85
86         Glib::RefPtr<Gtk::ListStore> build_midi_port_list (std::vector<std::string> const & ports, bool for_input);
87         void active_port_changed (Gtk::ComboBox*,bool for_input);
88
89         struct ActionColumns : public Gtk::TreeModel::ColumnRecord {
90                 ActionColumns() {
91                         add (name);
92                         add (path);
93                 }
94                 Gtk::TreeModelColumn<std::string> name;
95                 Gtk::TreeModelColumn<std::string> path;
96         };
97
98         ActionColumns action_columns;
99         Glib::RefPtr<Gtk::TreeStore> available_action_model;
100         std::map<std::string,std::string> action_map; // map from action names to paths
101
102         void build_mix_action_combo (Gtk::ComboBox&);
103         void build_proj_action_combo (Gtk::ComboBox&);
104         void build_trns_action_combo (Gtk::ComboBox&);
105
106         void build_available_action_menu ();
107         void action_changed (Gtk::ComboBox*, FaderPort::ButtonID);
108 };
109
110 }
111
112 using namespace PBD;
113 using namespace ARDOUR;
114 using namespace ArdourSurface;
115 using namespace std;
116 using namespace Gtk;
117 using namespace Gtkmm2ext;
118
119 void*
120 FaderPort::get_gui () const
121 {
122         if (!gui) {
123                 const_cast<FaderPort*>(this)->build_gui ();
124         }
125         static_cast<Gtk::VBox*>(gui)->show_all();
126         return gui;
127 }
128
129 void
130 FaderPort::tear_down_gui ()
131 {
132         if (gui) {
133                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
134                 if (w) {
135                         w->hide();
136                         delete w;
137                 }
138         }
139         delete (FPGUI*) gui;
140         gui = 0;
141 }
142
143 void
144 FaderPort::build_gui ()
145 {
146         gui = (void*) new FPGUI (*this);
147 }
148
149 /*--------------------*/
150
151 FPGUI::FPGUI (FaderPort& p)
152         : fp (p)
153         , table (2, 5)
154         , ignore_active_change (false)
155 {
156         set_border_width (12);
157
158         table.set_row_spacings (4);
159         table.set_col_spacings (6);
160         table.set_border_width (12);
161         table.set_homogeneous (false);
162
163         Gtk::Label* l;
164         Gtk::Alignment* align;
165         int row = 0;
166
167         vector<string> midi_inputs;
168         vector<string> midi_outputs;
169
170         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
171         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
172
173         update_port_combos (midi_inputs, midi_outputs);
174
175         input_combo.pack_start (midi_port_columns.short_name);
176         output_combo.pack_start (midi_port_columns.short_name);
177
178         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &input_combo, true));
179         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::active_port_changed), &output_combo, false));
180
181         l = manage (new Gtk::Label (_("Sends MIDI via:")));
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 (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
185         row++;
186
187         l = manage (new Gtk::Label (_("Receives MIDI via:")));
188         l->set_alignment (1.0, 0.5);
189         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
190         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
191         row++;
192
193         build_mix_action_combo (mix_combo);
194         build_proj_action_combo (proj_combo);
195         build_trns_action_combo (trns_combo);
196
197         l = manage (new Gtk::Label (_("Mix Button")));
198         l->set_alignment (1.0, 0.5);
199         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
200         align = manage (new Alignment);
201         align->set (0.0, 0.5);
202         align->add (mix_combo);
203         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
204         row++;
205
206         l = manage (new Gtk::Label (_("Proj Button")));
207         l->set_alignment (1.0, 0.5);
208         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
209         align = manage (new Alignment);
210         align->set (0.0, 0.5);
211         align->add (proj_combo);
212         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
213         row++;
214
215         l = manage (new Gtk::Label (_("Trns Button")));
216         l->set_alignment (1.0, 0.5);
217         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
218         align = manage (new Alignment);
219         align->set (0.0, 0.5);
220         align->add (trns_combo);
221         table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
222         row++;
223
224         pack_start (table, false, false);
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         vector<string> midi_inputs;
244         vector<string> midi_outputs;
245
246         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
247         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
248
249         update_port_combos (midi_inputs, midi_outputs);
250 }
251
252 void
253 FPGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& 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::build_mix_action_combo (Gtk::ComboBox& cb)
415 {
416         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (action_columns));
417         TreeIter rowp;
418         TreeModel::Row row;
419
420         rowp = model->append();
421         row = *(rowp);
422         row[action_columns.name] = _("Toggle Editor & Mixer Windows");
423         row[action_columns.path] = X_("Common/toggle-editor-mixer");
424
425         rowp = model->append();
426         row = *(rowp);
427         row[action_columns.name] = _("Show/Hide Editor mixer strip");
428         row[action_columns.path] = X_("Editor/show-editor-mixer");
429
430         cb.set_model (model);
431         cb.pack_start (action_columns.name);
432
433         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FPGUI::action_changed), &cb, FaderPort::Mix));
434 }
435
436 void
437 FPGUI::build_proj_action_combo (Gtk::ComboBox& cb)
438 {
439 }
440
441
442 void
443 FPGUI::build_trns_action_combo (Gtk::ComboBox& cb)
444 {
445 }
446
447 Glib::RefPtr<Gtk::ListStore>
448 FPGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
449 {
450         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
451         TreeModel::Row row;
452
453         row = *store->append ();
454         row[midi_port_columns.full_name] = string();
455         row[midi_port_columns.short_name] = _("Disconnected");
456
457         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
458                 row = *store->append ();
459                 row[midi_port_columns.full_name] = *p;
460                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
461                 if (pn.empty ()) {
462                         pn = (*p).substr ((*p).find (':') + 1);
463                 }
464                 row[midi_port_columns.short_name] = pn;
465         }
466
467         return store;
468 }
469
470 void
471 FPGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
472 {
473         if (ignore_active_change) {
474                 return;
475         }
476
477         TreeModel::iterator active = combo->get_active ();
478         string new_port = (*active)[midi_port_columns.full_name];
479
480         if (new_port.empty()) {
481                 if (for_input) {
482                         fp.input_port()->disconnect_all ();
483                 } else {
484                         fp.output_port()->disconnect_all ();
485                 }
486
487                 return;
488         }
489
490         if (for_input) {
491                 if (!fp.input_port()->connected_to (new_port)) {
492                         fp.input_port()->disconnect_all ();
493                         fp.input_port()->connect (new_port);
494                 }
495         } else {
496                 if (!fp.output_port()->connected_to (new_port)) {
497                         fp.output_port()->disconnect_all ();
498                         fp.output_port()->connect (new_port);
499                 }
500         }
501 }
502
503 void
504 FPGUI::action_changed (Gtk::ComboBox* cb, FaderPort::ButtonID id)
505 {
506         TreeModel::const_iterator row = cb->get_active ();
507         string action_path = (*row)[action_columns.path];
508
509         cerr << "Change " << id << " to " << action_path << endl;
510         
511         fp.set_action (id, action_path, true);
512 }