Remove ambiguity of Gtk and Windows API
[ardour.git] / gtk2_ardour / transport_masters_dialog.cc
1 /*
2     Copyright (C) 2018 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 "pbd/enumwriter.h"
21 #include "pbd/i18n.h"
22
23 #include "temporal/time.h"
24
25 #include "ardour/audioengine.h"
26 #include "ardour/session.h"
27 #include "ardour/transport_master.h"
28 #include "ardour/transport_master_manager.h"
29
30 #include "widgets/tooltips.h"
31
32 #include "gtkmm2ext/utils.h"
33 #include "gtkmm2ext/gui_thread.h"
34
35 #include "ardour_ui.h"
36 #include "transport_masters_dialog.h"
37
38 using namespace std;
39 using namespace Gtk;
40 using namespace Gtkmm2ext;
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace ArdourWidgets;
44
45 TransportMastersWidget::TransportMastersWidget ()
46         : table (4, 9)
47 {
48         pack_start (table, PACK_EXPAND_WIDGET, 12);
49
50         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
51         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
52         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
53         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format")));
54         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
55         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
56         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
57         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
58         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
59         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
60         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Accept")));
61         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock Synced")));
62         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/30")));
63
64         set_tooltip (col_title[11], _("<b>When enabled</b> the external timecode source is assumed to use 29.97 fps instead of 30000/1001.\n"
65                                      "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
66                                      "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
67                                      "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
68                                      "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
69                                      "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
70                              ));
71
72
73         table.set_spacings (6);
74
75         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
76
77         rebuild ();
78 }
79
80 TransportMastersWidget::~TransportMastersWidget ()
81 {
82         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
83                 delete *r;
84         }
85 }
86
87 void
88 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
89 {
90         cerr << "master changed to " << new_master << endl;
91 }
92
93 void
94 TransportMastersWidget::rebuild ()
95 {
96         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
97
98         container_clear (table);
99
100         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
101                 delete *r;
102         }
103
104         rows.clear ();
105         table.resize (masters.size()+1, 12);
106
107         table.attach (col_title[0], 0, 1, 0, 1);
108         table.attach (col_title[1], 1, 2, 0, 1);
109         table.attach (col_title[2], 2, 3, 0, 1);
110         table.attach (col_title[3], 3, 4, 0, 1);
111         table.attach (col_title[4], 4, 5, 0, 1);
112         table.attach (col_title[5], 5, 6, 0, 1);
113         table.attach (col_title[6], 6, 7, 0, 1);
114         table.attach (col_title[7], 7, 8, 0, 1);
115         table.attach (col_title[8], 8, 9, 0, 1);
116         table.attach (col_title[9], 9, 10, 0, 1);
117         table.attach (col_title[10], 10, 11, 0, 1);
118         table.attach (col_title[11], 11, 12, 0, 1);
119
120         uint32_t n = 1;
121
122         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
123
124                 Row* r = new Row;
125                 rows.push_back (r);
126
127                 r->tm = *m;
128                 r->label.set_text ((*m)->name());
129                 r->type.set_text (enum_2_string  ((*m)->type()));
130
131                 r->use_button.set_group (use_button_group);
132
133                 if (TransportMasterManager::instance().current() == r->tm) {
134                         r->use_button.set_active (true);
135                 }
136
137                 table.attach (r->type, 0, 1, n, n+1);
138                 table.attach (r->label, 1, 2, n, n+1);
139                 table.attach (r->format, 2, 3, n, n+1);
140                 table.attach (r->current, 3, 4, n, n+1);
141                 table.attach (r->timestamp, 4, 5, n, n+1);
142                 table.attach (r->delta, 5, 6, n, n+1);
143                 table.attach (r->collect_button, 6, 7, n, n+1);
144                 table.attach (r->use_button, 7, 8, n, n+1);
145                 table.attach (r->port_combo, 8, 9, n, n+1);
146                 table.attach (r->request_options, 9, 10, n, n+1);
147
148                 if (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm)) {
149                         table.attach (r->sclock_synced_button, 10, 11, n, n+1);
150                         r->sclock_synced_button.set_active (r->tm->sample_clock_synced());
151                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
152                         table.attach (r->fps_299730_button, 11, 12, n, n+1);
153                 }
154
155                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
156                 ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (*r, invalidator (*this), boost::bind (&TransportMastersWidget::Row::connection_handler, r), gui_context());
157
158                 r->collect_button.set_active (r->tm->collect());
159
160                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
161                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
162                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
163
164         }
165 }
166
167 TransportMastersWidget::Row::Row ()
168         : request_option_menu (0)
169         , ignore_active_change (false)
170 {
171 }
172
173 void
174 TransportMastersWidget::Row::use_button_toggled ()
175 {
176         if (use_button.get_active()) {
177                 Config->set_sync_source (tm->type());
178         }
179 }
180
181 void
182 TransportMastersWidget::Row::collect_button_toggled ()
183 {
184         tm->set_collect (collect_button.get_active());
185 }
186
187 void
188 TransportMastersWidget::Row::sync_button_toggled ()
189 {
190         tm->set_sample_clock_synced (sclock_synced_button.get_active());
191 }
192
193 bool
194 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
195 {
196         if (ev->button == 1) {
197                 if (!request_option_menu) {
198                         build_request_options ();
199                 }
200                 request_option_menu->popup (1, ev->time);
201                 return true;
202         }
203         return false;
204 }
205
206 void
207 TransportMastersWidget::Row::build_request_options ()
208 {
209         using namespace Gtk::Menu_Helpers;
210
211         request_option_menu = manage (new Menu);
212
213         MenuList& items (request_option_menu->items());
214
215         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
216         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
217         i->set_active (tm->request_mask() & TR_Speed);
218         items.push_back (CheckMenuElem (_("Accept locate commands")));
219         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
220         i->set_active (tm->request_mask() & TR_Locate);
221 }
222
223 void
224 TransportMastersWidget::Row::connection_handler ()
225 {
226 }
227
228 Glib::RefPtr<Gtk::ListStore>
229 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
230 {
231         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
232         TreeModel::Row row;
233
234         row = *store->append ();
235         row[port_columns.full_name] = string();
236         row[port_columns.short_name] = _("Disconnected");
237
238         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
239
240                 if (AudioEngine::instance()->port_is_mine (*p)) {
241                         continue;
242                 }
243
244                 row = *store->append ();
245                 row[port_columns.full_name] = *p;
246
247                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
248                 if (pn.empty ()) {
249                         pn = (*p).substr ((*p).find (':') + 1);
250                 }
251                 row[port_columns.short_name] = pn;
252         }
253
254         return store;
255 }
256
257 void
258 TransportMastersWidget::Row::populate_port_combo ()
259 {
260         if (!tm->port()) {
261                 port_combo.hide ();
262                 return;
263         } else {
264                 port_combo.show ();
265         }
266
267         vector<string> inputs;
268
269         if (tm->port()->type() == DataType::MIDI) {
270                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
271         } else {
272                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
273         }
274
275         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
276         bool input_found = false;
277         int n;
278
279         port_combo.set_model (input);
280
281         Gtk::TreeModel::Children children = input->children();
282         Gtk::TreeModel::Children::iterator i;
283         i = children.begin();
284         ++i; /* skip "Disconnected" */
285
286
287         for (n = 1;  i != children.end(); ++i, ++n) {
288                 string port_name = (*i)[port_columns.full_name];
289                 if (tm->port()->connected_to (port_name)) {
290                         port_combo.set_active (n);
291                         input_found = true;
292                         break;
293                 }
294         }
295
296         if (!input_found) {
297                 port_combo.set_active (0); /* disconnected */
298         }
299 }
300
301 void
302 TransportMastersWidget::Row::port_choice_changed ()
303 {
304         if (ignore_active_change) {
305                 return;
306         }
307
308         TreeModel::iterator active = port_combo.get_active ();
309         string new_port = (*active)[port_columns.full_name];
310
311         if (new_port.empty()) {
312                 tm->port()->disconnect_all ();
313                 return;
314         }
315
316         if (!tm->port()->connected_to (new_port)) {
317                 tm->port()->disconnect_all ();
318                 tm->port()->connect (new_port);
319         }
320 }
321
322 void
323 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
324 {
325         using namespace Timecode;
326
327         samplepos_t pos;
328         double speed;
329         stringstream ss;
330         Time t;
331         boost::shared_ptr<TimecodeTransportMaster> ttm;
332         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
333
334         if (s) {
335
336                 if (tm->speed_and_position (speed, pos, now)) {
337
338                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
339
340                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
341                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
342                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
343                                 char buf[8];
344                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
345                                 format.set_text (buf);
346                         } else {
347                                 format.set_text ("");
348                         }
349                         current.set_text (Timecode::timecode_format_time (t));
350                         timestamp.set_markup (tm->position_string());
351                         delta.set_markup (tm->delta_string ());
352
353                 }
354         }
355
356         populate_port_combo ();
357 }
358
359 void
360 TransportMastersWidget::update (samplepos_t audible)
361 {
362         samplepos_t now = AudioEngine::instance()->sample_time ();
363
364         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
365                 (*r)->update (_session, now);
366         }
367 }
368
369 void
370 TransportMastersWidget::on_map ()
371 {
372         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
373         Gtk::VBox::on_map ();
374 }
375
376 void
377 TransportMastersWidget::on_unmap ()
378 {
379         update_connection.disconnect ();
380         Gtk::VBox::on_unmap ();
381 }
382
383 TransportMastersDialog::TransportMastersDialog ()
384         : ArdourDialog (_("Transport Masters"))
385 {
386         get_vbox()->pack_start (w);
387         w.show ();
388 }
389
390 void
391 TransportMastersDialog::set_session (ARDOUR::Session* s)
392 {
393         ArdourDialog::set_session (s);
394         w.set_session (s);
395 }