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 #define BBT_BAR_CHAR "|"
61 #define BBT_SCANF_FORMAT "%" PRIu32 "%*c%" PRIu32 "%*c%" PRIu32
63 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
64 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
66 , is_transient (transient)
67 , is_duration (duration)
68 , editable (allow_edit)
69 , _follows_playhead (follows_playhead)
81 , mode_based_info_ratio (1.0)
84 , bbt_reference_time (-1)
89 , drag_field (Field (0))
92 set_flags (CAN_FOCUS);
94 _layout = Pango::Layout::create (get_pango_context());
95 _layout->set_attributes (normal_attributes);
98 _left_layout = Pango::Layout::create (get_pango_context());
99 _right_layout = Pango::Layout::create (get_pango_context());
102 set_widget_name (widget_name);
104 _mode = BBT; /* lie to force mode switch */
106 set (last_when, true);
109 clocks.push_back (this);
112 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
113 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
116 AudioClock::~AudioClock ()
118 delete foreground_attr;
123 AudioClock::set_widget_name (const string& str)
128 set_name (str + " clock");
138 AudioClock::on_realize ()
140 CairoWidget::on_realize ();
146 AudioClock::set_font ()
148 Glib::RefPtr<Gtk::Style> style = get_style ();
149 Pango::FontDescription font;
150 Pango::AttrFontDesc* font_attr;
152 if (!is_realized()) {
153 font = get_font_for_style (get_name());
155 font = style->get_font();
158 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
160 normal_attributes.change (*font_attr);
161 editing_attributes.change (*font_attr);
163 /* now a smaller version of the same font */
166 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
167 font.set_weight (Pango::WEIGHT_NORMAL);
168 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
170 info_attributes.change (*font_attr);
176 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
178 CairoWidget::set_active_state (s);
183 AudioClock::set_colors ()
189 uint32_t editing_color;
190 uint32_t cursor_color;
192 if (active_state()) {
193 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
194 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
195 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
196 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
198 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
199 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
200 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
201 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
204 /* store for bg and cursor in render() */
206 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
213 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
220 /* rescale for Pango colors ... sigh */
222 r = lrint (r * 65535.0);
223 g = lrint (g * 65535.0);
224 b = lrint (b * 65535.0);
226 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
227 r = lrint ((r/255.0) * 65535.0);
228 g = lrint ((g/255.0) * 65535.0);
229 b = lrint ((b/255.0) * 65535.0);
230 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
232 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
233 r = lrint ((r/255.0) * 65535.0);
234 g = lrint ((g/255.0) * 65535.0);
235 b = lrint ((b/255.0) * 65535.0);
236 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
238 normal_attributes.change (*foreground_attr);
239 info_attributes.change (*foreground_attr);
240 editing_attributes.change (*foreground_attr);
241 editing_attributes.change (*editing_attr);
244 _layout->set_attributes (normal_attributes);
246 _layout->set_attributes (editing_attributes);
250 _left_layout->set_attributes (info_attributes);
251 _right_layout->set_attributes (info_attributes);
258 AudioClock::render (cairo_t* cr)
260 double layout_x_offset;
262 /* main layout: rounded rect, plus the text */
265 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
267 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
269 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
275 cairo_move_to (cr, 0, 0);
278 layout_x_offset = (get_width() - layout_width)/2.0;
279 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
282 pango_cairo_show_layout (cr, _layout->gobj());
286 double h = get_height() - upper_height - separator_height;
289 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
292 if (mode_based_info_ratio != 1.0) {
294 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
298 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
300 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
305 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
306 pango_cairo_show_layout (cr, _left_layout->gobj());
310 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
311 get_width() - separator_height - left_rect_width, h,
314 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
315 get_width() - separator_height - left_rect_width, h);
320 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
321 pango_cairo_show_layout (cr, _right_layout->gobj());
324 /* no info to display, or just one */
328 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
330 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
338 const double cursor_width = 12; /* need em width here, not 16 */
340 if (!insert_map.empty()) {
341 Pango::Rectangle cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
343 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
346 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
348 2.0, cursor.get_height()/PANGO_SCALE);
351 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
352 (upper_height - layout_height)/2.0,
353 2.0, cursor.get_height()/PANGO_SCALE);
357 if (input_string.empty()) {
358 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
367 (upper_height - layout_height)/2.0,
377 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
379 CairoWidget::on_size_allocate (alloc);
382 upper_height = (get_height()/2.0) - 1.0;
384 upper_height = get_height();
389 AudioClock::on_size_request (Gtk::Requisition* req)
391 /* even for non fixed width clocks, the size we *ask* for never changes,
392 even though the size we receive might. so once we've computed it,
397 req->width = first_width;
398 req->height = first_height;
402 Glib::RefPtr<Pango::Layout> tmp;
403 Glib::RefPtr<Gtk::Style> style = get_style ();
404 Pango::FontDescription font;
406 tmp = Pango::Layout::create (get_pango_context());
408 if (!is_realized()) {
409 font = get_font_for_style (get_name());
411 font = style->get_font();
414 tmp->set_font_description (font);
417 /* this string is the longest thing we will ever display,
418 and also includes the BBT bar char that may descends below
419 the baseline a bit, and a comma for the minsecs mode
420 where we printf a fractional value (XXX or should)
423 tmp->set_text (" 8888888888:,|");
427 tmp->set_text (" 88:88:88:88");
430 tmp->set_text (" 888|88|8888");
433 tmp->set_text (" 88:88:88,888");
436 tmp->set_text (" 8888888888");
441 tmp->get_pixel_size (req->width, req->height);
443 layout_height = req->height;
444 layout_width = req->width;
446 /* now tackle height, for which we need to know the height of the lower
454 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
455 font.set_weight (Pango::WEIGHT_NORMAL);
456 tmp->set_font_description (font);
458 /* we only care about height, so put as much stuff in here
459 as possible that might change the height.
461 tmp->set_text ("qyhH|"); /* one ascender, one descender */
463 tmp->get_pixel_size (w, info_height);
465 /* silly extra padding that seems necessary to correct the info
466 * that pango just gave us. I have no idea why.
471 req->height += info_height;
472 req->height += separator_height;
475 first_height = req->height;
476 first_width = req->width;
480 AudioClock::show_edit_status (int length)
482 editing_attr->set_start_index (edit_string.length() - length);
483 editing_attr->set_end_index (edit_string.length());
485 editing_attributes.change (*foreground_attr);
486 editing_attributes.change (*editing_attr);
488 _layout->set_attributes (editing_attributes);
492 AudioClock::start_edit ()
494 pre_edit_string = _layout->get_text ();
495 if (!insert_map.empty()) {
496 edit_string = pre_edit_string;
498 edit_string.clear ();
499 _layout->set_text ("");
501 input_string.clear ();
506 Keyboard::magic_widget_grab_focus ();
511 AudioClock::end_edit (bool modify)
519 ok = timecode_validate_edit (edit_string);
523 ok = bbt_validate_edit (edit_string);
534 edit_string = pre_edit_string;
535 input_string.clear ();
536 _layout->set_text (edit_string);
537 show_edit_status (0);
538 /* edit attributes remain in use */
542 framepos_t pos = 0; /* stupid gcc */
546 pos = frames_from_timecode_string (edit_string);
551 pos = frame_duration_from_bbt_string (0, edit_string);
553 pos = frames_from_bbt_string (0, edit_string);
558 pos = frames_from_minsec_string (edit_string);
562 pos = frames_from_audioframes_string (edit_string);
567 _layout->set_attributes (normal_attributes);
568 ValueChanged(); /* EMIT_SIGNAL */
574 _layout->set_attributes (normal_attributes);
575 _layout->set_text (pre_edit_string);
586 AudioClock::drop_focus ()
588 Keyboard::magic_widget_drop_focus ();
592 /* move focus back to the default widget in the top level window */
594 Widget* top = get_toplevel();
596 if (top->is_toplevel ()) {
597 Window* win = dynamic_cast<Window*> (top);
604 AudioClock::parse_as_frames_distance (const std::string& str)
608 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
616 AudioClock::parse_as_minsec_distance (const std::string& str)
618 framecnt_t sr = _session->frame_rate();
624 switch (str.length()) {
631 sscanf (str.c_str(), "%" PRId32, &msecs);
632 return msecs * (sr / 1000);
635 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
636 return (secs * sr) + (msecs * (sr/1000));
639 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
640 return (secs * sr) + (msecs * (sr/1000));
643 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
644 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
647 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
648 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
651 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
652 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
655 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
656 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
666 AudioClock::parse_as_timecode_distance (const std::string& str)
668 double fps = _session->timecode_frames_per_second();
669 framecnt_t sr = _session->frame_rate();
675 switch (str.length()) {
680 sscanf (str.c_str(), "%" PRId32, &frames);
681 return lrint ((frames/(float)fps) * sr);
684 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
685 return (secs * sr) + lrint ((frames/(float)fps) * sr);
688 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
689 return (secs * sr) + lrint ((frames/(float)fps) * sr);
692 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
693 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
696 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
697 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
700 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
701 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
704 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
705 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
715 AudioClock::parse_as_bbt_distance (const std::string& str)
721 AudioClock::parse_as_distance (const std::string& instr)
725 /* the input string is in reverse order */
727 std::reverse (str.begin(), str.end());
731 return parse_as_timecode_distance (str);
734 return parse_as_frames_distance (str);
737 return parse_as_bbt_distance (str);
740 return parse_as_minsec_distance (str);
747 AudioClock::end_edit_relative (bool add)
749 framecnt_t frames = parse_as_distance (input_string);
754 _layout->set_attributes (normal_attributes);
758 set (current_time() + frames, true);
760 framepos_t c = current_time();
763 set (c - frames, true);
768 ValueChanged (); /* EMIT SIGNAL */
771 input_string.clear ();
777 AudioClock::session_configuration_changed (std::string p)
779 if (p == "sync-source" || p == "external-sync") {
780 set (current_time(), true);
784 if (p != "timecode-offset" && p != "timecode-offset-negative") {
793 current = current_duration ();
795 current = current_time ();
805 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
807 if ((!force && !is_visible()) || _session == 0) {
812 when = when - offset;
815 if (when == last_when && !force) {
823 set_timecode (when, force);
827 set_bbt (when, force);
831 set_minsec (when, force);
835 set_frames (when, force);
845 AudioClock::set_frames (framepos_t when, bool /*force*/)
848 bool negative = false;
851 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
854 _left_layout->set_text ("");
855 _right_layout->set_text ("");
867 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
869 snprintf (buf, sizeof (buf), " %10" PRId64, when);
872 _layout->set_text (buf);
875 framecnt_t rate = _session->frame_rate();
877 if (fmod (rate, 100.0) == 0.0) {
878 sprintf (buf, "SR %.1fkHz", rate/1000.0);
880 sprintf (buf, "SR %" PRId64, rate);
883 _left_layout->set_text (buf);
885 float vid_pullup = _session->config.get_video_pullup();
887 if (vid_pullup == 0.0) {
888 _right_layout->set_text (_("pullup: \u2012"));
890 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
891 _right_layout->set_text (buf);
897 AudioClock::set_minsec (framepos_t when, bool force)
905 bool negative = false;
908 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
911 _left_layout->set_text ("");
912 _right_layout->set_text ("");
924 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
925 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
926 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
927 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
928 secs = (int) floor (left / (float) _session->frame_rate());
929 left -= (framecnt_t) floor (secs * _session->frame_rate());
930 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
933 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
935 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
938 _layout->set_text (buf);
942 AudioClock::set_timecode (framepos_t when, bool force)
946 bool negative = false;
949 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
951 _left_layout->set_text ("");
952 _right_layout->set_text ("");
964 _session->timecode_duration (when, TC);
966 _session->timecode_time (when, TC);
969 if (TC.negative || negative) {
970 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
972 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
975 _layout->set_text (buf);
979 if (_session->config.get_external_sync()) {
980 switch (_session->config.get_sync_source()) {
982 _left_layout->set_text ("JACK");
985 _left_layout->set_text ("MTC");
988 _left_layout->set_text ("M-Clock");
992 _left_layout->set_text ("INT");
995 double timecode_frames = _session->timecode_frames_per_second();
997 if (fmod(timecode_frames, 1.0) == 0.0) {
998 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1000 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1003 _right_layout->set_text (buf);
1008 AudioClock::set_bbt (framepos_t when, bool force)
1011 Timecode::BBT_Time BBT;
1012 bool negative = false;
1015 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1017 _left_layout->set_text ("");
1018 _right_layout->set_text ("");
1028 /* handle a common case */
1035 _session->tempo_map().bbt_time (when, BBT);
1040 _session->tempo_map().bbt_time (when, BBT);
1044 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1045 BBT.bars, BBT.beats, BBT.ticks);
1047 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1048 BBT.bars, BBT.beats, BBT.ticks);
1051 _layout->set_text (buf);
1053 if (_right_layout) {
1056 if (bbt_reference_time < 0) {
1059 pos = bbt_reference_time;
1062 TempoMetric m (_session->tempo_map().metric_at (pos));
1064 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1065 _left_layout->set_text (buf);
1067 sprintf (buf, "%g/%g", m.meter().beats_per_bar(), m.meter().note_divisor());
1068 _right_layout->set_text (buf);
1073 AudioClock::set_session (Session *s)
1075 SessionHandlePtr::set_session (s);
1079 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1081 const XMLProperty* prop;
1082 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1083 AudioClock::Mode amode;
1086 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1087 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1089 if ((prop = (*i)->property (X_("mode"))) != 0) {
1090 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1093 if ((prop = (*i)->property (X_("on"))) != 0) {
1094 set_off (!string_is_affirmative (prop->value()));
1101 set (last_when, true);
1106 AudioClock::on_key_press_event (GdkEventKey* ev)
1112 /* return true for keys that we MIGHT use
1115 switch (ev->keyval) {
1138 case GDK_KP_Decimal:
1150 AudioClock::on_key_release_event (GdkEventKey *ev)
1159 switch (ev->keyval) {
1202 case GDK_KP_Subtract:
1203 end_edit_relative (false);
1209 end_edit_relative (true);
1222 ChangeAborted(); /* EMIT SIGNAL */
1227 input_string = input_string.substr (1, input_string.length() - 1);
1228 goto use_input_string;
1234 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1235 /* eat the key event, but do no nothing with it */
1239 input_string.insert (input_string.begin(), new_char);
1243 string::reverse_iterator ri;
1244 vector<int> insert_at;
1245 int highlight_length = 0;
1249 /* merge with pre-edit-string into edit string */
1253 /* get this one in the right order, and to the right width */
1254 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1255 edit_string.push_back (new_char);
1257 edit_string = edit_string.substr (0, edit_string.length() - 1);
1259 if (!edit_string.empty()) {
1260 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1261 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1264 highlight_length = edit_string.length();
1268 edit_string = pre_edit_string;
1270 /* backup through the original string, till we have
1271 * enough digits locations to put all the digits from
1275 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1276 if (isdigit (*ri)) {
1277 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1278 if (insert_at.size() == input_string.length()) {
1284 if (insert_at.size() != input_string.length()) {
1285 error << "something went wrong " << endmsg;
1287 for (int i = input_string.length() - 1; i >= 0; --i) {
1288 edit_string[insert_at[i]] = input_string[i];
1291 highlight_length = edit_string.length() - insert_at.back();
1297 if (edit_string != _layout->get_text()) {
1298 show_edit_status (highlight_length);
1299 _layout->set_text (edit_string);
1307 AudioClock::index_to_field (int index) const
1312 return Timecode_Hours;
1313 } else if (index < 7) {
1314 return Timecode_Minutes;
1315 } else if (index < 10) {
1316 return Timecode_Seconds;
1318 return Timecode_Frames;
1324 } else if (index < 7) {
1332 return Timecode_Hours;
1333 } else if (index < 6) {
1335 } else if (index < 9) {
1338 return MS_Milliseconds;
1350 AudioClock::on_button_press_event (GdkEventButton *ev)
1352 switch (ev->button) {
1354 if (editable && !_off) {
1356 /* make absolutely sure that the pointer is grabbed */
1357 gdk_pointer_grab(ev->window,false ,
1358 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1359 NULL,NULL,ev->time);
1361 drag_start_y = ev->y;
1369 /* the text has been centered vertically, so adjust
1373 y = ev->y - ((upper_height - layout_height)/2);
1374 x = ev->x - x_leading_padding;
1376 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1377 drag_field = index_to_field (index);
1379 drag_field = Field (0);
1393 AudioClock::on_button_release_event (GdkEventButton *ev)
1395 if (editable && !_off) {
1397 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1399 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1400 // we actually dragged so return without
1401 // setting editing focus, or we shift clicked
1404 if (ev->button == 1) {
1412 if (Keyboard::is_context_menu_event (ev)) {
1413 if (ops_menu == 0) {
1416 ops_menu->popup (1, ev->time);
1424 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1426 bool ret = CairoWidget::on_focus_out_event (ev);
1436 AudioClock::on_scroll_event (GdkEventScroll *ev)
1441 if (editing || _session == 0 || !editable || _off) {
1448 /* the text has been centered vertically, so adjust
1452 y = ev->y - ((upper_height - layout_height)/2);
1453 x = ev->x - x_leading_padding;
1455 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1456 /* not in the main layout */
1460 Field f = index_to_field (index);
1461 framepos_t frames = 0;
1463 switch (ev->direction) {
1466 frames = get_frame_step (f);
1468 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1471 set (current_time() + frames, true);
1472 ValueChanged (); /* EMIT_SIGNAL */
1476 case GDK_SCROLL_DOWN:
1477 frames = get_frame_step (f);
1479 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1483 if ((double)current_time() - (double)frames < 0.0) {
1486 set (current_time() - frames, true);
1489 ValueChanged (); /* EMIT_SIGNAL */
1502 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1504 if (editing || _session == 0 || !dragging) {
1508 float pixel_frame_scale_factor = 0.2f;
1510 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1511 pixel_frame_scale_factor = 0.1f;
1515 if (Keyboard::modifier_state_contains (ev->state,
1516 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1518 pixel_frame_scale_factor = 0.025f;
1521 double y_delta = ev->y - drag_y;
1523 drag_accum += y_delta*pixel_frame_scale_factor;
1527 if (trunc (drag_accum) != 0) {
1532 dir = (drag_accum < 0 ? 1:-1);
1533 pos = current_time();
1534 frames = get_frame_step (drag_field,pos,dir);
1536 if (frames != 0 && frames * drag_accum < current_time()) {
1537 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1543 ValueChanged(); /* EMIT_SIGNAL */
1550 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1553 Timecode::BBT_Time BBT;
1555 case Timecode_Hours:
1556 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1558 case Timecode_Minutes:
1559 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1561 case Timecode_Seconds:
1562 f = _session->frame_rate();
1564 case Timecode_Frames:
1565 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1573 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1576 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1579 f = (framecnt_t) _session->frame_rate();
1581 case MS_Milliseconds:
1582 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1589 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1595 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1601 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1604 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1613 AudioClock::current_time (framepos_t pos) const
1619 AudioClock::current_duration (framepos_t pos) const
1628 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1644 AudioClock::bbt_validate_edit (const string& str)
1648 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1652 if (!is_duration && any.bbt.bars == 0) {
1656 if (!is_duration && any.bbt.beats == 0) {
1664 AudioClock::timecode_validate_edit (const string& str)
1668 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1669 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1673 if (TC.minutes > 59 || TC.seconds > 59) {
1677 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1681 if (_session->timecode_drop_frames()) {
1682 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1691 AudioClock::frames_from_timecode_string (const string& str) const
1693 if (_session == 0) {
1700 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1701 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1705 TC.rate = _session->timecode_frames_per_second();
1706 TC.drop= _session->timecode_drop_frames();
1708 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1710 // timecode_tester ();
1716 AudioClock::frames_from_minsec_string (const string& str) const
1718 if (_session == 0) {
1722 int hrs, mins, secs, millisecs;
1723 framecnt_t sr = _session->frame_rate();
1725 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1726 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1730 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1734 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1736 if (_session == 0) {
1737 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1742 any.type = AnyTime::BBT;
1744 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1751 return _session->any_duration_to_frames (pos, any);
1753 return _session->convert_to_frames (any);
1759 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1761 if (_session == 0) {
1762 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1766 Timecode::BBT_Time bbt;
1768 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 0) {
1772 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1776 AudioClock::frames_from_audioframes_string (const string& str) const
1779 sscanf (str.c_str(), "%" PRId64, &f);
1784 AudioClock::build_ops_menu ()
1786 using namespace Menu_Helpers;
1787 ops_menu = new Menu;
1788 MenuList& ops_items = ops_menu->items();
1789 ops_menu->set_name ("ArdourContextMenu");
1791 if (!Profile->get_sae()) {
1792 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1794 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1795 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1796 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1798 if (editable && !_off && !is_duration && !_follows_playhead) {
1799 ops_items.push_back (SeparatorElem());
1800 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1801 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1806 AudioClock::set_from_playhead ()
1812 set (_session->transport_frame());
1817 AudioClock::locate ()
1819 if (!_session || is_duration) {
1823 _session->request_locate (current_time(), _session->transport_rolling ());
1827 AudioClock::set_mode (Mode m)
1837 _layout->set_text ("");
1840 _left_layout->set_text ("");
1841 _right_layout->set_text ("");
1846 mode_based_info_ratio = 0.5;
1847 insert_map.push_back (11);
1848 insert_map.push_back (10);
1849 insert_map.push_back (8);
1850 insert_map.push_back (7);
1851 insert_map.push_back (5);
1852 insert_map.push_back (4);
1853 insert_map.push_back (2);
1854 insert_map.push_back (1);
1858 mode_based_info_ratio = 0.5;
1859 insert_map.push_back (11);
1860 insert_map.push_back (10);
1861 insert_map.push_back (9);
1862 insert_map.push_back (8);
1863 insert_map.push_back (6);
1864 insert_map.push_back (5);
1865 insert_map.push_back (3);
1866 insert_map.push_back (2);
1867 insert_map.push_back (1);
1871 mode_based_info_ratio = 1.0;
1872 insert_map.push_back (12);
1873 insert_map.push_back (11);
1874 insert_map.push_back (10);
1875 insert_map.push_back (8);
1876 insert_map.push_back (7);
1877 insert_map.push_back (5);
1878 insert_map.push_back (4);
1879 insert_map.push_back (2);
1880 insert_map.push_back (1);
1884 mode_based_info_ratio = 0.5;
1888 set (last_when, true);
1890 if (!is_transient) {
1891 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1894 if (!_fixed_width) {
1895 /* display is different, allow us to resize */
1901 mode_changed (); /* EMIT SIGNAL (the member one) */
1905 AudioClock::set_bbt_reference (framepos_t pos)
1907 bbt_reference_time = pos;
1911 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1913 CairoWidget::on_style_changed (old_style);
1919 AudioClock::set_editable (bool yn)
1925 AudioClock::set_is_duration (bool yn)
1927 if (yn == is_duration) {
1932 set (last_when, true);
1936 AudioClock::set_off (bool yn)
1944 /* force a redraw. last_when will be preserved, but the clock text will
1948 set (last_when, true);
1952 AudioClock::focus ()
1959 AudioClock::set_corner_radius (double r)
1966 AudioClock::set_fixed_width (bool yn)
1972 AudioClock::dpi_reset ()
1974 /* force recomputation of size even if we are fixed width