2 Copyright (C) 1999 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <cstdio> // for sprintf
23 #include "pbd/convert.h"
24 #include "pbd/enumwriter.h"
26 #include <gtkmm/style.h>
27 #include <sigc++/bind.h>
29 #include "gtkmm2ext/cairocell.h"
30 #include "gtkmm2ext/utils.h"
31 #include "gtkmm2ext/rgb_macros.h"
33 #include "ardour/profile.h"
34 #include "ardour/lmath.h"
35 #include "ardour/session.h"
36 #include "ardour/slave.h"
37 #include "ardour/tempo.h"
38 #include "ardour/types.h"
40 #include "ardour_ui.h"
41 #include "audio_clock.h"
42 #include "gui_thread.h"
45 #include "ui_config.h"
50 using namespace ARDOUR;
51 using namespace ARDOUR_UI_UTILS;
56 using Gtkmm2ext::Keyboard;
58 sigc::signal<void> AudioClock::ModeChanged;
59 vector<AudioClock*> AudioClock::clocks;
61 #define BBT_BAR_CHAR "|"
62 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
64 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
65 bool allow_edit, bool follows_playhead, bool duration, bool with_info,
66 bool accept_on_focus_out)
69 , is_transient (transient)
70 , is_duration (duration)
71 , editable (allow_edit)
72 , _follows_playhead (follows_playhead)
73 , _accept_on_focus_out (accept_on_focus_out)
76 , _edit_by_click_field (false)
77 , _negative_allowed (false)
78 , edit_is_negative (false)
79 , _with_info (with_info)
84 , style_resets_first (true)
90 , bbt_reference_time (-1)
95 , drag_field (Field (0))
99 set_flags (CAN_FOCUS);
101 _layout = Pango::Layout::create (get_pango_context());
102 _layout->set_attributes (normal_attributes);
104 set_widget_name (widget_name);
106 _mode = BBT; /* lie to force mode switch */
108 set (last_when, true);
111 clocks.push_back (this);
114 _left_btn.set_sizing_text (_("0000000000000"));
115 // NB right_btn is in a size-group
117 _left_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
118 _right_btn.set_layout_font (UIConfiguration::instance().get_SmallFont());
120 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
121 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
124 AudioClock::~AudioClock ()
126 delete foreground_attr;
131 AudioClock::set_widget_name (const string& str)
136 set_name (str + " clock");
146 AudioClock::on_realize ()
148 Gtk::Requisition req;
150 CairoWidget::on_realize ();
152 set_clock_dimensions (req);
154 first_width = req.width;
155 first_height = req.height;
157 // XXX FIX ME: define font based on ... ???
163 AudioClock::set_font (Pango::FontDescription font)
165 Glib::RefPtr<Gtk::Style> style = get_style ();
166 Pango::AttrFontDesc* font_attr;
168 font_size = font.get_size();
169 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
171 normal_attributes.change (*font_attr);
172 editing_attributes.change (*font_attr);
175 /* get the figure width for the font. This doesn't have to super
176 * accurate since we only use it to measure the (roughly 1 character)
177 * offset from the position Pango tells us for the "cursor"
180 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
184 tmp->get_pixel_size (em_width, ignore_height);
186 /* force redraw of markup with new font-size */
187 set (last_when, true);
191 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
193 CairoWidget::set_active_state (s);
198 AudioClock::set_colors ()
204 uint32_t editing_color;
205 uint32_t cursor_color;
207 if (active_state()) {
208 bg_color = UIConfiguration::instance().color (string_compose ("%1 active: background", get_name()));
209 text_color = UIConfiguration::instance().color (string_compose ("%1 active: text", get_name()));
210 editing_color = UIConfiguration::instance().color (string_compose ("%1 active: edited text", get_name()));
211 cursor_color = UIConfiguration::instance().color (string_compose ("%1 active: cursor", get_name()));
213 bg_color = UIConfiguration::instance().color (string_compose ("%1: background", get_name()));
214 text_color = UIConfiguration::instance().color (string_compose ("%1: text", get_name()));
215 editing_color = UIConfiguration::instance().color (string_compose ("%1: edited text", get_name()));
216 cursor_color = UIConfiguration::instance().color (string_compose ("%1: cursor", get_name()));
219 /* store for bg and cursor in render() */
221 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
228 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
235 /* rescale for Pango colors ... sigh */
237 r = lrint (r * 65535.0);
238 g = lrint (g * 65535.0);
239 b = lrint (b * 65535.0);
241 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
242 r = lrint ((r/255.0) * 65535.0);
243 g = lrint ((g/255.0) * 65535.0);
244 b = lrint ((b/255.0) * 65535.0);
245 delete foreground_attr;
246 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
248 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
249 r = lrint ((r/255.0) * 65535.0);
250 g = lrint ((g/255.0) * 65535.0);
251 b = lrint ((b/255.0) * 65535.0);
253 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
255 normal_attributes.change (*foreground_attr);
256 editing_attributes.change (*foreground_attr);
257 editing_attributes.change (*editing_attr);
260 _layout->set_attributes (normal_attributes);
262 _layout->set_attributes (editing_attributes);
269 AudioClock::set_scale (double x, double y)
278 AudioClock::render (cairo_t* cr, cairo_rectangle_t*)
280 /* main layout: rounded rect, plus the text */
283 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
285 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), corner_radius);
287 cairo_rectangle (cr, 0, 0, get_width(), get_height());
292 double lw = layout_width * xscale;
293 double lh = layout_height * yscale;
295 cairo_move_to (cr, (get_width() - lw) / 2.0, (get_height() - lh) / 2.0);
297 if (xscale != 1.0 || yscale != 1.0) {
299 cairo_scale (cr, xscale, yscale);
302 pango_cairo_show_layout (cr, _layout->gobj());
304 if (xscale != 1.0 || yscale != 1.0) {
309 if (!insert_map.empty()) {
311 int xcenter = (get_width() - layout_width) /2;
313 if (input_string.length() < insert_map.size()) {
314 Pango::Rectangle cursor;
316 if (input_string.empty()) {
317 /* nothing entered yet, put cursor at the end
320 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
322 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
325 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
327 min (get_width() - 2.0,
328 (double) xcenter + cursor.get_x()/PANGO_SCALE + em_width),
329 (get_height() - layout_height)/2.0,
330 2.0, cursor.get_height()/PANGO_SCALE);
333 /* we've entered all possible digits, no cursor */
337 if (input_string.empty()) {
338 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
341 (get_height() - layout_height)/2.0,
350 AudioClock::set_clock_dimensions (Gtk::Requisition& req)
352 Glib::RefPtr<Pango::Layout> tmp;
353 Glib::RefPtr<Gtk::Style> style = get_style ();
354 Pango::FontDescription font;
356 tmp = Pango::Layout::create (get_pango_context());
358 if (!is_realized()) {
359 font = get_font_for_style (get_name());
361 font = style->get_font();
364 tmp->set_font_description (font);
366 /* this string is the longest thing we will ever display */
368 tmp->set_text (" 88:88:88,888 ");
370 tmp->set_text (" 88:88:88,88 ");
371 tmp->get_pixel_size (req.width, req.height);
373 layout_height = req.height;
374 layout_width = req.width;
378 AudioClock::on_size_request (Gtk::Requisition* req)
380 /* even for non fixed width clocks, the size we *ask* for never changes,
381 even though the size we receive might. so once we've computed it,
386 req->width = first_width;
387 req->height = first_height;
391 set_clock_dimensions (*req);
393 /* now tackle height, for which we need to know the height of the lower
399 AudioClock::show_edit_status (int length)
401 editing_attr->set_start_index (edit_string.length() - length);
402 editing_attr->set_end_index (edit_string.length());
404 editing_attributes.change (*foreground_attr);
405 editing_attributes.change (*editing_attr);
407 _layout->set_attributes (editing_attributes);
411 AudioClock::start_edit (Field f)
414 pre_edit_string = _layout->get_text ();
415 if (!insert_map.empty()) {
416 edit_string = pre_edit_string;
418 edit_string.clear ();
419 _layout->set_text ("");
422 input_string.clear ();
424 edit_is_negative = false;
427 input_string = get_field (f);
428 show_edit_status (merge_input_and_edit_string ());
429 _layout->set_text (edit_string);
434 Keyboard::magic_widget_grab_focus ();
440 AudioClock::get_field (Field f)
444 return edit_string.substr (1, 2);
446 case Timecode_Minutes:
447 return edit_string.substr (4, 2);
449 case Timecode_Seconds:
450 return edit_string.substr (7, 2);
452 case Timecode_Frames:
453 return edit_string.substr (10, 2);
456 return edit_string.substr (1, 2);
459 return edit_string.substr (4, 2);
462 return edit_string.substr (7, 2);
464 case MS_Milliseconds:
465 return edit_string.substr (10, 3);
468 return edit_string.substr (1, 3);
471 return edit_string.substr (5, 2);
474 return edit_string.substr (8, 4);
484 AudioClock::end_edit (bool modify)
492 ok = timecode_validate_edit (edit_string);
496 ok = bbt_validate_edit (edit_string);
500 ok = minsec_validate_edit (edit_string);
504 if (edit_string.length() < 1) {
505 edit_string = pre_edit_string;
511 edit_string = pre_edit_string;
512 input_string.clear ();
513 _layout->set_text (edit_string);
514 show_edit_status (0);
515 /* edit attributes remain in use */
519 framepos_t pos = 0; /* stupid gcc */
523 pos = frames_from_timecode_string (edit_string);
528 pos = frame_duration_from_bbt_string (bbt_reference_time, edit_string);
530 pos = frames_from_bbt_string (0, edit_string);
535 pos = frames_from_minsec_string (edit_string);
539 pos = frames_from_audioframes_string (edit_string);
544 _layout->set_attributes (normal_attributes);
545 ValueChanged(); /* EMIT_SIGNAL */
551 edit_is_negative = false;
552 _layout->set_attributes (normal_attributes);
553 _layout->set_text (pre_edit_string);
564 AudioClock::drop_focus ()
566 Keyboard::magic_widget_drop_focus ();
569 /* move focus back to the default widget in the top level window */
570 ARDOUR_UI::instance()->reset_focus (this);
575 AudioClock::parse_as_frames_distance (const std::string& str)
579 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
587 AudioClock::parse_as_minsec_distance (const std::string& str)
589 framecnt_t sr = _session->frame_rate();
595 switch (str.length()) {
602 sscanf (str.c_str(), "%" PRId32, &msecs);
603 return msecs * (sr / 1000);
606 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
607 return (secs * sr) + (msecs * (sr/1000));
610 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
611 return (secs * sr) + (msecs * (sr/1000));
614 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
615 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
618 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
619 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
622 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
623 return (hrs * 3600 * sr) + (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));
637 AudioClock::parse_as_timecode_distance (const std::string& str)
639 double fps = _session->timecode_frames_per_second();
640 framecnt_t sr = _session->frame_rate();
646 switch (str.length()) {
651 sscanf (str.c_str(), "%" PRId32, &frames);
652 return llrint ((frames/(float)fps) * sr);
655 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
656 return (secs * sr) + llrint ((frames/(float)fps) * sr);
659 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
660 return (secs * sr) + llrint ((frames/(float)fps) * sr);
663 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
664 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
667 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
668 return (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
671 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
672 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + llrint ((frames/(float)fps) * sr);
675 sscanf (str.c_str(), "%2" 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);
686 AudioClock::parse_as_bbt_distance (const std::string&)
692 AudioClock::parse_as_distance (const std::string& instr)
696 return parse_as_timecode_distance (instr);
699 return parse_as_frames_distance (instr);
702 return parse_as_bbt_distance (instr);
705 return parse_as_minsec_distance (instr);
712 AudioClock::end_edit_relative (bool add)
718 ok = timecode_validate_edit (edit_string);
722 ok = bbt_validate_edit (edit_string);
726 ok = minsec_validate_edit (edit_string);
734 edit_string = pre_edit_string;
735 input_string.clear ();
736 _layout->set_text (edit_string);
737 show_edit_status (0);
738 /* edit attributes remain in use */
743 framecnt_t frames = parse_as_distance (input_string);
748 _layout->set_attributes (normal_attributes);
752 set (current_time() + frames, true);
754 framepos_t c = current_time();
756 if (c > frames || _negative_allowed) {
757 set (c - frames, true);
762 ValueChanged (); /* EMIT SIGNAL */
765 input_string.clear ();
771 AudioClock::session_property_changed (const PropertyChange&)
773 set (last_when, true);
777 AudioClock::metric_position_changed ()
779 set (last_when, true);
783 AudioClock::session_configuration_changed (std::string p)
785 if (_negative_allowed) {
786 /* session option editor clock */
790 if (p == "sync-source" || p == "external-sync") {
791 set (current_time(), true);
795 if (p != "timecode-offset" && p != "timecode-offset-negative") {
804 current = current_duration ();
806 current = current_time ();
816 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
818 if ((!force && !is_visible()) || _session == 0) {
823 when = when - offset;
826 if (when == last_when && !force) {
827 #if 0 // XXX return if no change and no change forced. verify Aug/2014
828 if (_mode != Timecode && _mode != MinSec) {
829 /* may need to force display of TC source
830 * time, so don't return early.
832 /* ^^ Why was that?, delta times?
833 * Timecode FPS, pull-up/down, etc changes
834 * trigger a 'session_property_changed' which
835 * eventually calls set(last_when, true)
837 * re-rendering the clock every 40ms or so just
838 * because we can is not ideal.
853 set_timecode (when, force);
857 set_bbt (when, offset, force);
862 set_minsec (when, force);
866 set_frames (when, force);
872 _left_btn.set_sensitive (btn_en);
873 _right_btn.set_sensitive (btn_en);
874 _left_btn.set_visual_state (Gtkmm2ext::NoVisualState);
875 _right_btn.set_visual_state (Gtkmm2ext::NoVisualState);
877 _left_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
878 _right_btn.set_elements (ArdourButton::Element(ArdourButton::Edge|ArdourButton::Body|ArdourButton::Text));
879 _left_btn.set_alignment (.5, .5);
880 _right_btn.set_alignment (.5, .5);
881 set_tooltip (_left_btn, _("Change current tempo"));
882 set_tooltip (_right_btn, _("Change current time signature"));
884 _left_btn.set_elements (ArdourButton::Text);
885 _right_btn.set_elements (ArdourButton::Text);
886 _left_btn.set_alignment (0, .5);
887 _right_btn.set_alignment (1, .5);
888 set_tooltip (_left_btn, _(""));
889 set_tooltip (_right_btn, _(""));
898 AudioClock::set_slave_info ()
904 SyncSource sync_src = Config->get_sync_source();
906 if (_session->config.get_external_sync()) {
907 Slave* slave = _session->slave();
911 _left_btn.set_text (sync_source_to_string (sync_src, true));
912 _right_btn.set_text ("");
916 _left_btn.set_text (sync_source_to_string (sync_src, true));
917 _right_btn.set_text (slave->approximate_current_delta (), true);
919 _left_btn.set_text (_("--pending--"));
920 _right_btn.set_text ("");
927 TimecodeSlave* tcslave;
928 if ((tcslave = dynamic_cast<TimecodeSlave*>(_session->slave())) != 0) {
929 matching = (tcslave->apparent_timecode_format() == _session->config.get_timecode_format());
930 _left_btn.set_text (string_compose ("%1<span face=\"monospace\" foreground=\"%3\">%2</span>",
931 sync_source_to_string(sync_src, true)[0],
932 dynamic_cast<TimecodeSlave*>(slave)->approximate_current_position (),
933 matching ? "#66ff66" : "#ff3333"
935 _right_btn.set_text (slave->approximate_current_delta (), true);
938 _left_btn.set_text (_("--pending--"));
939 _right_btn.set_text ("");
944 _left_btn.set_text (string_compose ("%1/%2",
945 _("INT"), sync_source_to_string(sync_src, true)));
946 _right_btn.set_text ("");
951 AudioClock::set_frames (framepos_t when, bool /*force*/)
954 bool negative = false;
957 _layout->set_text (" ----------");
958 _left_btn.set_text ("");
959 _right_btn.set_text ("");
969 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
971 snprintf (buf, sizeof (buf), " %10" PRId64, when);
974 _layout->set_text (buf);
977 framecnt_t rate = _session->frame_rate();
979 if (fmod (rate, 100.0) == 0.0) {
980 sprintf (buf, "%.1fkHz", rate/1000.0);
982 sprintf (buf, "%" PRId64 "Hz", rate);
985 _left_btn.set_text (string_compose ("%1 %2", _("SR"), buf));
987 float vid_pullup = _session->config.get_video_pullup();
989 if (vid_pullup == 0.0) {
990 _right_btn.set_text ("");
992 sprintf (buf, _("%+.4f%%"), vid_pullup);
993 _right_btn.set_text (string_compose ("%1 %2", _("Pull"), buf));
999 AudioClock::print_minsec (framepos_t when, char* buf, size_t bufsize, float frame_rate)
1016 hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
1017 left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
1018 mins = (int) floor (left / (frame_rate * 60.0f));
1019 left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
1020 secs = (int) floor (left / (float) frame_rate);
1021 left -= (framecnt_t) floor ((double)(secs * frame_rate));
1022 millisecs = floor (left * 1000.0 / (float) frame_rate);
1025 snprintf (buf, bufsize, "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1027 snprintf (buf, bufsize, " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
1033 AudioClock::set_minsec (framepos_t when, bool /*force*/)
1038 _layout->set_text (" --:--:--.---");
1039 _left_btn.set_text ("");
1040 _right_btn.set_text ("");
1045 print_minsec (when, buf, sizeof (buf), _session->frame_rate());
1047 _layout->set_text (buf);
1052 AudioClock::set_timecode (framepos_t when, bool /*force*/)
1055 bool negative = false;
1058 _layout->set_text (" --:--:--:--");
1059 _left_btn.set_text ("");
1060 _right_btn.set_text ("");
1070 _session->timecode_duration (when, TC);
1072 _session->timecode_time (when, TC);
1075 TC.negative = TC.negative || negative;
1077 _layout->set_text (Timecode::timecode_format_time(TC));
1083 AudioClock::set_bbt (framepos_t when, framecnt_t offset, bool /*force*/)
1086 Timecode::BBT_Time BBT;
1087 bool negative = false;
1090 _layout->set_text (" ---|--|----");
1091 _left_btn.set_text ("");
1092 _right_btn.set_text ("");
1101 /* handle a common case */
1108 TempoMap& tmap (_session->tempo_map());
1111 offset = bbt_reference_time;
1114 const double divisions = tmap.meter_section_at_frame (offset).divisions_per_bar();
1115 Timecode::BBT_Time sub_bbt;
1118 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (offset));
1119 sub_bbt = tmap.bbt_at_frame (offset - when);
1121 BBT = tmap.bbt_at_beat (tmap.beat_at_frame (when + offset));
1122 sub_bbt = tmap.bbt_at_frame (offset);
1125 BBT.bars -= sub_bbt.bars;
1127 if (BBT.ticks < sub_bbt.ticks) {
1128 if (BBT.beats == 1) {
1130 BBT.beats = divisions;
1134 BBT.ticks = Timecode::BBT_Time::ticks_per_beat - (sub_bbt.ticks - BBT.ticks);
1136 BBT.ticks -= sub_bbt.ticks;
1139 if (BBT.beats < sub_bbt.beats) {
1141 BBT.beats = divisions - (sub_bbt.beats - BBT.beats);
1143 BBT.beats -= sub_bbt.beats;
1147 BBT = _session->tempo_map().bbt_at_frame (when);
1151 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1152 BBT.bars, BBT.beats, BBT.ticks);
1154 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1155 BBT.bars, BBT.beats, BBT.ticks);
1158 _layout->set_text (buf);
1163 if (bbt_reference_time < 0) {
1166 pos = bbt_reference_time;
1169 TempoMetric m (_session->tempo_map().metric_at (pos));
1171 if (m.tempo().note_type() == 4) {
1172 snprintf (buf, sizeof(buf), "\u2669 = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1173 _left_btn.set_text (string_compose ("%1", buf));
1174 } else if (m.tempo().note_type() == 8) {
1175 snprintf (buf, sizeof(buf), "\u266a = %.3f", _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1176 _left_btn.set_text (string_compose ("%1", buf));
1178 snprintf (buf, sizeof(buf), "%.1f = %.3f", m.tempo().note_type(), _session->tempo_map().tempo_at_frame (pos).note_types_per_minute());
1179 _left_btn.set_text (string_compose ("%1: %2", S_("Tempo|T"), buf));
1182 snprintf (buf, sizeof(buf), "%g/%g", m.meter().divisions_per_bar(), m.meter().note_divisor());
1183 _right_btn.set_text (string_compose ("%1: %2", S_("TimeSignature|TS"), buf));
1188 AudioClock::set_session (Session *s)
1190 SessionHandlePtr::set_session (s);
1194 Config->ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1195 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1196 _session->tempo_map().PropertyChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_property_changed, this, _1), gui_context());
1197 _session->tempo_map().MetricPositionChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::metric_position_changed, this), gui_context());
1199 XMLProperty const * prop;
1200 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1201 AudioClock::Mode amode;
1204 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1205 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1207 if ((prop = (*i)->property (X_("mode"))) != 0) {
1208 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1209 set_mode (amode, true);
1211 if ((prop = (*i)->property (X_("on"))) != 0) {
1212 set_off (!string_is_affirmative (prop->value()));
1219 set (last_when, true);
1224 AudioClock::on_key_press_event (GdkEventKey* ev)
1232 int highlight_length;
1235 switch (ev->keyval) {
1278 case GDK_KP_Subtract:
1279 if (_negative_allowed && input_string.empty()) {
1280 edit_is_negative = true;
1281 edit_string.replace(0,1,"-");
1282 _layout->set_text (edit_string);
1285 end_edit_relative (false);
1291 end_edit_relative (true);
1304 ChangeAborted(); /* EMIT SIGNAL */
1309 if (!input_string.empty()) {
1310 /* delete the last key entered
1312 input_string = input_string.substr (0, input_string.length() - 1);
1314 goto use_input_string;
1317 /* do not allow other keys to passthru to the rest of the GUI
1323 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1324 /* too many digits: eat the key event, but do nothing with it */
1328 input_string.push_back (new_char);
1334 /* get this one in the right order, and to the right width */
1335 if (ev->keyval == GDK_Delete || ev->keyval == GDK_BackSpace) {
1336 edit_string = edit_string.substr (0, edit_string.length() - 1);
1338 edit_string.push_back (new_char);
1340 if (!edit_string.empty()) {
1342 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1343 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1346 /* highlight the whole thing */
1347 highlight_length = edit_string.length();
1351 highlight_length = merge_input_and_edit_string ();
1354 if (edit_is_negative) {
1355 edit_string.replace(0,1,"-");
1357 if (!pre_edit_string.empty() && (pre_edit_string.at(0) == '-')) {
1358 edit_string.replace(0,1,"_");
1360 edit_string.replace(0,1," ");
1364 show_edit_status (highlight_length);
1365 _layout->set_text (edit_string);
1372 AudioClock::merge_input_and_edit_string ()
1374 /* merge with pre-edit-string into edit string */
1376 edit_string = pre_edit_string;
1378 if (input_string.empty()) {
1382 string::size_type target;
1383 for (string::size_type i = 0; i < input_string.length(); ++i) {
1384 target = insert_map[input_string.length() - 1 - i];
1385 edit_string[target] = input_string[i];
1387 /* highlight from end to wherever the last character was added */
1388 return edit_string.length() - insert_map[input_string.length()-1];
1393 AudioClock::on_key_release_event (GdkEventKey *ev)
1399 /* return true for keys that we used on press
1400 so that they cannot possibly do double-duty
1402 switch (ev->keyval) {
1425 case GDK_KP_Decimal:
1433 case GDK_KP_Subtract:
1441 AudioClock::index_to_field (int index) const
1446 return Timecode_Hours;
1447 } else if (index < 7) {
1448 return Timecode_Minutes;
1449 } else if (index < 10) {
1450 return Timecode_Seconds;
1452 return Timecode_Frames;
1458 } else if (index < 7) {
1466 return Timecode_Hours;
1467 } else if (index < 6) {
1469 } else if (index < 9) {
1472 return MS_Milliseconds;
1484 AudioClock::on_button_press_event (GdkEventButton *ev)
1486 switch (ev->button) {
1488 if (editable && !_off) {
1494 /* the text has been centered vertically, so adjust
1497 int xcenter = (get_width() - layout_width) /2;
1499 y = ev->y - ((get_height() - layout_height)/2);
1500 x = ev->x - xcenter;
1502 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1503 /* pretend it is a character on the far right */
1506 drag_field = index_to_field (index);
1508 /* make absolutely sure that the pointer is grabbed */
1509 gdk_pointer_grab(ev->window,false ,
1510 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1511 NULL,NULL,ev->time);
1513 drag_start_y = ev->y;
1527 AudioClock::on_button_release_event (GdkEventButton *ev)
1529 if (editable && !_off) {
1531 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1533 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1534 // we actually dragged so return without
1535 // setting editing focus, or we shift clicked
1538 if (ev->button == 1) {
1540 if (_edit_by_click_field) {
1542 int xcenter = (get_width() - layout_width) /2;
1545 int y = ev->y - ((get_height() - layout_height)/2);
1546 int x = ev->x - xcenter;
1549 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1553 f = index_to_field (index);
1556 case Timecode_Frames:
1557 case MS_Milliseconds:
1573 if (Keyboard::is_context_menu_event (ev)) {
1574 if (ops_menu == 0) {
1577 ops_menu->popup (1, ev->time);
1585 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1587 bool ret = CairoWidget::on_focus_out_event (ev);
1590 end_edit (_accept_on_focus_out);
1597 AudioClock::on_scroll_event (GdkEventScroll *ev)
1602 if (editing || _session == 0 || !editable || _off) {
1609 /* the text has been centered vertically, so adjust
1613 int xcenter = (get_width() - layout_width) /2;
1614 y = ev->y - ((get_height() - layout_height)/2);
1615 x = ev->x - xcenter;
1617 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1618 /* not in the main layout */
1622 Field f = index_to_field (index);
1623 framepos_t frames = 0;
1625 switch (ev->direction) {
1628 frames = get_frame_step (f, current_time(), 1);
1630 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1633 set (current_time() + frames, true);
1634 ValueChanged (); /* EMIT_SIGNAL */
1638 case GDK_SCROLL_DOWN:
1639 frames = get_frame_step (f, current_time(), -1);
1641 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1645 if (!_negative_allowed && (double)current_time() - (double)frames < 0.0) {
1648 set (current_time() - frames, true);
1651 ValueChanged (); /* EMIT_SIGNAL */
1664 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1666 if (editing || _session == 0 || !dragging) {
1670 float pixel_frame_scale_factor = 0.2f;
1672 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1673 pixel_frame_scale_factor = 0.1f;
1677 if (Keyboard::modifier_state_contains (ev->state,
1678 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1680 pixel_frame_scale_factor = 0.025f;
1683 double y_delta = ev->y - drag_y;
1685 drag_accum += y_delta*pixel_frame_scale_factor;
1689 if (floor (drag_accum) != 0) {
1694 dir = (drag_accum < 0 ? 1:-1);
1695 pos = current_time();
1696 frames = get_frame_step (drag_field, pos, dir);
1698 if (frames != 0 && frames * drag_accum < current_time()) {
1699 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1705 ValueChanged(); /* EMIT_SIGNAL */
1712 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1715 Timecode::BBT_Time BBT;
1717 case Timecode_Hours:
1718 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1720 case Timecode_Minutes:
1721 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1723 case Timecode_Seconds:
1724 f = _session->frame_rate();
1726 case Timecode_Frames:
1727 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1735 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1738 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1741 f = (framecnt_t) _session->frame_rate();
1743 case MS_Milliseconds:
1744 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1751 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1757 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1763 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1766 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1775 AudioClock::current_time (framepos_t) const
1781 AudioClock::current_duration (framepos_t pos) const
1790 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1806 AudioClock::bbt_validate_edit (const string& str)
1810 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1814 if (any.bbt.ticks > Timecode::BBT_Time::ticks_per_beat) {
1818 if (!is_duration && any.bbt.bars == 0) {
1822 if (!is_duration && any.bbt.beats == 0) {
1830 AudioClock::timecode_validate_edit (const string&)
1836 if (sscanf (_layout->get_text().c_str(), "%[- _]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
1837 ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1842 TC.hours = hours * -1;
1846 TC.negative = false;
1849 if (TC.negative && !_negative_allowed) {
1853 if (TC.hours > 23U || TC.minutes > 59U || TC.seconds > 59U) {
1857 if (TC.frames > (uint32_t) rint (_session->timecode_frames_per_second()) - 1) {
1861 if (_session->timecode_drop_frames()) {
1862 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1871 AudioClock::minsec_validate_edit (const string& str)
1873 int hrs, mins, secs, millisecs;
1875 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1879 if (hrs > 23 || mins > 59 || secs > 59 || millisecs > 999) {
1887 AudioClock::frames_from_timecode_string (const string& str) const
1889 if (_session == 0) {
1898 if (sscanf (str.c_str(), "%[- _]%d:%d:%d%[:;]%d", ignored, &hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
1899 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1902 TC.hours = abs(hours);
1903 TC.rate = _session->timecode_frames_per_second();
1904 TC.drop= _session->timecode_drop_frames();
1906 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1908 // timecode_tester ();
1909 if (edit_is_negative) {
1917 AudioClock::frames_from_minsec_string (const string& str) const
1919 if (_session == 0) {
1923 int hrs, mins, secs, millisecs;
1924 framecnt_t sr = _session->frame_rate();
1926 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1927 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1931 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1935 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1937 if (_session == 0) {
1938 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1943 any.type = AnyTime::BBT;
1945 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1952 return _session->any_duration_to_frames (pos, any);
1954 return _session->convert_to_frames (any);
1960 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1962 if (_session == 0) {
1963 error << "AudioClock::frame_duration_from_bbt_string() called with BBT mode but without session!" << endmsg;
1967 Timecode::BBT_Time bbt;
1969 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 3) {
1973 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1977 AudioClock::frames_from_audioframes_string (const string& str) const
1980 sscanf (str.c_str(), "%" PRId64, &f);
1985 AudioClock::copy_text_to_clipboard () const
1989 val = pre_edit_string;
1991 val = _layout->get_text ();
1993 const size_t trim = val.find_first_not_of(" ");
1994 if (trim == string::npos) {
1995 assert(0); // empty clock, can't be right.
1998 Glib::RefPtr<Clipboard> cl = Gtk::Clipboard::get();
1999 cl->set_text (val.substr(trim));
2003 AudioClock::build_ops_menu ()
2005 using namespace Menu_Helpers;
2006 ops_menu = new Menu;
2007 MenuList& ops_items = ops_menu->items();
2008 ops_menu->set_name ("ArdourContextMenu");
2010 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode, false)));
2011 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT, false)));
2012 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec, false)));
2013 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames, false)));
2015 if (editable && !_off && !is_duration && !_follows_playhead) {
2016 ops_items.push_back (SeparatorElem());
2017 ops_items.push_back (MenuElem (_("Set from Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
2018 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
2020 ops_items.push_back (SeparatorElem());
2021 ops_items.push_back (MenuElem (_("Copy to clipboard"), sigc::mem_fun(*this, &AudioClock::copy_text_to_clipboard)));
2025 AudioClock::set_from_playhead ()
2031 set (_session->transport_frame());
2036 AudioClock::locate ()
2038 if (!_session || is_duration) {
2042 _session->request_locate (current_time(), _session->transport_rolling ());
2046 AudioClock::set_mode (Mode m, bool noemit)
2056 _layout->set_text ("");
2058 Gtk::Requisition req;
2059 set_clock_dimensions (req);
2063 insert_map.push_back (11);
2064 insert_map.push_back (10);
2065 insert_map.push_back (8);
2066 insert_map.push_back (7);
2067 insert_map.push_back (5);
2068 insert_map.push_back (4);
2069 insert_map.push_back (2);
2070 insert_map.push_back (1);
2074 insert_map.push_back (11);
2075 insert_map.push_back (10);
2076 insert_map.push_back (9);
2077 insert_map.push_back (8);
2078 insert_map.push_back (6);
2079 insert_map.push_back (5);
2080 insert_map.push_back (3);
2081 insert_map.push_back (2);
2082 insert_map.push_back (1);
2086 insert_map.push_back (12);
2087 insert_map.push_back (11);
2088 insert_map.push_back (10);
2089 insert_map.push_back (8);
2090 insert_map.push_back (7);
2091 insert_map.push_back (5);
2092 insert_map.push_back (4);
2093 insert_map.push_back (2);
2094 insert_map.push_back (1);
2101 set (last_when, true);
2103 if (!is_transient && !noemit) {
2104 ModeChanged (); /* EMIT SIGNAL (the static one)*/
2107 mode_changed (); /* EMIT SIGNAL (the member one) */
2111 AudioClock::set_bbt_reference (framepos_t pos)
2113 bbt_reference_time = pos;
2117 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
2119 CairoWidget::on_style_changed (old_style);
2121 Gtk::Requisition req;
2122 set_clock_dimensions (req);
2124 /* XXXX fix me ... we shouldn't be using GTK styles anyway */
2130 AudioClock::set_editable (bool yn)
2136 AudioClock::set_is_duration (bool yn)
2138 if (yn == is_duration) {
2143 set (last_when, true);
2147 AudioClock::set_off (bool yn)
2155 /* force a redraw. last_when will be preserved, but the clock text will
2159 set (last_when, true);
2163 AudioClock::focus ()
2165 start_edit (Field (0));
2169 AudioClock::set_corner_radius (double r)
2178 AudioClock::dpi_reset ()
2180 /* force recomputation of size even if we are fixed width
2188 AudioClock::set_negative_allowed (bool yn)
2190 _negative_allowed = yn;