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,
69 bool accept_on_focus_out)
72 , is_transient (transient)
73 , is_duration (duration)
74 , editable (allow_edit)
75 , _follows_playhead (follows_playhead)
76 , _accept_on_focus_out (accept_on_focus_out)
79 , _edit_by_click_field (false)
80 , _negative_allowed (false)
81 , edit_is_negative (false)
86 , style_resets_first (true)
91 , mode_based_info_ratio (1.0)
95 , bbt_reference_time (-1)
100 , drag_field (Field (0))
104 set_flags (CAN_FOCUS);
106 _layout = Pango::Layout::create (get_pango_context());
107 _layout->set_attributes (normal_attributes);
110 _left_layout = Pango::Layout::create (get_pango_context());
111 _right_layout = Pango::Layout::create (get_pango_context());
114 set_widget_name (widget_name);
116 _mode = BBT; /* lie to force mode switch */
118 set (last_when, true);
121 clocks.push_back (this);
124 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
125 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
128 AudioClock::~AudioClock ()
130 delete foreground_attr;
135 AudioClock::set_widget_name (const string& str)
140 set_name (str + " clock");
150 AudioClock::on_realize ()
152 Gtk::Requisition req;
154 CairoWidget::on_realize ();
156 set_clock_dimensions (req);
158 first_width = req.width;
159 first_height = req.height;
161 // XXX FIX ME: define font based on ... ???
167 AudioClock::set_font (Pango::FontDescription font)
169 Glib::RefPtr<Gtk::Style> style = get_style ();
170 Pango::AttrFontDesc* font_attr;
172 font_size = font.get_size();
173 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
175 normal_attributes.change (*font_attr);
176 editing_attributes.change (*font_attr);
178 /* now a smaller version of the same font */
181 font.set_size (INFO_FONT_SIZE);
182 font.set_weight (Pango::WEIGHT_NORMAL);
183 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
185 info_attributes.change (*font_attr);
187 /* and an even smaller one */
191 /* get the figure width for the font. This doesn't have to super
192 * accurate since we only use it to measure the (roughly 1 character)
193 * offset from the position Pango tells us for the "cursor"
196 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
200 tmp->get_pixel_size (em_width, ignore_height);
202 /* force redraw of markup with new font-size */
203 set (last_when, true);
207 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
209 CairoWidget::set_active_state (s);
214 AudioClock::set_colors ()
220 uint32_t editing_color;
221 uint32_t cursor_color;
223 if (active_state()) {
224 bg_color = ARDOUR_UI::config()->color (string_compose ("%1 active: background", get_name()));
225 text_color = ARDOUR_UI::config()->color (string_compose ("%1 active: text", get_name()));
226 editing_color = ARDOUR_UI::config()->color (string_compose ("%1 active: edited text", get_name()));
227 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1 active: cursor", get_name()));
229 bg_color = ARDOUR_UI::config()->color (string_compose ("%1: background", get_name()));
230 text_color = ARDOUR_UI::config()->color (string_compose ("%1: text", get_name()));
231 editing_color = ARDOUR_UI::config()->color (string_compose ("%1: edited text", get_name()));
232 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1: cursor", get_name()));
235 /* store for bg and cursor in render() */
237 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
244 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
251 /* rescale for Pango colors ... sigh */
253 r = lrint (r * 65535.0);
254 g = lrint (g * 65535.0);
255 b = lrint (b * 65535.0);
257 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
258 r = lrint ((r/255.0) * 65535.0);
259 g = lrint ((g/255.0) * 65535.0);
260 b = lrint ((b/255.0) * 65535.0);
261 delete foreground_attr;
262 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
264 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
265 r = lrint ((r/255.0) * 65535.0);
266 g = lrint ((g/255.0) * 65535.0);
267 b = lrint ((b/255.0) * 65535.0);
269 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
271 normal_attributes.change (*foreground_attr);
272 info_attributes.change (*foreground_attr);
273 editing_attributes.change (*foreground_attr);
274 editing_attributes.change (*editing_attr);
277 _layout->set_attributes (normal_attributes);
279 _layout->set_attributes (editing_attributes);
286 AudioClock::set_scale (double x, double y)
295 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
297 /* main layout: rounded rect, plus the text */
300 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
303 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
305 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
308 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
313 double lw = layout_width * xscale;
314 double lh = layout_height * yscale;
316 cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
318 if (xscale != 1.0 || yscale != 1.0) {
320 cairo_scale (cr, xscale, yscale);
323 pango_cairo_show_layout (cr, _layout->gobj());
325 if (xscale != 1.0 || yscale != 1.0) {
331 double h = get_height() - upper_height - separator_height;
334 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
337 if (mode_based_info_ratio != 1.0) {
339 double left_rect_width = get_left_rect_width();
343 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
344 left_rect_width + (separator_height == 0 ? corner_radius : 0),
347 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
352 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
353 pango_cairo_show_layout (cr, _left_layout->gobj());
357 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
358 upper_height + separator_height,
359 get_width() - separator_height - left_rect_width,
362 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
363 get_width() - separator_height - left_rect_width, h);
369 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
370 /* right-align does not work per se beacuse layout width is unset.
371 * Using _right_layout->set_width([value >=0]) would also enable
372 * word-wrapping which is not wanted here.
373 * The solution is to custom align the layout depending on its size.
374 * if it is larger than the available space it will be cropped on the
375 * right edge rather than override text on the left side.
378 _right_layout->get_pixel_size(rw, rh);
379 x = get_width() - rw - separator_height - x_leading_padding;
380 if (x < x_leading_padding + left_rect_width + separator_height) {
381 /* rather cut off the right end than overlap with the text on the left */
382 x = x_leading_padding + left_rect_width + separator_height;
384 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
386 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
388 pango_cairo_show_layout (cr, _right_layout->gobj());
391 /* no info to display, or just one */
395 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
397 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
405 if (!insert_map.empty()) {
407 int xcenter = (get_width() - layout_width) /2;
409 if (input_string.length() < insert_map.size()) {
410 Pango::Rectangle cursor;
412 if (input_string.empty()) {
413 /* nothing entered yet, put cursor at the end
416 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
418 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
421 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
423 min (get_width() - 2.0,
424 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
425 (upper_height - layout_height)/2.0,
426 2.0, cursor.get_height()/PANGO_SCALE);
429 /* we've entered all possible digits, no cursor */
433 if (input_string.empty()) {
434 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
437 (upper_height - layout_height)/2.0,
446 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
448 CairoWidget::on_size_allocate (alloc);
451 upper_height = (get_height()/2.0) - 1.0;
453 upper_height = get_height();
458 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
460 Glib::RefPtr<Pango::Layout> tmp;
461 Glib::RefPtr<Gtk::Style> style = get_style ();
462 Pango::FontDescription font;
464 tmp = Pango::Layout::create (get_pango_context());
466 if (!is_realized()) {
467 font = get_font_for_style (get_name());
469 font = style->get_font();
472 tmp->set_font_description (font);
474 /* this string is the longest thing we will ever display */
476 tmp->set_text (" 88:88:88,888 ");
478 tmp->set_text (" 88:88:88,88 ");
479 tmp->get_pixel_size (req.width, req.height);
481 layout_height = req.height;
482 layout_width = req.width;
486 AudioClock::on_size_request (Gtk::Requisition* req)
488 /* even for non fixed width clocks, the size we *ask* for never changes,
489 even though the size we receive might. so once we've computed it,
494 req->width = first_width;
495 req->height = first_height;
499 set_clock_dimensions (*req);
501 /* now tackle height, for which we need to know the height of the lower
507 Glib::RefPtr<Pango::Layout> tmp;
508 Glib::RefPtr<Gtk::Style> style = get_style ();
509 Pango::FontDescription font;
512 tmp = Pango::Layout::create (get_pango_context());
514 if (!is_realized()) {
515 font = get_font_for_style (get_name());
517 font = style->get_font();
520 tmp->set_font_description (font);
522 font.set_size (INFO_FONT_SIZE);
523 font.set_weight (Pango::WEIGHT_NORMAL);
524 tmp->set_font_description (font);
526 /* we only care about height, so put as much stuff in here
527 as possible that might change the height.
529 tmp->set_text ("qyhH|"); /* one ascender, one descender */
531 tmp->get_pixel_size (w, info_height);
533 /* silly extra padding that seems necessary to correct the info
534 * that pango just gave us. I have no idea why.
537 req->height += info_height;
538 req->height += separator_height;
543 AudioClock::show_edit_status (int length)
545 editing_attr->set_start_index (edit_string.length() - length);
546 editing_attr->set_end_index (edit_string.length());
548 editing_attributes.change (*foreground_attr);
549 editing_attributes.change (*editing_attr);
551 _layout->set_attributes (editing_attributes);
555 AudioClock::start_edit (Field f)
558 pre_edit_string = _layout->get_text ();
559 if (!insert_map.empty()) {
560 edit_string = pre_edit_string;
562 edit_string.clear ();
563 _layout->set_text ("");
566 input_string.clear ();
568 edit_is_negative = false;
571 input_string = get_field (f);
572 show_edit_status (merge_input_and_edit_string ());
573 _layout->set_text (edit_string);
578 Keyboard::magic_widget_grab_focus ();
584 AudioClock::get_field (Field f)
588 return edit_string.substr (1, 2);
590 case Timecode_Minutes:
591 return edit_string.substr (4, 2);
593 case Timecode_Seconds:
594 return edit_string.substr (7, 2);
596 case Timecode_Frames:
597 return edit_string.substr (10, 2);
600 return edit_string.substr (1, 2);
603 return edit_string.substr (4, 2);
606 return edit_string.substr (7, 2);
608 case MS_Milliseconds:
609 return edit_string.substr (10, 3);
612 return edit_string.substr (1, 3);
615 return edit_string.substr (5, 2);
618 return edit_string.substr (8, 4);
628 AudioClock::end_edit (bool modify)
636 ok = timecode_validate_edit (edit_string);
640 ok = bbt_validate_edit (edit_string);
644 ok = minsec_validate_edit (edit_string);
648 if (edit_string.length() < 1) {
649 edit_string = pre_edit_string;
655 edit_string = pre_edit_string;
656 input_string.clear ();
657 _layout->set_text (edit_string);
658 show_edit_status (0);
659 /* edit attributes remain in use */
663 framepos_t pos = 0; /* stupid gcc */
667 pos = frames_from_timecode_string (edit_string);
672 pos = frame_duration_from_bbt_string (0, edit_string);
674 pos = frames_from_bbt_string (0, edit_string);
679 pos = frames_from_minsec_string (edit_string);
683 pos = frames_from_audioframes_string (edit_string);
688 _layout->set_attributes (normal_attributes);
689 ValueChanged(); /* EMIT_SIGNAL */
695 edit_is_negative = false;
696 _layout->set_attributes (normal_attributes);
697 _layout->set_text (pre_edit_string);
708 AudioClock::drop_focus ()
710 Keyboard::magic_widget_drop_focus ();
714 /* move focus back to the default widget in the top level window */
716 Widget* top = get_toplevel();
718 if (top->is_toplevel ()) {
719 Window* win = dynamic_cast<Window*> (top);
726 AudioClock::parse_as_frames_distance (const std::string& str)
730 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
738 AudioClock::parse_as_minsec_distance (const std::string& str)
740 framecnt_t sr = _session->frame_rate();
746 switch (str.length()) {
753 sscanf (str.c_str(), "%" PRId32, &msecs);
754 return msecs * (sr / 1000);
757 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
758 return (secs * sr) + (msecs * (sr/1000));
761 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
762 return (secs * sr) + (msecs * (sr/1000));
765 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
766 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
769 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
770 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
773 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
774 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
777 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
778 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
788 AudioClock::parse_as_timecode_distance (const std::string& str)
790 double fps = _session->timecode_frames_per_second();
791 framecnt_t sr = _session->frame_rate();
797 switch (str.length()) {
802 sscanf (str.c_str(), "%" PRId32, &frames);
803 return llrint ((frames/(float)fps) * sr);
806 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
807 return (secs * sr) + llrint ((frames/(float)fps) * sr);
810 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
811 return (secs * sr) + llrint ((frames/(float)fps) * sr);
814 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
815 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
818 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
819 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
822 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
823 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
826 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
827 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
837 AudioClock::parse_as_bbt_distance (const std::string&)
843 AudioClock::parse_as_distance (const std::string& instr)
847 return parse_as_timecode_distance (instr);
850 return parse_as_frames_distance (instr);
853 return parse_as_bbt_distance (instr);
856 return parse_as_minsec_distance (instr);
863 AudioClock::end_edit_relative (bool add)
869 ok = timecode_validate_edit (edit_string);
873 ok = bbt_validate_edit (edit_string);
877 ok = minsec_validate_edit (edit_string);
885 edit_string = pre_edit_string;
886 input_string.clear ();
887 _layout->set_text (edit_string);
888 show_edit_status (0);
889 /* edit attributes remain in use */
894 framecnt_t frames = parse_as_distance (input_string);
899 _layout->set_attributes (normal_attributes);
903 set (current_time() + frames, true);
905 framepos_t c = current_time();
907 if (c > frames || _negative_allowed) {
908 set (c - frames, true);
913 ValueChanged (); /* EMIT SIGNAL */
916 input_string.clear ();
922 AudioClock::session_property_changed (const PropertyChange&)
924 set (last_when, true);
928 AudioClock::session_configuration_changed (std::string p)
930 if (_negative_allowed) {
931 /* session option editor clock */
935 if (p == "sync-source" || p == "external-sync") {
936 set (current_time(), true);
940 if (p != "timecode-offset" && p != "timecode-offset-negative") {
949 current = current_duration ();
951 current = current_time ();
961 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
963 if ((!force && !is_visible()) || _session == 0) {
968 when = when - offset;
971 if (when == last_when && !force) {
972 #if 0 // XXX return if no change and no change forced. verify Aug/2014
973 if (_mode != Timecode && _mode != MinSec) {
974 /* may need to force display of TC source
975 * time, so don't return early.
977 /* ^^ Why was that?, delta times?
978 * Timecode FPS, pull-up/down, etc changes
979 * trigger a 'session_property_changed' which
980 * eventually calls set(last_when, true)
982 * re-rendering the clock every 40ms or so just
983 * because we can is not ideal.
994 _right_layout->set_alignment(Pango::ALIGN_LEFT);
1000 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1002 set_timecode (when, force);
1006 set_bbt (when, force);
1010 if (_right_layout) {
1011 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1013 set_minsec (when, force);
1017 set_frames (when, force);
1027 AudioClock::set_slave_info ()
1029 if (!_left_layout || !_right_layout) {
1033 SyncSource sync_src = Config->get_sync_source();
1035 if (_session->config.get_external_sync()) {
1036 Slave* slave = _session->slave();
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_text ("");
1046 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1047 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1048 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1049 INFO_FONT_SIZE, slave->approximate_current_delta()));
1051 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1052 INFO_FONT_SIZE, _("--pending--")));
1053 _right_layout->set_text ("");
1060 TimecodeSlave* tcslave;
1061 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1062 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1063 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1064 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1065 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1066 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1067 INFO_FONT_SIZE, slave->approximate_current_delta()));
1070 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1071 INFO_FONT_SIZE, _("--pending--")));
1072 _right_layout->set_text ("");
1077 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1078 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1079 _right_layout->set_text ("");
1084 AudioClock::set_frames (framepos_t when, bool /*force*/)
1087 bool negative = false;
1090 _layout->set_text (" ----------");
1093 _left_layout->set_text ("");
1094 _right_layout->set_text ("");
1106 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1108 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1111 _layout->set_text (buf);
1114 framecnt_t rate = _session->frame_rate();
1116 if (fmod (rate, 100.0) == 0.0) {
1117 sprintf (buf, "%.1fkHz", rate/1000.0);
1119 sprintf (buf, "%" PRId64 "Hz", rate);
1122 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1123 INFO_FONT_SIZE, _("SR"), buf));
1125 float vid_pullup = _session->config.get_video_pullup();
1127 if (vid_pullup == 0.0) {
1128 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1129 INFO_FONT_SIZE, _("Pull")));
1131 sprintf (buf, _("%+.4f%%"), vid_pullup);
1132 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1133 INFO_FONT_SIZE, _("Pull"), buf));
1139 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1156 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1157 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1158 mins = (int) floor (left / (frame_rate * 60.0f));
1159 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1160 secs = (int) floor (left / (float) frame_rate);
1161 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1162 millisecs = floor (left * 1000.0 / (float) frame_rate);
1165 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1167 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1173 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1178 _layout->set_text (" --:--:--.---");
1181 _left_layout->set_text ("");
1182 _right_layout->set_text ("");
1188 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1190 _layout->set_text (buf);
1195 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1198 bool negative = false;
1201 _layout->set_text (" --:--:--:--");
1203 _left_layout->set_text ("");
1204 _right_layout->set_text ("");
1216 _session->timecode_duration (when, TC);
1218 _session->timecode_time (when, TC);
1221 TC.negative = TC.negative || negative;
1223 _layout->set_text (Timecode::timecode_format_time(TC));
1229 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1232 Timecode::BBT_Time BBT;
1233 bool negative = false;
1236 _layout->set_text (" ---|--|----");
1238 _left_layout->set_text ("");
1239 _right_layout->set_text ("");
1249 /* handle a common case */
1256 _session->tempo_map().bbt_time (when, BBT);
1261 _session->tempo_map().bbt_time (when, BBT);
1265 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1266 BBT.bars, BBT.beats, BBT.ticks);
1268 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1269 BBT.bars, BBT.beats, BBT.ticks);
1272 _layout->set_text (buf);
1274 if (_right_layout) {
1277 if (bbt_reference_time < 0) {
1280 pos = bbt_reference_time;
1283 TempoMetric m (_session->tempo_map().metric_at (pos));
1285 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1286 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1287 INFO_FONT_SIZE, buf, _("Tempo")));
1289 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1290 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1291 INFO_FONT_SIZE, buf, _("Meter")));
1296 AudioClock::set_session (Session *s)
1298 SessionHandlePtr::set_session (s);
1302 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1303 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1305 const XMLProperty* prop;
1306 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1307 AudioClock::Mode amode;
1310 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1311 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1313 if ((prop = (*i)->property (X_("mode"))) != 0) {
1314 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1317 if ((prop = (*i)->property (X_("on"))) != 0) {
1318 set_off (!string_is_affirmative (prop->value()));
1325 set (last_when, true);
1330 AudioClock::on_key_press_event (GdkEventKey* ev)
1338 int highlight_length;
1341 switch (ev->keyval) {
1384 case GDK_KP_Subtract:
1385 if (_negative_allowed && input_string.empty()) {
1386 edit_is_negative = true;
1387 edit_string.replace(0,1,"-");
1388 _layout->set_text (edit_string);
1391 end_edit_relative (false);
1397 end_edit_relative (true);
1410 ChangeAborted(); /* EMIT SIGNAL */
1415 if (!input_string.empty()) {
1416 /* delete the last key entered
1418 input_string = input_string.substr (0, input_string.length() - 1);
1420 goto use_input_string;
1426 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1427 /* too many digits: eat the key event, but do nothing with it */
1431 input_string.push_back (new_char);
1437 /* get this one in the right order, and to the right width */
1438 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1439 edit_string = edit_string.substr (0, edit_string.length() - 1);
1441 edit_string.push_back (new_char);
1443 if (!edit_string.empty()) {
1445 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1446 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1449 /* highlight the whole thing */
1450 highlight_length = edit_string.length();
1454 highlight_length = merge_input_and_edit_string ();
1457 if (edit_is_negative) {
1458 edit_string.replace(0,1,"-");
1460 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1461 edit_string.replace(0,1,"_");
1463 edit_string.replace(0,1," ");
1467 show_edit_status (highlight_length);
1468 _layout->set_text (edit_string);
1475 AudioClock::merge_input_and_edit_string ()
1477 /* merge with pre-edit-string into edit string */
1479 edit_string = pre_edit_string;
1481 if (input_string.empty()) {
1485 string::size_type target;
1486 for (string::size_type i = 0; i < input_string.length(); ++i) {
1487 target = insert_map[input_string.length() - 1 - i];
1488 edit_string[target] = input_string[i];
1490 /* highlight from end to wherever the last character was added */
1491 return edit_string.length() - insert_map[input_string.length()-1];
1496 AudioClock::on_key_release_event (GdkEventKey *ev)
1502 /* return true for keys that we used on press
1503 so that they cannot possibly do double-duty
1505 switch (ev->keyval) {
1528 case GDK_KP_Decimal:
1536 case GDK_KP_Subtract:
1544 AudioClock::index_to_field (int index) const
1549 return Timecode_Hours;
1550 } else if (index < 7) {
1551 return Timecode_Minutes;
1552 } else if (index < 10) {
1553 return Timecode_Seconds;
1555 return Timecode_Frames;
1561 } else if (index < 7) {
1569 return Timecode_Hours;
1570 } else if (index < 6) {
1572 } else if (index < 9) {
1575 return MS_Milliseconds;
1587 AudioClock::on_button_press_event (GdkEventButton *ev)
1589 switch (ev->button) {
1591 if (editable && !_off) {
1597 /* the text has been centered vertically, so adjust
1600 int xcenter = (get_width() - layout_width) /2;
1602 y = ev->y - ((upper_height - layout_height)/2);
1603 x = ev->x - xcenter;
1605 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1606 /* pretend it is a character on the far right */
1609 drag_field = index_to_field (index);
1611 /* make absolutely sure that the pointer is grabbed */
1612 gdk_pointer_grab(ev->window,false ,
1613 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1614 NULL,NULL,ev->time);
1616 drag_start_y = ev->y;
1630 AudioClock::on_button_release_event (GdkEventButton *ev)
1632 if (editable && !_off) {
1634 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1636 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1637 // we actually dragged so return without
1638 // setting editing focus, or we shift clicked
1641 if (ev->button == 1) {
1643 if (_edit_by_click_field) {
1645 int xcenter = (get_width() - layout_width) /2;
1648 int y = ev->y - ((upper_height - layout_height)/2);
1649 int x = ev->x - xcenter;
1652 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1656 f = index_to_field (index);
1659 case Timecode_Frames:
1660 case MS_Milliseconds:
1676 if (Keyboard::is_context_menu_event (ev)) {
1677 if (ops_menu == 0) {
1680 ops_menu->popup (1, ev->time);
1688 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1690 bool ret = CairoWidget::on_focus_out_event (ev);
1693 end_edit (_accept_on_focus_out);
1700 AudioClock::on_scroll_event (GdkEventScroll *ev)
1705 if (editing || _session == 0 || !editable || _off) {
1712 /* the text has been centered vertically, so adjust
1716 int xcenter = (get_width() - layout_width) /2;
1717 y = ev->y - ((upper_height - layout_height)/2);
1718 x = ev->x - xcenter;
1720 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1721 /* not in the main layout */
1725 Field f = index_to_field (index);
1726 framepos_t frames = 0;
1728 switch (ev->direction) {
1731 frames = get_frame_step (f);
1733 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1736 set (current_time() + frames, true);
1737 ValueChanged (); /* EMIT_SIGNAL */
1741 case GDK_SCROLL_DOWN:
1742 frames = get_frame_step (f);
1744 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1748 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1751 set (current_time() - frames, true);
1754 ValueChanged (); /* EMIT_SIGNAL */
1767 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1769 if (editing || _session == 0 || !dragging) {
1773 float pixel_frame_scale_factor = 0.2f;
1775 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1776 pixel_frame_scale_factor = 0.1f;
1780 if (Keyboard::modifier_state_contains (ev->state,
1781 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1783 pixel_frame_scale_factor = 0.025f;
1786 double y_delta = ev->y - drag_y;
1788 drag_accum += y_delta*pixel_frame_scale_factor;
1792 if (floor (drag_accum) != 0) {
1797 dir = (drag_accum < 0 ? 1:-1);
1798 pos = current_time();
1799 frames = get_frame_step (drag_field, pos, dir);
1801 if (frames != 0 && frames * drag_accum < current_time()) {
1802 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1808 ValueChanged(); /* EMIT_SIGNAL */
1815 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1818 Timecode::BBT_Time BBT;
1820 case Timecode_Hours:
1821 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1823 case Timecode_Minutes:
1824 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1826 case Timecode_Seconds:
1827 f = _session->frame_rate();
1829 case Timecode_Frames:
1830 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1838 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1841 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1844 f = (framecnt_t) _session->frame_rate();
1846 case MS_Milliseconds:
1847 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1854 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1860 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1866 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1869 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1878 AudioClock::current_time (framepos_t) const
1884 AudioClock::current_duration (framepos_t pos) const
1893 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1909 AudioClock::bbt_validate_edit (const string& str)
1913 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1917 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1921 if (!is_duration && any.bbt.bars == 0) {
1925 if (!is_duration && any.bbt.beats == 0) {
1933 AudioClock::timecode_validate_edit (const string&)
1939 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1940 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1945 TC.hours = hours * -1;
1949 TC.negative = false;
1952 if (TC.negative && !_negative_allowed) {
1956 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1960 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1964 if (_session->timecode_drop_frames()) {
1965 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1974 AudioClock::minsec_validate_edit (const string& str)
1976 int hrs, mins, secs, millisecs;
1978 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1982 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1990 AudioClock::frames_from_timecode_string (const string& str) const
1992 if (_session == 0) {
2001 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2002 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2005 TC.hours = abs(hours);
2006 TC.rate = _session->timecode_frames_per_second();
2007 TC.drop= _session->timecode_drop_frames();
2009 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2011 // timecode_tester ();
2012 if (edit_is_negative) {
2020 AudioClock::frames_from_minsec_string (const string& str) const
2022 if (_session == 0) {
2026 int hrs, mins, secs, millisecs;
2027 framecnt_t sr = _session->frame_rate();
2029 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2030 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2034 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2038 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2040 if (_session == 0) {
2041 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2046 any.type = AnyTime::BBT;
2048 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2055 return _session->any_duration_to_frames (pos, any);
2057 return _session->convert_to_frames (any);
2063 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2065 if (_session == 0) {
2066 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2070 Timecode::BBT_Time bbt;
2072 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2076 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2080 AudioClock::frames_from_audioframes_string (const string& str) const
2083 sscanf (str.c_str(), "%" PRId64, &f);
2088 AudioClock::copy_text_to_clipboard () const
2092 val = pre_edit_string;
2094 val = _layout->get_text ();
2096 const size_t trim = val.find_first_not_of(" ");
2097 if (trim == string::npos) {
2098 assert(0); // empty clock, can't be right.
2101 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2102 cl->set_text (val.substr(trim));
2106 AudioClock::build_ops_menu ()
2108 using namespace Menu_Helpers;
2109 ops_menu = new Menu;
2110 MenuList& ops_items = ops_menu->items();
2111 ops_menu->set_name ("ArdourContextMenu");
2113 if (!Profile->get_sae()) {
2114 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2116 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2117 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2118 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2120 if (editable && !_off && !is_duration && !_follows_playhead) {
2121 ops_items.push_back (SeparatorElem());
2122 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2123 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2125 ops_items.push_back (SeparatorElem());
2126 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2130 AudioClock::set_from_playhead ()
2136 set (_session->transport_frame());
2141 AudioClock::locate ()
2143 if (!_session || is_duration) {
2147 _session->request_locate (current_time(), _session->transport_rolling ());
2151 AudioClock::set_mode (Mode m)
2161 _layout->set_text ("");
2163 Gtk::Requisition req;
2164 set_clock_dimensions (req);
2168 _left_layout->set_attributes (info_attributes);
2169 _right_layout->set_attributes (info_attributes);
2170 /* adjust info_height according to font size */
2172 _left_layout->set_text (" 1234567890");
2173 _left_layout->get_pixel_size (ignored, info_height);
2175 _left_layout->set_text ("");
2176 _right_layout->set_text ("");
2181 mode_based_info_ratio = 0.6;
2182 insert_map.push_back (11);
2183 insert_map.push_back (10);
2184 insert_map.push_back (8);
2185 insert_map.push_back (7);
2186 insert_map.push_back (5);
2187 insert_map.push_back (4);
2188 insert_map.push_back (2);
2189 insert_map.push_back (1);
2193 mode_based_info_ratio = 0.5;
2194 insert_map.push_back (11);
2195 insert_map.push_back (10);
2196 insert_map.push_back (9);
2197 insert_map.push_back (8);
2198 insert_map.push_back (6);
2199 insert_map.push_back (5);
2200 insert_map.push_back (3);
2201 insert_map.push_back (2);
2202 insert_map.push_back (1);
2206 mode_based_info_ratio = 0.6;
2207 insert_map.push_back (12);
2208 insert_map.push_back (11);
2209 insert_map.push_back (10);
2210 insert_map.push_back (8);
2211 insert_map.push_back (7);
2212 insert_map.push_back (5);
2213 insert_map.push_back (4);
2214 insert_map.push_back (2);
2215 insert_map.push_back (1);
2219 mode_based_info_ratio = 0.45;
2223 set (last_when, true);
2225 if (!is_transient) {
2226 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2229 mode_changed (); /* EMIT SIGNAL (the member one) */
2233 AudioClock::set_bbt_reference (framepos_t pos)
2235 bbt_reference_time = pos;
2239 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2241 CairoWidget::on_style_changed (old_style);
2243 Gtk::Requisition req;
2244 set_clock_dimensions (req);
2246 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2252 AudioClock::set_editable (bool yn)
2258 AudioClock::set_is_duration (bool yn)
2260 if (yn == is_duration) {
2265 set (last_when, true);
2269 AudioClock::set_off (bool yn)
2277 /* force a redraw. last_when will be preserved, but the clock text will
2281 set (last_when, true);
2285 AudioClock::focus ()
2287 start_edit (Field (0));
2291 AudioClock::set_corner_radius (double r)
2300 AudioClock::dpi_reset ()
2302 /* force recomputation of size even if we are fixed width
2310 AudioClock::set_negative_allowed (bool yn)
2312 _negative_allowed = yn;