Fix MTC slave implicit return on transport-stop
[ardour.git] / libs / surfaces / push2 / 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/bindings.h"
29 #include "gtkmm2ext/gui_thread.h"
30 #include "gtkmm2ext/utils.h"
31
32 #include "ardour/audioengine.h"
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/parameter_descriptor.h"
35
36 #include "push2.h"
37 #include "gui.h"
38
39 #include "pbd/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 Push2::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<Push2*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 Push2::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 gui;
69         gui = 0;
70 }
71
72 void
73 Push2::build_gui ()
74 {
75         gui = new P2GUI (*this);
76 }
77
78 /*--------------------*/
79
80 P2GUI::P2GUI (Push2& p)
81         : p2 (p)
82         , table (2, 5)
83         , action_table (5, 4)
84         , ignore_active_change (false)
85         , pressure_mode_label (_("Pressure Mode"))
86 {
87         set_border_width (12);
88
89         table.set_row_spacings (4);
90         table.set_col_spacings (6);
91         table.set_border_width (12);
92         table.set_homogeneous (false);
93
94         std::string data_file_path;
95         string name = "push2-small.png";
96         Searchpath spath(ARDOUR::ardour_data_search_path());
97         spath.add_subdirectory_to_paths ("icons");
98         find_file (spath, name, data_file_path);
99         if (!data_file_path.empty()) {
100                 image.set (data_file_path);
101                 hpacker.pack_start (image, false, false);
102         }
103
104         Gtk::Label* l;
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, &P2GUI::active_port_changed), &input_combo, true));
111         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &P2GUI::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         table.attach (pressure_mode_label, 0, 1, row, row+1, AttachOptions (0), AttachOptions (0));
128         table.attach (pressure_mode_selector, 1, 2, row, row+1, AttachOptions (FILL|EXPAND), AttachOptions (0));
129         row++;
130
131         hpacker.pack_start (table, true, true);
132
133         pressure_mode_selector.set_model (build_pressure_mode_columns());
134         pressure_mode_selector.pack_start (pressure_mode_columns.name);
135         pressure_mode_selector.set_active ((int) p2.pressure_mode());
136         pressure_mode_selector.signal_changed().connect (sigc::mem_fun (*this, &P2GUI::reprogram_pressure_mode));
137
138         set_spacing (12);
139
140         pack_start (hpacker, false, false);
141
142         /* update the port connection combos */
143
144         update_port_combos ();
145
146         /* catch future changes to connection state */
147
148         ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (port_reg_connection, invalidator (*this), boost::bind (&P2GUI::connection_handler, this), gui_context());
149         p2.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&P2GUI::connection_handler, this), gui_context());
150 }
151
152 P2GUI::~P2GUI ()
153 {
154 }
155
156 void
157 P2GUI::connection_handler ()
158 {
159         /* ignore all changes to combobox active strings here, because we're
160            updating them to match a new ("external") reality - we were called
161            because port connections have changed.
162         */
163
164         PBD::Unwinder<bool> ici (ignore_active_change, true);
165
166         update_port_combos ();
167 }
168
169 void
170 P2GUI::update_port_combos ()
171 {
172         vector<string> midi_inputs;
173         vector<string> midi_outputs;
174
175         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
176         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
177
178         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
179         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
180         bool input_found = false;
181         bool output_found = false;
182         int n;
183
184         input_combo.set_model (input);
185         output_combo.set_model (output);
186
187         Gtk::TreeModel::Children children = input->children();
188         Gtk::TreeModel::Children::iterator i;
189         i = children.begin();
190         ++i; /* skip "Disconnected" */
191
192
193         for (n = 1;  i != children.end(); ++i, ++n) {
194                 string port_name = (*i)[midi_port_columns.full_name];
195                 if (p2.input_port()->connected_to (port_name)) {
196                         input_combo.set_active (n);
197                         input_found = true;
198                         break;
199                 }
200         }
201
202         if (!input_found) {
203                 input_combo.set_active (0); /* disconnected */
204         }
205
206         children = output->children();
207         i = children.begin();
208         ++i; /* skip "Disconnected" */
209
210         for (n = 1;  i != children.end(); ++i, ++n) {
211                 string port_name = (*i)[midi_port_columns.full_name];
212                 if (p2.output_port()->connected_to (port_name)) {
213                         output_combo.set_active (n);
214                         output_found = true;
215                         break;
216                 }
217         }
218
219         if (!output_found) {
220                 output_combo.set_active (0); /* disconnected */
221         }
222 }
223
224 Glib::RefPtr<Gtk::ListStore>
225 P2GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
226 {
227         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
228         TreeModel::Row row;
229
230         row = *store->append ();
231         row[midi_port_columns.full_name] = string();
232         row[midi_port_columns.short_name] = _("Disconnected");
233
234         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
235                 row = *store->append ();
236                 row[midi_port_columns.full_name] = *p;
237                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
238                 if (pn.empty ()) {
239                         pn = (*p).substr ((*p).find (':') + 1);
240                 }
241                 row[midi_port_columns.short_name] = pn;
242         }
243
244         return store;
245 }
246
247 void
248 P2GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
249 {
250         if (ignore_active_change) {
251                 return;
252         }
253
254         TreeModel::iterator active = combo->get_active ();
255         string new_port = (*active)[midi_port_columns.full_name];
256
257         if (new_port.empty()) {
258                 if (for_input) {
259                         p2.input_port()->disconnect_all ();
260                 } else {
261                         p2.output_port()->disconnect_all ();
262                 }
263
264                 return;
265         }
266
267         if (for_input) {
268                 if (!p2.input_port()->connected_to (new_port)) {
269                         p2.input_port()->disconnect_all ();
270                         p2.input_port()->connect (new_port);
271                 }
272         } else {
273                 if (!p2.output_port()->connected_to (new_port)) {
274                         p2.output_port()->disconnect_all ();
275                         p2.output_port()->connect (new_port);
276                 }
277         }
278 }
279
280 Glib::RefPtr<Gtk::ListStore>
281 P2GUI::build_pressure_mode_columns ()
282 {
283         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (pressure_mode_columns);
284         TreeModel::Row row;
285
286         row = *store->append();
287         row[pressure_mode_columns.name] = _("AfterTouch (Channel Pressure)");
288         row[pressure_mode_columns.mode] = Push2::AfterTouch;
289
290         row = *store->append();
291         row[pressure_mode_columns.name] = _("Polyphonic Pressure (Note Pressure)");
292         row[pressure_mode_columns.mode] = Push2::PolyPressure;
293
294         return store;
295 }
296
297 void
298 P2GUI::reprogram_pressure_mode ()
299 {
300         Gtk::TreeModel::iterator iter = pressure_mode_selector.get_active();
301         Push2::PressureMode pm;
302
303         if (iter) {
304                 Gtk::TreeModel::Row row = *iter;
305                 if (row) {
306                         pm = row[pressure_mode_columns.mode];
307                 } else {
308                         pm = Push2::AfterTouch;
309                 }
310         } else {
311                 pm = Push2::AfterTouch;
312         }
313
314         cerr << "Reprogram pm to " << pm << endl;
315         p2.set_pressure_mode (pm);
316 }