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 (Config->get_sync_source()) {
1088 _left_layout->set_text ("JACK");
1091 _left_layout->set_text ("MTC");
1094 _left_layout->set_text ("M-Clock");
1097 _left_layout->set_text ("LTC");
1101 _left_layout->set_text ("INT");
1104 _right_layout->set_text ("TBDiscussed");
1109 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1112 Timecode::BBT_Time BBT;
1113 bool negative = false;
1116 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1118 _left_layout->set_text ("");
1119 _right_layout->set_text ("");
1129 /* handle a common case */
1136 _session->tempo_map().bbt_time (when, BBT);
1141 _session->tempo_map().bbt_time (when, BBT);
1145 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1146 BBT.bars, BBT.beats, BBT.ticks);
1148 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1149 BBT.bars, BBT.beats, BBT.ticks);
1152 _layout->set_text (buf);
1154 if (_right_layout) {
1157 if (bbt_reference_time < 0) {
1160 pos = bbt_reference_time;
1163 TempoMetric m (_session->tempo_map().metric_at (pos));
1165 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1166 _left_layout->set_text (buf);
1168 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1169 _right_layout->set_text (buf);
1174 AudioClock::set_session (Session *s)
1176 SessionHandlePtr::set_session (s);
1180 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1182 const XMLProperty* prop;
1183 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1184 AudioClock::Mode amode;
1187 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1188 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1190 if ((prop = (*i)->property (X_("mode"))) != 0) {
1191 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1194 if ((prop = (*i)->property (X_("on"))) != 0) {
1195 set_off (!string_is_affirmative (prop->value()));
1202 set (last_when, true);
1207 AudioClock::on_key_press_event (GdkEventKey* ev)
1215 int highlight_length;
1218 switch (ev->keyval) {
1261 case GDK_KP_Subtract:
1262 end_edit_relative (false);
1268 end_edit_relative (true);
1281 ChangeAborted(); /* EMIT SIGNAL */
1286 if (!input_string.empty()) {
1287 /* delete the last key entered
1289 input_string = input_string.substr (0, input_string.length() - 1);
1291 goto use_input_string;
1297 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1298 /* too many digits: eat the key event, but do nothing with it */
1302 input_string.push_back (new_char);
1308 /* get this one in the right order, and to the right width */
1309 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1310 edit_string = edit_string.substr (0, edit_string.length() - 1);
1312 edit_string.push_back (new_char);
1314 if (!edit_string.empty()) {
1316 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1317 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1320 /* highlight the whole thing */
1321 highlight_length = edit_string.length();
1325 highlight_length = merge_input_and_edit_string ();
1328 show_edit_status (highlight_length);
1329 _layout->set_text (edit_string);
1336 AudioClock::merge_input_and_edit_string ()
1338 /* merge with pre-edit-string into edit string */
1340 edit_string = pre_edit_string;
1342 if (input_string.empty()) {
1346 string::size_type target;
1347 for (string::size_type i = 0; i < input_string.length(); ++i) {
1348 target = insert_map[input_string.length() - 1 - i];
1349 edit_string[target] = input_string[i];
1351 /* highlight from end to wherever the last character was added */
1352 return edit_string.length() - insert_map[input_string.length()-1];
1357 AudioClock::on_key_release_event (GdkEventKey *ev)
1363 /* return true for keys that we used on press
1364 so that they cannot possibly do double-duty
1366 switch (ev->keyval) {
1389 case GDK_KP_Decimal:
1397 case GDK_KP_Subtract:
1405 AudioClock::index_to_field (int index) const
1410 return Timecode_Hours;
1411 } else if (index < 7) {
1412 return Timecode_Minutes;
1413 } else if (index < 10) {
1414 return Timecode_Seconds;
1416 return Timecode_Frames;
1422 } else if (index < 7) {
1430 return Timecode_Hours;
1431 } else if (index < 6) {
1433 } else if (index < 9) {
1436 return MS_Milliseconds;
1448 AudioClock::on_button_press_event (GdkEventButton *ev)
1450 switch (ev->button) {
1452 if (editable && !_off) {
1458 /* the text has been centered vertically, so adjust
1462 y = ev->y - ((upper_height - layout_height)/2);
1463 x = ev->x - layout_x_offset;
1465 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1466 drag_field = index_to_field (index);
1468 /* make absolutely sure that the pointer is grabbed */
1469 gdk_pointer_grab(ev->window,false ,
1470 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1471 NULL,NULL,ev->time);
1473 drag_start_y = ev->y;
1488 AudioClock::on_button_release_event (GdkEventButton *ev)
1490 if (editable && !_off) {
1492 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1494 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1495 // we actually dragged so return without
1496 // setting editing focus, or we shift clicked
1499 if (ev->button == 1) {
1501 if (_edit_by_click_field) {
1505 int y = ev->y - ((upper_height - layout_height)/2);
1506 int x = ev->x - layout_x_offset;
1509 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1513 f = index_to_field (index);
1516 case Timecode_Frames:
1517 case MS_Milliseconds:
1533 if (Keyboard::is_context_menu_event (ev)) {
1534 if (ops_menu == 0) {
1537 ops_menu->popup (1, ev->time);
1545 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1547 bool ret = CairoWidget::on_focus_out_event (ev);
1557 AudioClock::on_scroll_event (GdkEventScroll *ev)
1562 if (editing || _session == 0 || !editable || _off) {
1569 /* the text has been centered vertically, so adjust
1573 y = ev->y - ((upper_height - layout_height)/2);
1574 x = ev->x - layout_x_offset;
1576 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1577 /* not in the main layout */
1581 Field f = index_to_field (index);
1582 framepos_t frames = 0;
1584 switch (ev->direction) {
1587 frames = get_frame_step (f);
1589 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1592 set (current_time() + frames, true);
1593 ValueChanged (); /* EMIT_SIGNAL */
1597 case GDK_SCROLL_DOWN:
1598 frames = get_frame_step (f);
1600 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1604 if ((double)current_time() - (double)frames < 0.0) {
1607 set (current_time() - frames, true);
1610 ValueChanged (); /* EMIT_SIGNAL */
1623 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1625 if (editing || _session == 0 || !dragging) {
1629 float pixel_frame_scale_factor = 0.2f;
1631 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1632 pixel_frame_scale_factor = 0.1f;
1636 if (Keyboard::modifier_state_contains (ev->state,
1637 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1639 pixel_frame_scale_factor = 0.025f;
1642 double y_delta = ev->y - drag_y;
1644 drag_accum += y_delta*pixel_frame_scale_factor;
1648 if (trunc (drag_accum) != 0) {
1653 dir = (drag_accum < 0 ? 1:-1);
1654 pos = current_time();
1655 frames = get_frame_step (drag_field, pos, dir);
1657 if (frames != 0 && frames * drag_accum < current_time()) {
1658 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1664 ValueChanged(); /* EMIT_SIGNAL */
1671 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1674 Timecode::BBT_Time BBT;
1676 case Timecode_Hours:
1677 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1679 case Timecode_Minutes:
1680 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1682 case Timecode_Seconds:
1683 f = _session->frame_rate();
1685 case Timecode_Frames:
1686 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1694 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1697 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1700 f = (framecnt_t) _session->frame_rate();
1702 case MS_Milliseconds:
1703 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1710 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1716 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1722 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1725 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1734 AudioClock::current_time (framepos_t) const
1740 AudioClock::current_duration (framepos_t pos) const
1749 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1765 AudioClock::bbt_validate_edit (const string& str)
1769 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1773 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1777 if (!is_duration && any.bbt.bars == 0) {
1781 if (!is_duration && any.bbt.beats == 0) {
1789 AudioClock::timecode_validate_edit (const string&)
1793 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1794 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1798 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1802 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1806 if (_session->timecode_drop_frames()) {
1807 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1816 AudioClock::minsec_validate_edit (const string& str)
1818 int hrs, mins, secs, millisecs;
1820 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1824 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1832 AudioClock::frames_from_timecode_string (const string& str) const
1834 if (_session == 0) {
1841 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1842 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1846 TC.rate = _session->timecode_frames_per_second();
1847 TC.drop= _session->timecode_drop_frames();
1849 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1851 // timecode_tester ();
1857 AudioClock::frames_from_minsec_string (const string& str) const
1859 if (_session == 0) {
1863 int hrs, mins, secs, millisecs;
1864 framecnt_t sr = _session->frame_rate();
1866 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1867 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1871 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1875 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1877 if (_session == 0) {
1878 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1883 any.type = AnyTime::BBT;
1885 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1892 return _session->any_duration_to_frames (pos, any);
1894 return _session->convert_to_frames (any);
1900 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1902 if (_session == 0) {
1903 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1907 Timecode::BBT_Time bbt;
1909 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1913 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1917 AudioClock::frames_from_audioframes_string (const string& str) const
1920 sscanf (str.c_str(), "%" PRId64, &f);
1925 AudioClock::build_ops_menu ()
1927 using namespace Menu_Helpers;
1928 ops_menu = new Menu;
1929 MenuList& ops_items = ops_menu->items();
1930 ops_menu->set_name ("ArdourContextMenu");
1932 if (!Profile->get_sae()) {
1933 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1935 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1936 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1937 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1939 if (editable && !_off && !is_duration && !_follows_playhead) {
1940 ops_items.push_back (SeparatorElem());
1941 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1942 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1947 AudioClock::set_from_playhead ()
1953 set (_session->transport_frame());
1958 AudioClock::locate ()
1960 if (!_session || is_duration) {
1964 _session->request_locate (current_time(), _session->transport_rolling ());
1968 AudioClock::set_mode (Mode m)
1978 _layout->set_text ("");
1981 _left_layout->set_text ("");
1982 _right_layout->set_text ("");
1987 mode_based_info_ratio = 0.5;
1988 insert_map.push_back (11);
1989 insert_map.push_back (10);
1990 insert_map.push_back (8);
1991 insert_map.push_back (7);
1992 insert_map.push_back (5);
1993 insert_map.push_back (4);
1994 insert_map.push_back (2);
1995 insert_map.push_back (1);
1999 mode_based_info_ratio = 0.5;
2000 insert_map.push_back (11);
2001 insert_map.push_back (10);
2002 insert_map.push_back (9);
2003 insert_map.push_back (8);
2004 insert_map.push_back (6);
2005 insert_map.push_back (5);
2006 insert_map.push_back (3);
2007 insert_map.push_back (2);
2008 insert_map.push_back (1);
2012 mode_based_info_ratio = 1.0;
2013 insert_map.push_back (12);
2014 insert_map.push_back (11);
2015 insert_map.push_back (10);
2016 insert_map.push_back (8);
2017 insert_map.push_back (7);
2018 insert_map.push_back (5);
2019 insert_map.push_back (4);
2020 insert_map.push_back (2);
2021 insert_map.push_back (1);
2025 mode_based_info_ratio = 0.5;
2029 set (last_when, true);
2031 if (!is_transient) {
2032 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2035 if (!_fixed_width) {
2036 /* display is different, allow us to resize */
2042 mode_changed (); /* EMIT SIGNAL (the member one) */
2046 AudioClock::set_bbt_reference (framepos_t pos)
2048 bbt_reference_time = pos;
2052 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2054 CairoWidget::on_style_changed (old_style);
2060 AudioClock::set_editable (bool yn)
2066 AudioClock::set_is_duration (bool yn)
2068 if (yn == is_duration) {
2073 set (last_when, true);
2077 AudioClock::set_off (bool yn)
2085 /* force a redraw. last_when will be preserved, but the clock text will
2089 set (last_when, true);
2093 AudioClock::focus ()
2095 start_edit (Field (0));
2099 AudioClock::set_corner_radius (double r)
2106 AudioClock::set_fixed_width (bool yn)
2112 AudioClock::dpi_reset ()
2114 /* force recomputation of size even if we are fixed width