add dialog for adding new transport masters
[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         int r = d.run ();
123
124         switch (r) {
125         case RESPONSE_ACCEPT:
126                 break;
127         default:
128                 return;
129         }
130
131         TransportMasterManager::instance().add (d.get_type(), d.get_name());
132 }
133
134 void
135 TransportMastersWidget::rebuild ()
136 {
137         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
138
139         container_clear (table);
140
141         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
142                 delete *r;
143         }
144
145         rows.clear ();
146         table.resize (masters.size()+1, 14);
147
148         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
149                 table.attach (col_title[col], col, col+1, 0, 1);
150         }
151
152         uint32_t n = 1;
153
154         Gtk::RadioButtonGroup use_button_group;
155
156         for (TransportMasterManager::TransportMasters::const_iterator m = masters.begin(); m != masters.end(); ++m, ++n) {
157
158                 Row* r = new Row (*this);
159                 rows.push_back (r);
160
161                 r->tm = *m;
162                 r->label.set_text ((*m)->name());
163                 r->type.set_text (enum_2_string  ((*m)->type()));
164
165                 r->use_button.set_group (use_button_group);
166
167                 if (TransportMasterManager::instance().current() == r->tm) {
168                         r->use_button.set_active (true);
169                 }
170
171                 int col = 0;
172
173                 r->label_box.add (r->label);
174
175                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
176                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
177                 table.attach (r->type, col, col+1, n, n+1); ++col;
178                 table.attach (r->format, col, col+1, n, n+1); ++col;
179                 table.attach (r->current, col, col+1, n, n+1); ++col;
180                 table.attach (r->last, col, col+1, n, n+1); ++col;
181                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
182                 table.attach (r->delta, col, col+1, n, n+1); ++col;
183                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
184                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
185                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
186
187                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
188
189                 if (ttm) {
190                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
191                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
192                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
193                 } else {
194                         col += 2;
195                 }
196
197                 if (r->tm->removeable()) {
198                         table.attach (r->remove_button, col, col+1, n, n+1); ++col;
199                 } else {
200                         col++;
201                 }
202
203                 table.show_all ();
204
205                 // r->label_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
206                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
207                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
208                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
209                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
210                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
211                 r->remove_button.signal_clicked().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::remove_clicked));
212
213                 if (ttm) {
214                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
215                 }
216
217                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
218
219                 PropertyChange all_change;
220                 all_change.add (Properties::locked);
221                 all_change.add (Properties::collect);
222                 all_change.add (Properties::connected);
223
224                 if (ttm) {
225                         all_change.add (Properties::fr2997);
226                         all_change.add (Properties::sclock_synced);
227                 }
228
229                 r->prop_change (all_change);
230         }
231 }
232
233 TransportMastersWidget::Row::Row (TransportMastersWidget& p)
234         : parent (p)
235         , request_option_menu (0)
236         , remove_button (X_("x"))
237         , name_editor (0)
238         , save_when (0)
239         , ignore_active_change (false)
240 {
241 }
242
243 bool
244 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
245 {
246         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
247                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
248                 if (!toplevel) {
249                         return false;
250                 }
251                 name_editor = new FloatingTextEntry (toplevel, tm->name());
252                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
253                 name_editor->show ();
254                 return true;
255         }
256         return false;
257 }
258
259 void
260 TransportMastersWidget::Row::remove_clicked ()
261 {
262         TransportMasterManager::instance().remove (tm->name());
263 }
264
265 void
266 TransportMastersWidget::Row::name_edited (string str, int ignored)
267 {
268         tm->set_name (str);
269         /* floating text entry deletes itself */
270         name_editor = 0;
271 }
272
273 void
274 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
275 {
276         if (what_changed.contains (Properties::locked)) {
277         }
278
279         if (what_changed.contains (Properties::fr2997)) {
280                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
281         }
282
283         if (what_changed.contains (Properties::sclock_synced)) {
284                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
285         }
286
287         if (what_changed.contains (Properties::collect)) {
288                 collect_button.set_active (tm->collect());
289         }
290
291         if (what_changed.contains (Properties::connected)) {
292                 populate_port_combo ();
293         }
294
295         if (what_changed.contains (Properties::name)) {
296                 label.set_text (tm->name());
297         }
298 }
299
300 void
301 TransportMastersWidget::Row::use_button_toggled ()
302 {
303         if (use_button.get_active()) {
304                 parent.set_transport_master (tm);
305         }
306 }
307
308 void
309 TransportMastersWidget::Row::fr2997_button_toggled ()
310 {
311         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
312 }
313
314 void
315 TransportMastersWidget::Row::collect_button_toggled ()
316 {
317         tm->set_collect (collect_button.get_active());
318 }
319
320 void
321 TransportMastersWidget::Row::sync_button_toggled ()
322 {
323         tm->set_sample_clock_synced (sclock_synced_button.get_active());
324 }
325
326 bool
327 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
328 {
329         if (ev->button == 1) {
330                 if (!request_option_menu) {
331                         build_request_options ();
332                 }
333                 request_option_menu->popup (1, ev->time);
334                 return true;
335         }
336         return false;
337 }
338
339 void
340 TransportMastersWidget::Row::build_request_options ()
341 {
342         using namespace Gtk::Menu_Helpers;
343
344         request_option_menu = manage (new Menu);
345
346         MenuList& items (request_option_menu->items());
347
348         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
349         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
350         i->set_active (tm->request_mask() & TR_Speed);
351         items.push_back (CheckMenuElem (_("Accept locate commands")));
352         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
353         i->set_active (tm->request_mask() & TR_Locate);
354 }
355
356 Glib::RefPtr<Gtk::ListStore>
357 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
358 {
359         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
360         TreeModel::Row row;
361
362         row = *store->append ();
363         row[port_columns.full_name] = string();
364         row[port_columns.short_name] = _("Disconnected");
365
366         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
367
368                 if (AudioEngine::instance()->port_is_mine (*p)) {
369                         continue;
370                 }
371
372                 row = *store->append ();
373                 row[port_columns.full_name] = *p;
374
375                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
376                 if (pn.empty ()) {
377                         pn = (*p).substr ((*p).find (':') + 1);
378                 }
379                 row[port_columns.short_name] = pn;
380         }
381
382         return store;
383 }
384
385 void
386 TransportMastersWidget::Row::populate_port_combo ()
387 {
388         if (!tm->port()) {
389                 port_combo.hide ();
390                 return;
391         } else {
392                 port_combo.show ();
393         }
394
395         vector<string> inputs;
396
397         if (tm->port()->type() == DataType::MIDI) {
398                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
399         } else {
400                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
401         }
402
403         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
404         bool input_found = false;
405         int n;
406
407         port_combo.set_model (input);
408
409         Gtk::TreeModel::Children children = input->children();
410         Gtk::TreeModel::Children::iterator i;
411         i = children.begin();
412         ++i; /* skip "Disconnected" */
413
414
415         for (n = 1;  i != children.end(); ++i, ++n) {
416                 string port_name = (*i)[port_columns.full_name];
417                 if (tm->port()->connected_to (port_name)) {
418                         port_combo.set_active (n);
419                         input_found = true;
420                         break;
421                 }
422         }
423
424         if (!input_found) {
425                 port_combo.set_active (0); /* disconnected */
426         }
427 }
428
429 void
430 TransportMastersWidget::Row::port_choice_changed ()
431 {
432         if (ignore_active_change) {
433                 return;
434         }
435
436         TreeModel::iterator active = port_combo.get_active ();
437         string new_port = (*active)[port_columns.full_name];
438
439         if (new_port.empty()) {
440                 tm->port()->disconnect_all ();
441                 return;
442         }
443
444         if (!tm->port()->connected_to (new_port)) {
445                 tm->port()->disconnect_all ();
446                 tm->port()->connect (new_port);
447         }
448 }
449
450 void
451 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
452 {
453         using namespace Timecode;
454
455         samplepos_t pos;
456         double speed;
457         samplepos_t most_recent;
458         samplepos_t when;
459         stringstream ss;
460         Time t;
461         Time l;
462         boost::shared_ptr<TimecodeTransportMaster> ttm;
463         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
464
465         if (s) {
466
467                 if (tm->speed_and_position (speed, pos, most_recent, when, now)) {
468
469                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
470                         sample_to_timecode (most_recent, l, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
471
472                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
473                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
474                                 last.set_text (Timecode::timecode_format_time (l));
475                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
476                                 char buf[8];
477                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
478                                 format.set_text (buf);
479                                 last.set_text ("");
480                         } else {
481                                 format.set_text ("");
482                                 last.set_text ("");
483                         }
484                         current.set_text (Timecode::timecode_format_time (t));
485                         delta.set_markup (tm->delta_string ());
486
487                         char gap[32];
488                         const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
489                         if (abs (seconds) < 1.0) {
490                                 snprintf (gap, sizeof (gap), "%.3fs", seconds);
491                         } else if (abs (seconds) < 4.0) {
492                                 snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
493                         } else {
494                                 snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
495                         }
496                         timestamp.set_text (gap);
497                         save_when = when;
498
499                 } else {
500
501                         if (save_when) {
502                                 char gap[32];
503
504                                 const float seconds = (when - now) / (float) AudioEngine::instance()->sample_rate();
505                                 if (abs (seconds) < 1.0) {
506                                         snprintf (gap, sizeof (gap), "%.3fs", seconds);
507                                 } else if (abs (seconds) < 4.0) {
508                                         snprintf (gap, sizeof (gap), "%ds", (int) floor (seconds));
509                                 } else {
510                                         snprintf (gap, sizeof (gap), "%s", _(">4s ago"));
511                                 }
512                                 timestamp.set_text (gap);
513                                 save_when = when;
514                         }
515                         delta.set_text ("");
516                         current.set_text ("");
517                 }
518         }
519 }
520
521 void
522         TransportMastersWidget::update (samplepos_t /* audible */)
523 {
524         samplepos_t now = AudioEngine::instance()->sample_time ();
525
526         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
527                 (*r)->update (_session, now);
528         }
529 }
530
531 void
532 TransportMastersWidget::on_map ()
533 {
534         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
535         Gtk::VBox::on_map ();
536 }
537
538 void
539 TransportMastersWidget::on_unmap ()
540 {
541         update_connection.disconnect ();
542         Gtk::VBox::on_unmap ();
543 }
544
545 TransportMastersWindow::TransportMastersWindow ()
546         : ArdourWindow (_("Transport Masters"))
547 {
548         add (w);
549         w.show ();
550 }
551
552 void
553 TransportMastersWindow::on_realize ()
554 {
555         ArdourWindow::on_realize ();
556         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
557         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
558 }
559
560
561
562 void
563 TransportMastersWindow::set_session (ARDOUR::Session* s)
564 {
565         ArdourWindow::set_session (s);
566         w.set_session (s);
567 }
568
569 TransportMastersWidget::AddTransportMasterDialog::AddTransportMasterDialog ()
570         : ArdourDialog (_("Add Transport Master"), true, false)
571         , name_label (_("Name"))
572         , type_label (_("Type"))
573 {
574         name_hbox.set_spacing (6);
575         name_hbox.pack_start (name_label, false, false);
576         name_hbox.pack_start (name_entry, true, true);
577
578         type_hbox.set_spacing (6);
579         type_hbox.pack_start (type_label, false, false);
580         type_hbox.pack_start (type_combo, true, true);
581
582         vector<string> s;
583
584         s.push_back (X_("MTC"));
585         s.push_back (X_("LTC"));
586         s.push_back (X_("MIDI Clock"));
587
588         set_popdown_strings (type_combo, s);
589         type_combo.set_active_text (X_("LTC"));
590
591         get_vbox()->pack_start (name_hbox, false, false);
592         get_vbox()->pack_start (type_hbox, false, false);
593
594         add_button (_("Cancel"), RESPONSE_CANCEL);
595         add_button (_("Add"), RESPONSE_ACCEPT);
596
597         name_entry.show ();
598         type_combo.show ();
599         name_label.show ();
600         type_label.show ();
601         name_hbox.show ();
602         type_hbox.show ();
603 }
604
605 string
606 TransportMastersWidget::AddTransportMasterDialog::get_name () const
607 {
608         return name_entry.get_text ();
609 }
610
611 SyncSource
612 TransportMastersWidget::AddTransportMasterDialog::get_type() const
613 {
614         string t = type_combo.get_active_text ();
615
616         if (t == X_("MTC")) {
617                 return MTC;
618         } else if (t == X_("MIDI Clock")) {
619                 return MIDIClock;
620         }
621
622         return LTC;
623 }