BBT duration clock can only compare musical times.
[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 #include <sigc++/bind.h>
28
29 #include "gtkmm2ext/cairocell.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
32
33 #include "ardour/profile.h"
34 #include "ardour/lmath.h"
35 #include "ardour/session.h"
36 #include "ardour/slave.h"
37 #include "ardour/tempo.h"
38 #include "ardour/types.h"
39
40 #include "ardour_ui.h"
41 #include "audio_clock.h"
42 #include "gui_thread.h"
43 #include "keyboard.h"
44 #include "ui_config.h"
45 #include "utils.h"
46
47 #include "pbd/i18n.h"
48
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
51 using namespace PBD;
52 using namespace Gtk;
53 using namespace std;
54
55 using Gtkmm2ext::Keyboard;
56
57 sigc::signal<void> AudioClock::ModeChanged;
58 vector<AudioClock*> AudioClock::clocks;
59 const double AudioClock::info_font_scale_factor = 0.68;
60 const double AudioClock::separator_height = 0.0;
61 const double AudioClock::x_leading_padding = 6.0;
62
63 #define BBT_BAR_CHAR "|"
64 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
65 #define INFO_FONT_SIZE ((int)lrint(font_size * info_font_scale_factor))
66 #define TXTSPAN "<span font-family=\"Sans\" foreground=\"white\">"
67
68 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
69                         bool allow_edit, bool follows_playhead, bool duration, bool with_info,
70                         bool accept_on_focus_out)
71         : ops_menu (0)
72         , _name (clock_name)
73         , is_transient (transient)
74         , is_duration (duration)
75         , editable (allow_edit)
76         , _follows_playhead (follows_playhead)
77         , _accept_on_focus_out (accept_on_focus_out)
78         , _off (false)
79         , em_width (0)
80         , _edit_by_click_field (false)
81         , _negative_allowed (false)
82         , edit_is_negative (false)
83         , editing_attr (0)
84         , foreground_attr (0)
85         , first_height (0)
86         , first_width (0)
87         , style_resets_first (true)
88         , layout_height (0)
89         , layout_width (0)
90         , info_height (0)
91         , upper_height (0)
92         , mode_based_info_ratio (1.0)
93         , corner_radius (4)
94         , font_size (10240)
95         , editing (false)
96         , bbt_reference_time (-1)
97         , last_when(0)
98         , last_pdelta (0)
99         , last_sdelta (0)
100         , dragging (false)
101         , drag_field (Field (0))
102         , xscale (1.0)
103         , yscale (1.0)
104 {
105         set_flags (CAN_FOCUS);
106
107         _layout = Pango::Layout::create (get_pango_context());
108         _layout->set_attributes (normal_attributes);
109
110         if (with_info) {
111                 _left_layout = Pango::Layout::create (get_pango_context());
112                 _right_layout = Pango::Layout::create (get_pango_context());
113         }
114
115         set_widget_name (widget_name);
116
117         _mode = BBT; /* lie to force mode switch */
118         set_mode (Timecode);
119         set (last_when, true);
120
121         if (!is_transient) {
122                 clocks.push_back (this);
123         }
124
125         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
126         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
127 }
128
129 AudioClock::~AudioClock ()
130 {
131         delete foreground_attr;
132         delete editing_attr;
133 }
134
135 void
136 AudioClock::set_widget_name (const string& str)
137 {
138         if (str.empty()) {
139                 set_name ("clock");
140         } else {
141                 set_name (str + " clock");
142         }
143
144         if (is_realized()) {
145                 set_colors ();
146         }
147 }
148
149
150 void
151 AudioClock::on_realize ()
152 {
153         Gtk::Requisition req;
154
155         CairoWidget::on_realize ();
156
157         set_clock_dimensions (req);
158
159         first_width = req.width;
160         first_height = req.height;
161
162         // XXX FIX ME: define font based on ... ???
163         // set_font ();
164         set_colors ();
165 }
166
167 void
168 AudioClock::set_font (Pango::FontDescription font)
169 {
170         Glib::RefPtr<Gtk::Style> style = get_style ();
171         Pango::AttrFontDesc* font_attr;
172
173         font_size = font.get_size();
174         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
175
176         normal_attributes.change (*font_attr);
177         editing_attributes.change (*font_attr);
178
179         /* now a smaller version of the same font */
180
181         delete font_attr;
182         font.set_size (INFO_FONT_SIZE);
183         font.set_weight (Pango::WEIGHT_NORMAL);
184         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
185
186         info_attributes.change (*font_attr);
187
188         /* and an even smaller one */
189
190         delete font_attr;
191
192         /* get the figure width for the font. This doesn't have to super
193          * accurate since we only use it to measure the (roughly 1 character)
194          * offset from the position Pango tells us for the "cursor"
195          */
196
197         Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
198         int ignore_height;
199
200         tmp->set_text ("8");
201         tmp->get_pixel_size (em_width, ignore_height);
202
203         /* force redraw of markup with new font-size */
204         set (last_when, true);
205 }
206
207 void
208 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
209 {
210         CairoWidget::set_active_state (s);
211         set_colors ();
212 }
213
214 void
215 AudioClock::set_colors ()
216 {
217         int r, g, b, a;
218
219         uint32_t bg_color;
220         uint32_t text_color;
221         uint32_t editing_color;
222         uint32_t cursor_color;
223
224         if (active_state()) {
225                 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
226                 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
227                 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
228                 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
229         } else {
230                 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
231                 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
232                 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
233                 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
234         }
235
236         /* store for bg and cursor in render() */
237
238         UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
239
240         bg_r = r/255.0;
241         bg_g = g/255.0;
242         bg_b = b/255.0;
243         bg_a = a/255.0;
244
245         UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
246
247         cursor_r = r/255.0;
248         cursor_g = g/255.0;
249         cursor_b = b/255.0;
250         cursor_a = a/255.0;
251
252         /* rescale for Pango colors ... sigh */
253
254         r = lrint (r * 65535.0);
255         g = lrint (g * 65535.0);
256         b = lrint (b * 65535.0);
257
258         UINT_TO_RGBA (text_color, &r, &g, &b, &a);
259         r = lrint ((r/255.0) * 65535.0);
260         g = lrint ((g/255.0) * 65535.0);
261         b = lrint ((b/255.0) * 65535.0);
262         delete foreground_attr;
263         foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
264
265         UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
266         r = lrint ((r/255.0) * 65535.0);
267         g = lrint ((g/255.0) * 65535.0);
268         b = lrint ((b/255.0) * 65535.0);
269         delete editing_attr;
270         editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
271
272         normal_attributes.change (*foreground_attr);
273         info_attributes.change (*foreground_attr);
274         editing_attributes.change (*foreground_attr);
275         editing_attributes.change (*editing_attr);
276
277         if (!editing) {
278                 _layout->set_attributes (normal_attributes);
279         } else {
280                 _layout->set_attributes (editing_attributes);
281         }
282
283         queue_draw ();
284 }
285
286 void
287 AudioClock::set_scale (double x, double y)
288 {
289         xscale = x;
290         yscale = y;
291
292         queue_draw ();
293 }
294
295 void
296 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
297 {
298         /* main layout: rounded rect, plus the text */
299
300         if (_need_bg) {
301                 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
302                 if (corner_radius) {
303                         if (_left_layout) {
304                                 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
305                         } else {
306                                 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
307                         }
308                 } else {
309                         cairo_rectangle (cr, 0, 0, get_width(), upper_height);
310                 }
311                 cairo_fill (cr);
312         }
313
314         double lw = layout_width * xscale;
315         double lh = layout_height * yscale;
316
317         cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
318
319         if (xscale != 1.0 || yscale != 1.0) {
320                 cairo_save (cr);
321                 cairo_scale (cr, xscale, yscale);
322         }
323
324         pango_cairo_show_layout (cr, _layout->gobj());
325
326         if (xscale != 1.0 || yscale != 1.0) {
327                 cairo_restore (cr);
328         }
329
330         if (_left_layout) {
331
332                 double h = get_height() - upper_height - separator_height;
333
334                 if (_need_bg) {
335                         cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
336                 }
337
338                 if (mode_based_info_ratio != 1.0) {
339
340                         double left_rect_width = get_left_rect_width();
341
342                         if (_need_bg) {
343                                 if (corner_radius) {
344                                         Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
345                                                         left_rect_width + (separator_height == 0 ? corner_radius : 0),
346                                                         h, corner_radius);
347                                 } else {
348                                         cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
349                                 }
350                                 cairo_fill (cr);
351                         }
352
353                         cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
354                         pango_cairo_show_layout (cr, _left_layout->gobj());
355
356                         if (_need_bg) {
357                                 if (corner_radius) {
358                                         Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
359                                                         upper_height + separator_height,
360                                                         get_width() - separator_height - left_rect_width,
361                                                         h, corner_radius);
362                                 } else {
363                                         cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
364                                                          get_width() - separator_height - left_rect_width, h);
365                                 }
366                                 cairo_fill (cr);
367                         }
368
369
370                         if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
371                                 /* right-align does not work per se beacuse layout width is unset.
372                                  * Using _right_layout->set_width([value >=0]) would also enable
373                                  * word-wrapping which is not wanted here.
374                                  * The solution is to custom align the layout depending on its size.
375                                  * if it is larger than the available space it will be cropped on the
376                                  * right edge rather than override text on the left side.
377                                  */
378                                 int x, rw, rh;
379                                 _right_layout->get_pixel_size(rw, rh);
380                                 x = get_width() - rw - separator_height - x_leading_padding;
381                                 if (x < x_leading_padding + left_rect_width + separator_height) {
382                                         /* rather cut off the right end than overlap with the text on the left */
383                                         x = x_leading_padding + left_rect_width + separator_height;
384                                 }
385                                 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
386                         } else {
387                                 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
388                         }
389                         pango_cairo_show_layout (cr, _right_layout->gobj());
390
391                 } else {
392                         /* no info to display, or just one */
393
394                         if (_need_bg) {
395                                 if (corner_radius) {
396                                         Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
397                                 } else {
398                                         cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
399                                 }
400                                 cairo_fill (cr);
401                         }
402                 }
403         }
404
405         if (editing) {
406                 if (!insert_map.empty()) {
407
408                         int xcenter = (get_width() - layout_width) /2;
409
410                         if (input_string.length() < insert_map.size()) {
411                                 Pango::Rectangle cursor;
412
413                                 if (input_string.empty()) {
414                                         /* nothing entered yet, put cursor at the end
415                                            of string
416                                         */
417                                         cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
418                                 } else {
419                                         cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
420                                 }
421
422                                 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
423                                 cairo_rectangle (cr,
424                                                  min (get_width() - 2.0,
425                                                       (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
426                                                  (upper_height - layout_height)/2.0,
427                                                  2.0, cursor.get_height()/PANGO_SCALE);
428                                 cairo_fill (cr);
429                         } else {
430                                 /* we've entered all possible digits, no cursor */
431                         }
432
433                 } else {
434                         if (input_string.empty()) {
435                                 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
436                                 cairo_rectangle (cr,
437                                                  (get_width()/2.0),
438                                                  (upper_height - layout_height)/2.0,
439                                                  2.0, upper_height);
440                                 cairo_fill (cr);
441                         }
442                 }
443         }
444 }
445
446 void
447 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
448 {
449         CairoWidget::on_size_allocate (alloc);
450
451         if (_left_layout) {
452                 upper_height = (get_height()/2.0) - 1.0;
453         } else {
454                 upper_height = get_height();
455         }
456 }
457
458 void
459 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
460 {
461         Glib::RefPtr<Pango::Layout> tmp;
462         Glib::RefPtr<Gtk::Style> style = get_style ();
463         Pango::FontDescription font;
464
465         tmp = Pango::Layout::create (get_pango_context());
466
467         if (!is_realized()) {
468                 font = get_font_for_style (get_name());
469         } else {
470                 font = style->get_font();
471         }
472
473         tmp->set_font_description (font);
474
475         /* this string is the longest thing we will ever display */
476         if (_mode == MinSec)
477                 tmp->set_text (" 88:88:88,888 ");
478         else
479                 tmp->set_text (" 88:88:88,88 ");
480         tmp->get_pixel_size (req.width, req.height);
481
482         layout_height = req.height;
483         layout_width = req.width;
484 }
485
486 void
487 AudioClock::on_size_request (Gtk::Requisition* req)
488 {
489         /* even for non fixed width clocks, the size we *ask* for never changes,
490            even though the size we receive might. so once we've computed it,
491            just return it.
492         */
493
494         if (first_width) {
495                 req->width = first_width;
496                 req->height = first_height;
497                 return;
498         }
499
500         set_clock_dimensions (*req);
501
502         /* now tackle height, for which we need to know the height of the lower
503          * layout
504          */
505
506         if (_left_layout) {
507
508                 Glib::RefPtr<Pango::Layout> tmp;
509                 Glib::RefPtr<Gtk::Style> style = get_style ();
510                 Pango::FontDescription font;
511                 int w;
512
513                 tmp = Pango::Layout::create (get_pango_context());
514
515                 if (!is_realized()) {
516                         font = get_font_for_style (get_name());
517                 } else {
518                         font = style->get_font();
519                 }
520
521                 tmp->set_font_description (font);
522
523                 font.set_size (INFO_FONT_SIZE);
524                 font.set_weight (Pango::WEIGHT_NORMAL);
525                 tmp->set_font_description (font);
526
527                 /* we only care about height, so put as much stuff in here
528                    as possible that might change the height.
529                 */
530                 tmp->set_text ("qyhH|"); /* one ascender, one descender */
531
532                 tmp->get_pixel_size (w, info_height);
533
534                 /* silly extra padding that seems necessary to correct the info
535                  * that pango just gave us. I have no idea why.
536                  */
537
538                 req->height += info_height;
539                 req->height += separator_height;
540         }
541 }
542
543 void
544 AudioClock::show_edit_status (int length)
545 {
546         editing_attr->set_start_index (edit_string.length() - length);
547         editing_attr->set_end_index (edit_string.length());
548
549         editing_attributes.change (*foreground_attr);
550         editing_attributes.change (*editing_attr);
551
552         _layout->set_attributes (editing_attributes);
553 }
554
555 void
556 AudioClock::start_edit (Field f)
557 {
558         if (!editing) {
559                 pre_edit_string = _layout->get_text ();
560                 if (!insert_map.empty()) {
561                         edit_string = pre_edit_string;
562                 } else {
563                         edit_string.clear ();
564                         _layout->set_text ("");
565                 }
566
567                 input_string.clear ();
568                 editing = true;
569                 edit_is_negative = false;
570
571                 if (f) {
572                         input_string = get_field (f);
573                         show_edit_status (merge_input_and_edit_string ());
574                         _layout->set_text (edit_string);
575                 }
576
577                 queue_draw ();
578
579                 Keyboard::magic_widget_grab_focus ();
580                 grab_focus ();
581         }
582 }
583
584 string
585 AudioClock::get_field (Field f)
586 {
587         switch (f) {
588         case Timecode_Hours:
589                 return edit_string.substr (1, 2);
590                 break;
591         case Timecode_Minutes:
592                 return edit_string.substr (4, 2);
593                 break;
594         case Timecode_Seconds:
595                 return edit_string.substr (7, 2);
596                 break;
597         case Timecode_Frames:
598                 return edit_string.substr (10, 2);
599                 break;
600         case MS_Hours:
601                 return edit_string.substr (1, 2);
602                 break;
603         case MS_Minutes:
604                 return edit_string.substr (4, 2);
605                 break;
606         case MS_Seconds:
607                 return edit_string.substr (7, 2);
608                 break;
609         case MS_Milliseconds:
610                 return edit_string.substr (10, 3);
611                 break;
612         case Bars:
613                 return edit_string.substr (1, 3);
614                 break;
615         case Beats:
616                 return edit_string.substr (5, 2);
617                 break;
618         case Ticks:
619                 return edit_string.substr (8, 4);
620                 break;
621         case AudioFrames:
622                 return edit_string;
623                 break;
624         }
625         return "";
626 }
627
628 void
629 AudioClock::end_edit (bool modify)
630 {
631         if (modify) {
632
633                 bool ok = true;
634
635                 switch (_mode) {
636                 case Timecode:
637                         ok = timecode_validate_edit (edit_string);
638                         break;
639
640                 case BBT:
641                         ok = bbt_validate_edit (edit_string);
642                         break;
643
644                 case MinSec:
645                         ok = minsec_validate_edit (edit_string);
646                         break;
647
648                 case Frames:
649                         if (edit_string.length() < 1) {
650                                 edit_string = pre_edit_string;
651                         }
652                         break;
653                 }
654
655                 if (!ok) {
656                         edit_string = pre_edit_string;
657                         input_string.clear ();
658                         _layout->set_text (edit_string);
659                         show_edit_status (0);
660                         /* edit attributes remain in use */
661                 } else {
662
663                         editing = false;
664                         framepos_t pos = 0; /* stupid gcc */
665
666                         switch (_mode) {
667                         case Timecode:
668                                 pos = frames_from_timecode_string (edit_string);
669                                 break;
670
671                         case BBT:
672                                 if (is_duration) {
673                                         pos = frame_duration_from_bbt_string (0, edit_string);
674                                 } else {
675                                         pos = frames_from_bbt_string (0, edit_string);
676                                 }
677                                 break;
678
679                         case MinSec:
680                                 pos = frames_from_minsec_string (edit_string);
681                                 break;
682
683                         case Frames:
684                                 pos = frames_from_audioframes_string (edit_string);
685                                 break;
686                         }
687
688                         set (pos, true);
689                         _layout->set_attributes (normal_attributes);
690                         ValueChanged(); /* EMIT_SIGNAL */
691                 }
692
693         } else {
694
695                 editing = false;
696                 edit_is_negative = false;
697                 _layout->set_attributes (normal_attributes);
698                 _layout->set_text (pre_edit_string);
699         }
700
701         queue_draw ();
702
703         if (!editing) {
704                 drop_focus ();
705         }
706 }
707
708 void
709 AudioClock::drop_focus ()
710 {
711         Keyboard::magic_widget_drop_focus ();
712
713         if (has_focus()) {
714                 /* move focus back to the default widget in the top level window */
715                 ARDOUR_UI::instance()->reset_focus (this);
716         }
717 }
718
719 framecnt_t
720 AudioClock::parse_as_frames_distance (const std::string& str)
721 {
722         framecnt_t f;
723
724         if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
725                 return f;
726         }
727
728         return 0;
729 }
730
731 framecnt_t
732 AudioClock::parse_as_minsec_distance (const std::string& str)
733 {
734         framecnt_t sr = _session->frame_rate();
735         int msecs;
736         int secs;
737         int mins;
738         int hrs;
739
740         switch (str.length()) {
741         case 0:
742                 return 0;
743         case 1:
744         case 2:
745         case 3:
746         case 4:
747                 sscanf (str.c_str(), "%" PRId32, &msecs);
748                 return msecs * (sr / 1000);
749
750         case 5:
751                 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
752                 return (secs * sr) + (msecs * (sr/1000));
753
754         case 6:
755                 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
756                 return (secs * sr) + (msecs * (sr/1000));
757
758         case 7:
759                 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
760                 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
761
762         case 8:
763                 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
764                 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
765
766         case 9:
767                 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
768                 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
769
770         case 10:
771                 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
772                 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
773
774         default:
775                 break;
776         }
777
778         return 0;
779 }
780
781 framecnt_t
782 AudioClock::parse_as_timecode_distance (const std::string& str)
783 {
784         double fps = _session->timecode_frames_per_second();
785         framecnt_t sr = _session->frame_rate();
786         int frames;
787         int secs;
788         int mins;
789         int hrs;
790
791         switch (str.length()) {
792         case 0:
793                 return 0;
794         case 1:
795         case 2:
796                 sscanf (str.c_str(), "%" PRId32, &frames);
797                 return llrint ((frames/(float)fps) * sr);
798
799         case 3:
800                 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
801                 return (secs * sr) + llrint ((frames/(float)fps) * sr);
802
803         case 4:
804                 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
805                 return (secs * sr) + llrint ((frames/(float)fps) * sr);
806
807         case 5:
808                 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
809                 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
810
811         case 6:
812                 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
813                 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
814
815         case 7:
816                 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
817                 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
818
819         case 8:
820                 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
821                 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
822
823         default:
824                 break;
825         }
826
827         return 0;
828 }
829
830 framecnt_t
831 AudioClock::parse_as_bbt_distance (const std::string&)
832 {
833         return 0;
834 }
835
836 framecnt_t
837 AudioClock::parse_as_distance (const std::string& instr)
838 {
839         switch (_mode) {
840         case Timecode:
841                 return parse_as_timecode_distance (instr);
842                 break;
843         case Frames:
844                 return parse_as_frames_distance (instr);
845                 break;
846         case BBT:
847                 return parse_as_bbt_distance (instr);
848                 break;
849         case MinSec:
850                 return parse_as_minsec_distance (instr);
851                 break;
852         }
853         return 0;
854 }
855
856 void
857 AudioClock::end_edit_relative (bool add)
858 {
859         bool ok = true;
860
861         switch (_mode) {
862         case Timecode:
863                 ok = timecode_validate_edit (edit_string);
864                 break;
865
866         case BBT:
867                 ok = bbt_validate_edit (edit_string);
868                 break;
869
870         case MinSec:
871                 ok = minsec_validate_edit (edit_string);
872                 break;
873
874         case Frames:
875                 break;
876         }
877
878         if (!ok) {
879                 edit_string = pre_edit_string;
880                 input_string.clear ();
881                 _layout->set_text (edit_string);
882                 show_edit_status (0);
883                 /* edit attributes remain in use */
884                 queue_draw ();
885                 return;
886         }
887
888         framecnt_t frames = parse_as_distance (input_string);
889
890         editing = false;
891
892         editing = false;
893         _layout->set_attributes (normal_attributes);
894
895         if (frames != 0) {
896                 if (add) {
897                         set (current_time() + frames, true);
898                 } else {
899                         framepos_t c = current_time();
900
901                         if (c > frames || _negative_allowed) {
902                                 set (c - frames, true);
903                         } else {
904                                 set (0, true);
905                         }
906                 }
907                 ValueChanged (); /* EMIT SIGNAL */
908         }
909
910         input_string.clear ();
911         queue_draw ();
912         drop_focus ();
913 }
914
915 void
916 AudioClock::session_property_changed (const PropertyChange&)
917 {
918         set (last_when, true);
919 }
920
921 void
922 AudioClock::metric_position_changed ()
923 {
924         set (last_when, true);
925 }
926
927 void
928 AudioClock::session_configuration_changed (std::string p)
929 {
930         if (_negative_allowed) {
931                 /* session option editor clock */
932                 return;
933         }
934
935         if (p == "sync-source" || p == "external-sync") {
936                 set (current_time(), true);
937                 return;
938         }
939
940         if (p != "timecode-offset" && p != "timecode-offset-negative") {
941                 return;
942         }
943
944         framecnt_t current;
945
946         switch (_mode) {
947         case Timecode:
948                 if (is_duration) {
949                         current = current_duration ();
950                 } else {
951                         current = current_time ();
952                 }
953                 set (current, true);
954                 break;
955         default:
956                 break;
957         }
958 }
959
960 void
961 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
962 {
963         if ((!force && !is_visible()) || _session == 0) {
964                 return;
965         }
966
967         if (is_duration) {
968                 when = when - offset;
969         }
970
971         if (when == last_when && !force) {
972 #if 0 // XXX return if no change and no change forced. verify Aug/2014
973                 if (_mode != Timecode && _mode != MinSec) {
974                         /* may need to force display of TC source
975                          * time, so don't return early.
976                          */
977                         /* ^^ Why was that?,  delta times?
978                          * Timecode FPS, pull-up/down, etc changes
979                          * trigger a 'session_property_changed' which
980                          * eventually calls set(last_when, true)
981                          *
982                          * re-rendering the clock every 40ms or so just
983                          * because we can is not ideal.
984                          */
985                         return;
986                 }
987 #else
988                 return;
989 #endif
990         }
991
992         if (!editing) {
993                 if (_right_layout) {
994                         _right_layout->set_alignment(Pango::ALIGN_LEFT);
995                 }
996
997                 switch (_mode) {
998                 case Timecode:
999                         if (_right_layout) {
1000                                 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1001                         }
1002                         set_timecode (when, force);
1003                         break;
1004
1005                 case BBT:
1006                         set_bbt (when, offset, force);
1007                         break;
1008
1009                 case MinSec:
1010                         if (_right_layout) {
1011                                 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1012                         }
1013                         set_minsec (when, force);
1014                         break;
1015
1016                 case Frames:
1017                         set_frames (when, force);
1018                         break;
1019                 }
1020         }
1021
1022         queue_draw ();
1023         last_when = when;
1024 }
1025
1026 void
1027 AudioClock::set_slave_info ()
1028 {
1029         if (!_left_layout || !_right_layout) {
1030                 return;
1031         }
1032
1033         SyncSource sync_src = Config->get_sync_source();
1034
1035         if (_session->config.get_external_sync()) {
1036                 Slave* slave = _session->slave();
1037
1038                 switch (sync_src) {
1039                 case Engine:
1040                         _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1041                                                 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1042                         _right_layout->set_text ("");
1043                         break;
1044                 case MIDIClock:
1045                         if (slave) {
1046                                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1047                                                         INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1048                                 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1049                                                         INFO_FONT_SIZE, slave->approximate_current_delta()));
1050                         } else {
1051                                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1052                                                         INFO_FONT_SIZE, _("--pending--")));
1053                                 _right_layout->set_text ("");
1054                         }
1055                         break;
1056                 case LTC:
1057                 case MTC:
1058                         if (slave) {
1059                                 bool matching;
1060                                 TimecodeSlave* tcslave;
1061                                 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1062                                         matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1063                                         _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1064                                                                                   INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1065                                                                                   dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1066                                         _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1067                                                                                    INFO_FONT_SIZE, slave->approximate_current_delta()));
1068                                 }
1069                         } else {
1070                                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1071                                                         INFO_FONT_SIZE, _("--pending--")));
1072                                 _right_layout->set_text ("");
1073                         }
1074                         break;
1075                 }
1076         } else {
1077                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1078                                         INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1079                 _right_layout->set_text ("");
1080         }
1081 }
1082
1083 void
1084 AudioClock::set_frames (framepos_t when, bool /*force*/)
1085 {
1086         char buf[32];
1087         bool negative = false;
1088
1089         if (_off) {
1090                 _layout->set_text (" ----------");
1091
1092                 if (_left_layout) {
1093                         _left_layout->set_text ("");
1094                         _right_layout->set_text ("");
1095                 }
1096
1097                 return;
1098         }
1099
1100         if (when < 0) {
1101                 when = -when;
1102                 negative = true;
1103         }
1104
1105         if (negative) {
1106                 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1107         } else {
1108                 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1109         }
1110
1111         _layout->set_text (buf);
1112
1113         if (_left_layout) {
1114                 framecnt_t rate = _session->frame_rate();
1115
1116                 if (fmod (rate, 100.0) == 0.0) {
1117                         sprintf (buf, "%.1fkHz", rate/1000.0);
1118                 } else {
1119                         sprintf (buf, "%" PRId64 "Hz", rate);
1120                 }
1121
1122                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1123                                 INFO_FONT_SIZE, _("SR"), buf));
1124
1125                 float vid_pullup = _session->config.get_video_pullup();
1126
1127                 if (vid_pullup == 0.0) {
1128                         _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1129                                         INFO_FONT_SIZE, _("Pull")));
1130                 } else {
1131                         sprintf (buf, _("%+.4f%%"), vid_pullup);
1132                         _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1133                                         INFO_FONT_SIZE, _("Pull"), buf));
1134                 }
1135         }
1136 }
1137
1138 void
1139 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1140 {
1141         framecnt_t left;
1142         int hrs;
1143         int mins;
1144         int secs;
1145         int millisecs;
1146         bool negative;
1147
1148         if (when < 0) {
1149                 when = -when;
1150                 negative = true;
1151         } else {
1152                 negative = false;
1153         }
1154
1155         left = when;
1156         hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1157         left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1158         mins = (int) floor (left / (frame_rate * 60.0f));
1159         left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1160         secs = (int) floor (left / (float) frame_rate);
1161         left -= (framecnt_t) floor ((double)(secs * frame_rate));
1162         millisecs = floor (left * 1000.0 / (float) frame_rate);
1163
1164         if (negative) {
1165                 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1166         } else {
1167                 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1168         }
1169
1170 }
1171
1172 void
1173 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1174 {
1175         char buf[32];
1176
1177         if (_off) {
1178                 _layout->set_text (" --:--:--.---");
1179
1180                 if (_left_layout) {
1181                         _left_layout->set_text ("");
1182                         _right_layout->set_text ("");
1183                 }
1184
1185                 return;
1186         }
1187
1188         print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1189
1190         _layout->set_text (buf);
1191         set_slave_info();
1192 }
1193
1194 void
1195 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1196 {
1197         Timecode::Time TC;
1198         bool negative = false;
1199
1200         if (_off) {
1201                 _layout->set_text (" --:--:--:--");
1202                 if (_left_layout) {
1203                         _left_layout->set_text ("");
1204                         _right_layout->set_text ("");
1205                 }
1206
1207                 return;
1208         }
1209
1210         if (when < 0) {
1211                 when = -when;
1212                 negative = true;
1213         }
1214
1215         if (is_duration) {
1216                 _session->timecode_duration (when, TC);
1217         } else {
1218                 _session->timecode_time (when, TC);
1219         }
1220
1221         TC.negative = TC.negative || negative;
1222
1223         _layout->set_text (Timecode::timecode_format_time(TC));
1224
1225         set_slave_info();
1226 }
1227
1228 void
1229 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1230 {
1231         char buf[16];
1232         Timecode::BBT_Time BBT;
1233         bool negative = false;
1234
1235         if (_off) {
1236                 _layout->set_text (" ---|--|----");
1237                 if (_left_layout) {
1238                         _left_layout->set_text ("");
1239                         _right_layout->set_text ("");
1240                 }
1241                 return;
1242         }
1243
1244         if (when < 0) {
1245                 when = -when;
1246                 negative = true;
1247         }
1248
1249         /* handle a common case */
1250         if (is_duration) {
1251                 if (when == 0) {
1252                         BBT.bars = 0;
1253                         BBT.beats = 0;
1254                         BBT.ticks = 0;
1255                 } else {
1256                         TempoMap& tmap (_session->tempo_map());
1257
1258                         /* if offset is before beat 0, it is meaningless */
1259                         offset = max (offset, tmap.frame_at_beat (0.0));
1260
1261                         const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1262                         Timecode::BBT_Time sub_bbt;
1263
1264                         if (negative) {
1265                                 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1266                                 sub_bbt = tmap.bbt_at_frame (offset - when);
1267                         } else {
1268                                 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1269                                 sub_bbt = tmap.bbt_at_frame (offset);
1270                         }
1271
1272                         BBT.bars -= sub_bbt.bars;
1273
1274                         if (BBT.ticks < sub_bbt.ticks) {
1275                                 if (BBT.beats == 1) {
1276                                         BBT.bars--;
1277                                         BBT.beats = divisions;
1278                                 } else {
1279                                         BBT.beats--;
1280                                 }
1281                                 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1282                         } else {
1283                                 BBT.ticks -= sub_bbt.ticks;
1284                         }
1285
1286                         if (BBT.beats < sub_bbt.beats) {
1287                                 BBT.bars--;
1288                                 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1289                         } else {
1290                                 BBT.beats -= sub_bbt.beats;
1291                         }
1292                 }
1293         } else {
1294                 BBT = _session->tempo_map().bbt_at_frame (when);
1295         }
1296
1297         if (negative) {
1298                 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1299                           BBT.bars, BBT.beats, BBT.ticks);
1300         } else {
1301                 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1302                           BBT.bars, BBT.beats, BBT.ticks);
1303         }
1304
1305         _layout->set_text (buf);
1306
1307         if (_right_layout) {
1308                 framepos_t pos;
1309
1310                 if (bbt_reference_time < 0) {
1311                         pos = when;
1312                 } else {
1313                         pos = bbt_reference_time;
1314                 }
1315
1316                 TempoMetric m (_session->tempo_map().metric_at (pos));
1317
1318                 sprintf (buf, "%-5.3f", _session->tempo_map().tempo_at_frame (pos).beats_per_minute());
1319                 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1320                                                           INFO_FONT_SIZE, buf, _("Tempo")));
1321
1322                 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1323                 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1324                                                            INFO_FONT_SIZE, buf, _("Meter")));
1325         }
1326 }
1327
1328 void
1329 AudioClock::set_session (Session *s)
1330 {
1331         SessionHandlePtr::set_session (s);
1332
1333         if (_session) {
1334
1335                 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1336                 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1337                 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::metric_position_changed, this), gui_context());
1338
1339                 XMLProperty const * prop;
1340                 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1341                 AudioClock::Mode amode;
1342
1343                 if (node) {
1344                         for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1345                                 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1346
1347                                         if ((prop = (*i)->property (X_("mode"))) != 0) {
1348                                                 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1349                                                 set_mode (amode, true);
1350                                         }
1351                                         if ((prop = (*i)->property (X_("on"))) != 0) {
1352                                                 set_off (!string_is_affirmative (prop->value()));
1353                                         }
1354                                         break;
1355                                 }
1356                         }
1357                 }
1358
1359                 set (last_when, true);
1360         }
1361 }
1362
1363 bool
1364 AudioClock::on_key_press_event (GdkEventKey* ev)
1365 {
1366         if (!editing) {
1367                 return false;
1368         }
1369
1370         string new_text;
1371         char new_char = 0;
1372         int highlight_length;
1373         framepos_t pos;
1374
1375         switch (ev->keyval) {
1376         case GDK_0:
1377         case GDK_KP_0:
1378                 new_char = '0';
1379                 break;
1380         case GDK_1:
1381         case GDK_KP_1:
1382                 new_char = '1';
1383                 break;
1384         case GDK_2:
1385         case GDK_KP_2:
1386                 new_char = '2';
1387                 break;
1388         case GDK_3:
1389         case GDK_KP_3:
1390                 new_char = '3';
1391                 break;
1392         case GDK_4:
1393         case GDK_KP_4:
1394                 new_char = '4';
1395                 break;
1396         case GDK_5:
1397         case GDK_KP_5:
1398                 new_char = '5';
1399                 break;
1400         case GDK_6:
1401         case GDK_KP_6:
1402                 new_char = '6';
1403                 break;
1404         case GDK_7:
1405         case GDK_KP_7:
1406                 new_char = '7';
1407                 break;
1408         case GDK_8:
1409         case GDK_KP_8:
1410                 new_char = '8';
1411                 break;
1412         case GDK_9:
1413         case GDK_KP_9:
1414                 new_char = '9';
1415                 break;
1416
1417         case GDK_minus:
1418         case GDK_KP_Subtract:
1419                 if (_negative_allowed && input_string.empty()) {
1420                                 edit_is_negative = true;
1421                                 edit_string.replace(0,1,"-");
1422                                 _layout->set_text (edit_string);
1423                                 queue_draw ();
1424                 } else {
1425                         end_edit_relative (false);
1426                 }
1427                 return true;
1428                 break;
1429
1430         case GDK_plus:
1431                 end_edit_relative (true);
1432                 return true;
1433                 break;
1434
1435         case GDK_Tab:
1436         case GDK_Return:
1437         case GDK_KP_Enter:
1438                 end_edit (true);
1439                 return true;
1440                 break;
1441
1442         case GDK_Escape:
1443                 end_edit (false);
1444                 ChangeAborted();  /*  EMIT SIGNAL  */
1445                 return true;
1446
1447         case GDK_Delete:
1448         case GDK_BackSpace:
1449                 if (!input_string.empty()) {
1450                         /* delete the last key entered
1451                         */
1452                         input_string = input_string.substr (0, input_string.length() - 1);
1453                 }
1454                 goto use_input_string;
1455
1456         default:
1457                 /* do not allow other keys to passthru to the rest of the GUI
1458                    when editing.
1459                 */
1460                 return true;
1461         }
1462
1463         if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1464                 /* too many digits: eat the key event, but do nothing with it */
1465                 return true;
1466         }
1467
1468         input_string.push_back (new_char);
1469
1470   use_input_string:
1471
1472         switch (_mode) {
1473         case Frames:
1474                 /* get this one in the right order, and to the right width */
1475                 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1476                         edit_string = edit_string.substr (0, edit_string.length() - 1);
1477                 } else {
1478                         edit_string.push_back (new_char);
1479                 }
1480                 if (!edit_string.empty()) {
1481                         char buf[32];
1482                         sscanf (edit_string.c_str(), "%" PRId64, &pos);
1483                         snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1484                         edit_string = buf;
1485                 }
1486                 /* highlight the whole thing */
1487                 highlight_length = edit_string.length();
1488                 break;
1489
1490         default:
1491                 highlight_length = merge_input_and_edit_string ();
1492         }
1493
1494         if (edit_is_negative) {
1495                 edit_string.replace(0,1,"-");
1496         } else {
1497                 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1498                         edit_string.replace(0,1,"_");
1499                 } else {
1500                         edit_string.replace(0,1," ");
1501                 }
1502         }
1503
1504         show_edit_status (highlight_length);
1505         _layout->set_text (edit_string);
1506         queue_draw ();
1507
1508         return true;
1509 }
1510
1511 int
1512 AudioClock::merge_input_and_edit_string ()
1513 {
1514         /* merge with pre-edit-string into edit string */
1515
1516         edit_string = pre_edit_string;
1517
1518         if (input_string.empty()) {
1519                 return 0;
1520         }
1521
1522         string::size_type target;
1523         for (string::size_type i = 0; i < input_string.length(); ++i) {
1524                 target = insert_map[input_string.length() - 1 - i];
1525                 edit_string[target] = input_string[i];
1526         }
1527         /* highlight from end to wherever the last character was added */
1528         return edit_string.length() - insert_map[input_string.length()-1];
1529 }
1530
1531
1532 bool
1533 AudioClock::on_key_release_event (GdkEventKey *ev)
1534 {
1535         if (!editing) {
1536                 return false;
1537         }
1538
1539         /* return true for keys that we used on press
1540            so that they cannot possibly do double-duty
1541         */
1542         switch (ev->keyval) {
1543         case GDK_0:
1544         case GDK_KP_0:
1545         case GDK_1:
1546         case GDK_KP_1:
1547         case GDK_2:
1548         case GDK_KP_2:
1549         case GDK_3:
1550         case GDK_KP_3:
1551         case GDK_4:
1552         case GDK_KP_4:
1553         case GDK_5:
1554         case GDK_KP_5:
1555         case GDK_6:
1556         case GDK_KP_6:
1557         case GDK_7:
1558         case GDK_KP_7:
1559         case GDK_8:
1560         case GDK_KP_8:
1561         case GDK_9:
1562         case GDK_KP_9:
1563         case GDK_period:
1564         case GDK_comma:
1565         case GDK_KP_Decimal:
1566         case GDK_Tab:
1567         case GDK_Return:
1568         case GDK_KP_Enter:
1569         case GDK_Escape:
1570         case GDK_minus:
1571         case GDK_plus:
1572         case GDK_KP_Add:
1573         case GDK_KP_Subtract:
1574                 return true;
1575         default:
1576                 return false;
1577         }
1578 }
1579
1580 AudioClock::Field
1581 AudioClock::index_to_field (int index) const
1582 {
1583         switch (_mode) {
1584         case Timecode:
1585                 if (index < 4) {
1586                         return Timecode_Hours;
1587                 } else if (index < 7) {
1588                         return Timecode_Minutes;
1589                 } else if (index < 10) {
1590                         return Timecode_Seconds;
1591                 } else {
1592                         return Timecode_Frames;
1593                 }
1594                 break;
1595         case BBT:
1596                 if (index < 5) {
1597                         return Bars;
1598                 } else if (index < 7) {
1599                         return Beats;
1600                 } else {
1601                         return Ticks;
1602                 }
1603                 break;
1604         case MinSec:
1605                 if (index < 3) {
1606                         return Timecode_Hours;
1607                 } else if (index < 6) {
1608                         return MS_Minutes;
1609                 } else if (index < 9) {
1610                         return MS_Seconds;
1611                 } else {
1612                         return MS_Milliseconds;
1613                 }
1614                 break;
1615         case Frames:
1616                 return AudioFrames;
1617                 break;
1618         }
1619
1620         return Field (0);
1621 }
1622
1623 bool
1624 AudioClock::on_button_press_event (GdkEventButton *ev)
1625 {
1626         switch (ev->button) {
1627         case 1:
1628                 if (editable && !_off) {
1629                         int index;
1630                         int trailing;
1631                         int y;
1632                         int x;
1633
1634                         /* the text has been centered vertically, so adjust
1635                          * x and y.
1636                          */
1637                         int xcenter = (get_width() - layout_width) /2;
1638
1639                         y = ev->y - ((upper_height - layout_height)/2);
1640                         x = ev->x - xcenter;
1641
1642                         if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1643                                 /* pretend it is a character on the far right */
1644                                 index = 99;
1645                         }
1646                         drag_field = index_to_field (index);
1647                         dragging = true;
1648                         /* make absolutely sure that the pointer is grabbed */
1649                         gdk_pointer_grab(ev->window,false ,
1650                                          GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1651                                          NULL,NULL,ev->time);
1652                         drag_accum = 0;
1653                         drag_start_y = ev->y;
1654                         drag_y = ev->y;
1655                 }
1656                 break;
1657
1658         default:
1659                 return false;
1660                 break;
1661         }
1662
1663         return true;
1664 }
1665
1666 bool
1667 AudioClock::on_button_release_event (GdkEventButton *ev)
1668 {
1669         if (editable && !_off) {
1670                 if (dragging) {
1671                         gdk_pointer_ungrab (GDK_CURRENT_TIME);
1672                         dragging = false;
1673                         if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1674                                 // we actually dragged so return without
1675                                 // setting editing focus, or we shift clicked
1676                                 return true;
1677                         } else {
1678                                 if (ev->button == 1) {
1679
1680                                         if (_edit_by_click_field) {
1681
1682                                                 int xcenter = (get_width() - layout_width) /2;
1683                                                 int index = 0;
1684                                                 int trailing;
1685                                                 int y = ev->y - ((upper_height - layout_height)/2);
1686                                                 int x = ev->x - xcenter;
1687                                                 Field f;
1688
1689                                                 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1690                                                         return true;
1691                                                 }
1692
1693                                                 f = index_to_field (index);
1694
1695                                                 switch (f) {
1696                                                 case Timecode_Frames:
1697                                                 case MS_Milliseconds:
1698                                                 case Ticks:
1699                                                         f = Field (0);
1700                                                         break;
1701                                                 default:
1702                                                         break;
1703                                                 }
1704                                                 start_edit (f);
1705                                         } else {
1706                                                 start_edit ();
1707                                         }
1708                                 }
1709                         }
1710                 }
1711         }
1712
1713         if (Keyboard::is_context_menu_event (ev)) {
1714                 if (ops_menu == 0) {
1715                         build_ops_menu ();
1716                 }
1717                 ops_menu->popup (1, ev->time);
1718                 return true;
1719         }
1720
1721         return false;
1722 }
1723
1724 bool
1725 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1726 {
1727         bool ret = CairoWidget::on_focus_out_event (ev);
1728
1729         if (editing) {
1730                 end_edit (_accept_on_focus_out);
1731         }
1732
1733         return ret;
1734 }
1735
1736 bool
1737 AudioClock::on_scroll_event (GdkEventScroll *ev)
1738 {
1739         int index;
1740         int trailing;
1741
1742         if (editing || _session == 0 || !editable || _off) {
1743                 return false;
1744         }
1745
1746         int y;
1747         int x;
1748
1749         /* the text has been centered vertically, so adjust
1750          * x and y.
1751          */
1752
1753         int xcenter = (get_width() - layout_width) /2;
1754         y = ev->y - ((upper_height - layout_height)/2);
1755         x = ev->x - xcenter;
1756
1757         if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1758                 /* not in the main layout */
1759                 return false;
1760         }
1761
1762         Field f = index_to_field (index);
1763         framepos_t frames = 0;
1764
1765         switch (ev->direction) {
1766
1767         case GDK_SCROLL_UP:
1768                 frames = get_frame_step (f, current_time(), 1);
1769                 if (frames != 0) {
1770                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1771                                 frames *= 10;
1772                         }
1773                         set (current_time() + frames, true);
1774                         ValueChanged (); /* EMIT_SIGNAL */
1775                 }
1776                 break;
1777
1778         case GDK_SCROLL_DOWN:
1779                 frames = get_frame_step (f, current_time(), -1);
1780                 if (frames != 0) {
1781                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1782                                 frames *= 10;
1783                         }
1784
1785                         if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1786                                 set (0, true);
1787                         } else {
1788                                 set (current_time() - frames, true);
1789                         }
1790
1791                         ValueChanged (); /* EMIT_SIGNAL */
1792                 }
1793                 break;
1794
1795         default:
1796                 return false;
1797                 break;
1798         }
1799
1800         return true;
1801 }
1802
1803 bool
1804 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1805 {
1806         if (editing || _session == 0 || !dragging) {
1807                 return false;
1808         }
1809
1810         float pixel_frame_scale_factor = 0.2f;
1811
1812         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier))  {
1813                 pixel_frame_scale_factor = 0.1f;
1814         }
1815
1816
1817         if (Keyboard::modifier_state_contains (ev->state,
1818                                                Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1819
1820                 pixel_frame_scale_factor = 0.025f;
1821         }
1822
1823         double y_delta = ev->y - drag_y;
1824
1825         drag_accum +=  y_delta*pixel_frame_scale_factor;
1826
1827         drag_y = ev->y;
1828
1829         if (floor (drag_accum) != 0) {
1830
1831                 framepos_t frames;
1832                 framepos_t pos;
1833                 int dir;
1834                 dir = (drag_accum < 0 ? 1:-1);
1835                 pos = current_time();
1836                 frames = get_frame_step (drag_field, pos, dir);
1837
1838                 if (frames  != 0 &&  frames * drag_accum < current_time()) {
1839                         set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1840                 } else {
1841                         set (0 , false);
1842                 }
1843
1844                 drag_accum= 0;
1845                 ValueChanged();  /* EMIT_SIGNAL */
1846         }
1847
1848         return true;
1849 }
1850
1851 framepos_t
1852 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1853 {
1854         framecnt_t f = 0;
1855         Timecode::BBT_Time BBT;
1856         switch (field) {
1857         case Timecode_Hours:
1858                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1859                 break;
1860         case Timecode_Minutes:
1861                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1862                 break;
1863         case Timecode_Seconds:
1864                 f = _session->frame_rate();
1865                 break;
1866         case Timecode_Frames:
1867                 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1868                 break;
1869
1870         case AudioFrames:
1871                 f = 1;
1872                 break;
1873
1874         case MS_Hours:
1875                 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1876                 break;
1877         case MS_Minutes:
1878                 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1879                 break;
1880         case MS_Seconds:
1881                 f = (framecnt_t) _session->frame_rate();
1882                 break;
1883         case MS_Milliseconds:
1884                 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1885                 break;
1886
1887         case Bars:
1888                 BBT.bars = 1;
1889                 BBT.beats = 0;
1890                 BBT.ticks = 0;
1891                 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1892                 break;
1893         case Beats:
1894                 BBT.bars = 0;
1895                 BBT.beats = 1;
1896                 BBT.ticks = 0;
1897                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1898                 break;
1899         case Ticks:
1900                 BBT.bars = 0;
1901                 BBT.beats = 0;
1902                 BBT.ticks = 1;
1903                 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1904                 break;
1905         default:
1906                 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1907                 f = 0;
1908                 break;
1909         }
1910
1911         return f;
1912 }
1913
1914 framepos_t
1915 AudioClock::current_time (framepos_t) const
1916 {
1917         return last_when;
1918 }
1919
1920 framepos_t
1921 AudioClock::current_duration (framepos_t pos) const
1922 {
1923         framepos_t ret = 0;
1924
1925         switch (_mode) {
1926         case Timecode:
1927                 ret = last_when;
1928                 break;
1929         case BBT:
1930                 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1931                 break;
1932
1933         case MinSec:
1934                 ret = last_when;
1935                 break;
1936
1937         case Frames:
1938                 ret = last_when;
1939                 break;
1940         }
1941
1942         return ret;
1943 }
1944
1945 bool
1946 AudioClock::bbt_validate_edit (const string& str)
1947 {
1948         AnyTime any;
1949
1950         if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1951                 return false;
1952         }
1953
1954         if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1955                 return false;
1956         }
1957
1958         if (!is_duration && any.bbt.bars == 0) {
1959                 return false;
1960         }
1961
1962         if (!is_duration && any.bbt.beats == 0) {
1963                 return false;
1964         }
1965
1966         return true;
1967 }
1968
1969 bool
1970 AudioClock::timecode_validate_edit (const string&)
1971 {
1972         Timecode::Time TC;
1973         int hours;
1974         char ignored[2];
1975
1976         if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1977                     ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1978                 return false;
1979         }
1980
1981         if (hours < 0) {
1982                 TC.hours = hours * -1;
1983                 TC.negative = true;
1984         } else {
1985                 TC.hours = hours;
1986                 TC.negative = false;
1987         }
1988
1989         if (TC.negative && !_negative_allowed) {
1990                 return false;
1991         }
1992
1993         if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1994                 return false;
1995         }
1996
1997         if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1998                 return false;
1999         }
2000
2001         if (_session->timecode_drop_frames()) {
2002                 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
2003                         return false;
2004                 }
2005         }
2006
2007         return true;
2008 }
2009
2010 bool
2011 AudioClock::minsec_validate_edit (const string& str)
2012 {
2013         int hrs, mins, secs, millisecs;
2014
2015         if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2016                 return false;
2017         }
2018
2019         if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
2020                 return false;
2021         }
2022
2023         return true;
2024 }
2025
2026 framepos_t
2027 AudioClock::frames_from_timecode_string (const string& str) const
2028 {
2029         if (_session == 0) {
2030                 return 0;
2031         }
2032
2033         Timecode::Time TC;
2034         framepos_t sample;
2035         char ignored[2];
2036         int hours;
2037
2038         if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2039                 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2040                 return 0;
2041         }
2042         TC.hours = abs(hours);
2043         TC.rate = _session->timecode_frames_per_second();
2044         TC.drop= _session->timecode_drop_frames();
2045
2046         _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2047
2048         // timecode_tester ();
2049         if (edit_is_negative) {
2050                 sample = - sample;
2051         }
2052
2053         return sample;
2054 }
2055
2056 framepos_t
2057 AudioClock::frames_from_minsec_string (const string& str) const
2058 {
2059         if (_session == 0) {
2060                 return 0;
2061         }
2062
2063         int hrs, mins, secs, millisecs;
2064         framecnt_t sr = _session->frame_rate();
2065
2066         if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2067                 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2068                 return 0;
2069         }
2070
2071         return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2072 }
2073
2074 framepos_t
2075 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2076 {
2077         if (_session == 0) {
2078                 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2079                 return 0;
2080         }
2081
2082         AnyTime any;
2083         any.type = AnyTime::BBT;
2084
2085         if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2086                 return 0;
2087         }
2088
2089         if (is_duration) {
2090                 any.bbt.bars++;
2091                 any.bbt.beats++;
2092                 return _session->any_duration_to_frames (pos, any);
2093         } else {
2094                 return _session->convert_to_frames (any);
2095         }
2096 }
2097
2098
2099 framepos_t
2100 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2101 {
2102         if (_session == 0) {
2103                 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2104                 return 0;
2105         }
2106
2107         Timecode::BBT_Time bbt;
2108
2109         if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2110                 return 0;
2111         }
2112
2113         return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2114 }
2115
2116 framepos_t
2117 AudioClock::frames_from_audioframes_string (const string& str) const
2118 {
2119         framepos_t f;
2120         sscanf (str.c_str(), "%" PRId64, &f);
2121         return f;
2122 }
2123
2124 void
2125 AudioClock::copy_text_to_clipboard () const
2126 {
2127         string val;
2128         if (editing) {
2129                 val = pre_edit_string;
2130         } else {
2131                 val = _layout->get_text ();
2132         }
2133         const size_t trim = val.find_first_not_of(" ");
2134         if (trim == string::npos) {
2135                 assert(0); // empty clock, can't be right.
2136                 return;
2137         }
2138         Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2139         cl->set_text (val.substr(trim));
2140 }
2141
2142 void
2143 AudioClock::build_ops_menu ()
2144 {
2145         using namespace Menu_Helpers;
2146         ops_menu = new Menu;
2147         MenuList& ops_items = ops_menu->items();
2148         ops_menu->set_name ("ArdourContextMenu");
2149
2150         ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2151         ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2152         ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2153         ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2154
2155         if (editable && !_off && !is_duration && !_follows_playhead) {
2156                 ops_items.push_back (SeparatorElem());
2157                 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2158                 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2159         }
2160         ops_items.push_back (SeparatorElem());
2161         ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2162 }
2163
2164 void
2165 AudioClock::set_from_playhead ()
2166 {
2167         if (!_session) {
2168                 return;
2169         }
2170
2171         set (_session->transport_frame());
2172         ValueChanged ();
2173 }
2174
2175 void
2176 AudioClock::locate ()
2177 {
2178         if (!_session || is_duration) {
2179                 return;
2180         }
2181
2182         _session->request_locate (current_time(), _session->transport_rolling ());
2183 }
2184
2185 void
2186 AudioClock::set_mode (Mode m, bool noemit)
2187 {
2188         if (_mode == m) {
2189                 return;
2190         }
2191
2192         _mode = m;
2193
2194         insert_map.clear();
2195
2196         _layout->set_text ("");
2197
2198         Gtk::Requisition req;
2199         set_clock_dimensions (req);
2200
2201         if (_left_layout) {
2202
2203                 _left_layout->set_attributes (info_attributes);
2204                 _right_layout->set_attributes (info_attributes);
2205                 /* adjust info_height according to font size */
2206                 int ignored;
2207                 _left_layout->set_text (" 1234567890");
2208                 _left_layout->get_pixel_size (ignored, info_height);
2209
2210                 _left_layout->set_text ("");
2211                 _right_layout->set_text ("");
2212         }
2213
2214         switch (_mode) {
2215         case Timecode:
2216                 mode_based_info_ratio = 0.6;
2217                 insert_map.push_back (11);
2218                 insert_map.push_back (10);
2219                 insert_map.push_back (8);
2220                 insert_map.push_back (7);
2221                 insert_map.push_back (5);
2222                 insert_map.push_back (4);
2223                 insert_map.push_back (2);
2224                 insert_map.push_back (1);
2225                 break;
2226
2227         case BBT:
2228                 mode_based_info_ratio = 0.5;
2229                 insert_map.push_back (11);
2230                 insert_map.push_back (10);
2231                 insert_map.push_back (9);
2232                 insert_map.push_back (8);
2233                 insert_map.push_back (6);
2234                 insert_map.push_back (5);
2235                 insert_map.push_back (3);
2236                 insert_map.push_back (2);
2237                 insert_map.push_back (1);
2238                 break;
2239
2240         case MinSec:
2241                 mode_based_info_ratio = 0.6;
2242                 insert_map.push_back (12);
2243                 insert_map.push_back (11);
2244                 insert_map.push_back (10);
2245                 insert_map.push_back (8);
2246                 insert_map.push_back (7);
2247                 insert_map.push_back (5);
2248                 insert_map.push_back (4);
2249                 insert_map.push_back (2);
2250                 insert_map.push_back (1);
2251                 break;
2252
2253         case Frames:
2254                 mode_based_info_ratio = 0.45;
2255                 break;
2256         }
2257
2258         set (last_when, true);
2259
2260         if (!is_transient && !noemit) {
2261                 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2262         }
2263
2264         mode_changed (); /* EMIT SIGNAL (the member one) */
2265 }
2266
2267 void
2268 AudioClock::set_bbt_reference (framepos_t pos)
2269 {
2270         bbt_reference_time = pos;
2271 }
2272
2273 void
2274 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2275 {
2276         CairoWidget::on_style_changed (old_style);
2277
2278         Gtk::Requisition req;
2279         set_clock_dimensions (req);
2280
2281         /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2282         // set_font ();
2283         set_colors ();
2284 }
2285
2286 void
2287 AudioClock::set_editable (bool yn)
2288 {
2289         editable = yn;
2290 }
2291
2292 void
2293 AudioClock::set_is_duration (bool yn)
2294 {
2295         if (yn == is_duration) {
2296                 return;
2297         }
2298
2299         is_duration = yn;
2300         set (last_when, true);
2301 }
2302
2303 void
2304 AudioClock::set_off (bool yn)
2305 {
2306         if (_off == yn) {
2307                 return;
2308         }
2309
2310         _off = yn;
2311
2312         /* force a redraw. last_when will be preserved, but the clock text will
2313          * change
2314          */
2315
2316         set (last_when, true);
2317 }
2318
2319 void
2320 AudioClock::focus ()
2321 {
2322         start_edit (Field (0));
2323 }
2324
2325 void
2326 AudioClock::set_corner_radius (double r)
2327 {
2328         corner_radius = r;
2329         first_width = 0;
2330         first_height = 0;
2331         queue_resize ();
2332 }
2333
2334 void
2335 AudioClock::dpi_reset ()
2336 {
2337         /* force recomputation of size even if we are fixed width
2338          */
2339         first_width = 0;
2340         first_height = 0;
2341         queue_resize ();
2342 }
2343
2344 void
2345 AudioClock::set_negative_allowed (bool yn)
2346 {
2347         _negative_allowed = yn;
2348 }