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;
48 using namespace ARDOUR_UI_UTILS;
53 using Gtkmm2ext::Keyboard;
55 sigc::signal<void> AudioClock::ModeChanged;
56 vector<AudioClock*> AudioClock::clocks;
57 const double AudioClock::info_font_scale_factor = 0.50;
58 const double AudioClock::separator_height = 0.0;
59 const double AudioClock::x_leading_padding = 6.0;
61 #define BBT_BAR_CHAR "|"
62 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
63 #define INFO_FONT_SIZE ((int)lrint(font_size * info_font_scale_factor))
64 #define TXTSPAN "<span font-family=\"Sans\" foreground=\"white\">"
66 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
67 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
70 , is_transient (transient)
71 , is_duration (duration)
72 , editable (allow_edit)
73 , _follows_playhead (follows_playhead)
76 , _edit_by_click_field (false)
77 , _negative_allowed (false)
78 , edit_is_negative (false)
83 , style_resets_first (true)
88 , mode_based_info_ratio (1.0)
92 , bbt_reference_time (-1)
97 , drag_field (Field (0))
99 set_flags (CAN_FOCUS);
101 _layout = Pango::Layout::create (get_pango_context());
102 _layout->set_attributes (normal_attributes);
105 _left_layout = Pango::Layout::create (get_pango_context());
106 _right_layout = Pango::Layout::create (get_pango_context());
109 set_widget_name (widget_name);
111 _mode = BBT; /* lie to force mode switch */
113 set (last_when, true);
116 clocks.push_back (this);
119 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
120 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
123 AudioClock::~AudioClock ()
125 delete foreground_attr;
130 AudioClock::set_widget_name (const string& str)
135 set_name (str + " clock");
145 AudioClock::on_realize ()
147 Gtk::Requisition req;
149 CairoWidget::on_realize ();
151 set_clock_dimensions (req);
153 first_width = req.width;
154 first_height = req.height;
161 AudioClock::set_font ()
163 Glib::RefPtr<Gtk::Style> style = get_style ();
164 Pango::FontDescription font;
165 Pango::AttrFontDesc* font_attr;
167 if (!is_realized()) {
168 font = get_font_for_style (get_name());
170 font = style->get_font();
173 font_size = font.get_size();
175 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
177 normal_attributes.change (*font_attr);
178 editing_attributes.change (*font_attr);
180 /* now a smaller version of the same font */
183 font.set_size (INFO_FONT_SIZE);
184 font.set_weight (Pango::WEIGHT_NORMAL);
185 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
187 info_attributes.change (*font_attr);
189 /* and an even smaller one */
193 /* get the figure width for the font. This doesn't have to super
194 * accurate since we only use it to measure the (roughly 1 character)
195 * offset from the position Pango tells us for the "cursor"
198 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
202 tmp->get_pixel_size (em_width, ignore_height);
204 /* force redraw of markup with new font-size */
205 set (last_when, true);
209 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
211 CairoWidget::set_active_state (s);
216 AudioClock::set_colors ()
222 uint32_t editing_color;
223 uint32_t cursor_color;
225 if (active_state()) {
226 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
227 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
228 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
229 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
231 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
232 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
233 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
234 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
237 /* store for bg and cursor in render() */
239 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
246 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
253 /* rescale for Pango colors ... sigh */
255 r = lrint (r * 65535.0);
256 g = lrint (g * 65535.0);
257 b = lrint (b * 65535.0);
259 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
260 r = lrint ((r/255.0) * 65535.0);
261 g = lrint ((g/255.0) * 65535.0);
262 b = lrint ((b/255.0) * 65535.0);
263 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
265 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
266 r = lrint ((r/255.0) * 65535.0);
267 g = lrint ((g/255.0) * 65535.0);
268 b = lrint ((b/255.0) * 65535.0);
269 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
271 normal_attributes.change (*foreground_attr);
272 info_attributes.change (*foreground_attr);
273 editing_attributes.change (*foreground_attr);
274 editing_attributes.change (*editing_attr);
277 _layout->set_attributes (normal_attributes);
279 _layout->set_attributes (editing_attributes);
286 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
288 /* main layout: rounded rect, plus the text */
291 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
294 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
296 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
299 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
304 cairo_move_to (cr, (get_width() - layout_width) / 2.0, (upper_height - layout_height) / 2.0);
306 pango_cairo_show_layout (cr, _layout->gobj());
310 double h = get_height() - upper_height - separator_height;
313 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
316 if (mode_based_info_ratio != 1.0) {
318 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
322 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
323 left_rect_width + (separator_height == 0 ? corner_radius : 0),
326 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
331 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
332 pango_cairo_show_layout (cr, _left_layout->gobj());
336 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
337 upper_height + separator_height,
338 get_width() - separator_height - left_rect_width,
341 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
342 get_width() - separator_height - left_rect_width, h);
348 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
349 /* right-align does not work per se beacuse layout width is unset.
350 * Using _right_layout->set_width([value >=0]) would also enable
351 * word-wrapping which is not wanted here.
352 * The solution is to custom align the layout depending on its size.
353 * if it is larger than the available space it will be cropped on the
354 * right edge rather than override text on the left side.
357 _right_layout->get_pixel_size(rw, rh);
358 x = get_width() - rw - separator_height - x_leading_padding;
359 if (x < x_leading_padding + left_rect_width + separator_height) {
360 /* rather cut off the right end than overlap with the text on the left */
361 x = x_leading_padding + left_rect_width + separator_height;
363 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
365 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
367 pango_cairo_show_layout (cr, _right_layout->gobj());
370 /* no info to display, or just one */
374 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
376 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
384 if (!insert_map.empty()) {
386 int xcenter = (get_width() - layout_width) /2;
388 if (input_string.length() < insert_map.size()) {
389 Pango::Rectangle cursor;
391 if (input_string.empty()) {
392 /* nothing entered yet, put cursor at the end
395 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
397 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
400 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
402 min (get_width() - 2.0,
403 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
404 (upper_height - layout_height)/2.0,
405 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);
416 (upper_height - layout_height)/2.0,
425 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
427 CairoWidget::on_size_allocate (alloc);
430 upper_height = (get_height()/2.0) - 1.0;
432 upper_height = get_height();
437 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
439 Glib::RefPtr<Pango::Layout> tmp;
440 Glib::RefPtr<Gtk::Style> style = get_style ();
441 Pango::FontDescription font;
443 tmp = Pango::Layout::create (get_pango_context());
445 if (!is_realized()) {
446 font = get_font_for_style (get_name());
448 font = style->get_font();
451 tmp->set_font_description (font);
453 /* this string is the longest thing we will ever display */
454 tmp->set_text (" 88:88:88,888");
455 tmp->get_pixel_size (req.width, req.height);
457 layout_height = req.height;
458 layout_width = req.width;
462 AudioClock::on_size_request (Gtk::Requisition* req)
464 /* even for non fixed width clocks, the size we *ask* for never changes,
465 even though the size we receive might. so once we've computed it,
470 req->width = first_width;
471 req->height = first_height;
475 set_clock_dimensions (*req);
477 /* now tackle height, for which we need to know the height of the lower
483 Glib::RefPtr<Pango::Layout> tmp;
484 Glib::RefPtr<Gtk::Style> style = get_style ();
485 Pango::FontDescription font;
488 tmp = Pango::Layout::create (get_pango_context());
490 if (!is_realized()) {
491 font = get_font_for_style (get_name());
493 font = style->get_font();
496 tmp->set_font_description (font);
498 font.set_size (INFO_FONT_SIZE);
499 font.set_weight (Pango::WEIGHT_NORMAL);
500 tmp->set_font_description (font);
502 /* we only care about height, so put as much stuff in here
503 as possible that might change the height.
505 tmp->set_text ("qyhH|"); /* one ascender, one descender */
507 tmp->get_pixel_size (w, info_height);
509 /* silly extra padding that seems necessary to correct the info
510 * that pango just gave us. I have no idea why.
513 req->height += info_height;
514 req->height += separator_height;
519 AudioClock::show_edit_status (int length)
521 editing_attr->set_start_index (edit_string.length() - length);
522 editing_attr->set_end_index (edit_string.length());
524 editing_attributes.change (*foreground_attr);
525 editing_attributes.change (*editing_attr);
527 _layout->set_attributes (editing_attributes);
531 AudioClock::start_edit (Field f)
534 pre_edit_string = _layout->get_text ();
535 if (!insert_map.empty()) {
536 edit_string = pre_edit_string;
538 edit_string.clear ();
539 _layout->set_text ("");
542 input_string.clear ();
544 edit_is_negative = false;
547 input_string = get_field (f);
548 show_edit_status (merge_input_and_edit_string ());
549 _layout->set_text (edit_string);
554 Keyboard::magic_widget_grab_focus ();
560 AudioClock::get_field (Field f)
564 return edit_string.substr (1, 2);
566 case Timecode_Minutes:
567 return edit_string.substr (4, 2);
569 case Timecode_Seconds:
570 return edit_string.substr (7, 2);
572 case Timecode_Frames:
573 return edit_string.substr (10, 2);
576 return edit_string.substr (1, 2);
579 return edit_string.substr (4, 2);
582 return edit_string.substr (7, 2);
584 case MS_Milliseconds:
585 return edit_string.substr (10, 3);
588 return edit_string.substr (1, 3);
591 return edit_string.substr (5, 2);
594 return edit_string.substr (8, 4);
604 AudioClock::end_edit (bool modify)
612 ok = timecode_validate_edit (edit_string);
616 ok = bbt_validate_edit (edit_string);
620 ok = minsec_validate_edit (edit_string);
624 if (edit_string.length() < 1) {
625 edit_string = pre_edit_string;
631 edit_string = pre_edit_string;
632 input_string.clear ();
633 _layout->set_text (edit_string);
634 show_edit_status (0);
635 /* edit attributes remain in use */
639 framepos_t pos = 0; /* stupid gcc */
643 pos = frames_from_timecode_string (edit_string);
648 pos = frame_duration_from_bbt_string (0, edit_string);
650 pos = frames_from_bbt_string (0, edit_string);
655 pos = frames_from_minsec_string (edit_string);
659 pos = frames_from_audioframes_string (edit_string);
664 _layout->set_attributes (normal_attributes);
665 ValueChanged(); /* EMIT_SIGNAL */
671 edit_is_negative = false;
672 _layout->set_attributes (normal_attributes);
673 _layout->set_text (pre_edit_string);
684 AudioClock::drop_focus ()
686 Keyboard::magic_widget_drop_focus ();
690 /* move focus back to the default widget in the top level window */
692 Widget* top = get_toplevel();
694 if (top->is_toplevel ()) {
695 Window* win = dynamic_cast<Window*> (top);
702 AudioClock::parse_as_frames_distance (const std::string& str)
706 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
714 AudioClock::parse_as_minsec_distance (const std::string& str)
716 framecnt_t sr = _session->frame_rate();
722 switch (str.length()) {
729 sscanf (str.c_str(), "%" PRId32, &msecs);
730 return msecs * (sr / 1000);
733 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
734 return (secs * sr) + (msecs * (sr/1000));
737 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
738 return (secs * sr) + (msecs * (sr/1000));
741 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
742 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
745 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
746 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
749 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
750 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
753 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
754 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
764 AudioClock::parse_as_timecode_distance (const std::string& str)
766 double fps = _session->timecode_frames_per_second();
767 framecnt_t sr = _session->frame_rate();
773 switch (str.length()) {
778 sscanf (str.c_str(), "%" PRId32, &frames);
779 return lrint ((frames/(float)fps) * sr);
782 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
783 return (secs * sr) + lrint ((frames/(float)fps) * sr);
786 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
787 return (secs * sr) + lrint ((frames/(float)fps) * sr);
790 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
791 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
794 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
795 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
798 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
799 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
802 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
803 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
813 AudioClock::parse_as_bbt_distance (const std::string&)
819 AudioClock::parse_as_distance (const std::string& instr)
823 return parse_as_timecode_distance (instr);
826 return parse_as_frames_distance (instr);
829 return parse_as_bbt_distance (instr);
832 return parse_as_minsec_distance (instr);
839 AudioClock::end_edit_relative (bool add)
845 ok = timecode_validate_edit (edit_string);
849 ok = bbt_validate_edit (edit_string);
853 ok = minsec_validate_edit (edit_string);
861 edit_string = pre_edit_string;
862 input_string.clear ();
863 _layout->set_text (edit_string);
864 show_edit_status (0);
865 /* edit attributes remain in use */
870 framecnt_t frames = parse_as_distance (input_string);
875 _layout->set_attributes (normal_attributes);
879 set (current_time() + frames, true);
881 framepos_t c = current_time();
883 if (c > frames || _negative_allowed) {
884 set (c - frames, true);
889 ValueChanged (); /* EMIT SIGNAL */
892 input_string.clear ();
898 AudioClock::session_property_changed (const PropertyChange&)
900 set (last_when, true);
904 AudioClock::session_configuration_changed (std::string p)
906 if (_negative_allowed) {
907 /* session option editor clock */
911 if (p == "sync-source" || p == "external-sync") {
912 set (current_time(), true);
916 if (p != "timecode-offset" && p != "timecode-offset-negative") {
925 current = current_duration ();
927 current = current_time ();
937 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
939 if ((!force && !is_visible()) || _session == 0) {
944 when = when - offset;
947 if (when == last_when && !force) {
948 if (_mode != Timecode && _mode != MinSec) {
949 /* may need to force display of TC source
950 * time, so don't return early.
958 _right_layout->set_alignment(Pango::ALIGN_LEFT);
964 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
966 set_timecode (when, force);
970 set_bbt (when, force);
975 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
977 set_minsec (when, force);
981 set_frames (when, force);
991 AudioClock::set_slave_info ()
993 if (!_left_layout || !_right_layout) {
997 SyncSource sync_src = Config->get_sync_source();
999 if (_session->config.get_external_sync()) {
1000 Slave* slave = _session->slave();
1004 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1005 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1006 _right_layout->set_text ("");
1010 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1011 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1012 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1013 INFO_FONT_SIZE, slave->approximate_current_delta()));
1015 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1016 INFO_FONT_SIZE, _("--pending--")));
1017 _right_layout->set_text ("");
1024 TimecodeSlave* tcslave;
1025 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1026 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1027 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1028 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1029 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1030 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1031 INFO_FONT_SIZE, slave->approximate_current_delta()));
1034 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1035 INFO_FONT_SIZE, _("--pending--")));
1036 _right_layout->set_text ("");
1041 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1042 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1043 _right_layout->set_text ("");
1048 AudioClock::set_frames (framepos_t when, bool /*force*/)
1051 bool negative = false;
1054 _layout->set_text (" ----------");
1057 _left_layout->set_text ("");
1058 _right_layout->set_text ("");
1070 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1072 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1075 _layout->set_text (buf);
1078 framecnt_t rate = _session->frame_rate();
1080 if (fmod (rate, 100.0) == 0.0) {
1081 sprintf (buf, "%.1fkHz", rate/1000.0);
1083 sprintf (buf, "%" PRId64 "Hz", rate);
1086 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1087 INFO_FONT_SIZE, _("SR"), buf));
1089 float vid_pullup = _session->config.get_video_pullup();
1091 if (vid_pullup == 0.0) {
1092 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1093 INFO_FONT_SIZE, _("Pull")));
1095 sprintf (buf, _("%+.4f%%"), vid_pullup);
1096 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1097 INFO_FONT_SIZE, _("Pull"), buf));
1103 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1111 bool negative = false;
1114 _layout->set_text (" --:--:--.---");
1117 _left_layout->set_text ("");
1118 _right_layout->set_text ("");
1130 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1131 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1132 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1133 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1134 secs = (int) floor (left / (float) _session->frame_rate());
1135 left -= (framecnt_t) floor ((double)(secs * _session->frame_rate()));
1136 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1139 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1141 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1144 _layout->set_text (buf);
1149 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1152 bool negative = false;
1155 _layout->set_text (" --:--:--:--");
1157 _left_layout->set_text ("");
1158 _right_layout->set_text ("");
1170 _session->timecode_duration (when, TC);
1172 _session->timecode_time (when, TC);
1175 TC.negative = TC.negative || negative;
1177 _layout->set_text (Timecode::timecode_format_time(TC));
1183 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1186 Timecode::BBT_Time BBT;
1187 bool negative = false;
1190 _layout->set_text (" ---|--|----");
1192 _left_layout->set_text ("");
1193 _right_layout->set_text ("");
1203 /* handle a common case */
1210 _session->tempo_map().bbt_time (when, BBT);
1215 _session->tempo_map().bbt_time (when, BBT);
1219 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1220 BBT.bars, BBT.beats, BBT.ticks);
1222 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1223 BBT.bars, BBT.beats, BBT.ticks);
1226 _layout->set_text (buf);
1228 if (_right_layout) {
1231 if (bbt_reference_time < 0) {
1234 pos = bbt_reference_time;
1237 TempoMetric m (_session->tempo_map().metric_at (pos));
1239 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1240 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1241 INFO_FONT_SIZE, buf, _("Tempo")));
1243 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1244 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1245 INFO_FONT_SIZE, buf, _("Meter")));
1250 AudioClock::set_session (Session *s)
1252 SessionHandlePtr::set_session (s);
1256 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1257 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1259 const XMLProperty* prop;
1260 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1261 AudioClock::Mode amode;
1264 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1265 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1267 if ((prop = (*i)->property (X_("mode"))) != 0) {
1268 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1271 if ((prop = (*i)->property (X_("on"))) != 0) {
1272 set_off (!string_is_affirmative (prop->value()));
1279 set (last_when, true);
1284 AudioClock::on_key_press_event (GdkEventKey* ev)
1292 int highlight_length;
1295 switch (ev->keyval) {
1338 case GDK_KP_Subtract:
1339 if (_negative_allowed && input_string.empty()) {
1340 edit_is_negative = true;
1341 edit_string.replace(0,1,"-");
1342 _layout->set_text (edit_string);
1345 end_edit_relative (false);
1351 end_edit_relative (true);
1364 ChangeAborted(); /* EMIT SIGNAL */
1369 if (!input_string.empty()) {
1370 /* delete the last key entered
1372 input_string = input_string.substr (0, input_string.length() - 1);
1374 goto use_input_string;
1380 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1381 /* too many digits: eat the key event, but do nothing with it */
1385 input_string.push_back (new_char);
1391 /* get this one in the right order, and to the right width */
1392 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1393 edit_string = edit_string.substr (0, edit_string.length() - 1);
1395 edit_string.push_back (new_char);
1397 if (!edit_string.empty()) {
1399 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1400 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1403 /* highlight the whole thing */
1404 highlight_length = edit_string.length();
1408 highlight_length = merge_input_and_edit_string ();
1411 if (edit_is_negative) {
1412 edit_string.replace(0,1,"-");
1414 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1415 edit_string.replace(0,1,"_");
1417 edit_string.replace(0,1," ");
1421 show_edit_status (highlight_length);
1422 _layout->set_text (edit_string);
1429 AudioClock::merge_input_and_edit_string ()
1431 /* merge with pre-edit-string into edit string */
1433 edit_string = pre_edit_string;
1435 if (input_string.empty()) {
1439 string::size_type target;
1440 for (string::size_type i = 0; i < input_string.length(); ++i) {
1441 target = insert_map[input_string.length() - 1 - i];
1442 edit_string[target] = input_string[i];
1444 /* highlight from end to wherever the last character was added */
1445 return edit_string.length() - insert_map[input_string.length()-1];
1450 AudioClock::on_key_release_event (GdkEventKey *ev)
1456 /* return true for keys that we used on press
1457 so that they cannot possibly do double-duty
1459 switch (ev->keyval) {
1482 case GDK_KP_Decimal:
1490 case GDK_KP_Subtract:
1498 AudioClock::index_to_field (int index) const
1503 return Timecode_Hours;
1504 } else if (index < 7) {
1505 return Timecode_Minutes;
1506 } else if (index < 10) {
1507 return Timecode_Seconds;
1509 return Timecode_Frames;
1515 } else if (index < 7) {
1523 return Timecode_Hours;
1524 } else if (index < 6) {
1526 } else if (index < 9) {
1529 return MS_Milliseconds;
1541 AudioClock::on_button_press_event (GdkEventButton *ev)
1543 switch (ev->button) {
1545 if (editable && !_off) {
1551 /* the text has been centered vertically, so adjust
1554 int xcenter = (get_width() - layout_width) /2;
1556 y = ev->y - ((upper_height - layout_height)/2);
1557 x = ev->x - xcenter;
1559 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1560 /* pretend it is a character on the far right */
1563 drag_field = index_to_field (index);
1565 /* make absolutely sure that the pointer is grabbed */
1566 gdk_pointer_grab(ev->window,false ,
1567 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1568 NULL,NULL,ev->time);
1570 drag_start_y = ev->y;
1584 AudioClock::on_button_release_event (GdkEventButton *ev)
1586 if (editable && !_off) {
1588 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1590 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1591 // we actually dragged so return without
1592 // setting editing focus, or we shift clicked
1595 if (ev->button == 1) {
1597 if (_edit_by_click_field) {
1599 int xcenter = (get_width() - layout_width) /2;
1602 int y = ev->y - ((upper_height - layout_height)/2);
1603 int x = ev->x - xcenter;
1606 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1610 f = index_to_field (index);
1613 case Timecode_Frames:
1614 case MS_Milliseconds:
1630 if (Keyboard::is_context_menu_event (ev)) {
1631 if (ops_menu == 0) {
1634 ops_menu->popup (1, ev->time);
1642 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1644 bool ret = CairoWidget::on_focus_out_event (ev);
1654 AudioClock::on_scroll_event (GdkEventScroll *ev)
1659 if (editing || _session == 0 || !editable || _off) {
1666 /* the text has been centered vertically, so adjust
1670 int xcenter = (get_width() - layout_width) /2;
1671 y = ev->y - ((upper_height - layout_height)/2);
1672 x = ev->x - xcenter;
1674 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1675 /* not in the main layout */
1679 Field f = index_to_field (index);
1680 framepos_t frames = 0;
1682 switch (ev->direction) {
1685 frames = get_frame_step (f);
1687 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1690 set (current_time() + frames, true);
1691 ValueChanged (); /* EMIT_SIGNAL */
1695 case GDK_SCROLL_DOWN:
1696 frames = get_frame_step (f);
1698 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1702 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1705 set (current_time() - frames, true);
1708 ValueChanged (); /* EMIT_SIGNAL */
1721 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1723 if (editing || _session == 0 || !dragging) {
1727 float pixel_frame_scale_factor = 0.2f;
1729 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1730 pixel_frame_scale_factor = 0.1f;
1734 if (Keyboard::modifier_state_contains (ev->state,
1735 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1737 pixel_frame_scale_factor = 0.025f;
1740 double y_delta = ev->y - drag_y;
1742 drag_accum += y_delta*pixel_frame_scale_factor;
1746 if (floor (drag_accum) != 0) {
1751 dir = (drag_accum < 0 ? 1:-1);
1752 pos = current_time();
1753 frames = get_frame_step (drag_field, pos, dir);
1755 if (frames != 0 && frames * drag_accum < current_time()) {
1756 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1762 ValueChanged(); /* EMIT_SIGNAL */
1769 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1772 Timecode::BBT_Time BBT;
1774 case Timecode_Hours:
1775 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1777 case Timecode_Minutes:
1778 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1780 case Timecode_Seconds:
1781 f = _session->frame_rate();
1783 case Timecode_Frames:
1784 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1792 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1795 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1798 f = (framecnt_t) _session->frame_rate();
1800 case MS_Milliseconds:
1801 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1808 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1814 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1820 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1823 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1832 AudioClock::current_time (framepos_t) const
1838 AudioClock::current_duration (framepos_t pos) const
1847 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1863 AudioClock::bbt_validate_edit (const string& str)
1867 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1871 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1875 if (!is_duration && any.bbt.bars == 0) {
1879 if (!is_duration && any.bbt.beats == 0) {
1887 AudioClock::timecode_validate_edit (const string&)
1893 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1894 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1899 TC.hours = hours * -1;
1903 TC.negative = false;
1906 if (TC.negative && !_negative_allowed) {
1910 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1914 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1918 if (_session->timecode_drop_frames()) {
1919 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1928 AudioClock::minsec_validate_edit (const string& str)
1930 int hrs, mins, secs, millisecs;
1932 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1936 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1944 AudioClock::frames_from_timecode_string (const string& str) const
1946 if (_session == 0) {
1955 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1956 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1959 TC.hours = abs(hours);
1960 TC.rate = _session->timecode_frames_per_second();
1961 TC.drop= _session->timecode_drop_frames();
1963 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1965 // timecode_tester ();
1966 if (edit_is_negative) {
1974 AudioClock::frames_from_minsec_string (const string& str) const
1976 if (_session == 0) {
1980 int hrs, mins, secs, millisecs;
1981 framecnt_t sr = _session->frame_rate();
1983 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1984 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1988 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1992 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1994 if (_session == 0) {
1995 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2000 any.type = AnyTime::BBT;
2002 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2009 return _session->any_duration_to_frames (pos, any);
2011 return _session->convert_to_frames (any);
2017 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2019 if (_session == 0) {
2020 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2024 Timecode::BBT_Time bbt;
2026 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2030 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2034 AudioClock::frames_from_audioframes_string (const string& str) const
2037 sscanf (str.c_str(), "%" PRId64, &f);
2042 AudioClock::build_ops_menu ()
2044 using namespace Menu_Helpers;
2045 ops_menu = new Menu;
2046 MenuList& ops_items = ops_menu->items();
2047 ops_menu->set_name ("ArdourContextMenu");
2049 if (!Profile->get_sae()) {
2050 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2052 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2053 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2054 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2056 if (editable && !_off && !is_duration && !_follows_playhead) {
2057 ops_items.push_back (SeparatorElem());
2058 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2059 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2064 AudioClock::set_from_playhead ()
2070 set (_session->transport_frame());
2075 AudioClock::locate ()
2077 if (!_session || is_duration) {
2081 _session->request_locate (current_time(), _session->transport_rolling ());
2085 AudioClock::set_mode (Mode m)
2095 _layout->set_text ("");
2099 _left_layout->set_attributes (info_attributes);
2100 _right_layout->set_attributes (info_attributes);
2101 /* adjust info_height according to font size */
2103 _left_layout->set_text (" 1234567890");
2104 _left_layout->get_pixel_size (ignored, info_height);
2106 _left_layout->set_text ("");
2107 _right_layout->set_text ("");
2112 mode_based_info_ratio = 0.6;
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;
2125 insert_map.push_back (11);
2126 insert_map.push_back (10);
2127 insert_map.push_back (9);
2128 insert_map.push_back (8);
2129 insert_map.push_back (6);
2130 insert_map.push_back (5);
2131 insert_map.push_back (3);
2132 insert_map.push_back (2);
2133 insert_map.push_back (1);
2137 mode_based_info_ratio = 0.6;
2138 insert_map.push_back (12);
2139 insert_map.push_back (11);
2140 insert_map.push_back (10);
2141 insert_map.push_back (8);
2142 insert_map.push_back (7);
2143 insert_map.push_back (5);
2144 insert_map.push_back (4);
2145 insert_map.push_back (2);
2146 insert_map.push_back (1);
2150 mode_based_info_ratio = 0.45;
2154 set (last_when, true);
2156 if (!is_transient) {
2157 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2160 mode_changed (); /* EMIT SIGNAL (the member one) */
2164 AudioClock::set_bbt_reference (framepos_t pos)
2166 bbt_reference_time = pos;
2170 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2172 CairoWidget::on_style_changed (old_style);
2174 Gtk::Requisition req;
2175 set_clock_dimensions (req);
2182 AudioClock::set_editable (bool yn)
2188 AudioClock::set_is_duration (bool yn)
2190 if (yn == is_duration) {
2195 set (last_when, true);
2199 AudioClock::set_off (bool yn)
2207 /* force a redraw. last_when will be preserved, but the clock text will
2211 set (last_when, true);
2215 AudioClock::focus ()
2217 start_edit (Field (0));
2221 AudioClock::set_corner_radius (double r)
2230 AudioClock::dpi_reset ()
2232 /* force recomputation of size even if we are fixed width
2240 AudioClock::set_negative_allowed (bool yn)
2242 _negative_allowed = yn;