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);
273 AudioClock::render (cairo_t* cr)
275 /* main layout: rounded rect, plus the text */
278 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
281 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width() - corner_radius/2.0, upper_height, corner_radius);
283 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
286 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
292 cairo_move_to (cr, layout_x_offset, 0);
294 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
295 cairo_move_to (cr, layout_x_offset + xcenter, (upper_height - layout_height) / 2.0);
298 pango_cairo_show_layout (cr, _layout->gobj());
302 double h = get_height() - upper_height - separator_height;
305 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
308 if (mode_based_info_ratio != 1.0) {
310 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
314 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
315 left_rect_width + (separator_height == 0 ? corner_radius : 0),
316 h - corner_radius/2.0, corner_radius);
318 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
323 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
324 pango_cairo_show_layout (cr, _left_layout->gobj());
328 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
329 upper_height + separator_height,
330 get_width() - separator_height - left_rect_width - corner_radius/2.0,
331 h - corner_radius/2.0, corner_radius);
333 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
334 get_width() - separator_height - left_rect_width, h);
340 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
341 /* right-align does not work per se beacuse layout width is unset.
342 * Using _right_layout->set_width([value >=0]) would also enable
343 * word-wrapping which is not wanted here.
344 * The solution is to custom align the layout depending on its size.
345 * if it is larger than the available space it will be cropped on the
346 * right edge rather than override text on the left side.
349 _right_layout->get_pixel_size(rw, rh);
350 x = get_width() - rw- separator_height - x_leading_padding;
351 if (x < x_leading_padding + left_rect_width + separator_height) {
352 x = x_leading_padding + left_rect_width + separator_height;
354 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
356 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
358 pango_cairo_show_layout (cr, _right_layout->gobj());
361 /* no info to display, or just one */
365 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
367 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
375 if (!insert_map.empty()) {
377 int xcenter = layout_x_offset != 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
379 if (input_string.length() < insert_map.size()) {
380 Pango::Rectangle cursor;
382 if (input_string.empty()) {
383 /* nothing entered yet, put cursor at the end
386 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
388 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
391 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
394 min (get_width() - 2.0,
395 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + xcenter + em_width), 0,
396 2.0, cursor.get_height()/PANGO_SCALE);
399 min (get_width() - 2.0,
400 (double) layout_x_offset + xcenter + cursor.get_x()/PANGO_SCALE + em_width),
401 (upper_height - layout_height)/2.0,
402 2.0, cursor.get_height()/PANGO_SCALE);
406 /* we've entered all possible digits, no cursor */
410 if (input_string.empty()) {
411 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
420 (upper_height - layout_height)/2.0,
430 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
432 CairoWidget::on_size_allocate (alloc);
435 upper_height = (get_height()/2.0) - 1.0;
437 upper_height = get_height();
441 /* center display in available space
442 * NB. this only works if the containing widget is not the
443 * layout itself (eg. the session->property dialog)
445 layout_x_offset = (get_width() - layout_width)/2.0;
453 AudioClock::on_size_request (Gtk::Requisition* req)
455 /* even for non fixed width clocks, the size we *ask* for never changes,
456 even though the size we receive might. so once we've computed it,
461 req->width = first_width;
462 req->height = first_height;
466 Glib::RefPtr<Pango::Layout> tmp;
467 Glib::RefPtr<Gtk::Style> style = get_style ();
468 Pango::FontDescription font;
470 tmp = Pango::Layout::create (get_pango_context());
472 if (!is_realized()) {
473 font = get_font_for_style (get_name());
475 font = style->get_font();
478 tmp->set_font_description (font);
482 tmp->set_text ("-88:88:88:88");
483 tmp->get_pixel_size (_mode_width[Timecode], ignored);
484 tmp->set_text (" 8888|88|8888");
485 tmp->get_pixel_size (_mode_width[BBT], ignored);
486 tmp->set_text (" 88:88:88,888");
487 tmp->get_pixel_size (_mode_width[MinSec], ignored);
488 tmp->set_text (" 8888888888");
489 tmp->get_pixel_size (_mode_width[Frames], ignored);
491 /* this string is the longest thing we will ever display,
492 it does not include the BBT bar char that may descend
494 note; depending on BPM setting this may actually
495 not be sufficient for 24h worth of BBT
498 tmp->set_text (" 8888888888::,");
502 tmp->set_text ("-88:88:88:88");
505 tmp->set_text (" 8888|88|8888");
508 tmp->set_text (" 88:88:88,888");
511 tmp->set_text (" 8888888888");
516 tmp->get_pixel_size (req->width, req->height);
518 layout_height = req->height;
519 layout_width = req->width;
521 /* now tackle height, for which we need to know the height of the lower
529 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
530 font.set_weight (Pango::WEIGHT_NORMAL);
531 tmp->set_font_description (font);
533 /* we only care about height, so put as much stuff in here
534 as possible that might change the height.
536 tmp->set_text ("qyhH|"); /* one ascender, one descender */
538 tmp->get_pixel_size (w, info_height);
540 /* silly extra padding that seems necessary to correct the info
541 * that pango just gave us. I have no idea why.
544 req->height += info_height;
545 req->height += separator_height;
548 req->height += corner_radius/2.0;
549 req->width += corner_radius/2.0;
551 first_height = req->height;
552 first_width = req->width;
556 AudioClock::show_edit_status (int length)
558 editing_attr->set_start_index (edit_string.length() - length);
559 editing_attr->set_end_index (edit_string.length());
561 editing_attributes.change (*foreground_attr);
562 editing_attributes.change (*editing_attr);
564 _layout->set_attributes (editing_attributes);
568 AudioClock::start_edit (Field f)
570 pre_edit_string = _layout->get_text ();
571 if (!insert_map.empty()) {
572 edit_string = pre_edit_string;
574 edit_string.clear ();
575 _layout->set_text ("");
577 input_string.clear ();
581 input_string = get_field (f);
582 show_edit_status (merge_input_and_edit_string ());
583 _layout->set_text (edit_string);
588 Keyboard::magic_widget_grab_focus ();
593 AudioClock::get_field (Field f)
597 return edit_string.substr (1, 2);
599 case Timecode_Minutes:
600 return edit_string.substr (4, 2);
602 case Timecode_Seconds:
603 return edit_string.substr (7, 2);
605 case Timecode_Frames:
606 return edit_string.substr (10, 2);
609 return edit_string.substr (1, 2);
612 return edit_string.substr (4, 2);
615 return edit_string.substr (7, 2);
617 case MS_Milliseconds:
618 return edit_string.substr (10, 3);
621 return edit_string.substr (1, 3);
624 return edit_string.substr (5, 2);
627 return edit_string.substr (8, 4);
637 AudioClock::end_edit (bool modify)
645 ok = timecode_validate_edit (edit_string);
649 ok = bbt_validate_edit (edit_string);
653 ok = minsec_validate_edit (edit_string);
661 edit_string = pre_edit_string;
662 input_string.clear ();
663 _layout->set_text (edit_string);
664 show_edit_status (0);
665 /* edit attributes remain in use */
669 framepos_t pos = 0; /* stupid gcc */
673 pos = frames_from_timecode_string (edit_string);
678 pos = frame_duration_from_bbt_string (0, edit_string);
680 pos = frames_from_bbt_string (0, edit_string);
685 pos = frames_from_minsec_string (edit_string);
689 pos = frames_from_audioframes_string (edit_string);
694 _layout->set_attributes (normal_attributes);
695 ValueChanged(); /* EMIT_SIGNAL */
701 _layout->set_attributes (normal_attributes);
702 _layout->set_text (pre_edit_string);
713 AudioClock::drop_focus ()
715 Keyboard::magic_widget_drop_focus ();
719 /* move focus back to the default widget in the top level window */
721 Widget* top = get_toplevel();
723 if (top->is_toplevel ()) {
724 Window* win = dynamic_cast<Window*> (top);
731 AudioClock::parse_as_frames_distance (const std::string& str)
735 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
743 AudioClock::parse_as_minsec_distance (const std::string& str)
745 framecnt_t sr = _session->frame_rate();
751 switch (str.length()) {
758 sscanf (str.c_str(), "%" PRId32, &msecs);
759 return msecs * (sr / 1000);
762 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
763 return (secs * sr) + (msecs * (sr/1000));
766 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
767 return (secs * sr) + (msecs * (sr/1000));
770 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
771 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
774 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
775 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
778 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
779 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
782 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
783 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
793 AudioClock::parse_as_timecode_distance (const std::string& str)
795 double fps = _session->timecode_frames_per_second();
796 framecnt_t sr = _session->frame_rate();
802 switch (str.length()) {
807 sscanf (str.c_str(), "%" PRId32, &frames);
808 return lrint ((frames/(float)fps) * sr);
811 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
812 return (secs * sr) + lrint ((frames/(float)fps) * sr);
815 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
816 return (secs * sr) + lrint ((frames/(float)fps) * sr);
819 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
820 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
823 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
824 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
827 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
828 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
831 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
832 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
842 AudioClock::parse_as_bbt_distance (const std::string&)
848 AudioClock::parse_as_distance (const std::string& instr)
852 return parse_as_timecode_distance (instr);
855 return parse_as_frames_distance (instr);
858 return parse_as_bbt_distance (instr);
861 return parse_as_minsec_distance (instr);
868 AudioClock::end_edit_relative (bool add)
874 ok = timecode_validate_edit (edit_string);
878 ok = bbt_validate_edit (edit_string);
882 ok = minsec_validate_edit (edit_string);
890 edit_string = pre_edit_string;
891 input_string.clear ();
892 _layout->set_text (edit_string);
893 show_edit_status (0);
894 /* edit attributes remain in use */
899 framecnt_t frames = parse_as_distance (input_string);
904 _layout->set_attributes (normal_attributes);
908 set (current_time() + frames, true);
910 framepos_t c = current_time();
913 set (c - frames, true);
918 ValueChanged (); /* EMIT SIGNAL */
921 input_string.clear ();
927 AudioClock::session_configuration_changed (std::string p)
929 if (p == "sync-source" || p == "external-sync") {
930 set (current_time(), true);
934 if (p != "timecode-offset" && p != "timecode-offset-negative") {
943 current = current_duration ();
945 current = current_time ();
955 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
957 if ((!force && !is_visible()) || _session == 0) {
962 when = when - offset;
965 if (when == last_when && !force) {
966 if (_mode != Timecode) {
967 /* timecode may need to force display of TC source
968 * time, so don't return early.
976 _right_layout->set_alignment(Pango::ALIGN_LEFT);
982 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
984 set_timecode (when, force);
988 set_bbt (when, force);
992 set_minsec (when, force);
996 set_frames (when, force);
1006 AudioClock::set_frames (framepos_t when, bool /*force*/)
1009 bool negative = false;
1012 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1015 _left_layout->set_text ("");
1016 _right_layout->set_text ("");
1028 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1030 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1033 _layout->set_text (buf);
1036 framecnt_t rate = _session->frame_rate();
1038 if (fmod (rate, 100.0) == 0.0) {
1039 sprintf (buf, "%.1fkHz", rate/1000.0);
1041 sprintf (buf, "%" PRId64 "Hz", rate);
1044 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1045 INFO_FONT_SIZE, _("SR"), buf));
1047 float vid_pullup = _session->config.get_video_pullup();
1049 if (vid_pullup == 0.0) {
1050 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1051 INFO_FONT_SIZE, _("pullup: \u2012")));
1053 sprintf (buf, _("%+-6.4f%%"), vid_pullup);
1054 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1055 INFO_FONT_SIZE, buf));
1061 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1069 bool negative = false;
1072 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1075 _left_layout->set_text ("");
1076 _right_layout->set_text ("");
1088 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1089 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1090 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1091 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1092 secs = (int) floor (left / (float) _session->frame_rate());
1093 left -= (framecnt_t) floor (secs * _session->frame_rate());
1094 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1097 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1099 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1102 _layout->set_text (buf);
1106 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1109 bool negative = false;
1112 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1114 _left_layout->set_text ("");
1115 _right_layout->set_text ("");
1127 _session->timecode_duration (when, TC);
1129 _session->timecode_time (when, TC);
1132 TC.negative = TC.negative || negative;
1134 _layout->set_text (Timecode::timecode_format_time(TC));
1136 if (_left_layout && _right_layout) {
1138 SyncSource sync_src = Config->get_sync_source();
1140 if (_session->config.get_external_sync()) {
1141 Slave* slave = _session->slave();
1145 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1146 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1147 _right_layout->set_text ("");
1153 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1154 INFO_FONT_SIZE, dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1155 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1156 INFO_FONT_SIZE, slave->approximate_current_delta()));
1158 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1159 INFO_FONT_SIZE, _("--pending--")));
1160 _right_layout->set_text ("");
1165 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">INT/%2</span>",
1166 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1167 _right_layout->set_text ("");
1173 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1176 Timecode::BBT_Time BBT;
1177 bool negative = false;
1180 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1182 _left_layout->set_text ("");
1183 _right_layout->set_text ("");
1193 /* handle a common case */
1200 _session->tempo_map().bbt_time (when, BBT);
1205 _session->tempo_map().bbt_time (when, BBT);
1209 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1210 BBT.bars, BBT.beats, BBT.ticks);
1212 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1213 BBT.bars, BBT.beats, BBT.ticks);
1216 _layout->set_text (buf);
1218 if (_right_layout) {
1221 if (bbt_reference_time < 0) {
1224 pos = bbt_reference_time;
1227 TempoMetric m (_session->tempo_map().metric_at (pos));
1229 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1230 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1231 INFO_FONT_SIZE, buf));
1233 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1234 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1235 INFO_FONT_SIZE, buf));
1240 AudioClock::set_session (Session *s)
1242 SessionHandlePtr::set_session (s);
1246 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1248 const XMLProperty* prop;
1249 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1250 AudioClock::Mode amode;
1253 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1254 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1256 if ((prop = (*i)->property (X_("mode"))) != 0) {
1257 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1260 if ((prop = (*i)->property (X_("on"))) != 0) {
1261 set_off (!string_is_affirmative (prop->value()));
1268 set (last_when, true);
1273 AudioClock::on_key_press_event (GdkEventKey* ev)
1281 int highlight_length;
1284 switch (ev->keyval) {
1327 case GDK_KP_Subtract:
1328 end_edit_relative (false);
1334 end_edit_relative (true);
1347 ChangeAborted(); /* EMIT SIGNAL */
1352 if (!input_string.empty()) {
1353 /* delete the last key entered
1355 input_string = input_string.substr (0, input_string.length() - 1);
1357 goto use_input_string;
1363 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1364 /* too many digits: eat the key event, but do nothing with it */
1368 input_string.push_back (new_char);
1374 /* get this one in the right order, and to the right width */
1375 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1376 edit_string = edit_string.substr (0, edit_string.length() - 1);
1378 edit_string.push_back (new_char);
1380 if (!edit_string.empty()) {
1382 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1383 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1386 /* highlight the whole thing */
1387 highlight_length = edit_string.length();
1391 highlight_length = merge_input_and_edit_string ();
1394 show_edit_status (highlight_length);
1395 _layout->set_text (edit_string);
1402 AudioClock::merge_input_and_edit_string ()
1404 /* merge with pre-edit-string into edit string */
1406 edit_string = pre_edit_string;
1408 if (input_string.empty()) {
1412 string::size_type target;
1413 for (string::size_type i = 0; i < input_string.length(); ++i) {
1414 target = insert_map[input_string.length() - 1 - i];
1415 edit_string[target] = input_string[i];
1417 /* highlight from end to wherever the last character was added */
1418 return edit_string.length() - insert_map[input_string.length()-1];
1423 AudioClock::on_key_release_event (GdkEventKey *ev)
1429 /* return true for keys that we used on press
1430 so that they cannot possibly do double-duty
1432 switch (ev->keyval) {
1455 case GDK_KP_Decimal:
1463 case GDK_KP_Subtract:
1471 AudioClock::index_to_field (int index) const
1476 return Timecode_Hours;
1477 } else if (index < 7) {
1478 return Timecode_Minutes;
1479 } else if (index < 10) {
1480 return Timecode_Seconds;
1482 return Timecode_Frames;
1488 } else if (index < 7) {
1496 return Timecode_Hours;
1497 } else if (index < 6) {
1499 } else if (index < 9) {
1502 return MS_Milliseconds;
1514 AudioClock::on_button_press_event (GdkEventButton *ev)
1516 switch (ev->button) {
1518 if (editable && !_off) {
1524 /* the text has been centered vertically, so adjust
1528 y = ev->y - ((upper_height - layout_height)/2);
1529 x = ev->x - layout_x_offset;
1531 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1532 /* pretend it is a character on the far right */
1535 drag_field = index_to_field (index);
1537 /* make absolutely sure that the pointer is grabbed */
1538 gdk_pointer_grab(ev->window,false ,
1539 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1540 NULL,NULL,ev->time);
1542 drag_start_y = ev->y;
1556 AudioClock::on_button_release_event (GdkEventButton *ev)
1558 if (editable && !_off) {
1560 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1562 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1563 // we actually dragged so return without
1564 // setting editing focus, or we shift clicked
1567 if (ev->button == 1) {
1569 if (_edit_by_click_field) {
1573 int y = ev->y - ((upper_height - layout_height)/2);
1574 int x = ev->x - layout_x_offset;
1577 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1581 f = index_to_field (index);
1584 case Timecode_Frames:
1585 case MS_Milliseconds:
1601 if (Keyboard::is_context_menu_event (ev)) {
1602 if (ops_menu == 0) {
1605 ops_menu->popup (1, ev->time);
1613 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1615 bool ret = CairoWidget::on_focus_out_event (ev);
1625 AudioClock::on_scroll_event (GdkEventScroll *ev)
1630 if (editing || _session == 0 || !editable || _off) {
1637 /* the text has been centered vertically, so adjust
1641 y = ev->y - ((upper_height - layout_height)/2);
1642 x = ev->x - layout_x_offset;
1644 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1645 /* not in the main layout */
1649 Field f = index_to_field (index);
1650 framepos_t frames = 0;
1652 switch (ev->direction) {
1655 frames = get_frame_step (f);
1657 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1660 set (current_time() + frames, true);
1661 ValueChanged (); /* EMIT_SIGNAL */
1665 case GDK_SCROLL_DOWN:
1666 frames = get_frame_step (f);
1668 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1672 if ((double)current_time() - (double)frames < 0.0) {
1675 set (current_time() - frames, true);
1678 ValueChanged (); /* EMIT_SIGNAL */
1691 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1693 if (editing || _session == 0 || !dragging) {
1697 float pixel_frame_scale_factor = 0.2f;
1699 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1700 pixel_frame_scale_factor = 0.1f;
1704 if (Keyboard::modifier_state_contains (ev->state,
1705 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1707 pixel_frame_scale_factor = 0.025f;
1710 double y_delta = ev->y - drag_y;
1712 drag_accum += y_delta*pixel_frame_scale_factor;
1716 if (trunc (drag_accum) != 0) {
1721 dir = (drag_accum < 0 ? 1:-1);
1722 pos = current_time();
1723 frames = get_frame_step (drag_field, pos, dir);
1725 if (frames != 0 && frames * drag_accum < current_time()) {
1726 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1732 ValueChanged(); /* EMIT_SIGNAL */
1739 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1742 Timecode::BBT_Time BBT;
1744 case Timecode_Hours:
1745 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1747 case Timecode_Minutes:
1748 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1750 case Timecode_Seconds:
1751 f = _session->frame_rate();
1753 case Timecode_Frames:
1754 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1762 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1765 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1768 f = (framecnt_t) _session->frame_rate();
1770 case MS_Milliseconds:
1771 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1778 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1784 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1790 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1793 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1802 AudioClock::current_time (framepos_t) const
1808 AudioClock::current_duration (framepos_t pos) const
1817 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1833 AudioClock::bbt_validate_edit (const string& str)
1837 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1841 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1845 if (!is_duration && any.bbt.bars == 0) {
1849 if (!is_duration && any.bbt.beats == 0) {
1857 AudioClock::timecode_validate_edit (const string&)
1861 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1862 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1866 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1870 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1874 if (_session->timecode_drop_frames()) {
1875 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1884 AudioClock::minsec_validate_edit (const string& str)
1886 int hrs, mins, secs, millisecs;
1888 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1892 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1900 AudioClock::frames_from_timecode_string (const string& str) const
1902 if (_session == 0) {
1909 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1910 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1914 TC.rate = _session->timecode_frames_per_second();
1915 TC.drop= _session->timecode_drop_frames();
1917 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1919 // timecode_tester ();
1925 AudioClock::frames_from_minsec_string (const string& str) const
1927 if (_session == 0) {
1931 int hrs, mins, secs, millisecs;
1932 framecnt_t sr = _session->frame_rate();
1934 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1935 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1939 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1943 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1945 if (_session == 0) {
1946 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1951 any.type = AnyTime::BBT;
1953 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1960 return _session->any_duration_to_frames (pos, any);
1962 return _session->convert_to_frames (any);
1968 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1970 if (_session == 0) {
1971 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1975 Timecode::BBT_Time bbt;
1977 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1981 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1985 AudioClock::frames_from_audioframes_string (const string& str) const
1988 sscanf (str.c_str(), "%" PRId64, &f);
1993 AudioClock::build_ops_menu ()
1995 using namespace Menu_Helpers;
1996 ops_menu = new Menu;
1997 MenuList& ops_items = ops_menu->items();
1998 ops_menu->set_name ("ArdourContextMenu");
2000 if (!Profile->get_sae()) {
2001 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2003 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2004 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2005 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2007 if (editable && !_off && !is_duration && !_follows_playhead) {
2008 ops_items.push_back (SeparatorElem());
2009 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2010 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2015 AudioClock::set_from_playhead ()
2021 set (_session->transport_frame());
2026 AudioClock::locate ()
2028 if (!_session || is_duration) {
2032 _session->request_locate (current_time(), _session->transport_rolling ());
2036 AudioClock::set_mode (Mode m)
2046 _layout->set_text ("");
2050 _left_layout->set_attributes (info_attributes);
2051 _right_layout->set_attributes (info_attributes);
2052 /* adjust info_height according to font size */
2054 _left_layout->set_text (" 1234567890");
2055 _left_layout->get_pixel_size (ignored, info_height);
2058 _left_layout->set_text ("");
2059 _right_layout->set_text ("");
2064 mode_based_info_ratio = 0.5;
2065 insert_map.push_back (11);
2066 insert_map.push_back (10);
2067 insert_map.push_back (8);
2068 insert_map.push_back (7);
2069 insert_map.push_back (5);
2070 insert_map.push_back (4);
2071 insert_map.push_back (2);
2072 insert_map.push_back (1);
2076 mode_based_info_ratio = 0.5;
2077 insert_map.push_back (11);
2078 insert_map.push_back (10);
2079 insert_map.push_back (9);
2080 insert_map.push_back (8);
2081 insert_map.push_back (6);
2082 insert_map.push_back (5);
2083 insert_map.push_back (3);
2084 insert_map.push_back (2);
2085 insert_map.push_back (1);
2089 mode_based_info_ratio = 1.0;
2090 insert_map.push_back (12);
2091 insert_map.push_back (11);
2092 insert_map.push_back (10);
2093 insert_map.push_back (8);
2094 insert_map.push_back (7);
2095 insert_map.push_back (5);
2096 insert_map.push_back (4);
2097 insert_map.push_back (2);
2098 insert_map.push_back (1);
2102 mode_based_info_ratio = 0.5;
2106 set (last_when, true);
2108 if (!is_transient) {
2109 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2112 if (!_fixed_width) {
2113 /* display is different, allow us to resize */
2119 mode_changed (); /* EMIT SIGNAL (the member one) */
2123 AudioClock::set_bbt_reference (framepos_t pos)
2125 bbt_reference_time = pos;
2129 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2131 CairoWidget::on_style_changed (old_style);
2139 AudioClock::set_editable (bool yn)
2145 AudioClock::set_is_duration (bool yn)
2147 if (yn == is_duration) {
2152 set (last_when, true);
2156 AudioClock::set_off (bool yn)
2164 /* force a redraw. last_when will be preserved, but the clock text will
2168 set (last_when, true);
2172 AudioClock::focus ()
2174 start_edit (Field (0));
2178 AudioClock::set_corner_radius (double r)
2187 AudioClock::set_fixed_width (bool yn)
2193 AudioClock::dpi_reset ()
2195 /* force recomputation of size even if we are fixed width