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/lmath.h"
35 #include "ardour/session.h"
36 #include "ardour/slave.h"
37 #include "ardour/tempo.h"
38 #include "ardour/types.h"
40 #include "audio_clock.h"
43 #include "gui_thread.h"
44 #include "ui_config.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.68;
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,
68 bool accept_on_focus_out)
71 , is_transient (transient)
72 , is_duration (duration)
73 , editable (allow_edit)
74 , _follows_playhead (follows_playhead)
75 , _accept_on_focus_out (accept_on_focus_out)
78 , _edit_by_click_field (false)
79 , _negative_allowed (false)
80 , edit_is_negative (false)
85 , style_resets_first (true)
90 , mode_based_info_ratio (1.0)
94 , bbt_reference_time (-1)
99 , drag_field (Field (0))
103 set_flags (CAN_FOCUS);
105 _layout = Pango::Layout::create (get_pango_context());
106 _layout->set_attributes (normal_attributes);
109 _left_layout = Pango::Layout::create (get_pango_context());
110 _right_layout = Pango::Layout::create (get_pango_context());
113 set_widget_name (widget_name);
115 _mode = BBT; /* lie to force mode switch */
117 set (last_when, true);
120 clocks.push_back (this);
123 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
124 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
127 AudioClock::~AudioClock ()
129 delete foreground_attr;
134 AudioClock::set_widget_name (const string& str)
139 set_name (str + " clock");
149 AudioClock::on_realize ()
151 Gtk::Requisition req;
153 CairoWidget::on_realize ();
155 set_clock_dimensions (req);
157 first_width = req.width;
158 first_height = req.height;
160 // XXX FIX ME: define font based on ... ???
166 AudioClock::set_font (Pango::FontDescription font)
168 Glib::RefPtr<Gtk::Style> style = get_style ();
169 Pango::AttrFontDesc* font_attr;
171 font_size = font.get_size();
172 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
174 normal_attributes.change (*font_attr);
175 editing_attributes.change (*font_attr);
177 /* now a smaller version of the same font */
180 font.set_size (INFO_FONT_SIZE);
181 font.set_weight (Pango::WEIGHT_NORMAL);
182 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
184 info_attributes.change (*font_attr);
186 /* and an even smaller one */
190 /* get the figure width for the font. This doesn't have to super
191 * accurate since we only use it to measure the (roughly 1 character)
192 * offset from the position Pango tells us for the "cursor"
195 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
199 tmp->get_pixel_size (em_width, ignore_height);
202 /* force redraw of markup with new font-size */
203 set (last_when, true);
207 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
209 CairoWidget::set_active_state (s);
214 AudioClock::set_colors ()
220 uint32_t editing_color;
221 uint32_t cursor_color;
223 if (active_state()) {
224 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
225 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
226 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
227 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
229 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
230 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
231 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
232 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
235 /* store for bg and cursor in render() */
237 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
244 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
251 /* rescale for Pango colors ... sigh */
253 r = lrint (r * 65535.0);
254 g = lrint (g * 65535.0);
255 b = lrint (b * 65535.0);
257 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
258 r = lrint ((r/255.0) * 65535.0);
259 g = lrint ((g/255.0) * 65535.0);
260 b = lrint ((b/255.0) * 65535.0);
261 delete foreground_attr;
262 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
264 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
265 r = lrint ((r/255.0) * 65535.0);
266 g = lrint ((g/255.0) * 65535.0);
267 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::set_scale (double x, double y)
295 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
297 /* main layout: rounded rect, plus the text */
300 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
303 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
305 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
308 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
313 double lw = layout_width * xscale;
314 double lh = layout_height * yscale;
316 if (lw >= get_width()) {
317 cairo_move_to (cr, 0.0, (upper_height - lh) / 2.0);
319 cairo_move_to (cr, (get_width() - lw) / 2.0, (upper_height - lh) / 2.0);
322 if (xscale != 1.0 || yscale != 1.0) {
324 cairo_scale (cr, xscale, yscale);
327 pango_cairo_show_layout (cr, _layout->gobj());
329 if (xscale != 1.0 || yscale != 1.0) {
335 double h = get_height() - upper_height - separator_height;
338 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
341 if (mode_based_info_ratio != 1.0) {
343 double left_rect_width = get_left_rect_width();
347 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
348 left_rect_width + (separator_height == 0 ? corner_radius : 0),
351 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
356 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
357 pango_cairo_show_layout (cr, _left_layout->gobj());
361 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
362 upper_height + separator_height,
363 get_width() - separator_height - left_rect_width,
366 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
367 get_width() - separator_height - left_rect_width, h);
373 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
374 /* right-align does not work per se beacuse layout width is unset.
375 * Using _right_layout->set_width([value >=0]) would also enable
376 * word-wrapping which is not wanted here.
377 * The solution is to custom align the layout depending on its size.
378 * if it is larger than the available space it will be cropped on the
379 * right edge rather than override text on the left side.
382 _right_layout->get_pixel_size(rw, rh);
383 x = get_width() - rw - separator_height - x_leading_padding;
384 if (x < x_leading_padding + left_rect_width + separator_height) {
385 /* rather cut off the right end than overlap with the text on the left */
386 x = x_leading_padding + left_rect_width + separator_height;
388 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
390 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
392 pango_cairo_show_layout (cr, _right_layout->gobj());
395 /* no info to display, or just one */
399 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
401 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
409 Pango::Rectangle cursor;
411 if (!insert_map.empty()) {
413 if (input_string.length() < insert_map.size()) {
415 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
417 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
420 cursor.get_x()/PANGO_SCALE,
421 (upper_height - layout_height)/2.0,
423 cursor.get_height()/PANGO_SCALE);
427 /* we've entered all possible digits, no cursor */
431 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
433 if (edit_string.empty()) {
435 get_width() - em_width,
436 (upper_height - layout_height)/2.0,
437 em_width, upper_height);
439 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
441 cursor.get_x()/PANGO_SCALE,
442 (upper_height - layout_height)/2.0,
443 em_width, upper_height);
452 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
454 CairoWidget::on_size_allocate (alloc);
457 upper_height = (get_height()/2.0) - 1.0;
459 upper_height = get_height();
464 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
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);
480 /* this string is the longest thing we will ever display */
482 tmp->set_text (" 88:88:88,888 ");
484 tmp->set_text (" 88:88:88,88 ");
485 tmp->get_pixel_size (req.width, req.height);
488 layout_height = req.height;
489 layout_width = req.width;
491 /* get the figure width for the font. This doesn't have to super
492 * accurate since we only use it to measure the (roughly 1 character)
493 * offset from the position Pango tells us for the "cursor"
499 tmp->get_pixel_size (em_width, ignore_height);
503 AudioClock::on_size_request (Gtk::Requisition* req)
505 /* even for non fixed width clocks, the size we *ask* for never changes,
506 even though the size we receive might. so once we've computed it,
511 req->width = first_width;
512 req->height = first_height;
516 set_clock_dimensions (*req);
518 /* now tackle height, for which we need to know the height of the lower
524 Glib::RefPtr<Pango::Layout> tmp;
525 Glib::RefPtr<Gtk::Style> style = get_style ();
526 Pango::FontDescription font;
529 tmp = Pango::Layout::create (get_pango_context());
531 if (!is_realized()) {
532 font = get_font_for_style (get_name());
534 font = style->get_font();
537 tmp->set_font_description (font);
539 font.set_size (INFO_FONT_SIZE);
540 font.set_weight (Pango::WEIGHT_NORMAL);
541 tmp->set_font_description (font);
543 /* we only care about height, so put as much stuff in here
544 as possible that might change the height.
546 tmp->set_text ("qyhH|"); /* one ascender, one descender */
548 tmp->get_pixel_size (w, info_height);
550 /* silly extra padding that seems necessary to correct the info
551 * that pango just gave us. I have no idea why.
554 req->height += info_height;
555 req->height += separator_height;
560 AudioClock::show_edit_status (int length)
562 editing_attr->set_start_index (edit_string.length() - length);
563 editing_attr->set_end_index (edit_string.length());
565 editing_attributes.change (*foreground_attr);
566 editing_attributes.change (*editing_attr);
568 _layout->set_attributes (editing_attributes);
572 AudioClock::start_edit (Field f)
575 pre_edit_string = _layout->get_text ();
576 if (!insert_map.empty()) {
577 edit_string = pre_edit_string;
579 edit_string.clear ();
580 _layout->set_text ("");
583 input_string.clear ();
585 edit_is_negative = false;
588 input_string = get_field (f);
589 show_edit_status (merge_input_and_edit_string ());
590 _layout->set_text (edit_string);
595 Keyboard::magic_widget_grab_focus ();
601 AudioClock::get_field (Field f)
605 return edit_string.substr (1, 2);
607 case Timecode_Minutes:
608 return edit_string.substr (4, 2);
610 case Timecode_Seconds:
611 return edit_string.substr (7, 2);
613 case Timecode_Frames:
614 return edit_string.substr (10, 2);
617 return edit_string.substr (1, 2);
620 return edit_string.substr (4, 2);
623 return edit_string.substr (7, 2);
625 case MS_Milliseconds:
626 return edit_string.substr (10, 3);
629 return edit_string.substr (1, 3);
632 return edit_string.substr (5, 2);
635 return edit_string.substr (8, 4);
645 AudioClock::end_edit (bool modify)
653 ok = timecode_validate_edit (edit_string);
657 ok = bbt_validate_edit (edit_string);
661 ok = minsec_validate_edit (edit_string);
665 if (edit_string.length() < 1) {
666 edit_string = pre_edit_string;
672 edit_string = pre_edit_string;
673 input_string.clear ();
674 _layout->set_text (edit_string);
675 show_edit_status (0);
676 /* edit attributes remain in use */
680 framepos_t pos = 0; /* stupid gcc */
684 pos = frames_from_timecode_string (edit_string);
689 pos = frame_duration_from_bbt_string (0, edit_string);
691 pos = frames_from_bbt_string (0, edit_string);
696 pos = frames_from_minsec_string (edit_string);
700 pos = frames_from_audioframes_string (edit_string);
705 _layout->set_attributes (normal_attributes);
706 ValueChanged(); /* EMIT_SIGNAL */
712 edit_is_negative = false;
713 _layout->set_attributes (normal_attributes);
714 _layout->set_text (pre_edit_string);
725 AudioClock::drop_focus ()
727 Keyboard::magic_widget_drop_focus ();
731 /* move focus back to the default widget in the top level window */
733 Widget* top = get_toplevel();
735 if (top->is_toplevel ()) {
736 Window* win = dynamic_cast<Window*> (top);
743 AudioClock::parse_as_frames_distance (const std::string& str)
747 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
755 AudioClock::parse_as_minsec_distance (const std::string& str)
757 framecnt_t sr = _session->frame_rate();
763 switch (str.length()) {
770 sscanf (str.c_str(), "%" PRId32, &msecs);
771 return msecs * (sr / 1000);
774 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
775 return (secs * sr) + (msecs * (sr/1000));
778 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
779 return (secs * sr) + (msecs * (sr/1000));
782 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
783 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
786 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
787 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
790 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
791 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
794 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
795 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
805 AudioClock::parse_as_timecode_distance (const std::string& str)
807 double fps = _session->timecode_frames_per_second();
808 framecnt_t sr = _session->frame_rate();
814 switch (str.length()) {
819 sscanf (str.c_str(), "%" PRId32, &frames);
820 return llrint ((frames/(float)fps) * sr);
823 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
824 return (secs * sr) + llrint ((frames/(float)fps) * sr);
827 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
828 return (secs * sr) + llrint ((frames/(float)fps) * sr);
831 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
832 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
835 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
836 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
839 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
840 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
843 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
844 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
854 AudioClock::parse_as_bbt_distance (const std::string&)
860 AudioClock::parse_as_distance (const std::string& instr)
864 return parse_as_timecode_distance (instr);
867 return parse_as_frames_distance (instr);
870 return parse_as_bbt_distance (instr);
873 return parse_as_minsec_distance (instr);
880 AudioClock::end_edit_relative (bool add)
886 ok = timecode_validate_edit (edit_string);
890 ok = bbt_validate_edit (edit_string);
894 ok = minsec_validate_edit (edit_string);
902 edit_string = pre_edit_string;
903 input_string.clear ();
904 _layout->set_text (edit_string);
905 show_edit_status (0);
906 /* edit attributes remain in use */
911 framecnt_t frames = parse_as_distance (input_string);
916 _layout->set_attributes (normal_attributes);
920 set (current_time() + frames, true);
922 framepos_t c = current_time();
924 if (c > frames || _negative_allowed) {
925 set (c - frames, true);
930 ValueChanged (); /* EMIT SIGNAL */
933 input_string.clear ();
939 AudioClock::session_property_changed (const PropertyChange&)
941 set (last_when, true);
945 AudioClock::session_configuration_changed (std::string p)
947 if (_negative_allowed) {
948 /* session option editor clock */
952 if (p == "sync-source" || p == "external-sync") {
953 set (current_time(), true);
957 if (p != "timecode-offset" && p != "timecode-offset-negative") {
966 current = current_duration ();
968 current = current_time ();
978 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
980 if ((!force && !is_visible()) || _session == 0) {
985 when = when - offset;
988 if (when == last_when && !force) {
989 #if 0 // XXX return if no change and no change forced. verify Aug/2014
990 if (_mode != Timecode && _mode != MinSec) {
991 /* may need to force display of TC source
992 * time, so don't return early.
994 /* ^^ Why was that?, delta times?
995 * Timecode FPS, pull-up/down, etc changes
996 * trigger a 'session_property_changed' which
997 * eventually calls set(last_when, true)
999 * re-rendering the clock every 40ms or so just
1000 * because we can is not ideal.
1010 if (_right_layout) {
1011 _right_layout->set_alignment(Pango::ALIGN_LEFT);
1016 if (_right_layout) {
1017 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1019 set_timecode (when, force);
1023 set_bbt (when, force);
1027 if (_right_layout) {
1028 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1030 set_minsec (when, force);
1034 set_frames (when, force);
1044 AudioClock::set_slave_info ()
1046 if (!_left_layout || !_right_layout) {
1050 SyncSource sync_src = Config->get_sync_source();
1052 if (_session->config.get_external_sync()) {
1053 Slave* slave = _session->slave();
1057 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1058 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1059 _right_layout->set_text ("");
1063 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1064 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1065 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1066 INFO_FONT_SIZE, slave->approximate_current_delta()));
1068 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1069 INFO_FONT_SIZE, _("--pending--")));
1070 _right_layout->set_text ("");
1077 TimecodeSlave* tcslave;
1078 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1079 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1080 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span><span foreground=\"%3\">%4</span></span>",
1081 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1082 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1083 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1084 INFO_FONT_SIZE, slave->approximate_current_delta()));
1087 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2</span></span>",
1088 INFO_FONT_SIZE, _("--pending--")));
1089 _right_layout->set_text ("");
1094 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "INT/%2</span></span>",
1095 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1096 _right_layout->set_text ("");
1101 AudioClock::set_frames (framepos_t when, bool /*force*/)
1104 bool negative = false;
1107 _layout->set_text (" ----------");
1110 _left_layout->set_text ("");
1111 _right_layout->set_text ("");
1123 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1125 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1128 _layout->set_text (buf);
1131 framecnt_t rate = _session->frame_rate();
1133 if (fmod (rate, 100.0) == 0.0) {
1134 sprintf (buf, "%.1fkHz", rate/1000.0);
1136 sprintf (buf, "%" PRId64 "Hz", rate);
1139 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1140 INFO_FONT_SIZE, _("SR"), buf));
1142 float vid_pullup = _session->config.get_video_pullup();
1144 if (vid_pullup == 0.0) {
1145 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">off</span></span>",
1146 INFO_FONT_SIZE, _("Pull")));
1148 sprintf (buf, _("%+.4f%%"), vid_pullup);
1149 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%2 </span><span foreground=\"green\">%3</span></span>",
1150 INFO_FONT_SIZE, _("Pull"), buf));
1156 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1173 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1174 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1175 mins = (int) floor (left / (frame_rate * 60.0f));
1176 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1177 secs = (int) floor (left / (float) frame_rate);
1178 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1179 millisecs = floor (left * 1000.0 / (float) frame_rate);
1182 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1184 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1190 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1195 _layout->set_text (" --:--:--.---");
1198 _left_layout->set_text ("");
1199 _right_layout->set_text ("");
1205 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1207 _layout->set_text (buf);
1212 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1215 bool negative = false;
1218 _layout->set_text (" --:--:--:--");
1220 _left_layout->set_text ("");
1221 _right_layout->set_text ("");
1233 _session->timecode_duration (when, TC);
1235 _session->timecode_time (when, TC);
1238 TC.negative = TC.negative || negative;
1240 _layout->set_text (Timecode::timecode_format_time(TC));
1246 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1249 Timecode::BBT_Time BBT;
1250 bool negative = false;
1253 _layout->set_text (" ---|--|----");
1255 _left_layout->set_text ("");
1256 _right_layout->set_text ("");
1266 /* handle a common case */
1273 _session->tempo_map().bbt_time (when, BBT);
1278 _session->tempo_map().bbt_time (when, BBT);
1282 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1283 BBT.bars, BBT.beats, BBT.ticks);
1285 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1286 BBT.bars, BBT.beats, BBT.ticks);
1289 _layout->set_text (buf);
1291 if (_right_layout) {
1294 if (bbt_reference_time < 0) {
1297 pos = bbt_reference_time;
1300 TempoMetric m (_session->tempo_map().metric_at (pos));
1302 sprintf (buf, "%-5.1f", m.tempo().beats_per_minute());
1303 _left_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1304 INFO_FONT_SIZE, buf, _("Tempo")));
1306 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1307 _right_layout->set_markup (string_compose ("<span size=\"%1\">" TXTSPAN "%3</span> <span foreground=\"green\">%2</span></span>",
1308 INFO_FONT_SIZE, buf, _("Meter")));
1313 AudioClock::set_session (Session *s)
1315 SessionHandlePtr::set_session (s);
1319 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1320 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1322 const XMLProperty* prop;
1323 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1324 AudioClock::Mode amode;
1327 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1328 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1330 if ((prop = (*i)->property (X_("mode"))) != 0) {
1331 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1334 if ((prop = (*i)->property (X_("on"))) != 0) {
1335 set_off (!string_is_affirmative (prop->value()));
1342 set (last_when, true);
1347 AudioClock::on_key_press_event (GdkEventKey* ev)
1355 int highlight_length;
1358 switch (ev->keyval) {
1401 case GDK_KP_Subtract:
1402 if (_negative_allowed && input_string.empty()) {
1403 edit_is_negative = true;
1404 edit_string.replace(0,1,"-");
1405 _layout->set_text (edit_string);
1408 end_edit_relative (false);
1414 end_edit_relative (true);
1427 ChangeAborted(); /* EMIT SIGNAL */
1432 if (!input_string.empty()) {
1433 /* delete the last key entered
1435 input_string = input_string.substr (0, input_string.length() - 1);
1437 goto use_input_string;
1440 /* do not allow other keys to passthru to the rest of the GUI
1446 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1447 /* too many digits: eat the key event, but do nothing with it */
1451 input_string.push_back (new_char);
1457 /* get this one in the right order, and to the right width */
1458 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1459 edit_string = edit_string.substr (0, edit_string.length() - 1);
1461 edit_string.push_back (new_char);
1463 if (!edit_string.empty()) {
1465 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1466 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1469 /* highlight the whole thing */
1470 highlight_length = edit_string.length();
1474 highlight_length = merge_input_and_edit_string ();
1477 if (edit_is_negative) {
1478 edit_string.replace(0,1,"-");
1480 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1481 edit_string.replace(0,1,"_");
1483 edit_string.replace(0,1," ");
1487 show_edit_status (highlight_length);
1488 _layout->set_text (edit_string);
1495 AudioClock::merge_input_and_edit_string ()
1497 /* merge with pre-edit-string into edit string */
1499 edit_string = pre_edit_string;
1501 if (input_string.empty()) {
1505 string::size_type target;
1506 for (string::size_type i = 0; i < input_string.length(); ++i) {
1507 target = insert_map[input_string.length() - 1 - i];
1508 edit_string[target] = input_string[i];
1510 /* highlight from end to wherever the last character was added */
1511 return edit_string.length() - insert_map[input_string.length()-1];
1516 AudioClock::on_key_release_event (GdkEventKey *ev)
1522 /* return true for keys that we used on press
1523 so that they cannot possibly do double-duty
1525 switch (ev->keyval) {
1548 case GDK_KP_Decimal:
1556 case GDK_KP_Subtract:
1564 AudioClock::index_to_field (int index) const
1569 return Timecode_Hours;
1570 } else if (index < 7) {
1571 return Timecode_Minutes;
1572 } else if (index < 10) {
1573 return Timecode_Seconds;
1575 return Timecode_Frames;
1581 } else if (index < 7) {
1589 return Timecode_Hours;
1590 } else if (index < 6) {
1592 } else if (index < 9) {
1595 return MS_Milliseconds;
1607 AudioClock::on_button_press_event (GdkEventButton *ev)
1609 switch (ev->button) {
1611 if (editable && !_off) {
1617 /* the text has been centered vertically, so adjust
1620 int xcenter = (get_width() - layout_width) /2;
1622 y = ev->y - ((upper_height - layout_height)/2);
1623 x = ev->x - xcenter;
1625 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1626 /* pretend it is a character on the far right */
1629 drag_field = index_to_field (index);
1631 /* make absolutely sure that the pointer is grabbed */
1632 gdk_pointer_grab(ev->window,false ,
1633 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1634 NULL,NULL,ev->time);
1636 drag_start_y = ev->y;
1650 AudioClock::on_button_release_event (GdkEventButton *ev)
1652 if (editable && !_off) {
1654 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1656 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1657 // we actually dragged so return without
1658 // setting editing focus, or we shift clicked
1661 if (ev->button == 1) {
1663 if (_edit_by_click_field) {
1665 int xcenter = (get_width() - layout_width) /2;
1668 int y = ev->y - ((upper_height - layout_height)/2);
1669 int x = ev->x - xcenter;
1672 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1676 f = index_to_field (index);
1679 case Timecode_Frames:
1680 case MS_Milliseconds:
1696 if (Keyboard::is_context_menu_event (ev)) {
1697 if (ops_menu == 0) {
1700 ops_menu->popup (1, ev->time);
1708 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1710 bool ret = CairoWidget::on_focus_out_event (ev);
1713 end_edit (_accept_on_focus_out);
1720 AudioClock::on_scroll_event (GdkEventScroll *ev)
1725 if (editing || _session == 0 || !editable || _off) {
1732 /* the text has been centered vertically, so adjust
1736 int xcenter = (get_width() - layout_width) /2;
1737 y = ev->y - ((upper_height - layout_height)/2);
1738 x = ev->x - xcenter;
1740 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1741 /* not in the main layout */
1745 Field f = index_to_field (index);
1746 framepos_t frames = 0;
1748 switch (ev->direction) {
1751 frames = get_frame_step (f);
1753 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1756 set (current_time() + frames, true);
1757 ValueChanged (); /* EMIT_SIGNAL */
1761 case GDK_SCROLL_DOWN:
1762 frames = get_frame_step (f);
1764 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1768 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1771 set (current_time() - frames, true);
1774 ValueChanged (); /* EMIT_SIGNAL */
1787 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1789 if (editing || _session == 0 || !dragging) {
1793 float pixel_frame_scale_factor = 0.2f;
1795 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1796 pixel_frame_scale_factor = 0.1f;
1800 if (Keyboard::modifier_state_contains (ev->state,
1801 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1803 pixel_frame_scale_factor = 0.025f;
1806 double y_delta = ev->y - drag_y;
1808 drag_accum += y_delta*pixel_frame_scale_factor;
1812 if (floor (drag_accum) != 0) {
1817 dir = (drag_accum < 0 ? 1:-1);
1818 pos = current_time();
1819 frames = get_frame_step (drag_field, pos, dir);
1821 if (frames != 0 && frames * drag_accum < current_time()) {
1822 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1828 ValueChanged(); /* EMIT_SIGNAL */
1835 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1838 Timecode::BBT_Time BBT;
1840 case Timecode_Hours:
1841 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1843 case Timecode_Minutes:
1844 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1846 case Timecode_Seconds:
1847 f = _session->frame_rate();
1849 case Timecode_Frames:
1850 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1858 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1861 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1864 f = (framecnt_t) _session->frame_rate();
1866 case MS_Milliseconds:
1867 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1874 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1880 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1886 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1889 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1898 AudioClock::current_time (framepos_t) const
1904 AudioClock::current_duration (framepos_t pos) const
1913 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1929 AudioClock::bbt_validate_edit (const string& str)
1933 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1937 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1941 if (!is_duration && any.bbt.bars == 0) {
1945 if (!is_duration && any.bbt.beats == 0) {
1953 AudioClock::timecode_validate_edit (const string&)
1959 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1960 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1965 TC.hours = hours * -1;
1969 TC.negative = false;
1972 if (TC.negative && !_negative_allowed) {
1976 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1980 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1984 if (_session->timecode_drop_frames()) {
1985 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1994 AudioClock::minsec_validate_edit (const string& str)
1996 int hrs, mins, secs, millisecs;
1998 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2002 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
2010 AudioClock::frames_from_timecode_string (const string& str) const
2012 if (_session == 0) {
2021 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2022 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2025 TC.hours = abs(hours);
2026 TC.rate = _session->timecode_frames_per_second();
2027 TC.drop= _session->timecode_drop_frames();
2029 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2031 // timecode_tester ();
2032 if (edit_is_negative) {
2040 AudioClock::frames_from_minsec_string (const string& str) const
2042 if (_session == 0) {
2046 int hrs, mins, secs, millisecs;
2047 framecnt_t sr = _session->frame_rate();
2049 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2050 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2054 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2058 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2060 if (_session == 0) {
2061 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2066 any.type = AnyTime::BBT;
2068 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2075 return _session->any_duration_to_frames (pos, any);
2077 return _session->convert_to_frames (any);
2083 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2085 if (_session == 0) {
2086 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2090 Timecode::BBT_Time bbt;
2092 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2096 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2100 AudioClock::frames_from_audioframes_string (const string& str) const
2103 sscanf (str.c_str(), "%" PRId64, &f);
2108 AudioClock::copy_text_to_clipboard () const
2112 val = pre_edit_string;
2114 val = _layout->get_text ();
2116 const size_t trim = val.find_first_not_of(" ");
2117 if (trim == string::npos) {
2118 assert(0); // empty clock, can't be right.
2121 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2122 cl->set_text (val.substr(trim));
2126 AudioClock::build_ops_menu ()
2128 using namespace Menu_Helpers;
2129 ops_menu = new Menu;
2130 MenuList& ops_items = ops_menu->items();
2131 ops_menu->set_name ("ArdourContextMenu");
2133 if (!Profile->get_sae()) {
2134 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2136 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2137 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2138 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2140 if (editable && !_off && !is_duration && !_follows_playhead) {
2141 ops_items.push_back (SeparatorElem());
2142 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2143 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2145 ops_items.push_back (SeparatorElem());
2146 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2150 AudioClock::set_from_playhead ()
2156 set (_session->transport_frame());
2161 AudioClock::locate ()
2163 if (!_session || is_duration) {
2167 _session->request_locate (current_time(), _session->transport_rolling ());
2171 AudioClock::set_mode (Mode m)
2181 _layout->set_text ("");
2183 Gtk::Requisition req;
2184 set_clock_dimensions (req);
2188 _left_layout->set_attributes (info_attributes);
2189 _right_layout->set_attributes (info_attributes);
2190 /* adjust info_height according to font size */
2192 _left_layout->set_text (" 1234567890");
2193 _left_layout->get_pixel_size (ignored, info_height);
2195 _left_layout->set_text ("");
2196 _right_layout->set_text ("");
2201 mode_based_info_ratio = 0.6;
2202 insert_map.push_back (11);
2203 insert_map.push_back (10);
2204 insert_map.push_back (8);
2205 insert_map.push_back (7);
2206 insert_map.push_back (5);
2207 insert_map.push_back (4);
2208 insert_map.push_back (2);
2209 insert_map.push_back (1);
2213 mode_based_info_ratio = 0.5;
2214 insert_map.push_back (11);
2215 insert_map.push_back (10);
2216 insert_map.push_back (9);
2217 insert_map.push_back (8);
2218 insert_map.push_back (6);
2219 insert_map.push_back (5);
2220 insert_map.push_back (3);
2221 insert_map.push_back (2);
2222 insert_map.push_back (1);
2226 mode_based_info_ratio = 0.6;
2227 insert_map.push_back (12);
2228 insert_map.push_back (11);
2229 insert_map.push_back (10);
2230 insert_map.push_back (8);
2231 insert_map.push_back (7);
2232 insert_map.push_back (5);
2233 insert_map.push_back (4);
2234 insert_map.push_back (2);
2235 insert_map.push_back (1);
2239 mode_based_info_ratio = 0.45;
2243 set (last_when, true);
2245 if (!is_transient) {
2246 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2249 mode_changed (); /* EMIT SIGNAL (the member one) */
2253 AudioClock::set_bbt_reference (framepos_t pos)
2255 bbt_reference_time = pos;
2259 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2261 CairoWidget::on_style_changed (old_style);
2263 Gtk::Requisition req;
2264 set_clock_dimensions (req);
2266 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2272 AudioClock::set_editable (bool yn)
2278 AudioClock::set_is_duration (bool yn)
2280 if (yn == is_duration) {
2285 set (last_when, true);
2289 AudioClock::set_off (bool yn)
2297 /* force a redraw. last_when will be preserved, but the clock text will
2301 set (last_when, true);
2305 AudioClock::focus ()
2307 start_edit (Field (0));
2311 AudioClock::set_corner_radius (double r)
2320 AudioClock::dpi_reset ()
2322 /* force recomputation of size even if we are fixed width
2330 AudioClock::set_negative_allowed (bool yn)
2332 _negative_allowed = yn;