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 "gui_thread.h"
45 #include "ui_config.h"
50 using namespace ARDOUR;
51 using namespace ARDOUR_UI_UTILS;
56 using Gtkmm2ext::Keyboard;
58 sigc::signal<void> AudioClock::ModeChanged;
59 vector<AudioClock*> AudioClock::clocks;
61 #define BBT_BAR_CHAR "|"
62 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
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,
66 bool accept_on_focus_out)
69 , is_transient (transient)
70 , is_duration (duration)
71 , editable (allow_edit)
72 , _follows_playhead (follows_playhead)
73 , _accept_on_focus_out (accept_on_focus_out)
76 , _edit_by_click_field (false)
77 , _negative_allowed (false)
78 , edit_is_negative (false)
79 , _with_info (with_info)
84 , style_resets_first (true)
90 , bbt_reference_time (-1)
95 , drag_field (Field (0))
99 set_flags (CAN_FOCUS);
101 _layout = Pango::Layout::create (get_pango_context());
102 _layout->set_attributes (normal_attributes);
104 set_widget_name (widget_name);
106 _mode = BBT; /* lie to force mode switch */
108 set (last_when, true);
111 clocks.push_back (this);
114 _left_btn.set_sizing_text (_("0000000000000"));
115 // NB right_btn is in a size-group
117 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
118 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
120 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
121 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
124 AudioClock::~AudioClock ()
126 delete foreground_attr;
131 AudioClock::set_widget_name (const string& str)
136 set_name (str + " clock");
146 AudioClock::on_realize ()
148 Gtk::Requisition req;
150 CairoWidget::on_realize ();
152 set_clock_dimensions (req);
154 first_width = req.width;
155 first_height = req.height;
157 // XXX FIX ME: define font based on ... ???
163 AudioClock::set_font (Pango::FontDescription font)
165 Glib::RefPtr<Gtk::Style> style = get_style ();
166 Pango::AttrFontDesc* font_attr;
168 font_size = font.get_size();
169 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
171 normal_attributes.change (*font_attr);
172 editing_attributes.change (*font_attr);
175 /* get the figure width for the font. This doesn't have to super
176 * accurate since we only use it to measure the (roughly 1 character)
177 * offset from the position Pango tells us for the "cursor"
180 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
184 tmp->get_pixel_size (em_width, ignore_height);
186 /* force redraw of markup with new font-size */
187 set (last_when, true);
191 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
193 CairoWidget::set_active_state (s);
198 AudioClock::set_colors ()
204 uint32_t editing_color;
205 uint32_t cursor_color;
207 if (active_state()) {
208 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
209 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
210 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
211 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
213 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
214 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
215 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
216 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
219 /* store for bg and cursor in render() */
221 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
228 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
235 /* rescale for Pango colors ... sigh */
237 r = lrint (r * 65535.0);
238 g = lrint (g * 65535.0);
239 b = lrint (b * 65535.0);
241 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
242 r = lrint ((r/255.0) * 65535.0);
243 g = lrint ((g/255.0) * 65535.0);
244 b = lrint ((b/255.0) * 65535.0);
245 delete foreground_attr;
246 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
248 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
249 r = lrint ((r/255.0) * 65535.0);
250 g = lrint ((g/255.0) * 65535.0);
251 b = lrint ((b/255.0) * 65535.0);
253 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
255 normal_attributes.change (*foreground_attr);
256 editing_attributes.change (*foreground_attr);
257 editing_attributes.change (*editing_attr);
260 _layout->set_attributes (normal_attributes);
262 _layout->set_attributes (editing_attributes);
269 AudioClock::set_scale (double x, double y)
278 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
280 /* main layout: rounded rect, plus the text */
283 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
285 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
287 cairo_rectangle (cr, 0, 0, get_width(), get_height());
292 double lw = layout_width * xscale;
293 double lh = layout_height * yscale;
295 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
297 if (xscale != 1.0 || yscale != 1.0) {
299 cairo_scale (cr, xscale, yscale);
302 pango_cairo_show_layout (cr, _layout->gobj());
304 if (xscale != 1.0 || yscale != 1.0) {
309 if (!insert_map.empty()) {
311 int xcenter = (get_width() - layout_width) /2;
313 if (input_string.length() < insert_map.size()) {
314 Pango::Rectangle cursor;
316 if (input_string.empty()) {
317 /* nothing entered yet, put cursor at the end
320 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
322 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
325 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
327 min (get_width() - 2.0,
328 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
329 (get_height() - layout_height)/2.0,
330 2.0, cursor.get_height()/PANGO_SCALE);
333 /* we've entered all possible digits, no cursor */
337 if (input_string.empty()) {
338 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
341 (get_height() - layout_height)/2.0,
350 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
352 Glib::RefPtr<Pango::Layout> tmp;
353 Glib::RefPtr<Gtk::Style> style = get_style ();
354 Pango::FontDescription font;
356 tmp = Pango::Layout::create (get_pango_context());
358 if (!is_realized()) {
359 font = get_font_for_style (get_name());
361 font = style->get_font();
364 tmp->set_font_description (font);
366 /* this string is the longest thing we will ever display */
368 tmp->set_text (" 88:88:88,888 ");
370 tmp->set_text (" 88:88:88,88 ");
371 tmp->get_pixel_size (req.width, req.height);
373 layout_height = req.height;
374 layout_width = req.width;
378 AudioClock::on_size_request (Gtk::Requisition* req)
380 /* even for non fixed width clocks, the size we *ask* for never changes,
381 even though the size we receive might. so once we've computed it,
386 req->width = first_width;
387 req->height = first_height;
391 set_clock_dimensions (*req);
393 /* now tackle height, for which we need to know the height of the lower
399 AudioClock::show_edit_status (int length)
401 editing_attr->set_start_index (edit_string.length() - length);
402 editing_attr->set_end_index (edit_string.length());
404 editing_attributes.change (*foreground_attr);
405 editing_attributes.change (*editing_attr);
407 _layout->set_attributes (editing_attributes);
411 AudioClock::start_edit (Field f)
414 pre_edit_string = _layout->get_text ();
415 if (!insert_map.empty()) {
416 edit_string = pre_edit_string;
418 edit_string.clear ();
419 _layout->set_text ("");
422 input_string.clear ();
424 edit_is_negative = false;
427 input_string = get_field (f);
428 show_edit_status (merge_input_and_edit_string ());
429 _layout->set_text (edit_string);
434 Keyboard::magic_widget_grab_focus ();
440 AudioClock::get_field (Field f)
444 return edit_string.substr (1, 2);
446 case Timecode_Minutes:
447 return edit_string.substr (4, 2);
449 case Timecode_Seconds:
450 return edit_string.substr (7, 2);
452 case Timecode_Frames:
453 return edit_string.substr (10, 2);
456 return edit_string.substr (1, 2);
459 return edit_string.substr (4, 2);
462 return edit_string.substr (7, 2);
464 case MS_Milliseconds:
465 return edit_string.substr (10, 3);
468 return edit_string.substr (1, 3);
471 return edit_string.substr (5, 2);
474 return edit_string.substr (8, 4);
484 AudioClock::end_edit (bool modify)
492 ok = timecode_validate_edit (edit_string);
496 ok = bbt_validate_edit (edit_string);
500 ok = minsec_validate_edit (edit_string);
504 if (edit_string.length() < 1) {
505 edit_string = pre_edit_string;
511 edit_string = pre_edit_string;
512 input_string.clear ();
513 _layout->set_text (edit_string);
514 show_edit_status (0);
515 /* edit attributes remain in use */
519 framepos_t pos = 0; /* stupid gcc */
523 pos = frames_from_timecode_string (edit_string);
528 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
530 pos = frames_from_bbt_string (0, edit_string);
535 pos = frames_from_minsec_string (edit_string);
539 pos = frames_from_audioframes_string (edit_string);
544 _layout->set_attributes (normal_attributes);
545 ValueChanged(); /* EMIT_SIGNAL */
551 edit_is_negative = false;
552 _layout->set_attributes (normal_attributes);
553 _layout->set_text (pre_edit_string);
564 AudioClock::drop_focus ()
566 Keyboard::magic_widget_drop_focus ();
569 /* move focus back to the default widget in the top level window */
570 ARDOUR_UI::instance()->reset_focus (this);
575 AudioClock::parse_as_frames_distance (const std::string& str)
579 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
587 AudioClock::parse_as_minsec_distance (const std::string& str)
589 framecnt_t sr = _session->frame_rate();
595 switch (str.length()) {
602 sscanf (str.c_str(), "%" PRId32, &msecs);
603 return msecs * (sr / 1000);
606 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
607 return (secs * sr) + (msecs * (sr/1000));
610 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
611 return (secs * sr) + (msecs * (sr/1000));
614 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
615 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
618 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
619 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
622 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
623 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
626 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
627 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
637 AudioClock::parse_as_timecode_distance (const std::string& str)
639 double fps = _session->timecode_frames_per_second();
640 framecnt_t sr = _session->frame_rate();
646 switch (str.length()) {
651 sscanf (str.c_str(), "%" PRId32, &frames);
652 return llrint ((frames/(float)fps) * sr);
655 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
656 return (secs * sr) + llrint ((frames/(float)fps) * sr);
659 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
660 return (secs * sr) + llrint ((frames/(float)fps) * sr);
663 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
664 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
667 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
668 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
671 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
672 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
675 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
676 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
686 AudioClock::parse_as_bbt_distance (const std::string&)
692 AudioClock::parse_as_distance (const std::string& instr)
696 return parse_as_timecode_distance (instr);
699 return parse_as_frames_distance (instr);
702 return parse_as_bbt_distance (instr);
705 return parse_as_minsec_distance (instr);
712 AudioClock::end_edit_relative (bool add)
718 ok = timecode_validate_edit (edit_string);
722 ok = bbt_validate_edit (edit_string);
726 ok = minsec_validate_edit (edit_string);
734 edit_string = pre_edit_string;
735 input_string.clear ();
736 _layout->set_text (edit_string);
737 show_edit_status (0);
738 /* edit attributes remain in use */
743 framecnt_t frames = parse_as_distance (input_string);
748 _layout->set_attributes (normal_attributes);
752 set (current_time() + frames, true);
754 framepos_t c = current_time();
756 if (c > frames || _negative_allowed) {
757 set (c - frames, true);
762 ValueChanged (); /* EMIT SIGNAL */
765 input_string.clear ();
771 AudioClock::session_property_changed (const PropertyChange&)
773 set (last_when, true);
777 AudioClock::session_configuration_changed (std::string p)
779 if (_negative_allowed) {
780 /* session option editor clock */
784 if (p == "sync-source" || p == "external-sync") {
785 set (current_time(), true);
789 if (p != "timecode-offset" && p != "timecode-offset-negative") {
798 current = current_duration ();
800 current = current_time ();
810 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
812 if ((!force && !is_visible()) || _session == 0) {
817 when = when - offset;
820 if (when == last_when && !force) {
821 #if 0 // XXX return if no change and no change forced. verify Aug/2014
822 if (_mode != Timecode && _mode != MinSec) {
823 /* may need to force display of TC source
824 * time, so don't return early.
826 /* ^^ Why was that?, delta times?
827 * Timecode FPS, pull-up/down, etc changes
828 * trigger a 'session_property_changed' which
829 * eventually calls set(last_when, true)
831 * re-rendering the clock every 40ms or so just
832 * because we can is not ideal.
847 set_timecode (when, force);
851 set_bbt (when, offset, force);
856 set_minsec (when, force);
860 set_frames (when, force);
866 _left_btn.set_sensitive (btn_en);
867 _right_btn.set_sensitive (btn_en);
868 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
869 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
871 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
872 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
873 _left_btn.set_alignment (.5, .5);
874 _right_btn.set_alignment (.5, .5);
875 set_tooltip (_left_btn, _("Change current tempo"));
876 set_tooltip (_right_btn, _("Change current time signature"));
878 _left_btn.set_elements (ArdourButton::Text);
879 _right_btn.set_elements (ArdourButton::Text);
880 _left_btn.set_alignment (0, .5);
881 _right_btn.set_alignment (1, .5);
882 set_tooltip (_left_btn, _(""));
883 set_tooltip (_right_btn, _(""));
892 AudioClock::set_slave_info ()
898 SyncSource sync_src = Config->get_sync_source();
900 if (_session->config.get_external_sync()) {
901 Slave* slave = _session->slave();
905 _left_btn.set_text (sync_source_to_string (sync_src, true));
906 _right_btn.set_text ("");
910 _left_btn.set_text (sync_source_to_string (sync_src, true));
911 _right_btn.set_text (slave->approximate_current_delta (), true);
913 _left_btn.set_text (_("--pending--"));
914 _right_btn.set_text ("");
921 TimecodeSlave* tcslave;
922 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
923 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
924 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
925 sync_source_to_string(sync_src, true)[0],
926 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
927 matching ? "#66ff66" : "#ff3333"
929 _right_btn.set_text (slave->approximate_current_delta (), true);
932 _left_btn.set_text (_("--pending--"));
933 _right_btn.set_text ("");
938 _left_btn.set_text (string_compose ("%1/%2",
939 _("INT"), sync_source_to_string(sync_src, true)));
940 _right_btn.set_text ("");
945 AudioClock::set_frames (framepos_t when, bool /*force*/)
948 bool negative = false;
951 _layout->set_text (" ----------");
952 _left_btn.set_text ("");
953 _right_btn.set_text ("");
963 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
965 snprintf (buf, sizeof (buf), " %10" PRId64, when);
968 _layout->set_text (buf);
971 framecnt_t rate = _session->frame_rate();
973 if (fmod (rate, 100.0) == 0.0) {
974 sprintf (buf, "%.1fkHz", rate/1000.0);
976 sprintf (buf, "%" PRId64 "Hz", rate);
979 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf));
981 float vid_pullup = _session->config.get_video_pullup();
983 if (vid_pullup == 0.0) {
984 _right_btn.set_text ("");
986 sprintf (buf, _("%+.4f%%"), vid_pullup);
987 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf));
993 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1010 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1011 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1012 mins = (int) floor (left / (frame_rate * 60.0f));
1013 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1014 secs = (int) floor (left / (float) frame_rate);
1015 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1016 millisecs = floor (left * 1000.0 / (float) frame_rate);
1019 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1021 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1027 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1032 _layout->set_text (" --:--:--.---");
1033 _left_btn.set_text ("");
1034 _right_btn.set_text ("");
1039 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1041 _layout->set_text (buf);
1046 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1049 bool negative = false;
1052 _layout->set_text (" --:--:--:--");
1053 _left_btn.set_text ("");
1054 _right_btn.set_text ("");
1064 _session->timecode_duration (when, TC);
1066 _session->timecode_time (when, TC);
1069 TC.negative = TC.negative || negative;
1071 _layout->set_text (Timecode::timecode_format_time(TC));
1077 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1080 Timecode::BBT_Time BBT;
1081 bool negative = false;
1084 _layout->set_text (" ---|--|----");
1085 _left_btn.set_text ("");
1086 _right_btn.set_text ("");
1095 /* handle a common case */
1102 TempoMap& tmap (_session->tempo_map());
1105 offset = bbt_reference_time;
1108 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1109 Timecode::BBT_Time sub_bbt;
1112 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1113 sub_bbt = tmap.bbt_at_frame (offset - when);
1115 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1116 sub_bbt = tmap.bbt_at_frame (offset);
1119 BBT.bars -= sub_bbt.bars;
1121 if (BBT.ticks < sub_bbt.ticks) {
1122 if (BBT.beats == 1) {
1124 BBT.beats = divisions;
1128 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1130 BBT.ticks -= sub_bbt.ticks;
1133 if (BBT.beats < sub_bbt.beats) {
1135 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1137 BBT.beats -= sub_bbt.beats;
1141 BBT = _session->tempo_map().bbt_at_frame (when);
1145 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1146 BBT.bars, BBT.beats, BBT.ticks);
1148 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1149 BBT.bars, BBT.beats, BBT.ticks);
1152 _layout->set_text (buf);
1157 if (bbt_reference_time < 0) {
1160 pos = bbt_reference_time;
1163 TempoMetric m (_session->tempo_map().metric_at (pos));
1165 if (m.tempo().note_type() == 4) {
1166 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1167 _left_btn.set_text (string_compose ("%1", buf));
1168 } else if (m.tempo().note_type() == 8) {
1169 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1170 _left_btn.set_text (string_compose ("%1", buf));
1172 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1173 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf));
1176 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1177 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf));
1182 AudioClock::set_session (Session *s)
1184 SessionHandlePtr::set_session (s);
1188 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1189 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1190 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1191 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1193 XMLProperty const * prop;
1194 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1195 AudioClock::Mode amode;
1198 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1199 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1201 if ((prop = (*i)->property (X_("mode"))) != 0) {
1202 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1203 set_mode (amode, true);
1205 if ((prop = (*i)->property (X_("on"))) != 0) {
1206 set_off (!string_is_affirmative (prop->value()));
1213 set (last_when, true);
1218 AudioClock::on_key_press_event (GdkEventKey* ev)
1226 int highlight_length;
1229 switch (ev->keyval) {
1272 case GDK_KP_Subtract:
1273 if (_negative_allowed && input_string.empty()) {
1274 edit_is_negative = true;
1275 edit_string.replace(0,1,"-");
1276 _layout->set_text (edit_string);
1279 end_edit_relative (false);
1285 end_edit_relative (true);
1298 ChangeAborted(); /* EMIT SIGNAL */
1303 if (!input_string.empty()) {
1304 /* delete the last key entered
1306 input_string = input_string.substr (0, input_string.length() - 1);
1308 goto use_input_string;
1311 /* do not allow other keys to passthru to the rest of the GUI
1317 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1318 /* too many digits: eat the key event, but do nothing with it */
1322 input_string.push_back (new_char);
1328 /* get this one in the right order, and to the right width */
1329 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1330 edit_string = edit_string.substr (0, edit_string.length() - 1);
1332 edit_string.push_back (new_char);
1334 if (!edit_string.empty()) {
1336 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1337 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1340 /* highlight the whole thing */
1341 highlight_length = edit_string.length();
1345 highlight_length = merge_input_and_edit_string ();
1348 if (edit_is_negative) {
1349 edit_string.replace(0,1,"-");
1351 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1352 edit_string.replace(0,1,"_");
1354 edit_string.replace(0,1," ");
1358 show_edit_status (highlight_length);
1359 _layout->set_text (edit_string);
1366 AudioClock::merge_input_and_edit_string ()
1368 /* merge with pre-edit-string into edit string */
1370 edit_string = pre_edit_string;
1372 if (input_string.empty()) {
1376 string::size_type target;
1377 for (string::size_type i = 0; i < input_string.length(); ++i) {
1378 target = insert_map[input_string.length() - 1 - i];
1379 edit_string[target] = input_string[i];
1381 /* highlight from end to wherever the last character was added */
1382 return edit_string.length() - insert_map[input_string.length()-1];
1387 AudioClock::on_key_release_event (GdkEventKey *ev)
1393 /* return true for keys that we used on press
1394 so that they cannot possibly do double-duty
1396 switch (ev->keyval) {
1419 case GDK_KP_Decimal:
1427 case GDK_KP_Subtract:
1435 AudioClock::index_to_field (int index) const
1440 return Timecode_Hours;
1441 } else if (index < 7) {
1442 return Timecode_Minutes;
1443 } else if (index < 10) {
1444 return Timecode_Seconds;
1446 return Timecode_Frames;
1452 } else if (index < 7) {
1460 return Timecode_Hours;
1461 } else if (index < 6) {
1463 } else if (index < 9) {
1466 return MS_Milliseconds;
1478 AudioClock::on_button_press_event (GdkEventButton *ev)
1480 switch (ev->button) {
1482 if (editable && !_off) {
1488 /* the text has been centered vertically, so adjust
1491 int xcenter = (get_width() - layout_width) /2;
1493 y = ev->y - ((get_height() - layout_height)/2);
1494 x = ev->x - xcenter;
1496 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1497 /* pretend it is a character on the far right */
1500 drag_field = index_to_field (index);
1502 /* make absolutely sure that the pointer is grabbed */
1503 gdk_pointer_grab(ev->window,false ,
1504 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1505 NULL,NULL,ev->time);
1507 drag_start_y = ev->y;
1521 AudioClock::on_button_release_event (GdkEventButton *ev)
1523 if (editable && !_off) {
1525 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1527 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1528 // we actually dragged so return without
1529 // setting editing focus, or we shift clicked
1532 if (ev->button == 1) {
1534 if (_edit_by_click_field) {
1536 int xcenter = (get_width() - layout_width) /2;
1539 int y = ev->y - ((get_height() - layout_height)/2);
1540 int x = ev->x - xcenter;
1543 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1547 f = index_to_field (index);
1550 case Timecode_Frames:
1551 case MS_Milliseconds:
1567 if (Keyboard::is_context_menu_event (ev)) {
1568 if (ops_menu == 0) {
1571 ops_menu->popup (1, ev->time);
1579 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1581 bool ret = CairoWidget::on_focus_out_event (ev);
1584 end_edit (_accept_on_focus_out);
1591 AudioClock::on_scroll_event (GdkEventScroll *ev)
1596 if (editing || _session == 0 || !editable || _off) {
1603 /* the text has been centered vertically, so adjust
1607 int xcenter = (get_width() - layout_width) /2;
1608 y = ev->y - ((get_height() - layout_height)/2);
1609 x = ev->x - xcenter;
1611 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1612 /* not in the main layout */
1616 Field f = index_to_field (index);
1617 framepos_t frames = 0;
1619 switch (ev->direction) {
1622 frames = get_frame_step (f, current_time(), 1);
1624 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1627 set (current_time() + frames, true);
1628 ValueChanged (); /* EMIT_SIGNAL */
1632 case GDK_SCROLL_DOWN:
1633 frames = get_frame_step (f, current_time(), -1);
1635 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1639 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1642 set (current_time() - frames, true);
1645 ValueChanged (); /* EMIT_SIGNAL */
1658 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1660 if (editing || _session == 0 || !dragging) {
1664 float pixel_frame_scale_factor = 0.2f;
1666 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1667 pixel_frame_scale_factor = 0.1f;
1671 if (Keyboard::modifier_state_contains (ev->state,
1672 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1674 pixel_frame_scale_factor = 0.025f;
1677 double y_delta = ev->y - drag_y;
1679 drag_accum += y_delta*pixel_frame_scale_factor;
1683 if (floor (drag_accum) != 0) {
1688 dir = (drag_accum < 0 ? 1:-1);
1689 pos = current_time();
1690 frames = get_frame_step (drag_field, pos, dir);
1692 if (frames != 0 && frames * drag_accum < current_time()) {
1693 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1699 ValueChanged(); /* EMIT_SIGNAL */
1706 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1709 Timecode::BBT_Time BBT;
1711 case Timecode_Hours:
1712 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1714 case Timecode_Minutes:
1715 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1717 case Timecode_Seconds:
1718 f = _session->frame_rate();
1720 case Timecode_Frames:
1721 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1729 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1732 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1735 f = (framecnt_t) _session->frame_rate();
1737 case MS_Milliseconds:
1738 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1745 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1751 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1757 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1760 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1769 AudioClock::current_time (framepos_t) const
1775 AudioClock::current_duration (framepos_t pos) const
1784 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1800 AudioClock::bbt_validate_edit (const string& str)
1804 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1808 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1812 if (!is_duration && any.bbt.bars == 0) {
1816 if (!is_duration && any.bbt.beats == 0) {
1824 AudioClock::timecode_validate_edit (const string&)
1830 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1831 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1836 TC.hours = hours * -1;
1840 TC.negative = false;
1843 if (TC.negative && !_negative_allowed) {
1847 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1851 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1855 if (_session->timecode_drop_frames()) {
1856 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1865 AudioClock::minsec_validate_edit (const string& str)
1867 int hrs, mins, secs, millisecs;
1869 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1873 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1881 AudioClock::frames_from_timecode_string (const string& str) const
1883 if (_session == 0) {
1892 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1893 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1896 TC.hours = abs(hours);
1897 TC.rate = _session->timecode_frames_per_second();
1898 TC.drop= _session->timecode_drop_frames();
1900 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1902 // timecode_tester ();
1903 if (edit_is_negative) {
1911 AudioClock::frames_from_minsec_string (const string& str) const
1913 if (_session == 0) {
1917 int hrs, mins, secs, millisecs;
1918 framecnt_t sr = _session->frame_rate();
1920 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1921 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1925 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1929 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1931 if (_session == 0) {
1932 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1937 any.type = AnyTime::BBT;
1939 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1946 return _session->any_duration_to_frames (pos, any);
1948 return _session->convert_to_frames (any);
1954 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1956 if (_session == 0) {
1957 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
1961 Timecode::BBT_Time bbt;
1963 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1967 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1971 AudioClock::frames_from_audioframes_string (const string& str) const
1974 sscanf (str.c_str(), "%" PRId64, &f);
1979 AudioClock::copy_text_to_clipboard () const
1983 val = pre_edit_string;
1985 val = _layout->get_text ();
1987 const size_t trim = val.find_first_not_of(" ");
1988 if (trim == string::npos) {
1989 assert(0); // empty clock, can't be right.
1992 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
1993 cl->set_text (val.substr(trim));
1997 AudioClock::build_ops_menu ()
1999 using namespace Menu_Helpers;
2000 ops_menu = new Menu;
2001 MenuList& ops_items = ops_menu->items();
2002 ops_menu->set_name ("ArdourContextMenu");
2004 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2005 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2006 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2007 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2009 if (editable && !_off && !is_duration && !_follows_playhead) {
2010 ops_items.push_back (SeparatorElem());
2011 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2012 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2014 ops_items.push_back (SeparatorElem());
2015 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2019 AudioClock::set_from_playhead ()
2025 set (_session->transport_frame());
2030 AudioClock::locate ()
2032 if (!_session || is_duration) {
2036 _session->request_locate (current_time(), _session->transport_rolling ());
2040 AudioClock::set_mode (Mode m, bool noemit)
2050 _layout->set_text ("");
2052 Gtk::Requisition req;
2053 set_clock_dimensions (req);
2057 insert_map.push_back (11);
2058 insert_map.push_back (10);
2059 insert_map.push_back (8);
2060 insert_map.push_back (7);
2061 insert_map.push_back (5);
2062 insert_map.push_back (4);
2063 insert_map.push_back (2);
2064 insert_map.push_back (1);
2068 insert_map.push_back (11);
2069 insert_map.push_back (10);
2070 insert_map.push_back (9);
2071 insert_map.push_back (8);
2072 insert_map.push_back (6);
2073 insert_map.push_back (5);
2074 insert_map.push_back (3);
2075 insert_map.push_back (2);
2076 insert_map.push_back (1);
2080 insert_map.push_back (12);
2081 insert_map.push_back (11);
2082 insert_map.push_back (10);
2083 insert_map.push_back (8);
2084 insert_map.push_back (7);
2085 insert_map.push_back (5);
2086 insert_map.push_back (4);
2087 insert_map.push_back (2);
2088 insert_map.push_back (1);
2095 set (last_when, true);
2097 if (!is_transient && !noemit) {
2098 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2101 mode_changed (); /* EMIT SIGNAL (the member one) */
2105 AudioClock::set_bbt_reference (framepos_t pos)
2107 bbt_reference_time = pos;
2111 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2113 CairoWidget::on_style_changed (old_style);
2115 Gtk::Requisition req;
2116 set_clock_dimensions (req);
2118 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2124 AudioClock::set_editable (bool yn)
2130 AudioClock::set_is_duration (bool yn)
2132 if (yn == is_duration) {
2137 set (last_when, true);
2141 AudioClock::set_off (bool yn)
2149 /* force a redraw. last_when will be preserved, but the clock text will
2153 set (last_when, true);
2157 AudioClock::focus ()
2159 start_edit (Field (0));
2163 AudioClock::set_corner_radius (double r)
2172 AudioClock::dpi_reset ()
2174 /* force recomputation of size even if we are fixed width
2182 AudioClock::set_negative_allowed (bool yn)
2184 _negative_allowed = yn;