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 ()
131 delete foreground_attr;
136 AudioClock::set_widget_name (const string& str)
141 set_name (str + " clock");
151 AudioClock::on_realize ()
153 Gtk::Requisition req;
155 CairoWidget::on_realize ();
157 set_clock_dimensions (req);
159 first_width = req.width;
160 first_height = req.height;
162 // XXX FIX ME: define font based on ... ???
168 AudioClock::set_font (Pango::FontDescription font)
170 Glib::RefPtr<Gtk::Style> style = get_style ();
171 Pango::AttrFontDesc* font_attr;
173 font_size = font.get_size();
174 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
176 normal_attributes.change (*font_attr);
177 editing_attributes.change (*font_attr);
180 /* get the figure width for the font. This doesn't have to super
181 * accurate since we only use it to measure the (roughly 1 character)
182 * offset from the position Pango tells us for the "cursor"
185 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
189 tmp->get_pixel_size (em_width, ignore_height);
191 /* force redraw of markup with new font-size */
192 AudioClock::set (last_when, true);
196 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
198 CairoWidget::set_active_state (s);
203 AudioClock::set_colors ()
209 uint32_t editing_color;
210 uint32_t cursor_color;
212 if (active_state()) {
213 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
214 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
215 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
216 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
218 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
219 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
220 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
221 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
224 /* store for bg and cursor in render() */
226 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
233 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
240 /* rescale for Pango colors ... sigh */
242 r = lrint (r * 65535.0);
243 g = lrint (g * 65535.0);
244 b = lrint (b * 65535.0);
246 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
247 r = lrint ((r/255.0) * 65535.0);
248 g = lrint ((g/255.0) * 65535.0);
249 b = lrint ((b/255.0) * 65535.0);
250 delete foreground_attr;
251 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
253 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
254 r = lrint ((r/255.0) * 65535.0);
255 g = lrint ((g/255.0) * 65535.0);
256 b = lrint ((b/255.0) * 65535.0);
258 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
260 normal_attributes.change (*foreground_attr);
261 editing_attributes.change (*foreground_attr);
262 editing_attributes.change (*editing_attr);
265 _layout->set_attributes (normal_attributes);
267 _layout->set_attributes (editing_attributes);
274 AudioClock::set_scale (double x, double y)
283 AudioClock::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
285 cairo_t* cr = ctx->cobj();
286 /* main layout: rounded rect, plus the text */
289 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
291 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
293 cairo_rectangle (cr, 0, 0, get_width(), get_height());
298 double lw = layout_width * xscale;
299 double lh = layout_height * yscale;
301 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
303 if (xscale != 1.0 || yscale != 1.0) {
305 cairo_scale (cr, xscale, yscale);
308 pango_cairo_show_layout (cr, _layout->gobj());
310 if (xscale != 1.0 || yscale != 1.0) {
315 if (!insert_map.empty()) {
317 int xcenter = (get_width() - layout_width) /2;
319 if (input_string.length() < insert_map.size()) {
320 Pango::Rectangle cursor;
322 if (input_string.empty()) {
323 /* nothing entered yet, put cursor at the end
326 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
328 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
331 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
333 min (get_width() - 2.0,
334 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
335 (get_height() - layout_height)/2.0,
336 2.0, cursor.get_height()/PANGO_SCALE);
339 /* we've entered all possible digits, no cursor */
343 if (input_string.empty()) {
344 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
347 (get_height() - layout_height)/2.0,
356 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
358 Glib::RefPtr<Pango::Layout> tmp;
359 Glib::RefPtr<Gtk::Style> style = get_style ();
360 Pango::FontDescription font;
362 tmp = Pango::Layout::create (get_pango_context());
364 if (!is_realized()) {
365 font = get_font_for_style (get_name());
367 font = style->get_font();
370 tmp->set_font_description (font);
372 /* this string is the longest thing we will ever display */
374 tmp->set_text (" 88:88:88,888 ");
376 tmp->set_text (" 88:88:88,88 ");
377 tmp->get_pixel_size (req.width, req.height);
379 layout_height = req.height;
380 layout_width = req.width;
384 AudioClock::on_size_request (Gtk::Requisition* req)
386 /* even for non fixed width clocks, the size we *ask* for never changes,
387 even though the size we receive might. so once we've computed it,
392 req->width = first_width;
393 req->height = first_height;
397 set_clock_dimensions (*req);
399 /* now tackle height, for which we need to know the height of the lower
405 AudioClock::show_edit_status (int length)
407 editing_attr->set_start_index (edit_string.length() - length);
408 editing_attr->set_end_index (edit_string.length());
410 editing_attributes.change (*foreground_attr);
411 editing_attributes.change (*editing_attr);
413 _layout->set_attributes (editing_attributes);
417 AudioClock::start_edit (Field f)
420 pre_edit_string = _layout->get_text ();
421 if (!insert_map.empty()) {
422 edit_string = pre_edit_string;
424 edit_string.clear ();
425 _layout->set_text ("");
428 input_string.clear ();
430 edit_is_negative = false;
433 input_string = get_field (f);
434 show_edit_status (merge_input_and_edit_string ());
435 _layout->set_text (edit_string);
440 Keyboard::magic_widget_grab_focus ();
446 AudioClock::get_field (Field f)
450 return edit_string.substr (1, 2);
452 case Timecode_Minutes:
453 return edit_string.substr (4, 2);
455 case Timecode_Seconds:
456 return edit_string.substr (7, 2);
458 case Timecode_frames:
459 return edit_string.substr (10, 2);
462 return edit_string.substr (1, 2);
465 return edit_string.substr (4, 2);
468 return edit_string.substr (7, 2);
470 case MS_Milliseconds:
471 return edit_string.substr (10, 3);
474 return edit_string.substr (1, 3);
477 return edit_string.substr (5, 2);
480 return edit_string.substr (8, 4);
483 return edit_string.substr (0, 8);
485 return edit_string.substr (9, 1);
494 AudioClock::end_edit (bool modify)
502 ok = timecode_validate_edit (edit_string);
506 ok = bbt_validate_edit (edit_string);
510 ok = minsec_validate_edit (edit_string);
516 if (edit_string.length() < 1) {
517 edit_string = pre_edit_string;
523 edit_string = pre_edit_string;
524 input_string.clear ();
525 _layout->set_text (edit_string);
526 show_edit_status (0);
527 /* edit attributes remain in use */
531 samplepos_t pos = 0; /* stupid gcc */
535 pos = samples_from_timecode_string (edit_string);
540 pos = sample_duration_from_bbt_string (bbt_reference_time, edit_string);
542 pos = samples_from_bbt_string (0, edit_string);
547 pos = samples_from_minsec_string (edit_string);
551 pos = samples_from_seconds_string (edit_string);
555 pos = samples_from_audiosamples_string (edit_string);
559 AudioClock::set (pos, true);
560 _layout->set_attributes (normal_attributes);
561 ValueChanged(); /* EMIT_SIGNAL */
567 edit_is_negative = false;
568 _layout->set_attributes (normal_attributes);
569 _layout->set_text (pre_edit_string);
580 AudioClock::drop_focus ()
582 Keyboard::magic_widget_drop_focus ();
585 /* move focus back to the default widget in the top level window */
586 ARDOUR_UI::instance()->reset_focus (this);
591 AudioClock::parse_as_seconds_distance (const std::string& str)
595 if (sscanf (str.c_str(), "%f", &f) == 1) {
596 return f * _session->sample_rate();
603 AudioClock::parse_as_samples_distance (const std::string& str)
607 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
615 AudioClock::parse_as_minsec_distance (const std::string& str)
617 samplecnt_t sr = _session->sample_rate();
623 switch (str.length()) {
630 sscanf (str.c_str(), "%" PRId32, &msecs);
631 return msecs * (sr / 1000);
634 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
635 return (secs * sr) + (msecs * (sr/1000));
638 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
639 return (secs * sr) + (msecs * (sr/1000));
642 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
643 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
646 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
647 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
650 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
651 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
654 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
655 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
665 AudioClock::parse_as_timecode_distance (const std::string& str)
667 double fps = _session->timecode_frames_per_second();
668 samplecnt_t sr = _session->sample_rate();
674 switch (str.length()) {
679 sscanf (str.c_str(), "%" PRId32, &samples);
680 return llrint ((samples/(float)fps) * sr);
683 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &samples);
684 return (secs * sr) + llrint ((samples/(float)fps) * sr);
687 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &samples);
688 return (secs * sr) + llrint ((samples/(float)fps) * sr);
691 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &samples);
692 return (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
695 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &samples);
696 return (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
699 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &samples);
700 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
703 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &samples);
704 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((samples/(float)fps) * sr);
714 AudioClock::parse_as_bbt_distance (const std::string&)
720 AudioClock::parse_as_distance (const std::string& instr)
724 return parse_as_timecode_distance (instr);
727 return parse_as_samples_distance (instr);
730 return parse_as_bbt_distance (instr);
733 return parse_as_minsec_distance (instr);
736 return parse_as_seconds_distance (instr);
743 AudioClock::end_edit_relative (bool add)
749 ok = timecode_validate_edit (edit_string);
753 ok = bbt_validate_edit (edit_string);
757 ok = minsec_validate_edit (edit_string);
768 edit_string = pre_edit_string;
769 input_string.clear ();
770 _layout->set_text (edit_string);
771 show_edit_status (0);
772 /* edit attributes remain in use */
777 samplecnt_t samples = parse_as_distance (input_string);
782 _layout->set_attributes (normal_attributes);
786 AudioClock::set (current_time() + samples, true);
788 samplepos_t c = current_time();
790 if (c > samples || _negative_allowed) {
791 AudioClock::set (c - samples, true);
793 AudioClock::set (0, true);
796 ValueChanged (); /* EMIT SIGNAL */
799 input_string.clear ();
805 AudioClock::session_property_changed (const PropertyChange&)
807 AudioClock::set (last_when, true);
811 AudioClock::session_configuration_changed (std::string p)
813 if (_negative_allowed) {
814 /* session option editor clock */
818 if (p == "sync-source" || p == "external-sync") {
819 AudioClock::set (current_time(), true);
823 if (p != "timecode-offset" && p != "timecode-offset-negative") {
832 current = current_duration ();
834 current = current_time ();
836 AudioClock::set (current, true);
844 AudioClock::set (samplepos_t when, bool force, samplecnt_t offset)
846 if ((!force && !is_visible()) || _session == 0) {
852 when = when - offset;
855 if (when > _limit_pos) {
857 } else if (when < -_limit_pos) {
861 if (when == last_when && !force) {
862 #if 0 // XXX return if no change and no change forced. verify Aug/2014
863 if (_mode != Timecode && _mode != MinSec) {
864 /* may need to force display of TC source
865 * time, so don't return early.
867 /* ^^ Why was that?, delta times?
868 * Timecode FPS, pull-up/down, etc changes
869 * trigger a 'session_property_changed' which
870 * eventually calls set(last_when, true)
872 * re-rendering the clock every 40ms or so just
873 * because we can is not ideal.
888 set_timecode (when, force);
892 set_bbt (when, offset, force);
897 set_minsec (when, force);
901 set_seconds (when, force);
905 set_samples (when, force);
911 _left_btn.set_sensitive (btn_en);
912 _right_btn.set_sensitive (btn_en);
913 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
914 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
916 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
917 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
918 _left_btn.set_alignment (.5, .5);
919 _right_btn.set_alignment (.5, .5);
920 set_tooltip (_left_btn, _("Change current tempo"));
921 set_tooltip (_right_btn, _("Change current time signature"));
923 _left_btn.set_elements (ArdourButton::Text);
924 _right_btn.set_elements (ArdourButton::Text);
925 _left_btn.set_alignment (0, .5);
926 _right_btn.set_alignment (1, .5);
927 set_tooltip (_left_btn, _(""));
928 set_tooltip (_right_btn, _(""));
937 AudioClock::set_slave_info ()
943 boost::shared_ptr<TransportMaster> tm = TransportMasterManager::instance().current();
945 if (_session->transport_master_is_external()) {
947 switch (tm->type()) {
949 _left_btn.set_text (tm->name(), true);
950 _right_btn.set_text ("", true);
954 _left_btn.set_text (tm->display_name(), true);
955 _right_btn.set_text (tm->delta_string (), true);
957 _left_btn.set_text (_("--pending--"), true);
958 _right_btn.set_text ("", true);
965 boost::shared_ptr<TimecodeTransportMaster> tcmaster;
966 if ((tcmaster = boost::dynamic_pointer_cast<TimecodeTransportMaster>(tm)) != 0) {
967 matching = (tcmaster->apparent_timecode_format() == _session->config.get_timecode_format());
968 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
969 tm->display_name()[0],
970 tcmaster->position_string (),
971 matching ? "#66ff66" : "#ff3333"
973 _right_btn.set_text (tm->delta_string (), true);
976 _left_btn.set_text (_("--pending--"), true);
977 _right_btn.set_text ("", true);
982 _left_btn.set_text (string_compose ("%1/%2", _("INT"), tm->display_name()), true);
983 _right_btn.set_text ("", true);
988 AudioClock::set_out_of_bounds (bool negative)
992 _layout->set_text (" >>> -- <<< ");
994 _layout->set_text (" >>> ++ <<< ");
998 _layout->set_text (" <<<<<<<<<< ");
1000 _layout->set_text (" >>>>>>>>>> ");
1006 AudioClock::set_samples (samplepos_t when, bool /*force*/)
1009 bool negative = false;
1012 _layout->set_text (" ----------");
1013 _left_btn.set_text ("", true);
1014 _right_btn.set_text ("", true);
1023 if (when >= _limit_pos) {
1024 set_out_of_bounds (negative);
1025 } else if (negative) {
1026 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
1027 _layout->set_text (buf);
1029 snprintf (buf, sizeof (buf), " %10" PRId64, when);
1030 _layout->set_text (buf);
1034 samplecnt_t rate = _session->sample_rate();
1036 if (fmod (rate, 100.0) == 0.0) {
1037 sprintf (buf, "%.1fkHz", rate/1000.0);
1039 sprintf (buf, "%" PRId64 "Hz", rate);
1042 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
1044 float vid_pullup = _session->config.get_video_pullup();
1046 if (vid_pullup == 0.0) {
1047 _right_btn.set_text ("", true);
1049 sprintf (buf, _("%+.4f%%"), vid_pullup);
1050 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
1056 AudioClock::set_seconds (samplepos_t when, bool /*force*/)
1061 _layout->set_text (" ----------");
1062 _left_btn.set_text ("", true);
1063 _right_btn.set_text ("", true);
1067 if (when >= _limit_pos || when <= -_limit_pos) {
1068 set_out_of_bounds (when < 0);
1071 snprintf (buf, sizeof (buf), "%12.1f", when / (float)_session->sample_rate());
1073 snprintf (buf, sizeof (buf), " %11.1f", when / (float)_session->sample_rate());
1075 _layout->set_text (buf);
1082 AudioClock::print_minsec (samplepos_t when, char* buf, size_t bufsize, float sample_rate)
1099 hrs = (int) floor (left / (sample_rate * 60.0f * 60.0f));
1100 left -= (samplecnt_t) floor (hrs * sample_rate * 60.0f * 60.0f);
1101 mins = (int) floor (left / (sample_rate * 60.0f));
1102 left -= (samplecnt_t) floor (mins * sample_rate * 60.0f);
1103 secs = (int) floor (left / (float) sample_rate);
1104 left -= (samplecnt_t) floor ((double)(secs * sample_rate));
1105 millisecs = floor (left * 1000.0 / (float) sample_rate);
1108 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1110 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1116 AudioClock::set_minsec (samplepos_t when, bool /*force*/)
1121 _layout->set_text (" --:--:--.---");
1122 _left_btn.set_text ("", true);
1123 _right_btn.set_text ("", true);
1128 if (when >= _limit_pos || when <= -_limit_pos) {
1129 set_out_of_bounds (when < 0);
1131 print_minsec (when, buf, sizeof (buf), _session->sample_rate());
1132 _layout->set_text (buf);
1139 AudioClock::set_timecode (samplepos_t when, bool /*force*/)
1142 bool negative = false;
1145 _layout->set_text (" --:--:--:--");
1146 _left_btn.set_text ("", true);
1147 _right_btn.set_text ("", true);
1155 if (when >= _limit_pos) {
1156 set_out_of_bounds (negative);
1162 _session->timecode_duration (when, TC);
1164 _session->timecode_time (when, TC);
1167 TC.negative = TC.negative || negative;
1169 _layout->set_text (Timecode::timecode_format_time(TC));
1175 AudioClock::set_bbt (samplepos_t when, samplecnt_t offset, bool /*force*/)
1178 Timecode::BBT_Time BBT;
1179 bool negative = false;
1181 if (_off || when >= _limit_pos || when < -_limit_pos) {
1182 _layout->set_text (" ---|--|----");
1183 _left_btn.set_text ("", true);
1184 _right_btn.set_text ("", true);
1193 /* handle a common case */
1200 TempoMap& tmap (_session->tempo_map());
1203 offset = bbt_reference_time;
1206 const double divisions = tmap.meter_section_at_sample (offset).divisions_per_bar();
1207 Timecode::BBT_Time sub_bbt;
1210 BBT = tmap.bbt_at_beat (tmap.beat_at_sample (offset));
1211 sub_bbt = tmap.bbt_at_sample (offset - when);
1213 BBT = tmap.bbt_at_beat (tmap.beat_at_sample (when + offset));
1214 sub_bbt = tmap.bbt_at_sample (offset);
1217 BBT.bars -= sub_bbt.bars;
1219 if (BBT.ticks < sub_bbt.ticks) {
1220 if (BBT.beats == 1) {
1222 BBT.beats = divisions;
1226 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1228 BBT.ticks -= sub_bbt.ticks;
1231 if (BBT.beats < sub_bbt.beats) {
1233 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1235 BBT.beats -= sub_bbt.beats;
1239 BBT = _session->tempo_map().bbt_at_sample (when);
1243 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1244 BBT.bars, BBT.beats, BBT.ticks);
1246 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1247 BBT.bars, BBT.beats, BBT.ticks);
1250 _layout->set_text (buf);
1255 if (bbt_reference_time < 0) {
1258 pos = bbt_reference_time;
1261 TempoMetric m (_session->tempo_map().metric_at (pos));
1263 if (m.tempo().note_type() == 4) {
1264 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1265 _left_btn.set_text (string_compose ("%1", buf), true);
1266 } else if (m.tempo().note_type() == 8) {
1267 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1268 _left_btn.set_text (string_compose ("%1", buf), true);
1270 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_sample (pos).note_types_per_minute());
1271 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1274 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1275 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1280 AudioClock::set_session (Session *s)
1282 SessionHandlePtr::set_session (s);
1286 int64_t limit_sec = UIConfiguration::instance().get_clock_display_limit ();
1287 if (limit_sec > 0) {
1288 _limit_pos = (samplecnt_t) floor ((double)(limit_sec * _session->sample_rate()));
1291 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1292 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1293 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1294 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1296 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1299 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1301 if ((*i)->get_property (X_("name"), name) && name == _name) {
1303 AudioClock::Mode amode;
1304 if ((*i)->get_property (X_("mode"), amode)) {
1305 set_mode (amode, true);
1308 if ((*i)->get_property (X_("on"), on)) {
1316 AudioClock::set (last_when, true);
1321 AudioClock::on_key_press_event (GdkEventKey* ev)
1329 int highlight_length;
1332 switch (ev->keyval) {
1375 case GDK_KP_Subtract:
1376 if (_negative_allowed && input_string.empty()) {
1377 edit_is_negative = true;
1378 edit_string.replace(0,1,"-");
1379 _layout->set_text (edit_string);
1382 end_edit_relative (false);
1388 end_edit_relative (true);
1401 ChangeAborted(); /* EMIT SIGNAL */
1406 if (!input_string.empty()) {
1407 /* delete the last key entered
1409 input_string = input_string.substr (0, input_string.length() - 1);
1411 goto use_input_string;
1414 /* do not allow other keys to passthru to the rest of the GUI
1420 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1421 /* too many digits: eat the key event, but do nothing with it */
1425 input_string.push_back (new_char);
1431 /* get this one in the right order, and to the right width */
1432 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1433 edit_string = edit_string.substr (0, edit_string.length() - 1);
1435 edit_string.push_back (new_char);
1437 if (!edit_string.empty()) {
1439 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1440 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1443 /* highlight the whole thing */
1444 highlight_length = edit_string.length();
1448 highlight_length = merge_input_and_edit_string ();
1451 if (edit_is_negative) {
1452 edit_string.replace(0,1,"-");
1454 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1455 edit_string.replace(0,1,"_");
1457 edit_string.replace(0,1," ");
1461 show_edit_status (highlight_length);
1462 _layout->set_text (edit_string);
1469 AudioClock::merge_input_and_edit_string ()
1471 /* merge with pre-edit-string into edit string */
1473 edit_string = pre_edit_string;
1475 if (input_string.empty()) {
1479 string::size_type target;
1480 for (string::size_type i = 0; i < input_string.length(); ++i) {
1481 target = insert_map[input_string.length() - 1 - i];
1482 edit_string[target] = input_string[i];
1484 /* highlight from end to wherever the last character was added */
1485 return edit_string.length() - insert_map[input_string.length()-1];
1490 AudioClock::on_key_release_event (GdkEventKey *ev)
1496 /* return true for keys that we used on press
1497 so that they cannot possibly do double-duty
1499 switch (ev->keyval) {
1522 case GDK_KP_Decimal:
1530 case GDK_KP_Subtract:
1538 AudioClock::index_to_field (int index) const
1543 return Timecode_Hours;
1544 } else if (index < 7) {
1545 return Timecode_Minutes;
1546 } else if (index < 10) {
1547 return Timecode_Seconds;
1549 return Timecode_frames;
1555 } else if (index < 7) {
1563 return Timecode_Hours;
1564 } else if (index < 6) {
1566 } else if (index < 9) {
1569 return MS_Milliseconds;
1576 return SS_Deciseconds;
1588 AudioClock::on_button_press_event (GdkEventButton *ev)
1590 if (!_session || _session->actively_recording()) {
1591 /* swallow event, do nothing */
1595 switch (ev->button) {
1597 if (editable && !_off) {
1603 /* the text has been centered vertically, so adjust
1606 int xcenter = (get_width() - layout_width) /2;
1608 y = ev->y - ((get_height() - layout_height)/2);
1609 x = ev->x - xcenter;
1611 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1612 /* pretend it is a character on the far right */
1615 drag_field = index_to_field (index);
1617 /* make absolutely sure that the pointer is grabbed */
1618 gdk_pointer_grab(ev->window,false ,
1619 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1620 NULL,NULL,ev->time);
1622 drag_start_y = ev->y;
1636 AudioClock::on_button_release_event (GdkEventButton *ev)
1638 if (!_session || _session->actively_recording()) {
1639 /* swallow event, do nothing */
1643 if (editable && !_off) {
1645 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1647 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1648 // we actually dragged so return without
1649 // setting editing focus, or we shift clicked
1652 if (ev->button == 1) {
1654 if (_edit_by_click_field) {
1656 int xcenter = (get_width() - layout_width) /2;
1659 int y = ev->y - ((get_height() - layout_height)/2);
1660 int x = ev->x - xcenter;
1663 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1667 f = index_to_field (index);
1670 case Timecode_frames:
1671 case MS_Milliseconds:
1673 case SS_Deciseconds:
1688 if (Keyboard::is_context_menu_event (ev)) {
1689 if (ops_menu == 0) {
1692 ops_menu->popup (1, ev->time);
1700 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1702 bool ret = CairoWidget::on_focus_out_event (ev);
1705 end_edit (_accept_on_focus_out);
1712 AudioClock::on_scroll_event (GdkEventScroll *ev)
1717 if (editing || _session == 0 || !editable || _off || _session->actively_recording()) {
1724 /* the text has been centered vertically, so adjust
1728 int xcenter = (get_width() - layout_width) /2;
1729 y = ev->y - ((get_height() - layout_height)/2);
1730 x = ev->x - xcenter;
1732 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1733 /* not in the main layout */
1737 Field f = index_to_field (index);
1738 samplepos_t samples = 0;
1740 switch (ev->direction) {
1743 samples = get_sample_step (f, current_time(), 1);
1745 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1748 AudioClock::set (current_time() + samples, true);
1749 ValueChanged (); /* EMIT_SIGNAL */
1753 case GDK_SCROLL_DOWN:
1754 samples = get_sample_step (f, current_time(), -1);
1756 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1760 if (!_negative_allowed && (double)current_time() - (double)samples < 0.0) {
1761 AudioClock::set (0, true);
1763 AudioClock::set (current_time() - samples, true);
1766 ValueChanged (); /* EMIT_SIGNAL */
1779 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1781 if (editing || _session == 0 || !dragging || _session->actively_recording()) {
1785 float pixel_sample_scale_factor = 0.2f;
1787 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1788 pixel_sample_scale_factor = 0.1f;
1792 if (Keyboard::modifier_state_contains (ev->state,
1793 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1794 pixel_sample_scale_factor = 0.025f;
1797 double y_delta = ev->y - drag_y;
1799 drag_accum += y_delta*pixel_sample_scale_factor;
1803 if (floor (drag_accum) != 0) {
1805 samplepos_t samples;
1808 dir = (drag_accum < 0 ? 1:-1);
1809 pos = current_time();
1810 samples = get_sample_step (drag_field, pos, dir);
1812 if (samples != 0 && samples * drag_accum < current_time()) {
1813 AudioClock::set ((samplepos_t) floor (pos - drag_accum * samples), false); // minus because up is negative in GTK
1815 AudioClock::set (0 , false);
1819 ValueChanged(); /* EMIT_SIGNAL */
1826 AudioClock::get_sample_step (Field field, samplepos_t pos, int dir)
1829 Timecode::BBT_Time BBT;
1831 case Timecode_Hours:
1832 f = (samplecnt_t) floor (3600.0 * _session->sample_rate());
1834 case Timecode_Minutes:
1835 f = (samplecnt_t) floor (60.0 * _session->sample_rate());
1837 case Timecode_Seconds:
1838 f = _session->sample_rate();
1840 case Timecode_frames:
1841 f = (samplecnt_t) floor (_session->sample_rate() / _session->timecode_frames_per_second());
1849 f = (samplecnt_t) _session->sample_rate();
1851 case SS_Deciseconds:
1852 f = (samplecnt_t) _session->sample_rate() / 10.f;
1856 f = (samplecnt_t) floor (3600.0 * _session->sample_rate());
1859 f = (samplecnt_t) floor (60.0 * _session->sample_rate());
1862 f = (samplecnt_t) _session->sample_rate();
1864 case MS_Milliseconds:
1865 f = (samplecnt_t) floor (_session->sample_rate() / 1000.0);
1872 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1878 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1884 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1887 error << string_compose (_("programming error: %1"), "attempt to get samples from non-text field!") << endmsg;
1896 AudioClock::current_time (samplepos_t) const
1902 AudioClock::current_duration (samplepos_t pos) const
1904 samplepos_t ret = 0;
1908 ret = sample_duration_from_bbt_string (pos, _layout->get_text());
1923 AudioClock::bbt_validate_edit (string & str)
1927 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1931 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1935 if (!is_duration && any.bbt.bars == 0) {
1939 if (!is_duration && any.bbt.beats == 0) {
1940 /* user could not have mean zero beats because for a
1941 * non-duration clock that's impossible. Assume that they
1942 * mis-entered things and meant Bar|1|ticks
1946 snprintf (buf, sizeof (buf), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, any.bbt.bars, 1, any.bbt.ticks);
1954 AudioClock::timecode_validate_edit (const string&)
1960 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1961 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1966 TC.hours = hours * -1;
1970 TC.negative = false;
1973 if (TC.negative && !_negative_allowed) {
1977 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1981 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1985 if (_session->timecode_drop_frames()) {
1986 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1995 AudioClock::minsec_validate_edit (const string& str)
1997 int hrs, mins, secs, millisecs;
1999 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2003 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
2011 AudioClock::samples_from_timecode_string (const string& str) const
2013 if (_session == 0) {
2022 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
2023 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
2026 TC.hours = abs(hours);
2027 TC.rate = _session->timecode_frames_per_second();
2028 TC.drop= _session->timecode_drop_frames();
2030 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
2032 // timecode_tester ();
2033 if (edit_is_negative) {
2041 AudioClock::samples_from_minsec_string (const string& str) const
2043 if (_session == 0) {
2047 int hrs, mins, secs, millisecs;
2048 samplecnt_t sr = _session->sample_rate();
2050 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
2051 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
2055 return (samplepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
2059 AudioClock::samples_from_bbt_string (samplepos_t pos, const string& str) const
2061 if (_session == 0) {
2062 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
2067 any.type = AnyTime::BBT;
2069 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
2076 return _session->any_duration_to_samples (pos, any);
2078 return _session->convert_to_samples (any);
2084 AudioClock::sample_duration_from_bbt_string (samplepos_t pos, const string& str) const
2086 if (_session == 0) {
2087 error << "AudioClock::sample_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2091 Timecode::BBT_Time bbt;
2093 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2097 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2101 AudioClock::samples_from_seconds_string (const string& str) const
2104 sscanf (str.c_str(), "%f", &f);
2105 return f * _session->sample_rate();
2109 AudioClock::samples_from_audiosamples_string (const string& str) const
2112 sscanf (str.c_str(), "%" PRId64, &f);
2117 AudioClock::copy_text_to_clipboard () const
2121 val = pre_edit_string;
2123 val = _layout->get_text ();
2125 const size_t trim = val.find_first_not_of(" ");
2126 if (trim == string::npos) {
2127 assert(0); // empty clock, can't be right.
2130 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2131 cl->set_text (val.substr(trim));
2135 AudioClock::build_ops_menu ()
2137 using namespace Menu_Helpers;
2138 ops_menu = new Menu;
2139 MenuList& ops_items = ops_menu->items();
2140 ops_menu->set_name ("ArdourContextMenu");
2142 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2143 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2144 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2145 ops_items.push_back (MenuElem (_("Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Seconds, false)));
2146 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Samples, false)));
2148 if (editable && !_off && !is_duration && !_follows_playhead) {
2149 ops_items.push_back (SeparatorElem());
2150 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2151 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2153 ops_items.push_back (SeparatorElem());
2154 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2158 AudioClock::set_from_playhead ()
2164 AudioClock::set (_session->transport_sample());
2169 AudioClock::locate ()
2171 if (!_session || is_duration) {
2175 _session->request_locate (current_time(), _session->transport_rolling ());
2179 AudioClock::set_mode (Mode m, bool noemit)
2189 _layout->set_text ("");
2191 Gtk::Requisition req;
2192 set_clock_dimensions (req);
2196 insert_map.push_back (11);
2197 insert_map.push_back (10);
2198 insert_map.push_back (8);
2199 insert_map.push_back (7);
2200 insert_map.push_back (5);
2201 insert_map.push_back (4);
2202 insert_map.push_back (2);
2203 insert_map.push_back (1);
2207 insert_map.push_back (11);
2208 insert_map.push_back (10);
2209 insert_map.push_back (9);
2210 insert_map.push_back (8);
2211 insert_map.push_back (6);
2212 insert_map.push_back (5);
2213 insert_map.push_back (3);
2214 insert_map.push_back (2);
2215 insert_map.push_back (1);
2219 insert_map.push_back (12);
2220 insert_map.push_back (11);
2221 insert_map.push_back (10);
2222 insert_map.push_back (8);
2223 insert_map.push_back (7);
2224 insert_map.push_back (5);
2225 insert_map.push_back (4);
2226 insert_map.push_back (2);
2227 insert_map.push_back (1);
2231 insert_map.push_back (11);
2232 insert_map.push_back (9);
2233 insert_map.push_back (8);
2234 insert_map.push_back (7);
2235 insert_map.push_back (6);
2236 insert_map.push_back (5);
2237 insert_map.push_back (4);
2238 insert_map.push_back (3);
2239 insert_map.push_back (2);
2240 insert_map.push_back (1);
2247 AudioClock::set (last_when, true);
2249 if (!is_transient && !noemit) {
2250 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2253 mode_changed (); /* EMIT SIGNAL (the member one) */
2257 AudioClock::set_bbt_reference (samplepos_t pos)
2259 bbt_reference_time = pos;
2263 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2265 CairoWidget::on_style_changed (old_style);
2267 Gtk::Requisition req;
2268 set_clock_dimensions (req);
2270 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2276 AudioClock::set_editable (bool yn)
2282 AudioClock::set_is_duration (bool yn)
2284 if (yn == is_duration) {
2289 AudioClock::set (last_when, true);
2293 AudioClock::set_off (bool yn)
2301 /* force a redraw. last_when will be preserved, but the clock text will
2305 AudioClock::set (last_when, true);
2309 AudioClock::focus ()
2311 start_edit (Field (0));
2315 AudioClock::set_corner_radius (double r)
2324 AudioClock::dpi_reset ()
2326 /* force recomputation of size even if we are fixed width
2334 AudioClock::set_negative_allowed (bool yn)
2336 _negative_allowed = yn;