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.50;
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)lrint(font_size * info_font_scale_factor))
63 #define TXTSPAN "<span font-family=\"Sans\" foreground=\"white\">"
65 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
66 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
69 , is_transient (transient)
70 , is_duration (duration)
71 , editable (allow_edit)
72 , _follows_playhead (follows_playhead)
75 , _edit_by_click_field (false)
76 , _negative_allowed (false)
77 , edit_is_negative (false)
82 , style_resets_first (true)
87 , mode_based_info_ratio (1.0)
91 , bbt_reference_time (-1)
96 , drag_field (Field (0))
98 set_flags (CAN_FOCUS);
100 _layout = Pango::Layout::create (get_pango_context());
101 _layout->set_attributes (normal_attributes);
104 _left_layout = Pango::Layout::create (get_pango_context());
105 _right_layout = Pango::Layout::create (get_pango_context());
108 set_widget_name (widget_name);
110 _mode = BBT; /* lie to force mode switch */
112 set (last_when, true);
115 clocks.push_back (this);
118 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
119 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
122 AudioClock::~AudioClock ()
124 delete foreground_attr;
129 AudioClock::set_widget_name (const string& str)
134 set_name (str + " clock");
144 AudioClock::on_realize ()
146 Gtk::Requisition req;
148 CairoWidget::on_realize ();
150 set_clock_dimensions (req);
152 first_width = req.width;
153 first_height = req.height;
160 AudioClock::set_font ()
162 Glib::RefPtr<Gtk::Style> style = get_style ();
163 Pango::FontDescription font;
164 Pango::AttrFontDesc* font_attr;
166 if (!is_realized()) {
167 font = get_font_for_style (get_name());
169 font = style->get_font();
172 font_size = font.get_size();
174 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
176 normal_attributes.change (*font_attr);
177 editing_attributes.change (*font_attr);
179 /* now a smaller version of the same font */
182 font.set_size (INFO_FONT_SIZE);
183 font.set_weight (Pango::WEIGHT_NORMAL);
184 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
186 info_attributes.change (*font_attr);
188 /* and an even smaller one */
192 /* get the figure width for the font. This doesn't have to super
193 * accurate since we only use it to measure the (roughly 1 character)
194 * offset from the position Pango tells us for the "cursor"
197 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
201 tmp->get_pixel_size (em_width, ignore_height);
203 /* force redraw of markup with new font-size */
204 set (last_when, true);
208 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
210 CairoWidget::set_active_state (s);
215 AudioClock::set_colors ()
221 uint32_t editing_color;
222 uint32_t cursor_color;
224 if (active_state()) {
225 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
226 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
227 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
228 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
230 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
231 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
232 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
233 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
236 /* store for bg and cursor in render() */
238 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
245 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
252 /* rescale for Pango colors ... sigh */
254 r = lrint (r * 65535.0);
255 g = lrint (g * 65535.0);
256 b = lrint (b * 65535.0);
258 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
259 r = lrint ((r/255.0) * 65535.0);
260 g = lrint ((g/255.0) * 65535.0);
261 b = lrint ((b/255.0) * 65535.0);
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);
268 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
270 normal_attributes.change (*foreground_attr);
271 info_attributes.change (*foreground_attr);
272 editing_attributes.change (*foreground_attr);
273 editing_attributes.change (*editing_attr);
276 _layout->set_attributes (normal_attributes);
278 _layout->set_attributes (editing_attributes);
285 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
287 /* main layout: rounded rect, plus the text */
290 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
293 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
295 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
298 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
303 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
305 pango_cairo_show_layout (cr, _layout->gobj());
309 double h = get_height() - upper_height - separator_height;
312 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
315 if (mode_based_info_ratio != 1.0) {
317 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
321 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
322 left_rect_width + (separator_height == 0 ? corner_radius : 0),
325 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
330 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
331 pango_cairo_show_layout (cr, _left_layout->gobj());
335 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
336 upper_height + separator_height,
337 get_width() - separator_height - left_rect_width,
340 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
341 get_width() - separator_height - left_rect_width, h);
347 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
348 /* right-align does not work per se beacuse layout width is unset.
349 * Using _right_layout->set_width([value >=0]) would also enable
350 * word-wrapping which is not wanted here.
351 * The solution is to custom align the layout depending on its size.
352 * if it is larger than the available space it will be cropped on the
353 * right edge rather than override text on the left side.
356 _right_layout->get_pixel_size(rw, rh);
357 x = get_width() - rw - separator_height - x_leading_padding;
358 if (x < x_leading_padding + left_rect_width + separator_height) {
359 /* rather cut off the right end than overlap with the text on the left */
360 x = x_leading_padding + left_rect_width + separator_height;
362 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
364 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
366 pango_cairo_show_layout (cr, _right_layout->gobj());
369 /* no info to display, or just one */
373 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
375 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
383 if (!insert_map.empty()) {
385 int xcenter = (get_width() - layout_width) /2;
387 if (input_string.length() < insert_map.size()) {
388 Pango::Rectangle cursor;
390 if (input_string.empty()) {
391 /* nothing entered yet, put cursor at the end
394 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
396 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
399 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
401 min (get_width() - 2.0,
402 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
403 (upper_height - layout_height)/2.0,
404 2.0, cursor.get_height()/PANGO_SCALE);
407 /* we've entered all possible digits, no cursor */
411 if (input_string.empty()) {
412 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
415 (upper_height - layout_height)/2.0,
424 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
426 CairoWidget::on_size_allocate (alloc);
429 upper_height = (get_height()/2.0) - 1.0;
431 upper_height = get_height();
436 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
438 Glib::RefPtr<Pango::Layout> tmp;
439 Glib::RefPtr<Gtk::Style> style = get_style ();
440 Pango::FontDescription font;
442 tmp = Pango::Layout::create (get_pango_context());
444 if (!is_realized()) {
445 font = get_font_for_style (get_name());
447 font = style->get_font();
450 tmp->set_font_description (font);
452 /* this string is the longest thing we will ever display */
453 tmp->set_text (" 88:88:88,888");
454 tmp->get_pixel_size (req.width, req.height);
456 layout_height = req.height;
457 layout_width = req.width;
461 AudioClock::on_size_request (Gtk::Requisition* req)
463 /* even for non fixed width clocks, the size we *ask* for never changes,
464 even though the size we receive might. so once we've computed it,
469 req->width = first_width;
470 req->height = first_height;
474 set_clock_dimensions (*req);
476 /* now tackle height, for which we need to know the height of the lower
482 Glib::RefPtr<Pango::Layout> tmp;
483 Glib::RefPtr<Gtk::Style> style = get_style ();
484 Pango::FontDescription font;
487 tmp = Pango::Layout::create (get_pango_context());
489 if (!is_realized()) {
490 font = get_font_for_style (get_name());
492 font = style->get_font();
495 tmp->set_font_description (font);
497 font.set_size (INFO_FONT_SIZE);
498 font.set_weight (Pango::WEIGHT_NORMAL);
499 tmp->set_font_description (font);
501 /* we only care about height, so put as much stuff in here
502 as possible that might change the height.
504 tmp->set_text ("qyhH|"); /* one ascender, one descender */
506 tmp->get_pixel_size (w, info_height);
508 /* silly extra padding that seems necessary to correct the info
509 * that pango just gave us. I have no idea why.
512 req->height += info_height;
513 req->height += separator_height;
518 AudioClock::show_edit_status (int length)
520 editing_attr->set_start_index (edit_string.length() - length);
521 editing_attr->set_end_index (edit_string.length());
523 editing_attributes.change (*foreground_attr);
524 editing_attributes.change (*editing_attr);
526 _layout->set_attributes (editing_attributes);
530 AudioClock::start_edit (Field f)
533 pre_edit_string = _layout->get_text ();
534 if (!insert_map.empty()) {
535 edit_string = pre_edit_string;
537 edit_string.clear ();
538 _layout->set_text ("");
541 input_string.clear ();
543 edit_is_negative = false;
546 input_string = get_field (f);
547 show_edit_status (merge_input_and_edit_string ());
548 _layout->set_text (edit_string);
553 Keyboard::magic_widget_grab_focus ();
559 AudioClock::get_field (Field f)
563 return edit_string.substr (1, 2);
565 case Timecode_Minutes:
566 return edit_string.substr (4, 2);
568 case Timecode_Seconds:
569 return edit_string.substr (7, 2);
571 case Timecode_Frames:
572 return edit_string.substr (10, 2);
575 return edit_string.substr (1, 2);
578 return edit_string.substr (4, 2);
581 return edit_string.substr (7, 2);
583 case MS_Milliseconds:
584 return edit_string.substr (10, 3);
587 return edit_string.substr (1, 3);
590 return edit_string.substr (5, 2);
593 return edit_string.substr (8, 4);
603 AudioClock::end_edit (bool modify)
611 ok = timecode_validate_edit (edit_string);
615 ok = bbt_validate_edit (edit_string);
619 ok = minsec_validate_edit (edit_string);
623 if (edit_string.length() < 1) {
624 edit_string = pre_edit_string;
630 edit_string = pre_edit_string;
631 input_string.clear ();
632 _layout->set_text (edit_string);
633 show_edit_status (0);
634 /* edit attributes remain in use */
638 framepos_t pos = 0; /* stupid gcc */
642 pos = frames_from_timecode_string (edit_string);
647 pos = frame_duration_from_bbt_string (0, edit_string);
649 pos = frames_from_bbt_string (0, edit_string);
654 pos = frames_from_minsec_string (edit_string);
658 pos = frames_from_audioframes_string (edit_string);
663 _layout->set_attributes (normal_attributes);
664 ValueChanged(); /* EMIT_SIGNAL */
670 edit_is_negative = false;
671 _layout->set_attributes (normal_attributes);
672 _layout->set_text (pre_edit_string);
683 AudioClock::drop_focus ()
685 Keyboard::magic_widget_drop_focus ();
689 /* move focus back to the default widget in the top level window */
691 Widget* top = get_toplevel();
693 if (top->is_toplevel ()) {
694 Window* win = dynamic_cast<Window*> (top);
701 AudioClock::parse_as_frames_distance (const std::string& str)
705 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
713 AudioClock::parse_as_minsec_distance (const std::string& str)
715 framecnt_t sr = _session->frame_rate();
721 switch (str.length()) {
728 sscanf (str.c_str(), "%" PRId32, &msecs);
729 return msecs * (sr / 1000);
732 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
733 return (secs * sr) + (msecs * (sr/1000));
736 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
737 return (secs * sr) + (msecs * (sr/1000));
740 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
741 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
744 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
745 return (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));
752 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
753 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
763 AudioClock::parse_as_timecode_distance (const std::string& str)
765 double fps = _session->timecode_frames_per_second();
766 framecnt_t sr = _session->frame_rate();
772 switch (str.length()) {
777 sscanf (str.c_str(), "%" PRId32, &frames);
778 return lrint ((frames/(float)fps) * sr);
781 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
782 return (secs * sr) + lrint ((frames/(float)fps) * sr);
785 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
786 return (secs * sr) + lrint ((frames/(float)fps) * sr);
789 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
790 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
793 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
794 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
797 sscanf (str.c_str(), "%1" 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);
801 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
802 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
812 AudioClock::parse_as_bbt_distance (const std::string&)
818 AudioClock::parse_as_distance (const std::string& instr)
822 return parse_as_timecode_distance (instr);
825 return parse_as_frames_distance (instr);
828 return parse_as_bbt_distance (instr);
831 return parse_as_minsec_distance (instr);
838 AudioClock::end_edit_relative (bool add)
844 ok = timecode_validate_edit (edit_string);
848 ok = bbt_validate_edit (edit_string);
852 ok = minsec_validate_edit (edit_string);
860 edit_string = pre_edit_string;
861 input_string.clear ();
862 _layout->set_text (edit_string);
863 show_edit_status (0);
864 /* edit attributes remain in use */
869 framecnt_t frames = parse_as_distance (input_string);
874 _layout->set_attributes (normal_attributes);
878 set (current_time() + frames, true);
880 framepos_t c = current_time();
882 if (c > frames || _negative_allowed) {
883 set (c - frames, true);
888 ValueChanged (); /* EMIT SIGNAL */
891 input_string.clear ();
897 AudioClock::session_property_changed (const PropertyChange&)
899 set (last_when, true);
903 AudioClock::session_configuration_changed (std::string p)
905 if (_negative_allowed) {
906 /* session option editor clock */
910 if (p == "sync-source" || p == "external-sync") {
911 set (current_time(), true);
915 if (p != "timecode-offset" && p != "timecode-offset-negative") {
924 current = current_duration ();
926 current = current_time ();
936 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
938 if ((!force && !is_visible()) || _session == 0) {
943 when = when - offset;
946 if (when == last_when && !force) {
947 if (_mode != Timecode && _mode != MinSec) {
948 /* may need to force display of TC source
949 * time, so don't return early.
957 _right_layout->set_alignment(Pango::ALIGN_LEFT);
963 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
965 set_timecode (when, force);
969 set_bbt (when, force);
974 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
976 set_minsec (when, force);
980 set_frames (when, force);
990 AudioClock::set_slave_info ()
992 if (!_left_layout || !_right_layout) {
996 SyncSource sync_src = Config->get_sync_source();
998 if (_session->config.get_external_sync()) {
999 Slave* slave = _session->slave();
1003 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1004 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1005 _right_layout->set_text ("");
1009 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1010 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1011 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1012 INFO_FONT_SIZE, slave->approximate_current_delta()));
1014 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1015 INFO_FONT_SIZE, _("--pending--")));
1016 _right_layout->set_text ("");
1023 TimecodeSlave* tcslave;
1024 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1025 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1026 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1027 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1028 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1029 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1030 INFO_FONT_SIZE, slave->approximate_current_delta()));
1033 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1034 INFO_FONT_SIZE, _("--pending--")));
1035 _right_layout->set_text ("");
1040 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1041 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1042 _right_layout->set_text ("");
1047 AudioClock::set_frames (framepos_t when, bool /*force*/)
1050 bool negative = false;
1053 _layout->set_text (" ----------");
1056 _left_layout->set_text ("");
1057 _right_layout->set_text ("");
1069 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1071 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1074 _layout->set_text (buf);
1077 framecnt_t rate = _session->frame_rate();
1079 if (fmod (rate, 100.0) == 0.0) {
1080 sprintf (buf, "%.1fkHz", rate/1000.0);
1082 sprintf (buf, "%" PRId64 "Hz", rate);
1085 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1086 INFO_FONT_SIZE, _("SR"), buf));
1088 float vid_pullup = _session->config.get_video_pullup();
1090 if (vid_pullup == 0.0) {
1091 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1092 INFO_FONT_SIZE, _("Pull")));
1094 sprintf (buf, _("%+.4f%%"), vid_pullup);
1095 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1096 INFO_FONT_SIZE, _("Pull"), buf));
1102 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1110 bool negative = false;
1113 _layout->set_text (" --:--:--.---");
1116 _left_layout->set_text ("");
1117 _right_layout->set_text ("");
1129 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1130 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1131 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1132 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1133 secs = (int) floor (left / (float) _session->frame_rate());
1134 left -= (framecnt_t) floor ((double)(secs * _session->frame_rate()));
1135 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1138 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1140 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1143 _layout->set_text (buf);
1148 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1151 bool negative = false;
1154 _layout->set_text (" --:--:--:--");
1156 _left_layout->set_text ("");
1157 _right_layout->set_text ("");
1169 _session->timecode_duration (when, TC);
1171 _session->timecode_time (when, TC);
1174 TC.negative = TC.negative || negative;
1176 _layout->set_text (Timecode::timecode_format_time(TC));
1182 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1185 Timecode::BBT_Time BBT;
1186 bool negative = false;
1189 _layout->set_text (" ---|--|----");
1191 _left_layout->set_text ("");
1192 _right_layout->set_text ("");
1202 /* handle a common case */
1209 _session->tempo_map().bbt_time (when, BBT);
1214 _session->tempo_map().bbt_time (when, BBT);
1218 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1219 BBT.bars, BBT.beats, BBT.ticks);
1221 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1222 BBT.bars, BBT.beats, BBT.ticks);
1225 _layout->set_text (buf);
1227 if (_right_layout) {
1230 if (bbt_reference_time < 0) {
1233 pos = bbt_reference_time;
1236 TempoMetric m (_session->tempo_map().metric_at (pos));
1238 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1239 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1240 INFO_FONT_SIZE, buf, _("Tempo")));
1242 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1243 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1244 INFO_FONT_SIZE, buf, _("Meter")));
1249 AudioClock::set_session (Session *s)
1251 SessionHandlePtr::set_session (s);
1255 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1256 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1258 const XMLProperty* prop;
1259 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1260 AudioClock::Mode amode;
1263 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1264 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1266 if ((prop = (*i)->property (X_("mode"))) != 0) {
1267 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1270 if ((prop = (*i)->property (X_("on"))) != 0) {
1271 set_off (!string_is_affirmative (prop->value()));
1278 set (last_when, true);
1283 AudioClock::on_key_press_event (GdkEventKey* ev)
1291 int highlight_length;
1294 switch (ev->keyval) {
1337 case GDK_KP_Subtract:
1338 if (_negative_allowed && input_string.empty()) {
1339 edit_is_negative = true;
1340 edit_string.replace(0,1,"-");
1341 _layout->set_text (edit_string);
1344 end_edit_relative (false);
1350 end_edit_relative (true);
1363 ChangeAborted(); /* EMIT SIGNAL */
1368 if (!input_string.empty()) {
1369 /* delete the last key entered
1371 input_string = input_string.substr (0, input_string.length() - 1);
1373 goto use_input_string;
1379 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1380 /* too many digits: eat the key event, but do nothing with it */
1384 input_string.push_back (new_char);
1390 /* get this one in the right order, and to the right width */
1391 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1392 edit_string = edit_string.substr (0, edit_string.length() - 1);
1394 edit_string.push_back (new_char);
1396 if (!edit_string.empty()) {
1398 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1399 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1402 /* highlight the whole thing */
1403 highlight_length = edit_string.length();
1407 highlight_length = merge_input_and_edit_string ();
1410 if (edit_is_negative) {
1411 edit_string.replace(0,1,"-");
1413 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1414 edit_string.replace(0,1,"_");
1416 edit_string.replace(0,1," ");
1420 show_edit_status (highlight_length);
1421 _layout->set_text (edit_string);
1428 AudioClock::merge_input_and_edit_string ()
1430 /* merge with pre-edit-string into edit string */
1432 edit_string = pre_edit_string;
1434 if (input_string.empty()) {
1438 string::size_type target;
1439 for (string::size_type i = 0; i < input_string.length(); ++i) {
1440 target = insert_map[input_string.length() - 1 - i];
1441 edit_string[target] = input_string[i];
1443 /* highlight from end to wherever the last character was added */
1444 return edit_string.length() - insert_map[input_string.length()-1];
1449 AudioClock::on_key_release_event (GdkEventKey *ev)
1455 /* return true for keys that we used on press
1456 so that they cannot possibly do double-duty
1458 switch (ev->keyval) {
1481 case GDK_KP_Decimal:
1489 case GDK_KP_Subtract:
1497 AudioClock::index_to_field (int index) const
1502 return Timecode_Hours;
1503 } else if (index < 7) {
1504 return Timecode_Minutes;
1505 } else if (index < 10) {
1506 return Timecode_Seconds;
1508 return Timecode_Frames;
1514 } else if (index < 7) {
1522 return Timecode_Hours;
1523 } else if (index < 6) {
1525 } else if (index < 9) {
1528 return MS_Milliseconds;
1540 AudioClock::on_button_press_event (GdkEventButton *ev)
1542 switch (ev->button) {
1544 if (editable && !_off) {
1550 /* the text has been centered vertically, so adjust
1553 int xcenter = (get_width() - layout_width) /2;
1555 y = ev->y - ((upper_height - layout_height)/2);
1556 x = ev->x - xcenter;
1558 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1559 /* pretend it is a character on the far right */
1562 drag_field = index_to_field (index);
1564 /* make absolutely sure that the pointer is grabbed */
1565 gdk_pointer_grab(ev->window,false ,
1566 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1567 NULL,NULL,ev->time);
1569 drag_start_y = ev->y;
1583 AudioClock::on_button_release_event (GdkEventButton *ev)
1585 if (editable && !_off) {
1587 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1589 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1590 // we actually dragged so return without
1591 // setting editing focus, or we shift clicked
1594 if (ev->button == 1) {
1596 if (_edit_by_click_field) {
1598 int xcenter = (get_width() - layout_width) /2;
1601 int y = ev->y - ((upper_height - layout_height)/2);
1602 int x = ev->x - xcenter;
1605 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1609 f = index_to_field (index);
1612 case Timecode_Frames:
1613 case MS_Milliseconds:
1629 if (Keyboard::is_context_menu_event (ev)) {
1630 if (ops_menu == 0) {
1633 ops_menu->popup (1, ev->time);
1641 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1643 bool ret = CairoWidget::on_focus_out_event (ev);
1653 AudioClock::on_scroll_event (GdkEventScroll *ev)
1658 if (editing || _session == 0 || !editable || _off) {
1665 /* the text has been centered vertically, so adjust
1669 int xcenter = (get_width() - layout_width) /2;
1670 y = ev->y - ((upper_height - layout_height)/2);
1671 x = ev->x - xcenter;
1673 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1674 /* not in the main layout */
1678 Field f = index_to_field (index);
1679 framepos_t frames = 0;
1681 switch (ev->direction) {
1684 frames = get_frame_step (f);
1686 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1689 set (current_time() + frames, true);
1690 ValueChanged (); /* EMIT_SIGNAL */
1694 case GDK_SCROLL_DOWN:
1695 frames = get_frame_step (f);
1697 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1701 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1704 set (current_time() - frames, true);
1707 ValueChanged (); /* EMIT_SIGNAL */
1720 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1722 if (editing || _session == 0 || !dragging) {
1726 float pixel_frame_scale_factor = 0.2f;
1728 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1729 pixel_frame_scale_factor = 0.1f;
1733 if (Keyboard::modifier_state_contains (ev->state,
1734 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1736 pixel_frame_scale_factor = 0.025f;
1739 double y_delta = ev->y - drag_y;
1741 drag_accum += y_delta*pixel_frame_scale_factor;
1745 if (floor (drag_accum) != 0) {
1750 dir = (drag_accum < 0 ? 1:-1);
1751 pos = current_time();
1752 frames = get_frame_step (drag_field, pos, dir);
1754 if (frames != 0 && frames * drag_accum < current_time()) {
1755 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1761 ValueChanged(); /* EMIT_SIGNAL */
1768 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1771 Timecode::BBT_Time BBT;
1773 case Timecode_Hours:
1774 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1776 case Timecode_Minutes:
1777 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1779 case Timecode_Seconds:
1780 f = _session->frame_rate();
1782 case Timecode_Frames:
1783 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1791 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1794 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1797 f = (framecnt_t) _session->frame_rate();
1799 case MS_Milliseconds:
1800 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1807 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1813 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1819 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1822 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1831 AudioClock::current_time (framepos_t) const
1837 AudioClock::current_duration (framepos_t pos) const
1846 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1862 AudioClock::bbt_validate_edit (const string& str)
1866 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1870 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1874 if (!is_duration && any.bbt.bars == 0) {
1878 if (!is_duration && any.bbt.beats == 0) {
1886 AudioClock::timecode_validate_edit (const string&)
1892 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1893 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1898 TC.hours = hours * -1;
1902 TC.negative = false;
1905 if (TC.negative && !_negative_allowed) {
1909 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1913 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1917 if (_session->timecode_drop_frames()) {
1918 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1927 AudioClock::minsec_validate_edit (const string& str)
1929 int hrs, mins, secs, millisecs;
1931 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1935 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1943 AudioClock::frames_from_timecode_string (const string& str) const
1945 if (_session == 0) {
1954 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1955 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1958 TC.hours = abs(hours);
1959 TC.rate = _session->timecode_frames_per_second();
1960 TC.drop= _session->timecode_drop_frames();
1962 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1964 // timecode_tester ();
1965 if (edit_is_negative) {
1973 AudioClock::frames_from_minsec_string (const string& str) const
1975 if (_session == 0) {
1979 int hrs, mins, secs, millisecs;
1980 framecnt_t sr = _session->frame_rate();
1982 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1983 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1987 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1991 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1993 if (_session == 0) {
1994 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1999 any.type = AnyTime::BBT;
2001 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2008 return _session->any_duration_to_frames (pos, any);
2010 return _session->convert_to_frames (any);
2016 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2018 if (_session == 0) {
2019 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2023 Timecode::BBT_Time bbt;
2025 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2029 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2033 AudioClock::frames_from_audioframes_string (const string& str) const
2036 sscanf (str.c_str(), "%" PRId64, &f);
2041 AudioClock::build_ops_menu ()
2043 using namespace Menu_Helpers;
2044 ops_menu = new Menu;
2045 MenuList& ops_items = ops_menu->items();
2046 ops_menu->set_name ("ArdourContextMenu");
2048 if (!Profile->get_sae()) {
2049 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2051 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2052 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2053 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2055 if (editable && !_off && !is_duration && !_follows_playhead) {
2056 ops_items.push_back (SeparatorElem());
2057 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2058 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2063 AudioClock::set_from_playhead ()
2069 set (_session->transport_frame());
2074 AudioClock::locate ()
2076 if (!_session || is_duration) {
2080 _session->request_locate (current_time(), _session->transport_rolling ());
2084 AudioClock::set_mode (Mode m)
2094 _layout->set_text ("");
2098 _left_layout->set_attributes (info_attributes);
2099 _right_layout->set_attributes (info_attributes);
2100 /* adjust info_height according to font size */
2102 _left_layout->set_text (" 1234567890");
2103 _left_layout->get_pixel_size (ignored, info_height);
2105 _left_layout->set_text ("");
2106 _right_layout->set_text ("");
2111 mode_based_info_ratio = 0.6;
2112 insert_map.push_back (11);
2113 insert_map.push_back (10);
2114 insert_map.push_back (8);
2115 insert_map.push_back (7);
2116 insert_map.push_back (5);
2117 insert_map.push_back (4);
2118 insert_map.push_back (2);
2119 insert_map.push_back (1);
2123 mode_based_info_ratio = 0.5;
2124 insert_map.push_back (11);
2125 insert_map.push_back (10);
2126 insert_map.push_back (9);
2127 insert_map.push_back (8);
2128 insert_map.push_back (6);
2129 insert_map.push_back (5);
2130 insert_map.push_back (3);
2131 insert_map.push_back (2);
2132 insert_map.push_back (1);
2136 mode_based_info_ratio = 0.6;
2137 insert_map.push_back (12);
2138 insert_map.push_back (11);
2139 insert_map.push_back (10);
2140 insert_map.push_back (8);
2141 insert_map.push_back (7);
2142 insert_map.push_back (5);
2143 insert_map.push_back (4);
2144 insert_map.push_back (2);
2145 insert_map.push_back (1);
2149 mode_based_info_ratio = 0.45;
2153 set (last_when, true);
2155 if (!is_transient) {
2156 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2159 mode_changed (); /* EMIT SIGNAL (the member one) */
2163 AudioClock::set_bbt_reference (framepos_t pos)
2165 bbt_reference_time = pos;
2169 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2171 CairoWidget::on_style_changed (old_style);
2173 Gtk::Requisition req;
2174 set_clock_dimensions (req);
2181 AudioClock::set_editable (bool yn)
2187 AudioClock::set_is_duration (bool yn)
2189 if (yn == is_duration) {
2194 set (last_when, true);
2198 AudioClock::set_off (bool yn)
2206 /* force a redraw. last_when will be preserved, but the clock text will
2210 set (last_when, true);
2214 AudioClock::focus ()
2216 start_edit (Field (0));
2220 AudioClock::set_corner_radius (double r)
2229 AudioClock::dpi_reset ()
2231 /* force recomputation of size even if we are fixed width
2239 AudioClock::set_negative_allowed (bool yn)
2241 _negative_allowed = yn;