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