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)
83 , mode_based_info_ratio (1.0)
86 , bbt_reference_time (-1)
91 , drag_field (Field (0))
94 set_flags (CAN_FOCUS);
96 _layout = Pango::Layout::create (get_pango_context());
97 _layout->set_attributes (normal_attributes);
100 _left_layout = Pango::Layout::create (get_pango_context());
101 _right_layout = Pango::Layout::create (get_pango_context());
104 set_widget_name (widget_name);
106 _mode = BBT; /* lie to force mode switch */
108 set (last_when, true);
111 clocks.push_back (this);
114 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
115 DPIReset.connect (sigc::mem_fun (*this, &AudioClock::dpi_reset));
118 AudioClock::~AudioClock ()
120 delete foreground_attr;
125 AudioClock::set_widget_name (const string& str)
130 set_name (str + " clock");
140 AudioClock::on_realize ()
142 CairoWidget::on_realize ();
148 AudioClock::set_font ()
150 Glib::RefPtr<Gtk::Style> style = get_style ();
151 Pango::FontDescription font;
152 Pango::AttrFontDesc* font_attr;
154 if (!is_realized()) {
155 font = get_font_for_style (get_name());
157 font = style->get_font();
160 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
162 normal_attributes.change (*font_attr);
163 editing_attributes.change (*font_attr);
165 /* now a smaller version of the same font */
168 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
169 font.set_weight (Pango::WEIGHT_NORMAL);
170 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
172 info_attributes.change (*font_attr);
176 /* get the figure width for the font. This doesn't have to super
177 * accurate since we only use it to measure the (roughly 1 character)
178 * offset from the position Pango tells us for the "cursor"
181 Glib::RefPtr<Pango::Layout> tmp = Pango::Layout::create (get_pango_context());
185 tmp->get_pixel_size (em_width, ignore_height);
189 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
191 CairoWidget::set_active_state (s);
196 AudioClock::set_colors ()
202 uint32_t editing_color;
203 uint32_t cursor_color;
205 if (active_state()) {
206 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
207 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
208 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
209 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: cursor", get_name()));
211 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
212 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
213 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
214 cursor_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: cursor", get_name()));
217 /* store for bg and cursor in render() */
219 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
226 UINT_TO_RGBA (cursor_color, &r, &g, &b, &a);
233 /* rescale for Pango colors ... sigh */
235 r = lrint (r * 65535.0);
236 g = lrint (g * 65535.0);
237 b = lrint (b * 65535.0);
239 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
240 r = lrint ((r/255.0) * 65535.0);
241 g = lrint ((g/255.0) * 65535.0);
242 b = lrint ((b/255.0) * 65535.0);
243 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
245 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
246 r = lrint ((r/255.0) * 65535.0);
247 g = lrint ((g/255.0) * 65535.0);
248 b = lrint ((b/255.0) * 65535.0);
249 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
251 normal_attributes.change (*foreground_attr);
252 info_attributes.change (*foreground_attr);
253 editing_attributes.change (*foreground_attr);
254 editing_attributes.change (*editing_attr);
257 _layout->set_attributes (normal_attributes);
259 _layout->set_attributes (editing_attributes);
263 _left_layout->set_attributes (info_attributes);
264 _right_layout->set_attributes (info_attributes);
271 AudioClock::render (cairo_t* cr)
273 /* main layout: rounded rect, plus the text */
276 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
278 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, corner_radius);
280 cairo_rectangle (cr, 0, 0, get_width(), upper_height);
286 cairo_move_to (cr, layout_x_offset, 0);
288 cairo_move_to (cr, layout_x_offset, (upper_height - layout_height) / 2.0);
291 pango_cairo_show_layout (cr, _layout->gobj());
295 double h = get_height() - upper_height - separator_height;
298 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
301 if (mode_based_info_ratio != 1.0) {
303 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
307 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, corner_radius);
309 cairo_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h);
314 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
315 pango_cairo_show_layout (cr, _left_layout->gobj());
319 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
320 get_width() - separator_height - left_rect_width, h,
323 cairo_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
324 get_width() - separator_height - left_rect_width, h);
329 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
330 pango_cairo_show_layout (cr, _right_layout->gobj());
333 /* no info to display, or just one */
337 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, corner_radius);
339 cairo_rectangle (cr, 0, upper_height + separator_height, get_width(), h);
347 if (!insert_map.empty()) {
350 if (input_string.length() < insert_map.size()) {
351 Pango::Rectangle cursor;
353 if (input_string.empty()) {
354 /* nothing entered yet, put cursor at the end
357 cursor = _layout->get_cursor_strong_pos (edit_string.length() - 1);
359 cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
362 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
365 min (get_width() - 2.0, (double) cursor.get_x()/PANGO_SCALE + em_width), 0,
366 2.0, cursor.get_height()/PANGO_SCALE);
369 min (get_width() - 2.0, (double) layout_x_offset + cursor.get_x()/PANGO_SCALE + em_width),
370 (upper_height - layout_height)/2.0,
371 2.0, cursor.get_height()/PANGO_SCALE);
375 /* we've entered all possible digits, no cursor */
379 if (input_string.empty()) {
380 cairo_set_source_rgba (cr, cursor_r, cursor_g, cursor_b, cursor_a);
389 (upper_height - layout_height)/2.0,
399 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
401 CairoWidget::on_size_allocate (alloc);
404 upper_height = (get_height()/2.0) - 1.0;
406 upper_height = get_height();
410 /* center display in available space */
411 layout_x_offset = (get_width() - layout_width)/2.0;
419 AudioClock::on_size_request (Gtk::Requisition* req)
421 /* even for non fixed width clocks, the size we *ask* for never changes,
422 even though the size we receive might. so once we've computed it,
427 req->width = first_width;
428 req->height = first_height;
432 Glib::RefPtr<Pango::Layout> tmp;
433 Glib::RefPtr<Gtk::Style> style = get_style ();
434 Pango::FontDescription font;
436 tmp = Pango::Layout::create (get_pango_context());
438 if (!is_realized()) {
439 font = get_font_for_style (get_name());
441 font = style->get_font();
444 tmp->set_font_description (font);
447 /* this string is the longest thing we will ever display,
448 and also includes the BBT bar char that may descends below
449 the baseline a bit, and a comma for the minsecs mode
450 where we printf a fractional value (XXX or should)
453 tmp->set_text (" 8888888888:,|");
457 tmp->set_text (" 88:88:88:88");
460 tmp->set_text (" 888|88|8888");
463 tmp->set_text (" 88:88:88,888");
466 tmp->set_text (" 8888888888");
471 tmp->get_pixel_size (req->width, req->height);
473 layout_height = req->height;
474 layout_width = req->width;
476 /* now tackle height, for which we need to know the height of the lower
484 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
485 font.set_weight (Pango::WEIGHT_NORMAL);
486 tmp->set_font_description (font);
488 /* we only care about height, so put as much stuff in here
489 as possible that might change the height.
491 tmp->set_text ("qyhH|"); /* one ascender, one descender */
493 tmp->get_pixel_size (w, info_height);
495 /* silly extra padding that seems necessary to correct the info
496 * that pango just gave us. I have no idea why.
501 req->height += info_height;
502 req->height += separator_height;
505 first_height = req->height;
506 first_width = req->width;
510 AudioClock::show_edit_status (int length)
512 editing_attr->set_start_index (edit_string.length() - length);
513 editing_attr->set_end_index (edit_string.length());
515 editing_attributes.change (*foreground_attr);
516 editing_attributes.change (*editing_attr);
518 _layout->set_attributes (editing_attributes);
522 AudioClock::start_edit ()
524 pre_edit_string = _layout->get_text ();
525 if (!insert_map.empty()) {
526 edit_string = pre_edit_string;
528 edit_string.clear ();
529 _layout->set_text ("");
531 input_string.clear ();
536 Keyboard::magic_widget_grab_focus ();
541 AudioClock::end_edit (bool modify)
549 ok = timecode_validate_edit (edit_string);
553 ok = bbt_validate_edit (edit_string);
564 edit_string = pre_edit_string;
565 input_string.clear ();
566 _layout->set_text (edit_string);
567 show_edit_status (0);
568 /* edit attributes remain in use */
572 framepos_t pos = 0; /* stupid gcc */
576 pos = frames_from_timecode_string (edit_string);
581 pos = frame_duration_from_bbt_string (0, edit_string);
583 pos = frames_from_bbt_string (0, edit_string);
588 pos = frames_from_minsec_string (edit_string);
592 pos = frames_from_audioframes_string (edit_string);
597 _layout->set_attributes (normal_attributes);
598 ValueChanged(); /* EMIT_SIGNAL */
604 _layout->set_attributes (normal_attributes);
605 _layout->set_text (pre_edit_string);
616 AudioClock::drop_focus ()
618 Keyboard::magic_widget_drop_focus ();
622 /* move focus back to the default widget in the top level window */
624 Widget* top = get_toplevel();
626 if (top->is_toplevel ()) {
627 Window* win = dynamic_cast<Window*> (top);
634 AudioClock::parse_as_frames_distance (const std::string& str)
638 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
646 AudioClock::parse_as_minsec_distance (const std::string& str)
648 framecnt_t sr = _session->frame_rate();
654 switch (str.length()) {
661 sscanf (str.c_str(), "%" PRId32, &msecs);
662 return msecs * (sr / 1000);
665 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
666 return (secs * sr) + (msecs * (sr/1000));
669 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
670 return (secs * sr) + (msecs * (sr/1000));
673 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
674 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
677 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
678 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
681 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
682 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
685 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
686 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
696 AudioClock::parse_as_timecode_distance (const std::string& str)
698 double fps = _session->timecode_frames_per_second();
699 framecnt_t sr = _session->frame_rate();
705 switch (str.length()) {
710 sscanf (str.c_str(), "%" PRId32, &frames);
711 return lrint ((frames/(float)fps) * sr);
714 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
715 return (secs * sr) + lrint ((frames/(float)fps) * sr);
718 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
719 return (secs * sr) + lrint ((frames/(float)fps) * sr);
722 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
723 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
726 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
727 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
730 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
731 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
734 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
735 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
745 AudioClock::parse_as_bbt_distance (const std::string& str)
751 AudioClock::parse_as_distance (const std::string& instr)
755 return parse_as_timecode_distance (instr);
758 return parse_as_frames_distance (instr);
761 return parse_as_bbt_distance (instr);
764 return parse_as_minsec_distance (instr);
771 AudioClock::end_edit_relative (bool add)
773 framecnt_t frames = parse_as_distance (input_string);
778 _layout->set_attributes (normal_attributes);
782 set (current_time() + frames, true);
784 framepos_t c = current_time();
787 set (c - frames, true);
792 ValueChanged (); /* EMIT SIGNAL */
795 input_string.clear ();
801 AudioClock::session_configuration_changed (std::string p)
803 if (p == "sync-source" || p == "external-sync") {
804 set (current_time(), true);
808 if (p != "timecode-offset" && p != "timecode-offset-negative") {
817 current = current_duration ();
819 current = current_time ();
829 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
831 if ((!force && !is_visible()) || _session == 0) {
836 when = when - offset;
839 if (when == last_when && !force) {
847 set_timecode (when, force);
851 set_bbt (when, force);
855 set_minsec (when, force);
859 set_frames (when, force);
869 AudioClock::set_frames (framepos_t when, bool /*force*/)
872 bool negative = false;
875 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
878 _left_layout->set_text ("");
879 _right_layout->set_text ("");
891 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
893 snprintf (buf, sizeof (buf), " %10" PRId64, when);
896 _layout->set_text (buf);
899 framecnt_t rate = _session->frame_rate();
901 if (fmod (rate, 100.0) == 0.0) {
902 sprintf (buf, "SR %.1fkHz", rate/1000.0);
904 sprintf (buf, "SR %" PRId64, rate);
907 _left_layout->set_text (buf);
909 float vid_pullup = _session->config.get_video_pullup();
911 if (vid_pullup == 0.0) {
912 _right_layout->set_text (_("pullup: \u2012"));
914 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
915 _right_layout->set_text (buf);
921 AudioClock::set_minsec (framepos_t when, bool force)
929 bool negative = false;
932 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
935 _left_layout->set_text ("");
936 _right_layout->set_text ("");
948 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
949 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
950 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
951 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
952 secs = (int) floor (left / (float) _session->frame_rate());
953 left -= (framecnt_t) floor (secs * _session->frame_rate());
954 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
957 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
959 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
962 _layout->set_text (buf);
966 AudioClock::set_timecode (framepos_t when, bool force)
970 bool negative = false;
973 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
975 _left_layout->set_text ("");
976 _right_layout->set_text ("");
988 _session->timecode_duration (when, TC);
990 _session->timecode_time (when, TC);
993 if (TC.negative || negative) {
994 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
996 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
999 _layout->set_text (buf);
1003 if (_session->config.get_external_sync()) {
1004 switch (_session->config.get_sync_source()) {
1006 _left_layout->set_text ("JACK");
1009 _left_layout->set_text ("MTC");
1012 _left_layout->set_text ("M-Clock");
1016 _left_layout->set_text ("INT");
1019 double timecode_frames = _session->timecode_frames_per_second();
1021 if (fmod(timecode_frames, 1.0) == 0.0) {
1022 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
1024 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
1027 _right_layout->set_text (buf);
1032 AudioClock::set_bbt (framepos_t when, bool force)
1035 Timecode::BBT_Time BBT;
1036 bool negative = false;
1039 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
1041 _left_layout->set_text ("");
1042 _right_layout->set_text ("");
1052 /* handle a common case */
1059 _session->tempo_map().bbt_time (when, BBT);
1064 _session->tempo_map().bbt_time (when, BBT);
1068 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1069 BBT.bars, BBT.beats, BBT.ticks);
1071 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
1072 BBT.bars, BBT.beats, BBT.ticks);
1075 _layout->set_text (buf);
1077 if (_right_layout) {
1080 if (bbt_reference_time < 0) {
1083 pos = bbt_reference_time;
1086 TempoMetric m (_session->tempo_map().metric_at (pos));
1088 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
1089 _left_layout->set_text (buf);
1091 sprintf (buf, "%g/%g", m.meter().beats_per_bar(), m.meter().note_divisor());
1092 _right_layout->set_text (buf);
1097 AudioClock::set_session (Session *s)
1099 SessionHandlePtr::set_session (s);
1103 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
1105 const XMLProperty* prop;
1106 XMLNode* node = _session->extra_xml (X_("ClockModes"));
1107 AudioClock::Mode amode;
1110 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
1111 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
1113 if ((prop = (*i)->property (X_("mode"))) != 0) {
1114 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
1117 if ((prop = (*i)->property (X_("on"))) != 0) {
1118 set_off (!string_is_affirmative (prop->value()));
1125 set (last_when, true);
1130 AudioClock::on_key_press_event (GdkEventKey* ev)
1139 switch (ev->keyval) {
1182 case GDK_KP_Subtract:
1183 end_edit_relative (false);
1189 end_edit_relative (true);
1202 ChangeAborted(); /* EMIT SIGNAL */
1207 if (!input_string.empty()) {
1208 /* delete the last key entered
1210 input_string = input_string.substr (0, input_string.length() - 1);
1212 goto use_input_string;
1218 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1219 /* too many digits: eat the key event, but do nothing with it */
1223 input_string.push_back (new_char);
1227 int highlight_length = 0;
1230 /* merge with pre-edit-string into edit string */
1234 /* get this one in the right order, and to the right width */
1235 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1236 edit_string.push_back (new_char);
1238 edit_string = edit_string.substr (0, edit_string.length() - 1);
1240 if (!edit_string.empty()) {
1242 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1243 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1246 /* highlight the whole thing */
1247 highlight_length = edit_string.length();
1251 edit_string = pre_edit_string;
1253 if (input_string.empty()) {
1254 highlight_length = 0;
1256 // for (int i = input_string.length() - 1; i >= 0; --i) {
1257 string::size_type target;
1258 for (string::size_type i = 0; i < input_string.length(); ++i) {
1259 target = insert_map[input_string.length() - 1 - i];
1260 edit_string[target] = input_string[i];
1262 /* highlight from end to wherever the last character was added */
1263 highlight_length = edit_string.length() - insert_map[input_string.length()-1];
1269 show_edit_status (highlight_length);
1270 _layout->set_text (edit_string);
1278 AudioClock::on_key_release_event (GdkEventKey *ev)
1284 /* return true for keys that we used on press
1285 so that they cannot possibly do double-duty
1287 switch (ev->keyval) {
1310 case GDK_KP_Decimal:
1318 case GDK_KP_Subtract:
1326 AudioClock::index_to_field (int index) const
1331 return Timecode_Hours;
1332 } else if (index < 7) {
1333 return Timecode_Minutes;
1334 } else if (index < 10) {
1335 return Timecode_Seconds;
1337 return Timecode_Frames;
1343 } else if (index < 7) {
1351 return Timecode_Hours;
1352 } else if (index < 6) {
1354 } else if (index < 9) {
1357 return MS_Milliseconds;
1369 AudioClock::on_button_press_event (GdkEventButton *ev)
1371 switch (ev->button) {
1373 if (editable && !_off) {
1375 /* make absolutely sure that the pointer is grabbed */
1376 gdk_pointer_grab(ev->window,false ,
1377 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1378 NULL,NULL,ev->time);
1380 drag_start_y = ev->y;
1388 /* the text has been centered vertically, so adjust
1392 y = ev->y - ((upper_height - layout_height)/2);
1393 x = ev->x - layout_x_offset;
1395 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1396 drag_field = index_to_field (index);
1398 drag_field = Field (0);
1412 AudioClock::on_button_release_event (GdkEventButton *ev)
1414 if (editable && !_off) {
1416 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1418 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1419 // we actually dragged so return without
1420 // setting editing focus, or we shift clicked
1423 if (ev->button == 1) {
1431 if (Keyboard::is_context_menu_event (ev)) {
1432 if (ops_menu == 0) {
1435 ops_menu->popup (1, ev->time);
1443 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1445 bool ret = CairoWidget::on_focus_out_event (ev);
1455 AudioClock::on_scroll_event (GdkEventScroll *ev)
1460 if (editing || _session == 0 || !editable || _off) {
1467 /* the text has been centered vertically, so adjust
1471 y = ev->y - ((upper_height - layout_height)/2);
1472 x = ev->x - layout_x_offset;
1474 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1475 /* not in the main layout */
1479 Field f = index_to_field (index);
1480 framepos_t frames = 0;
1482 switch (ev->direction) {
1485 frames = get_frame_step (f);
1487 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1490 set (current_time() + frames, true);
1491 ValueChanged (); /* EMIT_SIGNAL */
1495 case GDK_SCROLL_DOWN:
1496 frames = get_frame_step (f);
1498 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1502 if ((double)current_time() - (double)frames < 0.0) {
1505 set (current_time() - frames, true);
1508 ValueChanged (); /* EMIT_SIGNAL */
1521 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1523 if (editing || _session == 0 || !dragging) {
1527 float pixel_frame_scale_factor = 0.2f;
1529 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1530 pixel_frame_scale_factor = 0.1f;
1534 if (Keyboard::modifier_state_contains (ev->state,
1535 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1537 pixel_frame_scale_factor = 0.025f;
1540 double y_delta = ev->y - drag_y;
1542 drag_accum += y_delta*pixel_frame_scale_factor;
1546 if (trunc (drag_accum) != 0) {
1551 dir = (drag_accum < 0 ? 1:-1);
1552 pos = current_time();
1553 frames = get_frame_step (drag_field,pos,dir);
1555 if (frames != 0 && frames * drag_accum < current_time()) {
1556 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1562 ValueChanged(); /* EMIT_SIGNAL */
1569 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1572 Timecode::BBT_Time BBT;
1574 case Timecode_Hours:
1575 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1577 case Timecode_Minutes:
1578 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1580 case Timecode_Seconds:
1581 f = _session->frame_rate();
1583 case Timecode_Frames:
1584 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1592 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1595 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1598 f = (framecnt_t) _session->frame_rate();
1600 case MS_Milliseconds:
1601 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1608 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1614 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1620 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1623 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1632 AudioClock::current_time (framepos_t pos) const
1638 AudioClock::current_duration (framepos_t pos) const
1647 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1663 AudioClock::bbt_validate_edit (const string& str)
1667 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1671 if (!is_duration && any.bbt.bars == 0) {
1675 if (!is_duration && any.bbt.beats == 0) {
1683 AudioClock::timecode_validate_edit (const string& str)
1687 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1688 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1692 if (TC.minutes > 59U || TC.seconds > 59U) {
1696 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1700 if (_session->timecode_drop_frames()) {
1701 if (TC.minutes % 10 && TC.seconds == 0U && TC.frames < 2U) {
1710 AudioClock::frames_from_timecode_string (const string& str) const
1712 if (_session == 0) {
1719 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1720 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1724 TC.rate = _session->timecode_frames_per_second();
1725 TC.drop= _session->timecode_drop_frames();
1727 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1729 // timecode_tester ();
1735 AudioClock::frames_from_minsec_string (const string& str) const
1737 if (_session == 0) {
1741 int hrs, mins, secs, millisecs;
1742 framecnt_t sr = _session->frame_rate();
1744 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1745 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1749 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1753 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1755 if (_session == 0) {
1756 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1761 any.type = AnyTime::BBT;
1763 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks) != 3) {
1770 return _session->any_duration_to_frames (pos, any);
1772 return _session->convert_to_frames (any);
1778 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1780 if (_session == 0) {
1781 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1785 Timecode::BBT_Time bbt;
1787 if (sscanf (str.c_str(), BBT_SCANF_FORMAT, &bbt.bars, &bbt.beats, &bbt.ticks) != 0) {
1791 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1795 AudioClock::frames_from_audioframes_string (const string& str) const
1798 sscanf (str.c_str(), "%" PRId64, &f);
1803 AudioClock::build_ops_menu ()
1805 using namespace Menu_Helpers;
1806 ops_menu = new Menu;
1807 MenuList& ops_items = ops_menu->items();
1808 ops_menu->set_name ("ArdourContextMenu");
1810 if (!Profile->get_sae()) {
1811 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1813 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1814 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1815 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1817 if (editable && !_off && !is_duration && !_follows_playhead) {
1818 ops_items.push_back (SeparatorElem());
1819 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1820 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1825 AudioClock::set_from_playhead ()
1831 set (_session->transport_frame());
1836 AudioClock::locate ()
1838 if (!_session || is_duration) {
1842 _session->request_locate (current_time(), _session->transport_rolling ());
1846 AudioClock::set_mode (Mode m)
1856 _layout->set_text ("");
1859 _left_layout->set_text ("");
1860 _right_layout->set_text ("");
1865 mode_based_info_ratio = 0.5;
1866 insert_map.push_back (11);
1867 insert_map.push_back (10);
1868 insert_map.push_back (8);
1869 insert_map.push_back (7);
1870 insert_map.push_back (5);
1871 insert_map.push_back (4);
1872 insert_map.push_back (2);
1873 insert_map.push_back (1);
1877 mode_based_info_ratio = 0.5;
1878 insert_map.push_back (11);
1879 insert_map.push_back (10);
1880 insert_map.push_back (9);
1881 insert_map.push_back (8);
1882 insert_map.push_back (6);
1883 insert_map.push_back (5);
1884 insert_map.push_back (3);
1885 insert_map.push_back (2);
1886 insert_map.push_back (1);
1890 mode_based_info_ratio = 1.0;
1891 insert_map.push_back (12);
1892 insert_map.push_back (11);
1893 insert_map.push_back (10);
1894 insert_map.push_back (8);
1895 insert_map.push_back (7);
1896 insert_map.push_back (5);
1897 insert_map.push_back (4);
1898 insert_map.push_back (2);
1899 insert_map.push_back (1);
1903 mode_based_info_ratio = 0.5;
1907 set (last_when, true);
1909 if (!is_transient) {
1910 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1913 if (!_fixed_width) {
1914 /* display is different, allow us to resize */
1920 mode_changed (); /* EMIT SIGNAL (the member one) */
1924 AudioClock::set_bbt_reference (framepos_t pos)
1926 bbt_reference_time = pos;
1930 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1932 CairoWidget::on_style_changed (old_style);
1938 AudioClock::set_editable (bool yn)
1944 AudioClock::set_is_duration (bool yn)
1946 if (yn == is_duration) {
1951 set (last_when, true);
1955 AudioClock::set_off (bool yn)
1963 /* force a redraw. last_when will be preserved, but the clock text will
1967 set (last_when, true);
1971 AudioClock::focus ()
1977 AudioClock::set_corner_radius (double r)
1984 AudioClock::set_fixed_width (bool yn)
1990 AudioClock::dpi_reset ()
1992 /* force recomputation of size even if we are fixed width