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.60;
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))
101 set_flags (CAN_FOCUS);
103 _layout = Pango::Layout::create (get_pango_context());
104 _layout->set_attributes (normal_attributes);
107 _left_layout = Pango::Layout::create (get_pango_context());
108 _right_layout = Pango::Layout::create (get_pango_context());
111 set_widget_name (widget_name);
113 _mode = BBT; /* lie to force mode switch */
115 set (last_when, true);
118 clocks.push_back (this);
121 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
122 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
125 AudioClock::~AudioClock ()
127 delete foreground_attr;
132 AudioClock::set_widget_name (const string& str)
137 set_name (str + " clock");
147 AudioClock::on_realize ()
149 Gtk::Requisition req;
151 CairoWidget::on_realize ();
153 set_clock_dimensions (req);
155 first_width = req.width;
156 first_height = req.height;
158 // XXX FIX ME: define font based on ... ???
164 AudioClock::set_font (Pango::FontDescription font)
166 Glib::RefPtr<Gtk::Style> style = get_style ();
167 Pango::AttrFontDesc* font_attr;
169 font_size = font.get_size();
170 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
172 normal_attributes.change (*font_attr);
173 editing_attributes.change (*font_attr);
175 /* now a smaller version of the same font */
178 font.set_size (INFO_FONT_SIZE);
179 font.set_weight (Pango::WEIGHT_NORMAL);
180 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
182 info_attributes.change (*font_attr);
184 /* and an even smaller one */
188 /* get the figure width for the font. This doesn't have to super
189 * accurate since we only use it to measure the (roughly 1 character)
190 * offset from the position Pango tells us for the "cursor"
193 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
197 tmp->get_pixel_size (em_width, ignore_height);
199 /* force redraw of markup with new font-size */
200 set (last_when, true);
204 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
206 CairoWidget::set_active_state (s);
211 AudioClock::set_colors ()
217 uint32_t editing_color;
218 uint32_t cursor_color;
220 if (active_state()) {
221 bg_color = ARDOUR_UI::config()->color (string_compose ("%1 active: background", get_name()));
222 text_color = ARDOUR_UI::config()->color (string_compose ("%1 active: text", get_name()));
223 editing_color = ARDOUR_UI::config()->color (string_compose ("%1 active: edited text", get_name()));
224 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1 active: cursor", get_name()));
226 bg_color = ARDOUR_UI::config()->color (string_compose ("%1: background", get_name()));
227 text_color = ARDOUR_UI::config()->color (string_compose ("%1: text", get_name()));
228 editing_color = ARDOUR_UI::config()->color (string_compose ("%1: edited text", get_name()));
229 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1: cursor", get_name()));
232 /* store for bg and cursor in render() */
234 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
241 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
248 /* rescale for Pango colors ... sigh */
250 r = lrint (r * 65535.0);
251 g = lrint (g * 65535.0);
252 b = lrint (b * 65535.0);
254 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
255 r = lrint ((r/255.0) * 65535.0);
256 g = lrint ((g/255.0) * 65535.0);
257 b = lrint ((b/255.0) * 65535.0);
258 delete foreground_attr;
259 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
261 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
262 r = lrint ((r/255.0) * 65535.0);
263 g = lrint ((g/255.0) * 65535.0);
264 b = lrint ((b/255.0) * 65535.0);
266 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
268 normal_attributes.change (*foreground_attr);
269 info_attributes.change (*foreground_attr);
270 editing_attributes.change (*foreground_attr);
271 editing_attributes.change (*editing_attr);
274 _layout->set_attributes (normal_attributes);
276 _layout->set_attributes (editing_attributes);
283 AudioClock::set_scale (double x, double y)
292 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
294 /* main layout: rounded rect, plus the text */
297 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
300 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
302 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
305 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
310 double lw = layout_width * xscale;
311 double lh = layout_height * yscale;
313 cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
315 if (xscale != 1.0 || yscale != 1.0) {
317 cairo_scale (cr, xscale, yscale);
320 pango_cairo_show_layout (cr, _layout->gobj());
322 if (xscale != 1.0 || yscale != 1.0) {
328 double h = get_height() - upper_height - separator_height;
331 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
334 if (mode_based_info_ratio != 1.0) {
336 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
340 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
341 left_rect_width + (separator_height == 0 ? corner_radius : 0),
344 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
349 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
350 pango_cairo_show_layout (cr, _left_layout->gobj());
354 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
355 upper_height + separator_height,
356 get_width() - separator_height - left_rect_width,
359 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
360 get_width() - separator_height - left_rect_width, h);
366 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
367 /* right-align does not work per se beacuse layout width is unset.
368 * Using _right_layout->set_width([value >=0]) would also enable
369 * word-wrapping which is not wanted here.
370 * The solution is to custom align the layout depending on its size.
371 * if it is larger than the available space it will be cropped on the
372 * right edge rather than override text on the left side.
375 _right_layout->get_pixel_size(rw, rh);
376 x = get_width() - rw - separator_height - x_leading_padding;
377 if (x < x_leading_padding + left_rect_width + separator_height) {
378 /* rather cut off the right end than overlap with the text on the left */
379 x = x_leading_padding + left_rect_width + separator_height;
381 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
383 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
385 pango_cairo_show_layout (cr, _right_layout->gobj());
388 /* no info to display, or just one */
392 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
394 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
402 if (!insert_map.empty()) {
404 int xcenter = (get_width() - layout_width) /2;
406 if (input_string.length() < insert_map.size()) {
407 Pango::Rectangle cursor;
409 if (input_string.empty()) {
410 /* nothing entered yet, put cursor at the end
413 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
415 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
418 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
420 min (get_width() - 2.0,
421 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
422 (upper_height - layout_height)/2.0,
423 2.0, cursor.get_height()/PANGO_SCALE);
426 /* we've entered all possible digits, no cursor */
430 if (input_string.empty()) {
431 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
434 (upper_height - layout_height)/2.0,
443 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
445 CairoWidget::on_size_allocate (alloc);
448 upper_height = (get_height()/2.0) - 1.0;
450 upper_height = get_height();
455 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
457 Glib::RefPtr<Pango::Layout> tmp;
458 Glib::RefPtr<Gtk::Style> style = get_style ();
459 Pango::FontDescription font;
461 tmp = Pango::Layout::create (get_pango_context());
463 if (!is_realized()) {
464 font = get_font_for_style (get_name());
466 font = style->get_font();
469 tmp->set_font_description (font);
471 /* this string is the longest thing we will ever display */
472 tmp->set_text (" 88:88:88,888");
473 tmp->get_pixel_size (req.width, req.height);
475 layout_height = req.height;
476 layout_width = req.width;
480 AudioClock::on_size_request (Gtk::Requisition* req)
482 /* even for non fixed width clocks, the size we *ask* for never changes,
483 even though the size we receive might. so once we've computed it,
488 req->width = first_width;
489 req->height = first_height;
493 set_clock_dimensions (*req);
495 /* now tackle height, for which we need to know the height of the lower
501 Glib::RefPtr<Pango::Layout> tmp;
502 Glib::RefPtr<Gtk::Style> style = get_style ();
503 Pango::FontDescription font;
506 tmp = Pango::Layout::create (get_pango_context());
508 if (!is_realized()) {
509 font = get_font_for_style (get_name());
511 font = style->get_font();
514 tmp->set_font_description (font);
516 font.set_size (INFO_FONT_SIZE);
517 font.set_weight (Pango::WEIGHT_NORMAL);
518 tmp->set_font_description (font);
520 /* we only care about height, so put as much stuff in here
521 as possible that might change the height.
523 tmp->set_text ("qyhH|"); /* one ascender, one descender */
525 tmp->get_pixel_size (w, info_height);
527 /* silly extra padding that seems necessary to correct the info
528 * that pango just gave us. I have no idea why.
531 req->height += info_height;
532 req->height += separator_height;
537 AudioClock::show_edit_status (int length)
539 editing_attr->set_start_index (edit_string.length() - length);
540 editing_attr->set_end_index (edit_string.length());
542 editing_attributes.change (*foreground_attr);
543 editing_attributes.change (*editing_attr);
545 _layout->set_attributes (editing_attributes);
549 AudioClock::start_edit (Field f)
552 pre_edit_string = _layout->get_text ();
553 if (!insert_map.empty()) {
554 edit_string = pre_edit_string;
556 edit_string.clear ();
557 _layout->set_text ("");
560 input_string.clear ();
562 edit_is_negative = false;
565 input_string = get_field (f);
566 show_edit_status (merge_input_and_edit_string ());
567 _layout->set_text (edit_string);
572 Keyboard::magic_widget_grab_focus ();
578 AudioClock::get_field (Field f)
582 return edit_string.substr (1, 2);
584 case Timecode_Minutes:
585 return edit_string.substr (4, 2);
587 case Timecode_Seconds:
588 return edit_string.substr (7, 2);
590 case Timecode_Frames:
591 return edit_string.substr (10, 2);
594 return edit_string.substr (1, 2);
597 return edit_string.substr (4, 2);
600 return edit_string.substr (7, 2);
602 case MS_Milliseconds:
603 return edit_string.substr (10, 3);
606 return edit_string.substr (1, 3);
609 return edit_string.substr (5, 2);
612 return edit_string.substr (8, 4);
622 AudioClock::end_edit (bool modify)
630 ok = timecode_validate_edit (edit_string);
634 ok = bbt_validate_edit (edit_string);
638 ok = minsec_validate_edit (edit_string);
642 if (edit_string.length() < 1) {
643 edit_string = pre_edit_string;
649 edit_string = pre_edit_string;
650 input_string.clear ();
651 _layout->set_text (edit_string);
652 show_edit_status (0);
653 /* edit attributes remain in use */
657 framepos_t pos = 0; /* stupid gcc */
661 pos = frames_from_timecode_string (edit_string);
666 pos = frame_duration_from_bbt_string (0, edit_string);
668 pos = frames_from_bbt_string (0, edit_string);
673 pos = frames_from_minsec_string (edit_string);
677 pos = frames_from_audioframes_string (edit_string);
682 _layout->set_attributes (normal_attributes);
683 ValueChanged(); /* EMIT_SIGNAL */
689 edit_is_negative = false;
690 _layout->set_attributes (normal_attributes);
691 _layout->set_text (pre_edit_string);
702 AudioClock::drop_focus ()
704 Keyboard::magic_widget_drop_focus ();
708 /* move focus back to the default widget in the top level window */
710 Widget* top = get_toplevel();
712 if (top->is_toplevel ()) {
713 Window* win = dynamic_cast<Window*> (top);
720 AudioClock::parse_as_frames_distance (const std::string& str)
724 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
732 AudioClock::parse_as_minsec_distance (const std::string& str)
734 framecnt_t sr = _session->frame_rate();
740 switch (str.length()) {
747 sscanf (str.c_str(), "%" PRId32, &msecs);
748 return msecs * (sr / 1000);
751 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
752 return (secs * sr) + (msecs * (sr/1000));
755 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
756 return (secs * sr) + (msecs * (sr/1000));
759 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
760 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
763 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
764 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
767 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
768 return (hrs * 3600 * sr) + (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));
782 AudioClock::parse_as_timecode_distance (const std::string& str)
784 double fps = _session->timecode_frames_per_second();
785 framecnt_t sr = _session->frame_rate();
791 switch (str.length()) {
796 sscanf (str.c_str(), "%" PRId32, &frames);
797 return llrint ((frames/(float)fps) * sr);
800 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
801 return (secs * sr) + llrint ((frames/(float)fps) * sr);
804 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
805 return (secs * sr) + llrint ((frames/(float)fps) * sr);
808 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
809 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
812 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
813 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
816 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
817 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
820 sscanf (str.c_str(), "%2" 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);
831 AudioClock::parse_as_bbt_distance (const std::string&)
837 AudioClock::parse_as_distance (const std::string& instr)
841 return parse_as_timecode_distance (instr);
844 return parse_as_frames_distance (instr);
847 return parse_as_bbt_distance (instr);
850 return parse_as_minsec_distance (instr);
857 AudioClock::end_edit_relative (bool add)
863 ok = timecode_validate_edit (edit_string);
867 ok = bbt_validate_edit (edit_string);
871 ok = minsec_validate_edit (edit_string);
879 edit_string = pre_edit_string;
880 input_string.clear ();
881 _layout->set_text (edit_string);
882 show_edit_status (0);
883 /* edit attributes remain in use */
888 framecnt_t frames = parse_as_distance (input_string);
893 _layout->set_attributes (normal_attributes);
897 set (current_time() + frames, true);
899 framepos_t c = current_time();
901 if (c > frames || _negative_allowed) {
902 set (c - frames, true);
907 ValueChanged (); /* EMIT SIGNAL */
910 input_string.clear ();
916 AudioClock::session_property_changed (const PropertyChange&)
918 set (last_when, true);
922 AudioClock::session_configuration_changed (std::string p)
924 if (_negative_allowed) {
925 /* session option editor clock */
929 if (p == "sync-source" || p == "external-sync") {
930 set (current_time(), true);
934 if (p != "timecode-offset" && p != "timecode-offset-negative") {
943 current = current_duration ();
945 current = current_time ();
955 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
957 if ((!force && !is_visible()) || _session == 0) {
962 when = when - offset;
965 if (when == last_when && !force) {
966 #if 0 // XXX return if no change and no change forced. verify Aug/2014
967 if (_mode != Timecode && _mode != MinSec) {
968 /* may need to force display of TC source
969 * time, so don't return early.
971 /* ^^ Why was that?, delta times?
972 * Timecode FPS, pull-up/down, etc changes
973 * trigger a 'session_property_changed' which
974 * eventually calls set(last_when, true)
976 * re-rendering the clock every 40ms or so just
977 * because we can is not ideal.
988 _right_layout->set_alignment(Pango::ALIGN_LEFT);
994 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
996 set_timecode (when, force);
1000 set_bbt (when, force);
1004 if (_right_layout) {
1005 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1007 set_minsec (when, force);
1011 set_frames (when, force);
1021 AudioClock::set_slave_info ()
1023 if (!_left_layout || !_right_layout) {
1027 SyncSource sync_src = Config->get_sync_source();
1029 if (_session->config.get_external_sync()) {
1030 Slave* slave = _session->slave();
1034 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1035 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1036 _right_layout->set_text ("");
1040 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1041 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1042 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1043 INFO_FONT_SIZE, slave->approximate_current_delta()));
1045 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1046 INFO_FONT_SIZE, _("--pending--")));
1047 _right_layout->set_text ("");
1054 TimecodeSlave* tcslave;
1055 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1056 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1057 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1058 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1059 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1060 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1061 INFO_FONT_SIZE, slave->approximate_current_delta()));
1064 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1065 INFO_FONT_SIZE, _("--pending--")));
1066 _right_layout->set_text ("");
1071 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1072 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1073 _right_layout->set_text ("");
1078 AudioClock::set_frames (framepos_t when, bool /*force*/)
1081 bool negative = false;
1084 _layout->set_text (" ----------");
1087 _left_layout->set_text ("");
1088 _right_layout->set_text ("");
1100 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1102 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1105 _layout->set_text (buf);
1108 framecnt_t rate = _session->frame_rate();
1110 if (fmod (rate, 100.0) == 0.0) {
1111 sprintf (buf, "%.1fkHz", rate/1000.0);
1113 sprintf (buf, "%" PRId64 "Hz", rate);
1116 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1117 INFO_FONT_SIZE, _("SR"), buf));
1119 float vid_pullup = _session->config.get_video_pullup();
1121 if (vid_pullup == 0.0) {
1122 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1123 INFO_FONT_SIZE, _("Pull")));
1125 sprintf (buf, _("%+.4f%%"), vid_pullup);
1126 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1127 INFO_FONT_SIZE, _("Pull"), buf));
1133 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1150 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1151 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1152 mins = (int) floor (left / (frame_rate * 60.0f));
1153 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1154 secs = (int) floor (left / (float) frame_rate);
1155 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1156 millisecs = floor (left * 1000.0 / (float) frame_rate);
1159 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1161 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1167 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1172 _layout->set_text (" --:--:--.---");
1175 _left_layout->set_text ("");
1176 _right_layout->set_text ("");
1182 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1184 _layout->set_text (buf);
1189 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1192 bool negative = false;
1195 _layout->set_text (" --:--:--:--");
1197 _left_layout->set_text ("");
1198 _right_layout->set_text ("");
1210 _session->timecode_duration (when, TC);
1212 _session->timecode_time (when, TC);
1215 TC.negative = TC.negative || negative;
1217 _layout->set_text (Timecode::timecode_format_time(TC));
1223 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1226 Timecode::BBT_Time BBT;
1227 bool negative = false;
1230 _layout->set_text (" ---|--|----");
1232 _left_layout->set_text ("");
1233 _right_layout->set_text ("");
1243 /* handle a common case */
1250 _session->tempo_map().bbt_time (when, BBT);
1255 _session->tempo_map().bbt_time (when, BBT);
1259 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1260 BBT.bars, BBT.beats, BBT.ticks);
1262 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1263 BBT.bars, BBT.beats, BBT.ticks);
1266 _layout->set_text (buf);
1268 if (_right_layout) {
1271 if (bbt_reference_time < 0) {
1274 pos = bbt_reference_time;
1277 TempoMetric m (_session->tempo_map().metric_at (pos));
1279 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1280 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1281 INFO_FONT_SIZE, buf, _("Tempo")));
1283 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1284 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1285 INFO_FONT_SIZE, buf, _("Meter")));
1290 AudioClock::set_session (Session *s)
1292 SessionHandlePtr::set_session (s);
1296 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1297 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1299 const XMLProperty* prop;
1300 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1301 AudioClock::Mode amode;
1304 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1305 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1307 if ((prop = (*i)->property (X_("mode"))) != 0) {
1308 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1311 if ((prop = (*i)->property (X_("on"))) != 0) {
1312 set_off (!string_is_affirmative (prop->value()));
1319 set (last_when, true);
1324 AudioClock::on_key_press_event (GdkEventKey* ev)
1332 int highlight_length;
1335 switch (ev->keyval) {
1378 case GDK_KP_Subtract:
1379 if (_negative_allowed && input_string.empty()) {
1380 edit_is_negative = true;
1381 edit_string.replace(0,1,"-");
1382 _layout->set_text (edit_string);
1385 end_edit_relative (false);
1391 end_edit_relative (true);
1404 ChangeAborted(); /* EMIT SIGNAL */
1409 if (!input_string.empty()) {
1410 /* delete the last key entered
1412 input_string = input_string.substr (0, input_string.length() - 1);
1414 goto use_input_string;
1420 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1421 /* too many digits: eat the key event, but do nothing with it */
1425 input_string.push_back (new_char);
1431 /* get this one in the right order, and to the right width */
1432 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1433 edit_string = edit_string.substr (0, edit_string.length() - 1);
1435 edit_string.push_back (new_char);
1437 if (!edit_string.empty()) {
1439 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1440 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1443 /* highlight the whole thing */
1444 highlight_length = edit_string.length();
1448 highlight_length = merge_input_and_edit_string ();
1451 if (edit_is_negative) {
1452 edit_string.replace(0,1,"-");
1454 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1455 edit_string.replace(0,1,"_");
1457 edit_string.replace(0,1," ");
1461 show_edit_status (highlight_length);
1462 _layout->set_text (edit_string);
1469 AudioClock::merge_input_and_edit_string ()
1471 /* merge with pre-edit-string into edit string */
1473 edit_string = pre_edit_string;
1475 if (input_string.empty()) {
1479 string::size_type target;
1480 for (string::size_type i = 0; i < input_string.length(); ++i) {
1481 target = insert_map[input_string.length() - 1 - i];
1482 edit_string[target] = input_string[i];
1484 /* highlight from end to wherever the last character was added */
1485 return edit_string.length() - insert_map[input_string.length()-1];
1490 AudioClock::on_key_release_event (GdkEventKey *ev)
1496 /* return true for keys that we used on press
1497 so that they cannot possibly do double-duty
1499 switch (ev->keyval) {
1522 case GDK_KP_Decimal:
1530 case GDK_KP_Subtract:
1538 AudioClock::index_to_field (int index) const
1543 return Timecode_Hours;
1544 } else if (index < 7) {
1545 return Timecode_Minutes;
1546 } else if (index < 10) {
1547 return Timecode_Seconds;
1549 return Timecode_Frames;
1555 } else if (index < 7) {
1563 return Timecode_Hours;
1564 } else if (index < 6) {
1566 } else if (index < 9) {
1569 return MS_Milliseconds;
1581 AudioClock::on_button_press_event (GdkEventButton *ev)
1583 switch (ev->button) {
1585 if (editable && !_off) {
1591 /* the text has been centered vertically, so adjust
1594 int xcenter = (get_width() - layout_width) /2;
1596 y = ev->y - ((upper_height - layout_height)/2);
1597 x = ev->x - xcenter;
1599 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1600 /* pretend it is a character on the far right */
1603 drag_field = index_to_field (index);
1605 /* make absolutely sure that the pointer is grabbed */
1606 gdk_pointer_grab(ev->window,false ,
1607 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1608 NULL,NULL,ev->time);
1610 drag_start_y = ev->y;
1624 AudioClock::on_button_release_event (GdkEventButton *ev)
1626 if (editable && !_off) {
1628 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1630 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1631 // we actually dragged so return without
1632 // setting editing focus, or we shift clicked
1635 if (ev->button == 1) {
1637 if (_edit_by_click_field) {
1639 int xcenter = (get_width() - layout_width) /2;
1642 int y = ev->y - ((upper_height - layout_height)/2);
1643 int x = ev->x - xcenter;
1646 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1650 f = index_to_field (index);
1653 case Timecode_Frames:
1654 case MS_Milliseconds:
1670 if (Keyboard::is_context_menu_event (ev)) {
1671 if (ops_menu == 0) {
1674 ops_menu->popup (1, ev->time);
1682 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1684 bool ret = CairoWidget::on_focus_out_event (ev);
1694 AudioClock::on_scroll_event (GdkEventScroll *ev)
1699 if (editing || _session == 0 || !editable || _off) {
1706 /* the text has been centered vertically, so adjust
1710 int xcenter = (get_width() - layout_width) /2;
1711 y = ev->y - ((upper_height - layout_height)/2);
1712 x = ev->x - xcenter;
1714 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1715 /* not in the main layout */
1719 Field f = index_to_field (index);
1720 framepos_t frames = 0;
1722 switch (ev->direction) {
1725 frames = get_frame_step (f);
1727 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1730 set (current_time() + frames, true);
1731 ValueChanged (); /* EMIT_SIGNAL */
1735 case GDK_SCROLL_DOWN:
1736 frames = get_frame_step (f);
1738 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1742 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1745 set (current_time() - frames, true);
1748 ValueChanged (); /* EMIT_SIGNAL */
1761 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1763 if (editing || _session == 0 || !dragging) {
1767 float pixel_frame_scale_factor = 0.2f;
1769 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1770 pixel_frame_scale_factor = 0.1f;
1774 if (Keyboard::modifier_state_contains (ev->state,
1775 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1777 pixel_frame_scale_factor = 0.025f;
1780 double y_delta = ev->y - drag_y;
1782 drag_accum += y_delta*pixel_frame_scale_factor;
1786 if (floor (drag_accum) != 0) {
1791 dir = (drag_accum < 0 ? 1:-1);
1792 pos = current_time();
1793 frames = get_frame_step (drag_field, pos, dir);
1795 if (frames != 0 && frames * drag_accum < current_time()) {
1796 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1802 ValueChanged(); /* EMIT_SIGNAL */
1809 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1812 Timecode::BBT_Time BBT;
1814 case Timecode_Hours:
1815 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1817 case Timecode_Minutes:
1818 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1820 case Timecode_Seconds:
1821 f = _session->frame_rate();
1823 case Timecode_Frames:
1824 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1832 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1835 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1838 f = (framecnt_t) _session->frame_rate();
1840 case MS_Milliseconds:
1841 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1848 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1854 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1860 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1863 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1872 AudioClock::current_time (framepos_t) const
1878 AudioClock::current_duration (framepos_t pos) const
1887 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1903 AudioClock::bbt_validate_edit (const string& str)
1907 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1911 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1915 if (!is_duration && any.bbt.bars == 0) {
1919 if (!is_duration && any.bbt.beats == 0) {
1927 AudioClock::timecode_validate_edit (const string&)
1933 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1934 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1939 TC.hours = hours * -1;
1943 TC.negative = false;
1946 if (TC.negative && !_negative_allowed) {
1950 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1954 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1958 if (_session->timecode_drop_frames()) {
1959 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1968 AudioClock::minsec_validate_edit (const string& str)
1970 int hrs, mins, secs, millisecs;
1972 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1976 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1984 AudioClock::frames_from_timecode_string (const string& str) const
1986 if (_session == 0) {
1995 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1996 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1999 TC.hours = abs(hours);
2000 TC.rate = _session->timecode_frames_per_second();
2001 TC.drop= _session->timecode_drop_frames();
2003 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2005 // timecode_tester ();
2006 if (edit_is_negative) {
2014 AudioClock::frames_from_minsec_string (const string& str) const
2016 if (_session == 0) {
2020 int hrs, mins, secs, millisecs;
2021 framecnt_t sr = _session->frame_rate();
2023 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2024 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2028 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2032 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2034 if (_session == 0) {
2035 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2040 any.type = AnyTime::BBT;
2042 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2049 return _session->any_duration_to_frames (pos, any);
2051 return _session->convert_to_frames (any);
2057 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2059 if (_session == 0) {
2060 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2064 Timecode::BBT_Time bbt;
2066 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2070 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2074 AudioClock::frames_from_audioframes_string (const string& str) const
2077 sscanf (str.c_str(), "%" PRId64, &f);
2082 AudioClock::build_ops_menu ()
2084 using namespace Menu_Helpers;
2085 ops_menu = new Menu;
2086 MenuList& ops_items = ops_menu->items();
2087 ops_menu->set_name ("ArdourContextMenu");
2089 if (!Profile->get_sae()) {
2090 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2092 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2093 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2094 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2096 if (editable && !_off && !is_duration && !_follows_playhead) {
2097 ops_items.push_back (SeparatorElem());
2098 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2099 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2104 AudioClock::set_from_playhead ()
2110 set (_session->transport_frame());
2115 AudioClock::locate ()
2117 if (!_session || is_duration) {
2121 _session->request_locate (current_time(), _session->transport_rolling ());
2125 AudioClock::set_mode (Mode m)
2135 _layout->set_text ("");
2139 _left_layout->set_attributes (info_attributes);
2140 _right_layout->set_attributes (info_attributes);
2141 /* adjust info_height according to font size */
2143 _left_layout->set_text (" 1234567890");
2144 _left_layout->get_pixel_size (ignored, info_height);
2146 _left_layout->set_text ("");
2147 _right_layout->set_text ("");
2152 mode_based_info_ratio = 0.6;
2153 insert_map.push_back (11);
2154 insert_map.push_back (10);
2155 insert_map.push_back (8);
2156 insert_map.push_back (7);
2157 insert_map.push_back (5);
2158 insert_map.push_back (4);
2159 insert_map.push_back (2);
2160 insert_map.push_back (1);
2164 mode_based_info_ratio = 0.5;
2165 insert_map.push_back (11);
2166 insert_map.push_back (10);
2167 insert_map.push_back (9);
2168 insert_map.push_back (8);
2169 insert_map.push_back (6);
2170 insert_map.push_back (5);
2171 insert_map.push_back (3);
2172 insert_map.push_back (2);
2173 insert_map.push_back (1);
2177 mode_based_info_ratio = 0.6;
2178 insert_map.push_back (12);
2179 insert_map.push_back (11);
2180 insert_map.push_back (10);
2181 insert_map.push_back (8);
2182 insert_map.push_back (7);
2183 insert_map.push_back (5);
2184 insert_map.push_back (4);
2185 insert_map.push_back (2);
2186 insert_map.push_back (1);
2190 mode_based_info_ratio = 0.45;
2194 set (last_when, true);
2196 if (!is_transient) {
2197 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2200 mode_changed (); /* EMIT SIGNAL (the member one) */
2204 AudioClock::set_bbt_reference (framepos_t pos)
2206 bbt_reference_time = pos;
2210 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2212 CairoWidget::on_style_changed (old_style);
2214 Gtk::Requisition req;
2215 set_clock_dimensions (req);
2217 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2223 AudioClock::set_editable (bool yn)
2229 AudioClock::set_is_duration (bool yn)
2231 if (yn == is_duration) {
2236 set (last_when, true);
2240 AudioClock::set_off (bool yn)
2248 /* force a redraw. last_when will be preserved, but the clock text will
2252 set (last_when, true);
2256 AudioClock::focus ()
2258 start_edit (Field (0));
2262 AudioClock::set_corner_radius (double r)
2271 AudioClock::dpi_reset ()
2273 /* force recomputation of size even if we are fixed width
2281 AudioClock::set_negative_allowed (bool yn)
2283 _negative_allowed = yn;