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>
28 #include "gtkmm2ext/cairocell.h"
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
32 #include "ardour/ardour.h"
33 #include "ardour/session.h"
34 #include "ardour/tempo.h"
35 #include "ardour/profile.h"
36 #include "ardour/slave.h"
37 #include <sigc++/bind.h>
39 #include "ardour_ui.h"
40 #include "audio_clock.h"
41 #include "global_signals.h"
44 #include "gui_thread.h"
47 using namespace ARDOUR;
52 using Gtkmm2ext::Keyboard;
54 sigc::signal<void> AudioClock::ModeChanged;
55 vector<AudioClock*> AudioClock::clocks;
56 const double AudioClock::info_font_scale_factor = 0.6;
57 const double AudioClock::separator_height = 2.0;
58 const double AudioClock::x_leading_padding = 6.0;
60 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
61 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
63 , is_transient (transient)
64 , is_duration (duration)
65 , editable (allow_edit)
66 , _follows_playhead (follows_playhead)
77 , mode_based_info_ratio (1.0)
80 , bbt_reference_time (-1)
85 , drag_field (Field (0))
88 set_flags (CAN_FOCUS);
90 _layout = Pango::Layout::create (get_pango_context());
91 _layout->set_attributes (normal_attributes);
94 _left_layout = Pango::Layout::create (get_pango_context());
95 _right_layout = Pango::Layout::create (get_pango_context());
98 set_widget_name (widget_name);
100 _mode = BBT; /* lie to force mode switch */
102 set (last_when, true);
105 clocks.push_back (this);
108 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
111 AudioClock::~AudioClock ()
113 delete foreground_attr;
118 AudioClock::set_widget_name (const string& str)
123 set_name (str + " clock");
133 AudioClock::on_realize ()
135 CairoWidget::on_realize ();
141 AudioClock::set_font ()
143 Glib::RefPtr<Gtk::Style> style = get_style ();
144 Pango::FontDescription font;
145 Pango::AttrFontDesc* font_attr;
147 if (!is_realized()) {
148 font = get_font_for_style (get_name());
150 font = style->get_font();
153 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
155 normal_attributes.change (*font_attr);
156 editing_attributes.change (*font_attr);
158 /* now a smaller version of the same font */
161 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
162 font.set_weight (Pango::WEIGHT_NORMAL);
163 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
165 info_attributes.change (*font_attr);
171 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
173 CairoWidget::set_active_state (s);
178 AudioClock::set_colors ()
184 uint32_t editing_color;
186 if (active_state()) {
187 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
188 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
189 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
191 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
192 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
193 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
196 /* store for bg in render() */
198 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
205 /* rescale for Pango colors ... sigh */
207 r = lrint (r * 65535.0);
208 g = lrint (g * 65535.0);
209 b = lrint (b * 65535.0);
211 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
212 r = lrint ((r/255.0) * 65535.0);
213 g = lrint ((g/255.0) * 65535.0);
214 b = lrint ((b/255.0) * 65535.0);
215 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
217 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
218 r = lrint ((r/255.0) * 65535.0);
219 g = lrint ((g/255.0) * 65535.0);
220 b = lrint ((b/255.0) * 65535.0);
221 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
223 normal_attributes.change (*foreground_attr);
224 info_attributes.change (*foreground_attr);
225 editing_attributes.change (*foreground_attr);
226 editing_attributes.change (*editing_attr);
229 _layout->set_attributes (normal_attributes);
231 _layout->set_attributes (editing_attributes);
235 _left_layout->set_attributes (info_attributes);
236 _right_layout->set_attributes (info_attributes);
243 AudioClock::render (cairo_t* cr)
245 /* main layout: rounded rect, plus the text */
248 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
250 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
252 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
258 cairo_move_to (cr, x_leading_padding, (upper_height - layout_height) / 2.0);
260 cairo_move_to (cr, x_leading_padding, 0);
263 pango_cairo_show_layout (cr, _layout->gobj());
267 double h = get_height() - upper_height - separator_height;
270 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
273 if (mode_based_info_ratio != 1.0) {
275 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
279 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
281 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
286 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
287 pango_cairo_show_layout (cr, _left_layout->gobj());
291 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
292 get_width() - separator_height - left_rect_width, h,
295 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
296 get_width() - separator_height - left_rect_width, h);
301 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
302 pango_cairo_show_layout (cr, _right_layout->gobj());
305 /* no info to display, or just one */
309 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
311 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
319 const double cursor_width = 12; /* need em width here, not 16 */
321 if (!insert_map.empty()) {
322 Pango::Rectangle cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
324 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.8);
326 x_leading_padding + cursor.get_x()/PANGO_SCALE + cursor_width,
327 (upper_height - layout_height)/2.0,
328 2.0, cursor.get_height()/PANGO_SCALE);
331 if (input_string.empty()) {
332 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.8);
335 (upper_height - layout_height)/2.0,
344 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
346 CairoWidget::on_size_allocate (alloc);
349 upper_height = (get_height()/2.0) - 1.0;
351 upper_height = get_height();
356 AudioClock::on_size_request (Gtk::Requisition* req)
358 /* our size never changes, so once we've computed it,
363 req->width = first_width;
364 req->height = first_height;
368 Glib::RefPtr<Pango::Layout> tmp;
369 Glib::RefPtr<Gtk::Style> style = get_style ();
370 Pango::FontDescription font;
373 tmp = Pango::Layout::create (get_pango_context());
375 if (!is_realized()) {
376 font = get_font_for_style (get_name());
378 font = style->get_font();
381 tmp->set_font_description (font);
383 /* this string is the longest thing we will ever display,
384 and also includes the BBT "|" that may descends below
385 the baseline a bit, and a comma for the minsecs mode
386 where we printf a fractional value (XXX or should)
389 tmp->set_text (" 88888888888,|");
391 tmp->get_pixel_size (req->width, req->height);
393 layout_height = req->height;
394 layout_width = req->width;
396 /* now tackle height, for which we need to know the height of the lower
404 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
405 font.set_weight (Pango::WEIGHT_NORMAL);
406 tmp->set_font_description (font);
408 /* we only care about height, so put as much stuff in here
409 as possible that might change the height.
411 tmp->set_text ("qyhH|"); /* one ascender, one descender */
413 tmp->get_pixel_size (w, info_height);
415 /* silly extra padding that seems necessary to correct the info
416 * that pango just gave us. I have no idea why.
421 req->height += info_height;
422 req->height += separator_height;
425 first_height = req->height;
426 first_width = req->width;
430 AudioClock::show_edit_status (int length)
432 editing_attr->set_start_index (edit_string.length() - length);
433 editing_attr->set_end_index (edit_string.length());
435 editing_attributes.change (*foreground_attr);
436 editing_attributes.change (*editing_attr);
438 _layout->set_attributes (editing_attributes);
442 AudioClock::start_edit ()
444 pre_edit_string = _layout->get_text ();
445 if (!insert_map.empty()) {
446 edit_string = pre_edit_string;
448 edit_string.clear ();
449 _layout->set_text ("");
451 input_string.clear ();
456 Keyboard::magic_widget_grab_focus ();
461 AudioClock::end_edit (bool modify)
469 ok = timecode_validate_edit (edit_string);
473 ok = bbt_validate_edit (edit_string);
484 edit_string = pre_edit_string;
485 input_string.clear ();
486 _layout->set_text (edit_string);
487 show_edit_status (0);
488 /* edit attributes remain in use */
492 framepos_t pos = 0; /* stupid gcc */
496 pos = frames_from_timecode_string (edit_string);
501 pos = frame_duration_from_bbt_string (0, edit_string);
503 pos = frames_from_bbt_string (0, edit_string);
508 pos = frames_from_minsec_string (edit_string);
512 pos = frames_from_audioframes_string (edit_string);
517 _layout->set_attributes (normal_attributes);
518 ValueChanged(); /* EMIT_SIGNAL */
524 _layout->set_attributes (normal_attributes);
525 _layout->set_text (pre_edit_string);
536 AudioClock::drop_focus ()
538 Keyboard::magic_widget_drop_focus ();
542 /* move focus back to the default widget in the top level window */
544 Widget* top = get_toplevel();
546 if (top->is_toplevel ()) {
547 Window* win = dynamic_cast<Window*> (top);
554 AudioClock::parse_as_frames_distance (const std::string& str)
558 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
566 AudioClock::parse_as_minsec_distance (const std::string& str)
568 framecnt_t sr = _session->frame_rate();
574 switch (str.length()) {
581 sscanf (str.c_str(), "%" PRId32, &msecs);
582 return msecs * (sr / 1000);
585 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
586 return (secs * sr) + (msecs * (sr/1000));
589 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
590 return (secs * sr) + (msecs * (sr/1000));
593 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
594 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
597 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
598 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
601 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
602 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
605 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
606 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
616 AudioClock::parse_as_timecode_distance (const std::string& str)
618 double fps = _session->timecode_frames_per_second();
619 framecnt_t sr = _session->frame_rate();
625 switch (str.length()) {
630 sscanf (str.c_str(), "%" PRId32, &frames);
631 return lrint ((frames/(float)fps) * sr);
634 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
635 return (secs * sr) + lrint ((frames/(float)fps) * sr);
638 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
639 return (secs * sr) + lrint ((frames/(float)fps) * sr);
642 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
643 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
646 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
647 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
650 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
651 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
654 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
655 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
665 AudioClock::parse_as_bbt_distance (const std::string& str)
671 AudioClock::parse_as_distance (const std::string& instr)
675 /* the input string is in reverse order */
677 std::reverse (str.begin(), str.end());
681 return parse_as_timecode_distance (str);
684 return parse_as_frames_distance (str);
687 return parse_as_bbt_distance (str);
690 return parse_as_minsec_distance (str);
697 AudioClock::end_edit_relative (bool add)
699 framecnt_t frames = parse_as_distance (input_string);
704 _layout->set_attributes (normal_attributes);
708 set (current_time() + frames, true);
710 framepos_t c = current_time();
713 set (c - frames, true);
718 ValueChanged (); /* EMIT SIGNAL */
721 input_string.clear ();
727 AudioClock::session_configuration_changed (std::string p)
729 if (p == "sync-source" || p == "external-sync") {
730 set (current_time(), true);
734 if (p != "timecode-offset" && p != "timecode-offset-negative") {
743 current = current_duration ();
745 current = current_time ();
755 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
757 if ((!force && !is_visible()) || _session == 0) {
762 when = when - offset;
765 if (when == last_when && !force) {
773 set_timecode (when, force);
777 set_bbt (when, force);
781 set_minsec (when, force);
785 set_frames (when, force);
795 AudioClock::set_frames (framepos_t when, bool /*force*/)
798 bool negative = false;
801 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
804 _left_layout->set_text ("");
805 _right_layout->set_text ("");
817 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
819 snprintf (buf, sizeof (buf), " %10" PRId64, when);
822 _layout->set_text (buf);
825 framecnt_t rate = _session->frame_rate();
827 if (fmod (rate, 100.0) == 0.0) {
828 sprintf (buf, "SR %.1fkHz", rate/1000.0);
830 sprintf (buf, "SR %" PRId64, rate);
833 _left_layout->set_text (buf);
835 float vid_pullup = _session->config.get_video_pullup();
837 if (vid_pullup == 0.0) {
838 _right_layout->set_text (_("pullup: \u2012"));
840 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
841 _right_layout->set_text (buf);
847 AudioClock::set_minsec (framepos_t when, bool force)
855 bool negative = false;
858 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
861 _left_layout->set_text ("");
862 _right_layout->set_text ("");
874 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
875 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
876 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
877 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
878 secs = (int) floor (left / (float) _session->frame_rate());
879 left -= (framecnt_t) floor (secs * _session->frame_rate());
880 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
883 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
885 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
888 _layout->set_text (buf);
892 AudioClock::set_timecode (framepos_t when, bool force)
896 bool negative = false;
899 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
901 _left_layout->set_text ("");
902 _right_layout->set_text ("");
914 _session->timecode_duration (when, TC);
916 _session->timecode_time (when, TC);
919 if (TC.negative || negative) {
920 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
922 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
925 _layout->set_text (buf);
929 if (_session->config.get_external_sync()) {
930 switch (_session->config.get_sync_source()) {
932 _left_layout->set_text ("JACK");
935 _left_layout->set_text ("MTC");
938 _left_layout->set_text ("M-Clock");
942 _left_layout->set_text ("INT");
945 double timecode_frames = _session->timecode_frames_per_second();
947 if (fmod(timecode_frames, 1.0) == 0.0) {
948 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
950 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
953 _right_layout->set_text (buf);
958 AudioClock::set_bbt (framepos_t when, bool force)
961 Timecode::BBT_Time BBT;
962 bool negative = false;
965 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
967 _left_layout->set_text ("");
968 _right_layout->set_text ("");
978 /* handle a common case */
985 _session->tempo_map().bbt_time (when, BBT);
990 _session->tempo_map().bbt_time (when, BBT);
994 snprintf (buf, sizeof (buf), "-%03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
996 snprintf (buf, sizeof (buf), " %03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
999 _layout->set_text (buf);
1001 if (_right_layout) {
1004 if (bbt_reference_time < 0) {
1007 pos = bbt_reference_time;
1010 TempoMetric m (_session->tempo_map().metric_at (pos));
1012 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1013 _left_layout->set_text (buf);
1015 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
1016 _right_layout->set_text (buf);
1021 AudioClock::set_session (Session *s)
1023 SessionHandlePtr::set_session (s);
1027 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1029 const XMLProperty* prop;
1030 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1031 AudioClock::Mode amode;
1034 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1035 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1037 if ((prop = (*i)->property (X_("mode"))) != 0) {
1038 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1041 if ((prop = (*i)->property (X_("on"))) != 0) {
1042 set_off (!string_is_affirmative (prop->value()));
1049 set (last_when, true);
1054 AudioClock::on_key_press_event (GdkEventKey* ev)
1060 /* return true for keys that we MIGHT use
1063 switch (ev->keyval) {
1086 case GDK_KP_Decimal:
1098 AudioClock::on_key_release_event (GdkEventKey *ev)
1107 switch (ev->keyval) {
1150 case GDK_KP_Subtract:
1151 end_edit_relative (false);
1157 end_edit_relative (true);
1170 ChangeAborted(); /* EMIT SIGNAL */
1175 input_string = input_string.substr (1, input_string.length() - 1);
1176 goto use_input_string;
1182 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1183 /* eat the key event, but do no nothing with it */
1187 input_string.insert (input_string.begin(), new_char);
1191 string::reverse_iterator ri;
1192 vector<int> insert_at;
1193 int highlight_length = 0;
1197 /* merge with pre-edit-string into edit string */
1201 /* get this one in the right order, and to the right width */
1202 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1203 edit_string.push_back (new_char);
1205 edit_string = edit_string.substr (0, edit_string.length() - 1);
1207 if (!edit_string.empty()) {
1208 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1209 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1212 highlight_length = edit_string.length();
1216 edit_string = pre_edit_string;
1218 /* backup through the original string, till we have
1219 * enough digits locations to put all the digits from
1223 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1224 if (isdigit (*ri)) {
1225 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1226 if (insert_at.size() == input_string.length()) {
1232 if (insert_at.size() != input_string.length()) {
1233 error << "something went wrong " << endmsg;
1235 for (int i = input_string.length() - 1; i >= 0; --i) {
1236 edit_string[insert_at[i]] = input_string[i];
1239 highlight_length = edit_string.length() - insert_at.back();
1245 if (edit_string != _layout->get_text()) {
1246 show_edit_status (highlight_length);
1247 _layout->set_text (edit_string);
1255 AudioClock::index_to_field (int index) const
1260 return Timecode_Hours;
1261 } else if (index < 7) {
1262 return Timecode_Minutes;
1263 } else if (index < 10) {
1264 return Timecode_Seconds;
1266 return Timecode_Frames;
1272 } else if (index < 7) {
1280 return Timecode_Hours;
1281 } else if (index < 6) {
1283 } else if (index < 9) {
1286 return MS_Milliseconds;
1298 AudioClock::on_button_press_event (GdkEventButton *ev)
1300 switch (ev->button) {
1302 if (editable && !_off) {
1304 /* make absolutely sure that the pointer is grabbed */
1305 gdk_pointer_grab(ev->window,false ,
1306 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1307 NULL,NULL,ev->time);
1309 drag_start_y = ev->y;
1317 /* the text has been centered vertically, so adjust
1321 y = ev->y - ((upper_height - layout_height)/2);
1322 x = ev->x - x_leading_padding;
1324 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1325 drag_field = index_to_field (index);
1327 drag_field = Field (0);
1341 AudioClock::on_button_release_event (GdkEventButton *ev)
1343 if (editable && !_off) {
1345 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1347 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1348 // we actually dragged so return without
1349 // setting editing focus, or we shift clicked
1352 if (ev->button == 1) {
1360 if (Keyboard::is_context_menu_event (ev)) {
1361 if (ops_menu == 0) {
1364 ops_menu->popup (1, ev->time);
1372 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1374 bool ret = CairoWidget::on_focus_out_event (ev);
1384 AudioClock::on_scroll_event (GdkEventScroll *ev)
1389 if (editing || _session == 0 || !editable || _off) {
1396 /* the text has been centered vertically, so adjust
1400 y = ev->y - ((upper_height - layout_height)/2);
1401 x = ev->x - x_leading_padding;
1403 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1404 /* not in the main layout */
1408 Field f = index_to_field (index);
1409 framepos_t frames = 0;
1411 switch (ev->direction) {
1414 frames = get_frame_step (f);
1416 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1419 set (current_time() + frames, true);
1420 ValueChanged (); /* EMIT_SIGNAL */
1424 case GDK_SCROLL_DOWN:
1425 frames = get_frame_step (f);
1427 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1431 if ((double)current_time() - (double)frames < 0.0) {
1434 set (current_time() - frames, true);
1437 ValueChanged (); /* EMIT_SIGNAL */
1450 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1452 if (editing || _session == 0 || !dragging) {
1456 float pixel_frame_scale_factor = 0.2f;
1458 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1459 pixel_frame_scale_factor = 0.1f;
1463 if (Keyboard::modifier_state_contains (ev->state,
1464 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1466 pixel_frame_scale_factor = 0.025f;
1469 double y_delta = ev->y - drag_y;
1471 drag_accum += y_delta*pixel_frame_scale_factor;
1475 if (trunc (drag_accum) != 0) {
1480 dir = (drag_accum < 0 ? 1:-1);
1481 pos = current_time();
1482 frames = get_frame_step (drag_field,pos,dir);
1484 if (frames != 0 && frames * drag_accum < current_time()) {
1485 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1491 ValueChanged(); /* EMIT_SIGNAL */
1498 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1501 Timecode::BBT_Time BBT;
1503 case Timecode_Hours:
1504 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1506 case Timecode_Minutes:
1507 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1509 case Timecode_Seconds:
1510 f = _session->frame_rate();
1512 case Timecode_Frames:
1513 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1521 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1524 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1527 f = (framecnt_t) _session->frame_rate();
1529 case MS_Milliseconds:
1530 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1537 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1543 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1549 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1552 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1561 AudioClock::current_time (framepos_t pos) const
1567 AudioClock::current_duration (framepos_t pos) const
1576 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1592 AudioClock::bbt_validate_edit (const string& str)
1596 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1598 if (!is_duration && any.bbt.bars == 0) {
1602 if (!is_duration && any.bbt.beats == 0) {
1610 AudioClock::timecode_validate_edit (const string& str)
1614 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1615 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1619 if (TC.minutes > 59 || TC.seconds > 59) {
1623 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1627 if (_session->timecode_drop_frames()) {
1628 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1637 AudioClock::frames_from_timecode_string (const string& str) const
1639 if (_session == 0) {
1646 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1647 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1651 TC.rate = _session->timecode_frames_per_second();
1652 TC.drop= _session->timecode_drop_frames();
1654 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1656 // timecode_tester ();
1662 AudioClock::frames_from_minsec_string (const string& str) const
1664 if (_session == 0) {
1668 int hrs, mins, secs, millisecs;
1669 framecnt_t sr = _session->frame_rate();
1671 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1672 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1676 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1680 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1682 if (_session == 0) {
1683 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1688 any.type = AnyTime::BBT;
1690 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1695 return _session->any_duration_to_frames (pos, any);
1697 return _session->convert_to_frames (any);
1703 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1705 if (_session == 0) {
1706 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1710 Timecode::BBT_Time bbt;
1712 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1714 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1718 AudioClock::frames_from_audioframes_string (const string& str) const
1721 sscanf (str.c_str(), "%" PRId64, &f);
1726 AudioClock::build_ops_menu ()
1728 using namespace Menu_Helpers;
1729 ops_menu = new Menu;
1730 MenuList& ops_items = ops_menu->items();
1731 ops_menu->set_name ("ArdourContextMenu");
1733 if (!Profile->get_sae()) {
1734 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1736 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1737 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1738 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1740 if (editable && !_off && !is_duration && !_follows_playhead) {
1741 ops_items.push_back (SeparatorElem());
1742 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1743 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1748 AudioClock::set_from_playhead ()
1754 set (_session->transport_frame());
1759 AudioClock::locate ()
1761 if (!_session || is_duration) {
1765 _session->request_locate (current_time(), _session->transport_rolling ());
1769 AudioClock::set_mode (Mode m)
1779 _layout->set_text ("");
1782 _left_layout->set_text ("");
1783 _right_layout->set_text ("");
1788 mode_based_info_ratio = 0.5;
1789 insert_map.push_back (11);
1790 insert_map.push_back (10);
1791 insert_map.push_back (8);
1792 insert_map.push_back (7);
1793 insert_map.push_back (5);
1794 insert_map.push_back (4);
1795 insert_map.push_back (2);
1796 insert_map.push_back (1);
1800 mode_based_info_ratio = 0.5;
1801 insert_map.push_back (11);
1802 insert_map.push_back (10);
1803 insert_map.push_back (9);
1804 insert_map.push_back (8);
1805 insert_map.push_back (6);
1806 insert_map.push_back (5);
1807 insert_map.push_back (3);
1808 insert_map.push_back (2);
1809 insert_map.push_back (1);
1813 mode_based_info_ratio = 1.0;
1814 insert_map.push_back (12);
1815 insert_map.push_back (11);
1816 insert_map.push_back (10);
1817 insert_map.push_back (8);
1818 insert_map.push_back (7);
1819 insert_map.push_back (5);
1820 insert_map.push_back (4);
1821 insert_map.push_back (2);
1822 insert_map.push_back (1);
1826 mode_based_info_ratio = 0.5;
1830 set (last_when, true);
1832 if (!is_transient) {
1833 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1836 mode_changed (); /* EMIT SIGNAL (the member one) */
1840 AudioClock::set_bbt_reference (framepos_t pos)
1842 bbt_reference_time = pos;
1846 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1848 CairoWidget::on_style_changed (old_style);
1854 AudioClock::set_editable (bool yn)
1860 AudioClock::set_is_duration (bool yn)
1862 if (yn == is_duration) {
1867 set (last_when, true);
1871 AudioClock::set_off (bool yn)
1879 /* force a redraw. last_when will be preserved, but the clock text will
1883 set (last_when, true);
1887 AudioClock::focus ()
1894 AudioClock::set_corner_radius (double r)