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/slave.h"
38 #include "ardour/tempo.h"
39 #include "ardour/types.h"
41 #include "ardour_ui.h"
42 #include "audio_clock.h"
43 #include "enums_convert.h"
44 #include "gui_thread.h"
46 #include "ui_config.h"
51 using namespace ARDOUR;
52 using namespace ARDOUR_UI_UTILS;
53 using namespace ArdourWidgets;
58 using Gtkmm2ext::Keyboard;
60 sigc::signal<void> AudioClock::ModeChanged;
61 vector<AudioClock*> AudioClock::clocks;
63 #define BBT_BAR_CHAR "|"
64 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
66 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
67 bool allow_edit, bool follows_playhead, bool duration, bool with_info,
68 bool accept_on_focus_out)
71 , is_transient (transient)
72 , is_duration (duration)
73 , editable (allow_edit)
74 , _follows_playhead (follows_playhead)
75 , _accept_on_focus_out (accept_on_focus_out)
78 , _edit_by_click_field (false)
79 , _negative_allowed (false)
80 , edit_is_negative (false)
81 , _limit_pos (INT64_MAX - 1)
82 , _with_info (with_info)
87 , style_resets_first (true)
93 , bbt_reference_time (-1)
98 , drag_field (Field (0))
102 set_flags (CAN_FOCUS);
104 _layout = Pango::Layout::create (get_pango_context());
105 _layout->set_attributes (normal_attributes);
107 set_widget_name (widget_name);
109 _mode = BBT; /* lie to force mode switch */
111 set (last_when, true);
114 clocks.push_back (this);
117 _left_btn.set_sizing_text (_("0000000000000"));
118 // NB right_btn is in a size-group
120 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
121 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
123 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
124 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
127 AudioClock::~AudioClock ()
129 delete foreground_attr;
134 AudioClock::set_widget_name (const string& str)
139 set_name (str + " clock");
149 AudioClock::on_realize ()
151 Gtk::Requisition req;
153 CairoWidget::on_realize ();
155 set_clock_dimensions (req);
157 first_width = req.width;
158 first_height = req.height;
160 // XXX FIX ME: define font based on ... ???
166 AudioClock::set_font (Pango::FontDescription font)
168 Glib::RefPtr<Gtk::Style> style = get_style ();
169 Pango::AttrFontDesc* font_attr;
171 font_size = font.get_size();
172 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
174 normal_attributes.change (*font_attr);
175 editing_attributes.change (*font_attr);
178 /* get the figure width for the font. This doesn't have to super
179 * accurate since we only use it to measure the (roughly 1 character)
180 * offset from the position Pango tells us for the "cursor"
183 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
187 tmp->get_pixel_size (em_width, ignore_height);
189 /* force redraw of markup with new font-size */
190 set (last_when, true);
194 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
196 CairoWidget::set_active_state (s);
201 AudioClock::set_colors ()
207 uint32_t editing_color;
208 uint32_t cursor_color;
210 if (active_state()) {
211 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
212 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
213 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
214 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
216 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
217 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
218 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
219 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
222 /* store for bg and cursor in render() */
224 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
231 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
238 /* rescale for Pango colors ... sigh */
240 r = lrint (r * 65535.0);
241 g = lrint (g * 65535.0);
242 b = lrint (b * 65535.0);
244 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
245 r = lrint ((r/255.0) * 65535.0);
246 g = lrint ((g/255.0) * 65535.0);
247 b = lrint ((b/255.0) * 65535.0);
248 delete foreground_attr;
249 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
251 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
252 r = lrint ((r/255.0) * 65535.0);
253 g = lrint ((g/255.0) * 65535.0);
254 b = lrint ((b/255.0) * 65535.0);
256 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
258 normal_attributes.change (*foreground_attr);
259 editing_attributes.change (*foreground_attr);
260 editing_attributes.change (*editing_attr);
263 _layout->set_attributes (normal_attributes);
265 _layout->set_attributes (editing_attributes);
272 AudioClock::set_scale (double x, double y)
281 AudioClock::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
283 cairo_t* cr = ctx->cobj();
284 /* main layout: rounded rect, plus the text */
287 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
289 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
291 cairo_rectangle (cr, 0, 0, get_width(), get_height());
296 double lw = layout_width * xscale;
297 double lh = layout_height * yscale;
299 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
301 if (xscale != 1.0 || yscale != 1.0) {
303 cairo_scale (cr, xscale, yscale);
306 pango_cairo_show_layout (cr, _layout->gobj());
308 if (xscale != 1.0 || yscale != 1.0) {
313 if (!insert_map.empty()) {
315 int xcenter = (get_width() - layout_width) /2;
317 if (input_string.length() < insert_map.size()) {
318 Pango::Rectangle cursor;
320 if (input_string.empty()) {
321 /* nothing entered yet, put cursor at the end
324 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
326 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
329 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
331 min (get_width() - 2.0,
332 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
333 (get_height() - layout_height)/2.0,
334 2.0, cursor.get_height()/PANGO_SCALE);
337 /* we've entered all possible digits, no cursor */
341 if (input_string.empty()) {
342 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
345 (get_height() - layout_height)/2.0,
354 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
356 Glib::RefPtr<Pango::Layout> tmp;
357 Glib::RefPtr<Gtk::Style> style = get_style ();
358 Pango::FontDescription font;
360 tmp = Pango::Layout::create (get_pango_context());
362 if (!is_realized()) {
363 font = get_font_for_style (get_name());
365 font = style->get_font();
368 tmp->set_font_description (font);
370 /* this string is the longest thing we will ever display */
372 tmp->set_text (" 88:88:88,888 ");
374 tmp->set_text (" 88:88:88,88 ");
375 tmp->get_pixel_size (req.width, req.height);
377 layout_height = req.height;
378 layout_width = req.width;
382 AudioClock::on_size_request (Gtk::Requisition* req)
384 /* even for non fixed width clocks, the size we *ask* for never changes,
385 even though the size we receive might. so once we've computed it,
390 req->width = first_width;
391 req->height = first_height;
395 set_clock_dimensions (*req);
397 /* now tackle height, for which we need to know the height of the lower
403 AudioClock::show_edit_status (int length)
405 editing_attr->set_start_index (edit_string.length() - length);
406 editing_attr->set_end_index (edit_string.length());
408 editing_attributes.change (*foreground_attr);
409 editing_attributes.change (*editing_attr);
411 _layout->set_attributes (editing_attributes);
415 AudioClock::start_edit (Field f)
418 pre_edit_string = _layout->get_text ();
419 if (!insert_map.empty()) {
420 edit_string = pre_edit_string;
422 edit_string.clear ();
423 _layout->set_text ("");
426 input_string.clear ();
428 edit_is_negative = false;
431 input_string = get_field (f);
432 show_edit_status (merge_input_and_edit_string ());
433 _layout->set_text (edit_string);
438 Keyboard::magic_widget_grab_focus ();
444 AudioClock::get_field (Field f)
448 return edit_string.substr (1, 2);
450 case Timecode_Minutes:
451 return edit_string.substr (4, 2);
453 case Timecode_Seconds:
454 return edit_string.substr (7, 2);
456 case Timecode_Frames:
457 return edit_string.substr (10, 2);
460 return edit_string.substr (1, 2);
463 return edit_string.substr (4, 2);
466 return edit_string.substr (7, 2);
468 case MS_Milliseconds:
469 return edit_string.substr (10, 3);
472 return edit_string.substr (1, 3);
475 return edit_string.substr (5, 2);
478 return edit_string.substr (8, 4);
488 AudioClock::end_edit (bool modify)
496 ok = timecode_validate_edit (edit_string);
500 ok = bbt_validate_edit (edit_string);
504 ok = minsec_validate_edit (edit_string);
508 if (edit_string.length() < 1) {
509 edit_string = pre_edit_string;
515 edit_string = pre_edit_string;
516 input_string.clear ();
517 _layout->set_text (edit_string);
518 show_edit_status (0);
519 /* edit attributes remain in use */
523 framepos_t pos = 0; /* stupid gcc */
527 pos = frames_from_timecode_string (edit_string);
532 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
534 pos = frames_from_bbt_string (0, edit_string);
539 pos = frames_from_minsec_string (edit_string);
543 pos = frames_from_audioframes_string (edit_string);
548 _layout->set_attributes (normal_attributes);
549 ValueChanged(); /* EMIT_SIGNAL */
555 edit_is_negative = false;
556 _layout->set_attributes (normal_attributes);
557 _layout->set_text (pre_edit_string);
568 AudioClock::drop_focus ()
570 Keyboard::magic_widget_drop_focus ();
573 /* move focus back to the default widget in the top level window */
574 ARDOUR_UI::instance()->reset_focus (this);
579 AudioClock::parse_as_frames_distance (const std::string& str)
583 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
591 AudioClock::parse_as_minsec_distance (const std::string& str)
593 framecnt_t sr = _session->frame_rate();
599 switch (str.length()) {
606 sscanf (str.c_str(), "%" PRId32, &msecs);
607 return msecs * (sr / 1000);
610 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
611 return (secs * sr) + (msecs * (sr/1000));
614 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
615 return (secs * sr) + (msecs * (sr/1000));
618 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
619 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
622 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
623 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
626 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
627 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
630 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
631 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
641 AudioClock::parse_as_timecode_distance (const std::string& str)
643 double fps = _session->timecode_frames_per_second();
644 framecnt_t sr = _session->frame_rate();
650 switch (str.length()) {
655 sscanf (str.c_str(), "%" PRId32, &frames);
656 return llrint ((frames/(float)fps) * sr);
659 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
660 return (secs * sr) + llrint ((frames/(float)fps) * sr);
663 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
664 return (secs * sr) + llrint ((frames/(float)fps) * sr);
667 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
668 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
671 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
672 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
675 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
676 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
679 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
680 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
690 AudioClock::parse_as_bbt_distance (const std::string&)
696 AudioClock::parse_as_distance (const std::string& instr)
700 return parse_as_timecode_distance (instr);
703 return parse_as_frames_distance (instr);
706 return parse_as_bbt_distance (instr);
709 return parse_as_minsec_distance (instr);
716 AudioClock::end_edit_relative (bool add)
722 ok = timecode_validate_edit (edit_string);
726 ok = bbt_validate_edit (edit_string);
730 ok = minsec_validate_edit (edit_string);
738 edit_string = pre_edit_string;
739 input_string.clear ();
740 _layout->set_text (edit_string);
741 show_edit_status (0);
742 /* edit attributes remain in use */
747 framecnt_t frames = parse_as_distance (input_string);
752 _layout->set_attributes (normal_attributes);
756 set (current_time() + frames, true);
758 framepos_t c = current_time();
760 if (c > frames || _negative_allowed) {
761 set (c - frames, true);
766 ValueChanged (); /* EMIT SIGNAL */
769 input_string.clear ();
775 AudioClock::session_property_changed (const PropertyChange&)
777 set (last_when, true);
781 AudioClock::session_configuration_changed (std::string p)
783 if (_negative_allowed) {
784 /* session option editor clock */
788 if (p == "sync-source" || p == "external-sync") {
789 set (current_time(), true);
793 if (p != "timecode-offset" && p != "timecode-offset-negative") {
802 current = current_duration ();
804 current = current_time ();
814 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
816 if ((!force && !is_visible()) || _session == 0) {
821 when = when - offset;
824 if (when > _limit_pos) {
826 } else if (when < -_limit_pos) {
830 if (when == last_when && !force) {
831 #if 0 // XXX return if no change and no change forced. verify Aug/2014
832 if (_mode != Timecode && _mode != MinSec) {
833 /* may need to force display of TC source
834 * time, so don't return early.
836 /* ^^ Why was that?, delta times?
837 * Timecode FPS, pull-up/down, etc changes
838 * trigger a 'session_property_changed' which
839 * eventually calls set(last_when, true)
841 * re-rendering the clock every 40ms or so just
842 * because we can is not ideal.
857 set_timecode (when, force);
861 set_bbt (when, offset, force);
866 set_minsec (when, force);
870 set_frames (when, force);
876 _left_btn.set_sensitive (btn_en);
877 _right_btn.set_sensitive (btn_en);
878 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
879 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
881 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
882 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
883 _left_btn.set_alignment (.5, .5);
884 _right_btn.set_alignment (.5, .5);
885 set_tooltip (_left_btn, _("Change current tempo"));
886 set_tooltip (_right_btn, _("Change current time signature"));
888 _left_btn.set_elements (ArdourButton::Text);
889 _right_btn.set_elements (ArdourButton::Text);
890 _left_btn.set_alignment (0, .5);
891 _right_btn.set_alignment (1, .5);
892 set_tooltip (_left_btn, _(""));
893 set_tooltip (_right_btn, _(""));
902 AudioClock::set_slave_info ()
908 SyncSource sync_src = Config->get_sync_source();
910 if (_session->config.get_external_sync()) {
911 Slave* slave = _session->slave();
915 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
916 _right_btn.set_text ("", true);
920 _left_btn.set_text (sync_source_to_string (sync_src, true), true);
921 _right_btn.set_text (slave->approximate_current_delta (), true);
923 _left_btn.set_text (_("--pending--"), true);
924 _right_btn.set_text ("", true);
931 TimecodeSlave* tcslave;
932 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
933 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
934 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
935 sync_source_to_string(sync_src, true)[0],
936 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
937 matching ? "#66ff66" : "#ff3333"
939 _right_btn.set_text (slave->approximate_current_delta (), true);
942 _left_btn.set_text (_("--pending--"), true);
943 _right_btn.set_text ("", true);
948 _left_btn.set_text (string_compose ("%1/%2",
949 _("INT"), sync_source_to_string(sync_src, true)), true);
950 _right_btn.set_text ("", true);
955 AudioClock::set_out_of_bounds (bool negative)
959 _layout->set_text (" >>> -- <<< ");
961 _layout->set_text (" >>> ++ <<< ");
965 _layout->set_text (" <<<<<<<<<< ");
967 _layout->set_text (" >>>>>>>>>> ");
973 AudioClock::set_frames (framepos_t when, bool /*force*/)
976 bool negative = false;
979 _layout->set_text (" ----------");
980 _left_btn.set_text ("", true);
981 _right_btn.set_text ("", true);
990 if (when >= _limit_pos) {
991 set_out_of_bounds (negative);
992 } else if (negative) {
993 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
994 _layout->set_text (buf);
996 snprintf (buf, sizeof (buf), " %10" PRId64, when);
997 _layout->set_text (buf);
1001 framecnt_t rate = _session->frame_rate();
1003 if (fmod (rate, 100.0) == 0.0) {
1004 sprintf (buf, "%.1fkHz", rate/1000.0);
1006 sprintf (buf, "%" PRId64 "Hz", rate);
1009 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf), true);
1011 float vid_pullup = _session->config.get_video_pullup();
1013 if (vid_pullup == 0.0) {
1014 _right_btn.set_text ("", true);
1016 sprintf (buf, _("%+.4f%%"), vid_pullup);
1017 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf), true);
1023 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1040 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1041 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1042 mins = (int) floor (left / (frame_rate * 60.0f));
1043 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1044 secs = (int) floor (left / (float) frame_rate);
1045 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1046 millisecs = floor (left * 1000.0 / (float) frame_rate);
1049 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1051 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1057 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1062 _layout->set_text (" --:--:--.---");
1063 _left_btn.set_text ("", true);
1064 _right_btn.set_text ("", true);
1069 if (when >= _limit_pos || when <= -_limit_pos) {
1070 set_out_of_bounds (when < 0);
1072 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1073 _layout->set_text (buf);
1080 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1083 bool negative = false;
1086 _layout->set_text (" --:--:--:--");
1087 _left_btn.set_text ("", true);
1088 _right_btn.set_text ("", true);
1096 if (when >= _limit_pos) {
1097 set_out_of_bounds (negative);
1103 _session->timecode_duration (when, TC);
1105 _session->timecode_time (when, TC);
1108 TC.negative = TC.negative || negative;
1110 _layout->set_text (Timecode::timecode_format_time(TC));
1116 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1119 Timecode::BBT_Time BBT;
1120 bool negative = false;
1122 if (_off || when >= _limit_pos || when < -_limit_pos) {
1123 _layout->set_text (" ---|--|----");
1124 _left_btn.set_text ("", true);
1125 _right_btn.set_text ("", true);
1134 /* handle a common case */
1141 TempoMap& tmap (_session->tempo_map());
1144 offset = bbt_reference_time;
1147 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1148 Timecode::BBT_Time sub_bbt;
1151 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1152 sub_bbt = tmap.bbt_at_frame (offset - when);
1154 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1155 sub_bbt = tmap.bbt_at_frame (offset);
1158 BBT.bars -= sub_bbt.bars;
1160 if (BBT.ticks < sub_bbt.ticks) {
1161 if (BBT.beats == 1) {
1163 BBT.beats = divisions;
1167 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1169 BBT.ticks -= sub_bbt.ticks;
1172 if (BBT.beats < sub_bbt.beats) {
1174 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1176 BBT.beats -= sub_bbt.beats;
1180 BBT = _session->tempo_map().bbt_at_frame (when);
1184 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1185 BBT.bars, BBT.beats, BBT.ticks);
1187 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1188 BBT.bars, BBT.beats, BBT.ticks);
1191 _layout->set_text (buf);
1196 if (bbt_reference_time < 0) {
1199 pos = bbt_reference_time;
1202 TempoMetric m (_session->tempo_map().metric_at (pos));
1204 if (m.tempo().note_type() == 4) {
1205 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1206 _left_btn.set_text (string_compose ("%1", buf), true);
1207 } else if (m.tempo().note_type() == 8) {
1208 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1209 _left_btn.set_text (string_compose ("%1", buf), true);
1211 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1212 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf), true);
1215 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1216 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf), true);
1221 AudioClock::set_session (Session *s)
1223 SessionHandlePtr::set_session (s);
1227 int64_t limit_sec = UIConfiguration::instance().get_clock_display_limit ();
1228 if (limit_sec > 0) {
1229 _limit_pos = (framecnt_t) floor ((double)(limit_sec * _session->frame_rate()));
1232 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1233 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1234 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1235 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1237 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1240 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1242 if ((*i)->get_property (X_("name"), name) && name == _name) {
1244 AudioClock::Mode amode;
1245 if ((*i)->get_property (X_("mode"), amode)) {
1246 set_mode (amode, true);
1249 if ((*i)->get_property (X_("on"), on)) {
1257 set (last_when, true);
1262 AudioClock::on_key_press_event (GdkEventKey* ev)
1270 int highlight_length;
1273 switch (ev->keyval) {
1316 case GDK_KP_Subtract:
1317 if (_negative_allowed && input_string.empty()) {
1318 edit_is_negative = true;
1319 edit_string.replace(0,1,"-");
1320 _layout->set_text (edit_string);
1323 end_edit_relative (false);
1329 end_edit_relative (true);
1342 ChangeAborted(); /* EMIT SIGNAL */
1347 if (!input_string.empty()) {
1348 /* delete the last key entered
1350 input_string = input_string.substr (0, input_string.length() - 1);
1352 goto use_input_string;
1355 /* do not allow other keys to passthru to the rest of the GUI
1361 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1362 /* too many digits: eat the key event, but do nothing with it */
1366 input_string.push_back (new_char);
1372 /* get this one in the right order, and to the right width */
1373 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1374 edit_string = edit_string.substr (0, edit_string.length() - 1);
1376 edit_string.push_back (new_char);
1378 if (!edit_string.empty()) {
1380 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1381 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1384 /* highlight the whole thing */
1385 highlight_length = edit_string.length();
1389 highlight_length = merge_input_and_edit_string ();
1392 if (edit_is_negative) {
1393 edit_string.replace(0,1,"-");
1395 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1396 edit_string.replace(0,1,"_");
1398 edit_string.replace(0,1," ");
1402 show_edit_status (highlight_length);
1403 _layout->set_text (edit_string);
1410 AudioClock::merge_input_and_edit_string ()
1412 /* merge with pre-edit-string into edit string */
1414 edit_string = pre_edit_string;
1416 if (input_string.empty()) {
1420 string::size_type target;
1421 for (string::size_type i = 0; i < input_string.length(); ++i) {
1422 target = insert_map[input_string.length() - 1 - i];
1423 edit_string[target] = input_string[i];
1425 /* highlight from end to wherever the last character was added */
1426 return edit_string.length() - insert_map[input_string.length()-1];
1431 AudioClock::on_key_release_event (GdkEventKey *ev)
1437 /* return true for keys that we used on press
1438 so that they cannot possibly do double-duty
1440 switch (ev->keyval) {
1463 case GDK_KP_Decimal:
1471 case GDK_KP_Subtract:
1479 AudioClock::index_to_field (int index) const
1484 return Timecode_Hours;
1485 } else if (index < 7) {
1486 return Timecode_Minutes;
1487 } else if (index < 10) {
1488 return Timecode_Seconds;
1490 return Timecode_Frames;
1496 } else if (index < 7) {
1504 return Timecode_Hours;
1505 } else if (index < 6) {
1507 } else if (index < 9) {
1510 return MS_Milliseconds;
1522 AudioClock::on_button_press_event (GdkEventButton *ev)
1524 if (!_session || _session->actively_recording()) {
1525 /* swallow event, do nothing */
1529 switch (ev->button) {
1531 if (editable && !_off) {
1537 /* the text has been centered vertically, so adjust
1540 int xcenter = (get_width() - layout_width) /2;
1542 y = ev->y - ((get_height() - layout_height)/2);
1543 x = ev->x - xcenter;
1545 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1546 /* pretend it is a character on the far right */
1549 drag_field = index_to_field (index);
1551 /* make absolutely sure that the pointer is grabbed */
1552 gdk_pointer_grab(ev->window,false ,
1553 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1554 NULL,NULL,ev->time);
1556 drag_start_y = ev->y;
1570 AudioClock::on_button_release_event (GdkEventButton *ev)
1572 if (!_session || _session->actively_recording()) {
1573 /* swallow event, do nothing */
1577 if (editable && !_off) {
1579 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1581 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1582 // we actually dragged so return without
1583 // setting editing focus, or we shift clicked
1586 if (ev->button == 1) {
1588 if (_edit_by_click_field) {
1590 int xcenter = (get_width() - layout_width) /2;
1593 int y = ev->y - ((get_height() - layout_height)/2);
1594 int x = ev->x - xcenter;
1597 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1601 f = index_to_field (index);
1604 case Timecode_Frames:
1605 case MS_Milliseconds:
1621 if (Keyboard::is_context_menu_event (ev)) {
1622 if (ops_menu == 0) {
1625 ops_menu->popup (1, ev->time);
1633 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1635 bool ret = CairoWidget::on_focus_out_event (ev);
1638 end_edit (_accept_on_focus_out);
1645 AudioClock::on_scroll_event (GdkEventScroll *ev)
1650 if (editing || _session == 0 || !editable || _off || _session->actively_recording()) {
1657 /* the text has been centered vertically, so adjust
1661 int xcenter = (get_width() - layout_width) /2;
1662 y = ev->y - ((get_height() - layout_height)/2);
1663 x = ev->x - xcenter;
1665 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1666 /* not in the main layout */
1670 Field f = index_to_field (index);
1671 framepos_t frames = 0;
1673 switch (ev->direction) {
1676 frames = get_frame_step (f, current_time(), 1);
1678 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1681 set (current_time() + frames, true);
1682 ValueChanged (); /* EMIT_SIGNAL */
1686 case GDK_SCROLL_DOWN:
1687 frames = get_frame_step (f, current_time(), -1);
1689 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1693 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1696 set (current_time() - frames, true);
1699 ValueChanged (); /* EMIT_SIGNAL */
1712 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1714 if (editing || _session == 0 || !dragging || _session->actively_recording()) {
1718 float pixel_frame_scale_factor = 0.2f;
1720 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1721 pixel_frame_scale_factor = 0.1f;
1725 if (Keyboard::modifier_state_contains (ev->state,
1726 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1727 pixel_frame_scale_factor = 0.025f;
1730 double y_delta = ev->y - drag_y;
1732 drag_accum += y_delta*pixel_frame_scale_factor;
1736 if (floor (drag_accum) != 0) {
1741 dir = (drag_accum < 0 ? 1:-1);
1742 pos = current_time();
1743 frames = get_frame_step (drag_field, pos, dir);
1745 if (frames != 0 && frames * drag_accum < current_time()) {
1746 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1752 ValueChanged(); /* EMIT_SIGNAL */
1759 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1762 Timecode::BBT_Time BBT;
1764 case Timecode_Hours:
1765 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1767 case Timecode_Minutes:
1768 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1770 case Timecode_Seconds:
1771 f = _session->frame_rate();
1773 case Timecode_Frames:
1774 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1782 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1785 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1788 f = (framecnt_t) _session->frame_rate();
1790 case MS_Milliseconds:
1791 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1798 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1804 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1810 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1813 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1822 AudioClock::current_time (framepos_t) const
1828 AudioClock::current_duration (framepos_t pos) const
1837 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1853 AudioClock::bbt_validate_edit (const string& str)
1857 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1861 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1865 if (!is_duration && any.bbt.bars == 0) {
1869 if (!is_duration && any.bbt.beats == 0) {
1877 AudioClock::timecode_validate_edit (const string&)
1883 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1884 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1889 TC.hours = hours * -1;
1893 TC.negative = false;
1896 if (TC.negative && !_negative_allowed) {
1900 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1904 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1908 if (_session->timecode_drop_frames()) {
1909 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1918 AudioClock::minsec_validate_edit (const string& str)
1920 int hrs, mins, secs, millisecs;
1922 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1926 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1934 AudioClock::frames_from_timecode_string (const string& str) const
1936 if (_session == 0) {
1945 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1946 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1949 TC.hours = abs(hours);
1950 TC.rate = _session->timecode_frames_per_second();
1951 TC.drop= _session->timecode_drop_frames();
1953 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1955 // timecode_tester ();
1956 if (edit_is_negative) {
1964 AudioClock::frames_from_minsec_string (const string& str) const
1966 if (_session == 0) {
1970 int hrs, mins, secs, millisecs;
1971 framecnt_t sr = _session->frame_rate();
1973 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1974 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1978 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1982 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1984 if (_session == 0) {
1985 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1990 any.type = AnyTime::BBT;
1992 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1999 return _session->any_duration_to_frames (pos, any);
2001 return _session->convert_to_frames (any);
2007 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
2009 if (_session == 0) {
2010 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
2014 Timecode::BBT_Time bbt;
2016 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
2020 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
2024 AudioClock::frames_from_audioframes_string (const string& str) const
2027 sscanf (str.c_str(), "%" PRId64, &f);
2032 AudioClock::copy_text_to_clipboard () const
2036 val = pre_edit_string;
2038 val = _layout->get_text ();
2040 const size_t trim = val.find_first_not_of(" ");
2041 if (trim == string::npos) {
2042 assert(0); // empty clock, can't be right.
2045 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
2046 cl->set_text (val.substr(trim));
2050 AudioClock::build_ops_menu ()
2052 using namespace Menu_Helpers;
2053 ops_menu = new Menu;
2054 MenuList& ops_items = ops_menu->items();
2055 ops_menu->set_name ("ArdourContextMenu");
2057 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2058 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2059 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2060 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2062 if (editable && !_off && !is_duration && !_follows_playhead) {
2063 ops_items.push_back (SeparatorElem());
2064 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2065 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2067 ops_items.push_back (SeparatorElem());
2068 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2072 AudioClock::set_from_playhead ()
2078 set (_session->transport_frame());
2083 AudioClock::locate ()
2085 if (!_session || is_duration) {
2089 _session->request_locate (current_time(), _session->transport_rolling ());
2093 AudioClock::set_mode (Mode m, bool noemit)
2103 _layout->set_text ("");
2105 Gtk::Requisition req;
2106 set_clock_dimensions (req);
2110 insert_map.push_back (11);
2111 insert_map.push_back (10);
2112 insert_map.push_back (8);
2113 insert_map.push_back (7);
2114 insert_map.push_back (5);
2115 insert_map.push_back (4);
2116 insert_map.push_back (2);
2117 insert_map.push_back (1);
2121 insert_map.push_back (11);
2122 insert_map.push_back (10);
2123 insert_map.push_back (9);
2124 insert_map.push_back (8);
2125 insert_map.push_back (6);
2126 insert_map.push_back (5);
2127 insert_map.push_back (3);
2128 insert_map.push_back (2);
2129 insert_map.push_back (1);
2133 insert_map.push_back (12);
2134 insert_map.push_back (11);
2135 insert_map.push_back (10);
2136 insert_map.push_back (8);
2137 insert_map.push_back (7);
2138 insert_map.push_back (5);
2139 insert_map.push_back (4);
2140 insert_map.push_back (2);
2141 insert_map.push_back (1);
2148 set (last_when, true);
2150 if (!is_transient && !noemit) {
2151 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2154 mode_changed (); /* EMIT SIGNAL (the member one) */
2158 AudioClock::set_bbt_reference (framepos_t pos)
2160 bbt_reference_time = pos;
2164 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2166 CairoWidget::on_style_changed (old_style);
2168 Gtk::Requisition req;
2169 set_clock_dimensions (req);
2171 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2177 AudioClock::set_editable (bool yn)
2183 AudioClock::set_is_duration (bool yn)
2185 if (yn == is_duration) {
2190 set (last_when, true);
2194 AudioClock::set_off (bool yn)
2202 /* force a redraw. last_when will be preserved, but the clock text will
2206 set (last_when, true);
2210 AudioClock::focus ()
2212 start_edit (Field (0));
2216 AudioClock::set_corner_radius (double r)
2225 AudioClock::dpi_reset ()
2227 /* force recomputation of size even if we are fixed width
2235 AudioClock::set_negative_allowed (bool yn)
2237 _negative_allowed = yn;