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;
48 using namespace ARDOUR_UI_UTILS;
53 using Gtkmm2ext::Keyboard;
55 sigc::signal<void> AudioClock::ModeChanged;
56 vector<AudioClock*> AudioClock::clocks;
57 const double AudioClock::info_font_scale_factor = 0.50;
58 const double AudioClock::separator_height = 0.0;
59 const double AudioClock::x_leading_padding = 6.0;
61 #define BBT_BAR_CHAR "|"
62 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
63 #define INFO_FONT_SIZE ((int)lrint(font_size * info_font_scale_factor))
64 #define TXTSPAN "<span font-family=\"Sans\" foreground=\"white\">"
66 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
67 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
70 , is_transient (transient)
71 , is_duration (duration)
72 , editable (allow_edit)
73 , _follows_playhead (follows_playhead)
76 , _edit_by_click_field (false)
77 , _negative_allowed (false)
78 , edit_is_negative (false)
83 , style_resets_first (true)
88 , mode_based_info_ratio (1.0)
92 , bbt_reference_time (-1)
97 , drag_field (Field (0))
99 set_flags (CAN_FOCUS);
101 _layout = Pango::Layout::create (get_pango_context());
102 _layout->set_attributes (normal_attributes);
105 _left_layout = Pango::Layout::create (get_pango_context());
106 _right_layout = Pango::Layout::create (get_pango_context());
109 set_widget_name (widget_name);
111 _mode = BBT; /* lie to force mode switch */
113 set (last_when, true);
116 clocks.push_back (this);
119 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
120 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
123 AudioClock::~AudioClock ()
125 delete foreground_attr;
130 AudioClock::set_widget_name (const string& str)
135 set_name (str + " clock");
145 AudioClock::on_realize ()
147 Gtk::Requisition req;
149 CairoWidget::on_realize ();
151 set_clock_dimensions (req);
153 first_width = req.width;
154 first_height = req.height;
161 AudioClock::set_font ()
163 Glib::RefPtr<Gtk::Style> style = get_style ();
164 Pango::FontDescription font;
165 Pango::AttrFontDesc* font_attr;
167 if (!is_realized()) {
168 font = get_font_for_style (get_name());
170 font = style->get_font();
173 font_size = font.get_size();
175 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
177 normal_attributes.change (*font_attr);
178 editing_attributes.change (*font_attr);
180 /* now a smaller version of the same font */
183 font.set_size (INFO_FONT_SIZE);
184 font.set_weight (Pango::WEIGHT_NORMAL);
185 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
187 info_attributes.change (*font_attr);
189 /* and an even smaller one */
193 /* get the figure width for the font. This doesn't have to super
194 * accurate since we only use it to measure the (roughly 1 character)
195 * offset from the position Pango tells us for the "cursor"
198 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
202 tmp->get_pixel_size (em_width, ignore_height);
204 /* force redraw of markup with new font-size */
205 set (last_when, true);
209 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
211 CairoWidget::set_active_state (s);
216 AudioClock::set_colors ()
222 uint32_t editing_color;
223 uint32_t cursor_color;
225 if (active_state()) {
226 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
227 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
228 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
229 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
231 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
232 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
233 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
234 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
237 /* store for bg and cursor in render() */
239 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
246 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
253 /* rescale for Pango colors ... sigh */
255 r = lrint (r * 65535.0);
256 g = lrint (g * 65535.0);
257 b = lrint (b * 65535.0);
259 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
260 r = lrint ((r/255.0) * 65535.0);
261 g = lrint ((g/255.0) * 65535.0);
262 b = lrint ((b/255.0) * 65535.0);
263 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
265 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
266 r = lrint ((r/255.0) * 65535.0);
267 g = lrint ((g/255.0) * 65535.0);
268 b = lrint ((b/255.0) * 65535.0);
269 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
271 normal_attributes.change (*foreground_attr);
272 info_attributes.change (*foreground_attr);
273 editing_attributes.change (*foreground_attr);
274 editing_attributes.change (*editing_attr);
277 _layout->set_attributes (normal_attributes);
279 _layout->set_attributes (editing_attributes);
286 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
288 /* main layout: rounded rect, plus the text */
291 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
294 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
296 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
299 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
304 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
306 pango_cairo_show_layout (cr, _layout->gobj());
310 double h = get_height() - upper_height - separator_height;
313 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
316 if (mode_based_info_ratio != 1.0) {
318 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
322 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
323 left_rect_width + (separator_height == 0 ? corner_radius : 0),
326 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
331 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
332 pango_cairo_show_layout (cr, _left_layout->gobj());
336 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
337 upper_height + separator_height,
338 get_width() - separator_height - left_rect_width,
341 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
342 get_width() - separator_height - left_rect_width, h);
348 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
349 /* right-align does not work per se beacuse layout width is unset.
350 * Using _right_layout->set_width([value >=0]) would also enable
351 * word-wrapping which is not wanted here.
352 * The solution is to custom align the layout depending on its size.
353 * if it is larger than the available space it will be cropped on the
354 * right edge rather than override text on the left side.
357 _right_layout->get_pixel_size(rw, rh);
358 x = get_width() - rw - separator_height - x_leading_padding;
359 if (x < x_leading_padding + left_rect_width + separator_height) {
360 /* rather cut off the right end than overlap with the text on the left */
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_bottom_half_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()) {
386 int xcenter = (get_width() - layout_width) /2;
388 if (input_string.length() < insert_map.size()) {
389 Pango::Rectangle cursor;
391 if (input_string.empty()) {
392 /* nothing entered yet, put cursor at the end
395 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
397 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
400 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
402 min (get_width() - 2.0,
403 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
404 (upper_height - layout_height)/2.0,
405 2.0, cursor.get_height()/PANGO_SCALE);
408 /* we've entered all possible digits, no cursor */
412 if (input_string.empty()) {
413 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
416 (upper_height - layout_height)/2.0,
425 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
427 CairoWidget::on_size_allocate (alloc);
430 upper_height = (get_height()/2.0) - 1.0;
432 upper_height = get_height();
437 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
439 Glib::RefPtr<Pango::Layout> tmp;
440 Glib::RefPtr<Gtk::Style> style = get_style ();
441 Pango::FontDescription font;
443 tmp = Pango::Layout::create (get_pango_context());
445 if (!is_realized()) {
446 font = get_font_for_style (get_name());
448 font = style->get_font();
451 tmp->set_font_description (font);
453 /* this string is the longest thing we will ever display */
454 tmp->set_text (" 88:88:88,888");
455 tmp->get_pixel_size (req.width, req.height);
457 layout_height = req.height;
458 layout_width = req.width;
462 AudioClock::on_size_request (Gtk::Requisition* req)
464 /* even for non fixed width clocks, the size we *ask* for never changes,
465 even though the size we receive might. so once we've computed it,
470 req->width = first_width;
471 req->height = first_height;
475 set_clock_dimensions (*req);
477 /* now tackle height, for which we need to know the height of the lower
483 Glib::RefPtr<Pango::Layout> tmp;
484 Glib::RefPtr<Gtk::Style> style = get_style ();
485 Pango::FontDescription font;
488 tmp = Pango::Layout::create (get_pango_context());
490 if (!is_realized()) {
491 font = get_font_for_style (get_name());
493 font = style->get_font();
496 tmp->set_font_description (font);
498 font.set_size (INFO_FONT_SIZE);
499 font.set_weight (Pango::WEIGHT_NORMAL);
500 tmp->set_font_description (font);
502 /* we only care about height, so put as much stuff in here
503 as possible that might change the height.
505 tmp->set_text ("qyhH|"); /* one ascender, one descender */
507 tmp->get_pixel_size (w, info_height);
509 /* silly extra padding that seems necessary to correct the info
510 * that pango just gave us. I have no idea why.
513 req->height += info_height;
514 req->height += separator_height;
519 AudioClock::show_edit_status (int length)
521 editing_attr->set_start_index (edit_string.length() - length);
522 editing_attr->set_end_index (edit_string.length());
524 editing_attributes.change (*foreground_attr);
525 editing_attributes.change (*editing_attr);
527 _layout->set_attributes (editing_attributes);
531 AudioClock::start_edit (Field f)
534 pre_edit_string = _layout->get_text ();
535 if (!insert_map.empty()) {
536 edit_string = pre_edit_string;
538 edit_string.clear ();
539 _layout->set_text ("");
542 input_string.clear ();
544 edit_is_negative = false;
547 input_string = get_field (f);
548 show_edit_status (merge_input_and_edit_string ());
549 _layout->set_text (edit_string);
554 Keyboard::magic_widget_grab_focus ();
560 AudioClock::get_field (Field f)
564 return edit_string.substr (1, 2);
566 case Timecode_Minutes:
567 return edit_string.substr (4, 2);
569 case Timecode_Seconds:
570 return edit_string.substr (7, 2);
572 case Timecode_Frames:
573 return edit_string.substr (10, 2);
576 return edit_string.substr (1, 2);
579 return edit_string.substr (4, 2);
582 return edit_string.substr (7, 2);
584 case MS_Milliseconds:
585 return edit_string.substr (10, 3);
588 return edit_string.substr (1, 3);
591 return edit_string.substr (5, 2);
594 return edit_string.substr (8, 4);
604 AudioClock::end_edit (bool modify)
612 ok = timecode_validate_edit (edit_string);
616 ok = bbt_validate_edit (edit_string);
620 ok = minsec_validate_edit (edit_string);
624 if (edit_string.length() < 1) {
625 edit_string = pre_edit_string;
631 edit_string = pre_edit_string;
632 input_string.clear ();
633 _layout->set_text (edit_string);
634 show_edit_status (0);
635 /* edit attributes remain in use */
639 framepos_t pos = 0; /* stupid gcc */
643 pos = frames_from_timecode_string (edit_string);
648 pos = frame_duration_from_bbt_string (0, edit_string);
650 pos = frames_from_bbt_string (0, edit_string);
655 pos = frames_from_minsec_string (edit_string);
659 pos = frames_from_audioframes_string (edit_string);
664 _layout->set_attributes (normal_attributes);
665 ValueChanged(); /* EMIT_SIGNAL */
671 edit_is_negative = false;
672 _layout->set_attributes (normal_attributes);
673 _layout->set_text (pre_edit_string);
684 AudioClock::drop_focus ()
686 Keyboard::magic_widget_drop_focus ();
690 /* move focus back to the default widget in the top level window */
692 Widget* top = get_toplevel();
694 if (top->is_toplevel ()) {
695 Window* win = dynamic_cast<Window*> (top);
702 AudioClock::parse_as_frames_distance (const std::string& str)
706 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
714 AudioClock::parse_as_minsec_distance (const std::string& str)
716 framecnt_t sr = _session->frame_rate();
722 switch (str.length()) {
729 sscanf (str.c_str(), "%" PRId32, &msecs);
730 return msecs * (sr / 1000);
733 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
734 return (secs * sr) + (msecs * (sr/1000));
737 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
738 return (secs * sr) + (msecs * (sr/1000));
741 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
742 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
745 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
746 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
749 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
750 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
753 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
754 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
764 AudioClock::parse_as_timecode_distance (const std::string& str)
766 double fps = _session->timecode_frames_per_second();
767 framecnt_t sr = _session->frame_rate();
773 switch (str.length()) {
778 sscanf (str.c_str(), "%" PRId32, &frames);
779 return llrint ((frames/(float)fps) * sr);
782 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
783 return (secs * sr) + llrint ((frames/(float)fps) * sr);
786 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
787 return (secs * sr) + llrint ((frames/(float)fps) * sr);
790 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
791 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
794 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
795 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
798 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
799 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
802 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
803 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
813 AudioClock::parse_as_bbt_distance (const std::string&)
819 AudioClock::parse_as_distance (const std::string& instr)
823 return parse_as_timecode_distance (instr);
826 return parse_as_frames_distance (instr);
829 return parse_as_bbt_distance (instr);
832 return parse_as_minsec_distance (instr);
839 AudioClock::end_edit_relative (bool add)
845 ok = timecode_validate_edit (edit_string);
849 ok = bbt_validate_edit (edit_string);
853 ok = minsec_validate_edit (edit_string);
861 edit_string = pre_edit_string;
862 input_string.clear ();
863 _layout->set_text (edit_string);
864 show_edit_status (0);
865 /* edit attributes remain in use */
870 framecnt_t frames = parse_as_distance (input_string);
875 _layout->set_attributes (normal_attributes);
879 set (current_time() + frames, true);
881 framepos_t c = current_time();
883 if (c > frames || _negative_allowed) {
884 set (c - frames, true);
889 ValueChanged (); /* EMIT SIGNAL */
892 input_string.clear ();
898 AudioClock::session_property_changed (const PropertyChange&)
900 set (last_when, true);
904 AudioClock::session_configuration_changed (std::string p)
906 if (_negative_allowed) {
907 /* session option editor clock */
911 if (p == "sync-source" || p == "external-sync") {
912 set (current_time(), true);
916 if (p != "timecode-offset" && p != "timecode-offset-negative") {
925 current = current_duration ();
927 current = current_time ();
937 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
939 if ((!force && !is_visible()) || _session == 0) {
944 when = when - offset;
947 if (when == last_when && !force) {
948 #if 0 // XXX return if no change and no change forced. verify Aug/2014
949 if (_mode != Timecode && _mode != MinSec) {
950 /* may need to force display of TC source
951 * time, so don't return early.
953 /* ^^ Why was that?, delta times?
954 * Timecode FPS, pull-up/down, etc changes
955 * trigger a 'session_property_changed' which
956 * eventually calls set(last_when, true)
958 * re-rendering the clock every 40ms or so just
959 * because we can is not ideal.
970 _right_layout->set_alignment(Pango::ALIGN_LEFT);
976 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
978 set_timecode (when, force);
982 set_bbt (when, force);
987 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
989 set_minsec (when, force);
993 set_frames (when, force);
1003 AudioClock::set_slave_info ()
1005 if (!_left_layout || !_right_layout) {
1009 SyncSource sync_src = Config->get_sync_source();
1011 if (_session->config.get_external_sync()) {
1012 Slave* slave = _session->slave();
1016 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1017 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1018 _right_layout->set_text ("");
1022 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1023 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1024 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1025 INFO_FONT_SIZE, slave->approximate_current_delta()));
1027 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1028 INFO_FONT_SIZE, _("--pending--")));
1029 _right_layout->set_text ("");
1036 TimecodeSlave* tcslave;
1037 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1038 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1039 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1040 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1041 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1042 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1043 INFO_FONT_SIZE, slave->approximate_current_delta()));
1046 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1047 INFO_FONT_SIZE, _("--pending--")));
1048 _right_layout->set_text ("");
1053 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1054 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1055 _right_layout->set_text ("");
1060 AudioClock::set_frames (framepos_t when, bool /*force*/)
1063 bool negative = false;
1066 _layout->set_text (" ----------");
1069 _left_layout->set_text ("");
1070 _right_layout->set_text ("");
1082 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1084 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1087 _layout->set_text (buf);
1090 framecnt_t rate = _session->frame_rate();
1092 if (fmod (rate, 100.0) == 0.0) {
1093 sprintf (buf, "%.1fkHz", rate/1000.0);
1095 sprintf (buf, "%" PRId64 "Hz", rate);
1098 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1099 INFO_FONT_SIZE, _("SR"), buf));
1101 float vid_pullup = _session->config.get_video_pullup();
1103 if (vid_pullup == 0.0) {
1104 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1105 INFO_FONT_SIZE, _("Pull")));
1107 sprintf (buf, _("%+.4f%%"), vid_pullup);
1108 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1109 INFO_FONT_SIZE, _("Pull"), buf));
1115 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1132 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1133 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1134 mins = (int) floor (left / (frame_rate * 60.0f));
1135 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1136 secs = (int) floor (left / (float) frame_rate);
1137 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1138 millisecs = floor (left * 1000.0 / (float) frame_rate);
1141 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1143 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1149 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1154 _layout->set_text (" --:--:--.---");
1157 _left_layout->set_text ("");
1158 _right_layout->set_text ("");
1164 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1166 _layout->set_text (buf);
1171 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1174 bool negative = false;
1177 _layout->set_text (" --:--:--:--");
1179 _left_layout->set_text ("");
1180 _right_layout->set_text ("");
1192 _session->timecode_duration (when, TC);
1194 _session->timecode_time (when, TC);
1197 TC.negative = TC.negative || negative;
1199 _layout->set_text (Timecode::timecode_format_time(TC));
1205 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1208 Timecode::BBT_Time BBT;
1209 bool negative = false;
1212 _layout->set_text (" ---|--|----");
1214 _left_layout->set_text ("");
1215 _right_layout->set_text ("");
1225 /* handle a common case */
1232 _session->tempo_map().bbt_time (when, BBT);
1237 _session->tempo_map().bbt_time (when, BBT);
1241 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1242 BBT.bars, BBT.beats, BBT.ticks);
1244 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1245 BBT.bars, BBT.beats, BBT.ticks);
1248 _layout->set_text (buf);
1250 if (_right_layout) {
1253 if (bbt_reference_time < 0) {
1256 pos = bbt_reference_time;
1259 TempoMetric m (_session->tempo_map().metric_at (pos));
1261 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1262 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1263 INFO_FONT_SIZE, buf, _("Tempo")));
1265 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1266 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1267 INFO_FONT_SIZE, buf, _("Meter")));
1272 AudioClock::set_session (Session *s)
1274 SessionHandlePtr::set_session (s);
1278 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1279 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1281 const XMLProperty* prop;
1282 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1283 AudioClock::Mode amode;
1286 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1287 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1289 if ((prop = (*i)->property (X_("mode"))) != 0) {
1290 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1293 if ((prop = (*i)->property (X_("on"))) != 0) {
1294 set_off (!string_is_affirmative (prop->value()));
1301 set (last_when, true);
1306 AudioClock::on_key_press_event (GdkEventKey* ev)
1314 int highlight_length;
1317 switch (ev->keyval) {
1360 case GDK_KP_Subtract:
1361 if (_negative_allowed && input_string.empty()) {
1362 edit_is_negative = true;
1363 edit_string.replace(0,1,"-");
1364 _layout->set_text (edit_string);
1367 end_edit_relative (false);
1373 end_edit_relative (true);
1386 ChangeAborted(); /* EMIT SIGNAL */
1391 if (!input_string.empty()) {
1392 /* delete the last key entered
1394 input_string = input_string.substr (0, input_string.length() - 1);
1396 goto use_input_string;
1402 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1403 /* too many digits: eat the key event, but do nothing with it */
1407 input_string.push_back (new_char);
1413 /* get this one in the right order, and to the right width */
1414 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1415 edit_string = edit_string.substr (0, edit_string.length() - 1);
1417 edit_string.push_back (new_char);
1419 if (!edit_string.empty()) {
1421 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1422 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1425 /* highlight the whole thing */
1426 highlight_length = edit_string.length();
1430 highlight_length = merge_input_and_edit_string ();
1433 if (edit_is_negative) {
1434 edit_string.replace(0,1,"-");
1436 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1437 edit_string.replace(0,1,"_");
1439 edit_string.replace(0,1," ");
1443 show_edit_status (highlight_length);
1444 _layout->set_text (edit_string);
1451 AudioClock::merge_input_and_edit_string ()
1453 /* merge with pre-edit-string into edit string */
1455 edit_string = pre_edit_string;
1457 if (input_string.empty()) {
1461 string::size_type target;
1462 for (string::size_type i = 0; i < input_string.length(); ++i) {
1463 target = insert_map[input_string.length() - 1 - i];
1464 edit_string[target] = input_string[i];
1466 /* highlight from end to wherever the last character was added */
1467 return edit_string.length() - insert_map[input_string.length()-1];
1472 AudioClock::on_key_release_event (GdkEventKey *ev)
1478 /* return true for keys that we used on press
1479 so that they cannot possibly do double-duty
1481 switch (ev->keyval) {
1504 case GDK_KP_Decimal:
1512 case GDK_KP_Subtract:
1520 AudioClock::index_to_field (int index) const
1525 return Timecode_Hours;
1526 } else if (index < 7) {
1527 return Timecode_Minutes;
1528 } else if (index < 10) {
1529 return Timecode_Seconds;
1531 return Timecode_Frames;
1537 } else if (index < 7) {
1545 return Timecode_Hours;
1546 } else if (index < 6) {
1548 } else if (index < 9) {
1551 return MS_Milliseconds;
1563 AudioClock::on_button_press_event (GdkEventButton *ev)
1565 switch (ev->button) {
1567 if (editable && !_off) {
1573 /* the text has been centered vertically, so adjust
1576 int xcenter = (get_width() - layout_width) /2;
1578 y = ev->y - ((upper_height - layout_height)/2);
1579 x = ev->x - xcenter;
1581 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1582 /* pretend it is a character on the far right */
1585 drag_field = index_to_field (index);
1587 /* make absolutely sure that the pointer is grabbed */
1588 gdk_pointer_grab(ev->window,false ,
1589 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1590 NULL,NULL,ev->time);
1592 drag_start_y = ev->y;
1606 AudioClock::on_button_release_event (GdkEventButton *ev)
1608 if (editable && !_off) {
1610 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1612 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1613 // we actually dragged so return without
1614 // setting editing focus, or we shift clicked
1617 if (ev->button == 1) {
1619 if (_edit_by_click_field) {
1621 int xcenter = (get_width() - layout_width) /2;
1624 int y = ev->y - ((upper_height - layout_height)/2);
1625 int x = ev->x - xcenter;
1628 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1632 f = index_to_field (index);
1635 case Timecode_Frames:
1636 case MS_Milliseconds:
1652 if (Keyboard::is_context_menu_event (ev)) {
1653 if (ops_menu == 0) {
1656 ops_menu->popup (1, ev->time);
1664 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1666 bool ret = CairoWidget::on_focus_out_event (ev);
1676 AudioClock::on_scroll_event (GdkEventScroll *ev)
1681 if (editing || _session == 0 || !editable || _off) {
1688 /* the text has been centered vertically, so adjust
1692 int xcenter = (get_width() - layout_width) /2;
1693 y = ev->y - ((upper_height - layout_height)/2);
1694 x = ev->x - xcenter;
1696 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1697 /* not in the main layout */
1701 Field f = index_to_field (index);
1702 framepos_t frames = 0;
1704 switch (ev->direction) {
1707 frames = get_frame_step (f);
1709 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1712 set (current_time() + frames, true);
1713 ValueChanged (); /* EMIT_SIGNAL */
1717 case GDK_SCROLL_DOWN:
1718 frames = get_frame_step (f);
1720 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1724 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1727 set (current_time() - frames, true);
1730 ValueChanged (); /* EMIT_SIGNAL */
1743 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1745 if (editing || _session == 0 || !dragging) {
1749 float pixel_frame_scale_factor = 0.2f;
1751 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1752 pixel_frame_scale_factor = 0.1f;
1756 if (Keyboard::modifier_state_contains (ev->state,
1757 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1759 pixel_frame_scale_factor = 0.025f;
1762 double y_delta = ev->y - drag_y;
1764 drag_accum += y_delta*pixel_frame_scale_factor;
1768 if (floor (drag_accum) != 0) {
1773 dir = (drag_accum < 0 ? 1:-1);
1774 pos = current_time();
1775 frames = get_frame_step (drag_field, pos, dir);
1777 if (frames != 0 && frames * drag_accum < current_time()) {
1778 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1784 ValueChanged(); /* EMIT_SIGNAL */
1791 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1794 Timecode::BBT_Time BBT;
1796 case Timecode_Hours:
1797 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1799 case Timecode_Minutes:
1800 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1802 case Timecode_Seconds:
1803 f = _session->frame_rate();
1805 case Timecode_Frames:
1806 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1814 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1817 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1820 f = (framecnt_t) _session->frame_rate();
1822 case MS_Milliseconds:
1823 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1830 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1836 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1842 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1845 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1854 AudioClock::current_time (framepos_t) const
1860 AudioClock::current_duration (framepos_t pos) const
1869 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1885 AudioClock::bbt_validate_edit (const string& str)
1889 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1893 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1897 if (!is_duration && any.bbt.bars == 0) {
1901 if (!is_duration && any.bbt.beats == 0) {
1909 AudioClock::timecode_validate_edit (const string&)
1915 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1916 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1921 TC.hours = hours * -1;
1925 TC.negative = false;
1928 if (TC.negative && !_negative_allowed) {
1932 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1936 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1940 if (_session->timecode_drop_frames()) {
1941 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1950 AudioClock::minsec_validate_edit (const string& str)
1952 int hrs, mins, secs, millisecs;
1954 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1958 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1966 AudioClock::frames_from_timecode_string (const string& str) const
1968 if (_session == 0) {
1977 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1978 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1981 TC.hours = abs(hours);
1982 TC.rate = _session->timecode_frames_per_second();
1983 TC.drop= _session->timecode_drop_frames();
1985 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1987 // timecode_tester ();
1988 if (edit_is_negative) {
1996 AudioClock::frames_from_minsec_string (const string& str) const
1998 if (_session == 0) {
2002 int hrs, mins, secs, millisecs;
2003 framecnt_t sr = _session->frame_rate();
2005 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2006 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2010 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2014 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2016 if (_session == 0) {
2017 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2022 any.type = AnyTime::BBT;
2024 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2031 return _session->any_duration_to_frames (pos, any);
2033 return _session->convert_to_frames (any);
2039 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2041 if (_session == 0) {
2042 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2046 Timecode::BBT_Time bbt;
2048 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2052 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2056 AudioClock::frames_from_audioframes_string (const string& str) const
2059 sscanf (str.c_str(), "%" PRId64, &f);
2064 AudioClock::build_ops_menu ()
2066 using namespace Menu_Helpers;
2067 ops_menu = new Menu;
2068 MenuList& ops_items = ops_menu->items();
2069 ops_menu->set_name ("ArdourContextMenu");
2071 if (!Profile->get_sae()) {
2072 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2074 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2075 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2076 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2078 if (editable && !_off && !is_duration && !_follows_playhead) {
2079 ops_items.push_back (SeparatorElem());
2080 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2081 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2086 AudioClock::set_from_playhead ()
2092 set (_session->transport_frame());
2097 AudioClock::locate ()
2099 if (!_session || is_duration) {
2103 _session->request_locate (current_time(), _session->transport_rolling ());
2107 AudioClock::set_mode (Mode m)
2117 _layout->set_text ("");
2121 _left_layout->set_attributes (info_attributes);
2122 _right_layout->set_attributes (info_attributes);
2123 /* adjust info_height according to font size */
2125 _left_layout->set_text (" 1234567890");
2126 _left_layout->get_pixel_size (ignored, info_height);
2128 _left_layout->set_text ("");
2129 _right_layout->set_text ("");
2134 mode_based_info_ratio = 0.6;
2135 insert_map.push_back (11);
2136 insert_map.push_back (10);
2137 insert_map.push_back (8);
2138 insert_map.push_back (7);
2139 insert_map.push_back (5);
2140 insert_map.push_back (4);
2141 insert_map.push_back (2);
2142 insert_map.push_back (1);
2146 mode_based_info_ratio = 0.5;
2147 insert_map.push_back (11);
2148 insert_map.push_back (10);
2149 insert_map.push_back (9);
2150 insert_map.push_back (8);
2151 insert_map.push_back (6);
2152 insert_map.push_back (5);
2153 insert_map.push_back (3);
2154 insert_map.push_back (2);
2155 insert_map.push_back (1);
2159 mode_based_info_ratio = 0.6;
2160 insert_map.push_back (12);
2161 insert_map.push_back (11);
2162 insert_map.push_back (10);
2163 insert_map.push_back (8);
2164 insert_map.push_back (7);
2165 insert_map.push_back (5);
2166 insert_map.push_back (4);
2167 insert_map.push_back (2);
2168 insert_map.push_back (1);
2172 mode_based_info_ratio = 0.45;
2176 set (last_when, true);
2178 if (!is_transient) {
2179 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2182 mode_changed (); /* EMIT SIGNAL (the member one) */
2186 AudioClock::set_bbt_reference (framepos_t pos)
2188 bbt_reference_time = pos;
2192 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2194 CairoWidget::on_style_changed (old_style);
2196 Gtk::Requisition req;
2197 set_clock_dimensions (req);
2204 AudioClock::set_editable (bool yn)
2210 AudioClock::set_is_duration (bool yn)
2212 if (yn == is_duration) {
2217 set (last_when, true);
2221 AudioClock::set_off (bool yn)
2229 /* force a redraw. last_when will be preserved, but the clock text will
2233 set (last_when, true);
2237 AudioClock::focus ()
2239 start_edit (Field (0));
2243 AudioClock::set_corner_radius (double r)
2252 AudioClock::dpi_reset ()
2254 /* force recomputation of size even if we are fixed width
2262 AudioClock::set_negative_allowed (bool yn)
2264 _negative_allowed = yn;