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.6;
57 const double AudioClock::separator_height = 2.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)
87 , bbt_reference_time (-1)
92 , drag_field (Field (0))
95 set_flags (CAN_FOCUS);
97 _layout = Pango::Layout::create (get_pango_context());
98 _layout->set_attributes (normal_attributes);
101 _left_layout = Pango::Layout::create (get_pango_context());
102 _right_layout = Pango::Layout::create (get_pango_context());
105 set_widget_name (widget_name);
107 _mode = BBT; /* lie to force mode switch */
109 set (last_when, true);
112 clocks.push_back (this);
115 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
116 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
119 AudioClock::~AudioClock ()
121 delete foreground_attr;
126 AudioClock::set_widget_name (const string& str)
131 set_name (str + " clock");
141 AudioClock::on_realize ()
143 CairoWidget::on_realize ();
149 AudioClock::set_font ()
151 Glib::RefPtr<Gtk::Style> style = get_style ();
152 Pango::FontDescription font;
153 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 */
181 font.set_size ((int) lrint (font_size * info_font_scale_factor * 0.75));
182 font.set_weight (Pango::WEIGHT_BOLD);
183 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
185 small_info_attributes.change (*font_attr);
189 /* get the figure width for the font. This doesn't have to super
190 * accurate since we only use it to measure the (roughly 1 character)
191 * offset from the position Pango tells us for the "cursor"
194 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
198 tmp->get_pixel_size (em_width, ignore_height);
202 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
204 CairoWidget::set_active_state (s);
209 AudioClock::set_colors ()
215 uint32_t editing_color;
216 uint32_t cursor_color;
218 if (active_state()) {
219 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
220 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
221 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
222 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
224 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
225 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
226 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
227 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
230 /* store for bg and cursor in render() */
232 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
239 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
246 /* rescale for Pango colors ... sigh */
248 r = lrint (r * 65535.0);
249 g = lrint (g * 65535.0);
250 b = lrint (b * 65535.0);
252 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
253 r = lrint ((r/255.0) * 65535.0);
254 g = lrint ((g/255.0) * 65535.0);
255 b = lrint ((b/255.0) * 65535.0);
256 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
258 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
259 r = lrint ((r/255.0) * 65535.0);
260 g = lrint ((g/255.0) * 65535.0);
261 b = lrint ((b/255.0) * 65535.0);
262 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
264 normal_attributes.change (*foreground_attr);
265 info_attributes.change (*foreground_attr);
266 small_info_attributes.change (*foreground_attr);
267 editing_attributes.change (*foreground_attr);
268 editing_attributes.change (*editing_attr);
271 _layout->set_attributes (normal_attributes);
273 _layout->set_attributes (editing_attributes);
277 if (_mode == Timecode) {
278 _left_layout->set_attributes (small_info_attributes);
279 _right_layout->set_attributes (small_info_attributes);
281 _left_layout->set_attributes (info_attributes);
282 _right_layout->set_attributes (info_attributes);
290 AudioClock::render (cairo_t* cr)
292 /* main layout: rounded rect, plus the text */
295 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
297 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
299 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
305 cairo_move_to (cr, layout_x_offset, 0);
307 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
310 pango_cairo_show_layout (cr, _layout->gobj());
314 double h = get_height() - upper_height - separator_height;
317 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
320 if (mode_based_info_ratio != 1.0) {
322 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
326 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
328 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
333 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
334 pango_cairo_show_layout (cr, _left_layout->gobj());
338 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
339 get_width() - separator_height - left_rect_width, h,
342 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
343 get_width() - separator_height - left_rect_width, h);
349 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
350 /* right-align does not work per se beacuse layout width is unset.
351 * Using _right_layout->set_width([value >=0]) would also enable
352 * word-wrapping which is not wanted here.
353 * The solution is to custom align the layout depending on its size.
354 * if it is larger than the available space it will be cropped on the
355 * right edge rather than override text on the left side.
358 _right_layout->get_pixel_size(rw, rh);
359 x = get_width() - rw- separator_height - x_leading_padding;
360 if (x < x_leading_padding + left_rect_width + separator_height) {
361 x = x_leading_padding + left_rect_width + separator_height;
363 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
365 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
367 pango_cairo_show_layout (cr, _right_layout->gobj());
370 /* no info to display, or just one */
374 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
376 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
384 if (!insert_map.empty()) {
387 if (input_string.length() < insert_map.size()) {
388 Pango::Rectangle cursor;
390 if (input_string.empty()) {
391 /* nothing entered yet, put cursor at the end
394 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
396 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
399 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
402 min (get_width() - 2.0,
403 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + em_width), 0,
404 2.0, cursor.get_height()/PANGO_SCALE);
407 min (get_width() - 2.0,
408 (double) layout_x_offset + cursor.get_x()/PANGO_SCALE + em_width),
409 (upper_height - layout_height)/2.0,
410 2.0, cursor.get_height()/PANGO_SCALE);
414 /* we've entered all possible digits, no cursor */
418 if (input_string.empty()) {
419 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
428 (upper_height - layout_height)/2.0,
438 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
440 CairoWidget::on_size_allocate (alloc);
443 upper_height = (get_height()/2.0) - 1.0;
445 upper_height = get_height();
449 /* center display in available space */
450 layout_x_offset = (get_width() - layout_width)/2.0;
458 AudioClock::on_size_request (Gtk::Requisition* req)
460 /* even for non fixed width clocks, the size we *ask* for never changes,
461 even though the size we receive might. so once we've computed it,
466 req->width = first_width;
467 req->height = first_height;
471 Glib::RefPtr<Pango::Layout> tmp;
472 Glib::RefPtr<Gtk::Style> style = get_style ();
473 Pango::FontDescription font;
475 tmp = Pango::Layout::create (get_pango_context());
477 if (!is_realized()) {
478 font = get_font_for_style (get_name());
480 font = style->get_font();
483 tmp->set_font_description (font);
486 /* this string is the longest thing we will ever display,
487 and also includes the BBT bar char that may descends below
488 the baseline a bit, and a comma for the minsecs mode
489 where we printf a fractional value (XXX or should)
492 tmp->set_text (" 8888888888:,|");
496 tmp->set_text (" 88:88:88:88");
499 tmp->set_text (" 888|88|8888");
502 tmp->set_text (" 88:88:88,888");
505 tmp->set_text (" 8888888888");
510 tmp->get_pixel_size (req->width, req->height);
512 layout_height = req->height;
513 layout_width = req->width;
515 /* now tackle height, for which we need to know the height of the lower
523 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
524 font.set_weight (Pango::WEIGHT_NORMAL);
525 tmp->set_font_description (font);
527 /* we only care about height, so put as much stuff in here
528 as possible that might change the height.
530 tmp->set_text ("qyhH|"); /* one ascender, one descender */
532 tmp->get_pixel_size (w, info_height);
534 /* silly extra padding that seems necessary to correct the info
535 * that pango just gave us. I have no idea why.
540 req->height += info_height;
541 req->height += separator_height;
544 first_height = req->height;
545 first_width = req->width;
549 AudioClock::show_edit_status (int length)
551 editing_attr->set_start_index (edit_string.length() - length);
552 editing_attr->set_end_index (edit_string.length());
554 editing_attributes.change (*foreground_attr);
555 editing_attributes.change (*editing_attr);
557 _layout->set_attributes (editing_attributes);
561 AudioClock::start_edit (Field f)
563 pre_edit_string = _layout->get_text ();
564 if (!insert_map.empty()) {
565 edit_string = pre_edit_string;
567 edit_string.clear ();
568 _layout->set_text ("");
570 input_string.clear ();
574 input_string = get_field (f);
575 show_edit_status (merge_input_and_edit_string ());
576 _layout->set_text (edit_string);
581 Keyboard::magic_widget_grab_focus ();
586 AudioClock::get_field (Field f)
590 return edit_string.substr (1, 2);
592 case Timecode_Minutes:
593 return edit_string.substr (4, 2);
595 case Timecode_Seconds:
596 return edit_string.substr (7, 2);
598 case Timecode_Frames:
599 return edit_string.substr (10, 2);
602 return edit_string.substr (1, 2);
605 return edit_string.substr (4, 2);
608 return edit_string.substr (7, 2);
610 case MS_Milliseconds:
611 return edit_string.substr (10, 3);
614 return edit_string.substr (1, 3);
617 return edit_string.substr (5, 2);
620 return edit_string.substr (8, 4);
630 AudioClock::end_edit (bool modify)
638 ok = timecode_validate_edit (edit_string);
642 ok = bbt_validate_edit (edit_string);
646 ok = minsec_validate_edit (edit_string);
654 edit_string = pre_edit_string;
655 input_string.clear ();
656 _layout->set_text (edit_string);
657 show_edit_status (0);
658 /* edit attributes remain in use */
662 framepos_t pos = 0; /* stupid gcc */
666 pos = frames_from_timecode_string (edit_string);
671 pos = frame_duration_from_bbt_string (0, edit_string);
673 pos = frames_from_bbt_string (0, edit_string);
678 pos = frames_from_minsec_string (edit_string);
682 pos = frames_from_audioframes_string (edit_string);
687 _layout->set_attributes (normal_attributes);
688 ValueChanged(); /* EMIT_SIGNAL */
694 _layout->set_attributes (normal_attributes);
695 _layout->set_text (pre_edit_string);
706 AudioClock::drop_focus ()
708 Keyboard::magic_widget_drop_focus ();
712 /* move focus back to the default widget in the top level window */
714 Widget* top = get_toplevel();
716 if (top->is_toplevel ()) {
717 Window* win = dynamic_cast<Window*> (top);
724 AudioClock::parse_as_frames_distance (const std::string& str)
728 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
736 AudioClock::parse_as_minsec_distance (const std::string& str)
738 framecnt_t sr = _session->frame_rate();
744 switch (str.length()) {
751 sscanf (str.c_str(), "%" PRId32, &msecs);
752 return msecs * (sr / 1000);
755 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
756 return (secs * sr) + (msecs * (sr/1000));
759 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
760 return (secs * sr) + (msecs * (sr/1000));
763 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
764 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
767 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
768 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
771 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
772 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
775 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
776 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
786 AudioClock::parse_as_timecode_distance (const std::string& str)
788 double fps = _session->timecode_frames_per_second();
789 framecnt_t sr = _session->frame_rate();
795 switch (str.length()) {
800 sscanf (str.c_str(), "%" PRId32, &frames);
801 return lrint ((frames/(float)fps) * sr);
804 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
805 return (secs * sr) + lrint ((frames/(float)fps) * sr);
808 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
809 return (secs * sr) + lrint ((frames/(float)fps) * sr);
812 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
813 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
816 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
817 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
820 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
821 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
824 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
825 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
835 AudioClock::parse_as_bbt_distance (const std::string&)
841 AudioClock::parse_as_distance (const std::string& instr)
845 return parse_as_timecode_distance (instr);
848 return parse_as_frames_distance (instr);
851 return parse_as_bbt_distance (instr);
854 return parse_as_minsec_distance (instr);
861 AudioClock::end_edit_relative (bool add)
867 ok = timecode_validate_edit (edit_string);
871 ok = bbt_validate_edit (edit_string);
875 ok = minsec_validate_edit (edit_string);
883 edit_string = pre_edit_string;
884 input_string.clear ();
885 _layout->set_text (edit_string);
886 show_edit_status (0);
887 /* edit attributes remain in use */
892 framecnt_t frames = parse_as_distance (input_string);
897 _layout->set_attributes (normal_attributes);
901 set (current_time() + frames, true);
903 framepos_t c = current_time();
906 set (c - frames, true);
911 ValueChanged (); /* EMIT SIGNAL */
914 input_string.clear ();
920 AudioClock::session_configuration_changed (std::string p)
922 if (p == "sync-source" || p == "external-sync") {
923 set (current_time(), true);
927 if (p != "timecode-offset" && p != "timecode-offset-negative") {
936 current = current_duration ();
938 current = current_time ();
948 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
950 if ((!force && !is_visible()) || _session == 0) {
955 when = when - offset;
958 if (when == last_when && !force) {
959 if (_mode != Timecode) {
960 /* timecode may need to force display of TC source
961 * time, so don't return early.
969 _right_layout->set_alignment(Pango::ALIGN_LEFT);
975 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
977 set_timecode (when, force);
981 set_bbt (when, force);
985 set_minsec (when, force);
989 set_frames (when, force);
999 AudioClock::set_frames (framepos_t when, bool /*force*/)
1002 bool negative = false;
1005 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1008 _left_layout->set_text ("");
1009 _right_layout->set_text ("");
1021 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1023 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1026 _layout->set_text (buf);
1029 framecnt_t rate = _session->frame_rate();
1031 if (fmod (rate, 100.0) == 0.0) {
1032 sprintf (buf, "SR %.1fkHz", rate/1000.0);
1034 sprintf (buf, "SR %" PRId64, rate);
1037 _left_layout->set_text (buf);
1039 float vid_pullup = _session->config.get_video_pullup();
1041 if (vid_pullup == 0.0) {
1042 _right_layout->set_text (_("pullup: \u2012"));
1044 sprintf (buf, _("%+-6.4f%%"), vid_pullup);
1045 _right_layout->set_text (buf);
1051 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1059 bool negative = false;
1062 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1065 _left_layout->set_text ("");
1066 _right_layout->set_text ("");
1078 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1079 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1080 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1081 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1082 secs = (int) floor (left / (float) _session->frame_rate());
1083 left -= (framecnt_t) floor (secs * _session->frame_rate());
1084 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1087 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1089 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1092 _layout->set_text (buf);
1096 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1099 bool negative = false;
1102 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1104 _left_layout->set_text ("");
1105 _right_layout->set_text ("");
1117 _session->timecode_duration (when, TC);
1119 _session->timecode_time (when, TC);
1122 TC.negative = TC.negative || negative;
1124 _layout->set_text (Timecode::timecode_format_time(TC));
1126 if (_left_layout && _right_layout) {
1127 SyncSource sync_src = Config->get_sync_source();
1129 if (_session->config.get_external_sync()) {
1130 Slave* slave = _session->slave();
1134 _left_layout->set_text (string_compose ("%1",
1135 sync_source_to_string(sync_src, true)));
1136 _right_layout->set_text ("");
1140 _left_layout->set_text (string_compose ("%1",
1141 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1142 _right_layout->set_text (slave->approximate_current_delta());
1144 _left_layout->set_text (string_compose ("--pending--",
1145 sync_source_to_string(sync_src, true)));
1146 _right_layout->set_text ("");
1151 _left_layout->set_text (string_compose ("%1",
1152 sync_source_to_string(sync_src, true)));
1153 _right_layout->set_text (slave->approximate_current_delta());
1155 _left_layout->set_text (string_compose ("%1 --pending--",
1156 sync_source_to_string(sync_src, true)));
1157 _right_layout->set_text ("");
1162 _left_layout->set_text (string_compose ("%1",
1163 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1164 _right_layout->set_text (slave->approximate_current_delta());
1166 _left_layout->set_text (string_compose ("%1 --pending--",
1167 sync_source_to_string(sync_src, true)));
1168 _right_layout->set_text ("");
1173 _left_layout->set_text (string_compose (_("INT/%1"),
1174 sync_source_to_string(sync_src, true)));
1175 _right_layout->set_text ("");
1181 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1184 Timecode::BBT_Time BBT;
1185 bool negative = false;
1188 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1190 _left_layout->set_text ("");
1191 _right_layout->set_text ("");
1201 /* handle a common case */
1208 _session->tempo_map().bbt_time (when, BBT);
1213 _session->tempo_map().bbt_time (when, BBT);
1217 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1218 BBT.bars, BBT.beats, BBT.ticks);
1220 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1221 BBT.bars, BBT.beats, BBT.ticks);
1224 _layout->set_text (buf);
1226 if (_right_layout) {
1229 if (bbt_reference_time < 0) {
1232 pos = bbt_reference_time;
1235 TempoMetric m (_session->tempo_map().metric_at (pos));
1237 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1238 _left_layout->set_text (buf);
1240 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1241 _right_layout->set_text (buf);
1246 AudioClock::set_session (Session *s)
1248 SessionHandlePtr::set_session (s);
1252 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1254 const XMLProperty* prop;
1255 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1256 AudioClock::Mode amode;
1259 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1260 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1262 if ((prop = (*i)->property (X_("mode"))) != 0) {
1263 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1266 if ((prop = (*i)->property (X_("on"))) != 0) {
1267 set_off (!string_is_affirmative (prop->value()));
1274 set (last_when, true);
1279 AudioClock::on_key_press_event (GdkEventKey* ev)
1287 int highlight_length;
1290 switch (ev->keyval) {
1333 case GDK_KP_Subtract:
1334 end_edit_relative (false);
1340 end_edit_relative (true);
1353 ChangeAborted(); /* EMIT SIGNAL */
1358 if (!input_string.empty()) {
1359 /* delete the last key entered
1361 input_string = input_string.substr (0, input_string.length() - 1);
1363 goto use_input_string;
1369 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1370 /* too many digits: eat the key event, but do nothing with it */
1374 input_string.push_back (new_char);
1380 /* get this one in the right order, and to the right width */
1381 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1382 edit_string = edit_string.substr (0, edit_string.length() - 1);
1384 edit_string.push_back (new_char);
1386 if (!edit_string.empty()) {
1388 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1389 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1392 /* highlight the whole thing */
1393 highlight_length = edit_string.length();
1397 highlight_length = merge_input_and_edit_string ();
1400 show_edit_status (highlight_length);
1401 _layout->set_text (edit_string);
1408 AudioClock::merge_input_and_edit_string ()
1410 /* merge with pre-edit-string into edit string */
1412 edit_string = pre_edit_string;
1414 if (input_string.empty()) {
1418 string::size_type target;
1419 for (string::size_type i = 0; i < input_string.length(); ++i) {
1420 target = insert_map[input_string.length() - 1 - i];
1421 edit_string[target] = input_string[i];
1423 /* highlight from end to wherever the last character was added */
1424 return edit_string.length() - insert_map[input_string.length()-1];
1429 AudioClock::on_key_release_event (GdkEventKey *ev)
1435 /* return true for keys that we used on press
1436 so that they cannot possibly do double-duty
1438 switch (ev->keyval) {
1461 case GDK_KP_Decimal:
1469 case GDK_KP_Subtract:
1477 AudioClock::index_to_field (int index) const
1482 return Timecode_Hours;
1483 } else if (index < 7) {
1484 return Timecode_Minutes;
1485 } else if (index < 10) {
1486 return Timecode_Seconds;
1488 return Timecode_Frames;
1494 } else if (index < 7) {
1502 return Timecode_Hours;
1503 } else if (index < 6) {
1505 } else if (index < 9) {
1508 return MS_Milliseconds;
1520 AudioClock::on_button_press_event (GdkEventButton *ev)
1522 switch (ev->button) {
1524 if (editable && !_off) {
1530 /* the text has been centered vertically, so adjust
1534 y = ev->y - ((upper_height - layout_height)/2);
1535 x = ev->x - layout_x_offset;
1537 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1538 /* pretend it is a character on the far right */
1541 drag_field = index_to_field (index);
1543 /* make absolutely sure that the pointer is grabbed */
1544 gdk_pointer_grab(ev->window,false ,
1545 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1546 NULL,NULL,ev->time);
1548 drag_start_y = ev->y;
1562 AudioClock::on_button_release_event (GdkEventButton *ev)
1564 if (editable && !_off) {
1566 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1568 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1569 // we actually dragged so return without
1570 // setting editing focus, or we shift clicked
1573 if (ev->button == 1) {
1575 if (_edit_by_click_field) {
1579 int y = ev->y - ((upper_height - layout_height)/2);
1580 int x = ev->x - layout_x_offset;
1583 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1587 f = index_to_field (index);
1590 case Timecode_Frames:
1591 case MS_Milliseconds:
1607 if (Keyboard::is_context_menu_event (ev)) {
1608 if (ops_menu == 0) {
1611 ops_menu->popup (1, ev->time);
1619 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1621 bool ret = CairoWidget::on_focus_out_event (ev);
1631 AudioClock::on_scroll_event (GdkEventScroll *ev)
1636 if (editing || _session == 0 || !editable || _off) {
1643 /* the text has been centered vertically, so adjust
1647 y = ev->y - ((upper_height - layout_height)/2);
1648 x = ev->x - layout_x_offset;
1650 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1651 /* not in the main layout */
1655 Field f = index_to_field (index);
1656 framepos_t frames = 0;
1658 switch (ev->direction) {
1661 frames = get_frame_step (f);
1663 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1666 set (current_time() + frames, true);
1667 ValueChanged (); /* EMIT_SIGNAL */
1671 case GDK_SCROLL_DOWN:
1672 frames = get_frame_step (f);
1674 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1678 if ((double)current_time() - (double)frames < 0.0) {
1681 set (current_time() - frames, true);
1684 ValueChanged (); /* EMIT_SIGNAL */
1697 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1699 if (editing || _session == 0 || !dragging) {
1703 float pixel_frame_scale_factor = 0.2f;
1705 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1706 pixel_frame_scale_factor = 0.1f;
1710 if (Keyboard::modifier_state_contains (ev->state,
1711 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1713 pixel_frame_scale_factor = 0.025f;
1716 double y_delta = ev->y - drag_y;
1718 drag_accum += y_delta*pixel_frame_scale_factor;
1722 if (trunc (drag_accum) != 0) {
1727 dir = (drag_accum < 0 ? 1:-1);
1728 pos = current_time();
1729 frames = get_frame_step (drag_field, pos, dir);
1731 if (frames != 0 && frames * drag_accum < current_time()) {
1732 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1738 ValueChanged(); /* EMIT_SIGNAL */
1745 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1748 Timecode::BBT_Time BBT;
1750 case Timecode_Hours:
1751 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1753 case Timecode_Minutes:
1754 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1756 case Timecode_Seconds:
1757 f = _session->frame_rate();
1759 case Timecode_Frames:
1760 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1768 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1771 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1774 f = (framecnt_t) _session->frame_rate();
1776 case MS_Milliseconds:
1777 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1784 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1790 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1796 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1799 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1808 AudioClock::current_time (framepos_t) const
1814 AudioClock::current_duration (framepos_t pos) const
1823 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1839 AudioClock::bbt_validate_edit (const string& str)
1843 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1847 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1851 if (!is_duration && any.bbt.bars == 0) {
1855 if (!is_duration && any.bbt.beats == 0) {
1863 AudioClock::timecode_validate_edit (const string&)
1867 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1868 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1872 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1876 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1880 if (_session->timecode_drop_frames()) {
1881 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1890 AudioClock::minsec_validate_edit (const string& str)
1892 int hrs, mins, secs, millisecs;
1894 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1898 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1906 AudioClock::frames_from_timecode_string (const string& str) const
1908 if (_session == 0) {
1915 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1916 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1920 TC.rate = _session->timecode_frames_per_second();
1921 TC.drop= _session->timecode_drop_frames();
1923 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1925 // timecode_tester ();
1931 AudioClock::frames_from_minsec_string (const string& str) const
1933 if (_session == 0) {
1937 int hrs, mins, secs, millisecs;
1938 framecnt_t sr = _session->frame_rate();
1940 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1941 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1945 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1949 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1951 if (_session == 0) {
1952 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1957 any.type = AnyTime::BBT;
1959 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1966 return _session->any_duration_to_frames (pos, any);
1968 return _session->convert_to_frames (any);
1974 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1976 if (_session == 0) {
1977 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1981 Timecode::BBT_Time bbt;
1983 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1987 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1991 AudioClock::frames_from_audioframes_string (const string& str) const
1994 sscanf (str.c_str(), "%" PRId64, &f);
1999 AudioClock::build_ops_menu ()
2001 using namespace Menu_Helpers;
2002 ops_menu = new Menu;
2003 MenuList& ops_items = ops_menu->items();
2004 ops_menu->set_name ("ArdourContextMenu");
2006 if (!Profile->get_sae()) {
2007 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2009 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2010 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2011 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2013 if (editable && !_off && !is_duration && !_follows_playhead) {
2014 ops_items.push_back (SeparatorElem());
2015 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2016 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2021 AudioClock::set_from_playhead ()
2027 set (_session->transport_frame());
2032 AudioClock::locate ()
2034 if (!_session || is_duration) {
2038 _session->request_locate (current_time(), _session->transport_rolling ());
2042 AudioClock::set_mode (Mode m)
2052 _layout->set_text ("");
2056 if (_mode == Timecode) {
2057 _left_layout->set_attributes (small_info_attributes);
2058 _right_layout->set_attributes (small_info_attributes);
2060 _left_layout->set_attributes (info_attributes);
2061 _right_layout->set_attributes (info_attributes);
2063 /* adjust info_height according to font size */
2065 _left_layout->set_text (" 1234567890");
2066 _left_layout->get_pixel_size (ignored, info_height);
2069 _left_layout->set_text ("");
2070 _right_layout->set_text ("");
2075 mode_based_info_ratio = 0.5;
2076 insert_map.push_back (11);
2077 insert_map.push_back (10);
2078 insert_map.push_back (8);
2079 insert_map.push_back (7);
2080 insert_map.push_back (5);
2081 insert_map.push_back (4);
2082 insert_map.push_back (2);
2083 insert_map.push_back (1);
2087 mode_based_info_ratio = 0.5;
2088 insert_map.push_back (11);
2089 insert_map.push_back (10);
2090 insert_map.push_back (9);
2091 insert_map.push_back (8);
2092 insert_map.push_back (6);
2093 insert_map.push_back (5);
2094 insert_map.push_back (3);
2095 insert_map.push_back (2);
2096 insert_map.push_back (1);
2100 mode_based_info_ratio = 1.0;
2101 insert_map.push_back (12);
2102 insert_map.push_back (11);
2103 insert_map.push_back (10);
2104 insert_map.push_back (8);
2105 insert_map.push_back (7);
2106 insert_map.push_back (5);
2107 insert_map.push_back (4);
2108 insert_map.push_back (2);
2109 insert_map.push_back (1);
2113 mode_based_info_ratio = 0.5;
2117 set (last_when, true);
2119 if (!is_transient) {
2120 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2123 if (!_fixed_width) {
2124 /* display is different, allow us to resize */
2130 mode_changed (); /* EMIT SIGNAL (the member one) */
2134 AudioClock::set_bbt_reference (framepos_t pos)
2136 bbt_reference_time = pos;
2140 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2142 CairoWidget::on_style_changed (old_style);
2148 AudioClock::set_editable (bool yn)
2154 AudioClock::set_is_duration (bool yn)
2156 if (yn == is_duration) {
2161 set (last_when, true);
2165 AudioClock::set_off (bool yn)
2173 /* force a redraw. last_when will be preserved, but the clock text will
2177 set (last_when, true);
2181 AudioClock::focus ()
2183 start_edit (Field (0));
2187 AudioClock::set_corner_radius (double r)
2194 AudioClock::set_fixed_width (bool yn)
2200 AudioClock::dpi_reset ()
2202 /* force recomputation of size even if we are fixed width