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 <sigc++/bind.h>
38 #include "ardour_ui.h"
39 #include "audio_clock.h"
40 #include "global_signals.h"
43 #include "gui_thread.h"
46 using namespace ARDOUR;
51 using Gtkmm2ext::Keyboard;
53 sigc::signal<void> AudioClock::ModeChanged;
54 vector<AudioClock*> AudioClock::clocks;
55 const double AudioClock::info_font_scale_factor = 0.6;
56 const double AudioClock::separator_height = 2.0;
57 const double AudioClock::x_leading_padding = 6.0;
59 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
60 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
62 , is_transient (transient)
63 , is_duration (duration)
64 , editable (allow_edit)
65 , _follows_playhead (follows_playhead)
70 , mode_based_info_ratio (1.0)
72 , bbt_reference_time (-1)
77 , drag_field (Field (0))
80 set_flags (CAN_FOCUS);
82 _layout = Pango::Layout::create (get_pango_context());
83 _layout->set_attributes (normal_attributes);
86 _left_layout = Pango::Layout::create (get_pango_context());
87 _right_layout = Pango::Layout::create (get_pango_context());
90 set_widget_name (widget_name);
92 _mode = BBT; /* lie to force mode switch */
94 set (last_when, true);
97 clocks.push_back (this);
100 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
103 AudioClock::~AudioClock ()
105 delete foreground_attr;
110 AudioClock::set_widget_name (const string& str)
115 set_name (str + " clock");
125 AudioClock::on_realize ()
127 CairoWidget::on_realize ();
133 AudioClock::set_font ()
135 Glib::RefPtr<Gtk::Style> style = get_style ();
136 Pango::FontDescription font;
137 Pango::AttrFontDesc* font_attr;
139 if (!is_realized()) {
140 font = get_font_for_style (get_name());
142 font = style->get_font();
145 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
147 normal_attributes.change (*font_attr);
148 editing_attributes.change (*font_attr);
150 /* now a smaller version of the same font */
153 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
154 font.set_weight (Pango::WEIGHT_NORMAL);
155 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
157 info_attributes.change (*font_attr);
163 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
165 CairoWidget::set_active_state (s);
170 AudioClock::set_colors ()
176 uint32_t editing_color;
178 if (active_state()) {
179 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
180 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
181 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
183 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
184 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
185 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
188 /* store for bg in ::render() */
190 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
191 r = lrint ((r/256.0) * 65535.0);
192 g = lrint ((g/256.0) * 65535.0);
193 b = lrint ((b/256.0) * 65535.0);
199 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
200 r = lrint ((r/256.0) * 65535.0);
201 g = lrint ((g/256.0) * 65535.0);
202 b = lrint ((b/256.0) * 65535.0);
203 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
205 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
206 r = lrint ((r/256.0) * 65535.0);
207 g = lrint ((g/256.0) * 65535.0);
208 b = lrint ((b/256.0) * 65535.0);
209 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
211 normal_attributes.change (*foreground_attr);
212 info_attributes.change (*foreground_attr);
213 editing_attributes.change (*foreground_attr);
214 editing_attributes.change (*editing_attr);
217 _layout->set_attributes (normal_attributes);
219 _layout->set_attributes (editing_attributes);
223 _left_layout->set_attributes (info_attributes);
224 _right_layout->set_attributes (info_attributes);
231 AudioClock::render (cairo_t* cr)
233 /* main layout: rounded rect, plus the text */
236 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
237 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, 9);
241 cairo_move_to (cr, x_leading_padding, (upper_height - layout_height) / 2.0);
242 pango_cairo_show_layout (cr, _layout->gobj());
246 double h = get_height() - upper_height - separator_height;
249 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
252 if (mode_based_info_ratio != 1.0) {
254 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
257 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, 9);
261 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
262 pango_cairo_show_layout (cr, _left_layout->gobj());
265 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
266 get_width() - separator_height - left_rect_width, h, 9);
270 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
271 pango_cairo_show_layout (cr, _right_layout->gobj());
274 /* no info to display, or just one */
277 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, 9);
285 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
287 CairoWidget::on_size_allocate (alloc);
290 upper_height = (get_height()/2.0) - 1.0;
292 upper_height = get_height();
297 AudioClock::on_size_request (Gtk::Requisition* req)
299 Glib::RefPtr<Pango::Layout> tmp;
300 Glib::RefPtr<Gtk::Style> style = get_style ();
301 Pango::FontDescription font;
303 tmp = Pango::Layout::create (get_pango_context());
305 if (!is_realized()) {
306 font = get_font_for_style (get_name());
308 font = style->get_font();
311 tmp->set_font_description (font);
313 /* this string is the longest thing we will ever display,
314 and also includes the BBT "|" that may descends below
315 the baseline a bit, and a comma for the minsecs mode
316 where we printf a fractional value (XXX or should)
319 tmp->set_text (" 88|88:88:88,88");
321 tmp->get_pixel_size (req->width, req->height);
323 layout_height = req->height;
324 layout_width = req->width;
326 /* now tackle height, for which we need to know the height of the lower
334 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
335 font.set_weight (Pango::WEIGHT_NORMAL);
336 tmp->set_font_description (font);
338 /* we only care about height, so put as much stuff in here
339 as possible that might change the height.
341 tmp->set_text ("qyhH|"); /* one ascender, one descender */
343 tmp->get_pixel_size (w, info_height);
345 /* silly extra padding that seems necessary to correct the info
346 * that pango just gave us. I have no idea why.
351 req->height += info_height;
352 req->height += separator_height;
357 AudioClock::show_edit_status (int length)
359 editing_attr->set_start_index (edit_string.length() - length);
360 editing_attr->set_end_index (edit_string.length());
362 editing_attributes.change (*foreground_attr);
363 editing_attributes.change (*editing_attr);
365 _layout->set_attributes (editing_attributes);
369 AudioClock::start_edit ()
371 edit_string = _layout->get_text ();
372 pre_edit_string = edit_string;
373 input_string.clear ();
378 Keyboard::magic_widget_grab_focus ();
383 AudioClock::end_edit (bool modify)
391 ok = timecode_validate_edit (edit_string);
395 ok = bbt_validate_edit (edit_string);
406 edit_string = pre_edit_string;
407 input_string.clear ();
408 _layout->set_text (edit_string);
409 show_edit_status (1);
410 /* edit attributes remain in use */
418 pos = frames_from_timecode_string (edit_string);
423 pos = frame_duration_from_bbt_string (0, edit_string);
425 pos = frames_from_bbt_string (0, edit_string);
430 pos = frames_from_minsec_string (edit_string);
434 pos = frames_from_audioframes_string (edit_string);
439 _layout->set_attributes (normal_attributes);
440 ValueChanged(); /* EMIT_SIGNAL */
446 _layout->set_attributes (normal_attributes);
447 _layout->set_text (pre_edit_string);
458 AudioClock::drop_focus ()
460 /* move focus back to the default widget in the top level window */
462 Keyboard::magic_widget_drop_focus ();
463 Widget* top = get_toplevel();
464 if (top->is_toplevel ()) {
465 Window* win = dynamic_cast<Window*> (top);
471 AudioClock::parse_as_frames_distance (const std::string& str)
475 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
483 AudioClock::parse_as_minsec_distance (const std::string& str)
485 framecnt_t sr = _session->frame_rate();
491 switch (str.length()) {
498 sscanf (str.c_str(), "%" PRId32, &msecs);
499 return msecs * (sr / 1000);
502 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
503 return (secs * sr) + (msecs * (sr/1000));
506 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
507 return (secs * sr) + (msecs * (sr/1000));
510 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
511 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
514 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
515 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
518 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
519 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
522 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
523 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
533 AudioClock::parse_as_timecode_distance (const std::string& str)
535 double fps = _session->timecode_frames_per_second();
536 framecnt_t sr = _session->frame_rate();
542 switch (str.length()) {
547 sscanf (str.c_str(), "%" PRId32, &frames);
548 return lrint ((frames/(float)fps) * sr);
551 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
552 return (secs * sr) + lrint ((frames/(float)fps) * sr);
555 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
556 return (secs * sr) + lrint ((frames/(float)fps) * sr);
559 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
560 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
563 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
564 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
567 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
568 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
571 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
572 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
582 AudioClock::parse_as_bbt_distance (const std::string& str)
588 AudioClock::parse_as_distance (const std::string& instr)
592 /* the input string is in reverse order */
594 std::reverse (str.begin(), str.end());
598 return parse_as_timecode_distance (str);
601 return parse_as_frames_distance (str);
604 return parse_as_bbt_distance (str);
607 return parse_as_minsec_distance (str);
614 AudioClock::end_edit_relative (bool add)
616 framecnt_t frames = parse_as_distance (input_string);
621 _layout->set_attributes (normal_attributes);
625 set (current_time() + frames, true);
627 framepos_t c = current_time();
630 set (c - frames, true);
635 ValueChanged (); /* EMIT SIGNAL */
638 input_string.clear ();
644 AudioClock::session_configuration_changed (std::string p)
646 if (p != "timecode-offset" && p != "timecode-offset-negative") {
655 current = current_duration ();
657 current = current_time ();
667 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
669 if ((!force && !is_visible()) || _session == 0) {
674 when = when - offset;
677 if (when == last_when && !force) {
685 set_timecode (when, force);
689 set_bbt (when, force);
693 set_minsec (when, force);
697 set_frames (when, force);
702 if (when != last_when || force) {
710 AudioClock::set_frames (framepos_t when, bool /*force*/)
715 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
718 _left_layout->set_text ("");
719 _right_layout->set_text ("");
725 snprintf (buf, sizeof (buf), "%10" PRId64, when);
726 _layout->set_text (buf);
729 framecnt_t rate = _session->frame_rate();
731 if (fmod (rate, 1000.0) == 0.000) {
732 sprintf (buf, "%" PRId64 "K", rate/1000);
734 sprintf (buf, "%" PRId64, rate);
737 _left_layout->set_text (buf);
739 float vid_pullup = _session->config.get_video_pullup();
741 if (vid_pullup == 0.0) {
742 _right_layout->set_text (_("none"));
744 sprintf (buf, "%-6.4f", vid_pullup);
745 _right_layout->set_text (buf);
751 AudioClock::set_minsec (framepos_t when, bool force)
759 bool negative = false;
762 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
765 _left_layout->set_text ("");
766 _right_layout->set_text ("");
778 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
779 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
780 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
781 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
782 secs = (int) floor (left / (float) _session->frame_rate());
783 left -= (framecnt_t) floor (secs * _session->frame_rate());
784 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
787 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
789 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
792 _layout->set_text (buf);
796 AudioClock::set_timecode (framepos_t when, bool force)
803 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
805 _left_layout->set_text ("");
806 _right_layout->set_text ("");
818 _session->timecode_duration (when, TC);
820 _session->timecode_time (when, TC);
823 if (TC.negative || negative) {
824 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
826 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
829 _layout->set_text (buf);
832 double timecode_frames = _session->timecode_frames_per_second();
834 if (fmod(timecode_frames, 1.0) == 0.0) {
835 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
837 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
840 _right_layout->set_text (buf);
845 AudioClock::set_bbt (framepos_t when, bool force)
848 Timecode::BBT_Time BBT;
852 _layout->set_text ("\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
854 _left_layout->set_text ("");
855 _right_layout->set_text ("");
865 /* handle a common case */
872 _session->tempo_map().bbt_time (when, BBT);
877 _session->tempo_map().bbt_time (when, BBT);
881 snprintf (buf, sizeof (buf), "-%02" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
883 snprintf (buf, sizeof (buf), " %02" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
886 _layout->set_text (buf);
891 if (bbt_reference_time < 0) {
894 pos = bbt_reference_time;
897 TempoMetric m (_session->tempo_map().metric_at (pos));
899 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
900 _left_layout->set_text (buf);
902 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
903 _right_layout->set_text (buf);
908 AudioClock::set_session (Session *s)
910 SessionHandlePtr::set_session (s);
914 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
916 const XMLProperty* prop;
917 XMLNode* node = _session->extra_xml (X_("ClockModes"));
918 AudioClock::Mode amode;
921 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
922 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
924 if ((prop = (*i)->property (X_("mode"))) != 0) {
925 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
928 if ((prop = (*i)->property (X_("on"))) != 0) {
929 set_off (!string_is_affirmative (prop->value()));
936 set (last_when, true);
941 AudioClock::on_key_press_event (GdkEventKey* ev)
947 /* return true for keys that we MIGHT use
950 switch (ev->keyval) {
985 AudioClock::on_key_release_event (GdkEventKey *ev)
994 switch (ev->keyval) {
1037 case GDK_KP_Subtract:
1038 end_edit_relative (false);
1044 end_edit_relative (true);
1057 ChangeAborted(); /* EMIT SIGNAL */
1064 if (input_string.length() >= insert_max) {
1065 /* eat the key event, but do no nothing with it */
1069 input_string.insert (input_string.begin(), new_char);
1071 string::reverse_iterator ri;
1072 vector<int> insert_at;
1073 int highlight_length;
1075 /* merge with pre-edit-string into edit string */
1079 edit_string = input_string;
1080 highlight_length = edit_string.length();
1084 edit_string = pre_edit_string;
1086 /* backup through the original string, till we have
1087 * enough digits locations to put all the digits from
1091 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1092 if (isdigit (*ri)) {
1093 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1094 if (insert_at.size() == input_string.length()) {
1100 if (insert_at.size() != input_string.length()) {
1101 error << "something went wrong " << endmsg;
1103 for (int i = input_string.length() - 1; i >= 0; --i) {
1104 edit_string[insert_at[i]] = input_string[i];
1107 highlight_length = edit_string.length() - insert_at.back();
1113 if (edit_string != _layout->get_text()) {
1114 show_edit_status (highlight_length);
1115 _layout->set_text (edit_string);
1123 AudioClock::index_to_field (int index) const
1128 return Timecode_Hours;
1129 } else if (index < 7) {
1130 return Timecode_Minutes;
1131 } else if (index < 10) {
1132 return Timecode_Seconds;
1133 } else if (index < 13) {
1134 return Timecode_Frames;
1142 } else if (index < 7) {
1144 } else if (index < 12) {
1153 return Timecode_Hours;
1154 } else if (index < 6) {
1156 } else if (index < 9) {
1158 } else if (index < 12) {
1159 return MS_Milliseconds;
1172 AudioClock::on_button_press_event (GdkEventButton *ev)
1174 switch (ev->button) {
1176 if (editable && !_off) {
1178 /* make absolutely sure that the pointer is grabbed */
1179 gdk_pointer_grab(ev->window,false ,
1180 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1181 NULL,NULL,ev->time);
1183 drag_start_y = ev->y;
1191 /* the text has been centered vertically, so adjust
1195 y = ev->y - ((upper_height - layout_height)/2);
1196 x = ev->x - x_leading_padding;
1198 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1199 drag_field = index_to_field (index);
1201 drag_field = Field (0);
1215 AudioClock::on_button_release_event (GdkEventButton *ev)
1217 if (editable && !_off) {
1219 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1221 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1222 // we actually dragged so return without
1223 // setting editing focus, or we shift clicked
1226 if (ev->button == 1) {
1234 if (Keyboard::is_context_menu_event (ev)) {
1235 if (ops_menu == 0) {
1238 ops_menu->popup (1, ev->time);
1246 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1248 bool ret = CairoWidget::on_focus_out_event (ev);
1258 AudioClock::on_scroll_event (GdkEventScroll *ev)
1263 if (_session == 0 || !editable || _off) {
1270 /* the text has been centered vertically, so adjust
1274 y = ev->y - ((upper_height - layout_height)/2);
1275 x = ev->x - x_leading_padding;
1277 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1278 /* not in the main layout */
1282 Field f = index_to_field (index);
1283 framepos_t frames = 0;
1285 switch (ev->direction) {
1288 frames = get_frame_step (f);
1290 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1293 set (current_time() + frames, true);
1294 ValueChanged (); /* EMIT_SIGNAL */
1298 case GDK_SCROLL_DOWN:
1299 frames = get_frame_step (f);
1301 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1305 if ((double)current_time() - (double)frames < 0.0) {
1308 set (current_time() - frames, true);
1311 ValueChanged (); /* EMIT_SIGNAL */
1324 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1326 if (_session == 0 || !dragging) {
1330 float pixel_frame_scale_factor = 0.2f;
1332 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1333 pixel_frame_scale_factor = 0.1f;
1337 if (Keyboard::modifier_state_contains (ev->state,
1338 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1340 pixel_frame_scale_factor = 0.025f;
1343 double y_delta = ev->y - drag_y;
1345 drag_accum += y_delta*pixel_frame_scale_factor;
1349 if (trunc (drag_accum) != 0) {
1354 dir = (drag_accum < 0 ? 1:-1);
1355 pos = current_time();
1356 frames = get_frame_step (drag_field,pos,dir);
1358 if (frames != 0 && frames * drag_accum < current_time()) {
1359 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1365 ValueChanged(); /* EMIT_SIGNAL */
1372 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1375 Timecode::BBT_Time BBT;
1377 case Timecode_Hours:
1378 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1380 case Timecode_Minutes:
1381 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1383 case Timecode_Seconds:
1384 f = _session->frame_rate();
1386 case Timecode_Frames:
1387 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1395 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1398 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1401 f = (framecnt_t) _session->frame_rate();
1403 case MS_Milliseconds:
1404 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1411 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1417 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1423 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1426 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1435 AudioClock::current_time (framepos_t pos) const
1441 AudioClock::current_duration (framepos_t pos) const
1450 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1466 AudioClock::bbt_validate_edit (const string& str)
1470 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1472 if (!is_duration && any.bbt.bars == 0) {
1476 if (!is_duration && any.bbt.beats == 0) {
1484 AudioClock::timecode_validate_edit (const string& str)
1488 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1489 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1493 if (TC.minutes > 59 || TC.seconds > 59) {
1497 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1501 if (_session->timecode_drop_frames()) {
1502 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1511 AudioClock::frames_from_timecode_string (const string& str) const
1513 if (_session == 0) {
1520 sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames);
1522 TC.rate = _session->timecode_frames_per_second();
1523 TC.drop= _session->timecode_drop_frames();
1525 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1527 // timecode_tester ();
1533 AudioClock::frames_from_minsec_string (const string& str) const
1535 if (_session == 0) {
1539 int hrs, mins, secs, millisecs;
1540 framecnt_t sr = _session->frame_rate();
1542 sscanf (str.c_str(), "%d:%d:%d:%d", &hrs, &mins, &secs, &millisecs);
1543 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1547 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1549 if (_session == 0) {
1550 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1555 any.type = AnyTime::BBT;
1557 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1562 return _session->any_duration_to_frames (pos, any);
1564 return _session->convert_to_frames (any);
1570 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1572 if (_session == 0) {
1573 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1577 Timecode::BBT_Time bbt;
1579 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1581 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1585 AudioClock::frames_from_audioframes_string (const string& str) const
1588 sscanf (str.c_str(), "%" PRId64, &f);
1593 AudioClock::build_ops_menu ()
1595 using namespace Menu_Helpers;
1596 ops_menu = new Menu;
1597 MenuList& ops_items = ops_menu->items();
1598 ops_menu->set_name ("ArdourContextMenu");
1600 if (!Profile->get_sae()) {
1601 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1603 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1604 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1605 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1607 if (editable && !_off && !is_duration && !_follows_playhead) {
1608 ops_items.push_back (SeparatorElem());
1609 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1610 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1615 AudioClock::set_from_playhead ()
1621 set (_session->transport_frame());
1626 AudioClock::locate ()
1628 if (!_session || is_duration) {
1632 _session->request_locate (current_time(), _session->transport_rolling ());
1636 AudioClock::set_mode (Mode m)
1646 insert_max = 9; // 8 digits + sign [-]2:2:2:2
1647 mode_based_info_ratio = 0.5;
1651 insert_max = 8; // 8 digits, 2|2|4
1652 mode_based_info_ratio = 0.5;
1656 insert_max = 9; // 7 digits 2:2:2.3
1657 mode_based_info_ratio = 1.0;
1661 insert_max = INT_MAX;
1662 mode_based_info_ratio = 1.0;
1666 set (last_when, true);
1668 if (!is_transient) {
1669 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1672 mode_changed (); /* EMIT SIGNAL (the member one) */
1676 AudioClock::set_bbt_reference (framepos_t pos)
1678 bbt_reference_time = pos;
1682 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1684 CairoWidget::on_style_changed (old_style);
1690 AudioClock::set_editable (bool yn)
1696 AudioClock::set_is_duration (bool yn)
1698 if (yn == is_duration) {
1703 set (last_when, true);
1707 AudioClock::set_off (bool yn)
1715 /* force a redraw. last_when will be preserved, but the clock text will
1719 set (last_when, true);
1723 AudioClock::focus ()