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.50;
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;
161 AudioClock::set_font ()
163 Glib::RefPtr<Gtk::Style> style = get_style ();
164 Pango::FontDescription font;
165 Pango::AttrFontDesc* font_attr;
167 if (!is_realized()) {
168 font = get_font_for_style (get_name());
170 font = style->get_font();
173 font_size = font.get_size();
175 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
177 normal_attributes.change (*font_attr);
178 editing_attributes.change (*font_attr);
180 /* now a smaller version of the same font */
183 font.set_size (INFO_FONT_SIZE);
184 font.set_weight (Pango::WEIGHT_NORMAL);
185 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
187 info_attributes.change (*font_attr);
189 /* and an even smaller one */
193 /* get the figure width for the font. This doesn't have to super
194 * accurate since we only use it to measure the (roughly 1 character)
195 * offset from the position Pango tells us for the "cursor"
198 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
202 tmp->get_pixel_size (em_width, ignore_height);
204 /* force redraw of markup with new font-size */
205 set (last_when, true);
209 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
211 CairoWidget::set_active_state (s);
216 AudioClock::set_colors ()
222 uint32_t editing_color;
223 uint32_t cursor_color;
225 if (active_state()) {
226 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
227 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
228 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
229 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
231 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
232 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
233 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
234 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
237 /* store for bg and cursor in render() */
239 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
246 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
253 /* rescale for Pango colors ... sigh */
255 r = lrint (r * 65535.0);
256 g = lrint (g * 65535.0);
257 b = lrint (b * 65535.0);
259 UINT_TO_RGBA (text_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);
263 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
265 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
266 r = lrint ((r/255.0) * 65535.0);
267 g = lrint ((g/255.0) * 65535.0);
268 b = lrint ((b/255.0) * 65535.0);
269 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
271 normal_attributes.change (*foreground_attr);
272 info_attributes.change (*foreground_attr);
273 editing_attributes.change (*foreground_attr);
274 editing_attributes.change (*editing_attr);
277 _layout->set_attributes (normal_attributes);
279 _layout->set_attributes (editing_attributes);
286 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
288 /* main layout: rounded rect, plus the text */
291 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
294 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
296 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
299 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
304 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
306 pango_cairo_show_layout (cr, _layout->gobj());
310 double h = get_height() - upper_height - separator_height;
313 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
316 if (mode_based_info_ratio != 1.0) {
318 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
322 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
323 left_rect_width + (separator_height == 0 ? corner_radius : 0),
326 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
331 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
332 pango_cairo_show_layout (cr, _left_layout->gobj());
336 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
337 upper_height + separator_height,
338 get_width() - separator_height - left_rect_width,
341 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
342 get_width() - separator_height - left_rect_width, h);
348 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
349 /* right-align does not work per se beacuse layout width is unset.
350 * Using _right_layout->set_width([value >=0]) would also enable
351 * word-wrapping which is not wanted here.
352 * The solution is to custom align the layout depending on its size.
353 * if it is larger than the available space it will be cropped on the
354 * right edge rather than override text on the left side.
357 _right_layout->get_pixel_size(rw, rh);
358 x = get_width() - rw - separator_height - x_leading_padding;
359 if (x < x_leading_padding + left_rect_width + separator_height) {
360 /* rather cut off the right end than overlap with the text on the left */
361 x = x_leading_padding + left_rect_width + separator_height;
363 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
365 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
367 pango_cairo_show_layout (cr, _right_layout->gobj());
370 /* no info to display, or just one */
374 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
376 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
384 if (!insert_map.empty()) {
386 int xcenter = (get_width() - layout_width) /2;
388 if (input_string.length() < insert_map.size()) {
389 Pango::Rectangle cursor;
391 if (input_string.empty()) {
392 /* nothing entered yet, put cursor at the end
395 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
397 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
400 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
402 min (get_width() - 2.0,
403 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
404 (upper_height - layout_height)/2.0,
405 2.0, cursor.get_height()/PANGO_SCALE);
408 /* we've entered all possible digits, no cursor */
412 if (input_string.empty()) {
413 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
416 (upper_height - layout_height)/2.0,
425 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
427 CairoWidget::on_size_allocate (alloc);
430 upper_height = (get_height()/2.0) - 1.0;
432 upper_height = get_height();
437 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
439 Glib::RefPtr<Pango::Layout> tmp;
440 Glib::RefPtr<Gtk::Style> style = get_style ();
441 Pango::FontDescription font;
443 tmp = Pango::Layout::create (get_pango_context());
445 if (!is_realized()) {
446 font = get_font_for_style (get_name());
448 font = style->get_font();
451 tmp->set_font_description (font);
453 /* this string is the longest thing we will ever display */
454 tmp->set_text (" 88:88:88,888");
455 tmp->get_pixel_size (req.width, req.height);
457 layout_height = req.height;
458 layout_width = req.width;
462 AudioClock::on_size_request (Gtk::Requisition* req)
464 /* even for non fixed width clocks, the size we *ask* for never changes,
465 even though the size we receive might. so once we've computed it,
470 req->width = first_width;
471 req->height = first_height;
475 set_clock_dimensions (*req);
477 /* now tackle height, for which we need to know the height of the lower
483 Glib::RefPtr<Pango::Layout> tmp;
484 Glib::RefPtr<Gtk::Style> style = get_style ();
485 Pango::FontDescription font;
488 tmp = Pango::Layout::create (get_pango_context());
490 if (!is_realized()) {
491 font = get_font_for_style (get_name());
493 font = style->get_font();
496 tmp->set_font_description (font);
498 font.set_size (INFO_FONT_SIZE);
499 font.set_weight (Pango::WEIGHT_NORMAL);
500 tmp->set_font_description (font);
502 /* we only care about height, so put as much stuff in here
503 as possible that might change the height.
505 tmp->set_text ("qyhH|"); /* one ascender, one descender */
507 tmp->get_pixel_size (w, info_height);
509 /* silly extra padding that seems necessary to correct the info
510 * that pango just gave us. I have no idea why.
513 req->height += info_height;
514 req->height += separator_height;
519 AudioClock::show_edit_status (int length)
521 editing_attr->set_start_index (edit_string.length() - length);
522 editing_attr->set_end_index (edit_string.length());
524 editing_attributes.change (*foreground_attr);
525 editing_attributes.change (*editing_attr);
527 _layout->set_attributes (editing_attributes);
531 AudioClock::start_edit (Field f)
534 pre_edit_string = _layout->get_text ();
535 if (!insert_map.empty()) {
536 edit_string = pre_edit_string;
538 edit_string.clear ();
539 _layout->set_text ("");
542 input_string.clear ();
544 edit_is_negative = false;
547 input_string = get_field (f);
548 show_edit_status (merge_input_and_edit_string ());
549 _layout->set_text (edit_string);
554 Keyboard::magic_widget_grab_focus ();
560 AudioClock::get_field (Field f)
564 return edit_string.substr (1, 2);
566 case Timecode_Minutes:
567 return edit_string.substr (4, 2);
569 case Timecode_Seconds:
570 return edit_string.substr (7, 2);
572 case Timecode_Frames:
573 return edit_string.substr (10, 2);
576 return edit_string.substr (1, 2);
579 return edit_string.substr (4, 2);
582 return edit_string.substr (7, 2);
584 case MS_Milliseconds:
585 return edit_string.substr (10, 3);
588 return edit_string.substr (1, 3);
591 return edit_string.substr (5, 2);
594 return edit_string.substr (8, 4);
604 AudioClock::end_edit (bool modify)
612 ok = timecode_validate_edit (edit_string);
616 ok = bbt_validate_edit (edit_string);
620 ok = minsec_validate_edit (edit_string);
624 if (edit_string.length() < 1) {
625 edit_string = pre_edit_string;
631 edit_string = pre_edit_string;
632 input_string.clear ();
633 _layout->set_text (edit_string);
634 show_edit_status (0);
635 /* edit attributes remain in use */
639 framepos_t pos = 0; /* stupid gcc */
643 pos = frames_from_timecode_string (edit_string);
648 pos = frame_duration_from_bbt_string (0, edit_string);
650 pos = frames_from_bbt_string (0, edit_string);
655 pos = frames_from_minsec_string (edit_string);
659 pos = frames_from_audioframes_string (edit_string);
664 _layout->set_attributes (normal_attributes);
665 ValueChanged(); /* EMIT_SIGNAL */
671 edit_is_negative = false;
672 _layout->set_attributes (normal_attributes);
673 _layout->set_text (pre_edit_string);
684 AudioClock::drop_focus ()
686 Keyboard::magic_widget_drop_focus ();
690 /* move focus back to the default widget in the top level window */
692 Widget* top = get_toplevel();
694 if (top->is_toplevel ()) {
695 Window* win = dynamic_cast<Window*> (top);
702 AudioClock::parse_as_frames_distance (const std::string& str)
706 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
714 AudioClock::parse_as_minsec_distance (const std::string& str)
716 framecnt_t sr = _session->frame_rate();
722 switch (str.length()) {
729 sscanf (str.c_str(), "%" PRId32, &msecs);
730 return msecs * (sr / 1000);
733 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
734 return (secs * sr) + (msecs * (sr/1000));
737 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
738 return (secs * sr) + (msecs * (sr/1000));
741 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
742 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
745 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
746 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
749 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
750 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
753 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
754 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
764 AudioClock::parse_as_timecode_distance (const std::string& str)
766 double fps = _session->timecode_frames_per_second();
767 framecnt_t sr = _session->frame_rate();
773 switch (str.length()) {
778 sscanf (str.c_str(), "%" PRId32, &frames);
779 return llrint ((frames/(float)fps) * sr);
782 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
783 return (secs * sr) + llrint ((frames/(float)fps) * sr);
786 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
787 return (secs * sr) + llrint ((frames/(float)fps) * sr);
790 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
791 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
794 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
795 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
798 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
799 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
802 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
803 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
813 AudioClock::parse_as_bbt_distance (const std::string&)
819 AudioClock::parse_as_distance (const std::string& instr)
823 return parse_as_timecode_distance (instr);
826 return parse_as_frames_distance (instr);
829 return parse_as_bbt_distance (instr);
832 return parse_as_minsec_distance (instr);
839 AudioClock::end_edit_relative (bool add)
845 ok = timecode_validate_edit (edit_string);
849 ok = bbt_validate_edit (edit_string);
853 ok = minsec_validate_edit (edit_string);
861 edit_string = pre_edit_string;
862 input_string.clear ();
863 _layout->set_text (edit_string);
864 show_edit_status (0);
865 /* edit attributes remain in use */
870 framecnt_t frames = parse_as_distance (input_string);
875 _layout->set_attributes (normal_attributes);
879 set (current_time() + frames, true);
881 framepos_t c = current_time();
883 if (c > frames || _negative_allowed) {
884 set (c - frames, true);
889 ValueChanged (); /* EMIT SIGNAL */
892 input_string.clear ();
898 AudioClock::session_property_changed (const PropertyChange&)
900 set (last_when, true);
904 AudioClock::session_configuration_changed (std::string p)
906 if (_negative_allowed) {
907 /* session option editor clock */
911 if (p == "sync-source" || p == "external-sync") {
912 set (current_time(), true);
916 if (p != "timecode-offset" && p != "timecode-offset-negative") {
925 current = current_duration ();
927 current = current_time ();
937 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
939 if ((!force && !is_visible()) || _session == 0) {
944 when = when - offset;
947 if (when == last_when && !force) {
948 if (_mode != Timecode && _mode != MinSec) {
949 /* may need to force display of TC source
950 * time, so don't return early.
958 _right_layout->set_alignment(Pango::ALIGN_LEFT);
964 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
966 set_timecode (when, force);
970 set_bbt (when, force);
975 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
977 set_minsec (when, force);
981 set_frames (when, force);
991 AudioClock::set_slave_info ()
993 if (!_left_layout || !_right_layout) {
997 SyncSource sync_src = Config->get_sync_source();
999 if (_session->config.get_external_sync()) {
1000 Slave* slave = _session->slave();
1004 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1005 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1006 _right_layout->set_text ("");
1010 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1011 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1012 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1013 INFO_FONT_SIZE, slave->approximate_current_delta()));
1015 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1016 INFO_FONT_SIZE, _("--pending--")));
1017 _right_layout->set_text ("");
1024 TimecodeSlave* tcslave;
1025 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1026 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1027 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1028 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1029 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1030 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1031 INFO_FONT_SIZE, slave->approximate_current_delta()));
1034 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1035 INFO_FONT_SIZE, _("--pending--")));
1036 _right_layout->set_text ("");
1041 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1042 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1043 _right_layout->set_text ("");
1048 AudioClock::set_frames (framepos_t when, bool /*force*/)
1051 bool negative = false;
1054 _layout->set_text (" ----------");
1057 _left_layout->set_text ("");
1058 _right_layout->set_text ("");
1070 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1072 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1075 _layout->set_text (buf);
1078 framecnt_t rate = _session->frame_rate();
1080 if (fmod (rate, 100.0) == 0.0) {
1081 sprintf (buf, "%.1fkHz", rate/1000.0);
1083 sprintf (buf, "%" PRId64 "Hz", rate);
1086 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1087 INFO_FONT_SIZE, _("SR"), buf));
1089 float vid_pullup = _session->config.get_video_pullup();
1091 if (vid_pullup == 0.0) {
1092 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1093 INFO_FONT_SIZE, _("Pull")));
1095 sprintf (buf, _("%+.4f%%"), vid_pullup);
1096 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1097 INFO_FONT_SIZE, _("Pull"), buf));
1103 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1120 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1121 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1122 mins = (int) floor (left / (frame_rate * 60.0f));
1123 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1124 secs = (int) floor (left / (float) frame_rate);
1125 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1126 millisecs = floor (left * 1000.0 / (float) frame_rate);
1129 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1131 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1137 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1142 _layout->set_text (" --:--:--.---");
1145 _left_layout->set_text ("");
1146 _right_layout->set_text ("");
1152 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1154 _layout->set_text (buf);
1159 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1162 bool negative = false;
1165 _layout->set_text (" --:--:--:--");
1167 _left_layout->set_text ("");
1168 _right_layout->set_text ("");
1180 _session->timecode_duration (when, TC);
1182 _session->timecode_time (when, TC);
1185 TC.negative = TC.negative || negative;
1187 _layout->set_text (Timecode::timecode_format_time(TC));
1193 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1196 Timecode::BBT_Time BBT;
1197 bool negative = false;
1200 _layout->set_text (" ---|--|----");
1202 _left_layout->set_text ("");
1203 _right_layout->set_text ("");
1213 /* handle a common case */
1220 _session->tempo_map().bbt_time (when, BBT);
1225 _session->tempo_map().bbt_time (when, BBT);
1229 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1230 BBT.bars, BBT.beats, BBT.ticks);
1232 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1233 BBT.bars, BBT.beats, BBT.ticks);
1236 _layout->set_text (buf);
1238 if (_right_layout) {
1241 if (bbt_reference_time < 0) {
1244 pos = bbt_reference_time;
1247 TempoMetric m (_session->tempo_map().metric_at (pos));
1249 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1250 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1251 INFO_FONT_SIZE, buf, _("Tempo")));
1253 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1254 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1255 INFO_FONT_SIZE, buf, _("Meter")));
1260 AudioClock::set_session (Session *s)
1262 SessionHandlePtr::set_session (s);
1266 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1267 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1269 const XMLProperty* prop;
1270 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1271 AudioClock::Mode amode;
1274 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1275 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1277 if ((prop = (*i)->property (X_("mode"))) != 0) {
1278 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1281 if ((prop = (*i)->property (X_("on"))) != 0) {
1282 set_off (!string_is_affirmative (prop->value()));
1289 set (last_when, true);
1294 AudioClock::on_key_press_event (GdkEventKey* ev)
1302 int highlight_length;
1305 switch (ev->keyval) {
1348 case GDK_KP_Subtract:
1349 if (_negative_allowed && input_string.empty()) {
1350 edit_is_negative = true;
1351 edit_string.replace(0,1,"-");
1352 _layout->set_text (edit_string);
1355 end_edit_relative (false);
1361 end_edit_relative (true);
1374 ChangeAborted(); /* EMIT SIGNAL */
1379 if (!input_string.empty()) {
1380 /* delete the last key entered
1382 input_string = input_string.substr (0, input_string.length() - 1);
1384 goto use_input_string;
1390 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1391 /* too many digits: eat the key event, but do nothing with it */
1395 input_string.push_back (new_char);
1401 /* get this one in the right order, and to the right width */
1402 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1403 edit_string = edit_string.substr (0, edit_string.length() - 1);
1405 edit_string.push_back (new_char);
1407 if (!edit_string.empty()) {
1409 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1410 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1413 /* highlight the whole thing */
1414 highlight_length = edit_string.length();
1418 highlight_length = merge_input_and_edit_string ();
1421 if (edit_is_negative) {
1422 edit_string.replace(0,1,"-");
1424 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1425 edit_string.replace(0,1,"_");
1427 edit_string.replace(0,1," ");
1431 show_edit_status (highlight_length);
1432 _layout->set_text (edit_string);
1439 AudioClock::merge_input_and_edit_string ()
1441 /* merge with pre-edit-string into edit string */
1443 edit_string = pre_edit_string;
1445 if (input_string.empty()) {
1449 string::size_type target;
1450 for (string::size_type i = 0; i < input_string.length(); ++i) {
1451 target = insert_map[input_string.length() - 1 - i];
1452 edit_string[target] = input_string[i];
1454 /* highlight from end to wherever the last character was added */
1455 return edit_string.length() - insert_map[input_string.length()-1];
1460 AudioClock::on_key_release_event (GdkEventKey *ev)
1466 /* return true for keys that we used on press
1467 so that they cannot possibly do double-duty
1469 switch (ev->keyval) {
1492 case GDK_KP_Decimal:
1500 case GDK_KP_Subtract:
1508 AudioClock::index_to_field (int index) const
1513 return Timecode_Hours;
1514 } else if (index < 7) {
1515 return Timecode_Minutes;
1516 } else if (index < 10) {
1517 return Timecode_Seconds;
1519 return Timecode_Frames;
1525 } else if (index < 7) {
1533 return Timecode_Hours;
1534 } else if (index < 6) {
1536 } else if (index < 9) {
1539 return MS_Milliseconds;
1551 AudioClock::on_button_press_event (GdkEventButton *ev)
1553 switch (ev->button) {
1555 if (editable && !_off) {
1561 /* the text has been centered vertically, so adjust
1564 int xcenter = (get_width() - layout_width) /2;
1566 y = ev->y - ((upper_height - layout_height)/2);
1567 x = ev->x - xcenter;
1569 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1570 /* pretend it is a character on the far right */
1573 drag_field = index_to_field (index);
1575 /* make absolutely sure that the pointer is grabbed */
1576 gdk_pointer_grab(ev->window,false ,
1577 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1578 NULL,NULL,ev->time);
1580 drag_start_y = ev->y;
1594 AudioClock::on_button_release_event (GdkEventButton *ev)
1596 if (editable && !_off) {
1598 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1600 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1601 // we actually dragged so return without
1602 // setting editing focus, or we shift clicked
1605 if (ev->button == 1) {
1607 if (_edit_by_click_field) {
1609 int xcenter = (get_width() - layout_width) /2;
1612 int y = ev->y - ((upper_height - layout_height)/2);
1613 int x = ev->x - xcenter;
1616 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1620 f = index_to_field (index);
1623 case Timecode_Frames:
1624 case MS_Milliseconds:
1640 if (Keyboard::is_context_menu_event (ev)) {
1641 if (ops_menu == 0) {
1644 ops_menu->popup (1, ev->time);
1652 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1654 bool ret = CairoWidget::on_focus_out_event (ev);
1664 AudioClock::on_scroll_event (GdkEventScroll *ev)
1669 if (editing || _session == 0 || !editable || _off) {
1676 /* the text has been centered vertically, so adjust
1680 int xcenter = (get_width() - layout_width) /2;
1681 y = ev->y - ((upper_height - layout_height)/2);
1682 x = ev->x - xcenter;
1684 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1685 /* not in the main layout */
1689 Field f = index_to_field (index);
1690 framepos_t frames = 0;
1692 switch (ev->direction) {
1695 frames = get_frame_step (f);
1697 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1700 set (current_time() + frames, true);
1701 ValueChanged (); /* EMIT_SIGNAL */
1705 case GDK_SCROLL_DOWN:
1706 frames = get_frame_step (f);
1708 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1712 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1715 set (current_time() - frames, true);
1718 ValueChanged (); /* EMIT_SIGNAL */
1731 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1733 if (editing || _session == 0 || !dragging) {
1737 float pixel_frame_scale_factor = 0.2f;
1739 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1740 pixel_frame_scale_factor = 0.1f;
1744 if (Keyboard::modifier_state_contains (ev->state,
1745 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1747 pixel_frame_scale_factor = 0.025f;
1750 double y_delta = ev->y - drag_y;
1752 drag_accum += y_delta*pixel_frame_scale_factor;
1756 if (floor (drag_accum) != 0) {
1761 dir = (drag_accum < 0 ? 1:-1);
1762 pos = current_time();
1763 frames = get_frame_step (drag_field, pos, dir);
1765 if (frames != 0 && frames * drag_accum < current_time()) {
1766 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1772 ValueChanged(); /* EMIT_SIGNAL */
1779 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1782 Timecode::BBT_Time BBT;
1784 case Timecode_Hours:
1785 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1787 case Timecode_Minutes:
1788 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1790 case Timecode_Seconds:
1791 f = _session->frame_rate();
1793 case Timecode_Frames:
1794 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1802 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1805 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1808 f = (framecnt_t) _session->frame_rate();
1810 case MS_Milliseconds:
1811 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1818 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1824 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1830 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1833 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1842 AudioClock::current_time (framepos_t) const
1848 AudioClock::current_duration (framepos_t pos) const
1857 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1873 AudioClock::bbt_validate_edit (const string& str)
1877 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1881 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1885 if (!is_duration && any.bbt.bars == 0) {
1889 if (!is_duration && any.bbt.beats == 0) {
1897 AudioClock::timecode_validate_edit (const string&)
1903 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1904 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1909 TC.hours = hours * -1;
1913 TC.negative = false;
1916 if (TC.negative && !_negative_allowed) {
1920 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1924 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1928 if (_session->timecode_drop_frames()) {
1929 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1938 AudioClock::minsec_validate_edit (const string& str)
1940 int hrs, mins, secs, millisecs;
1942 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1946 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1954 AudioClock::frames_from_timecode_string (const string& str) const
1956 if (_session == 0) {
1965 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1966 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1969 TC.hours = abs(hours);
1970 TC.rate = _session->timecode_frames_per_second();
1971 TC.drop= _session->timecode_drop_frames();
1973 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1975 // timecode_tester ();
1976 if (edit_is_negative) {
1984 AudioClock::frames_from_minsec_string (const string& str) const
1986 if (_session == 0) {
1990 int hrs, mins, secs, millisecs;
1991 framecnt_t sr = _session->frame_rate();
1993 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1994 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1998 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2002 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2004 if (_session == 0) {
2005 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2010 any.type = AnyTime::BBT;
2012 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2019 return _session->any_duration_to_frames (pos, any);
2021 return _session->convert_to_frames (any);
2027 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2029 if (_session == 0) {
2030 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2034 Timecode::BBT_Time bbt;
2036 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2040 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2044 AudioClock::frames_from_audioframes_string (const string& str) const
2047 sscanf (str.c_str(), "%" PRId64, &f);
2052 AudioClock::build_ops_menu ()
2054 using namespace Menu_Helpers;
2055 ops_menu = new Menu;
2056 MenuList& ops_items = ops_menu->items();
2057 ops_menu->set_name ("ArdourContextMenu");
2059 if (!Profile->get_sae()) {
2060 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2062 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2063 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2064 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2066 if (editable && !_off && !is_duration && !_follows_playhead) {
2067 ops_items.push_back (SeparatorElem());
2068 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2069 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2074 AudioClock::set_from_playhead ()
2080 set (_session->transport_frame());
2085 AudioClock::locate ()
2087 if (!_session || is_duration) {
2091 _session->request_locate (current_time(), _session->transport_rolling ());
2095 AudioClock::set_mode (Mode m)
2105 _layout->set_text ("");
2109 _left_layout->set_attributes (info_attributes);
2110 _right_layout->set_attributes (info_attributes);
2111 /* adjust info_height according to font size */
2113 _left_layout->set_text (" 1234567890");
2114 _left_layout->get_pixel_size (ignored, info_height);
2116 _left_layout->set_text ("");
2117 _right_layout->set_text ("");
2122 mode_based_info_ratio = 0.6;
2123 insert_map.push_back (11);
2124 insert_map.push_back (10);
2125 insert_map.push_back (8);
2126 insert_map.push_back (7);
2127 insert_map.push_back (5);
2128 insert_map.push_back (4);
2129 insert_map.push_back (2);
2130 insert_map.push_back (1);
2134 mode_based_info_ratio = 0.5;
2135 insert_map.push_back (11);
2136 insert_map.push_back (10);
2137 insert_map.push_back (9);
2138 insert_map.push_back (8);
2139 insert_map.push_back (6);
2140 insert_map.push_back (5);
2141 insert_map.push_back (3);
2142 insert_map.push_back (2);
2143 insert_map.push_back (1);
2147 mode_based_info_ratio = 0.6;
2148 insert_map.push_back (12);
2149 insert_map.push_back (11);
2150 insert_map.push_back (10);
2151 insert_map.push_back (8);
2152 insert_map.push_back (7);
2153 insert_map.push_back (5);
2154 insert_map.push_back (4);
2155 insert_map.push_back (2);
2156 insert_map.push_back (1);
2160 mode_based_info_ratio = 0.45;
2164 set (last_when, true);
2166 if (!is_transient) {
2167 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2170 mode_changed (); /* EMIT SIGNAL (the member one) */
2174 AudioClock::set_bbt_reference (framepos_t pos)
2176 bbt_reference_time = pos;
2180 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2182 CairoWidget::on_style_changed (old_style);
2184 Gtk::Requisition req;
2185 set_clock_dimensions (req);
2192 AudioClock::set_editable (bool yn)
2198 AudioClock::set_is_duration (bool yn)
2200 if (yn == is_duration) {
2205 set (last_when, true);
2209 AudioClock::set_off (bool yn)
2217 /* force a redraw. last_when will be preserved, but the clock text will
2221 set (last_when, true);
2225 AudioClock::focus ()
2227 start_edit (Field (0));
2231 AudioClock::set_corner_radius (double r)
2240 AudioClock::dpi_reset ()
2242 /* force recomputation of size even if we are fixed width
2250 AudioClock::set_negative_allowed (bool yn)
2252 _negative_allowed = yn;