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 , _limit_pos (INT64_MAX - 1)
81 , _with_info (with_info)
86 , style_resets_first (true)
92 , bbt_reference_time (-1)
97 , drag_field (Field (0))
101 set_flags (CAN_FOCUS);
103 _layout = Pango::Layout::create (get_pango_context());
104 _layout->set_attributes (normal_attributes);
106 set_widget_name (widget_name);
108 _mode = BBT; /* lie to force mode switch */
110 set (last_when, true);
113 clocks.push_back (this);
116 _left_btn.set_sizing_text (_("0000000000000"));
117 // NB right_btn is in a size-group
119 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
120 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
122 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
123 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
126 AudioClock::~AudioClock ()
128 delete foreground_attr;
133 AudioClock::set_widget_name (const string& str)
138 set_name (str + " clock");
148 AudioClock::on_realize ()
150 Gtk::Requisition req;
152 CairoWidget::on_realize ();
154 set_clock_dimensions (req);
156 first_width = req.width;
157 first_height = req.height;
159 // XXX FIX ME: define font based on ... ???
165 AudioClock::set_font (Pango::FontDescription font)
167 Glib::RefPtr<Gtk::Style> style = get_style ();
168 Pango::AttrFontDesc* font_attr;
170 font_size = font.get_size();
171 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
173 normal_attributes.change (*font_attr);
174 editing_attributes.change (*font_attr);
177 /* get the figure width for the font. This doesn't have to super
178 * accurate since we only use it to measure the (roughly 1 character)
179 * offset from the position Pango tells us for the "cursor"
182 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
186 tmp->get_pixel_size (em_width, ignore_height);
188 /* force redraw of markup with new font-size */
189 set (last_when, true);
193 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
195 CairoWidget::set_active_state (s);
200 AudioClock::set_colors ()
206 uint32_t editing_color;
207 uint32_t cursor_color;
209 if (active_state()) {
210 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
211 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
212 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
213 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
215 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
216 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
217 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
218 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
221 /* store for bg and cursor in render() */
223 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
230 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
237 /* rescale for Pango colors ... sigh */
239 r = lrint (r * 65535.0);
240 g = lrint (g * 65535.0);
241 b = lrint (b * 65535.0);
243 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
244 r = lrint ((r/255.0) * 65535.0);
245 g = lrint ((g/255.0) * 65535.0);
246 b = lrint ((b/255.0) * 65535.0);
247 delete foreground_attr;
248 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
250 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
251 r = lrint ((r/255.0) * 65535.0);
252 g = lrint ((g/255.0) * 65535.0);
253 b = lrint ((b/255.0) * 65535.0);
255 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
257 normal_attributes.change (*foreground_attr);
258 editing_attributes.change (*foreground_attr);
259 editing_attributes.change (*editing_attr);
262 _layout->set_attributes (normal_attributes);
264 _layout->set_attributes (editing_attributes);
271 AudioClock::set_scale (double x, double y)
280 AudioClock::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
282 cairo_t* cr = ctx->cobj();
283 /* main layout: rounded rect, plus the text */
286 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
288 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
290 cairo_rectangle (cr, 0, 0, get_width(), get_height());
295 double lw = layout_width * xscale;
296 double lh = layout_height * yscale;
298 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
300 if (xscale != 1.0 || yscale != 1.0) {
302 cairo_scale (cr, xscale, yscale);
305 pango_cairo_show_layout (cr, _layout->gobj());
307 if (xscale != 1.0 || yscale != 1.0) {
312 if (!insert_map.empty()) {
314 int xcenter = (get_width() - layout_width) /2;
316 if (input_string.length() < insert_map.size()) {
317 Pango::Rectangle cursor;
319 if (input_string.empty()) {
320 /* nothing entered yet, put cursor at the end
323 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
325 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
328 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
330 min (get_width() - 2.0,
331 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
332 (get_height() - layout_height)/2.0,
333 2.0, cursor.get_height()/PANGO_SCALE);
336 /* we've entered all possible digits, no cursor */
340 if (input_string.empty()) {
341 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
344 (get_height() - layout_height)/2.0,
353 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
355 Glib::RefPtr<Pango::Layout> tmp;
356 Glib::RefPtr<Gtk::Style> style = get_style ();
357 Pango::FontDescription font;
359 tmp = Pango::Layout::create (get_pango_context());
361 if (!is_realized()) {
362 font = get_font_for_style (get_name());
364 font = style->get_font();
367 tmp->set_font_description (font);
369 /* this string is the longest thing we will ever display */
371 tmp->set_text (" 88:88:88,888 ");
373 tmp->set_text (" 88:88:88,88 ");
374 tmp->get_pixel_size (req.width, req.height);
376 layout_height = req.height;
377 layout_width = req.width;
381 AudioClock::on_size_request (Gtk::Requisition* req)
383 /* even for non fixed width clocks, the size we *ask* for never changes,
384 even though the size we receive might. so once we've computed it,
389 req->width = first_width;
390 req->height = first_height;
394 set_clock_dimensions (*req);
396 /* now tackle height, for which we need to know the height of the lower
402 AudioClock::show_edit_status (int length)
404 editing_attr->set_start_index (edit_string.length() - length);
405 editing_attr->set_end_index (edit_string.length());
407 editing_attributes.change (*foreground_attr);
408 editing_attributes.change (*editing_attr);
410 _layout->set_attributes (editing_attributes);
414 AudioClock::start_edit (Field f)
417 pre_edit_string = _layout->get_text ();
418 if (!insert_map.empty()) {
419 edit_string = pre_edit_string;
421 edit_string.clear ();
422 _layout->set_text ("");
425 input_string.clear ();
427 edit_is_negative = false;
430 input_string = get_field (f);
431 show_edit_status (merge_input_and_edit_string ());
432 _layout->set_text (edit_string);
437 Keyboard::magic_widget_grab_focus ();
443 AudioClock::get_field (Field f)
447 return edit_string.substr (1, 2);
449 case Timecode_Minutes:
450 return edit_string.substr (4, 2);
452 case Timecode_Seconds:
453 return edit_string.substr (7, 2);
455 case Timecode_Frames:
456 return edit_string.substr (10, 2);
459 return edit_string.substr (1, 2);
462 return edit_string.substr (4, 2);
465 return edit_string.substr (7, 2);
467 case MS_Milliseconds:
468 return edit_string.substr (10, 3);
471 return edit_string.substr (1, 3);
474 return edit_string.substr (5, 2);
477 return edit_string.substr (8, 4);
487 AudioClock::end_edit (bool modify)
495 ok = timecode_validate_edit (edit_string);
499 ok = bbt_validate_edit (edit_string);
503 ok = minsec_validate_edit (edit_string);
507 if (edit_string.length() < 1) {
508 edit_string = pre_edit_string;
514 edit_string = pre_edit_string;
515 input_string.clear ();
516 _layout->set_text (edit_string);
517 show_edit_status (0);
518 /* edit attributes remain in use */
522 framepos_t pos = 0; /* stupid gcc */
526 pos = frames_from_timecode_string (edit_string);
531 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
533 pos = frames_from_bbt_string (0, edit_string);
538 pos = frames_from_minsec_string (edit_string);
542 pos = frames_from_audioframes_string (edit_string);
547 _layout->set_attributes (normal_attributes);
548 ValueChanged(); /* EMIT_SIGNAL */
554 edit_is_negative = false;
555 _layout->set_attributes (normal_attributes);
556 _layout->set_text (pre_edit_string);
567 AudioClock::drop_focus ()
569 Keyboard::magic_widget_drop_focus ();
572 /* move focus back to the default widget in the top level window */
573 ARDOUR_UI::instance()->reset_focus (this);
578 AudioClock::parse_as_frames_distance (const std::string& str)
582 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
590 AudioClock::parse_as_minsec_distance (const std::string& str)
592 framecnt_t sr = _session->frame_rate();
598 switch (str.length()) {
605 sscanf (str.c_str(), "%" PRId32, &msecs);
606 return msecs * (sr / 1000);
609 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
610 return (secs * sr) + (msecs * (sr/1000));
613 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
614 return (secs * sr) + (msecs * (sr/1000));
617 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
618 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
621 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
622 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
625 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
626 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
629 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
630 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
640 AudioClock::parse_as_timecode_distance (const std::string& str)
642 double fps = _session->timecode_frames_per_second();
643 framecnt_t sr = _session->frame_rate();
649 switch (str.length()) {
654 sscanf (str.c_str(), "%" PRId32, &frames);
655 return llrint ((frames/(float)fps) * sr);
658 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
659 return (secs * sr) + llrint ((frames/(float)fps) * sr);
662 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
663 return (secs * sr) + llrint ((frames/(float)fps) * sr);
666 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
667 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
670 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
671 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
674 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
675 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
678 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
679 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
689 AudioClock::parse_as_bbt_distance (const std::string&)
695 AudioClock::parse_as_distance (const std::string& instr)
699 return parse_as_timecode_distance (instr);
702 return parse_as_frames_distance (instr);
705 return parse_as_bbt_distance (instr);
708 return parse_as_minsec_distance (instr);
715 AudioClock::end_edit_relative (bool add)
721 ok = timecode_validate_edit (edit_string);
725 ok = bbt_validate_edit (edit_string);
729 ok = minsec_validate_edit (edit_string);
737 edit_string = pre_edit_string;
738 input_string.clear ();
739 _layout->set_text (edit_string);
740 show_edit_status (0);
741 /* edit attributes remain in use */
746 framecnt_t frames = parse_as_distance (input_string);
751 _layout->set_attributes (normal_attributes);
755 set (current_time() + frames, true);
757 framepos_t c = current_time();
759 if (c > frames || _negative_allowed) {
760 set (c - frames, true);
765 ValueChanged (); /* EMIT SIGNAL */
768 input_string.clear ();
774 AudioClock::session_property_changed (const PropertyChange&)
776 set (last_when, true);
780 AudioClock::session_configuration_changed (std::string p)
782 if (_negative_allowed) {
783 /* session option editor clock */
787 if (p == "sync-source" || p == "external-sync") {
788 set (current_time(), true);
792 if (p != "timecode-offset" && p != "timecode-offset-negative") {
801 current = current_duration ();
803 current = current_time ();
813 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
815 if ((!force && !is_visible()) || _session == 0) {
820 when = when - offset;
823 if (when > _limit_pos) {
825 } else if (when < -_limit_pos) {
829 if (when == last_when && !force) {
830 #if 0 // XXX return if no change and no change forced. verify Aug/2014
831 if (_mode != Timecode && _mode != MinSec) {
832 /* may need to force display of TC source
833 * time, so don't return early.
835 /* ^^ Why was that?, delta times?
836 * Timecode FPS, pull-up/down, etc changes
837 * trigger a 'session_property_changed' which
838 * eventually calls set(last_when, true)
840 * re-rendering the clock every 40ms or so just
841 * because we can is not ideal.
856 set_timecode (when, force);
860 set_bbt (when, offset, force);
865 set_minsec (when, force);
869 set_frames (when, force);
875 _left_btn.set_sensitive (btn_en);
876 _right_btn.set_sensitive (btn_en);
877 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
878 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
880 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
881 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
882 _left_btn.set_alignment (.5, .5);
883 _right_btn.set_alignment (.5, .5);
884 set_tooltip (_left_btn, _("Change current tempo"));
885 set_tooltip (_right_btn, _("Change current time signature"));
887 _left_btn.set_elements (ArdourButton::Text);
888 _right_btn.set_elements (ArdourButton::Text);
889 _left_btn.set_alignment (0, .5);
890 _right_btn.set_alignment (1, .5);
891 set_tooltip (_left_btn, _(""));
892 set_tooltip (_right_btn, _(""));
901 AudioClock::set_slave_info ()
907 SyncSource sync_src = Config->get_sync_source();
909 if (_session->config.get_external_sync()) {
910 Slave* slave = _session->slave();
914 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
915 _right_btn.set_text ("", true);
919 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
920 _right_btn.set_text (slave->approximate_current_delta (), true);
922 _left_btn.set_text (_("--pending--"), true);
923 _right_btn.set_text ("", true);
930 TimecodeSlave* tcslave;
931 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
932 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
933 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
934 sync_source_to_string(sync_src, true)[0],
935 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
936 matching ? "#66ff66" : "#ff3333"
938 _right_btn.set_text (slave->approximate_current_delta (), true);
941 _left_btn.set_text (_("--pending--"), true);
942 _right_btn.set_text ("", true);
947 _left_btn.set_text (string_compose ("%1/%2",
948 _("INT"), sync_source_to_string(sync_src, true)), true);
949 _right_btn.set_text ("", true);
954 AudioClock::set_out_of_bounds (bool negative)
958 _layout->set_text (" >>> -- <<< ");
960 _layout->set_text (" >>> ++ <<< ");
964 _layout->set_text (" <<<<<<<<<< ");
966 _layout->set_text (" >>>>>>>>>> ");
972 AudioClock::set_frames (framepos_t when, bool /*force*/)
975 bool negative = false;
978 _layout->set_text (" ----------");
979 _left_btn.set_text ("", true);
980 _right_btn.set_text ("", true);
989 if (when >= _limit_pos) {
990 set_out_of_bounds (negative);
991 } else if (negative) {
992 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
993 _layout->set_text (buf);
995 snprintf (buf, sizeof (buf), " %10" PRId64, when);
996 _layout->set_text (buf);
1000 framecnt_t rate = _session->frame_rate();
1002 if (fmod (rate, 100.0) == 0.0) {
1003 sprintf (buf, "%.1fkHz", rate/1000.0);
1005 sprintf (buf, "%" PRId64 "Hz", rate);
1008 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
1010 float vid_pullup = _session->config.get_video_pullup();
1012 if (vid_pullup == 0.0) {
1013 _right_btn.set_text ("", true);
1015 sprintf (buf, _("%+.4f%%"), vid_pullup);
1016 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
1022 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1039 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1040 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1041 mins = (int) floor (left / (frame_rate * 60.0f));
1042 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1043 secs = (int) floor (left / (float) frame_rate);
1044 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1045 millisecs = floor (left * 1000.0 / (float) frame_rate);
1048 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1050 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1056 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1061 _layout->set_text (" --:--:--.---");
1062 _left_btn.set_text ("", true);
1063 _right_btn.set_text ("", true);
1068 if (when >= _limit_pos || when <= -_limit_pos) {
1069 set_out_of_bounds (when < 0);
1071 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1072 _layout->set_text (buf);
1079 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1082 bool negative = false;
1085 _layout->set_text (" --:--:--:--");
1086 _left_btn.set_text ("", true);
1087 _right_btn.set_text ("", true);
1095 if (when >= _limit_pos) {
1096 set_out_of_bounds (negative);
1102 _session->timecode_duration (when, TC);
1104 _session->timecode_time (when, TC);
1107 TC.negative = TC.negative || negative;
1109 _layout->set_text (Timecode::timecode_format_time(TC));
1115 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1118 Timecode::BBT_Time BBT;
1119 bool negative = false;
1121 if (_off || when >= _limit_pos || when < -_limit_pos) {
1122 _layout->set_text (" ---|--|----");
1123 _left_btn.set_text ("", true);
1124 _right_btn.set_text ("", true);
1133 /* handle a common case */
1140 TempoMap& tmap (_session->tempo_map());
1143 offset = bbt_reference_time;
1146 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1147 Timecode::BBT_Time sub_bbt;
1150 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1151 sub_bbt = tmap.bbt_at_frame (offset - when);
1153 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1154 sub_bbt = tmap.bbt_at_frame (offset);
1157 BBT.bars -= sub_bbt.bars;
1159 if (BBT.ticks < sub_bbt.ticks) {
1160 if (BBT.beats == 1) {
1162 BBT.beats = divisions;
1166 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1168 BBT.ticks -= sub_bbt.ticks;
1171 if (BBT.beats < sub_bbt.beats) {
1173 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1175 BBT.beats -= sub_bbt.beats;
1179 BBT = _session->tempo_map().bbt_at_frame (when);
1183 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1184 BBT.bars, BBT.beats, BBT.ticks);
1186 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1187 BBT.bars, BBT.beats, BBT.ticks);
1190 _layout->set_text (buf);
1195 if (bbt_reference_time < 0) {
1198 pos = bbt_reference_time;
1201 TempoMetric m (_session->tempo_map().metric_at (pos));
1203 if (m.tempo().note_type() == 4) {
1204 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1205 _left_btn.set_text (string_compose ("%1", buf), true);
1206 } else if (m.tempo().note_type() == 8) {
1207 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1208 _left_btn.set_text (string_compose ("%1", buf), true);
1210 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1211 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1214 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1215 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1220 AudioClock::set_session (Session *s)
1222 SessionHandlePtr::set_session (s);
1226 int64_t limit_sec = UIConfiguration::instance().get_clock_display_limit ();
1227 if (limit_sec > 0) {
1228 _limit_pos = (framecnt_t) floor ((double)(limit_sec * _session->frame_rate()));
1231 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1232 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1233 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1234 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1236 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1239 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1241 if ((*i)->get_property (X_("name"), name) && name == _name) {
1243 AudioClock::Mode amode;
1244 if ((*i)->get_property (X_("mode"), amode)) {
1245 set_mode (amode, true);
1248 if ((*i)->get_property (X_("on"), on)) {
1256 set (last_when, true);
1261 AudioClock::on_key_press_event (GdkEventKey* ev)
1269 int highlight_length;
1272 switch (ev->keyval) {
1315 case GDK_KP_Subtract:
1316 if (_negative_allowed && input_string.empty()) {
1317 edit_is_negative = true;
1318 edit_string.replace(0,1,"-");
1319 _layout->set_text (edit_string);
1322 end_edit_relative (false);
1328 end_edit_relative (true);
1341 ChangeAborted(); /* EMIT SIGNAL */
1346 if (!input_string.empty()) {
1347 /* delete the last key entered
1349 input_string = input_string.substr (0, input_string.length() - 1);
1351 goto use_input_string;
1354 /* do not allow other keys to passthru to the rest of the GUI
1360 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1361 /* too many digits: eat the key event, but do nothing with it */
1365 input_string.push_back (new_char);
1371 /* get this one in the right order, and to the right width */
1372 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1373 edit_string = edit_string.substr (0, edit_string.length() - 1);
1375 edit_string.push_back (new_char);
1377 if (!edit_string.empty()) {
1379 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1380 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1383 /* highlight the whole thing */
1384 highlight_length = edit_string.length();
1388 highlight_length = merge_input_and_edit_string ();
1391 if (edit_is_negative) {
1392 edit_string.replace(0,1,"-");
1394 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1395 edit_string.replace(0,1,"_");
1397 edit_string.replace(0,1," ");
1401 show_edit_status (highlight_length);
1402 _layout->set_text (edit_string);
1409 AudioClock::merge_input_and_edit_string ()
1411 /* merge with pre-edit-string into edit string */
1413 edit_string = pre_edit_string;
1415 if (input_string.empty()) {
1419 string::size_type target;
1420 for (string::size_type i = 0; i < input_string.length(); ++i) {
1421 target = insert_map[input_string.length() - 1 - i];
1422 edit_string[target] = input_string[i];
1424 /* highlight from end to wherever the last character was added */
1425 return edit_string.length() - insert_map[input_string.length()-1];
1430 AudioClock::on_key_release_event (GdkEventKey *ev)
1436 /* return true for keys that we used on press
1437 so that they cannot possibly do double-duty
1439 switch (ev->keyval) {
1462 case GDK_KP_Decimal:
1470 case GDK_KP_Subtract:
1478 AudioClock::index_to_field (int index) const
1483 return Timecode_Hours;
1484 } else if (index < 7) {
1485 return Timecode_Minutes;
1486 } else if (index < 10) {
1487 return Timecode_Seconds;
1489 return Timecode_Frames;
1495 } else if (index < 7) {
1503 return Timecode_Hours;
1504 } else if (index < 6) {
1506 } else if (index < 9) {
1509 return MS_Milliseconds;
1521 AudioClock::on_button_press_event (GdkEventButton *ev)
1523 if (!_session || _session->actively_recording()) {
1524 /* swallow event, do nothing */
1528 switch (ev->button) {
1530 if (editable && !_off) {
1536 /* the text has been centered vertically, so adjust
1539 int xcenter = (get_width() - layout_width) /2;
1541 y = ev->y - ((get_height() - layout_height)/2);
1542 x = ev->x - xcenter;
1544 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1545 /* pretend it is a character on the far right */
1548 drag_field = index_to_field (index);
1550 /* make absolutely sure that the pointer is grabbed */
1551 gdk_pointer_grab(ev->window,false ,
1552 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1553 NULL,NULL,ev->time);
1555 drag_start_y = ev->y;
1569 AudioClock::on_button_release_event (GdkEventButton *ev)
1571 if (!_session || _session->actively_recording()) {
1572 /* swallow event, do nothing */
1576 if (editable && !_off) {
1578 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1580 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1581 // we actually dragged so return without
1582 // setting editing focus, or we shift clicked
1585 if (ev->button == 1) {
1587 if (_edit_by_click_field) {
1589 int xcenter = (get_width() - layout_width) /2;
1592 int y = ev->y - ((get_height() - layout_height)/2);
1593 int x = ev->x - xcenter;
1596 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1600 f = index_to_field (index);
1603 case Timecode_Frames:
1604 case MS_Milliseconds:
1620 if (Keyboard::is_context_menu_event (ev)) {
1621 if (ops_menu == 0) {
1624 ops_menu->popup (1, ev->time);
1632 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1634 bool ret = CairoWidget::on_focus_out_event (ev);
1637 end_edit (_accept_on_focus_out);
1644 AudioClock::on_scroll_event (GdkEventScroll *ev)
1649 if (editing || _session == 0 || !editable || _off || _session->actively_recording()) {
1656 /* the text has been centered vertically, so adjust
1660 int xcenter = (get_width() - layout_width) /2;
1661 y = ev->y - ((get_height() - layout_height)/2);
1662 x = ev->x - xcenter;
1664 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1665 /* not in the main layout */
1669 Field f = index_to_field (index);
1670 framepos_t frames = 0;
1672 switch (ev->direction) {
1675 frames = get_frame_step (f, current_time(), 1);
1677 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1680 set (current_time() + frames, true);
1681 ValueChanged (); /* EMIT_SIGNAL */
1685 case GDK_SCROLL_DOWN:
1686 frames = get_frame_step (f, current_time(), -1);
1688 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1692 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1695 set (current_time() - frames, true);
1698 ValueChanged (); /* EMIT_SIGNAL */
1711 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1713 if (editing || _session == 0 || !dragging || _session->actively_recording()) {
1717 float pixel_frame_scale_factor = 0.2f;
1719 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1720 pixel_frame_scale_factor = 0.1f;
1724 if (Keyboard::modifier_state_contains (ev->state,
1725 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1726 pixel_frame_scale_factor = 0.025f;
1729 double y_delta = ev->y - drag_y;
1731 drag_accum += y_delta*pixel_frame_scale_factor;
1735 if (floor (drag_accum) != 0) {
1740 dir = (drag_accum < 0 ? 1:-1);
1741 pos = current_time();
1742 frames = get_frame_step (drag_field, pos, dir);
1744 if (frames != 0 && frames * drag_accum < current_time()) {
1745 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1751 ValueChanged(); /* EMIT_SIGNAL */
1758 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1761 Timecode::BBT_Time BBT;
1763 case Timecode_Hours:
1764 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1766 case Timecode_Minutes:
1767 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1769 case Timecode_Seconds:
1770 f = _session->frame_rate();
1772 case Timecode_Frames:
1773 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1781 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1784 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1787 f = (framecnt_t) _session->frame_rate();
1789 case MS_Milliseconds:
1790 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1797 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1803 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1809 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1812 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1821 AudioClock::current_time (framepos_t) const
1827 AudioClock::current_duration (framepos_t pos) const
1836 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1852 AudioClock::bbt_validate_edit (const string& str)
1856 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1860 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1864 if (!is_duration && any.bbt.bars == 0) {
1868 if (!is_duration && any.bbt.beats == 0) {
1876 AudioClock::timecode_validate_edit (const string&)
1882 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1883 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1888 TC.hours = hours * -1;
1892 TC.negative = false;
1895 if (TC.negative && !_negative_allowed) {
1899 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1903 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1907 if (_session->timecode_drop_frames()) {
1908 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1917 AudioClock::minsec_validate_edit (const string& str)
1919 int hrs, mins, secs, millisecs;
1921 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1925 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1933 AudioClock::frames_from_timecode_string (const string& str) const
1935 if (_session == 0) {
1944 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1945 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1948 TC.hours = abs(hours);
1949 TC.rate = _session->timecode_frames_per_second();
1950 TC.drop= _session->timecode_drop_frames();
1952 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1954 // timecode_tester ();
1955 if (edit_is_negative) {
1963 AudioClock::frames_from_minsec_string (const string& str) const
1965 if (_session == 0) {
1969 int hrs, mins, secs, millisecs;
1970 framecnt_t sr = _session->frame_rate();
1972 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1973 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1977 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1981 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1983 if (_session == 0) {
1984 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1989 any.type = AnyTime::BBT;
1991 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1998 return _session->any_duration_to_frames (pos, any);
2000 return _session->convert_to_frames (any);
2006 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2008 if (_session == 0) {
2009 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2013 Timecode::BBT_Time bbt;
2015 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2019 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2023 AudioClock::frames_from_audioframes_string (const string& str) const
2026 sscanf (str.c_str(), "%" PRId64, &f);
2031 AudioClock::copy_text_to_clipboard () const
2035 val = pre_edit_string;
2037 val = _layout->get_text ();
2039 const size_t trim = val.find_first_not_of(" ");
2040 if (trim == string::npos) {
2041 assert(0); // empty clock, can't be right.
2044 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2045 cl->set_text (val.substr(trim));
2049 AudioClock::build_ops_menu ()
2051 using namespace Menu_Helpers;
2052 ops_menu = new Menu;
2053 MenuList& ops_items = ops_menu->items();
2054 ops_menu->set_name ("ArdourContextMenu");
2056 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2057 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2058 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2059 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2061 if (editable && !_off && !is_duration && !_follows_playhead) {
2062 ops_items.push_back (SeparatorElem());
2063 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2064 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2066 ops_items.push_back (SeparatorElem());
2067 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2071 AudioClock::set_from_playhead ()
2077 set (_session->transport_frame());
2082 AudioClock::locate ()
2084 if (!_session || is_duration) {
2088 _session->request_locate (current_time(), _session->transport_rolling ());
2092 AudioClock::set_mode (Mode m, bool noemit)
2102 _layout->set_text ("");
2104 Gtk::Requisition req;
2105 set_clock_dimensions (req);
2109 insert_map.push_back (11);
2110 insert_map.push_back (10);
2111 insert_map.push_back (8);
2112 insert_map.push_back (7);
2113 insert_map.push_back (5);
2114 insert_map.push_back (4);
2115 insert_map.push_back (2);
2116 insert_map.push_back (1);
2120 insert_map.push_back (11);
2121 insert_map.push_back (10);
2122 insert_map.push_back (9);
2123 insert_map.push_back (8);
2124 insert_map.push_back (6);
2125 insert_map.push_back (5);
2126 insert_map.push_back (3);
2127 insert_map.push_back (2);
2128 insert_map.push_back (1);
2132 insert_map.push_back (12);
2133 insert_map.push_back (11);
2134 insert_map.push_back (10);
2135 insert_map.push_back (8);
2136 insert_map.push_back (7);
2137 insert_map.push_back (5);
2138 insert_map.push_back (4);
2139 insert_map.push_back (2);
2140 insert_map.push_back (1);
2147 set (last_when, true);
2149 if (!is_transient && !noemit) {
2150 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2153 mode_changed (); /* EMIT SIGNAL (the member one) */
2157 AudioClock::set_bbt_reference (framepos_t pos)
2159 bbt_reference_time = pos;
2163 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2165 CairoWidget::on_style_changed (old_style);
2167 Gtk::Requisition req;
2168 set_clock_dimensions (req);
2170 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2176 AudioClock::set_editable (bool yn)
2182 AudioClock::set_is_duration (bool yn)
2184 if (yn == is_duration) {
2189 set (last_when, true);
2193 AudioClock::set_off (bool yn)
2201 /* force a redraw. last_when will be preserved, but the clock text will
2205 set (last_when, true);
2209 AudioClock::focus ()
2211 start_edit (Field (0));
2215 AudioClock::set_corner_radius (double r)
2224 AudioClock::dpi_reset ()
2226 /* force recomputation of size even if we are fixed width
2234 AudioClock::set_negative_allowed (bool yn)
2236 _negative_allowed = yn;