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 "widgets/tooltips.h"
35 #include "ardour/profile.h"
36 #include "ardour/lmath.h"
37 #include "ardour/session.h"
38 #include "ardour/slave.h"
39 #include "ardour/tempo.h"
40 #include "ardour/types.h"
42 #include "ardour_ui.h"
43 #include "audio_clock.h"
44 #include "enums_convert.h"
45 #include "gui_thread.h"
47 #include "ui_config.h"
52 using namespace ARDOUR;
53 using namespace ARDOUR_UI_UTILS;
54 using namespace ArdourWidgets;
59 using Gtkmm2ext::Keyboard;
61 sigc::signal<void> AudioClock::ModeChanged;
62 vector<AudioClock*> AudioClock::clocks;
64 #define BBT_BAR_CHAR "|"
65 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
67 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
68 bool allow_edit, bool follows_playhead, bool duration, bool with_info,
69 bool accept_on_focus_out)
72 , is_transient (transient)
73 , is_duration (duration)
74 , editable (allow_edit)
75 , _follows_playhead (follows_playhead)
76 , _accept_on_focus_out (accept_on_focus_out)
79 , _edit_by_click_field (false)
80 , _negative_allowed (false)
81 , edit_is_negative (false)
82 , _limit_pos (INT64_MAX - 1)
83 , _with_info (with_info)
88 , style_resets_first (true)
94 , bbt_reference_time (-1)
99 , drag_field (Field (0))
103 set_flags (CAN_FOCUS);
105 _layout = Pango::Layout::create (get_pango_context());
106 _layout->set_attributes (normal_attributes);
108 set_widget_name (widget_name);
110 _mode = BBT; /* lie to force mode switch */
112 set (last_when, true);
115 clocks.push_back (this);
118 _left_btn.set_sizing_text (_("0000000000000"));
119 // NB right_btn is in a size-group
121 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
122 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
124 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
125 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
128 AudioClock::~AudioClock ()
130 delete foreground_attr;
135 AudioClock::set_widget_name (const string& str)
140 set_name (str + " clock");
150 AudioClock::on_realize ()
152 Gtk::Requisition req;
154 CairoWidget::on_realize ();
156 set_clock_dimensions (req);
158 first_width = req.width;
159 first_height = req.height;
161 // XXX FIX ME: define font based on ... ???
167 AudioClock::set_font (Pango::FontDescription font)
169 Glib::RefPtr<Gtk::Style> style = get_style ();
170 Pango::AttrFontDesc* font_attr;
172 font_size = font.get_size();
173 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
175 normal_attributes.change (*font_attr);
176 editing_attributes.change (*font_attr);
179 /* get the figure width for the font. This doesn't have to super
180 * accurate since we only use it to measure the (roughly 1 character)
181 * offset from the position Pango tells us for the "cursor"
184 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
188 tmp->get_pixel_size (em_width, ignore_height);
190 /* force redraw of markup with new font-size */
191 set (last_when, true);
195 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
197 CairoWidget::set_active_state (s);
202 AudioClock::set_colors ()
208 uint32_t editing_color;
209 uint32_t cursor_color;
211 if (active_state()) {
212 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
213 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
214 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
215 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
217 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
218 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
219 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
220 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
223 /* store for bg and cursor in render() */
225 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
232 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
239 /* rescale for Pango colors ... sigh */
241 r = lrint (r * 65535.0);
242 g = lrint (g * 65535.0);
243 b = lrint (b * 65535.0);
245 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
246 r = lrint ((r/255.0) * 65535.0);
247 g = lrint ((g/255.0) * 65535.0);
248 b = lrint ((b/255.0) * 65535.0);
249 delete foreground_attr;
250 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
252 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
253 r = lrint ((r/255.0) * 65535.0);
254 g = lrint ((g/255.0) * 65535.0);
255 b = lrint ((b/255.0) * 65535.0);
257 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
259 normal_attributes.change (*foreground_attr);
260 editing_attributes.change (*foreground_attr);
261 editing_attributes.change (*editing_attr);
264 _layout->set_attributes (normal_attributes);
266 _layout->set_attributes (editing_attributes);
273 AudioClock::set_scale (double x, double y)
282 AudioClock::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
284 cairo_t* cr = ctx->cobj();
285 /* main layout: rounded rect, plus the text */
288 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
290 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
292 cairo_rectangle (cr, 0, 0, get_width(), get_height());
297 double lw = layout_width * xscale;
298 double lh = layout_height * yscale;
300 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
302 if (xscale != 1.0 || yscale != 1.0) {
304 cairo_scale (cr, xscale, yscale);
307 pango_cairo_show_layout (cr, _layout->gobj());
309 if (xscale != 1.0 || yscale != 1.0) {
314 if (!insert_map.empty()) {
316 int xcenter = (get_width() - layout_width) /2;
318 if (input_string.length() < insert_map.size()) {
319 Pango::Rectangle cursor;
321 if (input_string.empty()) {
322 /* nothing entered yet, put cursor at the end
325 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
327 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
330 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
332 min (get_width() - 2.0,
333 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
334 (get_height() - layout_height)/2.0,
335 2.0, cursor.get_height()/PANGO_SCALE);
338 /* we've entered all possible digits, no cursor */
342 if (input_string.empty()) {
343 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
346 (get_height() - layout_height)/2.0,
355 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
357 Glib::RefPtr<Pango::Layout> tmp;
358 Glib::RefPtr<Gtk::Style> style = get_style ();
359 Pango::FontDescription font;
361 tmp = Pango::Layout::create (get_pango_context());
363 if (!is_realized()) {
364 font = get_font_for_style (get_name());
366 font = style->get_font();
369 tmp->set_font_description (font);
371 /* this string is the longest thing we will ever display */
373 tmp->set_text (" 88:88:88,888 ");
375 tmp->set_text (" 88:88:88,88 ");
376 tmp->get_pixel_size (req.width, req.height);
378 layout_height = req.height;
379 layout_width = req.width;
383 AudioClock::on_size_request (Gtk::Requisition* req)
385 /* even for non fixed width clocks, the size we *ask* for never changes,
386 even though the size we receive might. so once we've computed it,
391 req->width = first_width;
392 req->height = first_height;
396 set_clock_dimensions (*req);
398 /* now tackle height, for which we need to know the height of the lower
404 AudioClock::show_edit_status (int length)
406 editing_attr->set_start_index (edit_string.length() - length);
407 editing_attr->set_end_index (edit_string.length());
409 editing_attributes.change (*foreground_attr);
410 editing_attributes.change (*editing_attr);
412 _layout->set_attributes (editing_attributes);
416 AudioClock::start_edit (Field f)
419 pre_edit_string = _layout->get_text ();
420 if (!insert_map.empty()) {
421 edit_string = pre_edit_string;
423 edit_string.clear ();
424 _layout->set_text ("");
427 input_string.clear ();
429 edit_is_negative = false;
432 input_string = get_field (f);
433 show_edit_status (merge_input_and_edit_string ());
434 _layout->set_text (edit_string);
439 Keyboard::magic_widget_grab_focus ();
445 AudioClock::get_field (Field f)
449 return edit_string.substr (1, 2);
451 case Timecode_Minutes:
452 return edit_string.substr (4, 2);
454 case Timecode_Seconds:
455 return edit_string.substr (7, 2);
457 case Timecode_Frames:
458 return edit_string.substr (10, 2);
461 return edit_string.substr (1, 2);
464 return edit_string.substr (4, 2);
467 return edit_string.substr (7, 2);
469 case MS_Milliseconds:
470 return edit_string.substr (10, 3);
473 return edit_string.substr (1, 3);
476 return edit_string.substr (5, 2);
479 return edit_string.substr (8, 4);
489 AudioClock::end_edit (bool modify)
497 ok = timecode_validate_edit (edit_string);
501 ok = bbt_validate_edit (edit_string);
505 ok = minsec_validate_edit (edit_string);
509 if (edit_string.length() < 1) {
510 edit_string = pre_edit_string;
516 edit_string = pre_edit_string;
517 input_string.clear ();
518 _layout->set_text (edit_string);
519 show_edit_status (0);
520 /* edit attributes remain in use */
524 framepos_t pos = 0; /* stupid gcc */
528 pos = frames_from_timecode_string (edit_string);
533 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
535 pos = frames_from_bbt_string (0, edit_string);
540 pos = frames_from_minsec_string (edit_string);
544 pos = frames_from_audioframes_string (edit_string);
549 _layout->set_attributes (normal_attributes);
550 ValueChanged(); /* EMIT_SIGNAL */
556 edit_is_negative = false;
557 _layout->set_attributes (normal_attributes);
558 _layout->set_text (pre_edit_string);
569 AudioClock::drop_focus ()
571 Keyboard::magic_widget_drop_focus ();
574 /* move focus back to the default widget in the top level window */
575 ARDOUR_UI::instance()->reset_focus (this);
580 AudioClock::parse_as_frames_distance (const std::string& str)
584 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
592 AudioClock::parse_as_minsec_distance (const std::string& str)
594 framecnt_t sr = _session->frame_rate();
600 switch (str.length()) {
607 sscanf (str.c_str(), "%" PRId32, &msecs);
608 return msecs * (sr / 1000);
611 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
612 return (secs * sr) + (msecs * (sr/1000));
615 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
616 return (secs * sr) + (msecs * (sr/1000));
619 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
620 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
623 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
624 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
627 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
628 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
631 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
632 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
642 AudioClock::parse_as_timecode_distance (const std::string& str)
644 double fps = _session->timecode_frames_per_second();
645 framecnt_t sr = _session->frame_rate();
651 switch (str.length()) {
656 sscanf (str.c_str(), "%" PRId32, &frames);
657 return llrint ((frames/(float)fps) * sr);
660 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
661 return (secs * sr) + llrint ((frames/(float)fps) * sr);
664 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
665 return (secs * sr) + llrint ((frames/(float)fps) * sr);
668 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
669 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
672 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
673 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
676 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
677 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
680 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
681 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
691 AudioClock::parse_as_bbt_distance (const std::string&)
697 AudioClock::parse_as_distance (const std::string& instr)
701 return parse_as_timecode_distance (instr);
704 return parse_as_frames_distance (instr);
707 return parse_as_bbt_distance (instr);
710 return parse_as_minsec_distance (instr);
717 AudioClock::end_edit_relative (bool add)
723 ok = timecode_validate_edit (edit_string);
727 ok = bbt_validate_edit (edit_string);
731 ok = minsec_validate_edit (edit_string);
739 edit_string = pre_edit_string;
740 input_string.clear ();
741 _layout->set_text (edit_string);
742 show_edit_status (0);
743 /* edit attributes remain in use */
748 framecnt_t frames = parse_as_distance (input_string);
753 _layout->set_attributes (normal_attributes);
757 set (current_time() + frames, true);
759 framepos_t c = current_time();
761 if (c > frames || _negative_allowed) {
762 set (c - frames, true);
767 ValueChanged (); /* EMIT SIGNAL */
770 input_string.clear ();
776 AudioClock::session_property_changed (const PropertyChange&)
778 set (last_when, true);
782 AudioClock::session_configuration_changed (std::string p)
784 if (_negative_allowed) {
785 /* session option editor clock */
789 if (p == "sync-source" || p == "external-sync") {
790 set (current_time(), true);
794 if (p != "timecode-offset" && p != "timecode-offset-negative") {
803 current = current_duration ();
805 current = current_time ();
815 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
817 if ((!force && !is_visible()) || _session == 0) {
822 when = when - offset;
825 if (when > _limit_pos) {
827 } else if (when < -_limit_pos) {
831 if (when == last_when && !force) {
832 #if 0 // XXX return if no change and no change forced. verify Aug/2014
833 if (_mode != Timecode && _mode != MinSec) {
834 /* may need to force display of TC source
835 * time, so don't return early.
837 /* ^^ Why was that?, delta times?
838 * Timecode FPS, pull-up/down, etc changes
839 * trigger a 'session_property_changed' which
840 * eventually calls set(last_when, true)
842 * re-rendering the clock every 40ms or so just
843 * because we can is not ideal.
858 set_timecode (when, force);
862 set_bbt (when, offset, force);
867 set_minsec (when, force);
871 set_frames (when, force);
877 _left_btn.set_sensitive (btn_en);
878 _right_btn.set_sensitive (btn_en);
879 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
880 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
882 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
883 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
884 _left_btn.set_alignment (.5, .5);
885 _right_btn.set_alignment (.5, .5);
886 set_tooltip (_left_btn, _("Change current tempo"));
887 set_tooltip (_right_btn, _("Change current time signature"));
889 _left_btn.set_elements (ArdourButton::Text);
890 _right_btn.set_elements (ArdourButton::Text);
891 _left_btn.set_alignment (0, .5);
892 _right_btn.set_alignment (1, .5);
893 set_tooltip (_left_btn, _(""));
894 set_tooltip (_right_btn, _(""));
903 AudioClock::set_slave_info ()
909 SyncSource sync_src = Config->get_sync_source();
911 if (_session->config.get_external_sync()) {
912 Slave* slave = _session->slave();
916 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
917 _right_btn.set_text ("", true);
921 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
922 _right_btn.set_text (slave->approximate_current_delta (), true);
924 _left_btn.set_text (_("--pending--"), true);
925 _right_btn.set_text ("", true);
932 TimecodeSlave* tcslave;
933 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
934 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
935 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
936 sync_source_to_string(sync_src, true)[0],
937 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
938 matching ? "#66ff66" : "#ff3333"
940 _right_btn.set_text (slave->approximate_current_delta (), true);
943 _left_btn.set_text (_("--pending--"), true);
944 _right_btn.set_text ("", true);
949 _left_btn.set_text (string_compose ("%1/%2",
950 _("INT"), sync_source_to_string(sync_src, true)), true);
951 _right_btn.set_text ("", true);
956 AudioClock::set_out_of_bounds (bool negative)
960 _layout->set_text (" >>> -- <<< ");
962 _layout->set_text (" >>> ++ <<< ");
966 _layout->set_text (" <<<<<<<<<< ");
968 _layout->set_text (" >>>>>>>>>> ");
974 AudioClock::set_frames (framepos_t when, bool /*force*/)
977 bool negative = false;
980 _layout->set_text (" ----------");
981 _left_btn.set_text ("", true);
982 _right_btn.set_text ("", true);
991 if (when >= _limit_pos) {
992 set_out_of_bounds (negative);
993 } else if (negative) {
994 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
995 _layout->set_text (buf);
997 snprintf (buf, sizeof (buf), " %10" PRId64, when);
998 _layout->set_text (buf);
1002 framecnt_t rate = _session->frame_rate();
1004 if (fmod (rate, 100.0) == 0.0) {
1005 sprintf (buf, "%.1fkHz", rate/1000.0);
1007 sprintf (buf, "%" PRId64 "Hz", rate);
1010 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
1012 float vid_pullup = _session->config.get_video_pullup();
1014 if (vid_pullup == 0.0) {
1015 _right_btn.set_text ("", true);
1017 sprintf (buf, _("%+.4f%%"), vid_pullup);
1018 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
1024 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1041 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1042 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1043 mins = (int) floor (left / (frame_rate * 60.0f));
1044 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1045 secs = (int) floor (left / (float) frame_rate);
1046 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1047 millisecs = floor (left * 1000.0 / (float) frame_rate);
1050 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1052 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1058 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1063 _layout->set_text (" --:--:--.---");
1064 _left_btn.set_text ("", true);
1065 _right_btn.set_text ("", true);
1070 if (when >= _limit_pos || when <= -_limit_pos) {
1071 set_out_of_bounds (when < 0);
1073 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1074 _layout->set_text (buf);
1081 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1084 bool negative = false;
1087 _layout->set_text (" --:--:--:--");
1088 _left_btn.set_text ("", true);
1089 _right_btn.set_text ("", true);
1097 if (when >= _limit_pos) {
1098 set_out_of_bounds (negative);
1104 _session->timecode_duration (when, TC);
1106 _session->timecode_time (when, TC);
1109 TC.negative = TC.negative || negative;
1111 _layout->set_text (Timecode::timecode_format_time(TC));
1117 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1120 Timecode::BBT_Time BBT;
1121 bool negative = false;
1123 if (_off || when >= _limit_pos || when < -_limit_pos) {
1124 _layout->set_text (" ---|--|----");
1125 _left_btn.set_text ("", true);
1126 _right_btn.set_text ("", true);
1135 /* handle a common case */
1142 TempoMap& tmap (_session->tempo_map());
1145 offset = bbt_reference_time;
1148 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1149 Timecode::BBT_Time sub_bbt;
1152 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1153 sub_bbt = tmap.bbt_at_frame (offset - when);
1155 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1156 sub_bbt = tmap.bbt_at_frame (offset);
1159 BBT.bars -= sub_bbt.bars;
1161 if (BBT.ticks < sub_bbt.ticks) {
1162 if (BBT.beats == 1) {
1164 BBT.beats = divisions;
1168 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1170 BBT.ticks -= sub_bbt.ticks;
1173 if (BBT.beats < sub_bbt.beats) {
1175 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1177 BBT.beats -= sub_bbt.beats;
1181 BBT = _session->tempo_map().bbt_at_frame (when);
1185 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1186 BBT.bars, BBT.beats, BBT.ticks);
1188 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1189 BBT.bars, BBT.beats, BBT.ticks);
1192 _layout->set_text (buf);
1197 if (bbt_reference_time < 0) {
1200 pos = bbt_reference_time;
1203 TempoMetric m (_session->tempo_map().metric_at (pos));
1205 if (m.tempo().note_type() == 4) {
1206 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1207 _left_btn.set_text (string_compose ("%1", buf), true);
1208 } else if (m.tempo().note_type() == 8) {
1209 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1210 _left_btn.set_text (string_compose ("%1", buf), true);
1212 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1213 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1216 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1217 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1222 AudioClock::set_session (Session *s)
1224 SessionHandlePtr::set_session (s);
1228 int64_t limit_sec = UIConfiguration::instance().get_clock_display_limit ();
1229 if (limit_sec > 0) {
1230 _limit_pos = (framecnt_t) floor ((double)(limit_sec * _session->frame_rate()));
1233 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1234 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1235 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1236 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1238 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1241 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1243 if ((*i)->get_property (X_("name"), name) && name == _name) {
1245 AudioClock::Mode amode;
1246 if ((*i)->get_property (X_("mode"), amode)) {
1247 set_mode (amode, true);
1250 if ((*i)->get_property (X_("on"), on)) {
1258 set (last_when, true);
1263 AudioClock::on_key_press_event (GdkEventKey* ev)
1271 int highlight_length;
1274 switch (ev->keyval) {
1317 case GDK_KP_Subtract:
1318 if (_negative_allowed && input_string.empty()) {
1319 edit_is_negative = true;
1320 edit_string.replace(0,1,"-");
1321 _layout->set_text (edit_string);
1324 end_edit_relative (false);
1330 end_edit_relative (true);
1343 ChangeAborted(); /* EMIT SIGNAL */
1348 if (!input_string.empty()) {
1349 /* delete the last key entered
1351 input_string = input_string.substr (0, input_string.length() - 1);
1353 goto use_input_string;
1356 /* do not allow other keys to passthru to the rest of the GUI
1362 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1363 /* too many digits: eat the key event, but do nothing with it */
1367 input_string.push_back (new_char);
1373 /* get this one in the right order, and to the right width */
1374 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1375 edit_string = edit_string.substr (0, edit_string.length() - 1);
1377 edit_string.push_back (new_char);
1379 if (!edit_string.empty()) {
1381 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1382 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1385 /* highlight the whole thing */
1386 highlight_length = edit_string.length();
1390 highlight_length = merge_input_and_edit_string ();
1393 if (edit_is_negative) {
1394 edit_string.replace(0,1,"-");
1396 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1397 edit_string.replace(0,1,"_");
1399 edit_string.replace(0,1," ");
1403 show_edit_status (highlight_length);
1404 _layout->set_text (edit_string);
1411 AudioClock::merge_input_and_edit_string ()
1413 /* merge with pre-edit-string into edit string */
1415 edit_string = pre_edit_string;
1417 if (input_string.empty()) {
1421 string::size_type target;
1422 for (string::size_type i = 0; i < input_string.length(); ++i) {
1423 target = insert_map[input_string.length() - 1 - i];
1424 edit_string[target] = input_string[i];
1426 /* highlight from end to wherever the last character was added */
1427 return edit_string.length() - insert_map[input_string.length()-1];
1432 AudioClock::on_key_release_event (GdkEventKey *ev)
1438 /* return true for keys that we used on press
1439 so that they cannot possibly do double-duty
1441 switch (ev->keyval) {
1464 case GDK_KP_Decimal:
1472 case GDK_KP_Subtract:
1480 AudioClock::index_to_field (int index) const
1485 return Timecode_Hours;
1486 } else if (index < 7) {
1487 return Timecode_Minutes;
1488 } else if (index < 10) {
1489 return Timecode_Seconds;
1491 return Timecode_Frames;
1497 } else if (index < 7) {
1505 return Timecode_Hours;
1506 } else if (index < 6) {
1508 } else if (index < 9) {
1511 return MS_Milliseconds;
1523 AudioClock::on_button_press_event (GdkEventButton *ev)
1525 if (!_session || _session->actively_recording()) {
1526 /* swallow event, do nothing */
1530 switch (ev->button) {
1532 if (editable && !_off) {
1538 /* the text has been centered vertically, so adjust
1541 int xcenter = (get_width() - layout_width) /2;
1543 y = ev->y - ((get_height() - layout_height)/2);
1544 x = ev->x - xcenter;
1546 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1547 /* pretend it is a character on the far right */
1550 drag_field = index_to_field (index);
1552 /* make absolutely sure that the pointer is grabbed */
1553 gdk_pointer_grab(ev->window,false ,
1554 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1555 NULL,NULL,ev->time);
1557 drag_start_y = ev->y;
1571 AudioClock::on_button_release_event (GdkEventButton *ev)
1573 if (!_session || _session->actively_recording()) {
1574 /* swallow event, do nothing */
1578 if (editable && !_off) {
1580 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1582 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1583 // we actually dragged so return without
1584 // setting editing focus, or we shift clicked
1587 if (ev->button == 1) {
1589 if (_edit_by_click_field) {
1591 int xcenter = (get_width() - layout_width) /2;
1594 int y = ev->y - ((get_height() - layout_height)/2);
1595 int x = ev->x - xcenter;
1598 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1602 f = index_to_field (index);
1605 case Timecode_Frames:
1606 case MS_Milliseconds:
1622 if (Keyboard::is_context_menu_event (ev)) {
1623 if (ops_menu == 0) {
1626 ops_menu->popup (1, ev->time);
1634 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1636 bool ret = CairoWidget::on_focus_out_event (ev);
1639 end_edit (_accept_on_focus_out);
1646 AudioClock::on_scroll_event (GdkEventScroll *ev)
1651 if (editing || _session == 0 || !editable || _off || _session->actively_recording()) {
1658 /* the text has been centered vertically, so adjust
1662 int xcenter = (get_width() - layout_width) /2;
1663 y = ev->y - ((get_height() - layout_height)/2);
1664 x = ev->x - xcenter;
1666 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1667 /* not in the main layout */
1671 Field f = index_to_field (index);
1672 framepos_t frames = 0;
1674 switch (ev->direction) {
1677 frames = get_frame_step (f, current_time(), 1);
1679 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1682 set (current_time() + frames, true);
1683 ValueChanged (); /* EMIT_SIGNAL */
1687 case GDK_SCROLL_DOWN:
1688 frames = get_frame_step (f, current_time(), -1);
1690 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1694 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1697 set (current_time() - frames, true);
1700 ValueChanged (); /* EMIT_SIGNAL */
1713 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1715 if (editing || _session == 0 || !dragging || _session->actively_recording()) {
1719 float pixel_frame_scale_factor = 0.2f;
1721 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1722 pixel_frame_scale_factor = 0.1f;
1726 if (Keyboard::modifier_state_contains (ev->state,
1727 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1728 pixel_frame_scale_factor = 0.025f;
1731 double y_delta = ev->y - drag_y;
1733 drag_accum += y_delta*pixel_frame_scale_factor;
1737 if (floor (drag_accum) != 0) {
1742 dir = (drag_accum < 0 ? 1:-1);
1743 pos = current_time();
1744 frames = get_frame_step (drag_field, pos, dir);
1746 if (frames != 0 && frames * drag_accum < current_time()) {
1747 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1753 ValueChanged(); /* EMIT_SIGNAL */
1760 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1763 Timecode::BBT_Time BBT;
1765 case Timecode_Hours:
1766 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1768 case Timecode_Minutes:
1769 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1771 case Timecode_Seconds:
1772 f = _session->frame_rate();
1774 case Timecode_Frames:
1775 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1783 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1786 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1789 f = (framecnt_t) _session->frame_rate();
1791 case MS_Milliseconds:
1792 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1799 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1805 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1811 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1814 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1823 AudioClock::current_time (framepos_t) const
1829 AudioClock::current_duration (framepos_t pos) const
1838 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1854 AudioClock::bbt_validate_edit (const string& str)
1858 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1862 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1866 if (!is_duration && any.bbt.bars == 0) {
1870 if (!is_duration && any.bbt.beats == 0) {
1878 AudioClock::timecode_validate_edit (const string&)
1884 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1885 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1890 TC.hours = hours * -1;
1894 TC.negative = false;
1897 if (TC.negative && !_negative_allowed) {
1901 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1905 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1909 if (_session->timecode_drop_frames()) {
1910 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1919 AudioClock::minsec_validate_edit (const string& str)
1921 int hrs, mins, secs, millisecs;
1923 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1927 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1935 AudioClock::frames_from_timecode_string (const string& str) const
1937 if (_session == 0) {
1946 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1947 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1950 TC.hours = abs(hours);
1951 TC.rate = _session->timecode_frames_per_second();
1952 TC.drop= _session->timecode_drop_frames();
1954 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1956 // timecode_tester ();
1957 if (edit_is_negative) {
1965 AudioClock::frames_from_minsec_string (const string& str) const
1967 if (_session == 0) {
1971 int hrs, mins, secs, millisecs;
1972 framecnt_t sr = _session->frame_rate();
1974 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1975 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1979 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1983 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1985 if (_session == 0) {
1986 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1991 any.type = AnyTime::BBT;
1993 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2000 return _session->any_duration_to_frames (pos, any);
2002 return _session->convert_to_frames (any);
2008 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2010 if (_session == 0) {
2011 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2015 Timecode::BBT_Time bbt;
2017 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2021 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2025 AudioClock::frames_from_audioframes_string (const string& str) const
2028 sscanf (str.c_str(), "%" PRId64, &f);
2033 AudioClock::copy_text_to_clipboard () const
2037 val = pre_edit_string;
2039 val = _layout->get_text ();
2041 const size_t trim = val.find_first_not_of(" ");
2042 if (trim == string::npos) {
2043 assert(0); // empty clock, can't be right.
2046 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2047 cl->set_text (val.substr(trim));
2051 AudioClock::build_ops_menu ()
2053 using namespace Menu_Helpers;
2054 ops_menu = new Menu;
2055 MenuList& ops_items = ops_menu->items();
2056 ops_menu->set_name ("ArdourContextMenu");
2058 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2059 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2060 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2061 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2063 if (editable && !_off && !is_duration && !_follows_playhead) {
2064 ops_items.push_back (SeparatorElem());
2065 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2066 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2068 ops_items.push_back (SeparatorElem());
2069 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2073 AudioClock::set_from_playhead ()
2079 set (_session->transport_frame());
2084 AudioClock::locate ()
2086 if (!_session || is_duration) {
2090 _session->request_locate (current_time(), _session->transport_rolling ());
2094 AudioClock::set_mode (Mode m, bool noemit)
2104 _layout->set_text ("");
2106 Gtk::Requisition req;
2107 set_clock_dimensions (req);
2111 insert_map.push_back (11);
2112 insert_map.push_back (10);
2113 insert_map.push_back (8);
2114 insert_map.push_back (7);
2115 insert_map.push_back (5);
2116 insert_map.push_back (4);
2117 insert_map.push_back (2);
2118 insert_map.push_back (1);
2122 insert_map.push_back (11);
2123 insert_map.push_back (10);
2124 insert_map.push_back (9);
2125 insert_map.push_back (8);
2126 insert_map.push_back (6);
2127 insert_map.push_back (5);
2128 insert_map.push_back (3);
2129 insert_map.push_back (2);
2130 insert_map.push_back (1);
2134 insert_map.push_back (12);
2135 insert_map.push_back (11);
2136 insert_map.push_back (10);
2137 insert_map.push_back (8);
2138 insert_map.push_back (7);
2139 insert_map.push_back (5);
2140 insert_map.push_back (4);
2141 insert_map.push_back (2);
2142 insert_map.push_back (1);
2149 set (last_when, true);
2151 if (!is_transient && !noemit) {
2152 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2155 mode_changed (); /* EMIT SIGNAL (the member one) */
2159 AudioClock::set_bbt_reference (framepos_t pos)
2161 bbt_reference_time = pos;
2165 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2167 CairoWidget::on_style_changed (old_style);
2169 Gtk::Requisition req;
2170 set_clock_dimensions (req);
2172 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2178 AudioClock::set_editable (bool yn)
2184 AudioClock::set_is_duration (bool yn)
2186 if (yn == is_duration) {
2191 set (last_when, true);
2195 AudioClock::set_off (bool yn)
2203 /* force a redraw. last_when will be preserved, but the clock text will
2207 set (last_when, true);
2211 AudioClock::focus ()
2213 start_edit (Field (0));
2217 AudioClock::set_corner_radius (double r)
2226 AudioClock::dpi_reset ()
2228 /* force recomputation of size even if we are fixed width
2236 AudioClock::set_negative_allowed (bool yn)
2238 _negative_allowed = yn;