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()) {
340 if (input_string.length() < insert_map.size()) {
341 Pango::Rectangle cursor;
343 if (input_string.empty()) {
344 /* nothing entered yet, put cursor at the end
347 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
349 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
352 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
355 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
357 2.0, cursor.get_height()/PANGO_SCALE);
360 layout_x_offset + cursor.get_x()/PANGO_SCALE + cursor_width,
361 (upper_height - layout_height)/2.0,
362 2.0, cursor.get_height()/PANGO_SCALE);
366 /* we've entered all possible digits, no cursor */
370 if (input_string.empty()) {
371 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
380 (upper_height - layout_height)/2.0,
390 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
392 CairoWidget::on_size_allocate (alloc);
395 upper_height = (get_height()/2.0) - 1.0;
397 upper_height = get_height();
401 /* center display in available space */
402 layout_x_offset = (get_width() - layout_width)/2.0;
411 AudioClock::on_size_request (Gtk::Requisition* req)
413 /* even for non fixed width clocks, the size we *ask* for never changes,
414 even though the size we receive might. so once we've computed it,
419 req->width = first_width;
420 req->height = first_height;
424 Glib::RefPtr<Pango::Layout> tmp;
425 Glib::RefPtr<Gtk::Style> style = get_style ();
426 Pango::FontDescription font;
428 tmp = Pango::Layout::create (get_pango_context());
430 if (!is_realized()) {
431 font = get_font_for_style (get_name());
433 font = style->get_font();
436 tmp->set_font_description (font);
439 /* this string is the longest thing we will ever display,
440 and also includes the BBT bar char that may descends below
441 the baseline a bit, and a comma for the minsecs mode
442 where we printf a fractional value (XXX or should)
445 tmp->set_text (" 8888888888:,|");
449 tmp->set_text (" 88:88:88:88");
452 tmp->set_text (" 888|88|8888");
455 tmp->set_text (" 88:88:88,888");
458 tmp->set_text (" 8888888888");
463 tmp->get_pixel_size (req->width, req->height);
465 layout_height = req->height;
466 layout_width = req->width;
468 /* now tackle height, for which we need to know the height of the lower
476 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
477 font.set_weight (Pango::WEIGHT_NORMAL);
478 tmp->set_font_description (font);
480 /* we only care about height, so put as much stuff in here
481 as possible that might change the height.
483 tmp->set_text ("qyhH|"); /* one ascender, one descender */
485 tmp->get_pixel_size (w, info_height);
487 /* silly extra padding that seems necessary to correct the info
488 * that pango just gave us. I have no idea why.
493 req->height += info_height;
494 req->height += separator_height;
497 first_height = req->height;
498 first_width = req->width;
502 AudioClock::show_edit_status (int length)
504 editing_attr->set_start_index (edit_string.length() - length);
505 editing_attr->set_end_index (edit_string.length());
507 editing_attributes.change (*foreground_attr);
508 editing_attributes.change (*editing_attr);
510 _layout->set_attributes (editing_attributes);
514 AudioClock::start_edit ()
516 pre_edit_string = _layout->get_text ();
517 if (!insert_map.empty()) {
518 edit_string = pre_edit_string;
520 edit_string.clear ();
521 _layout->set_text ("");
523 input_string.clear ();
528 Keyboard::magic_widget_grab_focus ();
533 AudioClock::end_edit (bool modify)
541 ok = timecode_validate_edit (edit_string);
545 ok = bbt_validate_edit (edit_string);
556 edit_string = pre_edit_string;
557 input_string.clear ();
558 _layout->set_text (edit_string);
559 show_edit_status (0);
560 /* edit attributes remain in use */
564 framepos_t pos = 0; /* stupid gcc */
568 pos = frames_from_timecode_string (edit_string);
573 pos = frame_duration_from_bbt_string (0, edit_string);
575 pos = frames_from_bbt_string (0, edit_string);
580 pos = frames_from_minsec_string (edit_string);
584 pos = frames_from_audioframes_string (edit_string);
589 _layout->set_attributes (normal_attributes);
590 ValueChanged(); /* EMIT_SIGNAL */
596 _layout->set_attributes (normal_attributes);
597 _layout->set_text (pre_edit_string);
608 AudioClock::drop_focus ()
610 Keyboard::magic_widget_drop_focus ();
614 /* move focus back to the default widget in the top level window */
616 Widget* top = get_toplevel();
618 if (top->is_toplevel ()) {
619 Window* win = dynamic_cast<Window*> (top);
626 AudioClock::parse_as_frames_distance (const std::string& str)
630 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
638 AudioClock::parse_as_minsec_distance (const std::string& str)
640 framecnt_t sr = _session->frame_rate();
646 switch (str.length()) {
653 sscanf (str.c_str(), "%" PRId32, &msecs);
654 return msecs * (sr / 1000);
657 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
658 return (secs * sr) + (msecs * (sr/1000));
661 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
662 return (secs * sr) + (msecs * (sr/1000));
665 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
666 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
669 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
670 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
673 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
674 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
677 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
678 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
688 AudioClock::parse_as_timecode_distance (const std::string& str)
690 double fps = _session->timecode_frames_per_second();
691 framecnt_t sr = _session->frame_rate();
697 switch (str.length()) {
702 sscanf (str.c_str(), "%" PRId32, &frames);
703 return lrint ((frames/(float)fps) * sr);
706 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
707 return (secs * sr) + lrint ((frames/(float)fps) * sr);
710 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
711 return (secs * sr) + lrint ((frames/(float)fps) * sr);
714 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
715 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
718 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
719 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
722 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
723 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
726 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
727 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
737 AudioClock::parse_as_bbt_distance (const std::string& str)
743 AudioClock::parse_as_distance (const std::string& instr)
747 return parse_as_timecode_distance (instr);
750 return parse_as_frames_distance (instr);
753 return parse_as_bbt_distance (instr);
756 return parse_as_minsec_distance (instr);
763 AudioClock::end_edit_relative (bool add)
765 framecnt_t frames = parse_as_distance (input_string);
770 _layout->set_attributes (normal_attributes);
774 set (current_time() + frames, true);
776 framepos_t c = current_time();
779 set (c - frames, true);
784 ValueChanged (); /* EMIT SIGNAL */
787 input_string.clear ();
793 AudioClock::session_configuration_changed (std::string p)
795 if (p == "sync-source" || p == "external-sync") {
796 set (current_time(), true);
800 if (p != "timecode-offset" && p != "timecode-offset-negative") {
809 current = current_duration ();
811 current = current_time ();
821 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
823 if ((!force && !is_visible()) || _session == 0) {
828 when = when - offset;
831 if (when == last_when && !force) {
839 set_timecode (when, force);
843 set_bbt (when, force);
847 set_minsec (when, force);
851 set_frames (when, force);
861 AudioClock::set_frames (framepos_t when, bool /*force*/)
864 bool negative = false;
867 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
870 _left_layout->set_text ("");
871 _right_layout->set_text ("");
883 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
885 snprintf (buf, sizeof (buf), " %10" PRId64, when);
888 _layout->set_text (buf);
891 framecnt_t rate = _session->frame_rate();
893 if (fmod (rate, 100.0) == 0.0) {
894 sprintf (buf, "SR %.1fkHz", rate/1000.0);
896 sprintf (buf, "SR %" PRId64, rate);
899 _left_layout->set_text (buf);
901 float vid_pullup = _session->config.get_video_pullup();
903 if (vid_pullup == 0.0) {
904 _right_layout->set_text (_("pullup: \u2012"));
906 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
907 _right_layout->set_text (buf);
913 AudioClock::set_minsec (framepos_t when, bool force)
921 bool negative = false;
924 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
927 _left_layout->set_text ("");
928 _right_layout->set_text ("");
940 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
941 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
942 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
943 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
944 secs = (int) floor (left / (float) _session->frame_rate());
945 left -= (framecnt_t) floor (secs * _session->frame_rate());
946 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
949 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
951 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
954 _layout->set_text (buf);
958 AudioClock::set_timecode (framepos_t when, bool force)
962 bool negative = false;
965 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
967 _left_layout->set_text ("");
968 _right_layout->set_text ("");
980 _session->timecode_duration (when, TC);
982 _session->timecode_time (when, TC);
985 if (TC.negative || negative) {
986 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
988 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
991 _layout->set_text (buf);
995 if (_session->config.get_external_sync()) {
996 switch (_session->config.get_sync_source()) {
998 _left_layout->set_text ("JACK");
1001 _left_layout->set_text ("MTC");
1004 _left_layout->set_text ("M-Clock");
1008 _left_layout->set_text ("INT");
1011 double timecode_frames = _session->timecode_frames_per_second();
1013 if (fmod(timecode_frames, 1.0) == 0.0) {
1014 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1016 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1019 _right_layout->set_text (buf);
1024 AudioClock::set_bbt (framepos_t when, bool force)
1027 Timecode::BBT_Time BBT;
1028 bool negative = false;
1031 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1033 _left_layout->set_text ("");
1034 _right_layout->set_text ("");
1044 /* handle a common case */
1051 _session->tempo_map().bbt_time (when, BBT);
1056 _session->tempo_map().bbt_time (when, BBT);
1060 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1061 BBT.bars, BBT.beats, BBT.ticks);
1063 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1064 BBT.bars, BBT.beats, BBT.ticks);
1067 _layout->set_text (buf);
1069 if (_right_layout) {
1072 if (bbt_reference_time < 0) {
1075 pos = bbt_reference_time;
1078 TempoMetric m (_session->tempo_map().metric_at (pos));
1080 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1081 _left_layout->set_text (buf);
1083 sprintf (buf, "%g/%g", m.meter().beats_per_bar(), m.meter().note_divisor());
1084 _right_layout->set_text (buf);
1089 AudioClock::set_session (Session *s)
1091 SessionHandlePtr::set_session (s);
1095 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1097 const XMLProperty* prop;
1098 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1099 AudioClock::Mode amode;
1102 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1103 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1105 if ((prop = (*i)->property (X_("mode"))) != 0) {
1106 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1109 if ((prop = (*i)->property (X_("on"))) != 0) {
1110 set_off (!string_is_affirmative (prop->value()));
1117 set (last_when, true);
1122 AudioClock::on_key_press_event (GdkEventKey* ev)
1131 switch (ev->keyval) {
1174 case GDK_KP_Subtract:
1175 end_edit_relative (false);
1181 end_edit_relative (true);
1194 ChangeAborted(); /* EMIT SIGNAL */
1199 if (!input_string.empty()) {
1200 /* delete the last key entered
1202 input_string = input_string.substr (0, input_string.length() - 1);
1204 goto use_input_string;
1210 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1211 /* too many digits: eat the key event, but do nothing with it */
1215 input_string.push_back (new_char);
1219 int highlight_length = 0;
1222 /* merge with pre-edit-string into edit string */
1226 /* get this one in the right order, and to the right width */
1227 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1228 edit_string.push_back (new_char);
1230 edit_string = edit_string.substr (0, edit_string.length() - 1);
1232 if (!edit_string.empty()) {
1234 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1235 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1238 /* highlight the whole thing */
1239 highlight_length = edit_string.length();
1243 edit_string = pre_edit_string;
1245 if (input_string.empty()) {
1246 highlight_length = 0;
1248 // for (int i = input_string.length() - 1; i >= 0; --i) {
1249 string::size_type target;
1250 for (string::size_type i = 0; i < input_string.length(); ++i) {
1251 target = insert_map[input_string.length() - 1 - i];
1252 edit_string[target] = input_string[i];
1254 /* highlight from end to wherever the last character was added */
1255 highlight_length = edit_string.length() - insert_map[input_string.length()-1];
1261 show_edit_status (highlight_length);
1262 _layout->set_text (edit_string);
1270 AudioClock::on_key_release_event (GdkEventKey *ev)
1276 /* return true for keys that we used on press
1277 so that they cannot possibly do double-duty
1279 switch (ev->keyval) {
1302 case GDK_KP_Decimal:
1310 case GDK_KP_Subtract:
1318 AudioClock::index_to_field (int index) const
1323 return Timecode_Hours;
1324 } else if (index < 7) {
1325 return Timecode_Minutes;
1326 } else if (index < 10) {
1327 return Timecode_Seconds;
1329 return Timecode_Frames;
1335 } else if (index < 7) {
1343 return Timecode_Hours;
1344 } else if (index < 6) {
1346 } else if (index < 9) {
1349 return MS_Milliseconds;
1361 AudioClock::on_button_press_event (GdkEventButton *ev)
1363 switch (ev->button) {
1365 if (editable && !_off) {
1367 /* make absolutely sure that the pointer is grabbed */
1368 gdk_pointer_grab(ev->window,false ,
1369 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1370 NULL,NULL,ev->time);
1372 drag_start_y = ev->y;
1380 /* the text has been centered vertically, so adjust
1384 y = ev->y - ((upper_height - layout_height)/2);
1385 x = ev->x - layout_x_offset;
1387 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1388 drag_field = index_to_field (index);
1390 drag_field = Field (0);
1404 AudioClock::on_button_release_event (GdkEventButton *ev)
1406 if (editable && !_off) {
1408 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1410 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1411 // we actually dragged so return without
1412 // setting editing focus, or we shift clicked
1415 if (ev->button == 1) {
1423 if (Keyboard::is_context_menu_event (ev)) {
1424 if (ops_menu == 0) {
1427 ops_menu->popup (1, ev->time);
1435 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1437 bool ret = CairoWidget::on_focus_out_event (ev);
1447 AudioClock::on_scroll_event (GdkEventScroll *ev)
1452 if (editing || _session == 0 || !editable || _off) {
1459 /* the text has been centered vertically, so adjust
1463 y = ev->y - ((upper_height - layout_height)/2);
1464 x = ev->x - layout_x_offset;
1466 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1467 /* not in the main layout */
1471 Field f = index_to_field (index);
1472 framepos_t frames = 0;
1474 switch (ev->direction) {
1477 frames = get_frame_step (f);
1479 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1482 set (current_time() + frames, true);
1483 ValueChanged (); /* EMIT_SIGNAL */
1487 case GDK_SCROLL_DOWN:
1488 frames = get_frame_step (f);
1490 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1494 if ((double)current_time() - (double)frames < 0.0) {
1497 set (current_time() - frames, true);
1500 ValueChanged (); /* EMIT_SIGNAL */
1513 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1515 if (editing || _session == 0 || !dragging) {
1519 float pixel_frame_scale_factor = 0.2f;
1521 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1522 pixel_frame_scale_factor = 0.1f;
1526 if (Keyboard::modifier_state_contains (ev->state,
1527 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1529 pixel_frame_scale_factor = 0.025f;
1532 double y_delta = ev->y - drag_y;
1534 drag_accum += y_delta*pixel_frame_scale_factor;
1538 if (trunc (drag_accum) != 0) {
1543 dir = (drag_accum < 0 ? 1:-1);
1544 pos = current_time();
1545 frames = get_frame_step (drag_field,pos,dir);
1547 if (frames != 0 && frames * drag_accum < current_time()) {
1548 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1554 ValueChanged(); /* EMIT_SIGNAL */
1561 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1564 Timecode::BBT_Time BBT;
1566 case Timecode_Hours:
1567 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1569 case Timecode_Minutes:
1570 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1572 case Timecode_Seconds:
1573 f = _session->frame_rate();
1575 case Timecode_Frames:
1576 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1584 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1587 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1590 f = (framecnt_t) _session->frame_rate();
1592 case MS_Milliseconds:
1593 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1600 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1606 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1612 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1615 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1624 AudioClock::current_time (framepos_t pos) const
1630 AudioClock::current_duration (framepos_t pos) const
1639 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1655 AudioClock::bbt_validate_edit (const string& str)
1659 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1663 if (!is_duration && any.bbt.bars == 0) {
1667 if (!is_duration && any.bbt.beats == 0) {
1675 AudioClock::timecode_validate_edit (const string& str)
1679 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1680 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1684 if (TC.minutes > 59U || TC.seconds > 59U) {
1688 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1692 if (_session->timecode_drop_frames()) {
1693 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1702 AudioClock::frames_from_timecode_string (const string& str) const
1704 if (_session == 0) {
1711 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1712 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1716 TC.rate = _session->timecode_frames_per_second();
1717 TC.drop= _session->timecode_drop_frames();
1719 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1721 // timecode_tester ();
1727 AudioClock::frames_from_minsec_string (const string& str) const
1729 if (_session == 0) {
1733 int hrs, mins, secs, millisecs;
1734 framecnt_t sr = _session->frame_rate();
1736 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1737 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1741 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1745 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1747 if (_session == 0) {
1748 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1753 any.type = AnyTime::BBT;
1755 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1762 return _session->any_duration_to_frames (pos, any);
1764 return _session->convert_to_frames (any);
1770 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1772 if (_session == 0) {
1773 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1777 Timecode::BBT_Time bbt;
1779 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 0) {
1783 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1787 AudioClock::frames_from_audioframes_string (const string& str) const
1790 sscanf (str.c_str(), "%" PRId64, &f);
1795 AudioClock::build_ops_menu ()
1797 using namespace Menu_Helpers;
1798 ops_menu = new Menu;
1799 MenuList& ops_items = ops_menu->items();
1800 ops_menu->set_name ("ArdourContextMenu");
1802 if (!Profile->get_sae()) {
1803 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1805 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1806 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1807 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1809 if (editable && !_off && !is_duration && !_follows_playhead) {
1810 ops_items.push_back (SeparatorElem());
1811 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1812 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1817 AudioClock::set_from_playhead ()
1823 set (_session->transport_frame());
1828 AudioClock::locate ()
1830 if (!_session || is_duration) {
1834 _session->request_locate (current_time(), _session->transport_rolling ());
1838 AudioClock::set_mode (Mode m)
1848 _layout->set_text ("");
1851 _left_layout->set_text ("");
1852 _right_layout->set_text ("");
1857 mode_based_info_ratio = 0.5;
1858 insert_map.push_back (11);
1859 insert_map.push_back (10);
1860 insert_map.push_back (8);
1861 insert_map.push_back (7);
1862 insert_map.push_back (5);
1863 insert_map.push_back (4);
1864 insert_map.push_back (2);
1865 insert_map.push_back (1);
1869 mode_based_info_ratio = 0.5;
1870 insert_map.push_back (11);
1871 insert_map.push_back (10);
1872 insert_map.push_back (9);
1873 insert_map.push_back (8);
1874 insert_map.push_back (6);
1875 insert_map.push_back (5);
1876 insert_map.push_back (3);
1877 insert_map.push_back (2);
1878 insert_map.push_back (1);
1882 mode_based_info_ratio = 1.0;
1883 insert_map.push_back (12);
1884 insert_map.push_back (11);
1885 insert_map.push_back (10);
1886 insert_map.push_back (8);
1887 insert_map.push_back (7);
1888 insert_map.push_back (5);
1889 insert_map.push_back (4);
1890 insert_map.push_back (2);
1891 insert_map.push_back (1);
1895 mode_based_info_ratio = 0.5;
1899 set (last_when, true);
1901 if (!is_transient) {
1902 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1905 if (!_fixed_width) {
1906 /* display is different, allow us to resize */
1912 mode_changed (); /* EMIT SIGNAL (the member one) */
1916 AudioClock::set_bbt_reference (framepos_t pos)
1918 bbt_reference_time = pos;
1922 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1924 CairoWidget::on_style_changed (old_style);
1930 AudioClock::set_editable (bool yn)
1936 AudioClock::set_is_duration (bool yn)
1938 if (yn == is_duration) {
1943 set (last_when, true);
1947 AudioClock::set_off (bool yn)
1955 /* force a redraw. last_when will be preserved, but the clock text will
1959 set (last_when, true);
1963 AudioClock::focus ()
1969 AudioClock::set_corner_radius (double r)
1976 AudioClock::set_fixed_width (bool yn)
1982 AudioClock::dpi_reset ()
1984 /* force recomputation of size even if we are fixed width