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