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/lmath.h"
35 #include "ardour/session.h"
36 #include "ardour/slave.h"
37 #include "ardour/tempo.h"
38 #include "ardour/types.h"
40 #include "ardour_ui.h"
41 #include "audio_clock.h"
42 #include "global_signals.h"
45 #include "gui_thread.h"
48 using namespace ARDOUR;
49 using namespace ARDOUR_UI_UTILS;
54 using Gtkmm2ext::Keyboard;
56 sigc::signal<void> AudioClock::ModeChanged;
57 vector<AudioClock*> AudioClock::clocks;
58 const double AudioClock::info_font_scale_factor = 0.68;
59 const double AudioClock::separator_height = 0.0;
60 const double AudioClock::x_leading_padding = 6.0;
62 #define BBT_BAR_CHAR "|"
63 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
64 #define INFO_FONT_SIZE ((int)lrint(font_size * info_font_scale_factor))
65 #define TXTSPAN "<span font-family=\"Sans\" foreground=\"white\">"
67 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
68 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
71 , is_transient (transient)
72 , is_duration (duration)
73 , editable (allow_edit)
74 , _follows_playhead (follows_playhead)
77 , _edit_by_click_field (false)
78 , _negative_allowed (false)
79 , edit_is_negative (false)
84 , style_resets_first (true)
89 , mode_based_info_ratio (1.0)
93 , bbt_reference_time (-1)
98 , drag_field (Field (0))
102 set_flags (CAN_FOCUS);
104 _layout = Pango::Layout::create (get_pango_context());
105 _layout->set_attributes (normal_attributes);
108 _left_layout = Pango::Layout::create (get_pango_context());
109 _right_layout = Pango::Layout::create (get_pango_context());
112 set_widget_name (widget_name);
114 _mode = BBT; /* lie to force mode switch */
116 set (last_when, true);
119 clocks.push_back (this);
122 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
123 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
126 AudioClock::~AudioClock ()
128 delete foreground_attr;
133 AudioClock::set_widget_name (const string& str)
138 set_name (str + " clock");
148 AudioClock::on_realize ()
150 Gtk::Requisition req;
152 CairoWidget::on_realize ();
154 set_clock_dimensions (req);
156 first_width = req.width;
157 first_height = req.height;
159 // XXX FIX ME: define font based on ... ???
165 AudioClock::set_font (Pango::FontDescription font)
167 Glib::RefPtr<Gtk::Style> style = get_style ();
168 Pango::AttrFontDesc* font_attr;
170 font_size = font.get_size();
171 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
173 normal_attributes.change (*font_attr);
174 editing_attributes.change (*font_attr);
176 /* now a smaller version of the same font */
179 font.set_size (INFO_FONT_SIZE);
180 font.set_weight (Pango::WEIGHT_NORMAL);
181 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
183 info_attributes.change (*font_attr);
185 /* and an even smaller one */
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);
200 /* force redraw of markup with new font-size */
201 set (last_when, true);
205 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
207 CairoWidget::set_active_state (s);
212 AudioClock::set_colors ()
218 uint32_t editing_color;
219 uint32_t cursor_color;
221 if (active_state()) {
222 bg_color = ARDOUR_UI::config()->color (string_compose ("%1 active: background", get_name()));
223 text_color = ARDOUR_UI::config()->color (string_compose ("%1 active: text", get_name()));
224 editing_color = ARDOUR_UI::config()->color (string_compose ("%1 active: edited text", get_name()));
225 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1 active: cursor", get_name()));
227 bg_color = ARDOUR_UI::config()->color (string_compose ("%1: background", get_name()));
228 text_color = ARDOUR_UI::config()->color (string_compose ("%1: text", get_name()));
229 editing_color = ARDOUR_UI::config()->color (string_compose ("%1: edited text", get_name()));
230 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1: cursor", get_name()));
233 /* store for bg and cursor in render() */
235 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
242 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
249 /* rescale for Pango colors ... sigh */
251 r = lrint (r * 65535.0);
252 g = lrint (g * 65535.0);
253 b = lrint (b * 65535.0);
255 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
256 r = lrint ((r/255.0) * 65535.0);
257 g = lrint ((g/255.0) * 65535.0);
258 b = lrint ((b/255.0) * 65535.0);
259 delete foreground_attr;
260 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
262 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
263 r = lrint ((r/255.0) * 65535.0);
264 g = lrint ((g/255.0) * 65535.0);
265 b = lrint ((b/255.0) * 65535.0);
267 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
269 normal_attributes.change (*foreground_attr);
270 info_attributes.change (*foreground_attr);
271 editing_attributes.change (*foreground_attr);
272 editing_attributes.change (*editing_attr);
275 _layout->set_attributes (normal_attributes);
277 _layout->set_attributes (editing_attributes);
284 AudioClock::set_scale (double x, double y)
293 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
295 /* main layout: rounded rect, plus the text */
298 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
301 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
303 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
306 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
311 double lw = layout_width * xscale;
312 double lh = layout_height * yscale;
314 cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
316 if (xscale != 1.0 || yscale != 1.0) {
318 cairo_scale (cr, xscale, yscale);
321 pango_cairo_show_layout (cr, _layout->gobj());
323 if (xscale != 1.0 || yscale != 1.0) {
329 double h = get_height() - upper_height - separator_height;
332 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
335 if (mode_based_info_ratio != 1.0) {
337 double left_rect_width = get_left_rect_width();
341 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
342 left_rect_width + (separator_height == 0 ? corner_radius : 0),
345 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
350 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
351 pango_cairo_show_layout (cr, _left_layout->gobj());
355 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
356 upper_height + separator_height,
357 get_width() - separator_height - left_rect_width,
360 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
361 get_width() - separator_height - left_rect_width, h);
367 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
368 /* right-align does not work per se beacuse layout width is unset.
369 * Using _right_layout->set_width([value >=0]) would also enable
370 * word-wrapping which is not wanted here.
371 * The solution is to custom align the layout depending on its size.
372 * if it is larger than the available space it will be cropped on the
373 * right edge rather than override text on the left side.
376 _right_layout->get_pixel_size(rw, rh);
377 x = get_width() - rw - separator_height - x_leading_padding;
378 if (x < x_leading_padding + left_rect_width + separator_height) {
379 /* rather cut off the right end than overlap with the text on the left */
380 x = x_leading_padding + left_rect_width + separator_height;
382 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
384 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
386 pango_cairo_show_layout (cr, _right_layout->gobj());
389 /* no info to display, or just one */
393 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
395 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
403 if (!insert_map.empty()) {
405 int xcenter = (get_width() - layout_width) /2;
407 if (input_string.length() < insert_map.size()) {
408 Pango::Rectangle cursor;
410 if (input_string.empty()) {
411 /* nothing entered yet, put cursor at the end
414 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
416 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
419 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
421 min (get_width() - 2.0,
422 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
423 (upper_height - layout_height)/2.0,
424 2.0, cursor.get_height()/PANGO_SCALE);
427 /* we've entered all possible digits, no cursor */
431 if (input_string.empty()) {
432 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
435 (upper_height - layout_height)/2.0,
444 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
446 CairoWidget::on_size_allocate (alloc);
449 upper_height = (get_height()/2.0) - 1.0;
451 upper_height = get_height();
456 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
458 Glib::RefPtr<Pango::Layout> tmp;
459 Glib::RefPtr<Gtk::Style> style = get_style ();
460 Pango::FontDescription font;
462 tmp = Pango::Layout::create (get_pango_context());
464 if (!is_realized()) {
465 font = get_font_for_style (get_name());
467 font = style->get_font();
470 tmp->set_font_description (font);
472 /* this string is the longest thing we will ever display */
474 tmp->set_text (" 88:88:88,888 ");
476 tmp->set_text (" 88:88:88,88 ");
477 tmp->get_pixel_size (req.width, req.height);
479 layout_height = req.height;
480 layout_width = req.width;
484 AudioClock::on_size_request (Gtk::Requisition* req)
486 /* even for non fixed width clocks, the size we *ask* for never changes,
487 even though the size we receive might. so once we've computed it,
492 req->width = first_width;
493 req->height = first_height;
497 set_clock_dimensions (*req);
499 /* now tackle height, for which we need to know the height of the lower
505 Glib::RefPtr<Pango::Layout> tmp;
506 Glib::RefPtr<Gtk::Style> style = get_style ();
507 Pango::FontDescription font;
510 tmp = Pango::Layout::create (get_pango_context());
512 if (!is_realized()) {
513 font = get_font_for_style (get_name());
515 font = style->get_font();
518 tmp->set_font_description (font);
520 font.set_size (INFO_FONT_SIZE);
521 font.set_weight (Pango::WEIGHT_NORMAL);
522 tmp->set_font_description (font);
524 /* we only care about height, so put as much stuff in here
525 as possible that might change the height.
527 tmp->set_text ("qyhH|"); /* one ascender, one descender */
529 tmp->get_pixel_size (w, info_height);
531 /* silly extra padding that seems necessary to correct the info
532 * that pango just gave us. I have no idea why.
535 req->height += info_height;
536 req->height += separator_height;
541 AudioClock::show_edit_status (int length)
543 editing_attr->set_start_index (edit_string.length() - length);
544 editing_attr->set_end_index (edit_string.length());
546 editing_attributes.change (*foreground_attr);
547 editing_attributes.change (*editing_attr);
549 _layout->set_attributes (editing_attributes);
553 AudioClock::start_edit (Field f)
556 pre_edit_string = _layout->get_text ();
557 if (!insert_map.empty()) {
558 edit_string = pre_edit_string;
560 edit_string.clear ();
561 _layout->set_text ("");
564 input_string.clear ();
566 edit_is_negative = false;
569 input_string = get_field (f);
570 show_edit_status (merge_input_and_edit_string ());
571 _layout->set_text (edit_string);
576 Keyboard::magic_widget_grab_focus ();
582 AudioClock::get_field (Field f)
586 return edit_string.substr (1, 2);
588 case Timecode_Minutes:
589 return edit_string.substr (4, 2);
591 case Timecode_Seconds:
592 return edit_string.substr (7, 2);
594 case Timecode_Frames:
595 return edit_string.substr (10, 2);
598 return edit_string.substr (1, 2);
601 return edit_string.substr (4, 2);
604 return edit_string.substr (7, 2);
606 case MS_Milliseconds:
607 return edit_string.substr (10, 3);
610 return edit_string.substr (1, 3);
613 return edit_string.substr (5, 2);
616 return edit_string.substr (8, 4);
626 AudioClock::end_edit (bool modify)
634 ok = timecode_validate_edit (edit_string);
638 ok = bbt_validate_edit (edit_string);
642 ok = minsec_validate_edit (edit_string);
646 if (edit_string.length() < 1) {
647 edit_string = pre_edit_string;
653 edit_string = pre_edit_string;
654 input_string.clear ();
655 _layout->set_text (edit_string);
656 show_edit_status (0);
657 /* edit attributes remain in use */
661 framepos_t pos = 0; /* stupid gcc */
665 pos = frames_from_timecode_string (edit_string);
670 pos = frame_duration_from_bbt_string (0, edit_string);
672 pos = frames_from_bbt_string (0, edit_string);
677 pos = frames_from_minsec_string (edit_string);
681 pos = frames_from_audioframes_string (edit_string);
686 _layout->set_attributes (normal_attributes);
687 ValueChanged(); /* EMIT_SIGNAL */
693 edit_is_negative = false;
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 llrint ((frames/(float)fps) * sr);
804 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
805 return (secs * sr) + llrint ((frames/(float)fps) * sr);
808 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
809 return (secs * sr) + llrint ((frames/(float)fps) * sr);
812 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
813 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
816 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
817 return (mins * 60 * sr) + (secs * sr) + llrint ((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) + llrint ((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) + llrint ((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();
905 if (c > frames || _negative_allowed) {
906 set (c - frames, true);
911 ValueChanged (); /* EMIT SIGNAL */
914 input_string.clear ();
920 AudioClock::session_property_changed (const PropertyChange&)
922 set (last_when, true);
926 AudioClock::session_configuration_changed (std::string p)
928 if (_negative_allowed) {
929 /* session option editor clock */
933 if (p == "sync-source" || p == "external-sync") {
934 set (current_time(), true);
938 if (p != "timecode-offset" && p != "timecode-offset-negative") {
947 current = current_duration ();
949 current = current_time ();
959 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
961 if ((!force && !is_visible()) || _session == 0) {
966 when = when - offset;
969 if (when == last_when && !force) {
970 #if 0 // XXX return if no change and no change forced. verify Aug/2014
971 if (_mode != Timecode && _mode != MinSec) {
972 /* may need to force display of TC source
973 * time, so don't return early.
975 /* ^^ Why was that?, delta times?
976 * Timecode FPS, pull-up/down, etc changes
977 * trigger a 'session_property_changed' which
978 * eventually calls set(last_when, true)
980 * re-rendering the clock every 40ms or so just
981 * because we can is not ideal.
992 _right_layout->set_alignment(Pango::ALIGN_LEFT);
998 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1000 set_timecode (when, force);
1004 set_bbt (when, force);
1008 if (_right_layout) {
1009 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1011 set_minsec (when, force);
1015 set_frames (when, force);
1025 AudioClock::set_slave_info ()
1027 if (!_left_layout || !_right_layout) {
1031 SyncSource sync_src = Config->get_sync_source();
1033 if (_session->config.get_external_sync()) {
1034 Slave* slave = _session->slave();
1038 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1039 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1040 _right_layout->set_text ("");
1044 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1045 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1046 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1047 INFO_FONT_SIZE, slave->approximate_current_delta()));
1049 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1050 INFO_FONT_SIZE, _("--pending--")));
1051 _right_layout->set_text ("");
1058 TimecodeSlave* tcslave;
1059 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1060 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1061 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1062 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1063 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1064 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1065 INFO_FONT_SIZE, slave->approximate_current_delta()));
1068 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1069 INFO_FONT_SIZE, _("--pending--")));
1070 _right_layout->set_text ("");
1075 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1076 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1077 _right_layout->set_text ("");
1082 AudioClock::set_frames (framepos_t when, bool /*force*/)
1085 bool negative = false;
1088 _layout->set_text (" ----------");
1091 _left_layout->set_text ("");
1092 _right_layout->set_text ("");
1104 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1106 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1109 _layout->set_text (buf);
1112 framecnt_t rate = _session->frame_rate();
1114 if (fmod (rate, 100.0) == 0.0) {
1115 sprintf (buf, "%.1fkHz", rate/1000.0);
1117 sprintf (buf, "%" PRId64 "Hz", rate);
1120 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1121 INFO_FONT_SIZE, _("SR"), buf));
1123 float vid_pullup = _session->config.get_video_pullup();
1125 if (vid_pullup == 0.0) {
1126 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1127 INFO_FONT_SIZE, _("Pull")));
1129 sprintf (buf, _("%+.4f%%"), vid_pullup);
1130 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1131 INFO_FONT_SIZE, _("Pull"), buf));
1137 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1154 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1155 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1156 mins = (int) floor (left / (frame_rate * 60.0f));
1157 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1158 secs = (int) floor (left / (float) frame_rate);
1159 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1160 millisecs = floor (left * 1000.0 / (float) frame_rate);
1163 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1165 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1171 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1176 _layout->set_text (" --:--:--.---");
1179 _left_layout->set_text ("");
1180 _right_layout->set_text ("");
1186 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1188 _layout->set_text (buf);
1193 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1196 bool negative = false;
1199 _layout->set_text (" --:--:--:--");
1201 _left_layout->set_text ("");
1202 _right_layout->set_text ("");
1214 _session->timecode_duration (when, TC);
1216 _session->timecode_time (when, TC);
1219 TC.negative = TC.negative || negative;
1221 _layout->set_text (Timecode::timecode_format_time(TC));
1227 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1230 Timecode::BBT_Time BBT;
1231 bool negative = false;
1234 _layout->set_text (" ---|--|----");
1236 _left_layout->set_text ("");
1237 _right_layout->set_text ("");
1247 /* handle a common case */
1254 _session->tempo_map().bbt_time (when, BBT);
1259 _session->tempo_map().bbt_time (when, BBT);
1263 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1264 BBT.bars, BBT.beats, BBT.ticks);
1266 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1267 BBT.bars, BBT.beats, BBT.ticks);
1270 _layout->set_text (buf);
1272 if (_right_layout) {
1275 if (bbt_reference_time < 0) {
1278 pos = bbt_reference_time;
1281 TempoMetric m (_session->tempo_map().metric_at (pos));
1283 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1284 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1285 INFO_FONT_SIZE, buf, _("Tempo")));
1287 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1288 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1289 INFO_FONT_SIZE, buf, _("Meter")));
1294 AudioClock::set_session (Session *s)
1296 SessionHandlePtr::set_session (s);
1300 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1301 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1303 const XMLProperty* prop;
1304 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1305 AudioClock::Mode amode;
1308 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1309 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1311 if ((prop = (*i)->property (X_("mode"))) != 0) {
1312 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1315 if ((prop = (*i)->property (X_("on"))) != 0) {
1316 set_off (!string_is_affirmative (prop->value()));
1323 set (last_when, true);
1328 AudioClock::on_key_press_event (GdkEventKey* ev)
1336 int highlight_length;
1339 switch (ev->keyval) {
1382 case GDK_KP_Subtract:
1383 if (_negative_allowed && input_string.empty()) {
1384 edit_is_negative = true;
1385 edit_string.replace(0,1,"-");
1386 _layout->set_text (edit_string);
1389 end_edit_relative (false);
1395 end_edit_relative (true);
1408 ChangeAborted(); /* EMIT SIGNAL */
1413 if (!input_string.empty()) {
1414 /* delete the last key entered
1416 input_string = input_string.substr (0, input_string.length() - 1);
1418 goto use_input_string;
1424 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1425 /* too many digits: eat the key event, but do nothing with it */
1429 input_string.push_back (new_char);
1435 /* get this one in the right order, and to the right width */
1436 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1437 edit_string = edit_string.substr (0, edit_string.length() - 1);
1439 edit_string.push_back (new_char);
1441 if (!edit_string.empty()) {
1443 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1444 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1447 /* highlight the whole thing */
1448 highlight_length = edit_string.length();
1452 highlight_length = merge_input_and_edit_string ();
1455 if (edit_is_negative) {
1456 edit_string.replace(0,1,"-");
1458 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1459 edit_string.replace(0,1,"_");
1461 edit_string.replace(0,1," ");
1465 show_edit_status (highlight_length);
1466 _layout->set_text (edit_string);
1473 AudioClock::merge_input_and_edit_string ()
1475 /* merge with pre-edit-string into edit string */
1477 edit_string = pre_edit_string;
1479 if (input_string.empty()) {
1483 string::size_type target;
1484 for (string::size_type i = 0; i < input_string.length(); ++i) {
1485 target = insert_map[input_string.length() - 1 - i];
1486 edit_string[target] = input_string[i];
1488 /* highlight from end to wherever the last character was added */
1489 return edit_string.length() - insert_map[input_string.length()-1];
1494 AudioClock::on_key_release_event (GdkEventKey *ev)
1500 /* return true for keys that we used on press
1501 so that they cannot possibly do double-duty
1503 switch (ev->keyval) {
1526 case GDK_KP_Decimal:
1534 case GDK_KP_Subtract:
1542 AudioClock::index_to_field (int index) const
1547 return Timecode_Hours;
1548 } else if (index < 7) {
1549 return Timecode_Minutes;
1550 } else if (index < 10) {
1551 return Timecode_Seconds;
1553 return Timecode_Frames;
1559 } else if (index < 7) {
1567 return Timecode_Hours;
1568 } else if (index < 6) {
1570 } else if (index < 9) {
1573 return MS_Milliseconds;
1585 AudioClock::on_button_press_event (GdkEventButton *ev)
1587 switch (ev->button) {
1589 if (editable && !_off) {
1595 /* the text has been centered vertically, so adjust
1598 int xcenter = (get_width() - layout_width) /2;
1600 y = ev->y - ((upper_height - layout_height)/2);
1601 x = ev->x - xcenter;
1603 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1604 /* pretend it is a character on the far right */
1607 drag_field = index_to_field (index);
1609 /* make absolutely sure that the pointer is grabbed */
1610 gdk_pointer_grab(ev->window,false ,
1611 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1612 NULL,NULL,ev->time);
1614 drag_start_y = ev->y;
1628 AudioClock::on_button_release_event (GdkEventButton *ev)
1630 if (editable && !_off) {
1632 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1634 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1635 // we actually dragged so return without
1636 // setting editing focus, or we shift clicked
1639 if (ev->button == 1) {
1641 if (_edit_by_click_field) {
1643 int xcenter = (get_width() - layout_width) /2;
1646 int y = ev->y - ((upper_height - layout_height)/2);
1647 int x = ev->x - xcenter;
1650 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1654 f = index_to_field (index);
1657 case Timecode_Frames:
1658 case MS_Milliseconds:
1674 if (Keyboard::is_context_menu_event (ev)) {
1675 if (ops_menu == 0) {
1678 ops_menu->popup (1, ev->time);
1686 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1688 bool ret = CairoWidget::on_focus_out_event (ev);
1698 AudioClock::on_scroll_event (GdkEventScroll *ev)
1703 if (editing || _session == 0 || !editable || _off) {
1710 /* the text has been centered vertically, so adjust
1714 int xcenter = (get_width() - layout_width) /2;
1715 y = ev->y - ((upper_height - layout_height)/2);
1716 x = ev->x - xcenter;
1718 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1719 /* not in the main layout */
1723 Field f = index_to_field (index);
1724 framepos_t frames = 0;
1726 switch (ev->direction) {
1729 frames = get_frame_step (f);
1731 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1734 set (current_time() + frames, true);
1735 ValueChanged (); /* EMIT_SIGNAL */
1739 case GDK_SCROLL_DOWN:
1740 frames = get_frame_step (f);
1742 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1746 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1749 set (current_time() - frames, true);
1752 ValueChanged (); /* EMIT_SIGNAL */
1765 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1767 if (editing || _session == 0 || !dragging) {
1771 float pixel_frame_scale_factor = 0.2f;
1773 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1774 pixel_frame_scale_factor = 0.1f;
1778 if (Keyboard::modifier_state_contains (ev->state,
1779 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1781 pixel_frame_scale_factor = 0.025f;
1784 double y_delta = ev->y - drag_y;
1786 drag_accum += y_delta*pixel_frame_scale_factor;
1790 if (floor (drag_accum) != 0) {
1795 dir = (drag_accum < 0 ? 1:-1);
1796 pos = current_time();
1797 frames = get_frame_step (drag_field, pos, dir);
1799 if (frames != 0 && frames * drag_accum < current_time()) {
1800 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1806 ValueChanged(); /* EMIT_SIGNAL */
1813 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1816 Timecode::BBT_Time BBT;
1818 case Timecode_Hours:
1819 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1821 case Timecode_Minutes:
1822 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1824 case Timecode_Seconds:
1825 f = _session->frame_rate();
1827 case Timecode_Frames:
1828 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1836 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1839 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1842 f = (framecnt_t) _session->frame_rate();
1844 case MS_Milliseconds:
1845 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1852 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1858 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1864 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1867 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1876 AudioClock::current_time (framepos_t) const
1882 AudioClock::current_duration (framepos_t pos) const
1891 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1907 AudioClock::bbt_validate_edit (const string& str)
1911 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1915 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1919 if (!is_duration && any.bbt.bars == 0) {
1923 if (!is_duration && any.bbt.beats == 0) {
1931 AudioClock::timecode_validate_edit (const string&)
1937 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1938 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1943 TC.hours = hours * -1;
1947 TC.negative = false;
1950 if (TC.negative && !_negative_allowed) {
1954 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1958 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1962 if (_session->timecode_drop_frames()) {
1963 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1972 AudioClock::minsec_validate_edit (const string& str)
1974 int hrs, mins, secs, millisecs;
1976 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1980 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1988 AudioClock::frames_from_timecode_string (const string& str) const
1990 if (_session == 0) {
1999 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2000 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2003 TC.hours = abs(hours);
2004 TC.rate = _session->timecode_frames_per_second();
2005 TC.drop= _session->timecode_drop_frames();
2007 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2009 // timecode_tester ();
2010 if (edit_is_negative) {
2018 AudioClock::frames_from_minsec_string (const string& str) const
2020 if (_session == 0) {
2024 int hrs, mins, secs, millisecs;
2025 framecnt_t sr = _session->frame_rate();
2027 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2028 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2032 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2036 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2038 if (_session == 0) {
2039 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2044 any.type = AnyTime::BBT;
2046 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2053 return _session->any_duration_to_frames (pos, any);
2055 return _session->convert_to_frames (any);
2061 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2063 if (_session == 0) {
2064 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2068 Timecode::BBT_Time bbt;
2070 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2074 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2078 AudioClock::frames_from_audioframes_string (const string& str) const
2081 sscanf (str.c_str(), "%" PRId64, &f);
2086 AudioClock::copy_text_to_clipboard () const
2090 val = pre_edit_string;
2092 val = _layout->get_text ();
2094 const size_t trim = val.find_first_not_of(" ");
2095 if (trim == string::npos) {
2096 assert(0); // empty clock, can't be right.
2099 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2100 cl->set_text (val.substr(trim));
2104 AudioClock::build_ops_menu ()
2106 using namespace Menu_Helpers;
2107 ops_menu = new Menu;
2108 MenuList& ops_items = ops_menu->items();
2109 ops_menu->set_name ("ArdourContextMenu");
2111 if (!Profile->get_sae()) {
2112 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2114 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2115 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2116 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2118 if (editable && !_off && !is_duration && !_follows_playhead) {
2119 ops_items.push_back (SeparatorElem());
2120 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2121 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2123 ops_items.push_back (SeparatorElem());
2124 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2128 AudioClock::set_from_playhead ()
2134 set (_session->transport_frame());
2139 AudioClock::locate ()
2141 if (!_session || is_duration) {
2145 _session->request_locate (current_time(), _session->transport_rolling ());
2149 AudioClock::set_mode (Mode m)
2159 _layout->set_text ("");
2161 Gtk::Requisition req;
2162 set_clock_dimensions (req);
2166 _left_layout->set_attributes (info_attributes);
2167 _right_layout->set_attributes (info_attributes);
2168 /* adjust info_height according to font size */
2170 _left_layout->set_text (" 1234567890");
2171 _left_layout->get_pixel_size (ignored, info_height);
2173 _left_layout->set_text ("");
2174 _right_layout->set_text ("");
2179 mode_based_info_ratio = 0.6;
2180 insert_map.push_back (11);
2181 insert_map.push_back (10);
2182 insert_map.push_back (8);
2183 insert_map.push_back (7);
2184 insert_map.push_back (5);
2185 insert_map.push_back (4);
2186 insert_map.push_back (2);
2187 insert_map.push_back (1);
2191 mode_based_info_ratio = 0.5;
2192 insert_map.push_back (11);
2193 insert_map.push_back (10);
2194 insert_map.push_back (9);
2195 insert_map.push_back (8);
2196 insert_map.push_back (6);
2197 insert_map.push_back (5);
2198 insert_map.push_back (3);
2199 insert_map.push_back (2);
2200 insert_map.push_back (1);
2204 mode_based_info_ratio = 0.6;
2205 insert_map.push_back (12);
2206 insert_map.push_back (11);
2207 insert_map.push_back (10);
2208 insert_map.push_back (8);
2209 insert_map.push_back (7);
2210 insert_map.push_back (5);
2211 insert_map.push_back (4);
2212 insert_map.push_back (2);
2213 insert_map.push_back (1);
2217 mode_based_info_ratio = 0.45;
2221 set (last_when, true);
2223 if (!is_transient) {
2224 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2227 mode_changed (); /* EMIT SIGNAL (the member one) */
2231 AudioClock::set_bbt_reference (framepos_t pos)
2233 bbt_reference_time = pos;
2237 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2239 CairoWidget::on_style_changed (old_style);
2241 Gtk::Requisition req;
2242 set_clock_dimensions (req);
2244 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2250 AudioClock::set_editable (bool yn)
2256 AudioClock::set_is_duration (bool yn)
2258 if (yn == is_duration) {
2263 set (last_when, true);
2267 AudioClock::set_off (bool yn)
2275 /* force a redraw. last_when will be preserved, but the clock text will
2279 set (last_when, true);
2283 AudioClock::focus ()
2285 start_edit (Field (0));
2289 AudioClock::set_corner_radius (double r)
2298 AudioClock::dpi_reset ()
2300 /* force recomputation of size even if we are fixed width
2308 AudioClock::set_negative_allowed (bool yn)
2310 _negative_allowed = yn;