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)
77 , _negative_allowed (false)
78 , edit_is_negative (false)
87 , mode_based_info_ratio (1.0)
91 , bbt_reference_time (-1)
96 , 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 CairoWidget::on_realize ();
153 AudioClock::set_font ()
155 Glib::RefPtr<Gtk::Style> style = get_style ();
156 Pango::FontDescription font;
157 Pango::AttrFontDesc* font_attr;
159 if (!is_realized()) {
160 font = get_font_for_style (get_name());
162 font = style->get_font();
165 font_size = font.get_size();
167 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
169 normal_attributes.change (*font_attr);
170 editing_attributes.change (*font_attr);
172 /* now a smaller version of the same font */
175 font.set_size ((int) lrint (font_size * info_font_scale_factor));
176 font.set_weight (Pango::WEIGHT_NORMAL);
177 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
179 info_attributes.change (*font_attr);
181 /* and an even smaller one */
185 /* get the figure width for the font. This doesn't have to super
186 * accurate since we only use it to measure the (roughly 1 character)
187 * offset from the position Pango tells us for the "cursor"
190 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
194 tmp->get_pixel_size (em_width, ignore_height);
196 /* force redraw of markup with new font-size */
197 set (last_when, true);
201 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
203 CairoWidget::set_active_state (s);
208 AudioClock::set_colors ()
214 uint32_t editing_color;
215 uint32_t cursor_color;
217 if (active_state()) {
218 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
219 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
220 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
221 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
223 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
224 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
225 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
226 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
229 /* store for bg and cursor in render() */
231 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
238 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
245 /* rescale for Pango colors ... sigh */
247 r = lrint (r * 65535.0);
248 g = lrint (g * 65535.0);
249 b = lrint (b * 65535.0);
251 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
252 r = lrint ((r/255.0) * 65535.0);
253 g = lrint ((g/255.0) * 65535.0);
254 b = lrint ((b/255.0) * 65535.0);
255 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
257 UINT_TO_RGBA (editing_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 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
263 normal_attributes.change (*foreground_attr);
264 info_attributes.change (*foreground_attr);
265 editing_attributes.change (*foreground_attr);
266 editing_attributes.change (*editing_attr);
269 _layout->set_attributes (normal_attributes);
271 _layout->set_attributes (editing_attributes);
278 AudioClock::render (cairo_t* cr)
280 /* main layout: rounded rect, plus the text */
283 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
286 Gtkmm2ext::rounded_top_half_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
288 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
291 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
297 cairo_move_to (cr, layout_x_offset, 0);
299 int xcenter = layout_x_offset > 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
300 cairo_move_to (cr, layout_x_offset + xcenter, (upper_height - layout_height) / 2.0);
303 pango_cairo_show_layout (cr, _layout->gobj());
307 double h = get_height() - upper_height - separator_height;
310 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
313 if (mode_based_info_ratio != 1.0) {
315 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
319 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height,
320 left_rect_width + (separator_height == 0 ? corner_radius : 0),
323 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
328 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
329 pango_cairo_show_layout (cr, _left_layout->gobj());
333 Gtkmm2ext::rounded_bottom_half_rectangle (cr, left_rect_width + separator_height,
334 upper_height + separator_height,
335 get_width() - separator_height - left_rect_width,
338 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
339 get_width() - separator_height - left_rect_width, h);
345 if (_right_layout->get_alignment() == Pango::ALIGN_RIGHT) {
346 /* right-align does not work per se beacuse layout width is unset.
347 * Using _right_layout->set_width([value >=0]) would also enable
348 * word-wrapping which is not wanted here.
349 * The solution is to custom align the layout depending on its size.
350 * if it is larger than the available space it will be cropped on the
351 * right edge rather than override text on the left side.
354 _right_layout->get_pixel_size(rw, rh);
355 x = get_width() - rw - separator_height - x_leading_padding;
356 if (x < x_leading_padding + left_rect_width + separator_height) {
357 /* rather cut off the right end than overlap with the text on the left */
358 x = x_leading_padding + left_rect_width + separator_height;
360 cairo_move_to (cr, x, upper_height + separator_height + ((h - info_height)/2.0));
362 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
364 pango_cairo_show_layout (cr, _right_layout->gobj());
367 /* no info to display, or just one */
371 Gtkmm2ext::rounded_bottom_half_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
373 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
381 if (!insert_map.empty()) {
383 int xcenter = layout_x_offset > 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
385 if (input_string.length() < insert_map.size()) {
386 Pango::Rectangle cursor;
388 if (input_string.empty()) {
389 /* nothing entered yet, put cursor at the end
392 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
394 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
397 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
400 min (get_width() - 2.0,
401 (double) cursor.get_x()/PANGO_SCALE + layout_x_offset + xcenter + em_width), 0,
402 2.0, cursor.get_height()/PANGO_SCALE);
405 min (get_width() - 2.0,
406 (double) layout_x_offset + xcenter + cursor.get_x()/PANGO_SCALE + em_width),
407 (upper_height - layout_height)/2.0,
408 2.0, cursor.get_height()/PANGO_SCALE);
412 /* we've entered all possible digits, no cursor */
416 if (input_string.empty()) {
417 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
426 (upper_height - layout_height)/2.0,
436 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
438 CairoWidget::on_size_allocate (alloc);
441 upper_height = (get_height()/2.0) - 1.0;
443 upper_height = get_height();
447 /* center display in available space
448 * NB. this only works if the containing widget is not the
449 * layout itself (eg. the session->property dialog)
451 layout_x_offset = (get_width() - layout_width)/2.0;
459 AudioClock::on_size_request (Gtk::Requisition* req)
461 /* even for non fixed width clocks, the size we *ask* for never changes,
462 even though the size we receive might. so once we've computed it,
467 req->width = first_width;
468 req->height = first_height;
472 Glib::RefPtr<Pango::Layout> tmp;
473 Glib::RefPtr<Gtk::Style> style = get_style ();
474 Pango::FontDescription font;
476 tmp = Pango::Layout::create (get_pango_context());
478 if (!is_realized()) {
479 font = get_font_for_style (get_name());
481 font = style->get_font();
484 tmp->set_font_description (font);
488 tmp->set_text ("-88:88:88:88");
489 tmp->get_pixel_size (_mode_width[Timecode], ignored);
490 tmp->set_text (" 88888|88|8888");
491 tmp->get_pixel_size (_mode_width[BBT], ignored);
492 tmp->set_text (" 88:88:88,888");
493 tmp->get_pixel_size (_mode_width[MinSec], ignored);
494 tmp->set_text (" 8888888888");
495 tmp->get_pixel_size (_mode_width[Frames], ignored);
497 /* this string is the longest thing we will ever display,
498 it does not include the BBT bar char that may descend
500 note; depending on BPM setting this may actually
501 not be sufficient for 24h worth of BBT
504 tmp->set_text (" 88888888888::,");
508 tmp->set_text ("-88:88:88:88");
511 tmp->set_text (" 88888|88|8888");
514 tmp->set_text (" 88:88:88,888");
517 tmp->set_text (" 8888888888");
522 tmp->get_pixel_size (req->width, req->height);
524 layout_height = req->height;
525 layout_width = req->width;
527 /* now tackle height, for which we need to know the height of the lower
535 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
536 font.set_weight (Pango::WEIGHT_NORMAL);
537 tmp->set_font_description (font);
539 /* we only care about height, so put as much stuff in here
540 as possible that might change the height.
542 tmp->set_text ("qyhH|"); /* one ascender, one descender */
544 tmp->get_pixel_size (w, info_height);
546 /* silly extra padding that seems necessary to correct the info
547 * that pango just gave us. I have no idea why.
550 req->height += info_height;
551 req->height += separator_height;
554 first_height = req->height;
555 first_width = req->width;
559 AudioClock::show_edit_status (int length)
561 editing_attr->set_start_index (edit_string.length() - length);
562 editing_attr->set_end_index (edit_string.length());
564 editing_attributes.change (*foreground_attr);
565 editing_attributes.change (*editing_attr);
567 _layout->set_attributes (editing_attributes);
571 AudioClock::start_edit (Field f)
573 pre_edit_string = _layout->get_text ();
574 if (!insert_map.empty()) {
575 edit_string = pre_edit_string;
577 edit_string.clear ();
578 _layout->set_text ("");
581 input_string.clear ();
583 edit_is_negative = false;
586 input_string = get_field (f);
587 show_edit_status (merge_input_and_edit_string ());
588 _layout->set_text (edit_string);
593 Keyboard::magic_widget_grab_focus ();
598 AudioClock::get_field (Field f)
602 return edit_string.substr (1, 2);
604 case Timecode_Minutes:
605 return edit_string.substr (4, 2);
607 case Timecode_Seconds:
608 return edit_string.substr (7, 2);
610 case Timecode_Frames:
611 return edit_string.substr (10, 2);
614 return edit_string.substr (1, 2);
617 return edit_string.substr (4, 2);
620 return edit_string.substr (7, 2);
622 case MS_Milliseconds:
623 return edit_string.substr (10, 3);
626 return edit_string.substr (1, 3);
629 return edit_string.substr (5, 2);
632 return edit_string.substr (8, 4);
642 AudioClock::end_edit (bool modify)
650 ok = timecode_validate_edit (edit_string);
654 ok = bbt_validate_edit (edit_string);
658 ok = minsec_validate_edit (edit_string);
666 edit_string = pre_edit_string;
667 input_string.clear ();
668 _layout->set_text (edit_string);
669 show_edit_status (0);
670 /* edit attributes remain in use */
674 framepos_t pos = 0; /* stupid gcc */
678 pos = frames_from_timecode_string (edit_string);
683 pos = frame_duration_from_bbt_string (0, edit_string);
685 pos = frames_from_bbt_string (0, edit_string);
690 pos = frames_from_minsec_string (edit_string);
694 pos = frames_from_audioframes_string (edit_string);
699 _layout->set_attributes (normal_attributes);
700 ValueChanged(); /* EMIT_SIGNAL */
706 edit_is_negative = false;
707 _layout->set_attributes (normal_attributes);
708 _layout->set_text (pre_edit_string);
719 AudioClock::drop_focus ()
721 Keyboard::magic_widget_drop_focus ();
725 /* move focus back to the default widget in the top level window */
727 Widget* top = get_toplevel();
729 if (top->is_toplevel ()) {
730 Window* win = dynamic_cast<Window*> (top);
737 AudioClock::parse_as_frames_distance (const std::string& str)
741 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
749 AudioClock::parse_as_minsec_distance (const std::string& str)
751 framecnt_t sr = _session->frame_rate();
757 switch (str.length()) {
764 sscanf (str.c_str(), "%" PRId32, &msecs);
765 return msecs * (sr / 1000);
768 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
769 return (secs * sr) + (msecs * (sr/1000));
772 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
773 return (secs * sr) + (msecs * (sr/1000));
776 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
777 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
780 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
781 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
784 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
785 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
788 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
789 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
799 AudioClock::parse_as_timecode_distance (const std::string& str)
801 double fps = _session->timecode_frames_per_second();
802 framecnt_t sr = _session->frame_rate();
808 switch (str.length()) {
813 sscanf (str.c_str(), "%" PRId32, &frames);
814 return lrint ((frames/(float)fps) * sr);
817 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
818 return (secs * sr) + lrint ((frames/(float)fps) * sr);
821 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
822 return (secs * sr) + lrint ((frames/(float)fps) * sr);
825 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
826 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
829 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
830 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
833 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
834 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
837 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
838 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
848 AudioClock::parse_as_bbt_distance (const std::string&)
854 AudioClock::parse_as_distance (const std::string& instr)
858 return parse_as_timecode_distance (instr);
861 return parse_as_frames_distance (instr);
864 return parse_as_bbt_distance (instr);
867 return parse_as_minsec_distance (instr);
874 AudioClock::end_edit_relative (bool add)
880 ok = timecode_validate_edit (edit_string);
884 ok = bbt_validate_edit (edit_string);
888 ok = minsec_validate_edit (edit_string);
896 edit_string = pre_edit_string;
897 input_string.clear ();
898 _layout->set_text (edit_string);
899 show_edit_status (0);
900 /* edit attributes remain in use */
905 framecnt_t frames = parse_as_distance (input_string);
910 _layout->set_attributes (normal_attributes);
914 set (current_time() + frames, true);
916 framepos_t c = current_time();
918 if (c > frames || _negative_allowed) {
919 set (c - frames, true);
924 ValueChanged (); /* EMIT SIGNAL */
927 input_string.clear ();
933 AudioClock::session_property_changed (const PropertyChange& p)
935 set (last_when, true);
939 AudioClock::session_configuration_changed (std::string p)
941 if (_negative_allowed) {
942 /* session option editor clock */
946 if (p == "sync-source" || p == "external-sync") {
947 set (current_time(), true);
951 if (p != "timecode-offset" && p != "timecode-offset-negative") {
960 current = current_duration ();
962 current = current_time ();
972 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
974 if ((!force && !is_visible()) || _session == 0) {
979 when = when - offset;
982 if (when == last_when && !force) {
983 if (_mode != Timecode && _mode != MinSec) {
984 /* may need to force display of TC source
985 * time, so don't return early.
993 _right_layout->set_alignment(Pango::ALIGN_LEFT);
999 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1001 set_timecode (when, force);
1005 set_bbt (when, force);
1009 if (_right_layout) {
1010 _right_layout->set_alignment(Pango::ALIGN_RIGHT);
1012 set_minsec (when, force);
1016 set_frames (when, force);
1026 AudioClock::set_slave_info ()
1028 if (!_left_layout || !_right_layout) {
1032 SyncSource sync_src = Config->get_sync_source();
1034 if (_session->config.get_external_sync()) {
1035 Slave* slave = _session->slave();
1039 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1040 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1041 _right_layout->set_text ("");
1045 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1046 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1047 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1048 INFO_FONT_SIZE, slave->approximate_current_delta()));
1050 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1051 INFO_FONT_SIZE, _("--pending--")));
1052 _right_layout->set_text ("");
1059 TimecodeSlave* tcslave;
1060 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
1061 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
1062 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2</span><span foreground=\"%3\">%4</span></span>",
1063 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)[0], (matching?"green":"red"),
1064 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position()));
1065 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1066 INFO_FONT_SIZE, slave->approximate_current_delta()));
1069 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">%2</span>",
1070 INFO_FONT_SIZE, _("--pending--")));
1071 _right_layout->set_text ("");
1076 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">INT/%2</span>",
1077 INFO_FONT_SIZE, sync_source_to_string(sync_src, true)));
1078 _right_layout->set_text ("");
1083 AudioClock::set_frames (framepos_t when, bool /*force*/)
1086 bool negative = false;
1089 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
1092 _left_layout->set_text ("");
1093 _right_layout->set_text ("");
1105 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1107 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1110 _layout->set_text (buf);
1113 framecnt_t rate = _session->frame_rate();
1115 if (fmod (rate, 100.0) == 0.0) {
1116 sprintf (buf, "%.1fkHz", rate/1000.0);
1118 sprintf (buf, "%" PRId64 "Hz", rate);
1121 _left_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1122 INFO_FONT_SIZE, _("SR"), buf));
1124 float vid_pullup = _session->config.get_video_pullup();
1126 if (vid_pullup == 0.0) {
1127 _right_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">off</span></span>",
1128 INFO_FONT_SIZE, _("Pull")));
1130 sprintf (buf, _("%+.4f%%"), vid_pullup);
1131 _right_layout->set_markup (string_compose ("<span size=\"%1\"><span foreground=\"white\">%2 </span><span foreground=\"green\">%3</span></span>",
1132 INFO_FONT_SIZE, _("Pull"), buf));
1138 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1146 bool negative = false;
1149 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
1152 _left_layout->set_text ("");
1153 _right_layout->set_text ("");
1165 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
1166 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
1167 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
1168 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
1169 secs = (int) floor (left / (float) _session->frame_rate());
1170 left -= (framecnt_t) floor (secs * _session->frame_rate());
1171 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
1174 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1176 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1179 _layout->set_text (buf);
1184 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1187 bool negative = false;
1190 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
1192 _left_layout->set_text ("");
1193 _right_layout->set_text ("");
1205 _session->timecode_duration (when, TC);
1207 _session->timecode_time (when, TC);
1210 TC.negative = TC.negative || negative;
1212 _layout->set_text (Timecode::timecode_format_time(TC));
1218 AudioClock::set_bbt (framepos_t when, bool /*force*/)
1221 Timecode::BBT_Time BBT;
1222 bool negative = false;
1225 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1227 _left_layout->set_text ("");
1228 _right_layout->set_text ("");
1238 /* handle a common case */
1245 _session->tempo_map().bbt_time (when, BBT);
1250 _session->tempo_map().bbt_time (when, BBT);
1254 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1255 BBT.bars, BBT.beats, BBT.ticks);
1257 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1258 BBT.bars, BBT.beats, BBT.ticks);
1261 _layout->set_text (buf);
1263 if (_right_layout) {
1266 if (bbt_reference_time < 0) {
1269 pos = bbt_reference_time;
1272 TempoMetric m (_session->tempo_map().metric_at (pos));
1274 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1275 _left_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">Tempo <span foreground=\"green\">%2</span></span>",
1276 INFO_FONT_SIZE, buf));
1278 sprintf (buf, "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1279 _right_layout->set_markup (string_compose ("<span size=\"%1\" foreground=\"white\">Meter <span foreground=\"green\">%2</span></span>",
1280 INFO_FONT_SIZE, buf));
1285 AudioClock::set_session (Session *s)
1287 SessionHandlePtr::set_session (s);
1291 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1292 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1294 const XMLProperty* prop;
1295 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1296 AudioClock::Mode amode;
1299 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1300 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1302 if ((prop = (*i)->property (X_("mode"))) != 0) {
1303 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1306 if ((prop = (*i)->property (X_("on"))) != 0) {
1307 set_off (!string_is_affirmative (prop->value()));
1314 set (last_when, true);
1319 AudioClock::on_key_press_event (GdkEventKey* ev)
1327 int highlight_length;
1330 switch (ev->keyval) {
1373 case GDK_KP_Subtract:
1374 if (_negative_allowed && input_string.empty()) {
1375 edit_is_negative = true;
1376 edit_string.replace(0,1,"-");
1377 _layout->set_text (edit_string);
1380 end_edit_relative (false);
1386 end_edit_relative (true);
1399 ChangeAborted(); /* EMIT SIGNAL */
1404 if (!input_string.empty()) {
1405 /* delete the last key entered
1407 input_string = input_string.substr (0, input_string.length() - 1);
1409 goto use_input_string;
1415 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1416 /* too many digits: eat the key event, but do nothing with it */
1420 input_string.push_back (new_char);
1426 /* get this one in the right order, and to the right width */
1427 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1428 edit_string = edit_string.substr (0, edit_string.length() - 1);
1430 edit_string.push_back (new_char);
1432 if (!edit_string.empty()) {
1434 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1435 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1438 /* highlight the whole thing */
1439 highlight_length = edit_string.length();
1443 highlight_length = merge_input_and_edit_string ();
1446 if (edit_is_negative) {
1447 edit_string.replace(0,1,"-");
1449 /* TODO think about this case.
1450 * The TC will be positive unless the edit is relative.
1452 edit_string.replace(0,1," ");
1455 show_edit_status (highlight_length);
1456 _layout->set_text (edit_string);
1463 AudioClock::merge_input_and_edit_string ()
1465 /* merge with pre-edit-string into edit string */
1467 edit_string = pre_edit_string;
1469 if (input_string.empty()) {
1473 string::size_type target;
1474 for (string::size_type i = 0; i < input_string.length(); ++i) {
1475 target = insert_map[input_string.length() - 1 - i];
1476 edit_string[target] = input_string[i];
1478 /* highlight from end to wherever the last character was added */
1479 return edit_string.length() - insert_map[input_string.length()-1];
1484 AudioClock::on_key_release_event (GdkEventKey *ev)
1490 /* return true for keys that we used on press
1491 so that they cannot possibly do double-duty
1493 switch (ev->keyval) {
1516 case GDK_KP_Decimal:
1524 case GDK_KP_Subtract:
1532 AudioClock::index_to_field (int index) const
1537 return Timecode_Hours;
1538 } else if (index < 7) {
1539 return Timecode_Minutes;
1540 } else if (index < 10) {
1541 return Timecode_Seconds;
1543 return Timecode_Frames;
1549 } else if (index < 7) {
1557 return Timecode_Hours;
1558 } else if (index < 6) {
1560 } else if (index < 9) {
1563 return MS_Milliseconds;
1575 AudioClock::on_button_press_event (GdkEventButton *ev)
1577 switch (ev->button) {
1579 if (editable && !_off) {
1585 /* the text has been centered vertically, so adjust
1588 int xcenter = !_fixed_width || layout_x_offset > 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1590 y = ev->y - ((upper_height - layout_height)/2);
1591 x = ev->x - layout_x_offset - xcenter;
1593 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1594 /* pretend it is a character on the far right */
1597 drag_field = index_to_field (index);
1599 /* make absolutely sure that the pointer is grabbed */
1600 gdk_pointer_grab(ev->window,false ,
1601 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1602 NULL,NULL,ev->time);
1604 drag_start_y = ev->y;
1618 AudioClock::on_button_release_event (GdkEventButton *ev)
1620 if (editable && !_off) {
1622 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1624 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1625 // we actually dragged so return without
1626 // setting editing focus, or we shift clicked
1629 if (ev->button == 1) {
1631 if (_edit_by_click_field) {
1633 int xcenter = !_fixed_width || layout_x_offset > 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1636 int y = ev->y - ((upper_height - layout_height)/2);
1637 int x = ev->x - layout_x_offset - xcenter;
1640 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1644 f = index_to_field (index);
1647 case Timecode_Frames:
1648 case MS_Milliseconds:
1664 if (Keyboard::is_context_menu_event (ev)) {
1665 if (ops_menu == 0) {
1668 ops_menu->popup (1, ev->time);
1676 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1678 bool ret = CairoWidget::on_focus_out_event (ev);
1688 AudioClock::on_scroll_event (GdkEventScroll *ev)
1693 if (editing || _session == 0 || !editable || _off) {
1700 /* the text has been centered vertically, so adjust
1704 int xcenter = !_fixed_width || layout_x_offset > 0 ? 0 : (get_width() - _mode_width[_mode]) /2;
1705 y = ev->y - ((upper_height - layout_height)/2);
1706 x = ev->x - layout_x_offset - xcenter;
1708 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1709 /* not in the main layout */
1713 Field f = index_to_field (index);
1714 framepos_t frames = 0;
1716 switch (ev->direction) {
1719 frames = get_frame_step (f);
1721 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1724 set (current_time() + frames, true);
1725 ValueChanged (); /* EMIT_SIGNAL */
1729 case GDK_SCROLL_DOWN:
1730 frames = get_frame_step (f);
1732 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1736 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1739 set (current_time() - frames, true);
1742 ValueChanged (); /* EMIT_SIGNAL */
1755 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1757 if (editing || _session == 0 || !dragging) {
1761 float pixel_frame_scale_factor = 0.2f;
1763 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1764 pixel_frame_scale_factor = 0.1f;
1768 if (Keyboard::modifier_state_contains (ev->state,
1769 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1771 pixel_frame_scale_factor = 0.025f;
1774 double y_delta = ev->y - drag_y;
1776 drag_accum += y_delta*pixel_frame_scale_factor;
1780 if (trunc (drag_accum) != 0) {
1785 dir = (drag_accum < 0 ? 1:-1);
1786 pos = current_time();
1787 frames = get_frame_step (drag_field, pos, dir);
1789 if (frames != 0 && frames * drag_accum < current_time()) {
1790 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1796 ValueChanged(); /* EMIT_SIGNAL */
1803 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1806 Timecode::BBT_Time BBT;
1808 case Timecode_Hours:
1809 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1811 case Timecode_Minutes:
1812 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1814 case Timecode_Seconds:
1815 f = _session->frame_rate();
1817 case Timecode_Frames:
1818 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1826 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1829 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1832 f = (framecnt_t) _session->frame_rate();
1834 case MS_Milliseconds:
1835 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1842 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1848 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1854 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1857 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1866 AudioClock::current_time (framepos_t) const
1872 AudioClock::current_duration (framepos_t pos) const
1881 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1897 AudioClock::bbt_validate_edit (const string& str)
1901 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1905 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1909 if (!is_duration && any.bbt.bars == 0) {
1913 if (!is_duration && any.bbt.beats == 0) {
1921 AudioClock::timecode_validate_edit (const string&)
1927 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1928 &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 5) {
1933 TC.hours = hours * -1;
1937 TC.negative = false;
1940 if (TC.negative && !_negative_allowed) {
1944 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1948 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1952 if (_session->timecode_drop_frames()) {
1953 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1962 AudioClock::minsec_validate_edit (const string& str)
1964 int hrs, mins, secs, millisecs;
1966 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1970 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1978 AudioClock::frames_from_timecode_string (const string& str) const
1980 if (_session == 0) {
1989 if (sscanf (str.c_str(), "%d:%d:%d%[:;]%d", &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 5) {
1990 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1993 TC.hours = abs(hours);
1994 TC.rate = _session->timecode_frames_per_second();
1995 TC.drop= _session->timecode_drop_frames();
1997 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1999 // timecode_tester ();
2000 if (edit_is_negative) {
2008 AudioClock::frames_from_minsec_string (const string& str) const
2010 if (_session == 0) {
2014 int hrs, mins, secs, millisecs;
2015 framecnt_t sr = _session->frame_rate();
2017 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2018 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2022 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2026 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
2028 if (_session == 0) {
2029 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2034 any.type = AnyTime::BBT;
2036 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2043 return _session->any_duration_to_frames (pos, any);
2045 return _session->convert_to_frames (any);
2051 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2053 if (_session == 0) {
2054 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2058 Timecode::BBT_Time bbt;
2060 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2064 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2068 AudioClock::frames_from_audioframes_string (const string& str) const
2071 sscanf (str.c_str(), "%" PRId64, &f);
2076 AudioClock::build_ops_menu ()
2078 using namespace Menu_Helpers;
2079 ops_menu = new Menu;
2080 MenuList& ops_items = ops_menu->items();
2081 ops_menu->set_name ("ArdourContextMenu");
2083 if (!Profile->get_sae()) {
2084 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
2086 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
2087 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
2088 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
2090 if (editable && !_off && !is_duration && !_follows_playhead) {
2091 ops_items.push_back (SeparatorElem());
2092 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2093 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2098 AudioClock::set_from_playhead ()
2104 set (_session->transport_frame());
2109 AudioClock::locate ()
2111 if (!_session || is_duration) {
2115 _session->request_locate (current_time(), _session->transport_rolling ());
2119 AudioClock::set_mode (Mode m)
2129 _layout->set_text ("");
2133 _left_layout->set_attributes (info_attributes);
2134 _right_layout->set_attributes (info_attributes);
2135 /* adjust info_height according to font size */
2137 _left_layout->set_text (" 1234567890");
2138 _left_layout->get_pixel_size (ignored, info_height);
2140 _left_layout->set_text ("");
2141 _right_layout->set_text ("");
2146 mode_based_info_ratio = 0.5;
2147 insert_map.push_back (11);
2148 insert_map.push_back (10);
2149 insert_map.push_back (8);
2150 insert_map.push_back (7);
2151 insert_map.push_back (5);
2152 insert_map.push_back (4);
2153 insert_map.push_back (2);
2154 insert_map.push_back (1);
2158 mode_based_info_ratio = 0.5;
2159 insert_map.push_back (11);
2160 insert_map.push_back (10);
2161 insert_map.push_back (9);
2162 insert_map.push_back (8);
2163 insert_map.push_back (6);
2164 insert_map.push_back (5);
2165 insert_map.push_back (3);
2166 insert_map.push_back (2);
2167 insert_map.push_back (1);
2171 mode_based_info_ratio = 0.5;
2172 insert_map.push_back (12);
2173 insert_map.push_back (11);
2174 insert_map.push_back (10);
2175 insert_map.push_back (8);
2176 insert_map.push_back (7);
2177 insert_map.push_back (5);
2178 insert_map.push_back (4);
2179 insert_map.push_back (2);
2180 insert_map.push_back (1);
2184 mode_based_info_ratio = 0.5;
2188 set (last_when, true);
2190 if (!is_transient) {
2191 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2194 if (!_fixed_width) {
2195 /* display is different, allow us to resize */
2201 mode_changed (); /* EMIT SIGNAL (the member one) */
2205 AudioClock::set_bbt_reference (framepos_t pos)
2207 bbt_reference_time = pos;
2211 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2213 CairoWidget::on_style_changed (old_style);
2221 AudioClock::set_editable (bool yn)
2227 AudioClock::set_is_duration (bool yn)
2229 if (yn == is_duration) {
2234 set (last_when, true);
2238 AudioClock::set_off (bool yn)
2246 /* force a redraw. last_when will be preserved, but the clock text will
2250 set (last_when, true);
2254 AudioClock::focus ()
2256 start_edit (Field (0));
2260 AudioClock::set_corner_radius (double r)
2269 AudioClock::set_fixed_width (bool yn)
2275 AudioClock::dpi_reset ()
2277 /* force recomputation of size even if we are fixed width
2285 AudioClock::set_negative_allowed (bool yn)
2287 _negative_allowed = yn;