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)
82 , mode_based_info_ratio (1.0)
85 , bbt_reference_time (-1)
90 , drag_field (Field (0))
93 set_flags (CAN_FOCUS);
95 _layout = Pango::Layout::create (get_pango_context());
96 _layout->set_attributes (normal_attributes);
99 _left_layout = Pango::Layout::create (get_pango_context());
100 _right_layout = Pango::Layout::create (get_pango_context());
103 set_widget_name (widget_name);
105 _mode = BBT; /* lie to force mode switch */
107 set (last_when, true);
110 clocks.push_back (this);
113 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
114 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
117 AudioClock::~AudioClock ()
119 delete foreground_attr;
124 AudioClock::set_widget_name (const string& str)
129 set_name (str + " clock");
139 AudioClock::on_realize ()
141 CairoWidget::on_realize ();
147 AudioClock::set_font ()
149 Glib::RefPtr<Gtk::Style> style = get_style ();
150 Pango::FontDescription font;
151 Pango::AttrFontDesc* font_attr;
153 if (!is_realized()) {
154 font = get_font_for_style (get_name());
156 font = style->get_font();
159 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
161 normal_attributes.change (*font_attr);
162 editing_attributes.change (*font_attr);
164 /* now a smaller version of the same font */
167 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
168 font.set_weight (Pango::WEIGHT_NORMAL);
169 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
171 info_attributes.change (*font_attr);
177 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
179 CairoWidget::set_active_state (s);
184 AudioClock::set_colors ()
190 uint32_t editing_color;
191 uint32_t cursor_color;
193 if (active_state()) {
194 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
195 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
196 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
197 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
199 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
200 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
201 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
202 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
205 /* store for bg and cursor in render() */
207 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
214 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
221 /* rescale for Pango colors ... sigh */
223 r = lrint (r * 65535.0);
224 g = lrint (g * 65535.0);
225 b = lrint (b * 65535.0);
227 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
228 r = lrint ((r/255.0) * 65535.0);
229 g = lrint ((g/255.0) * 65535.0);
230 b = lrint ((b/255.0) * 65535.0);
231 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
233 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
234 r = lrint ((r/255.0) * 65535.0);
235 g = lrint ((g/255.0) * 65535.0);
236 b = lrint ((b/255.0) * 65535.0);
237 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
239 normal_attributes.change (*foreground_attr);
240 info_attributes.change (*foreground_attr);
241 editing_attributes.change (*foreground_attr);
242 editing_attributes.change (*editing_attr);
245 _layout->set_attributes (normal_attributes);
247 _layout->set_attributes (editing_attributes);
251 _left_layout->set_attributes (info_attributes);
252 _right_layout->set_attributes (info_attributes);
259 AudioClock::render (cairo_t* cr)
261 /* main layout: rounded rect, plus the text */
264 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
266 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
268 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
274 cairo_move_to (cr, layout_x_offset, 0);
276 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
279 pango_cairo_show_layout (cr, _layout->gobj());
283 double h = get_height() - upper_height - separator_height;
286 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
289 if (mode_based_info_ratio != 1.0) {
291 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
295 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
297 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
302 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
303 pango_cairo_show_layout (cr, _left_layout->gobj());
307 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
308 get_width() - separator_height - left_rect_width, h,
311 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
312 get_width() - separator_height - left_rect_width, h);
317 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
318 pango_cairo_show_layout (cr, _right_layout->gobj());
321 /* no info to display, or just one */
325 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
327 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
335 const double cursor_width = 12; /* need em width here, not 16 */
337 if (!insert_map.empty()) {
338 Pango::Rectangle cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
340 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
343 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
345 2.0, cursor.get_height()/PANGO_SCALE);
348 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
349 (upper_height - layout_height)/2.0,
350 2.0, cursor.get_height()/PANGO_SCALE);
354 if (input_string.empty()) {
355 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
364 (upper_height - layout_height)/2.0,
374 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
376 CairoWidget::on_size_allocate (alloc);
379 upper_height = (get_height()/2.0) - 1.0;
381 upper_height = get_height();
385 /* center display in available space */
386 layout_x_offset = (get_width() - layout_width)/2.0;
395 AudioClock::on_size_request (Gtk::Requisition* req)
397 /* even for non fixed width clocks, the size we *ask* for never changes,
398 even though the size we receive might. so once we've computed it,
403 req->width = first_width;
404 req->height = first_height;
408 Glib::RefPtr<Pango::Layout> tmp;
409 Glib::RefPtr<Gtk::Style> style = get_style ();
410 Pango::FontDescription font;
412 tmp = Pango::Layout::create (get_pango_context());
414 if (!is_realized()) {
415 font = get_font_for_style (get_name());
417 font = style->get_font();
420 tmp->set_font_description (font);
423 /* this string is the longest thing we will ever display,
424 and also includes the BBT bar char that may descends below
425 the baseline a bit, and a comma for the minsecs mode
426 where we printf a fractional value (XXX or should)
429 tmp->set_text (" 8888888888:,|");
433 tmp->set_text (" 88:88:88:88");
436 tmp->set_text (" 888|88|8888");
439 tmp->set_text (" 88:88:88,888");
442 tmp->set_text (" 8888888888");
447 tmp->get_pixel_size (req->width, req->height);
449 layout_height = req->height;
450 layout_width = req->width;
452 /* now tackle height, for which we need to know the height of the lower
460 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
461 font.set_weight (Pango::WEIGHT_NORMAL);
462 tmp->set_font_description (font);
464 /* we only care about height, so put as much stuff in here
465 as possible that might change the height.
467 tmp->set_text ("qyhH|"); /* one ascender, one descender */
469 tmp->get_pixel_size (w, info_height);
471 /* silly extra padding that seems necessary to correct the info
472 * that pango just gave us. I have no idea why.
477 req->height += info_height;
478 req->height += separator_height;
481 first_height = req->height;
482 first_width = req->width;
486 AudioClock::show_edit_status (int length)
488 editing_attr->set_start_index (edit_string.length() - length);
489 editing_attr->set_end_index (edit_string.length());
491 editing_attributes.change (*foreground_attr);
492 editing_attributes.change (*editing_attr);
494 _layout->set_attributes (editing_attributes);
498 AudioClock::start_edit ()
500 pre_edit_string = _layout->get_text ();
501 if (!insert_map.empty()) {
502 edit_string = pre_edit_string;
504 edit_string.clear ();
505 _layout->set_text ("");
507 input_string.clear ();
512 Keyboard::magic_widget_grab_focus ();
517 AudioClock::end_edit (bool modify)
525 ok = timecode_validate_edit (edit_string);
529 ok = bbt_validate_edit (edit_string);
540 edit_string = pre_edit_string;
541 input_string.clear ();
542 _layout->set_text (edit_string);
543 show_edit_status (0);
544 /* edit attributes remain in use */
548 framepos_t pos = 0; /* stupid gcc */
552 pos = frames_from_timecode_string (edit_string);
557 pos = frame_duration_from_bbt_string (0, edit_string);
559 pos = frames_from_bbt_string (0, edit_string);
564 pos = frames_from_minsec_string (edit_string);
568 pos = frames_from_audioframes_string (edit_string);
573 _layout->set_attributes (normal_attributes);
574 ValueChanged(); /* EMIT_SIGNAL */
580 _layout->set_attributes (normal_attributes);
581 _layout->set_text (pre_edit_string);
592 AudioClock::drop_focus ()
594 Keyboard::magic_widget_drop_focus ();
598 /* move focus back to the default widget in the top level window */
600 Widget* top = get_toplevel();
602 if (top->is_toplevel ()) {
603 Window* win = dynamic_cast<Window*> (top);
610 AudioClock::parse_as_frames_distance (const std::string& str)
614 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
622 AudioClock::parse_as_minsec_distance (const std::string& str)
624 framecnt_t sr = _session->frame_rate();
630 switch (str.length()) {
637 sscanf (str.c_str(), "%" PRId32, &msecs);
638 return msecs * (sr / 1000);
641 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
642 return (secs * sr) + (msecs * (sr/1000));
645 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
646 return (secs * sr) + (msecs * (sr/1000));
649 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
650 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
653 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
654 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
657 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
658 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
661 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
662 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
672 AudioClock::parse_as_timecode_distance (const std::string& str)
674 double fps = _session->timecode_frames_per_second();
675 framecnt_t sr = _session->frame_rate();
681 switch (str.length()) {
686 sscanf (str.c_str(), "%" PRId32, &frames);
687 return lrint ((frames/(float)fps) * sr);
690 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
691 return (secs * sr) + lrint ((frames/(float)fps) * sr);
694 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
695 return (secs * sr) + lrint ((frames/(float)fps) * sr);
698 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
699 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
702 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
703 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
706 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
707 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
710 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
711 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
721 AudioClock::parse_as_bbt_distance (const std::string& str)
727 AudioClock::parse_as_distance (const std::string& instr)
731 /* the input string is in reverse order */
733 std::reverse (str.begin(), str.end());
737 return parse_as_timecode_distance (str);
740 return parse_as_frames_distance (str);
743 return parse_as_bbt_distance (str);
746 return parse_as_minsec_distance (str);
753 AudioClock::end_edit_relative (bool add)
755 framecnt_t frames = parse_as_distance (input_string);
760 _layout->set_attributes (normal_attributes);
764 set (current_time() + frames, true);
766 framepos_t c = current_time();
769 set (c - frames, true);
774 ValueChanged (); /* EMIT SIGNAL */
777 input_string.clear ();
783 AudioClock::session_configuration_changed (std::string p)
785 if (p == "sync-source" || p == "external-sync") {
786 set (current_time(), true);
790 if (p != "timecode-offset" && p != "timecode-offset-negative") {
799 current = current_duration ();
801 current = current_time ();
811 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
813 if ((!force && !is_visible()) || _session == 0) {
818 when = when - offset;
821 if (when == last_when && !force) {
829 set_timecode (when, force);
833 set_bbt (when, force);
837 set_minsec (when, force);
841 set_frames (when, force);
851 AudioClock::set_frames (framepos_t when, bool /*force*/)
854 bool negative = false;
857 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
860 _left_layout->set_text ("");
861 _right_layout->set_text ("");
873 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
875 snprintf (buf, sizeof (buf), " %10" PRId64, when);
878 _layout->set_text (buf);
881 framecnt_t rate = _session->frame_rate();
883 if (fmod (rate, 100.0) == 0.0) {
884 sprintf (buf, "SR %.1fkHz", rate/1000.0);
886 sprintf (buf, "SR %" PRId64, rate);
889 _left_layout->set_text (buf);
891 float vid_pullup = _session->config.get_video_pullup();
893 if (vid_pullup == 0.0) {
894 _right_layout->set_text (_("pullup: \u2012"));
896 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
897 _right_layout->set_text (buf);
903 AudioClock::set_minsec (framepos_t when, bool force)
911 bool negative = false;
914 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
917 _left_layout->set_text ("");
918 _right_layout->set_text ("");
930 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
931 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
932 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
933 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
934 secs = (int) floor (left / (float) _session->frame_rate());
935 left -= (framecnt_t) floor (secs * _session->frame_rate());
936 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
939 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
941 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
944 _layout->set_text (buf);
948 AudioClock::set_timecode (framepos_t when, bool force)
952 bool negative = false;
955 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
957 _left_layout->set_text ("");
958 _right_layout->set_text ("");
970 _session->timecode_duration (when, TC);
972 _session->timecode_time (when, TC);
975 if (TC.negative || negative) {
976 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
978 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
981 _layout->set_text (buf);
985 if (_session->config.get_external_sync()) {
986 switch (_session->config.get_sync_source()) {
988 _left_layout->set_text ("JACK");
991 _left_layout->set_text ("MTC");
994 _left_layout->set_text ("M-Clock");
998 _left_layout->set_text ("INT");
1001 double timecode_frames = _session->timecode_frames_per_second();
1003 if (fmod(timecode_frames, 1.0) == 0.0) {
1004 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1006 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1009 _right_layout->set_text (buf);
1014 AudioClock::set_bbt (framepos_t when, bool force)
1017 Timecode::BBT_Time BBT;
1018 bool negative = false;
1021 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1023 _left_layout->set_text ("");
1024 _right_layout->set_text ("");
1034 /* handle a common case */
1041 _session->tempo_map().bbt_time (when, BBT);
1046 _session->tempo_map().bbt_time (when, BBT);
1050 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1051 BBT.bars, BBT.beats, BBT.ticks);
1053 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1054 BBT.bars, BBT.beats, BBT.ticks);
1057 _layout->set_text (buf);
1059 if (_right_layout) {
1062 if (bbt_reference_time < 0) {
1065 pos = bbt_reference_time;
1068 TempoMetric m (_session->tempo_map().metric_at (pos));
1070 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1071 _left_layout->set_text (buf);
1073 sprintf (buf, "%g/%g", m.meter().beats_per_bar(), m.meter().note_divisor());
1074 _right_layout->set_text (buf);
1079 AudioClock::set_session (Session *s)
1081 SessionHandlePtr::set_session (s);
1085 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1087 const XMLProperty* prop;
1088 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1089 AudioClock::Mode amode;
1092 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1093 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1095 if ((prop = (*i)->property (X_("mode"))) != 0) {
1096 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1099 if ((prop = (*i)->property (X_("on"))) != 0) {
1100 set_off (!string_is_affirmative (prop->value()));
1107 set (last_when, true);
1112 AudioClock::on_key_press_event (GdkEventKey* ev)
1118 /* return true for keys that we MIGHT use
1121 switch (ev->keyval) {
1144 case GDK_KP_Decimal:
1156 AudioClock::on_key_release_event (GdkEventKey *ev)
1165 switch (ev->keyval) {
1208 case GDK_KP_Subtract:
1209 end_edit_relative (false);
1215 end_edit_relative (true);
1228 ChangeAborted(); /* EMIT SIGNAL */
1233 input_string = input_string.substr (1, input_string.length() - 1);
1234 goto use_input_string;
1240 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1241 /* eat the key event, but do no nothing with it */
1245 input_string.insert (input_string.begin(), new_char);
1249 string::reverse_iterator ri;
1250 vector<int> insert_at;
1251 int highlight_length = 0;
1255 /* merge with pre-edit-string into edit string */
1259 /* get this one in the right order, and to the right width */
1260 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1261 edit_string.push_back (new_char);
1263 edit_string = edit_string.substr (0, edit_string.length() - 1);
1265 if (!edit_string.empty()) {
1266 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1267 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1270 highlight_length = edit_string.length();
1274 edit_string = pre_edit_string;
1276 /* backup through the original string, till we have
1277 * enough digits locations to put all the digits from
1281 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1282 if (isdigit (*ri)) {
1283 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1284 if (insert_at.size() == input_string.length()) {
1290 if (insert_at.size() != input_string.length()) {
1291 error << "something went wrong " << endmsg;
1293 for (int i = input_string.length() - 1; i >= 0; --i) {
1294 edit_string[insert_at[i]] = input_string[i];
1297 highlight_length = edit_string.length() - insert_at.back();
1303 if (edit_string != _layout->get_text()) {
1304 show_edit_status (highlight_length);
1305 _layout->set_text (edit_string);
1313 AudioClock::index_to_field (int index) const
1318 return Timecode_Hours;
1319 } else if (index < 7) {
1320 return Timecode_Minutes;
1321 } else if (index < 10) {
1322 return Timecode_Seconds;
1324 return Timecode_Frames;
1330 } else if (index < 7) {
1338 return Timecode_Hours;
1339 } else if (index < 6) {
1341 } else if (index < 9) {
1344 return MS_Milliseconds;
1356 AudioClock::on_button_press_event (GdkEventButton *ev)
1358 switch (ev->button) {
1360 if (editable && !_off) {
1362 /* make absolutely sure that the pointer is grabbed */
1363 gdk_pointer_grab(ev->window,false ,
1364 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1365 NULL,NULL,ev->time);
1367 drag_start_y = ev->y;
1375 /* the text has been centered vertically, so adjust
1379 y = ev->y - ((upper_height - layout_height)/2);
1380 x = ev->x - layout_x_offset;
1382 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1383 drag_field = index_to_field (index);
1385 drag_field = Field (0);
1399 AudioClock::on_button_release_event (GdkEventButton *ev)
1401 if (editable && !_off) {
1403 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1405 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1406 // we actually dragged so return without
1407 // setting editing focus, or we shift clicked
1410 if (ev->button == 1) {
1418 if (Keyboard::is_context_menu_event (ev)) {
1419 if (ops_menu == 0) {
1422 ops_menu->popup (1, ev->time);
1430 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1432 bool ret = CairoWidget::on_focus_out_event (ev);
1442 AudioClock::on_scroll_event (GdkEventScroll *ev)
1447 if (editing || _session == 0 || !editable || _off) {
1454 /* the text has been centered vertically, so adjust
1458 y = ev->y - ((upper_height - layout_height)/2);
1459 x = ev->x - layout_x_offset;
1461 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1462 /* not in the main layout */
1466 Field f = index_to_field (index);
1467 framepos_t frames = 0;
1469 switch (ev->direction) {
1472 frames = get_frame_step (f);
1474 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1477 set (current_time() + frames, true);
1478 ValueChanged (); /* EMIT_SIGNAL */
1482 case GDK_SCROLL_DOWN:
1483 frames = get_frame_step (f);
1485 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1489 if ((double)current_time() - (double)frames < 0.0) {
1492 set (current_time() - frames, true);
1495 ValueChanged (); /* EMIT_SIGNAL */
1508 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1510 if (editing || _session == 0 || !dragging) {
1514 float pixel_frame_scale_factor = 0.2f;
1516 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1517 pixel_frame_scale_factor = 0.1f;
1521 if (Keyboard::modifier_state_contains (ev->state,
1522 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1524 pixel_frame_scale_factor = 0.025f;
1527 double y_delta = ev->y - drag_y;
1529 drag_accum += y_delta*pixel_frame_scale_factor;
1533 if (trunc (drag_accum) != 0) {
1538 dir = (drag_accum < 0 ? 1:-1);
1539 pos = current_time();
1540 frames = get_frame_step (drag_field,pos,dir);
1542 if (frames != 0 && frames * drag_accum < current_time()) {
1543 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1549 ValueChanged(); /* EMIT_SIGNAL */
1556 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1559 Timecode::BBT_Time BBT;
1561 case Timecode_Hours:
1562 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1564 case Timecode_Minutes:
1565 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1567 case Timecode_Seconds:
1568 f = _session->frame_rate();
1570 case Timecode_Frames:
1571 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1579 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1582 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1585 f = (framecnt_t) _session->frame_rate();
1587 case MS_Milliseconds:
1588 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1595 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1601 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1607 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1610 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1619 AudioClock::current_time (framepos_t pos) const
1625 AudioClock::current_duration (framepos_t pos) const
1634 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1650 AudioClock::bbt_validate_edit (const string& str)
1654 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1658 if (!is_duration && any.bbt.bars == 0) {
1662 if (!is_duration && any.bbt.beats == 0) {
1670 AudioClock::timecode_validate_edit (const string& str)
1674 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1675 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1679 if (TC.minutes > 59 || TC.seconds > 59) {
1683 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1687 if (_session->timecode_drop_frames()) {
1688 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1697 AudioClock::frames_from_timecode_string (const string& str) const
1699 if (_session == 0) {
1706 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1707 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1711 TC.rate = _session->timecode_frames_per_second();
1712 TC.drop= _session->timecode_drop_frames();
1714 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1716 // timecode_tester ();
1722 AudioClock::frames_from_minsec_string (const string& str) const
1724 if (_session == 0) {
1728 int hrs, mins, secs, millisecs;
1729 framecnt_t sr = _session->frame_rate();
1731 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1732 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1736 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1740 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1742 if (_session == 0) {
1743 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1748 any.type = AnyTime::BBT;
1750 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1757 return _session->any_duration_to_frames (pos, any);
1759 return _session->convert_to_frames (any);
1765 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1767 if (_session == 0) {
1768 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1772 Timecode::BBT_Time bbt;
1774 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 0) {
1778 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1782 AudioClock::frames_from_audioframes_string (const string& str) const
1785 sscanf (str.c_str(), "%" PRId64, &f);
1790 AudioClock::build_ops_menu ()
1792 using namespace Menu_Helpers;
1793 ops_menu = new Menu;
1794 MenuList& ops_items = ops_menu->items();
1795 ops_menu->set_name ("ArdourContextMenu");
1797 if (!Profile->get_sae()) {
1798 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1800 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1801 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1802 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1804 if (editable && !_off && !is_duration && !_follows_playhead) {
1805 ops_items.push_back (SeparatorElem());
1806 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1807 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1812 AudioClock::set_from_playhead ()
1818 set (_session->transport_frame());
1823 AudioClock::locate ()
1825 if (!_session || is_duration) {
1829 _session->request_locate (current_time(), _session->transport_rolling ());
1833 AudioClock::set_mode (Mode m)
1843 _layout->set_text ("");
1846 _left_layout->set_text ("");
1847 _right_layout->set_text ("");
1852 mode_based_info_ratio = 0.5;
1853 insert_map.push_back (11);
1854 insert_map.push_back (10);
1855 insert_map.push_back (8);
1856 insert_map.push_back (7);
1857 insert_map.push_back (5);
1858 insert_map.push_back (4);
1859 insert_map.push_back (2);
1860 insert_map.push_back (1);
1864 mode_based_info_ratio = 0.5;
1865 insert_map.push_back (11);
1866 insert_map.push_back (10);
1867 insert_map.push_back (9);
1868 insert_map.push_back (8);
1869 insert_map.push_back (6);
1870 insert_map.push_back (5);
1871 insert_map.push_back (3);
1872 insert_map.push_back (2);
1873 insert_map.push_back (1);
1877 mode_based_info_ratio = 1.0;
1878 insert_map.push_back (12);
1879 insert_map.push_back (11);
1880 insert_map.push_back (10);
1881 insert_map.push_back (8);
1882 insert_map.push_back (7);
1883 insert_map.push_back (5);
1884 insert_map.push_back (4);
1885 insert_map.push_back (2);
1886 insert_map.push_back (1);
1890 mode_based_info_ratio = 0.5;
1894 set (last_when, true);
1896 if (!is_transient) {
1897 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1900 if (!_fixed_width) {
1901 /* display is different, allow us to resize */
1907 mode_changed (); /* EMIT SIGNAL (the member one) */
1911 AudioClock::set_bbt_reference (framepos_t pos)
1913 bbt_reference_time = pos;
1917 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1919 CairoWidget::on_style_changed (old_style);
1925 AudioClock::set_editable (bool yn)
1931 AudioClock::set_is_duration (bool yn)
1933 if (yn == is_duration) {
1938 set (last_when, true);
1942 AudioClock::set_off (bool yn)
1950 /* force a redraw. last_when will be preserved, but the clock text will
1954 set (last_when, true);
1958 AudioClock::focus ()
1964 AudioClock::set_corner_radius (double r)
1971 AudioClock::set_fixed_width (bool yn)
1977 AudioClock::dpi_reset ()
1979 /* force recomputation of size even if we are fixed width