2 Copyright (C) 1999 Paul Davis
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.
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.
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.
20 #include <cstdio> // for sprintf
23 #include "pbd/convert.h"
24 #include "pbd/enumwriter.h"
26 #include <gtkmm/style.h>
27 #include <sigc++/bind.h>
29 #include "gtkmm2ext/cairocell.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
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"
39 #include "ardour_ui.h"
40 #include "audio_clock.h"
41 #include "global_signals.h"
44 #include "gui_thread.h"
47 using namespace ARDOUR;
52 using Gtkmm2ext::Keyboard;
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;
60 #define BBT_BAR_CHAR "|"
61 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
63 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
64 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
67 , is_transient (transient)
68 , is_duration (duration)
69 , editable (allow_edit)
70 , _follows_playhead (follows_playhead)
75 , _edit_by_click_field (false)
84 , mode_based_info_ratio (1.0)
88 , bbt_reference_time (-1)
93 , drag_field (Field (0))
96 set_flags (CAN_FOCUS);
98 _layout = Pango::Layout::create (get_pango_context());
99 _layout->set_attributes (normal_attributes);
102 _left_layout = Pango::Layout::create (get_pango_context());
103 _right_layout = Pango::Layout::create (get_pango_context());
106 set_widget_name (widget_name);
108 _mode = BBT; /* lie to force mode switch */
110 set (last_when, true);
113 clocks.push_back (this);
116 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
117 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
120 AudioClock::~AudioClock ()
122 delete foreground_attr;
127 AudioClock::set_widget_name (const string& str)
132 set_name (str + " clock");
142 AudioClock::on_realize ()
144 CairoWidget::on_realize ();
150 AudioClock::set_font ()
152 Glib::RefPtr<Gtk::Style> style = get_style ();
153 Pango::FontDescription font;
154 Pango::AttrFontDesc* font_attr;
156 if (!is_realized()) {
157 font = get_font_for_style (get_name());
159 font = style->get_font();
162 font_size = font.get_size();
164 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
166 normal_attributes.change (*font_attr);
167 editing_attributes.change (*font_attr);
169 /* now a smaller version of the same font */
172 font.set_size ((int) lrint (font_size * info_font_scale_factor));
173 font.set_weight (Pango::WEIGHT_NORMAL);
174 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
176 info_attributes.change (*font_attr);
178 /* and an even smaller one */
182 /* get the figure width for the font. This doesn't have to super
183 * accurate since we only use it to measure the (roughly 1 character)
184 * offset from the position Pango tells us for the "cursor"
187 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
191 tmp->get_pixel_size (em_width, ignore_height);
195 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
197 CairoWidget::set_active_state (s);
202 AudioClock::set_colors ()
208 uint32_t editing_color;
209 uint32_t cursor_color;
211 if (active_state()) {
212 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
213 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
214 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
215 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
217 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
218 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
219 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
220 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
223 /* store for bg and cursor in render() */
225 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
232 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
239 /* rescale for Pango colors ... sigh */
241 r = lrint (r * 65535.0);
242 g = lrint (g * 65535.0);
243 b = lrint (b * 65535.0);
245 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
246 r = lrint ((r/255.0) * 65535.0);
247 g = lrint ((g/255.0) * 65535.0);
248 b = lrint ((b/255.0) * 65535.0);
249 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
251 UINT_TO_RGBA (editing_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 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
257 normal_attributes.change (*foreground_attr);
258 info_attributes.change (*foreground_attr);
259 editing_attributes.change (*foreground_attr);
260 editing_attributes.change (*editing_attr);
263 _layout->set_attributes (normal_attributes);
265 _layout->set_attributes (editing_attributes);
269 _left_layout->set_attributes (info_attributes);
270 _right_layout->set_attributes (info_attributes);
277 AudioClock::render (cairo_t* cr)
279 /* main layout: rounded rect, plus the text */
282 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
285 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
287 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
290 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
296 cairo_move_to (cr, layout_x_offset, 0);
298 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
299 cairo_move_to (cr, layout_x_offset + xcenter, (upper_height - layout_height) / 2.0);
302 pango_cairo_show_layout (cr, _layout->gobj());
306 double h = get_height() - upper_height - separator_height;
309 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
312 if (mode_based_info_ratio != 1.0) {
314 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
318 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
320 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
325 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
326 pango_cairo_show_layout (cr, _left_layout->gobj());
330 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
331 get_width() - separator_height - left_rect_width, h,
334 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
335 get_width() - separator_height - left_rect_width, h);
341 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
342 /* right-align does not work per se beacuse layout width is unset.
343 * Using _right_layout->set_width([value >=0]) would also enable
344 * word-wrapping which is not wanted here.
345 * The solution is to custom align the layout depending on its size.
346 * if it is larger than the available space it will be cropped on the
347 * right edge rather than override text on the left side.
350 _right_layout->get_pixel_size(rw, rh);
351 x = get_width() - rw- separator_height - x_leading_padding;
352 if (x < x_leading_padding + left_rect_width + separator_height) {
353 x = x_leading_padding + left_rect_width + separator_height;
355 cairo_move_to (cr, x, upper_height + separator_height + ((h - rh)/2.0));
357 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
359 pango_cairo_show_layout (cr, _right_layout->gobj());
362 /* no info to display, or just one */
366 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
368 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
376 if (!insert_map.empty()) {
378 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
380 if (input_string.length() < insert_map.size()) {
381 Pango::Rectangle cursor;
383 if (input_string.empty()) {
384 /* nothing entered yet, put cursor at the end
387 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
389 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
392 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
395 min (get_width() - 2.0,
396 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + xcenter + em_width), 0,
397 2.0, cursor.get_height()/PANGO_SCALE);
400 min (get_width() - 2.0,
401 (double) layout_x_offset + xcenter + cursor.get_x()/PANGO_SCALE + em_width),
402 (upper_height - layout_height)/2.0,
403 2.0, cursor.get_height()/PANGO_SCALE);
407 /* we've entered all possible digits, no cursor */
411 if (input_string.empty()) {
412 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
421 (upper_height - layout_height)/2.0,
431 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
433 CairoWidget::on_size_allocate (alloc);
436 upper_height = (get_height()/2.0) - 1.0;
438 upper_height = get_height();
442 /* center display in available space
443 * NB. this only works if the containing widget is not the
444 * layout itself (eg. the session->property dialog)
446 layout_x_offset = (get_width() - layout_width)/2.0;
454 AudioClock::on_size_request (Gtk::Requisition* req)
456 /* even for non fixed width clocks, the size we *ask* for never changes,
457 even though the size we receive might. so once we've computed it,
462 req->width = first_width;
463 req->height = first_height;
467 Glib::RefPtr<Pango::Layout> tmp;
468 Glib::RefPtr<Gtk::Style> style = get_style ();
469 Pango::FontDescription font;
471 tmp = Pango::Layout::create (get_pango_context());
473 if (!is_realized()) {
474 font = get_font_for_style (get_name());
476 font = style->get_font();
479 tmp->set_font_description (font);
483 tmp->set_text ("-88:88:88:88");
484 tmp->get_pixel_size (_mode_width[Timecode], ignored);
485 tmp->set_text (" 8888|88|8888");
486 tmp->get_pixel_size (_mode_width[BBT], ignored);
487 tmp->set_text (" 88:88:88,888");
488 tmp->get_pixel_size (_mode_width[MinSec], ignored);
489 tmp->set_text (" 8888888888");
490 tmp->get_pixel_size (_mode_width[Frames], ignored);
492 /* this string is the longest thing we will ever display,
493 and also includes the BBT bar char that may descends below
494 the baseline a bit, and a comma for the minsecs mode
495 where we printf a fractional value (XXX or should)
498 tmp->set_text (" 8888888888:,|");
502 tmp->set_text ("-88:88:88:88");
505 tmp->set_text (" 8888|88|8888");
508 tmp->set_text (" 88:88:88,888");
511 tmp->set_text (" 8888888888");
516 tmp->get_pixel_size (req->width, req->height);
518 layout_height = req->height;
519 layout_width = req->width;
521 /* now tackle height, for which we need to know the height of the lower
529 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
530 font.set_weight (Pango::WEIGHT_NORMAL);
531 tmp->set_font_description (font);
533 /* we only care about height, so put as much stuff in here
534 as possible that might change the height.
536 tmp->set_text ("qyhH|"); /* one ascender, one descender */
538 tmp->get_pixel_size (w, info_height);
540 /* silly extra padding that seems necessary to correct the info
541 * that pango just gave us. I have no idea why.
546 req->height += info_height;
547 req->height += separator_height;
550 first_height = req->height;
551 first_width = req->width;
555 AudioClock::show_edit_status (int length)
557 editing_attr->set_start_index (edit_string.length() - length);
558 editing_attr->set_end_index (edit_string.length());
560 editing_attributes.change (*foreground_attr);
561 editing_attributes.change (*editing_attr);
563 _layout->set_attributes (editing_attributes);
567 AudioClock::start_edit (Field f)
569 pre_edit_string = _layout->get_text ();
570 if (!insert_map.empty()) {
571 edit_string = pre_edit_string;
573 edit_string.clear ();
574 _layout->set_text ("");
576 input_string.clear ();
580 input_string = get_field (f);
581 show_edit_status (merge_input_and_edit_string ());
582 _layout->set_text (edit_string);
587 Keyboard::magic_widget_grab_focus ();
592 AudioClock::get_field (Field f)
596 return edit_string.substr (1, 2);
598 case Timecode_Minutes:
599 return edit_string.substr (4, 2);
601 case Timecode_Seconds:
602 return edit_string.substr (7, 2);
604 case Timecode_Frames:
605 return edit_string.substr (10, 2);
608 return edit_string.substr (1, 2);
611 return edit_string.substr (4, 2);
614 return edit_string.substr (7, 2);
616 case MS_Milliseconds:
617 return edit_string.substr (10, 3);
620 return edit_string.substr (1, 3);
623 return edit_string.substr (5, 2);
626 return edit_string.substr (8, 4);
636 AudioClock::end_edit (bool modify)
644 ok = timecode_validate_edit (edit_string);
648 ok = bbt_validate_edit (edit_string);
652 ok = minsec_validate_edit (edit_string);
660 edit_string = pre_edit_string;
661 input_string.clear ();
662 _layout->set_text (edit_string);
663 show_edit_status (0);
664 /* edit attributes remain in use */
668 framepos_t pos = 0; /* stupid gcc */
672 pos = frames_from_timecode_string (edit_string);
677 pos = frame_duration_from_bbt_string (0, edit_string);
679 pos = frames_from_bbt_string (0, edit_string);
684 pos = frames_from_minsec_string (edit_string);
688 pos = frames_from_audioframes_string (edit_string);
693 _layout->set_attributes (normal_attributes);
694 ValueChanged(); /* EMIT_SIGNAL */
700 _layout->set_attributes (normal_attributes);
701 _layout->set_text (pre_edit_string);
712 AudioClock::drop_focus ()
714 Keyboard::magic_widget_drop_focus ();
718 /* move focus back to the default widget in the top level window */
720 Widget* top = get_toplevel();
722 if (top->is_toplevel ()) {
723 Window* win = dynamic_cast<Window*> (top);
730 AudioClock::parse_as_frames_distance (const std::string& str)
734 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
742 AudioClock::parse_as_minsec_distance (const std::string& str)
744 framecnt_t sr = _session->frame_rate();
750 switch (str.length()) {
757 sscanf (str.c_str(), "%" PRId32, &msecs);
758 return msecs * (sr / 1000);
761 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
762 return (secs * sr) + (msecs * (sr/1000));
765 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
766 return (secs * sr) + (msecs * (sr/1000));
769 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
770 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
773 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
774 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
777 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
778 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
781 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
782 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
792 AudioClock::parse_as_timecode_distance (const std::string& str)
794 double fps = _session->timecode_frames_per_second();
795 framecnt_t sr = _session->frame_rate();
801 switch (str.length()) {
806 sscanf (str.c_str(), "%" PRId32, &frames);
807 return lrint ((frames/(float)fps) * sr);
810 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
811 return (secs * sr) + lrint ((frames/(float)fps) * sr);
814 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
815 return (secs * sr) + lrint ((frames/(float)fps) * sr);
818 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
819 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
822 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
823 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
826 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
827 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
830 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
831 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
841 AudioClock::parse_as_bbt_distance (const std::string&)
847 AudioClock::parse_as_distance (const std::string& instr)
851 return parse_as_timecode_distance (instr);
854 return parse_as_frames_distance (instr);
857 return parse_as_bbt_distance (instr);
860 return parse_as_minsec_distance (instr);
867 AudioClock::end_edit_relative (bool add)
873 ok = timecode_validate_edit (edit_string);
877 ok = bbt_validate_edit (edit_string);
881 ok = minsec_validate_edit (edit_string);
889 edit_string = pre_edit_string;
890 input_string.clear ();
891 _layout->set_text (edit_string);
892 show_edit_status (0);
893 /* edit attributes remain in use */
898 framecnt_t frames = parse_as_distance (input_string);
903 _layout->set_attributes (normal_attributes);
907 set (current_time() + frames, true);
909 framepos_t c = current_time();
912 set (c - frames, true);
917 ValueChanged (); /* EMIT SIGNAL */
920 input_string.clear ();
926 AudioClock::session_configuration_changed (std::string p)
928 if (p == "sync-source" || p == "external-sync") {
929 set (current_time(), true);
933 if (p != "timecode-offset" && p != "timecode-offset-negative") {
942 current = current_duration ();
944 current = current_time ();
954 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
956 if ((!force && !is_visible()) || _session == 0) {
961 when = when - offset;
964 if (when == last_when && !force) {
965 if (_mode != Timecode) {
966 /* timecode may need to force display of TC source
967 * time, so don't return early.
975 _right_layout->set_alignment(Pango::ALIGN_LEFT);
981 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
983 set_timecode (when, force);
987 set_bbt (when, force);
991 set_minsec (when, force);
995 set_frames (when, force);
1005 AudioClock::set_frames (framepos_t when, bool /*force*/)
1008 bool negative = false;
1011 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1014 _left_layout->set_text ("");
1015 _right_layout->set_text ("");
1027 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1029 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1032 _layout->set_text (buf);
1035 framecnt_t rate = _session->frame_rate();
1037 if (fmod (rate, 100.0) == 0.0) {
1038 sprintf (buf, "SR %.1fkHz", rate/1000.0);
1040 sprintf (buf, "SR %" PRId64, rate);
1043 _left_layout->set_text (buf);
1045 float vid_pullup = _session->config.get_video_pullup();
1047 if (vid_pullup == 0.0) {
1048 _right_layout->set_text (_("pullup: \u2012"));
1050 sprintf (buf, _("%+-6.4f%%"), vid_pullup);
1051 _right_layout->set_text (buf);
1057 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1065 bool negative = false;
1068 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1071 _left_layout->set_text ("");
1072 _right_layout->set_text ("");
1084 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1085 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1086 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1087 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1088 secs = (int) floor (left / (float) _session->frame_rate());
1089 left -= (framecnt_t) floor (secs * _session->frame_rate());
1090 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1093 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1095 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1098 _layout->set_text (buf);
1102 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1105 bool negative = false;
1108 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1110 _left_layout->set_text ("");
1111 _right_layout->set_text ("");
1123 _session->timecode_duration (when, TC);
1125 _session->timecode_time (when, TC);
1128 TC.negative = TC.negative || negative;
1130 _layout->set_text (Timecode::timecode_format_time(TC));
1132 if (_left_layout && _right_layout) {
1134 SyncSource sync_src = Config->get_sync_source();
1136 if (_session->config.get_external_sync()) {
1137 Slave* slave = _session->slave();
1141 _left_layout->set_text (string_compose ("%1",
1142 sync_source_to_string(sync_src, true)));
1143 _right_layout->set_text ("");
1147 _left_layout->set_text (string_compose ("%1",
1148 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1149 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1150 (int)round(font_size * info_font_scale_factor * .9), slave->approximate_current_delta()));
1152 _left_layout->set_text (string_compose ("--pending--",
1153 sync_source_to_string(sync_src, true)));
1154 _right_layout->set_text ("");
1159 _left_layout->set_text (string_compose ("%1",
1160 sync_source_to_string(sync_src, true)));
1161 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1162 (int)round(font_size * info_font_scale_factor * .9), slave->approximate_current_delta()));
1164 _left_layout->set_text (string_compose ("%1 --pending--",
1165 sync_source_to_string(sync_src, true)));
1166 _right_layout->set_text ("");
1171 _left_layout->set_text (string_compose ("%1",
1172 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1173 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1174 (int)round(font_size * info_font_scale_factor * .9), slave->approximate_current_delta()));
1176 _left_layout->set_text (string_compose ("%1 --pending--",
1177 sync_source_to_string(sync_src, true)));
1178 _right_layout->set_text ("");
1183 _left_layout->set_text (string_compose (_("INT/%1"),
1184 sync_source_to_string(sync_src, true)));
1185 _right_layout->set_text ("");
1191 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1194 Timecode::BBT_Time BBT;
1195 bool negative = false;
1198 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1200 _left_layout->set_text ("");
1201 _right_layout->set_text ("");
1211 /* handle a common case */
1218 _session->tempo_map().bbt_time (when, BBT);
1223 _session->tempo_map().bbt_time (when, BBT);
1227 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1228 BBT.bars, BBT.beats, BBT.ticks);
1230 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1231 BBT.bars, BBT.beats, BBT.ticks);
1234 _layout->set_text (buf);
1236 if (_right_layout) {
1239 if (bbt_reference_time < 0) {
1242 pos = bbt_reference_time;
1245 TempoMetric m (_session->tempo_map().metric_at (pos));
1247 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1248 _left_layout->set_text (buf);
1250 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1251 _right_layout->set_text (buf);
1256 AudioClock::set_session (Session *s)
1258 SessionHandlePtr::set_session (s);
1262 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1264 const XMLProperty* prop;
1265 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1266 AudioClock::Mode amode;
1269 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1270 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1272 if ((prop = (*i)->property (X_("mode"))) != 0) {
1273 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1276 if ((prop = (*i)->property (X_("on"))) != 0) {
1277 set_off (!string_is_affirmative (prop->value()));
1284 set (last_when, true);
1289 AudioClock::on_key_press_event (GdkEventKey* ev)
1297 int highlight_length;
1300 switch (ev->keyval) {
1343 case GDK_KP_Subtract:
1344 end_edit_relative (false);
1350 end_edit_relative (true);
1363 ChangeAborted(); /* EMIT SIGNAL */
1368 if (!input_string.empty()) {
1369 /* delete the last key entered
1371 input_string = input_string.substr (0, input_string.length() - 1);
1373 goto use_input_string;
1379 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1380 /* too many digits: eat the key event, but do nothing with it */
1384 input_string.push_back (new_char);
1390 /* get this one in the right order, and to the right width */
1391 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1392 edit_string = edit_string.substr (0, edit_string.length() - 1);
1394 edit_string.push_back (new_char);
1396 if (!edit_string.empty()) {
1398 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1399 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1402 /* highlight the whole thing */
1403 highlight_length = edit_string.length();
1407 highlight_length = merge_input_and_edit_string ();
1410 show_edit_status (highlight_length);
1411 _layout->set_text (edit_string);
1418 AudioClock::merge_input_and_edit_string ()
1420 /* merge with pre-edit-string into edit string */
1422 edit_string = pre_edit_string;
1424 if (input_string.empty()) {
1428 string::size_type target;
1429 for (string::size_type i = 0; i < input_string.length(); ++i) {
1430 target = insert_map[input_string.length() - 1 - i];
1431 edit_string[target] = input_string[i];
1433 /* highlight from end to wherever the last character was added */
1434 return edit_string.length() - insert_map[input_string.length()-1];
1439 AudioClock::on_key_release_event (GdkEventKey *ev)
1445 /* return true for keys that we used on press
1446 so that they cannot possibly do double-duty
1448 switch (ev->keyval) {
1471 case GDK_KP_Decimal:
1479 case GDK_KP_Subtract:
1487 AudioClock::index_to_field (int index) const
1492 return Timecode_Hours;
1493 } else if (index < 7) {
1494 return Timecode_Minutes;
1495 } else if (index < 10) {
1496 return Timecode_Seconds;
1498 return Timecode_Frames;
1504 } else if (index < 7) {
1512 return Timecode_Hours;
1513 } else if (index < 6) {
1515 } else if (index < 9) {
1518 return MS_Milliseconds;
1530 AudioClock::on_button_press_event (GdkEventButton *ev)
1532 switch (ev->button) {
1534 if (editable && !_off) {
1540 /* the text has been centered vertically, so adjust
1544 y = ev->y - ((upper_height - layout_height)/2);
1545 x = ev->x - layout_x_offset;
1547 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1548 /* pretend it is a character on the far right */
1551 drag_field = index_to_field (index);
1553 /* make absolutely sure that the pointer is grabbed */
1554 gdk_pointer_grab(ev->window,false ,
1555 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1556 NULL,NULL,ev->time);
1558 drag_start_y = ev->y;
1572 AudioClock::on_button_release_event (GdkEventButton *ev)
1574 if (editable && !_off) {
1576 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1578 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1579 // we actually dragged so return without
1580 // setting editing focus, or we shift clicked
1583 if (ev->button == 1) {
1585 if (_edit_by_click_field) {
1589 int y = ev->y - ((upper_height - layout_height)/2);
1590 int x = ev->x - layout_x_offset;
1593 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1597 f = index_to_field (index);
1600 case Timecode_Frames:
1601 case MS_Milliseconds:
1617 if (Keyboard::is_context_menu_event (ev)) {
1618 if (ops_menu == 0) {
1621 ops_menu->popup (1, ev->time);
1629 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1631 bool ret = CairoWidget::on_focus_out_event (ev);
1641 AudioClock::on_scroll_event (GdkEventScroll *ev)
1646 if (editing || _session == 0 || !editable || _off) {
1653 /* the text has been centered vertically, so adjust
1657 y = ev->y - ((upper_height - layout_height)/2);
1658 x = ev->x - layout_x_offset;
1660 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1661 /* not in the main layout */
1665 Field f = index_to_field (index);
1666 framepos_t frames = 0;
1668 switch (ev->direction) {
1671 frames = get_frame_step (f);
1673 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1676 set (current_time() + frames, true);
1677 ValueChanged (); /* EMIT_SIGNAL */
1681 case GDK_SCROLL_DOWN:
1682 frames = get_frame_step (f);
1684 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1688 if ((double)current_time() - (double)frames < 0.0) {
1691 set (current_time() - frames, true);
1694 ValueChanged (); /* EMIT_SIGNAL */
1707 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1709 if (editing || _session == 0 || !dragging) {
1713 float pixel_frame_scale_factor = 0.2f;
1715 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1716 pixel_frame_scale_factor = 0.1f;
1720 if (Keyboard::modifier_state_contains (ev->state,
1721 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1723 pixel_frame_scale_factor = 0.025f;
1726 double y_delta = ev->y - drag_y;
1728 drag_accum += y_delta*pixel_frame_scale_factor;
1732 if (trunc (drag_accum) != 0) {
1737 dir = (drag_accum < 0 ? 1:-1);
1738 pos = current_time();
1739 frames = get_frame_step (drag_field, pos, dir);
1741 if (frames != 0 && frames * drag_accum < current_time()) {
1742 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1748 ValueChanged(); /* EMIT_SIGNAL */
1755 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1758 Timecode::BBT_Time BBT;
1760 case Timecode_Hours:
1761 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1763 case Timecode_Minutes:
1764 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1766 case Timecode_Seconds:
1767 f = _session->frame_rate();
1769 case Timecode_Frames:
1770 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1778 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1781 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1784 f = (framecnt_t) _session->frame_rate();
1786 case MS_Milliseconds:
1787 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1794 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1800 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1806 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1809 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1818 AudioClock::current_time (framepos_t) const
1824 AudioClock::current_duration (framepos_t pos) const
1833 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1849 AudioClock::bbt_validate_edit (const string& str)
1853 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1857 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1861 if (!is_duration && any.bbt.bars == 0) {
1865 if (!is_duration && any.bbt.beats == 0) {
1873 AudioClock::timecode_validate_edit (const string&)
1877 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1878 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1882 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1886 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1890 if (_session->timecode_drop_frames()) {
1891 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1900 AudioClock::minsec_validate_edit (const string& str)
1902 int hrs, mins, secs, millisecs;
1904 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1908 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1916 AudioClock::frames_from_timecode_string (const string& str) const
1918 if (_session == 0) {
1925 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1926 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1930 TC.rate = _session->timecode_frames_per_second();
1931 TC.drop= _session->timecode_drop_frames();
1933 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1935 // timecode_tester ();
1941 AudioClock::frames_from_minsec_string (const string& str) const
1943 if (_session == 0) {
1947 int hrs, mins, secs, millisecs;
1948 framecnt_t sr = _session->frame_rate();
1950 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1951 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1955 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1959 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1961 if (_session == 0) {
1962 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1967 any.type = AnyTime::BBT;
1969 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1976 return _session->any_duration_to_frames (pos, any);
1978 return _session->convert_to_frames (any);
1984 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1986 if (_session == 0) {
1987 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1991 Timecode::BBT_Time bbt;
1993 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1997 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2001 AudioClock::frames_from_audioframes_string (const string& str) const
2004 sscanf (str.c_str(), "%" PRId64, &f);
2009 AudioClock::build_ops_menu ()
2011 using namespace Menu_Helpers;
2012 ops_menu = new Menu;
2013 MenuList& ops_items = ops_menu->items();
2014 ops_menu->set_name ("ArdourContextMenu");
2016 if (!Profile->get_sae()) {
2017 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2019 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2020 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2021 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2023 if (editable && !_off && !is_duration && !_follows_playhead) {
2024 ops_items.push_back (SeparatorElem());
2025 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2026 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2031 AudioClock::set_from_playhead ()
2037 set (_session->transport_frame());
2042 AudioClock::locate ()
2044 if (!_session || is_duration) {
2048 _session->request_locate (current_time(), _session->transport_rolling ());
2052 AudioClock::set_mode (Mode m)
2062 _layout->set_text ("");
2066 _left_layout->set_attributes (info_attributes);
2067 _right_layout->set_attributes (info_attributes);
2068 /* adjust info_height according to font size */
2070 _left_layout->set_text (" 1234567890");
2071 _left_layout->get_pixel_size (ignored, info_height);
2074 _left_layout->set_text ("");
2075 _right_layout->set_text ("");
2080 mode_based_info_ratio = 0.5;
2081 insert_map.push_back (11);
2082 insert_map.push_back (10);
2083 insert_map.push_back (8);
2084 insert_map.push_back (7);
2085 insert_map.push_back (5);
2086 insert_map.push_back (4);
2087 insert_map.push_back (2);
2088 insert_map.push_back (1);
2092 mode_based_info_ratio = 0.5;
2093 insert_map.push_back (11);
2094 insert_map.push_back (10);
2095 insert_map.push_back (9);
2096 insert_map.push_back (8);
2097 insert_map.push_back (6);
2098 insert_map.push_back (5);
2099 insert_map.push_back (3);
2100 insert_map.push_back (2);
2101 insert_map.push_back (1);
2105 mode_based_info_ratio = 1.0;
2106 insert_map.push_back (12);
2107 insert_map.push_back (11);
2108 insert_map.push_back (10);
2109 insert_map.push_back (8);
2110 insert_map.push_back (7);
2111 insert_map.push_back (5);
2112 insert_map.push_back (4);
2113 insert_map.push_back (2);
2114 insert_map.push_back (1);
2118 mode_based_info_ratio = 0.5;
2122 set (last_when, true);
2124 if (!is_transient) {
2125 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2128 if (!_fixed_width) {
2129 /* display is different, allow us to resize */
2135 mode_changed (); /* EMIT SIGNAL (the member one) */
2139 AudioClock::set_bbt_reference (framepos_t pos)
2141 bbt_reference_time = pos;
2145 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2147 CairoWidget::on_style_changed (old_style);
2155 AudioClock::set_editable (bool yn)
2161 AudioClock::set_is_duration (bool yn)
2163 if (yn == is_duration) {
2168 set (last_when, true);
2172 AudioClock::set_off (bool yn)
2180 /* force a redraw. last_when will be preserved, but the clock text will
2184 set (last_when, true);
2188 AudioClock::focus ()
2190 start_edit (Field (0));
2194 AudioClock::set_corner_radius (double r)
2201 AudioClock::set_fixed_width (bool yn)
2207 AudioClock::dpi_reset ()
2209 /* force recomputation of size even if we are fixed width