add name editing for 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
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 "floating_text_entry.h"
37 #include "transport_masters_dialog.h"
38
39
40 using namespace std;
41 using namespace Gtk;
42 using namespace Gtkmm2ext;
43 using namespace ARDOUR;
44 using namespace PBD;
45 using namespace ArdourWidgets;
46
47 TransportMastersWidget::TransportMastersWidget ()
48         : table (4, 13)
49 {
50         pack_start (table, PACK_EXPAND_WIDGET, 12);
51
52         col_title[0].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Use")));
53         col_title[1].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Name")));
54         col_title[2].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Type")));
55         col_title[3].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Format/\nBPM")));
56         col_title[4].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Current")));
57         col_title[5].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Last")));
58         col_title[6].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Timestamp")));
59         col_title[7].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Delta")));
60         col_title[8].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Collect")));
61         col_title[9].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Data Source")));
62         col_title[10].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Active\nCommands")));
63         col_title[11].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Clock\nSynced")));
64         col_title[12].set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("29.97/30")));
65
66         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"
67                                       "SMPTE 12M-1999 specifies 29.97df as 30000/1001. The spec further mentions that "
68                                       "drop-sample timecode has an accumulated error of -86ms over a 24-hour period.\n"
69                                       "Drop-sample timecode would compensate exactly for a NTSC color frame rate of 30 * 0.9990 (ie 29.970000). "
70                                       "That is not the actual rate. However, some vendors use that rate - despite it being against the specs - "
71                                       "because the variant of using exactly 29.97 fps has zero timecode drift.\n"
72                              ));
73
74         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"
75                                                       "being used by %1."), PROGRAM_NAME));
76
77         table.set_spacings (6);
78
79         TransportMasterManager::instance().CurrentChanged.connect (current_connection, invalidator (*this), boost::bind (&TransportMastersWidget::current_changed, this, _1, _2), gui_context());
80
81         rebuild ();
82 }
83
84 TransportMastersWidget::~TransportMastersWidget ()
85 {
86         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
87                 delete *r;
88         }
89 }
90
91 void
92 TransportMastersWidget::current_changed (boost::shared_ptr<TransportMaster> old_master, boost::shared_ptr<TransportMaster> new_master)
93 {
94         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
95                 if ((*r)->tm == new_master) {
96                         (*r)->use_button.set_active (true);
97                         break; /* there can only be one */
98                 }
99         }
100 }
101
102 void
103 TransportMastersWidget::rebuild ()
104 {
105         TransportMasterManager::TransportMasters const & masters (TransportMasterManager::instance().transport_masters());
106
107         container_clear (table);
108
109         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
110                 delete *r;
111         }
112
113         rows.clear ();
114         table.resize (masters.size()+1, 13);
115
116         for (size_t col = 0; col < sizeof (col_title) / sizeof (col_title[0]); ++col) {
117                 table.attach (col_title[col], col, col+1, 0, 1);
118         }
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                 int col = 0;
138
139                 r->label_box.add (r->label);
140
141                 table.attach (r->use_button, col, col+1, n, n+1); ++col;
142                 table.attach (r->label_box, col, col+1, n, n+1); ++col;
143                 table.attach (r->type, col, col+1, n, n+1); ++col;
144                 table.attach (r->format, col, col+1, n, n+1); ++col;
145                 table.attach (r->current, col, col+1, n, n+1); ++col;
146                 table.attach (r->last, col, col+1, n, n+1); ++col;
147                 table.attach (r->timestamp, col, col+1, n, n+1); ++col;
148                 table.attach (r->delta, col, col+1, n, n+1); ++col;
149                 table.attach (r->collect_button, col, col+1, n, n+1); ++col;
150                 table.attach (r->port_combo, col, col+1, n, n+1); ++col;
151                 table.attach (r->request_options, col, col+1, n, n+1); ++col;
152
153                 boost::shared_ptr<TimecodeTransportMaster> ttm (boost::dynamic_pointer_cast<TimecodeTransportMaster> (r->tm));
154
155                 if (ttm) {
156                         table.attach (r->sclock_synced_button, col, col+1, n, n+1); ++col;
157                         table.attach (r->fr2997_button, col, col+1, n, n+1); ++col;
158                         r->fr2997_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::fr2997_button_toggled));
159                 }
160
161                 r->label_box.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
162                 r->label_box.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::name_press));
163                 r->port_combo.signal_changed().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::port_choice_changed));
164                 r->use_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::use_button_toggled));
165                 r->collect_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::collect_button_toggled));
166                 r->request_options.signal_button_press_event().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::request_option_press), false);
167
168                 if (ttm) {
169                         r->sclock_synced_button.signal_toggled().connect (sigc::mem_fun (*r, &TransportMastersWidget::Row::sync_button_toggled));
170                 }
171
172                 r->tm->PropertyChanged.connect (r->property_change_connection, invalidator (*this), boost::bind (&TransportMastersWidget::Row::prop_change, r, _1), gui_context());
173
174                 PropertyChange all_change;
175                 all_change.add (Properties::locked);
176                 all_change.add (Properties::collect);
177                 all_change.add (Properties::connected);
178
179                 if (ttm) {
180                         all_change.add (Properties::fr2997);
181                         all_change.add (Properties::sclock_synced);
182                 }
183
184                 r->prop_change (all_change);
185         }
186 }
187
188 TransportMastersWidget::Row::Row ()
189         : request_option_menu (0)
190         , name_editor (0)
191         , ignore_active_change (false)
192 {
193 }
194
195 bool
196 TransportMastersWidget::Row::name_press (GdkEventButton* ev)
197 {
198         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
199                 Gtk::Window* toplevel = dynamic_cast<Gtk::Window*> (label.get_toplevel());
200                 if (!toplevel) {
201                         return false;
202                 }
203                 name_editor = new FloatingTextEntry (toplevel, tm->name());
204                 name_editor->use_text.connect (sigc::mem_fun (*this, &TransportMastersWidget::Row::name_edited));
205                 name_editor->show ();
206                 return true;
207         }
208         return false;
209 }
210
211 void
212 TransportMastersWidget::Row::name_edited (string str, int ignored)
213 {
214         tm->set_name (str);
215         /* floating text entry deletes itself */
216         name_editor = 0;
217 }
218
219 void
220 TransportMastersWidget::Row::prop_change (PropertyChange what_changed)
221 {
222         if (what_changed.contains (Properties::locked)) {
223         }
224
225         if (what_changed.contains (Properties::fr2997)) {
226                 fr2997_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->fr2997());
227         }
228
229         if (what_changed.contains (Properties::sclock_synced)) {
230                 sclock_synced_button.set_active (boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm)->sample_clock_synced());
231         }
232
233         if (what_changed.contains (Properties::collect)) {
234                 collect_button.set_active (tm->collect());
235         }
236
237         if (what_changed.contains (Properties::connected)) {
238                 populate_port_combo ();
239         }
240
241         if (what_changed.contains (Properties::name)) {
242                 label.set_text (tm->name());
243         }
244 }
245
246 void
247 TransportMastersWidget::Row::use_button_toggled ()
248 {
249         if (use_button.get_active()) {
250                 Config->set_sync_source (tm->type());
251         }
252 }
253
254 void
255 TransportMastersWidget::Row::fr2997_button_toggled ()
256 {
257         boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)->set_fr2997 (fr2997_button.get_active());
258 }
259
260 void
261 TransportMastersWidget::Row::collect_button_toggled ()
262 {
263         tm->set_collect (collect_button.get_active());
264 }
265
266 void
267 TransportMastersWidget::Row::sync_button_toggled ()
268 {
269         tm->set_sample_clock_synced (sclock_synced_button.get_active());
270 }
271
272 bool
273 TransportMastersWidget::Row::request_option_press (GdkEventButton* ev)
274 {
275         if (ev->button == 1) {
276                 if (!request_option_menu) {
277                         build_request_options ();
278                 }
279                 request_option_menu->popup (1, ev->time);
280                 return true;
281         }
282         return false;
283 }
284
285 void
286 TransportMastersWidget::Row::build_request_options ()
287 {
288         using namespace Gtk::Menu_Helpers;
289
290         request_option_menu = manage (new Menu);
291
292         MenuList& items (request_option_menu->items());
293
294         items.push_back (CheckMenuElem (_("Accept speed-changing commands (start/stop)")));
295         Gtk::CheckMenuItem* i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
296         i->set_active (tm->request_mask() & TR_Speed);
297         items.push_back (CheckMenuElem (_("Accept locate commands")));
298         i = dynamic_cast<Gtk::CheckMenuItem *> (&items.back ());
299         i->set_active (tm->request_mask() & TR_Locate);
300 }
301
302 Glib::RefPtr<Gtk::ListStore>
303 TransportMastersWidget::Row::build_port_list (vector<string> const & ports)
304 {
305         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (port_columns);
306         TreeModel::Row row;
307
308         row = *store->append ();
309         row[port_columns.full_name] = string();
310         row[port_columns.short_name] = _("Disconnected");
311
312         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
313
314                 if (AudioEngine::instance()->port_is_mine (*p)) {
315                         continue;
316                 }
317
318                 row = *store->append ();
319                 row[port_columns.full_name] = *p;
320
321                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
322                 if (pn.empty ()) {
323                         pn = (*p).substr ((*p).find (':') + 1);
324                 }
325                 row[port_columns.short_name] = pn;
326         }
327
328         return store;
329 }
330
331 void
332 TransportMastersWidget::Row::populate_port_combo ()
333 {
334         if (!tm->port()) {
335                 port_combo.hide ();
336                 return;
337         } else {
338                 port_combo.show ();
339         }
340
341         vector<string> inputs;
342
343         if (tm->port()->type() == DataType::MIDI) {
344                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
345         } else {
346                 ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::AUDIO, ARDOUR::PortFlags (ARDOUR::IsOutput), inputs);
347         }
348
349         Glib::RefPtr<Gtk::ListStore> input = build_port_list (inputs);
350         bool input_found = false;
351         int n;
352
353         port_combo.set_model (input);
354
355         Gtk::TreeModel::Children children = input->children();
356         Gtk::TreeModel::Children::iterator i;
357         i = children.begin();
358         ++i; /* skip "Disconnected" */
359
360
361         for (n = 1;  i != children.end(); ++i, ++n) {
362                 string port_name = (*i)[port_columns.full_name];
363                 if (tm->port()->connected_to (port_name)) {
364                         port_combo.set_active (n);
365                         input_found = true;
366                         break;
367                 }
368         }
369
370         if (!input_found) {
371                 port_combo.set_active (0); /* disconnected */
372         }
373 }
374
375 void
376 TransportMastersWidget::Row::port_choice_changed ()
377 {
378         if (ignore_active_change) {
379                 return;
380         }
381
382         TreeModel::iterator active = port_combo.get_active ();
383         string new_port = (*active)[port_columns.full_name];
384
385         if (new_port.empty()) {
386                 tm->port()->disconnect_all ();
387                 return;
388         }
389
390         if (!tm->port()->connected_to (new_port)) {
391                 tm->port()->disconnect_all ();
392                 tm->port()->connect (new_port);
393         }
394 }
395
396 void
397 TransportMastersWidget::Row::update (Session* s, samplepos_t now)
398 {
399         using namespace Timecode;
400
401         samplepos_t pos;
402         double speed;
403         samplepos_t last;
404         samplepos_t when;
405         stringstream ss;
406         Time t;
407         boost::shared_ptr<TimecodeTransportMaster> ttm;
408         boost::shared_ptr<MIDIClock_TransportMaster> mtm;
409
410         if (s) {
411
412                 if (tm->speed_and_position (speed, pos, last, when, now)) {
413
414                         sample_to_timecode (pos, t, false, false, 25, false, AudioEngine::instance()->sample_rate(), 100, false, 0);
415
416                         if ((ttm = boost::dynamic_pointer_cast<TimecodeTransportMaster> (tm))) {
417                                 format.set_text (timecode_format_name (ttm->apparent_timecode_format()));
418                         } else if ((mtm = boost::dynamic_pointer_cast<MIDIClock_TransportMaster> (tm))) {
419                                 char buf[8];
420                                 snprintf (buf, sizeof (buf), "%.1f", mtm->bpm());
421                                 format.set_text (buf);
422                         } else {
423                                 format.set_text ("");
424                         }
425                         current.set_text (Timecode::timecode_format_time (t));
426                         timestamp.set_markup (tm->position_string());
427                         delta.set_markup (tm->delta_string ());
428
429                 }
430         }
431 }
432
433 void
434 TransportMastersWidget::update (samplepos_t audible)
435 {
436         samplepos_t now = AudioEngine::instance()->sample_time ();
437
438         for (vector<Row*>::iterator r = rows.begin(); r != rows.end(); ++r) {
439                 (*r)->update (_session, now);
440         }
441 }
442
443 void
444 TransportMastersWidget::on_map ()
445 {
446         update_connection = ARDOUR_UI::Clock.connect (sigc::mem_fun (*this, &TransportMastersWidget::update));
447         Gtk::VBox::on_map ();
448 }
449
450 void
451 TransportMastersWidget::on_unmap ()
452 {
453         update_connection.disconnect ();
454         Gtk::VBox::on_unmap ();
455 }
456
457 TransportMastersWindow::TransportMastersWindow ()
458         : ArdourWindow (_("Transport Masters"))
459 {
460         add (w);
461         w.show ();
462 }
463
464 void
465 TransportMastersWindow::on_realize ()
466 {
467         ArdourWindow::on_realize ();
468         /* (try to) ensure that resizing is possible and the window can be moved (and closed) */
469         get_window()->set_decorations (Gdk::DECOR_BORDER | Gdk::DECOR_RESIZEH | Gdk::DECOR_TITLE | Gdk::DECOR_MENU);
470 }
471
472
473
474 void
475 TransportMastersWindow::set_session (ARDOUR::Session* s)
476 {
477         ArdourWindow::set_session (s);
478         w.set_session (s);
479 }