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 "ardour_ui.h"
41 #include "audio_clock.h"
42 #include "enums_convert.h"
43 #include "gui_thread.h"
46 #include "ui_config.h"
51 using namespace ARDOUR;
52 using namespace ARDOUR_UI_UTILS;
57 using Gtkmm2ext::Keyboard;
59 sigc::signal<void> AudioClock::ModeChanged;
60 vector<AudioClock*> AudioClock::clocks;
62 #define BBT_BAR_CHAR "|"
63 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
65 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
66 bool allow_edit, bool follows_playhead, bool duration, bool with_info,
67 bool accept_on_focus_out)
70 , is_transient (transient)
71 , is_duration (duration)
72 , editable (allow_edit)
73 , _follows_playhead (follows_playhead)
74 , _accept_on_focus_out (accept_on_focus_out)
77 , _edit_by_click_field (false)
78 , _negative_allowed (false)
79 , edit_is_negative (false)
80 , _with_info (with_info)
85 , style_resets_first (true)
91 , bbt_reference_time (-1)
96 , drag_field (Field (0))
100 set_flags (CAN_FOCUS);
102 _layout = Pango::Layout::create (get_pango_context());
103 _layout->set_attributes (normal_attributes);
105 set_widget_name (widget_name);
107 _mode = BBT; /* lie to force mode switch */
109 set (last_when, true);
112 clocks.push_back (this);
115 _left_btn.set_sizing_text (_("0000000000000"));
116 // NB right_btn is in a size-group
118 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
119 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
121 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
122 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
125 AudioClock::~AudioClock ()
127 delete foreground_attr;
132 AudioClock::set_widget_name (const string& str)
137 set_name (str + " clock");
147 AudioClock::on_realize ()
149 Gtk::Requisition req;
151 CairoWidget::on_realize ();
153 set_clock_dimensions (req);
155 first_width = req.width;
156 first_height = req.height;
158 // XXX FIX ME: define font based on ... ???
164 AudioClock::set_font (Pango::FontDescription font)
166 Glib::RefPtr<Gtk::Style> style = get_style ();
167 Pango::AttrFontDesc* font_attr;
169 font_size = font.get_size();
170 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
172 normal_attributes.change (*font_attr);
173 editing_attributes.change (*font_attr);
176 /* get the figure width for the font. This doesn't have to super
177 * accurate since we only use it to measure the (roughly 1 character)
178 * offset from the position Pango tells us for the "cursor"
181 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
185 tmp->get_pixel_size (em_width, ignore_height);
187 /* force redraw of markup with new font-size */
188 set (last_when, true);
192 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
194 CairoWidget::set_active_state (s);
199 AudioClock::set_colors ()
205 uint32_t editing_color;
206 uint32_t cursor_color;
208 if (active_state()) {
209 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
210 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
211 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
212 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
214 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
215 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
216 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
217 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
220 /* store for bg and cursor in render() */
222 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
229 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
236 /* rescale for Pango colors ... sigh */
238 r = lrint (r * 65535.0);
239 g = lrint (g * 65535.0);
240 b = lrint (b * 65535.0);
242 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
243 r = lrint ((r/255.0) * 65535.0);
244 g = lrint ((g/255.0) * 65535.0);
245 b = lrint ((b/255.0) * 65535.0);
246 delete foreground_attr;
247 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
249 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
250 r = lrint ((r/255.0) * 65535.0);
251 g = lrint ((g/255.0) * 65535.0);
252 b = lrint ((b/255.0) * 65535.0);
254 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
256 normal_attributes.change (*foreground_attr);
257 editing_attributes.change (*foreground_attr);
258 editing_attributes.change (*editing_attr);
261 _layout->set_attributes (normal_attributes);
263 _layout->set_attributes (editing_attributes);
270 AudioClock::set_scale (double x, double y)
279 AudioClock::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
281 cairo_t* cr = ctx->cobj();
282 /* main layout: rounded rect, plus the text */
285 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
287 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
289 cairo_rectangle (cr, 0, 0, get_width(), get_height());
294 double lw = layout_width * xscale;
295 double lh = layout_height * yscale;
297 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
299 if (xscale != 1.0 || yscale != 1.0) {
301 cairo_scale (cr, xscale, yscale);
304 pango_cairo_show_layout (cr, _layout->gobj());
306 if (xscale != 1.0 || yscale != 1.0) {
311 if (!insert_map.empty()) {
313 int xcenter = (get_width() - layout_width) /2;
315 if (input_string.length() < insert_map.size()) {
316 Pango::Rectangle cursor;
318 if (input_string.empty()) {
319 /* nothing entered yet, put cursor at the end
322 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
324 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
327 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
329 min (get_width() - 2.0,
330 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
331 (get_height() - layout_height)/2.0,
332 2.0, cursor.get_height()/PANGO_SCALE);
335 /* we've entered all possible digits, no cursor */
339 if (input_string.empty()) {
340 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
343 (get_height() - layout_height)/2.0,
352 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
354 Glib::RefPtr<Pango::Layout> tmp;
355 Glib::RefPtr<Gtk::Style> style = get_style ();
356 Pango::FontDescription font;
358 tmp = Pango::Layout::create (get_pango_context());
360 if (!is_realized()) {
361 font = get_font_for_style (get_name());
363 font = style->get_font();
366 tmp->set_font_description (font);
368 /* this string is the longest thing we will ever display */
370 tmp->set_text (" 88:88:88,888 ");
372 tmp->set_text (" 88:88:88,88 ");
373 tmp->get_pixel_size (req.width, req.height);
375 layout_height = req.height;
376 layout_width = req.width;
380 AudioClock::on_size_request (Gtk::Requisition* req)
382 /* even for non fixed width clocks, the size we *ask* for never changes,
383 even though the size we receive might. so once we've computed it,
388 req->width = first_width;
389 req->height = first_height;
393 set_clock_dimensions (*req);
395 /* now tackle height, for which we need to know the height of the lower
401 AudioClock::show_edit_status (int length)
403 editing_attr->set_start_index (edit_string.length() - length);
404 editing_attr->set_end_index (edit_string.length());
406 editing_attributes.change (*foreground_attr);
407 editing_attributes.change (*editing_attr);
409 _layout->set_attributes (editing_attributes);
413 AudioClock::start_edit (Field f)
416 pre_edit_string = _layout->get_text ();
417 if (!insert_map.empty()) {
418 edit_string = pre_edit_string;
420 edit_string.clear ();
421 _layout->set_text ("");
424 input_string.clear ();
426 edit_is_negative = false;
429 input_string = get_field (f);
430 show_edit_status (merge_input_and_edit_string ());
431 _layout->set_text (edit_string);
436 Keyboard::magic_widget_grab_focus ();
442 AudioClock::get_field (Field f)
446 return edit_string.substr (1, 2);
448 case Timecode_Minutes:
449 return edit_string.substr (4, 2);
451 case Timecode_Seconds:
452 return edit_string.substr (7, 2);
454 case Timecode_Frames:
455 return edit_string.substr (10, 2);
458 return edit_string.substr (1, 2);
461 return edit_string.substr (4, 2);
464 return edit_string.substr (7, 2);
466 case MS_Milliseconds:
467 return edit_string.substr (10, 3);
470 return edit_string.substr (1, 3);
473 return edit_string.substr (5, 2);
476 return edit_string.substr (8, 4);
486 AudioClock::end_edit (bool modify)
494 ok = timecode_validate_edit (edit_string);
498 ok = bbt_validate_edit (edit_string);
502 ok = minsec_validate_edit (edit_string);
506 if (edit_string.length() < 1) {
507 edit_string = pre_edit_string;
513 edit_string = pre_edit_string;
514 input_string.clear ();
515 _layout->set_text (edit_string);
516 show_edit_status (0);
517 /* edit attributes remain in use */
521 framepos_t pos = 0; /* stupid gcc */
525 pos = frames_from_timecode_string (edit_string);
530 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
532 pos = frames_from_bbt_string (0, edit_string);
537 pos = frames_from_minsec_string (edit_string);
541 pos = frames_from_audioframes_string (edit_string);
546 _layout->set_attributes (normal_attributes);
547 ValueChanged(); /* EMIT_SIGNAL */
553 edit_is_negative = false;
554 _layout->set_attributes (normal_attributes);
555 _layout->set_text (pre_edit_string);
566 AudioClock::drop_focus ()
568 Keyboard::magic_widget_drop_focus ();
571 /* move focus back to the default widget in the top level window */
572 ARDOUR_UI::instance()->reset_focus (this);
577 AudioClock::parse_as_frames_distance (const std::string& str)
581 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
589 AudioClock::parse_as_minsec_distance (const std::string& str)
591 framecnt_t sr = _session->frame_rate();
597 switch (str.length()) {
604 sscanf (str.c_str(), "%" PRId32, &msecs);
605 return msecs * (sr / 1000);
608 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
609 return (secs * sr) + (msecs * (sr/1000));
612 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
613 return (secs * sr) + (msecs * (sr/1000));
616 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
617 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
620 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
621 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
624 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
625 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
628 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
629 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
639 AudioClock::parse_as_timecode_distance (const std::string& str)
641 double fps = _session->timecode_frames_per_second();
642 framecnt_t sr = _session->frame_rate();
648 switch (str.length()) {
653 sscanf (str.c_str(), "%" PRId32, &frames);
654 return llrint ((frames/(float)fps) * sr);
657 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
658 return (secs * sr) + llrint ((frames/(float)fps) * sr);
661 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
662 return (secs * sr) + llrint ((frames/(float)fps) * sr);
665 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
666 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
669 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
670 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
673 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
674 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
677 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
678 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
688 AudioClock::parse_as_bbt_distance (const std::string&)
694 AudioClock::parse_as_distance (const std::string& instr)
698 return parse_as_timecode_distance (instr);
701 return parse_as_frames_distance (instr);
704 return parse_as_bbt_distance (instr);
707 return parse_as_minsec_distance (instr);
714 AudioClock::end_edit_relative (bool add)
720 ok = timecode_validate_edit (edit_string);
724 ok = bbt_validate_edit (edit_string);
728 ok = minsec_validate_edit (edit_string);
736 edit_string = pre_edit_string;
737 input_string.clear ();
738 _layout->set_text (edit_string);
739 show_edit_status (0);
740 /* edit attributes remain in use */
745 framecnt_t frames = parse_as_distance (input_string);
750 _layout->set_attributes (normal_attributes);
754 set (current_time() + frames, true);
756 framepos_t c = current_time();
758 if (c > frames || _negative_allowed) {
759 set (c - frames, true);
764 ValueChanged (); /* EMIT SIGNAL */
767 input_string.clear ();
773 AudioClock::session_property_changed (const PropertyChange&)
775 set (last_when, true);
779 AudioClock::session_configuration_changed (std::string p)
781 if (_negative_allowed) {
782 /* session option editor clock */
786 if (p == "sync-source" || p == "external-sync") {
787 set (current_time(), true);
791 if (p != "timecode-offset" && p != "timecode-offset-negative") {
800 current = current_duration ();
802 current = current_time ();
812 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
814 if ((!force && !is_visible()) || _session == 0) {
819 when = when - offset;
822 if (when == last_when && !force) {
823 #if 0 // XXX return if no change and no change forced. verify Aug/2014
824 if (_mode != Timecode && _mode != MinSec) {
825 /* may need to force display of TC source
826 * time, so don't return early.
828 /* ^^ Why was that?, delta times?
829 * Timecode FPS, pull-up/down, etc changes
830 * trigger a 'session_property_changed' which
831 * eventually calls set(last_when, true)
833 * re-rendering the clock every 40ms or so just
834 * because we can is not ideal.
849 set_timecode (when, force);
853 set_bbt (when, offset, force);
858 set_minsec (when, force);
862 set_frames (when, force);
868 _left_btn.set_sensitive (btn_en);
869 _right_btn.set_sensitive (btn_en);
870 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
871 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
873 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
874 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
875 _left_btn.set_alignment (.5, .5);
876 _right_btn.set_alignment (.5, .5);
877 set_tooltip (_left_btn, _("Change current tempo"));
878 set_tooltip (_right_btn, _("Change current time signature"));
880 _left_btn.set_elements (ArdourButton::Text);
881 _right_btn.set_elements (ArdourButton::Text);
882 _left_btn.set_alignment (0, .5);
883 _right_btn.set_alignment (1, .5);
884 set_tooltip (_left_btn, _(""));
885 set_tooltip (_right_btn, _(""));
894 AudioClock::set_slave_info ()
900 SyncSource sync_src = Config->get_sync_source();
902 if (_session->config.get_external_sync()) {
903 Slave* slave = _session->slave();
907 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
908 _right_btn.set_text ("", true);
912 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
913 _right_btn.set_text (slave->approximate_current_delta (), true);
915 _left_btn.set_text (_("--pending--"), true);
916 _right_btn.set_text ("", true);
923 TimecodeSlave* tcslave;
924 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
925 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
926 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
927 sync_source_to_string(sync_src, true)[0],
928 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
929 matching ? "#66ff66" : "#ff3333"
931 _right_btn.set_text (slave->approximate_current_delta (), true);
934 _left_btn.set_text (_("--pending--"), true);
935 _right_btn.set_text ("", true);
940 _left_btn.set_text (string_compose ("%1/%2",
941 _("INT"), sync_source_to_string(sync_src, true)), true);
942 _right_btn.set_text ("", true);
947 AudioClock::set_frames (framepos_t when, bool /*force*/)
950 bool negative = false;
953 _layout->set_text (" ----------");
954 _left_btn.set_text ("", true);
955 _right_btn.set_text ("", true);
965 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
967 snprintf (buf, sizeof (buf), " %10" PRId64, when);
970 _layout->set_text (buf);
973 framecnt_t rate = _session->frame_rate();
975 if (fmod (rate, 100.0) == 0.0) {
976 sprintf (buf, "%.1fkHz", rate/1000.0);
978 sprintf (buf, "%" PRId64 "Hz", rate);
981 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
983 float vid_pullup = _session->config.get_video_pullup();
985 if (vid_pullup == 0.0) {
986 _right_btn.set_text ("", true);
988 sprintf (buf, _("%+.4f%%"), vid_pullup);
989 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
995 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1012 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1013 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1014 mins = (int) floor (left / (frame_rate * 60.0f));
1015 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1016 secs = (int) floor (left / (float) frame_rate);
1017 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1018 millisecs = floor (left * 1000.0 / (float) frame_rate);
1021 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1023 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1029 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1034 _layout->set_text (" --:--:--.---");
1035 _left_btn.set_text ("", true);
1036 _right_btn.set_text ("", true);
1041 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1043 _layout->set_text (buf);
1048 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1051 bool negative = false;
1054 _layout->set_text (" --:--:--:--");
1055 _left_btn.set_text ("", true);
1056 _right_btn.set_text ("", true);
1066 _session->timecode_duration (when, TC);
1068 _session->timecode_time (when, TC);
1071 TC.negative = TC.negative || negative;
1073 _layout->set_text (Timecode::timecode_format_time(TC));
1079 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1082 Timecode::BBT_Time BBT;
1083 bool negative = false;
1086 _layout->set_text (" ---|--|----");
1087 _left_btn.set_text ("", true);
1088 _right_btn.set_text ("", true);
1097 /* handle a common case */
1104 TempoMap& tmap (_session->tempo_map());
1107 offset = bbt_reference_time;
1110 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1111 Timecode::BBT_Time sub_bbt;
1114 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1115 sub_bbt = tmap.bbt_at_frame (offset - when);
1117 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1118 sub_bbt = tmap.bbt_at_frame (offset);
1121 BBT.bars -= sub_bbt.bars;
1123 if (BBT.ticks < sub_bbt.ticks) {
1124 if (BBT.beats == 1) {
1126 BBT.beats = divisions;
1130 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1132 BBT.ticks -= sub_bbt.ticks;
1135 if (BBT.beats < sub_bbt.beats) {
1137 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1139 BBT.beats -= sub_bbt.beats;
1143 BBT = _session->tempo_map().bbt_at_frame (when);
1147 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1148 BBT.bars, BBT.beats, BBT.ticks);
1150 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1151 BBT.bars, BBT.beats, BBT.ticks);
1154 _layout->set_text (buf);
1159 if (bbt_reference_time < 0) {
1162 pos = bbt_reference_time;
1165 TempoMetric m (_session->tempo_map().metric_at (pos));
1167 if (m.tempo().note_type() == 4) {
1168 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1169 _left_btn.set_text (string_compose ("%1", buf), true);
1170 } else if (m.tempo().note_type() == 8) {
1171 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1172 _left_btn.set_text (string_compose ("%1", buf), true);
1174 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1175 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1178 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1179 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1184 AudioClock::set_session (Session *s)
1186 SessionHandlePtr::set_session (s);
1190 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1191 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1192 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1193 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1195 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1198 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1200 if ((*i)->get_property (X_("name"), name) && name == _name) {
1202 AudioClock::Mode amode;
1203 if ((*i)->get_property (X_("mode"), amode)) {
1204 set_mode (amode, true);
1207 if ((*i)->get_property (X_("on"), on)) {
1215 set (last_when, true);
1220 AudioClock::on_key_press_event (GdkEventKey* ev)
1228 int highlight_length;
1231 switch (ev->keyval) {
1274 case GDK_KP_Subtract:
1275 if (_negative_allowed && input_string.empty()) {
1276 edit_is_negative = true;
1277 edit_string.replace(0,1,"-");
1278 _layout->set_text (edit_string);
1281 end_edit_relative (false);
1287 end_edit_relative (true);
1300 ChangeAborted(); /* EMIT SIGNAL */
1305 if (!input_string.empty()) {
1306 /* delete the last key entered
1308 input_string = input_string.substr (0, input_string.length() - 1);
1310 goto use_input_string;
1313 /* do not allow other keys to passthru to the rest of the GUI
1319 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1320 /* too many digits: eat the key event, but do nothing with it */
1324 input_string.push_back (new_char);
1330 /* get this one in the right order, and to the right width */
1331 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1332 edit_string = edit_string.substr (0, edit_string.length() - 1);
1334 edit_string.push_back (new_char);
1336 if (!edit_string.empty()) {
1338 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1339 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1342 /* highlight the whole thing */
1343 highlight_length = edit_string.length();
1347 highlight_length = merge_input_and_edit_string ();
1350 if (edit_is_negative) {
1351 edit_string.replace(0,1,"-");
1353 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1354 edit_string.replace(0,1,"_");
1356 edit_string.replace(0,1," ");
1360 show_edit_status (highlight_length);
1361 _layout->set_text (edit_string);
1368 AudioClock::merge_input_and_edit_string ()
1370 /* merge with pre-edit-string into edit string */
1372 edit_string = pre_edit_string;
1374 if (input_string.empty()) {
1378 string::size_type target;
1379 for (string::size_type i = 0; i < input_string.length(); ++i) {
1380 target = insert_map[input_string.length() - 1 - i];
1381 edit_string[target] = input_string[i];
1383 /* highlight from end to wherever the last character was added */
1384 return edit_string.length() - insert_map[input_string.length()-1];
1389 AudioClock::on_key_release_event (GdkEventKey *ev)
1395 /* return true for keys that we used on press
1396 so that they cannot possibly do double-duty
1398 switch (ev->keyval) {
1421 case GDK_KP_Decimal:
1429 case GDK_KP_Subtract:
1437 AudioClock::index_to_field (int index) const
1442 return Timecode_Hours;
1443 } else if (index < 7) {
1444 return Timecode_Minutes;
1445 } else if (index < 10) {
1446 return Timecode_Seconds;
1448 return Timecode_Frames;
1454 } else if (index < 7) {
1462 return Timecode_Hours;
1463 } else if (index < 6) {
1465 } else if (index < 9) {
1468 return MS_Milliseconds;
1480 AudioClock::on_button_press_event (GdkEventButton *ev)
1482 switch (ev->button) {
1484 if (editable && !_off) {
1490 /* the text has been centered vertically, so adjust
1493 int xcenter = (get_width() - layout_width) /2;
1495 y = ev->y - ((get_height() - layout_height)/2);
1496 x = ev->x - xcenter;
1498 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1499 /* pretend it is a character on the far right */
1502 drag_field = index_to_field (index);
1504 /* make absolutely sure that the pointer is grabbed */
1505 gdk_pointer_grab(ev->window,false ,
1506 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1507 NULL,NULL,ev->time);
1509 drag_start_y = ev->y;
1523 AudioClock::on_button_release_event (GdkEventButton *ev)
1525 if (editable && !_off) {
1527 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1529 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1530 // we actually dragged so return without
1531 // setting editing focus, or we shift clicked
1534 if (ev->button == 1) {
1536 if (_edit_by_click_field) {
1538 int xcenter = (get_width() - layout_width) /2;
1541 int y = ev->y - ((get_height() - layout_height)/2);
1542 int x = ev->x - xcenter;
1545 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1549 f = index_to_field (index);
1552 case Timecode_Frames:
1553 case MS_Milliseconds:
1569 if (Keyboard::is_context_menu_event (ev)) {
1570 if (ops_menu == 0) {
1573 ops_menu->popup (1, ev->time);
1581 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1583 bool ret = CairoWidget::on_focus_out_event (ev);
1586 end_edit (_accept_on_focus_out);
1593 AudioClock::on_scroll_event (GdkEventScroll *ev)
1598 if (editing || _session == 0 || !editable || _off) {
1605 /* the text has been centered vertically, so adjust
1609 int xcenter = (get_width() - layout_width) /2;
1610 y = ev->y - ((get_height() - layout_height)/2);
1611 x = ev->x - xcenter;
1613 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1614 /* not in the main layout */
1618 Field f = index_to_field (index);
1619 framepos_t frames = 0;
1621 switch (ev->direction) {
1624 frames = get_frame_step (f, current_time(), 1);
1626 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1629 set (current_time() + frames, true);
1630 ValueChanged (); /* EMIT_SIGNAL */
1634 case GDK_SCROLL_DOWN:
1635 frames = get_frame_step (f, current_time(), -1);
1637 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1641 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1644 set (current_time() - frames, true);
1647 ValueChanged (); /* EMIT_SIGNAL */
1660 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1662 if (editing || _session == 0 || !dragging) {
1666 float pixel_frame_scale_factor = 0.2f;
1668 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1669 pixel_frame_scale_factor = 0.1f;
1673 if (Keyboard::modifier_state_contains (ev->state,
1674 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1676 pixel_frame_scale_factor = 0.025f;
1679 double y_delta = ev->y - drag_y;
1681 drag_accum += y_delta*pixel_frame_scale_factor;
1685 if (floor (drag_accum) != 0) {
1690 dir = (drag_accum < 0 ? 1:-1);
1691 pos = current_time();
1692 frames = get_frame_step (drag_field, pos, dir);
1694 if (frames != 0 && frames * drag_accum < current_time()) {
1695 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1701 ValueChanged(); /* EMIT_SIGNAL */
1708 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1711 Timecode::BBT_Time BBT;
1713 case Timecode_Hours:
1714 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1716 case Timecode_Minutes:
1717 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1719 case Timecode_Seconds:
1720 f = _session->frame_rate();
1722 case Timecode_Frames:
1723 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1731 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1734 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1737 f = (framecnt_t) _session->frame_rate();
1739 case MS_Milliseconds:
1740 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1747 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1753 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1759 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1762 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1771 AudioClock::current_time (framepos_t) const
1777 AudioClock::current_duration (framepos_t pos) const
1786 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1802 AudioClock::bbt_validate_edit (const string& str)
1806 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1810 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1814 if (!is_duration && any.bbt.bars == 0) {
1818 if (!is_duration && any.bbt.beats == 0) {
1826 AudioClock::timecode_validate_edit (const string&)
1832 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1833 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1838 TC.hours = hours * -1;
1842 TC.negative = false;
1845 if (TC.negative && !_negative_allowed) {
1849 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1853 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1857 if (_session->timecode_drop_frames()) {
1858 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1867 AudioClock::minsec_validate_edit (const string& str)
1869 int hrs, mins, secs, millisecs;
1871 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1875 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1883 AudioClock::frames_from_timecode_string (const string& str) const
1885 if (_session == 0) {
1894 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1895 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1898 TC.hours = abs(hours);
1899 TC.rate = _session->timecode_frames_per_second();
1900 TC.drop= _session->timecode_drop_frames();
1902 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1904 // timecode_tester ();
1905 if (edit_is_negative) {
1913 AudioClock::frames_from_minsec_string (const string& str) const
1915 if (_session == 0) {
1919 int hrs, mins, secs, millisecs;
1920 framecnt_t sr = _session->frame_rate();
1922 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1923 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1927 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1931 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1933 if (_session == 0) {
1934 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1939 any.type = AnyTime::BBT;
1941 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1948 return _session->any_duration_to_frames (pos, any);
1950 return _session->convert_to_frames (any);
1956 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1958 if (_session == 0) {
1959 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
1963 Timecode::BBT_Time bbt;
1965 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1969 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1973 AudioClock::frames_from_audioframes_string (const string& str) const
1976 sscanf (str.c_str(), "%" PRId64, &f);
1981 AudioClock::copy_text_to_clipboard () const
1985 val = pre_edit_string;
1987 val = _layout->get_text ();
1989 const size_t trim = val.find_first_not_of(" ");
1990 if (trim == string::npos) {
1991 assert(0); // empty clock, can't be right.
1994 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
1995 cl->set_text (val.substr(trim));
1999 AudioClock::build_ops_menu ()
2001 using namespace Menu_Helpers;
2002 ops_menu = new Menu;
2003 MenuList& ops_items = ops_menu->items();
2004 ops_menu->set_name ("ArdourContextMenu");
2006 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2007 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2008 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2009 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2011 if (editable && !_off && !is_duration && !_follows_playhead) {
2012 ops_items.push_back (SeparatorElem());
2013 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2014 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2016 ops_items.push_back (SeparatorElem());
2017 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2021 AudioClock::set_from_playhead ()
2027 set (_session->transport_frame());
2032 AudioClock::locate ()
2034 if (!_session || is_duration) {
2038 _session->request_locate (current_time(), _session->transport_rolling ());
2042 AudioClock::set_mode (Mode m, bool noemit)
2052 _layout->set_text ("");
2054 Gtk::Requisition req;
2055 set_clock_dimensions (req);
2059 insert_map.push_back (11);
2060 insert_map.push_back (10);
2061 insert_map.push_back (8);
2062 insert_map.push_back (7);
2063 insert_map.push_back (5);
2064 insert_map.push_back (4);
2065 insert_map.push_back (2);
2066 insert_map.push_back (1);
2070 insert_map.push_back (11);
2071 insert_map.push_back (10);
2072 insert_map.push_back (9);
2073 insert_map.push_back (8);
2074 insert_map.push_back (6);
2075 insert_map.push_back (5);
2076 insert_map.push_back (3);
2077 insert_map.push_back (2);
2078 insert_map.push_back (1);
2082 insert_map.push_back (12);
2083 insert_map.push_back (11);
2084 insert_map.push_back (10);
2085 insert_map.push_back (8);
2086 insert_map.push_back (7);
2087 insert_map.push_back (5);
2088 insert_map.push_back (4);
2089 insert_map.push_back (2);
2090 insert_map.push_back (1);
2097 set (last_when, true);
2099 if (!is_transient && !noemit) {
2100 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2103 mode_changed (); /* EMIT SIGNAL (the member one) */
2107 AudioClock::set_bbt_reference (framepos_t pos)
2109 bbt_reference_time = pos;
2113 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2115 CairoWidget::on_style_changed (old_style);
2117 Gtk::Requisition req;
2118 set_clock_dimensions (req);
2120 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2126 AudioClock::set_editable (bool yn)
2132 AudioClock::set_is_duration (bool yn)
2134 if (yn == is_duration) {
2139 set (last_when, true);
2143 AudioClock::set_off (bool yn)
2151 /* force a redraw. last_when will be preserved, but the clock text will
2155 set (last_when, true);
2159 AudioClock::focus ()
2161 start_edit (Field (0));
2165 AudioClock::set_corner_radius (double r)
2174 AudioClock::dpi_reset ()
2176 /* force recomputation of size even if we are fixed width
2184 AudioClock::set_negative_allowed (bool yn)
2186 _negative_allowed = yn;