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>
28 #include "gtkmm2ext/cairocell.h"
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
32 #include "ardour/ardour.h"
33 #include "ardour/session.h"
34 #include "ardour/tempo.h"
35 #include "ardour/profile.h"
36 #include "ardour/slave.h"
37 #include <sigc++/bind.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.6;
57 const double AudioClock::separator_height = 2.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
63 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
64 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
66 , is_transient (transient)
67 , is_duration (duration)
68 , editable (allow_edit)
69 , _follows_playhead (follows_playhead)
74 , _edit_by_click_field (false)
84 , mode_based_info_ratio (1.0)
87 , bbt_reference_time (-1)
92 , drag_field (Field (0))
95 set_flags (CAN_FOCUS);
97 _layout = Pango::Layout::create (get_pango_context());
98 _layout->set_attributes (normal_attributes);
101 _left_layout = Pango::Layout::create (get_pango_context());
102 _right_layout = Pango::Layout::create (get_pango_context());
105 set_widget_name (widget_name);
107 _mode = BBT; /* lie to force mode switch */
109 set (last_when, true);
112 clocks.push_back (this);
115 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
116 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
119 AudioClock::~AudioClock ()
121 delete foreground_attr;
126 AudioClock::set_widget_name (const string& str)
131 set_name (str + " clock");
141 AudioClock::on_realize ()
143 CairoWidget::on_realize ();
149 AudioClock::set_font ()
151 Glib::RefPtr<Gtk::Style> style = get_style ();
152 Pango::FontDescription font;
153 Pango::AttrFontDesc* font_attr;
155 if (!is_realized()) {
156 font = get_font_for_style (get_name());
158 font = style->get_font();
161 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
163 normal_attributes.change (*font_attr);
164 editing_attributes.change (*font_attr);
166 /* now a smaller version of the same font */
169 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
170 font.set_weight (Pango::WEIGHT_NORMAL);
171 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
173 info_attributes.change (*font_attr);
177 /* get the figure width for the font. This doesn't have to super
178 * accurate since we only use it to measure the (roughly 1 character)
179 * offset from the position Pango tells us for the "cursor"
182 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
186 tmp->get_pixel_size (em_width, ignore_height);
190 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
192 CairoWidget::set_active_state (s);
197 AudioClock::set_colors ()
203 uint32_t editing_color;
204 uint32_t cursor_color;
206 if (active_state()) {
207 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
208 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
209 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
210 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
212 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
213 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
214 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
215 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
218 /* store for bg and cursor in render() */
220 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
227 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
234 /* rescale for Pango colors ... sigh */
236 r = lrint (r * 65535.0);
237 g = lrint (g * 65535.0);
238 b = lrint (b * 65535.0);
240 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
241 r = lrint ((r/255.0) * 65535.0);
242 g = lrint ((g/255.0) * 65535.0);
243 b = lrint ((b/255.0) * 65535.0);
244 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
246 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
247 r = lrint ((r/255.0) * 65535.0);
248 g = lrint ((g/255.0) * 65535.0);
249 b = lrint ((b/255.0) * 65535.0);
250 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
252 normal_attributes.change (*foreground_attr);
253 info_attributes.change (*foreground_attr);
254 editing_attributes.change (*foreground_attr);
255 editing_attributes.change (*editing_attr);
258 _layout->set_attributes (normal_attributes);
260 _layout->set_attributes (editing_attributes);
264 _left_layout->set_attributes (info_attributes);
265 _right_layout->set_attributes (info_attributes);
272 AudioClock::render (cairo_t* cr)
274 /* main layout: rounded rect, plus the text */
277 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
279 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
281 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
287 cairo_move_to (cr, layout_x_offset, 0);
289 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
292 pango_cairo_show_layout (cr, _layout->gobj());
296 double h = get_height() - upper_height - separator_height;
299 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
302 if (mode_based_info_ratio != 1.0) {
304 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
308 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
310 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
315 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
316 pango_cairo_show_layout (cr, _left_layout->gobj());
320 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
321 get_width() - separator_height - left_rect_width, h,
324 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
325 get_width() - separator_height - left_rect_width, h);
330 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
331 pango_cairo_show_layout (cr, _right_layout->gobj());
334 /* no info to display, or just one */
338 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
340 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
348 if (!insert_map.empty()) {
351 if (input_string.length() < insert_map.size()) {
352 Pango::Rectangle cursor;
354 if (input_string.empty()) {
355 /* nothing entered yet, put cursor at the end
358 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
360 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
363 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
366 min (get_width() - 2.0,
367 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + em_width), 0,
368 2.0, cursor.get_height()/PANGO_SCALE);
371 min (get_width() - 2.0,
372 (double) layout_x_offset + cursor.get_x()/PANGO_SCALE + em_width),
373 (upper_height - layout_height)/2.0,
374 2.0, cursor.get_height()/PANGO_SCALE);
378 /* we've entered all possible digits, no cursor */
382 if (input_string.empty()) {
383 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
392 (upper_height - layout_height)/2.0,
402 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
404 CairoWidget::on_size_allocate (alloc);
407 upper_height = (get_height()/2.0) - 1.0;
409 upper_height = get_height();
413 /* center display in available space */
414 layout_x_offset = (get_width() - layout_width)/2.0;
422 AudioClock::on_size_request (Gtk::Requisition* req)
424 /* even for non fixed width clocks, the size we *ask* for never changes,
425 even though the size we receive might. so once we've computed it,
430 req->width = first_width;
431 req->height = first_height;
435 Glib::RefPtr<Pango::Layout> tmp;
436 Glib::RefPtr<Gtk::Style> style = get_style ();
437 Pango::FontDescription font;
439 tmp = Pango::Layout::create (get_pango_context());
441 if (!is_realized()) {
442 font = get_font_for_style (get_name());
444 font = style->get_font();
447 tmp->set_font_description (font);
450 /* this string is the longest thing we will ever display,
451 and also includes the BBT bar char that may descends below
452 the baseline a bit, and a comma for the minsecs mode
453 where we printf a fractional value (XXX or should)
456 tmp->set_text (" 8888888888:,|");
460 tmp->set_text (" 88:88:88:88");
463 tmp->set_text (" 888|88|8888");
466 tmp->set_text (" 88:88:88,888");
469 tmp->set_text (" 8888888888");
474 tmp->get_pixel_size (req->width, req->height);
476 layout_height = req->height;
477 layout_width = req->width;
479 /* now tackle height, for which we need to know the height of the lower
487 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
488 font.set_weight (Pango::WEIGHT_NORMAL);
489 tmp->set_font_description (font);
491 /* we only care about height, so put as much stuff in here
492 as possible that might change the height.
494 tmp->set_text ("qyhH|"); /* one ascender, one descender */
496 tmp->get_pixel_size (w, info_height);
498 /* silly extra padding that seems necessary to correct the info
499 * that pango just gave us. I have no idea why.
504 req->height += info_height;
505 req->height += separator_height;
508 first_height = req->height;
509 first_width = req->width;
513 AudioClock::show_edit_status (int length)
515 editing_attr->set_start_index (edit_string.length() - length);
516 editing_attr->set_end_index (edit_string.length());
518 editing_attributes.change (*foreground_attr);
519 editing_attributes.change (*editing_attr);
521 _layout->set_attributes (editing_attributes);
525 AudioClock::start_edit (Field f)
527 pre_edit_string = _layout->get_text ();
528 if (!insert_map.empty()) {
529 edit_string = pre_edit_string;
531 edit_string.clear ();
532 _layout->set_text ("");
534 input_string.clear ();
538 input_string = get_field (f);
539 show_edit_status (merge_input_and_edit_string ());
540 _layout->set_text (edit_string);
545 Keyboard::magic_widget_grab_focus ();
550 AudioClock::get_field (Field f)
554 return edit_string.substr (1, 2);
556 case Timecode_Minutes:
557 return edit_string.substr (4, 2);
559 case Timecode_Seconds:
560 return edit_string.substr (7, 2);
562 case Timecode_Frames:
563 return edit_string.substr (10, 2);
566 return edit_string.substr (1, 2);
569 return edit_string.substr (4, 2);
572 return edit_string.substr (7, 2);
574 case MS_Milliseconds:
575 return edit_string.substr (10, 3);
578 return edit_string.substr (1, 3);
581 return edit_string.substr (5, 2);
584 return edit_string.substr (8, 4);
594 AudioClock::end_edit (bool modify)
602 ok = timecode_validate_edit (edit_string);
606 ok = bbt_validate_edit (edit_string);
610 ok = minsec_validate_edit (edit_string);
618 edit_string = pre_edit_string;
619 input_string.clear ();
620 _layout->set_text (edit_string);
621 show_edit_status (0);
622 /* edit attributes remain in use */
626 framepos_t pos = 0; /* stupid gcc */
630 pos = frames_from_timecode_string (edit_string);
635 pos = frame_duration_from_bbt_string (0, edit_string);
637 pos = frames_from_bbt_string (0, edit_string);
642 pos = frames_from_minsec_string (edit_string);
646 pos = frames_from_audioframes_string (edit_string);
651 _layout->set_attributes (normal_attributes);
652 ValueChanged(); /* EMIT_SIGNAL */
658 _layout->set_attributes (normal_attributes);
659 _layout->set_text (pre_edit_string);
670 AudioClock::drop_focus ()
672 Keyboard::magic_widget_drop_focus ();
676 /* move focus back to the default widget in the top level window */
678 Widget* top = get_toplevel();
680 if (top->is_toplevel ()) {
681 Window* win = dynamic_cast<Window*> (top);
688 AudioClock::parse_as_frames_distance (const std::string& str)
692 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
700 AudioClock::parse_as_minsec_distance (const std::string& str)
702 framecnt_t sr = _session->frame_rate();
708 switch (str.length()) {
715 sscanf (str.c_str(), "%" PRId32, &msecs);
716 return msecs * (sr / 1000);
719 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
720 return (secs * sr) + (msecs * (sr/1000));
723 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
724 return (secs * sr) + (msecs * (sr/1000));
727 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
728 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
731 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
732 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
735 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
736 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
739 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
740 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
750 AudioClock::parse_as_timecode_distance (const std::string& str)
752 double fps = _session->timecode_frames_per_second();
753 framecnt_t sr = _session->frame_rate();
759 switch (str.length()) {
764 sscanf (str.c_str(), "%" PRId32, &frames);
765 return lrint ((frames/(float)fps) * sr);
768 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
769 return (secs * sr) + lrint ((frames/(float)fps) * sr);
772 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
773 return (secs * sr) + lrint ((frames/(float)fps) * sr);
776 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
777 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
780 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
781 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
784 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
785 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
788 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
789 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
799 AudioClock::parse_as_bbt_distance (const std::string& str)
805 AudioClock::parse_as_distance (const std::string& instr)
809 return parse_as_timecode_distance (instr);
812 return parse_as_frames_distance (instr);
815 return parse_as_bbt_distance (instr);
818 return parse_as_minsec_distance (instr);
825 AudioClock::end_edit_relative (bool add)
831 ok = timecode_validate_edit (edit_string);
835 ok = bbt_validate_edit (edit_string);
839 ok = minsec_validate_edit (edit_string);
847 edit_string = pre_edit_string;
848 input_string.clear ();
849 _layout->set_text (edit_string);
850 show_edit_status (0);
851 /* edit attributes remain in use */
856 framecnt_t frames = parse_as_distance (input_string);
861 _layout->set_attributes (normal_attributes);
865 set (current_time() + frames, true);
867 framepos_t c = current_time();
870 set (c - frames, true);
875 ValueChanged (); /* EMIT SIGNAL */
878 input_string.clear ();
884 AudioClock::session_configuration_changed (std::string p)
886 if (p == "sync-source" || p == "external-sync") {
887 set (current_time(), true);
891 if (p != "timecode-offset" && p != "timecode-offset-negative") {
900 current = current_duration ();
902 current = current_time ();
912 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
914 if ((!force && !is_visible()) || _session == 0) {
919 when = when - offset;
922 if (when == last_when && !force) {
930 set_timecode (when, force);
934 set_bbt (when, force);
938 set_minsec (when, force);
942 set_frames (when, force);
952 AudioClock::set_frames (framepos_t when, bool /*force*/)
955 bool negative = false;
958 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
961 _left_layout->set_text ("");
962 _right_layout->set_text ("");
974 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
976 snprintf (buf, sizeof (buf), " %10" PRId64, when);
979 _layout->set_text (buf);
982 framecnt_t rate = _session->frame_rate();
984 if (fmod (rate, 100.0) == 0.0) {
985 sprintf (buf, "SR %.1fkHz", rate/1000.0);
987 sprintf (buf, "SR %" PRId64, rate);
990 _left_layout->set_text (buf);
992 float vid_pullup = _session->config.get_video_pullup();
994 if (vid_pullup == 0.0) {
995 _right_layout->set_text (_("pullup: \u2012"));
997 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
998 _right_layout->set_text (buf);
1004 AudioClock::set_minsec (framepos_t when, bool force)
1012 bool negative = false;
1015 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1018 _left_layout->set_text ("");
1019 _right_layout->set_text ("");
1031 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1032 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1033 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1034 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1035 secs = (int) floor (left / (float) _session->frame_rate());
1036 left -= (framecnt_t) floor (secs * _session->frame_rate());
1037 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1040 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1042 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1045 _layout->set_text (buf);
1049 AudioClock::set_timecode (framepos_t when, bool force)
1053 bool negative = false;
1056 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1058 _left_layout->set_text ("");
1059 _right_layout->set_text ("");
1071 _session->timecode_duration (when, TC);
1073 _session->timecode_time (when, TC);
1076 if (TC.negative || negative) {
1077 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
1079 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
1082 _layout->set_text (buf);
1086 if (_session->config.get_external_sync()) {
1087 switch (_session->config.get_sync_source()) {
1089 _left_layout->set_text ("JACK");
1092 _left_layout->set_text ("MTC");
1095 _left_layout->set_text ("M-Clock");
1099 _left_layout->set_text ("INT");
1102 double timecode_frames = _session->timecode_frames_per_second();
1104 if (fmod(timecode_frames, 1.0) == 0.0) {
1105 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1107 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1110 _right_layout->set_text (buf);
1115 AudioClock::set_bbt (framepos_t when, bool force)
1118 Timecode::BBT_Time BBT;
1119 bool negative = false;
1122 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1124 _left_layout->set_text ("");
1125 _right_layout->set_text ("");
1135 /* handle a common case */
1142 _session->tempo_map().bbt_time (when, BBT);
1147 _session->tempo_map().bbt_time (when, BBT);
1151 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1152 BBT.bars, BBT.beats, BBT.ticks);
1154 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1155 BBT.bars, BBT.beats, BBT.ticks);
1158 _layout->set_text (buf);
1160 if (_right_layout) {
1163 if (bbt_reference_time < 0) {
1166 pos = bbt_reference_time;
1169 TempoMetric m (_session->tempo_map().metric_at (pos));
1171 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1172 _left_layout->set_text (buf);
1174 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1175 _right_layout->set_text (buf);
1180 AudioClock::set_session (Session *s)
1182 SessionHandlePtr::set_session (s);
1186 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1188 const XMLProperty* prop;
1189 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1190 AudioClock::Mode amode;
1193 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1194 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1196 if ((prop = (*i)->property (X_("mode"))) != 0) {
1197 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1200 if ((prop = (*i)->property (X_("on"))) != 0) {
1201 set_off (!string_is_affirmative (prop->value()));
1208 set (last_when, true);
1213 AudioClock::on_key_press_event (GdkEventKey* ev)
1221 int highlight_length;
1224 switch (ev->keyval) {
1267 case GDK_KP_Subtract:
1268 end_edit_relative (false);
1274 end_edit_relative (true);
1287 ChangeAborted(); /* EMIT SIGNAL */
1292 if (!input_string.empty()) {
1293 /* delete the last key entered
1295 input_string = input_string.substr (0, input_string.length() - 1);
1297 goto use_input_string;
1303 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1304 /* too many digits: eat the key event, but do nothing with it */
1308 input_string.push_back (new_char);
1314 /* get this one in the right order, and to the right width */
1315 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1316 edit_string = edit_string.substr (0, edit_string.length() - 1);
1318 edit_string.push_back (new_char);
1320 if (!edit_string.empty()) {
1322 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1323 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1326 /* highlight the whole thing */
1327 highlight_length = edit_string.length();
1331 highlight_length = merge_input_and_edit_string ();
1334 show_edit_status (highlight_length);
1335 _layout->set_text (edit_string);
1342 AudioClock::merge_input_and_edit_string ()
1344 /* merge with pre-edit-string into edit string */
1346 edit_string = pre_edit_string;
1348 if (input_string.empty()) {
1352 string::size_type target;
1353 for (string::size_type i = 0; i < input_string.length(); ++i) {
1354 target = insert_map[input_string.length() - 1 - i];
1355 edit_string[target] = input_string[i];
1357 /* highlight from end to wherever the last character was added */
1358 return edit_string.length() - insert_map[input_string.length()-1];
1363 AudioClock::on_key_release_event (GdkEventKey *ev)
1369 /* return true for keys that we used on press
1370 so that they cannot possibly do double-duty
1372 switch (ev->keyval) {
1395 case GDK_KP_Decimal:
1403 case GDK_KP_Subtract:
1411 AudioClock::index_to_field (int index) const
1416 return Timecode_Hours;
1417 } else if (index < 7) {
1418 return Timecode_Minutes;
1419 } else if (index < 10) {
1420 return Timecode_Seconds;
1422 return Timecode_Frames;
1428 } else if (index < 7) {
1436 return Timecode_Hours;
1437 } else if (index < 6) {
1439 } else if (index < 9) {
1442 return MS_Milliseconds;
1454 AudioClock::on_button_press_event (GdkEventButton *ev)
1456 switch (ev->button) {
1458 if (editable && !_off) {
1460 /* make absolutely sure that the pointer is grabbed */
1461 gdk_pointer_grab(ev->window,false ,
1462 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1463 NULL,NULL,ev->time);
1465 drag_start_y = ev->y;
1473 /* the text has been centered vertically, so adjust
1477 y = ev->y - ((upper_height - layout_height)/2);
1478 x = ev->x - layout_x_offset;
1480 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1481 drag_field = index_to_field (index);
1483 drag_field = Field (0);
1497 AudioClock::on_button_release_event (GdkEventButton *ev)
1499 if (editable && !_off) {
1501 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1503 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1504 // we actually dragged so return without
1505 // setting editing focus, or we shift clicked
1508 if (ev->button == 1) {
1510 if (_edit_by_click_field) {
1514 int y = ev->y - ((upper_height - layout_height)/2);
1515 int x = ev->x - layout_x_offset;
1518 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1522 f = index_to_field (index);
1525 case Timecode_Frames:
1526 case MS_Milliseconds:
1542 if (Keyboard::is_context_menu_event (ev)) {
1543 if (ops_menu == 0) {
1546 ops_menu->popup (1, ev->time);
1554 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1556 bool ret = CairoWidget::on_focus_out_event (ev);
1566 AudioClock::on_scroll_event (GdkEventScroll *ev)
1571 if (editing || _session == 0 || !editable || _off) {
1578 /* the text has been centered vertically, so adjust
1582 y = ev->y - ((upper_height - layout_height)/2);
1583 x = ev->x - layout_x_offset;
1585 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1586 /* not in the main layout */
1590 Field f = index_to_field (index);
1591 framepos_t frames = 0;
1593 switch (ev->direction) {
1596 frames = get_frame_step (f);
1598 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1601 set (current_time() + frames, true);
1602 ValueChanged (); /* EMIT_SIGNAL */
1606 case GDK_SCROLL_DOWN:
1607 frames = get_frame_step (f);
1609 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1613 if ((double)current_time() - (double)frames < 0.0) {
1616 set (current_time() - frames, true);
1619 ValueChanged (); /* EMIT_SIGNAL */
1632 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1634 if (editing || _session == 0 || !dragging) {
1638 float pixel_frame_scale_factor = 0.2f;
1640 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1641 pixel_frame_scale_factor = 0.1f;
1645 if (Keyboard::modifier_state_contains (ev->state,
1646 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1648 pixel_frame_scale_factor = 0.025f;
1651 double y_delta = ev->y - drag_y;
1653 drag_accum += y_delta*pixel_frame_scale_factor;
1657 if (trunc (drag_accum) != 0) {
1662 dir = (drag_accum < 0 ? 1:-1);
1663 pos = current_time();
1664 frames = get_frame_step (drag_field,pos,dir);
1666 if (frames != 0 && frames * drag_accum < current_time()) {
1667 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1673 ValueChanged(); /* EMIT_SIGNAL */
1680 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1683 Timecode::BBT_Time BBT;
1685 case Timecode_Hours:
1686 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1688 case Timecode_Minutes:
1689 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1691 case Timecode_Seconds:
1692 f = _session->frame_rate();
1694 case Timecode_Frames:
1695 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1703 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1706 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1709 f = (framecnt_t) _session->frame_rate();
1711 case MS_Milliseconds:
1712 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1719 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1725 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1731 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1734 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1743 AudioClock::current_time (framepos_t pos) const
1749 AudioClock::current_duration (framepos_t pos) const
1758 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1774 AudioClock::bbt_validate_edit (const string& str)
1778 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1782 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_bar_division) {
1786 if (!is_duration && any.bbt.bars == 0) {
1790 if (!is_duration && any.bbt.beats == 0) {
1798 AudioClock::timecode_validate_edit (const string& str)
1802 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1803 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1807 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1811 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1815 if (_session->timecode_drop_frames()) {
1816 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1825 AudioClock::minsec_validate_edit (const string& str)
1827 int hrs, mins, secs, millisecs;
1829 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1833 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1841 AudioClock::frames_from_timecode_string (const string& str) const
1843 if (_session == 0) {
1850 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1851 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1855 TC.rate = _session->timecode_frames_per_second();
1856 TC.drop= _session->timecode_drop_frames();
1858 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1860 // timecode_tester ();
1866 AudioClock::frames_from_minsec_string (const string& str) const
1868 if (_session == 0) {
1872 int hrs, mins, secs, millisecs;
1873 framecnt_t sr = _session->frame_rate();
1875 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1876 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1880 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1884 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1886 if (_session == 0) {
1887 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1892 any.type = AnyTime::BBT;
1894 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1901 return _session->any_duration_to_frames (pos, any);
1903 return _session->convert_to_frames (any);
1909 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1911 if (_session == 0) {
1912 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1916 Timecode::BBT_Time bbt;
1918 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1922 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1926 AudioClock::frames_from_audioframes_string (const string& str) const
1929 sscanf (str.c_str(), "%" PRId64, &f);
1934 AudioClock::build_ops_menu ()
1936 using namespace Menu_Helpers;
1937 ops_menu = new Menu;
1938 MenuList& ops_items = ops_menu->items();
1939 ops_menu->set_name ("ArdourContextMenu");
1941 if (!Profile->get_sae()) {
1942 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1944 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1945 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1946 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1948 if (editable && !_off && !is_duration && !_follows_playhead) {
1949 ops_items.push_back (SeparatorElem());
1950 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1951 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1956 AudioClock::set_from_playhead ()
1962 set (_session->transport_frame());
1967 AudioClock::locate ()
1969 if (!_session || is_duration) {
1973 _session->request_locate (current_time(), _session->transport_rolling ());
1977 AudioClock::set_mode (Mode m)
1987 _layout->set_text ("");
1990 _left_layout->set_text ("");
1991 _right_layout->set_text ("");
1996 mode_based_info_ratio = 0.5;
1997 insert_map.push_back (11);
1998 insert_map.push_back (10);
1999 insert_map.push_back (8);
2000 insert_map.push_back (7);
2001 insert_map.push_back (5);
2002 insert_map.push_back (4);
2003 insert_map.push_back (2);
2004 insert_map.push_back (1);
2008 mode_based_info_ratio = 0.5;
2009 insert_map.push_back (11);
2010 insert_map.push_back (10);
2011 insert_map.push_back (9);
2012 insert_map.push_back (8);
2013 insert_map.push_back (6);
2014 insert_map.push_back (5);
2015 insert_map.push_back (3);
2016 insert_map.push_back (2);
2017 insert_map.push_back (1);
2021 mode_based_info_ratio = 1.0;
2022 insert_map.push_back (12);
2023 insert_map.push_back (11);
2024 insert_map.push_back (10);
2025 insert_map.push_back (8);
2026 insert_map.push_back (7);
2027 insert_map.push_back (5);
2028 insert_map.push_back (4);
2029 insert_map.push_back (2);
2030 insert_map.push_back (1);
2034 mode_based_info_ratio = 0.5;
2038 set (last_when, true);
2040 if (!is_transient) {
2041 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2044 if (!_fixed_width) {
2045 /* display is different, allow us to resize */
2051 mode_changed (); /* EMIT SIGNAL (the member one) */
2055 AudioClock::set_bbt_reference (framepos_t pos)
2057 bbt_reference_time = pos;
2061 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2063 CairoWidget::on_style_changed (old_style);
2069 AudioClock::set_editable (bool yn)
2075 AudioClock::set_is_duration (bool yn)
2077 if (yn == is_duration) {
2082 set (last_when, true);
2086 AudioClock::set_off (bool yn)
2094 /* force a redraw. last_when will be preserved, but the clock text will
2098 set (last_when, true);
2102 AudioClock::focus ()
2104 start_edit (Field (0));
2108 AudioClock::set_corner_radius (double r)
2115 AudioClock::set_fixed_width (bool yn)
2121 AudioClock::dpi_reset ()
2123 /* force recomputation of size even if we are fixed width