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 "audio_clock.h"
43 #include "gui_thread.h"
44 #include "ui_config.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.68;
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,
68 bool accept_on_focus_out)
71 , is_transient (transient)
72 , is_duration (duration)
73 , editable (allow_edit)
74 , _follows_playhead (follows_playhead)
75 , _accept_on_focus_out (accept_on_focus_out)
78 , _edit_by_click_field (false)
79 , _negative_allowed (false)
80 , edit_is_negative (false)
85 , style_resets_first (true)
90 , mode_based_info_ratio (1.0)
94 , bbt_reference_time (-1)
99 , drag_field (Field (0))
103 set_flags (CAN_FOCUS);
105 _layout = Pango::Layout::create (get_pango_context());
106 _layout->set_attributes (normal_attributes);
109 _left_layout = Pango::Layout::create (get_pango_context());
110 _right_layout = Pango::Layout::create (get_pango_context());
113 set_widget_name (widget_name);
115 _mode = BBT; /* lie to force mode switch */
117 set (last_when, true);
120 clocks.push_back (this);
123 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
124 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
127 AudioClock::~AudioClock ()
129 delete foreground_attr;
134 AudioClock::set_widget_name (const string& str)
139 set_name (str + " clock");
149 AudioClock::on_realize ()
151 Gtk::Requisition req;
153 CairoWidget::on_realize ();
155 set_clock_dimensions (req);
157 first_width = req.width;
158 first_height = req.height;
160 // XXX FIX ME: define font based on ... ???
166 AudioClock::set_font (Pango::FontDescription font)
168 Glib::RefPtr<Gtk::Style> style = get_style ();
169 Pango::AttrFontDesc* font_attr;
171 font_size = font.get_size();
172 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
174 normal_attributes.change (*font_attr);
175 editing_attributes.change (*font_attr);
177 /* now a smaller version of the same font */
180 font.set_size (INFO_FONT_SIZE);
181 font.set_weight (Pango::WEIGHT_NORMAL);
182 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
184 info_attributes.change (*font_attr);
186 /* and an even smaller one */
190 /* get the figure width for the font. This doesn't have to super
191 * accurate since we only use it to measure the (roughly 1 character)
192 * offset from the position Pango tells us for the "cursor"
195 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
199 tmp->get_pixel_size (em_width, ignore_height);
201 /* force redraw of markup with new font-size */
202 set (last_when, true);
206 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
208 CairoWidget::set_active_state (s);
213 AudioClock::set_colors ()
219 uint32_t editing_color;
220 uint32_t cursor_color;
222 if (active_state()) {
223 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
224 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
225 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
226 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
228 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
229 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
230 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
231 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
234 /* store for bg and cursor in render() */
236 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
243 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
250 /* rescale for Pango colors ... sigh */
252 r = lrint (r * 65535.0);
253 g = lrint (g * 65535.0);
254 b = lrint (b * 65535.0);
256 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
257 r = lrint ((r/255.0) * 65535.0);
258 g = lrint ((g/255.0) * 65535.0);
259 b = lrint ((b/255.0) * 65535.0);
260 delete foreground_attr;
261 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
263 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
264 r = lrint ((r/255.0) * 65535.0);
265 g = lrint ((g/255.0) * 65535.0);
266 b = lrint ((b/255.0) * 65535.0);
268 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
270 normal_attributes.change (*foreground_attr);
271 info_attributes.change (*foreground_attr);
272 editing_attributes.change (*foreground_attr);
273 editing_attributes.change (*editing_attr);
276 _layout->set_attributes (normal_attributes);
278 _layout->set_attributes (editing_attributes);
285 AudioClock::set_scale (double x, double y)
294 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
296 /* main layout: rounded rect, plus the text */
299 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
302 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
304 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
307 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
312 double lw = layout_width * xscale;
313 double lh = layout_height * yscale;
315 cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
317 if (xscale != 1.0 || yscale != 1.0) {
319 cairo_scale (cr, xscale, yscale);
322 pango_cairo_show_layout (cr, _layout->gobj());
324 if (xscale != 1.0 || yscale != 1.0) {
330 double h = get_height() - upper_height - separator_height;
333 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
336 if (mode_based_info_ratio != 1.0) {
338 double left_rect_width = get_left_rect_width();
342 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
343 left_rect_width + (separator_height == 0 ? corner_radius : 0),
346 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
351 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
352 pango_cairo_show_layout (cr, _left_layout->gobj());
356 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
357 upper_height + separator_height,
358 get_width() - separator_height - left_rect_width,
361 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
362 get_width() - separator_height - left_rect_width, h);
368 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
369 /* right-align does not work per se beacuse layout width is unset.
370 * Using _right_layout->set_width([value >=0]) would also enable
371 * word-wrapping which is not wanted here.
372 * The solution is to custom align the layout depending on its size.
373 * if it is larger than the available space it will be cropped on the
374 * right edge rather than override text on the left side.
377 _right_layout->get_pixel_size(rw, rh);
378 x = get_width() - rw - separator_height - x_leading_padding;
379 if (x < x_leading_padding + left_rect_width + separator_height) {
380 /* rather cut off the right end than overlap with the text on the left */
381 x = x_leading_padding + left_rect_width + separator_height;
383 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
385 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
387 pango_cairo_show_layout (cr, _right_layout->gobj());
390 /* no info to display, or just one */
394 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
396 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
404 if (!insert_map.empty()) {
406 int xcenter = (get_width() - layout_width) /2;
408 if (input_string.length() < insert_map.size()) {
409 Pango::Rectangle cursor;
411 if (input_string.empty()) {
412 /* nothing entered yet, put cursor at the end
415 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
417 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
420 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
422 min (get_width() - 2.0,
423 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
424 (upper_height - layout_height)/2.0,
425 2.0, cursor.get_height()/PANGO_SCALE);
428 /* we've entered all possible digits, no cursor */
432 if (input_string.empty()) {
433 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
436 (upper_height - layout_height)/2.0,
445 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
447 CairoWidget::on_size_allocate (alloc);
450 upper_height = (get_height()/2.0) - 1.0;
452 upper_height = get_height();
457 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
459 Glib::RefPtr<Pango::Layout> tmp;
460 Glib::RefPtr<Gtk::Style> style = get_style ();
461 Pango::FontDescription font;
463 tmp = Pango::Layout::create (get_pango_context());
465 if (!is_realized()) {
466 font = get_font_for_style (get_name());
468 font = style->get_font();
471 tmp->set_font_description (font);
473 /* this string is the longest thing we will ever display */
475 tmp->set_text (" 88:88:88,888 ");
477 tmp->set_text (" 88:88:88,88 ");
478 tmp->get_pixel_size (req.width, req.height);
480 layout_height = req.height;
481 layout_width = req.width;
485 AudioClock::on_size_request (Gtk::Requisition* req)
487 /* even for non fixed width clocks, the size we *ask* for never changes,
488 even though the size we receive might. so once we've computed it,
493 req->width = first_width;
494 req->height = first_height;
498 set_clock_dimensions (*req);
500 /* now tackle height, for which we need to know the height of the lower
506 Glib::RefPtr<Pango::Layout> tmp;
507 Glib::RefPtr<Gtk::Style> style = get_style ();
508 Pango::FontDescription font;
511 tmp = Pango::Layout::create (get_pango_context());
513 if (!is_realized()) {
514 font = get_font_for_style (get_name());
516 font = style->get_font();
519 tmp->set_font_description (font);
521 font.set_size (INFO_FONT_SIZE);
522 font.set_weight (Pango::WEIGHT_NORMAL);
523 tmp->set_font_description (font);
525 /* we only care about height, so put as much stuff in here
526 as possible that might change the height.
528 tmp->set_text ("qyhH|"); /* one ascender, one descender */
530 tmp->get_pixel_size (w, info_height);
532 /* silly extra padding that seems necessary to correct the info
533 * that pango just gave us. I have no idea why.
536 req->height += info_height;
537 req->height += separator_height;
542 AudioClock::show_edit_status (int length)
544 editing_attr->set_start_index (edit_string.length() - length);
545 editing_attr->set_end_index (edit_string.length());
547 editing_attributes.change (*foreground_attr);
548 editing_attributes.change (*editing_attr);
550 _layout->set_attributes (editing_attributes);
554 AudioClock::start_edit (Field f)
557 pre_edit_string = _layout->get_text ();
558 if (!insert_map.empty()) {
559 edit_string = pre_edit_string;
561 edit_string.clear ();
562 _layout->set_text ("");
565 input_string.clear ();
567 edit_is_negative = false;
570 input_string = get_field (f);
571 show_edit_status (merge_input_and_edit_string ());
572 _layout->set_text (edit_string);
577 Keyboard::magic_widget_grab_focus ();
583 AudioClock::get_field (Field f)
587 return edit_string.substr (1, 2);
589 case Timecode_Minutes:
590 return edit_string.substr (4, 2);
592 case Timecode_Seconds:
593 return edit_string.substr (7, 2);
595 case Timecode_Frames:
596 return edit_string.substr (10, 2);
599 return edit_string.substr (1, 2);
602 return edit_string.substr (4, 2);
605 return edit_string.substr (7, 2);
607 case MS_Milliseconds:
608 return edit_string.substr (10, 3);
611 return edit_string.substr (1, 3);
614 return edit_string.substr (5, 2);
617 return edit_string.substr (8, 4);
627 AudioClock::end_edit (bool modify)
635 ok = timecode_validate_edit (edit_string);
639 ok = bbt_validate_edit (edit_string);
643 ok = minsec_validate_edit (edit_string);
647 if (edit_string.length() < 1) {
648 edit_string = pre_edit_string;
654 edit_string = pre_edit_string;
655 input_string.clear ();
656 _layout->set_text (edit_string);
657 show_edit_status (0);
658 /* edit attributes remain in use */
662 framepos_t pos = 0; /* stupid gcc */
666 pos = frames_from_timecode_string (edit_string);
671 pos = frame_duration_from_bbt_string (0, edit_string);
673 pos = frames_from_bbt_string (0, edit_string);
678 pos = frames_from_minsec_string (edit_string);
682 pos = frames_from_audioframes_string (edit_string);
687 _layout->set_attributes (normal_attributes);
688 ValueChanged(); /* EMIT_SIGNAL */
694 edit_is_negative = false;
695 _layout->set_attributes (normal_attributes);
696 _layout->set_text (pre_edit_string);
707 AudioClock::drop_focus ()
709 Keyboard::magic_widget_drop_focus ();
713 /* move focus back to the default widget in the top level window */
715 Widget* top = get_toplevel();
717 if (top->is_toplevel ()) {
718 Window* win = dynamic_cast<Window*> (top);
725 AudioClock::parse_as_frames_distance (const std::string& str)
729 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
737 AudioClock::parse_as_minsec_distance (const std::string& str)
739 framecnt_t sr = _session->frame_rate();
745 switch (str.length()) {
752 sscanf (str.c_str(), "%" PRId32, &msecs);
753 return msecs * (sr / 1000);
756 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
757 return (secs * sr) + (msecs * (sr/1000));
760 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
761 return (secs * sr) + (msecs * (sr/1000));
764 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
765 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
768 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
769 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
772 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
773 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
776 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
777 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
787 AudioClock::parse_as_timecode_distance (const std::string& str)
789 double fps = _session->timecode_frames_per_second();
790 framecnt_t sr = _session->frame_rate();
796 switch (str.length()) {
801 sscanf (str.c_str(), "%" PRId32, &frames);
802 return llrint ((frames/(float)fps) * sr);
805 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
806 return (secs * sr) + llrint ((frames/(float)fps) * sr);
809 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
810 return (secs * sr) + llrint ((frames/(float)fps) * sr);
813 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
814 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
817 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
818 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
821 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
822 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
825 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
826 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
836 AudioClock::parse_as_bbt_distance (const std::string&)
842 AudioClock::parse_as_distance (const std::string& instr)
846 return parse_as_timecode_distance (instr);
849 return parse_as_frames_distance (instr);
852 return parse_as_bbt_distance (instr);
855 return parse_as_minsec_distance (instr);
862 AudioClock::end_edit_relative (bool add)
868 ok = timecode_validate_edit (edit_string);
872 ok = bbt_validate_edit (edit_string);
876 ok = minsec_validate_edit (edit_string);
884 edit_string = pre_edit_string;
885 input_string.clear ();
886 _layout->set_text (edit_string);
887 show_edit_status (0);
888 /* edit attributes remain in use */
893 framecnt_t frames = parse_as_distance (input_string);
898 _layout->set_attributes (normal_attributes);
902 set (current_time() + frames, true);
904 framepos_t c = current_time();
906 if (c > frames || _negative_allowed) {
907 set (c - frames, true);
912 ValueChanged (); /* EMIT SIGNAL */
915 input_string.clear ();
921 AudioClock::session_property_changed (const PropertyChange&)
923 set (last_when, true);
927 AudioClock::metric_position_changed ()
929 set (last_when, true);
933 AudioClock::session_configuration_changed (std::string p)
935 if (_negative_allowed) {
936 /* session option editor clock */
940 if (p == "sync-source" || p == "external-sync") {
941 set (current_time(), true);
945 if (p != "timecode-offset" && p != "timecode-offset-negative") {
954 current = current_duration ();
956 current = current_time ();
966 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
968 if ((!force && !is_visible()) || _session == 0) {
973 when = when - offset;
976 if (when == last_when && !force) {
977 #if 0 // XXX return if no change and no change forced. verify Aug/2014
978 if (_mode != Timecode && _mode != MinSec) {
979 /* may need to force display of TC source
980 * time, so don't return early.
982 /* ^^ Why was that?, delta times?
983 * Timecode FPS, pull-up/down, etc changes
984 * trigger a 'session_property_changed' which
985 * eventually calls set(last_when, true)
987 * re-rendering the clock every 40ms or so just
988 * because we can is not ideal.
999 _right_layout->set_alignment(Pango::ALIGN_LEFT);
1004 if (_right_layout) {
1005 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1007 set_timecode (when, force);
1011 set_bbt (when, force);
1015 if (_right_layout) {
1016 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1018 set_minsec (when, force);
1022 set_frames (when, force);
1032 AudioClock::set_slave_info ()
1034 if (!_left_layout || !_right_layout) {
1038 SyncSource sync_src = Config->get_sync_source();
1040 if (_session->config.get_external_sync()) {
1041 Slave* slave = _session->slave();
1045 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1046 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1047 _right_layout->set_text ("");
1051 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1052 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1053 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1054 INFO_FONT_SIZE, slave->approximate_current_delta()));
1056 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1057 INFO_FONT_SIZE, _("--pending--")));
1058 _right_layout->set_text ("");
1065 TimecodeSlave* tcslave;
1066 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1067 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1068 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1069 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1070 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1071 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1072 INFO_FONT_SIZE, slave->approximate_current_delta()));
1075 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1076 INFO_FONT_SIZE, _("--pending--")));
1077 _right_layout->set_text ("");
1082 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1083 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1084 _right_layout->set_text ("");
1089 AudioClock::set_frames (framepos_t when, bool /*force*/)
1092 bool negative = false;
1095 _layout->set_text (" ----------");
1098 _left_layout->set_text ("");
1099 _right_layout->set_text ("");
1111 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1113 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1116 _layout->set_text (buf);
1119 framecnt_t rate = _session->frame_rate();
1121 if (fmod (rate, 100.0) == 0.0) {
1122 sprintf (buf, "%.1fkHz", rate/1000.0);
1124 sprintf (buf, "%" PRId64 "Hz", rate);
1127 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1128 INFO_FONT_SIZE, _("SR"), buf));
1130 float vid_pullup = _session->config.get_video_pullup();
1132 if (vid_pullup == 0.0) {
1133 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1134 INFO_FONT_SIZE, _("Pull")));
1136 sprintf (buf, _("%+.4f%%"), vid_pullup);
1137 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1138 INFO_FONT_SIZE, _("Pull"), buf));
1144 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1161 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1162 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1163 mins = (int) floor (left / (frame_rate * 60.0f));
1164 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1165 secs = (int) floor (left / (float) frame_rate);
1166 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1167 millisecs = floor (left * 1000.0 / (float) frame_rate);
1170 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1172 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1178 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1183 _layout->set_text (" --:--:--.---");
1186 _left_layout->set_text ("");
1187 _right_layout->set_text ("");
1193 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1195 _layout->set_text (buf);
1200 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1203 bool negative = false;
1206 _layout->set_text (" --:--:--:--");
1208 _left_layout->set_text ("");
1209 _right_layout->set_text ("");
1221 _session->timecode_duration (when, TC);
1223 _session->timecode_time (when, TC);
1226 TC.negative = TC.negative || negative;
1228 _layout->set_text (Timecode::timecode_format_time(TC));
1234 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1237 Timecode::BBT_Time BBT;
1238 bool negative = false;
1241 _layout->set_text (" ---|--|----");
1243 _left_layout->set_text ("");
1244 _right_layout->set_text ("");
1254 /* handle a common case */
1261 BBT = _session->tempo_map().bbt_at_frame (when);
1266 BBT = _session->tempo_map().bbt_at_frame (when);
1270 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1271 BBT.bars, BBT.beats, BBT.ticks);
1273 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1274 BBT.bars, BBT.beats, BBT.ticks);
1277 _layout->set_text (buf);
1279 if (_right_layout) {
1282 if (bbt_reference_time < 0) {
1285 pos = bbt_reference_time;
1288 TempoMetric m (_session->tempo_map().metric_at (pos));
1290 sprintf (buf, "%-5.3f", _session->tempo_map().tempo_at_frame (pos).beats_per_minute());
1291 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1292 INFO_FONT_SIZE, buf, _("Tempo")));
1294 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1295 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1296 INFO_FONT_SIZE, buf, _("Meter")));
1301 AudioClock::set_session (Session *s)
1303 SessionHandlePtr::set_session (s);
1307 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1308 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1309 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::metric_position_changed, this), gui_context());
1311 XMLProperty const * prop;
1312 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1313 AudioClock::Mode amode;
1316 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1317 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1319 if ((prop = (*i)->property (X_("mode"))) != 0) {
1320 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1321 set_mode (amode, true);
1323 if ((prop = (*i)->property (X_("on"))) != 0) {
1324 set_off (!string_is_affirmative (prop->value()));
1331 set (last_when, true);
1336 AudioClock::on_key_press_event (GdkEventKey* ev)
1344 int highlight_length;
1347 switch (ev->keyval) {
1390 case GDK_KP_Subtract:
1391 if (_negative_allowed && input_string.empty()) {
1392 edit_is_negative = true;
1393 edit_string.replace(0,1,"-");
1394 _layout->set_text (edit_string);
1397 end_edit_relative (false);
1403 end_edit_relative (true);
1416 ChangeAborted(); /* EMIT SIGNAL */
1421 if (!input_string.empty()) {
1422 /* delete the last key entered
1424 input_string = input_string.substr (0, input_string.length() - 1);
1426 goto use_input_string;
1429 /* do not allow other keys to passthru to the rest of the GUI
1435 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1436 /* too many digits: eat the key event, but do nothing with it */
1440 input_string.push_back (new_char);
1446 /* get this one in the right order, and to the right width */
1447 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1448 edit_string = edit_string.substr (0, edit_string.length() - 1);
1450 edit_string.push_back (new_char);
1452 if (!edit_string.empty()) {
1454 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1455 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1458 /* highlight the whole thing */
1459 highlight_length = edit_string.length();
1463 highlight_length = merge_input_and_edit_string ();
1466 if (edit_is_negative) {
1467 edit_string.replace(0,1,"-");
1469 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1470 edit_string.replace(0,1,"_");
1472 edit_string.replace(0,1," ");
1476 show_edit_status (highlight_length);
1477 _layout->set_text (edit_string);
1484 AudioClock::merge_input_and_edit_string ()
1486 /* merge with pre-edit-string into edit string */
1488 edit_string = pre_edit_string;
1490 if (input_string.empty()) {
1494 string::size_type target;
1495 for (string::size_type i = 0; i < input_string.length(); ++i) {
1496 target = insert_map[input_string.length() - 1 - i];
1497 edit_string[target] = input_string[i];
1499 /* highlight from end to wherever the last character was added */
1500 return edit_string.length() - insert_map[input_string.length()-1];
1505 AudioClock::on_key_release_event (GdkEventKey *ev)
1511 /* return true for keys that we used on press
1512 so that they cannot possibly do double-duty
1514 switch (ev->keyval) {
1537 case GDK_KP_Decimal:
1545 case GDK_KP_Subtract:
1553 AudioClock::index_to_field (int index) const
1558 return Timecode_Hours;
1559 } else if (index < 7) {
1560 return Timecode_Minutes;
1561 } else if (index < 10) {
1562 return Timecode_Seconds;
1564 return Timecode_Frames;
1570 } else if (index < 7) {
1578 return Timecode_Hours;
1579 } else if (index < 6) {
1581 } else if (index < 9) {
1584 return MS_Milliseconds;
1596 AudioClock::on_button_press_event (GdkEventButton *ev)
1598 switch (ev->button) {
1600 if (editable && !_off) {
1606 /* the text has been centered vertically, so adjust
1609 int xcenter = (get_width() - layout_width) /2;
1611 y = ev->y - ((upper_height - layout_height)/2);
1612 x = ev->x - xcenter;
1614 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1615 /* pretend it is a character on the far right */
1618 drag_field = index_to_field (index);
1620 /* make absolutely sure that the pointer is grabbed */
1621 gdk_pointer_grab(ev->window,false ,
1622 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1623 NULL,NULL,ev->time);
1625 drag_start_y = ev->y;
1639 AudioClock::on_button_release_event (GdkEventButton *ev)
1641 if (editable && !_off) {
1643 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1645 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1646 // we actually dragged so return without
1647 // setting editing focus, or we shift clicked
1650 if (ev->button == 1) {
1652 if (_edit_by_click_field) {
1654 int xcenter = (get_width() - layout_width) /2;
1657 int y = ev->y - ((upper_height - layout_height)/2);
1658 int x = ev->x - xcenter;
1661 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1665 f = index_to_field (index);
1668 case Timecode_Frames:
1669 case MS_Milliseconds:
1685 if (Keyboard::is_context_menu_event (ev)) {
1686 if (ops_menu == 0) {
1689 ops_menu->popup (1, ev->time);
1697 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1699 bool ret = CairoWidget::on_focus_out_event (ev);
1702 end_edit (_accept_on_focus_out);
1709 AudioClock::on_scroll_event (GdkEventScroll *ev)
1714 if (editing || _session == 0 || !editable || _off) {
1721 /* the text has been centered vertically, so adjust
1725 int xcenter = (get_width() - layout_width) /2;
1726 y = ev->y - ((upper_height - layout_height)/2);
1727 x = ev->x - xcenter;
1729 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1730 /* not in the main layout */
1734 Field f = index_to_field (index);
1735 framepos_t frames = 0;
1737 switch (ev->direction) {
1740 frames = get_frame_step (f);
1742 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1745 set (current_time() + frames, true);
1746 ValueChanged (); /* EMIT_SIGNAL */
1750 case GDK_SCROLL_DOWN:
1751 frames = get_frame_step (f);
1753 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1757 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1760 set (current_time() - frames, true);
1763 ValueChanged (); /* EMIT_SIGNAL */
1776 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1778 if (editing || _session == 0 || !dragging) {
1782 float pixel_frame_scale_factor = 0.2f;
1784 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1785 pixel_frame_scale_factor = 0.1f;
1789 if (Keyboard::modifier_state_contains (ev->state,
1790 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1792 pixel_frame_scale_factor = 0.025f;
1795 double y_delta = ev->y - drag_y;
1797 drag_accum += y_delta*pixel_frame_scale_factor;
1801 if (floor (drag_accum) != 0) {
1806 dir = (drag_accum < 0 ? 1:-1);
1807 pos = current_time();
1808 frames = get_frame_step (drag_field, pos, dir);
1810 if (frames != 0 && frames * drag_accum < current_time()) {
1811 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1817 ValueChanged(); /* EMIT_SIGNAL */
1824 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1827 Timecode::BBT_Time BBT;
1829 case Timecode_Hours:
1830 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1832 case Timecode_Minutes:
1833 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1835 case Timecode_Seconds:
1836 f = _session->frame_rate();
1838 case Timecode_Frames:
1839 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1847 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1850 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1853 f = (framecnt_t) _session->frame_rate();
1855 case MS_Milliseconds:
1856 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1863 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1869 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1875 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1878 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1887 AudioClock::current_time (framepos_t) const
1893 AudioClock::current_duration (framepos_t pos) const
1902 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1918 AudioClock::bbt_validate_edit (const string& str)
1922 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1926 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1930 if (!is_duration && any.bbt.bars == 0) {
1934 if (!is_duration && any.bbt.beats == 0) {
1942 AudioClock::timecode_validate_edit (const string&)
1948 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1949 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1954 TC.hours = hours * -1;
1958 TC.negative = false;
1961 if (TC.negative && !_negative_allowed) {
1965 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1969 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1973 if (_session->timecode_drop_frames()) {
1974 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1983 AudioClock::minsec_validate_edit (const string& str)
1985 int hrs, mins, secs, millisecs;
1987 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1991 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1999 AudioClock::frames_from_timecode_string (const string& str) const
2001 if (_session == 0) {
2010 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2011 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2014 TC.hours = abs(hours);
2015 TC.rate = _session->timecode_frames_per_second();
2016 TC.drop= _session->timecode_drop_frames();
2018 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2020 // timecode_tester ();
2021 if (edit_is_negative) {
2029 AudioClock::frames_from_minsec_string (const string& str) const
2031 if (_session == 0) {
2035 int hrs, mins, secs, millisecs;
2036 framecnt_t sr = _session->frame_rate();
2038 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2039 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2043 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2047 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2049 if (_session == 0) {
2050 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2055 any.type = AnyTime::BBT;
2057 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2064 return _session->any_duration_to_frames (pos, any);
2066 return _session->convert_to_frames (any);
2072 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2074 if (_session == 0) {
2075 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2079 Timecode::BBT_Time bbt;
2081 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2085 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2089 AudioClock::frames_from_audioframes_string (const string& str) const
2092 sscanf (str.c_str(), "%" PRId64, &f);
2097 AudioClock::copy_text_to_clipboard () const
2101 val = pre_edit_string;
2103 val = _layout->get_text ();
2105 const size_t trim = val.find_first_not_of(" ");
2106 if (trim == string::npos) {
2107 assert(0); // empty clock, can't be right.
2110 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2111 cl->set_text (val.substr(trim));
2115 AudioClock::build_ops_menu ()
2117 using namespace Menu_Helpers;
2118 ops_menu = new Menu;
2119 MenuList& ops_items = ops_menu->items();
2120 ops_menu->set_name ("ArdourContextMenu");
2122 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2123 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2124 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2125 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2127 if (editable && !_off && !is_duration && !_follows_playhead) {
2128 ops_items.push_back (SeparatorElem());
2129 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2130 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2132 ops_items.push_back (SeparatorElem());
2133 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2137 AudioClock::set_from_playhead ()
2143 set (_session->transport_frame());
2148 AudioClock::locate ()
2150 if (!_session || is_duration) {
2154 _session->request_locate (current_time(), _session->transport_rolling ());
2158 AudioClock::set_mode (Mode m, bool noemit)
2168 _layout->set_text ("");
2170 Gtk::Requisition req;
2171 set_clock_dimensions (req);
2175 _left_layout->set_attributes (info_attributes);
2176 _right_layout->set_attributes (info_attributes);
2177 /* adjust info_height according to font size */
2179 _left_layout->set_text (" 1234567890");
2180 _left_layout->get_pixel_size (ignored, info_height);
2182 _left_layout->set_text ("");
2183 _right_layout->set_text ("");
2188 mode_based_info_ratio = 0.6;
2189 insert_map.push_back (11);
2190 insert_map.push_back (10);
2191 insert_map.push_back (8);
2192 insert_map.push_back (7);
2193 insert_map.push_back (5);
2194 insert_map.push_back (4);
2195 insert_map.push_back (2);
2196 insert_map.push_back (1);
2200 mode_based_info_ratio = 0.5;
2201 insert_map.push_back (11);
2202 insert_map.push_back (10);
2203 insert_map.push_back (9);
2204 insert_map.push_back (8);
2205 insert_map.push_back (6);
2206 insert_map.push_back (5);
2207 insert_map.push_back (3);
2208 insert_map.push_back (2);
2209 insert_map.push_back (1);
2213 mode_based_info_ratio = 0.6;
2214 insert_map.push_back (12);
2215 insert_map.push_back (11);
2216 insert_map.push_back (10);
2217 insert_map.push_back (8);
2218 insert_map.push_back (7);
2219 insert_map.push_back (5);
2220 insert_map.push_back (4);
2221 insert_map.push_back (2);
2222 insert_map.push_back (1);
2226 mode_based_info_ratio = 0.45;
2230 set (last_when, true);
2232 if (!is_transient && !noemit) {
2233 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2236 mode_changed (); /* EMIT SIGNAL (the member one) */
2240 AudioClock::set_bbt_reference (framepos_t pos)
2242 bbt_reference_time = pos;
2246 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2248 CairoWidget::on_style_changed (old_style);
2250 Gtk::Requisition req;
2251 set_clock_dimensions (req);
2253 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2259 AudioClock::set_editable (bool yn)
2265 AudioClock::set_is_duration (bool yn)
2267 if (yn == is_duration) {
2272 set (last_when, true);
2276 AudioClock::set_off (bool yn)
2284 /* force a redraw. last_when will be preserved, but the clock text will
2288 set (last_when, true);
2292 AudioClock::focus ()
2294 start_edit (Field (0));
2298 AudioClock::set_corner_radius (double r)
2307 AudioClock::dpi_reset ()
2309 /* force recomputation of size even if we are fixed width
2317 AudioClock::set_negative_allowed (bool yn)
2319 _negative_allowed = yn;