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/types.h"
34 #include "ardour/session.h"
35 #include "ardour/tempo.h"
36 #include "ardour/profile.h"
38 #include "ardour_ui.h"
39 #include "audio_clock.h"
40 #include "global_signals.h"
43 #include "gui_thread.h"
46 using namespace ARDOUR;
51 using Gtkmm2ext::Keyboard;
53 sigc::signal<void> AudioClock::ModeChanged;
54 vector<AudioClock*> AudioClock::clocks;
55 const double AudioClock::info_font_scale_factor = 0.6;
56 const double AudioClock::separator_height = 2.0;
57 const double AudioClock::x_leading_padding = 6.0;
59 #define BBT_BAR_CHAR "|"
60 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
62 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
63 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)
83 , mode_based_info_ratio (1.0)
86 , bbt_reference_time (-1)
91 , drag_field (Field (0))
94 set_flags (CAN_FOCUS);
96 _layout = Pango::Layout::create (get_pango_context());
97 _layout->set_attributes (normal_attributes);
100 _left_layout = Pango::Layout::create (get_pango_context());
101 _right_layout = Pango::Layout::create (get_pango_context());
104 set_widget_name (widget_name);
106 _mode = BBT; /* lie to force mode switch */
108 set (last_when, true);
111 clocks.push_back (this);
114 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
115 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
118 AudioClock::~AudioClock ()
120 delete foreground_attr;
125 AudioClock::set_widget_name (const string& str)
130 set_name (str + " clock");
140 AudioClock::on_realize ()
142 CairoWidget::on_realize ();
148 AudioClock::set_font ()
150 Glib::RefPtr<Gtk::Style> style = get_style ();
151 Pango::FontDescription font;
152 Pango::AttrFontDesc* font_attr;
154 if (!is_realized()) {
155 font = get_font_for_style (get_name());
157 font = style->get_font();
160 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
162 normal_attributes.change (*font_attr);
163 editing_attributes.change (*font_attr);
165 /* now a smaller version of the same font */
168 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
169 font.set_weight (Pango::WEIGHT_NORMAL);
170 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
172 info_attributes.change (*font_attr);
176 /* get the figure width for the font. This doesn't have to super
177 * accurate since we only use it to measure the (roughly 1 character)
178 * offset from the position Pango tells us for the "cursor"
181 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
185 tmp->get_pixel_size (em_width, ignore_height);
189 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
191 CairoWidget::set_active_state (s);
196 AudioClock::set_colors ()
202 uint32_t editing_color;
203 uint32_t cursor_color;
205 if (active_state()) {
206 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
207 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
208 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
209 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
211 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
212 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
213 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
214 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
217 /* store for bg and cursor in render() */
219 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
226 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
233 /* rescale for Pango colors ... sigh */
235 r = lrint (r * 65535.0);
236 g = lrint (g * 65535.0);
237 b = lrint (b * 65535.0);
239 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
240 r = lrint ((r/255.0) * 65535.0);
241 g = lrint ((g/255.0) * 65535.0);
242 b = lrint ((b/255.0) * 65535.0);
243 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
245 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
246 r = lrint ((r/255.0) * 65535.0);
247 g = lrint ((g/255.0) * 65535.0);
248 b = lrint ((b/255.0) * 65535.0);
249 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
251 normal_attributes.change (*foreground_attr);
252 info_attributes.change (*foreground_attr);
253 editing_attributes.change (*foreground_attr);
254 editing_attributes.change (*editing_attr);
257 _layout->set_attributes (normal_attributes);
259 _layout->set_attributes (editing_attributes);
263 _left_layout->set_attributes (info_attributes);
264 _right_layout->set_attributes (info_attributes);
271 AudioClock::render (cairo_t* cr)
273 /* main layout: rounded rect, plus the text */
276 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
278 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
280 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
286 cairo_move_to (cr, layout_x_offset, 0);
288 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
291 pango_cairo_show_layout (cr, _layout->gobj());
295 double h = get_height() - upper_height - separator_height;
298 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
301 if (mode_based_info_ratio != 1.0) {
303 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
307 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
309 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
314 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
315 pango_cairo_show_layout (cr, _left_layout->gobj());
319 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
320 get_width() - separator_height - left_rect_width, h,
323 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
324 get_width() - separator_height - left_rect_width, h);
329 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
330 pango_cairo_show_layout (cr, _right_layout->gobj());
333 /* no info to display, or just one */
337 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
339 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
347 if (!insert_map.empty()) {
350 if (input_string.length() < insert_map.size()) {
351 Pango::Rectangle cursor;
353 if (input_string.empty()) {
354 /* nothing entered yet, put cursor at the end
357 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
359 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
362 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
365 min (get_width() - 2.0,
366 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + em_width), 0,
367 2.0, cursor.get_height()/PANGO_SCALE);
370 min (get_width() - 2.0,
371 (double) layout_x_offset + cursor.get_x()/PANGO_SCALE + em_width),
372 (upper_height - layout_height)/2.0,
373 2.0, cursor.get_height()/PANGO_SCALE);
377 /* we've entered all possible digits, no cursor */
381 if (input_string.empty()) {
382 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
391 (upper_height - layout_height)/2.0,
401 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
403 CairoWidget::on_size_allocate (alloc);
406 upper_height = (get_height()/2.0) - 1.0;
408 upper_height = get_height();
412 /* center display in available space */
413 layout_x_offset = (get_width() - layout_width)/2.0;
421 AudioClock::on_size_request (Gtk::Requisition* req)
423 /* even for non fixed width clocks, the size we *ask* for never changes,
424 even though the size we receive might. so once we've computed it,
429 req->width = first_width;
430 req->height = first_height;
434 Glib::RefPtr<Pango::Layout> tmp;
435 Glib::RefPtr<Gtk::Style> style = get_style ();
436 Pango::FontDescription font;
438 tmp = Pango::Layout::create (get_pango_context());
440 if (!is_realized()) {
441 font = get_font_for_style (get_name());
443 font = style->get_font();
446 tmp->set_font_description (font);
449 /* this string is the longest thing we will ever display,
450 and also includes the BBT bar char that may descends below
451 the baseline a bit, and a comma for the minsecs mode
452 where we printf a fractional value (XXX or should)
455 tmp->set_text (" 8888888888:,|");
459 tmp->set_text (" 88:88:88:88");
462 tmp->set_text (" 888|88|8888");
465 tmp->set_text (" 88:88:88,888");
468 tmp->set_text (" 8888888888");
473 tmp->get_pixel_size (req->width, req->height);
475 layout_height = req->height;
476 layout_width = req->width;
478 /* now tackle height, for which we need to know the height of the lower
486 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
487 font.set_weight (Pango::WEIGHT_NORMAL);
488 tmp->set_font_description (font);
490 /* we only care about height, so put as much stuff in here
491 as possible that might change the height.
493 tmp->set_text ("qyhH|"); /* one ascender, one descender */
495 tmp->get_pixel_size (w, info_height);
497 /* silly extra padding that seems necessary to correct the info
498 * that pango just gave us. I have no idea why.
503 req->height += info_height;
504 req->height += separator_height;
507 first_height = req->height;
508 first_width = req->width;
512 AudioClock::show_edit_status (int length)
514 editing_attr->set_start_index (edit_string.length() - length);
515 editing_attr->set_end_index (edit_string.length());
517 editing_attributes.change (*foreground_attr);
518 editing_attributes.change (*editing_attr);
520 _layout->set_attributes (editing_attributes);
524 AudioClock::start_edit (Field f)
526 pre_edit_string = _layout->get_text ();
527 if (!insert_map.empty()) {
528 edit_string = pre_edit_string;
530 edit_string.clear ();
531 _layout->set_text ("");
533 input_string.clear ();
537 input_string = get_field (f);
538 show_edit_status (merge_input_and_edit_string ());
539 _layout->set_text (edit_string);
544 Keyboard::magic_widget_grab_focus ();
549 AudioClock::get_field (Field f)
553 return edit_string.substr (1, 2);
555 case Timecode_Minutes:
556 return edit_string.substr (4, 2);
558 case Timecode_Seconds:
559 return edit_string.substr (7, 2);
561 case Timecode_Frames:
562 return edit_string.substr (10, 2);
565 return edit_string.substr (1, 2);
568 return edit_string.substr (4, 2);
571 return edit_string.substr (7, 2);
573 case MS_Milliseconds:
574 return edit_string.substr (10, 3);
577 return edit_string.substr (1, 3);
580 return edit_string.substr (5, 2);
583 return edit_string.substr (8, 4);
593 AudioClock::end_edit (bool modify)
601 ok = timecode_validate_edit (edit_string);
605 ok = bbt_validate_edit (edit_string);
609 ok = minsec_validate_edit (edit_string);
617 edit_string = pre_edit_string;
618 input_string.clear ();
619 _layout->set_text (edit_string);
620 show_edit_status (0);
621 /* edit attributes remain in use */
625 framepos_t pos = 0; /* stupid gcc */
629 pos = frames_from_timecode_string (edit_string);
634 pos = frame_duration_from_bbt_string (0, edit_string);
636 pos = frames_from_bbt_string (0, edit_string);
641 pos = frames_from_minsec_string (edit_string);
645 pos = frames_from_audioframes_string (edit_string);
650 _layout->set_attributes (normal_attributes);
651 ValueChanged(); /* EMIT_SIGNAL */
657 _layout->set_attributes (normal_attributes);
658 _layout->set_text (pre_edit_string);
669 AudioClock::drop_focus ()
671 Keyboard::magic_widget_drop_focus ();
675 /* move focus back to the default widget in the top level window */
677 Widget* top = get_toplevel();
679 if (top->is_toplevel ()) {
680 Window* win = dynamic_cast<Window*> (top);
687 AudioClock::parse_as_frames_distance (const std::string& str)
691 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
699 AudioClock::parse_as_minsec_distance (const std::string& str)
701 framecnt_t sr = _session->frame_rate();
707 switch (str.length()) {
714 sscanf (str.c_str(), "%" PRId32, &msecs);
715 return msecs * (sr / 1000);
718 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
719 return (secs * sr) + (msecs * (sr/1000));
722 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
723 return (secs * sr) + (msecs * (sr/1000));
726 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
727 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
730 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
731 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
734 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
735 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
738 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
739 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
749 AudioClock::parse_as_timecode_distance (const std::string& str)
751 double fps = _session->timecode_frames_per_second();
752 framecnt_t sr = _session->frame_rate();
758 switch (str.length()) {
763 sscanf (str.c_str(), "%" PRId32, &frames);
764 return lrint ((frames/(float)fps) * sr);
767 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
768 return (secs * sr) + lrint ((frames/(float)fps) * sr);
771 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
772 return (secs * sr) + lrint ((frames/(float)fps) * sr);
775 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
776 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
779 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
780 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
783 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
784 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
787 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
788 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
798 AudioClock::parse_as_bbt_distance (const std::string&)
804 AudioClock::parse_as_distance (const std::string& instr)
808 return parse_as_timecode_distance (instr);
811 return parse_as_frames_distance (instr);
814 return parse_as_bbt_distance (instr);
817 return parse_as_minsec_distance (instr);
824 AudioClock::end_edit_relative (bool add)
830 ok = timecode_validate_edit (edit_string);
834 ok = bbt_validate_edit (edit_string);
838 ok = minsec_validate_edit (edit_string);
846 edit_string = pre_edit_string;
847 input_string.clear ();
848 _layout->set_text (edit_string);
849 show_edit_status (0);
850 /* edit attributes remain in use */
855 framecnt_t frames = parse_as_distance (input_string);
860 _layout->set_attributes (normal_attributes);
864 set (current_time() + frames, true);
866 framepos_t c = current_time();
869 set (c - frames, true);
874 ValueChanged (); /* EMIT SIGNAL */
877 input_string.clear ();
883 AudioClock::session_configuration_changed (std::string p)
885 if (p == "sync-source" || p == "external-sync") {
886 set (current_time(), true);
890 if (p != "timecode-offset" && p != "timecode-offset-negative") {
899 current = current_duration ();
901 current = current_time ();
911 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
913 if ((!force && !is_visible()) || _session == 0) {
918 when = when - offset;
921 if (when == last_when && !force) {
929 set_timecode (when, force);
933 set_bbt (when, force);
937 set_minsec (when, force);
941 set_frames (when, force);
951 AudioClock::set_frames (framepos_t when, bool /*force*/)
954 bool negative = false;
957 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
960 _left_layout->set_text ("");
961 _right_layout->set_text ("");
973 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
975 snprintf (buf, sizeof (buf), " %10" PRId64, when);
978 _layout->set_text (buf);
981 framecnt_t rate = _session->frame_rate();
983 if (fmod (rate, 100.0) == 0.0) {
984 sprintf (buf, "SR %.1fkHz", rate/1000.0);
986 sprintf (buf, "SR %" PRId64, rate);
989 _left_layout->set_text (buf);
991 float vid_pullup = _session->config.get_video_pullup();
993 if (vid_pullup == 0.0) {
994 _right_layout->set_text (_("pullup: \u2012"));
996 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
997 _right_layout->set_text (buf);
1003 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1011 bool negative = false;
1014 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1017 _left_layout->set_text ("");
1018 _right_layout->set_text ("");
1030 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1031 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1032 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1033 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1034 secs = (int) floor (left / (float) _session->frame_rate());
1035 left -= (framecnt_t) floor (secs * _session->frame_rate());
1036 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1039 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1041 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1044 _layout->set_text (buf);
1048 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1052 bool negative = false;
1055 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1057 _left_layout->set_text ("");
1058 _right_layout->set_text ("");
1070 _session->timecode_duration (when, TC);
1072 _session->timecode_time (when, TC);
1075 if (TC.negative || negative) {
1076 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
1078 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
1081 _layout->set_text (buf);
1085 if (_session->config.get_external_sync()) {
1086 switch (_session->config.get_sync_source()) {
1088 _left_layout->set_text ("JACK");
1091 _left_layout->set_text ("MTC");
1094 _left_layout->set_text ("M-Clock");
1098 _left_layout->set_text ("INT");
1101 double timecode_frames = _session->timecode_frames_per_second();
1103 if (fmod(timecode_frames, 1.0) == 0.0) {
1104 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1106 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1109 _right_layout->set_text (buf);
1114 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1117 Timecode::BBT_Time BBT;
1118 bool negative = false;
1121 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1123 _left_layout->set_text ("");
1124 _right_layout->set_text ("");
1134 /* handle a common case */
1141 _session->tempo_map().bbt_time (when, BBT);
1146 _session->tempo_map().bbt_time (when, BBT);
1150 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1151 BBT.bars, BBT.beats, BBT.ticks);
1153 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1154 BBT.bars, BBT.beats, BBT.ticks);
1157 _layout->set_text (buf);
1159 if (_right_layout) {
1162 if (bbt_reference_time < 0) {
1165 pos = bbt_reference_time;
1168 TempoMetric m (_session->tempo_map().metric_at (pos));
1170 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1171 _left_layout->set_text (buf);
1173 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1174 _right_layout->set_text (buf);
1179 AudioClock::set_session (Session *s)
1181 SessionHandlePtr::set_session (s);
1185 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1187 const XMLProperty* prop;
1188 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1189 AudioClock::Mode amode;
1192 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1193 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1195 if ((prop = (*i)->property (X_("mode"))) != 0) {
1196 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1199 if ((prop = (*i)->property (X_("on"))) != 0) {
1200 set_off (!string_is_affirmative (prop->value()));
1207 set (last_when, true);
1212 AudioClock::on_key_press_event (GdkEventKey* ev)
1220 int highlight_length;
1223 switch (ev->keyval) {
1266 case GDK_KP_Subtract:
1267 end_edit_relative (false);
1273 end_edit_relative (true);
1286 ChangeAborted(); /* EMIT SIGNAL */
1291 if (!input_string.empty()) {
1292 /* delete the last key entered
1294 input_string = input_string.substr (0, input_string.length() - 1);
1296 goto use_input_string;
1302 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1303 /* too many digits: eat the key event, but do nothing with it */
1307 input_string.push_back (new_char);
1313 /* get this one in the right order, and to the right width */
1314 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1315 edit_string = edit_string.substr (0, edit_string.length() - 1);
1317 edit_string.push_back (new_char);
1319 if (!edit_string.empty()) {
1321 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1322 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1325 /* highlight the whole thing */
1326 highlight_length = edit_string.length();
1330 highlight_length = merge_input_and_edit_string ();
1333 show_edit_status (highlight_length);
1334 _layout->set_text (edit_string);
1341 AudioClock::merge_input_and_edit_string ()
1343 /* merge with pre-edit-string into edit string */
1345 edit_string = pre_edit_string;
1347 if (input_string.empty()) {
1351 string::size_type target;
1352 for (string::size_type i = 0; i < input_string.length(); ++i) {
1353 target = insert_map[input_string.length() - 1 - i];
1354 edit_string[target] = input_string[i];
1356 /* highlight from end to wherever the last character was added */
1357 return edit_string.length() - insert_map[input_string.length()-1];
1362 AudioClock::on_key_release_event (GdkEventKey *ev)
1368 /* return true for keys that we used on press
1369 so that they cannot possibly do double-duty
1371 switch (ev->keyval) {
1394 case GDK_KP_Decimal:
1402 case GDK_KP_Subtract:
1410 AudioClock::index_to_field (int index) const
1415 return Timecode_Hours;
1416 } else if (index < 7) {
1417 return Timecode_Minutes;
1418 } else if (index < 10) {
1419 return Timecode_Seconds;
1421 return Timecode_Frames;
1427 } else if (index < 7) {
1435 return Timecode_Hours;
1436 } else if (index < 6) {
1438 } else if (index < 9) {
1441 return MS_Milliseconds;
1453 AudioClock::on_button_press_event (GdkEventButton *ev)
1455 switch (ev->button) {
1457 if (editable && !_off) {
1459 /* make absolutely sure that the pointer is grabbed */
1460 gdk_pointer_grab(ev->window,false ,
1461 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1462 NULL,NULL,ev->time);
1464 drag_start_y = ev->y;
1472 /* the text has been centered vertically, so adjust
1476 y = ev->y - ((upper_height - layout_height)/2);
1477 x = ev->x - layout_x_offset;
1479 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1480 drag_field = index_to_field (index);
1482 drag_field = Field (0);
1496 AudioClock::on_button_release_event (GdkEventButton *ev)
1498 if (editable && !_off) {
1500 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1502 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1503 // we actually dragged so return without
1504 // setting editing focus, or we shift clicked
1507 if (ev->button == 1) {
1509 if (_edit_by_click_field) {
1513 int y = ev->y - ((upper_height - layout_height)/2);
1514 int x = ev->x - layout_x_offset;
1517 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1521 f = index_to_field (index);
1524 case Timecode_Frames:
1525 case MS_Milliseconds:
1541 if (Keyboard::is_context_menu_event (ev)) {
1542 if (ops_menu == 0) {
1545 ops_menu->popup (1, ev->time);
1553 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1555 bool ret = CairoWidget::on_focus_out_event (ev);
1565 AudioClock::on_scroll_event (GdkEventScroll *ev)
1570 if (editing || _session == 0 || !editable || _off) {
1577 /* the text has been centered vertically, so adjust
1581 y = ev->y - ((upper_height - layout_height)/2);
1582 x = ev->x - layout_x_offset;
1584 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1585 /* not in the main layout */
1589 Field f = index_to_field (index);
1590 framepos_t frames = 0;
1592 switch (ev->direction) {
1595 frames = get_frame_step (f);
1597 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1600 set (current_time() + frames, true);
1601 ValueChanged (); /* EMIT_SIGNAL */
1605 case GDK_SCROLL_DOWN:
1606 frames = get_frame_step (f);
1608 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1612 if ((double)current_time() - (double)frames < 0.0) {
1615 set (current_time() - frames, true);
1618 ValueChanged (); /* EMIT_SIGNAL */
1631 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1633 if (editing || _session == 0 || !dragging) {
1637 float pixel_frame_scale_factor = 0.2f;
1639 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1640 pixel_frame_scale_factor = 0.1f;
1644 if (Keyboard::modifier_state_contains (ev->state,
1645 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1647 pixel_frame_scale_factor = 0.025f;
1650 double y_delta = ev->y - drag_y;
1652 drag_accum += y_delta*pixel_frame_scale_factor;
1656 if (trunc (drag_accum) != 0) {
1661 dir = (drag_accum < 0 ? 1:-1);
1662 pos = current_time();
1663 frames = get_frame_step (drag_field,pos,dir);
1665 if (frames != 0 && frames * drag_accum < current_time()) {
1666 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1672 ValueChanged(); /* EMIT_SIGNAL */
1679 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1682 Timecode::BBT_Time BBT;
1684 case Timecode_Hours:
1685 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1687 case Timecode_Minutes:
1688 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1690 case Timecode_Seconds:
1691 f = _session->frame_rate();
1693 case Timecode_Frames:
1694 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1702 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1705 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1708 f = (framecnt_t) _session->frame_rate();
1710 case MS_Milliseconds:
1711 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1718 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1724 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1730 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1733 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1742 AudioClock::current_time (framepos_t) const
1748 AudioClock::current_duration (framepos_t pos) const
1757 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1773 AudioClock::bbt_validate_edit (const string& str)
1777 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1781 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1785 if (!is_duration && any.bbt.bars == 0) {
1789 if (!is_duration && any.bbt.beats == 0) {
1797 AudioClock::timecode_validate_edit (const string&)
1801 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1802 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1806 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1810 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1814 if (_session->timecode_drop_frames()) {
1815 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1824 AudioClock::minsec_validate_edit (const string& str)
1826 int hrs, mins, secs, millisecs;
1828 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1832 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1840 AudioClock::frames_from_timecode_string (const string& str) const
1842 if (_session == 0) {
1849 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1850 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1854 TC.rate = _session->timecode_frames_per_second();
1855 TC.drop= _session->timecode_drop_frames();
1857 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1859 // timecode_tester ();
1865 AudioClock::frames_from_minsec_string (const string& str) const
1867 if (_session == 0) {
1871 int hrs, mins, secs, millisecs;
1872 framecnt_t sr = _session->frame_rate();
1874 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1875 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1879 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1883 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1885 if (_session == 0) {
1886 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1891 any.type = AnyTime::BBT;
1893 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1900 return _session->any_duration_to_frames (pos, any);
1902 return _session->convert_to_frames (any);
1908 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1910 if (_session == 0) {
1911 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1915 Timecode::BBT_Time bbt;
1917 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1921 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1925 AudioClock::frames_from_audioframes_string (const string& str) const
1928 sscanf (str.c_str(), "%" PRId64, &f);
1933 AudioClock::build_ops_menu ()
1935 using namespace Menu_Helpers;
1936 ops_menu = new Menu;
1937 MenuList& ops_items = ops_menu->items();
1938 ops_menu->set_name ("ArdourContextMenu");
1940 if (!Profile->get_sae()) {
1941 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1943 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1944 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1945 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1947 if (editable && !_off && !is_duration && !_follows_playhead) {
1948 ops_items.push_back (SeparatorElem());
1949 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1950 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1955 AudioClock::set_from_playhead ()
1961 set (_session->transport_frame());
1966 AudioClock::locate ()
1968 if (!_session || is_duration) {
1972 _session->request_locate (current_time(), _session->transport_rolling ());
1976 AudioClock::set_mode (Mode m)
1986 _layout->set_text ("");
1989 _left_layout->set_text ("");
1990 _right_layout->set_text ("");
1995 mode_based_info_ratio = 0.5;
1996 insert_map.push_back (11);
1997 insert_map.push_back (10);
1998 insert_map.push_back (8);
1999 insert_map.push_back (7);
2000 insert_map.push_back (5);
2001 insert_map.push_back (4);
2002 insert_map.push_back (2);
2003 insert_map.push_back (1);
2007 mode_based_info_ratio = 0.5;
2008 insert_map.push_back (11);
2009 insert_map.push_back (10);
2010 insert_map.push_back (9);
2011 insert_map.push_back (8);
2012 insert_map.push_back (6);
2013 insert_map.push_back (5);
2014 insert_map.push_back (3);
2015 insert_map.push_back (2);
2016 insert_map.push_back (1);
2020 mode_based_info_ratio = 1.0;
2021 insert_map.push_back (12);
2022 insert_map.push_back (11);
2023 insert_map.push_back (10);
2024 insert_map.push_back (8);
2025 insert_map.push_back (7);
2026 insert_map.push_back (5);
2027 insert_map.push_back (4);
2028 insert_map.push_back (2);
2029 insert_map.push_back (1);
2033 mode_based_info_ratio = 0.5;
2037 set (last_when, true);
2039 if (!is_transient) {
2040 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2043 if (!_fixed_width) {
2044 /* display is different, allow us to resize */
2050 mode_changed (); /* EMIT SIGNAL (the member one) */
2054 AudioClock::set_bbt_reference (framepos_t pos)
2056 bbt_reference_time = pos;
2060 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2062 CairoWidget::on_style_changed (old_style);
2068 AudioClock::set_editable (bool yn)
2074 AudioClock::set_is_duration (bool yn)
2076 if (yn == is_duration) {
2081 set (last_when, true);
2085 AudioClock::set_off (bool yn)
2093 /* force a redraw. last_when will be preserved, but the clock text will
2097 set (last_when, true);
2101 AudioClock::focus ()
2103 start_edit (Field (0));
2107 AudioClock::set_corner_radius (double r)
2114 AudioClock::set_fixed_width (bool yn)
2120 AudioClock::dpi_reset ()
2122 /* force recomputation of size even if we are fixed width