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))
99 set_flags (CAN_FOCUS);
101 _layout = Pango::Layout::create (get_pango_context());
102 _layout->set_attributes (normal_attributes);
105 _left_layout = Pango::Layout::create (get_pango_context());
106 _right_layout = Pango::Layout::create (get_pango_context());
109 set_widget_name (widget_name);
111 _mode = BBT; /* lie to force mode switch */
113 set (last_when, true);
116 clocks.push_back (this);
119 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
120 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
123 AudioClock::~AudioClock ()
125 delete foreground_attr;
130 AudioClock::set_widget_name (const string& str)
135 set_name (str + " clock");
145 AudioClock::on_realize ()
147 Gtk::Requisition req;
149 CairoWidget::on_realize ();
151 set_clock_dimensions (req);
153 first_width = req.width;
154 first_height = req.height;
156 // XXX FIX ME: define font based on ... ???
162 AudioClock::set_font (Pango::FontDescription font)
164 Glib::RefPtr<Gtk::Style> style = get_style ();
165 Pango::AttrFontDesc* font_attr;
167 font_size = font.get_size();
168 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
170 normal_attributes.change (*font_attr);
171 editing_attributes.change (*font_attr);
173 /* now a smaller version of the same font */
176 font.set_size (INFO_FONT_SIZE);
177 font.set_weight (Pango::WEIGHT_NORMAL);
178 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
180 info_attributes.change (*font_attr);
182 /* and an even smaller one */
186 /* get the figure width for the font. This doesn't have to super
187 * accurate since we only use it to measure the (roughly 1 character)
188 * offset from the position Pango tells us for the "cursor"
191 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
195 tmp->get_pixel_size (em_width, ignore_height);
197 /* force redraw of markup with new font-size */
198 set (last_when, true);
202 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
204 CairoWidget::set_active_state (s);
209 AudioClock::set_colors ()
215 uint32_t editing_color;
216 uint32_t cursor_color;
218 if (active_state()) {
219 bg_color = ARDOUR_UI::config()->color (string_compose ("%1 active: background", get_name()));
220 text_color = ARDOUR_UI::config()->color (string_compose ("%1 active: text", get_name()));
221 editing_color = ARDOUR_UI::config()->color (string_compose ("%1 active: edited text", get_name()));
222 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1 active: cursor", get_name()));
224 bg_color = ARDOUR_UI::config()->color (string_compose ("%1: background", get_name()));
225 text_color = ARDOUR_UI::config()->color (string_compose ("%1: text", get_name()));
226 editing_color = ARDOUR_UI::config()->color (string_compose ("%1: edited text", get_name()));
227 cursor_color = ARDOUR_UI::config()->color (string_compose ("%1: cursor", get_name()));
230 /* store for bg and cursor in render() */
232 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
239 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
246 /* rescale for Pango colors ... sigh */
248 r = lrint (r * 65535.0);
249 g = lrint (g * 65535.0);
250 b = lrint (b * 65535.0);
252 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
253 r = lrint ((r/255.0) * 65535.0);
254 g = lrint ((g/255.0) * 65535.0);
255 b = lrint ((b/255.0) * 65535.0);
256 delete foreground_attr;
257 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
259 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
260 r = lrint ((r/255.0) * 65535.0);
261 g = lrint ((g/255.0) * 65535.0);
262 b = lrint ((b/255.0) * 65535.0);
264 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
266 normal_attributes.change (*foreground_attr);
267 info_attributes.change (*foreground_attr);
268 editing_attributes.change (*foreground_attr);
269 editing_attributes.change (*editing_attr);
272 _layout->set_attributes (normal_attributes);
274 _layout->set_attributes (editing_attributes);
281 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
283 /* main layout: rounded rect, plus the text */
286 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
289 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
291 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
294 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
299 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
301 pango_cairo_show_layout (cr, _layout->gobj());
305 double h = get_height() - upper_height - separator_height;
308 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
311 if (mode_based_info_ratio != 1.0) {
313 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
317 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
318 left_rect_width + (separator_height == 0 ? corner_radius : 0),
321 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
326 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
327 pango_cairo_show_layout (cr, _left_layout->gobj());
331 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
332 upper_height + separator_height,
333 get_width() - separator_height - left_rect_width,
336 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
337 get_width() - separator_height - left_rect_width, h);
343 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
344 /* right-align does not work per se beacuse layout width is unset.
345 * Using _right_layout->set_width([value >=0]) would also enable
346 * word-wrapping which is not wanted here.
347 * The solution is to custom align the layout depending on its size.
348 * if it is larger than the available space it will be cropped on the
349 * right edge rather than override text on the left side.
352 _right_layout->get_pixel_size(rw, rh);
353 x = get_width() - rw - separator_height - x_leading_padding;
354 if (x < x_leading_padding + left_rect_width + separator_height) {
355 /* rather cut off the right end than overlap with the text on the left */
356 x = x_leading_padding + left_rect_width + separator_height;
358 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
360 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
362 pango_cairo_show_layout (cr, _right_layout->gobj());
365 /* no info to display, or just one */
369 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
371 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
379 if (!insert_map.empty()) {
381 int xcenter = (get_width() - layout_width) /2;
383 if (input_string.length() < insert_map.size()) {
384 Pango::Rectangle cursor;
386 if (input_string.empty()) {
387 /* nothing entered yet, put cursor at the end
390 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
392 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
395 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
397 min (get_width() - 2.0,
398 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
399 (upper_height - layout_height)/2.0,
400 2.0, cursor.get_height()/PANGO_SCALE);
403 /* we've entered all possible digits, no cursor */
407 if (input_string.empty()) {
408 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
411 (upper_height - layout_height)/2.0,
420 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
422 CairoWidget::on_size_allocate (alloc);
425 upper_height = (get_height()/2.0) - 1.0;
427 upper_height = get_height();
432 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
434 Glib::RefPtr<Pango::Layout> tmp;
435 Glib::RefPtr<Gtk::Style> style = get_style ();
436 Pango::FontDescription font;
438 tmp = Pango::Layout::create (get_pango_context());
440 if (!is_realized()) {
441 font = get_font_for_style (get_name());
443 font = style->get_font();
446 tmp->set_font_description (font);
448 /* this string is the longest thing we will ever display */
449 tmp->set_text (" 88:88:88,888");
450 tmp->get_pixel_size (req.width, req.height);
452 layout_height = req.height;
453 layout_width = req.width;
457 AudioClock::on_size_request (Gtk::Requisition* req)
459 /* even for non fixed width clocks, the size we *ask* for never changes,
460 even though the size we receive might. so once we've computed it,
465 req->width = first_width;
466 req->height = first_height;
470 set_clock_dimensions (*req);
472 /* now tackle height, for which we need to know the height of the lower
478 Glib::RefPtr<Pango::Layout> tmp;
479 Glib::RefPtr<Gtk::Style> style = get_style ();
480 Pango::FontDescription font;
483 tmp = Pango::Layout::create (get_pango_context());
485 if (!is_realized()) {
486 font = get_font_for_style (get_name());
488 font = style->get_font();
491 tmp->set_font_description (font);
493 font.set_size (INFO_FONT_SIZE);
494 font.set_weight (Pango::WEIGHT_NORMAL);
495 tmp->set_font_description (font);
497 /* we only care about height, so put as much stuff in here
498 as possible that might change the height.
500 tmp->set_text ("qyhH|"); /* one ascender, one descender */
502 tmp->get_pixel_size (w, info_height);
504 /* silly extra padding that seems necessary to correct the info
505 * that pango just gave us. I have no idea why.
508 req->height += info_height;
509 req->height += separator_height;
514 AudioClock::show_edit_status (int length)
516 editing_attr->set_start_index (edit_string.length() - length);
517 editing_attr->set_end_index (edit_string.length());
519 editing_attributes.change (*foreground_attr);
520 editing_attributes.change (*editing_attr);
522 _layout->set_attributes (editing_attributes);
526 AudioClock::start_edit (Field f)
529 pre_edit_string = _layout->get_text ();
530 if (!insert_map.empty()) {
531 edit_string = pre_edit_string;
533 edit_string.clear ();
534 _layout->set_text ("");
537 input_string.clear ();
539 edit_is_negative = false;
542 input_string = get_field (f);
543 show_edit_status (merge_input_and_edit_string ());
544 _layout->set_text (edit_string);
549 Keyboard::magic_widget_grab_focus ();
555 AudioClock::get_field (Field f)
559 return edit_string.substr (1, 2);
561 case Timecode_Minutes:
562 return edit_string.substr (4, 2);
564 case Timecode_Seconds:
565 return edit_string.substr (7, 2);
567 case Timecode_Frames:
568 return edit_string.substr (10, 2);
571 return edit_string.substr (1, 2);
574 return edit_string.substr (4, 2);
577 return edit_string.substr (7, 2);
579 case MS_Milliseconds:
580 return edit_string.substr (10, 3);
583 return edit_string.substr (1, 3);
586 return edit_string.substr (5, 2);
589 return edit_string.substr (8, 4);
599 AudioClock::end_edit (bool modify)
607 ok = timecode_validate_edit (edit_string);
611 ok = bbt_validate_edit (edit_string);
615 ok = minsec_validate_edit (edit_string);
619 if (edit_string.length() < 1) {
620 edit_string = pre_edit_string;
626 edit_string = pre_edit_string;
627 input_string.clear ();
628 _layout->set_text (edit_string);
629 show_edit_status (0);
630 /* edit attributes remain in use */
634 framepos_t pos = 0; /* stupid gcc */
638 pos = frames_from_timecode_string (edit_string);
643 pos = frame_duration_from_bbt_string (0, edit_string);
645 pos = frames_from_bbt_string (0, edit_string);
650 pos = frames_from_minsec_string (edit_string);
654 pos = frames_from_audioframes_string (edit_string);
659 _layout->set_attributes (normal_attributes);
660 ValueChanged(); /* EMIT_SIGNAL */
666 edit_is_negative = false;
667 _layout->set_attributes (normal_attributes);
668 _layout->set_text (pre_edit_string);
679 AudioClock::drop_focus ()
681 Keyboard::magic_widget_drop_focus ();
685 /* move focus back to the default widget in the top level window */
687 Widget* top = get_toplevel();
689 if (top->is_toplevel ()) {
690 Window* win = dynamic_cast<Window*> (top);
697 AudioClock::parse_as_frames_distance (const std::string& str)
701 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
709 AudioClock::parse_as_minsec_distance (const std::string& str)
711 framecnt_t sr = _session->frame_rate();
717 switch (str.length()) {
724 sscanf (str.c_str(), "%" PRId32, &msecs);
725 return msecs * (sr / 1000);
728 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
729 return (secs * sr) + (msecs * (sr/1000));
732 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
733 return (secs * sr) + (msecs * (sr/1000));
736 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
737 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
740 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
741 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
744 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
745 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
748 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
749 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
759 AudioClock::parse_as_timecode_distance (const std::string& str)
761 double fps = _session->timecode_frames_per_second();
762 framecnt_t sr = _session->frame_rate();
768 switch (str.length()) {
773 sscanf (str.c_str(), "%" PRId32, &frames);
774 return llrint ((frames/(float)fps) * sr);
777 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
778 return (secs * sr) + llrint ((frames/(float)fps) * sr);
781 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
782 return (secs * sr) + llrint ((frames/(float)fps) * sr);
785 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
786 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
789 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
790 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
793 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
794 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
797 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
798 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
808 AudioClock::parse_as_bbt_distance (const std::string&)
814 AudioClock::parse_as_distance (const std::string& instr)
818 return parse_as_timecode_distance (instr);
821 return parse_as_frames_distance (instr);
824 return parse_as_bbt_distance (instr);
827 return parse_as_minsec_distance (instr);
834 AudioClock::end_edit_relative (bool add)
840 ok = timecode_validate_edit (edit_string);
844 ok = bbt_validate_edit (edit_string);
848 ok = minsec_validate_edit (edit_string);
856 edit_string = pre_edit_string;
857 input_string.clear ();
858 _layout->set_text (edit_string);
859 show_edit_status (0);
860 /* edit attributes remain in use */
865 framecnt_t frames = parse_as_distance (input_string);
870 _layout->set_attributes (normal_attributes);
874 set (current_time() + frames, true);
876 framepos_t c = current_time();
878 if (c > frames || _negative_allowed) {
879 set (c - frames, true);
884 ValueChanged (); /* EMIT SIGNAL */
887 input_string.clear ();
893 AudioClock::session_property_changed (const PropertyChange&)
895 set (last_when, true);
899 AudioClock::session_configuration_changed (std::string p)
901 if (_negative_allowed) {
902 /* session option editor clock */
906 if (p == "sync-source" || p == "external-sync") {
907 set (current_time(), true);
911 if (p != "timecode-offset" && p != "timecode-offset-negative") {
920 current = current_duration ();
922 current = current_time ();
932 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
934 if ((!force && !is_visible()) || _session == 0) {
939 when = when - offset;
942 if (when == last_when && !force) {
943 #if 0 // XXX return if no change and no change forced. verify Aug/2014
944 if (_mode != Timecode && _mode != MinSec) {
945 /* may need to force display of TC source
946 * time, so don't return early.
948 /* ^^ Why was that?, delta times?
949 * Timecode FPS, pull-up/down, etc changes
950 * trigger a 'session_property_changed' which
951 * eventually calls set(last_when, true)
953 * re-rendering the clock every 40ms or so just
954 * because we can is not ideal.
965 _right_layout->set_alignment(Pango::ALIGN_LEFT);
971 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
973 set_timecode (when, force);
977 set_bbt (when, force);
982 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
984 set_minsec (when, force);
988 set_frames (when, force);
998 AudioClock::set_slave_info ()
1000 if (!_left_layout || !_right_layout) {
1004 SyncSource sync_src = Config->get_sync_source();
1006 if (_session->config.get_external_sync()) {
1007 Slave* slave = _session->slave();
1011 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1012 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1013 _right_layout->set_text ("");
1017 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1018 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1019 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1020 INFO_FONT_SIZE, slave->approximate_current_delta()));
1022 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1023 INFO_FONT_SIZE, _("--pending--")));
1024 _right_layout->set_text ("");
1031 TimecodeSlave* tcslave;
1032 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1033 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1034 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1035 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1036 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1037 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1038 INFO_FONT_SIZE, slave->approximate_current_delta()));
1041 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1042 INFO_FONT_SIZE, _("--pending--")));
1043 _right_layout->set_text ("");
1048 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1049 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1050 _right_layout->set_text ("");
1055 AudioClock::set_frames (framepos_t when, bool /*force*/)
1058 bool negative = false;
1061 _layout->set_text (" ----------");
1064 _left_layout->set_text ("");
1065 _right_layout->set_text ("");
1077 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1079 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1082 _layout->set_text (buf);
1085 framecnt_t rate = _session->frame_rate();
1087 if (fmod (rate, 100.0) == 0.0) {
1088 sprintf (buf, "%.1fkHz", rate/1000.0);
1090 sprintf (buf, "%" PRId64 "Hz", rate);
1093 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1094 INFO_FONT_SIZE, _("SR"), buf));
1096 float vid_pullup = _session->config.get_video_pullup();
1098 if (vid_pullup == 0.0) {
1099 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1100 INFO_FONT_SIZE, _("Pull")));
1102 sprintf (buf, _("%+.4f%%"), vid_pullup);
1103 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1104 INFO_FONT_SIZE, _("Pull"), buf));
1110 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1127 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1128 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1129 mins = (int) floor (left / (frame_rate * 60.0f));
1130 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1131 secs = (int) floor (left / (float) frame_rate);
1132 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1133 millisecs = floor (left * 1000.0 / (float) frame_rate);
1136 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1138 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1144 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1149 _layout->set_text (" --:--:--.---");
1152 _left_layout->set_text ("");
1153 _right_layout->set_text ("");
1159 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1161 _layout->set_text (buf);
1166 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1169 bool negative = false;
1172 _layout->set_text (" --:--:--:--");
1174 _left_layout->set_text ("");
1175 _right_layout->set_text ("");
1187 _session->timecode_duration (when, TC);
1189 _session->timecode_time (when, TC);
1192 TC.negative = TC.negative || negative;
1194 _layout->set_text (Timecode::timecode_format_time(TC));
1200 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1203 Timecode::BBT_Time BBT;
1204 bool negative = false;
1207 _layout->set_text (" ---|--|----");
1209 _left_layout->set_text ("");
1210 _right_layout->set_text ("");
1220 /* handle a common case */
1227 _session->tempo_map().bbt_time (when, BBT);
1232 _session->tempo_map().bbt_time (when, BBT);
1236 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1237 BBT.bars, BBT.beats, BBT.ticks);
1239 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1240 BBT.bars, BBT.beats, BBT.ticks);
1243 _layout->set_text (buf);
1245 if (_right_layout) {
1248 if (bbt_reference_time < 0) {
1251 pos = bbt_reference_time;
1254 TempoMetric m (_session->tempo_map().metric_at (pos));
1256 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1257 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1258 INFO_FONT_SIZE, buf, _("Tempo")));
1260 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1261 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1262 INFO_FONT_SIZE, buf, _("Meter")));
1267 AudioClock::set_session (Session *s)
1269 SessionHandlePtr::set_session (s);
1273 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1274 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1276 const XMLProperty* prop;
1277 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1278 AudioClock::Mode amode;
1281 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1282 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1284 if ((prop = (*i)->property (X_("mode"))) != 0) {
1285 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1288 if ((prop = (*i)->property (X_("on"))) != 0) {
1289 set_off (!string_is_affirmative (prop->value()));
1296 set (last_when, true);
1301 AudioClock::on_key_press_event (GdkEventKey* ev)
1309 int highlight_length;
1312 switch (ev->keyval) {
1355 case GDK_KP_Subtract:
1356 if (_negative_allowed && input_string.empty()) {
1357 edit_is_negative = true;
1358 edit_string.replace(0,1,"-");
1359 _layout->set_text (edit_string);
1362 end_edit_relative (false);
1368 end_edit_relative (true);
1381 ChangeAborted(); /* EMIT SIGNAL */
1386 if (!input_string.empty()) {
1387 /* delete the last key entered
1389 input_string = input_string.substr (0, input_string.length() - 1);
1391 goto use_input_string;
1397 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1398 /* too many digits: eat the key event, but do nothing with it */
1402 input_string.push_back (new_char);
1408 /* get this one in the right order, and to the right width */
1409 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1410 edit_string = edit_string.substr (0, edit_string.length() - 1);
1412 edit_string.push_back (new_char);
1414 if (!edit_string.empty()) {
1416 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1417 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1420 /* highlight the whole thing */
1421 highlight_length = edit_string.length();
1425 highlight_length = merge_input_and_edit_string ();
1428 if (edit_is_negative) {
1429 edit_string.replace(0,1,"-");
1431 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1432 edit_string.replace(0,1,"_");
1434 edit_string.replace(0,1," ");
1438 show_edit_status (highlight_length);
1439 _layout->set_text (edit_string);
1446 AudioClock::merge_input_and_edit_string ()
1448 /* merge with pre-edit-string into edit string */
1450 edit_string = pre_edit_string;
1452 if (input_string.empty()) {
1456 string::size_type target;
1457 for (string::size_type i = 0; i < input_string.length(); ++i) {
1458 target = insert_map[input_string.length() - 1 - i];
1459 edit_string[target] = input_string[i];
1461 /* highlight from end to wherever the last character was added */
1462 return edit_string.length() - insert_map[input_string.length()-1];
1467 AudioClock::on_key_release_event (GdkEventKey *ev)
1473 /* return true for keys that we used on press
1474 so that they cannot possibly do double-duty
1476 switch (ev->keyval) {
1499 case GDK_KP_Decimal:
1507 case GDK_KP_Subtract:
1515 AudioClock::index_to_field (int index) const
1520 return Timecode_Hours;
1521 } else if (index < 7) {
1522 return Timecode_Minutes;
1523 } else if (index < 10) {
1524 return Timecode_Seconds;
1526 return Timecode_Frames;
1532 } else if (index < 7) {
1540 return Timecode_Hours;
1541 } else if (index < 6) {
1543 } else if (index < 9) {
1546 return MS_Milliseconds;
1558 AudioClock::on_button_press_event (GdkEventButton *ev)
1560 switch (ev->button) {
1562 if (editable && !_off) {
1568 /* the text has been centered vertically, so adjust
1571 int xcenter = (get_width() - layout_width) /2;
1573 y = ev->y - ((upper_height - layout_height)/2);
1574 x = ev->x - xcenter;
1576 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1577 /* pretend it is a character on the far right */
1580 drag_field = index_to_field (index);
1582 /* make absolutely sure that the pointer is grabbed */
1583 gdk_pointer_grab(ev->window,false ,
1584 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1585 NULL,NULL,ev->time);
1587 drag_start_y = ev->y;
1601 AudioClock::on_button_release_event (GdkEventButton *ev)
1603 if (editable && !_off) {
1605 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1607 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1608 // we actually dragged so return without
1609 // setting editing focus, or we shift clicked
1612 if (ev->button == 1) {
1614 if (_edit_by_click_field) {
1616 int xcenter = (get_width() - layout_width) /2;
1619 int y = ev->y - ((upper_height - layout_height)/2);
1620 int x = ev->x - xcenter;
1623 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1627 f = index_to_field (index);
1630 case Timecode_Frames:
1631 case MS_Milliseconds:
1647 if (Keyboard::is_context_menu_event (ev)) {
1648 if (ops_menu == 0) {
1651 ops_menu->popup (1, ev->time);
1659 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1661 bool ret = CairoWidget::on_focus_out_event (ev);
1671 AudioClock::on_scroll_event (GdkEventScroll *ev)
1676 if (editing || _session == 0 || !editable || _off) {
1683 /* the text has been centered vertically, so adjust
1687 int xcenter = (get_width() - layout_width) /2;
1688 y = ev->y - ((upper_height - layout_height)/2);
1689 x = ev->x - xcenter;
1691 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1692 /* not in the main layout */
1696 Field f = index_to_field (index);
1697 framepos_t frames = 0;
1699 switch (ev->direction) {
1702 frames = get_frame_step (f);
1704 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1707 set (current_time() + frames, true);
1708 ValueChanged (); /* EMIT_SIGNAL */
1712 case GDK_SCROLL_DOWN:
1713 frames = get_frame_step (f);
1715 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1719 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1722 set (current_time() - frames, true);
1725 ValueChanged (); /* EMIT_SIGNAL */
1738 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1740 if (editing || _session == 0 || !dragging) {
1744 float pixel_frame_scale_factor = 0.2f;
1746 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1747 pixel_frame_scale_factor = 0.1f;
1751 if (Keyboard::modifier_state_contains (ev->state,
1752 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1754 pixel_frame_scale_factor = 0.025f;
1757 double y_delta = ev->y - drag_y;
1759 drag_accum += y_delta*pixel_frame_scale_factor;
1763 if (floor (drag_accum) != 0) {
1768 dir = (drag_accum < 0 ? 1:-1);
1769 pos = current_time();
1770 frames = get_frame_step (drag_field, pos, dir);
1772 if (frames != 0 && frames * drag_accum < current_time()) {
1773 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1779 ValueChanged(); /* EMIT_SIGNAL */
1786 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1789 Timecode::BBT_Time BBT;
1791 case Timecode_Hours:
1792 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1794 case Timecode_Minutes:
1795 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1797 case Timecode_Seconds:
1798 f = _session->frame_rate();
1800 case Timecode_Frames:
1801 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1809 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1812 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1815 f = (framecnt_t) _session->frame_rate();
1817 case MS_Milliseconds:
1818 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1825 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1831 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1837 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1840 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1849 AudioClock::current_time (framepos_t) const
1855 AudioClock::current_duration (framepos_t pos) const
1864 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1880 AudioClock::bbt_validate_edit (const string& str)
1884 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1888 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1892 if (!is_duration && any.bbt.bars == 0) {
1896 if (!is_duration && any.bbt.beats == 0) {
1904 AudioClock::timecode_validate_edit (const string&)
1910 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1911 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1916 TC.hours = hours * -1;
1920 TC.negative = false;
1923 if (TC.negative && !_negative_allowed) {
1927 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1931 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1935 if (_session->timecode_drop_frames()) {
1936 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1945 AudioClock::minsec_validate_edit (const string& str)
1947 int hrs, mins, secs, millisecs;
1949 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1953 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1961 AudioClock::frames_from_timecode_string (const string& str) const
1963 if (_session == 0) {
1972 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1973 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1976 TC.hours = abs(hours);
1977 TC.rate = _session->timecode_frames_per_second();
1978 TC.drop= _session->timecode_drop_frames();
1980 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1982 // timecode_tester ();
1983 if (edit_is_negative) {
1991 AudioClock::frames_from_minsec_string (const string& str) const
1993 if (_session == 0) {
1997 int hrs, mins, secs, millisecs;
1998 framecnt_t sr = _session->frame_rate();
2000 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2001 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2005 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2009 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2011 if (_session == 0) {
2012 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2017 any.type = AnyTime::BBT;
2019 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2026 return _session->any_duration_to_frames (pos, any);
2028 return _session->convert_to_frames (any);
2034 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2036 if (_session == 0) {
2037 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2041 Timecode::BBT_Time bbt;
2043 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2047 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2051 AudioClock::frames_from_audioframes_string (const string& str) const
2054 sscanf (str.c_str(), "%" PRId64, &f);
2059 AudioClock::build_ops_menu ()
2061 using namespace Menu_Helpers;
2062 ops_menu = new Menu;
2063 MenuList& ops_items = ops_menu->items();
2064 ops_menu->set_name ("ArdourContextMenu");
2066 if (!Profile->get_sae()) {
2067 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2069 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2070 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2071 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2073 if (editable && !_off && !is_duration && !_follows_playhead) {
2074 ops_items.push_back (SeparatorElem());
2075 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2076 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2081 AudioClock::set_from_playhead ()
2087 set (_session->transport_frame());
2092 AudioClock::locate ()
2094 if (!_session || is_duration) {
2098 _session->request_locate (current_time(), _session->transport_rolling ());
2102 AudioClock::set_mode (Mode m)
2112 _layout->set_text ("");
2116 _left_layout->set_attributes (info_attributes);
2117 _right_layout->set_attributes (info_attributes);
2118 /* adjust info_height according to font size */
2120 _left_layout->set_text (" 1234567890");
2121 _left_layout->get_pixel_size (ignored, info_height);
2123 _left_layout->set_text ("");
2124 _right_layout->set_text ("");
2129 mode_based_info_ratio = 0.6;
2130 insert_map.push_back (11);
2131 insert_map.push_back (10);
2132 insert_map.push_back (8);
2133 insert_map.push_back (7);
2134 insert_map.push_back (5);
2135 insert_map.push_back (4);
2136 insert_map.push_back (2);
2137 insert_map.push_back (1);
2141 mode_based_info_ratio = 0.5;
2142 insert_map.push_back (11);
2143 insert_map.push_back (10);
2144 insert_map.push_back (9);
2145 insert_map.push_back (8);
2146 insert_map.push_back (6);
2147 insert_map.push_back (5);
2148 insert_map.push_back (3);
2149 insert_map.push_back (2);
2150 insert_map.push_back (1);
2154 mode_based_info_ratio = 0.6;
2155 insert_map.push_back (12);
2156 insert_map.push_back (11);
2157 insert_map.push_back (10);
2158 insert_map.push_back (8);
2159 insert_map.push_back (7);
2160 insert_map.push_back (5);
2161 insert_map.push_back (4);
2162 insert_map.push_back (2);
2163 insert_map.push_back (1);
2167 mode_based_info_ratio = 0.45;
2171 set (last_when, true);
2173 if (!is_transient) {
2174 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2177 mode_changed (); /* EMIT SIGNAL (the member one) */
2181 AudioClock::set_bbt_reference (framepos_t pos)
2183 bbt_reference_time = pos;
2187 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2189 CairoWidget::on_style_changed (old_style);
2191 Gtk::Requisition req;
2192 set_clock_dimensions (req);
2194 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2200 AudioClock::set_editable (bool yn)
2206 AudioClock::set_is_duration (bool yn)
2208 if (yn == is_duration) {
2213 set (last_when, true);
2217 AudioClock::set_off (bool yn)
2225 /* force a redraw. last_when will be preserved, but the clock text will
2229 set (last_when, true);
2233 AudioClock::focus ()
2235 start_edit (Field (0));
2239 AudioClock::set_corner_radius (double r)
2248 AudioClock::dpi_reset ()
2250 /* force recomputation of size even if we are fixed width
2258 AudioClock::set_negative_allowed (bool yn)
2260 _negative_allowed = yn;