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/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
32 #include "widgets/tooltips.h"
34 #include "ardour/profile.h"
35 #include "ardour/lmath.h"
36 #include "ardour/session.h"
37 #include "ardour/transport_master.h"
38 #include "ardour/tempo.h"
39 #include "ardour/transport_master_manager.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 AudioClock::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 AudioClock::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);
482 return edit_string.substr (0, 8);
484 return edit_string.substr (9, 1);
493 AudioClock::end_edit (bool modify)
501 ok = timecode_validate_edit (edit_string);
505 ok = bbt_validate_edit (edit_string);
509 ok = minsec_validate_edit (edit_string);
515 if (edit_string.length() < 1) {
516 edit_string = pre_edit_string;
522 edit_string = pre_edit_string;
523 input_string.clear ();
524 _layout->set_text (edit_string);
525 show_edit_status (0);
526 /* edit attributes remain in use */
530 samplepos_t pos = 0; /* stupid gcc */
534 pos = samples_from_timecode_string (edit_string);
539 pos = sample_duration_from_bbt_string (bbt_reference_time, edit_string);
541 pos = samples_from_bbt_string (0, edit_string);
546 pos = samples_from_minsec_string (edit_string);
550 pos = samples_from_seconds_string (edit_string);
554 pos = samples_from_audiosamples_string (edit_string);
558 AudioClock::set (pos, true);
559 _layout->set_attributes (normal_attributes);
560 ValueChanged(); /* EMIT_SIGNAL */
566 edit_is_negative = false;
567 _layout->set_attributes (normal_attributes);
568 _layout->set_text (pre_edit_string);
579 AudioClock::drop_focus ()
581 Keyboard::magic_widget_drop_focus ();
584 /* move focus back to the default widget in the top level window */
585 ARDOUR_UI::instance()->reset_focus (this);
590 AudioClock::parse_as_seconds_distance (const std::string& str)
594 if (sscanf (str.c_str(), "%f", &f) == 1) {
595 return f * _session->sample_rate();
602 AudioClock::parse_as_samples_distance (const std::string& str)
606 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
614 AudioClock::parse_as_minsec_distance (const std::string& str)
616 samplecnt_t sr = _session->sample_rate();
622 switch (str.length()) {
629 sscanf (str.c_str(), "%" PRId32, &msecs);
630 return msecs * (sr / 1000);
633 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
634 return (secs * sr) + (msecs * (sr/1000));
637 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
638 return (secs * sr) + (msecs * (sr/1000));
641 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
642 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
645 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
646 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
649 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
650 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
653 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
654 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
664 AudioClock::parse_as_timecode_distance (const std::string& str)
666 double fps = _session->timecode_frames_per_second();
667 samplecnt_t sr = _session->sample_rate();
673 switch (str.length()) {
678 sscanf (str.c_str(), "%" PRId32, &samples);
679 return llrint ((samples/(float)fps) * sr);
682 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &samples);
683 return (secs * sr) + llrint ((samples/(float)fps) * sr);
686 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &samples);
687 return (secs * sr) + llrint ((samples/(float)fps) * sr);
690 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &samples);
691 return (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
694 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &samples);
695 return (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
698 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &samples);
699 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
702 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &samples);
703 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
713 AudioClock::parse_as_bbt_distance (const std::string&)
719 AudioClock::parse_as_distance (const std::string& instr)
723 return parse_as_timecode_distance (instr);
726 return parse_as_samples_distance (instr);
729 return parse_as_bbt_distance (instr);
732 return parse_as_minsec_distance (instr);
735 return parse_as_seconds_distance (instr);
742 AudioClock::end_edit_relative (bool add)
748 ok = timecode_validate_edit (edit_string);
752 ok = bbt_validate_edit (edit_string);
756 ok = minsec_validate_edit (edit_string);
767 edit_string = pre_edit_string;
768 input_string.clear ();
769 _layout->set_text (edit_string);
770 show_edit_status (0);
771 /* edit attributes remain in use */
776 samplecnt_t samples = parse_as_distance (input_string);
781 _layout->set_attributes (normal_attributes);
785 AudioClock::set (current_time() + samples, true);
787 samplepos_t c = current_time();
789 if (c > samples || _negative_allowed) {
790 AudioClock::set (c - samples, true);
792 AudioClock::set (0, true);
795 ValueChanged (); /* EMIT SIGNAL */
798 input_string.clear ();
804 AudioClock::session_property_changed (const PropertyChange&)
806 AudioClock::set (last_when, true);
810 AudioClock::session_configuration_changed (std::string p)
812 if (_negative_allowed) {
813 /* session option editor clock */
817 if (p == "sync-source" || p == "external-sync") {
818 AudioClock::set (current_time(), true);
822 if (p != "timecode-offset" && p != "timecode-offset-negative") {
831 current = current_duration ();
833 current = current_time ();
835 AudioClock::set (current, true);
843 AudioClock::set (samplepos_t when, bool force, samplecnt_t offset)
845 if ((!force && !is_visible()) || _session == 0) {
851 when = when - offset;
854 if (when > _limit_pos) {
856 } else if (when < -_limit_pos) {
860 if (when == last_when && !force) {
861 #if 0 // XXX return if no change and no change forced. verify Aug/2014
862 if (_mode != Timecode && _mode != MinSec) {
863 /* may need to force display of TC source
864 * time, so don't return early.
866 /* ^^ Why was that?, delta times?
867 * Timecode FPS, pull-up/down, etc changes
868 * trigger a 'session_property_changed' which
869 * eventually calls set(last_when, true)
871 * re-rendering the clock every 40ms or so just
872 * because we can is not ideal.
887 set_timecode (when, force);
891 set_bbt (when, offset, force);
896 set_minsec (when, force);
900 set_seconds (when, force);
904 set_samples (when, force);
910 _left_btn.set_sensitive (btn_en);
911 _right_btn.set_sensitive (btn_en);
912 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
913 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
915 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
916 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
917 _left_btn.set_alignment (.5, .5);
918 _right_btn.set_alignment (.5, .5);
919 set_tooltip (_left_btn, _("Change current tempo"));
920 set_tooltip (_right_btn, _("Change current time signature"));
922 _left_btn.set_elements (ArdourButton::Text);
923 _right_btn.set_elements (ArdourButton::Text);
924 _left_btn.set_alignment (0, .5);
925 _right_btn.set_alignment (1, .5);
926 set_tooltip (_left_btn, _(""));
927 set_tooltip (_right_btn, _(""));
936 AudioClock::set_slave_info ()
942 boost::shared_ptr<TransportMaster> tm = TransportMasterManager::instance().current();
944 if (_session->transport_master_is_external()) {
946 switch (tm->type()) {
948 _left_btn.set_text (tm->name(), true);
949 _right_btn.set_text ("", true);
953 _left_btn.set_text (tm->display_name(), true);
954 _right_btn.set_text (tm->delta_string (), true);
956 _left_btn.set_text (_("--pending--"), true);
957 _right_btn.set_text ("", true);
964 boost::shared_ptr<TimecodeTransportMaster> tcmaster;
965 if ((tcmaster = boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)) != 0) {
966 matching = (tcmaster->apparent_timecode_format() == _session->config.get_timecode_format());
967 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
968 tm->display_name()[0],
969 tcmaster->position_string (),
970 matching ? "#66ff66" : "#ff3333"
972 _right_btn.set_text (tm->delta_string (), true);
975 _left_btn.set_text (_("--pending--"), true);
976 _right_btn.set_text ("", true);
981 _left_btn.set_text (string_compose ("%1/%2", _("INT"), tm->display_name()), true);
982 _right_btn.set_text ("", true);
987 AudioClock::set_out_of_bounds (bool negative)
991 _layout->set_text (" >>> -- <<< ");
993 _layout->set_text (" >>> ++ <<< ");
997 _layout->set_text (" <<<<<<<<<< ");
999 _layout->set_text (" >>>>>>>>>> ");
1005 AudioClock::set_samples (samplepos_t when, bool /*force*/)
1008 bool negative = false;
1011 _layout->set_text (" ----------");
1012 _left_btn.set_text ("", true);
1013 _right_btn.set_text ("", true);
1022 if (when >= _limit_pos) {
1023 set_out_of_bounds (negative);
1024 } else if (negative) {
1025 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1026 _layout->set_text (buf);
1028 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1029 _layout->set_text (buf);
1033 samplecnt_t rate = _session->sample_rate();
1035 if (fmod (rate, 100.0) == 0.0) {
1036 sprintf (buf, "%.1fkHz", rate/1000.0);
1038 sprintf (buf, "%" PRId64 "Hz", rate);
1041 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
1043 float vid_pullup = _session->config.get_video_pullup();
1045 if (vid_pullup == 0.0) {
1046 _right_btn.set_text ("", true);
1048 sprintf (buf, _("%+.4f%%"), vid_pullup);
1049 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
1055 AudioClock::set_seconds (samplepos_t when, bool /*force*/)
1060 _layout->set_text (" ----------");
1061 _left_btn.set_text ("", true);
1062 _right_btn.set_text ("", true);
1066 if (when >= _limit_pos || when <= -_limit_pos) {
1067 set_out_of_bounds (when < 0);
1070 snprintf (buf, sizeof (buf), "%12.1f", when / (float)_session->sample_rate());
1072 snprintf (buf, sizeof (buf), " %11.1f", when / (float)_session->sample_rate());
1074 _layout->set_text (buf);
1081 AudioClock::print_minsec (samplepos_t when, char* buf, size_t bufsize, float sample_rate)
1098 hrs = (int) floor (left / (sample_rate * 60.0f * 60.0f));
1099 left -= (samplecnt_t) floor (hrs * sample_rate * 60.0f * 60.0f);
1100 mins = (int) floor (left / (sample_rate * 60.0f));
1101 left -= (samplecnt_t) floor (mins * sample_rate * 60.0f);
1102 secs = (int) floor (left / (float) sample_rate);
1103 left -= (samplecnt_t) floor ((double)(secs * sample_rate));
1104 millisecs = floor (left * 1000.0 / (float) sample_rate);
1107 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1109 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1115 AudioClock::set_minsec (samplepos_t when, bool /*force*/)
1120 _layout->set_text (" --:--:--.---");
1121 _left_btn.set_text ("", true);
1122 _right_btn.set_text ("", true);
1127 if (when >= _limit_pos || when <= -_limit_pos) {
1128 set_out_of_bounds (when < 0);
1130 print_minsec (when, buf, sizeof (buf), _session->sample_rate());
1131 _layout->set_text (buf);
1138 AudioClock::set_timecode (samplepos_t when, bool /*force*/)
1141 bool negative = false;
1144 _layout->set_text (" --:--:--:--");
1145 _left_btn.set_text ("", true);
1146 _right_btn.set_text ("", true);
1154 if (when >= _limit_pos) {
1155 set_out_of_bounds (negative);
1161 _session->timecode_duration (when, TC);
1163 _session->timecode_time (when, TC);
1166 TC.negative = TC.negative || negative;
1168 _layout->set_text (Timecode::timecode_format_time(TC));
1174 AudioClock::set_bbt (samplepos_t when, samplecnt_t offset, bool /*force*/)
1177 Timecode::BBT_Time BBT;
1178 bool negative = false;
1180 if (_off || when >= _limit_pos || when < -_limit_pos) {
1181 _layout->set_text (" ---|--|----");
1182 _left_btn.set_text ("", true);
1183 _right_btn.set_text ("", true);
1192 /* handle a common case */
1199 TempoMap& tmap (_session->tempo_map());
1202 offset = bbt_reference_time;
1205 const double divisions = tmap.meter_section_at_sample (offset).divisions_per_bar();
1206 Timecode::BBT_Time sub_bbt;
1209 BBT = tmap.bbt_at_beat (tmap.beat_at_sample (offset));
1210 sub_bbt = tmap.bbt_at_sample (offset - when);
1212 BBT = tmap.bbt_at_beat (tmap.beat_at_sample (when + offset));
1213 sub_bbt = tmap.bbt_at_sample (offset);
1216 BBT.bars -= sub_bbt.bars;
1218 if (BBT.ticks < sub_bbt.ticks) {
1219 if (BBT.beats == 1) {
1221 BBT.beats = divisions;
1225 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1227 BBT.ticks -= sub_bbt.ticks;
1230 if (BBT.beats < sub_bbt.beats) {
1232 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1234 BBT.beats -= sub_bbt.beats;
1238 BBT = _session->tempo_map().bbt_at_sample (when);
1242 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1243 BBT.bars, BBT.beats, BBT.ticks);
1245 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1246 BBT.bars, BBT.beats, BBT.ticks);
1249 _layout->set_text (buf);
1254 if (bbt_reference_time < 0) {
1257 pos = bbt_reference_time;
1260 TempoMetric m (_session->tempo_map().metric_at (pos));
1262 if (m.tempo().note_type() == 4) {
1263 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1264 _left_btn.set_text (string_compose ("%1", buf), true);
1265 } else if (m.tempo().note_type() == 8) {
1266 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1267 _left_btn.set_text (string_compose ("%1", buf), true);
1269 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1270 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1273 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1274 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1279 AudioClock::set_session (Session *s)
1281 SessionHandlePtr::set_session (s);
1285 int64_t limit_sec = UIConfiguration::instance().get_clock_display_limit ();
1286 if (limit_sec > 0) {
1287 _limit_pos = (samplecnt_t) floor ((double)(limit_sec * _session->sample_rate()));
1290 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1291 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1292 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1293 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1295 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1298 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1300 if ((*i)->get_property (X_("name"), name) && name == _name) {
1302 AudioClock::Mode amode;
1303 if ((*i)->get_property (X_("mode"), amode)) {
1304 set_mode (amode, true);
1307 if ((*i)->get_property (X_("on"), on)) {
1315 AudioClock::set (last_when, true);
1320 AudioClock::on_key_press_event (GdkEventKey* ev)
1328 int highlight_length;
1331 switch (ev->keyval) {
1374 case GDK_KP_Subtract:
1375 if (_negative_allowed && input_string.empty()) {
1376 edit_is_negative = true;
1377 edit_string.replace(0,1,"-");
1378 _layout->set_text (edit_string);
1381 end_edit_relative (false);
1387 end_edit_relative (true);
1400 ChangeAborted(); /* EMIT SIGNAL */
1405 if (!input_string.empty()) {
1406 /* delete the last key entered
1408 input_string = input_string.substr (0, input_string.length() - 1);
1410 goto use_input_string;
1413 /* do not allow other keys to passthru to the rest of the GUI
1419 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1420 /* too many digits: eat the key event, but do nothing with it */
1424 input_string.push_back (new_char);
1430 /* get this one in the right order, and to the right width */
1431 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1432 edit_string = edit_string.substr (0, edit_string.length() - 1);
1434 edit_string.push_back (new_char);
1436 if (!edit_string.empty()) {
1438 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1439 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1442 /* highlight the whole thing */
1443 highlight_length = edit_string.length();
1447 highlight_length = merge_input_and_edit_string ();
1450 if (edit_is_negative) {
1451 edit_string.replace(0,1,"-");
1453 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1454 edit_string.replace(0,1,"_");
1456 edit_string.replace(0,1," ");
1460 show_edit_status (highlight_length);
1461 _layout->set_text (edit_string);
1468 AudioClock::merge_input_and_edit_string ()
1470 /* merge with pre-edit-string into edit string */
1472 edit_string = pre_edit_string;
1474 if (input_string.empty()) {
1478 string::size_type target;
1479 for (string::size_type i = 0; i < input_string.length(); ++i) {
1480 target = insert_map[input_string.length() - 1 - i];
1481 edit_string[target] = input_string[i];
1483 /* highlight from end to wherever the last character was added */
1484 return edit_string.length() - insert_map[input_string.length()-1];
1489 AudioClock::on_key_release_event (GdkEventKey *ev)
1495 /* return true for keys that we used on press
1496 so that they cannot possibly do double-duty
1498 switch (ev->keyval) {
1521 case GDK_KP_Decimal:
1529 case GDK_KP_Subtract:
1537 AudioClock::index_to_field (int index) const
1542 return Timecode_Hours;
1543 } else if (index < 7) {
1544 return Timecode_Minutes;
1545 } else if (index < 10) {
1546 return Timecode_Seconds;
1548 return Timecode_frames;
1554 } else if (index < 7) {
1562 return Timecode_Hours;
1563 } else if (index < 6) {
1565 } else if (index < 9) {
1568 return MS_Milliseconds;
1575 return SS_Deciseconds;
1587 AudioClock::on_button_press_event (GdkEventButton *ev)
1589 if (!_session || _session->actively_recording()) {
1590 /* swallow event, do nothing */
1594 switch (ev->button) {
1596 if (editable && !_off) {
1602 /* the text has been centered vertically, so adjust
1605 int xcenter = (get_width() - layout_width) /2;
1607 y = ev->y - ((get_height() - layout_height)/2);
1608 x = ev->x - xcenter;
1610 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1611 /* pretend it is a character on the far right */
1614 drag_field = index_to_field (index);
1616 /* make absolutely sure that the pointer is grabbed */
1617 gdk_pointer_grab(ev->window,false ,
1618 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1619 NULL,NULL,ev->time);
1621 drag_start_y = ev->y;
1635 AudioClock::on_button_release_event (GdkEventButton *ev)
1637 if (!_session || _session->actively_recording()) {
1638 /* swallow event, do nothing */
1642 if (editable && !_off) {
1644 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1646 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1647 // we actually dragged so return without
1648 // setting editing focus, or we shift clicked
1651 if (ev->button == 1) {
1653 if (_edit_by_click_field) {
1655 int xcenter = (get_width() - layout_width) /2;
1658 int y = ev->y - ((get_height() - layout_height)/2);
1659 int x = ev->x - xcenter;
1662 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1666 f = index_to_field (index);
1669 case Timecode_frames:
1670 case MS_Milliseconds:
1672 case SS_Deciseconds:
1687 if (Keyboard::is_context_menu_event (ev)) {
1688 if (ops_menu == 0) {
1691 ops_menu->popup (1, ev->time);
1699 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1701 bool ret = CairoWidget::on_focus_out_event (ev);
1704 end_edit (_accept_on_focus_out);
1711 AudioClock::on_scroll_event (GdkEventScroll *ev)
1716 if (editing || _session == 0 || !editable || _off || _session->actively_recording()) {
1723 /* the text has been centered vertically, so adjust
1727 int xcenter = (get_width() - layout_width) /2;
1728 y = ev->y - ((get_height() - layout_height)/2);
1729 x = ev->x - xcenter;
1731 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1732 /* not in the main layout */
1736 Field f = index_to_field (index);
1737 samplepos_t samples = 0;
1739 switch (ev->direction) {
1742 samples = get_sample_step (f, current_time(), 1);
1744 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1747 AudioClock::set (current_time() + samples, true);
1748 ValueChanged (); /* EMIT_SIGNAL */
1752 case GDK_SCROLL_DOWN:
1753 samples = get_sample_step (f, current_time(), -1);
1755 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1759 if (!_negative_allowed && (double)current_time() - (double)samples < 0.0) {
1760 AudioClock::set (0, true);
1762 AudioClock::set (current_time() - samples, true);
1765 ValueChanged (); /* EMIT_SIGNAL */
1778 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1780 if (editing || _session == 0 || !dragging || _session->actively_recording()) {
1784 float pixel_sample_scale_factor = 0.2f;
1786 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1787 pixel_sample_scale_factor = 0.1f;
1791 if (Keyboard::modifier_state_contains (ev->state,
1792 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1793 pixel_sample_scale_factor = 0.025f;
1796 double y_delta = ev->y - drag_y;
1798 drag_accum += y_delta*pixel_sample_scale_factor;
1802 if (floor (drag_accum) != 0) {
1804 samplepos_t samples;
1807 dir = (drag_accum < 0 ? 1:-1);
1808 pos = current_time();
1809 samples = get_sample_step (drag_field, pos, dir);
1811 if (samples != 0 && samples * drag_accum < current_time()) {
1812 AudioClock::set ((samplepos_t) floor (pos - drag_accum * samples), false); // minus because up is negative in GTK
1814 AudioClock::set (0 , false);
1818 ValueChanged(); /* EMIT_SIGNAL */
1825 AudioClock::get_sample_step (Field field, samplepos_t pos, int dir)
1828 Timecode::BBT_Time BBT;
1830 case Timecode_Hours:
1831 f = (samplecnt_t) floor (3600.0 * _session->sample_rate());
1833 case Timecode_Minutes:
1834 f = (samplecnt_t) floor (60.0 * _session->sample_rate());
1836 case Timecode_Seconds:
1837 f = _session->sample_rate();
1839 case Timecode_frames:
1840 f = (samplecnt_t) floor (_session->sample_rate() / _session->timecode_frames_per_second());
1848 f = (samplecnt_t) _session->sample_rate();
1850 case SS_Deciseconds:
1851 f = (samplecnt_t) _session->sample_rate() / 10.f;
1855 f = (samplecnt_t) floor (3600.0 * _session->sample_rate());
1858 f = (samplecnt_t) floor (60.0 * _session->sample_rate());
1861 f = (samplecnt_t) _session->sample_rate();
1863 case MS_Milliseconds:
1864 f = (samplecnt_t) floor (_session->sample_rate() / 1000.0);
1871 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1877 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1883 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1886 error << string_compose (_("programming error: %1"), "attempt to get samples from non-text field!") << endmsg;
1895 AudioClock::current_time (samplepos_t) const
1901 AudioClock::current_duration (samplepos_t pos) const
1903 samplepos_t ret = 0;
1907 ret = sample_duration_from_bbt_string (pos, _layout->get_text());
1922 AudioClock::bbt_validate_edit (string & str)
1926 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1930 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1934 if (!is_duration && any.bbt.bars == 0) {
1938 if (!is_duration && any.bbt.beats == 0) {
1939 /* user could not have mean zero beats because for a
1940 * non-duration clock that's impossible. Assume that they
1941 * mis-entered things and meant Bar|1|ticks
1945 snprintf (buf, sizeof (buf), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, any.bbt.bars, 1, any.bbt.ticks);
1953 AudioClock::timecode_validate_edit (const string&)
1959 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1960 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1965 TC.hours = hours * -1;
1969 TC.negative = false;
1972 if (TC.negative && !_negative_allowed) {
1976 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1980 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1984 if (_session->timecode_drop_frames()) {
1985 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1994 AudioClock::minsec_validate_edit (const string& str)
1996 int hrs, mins, secs, millisecs;
1998 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2002 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
2010 AudioClock::samples_from_timecode_string (const string& str) const
2012 if (_session == 0) {
2021 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2022 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2025 TC.hours = abs(hours);
2026 TC.rate = _session->timecode_frames_per_second();
2027 TC.drop= _session->timecode_drop_frames();
2029 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2031 // timecode_tester ();
2032 if (edit_is_negative) {
2040 AudioClock::samples_from_minsec_string (const string& str) const
2042 if (_session == 0) {
2046 int hrs, mins, secs, millisecs;
2047 samplecnt_t sr = _session->sample_rate();
2049 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2050 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2054 return (samplepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2058 AudioClock::samples_from_bbt_string (samplepos_t pos, const string& str) const
2060 if (_session == 0) {
2061 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2066 any.type = AnyTime::BBT;
2068 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2075 return _session->any_duration_to_samples (pos, any);
2077 return _session->convert_to_samples (any);
2083 AudioClock::sample_duration_from_bbt_string (samplepos_t pos, const string& str) const
2085 if (_session == 0) {
2086 error << "AudioClock::sample_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2090 Timecode::BBT_Time bbt;
2092 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2096 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2100 AudioClock::samples_from_seconds_string (const string& str) const
2103 sscanf (str.c_str(), "%f", &f);
2104 return f * _session->sample_rate();
2108 AudioClock::samples_from_audiosamples_string (const string& str) const
2111 sscanf (str.c_str(), "%" PRId64, &f);
2116 AudioClock::copy_text_to_clipboard () const
2120 val = pre_edit_string;
2122 val = _layout->get_text ();
2124 const size_t trim = val.find_first_not_of(" ");
2125 if (trim == string::npos) {
2126 assert(0); // empty clock, can't be right.
2129 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2130 cl->set_text (val.substr(trim));
2134 AudioClock::build_ops_menu ()
2136 using namespace Menu_Helpers;
2137 ops_menu = new Menu;
2138 MenuList& ops_items = ops_menu->items();
2139 ops_menu->set_name ("ArdourContextMenu");
2141 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2142 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2143 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2144 ops_items.push_back (MenuElem (_("Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Seconds, false)));
2145 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Samples, false)));
2147 if (editable && !_off && !is_duration && !_follows_playhead) {
2148 ops_items.push_back (SeparatorElem());
2149 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2150 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2152 ops_items.push_back (SeparatorElem());
2153 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2157 AudioClock::set_from_playhead ()
2163 AudioClock::set (_session->transport_sample());
2168 AudioClock::locate ()
2170 if (!_session || is_duration) {
2174 _session->request_locate (current_time(), _session->transport_rolling ());
2178 AudioClock::set_mode (Mode m, bool noemit)
2188 _layout->set_text ("");
2190 Gtk::Requisition req;
2191 set_clock_dimensions (req);
2195 insert_map.push_back (11);
2196 insert_map.push_back (10);
2197 insert_map.push_back (8);
2198 insert_map.push_back (7);
2199 insert_map.push_back (5);
2200 insert_map.push_back (4);
2201 insert_map.push_back (2);
2202 insert_map.push_back (1);
2206 insert_map.push_back (11);
2207 insert_map.push_back (10);
2208 insert_map.push_back (9);
2209 insert_map.push_back (8);
2210 insert_map.push_back (6);
2211 insert_map.push_back (5);
2212 insert_map.push_back (3);
2213 insert_map.push_back (2);
2214 insert_map.push_back (1);
2218 insert_map.push_back (12);
2219 insert_map.push_back (11);
2220 insert_map.push_back (10);
2221 insert_map.push_back (8);
2222 insert_map.push_back (7);
2223 insert_map.push_back (5);
2224 insert_map.push_back (4);
2225 insert_map.push_back (2);
2226 insert_map.push_back (1);
2230 insert_map.push_back (11);
2231 insert_map.push_back (9);
2232 insert_map.push_back (8);
2233 insert_map.push_back (7);
2234 insert_map.push_back (6);
2235 insert_map.push_back (5);
2236 insert_map.push_back (4);
2237 insert_map.push_back (3);
2238 insert_map.push_back (2);
2239 insert_map.push_back (1);
2246 AudioClock::set (last_when, true);
2248 if (!is_transient && !noemit) {
2249 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2252 mode_changed (); /* EMIT SIGNAL (the member one) */
2256 AudioClock::set_bbt_reference (samplepos_t pos)
2258 bbt_reference_time = pos;
2262 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2264 CairoWidget::on_style_changed (old_style);
2266 Gtk::Requisition req;
2267 set_clock_dimensions (req);
2269 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2275 AudioClock::set_editable (bool yn)
2281 AudioClock::set_is_duration (bool yn)
2283 if (yn == is_duration) {
2288 AudioClock::set (last_when, true);
2292 AudioClock::set_off (bool yn)
2300 /* force a redraw. last_when will be preserved, but the clock text will
2304 AudioClock::set (last_when, true);
2308 AudioClock::focus ()
2310 start_edit (Field (0));
2314 AudioClock::set_corner_radius (double r)
2323 AudioClock::dpi_reset ()
2325 /* force recomputation of size even if we are fixed width
2333 AudioClock::set_negative_allowed (bool yn)
2335 _negative_allowed = yn;