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