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::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
280 cairo_t* cr = ctx->cobj();
281 /* main layout: rounded rect, plus the text */
284 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
286 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
288 cairo_rectangle (cr, 0, 0, get_width(), get_height());
293 double lw = layout_width * xscale;
294 double lh = layout_height * yscale;
296 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
298 if (xscale != 1.0 || yscale != 1.0) {
300 cairo_scale (cr, xscale, yscale);
303 pango_cairo_show_layout (cr, _layout->gobj());
305 if (xscale != 1.0 || yscale != 1.0) {
310 if (!insert_map.empty()) {
312 int xcenter = (get_width() - layout_width) /2;
314 if (input_string.length() < insert_map.size()) {
315 Pango::Rectangle cursor;
317 if (input_string.empty()) {
318 /* nothing entered yet, put cursor at the end
321 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
323 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
326 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
328 min (get_width() - 2.0,
329 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
330 (get_height() - layout_height)/2.0,
331 2.0, cursor.get_height()/PANGO_SCALE);
334 /* we've entered all possible digits, no cursor */
338 if (input_string.empty()) {
339 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
342 (get_height() - layout_height)/2.0,
351 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
353 Glib::RefPtr<Pango::Layout> tmp;
354 Glib::RefPtr<Gtk::Style> style = get_style ();
355 Pango::FontDescription font;
357 tmp = Pango::Layout::create (get_pango_context());
359 if (!is_realized()) {
360 font = get_font_for_style (get_name());
362 font = style->get_font();
365 tmp->set_font_description (font);
367 /* this string is the longest thing we will ever display */
369 tmp->set_text (" 88:88:88,888 ");
371 tmp->set_text (" 88:88:88,88 ");
372 tmp->get_pixel_size (req.width, req.height);
374 layout_height = req.height;
375 layout_width = req.width;
379 AudioClock::on_size_request (Gtk::Requisition* req)
381 /* even for non fixed width clocks, the size we *ask* for never changes,
382 even though the size we receive might. so once we've computed it,
387 req->width = first_width;
388 req->height = first_height;
392 set_clock_dimensions (*req);
394 /* now tackle height, for which we need to know the height of the lower
400 AudioClock::show_edit_status (int length)
402 editing_attr->set_start_index (edit_string.length() - length);
403 editing_attr->set_end_index (edit_string.length());
405 editing_attributes.change (*foreground_attr);
406 editing_attributes.change (*editing_attr);
408 _layout->set_attributes (editing_attributes);
412 AudioClock::start_edit (Field f)
415 pre_edit_string = _layout->get_text ();
416 if (!insert_map.empty()) {
417 edit_string = pre_edit_string;
419 edit_string.clear ();
420 _layout->set_text ("");
423 input_string.clear ();
425 edit_is_negative = false;
428 input_string = get_field (f);
429 show_edit_status (merge_input_and_edit_string ());
430 _layout->set_text (edit_string);
435 Keyboard::magic_widget_grab_focus ();
441 AudioClock::get_field (Field f)
445 return edit_string.substr (1, 2);
447 case Timecode_Minutes:
448 return edit_string.substr (4, 2);
450 case Timecode_Seconds:
451 return edit_string.substr (7, 2);
453 case Timecode_Frames:
454 return edit_string.substr (10, 2);
457 return edit_string.substr (1, 2);
460 return edit_string.substr (4, 2);
463 return edit_string.substr (7, 2);
465 case MS_Milliseconds:
466 return edit_string.substr (10, 3);
469 return edit_string.substr (1, 3);
472 return edit_string.substr (5, 2);
475 return edit_string.substr (8, 4);
485 AudioClock::end_edit (bool modify)
493 ok = timecode_validate_edit (edit_string);
497 ok = bbt_validate_edit (edit_string);
501 ok = minsec_validate_edit (edit_string);
505 if (edit_string.length() < 1) {
506 edit_string = pre_edit_string;
512 edit_string = pre_edit_string;
513 input_string.clear ();
514 _layout->set_text (edit_string);
515 show_edit_status (0);
516 /* edit attributes remain in use */
520 framepos_t pos = 0; /* stupid gcc */
524 pos = frames_from_timecode_string (edit_string);
529 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
531 pos = frames_from_bbt_string (0, edit_string);
536 pos = frames_from_minsec_string (edit_string);
540 pos = frames_from_audioframes_string (edit_string);
545 _layout->set_attributes (normal_attributes);
546 ValueChanged(); /* EMIT_SIGNAL */
552 edit_is_negative = false;
553 _layout->set_attributes (normal_attributes);
554 _layout->set_text (pre_edit_string);
565 AudioClock::drop_focus ()
567 Keyboard::magic_widget_drop_focus ();
570 /* move focus back to the default widget in the top level window */
571 ARDOUR_UI::instance()->reset_focus (this);
576 AudioClock::parse_as_frames_distance (const std::string& str)
580 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
588 AudioClock::parse_as_minsec_distance (const std::string& str)
590 framecnt_t sr = _session->frame_rate();
596 switch (str.length()) {
603 sscanf (str.c_str(), "%" PRId32, &msecs);
604 return msecs * (sr / 1000);
607 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
608 return (secs * sr) + (msecs * (sr/1000));
611 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
612 return (secs * sr) + (msecs * (sr/1000));
615 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
616 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
619 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
620 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
623 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
624 return (hrs * 3600 * sr) + (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));
638 AudioClock::parse_as_timecode_distance (const std::string& str)
640 double fps = _session->timecode_frames_per_second();
641 framecnt_t sr = _session->frame_rate();
647 switch (str.length()) {
652 sscanf (str.c_str(), "%" PRId32, &frames);
653 return llrint ((frames/(float)fps) * sr);
656 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
657 return (secs * sr) + llrint ((frames/(float)fps) * sr);
660 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
661 return (secs * sr) + llrint ((frames/(float)fps) * sr);
664 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
665 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
668 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
669 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
672 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
673 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
676 sscanf (str.c_str(), "%2" 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);
687 AudioClock::parse_as_bbt_distance (const std::string&)
693 AudioClock::parse_as_distance (const std::string& instr)
697 return parse_as_timecode_distance (instr);
700 return parse_as_frames_distance (instr);
703 return parse_as_bbt_distance (instr);
706 return parse_as_minsec_distance (instr);
713 AudioClock::end_edit_relative (bool add)
719 ok = timecode_validate_edit (edit_string);
723 ok = bbt_validate_edit (edit_string);
727 ok = minsec_validate_edit (edit_string);
735 edit_string = pre_edit_string;
736 input_string.clear ();
737 _layout->set_text (edit_string);
738 show_edit_status (0);
739 /* edit attributes remain in use */
744 framecnt_t frames = parse_as_distance (input_string);
749 _layout->set_attributes (normal_attributes);
753 set (current_time() + frames, true);
755 framepos_t c = current_time();
757 if (c > frames || _negative_allowed) {
758 set (c - frames, true);
763 ValueChanged (); /* EMIT SIGNAL */
766 input_string.clear ();
772 AudioClock::session_property_changed (const PropertyChange&)
774 set (last_when, true);
778 AudioClock::session_configuration_changed (std::string p)
780 if (_negative_allowed) {
781 /* session option editor clock */
785 if (p == "sync-source" || p == "external-sync") {
786 set (current_time(), true);
790 if (p != "timecode-offset" && p != "timecode-offset-negative") {
799 current = current_duration ();
801 current = current_time ();
811 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
813 if ((!force && !is_visible()) || _session == 0) {
818 when = when - offset;
821 if (when == last_when && !force) {
822 #if 0 // XXX return if no change and no change forced. verify Aug/2014
823 if (_mode != Timecode && _mode != MinSec) {
824 /* may need to force display of TC source
825 * time, so don't return early.
827 /* ^^ Why was that?, delta times?
828 * Timecode FPS, pull-up/down, etc changes
829 * trigger a 'session_property_changed' which
830 * eventually calls set(last_when, true)
832 * re-rendering the clock every 40ms or so just
833 * because we can is not ideal.
848 set_timecode (when, force);
852 set_bbt (when, offset, force);
857 set_minsec (when, force);
861 set_frames (when, force);
867 _left_btn.set_sensitive (btn_en);
868 _right_btn.set_sensitive (btn_en);
869 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
870 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
872 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
873 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
874 _left_btn.set_alignment (.5, .5);
875 _right_btn.set_alignment (.5, .5);
876 set_tooltip (_left_btn, _("Change current tempo"));
877 set_tooltip (_right_btn, _("Change current time signature"));
879 _left_btn.set_elements (ArdourButton::Text);
880 _right_btn.set_elements (ArdourButton::Text);
881 _left_btn.set_alignment (0, .5);
882 _right_btn.set_alignment (1, .5);
883 set_tooltip (_left_btn, _(""));
884 set_tooltip (_right_btn, _(""));
893 AudioClock::set_slave_info ()
899 SyncSource sync_src = Config->get_sync_source();
901 if (_session->config.get_external_sync()) {
902 Slave* slave = _session->slave();
906 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
907 _right_btn.set_text ("", true);
911 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
912 _right_btn.set_text (slave->approximate_current_delta (), true);
914 _left_btn.set_text (_("--pending--"), true);
915 _right_btn.set_text ("", true);
922 TimecodeSlave* tcslave;
923 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
924 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
925 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
926 sync_source_to_string(sync_src, true)[0],
927 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
928 matching ? "#66ff66" : "#ff3333"
930 _right_btn.set_text (slave->approximate_current_delta (), true);
933 _left_btn.set_text (_("--pending--"), true);
934 _right_btn.set_text ("", true);
939 _left_btn.set_text (string_compose ("%1/%2",
940 _("INT"), sync_source_to_string(sync_src, true)), true);
941 _right_btn.set_text ("", true);
946 AudioClock::set_frames (framepos_t when, bool /*force*/)
949 bool negative = false;
952 _layout->set_text (" ----------");
953 _left_btn.set_text ("", true);
954 _right_btn.set_text ("", true);
964 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
966 snprintf (buf, sizeof (buf), " %10" PRId64, when);
969 _layout->set_text (buf);
972 framecnt_t rate = _session->frame_rate();
974 if (fmod (rate, 100.0) == 0.0) {
975 sprintf (buf, "%.1fkHz", rate/1000.0);
977 sprintf (buf, "%" PRId64 "Hz", rate);
980 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
982 float vid_pullup = _session->config.get_video_pullup();
984 if (vid_pullup == 0.0) {
985 _right_btn.set_text ("", true);
987 sprintf (buf, _("%+.4f%%"), vid_pullup);
988 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
994 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1011 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1012 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1013 mins = (int) floor (left / (frame_rate * 60.0f));
1014 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1015 secs = (int) floor (left / (float) frame_rate);
1016 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1017 millisecs = floor (left * 1000.0 / (float) frame_rate);
1020 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1022 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1028 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1033 _layout->set_text (" --:--:--.---");
1034 _left_btn.set_text ("", true);
1035 _right_btn.set_text ("", true);
1040 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1042 _layout->set_text (buf);
1047 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1050 bool negative = false;
1053 _layout->set_text (" --:--:--:--");
1054 _left_btn.set_text ("", true);
1055 _right_btn.set_text ("", true);
1065 _session->timecode_duration (when, TC);
1067 _session->timecode_time (when, TC);
1070 TC.negative = TC.negative || negative;
1072 _layout->set_text (Timecode::timecode_format_time(TC));
1078 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1081 Timecode::BBT_Time BBT;
1082 bool negative = false;
1085 _layout->set_text (" ---|--|----");
1086 _left_btn.set_text ("", true);
1087 _right_btn.set_text ("", true);
1096 /* handle a common case */
1103 TempoMap& tmap (_session->tempo_map());
1106 offset = bbt_reference_time;
1109 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1110 Timecode::BBT_Time sub_bbt;
1113 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1114 sub_bbt = tmap.bbt_at_frame (offset - when);
1116 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1117 sub_bbt = tmap.bbt_at_frame (offset);
1120 BBT.bars -= sub_bbt.bars;
1122 if (BBT.ticks < sub_bbt.ticks) {
1123 if (BBT.beats == 1) {
1125 BBT.beats = divisions;
1129 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1131 BBT.ticks -= sub_bbt.ticks;
1134 if (BBT.beats < sub_bbt.beats) {
1136 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1138 BBT.beats -= sub_bbt.beats;
1142 BBT = _session->tempo_map().bbt_at_frame (when);
1146 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1147 BBT.bars, BBT.beats, BBT.ticks);
1149 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1150 BBT.bars, BBT.beats, BBT.ticks);
1153 _layout->set_text (buf);
1158 if (bbt_reference_time < 0) {
1161 pos = bbt_reference_time;
1164 TempoMetric m (_session->tempo_map().metric_at (pos));
1166 if (m.tempo().note_type() == 4) {
1167 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1168 _left_btn.set_text (string_compose ("%1", buf), true);
1169 } else if (m.tempo().note_type() == 8) {
1170 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1171 _left_btn.set_text (string_compose ("%1", buf), true);
1173 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1174 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1177 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1178 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1183 AudioClock::set_session (Session *s)
1185 SessionHandlePtr::set_session (s);
1189 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1190 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1191 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1192 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1194 XMLProperty const * prop;
1195 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1196 AudioClock::Mode amode;
1199 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1200 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1202 if ((prop = (*i)->property (X_("mode"))) != 0) {
1203 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1204 set_mode (amode, true);
1206 if ((prop = (*i)->property (X_("on"))) != 0) {
1207 set_off (!string_is_affirmative (prop->value()));
1214 set (last_when, true);
1219 AudioClock::on_key_press_event (GdkEventKey* ev)
1227 int highlight_length;
1230 switch (ev->keyval) {
1273 case GDK_KP_Subtract:
1274 if (_negative_allowed && input_string.empty()) {
1275 edit_is_negative = true;
1276 edit_string.replace(0,1,"-");
1277 _layout->set_text (edit_string);
1280 end_edit_relative (false);
1286 end_edit_relative (true);
1299 ChangeAborted(); /* EMIT SIGNAL */
1304 if (!input_string.empty()) {
1305 /* delete the last key entered
1307 input_string = input_string.substr (0, input_string.length() - 1);
1309 goto use_input_string;
1312 /* do not allow other keys to passthru to the rest of the GUI
1318 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1319 /* too many digits: eat the key event, but do nothing with it */
1323 input_string.push_back (new_char);
1329 /* get this one in the right order, and to the right width */
1330 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1331 edit_string = edit_string.substr (0, edit_string.length() - 1);
1333 edit_string.push_back (new_char);
1335 if (!edit_string.empty()) {
1337 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1338 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1341 /* highlight the whole thing */
1342 highlight_length = edit_string.length();
1346 highlight_length = merge_input_and_edit_string ();
1349 if (edit_is_negative) {
1350 edit_string.replace(0,1,"-");
1352 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1353 edit_string.replace(0,1,"_");
1355 edit_string.replace(0,1," ");
1359 show_edit_status (highlight_length);
1360 _layout->set_text (edit_string);
1367 AudioClock::merge_input_and_edit_string ()
1369 /* merge with pre-edit-string into edit string */
1371 edit_string = pre_edit_string;
1373 if (input_string.empty()) {
1377 string::size_type target;
1378 for (string::size_type i = 0; i < input_string.length(); ++i) {
1379 target = insert_map[input_string.length() - 1 - i];
1380 edit_string[target] = input_string[i];
1382 /* highlight from end to wherever the last character was added */
1383 return edit_string.length() - insert_map[input_string.length()-1];
1388 AudioClock::on_key_release_event (GdkEventKey *ev)
1394 /* return true for keys that we used on press
1395 so that they cannot possibly do double-duty
1397 switch (ev->keyval) {
1420 case GDK_KP_Decimal:
1428 case GDK_KP_Subtract:
1436 AudioClock::index_to_field (int index) const
1441 return Timecode_Hours;
1442 } else if (index < 7) {
1443 return Timecode_Minutes;
1444 } else if (index < 10) {
1445 return Timecode_Seconds;
1447 return Timecode_Frames;
1453 } else if (index < 7) {
1461 return Timecode_Hours;
1462 } else if (index < 6) {
1464 } else if (index < 9) {
1467 return MS_Milliseconds;
1479 AudioClock::on_button_press_event (GdkEventButton *ev)
1481 switch (ev->button) {
1483 if (editable && !_off) {
1489 /* the text has been centered vertically, so adjust
1492 int xcenter = (get_width() - layout_width) /2;
1494 y = ev->y - ((get_height() - layout_height)/2);
1495 x = ev->x - xcenter;
1497 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1498 /* pretend it is a character on the far right */
1501 drag_field = index_to_field (index);
1503 /* make absolutely sure that the pointer is grabbed */
1504 gdk_pointer_grab(ev->window,false ,
1505 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1506 NULL,NULL,ev->time);
1508 drag_start_y = ev->y;
1522 AudioClock::on_button_release_event (GdkEventButton *ev)
1524 if (editable && !_off) {
1526 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1528 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1529 // we actually dragged so return without
1530 // setting editing focus, or we shift clicked
1533 if (ev->button == 1) {
1535 if (_edit_by_click_field) {
1537 int xcenter = (get_width() - layout_width) /2;
1540 int y = ev->y - ((get_height() - layout_height)/2);
1541 int x = ev->x - xcenter;
1544 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1548 f = index_to_field (index);
1551 case Timecode_Frames:
1552 case MS_Milliseconds:
1568 if (Keyboard::is_context_menu_event (ev)) {
1569 if (ops_menu == 0) {
1572 ops_menu->popup (1, ev->time);
1580 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1582 bool ret = CairoWidget::on_focus_out_event (ev);
1585 end_edit (_accept_on_focus_out);
1592 AudioClock::on_scroll_event (GdkEventScroll *ev)
1597 if (editing || _session == 0 || !editable || _off) {
1604 /* the text has been centered vertically, so adjust
1608 int xcenter = (get_width() - layout_width) /2;
1609 y = ev->y - ((get_height() - layout_height)/2);
1610 x = ev->x - xcenter;
1612 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1613 /* not in the main layout */
1617 Field f = index_to_field (index);
1618 framepos_t frames = 0;
1620 switch (ev->direction) {
1623 frames = get_frame_step (f, current_time(), 1);
1625 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1628 set (current_time() + frames, true);
1629 ValueChanged (); /* EMIT_SIGNAL */
1633 case GDK_SCROLL_DOWN:
1634 frames = get_frame_step (f, current_time(), -1);
1636 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1640 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1643 set (current_time() - frames, true);
1646 ValueChanged (); /* EMIT_SIGNAL */
1659 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1661 if (editing || _session == 0 || !dragging) {
1665 float pixel_frame_scale_factor = 0.2f;
1667 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1668 pixel_frame_scale_factor = 0.1f;
1672 if (Keyboard::modifier_state_contains (ev->state,
1673 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1675 pixel_frame_scale_factor = 0.025f;
1678 double y_delta = ev->y - drag_y;
1680 drag_accum += y_delta*pixel_frame_scale_factor;
1684 if (floor (drag_accum) != 0) {
1689 dir = (drag_accum < 0 ? 1:-1);
1690 pos = current_time();
1691 frames = get_frame_step (drag_field, pos, dir);
1693 if (frames != 0 && frames * drag_accum < current_time()) {
1694 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1700 ValueChanged(); /* EMIT_SIGNAL */
1707 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1710 Timecode::BBT_Time BBT;
1712 case Timecode_Hours:
1713 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1715 case Timecode_Minutes:
1716 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1718 case Timecode_Seconds:
1719 f = _session->frame_rate();
1721 case Timecode_Frames:
1722 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1730 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1733 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1736 f = (framecnt_t) _session->frame_rate();
1738 case MS_Milliseconds:
1739 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1746 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1752 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1758 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1761 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1770 AudioClock::current_time (framepos_t) const
1776 AudioClock::current_duration (framepos_t pos) const
1785 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1801 AudioClock::bbt_validate_edit (const string& str)
1805 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1809 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1813 if (!is_duration && any.bbt.bars == 0) {
1817 if (!is_duration && any.bbt.beats == 0) {
1825 AudioClock::timecode_validate_edit (const string&)
1831 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1832 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1837 TC.hours = hours * -1;
1841 TC.negative = false;
1844 if (TC.negative && !_negative_allowed) {
1848 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1852 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1856 if (_session->timecode_drop_frames()) {
1857 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1866 AudioClock::minsec_validate_edit (const string& str)
1868 int hrs, mins, secs, millisecs;
1870 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1874 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1882 AudioClock::frames_from_timecode_string (const string& str) const
1884 if (_session == 0) {
1893 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1894 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1897 TC.hours = abs(hours);
1898 TC.rate = _session->timecode_frames_per_second();
1899 TC.drop= _session->timecode_drop_frames();
1901 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1903 // timecode_tester ();
1904 if (edit_is_negative) {
1912 AudioClock::frames_from_minsec_string (const string& str) const
1914 if (_session == 0) {
1918 int hrs, mins, secs, millisecs;
1919 framecnt_t sr = _session->frame_rate();
1921 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1922 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1926 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1930 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1932 if (_session == 0) {
1933 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1938 any.type = AnyTime::BBT;
1940 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1947 return _session->any_duration_to_frames (pos, any);
1949 return _session->convert_to_frames (any);
1955 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1957 if (_session == 0) {
1958 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
1962 Timecode::BBT_Time bbt;
1964 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1968 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1972 AudioClock::frames_from_audioframes_string (const string& str) const
1975 sscanf (str.c_str(), "%" PRId64, &f);
1980 AudioClock::copy_text_to_clipboard () const
1984 val = pre_edit_string;
1986 val = _layout->get_text ();
1988 const size_t trim = val.find_first_not_of(" ");
1989 if (trim == string::npos) {
1990 assert(0); // empty clock, can't be right.
1993 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
1994 cl->set_text (val.substr(trim));
1998 AudioClock::build_ops_menu ()
2000 using namespace Menu_Helpers;
2001 ops_menu = new Menu;
2002 MenuList& ops_items = ops_menu->items();
2003 ops_menu->set_name ("ArdourContextMenu");
2005 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2006 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2007 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2008 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2010 if (editable && !_off && !is_duration && !_follows_playhead) {
2011 ops_items.push_back (SeparatorElem());
2012 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2013 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2015 ops_items.push_back (SeparatorElem());
2016 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2020 AudioClock::set_from_playhead ()
2026 set (_session->transport_frame());
2031 AudioClock::locate ()
2033 if (!_session || is_duration) {
2037 _session->request_locate (current_time(), _session->transport_rolling ());
2041 AudioClock::set_mode (Mode m, bool noemit)
2051 _layout->set_text ("");
2053 Gtk::Requisition req;
2054 set_clock_dimensions (req);
2058 insert_map.push_back (11);
2059 insert_map.push_back (10);
2060 insert_map.push_back (8);
2061 insert_map.push_back (7);
2062 insert_map.push_back (5);
2063 insert_map.push_back (4);
2064 insert_map.push_back (2);
2065 insert_map.push_back (1);
2069 insert_map.push_back (11);
2070 insert_map.push_back (10);
2071 insert_map.push_back (9);
2072 insert_map.push_back (8);
2073 insert_map.push_back (6);
2074 insert_map.push_back (5);
2075 insert_map.push_back (3);
2076 insert_map.push_back (2);
2077 insert_map.push_back (1);
2081 insert_map.push_back (12);
2082 insert_map.push_back (11);
2083 insert_map.push_back (10);
2084 insert_map.push_back (8);
2085 insert_map.push_back (7);
2086 insert_map.push_back (5);
2087 insert_map.push_back (4);
2088 insert_map.push_back (2);
2089 insert_map.push_back (1);
2096 set (last_when, true);
2098 if (!is_transient && !noemit) {
2099 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2102 mode_changed (); /* EMIT SIGNAL (the member one) */
2106 AudioClock::set_bbt_reference (framepos_t pos)
2108 bbt_reference_time = pos;
2112 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2114 CairoWidget::on_style_changed (old_style);
2116 Gtk::Requisition req;
2117 set_clock_dimensions (req);
2119 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2125 AudioClock::set_editable (bool yn)
2131 AudioClock::set_is_duration (bool yn)
2133 if (yn == is_duration) {
2138 set (last_when, true);
2142 AudioClock::set_off (bool yn)
2150 /* force a redraw. last_when will be preserved, but the clock text will
2154 set (last_when, true);
2158 AudioClock::focus ()
2160 start_edit (Field (0));
2164 AudioClock::set_corner_radius (double r)
2173 AudioClock::dpi_reset ()
2175 /* force recomputation of size even if we are fixed width
2183 AudioClock::set_negative_allowed (bool yn)
2185 _negative_allowed = yn;