2 Copyright (C) 1999 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <cstdio> // for sprintf
23 #include "pbd/convert.h"
24 #include "pbd/enumwriter.h"
26 #include <gtkmm/style.h>
27 #include <sigc++/bind.h>
29 #include "gtkmm2ext/cairocell.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
33 #include "ardour/profile.h"
34 #include "ardour/session.h"
35 #include "ardour/slave.h"
36 #include "ardour/tempo.h"
37 #include "ardour/types.h"
39 #include "ardour_ui.h"
40 #include "audio_clock.h"
41 #include "global_signals.h"
44 #include "gui_thread.h"
47 using namespace ARDOUR;
52 using Gtkmm2ext::Keyboard;
54 sigc::signal<void> AudioClock::ModeChanged;
55 vector<AudioClock*> AudioClock::clocks;
56 const double AudioClock::info_font_scale_factor = 0.5;
57 const double AudioClock::separator_height = 0.0;
58 const double AudioClock::x_leading_padding = 6.0;
60 #define BBT_BAR_CHAR "|"
61 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
62 #define INFO_FONT_SIZE ((int)round(font_size * info_font_scale_factor))
64 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
65 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
68 , is_transient (transient)
69 , is_duration (duration)
70 , editable (allow_edit)
71 , _follows_playhead (follows_playhead)
76 , _edit_by_click_field (false)
85 , mode_based_info_ratio (1.0)
89 , bbt_reference_time (-1)
94 , drag_field (Field (0))
97 set_flags (CAN_FOCUS);
99 _layout = Pango::Layout::create (get_pango_context());
100 _layout->set_attributes (normal_attributes);
103 _left_layout = Pango::Layout::create (get_pango_context());
104 _right_layout = Pango::Layout::create (get_pango_context());
107 set_widget_name (widget_name);
109 _mode = BBT; /* lie to force mode switch */
111 set (last_when, true);
114 clocks.push_back (this);
117 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
118 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
121 AudioClock::~AudioClock ()
123 delete foreground_attr;
128 AudioClock::set_widget_name (const string& str)
133 set_name (str + " clock");
143 AudioClock::on_realize ()
145 CairoWidget::on_realize ();
151 AudioClock::set_font ()
153 Glib::RefPtr<Gtk::Style> style = get_style ();
154 Pango::FontDescription font;
155 Pango::AttrFontDesc* font_attr;
157 if (!is_realized()) {
158 font = get_font_for_style (get_name());
160 font = style->get_font();
163 font_size = font.get_size();
165 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
167 normal_attributes.change (*font_attr);
168 editing_attributes.change (*font_attr);
170 /* now a smaller version of the same font */
173 font.set_size ((int) lrint (font_size * info_font_scale_factor));
174 font.set_weight (Pango::WEIGHT_NORMAL);
175 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
177 info_attributes.change (*font_attr);
179 /* and an even smaller one */
183 /* get the figure width for the font. This doesn't have to super
184 * accurate since we only use it to measure the (roughly 1 character)
185 * offset from the position Pango tells us for the "cursor"
188 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
192 tmp->get_pixel_size (em_width, ignore_height);
196 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
198 CairoWidget::set_active_state (s);
203 AudioClock::set_colors ()
209 uint32_t editing_color;
210 uint32_t cursor_color;
212 if (active_state()) {
213 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
214 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
215 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
216 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
218 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
219 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
220 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
221 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
224 /* store for bg and cursor in render() */
226 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
233 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
240 /* rescale for Pango colors ... sigh */
242 r = lrint (r * 65535.0);
243 g = lrint (g * 65535.0);
244 b = lrint (b * 65535.0);
246 UINT_TO_RGBA (text_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 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
252 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
253 r = lrint ((r/255.0) * 65535.0);
254 g = lrint ((g/255.0) * 65535.0);
255 b = lrint ((b/255.0) * 65535.0);
256 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
258 normal_attributes.change (*foreground_attr);
259 info_attributes.change (*foreground_attr);
260 editing_attributes.change (*foreground_attr);
261 editing_attributes.change (*editing_attr);
264 _layout->set_attributes (normal_attributes);
266 _layout->set_attributes (editing_attributes);
270 _left_layout->set_attributes (info_attributes);
271 _right_layout->set_attributes (info_attributes);
278 AudioClock::render (cairo_t* cr)
280 /* main layout: rounded rect, plus the text */
283 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
286 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
288 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
291 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
297 cairo_move_to (cr, layout_x_offset, 0);
299 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
300 cairo_move_to (cr, layout_x_offset + xcenter, (upper_height - layout_height) / 2.0);
303 pango_cairo_show_layout (cr, _layout->gobj());
307 double h = get_height() - upper_height - separator_height;
310 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
313 if (mode_based_info_ratio != 1.0) {
315 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
319 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
321 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
326 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
327 pango_cairo_show_layout (cr, _left_layout->gobj());
331 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
332 get_width() - separator_height - left_rect_width, h,
335 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
336 get_width() - separator_height - left_rect_width, h);
342 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
343 /* right-align does not work per se beacuse layout width is unset.
344 * Using _right_layout->set_width([value >=0]) would also enable
345 * word-wrapping which is not wanted here.
346 * The solution is to custom align the layout depending on its size.
347 * if it is larger than the available space it will be cropped on the
348 * right edge rather than override text on the left side.
351 _right_layout->get_pixel_size(rw, rh);
352 x = get_width() - rw- separator_height - x_leading_padding;
353 if (x < x_leading_padding + left_rect_width + separator_height) {
354 x = x_leading_padding + left_rect_width + separator_height;
356 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
358 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
360 pango_cairo_show_layout (cr, _right_layout->gobj());
363 /* no info to display, or just one */
367 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
369 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
377 if (!insert_map.empty()) {
379 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
381 if (input_string.length() < insert_map.size()) {
382 Pango::Rectangle cursor;
384 if (input_string.empty()) {
385 /* nothing entered yet, put cursor at the end
388 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
390 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
393 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
396 min (get_width() - 2.0,
397 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + xcenter + em_width), 0,
398 2.0, cursor.get_height()/PANGO_SCALE);
401 min (get_width() - 2.0,
402 (double) layout_x_offset + xcenter + cursor.get_x()/PANGO_SCALE + em_width),
403 (upper_height - layout_height)/2.0,
404 2.0, cursor.get_height()/PANGO_SCALE);
408 /* we've entered all possible digits, no cursor */
412 if (input_string.empty()) {
413 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
422 (upper_height - layout_height)/2.0,
432 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
434 CairoWidget::on_size_allocate (alloc);
437 upper_height = (get_height()/2.0) - 1.0;
439 upper_height = get_height();
443 /* center display in available space
444 * NB. this only works if the containing widget is not the
445 * layout itself (eg. the session->property dialog)
447 layout_x_offset = (get_width() - layout_width)/2.0;
455 AudioClock::on_size_request (Gtk::Requisition* req)
457 /* even for non fixed width clocks, the size we *ask* for never changes,
458 even though the size we receive might. so once we've computed it,
463 req->width = first_width;
464 req->height = first_height;
468 Glib::RefPtr<Pango::Layout> tmp;
469 Glib::RefPtr<Gtk::Style> style = get_style ();
470 Pango::FontDescription font;
472 tmp = Pango::Layout::create (get_pango_context());
474 if (!is_realized()) {
475 font = get_font_for_style (get_name());
477 font = style->get_font();
480 tmp->set_font_description (font);
484 tmp->set_text ("-88:88:88:88");
485 tmp->get_pixel_size (_mode_width[Timecode], ignored);
486 tmp->set_text (" 8888|88|8888");
487 tmp->get_pixel_size (_mode_width[BBT], ignored);
488 tmp->set_text (" 88:88:88,888");
489 tmp->get_pixel_size (_mode_width[MinSec], ignored);
490 tmp->set_text (" 8888888888");
491 tmp->get_pixel_size (_mode_width[Frames], ignored);
493 /* this string is the longest thing we will ever display,
494 it does not include the BBT bar char that may descend
496 note; depending on BPM setting this may actually
497 not be sufficient for 24h worth of BBT
500 tmp->set_text (" 8888888888::,");
504 tmp->set_text ("-88:88:88:88");
507 tmp->set_text (" 8888|88|8888");
510 tmp->set_text (" 88:88:88,888");
513 tmp->set_text (" 8888888888");
518 tmp->get_pixel_size (req->width, req->height);
520 layout_height = req->height;
521 layout_width = req->width;
523 /* now tackle height, for which we need to know the height of the lower
531 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
532 font.set_weight (Pango::WEIGHT_NORMAL);
533 tmp->set_font_description (font);
535 /* we only care about height, so put as much stuff in here
536 as possible that might change the height.
538 tmp->set_text ("qyhH|"); /* one ascender, one descender */
540 tmp->get_pixel_size (w, info_height);
542 /* silly extra padding that seems necessary to correct the info
543 * that pango just gave us. I have no idea why.
546 req->height += info_height;
547 req->height += separator_height;
550 first_height = req->height;
551 first_width = req->width;
555 AudioClock::show_edit_status (int length)
557 editing_attr->set_start_index (edit_string.length() - length);
558 editing_attr->set_end_index (edit_string.length());
560 editing_attributes.change (*foreground_attr);
561 editing_attributes.change (*editing_attr);
563 _layout->set_attributes (editing_attributes);
567 AudioClock::start_edit (Field f)
569 pre_edit_string = _layout->get_text ();
570 if (!insert_map.empty()) {
571 edit_string = pre_edit_string;
573 edit_string.clear ();
574 _layout->set_text ("");
576 input_string.clear ();
580 input_string = get_field (f);
581 show_edit_status (merge_input_and_edit_string ());
582 _layout->set_text (edit_string);
587 Keyboard::magic_widget_grab_focus ();
592 AudioClock::get_field (Field f)
596 return edit_string.substr (1, 2);
598 case Timecode_Minutes:
599 return edit_string.substr (4, 2);
601 case Timecode_Seconds:
602 return edit_string.substr (7, 2);
604 case Timecode_Frames:
605 return edit_string.substr (10, 2);
608 return edit_string.substr (1, 2);
611 return edit_string.substr (4, 2);
614 return edit_string.substr (7, 2);
616 case MS_Milliseconds:
617 return edit_string.substr (10, 3);
620 return edit_string.substr (1, 3);
623 return edit_string.substr (5, 2);
626 return edit_string.substr (8, 4);
636 AudioClock::end_edit (bool modify)
644 ok = timecode_validate_edit (edit_string);
648 ok = bbt_validate_edit (edit_string);
652 ok = minsec_validate_edit (edit_string);
660 edit_string = pre_edit_string;
661 input_string.clear ();
662 _layout->set_text (edit_string);
663 show_edit_status (0);
664 /* edit attributes remain in use */
668 framepos_t pos = 0; /* stupid gcc */
672 pos = frames_from_timecode_string (edit_string);
677 pos = frame_duration_from_bbt_string (0, edit_string);
679 pos = frames_from_bbt_string (0, edit_string);
684 pos = frames_from_minsec_string (edit_string);
688 pos = frames_from_audioframes_string (edit_string);
693 _layout->set_attributes (normal_attributes);
694 ValueChanged(); /* EMIT_SIGNAL */
700 _layout->set_attributes (normal_attributes);
701 _layout->set_text (pre_edit_string);
712 AudioClock::drop_focus ()
714 Keyboard::magic_widget_drop_focus ();
718 /* move focus back to the default widget in the top level window */
720 Widget* top = get_toplevel();
722 if (top->is_toplevel ()) {
723 Window* win = dynamic_cast<Window*> (top);
730 AudioClock::parse_as_frames_distance (const std::string& str)
734 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
742 AudioClock::parse_as_minsec_distance (const std::string& str)
744 framecnt_t sr = _session->frame_rate();
750 switch (str.length()) {
757 sscanf (str.c_str(), "%" PRId32, &msecs);
758 return msecs * (sr / 1000);
761 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
762 return (secs * sr) + (msecs * (sr/1000));
765 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
766 return (secs * sr) + (msecs * (sr/1000));
769 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
770 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
773 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
774 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
777 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
778 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
781 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
782 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
792 AudioClock::parse_as_timecode_distance (const std::string& str)
794 double fps = _session->timecode_frames_per_second();
795 framecnt_t sr = _session->frame_rate();
801 switch (str.length()) {
806 sscanf (str.c_str(), "%" PRId32, &frames);
807 return lrint ((frames/(float)fps) * sr);
810 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
811 return (secs * sr) + lrint ((frames/(float)fps) * sr);
814 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
815 return (secs * sr) + lrint ((frames/(float)fps) * sr);
818 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
819 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
822 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
823 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
826 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
827 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
830 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
831 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
841 AudioClock::parse_as_bbt_distance (const std::string&)
847 AudioClock::parse_as_distance (const std::string& instr)
851 return parse_as_timecode_distance (instr);
854 return parse_as_frames_distance (instr);
857 return parse_as_bbt_distance (instr);
860 return parse_as_minsec_distance (instr);
867 AudioClock::end_edit_relative (bool add)
873 ok = timecode_validate_edit (edit_string);
877 ok = bbt_validate_edit (edit_string);
881 ok = minsec_validate_edit (edit_string);
889 edit_string = pre_edit_string;
890 input_string.clear ();
891 _layout->set_text (edit_string);
892 show_edit_status (0);
893 /* edit attributes remain in use */
898 framecnt_t frames = parse_as_distance (input_string);
903 _layout->set_attributes (normal_attributes);
907 set (current_time() + frames, true);
909 framepos_t c = current_time();
912 set (c - frames, true);
917 ValueChanged (); /* EMIT SIGNAL */
920 input_string.clear ();
926 AudioClock::session_configuration_changed (std::string p)
928 if (p == "sync-source" || p == "external-sync") {
929 set (current_time(), true);
933 if (p != "timecode-offset" && p != "timecode-offset-negative") {
942 current = current_duration ();
944 current = current_time ();
954 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
956 if ((!force && !is_visible()) || _session == 0) {
961 when = when - offset;
964 if (when == last_when && !force) {
965 if (_mode != Timecode) {
966 /* timecode may need to force display of TC source
967 * time, so don't return early.
975 _right_layout->set_alignment(Pango::ALIGN_LEFT);
981 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
983 set_timecode (when, force);
987 set_bbt (when, force);
991 set_minsec (when, force);
995 set_frames (when, force);
1005 AudioClock::set_frames (framepos_t when, bool /*force*/)
1008 bool negative = false;
1011 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1014 _left_layout->set_text ("");
1015 _right_layout->set_text ("");
1027 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1029 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1032 _layout->set_text (buf);
1035 framecnt_t rate = _session->frame_rate();
1037 if (fmod (rate, 100.0) == 0.0) {
1038 sprintf (buf, "%.1fkHz", rate/1000.0);
1040 sprintf (buf, "%" PRId64 "Hz", rate);
1043 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1044 INFO_FONT_SIZE, _("SR"), buf));
1046 float vid_pullup = _session->config.get_video_pullup();
1048 if (vid_pullup == 0.0) {
1049 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1050 INFO_FONT_SIZE, _("pullup: \u2012")));
1052 sprintf (buf, _("%+-6.4f%%"), vid_pullup);
1053 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1054 INFO_FONT_SIZE, buf));
1060 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1068 bool negative = false;
1071 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1074 _left_layout->set_text ("");
1075 _right_layout->set_text ("");
1087 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1088 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1089 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1090 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1091 secs = (int) floor (left / (float) _session->frame_rate());
1092 left -= (framecnt_t) floor (secs * _session->frame_rate());
1093 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1096 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1098 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1101 _layout->set_text (buf);
1105 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1108 bool negative = false;
1111 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1113 _left_layout->set_text ("");
1114 _right_layout->set_text ("");
1126 _session->timecode_duration (when, TC);
1128 _session->timecode_time (when, TC);
1131 TC.negative = TC.negative || negative;
1133 _layout->set_text (Timecode::timecode_format_time(TC));
1135 if (_left_layout && _right_layout) {
1137 SyncSource sync_src = Config->get_sync_source();
1139 if (_session->config.get_external_sync()) {
1140 Slave* slave = _session->slave();
1144 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1145 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1146 _right_layout->set_text ("");
1152 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1153 INFO_FONT_SIZE, dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1154 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1155 INFO_FONT_SIZE, slave->approximate_current_delta()));
1157 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1158 INFO_FONT_SIZE, _("--pending--")));
1159 _right_layout->set_text ("");
1164 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">INT/%2</span>",
1165 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1166 _right_layout->set_text ("");
1172 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1175 Timecode::BBT_Time BBT;
1176 bool negative = false;
1179 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1181 _left_layout->set_text ("");
1182 _right_layout->set_text ("");
1192 /* handle a common case */
1199 _session->tempo_map().bbt_time (when, BBT);
1204 _session->tempo_map().bbt_time (when, BBT);
1208 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1209 BBT.bars, BBT.beats, BBT.ticks);
1211 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1212 BBT.bars, BBT.beats, BBT.ticks);
1215 _layout->set_text (buf);
1217 if (_right_layout) {
1220 if (bbt_reference_time < 0) {
1223 pos = bbt_reference_time;
1226 TempoMetric m (_session->tempo_map().metric_at (pos));
1228 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1229 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1230 INFO_FONT_SIZE, buf));
1232 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1233 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1234 INFO_FONT_SIZE, buf));
1239 AudioClock::set_session (Session *s)
1241 SessionHandlePtr::set_session (s);
1245 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1247 const XMLProperty* prop;
1248 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1249 AudioClock::Mode amode;
1252 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1253 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1255 if ((prop = (*i)->property (X_("mode"))) != 0) {
1256 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1259 if ((prop = (*i)->property (X_("on"))) != 0) {
1260 set_off (!string_is_affirmative (prop->value()));
1267 set (last_when, true);
1272 AudioClock::on_key_press_event (GdkEventKey* ev)
1280 int highlight_length;
1283 switch (ev->keyval) {
1326 case GDK_KP_Subtract:
1327 end_edit_relative (false);
1333 end_edit_relative (true);
1346 ChangeAborted(); /* EMIT SIGNAL */
1351 if (!input_string.empty()) {
1352 /* delete the last key entered
1354 input_string = input_string.substr (0, input_string.length() - 1);
1356 goto use_input_string;
1362 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1363 /* too many digits: eat the key event, but do nothing with it */
1367 input_string.push_back (new_char);
1373 /* get this one in the right order, and to the right width */
1374 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1375 edit_string = edit_string.substr (0, edit_string.length() - 1);
1377 edit_string.push_back (new_char);
1379 if (!edit_string.empty()) {
1381 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1382 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1385 /* highlight the whole thing */
1386 highlight_length = edit_string.length();
1390 highlight_length = merge_input_and_edit_string ();
1393 show_edit_status (highlight_length);
1394 _layout->set_text (edit_string);
1401 AudioClock::merge_input_and_edit_string ()
1403 /* merge with pre-edit-string into edit string */
1405 edit_string = pre_edit_string;
1407 if (input_string.empty()) {
1411 string::size_type target;
1412 for (string::size_type i = 0; i < input_string.length(); ++i) {
1413 target = insert_map[input_string.length() - 1 - i];
1414 edit_string[target] = input_string[i];
1416 /* highlight from end to wherever the last character was added */
1417 return edit_string.length() - insert_map[input_string.length()-1];
1422 AudioClock::on_key_release_event (GdkEventKey *ev)
1428 /* return true for keys that we used on press
1429 so that they cannot possibly do double-duty
1431 switch (ev->keyval) {
1454 case GDK_KP_Decimal:
1462 case GDK_KP_Subtract:
1470 AudioClock::index_to_field (int index) const
1475 return Timecode_Hours;
1476 } else if (index < 7) {
1477 return Timecode_Minutes;
1478 } else if (index < 10) {
1479 return Timecode_Seconds;
1481 return Timecode_Frames;
1487 } else if (index < 7) {
1495 return Timecode_Hours;
1496 } else if (index < 6) {
1498 } else if (index < 9) {
1501 return MS_Milliseconds;
1513 AudioClock::on_button_press_event (GdkEventButton *ev)
1515 switch (ev->button) {
1517 if (editable && !_off) {
1523 /* the text has been centered vertically, so adjust
1527 y = ev->y - ((upper_height - layout_height)/2);
1528 x = ev->x - layout_x_offset;
1530 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1531 /* pretend it is a character on the far right */
1534 drag_field = index_to_field (index);
1536 /* make absolutely sure that the pointer is grabbed */
1537 gdk_pointer_grab(ev->window,false ,
1538 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1539 NULL,NULL,ev->time);
1541 drag_start_y = ev->y;
1555 AudioClock::on_button_release_event (GdkEventButton *ev)
1557 if (editable && !_off) {
1559 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1561 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1562 // we actually dragged so return without
1563 // setting editing focus, or we shift clicked
1566 if (ev->button == 1) {
1568 if (_edit_by_click_field) {
1572 int y = ev->y - ((upper_height - layout_height)/2);
1573 int x = ev->x - layout_x_offset;
1576 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1580 f = index_to_field (index);
1583 case Timecode_Frames:
1584 case MS_Milliseconds:
1600 if (Keyboard::is_context_menu_event (ev)) {
1601 if (ops_menu == 0) {
1604 ops_menu->popup (1, ev->time);
1612 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1614 bool ret = CairoWidget::on_focus_out_event (ev);
1624 AudioClock::on_scroll_event (GdkEventScroll *ev)
1629 if (editing || _session == 0 || !editable || _off) {
1636 /* the text has been centered vertically, so adjust
1640 y = ev->y - ((upper_height - layout_height)/2);
1641 x = ev->x - layout_x_offset;
1643 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1644 /* not in the main layout */
1648 Field f = index_to_field (index);
1649 framepos_t frames = 0;
1651 switch (ev->direction) {
1654 frames = get_frame_step (f);
1656 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1659 set (current_time() + frames, true);
1660 ValueChanged (); /* EMIT_SIGNAL */
1664 case GDK_SCROLL_DOWN:
1665 frames = get_frame_step (f);
1667 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1671 if ((double)current_time() - (double)frames < 0.0) {
1674 set (current_time() - frames, true);
1677 ValueChanged (); /* EMIT_SIGNAL */
1690 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1692 if (editing || _session == 0 || !dragging) {
1696 float pixel_frame_scale_factor = 0.2f;
1698 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1699 pixel_frame_scale_factor = 0.1f;
1703 if (Keyboard::modifier_state_contains (ev->state,
1704 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1706 pixel_frame_scale_factor = 0.025f;
1709 double y_delta = ev->y - drag_y;
1711 drag_accum += y_delta*pixel_frame_scale_factor;
1715 if (trunc (drag_accum) != 0) {
1720 dir = (drag_accum < 0 ? 1:-1);
1721 pos = current_time();
1722 frames = get_frame_step (drag_field, pos, dir);
1724 if (frames != 0 && frames * drag_accum < current_time()) {
1725 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1731 ValueChanged(); /* EMIT_SIGNAL */
1738 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1741 Timecode::BBT_Time BBT;
1743 case Timecode_Hours:
1744 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1746 case Timecode_Minutes:
1747 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1749 case Timecode_Seconds:
1750 f = _session->frame_rate();
1752 case Timecode_Frames:
1753 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1761 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1764 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1767 f = (framecnt_t) _session->frame_rate();
1769 case MS_Milliseconds:
1770 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1777 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1783 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1789 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1792 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1801 AudioClock::current_time (framepos_t) const
1807 AudioClock::current_duration (framepos_t pos) const
1816 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1832 AudioClock::bbt_validate_edit (const string& str)
1836 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1840 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1844 if (!is_duration && any.bbt.bars == 0) {
1848 if (!is_duration && any.bbt.beats == 0) {
1856 AudioClock::timecode_validate_edit (const string&)
1860 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1861 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1865 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1869 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1873 if (_session->timecode_drop_frames()) {
1874 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1883 AudioClock::minsec_validate_edit (const string& str)
1885 int hrs, mins, secs, millisecs;
1887 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1891 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1899 AudioClock::frames_from_timecode_string (const string& str) const
1901 if (_session == 0) {
1908 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1909 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1913 TC.rate = _session->timecode_frames_per_second();
1914 TC.drop= _session->timecode_drop_frames();
1916 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1918 // timecode_tester ();
1924 AudioClock::frames_from_minsec_string (const string& str) const
1926 if (_session == 0) {
1930 int hrs, mins, secs, millisecs;
1931 framecnt_t sr = _session->frame_rate();
1933 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1934 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1938 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1942 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1944 if (_session == 0) {
1945 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1950 any.type = AnyTime::BBT;
1952 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1959 return _session->any_duration_to_frames (pos, any);
1961 return _session->convert_to_frames (any);
1967 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1969 if (_session == 0) {
1970 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1974 Timecode::BBT_Time bbt;
1976 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1980 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1984 AudioClock::frames_from_audioframes_string (const string& str) const
1987 sscanf (str.c_str(), "%" PRId64, &f);
1992 AudioClock::build_ops_menu ()
1994 using namespace Menu_Helpers;
1995 ops_menu = new Menu;
1996 MenuList& ops_items = ops_menu->items();
1997 ops_menu->set_name ("ArdourContextMenu");
1999 if (!Profile->get_sae()) {
2000 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2002 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2003 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2004 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2006 if (editable && !_off && !is_duration && !_follows_playhead) {
2007 ops_items.push_back (SeparatorElem());
2008 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2009 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2014 AudioClock::set_from_playhead ()
2020 set (_session->transport_frame());
2025 AudioClock::locate ()
2027 if (!_session || is_duration) {
2031 _session->request_locate (current_time(), _session->transport_rolling ());
2035 AudioClock::set_mode (Mode m)
2045 _layout->set_text ("");
2049 _left_layout->set_attributes (info_attributes);
2050 _right_layout->set_attributes (info_attributes);
2051 /* adjust info_height according to font size */
2053 _left_layout->set_text (" 1234567890");
2054 _left_layout->get_pixel_size (ignored, info_height);
2057 _left_layout->set_text ("");
2058 _right_layout->set_text ("");
2063 mode_based_info_ratio = 0.5;
2064 insert_map.push_back (11);
2065 insert_map.push_back (10);
2066 insert_map.push_back (8);
2067 insert_map.push_back (7);
2068 insert_map.push_back (5);
2069 insert_map.push_back (4);
2070 insert_map.push_back (2);
2071 insert_map.push_back (1);
2075 mode_based_info_ratio = 0.5;
2076 insert_map.push_back (11);
2077 insert_map.push_back (10);
2078 insert_map.push_back (9);
2079 insert_map.push_back (8);
2080 insert_map.push_back (6);
2081 insert_map.push_back (5);
2082 insert_map.push_back (3);
2083 insert_map.push_back (2);
2084 insert_map.push_back (1);
2088 mode_based_info_ratio = 1.0;
2089 insert_map.push_back (12);
2090 insert_map.push_back (11);
2091 insert_map.push_back (10);
2092 insert_map.push_back (8);
2093 insert_map.push_back (7);
2094 insert_map.push_back (5);
2095 insert_map.push_back (4);
2096 insert_map.push_back (2);
2097 insert_map.push_back (1);
2101 mode_based_info_ratio = 0.5;
2105 set (last_when, true);
2107 if (!is_transient) {
2108 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2111 if (!_fixed_width) {
2112 /* display is different, allow us to resize */
2118 mode_changed (); /* EMIT SIGNAL (the member one) */
2122 AudioClock::set_bbt_reference (framepos_t pos)
2124 bbt_reference_time = pos;
2128 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2130 CairoWidget::on_style_changed (old_style);
2138 AudioClock::set_editable (bool yn)
2144 AudioClock::set_is_duration (bool yn)
2146 if (yn == is_duration) {
2151 set (last_when, true);
2155 AudioClock::set_off (bool yn)
2163 /* force a redraw. last_when will be preserved, but the clock text will
2167 set (last_when, true);
2171 AudioClock::focus ()
2173 start_edit (Field (0));
2177 AudioClock::set_corner_radius (double r)
2184 AudioClock::set_fixed_width (bool yn)
2190 AudioClock::dpi_reset ()
2192 /* force recomputation of size even if we are fixed width