Fix ExportFormatSpecification copy-c'tor
[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 #include <gtkmm/stock.h>
20
21 #include "pbd/enumwriter.h"
22 #include "pbd/i18n.h"
23
24 #include "temporal/time.h"
25
26 #include "ardour/audioengine.h"
27 #include "ardour/session.h"
28 #include "ardour/transport_master.h"
29 #include "ardour/transport_master_manager.h"
30
31 #include "widgets/tooltips.h"
32
33 #include "gtkmm2ext/utils.h"
34 #include "gtkmm2ext/gui_thread.h"
35
36 #include "ardour_ui.h"
37 #include "floating_text_entry.h"
38 #include "transport_masters_dialog.h"
39
40
41 using namespace std;
42 using namespace Gtk;
43 using namespace Gtkmm2ext;
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace ArdourWidgets;
47
48 TransportMastersWidget::TransportMastersWidget ()
49         : table (4, 13)
50         , add_button (_("Add a new Transport Master"))
51 {
52         pack_start (table, PACK_EXPAND_WIDGET, 12);
53         pack_start (add_button, FALSE, FALSE);
54
55         add_button.signal_clicked ().connect (sigc::mem_fun (*this, &TransportMastersWidget::add_master));
56
57         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
58         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
59         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
60         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format/\nBPM")));
61         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
62         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Last")));
63         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
64         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
65         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
66         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
67         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Active\nCommands")));
68         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock\nSynced")));
69         col_title[12].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/\n30")));
70         col_title[13].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Remove")));
71
72         set_tooltip (col_title[12], _("<b>When enabled</b> the external timecode source is assumed to use 29.97 fps instead of 30000/1001.\n"
73                                       "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
74                                       "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
75                                       "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
76                                       "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
77                                       "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
78                              ));
79
80         set_tooltip (col_title[11], string_compose (_("<b>When enabled</b> the external timecode source is assumed to be sample-clock synced to the audio interface\n"
81                                                       "being used by %1."), PROGRAM_NAME));
82
83         table.set_spacings (6);
84
85         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
86         TransportMasterManager::instance().Added.connect (add_connection, invalidator (*this), boost::bind (&TransportMastersWidget::rebuild, this), gui_context());
87         TransportMasterManager::instance().Removed.connect (remove_connection, invalidator (*this), boost::bind (&TransportMastersWidget::rebuild, this), gui_context());
88
89         rebuild ();
90 }
91
92 TransportMastersWidget::~TransportMastersWidget ()
93 {
94         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
95                 delete *r;
96         }
97 }
98
99 void
100 TransportMastersWidget::set_transport_master (boost::shared_ptr<TransportMaster> tm)
101 {
102         _session->request_sync_source (tm);
103 }
104
105 void
106 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
107 {
108         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
109                 if ((*r)->tm == new_master) {
110                         (*r)->use_button.set_active (true);
111                         break; /* there can only be one */
112                 }
113         }
114 }
115
116 void
117 TransportMastersWidget::add_master ()
118 {
119         AddTransportMasterDialog d;
120
121         d.present ();
122         string name;
123
124         while (name.empty()) {
125
126                 int r = d.run ();
127
128                 switch (r) {
129                 case RESPONSE_ACCEPT:
130                         name = d.get_name();
131                         break;
132                 default:
133                         return;
134                 }
135         }
136
137         d.hide ();
138
139         if (TransportMasterManager::instance().add (d.get_type(), name)) {
140                 MessageDialog msg (_("New transport master not added - check error log for details"));
141                 msg.run ();
142         }
143 }
144
145 void
146 TransportMastersWidget::rebuild ()
147 {
148         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
149
150         container_clear (table);
151
152         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
153                 delete *r;
154         }
155
156         rows.clear ();
157         table.resize (masters.size()+1, 14);
158
159         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
160                 table.attach (col_title[col], col, col+1, 0, 1);
161         }
162
163         uint32_t n = 1;
164
165         Gtk::RadioButtonGroup use_button_group;
166
167         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
168
169                 Row* r = new Row (*this);
170                 rows.push_back (r);
171
172                 r->tm = *m;
173                 r->label.set_text ((*m)->name());
174                 r->type.set_text (enum_2_string  ((*m)->type()));
175
176                 r->use_button.set_group (use_button_group);
177
178                 if (TransportMasterManager::instance().current() == r->tm) {
179                         r->use_button.set_active (true);
180                 }
181
182                 int col = 0;
183
184                 r->label_box.add (r->label);
185
186                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
187                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
188                 table.attach (r->type, col, col+1, n, n+1); ++col;
189                 table.attach (r->format, col, col+1, n, n+1); ++col;
190                 table.attach (r->current, col, col+1, n, n+1); ++col;
191                 table.attach (r->last, col, col+1, n, n+1); ++col;
192                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
193                 table.attach (r->delta, col, col+1, n, n+1); ++col;
194                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
195                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
196                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
197
198                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
199
200                 if (ttm) {
201                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
202                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
203                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
204                 } else {
205                         col += 2;
206                 }
207
208                 if (r->tm->removeable()) {
209                         table.attach (r->remove_button, col, col+1, n, n+1); ++col;
210                 } else {
211                         col++;
212                 }
213
214                 table.show_all ();
215
216                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
217                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
218                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
219                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
220                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
221                 r->remove_button.signal_clicked().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::remove_clicked));
222
223                 if (ttm) {
224                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
225                 }
226
227                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
228
229                 PropertyChange all_change;
230                 all_change.add (Properties::locked);
231                 all_change.add (Properties::collect);
232                 all_change.add (Properties::connected);
233
234                 if (ttm) {
235                         all_change.add (Properties::fr2997);
236                         all_change.add (Properties::sclock_synced);
237                 }
238
239                 r->prop_change (all_change);
240         }
241 }
242
243 TransportMastersWidget::Row::Row (TransportMastersWidget& p)
244         : parent (p)
245         , request_option_menu (0)
246         , remove_button (X_("x"))
247         , name_editor (0)
248         , save_when (0)
249         , ignore_active_change (false)
250 {
251 }
252
253 bool
254 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
255 {
256         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
257                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
258                 if (!toplevel) {
259                         return false;
260                 }
261                 name_editor = new FloatingTextEntry (toplevel, tm->name());
262                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
263                 name_editor->show ();
264                 return true;
265         }
266         return false;
267 }
268
269 bool
270 TransportMastersWidget::idle_remove (TransportMastersWidget::Row* row)
271 {
272         TransportMasterManager::instance().remove (row->tm->name());
273         return false;
274 }
275
276 void
277 TransportMastersWidget::Row::remove_clicked ()
278 {
279         /* have to do this via an idle callback, because it will destroy the
280            widget from which this callback was initiated.
281         */
282         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (parent, &TransportMastersWidget::idle_remove), this));
283 }
284
285 void
286 TransportMastersWidget::Row::name_edited (string str, int ignored)
287 {
288         tm->set_name (str);
289         /* floating text entry deletes itself */
290         name_editor = 0;
291 }
292
293 void
294 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
295 {
296         if (what_changed.contains (Properties::locked)) {
297         }
298
299         if (what_changed.contains (Properties::fr2997)) {
300                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
301         }
302
303         if (what_changed.contains (Properties::sclock_synced)) {
304                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
305         }
306
307         if (what_changed.contains (Properties::collect)) {
308                 collect_button.set_active (tm->collect());
309         }
310
311         if (what_changed.contains (Properties::connected)) {
312                 populate_port_combo ();
313         }
314
315         if (what_changed.contains (Properties::name)) {
316                 label.set_text (tm->name());
317         }
318 }
319
320 void
321 TransportMastersWidget::Row::use_button_toggled ()
322 {
323         if (use_button.get_active()) {
324                 parent.set_transport_master (tm);
325         }
326 }
327
328 void
329 TransportMastersWidget::Row::fr2997_button_toggled ()
330 {
331         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
332 }
333
334 void
335 TransportMastersWidget::Row::collect_button_toggled ()
336 {
337         tm->set_collect (collect_button.get_active());
338 }
339
340 void
341 TransportMastersWidget::Row::sync_button_toggled ()
342 {
343         tm->set_sample_clock_synced (sclock_synced_button.get_active());
344 }
345
346 bool
347 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
348 {
349         if (ev->button == 1) {
350                 if (!request_option_menu) {
351                         build_request_options ();
352                 }
353                 request_option_menu->popup (1, ev->time);
354                 return true;
355         }
356         return false;
357 }
358
359 void
360 TransportMastersWidget::Row::build_request_options ()
361 {
362         using namespace Gtk::Menu_Helpers;
363
364         request_option_menu = manage (new Menu);
365
366         MenuList& items (request_option_menu->items());
367
368         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
369         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
370         i->set_active (tm->request_mask() & TR_Speed);
371         items.push_back (CheckMenuElem (_("Accept locate commands")));
372         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
373         i->set_active (tm->request_mask() & TR_Locate);
374 }
375
376 Glib::RefPtr<Gtk::ListStore>
377 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
378 {
379         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
380         TreeModel::Row row;
381
382         row = *store->append ();
383         row[port_columns.full_name] = string();
384         row[port_columns.short_name] = _("Disconnected");
385
386         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
387
388                 if (AudioEngine::instance()->port_is_mine (*p)) {
389                         continue;
390                 }
391
392                 row = *store->append ();
393                 row[port_columns.full_name] = *p;
394
395                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
396                 if (pn.empty ()) {
397                         pn = (*p).substr ((*p).find (':') + 1);
398                 }
399                 row[port_columns.short_name] = pn;
400         }
401
402         return store;
403 }
404
405 void
406 TransportMastersWidget::Row::populate_port_combo ()
407 {
408         if (!tm->port()) {
409                 port_combo.hide ();
410                 return;
411         } else {
412                 port_combo.show ();
413         }
414
415         vector<string> inputs;
416
417         if (tm->port()->type() == DataType::MIDI) {
418                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
419         } else {
420                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
421         }
422
423         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
424         bool input_found = false;
425         int n;
426
427         port_combo.set_model (input);
428
429         Gtk::TreeModel::Children children = input->children();
430         Gtk::TreeModel::Children::iterator i;
431         i = children.begin();
432         ++i; /* skip "Disconnected" */
433
434
435         for (n = 1;  i != children.end(); ++i, ++n) {
436                 string port_name = (*i)[port_columns.full_name];
437                 if (tm->port()->connected_to (port_name)) {
438                         port_combo.set_active (n);
439                         input_found = true;
440                         break;
441                 }
442         }
443
444         if (!input_found) {
445                 port_combo.set_active (0); /* disconnected */
446         }
447 }
448
449 void
450 TransportMastersWidget::Row::port_choice_changed ()
451 {
452         if (ignore_active_change) {
453                 return;
454         }
455
456         TreeModel::iterator active = port_combo.get_active ();
457         string new_port = (*active)[port_columns.full_name];
458
459         if (new_port.empty()) {
460                 tm->port()->disconnect_all ();
461                 return;
462         }
463
464         if (!tm->port()->connected_to (new_port)) {
465                 tm->port()->disconnect_all ();
466                 tm->port()->connect (new_port);
467         }
468 }
469
470 void
471 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
472 {
473         using namespace Timecode;
474
475         samplepos_t pos;
476         double speed;
477         samplepos_t most_recent;
478         samplepos_t when;
479         stringstream ss;
480         Time t;
481         Time l;
482         boost::shared_ptr<TimecodeTransportMaster> ttm;
483         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
484
485         if (s) {
486
487                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
488
489                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
490                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
491
492                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
493                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
494                                 last.set_text (Timecode::timecode_format_time (l));
495                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
496                                 char buf[8];
497                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
498                                 format.set_text (buf);
499                                 last.set_text ("");
500                         } else {
501                                 format.set_text ("");
502                                 last.set_text ("");
503                         }
504                         current.set_text (Timecode::timecode_format_time (t));
505                         delta.set_markup (tm->delta_string ());
506
507                         char gap[32];
508                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
509                         if (abs (seconds) < 1.0) {
510                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
511                         } else if (abs (seconds) < 4.0) {
512                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
513                         } else {
514                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
515                         }
516                         timestamp.set_text (gap);
517                         save_when = when;
518
519                 } else {
520
521                         if (save_when) {
522                                 char gap[32];
523
524                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
525                                 if (abs (seconds) < 1.0) {
526                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
527                                 } else if (abs (seconds) < 4.0) {
528                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
529                                 } else {
530                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
531                                 }
532                                 timestamp.set_text (gap);
533                                 save_when = when;
534                         }
535                         delta.set_text ("");
536                         current.set_text ("");
537                 }
538         }
539 }
540
541 void
542 TransportMastersWidget::update (samplepos_t /* audible */)
543 {
544         samplepos_t now = AudioEngine::instance()->sample_time ();
545
546         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
547                 (*r)->update (_session, now);
548         }
549 }
550
551 void
552 TransportMastersWidget::on_map ()
553 {
554         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
555         Gtk::VBox::on_map ();
556 }
557
558 void
559 TransportMastersWidget::on_unmap ()
560 {
561         update_connection.disconnect ();
562         Gtk::VBox::on_unmap ();
563 }
564
565 TransportMastersWindow::TransportMastersWindow ()
566         : ArdourWindow (_("Transport Masters"))
567 {
568         add (w);
569         w.show ();
570 }
571
572 void
573 TransportMastersWindow::on_realize ()
574 {
575         ArdourWindow::on_realize ();
576         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
577         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
578 }
579
580
581
582 void
583 TransportMastersWindow::set_session (ARDOUR::Session* s)
584 {
585         ArdourWindow::set_session (s);
586         w.set_session (s);
587 }
588
589 TransportMastersWidget::AddTransportMasterDialog::AddTransportMasterDialog ()
590         : ArdourDialog (_("Add Transport Master"), true, false)
591         , name_label (_("Name"))
592         , type_label (_("Type"))
593 {
594         name_hbox.set_spacing (6);
595         name_hbox.pack_start (name_label, false, false);
596         name_hbox.pack_start (name_entry, true, true);
597
598         type_hbox.set_spacing (6);
599         type_hbox.pack_start (type_label, false, false);
600         type_hbox.pack_start (type_combo, true, true);
601
602         vector<string> s;
603
604         s.push_back (X_("MTC"));
605         s.push_back (X_("LTC"));
606         s.push_back (X_("MIDI Clock"));
607
608         set_popdown_strings (type_combo, s);
609         type_combo.set_active_text (X_("LTC"));
610
611         get_vbox()->pack_start (name_hbox, false, false);
612         get_vbox()->pack_start (type_hbox, false, false);
613
614         add_button (_("Cancel"), RESPONSE_CANCEL);
615         add_button (_("Add"), RESPONSE_ACCEPT);
616
617         name_entry.show ();
618         type_combo.show ();
619         name_label.show ();
620         type_label.show ();
621         name_hbox.show ();
622         type_hbox.show ();
623
624         name_entry.signal_activate().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), Gtk::RESPONSE_ACCEPT));
625 }
626
627 string
628 TransportMastersWidget::AddTransportMasterDialog::get_name () const
629 {
630         return name_entry.get_text ();
631 }
632
633 SyncSource
634 TransportMastersWidget::AddTransportMasterDialog::get_type() const
635 {
636         string t = type_combo.get_active_text ();
637
638         if (t == X_("MTC")) {
639                 return MTC;
640         } else if (t == X_("MIDI Clock")) {
641                 return MIDIClock;
642         }
643
644         return LTC;
645 }