bc9783cf860e633f43225bf56c3a5b2781b6c8a7
[ardour.git] / gtk2_ardour / audio_clock.cc
1 /*
2     Copyright (C) 1999 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 <cstdio> // for sprintf
21 #include <cmath>
22
23 #include "pbd/convert.h"
24 #include "pbd/enumwriter.h"
25
26 #include <gtkmm/style.h>
27
28 #include "gtkmm2ext/cairocell.h"
29 #include <gtkmm2ext/utils.h>
30
31 #include "ardour/ardour.h"
32 #include "ardour/session.h"
33 #include "ardour/tempo.h"
34 #include "ardour/profile.h"
35 #include <sigc++/bind.h>
36
37 #include "ardour_ui.h"
38 #include "audio_clock.h"
39 #include "utils.h"
40 #include "keyboard.h"
41 #include "gui_thread.h"
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace std;
48
49 using Gtkmm2ext::Keyboard;
50
51 using PBD::atoi;
52 using PBD::atof;
53
54 sigc::signal<void> AudioClock::ModeChanged;
55 vector<AudioClock*> AudioClock::clocks;
56
57 uint32_t AudioClock::field_length[] = {
58         2, /* Timecode_Hours */
59         2, /* Timecode_Minutes */
60         2, /* Timecode_Seconds */
61         2, /* Timecode_Frames */
62         2, /* MS_Hours */
63         2, /* MS_Minutes */
64         2, /* MS_Seconds */
65         3, /* MS_Milliseconds */
66         4, /* Bars */
67         2, /* Beats */
68         4, /* Ticks */
69         10, /* AudioFrames */
70 };
71
72 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
73                         bool allow_edit, bool follows_playhead, bool duration, bool with_info)
74         : _name (clock_name)
75         , is_transient (transient)
76         , is_duration (duration)
77         , editable (allow_edit)
78         , _follows_playhead (follows_playhead)
79         , supplemental_left (0)
80         , supplemental_right (0)
81         , last_when(0)
82         , _canonical_time_is_displayed (true)
83         , _canonical_time (0)
84 {
85         last_when = 0;
86         
87         last_hrs = 9999;
88         last_mins = 9999;
89         last_secs = 9999;
90         last_frames = 99999;
91
92         ms_last_hrs = 9999;
93         ms_last_mins = 9999;
94         ms_last_secs = 9999;
95         ms_last_millisecs = 99999;
96
97         last_negative = false;
98         
99         last_pdelta = 0;
100         last_sdelta = 0;
101         key_entry_state = 0;
102         ops_menu = 0;
103         dragging = false;
104         bbt_reference_time = -1;
105         editing_field = (Field) 0;
106
107         /* basic per-mode editable text "arrays" */
108
109         display = new CairoEditableText ();
110
111         _fixed_cells[Colon1] = new CairoCharCell (Colon1, ':');
112         _fixed_cells[Colon2] = new CairoCharCell (Colon2, ':');
113         _fixed_cells[Colon3] = new CairoCharCell (Colon3, ':');
114         _fixed_cells[Bar1] = new CairoCharCell (Bar1, '|');
115         _fixed_cells[Bar2] = new CairoCharCell (Bar2, '|');
116         
117         // add an extra character for the negative sign
118         _text_cells[Timecode_Hours] = new CairoTextCell (Timecode_Hours, field_length[Timecode_Hours] + 1); 
119         _text_cells[Timecode_Minutes] = new CairoTextCell (Timecode_Minutes, field_length[Timecode_Minutes]);
120         _text_cells[Timecode_Seconds] = new CairoTextCell (Timecode_Seconds, field_length[Timecode_Seconds]);
121         _text_cells[Timecode_Frames] = new CairoTextCell (Timecode_Frames, field_length[Timecode_Frames]);
122
123         /* Minutes/Seconds */
124         
125         _text_cells[MS_Hours] = new CairoTextCell (MS_Hours, field_length[MS_Hours]);
126         _text_cells[MS_Minutes] = new CairoTextCell (MS_Minutes, field_length[MS_Minutes]);
127         _text_cells[MS_Seconds] = new CairoTextCell (MS_Seconds, field_length[MS_Seconds]);
128         _text_cells[MS_Milliseconds] = new CairoTextCell (MS_Milliseconds, field_length[MS_Milliseconds]);
129
130         /* Beats/Bars/Ticks */
131         
132         _text_cells[Bars] = new CairoTextCell (Bars, field_length[Bars]);
133         _text_cells[Beats] = new CairoTextCell (Beats, field_length[Beats]);
134         _text_cells[Ticks] = new CairoTextCell (Ticks, field_length[Ticks]);
135
136         /* Audio Frames */
137         
138         _text_cells[AudioFrames] = new CairoTextCell (AudioFrames, field_length[AudioFrames]);
139
140         set_homogeneous (false);
141
142         if (with_info) {
143
144                 supplemental_left = new CairoEditableText ();
145                 supplemental_right = new CairoEditableText ();
146
147                 /* field lengths of these cells will be set dynamically by ::set_mode()
148                  */
149
150                 _text_cells[LowerLeft1] = new CairoTextCell (LowerLeft1, 0);
151                 _text_cells[LowerLeft2] = new CairoTextCell (LowerLeft2, 0);
152                 _text_cells[LowerRight1] = new CairoTextCell (LowerRight1, 0);
153                 _text_cells[LowerRight2] = new CairoTextCell (LowerRight2, 0);
154                 
155                 bottom.set_spacing (1);
156                 bottom.set_homogeneous (false);
157                 bottom.pack_start (*supplemental_left, true, true);
158                 bottom.pack_start (*supplemental_right, true, true);
159
160                 top.pack_start (*display, true, true);
161                 
162                 set_spacing (1);
163                 
164                 pack_start (top, true, true);
165                 pack_start (bottom, true, true);
166         } else {
167                 pack_start (*display, true, true);
168         }
169
170         show_all ();
171
172         set_widget_name (widget_name);
173
174         _mode = BBT; /* lie to force mode switch */
175         set_mode (Timecode);
176         set (last_when, true);
177
178         if (!is_transient) {
179                 clocks.push_back (this);
180         }
181 }
182         
183 AudioClock::~AudioClock ()
184 {
185         /* these are not manage()'d, so that we can add/remove
186            them from containers as necessary.
187         */
188         delete display;
189         delete supplemental_left;
190         delete supplemental_right;
191         /* XXX need to delete all cells too */
192 }
193
194 void
195 AudioClock::set_widget_name (const string& name)
196 {
197         Widget::set_name (name);
198
199         set_theme ();
200 }
201
202 void
203 AudioClock::set_theme ()
204 {
205         Glib::RefPtr<Gtk::Style> style = get_style ();
206         double r, g, b, a;
207
208         if (!style) {
209                 return;
210         }
211
212         Pango::FontDescription font; 
213
214         if (!is_realized()) {
215                 font = get_font_for_style (get_name());
216         } else {
217                 font = style->get_font();
218         }
219
220         display->set_font (font);
221
222         /* propagate font style,but smaller, into supplemental text */
223         if (supplemental_left) {
224                 boost::shared_ptr<CairoFontDescription> smaller_font (new CairoFontDescription (*display->font().get()));
225                 smaller_font->set_size (12);
226                 supplemental_right->set_font (smaller_font);
227                 supplemental_left->set_font (smaller_font);
228         }
229
230         Gdk::Color bg = style->get_base (Gtk::STATE_NORMAL);
231         Gdk::Color fg = style->get_text (Gtk::STATE_NORMAL);
232         Gdk::Color eg = style->get_text (Gtk::STATE_ACTIVE);
233
234         r = bg.get_red_p ();
235         g = bg.get_green_p ();
236         b = bg.get_blue_p ();
237         a = 1.0;
238
239         display->set_bg (r, g, b, a);
240
241         if (supplemental_right) {
242                 supplemental_right->set_bg (r,g,b,a);
243                 supplemental_left->set_bg (r,g,b,a);
244         }
245
246         r = fg.get_red_p ();
247         g = fg.get_green_p ();
248         b = fg.get_blue_p ();
249         a = 1.0;
250
251         display->set_colors (r, g, b, a);
252
253         if (supplemental_right) {
254                 supplemental_right->set_colors (r,g,b,a);
255                 supplemental_left->set_colors (r,g,b,a);
256         }
257
258         r = eg.get_red_p ();
259         g = eg.get_green_p ();
260         b = eg.get_blue_p ();
261         a = 1.0;
262
263         display->set_edit_colors (r, g, b, a);
264
265         if (supplemental_right) {
266                 supplemental_right->set_edit_colors (r,g,b,a);
267                 supplemental_left->set_edit_colors (r,g,b,a);
268         }
269
270         queue_draw ();
271 }
272
273 void
274 AudioClock::focus ()
275 {
276 }
277
278 void
279 AudioClock::end_edit ()
280 {
281         display->stop_editing ();
282         editing_field = (Field) 0;
283         key_entry_state = 0;
284
285         /* move focus back to the default widget in the top level window */
286
287         Keyboard::magic_widget_drop_focus ();
288
289         Widget* top = get_toplevel();
290
291         if (top->is_toplevel ()) {
292                 Window* win = dynamic_cast<Window*> (top);
293                 win->grab_focus ();
294         }
295 }
296
297 void
298 AudioClock::on_realize ()
299 {
300         VBox::on_realize ();
301
302         /* styles are not available until the widgets are bound to a window */
303         
304         set_theme ();
305 }
306
307 void
308 AudioClock::set (framepos_t when, bool force, framecnt_t offset, char which)
309 {
310         if ((!force && !is_visible()) || _session == 0) {
311                 return;
312         }
313
314         bool const pdelta = Config->get_primary_clock_delta_edit_cursor ();
315         bool const sdelta = Config->get_secondary_clock_delta_edit_cursor ();
316
317         if (offset && which == 'p' && pdelta) {
318                 when = (when > offset) ? when - offset : offset - when;
319         } else if (offset && which == 's' && sdelta) {
320                 when = (when > offset) ? when - offset : offset - when;
321         }
322
323         if (when == last_when && !force) {
324                 return;
325         }
326
327         if (which == 'p' && pdelta && !last_pdelta) {
328                 set_widget_name("TransportClockDisplayDelta");
329                 last_pdelta = true;
330         } else if (which == 'p' && !pdelta && last_pdelta) {
331                 set_widget_name("TransportClockDisplay");
332                 last_pdelta = false;
333         } else if (which == 's' && sdelta && !last_sdelta) {
334                 set_widget_name("SecondaryClockDisplayDelta");
335                 last_sdelta = true;
336         } else if (which == 's' && !sdelta && last_sdelta) {
337                 set_widget_name("SecondaryClockDisplay");
338                 last_sdelta = false;
339         }
340
341         switch (_mode) {
342         case Timecode:
343                 set_timecode (when, force);
344                 break;
345
346         case BBT:
347                 set_bbt (when, force);
348                 break;
349
350         case MinSec:
351                 set_minsec (when, force);
352                 break;
353
354         case Frames:
355                 set_frames (when, force);
356                 break;
357
358         case Off:
359                 break;
360         }
361
362         last_when = when;
363
364         /* we're setting the time from a frames value, so keep it as the canonical value */
365         _canonical_time = when;
366         _canonical_time_is_displayed = false;
367 }
368
369 void
370 AudioClock::session_configuration_changed (std::string p)
371 {
372         if (p != "timecode-offset" && p != "timecode-offset-negative") {
373                 return;
374         }
375
376         framecnt_t current;
377
378         switch (_mode) {
379         case Timecode:
380                 if (is_duration) {
381                         current = current_duration ();
382                 } else {
383                         current = current_time ();
384                 }
385                 set (current, true);
386                 break;
387         default:
388                 break;
389         }
390 }
391
392 void
393 AudioClock::set_frames (framepos_t when, bool /*force*/)
394 {
395         char buf[32];
396         snprintf (buf, sizeof (buf), "%" PRId64, when);
397         
398         display->set_text (_text_cells[AudioFrames], buf);
399
400         if (supplemental_left) {
401                 framecnt_t rate = _session->frame_rate();
402
403                 if (fmod (rate, 1000.0) == 0.000) {
404                         sprintf (buf, "%" PRId64 "K", rate/1000);
405                 } else {
406                         sprintf (buf, "%" PRId64, rate);
407                 }
408
409                 supplemental_left->set_text (_text_cells[LowerLeft2], buf);
410
411                 float vid_pullup = _session->config.get_video_pullup();
412
413                 if (vid_pullup == 0.0) {
414                         supplemental_right->set_text (_text_cells[LowerRight2], _("none"));
415                 } else {
416                         sprintf (buf, "%-6.4f", vid_pullup);
417                         supplemental_right->set_text (_text_cells[LowerRight2], buf);
418                 }
419         }
420 }
421
422 void
423 AudioClock::set_minsec (framepos_t when, bool force)
424 {
425         char buf[32];
426         framecnt_t left;
427         int hrs;
428         int mins;
429         int secs;
430         int millisecs;
431
432         left = when;
433         hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
434         left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
435         mins = (int) floor (left / (_session->frame_rate() * 60.0f));
436         left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
437         secs = (int) floor (left / (float) _session->frame_rate());
438         left -= (framecnt_t) floor (secs * _session->frame_rate());
439         millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
440
441         if (force || hrs != ms_last_hrs) {
442                 sprintf (buf, "%02d", hrs);
443                 display->set_text (_text_cells[MS_Hours], buf);
444                 ms_last_hrs = hrs;
445         }
446
447         if (force || mins != ms_last_mins) {
448                 sprintf (buf, "%02d", mins);
449                 display->set_text (_text_cells[MS_Minutes], buf);
450                 ms_last_mins = mins;
451         }
452
453         if (force || secs != ms_last_secs) {
454                 sprintf (buf, "%02d", secs);
455                 display->set_text (_text_cells[MS_Seconds], buf);
456                 ms_last_secs = secs;
457         }
458
459         if (force || millisecs != ms_last_millisecs) {
460                 sprintf (buf, "%03d", millisecs);
461                 display->set_text (_text_cells[MS_Milliseconds], buf);
462                 ms_last_millisecs = millisecs;
463         }
464 }
465
466 void
467 AudioClock::set_timecode (framepos_t when, bool force)
468 {
469         char buf[32];
470         Timecode::Time TC;
471
472         if (is_duration) {
473                 _session->timecode_duration (when, TC);
474         } else {
475                 _session->timecode_time (when, TC);
476         }
477
478         if (force || TC.hours != last_hrs || TC.negative != last_negative) {
479                 if (TC.negative) {
480                         sprintf (buf, "-%0*" PRIu32, field_length[Timecode_Hours], TC.hours);
481                 } else {
482                         sprintf (buf, " %0*" PRIu32, field_length[Timecode_Hours], TC.hours);
483                 }
484                 display->set_text (_text_cells[Timecode_Hours], buf);
485                 last_hrs = TC.hours;
486                 last_negative = TC.negative;
487         }
488
489         if (force || TC.minutes != last_mins) {
490                 sprintf (buf, "%0*" PRIu32, field_length[Timecode_Minutes], TC.minutes);
491                 display->set_text (_text_cells[Timecode_Minutes], buf);
492                 last_mins = TC.minutes;
493         }
494
495         if (force || TC.seconds != last_secs) {
496                 sprintf (buf, "%0*" PRIu32, field_length[Timecode_Seconds], TC.seconds);
497                 display->set_text (_text_cells[Timecode_Seconds], buf);
498                 last_secs = TC.seconds;
499         }
500
501         if (force || TC.frames != last_frames) {
502                 sprintf (buf, "%0*" PRIu32, field_length[Timecode_Frames], TC.frames);
503                 display->set_text (_text_cells[Timecode_Frames], buf);
504                 last_frames = TC.frames;
505         }
506
507         if (supplemental_right) {
508                 double timecode_frames = _session->timecode_frames_per_second();
509                 bool drop;
510
511                 if ((fabs(timecode_frames - 29.97) < 0.0001) || timecode_frames == 30) {
512                         if (_session->timecode_drop_frames()) {
513                                 drop = true;
514                         } else {
515                                 drop = false;
516                         }
517                 } 
518         
519                 if (fmod(timecode_frames, 1.0) == 0.0) {
520                         sprintf (buf, "%u %s", int (timecode_frames), (drop ? "D" : ""));
521                 } else {
522                         sprintf (buf, "%.2f %s", timecode_frames, (drop ? "D" : ""));
523                 }
524
525                 supplemental_right->set_text (_text_cells[LowerRight2], buf);
526         }
527 }
528
529 void
530 AudioClock::set_bbt (framepos_t when, bool force)
531 {
532         char buf[16];
533         Timecode::BBT_Time BBT;
534
535         /* handle a common case */
536         if (is_duration) {
537                 if (when == 0) {
538                         BBT.bars = 0;
539                         BBT.beats = 0;
540                         BBT.ticks = 0;
541                 } else {
542                         _session->tempo_map().bbt_time (when, BBT);
543                         BBT.bars--;
544                         BBT.beats--;
545                 }
546         } else {
547                 _session->tempo_map().bbt_time (when, BBT);
548         }
549
550         sprintf (buf, "%0*" PRIu32, field_length[Bars], BBT.bars);
551         if (force || _text_cells[Bars]->get_text () != buf) {
552                 display->set_text (_text_cells[Bars], buf);
553         }
554         sprintf (buf, "%0*" PRIu32, field_length[Beats], BBT.beats);
555         if (force || _text_cells[Beats]->get_text () != buf) {
556                 display->set_text (_text_cells[Beats], buf);
557         }
558         sprintf (buf, "%0*" PRIu32, field_length[Ticks], BBT.ticks);
559         if (force || _text_cells[Ticks]->get_text () != buf) {
560                 display->set_text (_text_cells[Ticks], buf);
561         }
562
563         if (supplemental_right) {
564                 framepos_t pos;
565
566                 if (bbt_reference_time < 0) {
567                         pos = when;
568                 } else {
569                         pos = bbt_reference_time;
570                 }
571
572                 TempoMetric m (_session->tempo_map().metric_at (pos));
573
574                 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
575                 supplemental_left->set_text (_text_cells[LowerLeft2], buf);
576
577                 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
578                 supplemental_right->set_text (_text_cells[LowerRight2], buf);
579         }
580 }
581
582 void
583 AudioClock::set_session (Session *s)
584 {
585         SessionHandlePtr::set_session (s);
586
587         if (_session) {
588
589                 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
590
591                 XMLProperty* prop;
592                 XMLNode* node = _session->extra_xml (X_("ClockModes"));
593                 AudioClock::Mode amode;
594
595                 if (node) {
596                         if ((prop = node->property (_name)) != 0) {
597                                 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
598                                 set_mode (amode);
599                         }
600                 }
601
602                 set (last_when, true);
603         }
604 }
605
606 void
607 AudioClock::edit_next_field ()
608 {
609         /* move on to the next field.
610          */
611         
612         switch (editing_field) {
613                 
614                 /* Timecode */
615                 
616         case Timecode_Hours:
617                 editing_field = Timecode_Minutes;
618                 display->start_editing (_text_cells[Timecode_Minutes]);
619                 break;
620         case Timecode_Minutes:
621                 editing_field = Timecode_Seconds;
622                 display->start_editing (_text_cells[Timecode_Seconds]);
623                 break;
624         case Timecode_Seconds:
625                 editing_field = Timecode_Frames;
626                 display->start_editing (_text_cells[Timecode_Frames]);
627                 break;
628         case Timecode_Frames:
629                 end_edit ();
630                 break;
631                 
632                 /* Min:Sec */
633                 
634         case MS_Hours:
635                 editing_field = MS_Minutes;
636                 display->start_editing (_text_cells[MS_Minutes]);
637                 break;
638         case MS_Minutes:
639                 editing_field = MS_Seconds;
640                 display->start_editing (_text_cells[MS_Seconds]);
641                 break;
642         case MS_Seconds:
643                 editing_field = MS_Milliseconds;
644                 display->start_editing (_text_cells[MS_Milliseconds]);
645                 break;
646         case MS_Milliseconds:
647                 end_edit ();
648                 break;
649                 
650                 /* BBT */
651                 
652         case Bars:
653                 editing_field = Beats;
654                 display->start_editing (_text_cells[Beats]);
655                 break;
656         case Beats:
657                 editing_field = Ticks;
658                 display->start_editing (_text_cells[Ticks]);
659                 break;
660         case Ticks:
661                 end_edit ();
662                 break;
663                 
664                 /* audio frames */
665         case AudioFrames:
666                 end_edit ();
667                 break;
668                 
669         default:
670                 break;
671         }
672
673         key_entry_state = 0;
674 }
675
676 bool
677 AudioClock::on_key_press_event (GdkEventKey* ev)
678 {
679         /* return true for keys that we MIGHT use 
680            at release
681         */
682         switch (ev->keyval) {
683         case GDK_0:
684         case GDK_KP_0:
685         case GDK_1:
686         case GDK_KP_1:
687         case GDK_2:
688         case GDK_KP_2:
689         case GDK_3:
690         case GDK_KP_3:
691         case GDK_4:
692         case GDK_KP_4:
693         case GDK_5:
694         case GDK_KP_5:
695         case GDK_6:
696         case GDK_KP_6:
697         case GDK_7:
698         case GDK_KP_7:
699         case GDK_8:
700         case GDK_KP_8:
701         case GDK_9:
702         case GDK_KP_9:
703         case GDK_period:
704         case GDK_comma:
705         case GDK_KP_Decimal:
706         case GDK_Tab:
707         case GDK_Return:
708         case GDK_KP_Enter:
709         case GDK_Escape:
710                 return true;
711         default:
712                 return false;
713         }
714 }
715
716 bool
717 AudioClock::on_key_release_event (GdkEventKey *ev)
718 {
719         if (editing_field == 0) {
720                 return false;
721         }
722
723         CairoTextCell *cell = _text_cells[editing_field];
724
725         if (!cell) {
726                 return false;
727         }
728
729         string new_text;
730         char new_char = 0;
731         bool move_on = false;
732
733         switch (ev->keyval) {
734         case GDK_0:
735         case GDK_KP_0:
736                 new_char = '0';
737                 break;
738         case GDK_1:
739         case GDK_KP_1:
740                 new_char = '1';
741                 break;
742         case GDK_2:
743         case GDK_KP_2:
744                 new_char = '2';
745                 break;
746         case GDK_3:
747         case GDK_KP_3:
748                 new_char = '3';
749                 break;
750         case GDK_4:
751         case GDK_KP_4:
752                 new_char = '4';
753                 break;
754         case GDK_5:
755         case GDK_KP_5:
756                 new_char = '5';
757                 break;
758         case GDK_6:
759         case GDK_KP_6:
760                 new_char = '6';
761                 break;
762         case GDK_7:
763         case GDK_KP_7:
764                 new_char = '7';
765                 break;
766         case GDK_8:
767         case GDK_KP_8:
768                 new_char = '8';
769                 break;
770         case GDK_9:
771         case GDK_KP_9:
772                 new_char = '9';
773                 break;
774
775         case GDK_period:
776         case GDK_comma:
777         case GDK_KP_Decimal:
778                 if (_mode == MinSec && editing_field == MS_Seconds) {
779                         new_char = '.'; // XXX i18n
780                 } else {
781                         return false;
782                 }
783                 break;
784
785         case GDK_Tab:
786         case GDK_Return:
787         case GDK_KP_Enter:
788                 move_on = true;
789                 break;
790
791         case GDK_Escape:
792                 end_edit ();
793                 ChangeAborted();  /*  EMIT SIGNAL  */
794                 return true;
795
796         default:
797                 return false;
798         }
799
800         if (!move_on) {
801
802                 if (key_entry_state == 0) {
803
804                         /* initialize with a fresh new string */
805
806                         if (editing_field != AudioFrames) {
807                                 for (uint32_t xn = 0; xn < field_length[editing_field] - 1; ++xn) {
808                                         new_text += '0';
809                                 }
810                         } else {
811                                 new_text = "";
812                         }
813
814                 } else {
815
816                         string existing = cell->get_text();
817                         if (existing.length() >= field_length[editing_field]) {
818                                 new_text = existing.substr (1, field_length[editing_field] - 1);
819                         } else {
820                                 new_text = existing.substr (0, field_length[editing_field] - 1);
821                         }
822                 }
823
824                 new_text += new_char;
825                 display->set_text (cell, new_text);
826                 _canonical_time_is_displayed = true;
827                 key_entry_state++;
828         }
829
830         if (key_entry_state == field_length[editing_field]) {
831                 move_on = true;
832         }
833
834         if (move_on) {
835
836                 if (key_entry_state) {
837
838                         /* if key_entry_state != then we edited the text
839                          */
840
841                         char buf[16];
842
843                         switch (editing_field) {
844                         case Timecode_Hours:
845                         case Timecode_Minutes:
846                         case Timecode_Seconds:
847                         case Timecode_Frames:
848                                 // Check Timecode fields for sanity (may also adjust fields)
849                                 timecode_sanitize_display();
850                                 break;
851                         case Bars:
852                         case Beats:
853                         case Ticks:
854                                 // Bars should never be zero, unless this clock is for a duration
855                                 if (atoi (_text_cells[Bars]->get_text()) == 0 && !is_duration) {
856                                         snprintf (buf, sizeof (buf), "%0*" PRIu32, field_length[Bars], 1);
857                                         display->set_text (_text_cells[Bars], buf);
858                                         _canonical_time_is_displayed = true;
859                                 }
860                                 //  beats should never be zero, unless this clock is for a duration
861                                 if (atoi (_text_cells[Beats]->get_text()) == 0 && !is_duration) {
862                                         snprintf (buf, sizeof (buf), "%0*" PRIu32, field_length[Beats], 1);
863                                         display->set_text (_text_cells[Beats], buf);
864                                         _canonical_time_is_displayed = true;
865                                 }
866                                 break;
867                         default:
868                                 break;
869                         }
870
871                         ValueChanged(); /* EMIT_SIGNAL */
872                 }
873                 
874                 edit_next_field ();
875         }
876
877         //if user hit Enter, lose focus
878         switch (ev->keyval) {
879         case GDK_Return:
880         case GDK_KP_Enter:
881                 end_edit ();
882         }
883
884         return true;
885 }
886
887 bool
888 AudioClock::button_press (GdkEventButton *ev, CairoCell* cell)
889 {
890         switch (ev->button) {
891         case 1:
892                 if (editable) {
893                         if (cell) {
894                                 editing_field = (Field) cell->id ();
895                                 display->start_editing (cell);
896                         }
897                         
898                         Keyboard::magic_widget_grab_focus ();
899                         
900                         /* make absolutely sure that the pointer is grabbed */
901                         gdk_pointer_grab(ev->window,false ,
902                                          GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
903                                          NULL,NULL,ev->time);
904                         dragging = true;
905                         drag_accum = 0;
906                         drag_start_y = ev->y;
907                         drag_y = ev->y;
908                 }
909                 break;
910                 
911         default:
912                 return false;
913                 break;
914         }
915
916         return true;
917 }
918
919 bool
920 AudioClock::button_release (GdkEventButton *ev, CairoCell* cell)
921 {
922         if (editable) {
923                 if (dragging) {
924                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
925                         dragging = false;
926                         if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
927                                 // we actually dragged so return without setting editing focus, or we shift clicked
928                                 return true;
929                         }
930                 }
931         }
932
933         if (Keyboard::is_context_menu_event (ev)) {
934                 if (ops_menu == 0) {
935                         build_ops_menu ();
936                 }
937                 ops_menu->popup (1, ev->time);
938                 return true;
939         }
940
941         return false;
942 }
943
944 bool
945 AudioClock::scroll (GdkEventScroll *ev, CairoCell* cell)
946 {
947         if (_session == 0 || !editable) {
948                 return false;
949         }
950
951         framepos_t frames = 0;
952
953         switch (ev->direction) {
954
955         case GDK_SCROLL_UP:
956                 frames = get_frames ((Field) cell->id());
957                 if (frames != 0) {
958                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
959                                 frames *= 10;
960                         }
961                         set (current_time() + frames, true);
962                         ValueChanged (); /* EMIT_SIGNAL */
963                 }
964                 break;
965                 
966         case GDK_SCROLL_DOWN:
967                 frames = get_frames ((Field) cell->id());
968                 if (frames != 0) {
969                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
970                                 frames *= 10;
971                         }
972                         
973                         if ((double)current_time() - (double)frames < 0.0) {
974                                 set (0, true);
975                         } else {
976                                 set (current_time() - frames, true);
977                         }
978                         
979                         ValueChanged (); /* EMIT_SIGNAL */
980                 }
981                 break;
982                 
983         default:
984                 return false;
985                 break;
986         }
987         
988         return true;
989 }
990
991 bool
992 AudioClock::field_motion_notify_event (GdkEventMotion *ev, Field field)
993 {
994         if (_session == 0 || !dragging) {
995                 return false;
996         }
997
998         float pixel_frame_scale_factor = 0.2f;
999
1000 /*
1001         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier))  {
1002                 pixel_frame_scale_factor = 0.1f;
1003         }
1004
1005
1006         if (Keyboard::modifier_state_contains (ev->state,
1007                                                Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1008
1009                 pixel_frame_scale_factor = 0.025f;
1010         }
1011 */
1012         double y_delta = ev->y - drag_y;
1013
1014         drag_accum +=  y_delta*pixel_frame_scale_factor;
1015
1016         drag_y = ev->y;
1017
1018         if (trunc(drag_accum) != 0) {
1019
1020                 framepos_t frames;
1021                 framepos_t pos;
1022                 int dir;
1023                 dir = (drag_accum < 0 ? 1:-1);
1024                 pos = current_time();
1025                 frames = get_frames (field,pos,dir);
1026
1027                 if (frames  != 0 &&  frames * drag_accum < current_time()) {
1028
1029                         set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in computer-land
1030
1031                 } else {
1032                         set (0 , false);
1033
1034                 }
1035
1036                 drag_accum= 0;
1037                 ValueChanged();  /* EMIT_SIGNAL */
1038
1039
1040         }
1041
1042         return true;
1043 }
1044
1045 framepos_t
1046 AudioClock::get_frames (Field field, framepos_t pos, int dir)
1047 {
1048         framecnt_t f = 0;
1049         Timecode::BBT_Time BBT;
1050         switch (field) {
1051         case Timecode_Hours:
1052                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1053                 break;
1054         case Timecode_Minutes:
1055                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1056                 break;
1057         case Timecode_Seconds:
1058                 f = _session->frame_rate();
1059                 break;
1060         case Timecode_Frames:
1061                 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1062                 break;
1063
1064         case AudioFrames:
1065                 f = 1;
1066                 break;
1067
1068         case MS_Hours:
1069                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1070                 break;
1071         case MS_Minutes:
1072                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1073                 break;
1074         case MS_Seconds:
1075                 f = (framecnt_t) _session->frame_rate();
1076                 break;
1077         case MS_Milliseconds:
1078                 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1079                 break;
1080
1081         case Bars:
1082                 BBT.bars = 1;
1083                 BBT.beats = 0;
1084                 BBT.ticks = 0;
1085                 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1086                 break;
1087         case Beats:
1088                 BBT.bars = 0;
1089                 BBT.beats = 1;
1090                 BBT.ticks = 0;
1091                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1092                 break;
1093         case Ticks:
1094                 BBT.bars = 0;
1095                 BBT.beats = 0;
1096                 BBT.ticks = 1;
1097                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1098                 break;
1099         default:
1100                 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1101                 f = 0;
1102                 break;
1103         }
1104
1105         return f;
1106 }
1107
1108 framepos_t
1109 AudioClock::current_time (framepos_t pos) const
1110 {
1111         if (!_canonical_time_is_displayed) {
1112                 return _canonical_time;
1113         }
1114
1115         framepos_t ret = 0;
1116
1117         switch (_mode) {
1118         case Timecode:
1119                 ret = timecode_frame_from_display ();
1120                 break;
1121         case BBT:
1122                 ret = bbt_frame_from_display (pos);
1123                 break;
1124
1125         case MinSec:
1126                 ret = minsec_frame_from_display ();
1127                 break;
1128
1129         case Frames:
1130                 ret = audio_frame_from_display ();
1131                 break;
1132
1133         case Off:
1134                 break;
1135         }
1136
1137         return ret;
1138 }
1139
1140 framepos_t
1141 AudioClock::current_duration (framepos_t pos) const
1142 {
1143         framepos_t ret = 0;
1144
1145         switch (_mode) {
1146         case Timecode:
1147                 ret = timecode_frame_from_display ();
1148                 break;
1149         case BBT:
1150                 ret = bbt_frame_duration_from_display (pos);
1151                 break;
1152
1153         case MinSec:
1154                 ret = minsec_frame_from_display ();
1155                 break;
1156
1157         case Frames:
1158                 ret = audio_frame_from_display ();
1159                 break;
1160
1161         case Off:
1162                 break;
1163         }
1164
1165         return ret;
1166 }
1167
1168 void
1169 AudioClock::timecode_sanitize_display()
1170 {
1171         // Check Timecode fields for sanity, possibly adjusting values
1172         if (atoi (_text_cells[Timecode_Minutes]->get_text()) > 59) {
1173                 display->set_text (_text_cells[Timecode_Minutes], "59");
1174                 _canonical_time_is_displayed = true;
1175         }
1176
1177         if (atoi (_text_cells[Timecode_Seconds]->get_text()) > 59) {
1178                 display->set_text (_text_cells[Timecode_Seconds], "59");
1179                 _canonical_time_is_displayed = true;
1180         }
1181
1182         switch ((long)rint(_session->timecode_frames_per_second())) {
1183         case 24:
1184                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 23) {
1185                         display->set_text (_text_cells[Timecode_Frames], "23");
1186                         _canonical_time_is_displayed = true;
1187                 }
1188                 break;
1189         case 25:
1190                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 24) {
1191                         display->set_text (_text_cells[Timecode_Frames], "24");
1192                         _canonical_time_is_displayed = true;
1193                 }
1194                 break;
1195         case 30:
1196                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 29) {
1197                         display->set_text (_text_cells[Timecode_Frames], "29");
1198                         _canonical_time_is_displayed = true;
1199                 }
1200                 break;
1201         default:
1202                 break;
1203         }
1204
1205         if (_session->timecode_drop_frames()) {
1206                 if ((atoi (_text_cells[Timecode_Minutes]->get_text()) % 10) && (atoi (_text_cells[Timecode_Seconds]->get_text()) == 0) && (atoi (_text_cells[Timecode_Frames]->get_text()) < 2)) {
1207                         display->set_text (_text_cells[Timecode_Frames], "02");
1208                         _canonical_time_is_displayed = true;
1209                 }
1210         }
1211 }
1212
1213 /** This is necessary because operator[] isn't const with std::map.
1214  *  @param f Field.
1215  *  @return Label widget.
1216  */
1217 CairoTextCell*
1218 AudioClock::label (Field f) const
1219 {
1220         std::map<Field,CairoTextCell*>::const_iterator i = _text_cells.find (f);
1221         assert (i != _text_cells.end ());
1222
1223         return i->second;
1224 }
1225
1226 framepos_t
1227 AudioClock::timecode_frame_from_display () const
1228 {
1229         if (_session == 0) {
1230                 return 0;
1231         }
1232
1233         Timecode::Time TC;
1234         framepos_t sample;
1235
1236         TC.hours = atoi (label (Timecode_Hours)->get_text());
1237         TC.minutes = atoi (label (Timecode_Minutes)->get_text());
1238         TC.seconds = atoi (label (Timecode_Seconds)->get_text());
1239         TC.frames = atoi (label (Timecode_Frames)->get_text());
1240         TC.rate = _session->timecode_frames_per_second();
1241         TC.drop= _session->timecode_drop_frames();
1242
1243         _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1244
1245
1246 #if 0
1247 #define Timecode_SAMPLE_TEST_1
1248 #define Timecode_SAMPLE_TEST_2
1249 #define Timecode_SAMPLE_TEST_3
1250 #define Timecode_SAMPLE_TEST_4
1251 #define Timecode_SAMPLE_TEST_5
1252 #define Timecode_SAMPLE_TEST_6
1253 #define Timecode_SAMPLE_TEST_7
1254
1255         // Testcode for timecode<->sample conversions (P.S.)
1256         Timecode::Time timecode1;
1257         framepos_t sample1;
1258         framepos_t oldsample = 0;
1259         Timecode::Time timecode2;
1260         framecnt_t sample_increment;
1261
1262         sample_increment = (framecnt_t)rint(_session->frame_rate() / _session->timecode_frames_per_second);
1263
1264 #ifdef Timecode_SAMPLE_TEST_1
1265         // Test 1: use_offset = false, use_subframes = false
1266         cout << "use_offset = false, use_subframes = false" << endl;
1267         for (int i = 0; i < 108003; i++) {
1268                 _session->timecode_to_sample( timecode1, sample1, false /* use_offset */, false /* use_subframes */ );
1269                 _session->sample_to_timecode( sample1, timecode2, false /* use_offset */, false /* use_subframes */ );
1270
1271                 if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1272                         cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1273                         cout << "timecode1: " << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1274                         cout << "sample: " << sample1 << endl;
1275                         cout << "sample: " << sample1 << " -> ";
1276                         cout << "timecode2: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1277                         break;
1278                 }
1279
1280                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1281                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1282                         cout << "timecode1: " << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1283                         cout << "sample: " << sample1 << endl;
1284                         cout << "sample: " << sample1 << " -> ";
1285                         cout << "timecode2: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1286                         break;
1287                 }
1288                 oldsample = sample1;
1289                 _session->timecode_increment( timecode1 );
1290         }
1291
1292         cout << "sample_increment: " << sample_increment << endl;
1293         cout << "sample: " << sample1 << " -> ";
1294         cout << "timecode: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1295 #endif
1296
1297 #ifdef Timecode_SAMPLE_TEST_2
1298         // Test 2: use_offset = true, use_subframes = false
1299         cout << "use_offset = true, use_subframes = false" << endl;
1300
1301         timecode1.hours = 0;
1302         timecode1.minutes = 0;
1303         timecode1.seconds = 0;
1304         timecode1.frames = 0;
1305         timecode1.subframes = 0;
1306         sample1 = oldsample = 0;
1307
1308         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1309         cout << "Starting at sample: " << sample1 << " -> ";
1310         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1311
1312         for (int i = 0; i < 108003; i++) {
1313                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1314                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1315
1316 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1317 //     cout << "sample: " << sample1 << endl;
1318 //     cout << "sample: " << sample1 << " -> ";
1319 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1320
1321                 if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1322                         cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1323                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1324                         cout << "sample: " << sample1 << endl;
1325                         cout << "sample: " << sample1 << " -> ";
1326                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1327                         break;
1328                 }
1329
1330                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1331                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1332                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1333                         cout << "sample: " << sample1 << endl;
1334                         cout << "sample: " << sample1 << " -> ";
1335                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1336                         break;
1337                 }
1338                 oldsample = sample1;
1339                 _session->timecode_increment( timecode1 );
1340         }
1341
1342         cout << "sample_increment: " << sample_increment << endl;
1343         cout << "sample: " << sample1 << " -> ";
1344         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1345 #endif
1346
1347 #ifdef Timecode_SAMPLE_TEST_3
1348         // Test 3: use_offset = true, use_subframes = false, decrement
1349         cout << "use_offset = true, use_subframes = false, decrement" << endl;
1350
1351         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1352         cout << "Starting at sample: " << sample1 << " -> ";
1353         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1354
1355         for (int i = 0; i < 108003; i++) {
1356                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1357                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1358
1359 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1360 //     cout << "sample: " << sample1 << endl;
1361 //     cout << "sample: " << sample1 << " -> ";
1362 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1363
1364                 if ((i > 0) && ( ((oldsample - sample1) != sample_increment) && ((oldsample - sample1) != (sample_increment + 1)) && ((oldsample - sample1) != (sample_increment - 1)))) {
1365                         cout << "ERROR: sample increment not right: " << (oldsample - sample1) << " != " << sample_increment << endl;
1366                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1367                         cout << "sample: " << sample1 << endl;
1368                         cout << "sample: " << sample1 << " -> ";
1369                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1370                         break;
1371                 }
1372
1373                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1374                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1375                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1376                         cout << "sample: " << sample1 << endl;
1377                         cout << "sample: " << sample1 << " -> ";
1378                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1379                         break;
1380                 }
1381                 oldsample = sample1;
1382                 _session->timecode_decrement( timecode1 );
1383         }
1384
1385         cout << "sample_decrement: " << sample_increment << endl;
1386         cout << "sample: " << sample1 << " -> ";
1387         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1388 #endif
1389
1390
1391 #ifdef Timecode_SAMPLE_TEST_4
1392         // Test 4: use_offset = true, use_subframes = true
1393         cout << "use_offset = true, use_subframes = true" << endl;
1394
1395         for (long sub = 5; sub < 80; sub += 5) {
1396                 timecode1.hours = 0;
1397                 timecode1.minutes = 0;
1398                 timecode1.seconds = 0;
1399                 timecode1.frames = 0;
1400                 timecode1.subframes = 0;
1401                 sample1 = oldsample = (sample_increment * sub) / 80;
1402
1403                 _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, true /* use_subframes */ );
1404
1405                 cout << "starting at sample: " << sample1 << " -> ";
1406                 cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1407
1408                 for (int i = 0; i < 108003; i++) {
1409                         _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, true /* use_subframes */ );
1410                         _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, true /* use_subframes */ );
1411
1412                         if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1413                                 cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1414                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1415                                 cout << "sample: " << sample1 << endl;
1416                                 cout << "sample: " << sample1 << " -> ";
1417                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1418                                 //break;
1419                         }
1420
1421                         if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames || timecode2.subframes != timecode1.subframes) {
1422                                 cout << "ERROR: timecode2 not equal timecode1" << endl;
1423                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1424                                 cout << "sample: " << sample1 << endl;
1425                                 cout << "sample: " << sample1 << " -> ";
1426                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1427                                 break;
1428                         }
1429                         oldsample = sample1;
1430                         _session->timecode_increment( timecode1 );
1431                 }
1432
1433                 cout << "sample_increment: " << sample_increment << endl;
1434                 cout << "sample: " << sample1 << " -> ";
1435                 cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1436
1437                 for (int i = 0; i < 108003; i++) {
1438                         _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, true /* use_subframes */ );
1439                         _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, true /* use_subframes */ );
1440
1441                         if ((i > 0) && ( ((oldsample - sample1) != sample_increment) && ((oldsample - sample1) != (sample_increment + 1)) && ((oldsample - sample1) != (sample_increment - 1)))) {
1442                                 cout << "ERROR: sample increment not right: " << (oldsample - sample1) << " != " << sample_increment << endl;
1443                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1444                                 cout << "sample: " << sample1 << endl;
1445                                 cout << "sample: " << sample1 << " -> ";
1446                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1447                                 //break;
1448                         }
1449
1450                         if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames || timecode2.subframes != timecode1.subframes) {
1451                                 cout << "ERROR: timecode2 not equal timecode1" << endl;
1452                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1453                                 cout << "sample: " << sample1 << endl;
1454                                 cout << "sample: " << sample1 << " -> ";
1455                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1456                                 break;
1457                         }
1458                         oldsample = sample1;
1459                         _session->timecode_decrement( timecode1 );
1460                 }
1461
1462                 cout << "sample_decrement: " << sample_increment << endl;
1463                 cout << "sample: " << sample1 << " -> ";
1464                 cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1465         }
1466 #endif
1467
1468
1469 #ifdef Timecode_SAMPLE_TEST_5
1470         // Test 5: use_offset = true, use_subframes = false, increment seconds
1471         cout << "use_offset = true, use_subframes = false, increment seconds" << endl;
1472
1473         timecode1.hours = 0;
1474         timecode1.minutes = 0;
1475         timecode1.seconds = 0;
1476         timecode1.frames = 0;
1477         timecode1.subframes = 0;
1478         sample1 = oldsample = 0;
1479         sample_increment = _session->frame_rate();
1480
1481         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1482         cout << "Starting at sample: " << sample1 << " -> ";
1483         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1484
1485         for (int i = 0; i < 3600; i++) {
1486                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1487                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1488
1489 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1490 //     cout << "sample: " << sample1 << endl;
1491 //     cout << "sample: " << sample1 << " -> ";
1492 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1493
1494 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1495 //     {
1496 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1497 //       break;
1498 //     }
1499
1500                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1501                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1502                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1503                         cout << "sample: " << sample1 << endl;
1504                         cout << "sample: " << sample1 << " -> ";
1505                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1506                         break;
1507                 }
1508                 oldsample = sample1;
1509                 _session->timecode_increment_seconds( timecode1 );
1510         }
1511
1512         cout << "sample_increment: " << sample_increment << endl;
1513         cout << "sample: " << sample1 << " -> ";
1514         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1515 #endif
1516
1517
1518 #ifdef Timecode_SAMPLE_TEST_6
1519         // Test 6: use_offset = true, use_subframes = false, increment minutes
1520         cout << "use_offset = true, use_subframes = false, increment minutes" << endl;
1521
1522         timecode1.hours = 0;
1523         timecode1.minutes = 0;
1524         timecode1.seconds = 0;
1525         timecode1.frames = 0;
1526         timecode1.subframes = 0;
1527         sample1 = oldsample = 0;
1528         sample_increment = _session->frame_rate() * 60;
1529
1530         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1531         cout << "Starting at sample: " << sample1 << " -> ";
1532         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1533
1534         for (int i = 0; i < 60; i++) {
1535                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1536                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1537
1538 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1539 //     cout << "sample: " << sample1 << endl;
1540 //     cout << "sample: " << sample1 << " -> ";
1541 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1542
1543 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1544 //     {
1545 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1546 //       break;
1547 //     }
1548
1549                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1550                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1551                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1552                         cout << "sample: " << sample1 << endl;
1553                         cout << "sample: " << sample1 << " -> ";
1554                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1555                         break;
1556                 }
1557                 oldsample = sample1;
1558                 _session->timecode_increment_minutes( timecode1 );
1559         }
1560
1561         cout << "sample_increment: " << sample_increment << endl;
1562         cout << "sample: " << sample1 << " -> ";
1563         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1564 #endif
1565
1566 #ifdef Timecode_SAMPLE_TEST_7
1567         // Test 7: use_offset = true, use_subframes = false, increment hours
1568         cout << "use_offset = true, use_subframes = false, increment hours" << endl;
1569
1570         timecode1.hours = 0;
1571         timecode1.minutes = 0;
1572         timecode1.seconds = 0;
1573         timecode1.frames = 0;
1574         timecode1.subframes = 0;
1575         sample1 = oldsample = 0;
1576         sample_increment = _session->frame_rate() * 60 * 60;
1577
1578         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1579         cout << "Starting at sample: " << sample1 << " -> ";
1580         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1581
1582         for (int i = 0; i < 10; i++) {
1583                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1584                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1585
1586 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1587 //     cout << "sample: " << sample1 << endl;
1588 //     cout << "sample: " << sample1 << " -> ";
1589 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1590
1591 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1592 //     {
1593 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1594 //       break;
1595 //     }
1596
1597                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1598                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1599                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1600                         cout << "sample: " << sample1 << endl;
1601                         cout << "sample: " << sample1 << " -> ";
1602                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1603                         break;
1604                 }
1605                 oldsample = sample1;
1606                 _session->timecode_increment_hours( timecode1 );
1607         }
1608
1609         cout << "sample_increment: " << sample_increment << endl;
1610         cout << "sample: " << sample1 << " -> ";
1611         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1612 #endif
1613
1614 #endif
1615
1616         return sample;
1617 }
1618
1619 framepos_t
1620 AudioClock::minsec_frame_from_display () const
1621 {
1622         if (_session == 0) {
1623                 return 0;
1624         }
1625
1626         int hrs = atoi (label (MS_Hours)->get_text());
1627         int mins = atoi (label (MS_Minutes)->get_text());
1628         int secs = atoi (label (MS_Seconds)->get_text());
1629         int millisecs = atoi (label (MS_Milliseconds)->get_text());
1630
1631         framecnt_t sr = _session->frame_rate();
1632
1633         return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1634 }
1635
1636 framepos_t
1637 AudioClock::bbt_frame_from_display (framepos_t pos) const
1638 {
1639         if (_session == 0) {
1640                 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1641                 return 0;
1642         }
1643
1644         AnyTime any;
1645         any.type = AnyTime::BBT;
1646
1647         any.bbt.bars = atoi (label (Bars)->get_text());
1648         any.bbt.beats = atoi (label (Beats)->get_text());
1649         any.bbt.ticks = atoi (label (Ticks)->get_text());
1650
1651         if (is_duration) {
1652                 any.bbt.bars++;
1653                 any.bbt.beats++;
1654                 return _session->any_duration_to_frames (pos, any);
1655         } else {
1656                 return _session->convert_to_frames (any);
1657         }
1658 }
1659
1660
1661 framepos_t
1662 AudioClock::bbt_frame_duration_from_display (framepos_t pos) const
1663 {
1664         if (_session == 0) {
1665                 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1666                 return 0;
1667         }
1668
1669         Timecode::BBT_Time bbt;
1670
1671
1672         bbt.bars = atoi (label (Bars)->get_text());
1673         bbt.beats = atoi (label (Beats)->get_text());
1674         bbt.ticks = atoi (label (Ticks)->get_text());
1675
1676         return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1677 }
1678
1679 framepos_t
1680 AudioClock::audio_frame_from_display () const
1681 {
1682         return (framepos_t) atoi (label (AudioFrames)->get_text ());
1683 }
1684
1685 void
1686 AudioClock::build_ops_menu ()
1687 {
1688         using namespace Menu_Helpers;
1689         ops_menu = new Menu;
1690         MenuList& ops_items = ops_menu->items();
1691         ops_menu->set_name ("ArdourContextMenu");
1692
1693         if (!Profile->get_sae()) {
1694                 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1695         }
1696         ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1697         ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1698         ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1699         ops_items.push_back (MenuElem (_("Off"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Off)));
1700
1701         if (editable && !is_duration && !_follows_playhead) {
1702                 ops_items.push_back (SeparatorElem());
1703                 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1704                 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1705         }
1706 }
1707
1708 void
1709 AudioClock::set_from_playhead ()
1710 {
1711         if (!_session) {
1712                 return;
1713         }
1714
1715         set (_session->transport_frame());
1716         ValueChanged ();
1717 }
1718
1719 void
1720 AudioClock::locate ()
1721 {
1722         if (!_session || is_duration) {
1723                 return;
1724         }
1725
1726         _session->request_locate (current_time(), _session->transport_rolling ());
1727 }
1728
1729 void
1730 AudioClock::disconnect_signals ()
1731 {
1732         scroll_connection.disconnect ();
1733         button_press_connection.disconnect ();
1734         button_release_connection.disconnect ();
1735 }
1736
1737 void
1738 AudioClock::connect_signals ()
1739 {
1740         disconnect_signals ();
1741
1742         scroll_connection = display->scroll.connect (sigc::mem_fun (*this, &AudioClock::scroll));
1743         button_press_connection = display->button_press.connect (sigc::mem_fun (*this, &AudioClock::button_press));
1744         button_release_connection = display->button_release.connect (sigc::mem_fun (*this, &AudioClock::button_release));
1745 }
1746
1747 void
1748 AudioClock::set_mode (Mode m)
1749 {
1750         if (_mode == m) {
1751                 return;
1752         }
1753
1754         _mode = m;
1755
1756         display->clear_cells ();
1757
1758         if (supplemental_left) {
1759                 supplemental_left->clear_cells ();
1760                 supplemental_right->clear_cells ();
1761         }
1762
1763         switch (_mode) {
1764         case Timecode:
1765                 display->add_cell (_text_cells[Timecode_Hours]);
1766                 display->add_cell (_fixed_cells[Colon1]);
1767                 display->add_cell (_text_cells[Timecode_Minutes]);
1768                 display->add_cell (_fixed_cells[Colon2]);
1769                 display->add_cell (_text_cells[Timecode_Seconds]);
1770                 display->add_cell (_fixed_cells[Colon3]);
1771                 display->add_cell (_text_cells[Timecode_Frames]);
1772
1773                 if (supplemental_left) {
1774                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1775                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1776                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1777                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1778
1779                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 4);
1780                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 8);
1781
1782                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 4);
1783                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 6.25);
1784
1785                         supplemental_left->set_text (_text_cells[LowerLeft1], _("EXT"));
1786                         supplemental_right->set_text (_text_cells[LowerRight1], _("FPS"));
1787                 }
1788                 break;
1789
1790         case BBT:
1791                 display->add_cell (_text_cells[Bars]);
1792                 display->add_cell (_fixed_cells[Bar1]);
1793                 display->add_cell (_text_cells[Beats]);
1794                 display->add_cell (_fixed_cells[Bar2]);
1795                 display->add_cell (_text_cells[Ticks]);
1796                 if (supplemental_left) {
1797                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1798                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1799                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1800                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1801
1802                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 1);
1803                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 5.25);
1804
1805                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 1);
1806                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 5);
1807
1808                         supplemental_left->set_text (_text_cells[LowerLeft1], _("M"));
1809                         supplemental_right->set_text (_text_cells[LowerRight1], _("T"));
1810                 }
1811                 break;
1812
1813         case MinSec:
1814                 display->add_cell (_text_cells[MS_Hours]);
1815                 display->add_cell (_fixed_cells[Colon1]);
1816                 display->add_cell (_text_cells[MS_Minutes]);
1817                 display->add_cell (_fixed_cells[Colon2]);
1818                 display->add_cell (_text_cells[MS_Seconds]);
1819                 display->add_cell (_fixed_cells[Colon3]);
1820                 display->add_cell (_text_cells[MS_Milliseconds]);
1821                 break;
1822
1823         case Frames:
1824                 display->add_cell (_text_cells[AudioFrames]);
1825                 if (supplemental_left) {
1826                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1827                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1828                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1829                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1830
1831                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 3);
1832                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 5);
1833                         
1834                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 5);
1835                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 5);
1836
1837                         supplemental_left->set_text (_text_cells[LowerLeft1], _("SR"));
1838                         supplemental_right->set_text (_text_cells[LowerRight1], _("Pull"));
1839                 }
1840                 break;
1841
1842         case Off:
1843                 break;
1844         }
1845
1846         if (supplemental_left) {
1847                 /* clear information cells */
1848                 supplemental_left->set_text (_text_cells[LowerLeft2], _(""));
1849                 supplemental_right->set_text (_text_cells[LowerRight2], _(""));
1850
1851                 /* propagate font style,but smaller, into cells */
1852                 boost::shared_ptr<CairoFontDescription> smaller_font (new CairoFontDescription (*display->font().get()));
1853                 smaller_font->set_size (12);
1854                 supplemental_right->set_font (smaller_font);
1855                 supplemental_left->set_font (smaller_font);
1856         }
1857
1858         if (_mode != Off) {
1859                 connect_signals ();
1860         } else {
1861                 disconnect_signals  ();
1862         }
1863
1864         set (last_when, true);
1865
1866         if (!is_transient) {
1867                 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1868         }
1869
1870         mode_changed (); /* EMIT SIGNAL (the member one) */
1871 }
1872
1873 void
1874 AudioClock::set_bbt_reference (framepos_t pos)
1875 {
1876         bbt_reference_time = pos;
1877 }
1878
1879 void
1880 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1881 {
1882         VBox::on_style_changed (old_style);
1883         set_theme ();
1884 }
1885
1886 void
1887 AudioClock::set_is_duration (bool yn)
1888 {
1889         if (yn == is_duration) {
1890                 return;
1891         }
1892
1893         is_duration = yn;
1894         set (last_when, true, 0, 's');
1895 }
1896