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