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;
52 using Gtkmm2ext::Keyboard;
54 sigc::signal<void> AudioClock::ModeChanged;
55 vector<AudioClock*> AudioClock::clocks;
56 const double AudioClock::info_font_scale_factor = 0.5;
57 const double AudioClock::separator_height = 0.0;
58 const double AudioClock::x_leading_padding = 6.0;
60 #define BBT_BAR_CHAR "|"
61 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
62 #define INFO_FONT_SIZE ((int)round(font_size * info_font_scale_factor))
64 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
65 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
68 , is_transient (transient)
69 , is_duration (duration)
70 , editable (allow_edit)
71 , _follows_playhead (follows_playhead)
74 , _edit_by_click_field (false)
75 , _negative_allowed (false)
76 , edit_is_negative (false)
81 , style_resets_first (true)
86 , mode_based_info_ratio (1.0)
90 , bbt_reference_time (-1)
95 , drag_field (Field (0))
97 set_flags (CAN_FOCUS);
99 _layout = Pango::Layout::create (get_pango_context());
100 _layout->set_attributes (normal_attributes);
103 _left_layout = Pango::Layout::create (get_pango_context());
104 _right_layout = Pango::Layout::create (get_pango_context());
107 set_widget_name (widget_name);
109 _mode = BBT; /* lie to force mode switch */
111 set (last_when, true);
114 clocks.push_back (this);
117 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
118 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
121 AudioClock::~AudioClock ()
123 delete foreground_attr;
128 AudioClock::set_widget_name (const string& str)
133 set_name (str + " clock");
143 AudioClock::on_realize ()
145 Gtk::Requisition req;
147 CairoWidget::on_realize ();
149 set_clock_dimensions (req);
151 first_width = req.width;
152 first_height = req.height;
159 AudioClock::set_font ()
161 Glib::RefPtr<Gtk::Style> style = get_style ();
162 Pango::FontDescription font;
163 Pango::AttrFontDesc* font_attr;
165 if (!is_realized()) {
166 font = get_font_for_style (get_name());
168 font = style->get_font();
171 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 ((int) lrint (font_size * info_font_scale_factor));
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_by_name (string_compose ("%1 active: background", get_name()));
225 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
226 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
227 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
229 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
230 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
231 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
232 cursor_color = ARDOUR_UI::config()->color_by_name (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 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);
267 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
269 normal_attributes.change (*foreground_attr);
270 info_attributes.change (*foreground_attr);
271 editing_attributes.change (*foreground_attr);
272 editing_attributes.change (*editing_attr);
275 _layout->set_attributes (normal_attributes);
277 _layout->set_attributes (editing_attributes);
284 AudioClock::render (cairo_t* cr)
286 /* main layout: rounded rect, plus the text */
289 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
292 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
294 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
297 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
302 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
304 pango_cairo_show_layout (cr, _layout->gobj());
308 double h = get_height() - upper_height - separator_height;
311 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
314 if (mode_based_info_ratio != 1.0) {
316 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
320 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
321 left_rect_width + (separator_height == 0 ? corner_radius : 0),
324 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
329 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
330 pango_cairo_show_layout (cr, _left_layout->gobj());
334 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
335 upper_height + separator_height,
336 get_width() - separator_height - left_rect_width,
339 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
340 get_width() - separator_height - left_rect_width, h);
346 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
347 /* right-align does not work per se beacuse layout width is unset.
348 * Using _right_layout->set_width([value >=0]) would also enable
349 * word-wrapping which is not wanted here.
350 * The solution is to custom align the layout depending on its size.
351 * if it is larger than the available space it will be cropped on the
352 * right edge rather than override text on the left side.
355 _right_layout->get_pixel_size(rw, rh);
356 x = get_width() - rw - separator_height - x_leading_padding;
357 if (x < x_leading_padding + left_rect_width + separator_height) {
358 /* rather cut off the right end than overlap with the text on the left */
359 x = x_leading_padding + left_rect_width + separator_height;
361 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
363 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
365 pango_cairo_show_layout (cr, _right_layout->gobj());
368 /* no info to display, or just one */
372 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
374 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
382 if (!insert_map.empty()) {
384 int xcenter = (get_width() - layout_width) /2;
386 if (input_string.length() < insert_map.size()) {
387 Pango::Rectangle cursor;
389 if (input_string.empty()) {
390 /* nothing entered yet, put cursor at the end
393 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
395 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
398 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
400 min (get_width() - 2.0,
401 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
402 (upper_height - layout_height)/2.0,
403 2.0, cursor.get_height()/PANGO_SCALE);
406 /* we've entered all possible digits, no cursor */
410 if (input_string.empty()) {
411 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
414 (upper_height - layout_height)/2.0,
423 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
425 CairoWidget::on_size_allocate (alloc);
428 upper_height = (get_height()/2.0) - 1.0;
430 upper_height = get_height();
435 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
437 Glib::RefPtr<Pango::Layout> tmp;
438 Glib::RefPtr<Gtk::Style> style = get_style ();
439 Pango::FontDescription font;
441 tmp = Pango::Layout::create (get_pango_context());
443 if (!is_realized()) {
444 font = get_font_for_style (get_name());
446 font = style->get_font();
449 tmp->set_font_description (font);
451 /* this string is the longest thing we will ever display */
452 tmp->set_text (" 88:88:88,888");
453 tmp->get_pixel_size (req.width, req.height);
455 layout_height = req.height;
456 layout_width = req.width;
460 AudioClock::on_size_request (Gtk::Requisition* req)
462 /* even for non fixed width clocks, the size we *ask* for never changes,
463 even though the size we receive might. so once we've computed it,
468 req->width = first_width;
469 req->height = first_height;
473 set_clock_dimensions (*req);
475 /* now tackle height, for which we need to know the height of the lower
481 Glib::RefPtr<Pango::Layout> tmp;
482 Glib::RefPtr<Gtk::Style> style = get_style ();
483 Pango::FontDescription font;
486 tmp = Pango::Layout::create (get_pango_context());
488 if (!is_realized()) {
489 font = get_font_for_style (get_name());
491 font = style->get_font();
494 tmp->set_font_description (font);
496 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
497 font.set_weight (Pango::WEIGHT_NORMAL);
498 tmp->set_font_description (font);
500 /* we only care about height, so put as much stuff in here
501 as possible that might change the height.
503 tmp->set_text ("qyhH|"); /* one ascender, one descender */
505 tmp->get_pixel_size (w, info_height);
507 /* silly extra padding that seems necessary to correct the info
508 * that pango just gave us. I have no idea why.
511 req->height += info_height;
512 req->height += separator_height;
517 AudioClock::show_edit_status (int length)
519 editing_attr->set_start_index (edit_string.length() - length);
520 editing_attr->set_end_index (edit_string.length());
522 editing_attributes.change (*foreground_attr);
523 editing_attributes.change (*editing_attr);
525 _layout->set_attributes (editing_attributes);
529 AudioClock::start_edit (Field f)
532 pre_edit_string = _layout->get_text ();
533 if (!insert_map.empty()) {
534 edit_string = pre_edit_string;
536 edit_string.clear ();
537 _layout->set_text ("");
540 input_string.clear ();
542 edit_is_negative = false;
545 input_string = get_field (f);
546 show_edit_status (merge_input_and_edit_string ());
547 _layout->set_text (edit_string);
552 Keyboard::magic_widget_grab_focus ();
558 AudioClock::get_field (Field f)
562 return edit_string.substr (1, 2);
564 case Timecode_Minutes:
565 return edit_string.substr (4, 2);
567 case Timecode_Seconds:
568 return edit_string.substr (7, 2);
570 case Timecode_Frames:
571 return edit_string.substr (10, 2);
574 return edit_string.substr (1, 2);
577 return edit_string.substr (4, 2);
580 return edit_string.substr (7, 2);
582 case MS_Milliseconds:
583 return edit_string.substr (10, 3);
586 return edit_string.substr (1, 3);
589 return edit_string.substr (5, 2);
592 return edit_string.substr (8, 4);
602 AudioClock::end_edit (bool modify)
610 ok = timecode_validate_edit (edit_string);
614 ok = bbt_validate_edit (edit_string);
618 ok = minsec_validate_edit (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 lrint ((frames/(float)fps) * sr);
777 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
778 return (secs * sr) + lrint ((frames/(float)fps) * sr);
781 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
782 return (secs * sr) + lrint ((frames/(float)fps) * sr);
785 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
786 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
789 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
790 return (mins * 60 * sr) + (secs * sr) + lrint ((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) + lrint ((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) + lrint ((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& p)
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 (_mode != Timecode && _mode != MinSec) {
944 /* may need to force display of TC source
945 * time, so don't return early.
953 _right_layout->set_alignment(Pango::ALIGN_LEFT);
959 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
961 set_timecode (when, force);
965 set_bbt (when, force);
970 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
972 set_minsec (when, force);
976 set_frames (when, force);
986 AudioClock::set_slave_info ()
988 if (!_left_layout || !_right_layout) {
992 SyncSource sync_src = Config->get_sync_source();
994 if (_session->config.get_external_sync()) {
995 Slave* slave = _session->slave();
999 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1000 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1001 _right_layout->set_text ("");
1005 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1006 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1007 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1008 INFO_FONT_SIZE, slave->approximate_current_delta()));
1010 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1011 INFO_FONT_SIZE, _("--pending--")));
1012 _right_layout->set_text ("");
1019 TimecodeSlave* tcslave;
1020 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1021 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1022 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2</span><span foreground=\"%3\">%4</span></span>",
1023 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1024 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1025 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1026 INFO_FONT_SIZE, slave->approximate_current_delta()));
1029 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1030 INFO_FONT_SIZE, _("--pending--")));
1031 _right_layout->set_text ("");
1036 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">INT/%2</span>",
1037 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1038 _right_layout->set_text ("");
1043 AudioClock::set_frames (framepos_t when, bool /*force*/)
1046 bool negative = false;
1049 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1052 _left_layout->set_text ("");
1053 _right_layout->set_text ("");
1065 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1067 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1070 _layout->set_text (buf);
1073 framecnt_t rate = _session->frame_rate();
1075 if (fmod (rate, 100.0) == 0.0) {
1076 sprintf (buf, "%.1fkHz", rate/1000.0);
1078 sprintf (buf, "%" PRId64 "Hz", rate);
1081 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1082 INFO_FONT_SIZE, _("SR"), buf));
1084 float vid_pullup = _session->config.get_video_pullup();
1086 if (vid_pullup == 0.0) {
1087 _right_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">off</span></span>",
1088 INFO_FONT_SIZE, _("Pull")));
1090 sprintf (buf, _("%+.4f%%"), vid_pullup);
1091 _right_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1092 INFO_FONT_SIZE, _("Pull"), buf));
1098 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1106 bool negative = false;
1109 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1112 _left_layout->set_text ("");
1113 _right_layout->set_text ("");
1125 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1126 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1127 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1128 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1129 secs = (int) floor (left / (float) _session->frame_rate());
1130 left -= (framecnt_t) floor (secs * _session->frame_rate());
1131 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1134 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1136 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1139 _layout->set_text (buf);
1144 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1147 bool negative = false;
1150 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1152 _left_layout->set_text ("");
1153 _right_layout->set_text ("");
1165 _session->timecode_duration (when, TC);
1167 _session->timecode_time (when, TC);
1170 TC.negative = TC.negative || negative;
1172 _layout->set_text (Timecode::timecode_format_time(TC));
1178 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1181 Timecode::BBT_Time BBT;
1182 bool negative = false;
1185 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1187 _left_layout->set_text ("");
1188 _right_layout->set_text ("");
1198 /* handle a common case */
1205 _session->tempo_map().bbt_time (when, BBT);
1210 _session->tempo_map().bbt_time (when, BBT);
1214 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1215 BBT.bars, BBT.beats, BBT.ticks);
1217 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1218 BBT.bars, BBT.beats, BBT.ticks);
1221 _layout->set_text (buf);
1223 if (_right_layout) {
1226 if (bbt_reference_time < 0) {
1229 pos = bbt_reference_time;
1232 TempoMetric m (_session->tempo_map().metric_at (pos));
1234 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1235 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%3 <span foreground=\"green\">%2</span></span>",
1236 INFO_FONT_SIZE, buf, _("Tempo")));
1238 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1239 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%3 <span foreground=\"green\">%2</span></span>",
1240 INFO_FONT_SIZE, buf, _("Meter")));
1245 AudioClock::set_session (Session *s)
1247 SessionHandlePtr::set_session (s);
1251 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1252 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1254 const XMLProperty* prop;
1255 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1256 AudioClock::Mode amode;
1259 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1260 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1262 if ((prop = (*i)->property (X_("mode"))) != 0) {
1263 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1266 if ((prop = (*i)->property (X_("on"))) != 0) {
1267 set_off (!string_is_affirmative (prop->value()));
1274 set (last_when, true);
1279 AudioClock::on_key_press_event (GdkEventKey* ev)
1287 int highlight_length;
1290 switch (ev->keyval) {
1333 case GDK_KP_Subtract:
1334 if (_negative_allowed && input_string.empty()) {
1335 edit_is_negative = true;
1336 edit_string.replace(0,1,"-");
1337 _layout->set_text (edit_string);
1340 end_edit_relative (false);
1346 end_edit_relative (true);
1359 ChangeAborted(); /* EMIT SIGNAL */
1364 if (!input_string.empty()) {
1365 /* delete the last key entered
1367 input_string = input_string.substr (0, input_string.length() - 1);
1369 goto use_input_string;
1375 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1376 /* too many digits: eat the key event, but do nothing with it */
1380 input_string.push_back (new_char);
1386 /* get this one in the right order, and to the right width */
1387 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1388 edit_string = edit_string.substr (0, edit_string.length() - 1);
1390 edit_string.push_back (new_char);
1392 if (!edit_string.empty()) {
1394 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1395 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1398 /* highlight the whole thing */
1399 highlight_length = edit_string.length();
1403 highlight_length = merge_input_and_edit_string ();
1406 if (edit_is_negative) {
1407 edit_string.replace(0,1,"-");
1409 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1410 edit_string.replace(0,1,"_");
1412 edit_string.replace(0,1," ");
1416 show_edit_status (highlight_length);
1417 _layout->set_text (edit_string);
1424 AudioClock::merge_input_and_edit_string ()
1426 /* merge with pre-edit-string into edit string */
1428 edit_string = pre_edit_string;
1430 if (input_string.empty()) {
1434 string::size_type target;
1435 for (string::size_type i = 0; i < input_string.length(); ++i) {
1436 target = insert_map[input_string.length() - 1 - i];
1437 edit_string[target] = input_string[i];
1439 /* highlight from end to wherever the last character was added */
1440 return edit_string.length() - insert_map[input_string.length()-1];
1445 AudioClock::on_key_release_event (GdkEventKey *ev)
1451 /* return true for keys that we used on press
1452 so that they cannot possibly do double-duty
1454 switch (ev->keyval) {
1477 case GDK_KP_Decimal:
1485 case GDK_KP_Subtract:
1493 AudioClock::index_to_field (int index) const
1498 return Timecode_Hours;
1499 } else if (index < 7) {
1500 return Timecode_Minutes;
1501 } else if (index < 10) {
1502 return Timecode_Seconds;
1504 return Timecode_Frames;
1510 } else if (index < 7) {
1518 return Timecode_Hours;
1519 } else if (index < 6) {
1521 } else if (index < 9) {
1524 return MS_Milliseconds;
1536 AudioClock::on_button_press_event (GdkEventButton *ev)
1538 switch (ev->button) {
1540 if (editable && !_off) {
1546 /* the text has been centered vertically, so adjust
1549 int xcenter = (get_width() - layout_width) /2;
1551 y = ev->y - ((upper_height - layout_height)/2);
1552 x = ev->x - xcenter;
1554 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1555 /* pretend it is a character on the far right */
1558 drag_field = index_to_field (index);
1560 /* make absolutely sure that the pointer is grabbed */
1561 gdk_pointer_grab(ev->window,false ,
1562 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1563 NULL,NULL,ev->time);
1565 drag_start_y = ev->y;
1579 AudioClock::on_button_release_event (GdkEventButton *ev)
1581 if (editable && !_off) {
1583 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1585 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1586 // we actually dragged so return without
1587 // setting editing focus, or we shift clicked
1590 if (ev->button == 1) {
1592 if (_edit_by_click_field) {
1594 int xcenter = (get_width() - layout_width) /2;
1597 int y = ev->y - ((upper_height - layout_height)/2);
1598 int x = ev->x - xcenter;
1601 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1605 f = index_to_field (index);
1608 case Timecode_Frames:
1609 case MS_Milliseconds:
1625 if (Keyboard::is_context_menu_event (ev)) {
1626 if (ops_menu == 0) {
1629 ops_menu->popup (1, ev->time);
1637 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1639 bool ret = CairoWidget::on_focus_out_event (ev);
1649 AudioClock::on_scroll_event (GdkEventScroll *ev)
1654 if (editing || _session == 0 || !editable || _off) {
1661 /* the text has been centered vertically, so adjust
1665 int xcenter = (get_width() - layout_width) /2;
1666 y = ev->y - ((upper_height - layout_height)/2);
1667 x = ev->x - xcenter;
1669 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1670 /* not in the main layout */
1674 Field f = index_to_field (index);
1675 framepos_t frames = 0;
1677 switch (ev->direction) {
1680 frames = get_frame_step (f);
1682 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1685 set (current_time() + frames, true);
1686 ValueChanged (); /* EMIT_SIGNAL */
1690 case GDK_SCROLL_DOWN:
1691 frames = get_frame_step (f);
1693 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1697 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1700 set (current_time() - frames, true);
1703 ValueChanged (); /* EMIT_SIGNAL */
1716 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1718 if (editing || _session == 0 || !dragging) {
1722 float pixel_frame_scale_factor = 0.2f;
1724 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1725 pixel_frame_scale_factor = 0.1f;
1729 if (Keyboard::modifier_state_contains (ev->state,
1730 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1732 pixel_frame_scale_factor = 0.025f;
1735 double y_delta = ev->y - drag_y;
1737 drag_accum += y_delta*pixel_frame_scale_factor;
1741 if (trunc (drag_accum) != 0) {
1746 dir = (drag_accum < 0 ? 1:-1);
1747 pos = current_time();
1748 frames = get_frame_step (drag_field, pos, dir);
1750 if (frames != 0 && frames * drag_accum < current_time()) {
1751 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1757 ValueChanged(); /* EMIT_SIGNAL */
1764 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1767 Timecode::BBT_Time BBT;
1769 case Timecode_Hours:
1770 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1772 case Timecode_Minutes:
1773 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1775 case Timecode_Seconds:
1776 f = _session->frame_rate();
1778 case Timecode_Frames:
1779 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1787 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1790 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1793 f = (framecnt_t) _session->frame_rate();
1795 case MS_Milliseconds:
1796 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1803 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1809 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1815 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1818 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1827 AudioClock::current_time (framepos_t) const
1833 AudioClock::current_duration (framepos_t pos) const
1842 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1858 AudioClock::bbt_validate_edit (const string& str)
1862 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1866 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1870 if (!is_duration && any.bbt.bars == 0) {
1874 if (!is_duration && any.bbt.beats == 0) {
1882 AudioClock::timecode_validate_edit (const string&)
1888 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1889 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1894 TC.hours = hours * -1;
1898 TC.negative = false;
1901 if (TC.negative && !_negative_allowed) {
1905 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1909 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1913 if (_session->timecode_drop_frames()) {
1914 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1923 AudioClock::minsec_validate_edit (const string& str)
1925 int hrs, mins, secs, millisecs;
1927 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1931 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1939 AudioClock::frames_from_timecode_string (const string& str) const
1941 if (_session == 0) {
1950 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1951 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1954 TC.hours = abs(hours);
1955 TC.rate = _session->timecode_frames_per_second();
1956 TC.drop= _session->timecode_drop_frames();
1958 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1960 // timecode_tester ();
1961 if (edit_is_negative) {
1969 AudioClock::frames_from_minsec_string (const string& str) const
1971 if (_session == 0) {
1975 int hrs, mins, secs, millisecs;
1976 framecnt_t sr = _session->frame_rate();
1978 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1979 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1983 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1987 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1989 if (_session == 0) {
1990 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1995 any.type = AnyTime::BBT;
1997 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2004 return _session->any_duration_to_frames (pos, any);
2006 return _session->convert_to_frames (any);
2012 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2014 if (_session == 0) {
2015 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2019 Timecode::BBT_Time bbt;
2021 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2025 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2029 AudioClock::frames_from_audioframes_string (const string& str) const
2032 sscanf (str.c_str(), "%" PRId64, &f);
2037 AudioClock::build_ops_menu ()
2039 using namespace Menu_Helpers;
2040 ops_menu = new Menu;
2041 MenuList& ops_items = ops_menu->items();
2042 ops_menu->set_name ("ArdourContextMenu");
2044 if (!Profile->get_sae()) {
2045 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2047 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2048 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2049 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2051 if (editable && !_off && !is_duration && !_follows_playhead) {
2052 ops_items.push_back (SeparatorElem());
2053 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2054 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2059 AudioClock::set_from_playhead ()
2065 set (_session->transport_frame());
2070 AudioClock::locate ()
2072 if (!_session || is_duration) {
2076 _session->request_locate (current_time(), _session->transport_rolling ());
2080 AudioClock::set_mode (Mode m)
2090 _layout->set_text ("");
2094 _left_layout->set_attributes (info_attributes);
2095 _right_layout->set_attributes (info_attributes);
2096 /* adjust info_height according to font size */
2098 _left_layout->set_text (" 1234567890");
2099 _left_layout->get_pixel_size (ignored, info_height);
2101 _left_layout->set_text ("");
2102 _right_layout->set_text ("");
2107 mode_based_info_ratio = 0.5;
2108 insert_map.push_back (11);
2109 insert_map.push_back (10);
2110 insert_map.push_back (8);
2111 insert_map.push_back (7);
2112 insert_map.push_back (5);
2113 insert_map.push_back (4);
2114 insert_map.push_back (2);
2115 insert_map.push_back (1);
2119 mode_based_info_ratio = 0.5;
2120 insert_map.push_back (11);
2121 insert_map.push_back (10);
2122 insert_map.push_back (9);
2123 insert_map.push_back (8);
2124 insert_map.push_back (6);
2125 insert_map.push_back (5);
2126 insert_map.push_back (3);
2127 insert_map.push_back (2);
2128 insert_map.push_back (1);
2132 mode_based_info_ratio = 0.5;
2133 insert_map.push_back (12);
2134 insert_map.push_back (11);
2135 insert_map.push_back (10);
2136 insert_map.push_back (8);
2137 insert_map.push_back (7);
2138 insert_map.push_back (5);
2139 insert_map.push_back (4);
2140 insert_map.push_back (2);
2141 insert_map.push_back (1);
2145 mode_based_info_ratio = 0.5;
2149 set (last_when, true);
2151 if (!is_transient) {
2152 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2155 mode_changed (); /* EMIT SIGNAL (the member one) */
2159 AudioClock::set_bbt_reference (framepos_t pos)
2161 bbt_reference_time = pos;
2165 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2167 CairoWidget::on_style_changed (old_style);
2169 Gtk::Requisition req;
2170 set_clock_dimensions (req);
2177 AudioClock::set_editable (bool yn)
2183 AudioClock::set_is_duration (bool yn)
2185 if (yn == is_duration) {
2190 set (last_when, true);
2194 AudioClock::set_off (bool yn)
2202 /* force a redraw. last_when will be preserved, but the clock text will
2206 set (last_when, true);
2210 AudioClock::focus ()
2212 start_edit (Field (0));
2216 AudioClock::set_corner_radius (double r)
2225 AudioClock::dpi_reset ()
2227 /* force recomputation of size even if we are fixed width
2235 AudioClock::set_negative_allowed (bool yn)
2237 _negative_allowed = yn;