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 > corner_radius/4.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 > corner_radius/4.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 && _mode != MinSec) {
967 /* 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);
993 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
995 set_minsec (when, force);
999 set_frames (when, force);
1009 AudioClock::set_slave_info ()
1011 if (!_left_layout || !_right_layout) {
1015 SyncSource sync_src = Config->get_sync_source();
1017 if (_session->config.get_external_sync()) {
1018 Slave* slave = _session->slave();
1022 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1023 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1024 _right_layout->set_text ("");
1031 TimecodeSlave* tcslave;
1032 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1033 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1037 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"%2\">%3</span>",
1038 INFO_FONT_SIZE, (matching?"green":"red"),
1039 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1040 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1041 INFO_FONT_SIZE, slave->approximate_current_delta()));
1043 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1044 INFO_FONT_SIZE, _("--pending--")));
1045 _right_layout->set_text ("");
1050 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">INT/%2</span>",
1051 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1052 _right_layout->set_text ("");
1057 AudioClock::set_frames (framepos_t when, bool /*force*/)
1060 bool negative = false;
1063 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1066 _left_layout->set_text ("");
1067 _right_layout->set_text ("");
1079 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1081 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1084 _layout->set_text (buf);
1087 framecnt_t rate = _session->frame_rate();
1089 if (fmod (rate, 100.0) == 0.0) {
1090 sprintf (buf, "%.1fkHz", rate/1000.0);
1092 sprintf (buf, "%" PRId64 "Hz", rate);
1095 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1096 INFO_FONT_SIZE, _("SR"), buf));
1098 float vid_pullup = _session->config.get_video_pullup();
1100 if (vid_pullup == 0.0) {
1101 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1102 INFO_FONT_SIZE, _("pullup: \u2012")));
1104 sprintf (buf, _("%+-6.4f%%"), vid_pullup);
1105 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1106 INFO_FONT_SIZE, buf));
1112 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1120 bool negative = false;
1123 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1126 _left_layout->set_text ("");
1127 _right_layout->set_text ("");
1139 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1140 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1141 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1142 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1143 secs = (int) floor (left / (float) _session->frame_rate());
1144 left -= (framecnt_t) floor (secs * _session->frame_rate());
1145 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1148 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1150 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1153 _layout->set_text (buf);
1158 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1161 bool negative = false;
1164 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1166 _left_layout->set_text ("");
1167 _right_layout->set_text ("");
1179 _session->timecode_duration (when, TC);
1181 _session->timecode_time (when, TC);
1184 TC.negative = TC.negative || negative;
1186 _layout->set_text (Timecode::timecode_format_time(TC));
1192 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1195 Timecode::BBT_Time BBT;
1196 bool negative = false;
1199 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1201 _left_layout->set_text ("");
1202 _right_layout->set_text ("");
1212 /* handle a common case */
1219 _session->tempo_map().bbt_time (when, BBT);
1224 _session->tempo_map().bbt_time (when, BBT);
1228 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1229 BBT.bars, BBT.beats, BBT.ticks);
1231 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1232 BBT.bars, BBT.beats, BBT.ticks);
1235 _layout->set_text (buf);
1237 if (_right_layout) {
1240 if (bbt_reference_time < 0) {
1243 pos = bbt_reference_time;
1246 TempoMetric m (_session->tempo_map().metric_at (pos));
1248 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1249 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1250 INFO_FONT_SIZE, buf));
1252 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1253 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"green\">%2</span>",
1254 INFO_FONT_SIZE, buf));
1259 AudioClock::set_session (Session *s)
1261 SessionHandlePtr::set_session (s);
1265 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1267 const XMLProperty* prop;
1268 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1269 AudioClock::Mode amode;
1272 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1273 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1275 if ((prop = (*i)->property (X_("mode"))) != 0) {
1276 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1279 if ((prop = (*i)->property (X_("on"))) != 0) {
1280 set_off (!string_is_affirmative (prop->value()));
1287 set (last_when, true);
1292 AudioClock::on_key_press_event (GdkEventKey* ev)
1300 int highlight_length;
1303 switch (ev->keyval) {
1346 case GDK_KP_Subtract:
1347 end_edit_relative (false);
1353 end_edit_relative (true);
1366 ChangeAborted(); /* EMIT SIGNAL */
1371 if (!input_string.empty()) {
1372 /* delete the last key entered
1374 input_string = input_string.substr (0, input_string.length() - 1);
1376 goto use_input_string;
1382 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1383 /* too many digits: eat the key event, but do nothing with it */
1387 input_string.push_back (new_char);
1393 /* get this one in the right order, and to the right width */
1394 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1395 edit_string = edit_string.substr (0, edit_string.length() - 1);
1397 edit_string.push_back (new_char);
1399 if (!edit_string.empty()) {
1401 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1402 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1405 /* highlight the whole thing */
1406 highlight_length = edit_string.length();
1410 highlight_length = merge_input_and_edit_string ();
1413 show_edit_status (highlight_length);
1414 _layout->set_text (edit_string);
1421 AudioClock::merge_input_and_edit_string ()
1423 /* merge with pre-edit-string into edit string */
1425 edit_string = pre_edit_string;
1427 if (input_string.empty()) {
1431 string::size_type target;
1432 for (string::size_type i = 0; i < input_string.length(); ++i) {
1433 target = insert_map[input_string.length() - 1 - i];
1434 edit_string[target] = input_string[i];
1436 /* highlight from end to wherever the last character was added */
1437 return edit_string.length() - insert_map[input_string.length()-1];
1442 AudioClock::on_key_release_event (GdkEventKey *ev)
1448 /* return true for keys that we used on press
1449 so that they cannot possibly do double-duty
1451 switch (ev->keyval) {
1474 case GDK_KP_Decimal:
1482 case GDK_KP_Subtract:
1490 AudioClock::index_to_field (int index) const
1495 return Timecode_Hours;
1496 } else if (index < 7) {
1497 return Timecode_Minutes;
1498 } else if (index < 10) {
1499 return Timecode_Seconds;
1501 return Timecode_Frames;
1507 } else if (index < 7) {
1515 return Timecode_Hours;
1516 } else if (index < 6) {
1518 } else if (index < 9) {
1521 return MS_Milliseconds;
1533 AudioClock::on_button_press_event (GdkEventButton *ev)
1535 switch (ev->button) {
1537 if (editable && !_off) {
1543 /* the text has been centered vertically, so adjust
1546 int xcenter = !_fixed_width || layout_x_offset > corner_radius/4.0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1548 y = ev->y - ((upper_height - layout_height)/2);
1549 x = ev->x - layout_x_offset - xcenter;
1551 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1552 /* pretend it is a character on the far right */
1555 drag_field = index_to_field (index);
1557 /* make absolutely sure that the pointer is grabbed */
1558 gdk_pointer_grab(ev->window,false ,
1559 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1560 NULL,NULL,ev->time);
1562 drag_start_y = ev->y;
1576 AudioClock::on_button_release_event (GdkEventButton *ev)
1578 if (editable && !_off) {
1580 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1582 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1583 // we actually dragged so return without
1584 // setting editing focus, or we shift clicked
1587 if (ev->button == 1) {
1589 if (_edit_by_click_field) {
1591 int xcenter = !_fixed_width || layout_x_offset > corner_radius/4.0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1594 int y = ev->y - ((upper_height - layout_height)/2);
1595 int x = ev->x - layout_x_offset - xcenter;
1598 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1602 f = index_to_field (index);
1605 case Timecode_Frames:
1606 case MS_Milliseconds:
1622 if (Keyboard::is_context_menu_event (ev)) {
1623 if (ops_menu == 0) {
1626 ops_menu->popup (1, ev->time);
1634 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1636 bool ret = CairoWidget::on_focus_out_event (ev);
1646 AudioClock::on_scroll_event (GdkEventScroll *ev)
1651 if (editing || _session == 0 || !editable || _off) {
1658 /* the text has been centered vertically, so adjust
1662 int xcenter = !_fixed_width || layout_x_offset > corner_radius/4.0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1663 y = ev->y - ((upper_height - layout_height)/2);
1664 x = ev->x - layout_x_offset - xcenter;
1666 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1667 /* not in the main layout */
1671 Field f = index_to_field (index);
1672 framepos_t frames = 0;
1674 switch (ev->direction) {
1677 frames = get_frame_step (f);
1679 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1682 set (current_time() + frames, true);
1683 ValueChanged (); /* EMIT_SIGNAL */
1687 case GDK_SCROLL_DOWN:
1688 frames = get_frame_step (f);
1690 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1694 if ((double)current_time() - (double)frames < 0.0) {
1697 set (current_time() - frames, true);
1700 ValueChanged (); /* EMIT_SIGNAL */
1713 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1715 if (editing || _session == 0 || !dragging) {
1719 float pixel_frame_scale_factor = 0.2f;
1721 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1722 pixel_frame_scale_factor = 0.1f;
1726 if (Keyboard::modifier_state_contains (ev->state,
1727 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1729 pixel_frame_scale_factor = 0.025f;
1732 double y_delta = ev->y - drag_y;
1734 drag_accum += y_delta*pixel_frame_scale_factor;
1738 if (trunc (drag_accum) != 0) {
1743 dir = (drag_accum < 0 ? 1:-1);
1744 pos = current_time();
1745 frames = get_frame_step (drag_field, pos, dir);
1747 if (frames != 0 && frames * drag_accum < current_time()) {
1748 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1754 ValueChanged(); /* EMIT_SIGNAL */
1761 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1764 Timecode::BBT_Time BBT;
1766 case Timecode_Hours:
1767 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1769 case Timecode_Minutes:
1770 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1772 case Timecode_Seconds:
1773 f = _session->frame_rate();
1775 case Timecode_Frames:
1776 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1784 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1787 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1790 f = (framecnt_t) _session->frame_rate();
1792 case MS_Milliseconds:
1793 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1800 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1806 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1812 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1815 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1824 AudioClock::current_time (framepos_t) const
1830 AudioClock::current_duration (framepos_t pos) const
1839 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1855 AudioClock::bbt_validate_edit (const string& str)
1859 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1863 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1867 if (!is_duration && any.bbt.bars == 0) {
1871 if (!is_duration && any.bbt.beats == 0) {
1879 AudioClock::timecode_validate_edit (const string&)
1883 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1884 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1888 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1892 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1896 if (_session->timecode_drop_frames()) {
1897 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1906 AudioClock::minsec_validate_edit (const string& str)
1908 int hrs, mins, secs, millisecs;
1910 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1914 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1922 AudioClock::frames_from_timecode_string (const string& str) const
1924 if (_session == 0) {
1931 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1932 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1936 TC.rate = _session->timecode_frames_per_second();
1937 TC.drop= _session->timecode_drop_frames();
1939 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1941 // timecode_tester ();
1947 AudioClock::frames_from_minsec_string (const string& str) const
1949 if (_session == 0) {
1953 int hrs, mins, secs, millisecs;
1954 framecnt_t sr = _session->frame_rate();
1956 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1957 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1961 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1965 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1967 if (_session == 0) {
1968 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1973 any.type = AnyTime::BBT;
1975 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1982 return _session->any_duration_to_frames (pos, any);
1984 return _session->convert_to_frames (any);
1990 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1992 if (_session == 0) {
1993 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1997 Timecode::BBT_Time bbt;
1999 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2003 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2007 AudioClock::frames_from_audioframes_string (const string& str) const
2010 sscanf (str.c_str(), "%" PRId64, &f);
2015 AudioClock::build_ops_menu ()
2017 using namespace Menu_Helpers;
2018 ops_menu = new Menu;
2019 MenuList& ops_items = ops_menu->items();
2020 ops_menu->set_name ("ArdourContextMenu");
2022 if (!Profile->get_sae()) {
2023 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2025 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2026 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2027 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2029 if (editable && !_off && !is_duration && !_follows_playhead) {
2030 ops_items.push_back (SeparatorElem());
2031 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2032 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2037 AudioClock::set_from_playhead ()
2043 set (_session->transport_frame());
2048 AudioClock::locate ()
2050 if (!_session || is_duration) {
2054 _session->request_locate (current_time(), _session->transport_rolling ());
2058 AudioClock::set_mode (Mode m)
2068 _layout->set_text ("");
2072 _left_layout->set_attributes (info_attributes);
2073 _right_layout->set_attributes (info_attributes);
2074 /* adjust info_height according to font size */
2076 _left_layout->set_text (" 1234567890");
2077 _left_layout->get_pixel_size (ignored, info_height);
2080 _left_layout->set_text ("");
2081 _right_layout->set_text ("");
2086 mode_based_info_ratio = 0.5;
2087 insert_map.push_back (11);
2088 insert_map.push_back (10);
2089 insert_map.push_back (8);
2090 insert_map.push_back (7);
2091 insert_map.push_back (5);
2092 insert_map.push_back (4);
2093 insert_map.push_back (2);
2094 insert_map.push_back (1);
2098 mode_based_info_ratio = 0.5;
2099 insert_map.push_back (11);
2100 insert_map.push_back (10);
2101 insert_map.push_back (9);
2102 insert_map.push_back (8);
2103 insert_map.push_back (6);
2104 insert_map.push_back (5);
2105 insert_map.push_back (3);
2106 insert_map.push_back (2);
2107 insert_map.push_back (1);
2111 mode_based_info_ratio = 0.5;
2112 insert_map.push_back (12);
2113 insert_map.push_back (11);
2114 insert_map.push_back (10);
2115 insert_map.push_back (8);
2116 insert_map.push_back (7);
2117 insert_map.push_back (5);
2118 insert_map.push_back (4);
2119 insert_map.push_back (2);
2120 insert_map.push_back (1);
2124 mode_based_info_ratio = 0.5;
2128 set (last_when, true);
2130 if (!is_transient) {
2131 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2134 if (!_fixed_width) {
2135 /* display is different, allow us to resize */
2141 mode_changed (); /* EMIT SIGNAL (the member one) */
2145 AudioClock::set_bbt_reference (framepos_t pos)
2147 bbt_reference_time = pos;
2151 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2153 CairoWidget::on_style_changed (old_style);
2161 AudioClock::set_editable (bool yn)
2167 AudioClock::set_is_duration (bool yn)
2169 if (yn == is_duration) {
2174 set (last_when, true);
2178 AudioClock::set_off (bool yn)
2186 /* force a redraw. last_when will be preserved, but the clock text will
2190 set (last_when, true);
2194 AudioClock::focus ()
2196 start_edit (Field (0));
2200 AudioClock::set_corner_radius (double r)
2209 AudioClock::set_fixed_width (bool yn)
2215 AudioClock::dpi_reset ()
2217 /* force recomputation of size even if we are fixed width