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