83a0b8719bff04aba8f25d22d956562b90aaff75
[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 (cell) {
893                         editing_field = (Field) cell->id ();
894                         display->start_editing (cell);
895                 }
896
897                 Keyboard::magic_widget_grab_focus ();
898
899                 /* make absolutely sure that the pointer is grabbed */
900                 gdk_pointer_grab(ev->window,false ,
901                                  GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
902                                  NULL,NULL,ev->time);
903                 dragging = true;
904                 drag_accum = 0;
905                 drag_start_y = ev->y;
906                 drag_y = ev->y;
907                 break;
908                 
909         default:
910                 return false;
911                 break;
912         }
913
914         return true;
915 }
916
917 bool
918 AudioClock::button_release (GdkEventButton *ev, CairoCell* cell)
919 {
920         cerr << "button press, cell = " << cell << endl;
921
922         if (dragging) {
923                 gdk_pointer_ungrab (GDK_CURRENT_TIME);
924                 dragging = false;
925                 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
926                         // we actually dragged so return without setting editing focus, or we shift clicked
927                         return true;
928                 }
929         }
930
931         if (!editable) {
932                 if (ops_menu == 0) {
933                         build_ops_menu ();
934                 }
935                 ops_menu->popup (1, ev->time);
936                 return true;
937         }
938
939         if (Keyboard::is_context_menu_event (ev)) {
940                 if (ops_menu == 0) {
941                         build_ops_menu ();
942                 }
943                 ops_menu->popup (1, ev->time);
944                 return true;
945         }
946
947         return true;
948 }
949
950 bool
951 AudioClock::scroll (GdkEventScroll *ev, CairoCell* cell)
952 {
953         if (_session == 0) {
954                 return false;
955         }
956
957         framepos_t frames = 0;
958
959         switch (ev->direction) {
960
961         case GDK_SCROLL_UP:
962                 frames = get_frames ((Field) cell->id());
963                 if (frames != 0) {
964                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
965                                 frames *= 10;
966                         }
967                         set (current_time() + frames, true);
968                         ValueChanged (); /* EMIT_SIGNAL */
969                 }
970                 break;
971                 
972         case GDK_SCROLL_DOWN:
973                 frames = get_frames ((Field) cell->id());
974                 if (frames != 0) {
975                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
976                                 frames *= 10;
977                         }
978                         
979                         if ((double)current_time() - (double)frames < 0.0) {
980                                 set (0, true);
981                         } else {
982                                 set (current_time() - frames, true);
983                         }
984                         
985                         ValueChanged (); /* EMIT_SIGNAL */
986                 }
987                 break;
988                 
989         default:
990                 return false;
991                 break;
992         }
993         
994         return true;
995 }
996
997 bool
998 AudioClock::field_motion_notify_event (GdkEventMotion *ev, Field field)
999 {
1000         if (_session == 0 || !dragging) {
1001                 return false;
1002         }
1003
1004         float pixel_frame_scale_factor = 0.2f;
1005
1006 /*
1007         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier))  {
1008                 pixel_frame_scale_factor = 0.1f;
1009         }
1010
1011
1012         if (Keyboard::modifier_state_contains (ev->state,
1013                                                Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1014
1015                 pixel_frame_scale_factor = 0.025f;
1016         }
1017 */
1018         double y_delta = ev->y - drag_y;
1019
1020         drag_accum +=  y_delta*pixel_frame_scale_factor;
1021
1022         drag_y = ev->y;
1023
1024         if (trunc(drag_accum) != 0) {
1025
1026                 framepos_t frames;
1027                 framepos_t pos;
1028                 int dir;
1029                 dir = (drag_accum < 0 ? 1:-1);
1030                 pos = current_time();
1031                 frames = get_frames (field,pos,dir);
1032
1033                 if (frames  != 0 &&  frames * drag_accum < current_time()) {
1034
1035                         set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in computer-land
1036
1037                 } else {
1038                         set (0 , false);
1039
1040                 }
1041
1042                 drag_accum= 0;
1043                 ValueChanged();  /* EMIT_SIGNAL */
1044
1045
1046         }
1047
1048         return true;
1049 }
1050
1051 framepos_t
1052 AudioClock::get_frames (Field field, framepos_t pos, int dir)
1053 {
1054         framecnt_t f = 0;
1055         Timecode::BBT_Time BBT;
1056         switch (field) {
1057         case Timecode_Hours:
1058                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1059                 break;
1060         case Timecode_Minutes:
1061                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1062                 break;
1063         case Timecode_Seconds:
1064                 f = _session->frame_rate();
1065                 break;
1066         case Timecode_Frames:
1067                 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1068                 break;
1069
1070         case AudioFrames:
1071                 f = 1;
1072                 break;
1073
1074         case MS_Hours:
1075                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1076                 break;
1077         case MS_Minutes:
1078                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1079                 break;
1080         case MS_Seconds:
1081                 f = (framecnt_t) _session->frame_rate();
1082                 break;
1083         case MS_Milliseconds:
1084                 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1085                 break;
1086
1087         case Bars:
1088                 BBT.bars = 1;
1089                 BBT.beats = 0;
1090                 BBT.ticks = 0;
1091                 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1092                 break;
1093         case Beats:
1094                 BBT.bars = 0;
1095                 BBT.beats = 1;
1096                 BBT.ticks = 0;
1097                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1098                 break;
1099         case Ticks:
1100                 BBT.bars = 0;
1101                 BBT.beats = 0;
1102                 BBT.ticks = 1;
1103                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1104                 break;
1105         default:
1106                 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1107                 f = 0;
1108                 break;
1109         }
1110
1111         return f;
1112 }
1113
1114 framepos_t
1115 AudioClock::current_time (framepos_t pos) const
1116 {
1117         if (!_canonical_time_is_displayed) {
1118                 return _canonical_time;
1119         }
1120
1121         framepos_t ret = 0;
1122
1123         switch (_mode) {
1124         case Timecode:
1125                 ret = timecode_frame_from_display ();
1126                 break;
1127         case BBT:
1128                 ret = bbt_frame_from_display (pos);
1129                 break;
1130
1131         case MinSec:
1132                 ret = minsec_frame_from_display ();
1133                 break;
1134
1135         case Frames:
1136                 ret = audio_frame_from_display ();
1137                 break;
1138
1139         case Off:
1140                 break;
1141         }
1142
1143         return ret;
1144 }
1145
1146 framepos_t
1147 AudioClock::current_duration (framepos_t pos) const
1148 {
1149         framepos_t ret = 0;
1150
1151         switch (_mode) {
1152         case Timecode:
1153                 ret = timecode_frame_from_display ();
1154                 break;
1155         case BBT:
1156                 ret = bbt_frame_duration_from_display (pos);
1157                 break;
1158
1159         case MinSec:
1160                 ret = minsec_frame_from_display ();
1161                 break;
1162
1163         case Frames:
1164                 ret = audio_frame_from_display ();
1165                 break;
1166
1167         case Off:
1168                 break;
1169         }
1170
1171         return ret;
1172 }
1173
1174 void
1175 AudioClock::timecode_sanitize_display()
1176 {
1177         // Check Timecode fields for sanity, possibly adjusting values
1178         if (atoi (_text_cells[Timecode_Minutes]->get_text()) > 59) {
1179                 display->set_text (_text_cells[Timecode_Minutes], "59");
1180                 _canonical_time_is_displayed = true;
1181         }
1182
1183         if (atoi (_text_cells[Timecode_Seconds]->get_text()) > 59) {
1184                 display->set_text (_text_cells[Timecode_Seconds], "59");
1185                 _canonical_time_is_displayed = true;
1186         }
1187
1188         switch ((long)rint(_session->timecode_frames_per_second())) {
1189         case 24:
1190                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 23) {
1191                         display->set_text (_text_cells[Timecode_Frames], "23");
1192                         _canonical_time_is_displayed = true;
1193                 }
1194                 break;
1195         case 25:
1196                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 24) {
1197                         display->set_text (_text_cells[Timecode_Frames], "24");
1198                         _canonical_time_is_displayed = true;
1199                 }
1200                 break;
1201         case 30:
1202                 if (atoi (_text_cells[Timecode_Frames]->get_text()) > 29) {
1203                         display->set_text (_text_cells[Timecode_Frames], "29");
1204                         _canonical_time_is_displayed = true;
1205                 }
1206                 break;
1207         default:
1208                 break;
1209         }
1210
1211         if (_session->timecode_drop_frames()) {
1212                 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)) {
1213                         display->set_text (_text_cells[Timecode_Frames], "02");
1214                         _canonical_time_is_displayed = true;
1215                 }
1216         }
1217 }
1218
1219 /** This is necessary because operator[] isn't const with std::map.
1220  *  @param f Field.
1221  *  @return Label widget.
1222  */
1223 CairoTextCell*
1224 AudioClock::label (Field f) const
1225 {
1226         std::map<Field,CairoTextCell*>::const_iterator i = _text_cells.find (f);
1227         assert (i != _text_cells.end ());
1228
1229         return i->second;
1230 }
1231
1232 framepos_t
1233 AudioClock::timecode_frame_from_display () const
1234 {
1235         if (_session == 0) {
1236                 return 0;
1237         }
1238
1239         Timecode::Time TC;
1240         framepos_t sample;
1241
1242         TC.hours = atoi (label (Timecode_Hours)->get_text());
1243         TC.minutes = atoi (label (Timecode_Minutes)->get_text());
1244         TC.seconds = atoi (label (Timecode_Seconds)->get_text());
1245         TC.frames = atoi (label (Timecode_Frames)->get_text());
1246         TC.rate = _session->timecode_frames_per_second();
1247         TC.drop= _session->timecode_drop_frames();
1248
1249         _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1250
1251
1252 #if 0
1253 #define Timecode_SAMPLE_TEST_1
1254 #define Timecode_SAMPLE_TEST_2
1255 #define Timecode_SAMPLE_TEST_3
1256 #define Timecode_SAMPLE_TEST_4
1257 #define Timecode_SAMPLE_TEST_5
1258 #define Timecode_SAMPLE_TEST_6
1259 #define Timecode_SAMPLE_TEST_7
1260
1261         // Testcode for timecode<->sample conversions (P.S.)
1262         Timecode::Time timecode1;
1263         framepos_t sample1;
1264         framepos_t oldsample = 0;
1265         Timecode::Time timecode2;
1266         framecnt_t sample_increment;
1267
1268         sample_increment = (framecnt_t)rint(_session->frame_rate() / _session->timecode_frames_per_second);
1269
1270 #ifdef Timecode_SAMPLE_TEST_1
1271         // Test 1: use_offset = false, use_subframes = false
1272         cout << "use_offset = false, use_subframes = false" << endl;
1273         for (int i = 0; i < 108003; i++) {
1274                 _session->timecode_to_sample( timecode1, sample1, false /* use_offset */, false /* use_subframes */ );
1275                 _session->sample_to_timecode( sample1, timecode2, false /* use_offset */, false /* use_subframes */ );
1276
1277                 if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1278                         cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1279                         cout << "timecode1: " << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1280                         cout << "sample: " << sample1 << endl;
1281                         cout << "sample: " << sample1 << " -> ";
1282                         cout << "timecode2: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1283                         break;
1284                 }
1285
1286                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1287                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1288                         cout << "timecode1: " << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1289                         cout << "sample: " << sample1 << endl;
1290                         cout << "sample: " << sample1 << " -> ";
1291                         cout << "timecode2: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1292                         break;
1293                 }
1294                 oldsample = sample1;
1295                 _session->timecode_increment( timecode1 );
1296         }
1297
1298         cout << "sample_increment: " << sample_increment << endl;
1299         cout << "sample: " << sample1 << " -> ";
1300         cout << "timecode: " << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1301 #endif
1302
1303 #ifdef Timecode_SAMPLE_TEST_2
1304         // Test 2: use_offset = true, use_subframes = false
1305         cout << "use_offset = true, use_subframes = false" << endl;
1306
1307         timecode1.hours = 0;
1308         timecode1.minutes = 0;
1309         timecode1.seconds = 0;
1310         timecode1.frames = 0;
1311         timecode1.subframes = 0;
1312         sample1 = oldsample = 0;
1313
1314         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1315         cout << "Starting at sample: " << sample1 << " -> ";
1316         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1317
1318         for (int i = 0; i < 108003; i++) {
1319                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1320                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1321
1322 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1323 //     cout << "sample: " << sample1 << endl;
1324 //     cout << "sample: " << sample1 << " -> ";
1325 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1326
1327                 if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1328                         cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1329                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1330                         cout << "sample: " << sample1 << endl;
1331                         cout << "sample: " << sample1 << " -> ";
1332                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1333                         break;
1334                 }
1335
1336                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1337                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1338                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1339                         cout << "sample: " << sample1 << endl;
1340                         cout << "sample: " << sample1 << " -> ";
1341                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1342                         break;
1343                 }
1344                 oldsample = sample1;
1345                 _session->timecode_increment( timecode1 );
1346         }
1347
1348         cout << "sample_increment: " << sample_increment << endl;
1349         cout << "sample: " << sample1 << " -> ";
1350         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1351 #endif
1352
1353 #ifdef Timecode_SAMPLE_TEST_3
1354         // Test 3: use_offset = true, use_subframes = false, decrement
1355         cout << "use_offset = true, use_subframes = false, decrement" << endl;
1356
1357         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1358         cout << "Starting at sample: " << sample1 << " -> ";
1359         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1360
1361         for (int i = 0; i < 108003; i++) {
1362                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1363                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1364
1365 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1366 //     cout << "sample: " << sample1 << endl;
1367 //     cout << "sample: " << sample1 << " -> ";
1368 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1369
1370                 if ((i > 0) && ( ((oldsample - sample1) != sample_increment) && ((oldsample - sample1) != (sample_increment + 1)) && ((oldsample - sample1) != (sample_increment - 1)))) {
1371                         cout << "ERROR: sample increment not right: " << (oldsample - sample1) << " != " << sample_increment << endl;
1372                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1373                         cout << "sample: " << sample1 << endl;
1374                         cout << "sample: " << sample1 << " -> ";
1375                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1376                         break;
1377                 }
1378
1379                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1380                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1381                         cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1382                         cout << "sample: " << sample1 << endl;
1383                         cout << "sample: " << sample1 << " -> ";
1384                         cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1385                         break;
1386                 }
1387                 oldsample = sample1;
1388                 _session->timecode_decrement( timecode1 );
1389         }
1390
1391         cout << "sample_decrement: " << sample_increment << endl;
1392         cout << "sample: " << sample1 << " -> ";
1393         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1394 #endif
1395
1396
1397 #ifdef Timecode_SAMPLE_TEST_4
1398         // Test 4: use_offset = true, use_subframes = true
1399         cout << "use_offset = true, use_subframes = true" << endl;
1400
1401         for (long sub = 5; sub < 80; sub += 5) {
1402                 timecode1.hours = 0;
1403                 timecode1.minutes = 0;
1404                 timecode1.seconds = 0;
1405                 timecode1.frames = 0;
1406                 timecode1.subframes = 0;
1407                 sample1 = oldsample = (sample_increment * sub) / 80;
1408
1409                 _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, true /* use_subframes */ );
1410
1411                 cout << "starting at sample: " << sample1 << " -> ";
1412                 cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1413
1414                 for (int i = 0; i < 108003; i++) {
1415                         _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, true /* use_subframes */ );
1416                         _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, true /* use_subframes */ );
1417
1418                         if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1)))) {
1419                                 cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1420                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1421                                 cout << "sample: " << sample1 << endl;
1422                                 cout << "sample: " << sample1 << " -> ";
1423                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1424                                 //break;
1425                         }
1426
1427                         if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames || timecode2.subframes != timecode1.subframes) {
1428                                 cout << "ERROR: timecode2 not equal timecode1" << endl;
1429                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1430                                 cout << "sample: " << sample1 << endl;
1431                                 cout << "sample: " << sample1 << " -> ";
1432                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1433                                 break;
1434                         }
1435                         oldsample = sample1;
1436                         _session->timecode_increment( timecode1 );
1437                 }
1438
1439                 cout << "sample_increment: " << sample_increment << endl;
1440                 cout << "sample: " << sample1 << " -> ";
1441                 cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1442
1443                 for (int i = 0; i < 108003; i++) {
1444                         _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, true /* use_subframes */ );
1445                         _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, true /* use_subframes */ );
1446
1447                         if ((i > 0) && ( ((oldsample - sample1) != sample_increment) && ((oldsample - sample1) != (sample_increment + 1)) && ((oldsample - sample1) != (sample_increment - 1)))) {
1448                                 cout << "ERROR: sample increment not right: " << (oldsample - sample1) << " != " << sample_increment << endl;
1449                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1450                                 cout << "sample: " << sample1 << endl;
1451                                 cout << "sample: " << sample1 << " -> ";
1452                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1453                                 //break;
1454                         }
1455
1456                         if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames || timecode2.subframes != timecode1.subframes) {
1457                                 cout << "ERROR: timecode2 not equal timecode1" << endl;
1458                                 cout << "timecode1: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1459                                 cout << "sample: " << sample1 << endl;
1460                                 cout << "sample: " << sample1 << " -> ";
1461                                 cout << "timecode2: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1462                                 break;
1463                         }
1464                         oldsample = sample1;
1465                         _session->timecode_decrement( timecode1 );
1466                 }
1467
1468                 cout << "sample_decrement: " << sample_increment << endl;
1469                 cout << "sample: " << sample1 << " -> ";
1470                 cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1471         }
1472 #endif
1473
1474
1475 #ifdef Timecode_SAMPLE_TEST_5
1476         // Test 5: use_offset = true, use_subframes = false, increment seconds
1477         cout << "use_offset = true, use_subframes = false, increment seconds" << endl;
1478
1479         timecode1.hours = 0;
1480         timecode1.minutes = 0;
1481         timecode1.seconds = 0;
1482         timecode1.frames = 0;
1483         timecode1.subframes = 0;
1484         sample1 = oldsample = 0;
1485         sample_increment = _session->frame_rate();
1486
1487         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1488         cout << "Starting at sample: " << sample1 << " -> ";
1489         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1490
1491         for (int i = 0; i < 3600; i++) {
1492                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1493                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1494
1495 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1496 //     cout << "sample: " << sample1 << endl;
1497 //     cout << "sample: " << sample1 << " -> ";
1498 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1499
1500 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1501 //     {
1502 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1503 //       break;
1504 //     }
1505
1506                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1507                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1508                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1509                         cout << "sample: " << sample1 << endl;
1510                         cout << "sample: " << sample1 << " -> ";
1511                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1512                         break;
1513                 }
1514                 oldsample = sample1;
1515                 _session->timecode_increment_seconds( timecode1 );
1516         }
1517
1518         cout << "sample_increment: " << sample_increment << endl;
1519         cout << "sample: " << sample1 << " -> ";
1520         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1521 #endif
1522
1523
1524 #ifdef Timecode_SAMPLE_TEST_6
1525         // Test 6: use_offset = true, use_subframes = false, increment minutes
1526         cout << "use_offset = true, use_subframes = false, increment minutes" << endl;
1527
1528         timecode1.hours = 0;
1529         timecode1.minutes = 0;
1530         timecode1.seconds = 0;
1531         timecode1.frames = 0;
1532         timecode1.subframes = 0;
1533         sample1 = oldsample = 0;
1534         sample_increment = _session->frame_rate() * 60;
1535
1536         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1537         cout << "Starting at sample: " << sample1 << " -> ";
1538         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1539
1540         for (int i = 0; i < 60; i++) {
1541                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1542                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1543
1544 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1545 //     cout << "sample: " << sample1 << endl;
1546 //     cout << "sample: " << sample1 << " -> ";
1547 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1548
1549 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1550 //     {
1551 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1552 //       break;
1553 //     }
1554
1555                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1556                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1557                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1558                         cout << "sample: " << sample1 << endl;
1559                         cout << "sample: " << sample1 << " -> ";
1560                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1561                         break;
1562                 }
1563                 oldsample = sample1;
1564                 _session->timecode_increment_minutes( timecode1 );
1565         }
1566
1567         cout << "sample_increment: " << sample_increment << endl;
1568         cout << "sample: " << sample1 << " -> ";
1569         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1570 #endif
1571
1572 #ifdef Timecode_SAMPLE_TEST_7
1573         // Test 7: use_offset = true, use_subframes = false, increment hours
1574         cout << "use_offset = true, use_subframes = false, increment hours" << endl;
1575
1576         timecode1.hours = 0;
1577         timecode1.minutes = 0;
1578         timecode1.seconds = 0;
1579         timecode1.frames = 0;
1580         timecode1.subframes = 0;
1581         sample1 = oldsample = 0;
1582         sample_increment = _session->frame_rate() * 60 * 60;
1583
1584         _session->sample_to_timecode( sample1, timecode1, true /* use_offset */, false /* use_subframes */ );
1585         cout << "Starting at sample: " << sample1 << " -> ";
1586         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << endl;
1587
1588         for (int i = 0; i < 10; i++) {
1589                 _session->timecode_to_sample( timecode1, sample1, true /* use_offset */, false /* use_subframes */ );
1590                 _session->sample_to_timecode( sample1, timecode2, true /* use_offset */, false /* use_subframes */ );
1591
1592 //     cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1593 //     cout << "sample: " << sample1 << endl;
1594 //     cout << "sample: " << sample1 << " -> ";
1595 //     cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1596
1597 //     if ((i > 0) && ( ((sample1 - oldsample) != sample_increment) && ((sample1 - oldsample) != (sample_increment + 1)) && ((sample1 - oldsample) != (sample_increment - 1))))
1598 //     {
1599 //       cout << "ERROR: sample increment not right: " << (sample1 - oldsample) << " != " << sample_increment << endl;
1600 //       break;
1601 //     }
1602
1603                 if (timecode2.hours != timecode1.hours || timecode2.minutes != timecode1.minutes || timecode2.seconds != timecode2.seconds || timecode2.frames != timecode1.frames) {
1604                         cout << "ERROR: timecode2 not equal timecode1" << endl;
1605                         cout << "timecode: " << (timecode1.negative ? "-" : "") << timecode1.hours << ":" << timecode1.minutes << ":" << timecode1.seconds << ":" << timecode1.frames << "::" << timecode1.subframes << " -> ";
1606                         cout << "sample: " << sample1 << endl;
1607                         cout << "sample: " << sample1 << " -> ";
1608                         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1609                         break;
1610                 }
1611                 oldsample = sample1;
1612                 _session->timecode_increment_hours( timecode1 );
1613         }
1614
1615         cout << "sample_increment: " << sample_increment << endl;
1616         cout << "sample: " << sample1 << " -> ";
1617         cout << "timecode: " << (timecode2.negative ? "-" : "") << timecode2.hours << ":" << timecode2.minutes << ":" << timecode2.seconds << ":" << timecode2.frames << "::" << timecode2.subframes << endl;
1618 #endif
1619
1620 #endif
1621
1622         return sample;
1623 }
1624
1625 framepos_t
1626 AudioClock::minsec_frame_from_display () const
1627 {
1628         if (_session == 0) {
1629                 return 0;
1630         }
1631
1632         int hrs = atoi (label (MS_Hours)->get_text());
1633         int mins = atoi (label (MS_Minutes)->get_text());
1634         int secs = atoi (label (MS_Seconds)->get_text());
1635         int millisecs = atoi (label (MS_Milliseconds)->get_text());
1636
1637         framecnt_t sr = _session->frame_rate();
1638
1639         return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1640 }
1641
1642 framepos_t
1643 AudioClock::bbt_frame_from_display (framepos_t pos) const
1644 {
1645         if (_session == 0) {
1646                 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1647                 return 0;
1648         }
1649
1650         AnyTime any;
1651         any.type = AnyTime::BBT;
1652
1653         any.bbt.bars = atoi (label (Bars)->get_text());
1654         any.bbt.beats = atoi (label (Beats)->get_text());
1655         any.bbt.ticks = atoi (label (Ticks)->get_text());
1656
1657         if (is_duration) {
1658                 any.bbt.bars++;
1659                 any.bbt.beats++;
1660                 return _session->any_duration_to_frames (pos, any);
1661         } else {
1662                 return _session->convert_to_frames (any);
1663         }
1664 }
1665
1666
1667 framepos_t
1668 AudioClock::bbt_frame_duration_from_display (framepos_t pos) const
1669 {
1670         if (_session == 0) {
1671                 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1672                 return 0;
1673         }
1674
1675         Timecode::BBT_Time bbt;
1676
1677
1678         bbt.bars = atoi (label (Bars)->get_text());
1679         bbt.beats = atoi (label (Beats)->get_text());
1680         bbt.ticks = atoi (label (Ticks)->get_text());
1681
1682         return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1683 }
1684
1685 framepos_t
1686 AudioClock::audio_frame_from_display () const
1687 {
1688         return (framepos_t) atoi (label (AudioFrames)->get_text ());
1689 }
1690
1691 void
1692 AudioClock::build_ops_menu ()
1693 {
1694         using namespace Menu_Helpers;
1695         ops_menu = new Menu;
1696         MenuList& ops_items = ops_menu->items();
1697         ops_menu->set_name ("ArdourContextMenu");
1698
1699         if (!Profile->get_sae()) {
1700                 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1701         }
1702         ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1703         ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1704         ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1705         ops_items.push_back (MenuElem (_("Off"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Off)));
1706
1707         if (editable && !is_duration && !_follows_playhead) {
1708                 ops_items.push_back (SeparatorElem());
1709                 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1710                 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1711         }
1712 }
1713
1714 void
1715 AudioClock::set_from_playhead ()
1716 {
1717         if (!_session) {
1718                 return;
1719         }
1720
1721         set (_session->transport_frame());
1722         ValueChanged ();
1723 }
1724
1725 void
1726 AudioClock::locate ()
1727 {
1728         if (!_session || is_duration) {
1729                 return;
1730         }
1731
1732         _session->request_locate (current_time(), _session->transport_rolling ());
1733 }
1734
1735 void
1736 AudioClock::disconnect_signals ()
1737 {
1738         scroll_connection.disconnect ();
1739         button_press_connection.disconnect ();
1740         button_release_connection.disconnect ();
1741 }
1742
1743 void
1744 AudioClock::connect_signals ()
1745 {
1746         disconnect_signals ();
1747
1748         if (editable) {
1749                 scroll_connection = display->scroll.connect (sigc::mem_fun (*this, &AudioClock::scroll));
1750                 button_press_connection = display->button_press.connect (sigc::mem_fun (*this, &AudioClock::button_press));
1751                 button_release_connection = display->button_release.connect (sigc::mem_fun (*this, &AudioClock::button_release));
1752         }       
1753 }
1754
1755 void
1756 AudioClock::set_mode (Mode m)
1757 {
1758         if (_mode == m) {
1759                 return;
1760         }
1761
1762         _mode = m;
1763
1764         display->clear_cells ();
1765
1766         if (supplemental_left) {
1767                 supplemental_left->clear_cells ();
1768                 supplemental_right->clear_cells ();
1769         }
1770
1771         switch (_mode) {
1772         case Timecode:
1773                 display->add_cell (_text_cells[Timecode_Hours]);
1774                 display->add_cell (_fixed_cells[Colon1]);
1775                 display->add_cell (_text_cells[Timecode_Minutes]);
1776                 display->add_cell (_fixed_cells[Colon2]);
1777                 display->add_cell (_text_cells[Timecode_Seconds]);
1778                 display->add_cell (_fixed_cells[Colon3]);
1779                 display->add_cell (_text_cells[Timecode_Frames]);
1780
1781                 if (supplemental_left) {
1782                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1783                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1784                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1785                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1786
1787                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 4);
1788                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 8);
1789
1790                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 4);
1791                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 6.25);
1792
1793                         supplemental_left->set_text (_text_cells[LowerLeft1], _("EXT"));
1794                         supplemental_right->set_text (_text_cells[LowerRight1], _("FPS"));
1795                 }
1796                 break;
1797
1798         case BBT:
1799                 display->add_cell (_text_cells[Bars]);
1800                 display->add_cell (_fixed_cells[Bar1]);
1801                 display->add_cell (_text_cells[Beats]);
1802                 display->add_cell (_fixed_cells[Bar2]);
1803                 display->add_cell (_text_cells[Ticks]);
1804                 if (supplemental_left) {
1805                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1806                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1807                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1808                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1809
1810                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 1);
1811                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 5.25);
1812
1813                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 1);
1814                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 5);
1815
1816                         supplemental_left->set_text (_text_cells[LowerLeft1], _("M"));
1817                         supplemental_right->set_text (_text_cells[LowerRight1], _("T"));
1818                 }
1819                 break;
1820
1821         case MinSec:
1822                 display->add_cell (_text_cells[MS_Hours]);
1823                 display->add_cell (_fixed_cells[Colon1]);
1824                 display->add_cell (_text_cells[MS_Minutes]);
1825                 display->add_cell (_fixed_cells[Colon2]);
1826                 display->add_cell (_text_cells[MS_Seconds]);
1827                 display->add_cell (_fixed_cells[Colon3]);
1828                 display->add_cell (_text_cells[MS_Milliseconds]);
1829                 break;
1830
1831         case Frames:
1832                 display->add_cell (_text_cells[AudioFrames]);
1833                 if (supplemental_left) {
1834                         supplemental_left->add_cell (_text_cells[LowerLeft1]);
1835                         supplemental_left->add_cell (_text_cells[LowerLeft2]);
1836                         supplemental_right->add_cell (_text_cells[LowerRight1]);
1837                         supplemental_right->add_cell (_text_cells[LowerRight2]);
1838
1839                         supplemental_left->set_width_chars (_text_cells[LowerLeft1], 3);
1840                         supplemental_left->set_width_chars (_text_cells[LowerLeft2], 5);
1841                         
1842                         supplemental_right->set_width_chars (_text_cells[LowerRight1], 5);
1843                         supplemental_right->set_width_chars (_text_cells[LowerRight2], 5);
1844
1845                         supplemental_left->set_text (_text_cells[LowerLeft1], _("SR"));
1846                         supplemental_right->set_text (_text_cells[LowerRight1], _("Pull"));
1847                 }
1848                 break;
1849
1850         case Off:
1851                 break;
1852         }
1853
1854         if (supplemental_left) {
1855                 /* clear information cells */
1856                 supplemental_left->set_text (_text_cells[LowerLeft2], _(""));
1857                 supplemental_right->set_text (_text_cells[LowerRight2], _(""));
1858
1859                 /* propagate font style,but smaller, into cells */
1860                 boost::shared_ptr<CairoFontDescription> smaller_font (new CairoFontDescription (*display->font().get()));
1861                 smaller_font->set_size (12);
1862                 supplemental_right->set_font (smaller_font);
1863                 supplemental_left->set_font (smaller_font);
1864         }
1865
1866         if (_mode != Off) {
1867                 connect_signals ();
1868         } else {
1869                 disconnect_signals  ();
1870         }
1871
1872         set (last_when, true);
1873
1874         if (!is_transient) {
1875                 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1876         }
1877
1878         mode_changed (); /* EMIT SIGNAL (the member one) */
1879 }
1880
1881 void
1882 AudioClock::set_bbt_reference (framepos_t pos)
1883 {
1884         bbt_reference_time = pos;
1885 }
1886
1887 void
1888 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1889 {
1890         VBox::on_style_changed (old_style);
1891         set_theme ();
1892 }
1893
1894 void
1895 AudioClock::set_is_duration (bool yn)
1896 {
1897         if (yn == is_duration) {
1898                 return;
1899         }
1900
1901         is_duration = yn;
1902         set (last_when, true, 0, 's');
1903 }
1904