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)
761 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
764 _left_layout->set_text ("");
765 _right_layout->set_text ("");
772 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
773 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
774 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
775 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
776 secs = (int) floor (left / (float) _session->frame_rate());
777 left -= (framecnt_t) floor (secs * _session->frame_rate());
778 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
780 snprintf (buf, sizeof (buf), "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%03" PRIu32, hrs, mins, secs, millisecs);
781 _layout->set_text (buf);
785 AudioClock::set_timecode (framepos_t when, bool force)
791 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
793 _left_layout->set_text ("");
794 _right_layout->set_text ("");
801 _session->timecode_duration (when, TC);
803 _session->timecode_time (when, TC);
807 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
809 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
812 _layout->set_text (buf);
815 double timecode_frames = _session->timecode_frames_per_second();
817 if (fmod(timecode_frames, 1.0) == 0.0) {
818 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
820 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
823 _right_layout->set_text (buf);
828 AudioClock::set_bbt (framepos_t when, bool force)
831 Timecode::BBT_Time BBT;
834 _layout->set_text ("\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
836 _left_layout->set_text ("");
837 _right_layout->set_text ("");
842 /* handle a common case */
849 _session->tempo_map().bbt_time (when, BBT);
854 _session->tempo_map().bbt_time (when, BBT);
857 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
858 _layout->set_text (buf);
863 if (bbt_reference_time < 0) {
866 pos = bbt_reference_time;
869 TempoMetric m (_session->tempo_map().metric_at (pos));
871 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
872 _left_layout->set_text (buf);
874 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
875 _right_layout->set_text (buf);
880 AudioClock::set_session (Session *s)
882 SessionHandlePtr::set_session (s);
886 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
888 const XMLProperty* prop;
889 XMLNode* node = _session->extra_xml (X_("ClockModes"));
890 AudioClock::Mode amode;
893 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
894 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
896 if ((prop = (*i)->property (X_("mode"))) != 0) {
897 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
900 if ((prop = (*i)->property (X_("on"))) != 0) {
901 set_off (!string_is_affirmative (prop->value()));
908 set (last_when, true);
913 AudioClock::on_key_press_event (GdkEventKey* ev)
919 /* return true for keys that we MIGHT use
922 switch (ev->keyval) {
957 AudioClock::on_key_release_event (GdkEventKey *ev)
966 switch (ev->keyval) {
1009 case GDK_KP_Subtract:
1010 end_edit_relative (false);
1016 end_edit_relative (true);
1029 ChangeAborted(); /* EMIT SIGNAL */
1036 if (input_string.length() >= insert_max) {
1037 /* eat the key event, but do no nothing with it */
1041 input_string.insert (input_string.begin(), new_char);
1043 string::reverse_iterator ri;
1044 vector<int> insert_at;
1045 int highlight_length;
1047 /* merge with pre-edit-string into edit string */
1051 edit_string = input_string;
1052 highlight_length = edit_string.length();
1056 edit_string = pre_edit_string;
1058 /* backup through the original string, till we have
1059 * enough digits locations to put all the digits from
1063 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1064 if (isdigit (*ri)) {
1065 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1066 if (insert_at.size() == input_string.length()) {
1072 if (insert_at.size() != input_string.length()) {
1073 error << "something went wrong " << endmsg;
1075 for (int i = input_string.length() - 1; i >= 0; --i) {
1076 edit_string[insert_at[i]] = input_string[i];
1079 highlight_length = edit_string.length() - insert_at.back();
1085 if (edit_string != _layout->get_text()) {
1086 show_edit_status (highlight_length);
1087 _layout->set_text (edit_string);
1095 AudioClock::index_to_field (int index) const
1100 return Timecode_Hours;
1101 } else if (index < 7) {
1102 return Timecode_Minutes;
1103 } else if (index < 10) {
1104 return Timecode_Seconds;
1105 } else if (index < 13) {
1106 return Timecode_Frames;
1114 } else if (index < 7) {
1116 } else if (index < 12) {
1125 return Timecode_Hours;
1126 } else if (index < 6) {
1128 } else if (index < 9) {
1130 } else if (index < 12) {
1131 return MS_Milliseconds;
1144 AudioClock::on_button_press_event (GdkEventButton *ev)
1146 switch (ev->button) {
1148 if (editable && !_off) {
1150 /* make absolutely sure that the pointer is grabbed */
1151 gdk_pointer_grab(ev->window,false ,
1152 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1153 NULL,NULL,ev->time);
1155 drag_start_y = ev->y;
1163 /* the text has been centered vertically, so adjust
1167 y = ev->y - ((get_height() - layout_height)/2);
1168 x = ev->x - x_leading_padding;
1170 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1171 drag_field = index_to_field (index);
1173 drag_field = Field (0);
1187 AudioClock::on_button_release_event (GdkEventButton *ev)
1189 if (editable && !_off) {
1191 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1193 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1194 // we actually dragged so return without
1195 // setting editing focus, or we shift clicked
1198 if (ev->button == 1) {
1206 if (Keyboard::is_context_menu_event (ev)) {
1207 if (ops_menu == 0) {
1210 ops_menu->popup (1, ev->time);
1218 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1220 bool ret = CairoWidget::on_focus_out_event (ev);
1230 AudioClock::on_scroll_event (GdkEventScroll *ev)
1235 if (_session == 0 || !editable || _off) {
1242 /* the text has been centered vertically, so adjust
1246 y = ev->y - ((get_height() - layout_height)/2);
1247 x = ev->x - x_leading_padding;
1249 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1250 /* not in the main layout */
1254 Field f = index_to_field (index);
1255 framepos_t frames = 0;
1257 switch (ev->direction) {
1260 frames = get_frame_step (f);
1262 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1265 set (current_time() + frames, true);
1266 ValueChanged (); /* EMIT_SIGNAL */
1270 case GDK_SCROLL_DOWN:
1271 frames = get_frame_step (f);
1273 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1277 if ((double)current_time() - (double)frames < 0.0) {
1280 set (current_time() - frames, true);
1283 ValueChanged (); /* EMIT_SIGNAL */
1296 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1298 if (_session == 0 || !dragging) {
1302 float pixel_frame_scale_factor = 0.2f;
1304 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1305 pixel_frame_scale_factor = 0.1f;
1309 if (Keyboard::modifier_state_contains (ev->state,
1310 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1312 pixel_frame_scale_factor = 0.025f;
1315 double y_delta = ev->y - drag_y;
1317 drag_accum += y_delta*pixel_frame_scale_factor;
1321 if (trunc (drag_accum) != 0) {
1326 dir = (drag_accum < 0 ? 1:-1);
1327 pos = current_time();
1328 frames = get_frame_step (drag_field,pos,dir);
1330 if (frames != 0 && frames * drag_accum < current_time()) {
1331 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1337 ValueChanged(); /* EMIT_SIGNAL */
1344 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1347 Timecode::BBT_Time BBT;
1349 case Timecode_Hours:
1350 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1352 case Timecode_Minutes:
1353 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1355 case Timecode_Seconds:
1356 f = _session->frame_rate();
1358 case Timecode_Frames:
1359 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1367 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1370 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1373 f = (framecnt_t) _session->frame_rate();
1375 case MS_Milliseconds:
1376 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1383 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1389 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1395 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1398 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1407 AudioClock::current_time (framepos_t pos) const
1413 AudioClock::current_duration (framepos_t pos) const
1422 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1438 AudioClock::bbt_validate_edit (const string& str)
1442 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1444 if (!is_duration && any.bbt.bars == 0) {
1448 if (!is_duration && any.bbt.beats == 0) {
1456 AudioClock::timecode_validate_edit (const string& str)
1460 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1461 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1465 if (TC.minutes > 59 || TC.seconds > 59) {
1469 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1473 if (_session->timecode_drop_frames()) {
1474 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1483 AudioClock::frames_from_timecode_string (const string& str) const
1485 if (_session == 0) {
1492 sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames);
1494 TC.rate = _session->timecode_frames_per_second();
1495 TC.drop= _session->timecode_drop_frames();
1497 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1499 // timecode_tester ();
1505 AudioClock::frames_from_minsec_string (const string& str) const
1507 if (_session == 0) {
1511 int hrs, mins, secs, millisecs;
1512 framecnt_t sr = _session->frame_rate();
1514 sscanf (str.c_str(), "%d:%d:%d:%d", &hrs, &mins, &secs, &millisecs);
1515 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1519 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1521 if (_session == 0) {
1522 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1527 any.type = AnyTime::BBT;
1529 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1534 return _session->any_duration_to_frames (pos, any);
1536 return _session->convert_to_frames (any);
1542 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1544 if (_session == 0) {
1545 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1549 Timecode::BBT_Time bbt;
1551 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1553 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1557 AudioClock::frames_from_audioframes_string (const string& str) const
1560 sscanf (str.c_str(), "%" PRId64, &f);
1565 AudioClock::build_ops_menu ()
1567 using namespace Menu_Helpers;
1568 ops_menu = new Menu;
1569 MenuList& ops_items = ops_menu->items();
1570 ops_menu->set_name ("ArdourContextMenu");
1572 if (!Profile->get_sae()) {
1573 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1575 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1576 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1577 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1579 if (editable && !_off && !is_duration && !_follows_playhead) {
1580 ops_items.push_back (SeparatorElem());
1581 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1582 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1587 AudioClock::set_from_playhead ()
1593 set (_session->transport_frame());
1598 AudioClock::locate ()
1600 if (!_session || is_duration) {
1604 _session->request_locate (current_time(), _session->transport_rolling ());
1608 AudioClock::set_mode (Mode m)
1618 insert_max = 9; // 8 digits + sign [-]2:2:2:2
1619 mode_based_info_ratio = 0.5;
1623 insert_max = 8; // 8 digits, 2|2|4
1624 mode_based_info_ratio = 0.5;
1628 insert_max = 9; // 7 digits 2:2:2.3
1629 mode_based_info_ratio = 1.0;
1633 insert_max = INT_MAX;
1634 mode_based_info_ratio = 1.0;
1638 set (last_when, true);
1640 if (!is_transient) {
1641 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1644 mode_changed (); /* EMIT SIGNAL (the member one) */
1648 AudioClock::set_bbt_reference (framepos_t pos)
1650 bbt_reference_time = pos;
1654 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1656 CairoWidget::on_style_changed (old_style);
1662 AudioClock::set_editable (bool yn)
1668 AudioClock::set_is_duration (bool yn)
1670 if (yn == is_duration) {
1675 set (last_when, true);
1679 AudioClock::set_off (bool yn)
1687 /* force a redraw. last_when will be preserved, but the clock text will
1691 set (last_when, true);
1695 AudioClock::focus ()