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 AudioClock::AudioClock (const string& clock_name, bool transient, const string& widget_name,
61 bool allow_edit, bool follows_playhead, bool duration, bool with_info)
63 , is_transient (transient)
64 , is_duration (duration)
65 , editable (allow_edit)
66 , _follows_playhead (follows_playhead)
71 , mode_based_info_ratio (1.0)
73 , bbt_reference_time (-1)
78 , drag_field (Field (0))
81 set_flags (CAN_FOCUS);
83 _layout = Pango::Layout::create (get_pango_context());
84 _layout->set_attributes (normal_attributes);
87 _left_layout = Pango::Layout::create (get_pango_context());
88 _right_layout = Pango::Layout::create (get_pango_context());
91 set_widget_name (widget_name);
93 _mode = BBT; /* lie to force mode switch */
95 set (last_when, true);
98 clocks.push_back (this);
101 ColorsChanged.connect (sigc::mem_fun (*this, &AudioClock::set_colors));
104 AudioClock::~AudioClock ()
106 delete foreground_attr;
111 AudioClock::set_widget_name (const string& str)
116 set_name (str + " clock");
126 AudioClock::on_realize ()
128 CairoWidget::on_realize ();
134 AudioClock::set_font ()
136 Glib::RefPtr<Gtk::Style> style = get_style ();
137 Pango::FontDescription font;
138 Pango::AttrFontDesc* font_attr;
140 if (!is_realized()) {
141 font = get_font_for_style (get_name());
143 font = style->get_font();
146 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
148 normal_attributes.change (*font_attr);
149 editing_attributes.change (*font_attr);
151 /* now a smaller version of the same font */
154 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
155 font.set_weight (Pango::WEIGHT_NORMAL);
156 font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
158 info_attributes.change (*font_attr);
164 AudioClock::set_active_state (Gtkmm2ext::ActiveState s)
166 CairoWidget::set_active_state (s);
171 AudioClock::set_colors ()
177 uint32_t editing_color;
179 if (active_state()) {
180 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: background", get_name()));
181 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: text", get_name()));
182 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1 active: edited text", get_name()));
184 bg_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: background", get_name()));
185 text_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: text", get_name()));
186 editing_color = ARDOUR_UI::config()->color_by_name (string_compose ("%1: edited text", get_name()));
189 /* store for bg in render() */
191 UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
192 r = lrint ((r/256.0) * 65535.0);
193 g = lrint ((g/256.0) * 65535.0);
194 b = lrint ((b/256.0) * 65535.0);
200 UINT_TO_RGBA (text_color, &r, &g, &b, &a);
201 r = lrint ((r/256.0) * 65535.0);
202 g = lrint ((g/256.0) * 65535.0);
203 b = lrint ((b/256.0) * 65535.0);
204 foreground_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
206 UINT_TO_RGBA (editing_color, &r, &g, &b, &a);
207 r = lrint ((r/256.0) * 65535.0);
208 g = lrint ((g/256.0) * 65535.0);
209 b = lrint ((b/256.0) * 65535.0);
210 editing_attr = new Pango::AttrColor (Pango::Attribute::create_attr_foreground (r, g, b));
212 normal_attributes.change (*foreground_attr);
213 info_attributes.change (*foreground_attr);
214 editing_attributes.change (*foreground_attr);
215 editing_attributes.change (*editing_attr);
218 _layout->set_attributes (normal_attributes);
220 _layout->set_attributes (editing_attributes);
224 _left_layout->set_attributes (info_attributes);
225 _right_layout->set_attributes (info_attributes);
232 AudioClock::render (cairo_t* cr)
234 /* main layout: rounded rect, plus the text */
237 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
238 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), upper_height, 9);
242 cairo_move_to (cr, x_leading_padding, (upper_height - layout_height) / 2.0);
243 pango_cairo_show_layout (cr, _layout->gobj());
247 double h = get_height() - upper_height - separator_height;
250 cairo_set_source_rgba (cr, bg_r, bg_g, bg_b, bg_a);
253 if (mode_based_info_ratio != 1.0) {
255 double left_rect_width = round (((get_width() - separator_height) * mode_based_info_ratio) + 0.5);
258 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, left_rect_width, h, 9);
262 cairo_move_to (cr, x_leading_padding, upper_height + separator_height + ((h - info_height)/2.0));
263 pango_cairo_show_layout (cr, _left_layout->gobj());
266 Gtkmm2ext::rounded_rectangle (cr, left_rect_width + separator_height, upper_height + separator_height,
267 get_width() - separator_height - left_rect_width, h, 9);
271 cairo_move_to (cr, x_leading_padding + left_rect_width + separator_height, upper_height + separator_height + ((h - info_height)/2.0));
272 pango_cairo_show_layout (cr, _right_layout->gobj());
275 /* no info to display, or just one */
278 Gtkmm2ext::rounded_rectangle (cr, 0, upper_height + separator_height, get_width(), h, 9);
285 const double cursor_width = 12; /* need em width here, not 16 */
287 if (!insert_map.empty()) {
288 Pango::Rectangle cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
290 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.8);
292 x_leading_padding + cursor.get_x()/PANGO_SCALE + cursor_width,
293 (upper_height - layout_height)/2.0,
294 2.0, cursor.get_height()/PANGO_SCALE);
297 if (input_string.empty()) {
298 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.8);
301 (upper_height - layout_height)/2.0,
310 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
312 CairoWidget::on_size_allocate (alloc);
315 upper_height = (get_height()/2.0) - 1.0;
317 upper_height = get_height();
322 AudioClock::on_size_request (Gtk::Requisition* req)
324 Glib::RefPtr<Pango::Layout> tmp;
325 Glib::RefPtr<Gtk::Style> style = get_style ();
326 Pango::FontDescription font;
328 tmp = Pango::Layout::create (get_pango_context());
330 if (!is_realized()) {
331 font = get_font_for_style (get_name());
333 font = style->get_font();
336 tmp->set_font_description (font);
338 /* this string is the longest thing we will ever display,
339 and also includes the BBT "|" that may descends below
340 the baseline a bit, and a comma for the minsecs mode
341 where we printf a fractional value (XXX or should)
344 tmp->set_text (" 88888888888,|");
346 tmp->get_pixel_size (req->width, req->height);
348 layout_height = req->height;
349 layout_width = req->width;
351 /* now tackle height, for which we need to know the height of the lower
359 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
360 font.set_weight (Pango::WEIGHT_NORMAL);
361 tmp->set_font_description (font);
363 /* we only care about height, so put as much stuff in here
364 as possible that might change the height.
366 tmp->set_text ("qyhH|"); /* one ascender, one descender */
368 tmp->get_pixel_size (w, info_height);
370 /* silly extra padding that seems necessary to correct the info
371 * that pango just gave us. I have no idea why.
376 req->height += info_height;
377 req->height += separator_height;
382 AudioClock::show_edit_status (int length)
384 editing_attr->set_start_index (edit_string.length() - length);
385 editing_attr->set_end_index (edit_string.length());
387 editing_attributes.change (*foreground_attr);
388 editing_attributes.change (*editing_attr);
390 _layout->set_attributes (editing_attributes);
394 AudioClock::start_edit ()
396 pre_edit_string = _layout->get_text ();
397 if (!insert_map.empty()) {
398 edit_string = pre_edit_string;
400 edit_string.clear ();
401 _layout->set_text ("");
403 input_string.clear ();
408 Keyboard::magic_widget_grab_focus ();
413 AudioClock::end_edit (bool modify)
421 ok = timecode_validate_edit (edit_string);
425 ok = bbt_validate_edit (edit_string);
436 edit_string = pre_edit_string;
437 input_string.clear ();
438 _layout->set_text (edit_string);
439 show_edit_status (0);
440 /* edit attributes remain in use */
444 framepos_t pos = 0; /* stupid gcc */
448 pos = frames_from_timecode_string (edit_string);
453 pos = frame_duration_from_bbt_string (0, edit_string);
455 pos = frames_from_bbt_string (0, edit_string);
460 pos = frames_from_minsec_string (edit_string);
464 pos = frames_from_audioframes_string (edit_string);
469 _layout->set_attributes (normal_attributes);
470 ValueChanged(); /* EMIT_SIGNAL */
476 _layout->set_attributes (normal_attributes);
477 _layout->set_text (pre_edit_string);
488 AudioClock::drop_focus ()
490 Keyboard::magic_widget_drop_focus ();
494 /* move focus back to the default widget in the top level window */
496 Widget* top = get_toplevel();
498 if (top->is_toplevel ()) {
499 Window* win = dynamic_cast<Window*> (top);
506 AudioClock::parse_as_frames_distance (const std::string& str)
510 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
518 AudioClock::parse_as_minsec_distance (const std::string& str)
520 framecnt_t sr = _session->frame_rate();
526 switch (str.length()) {
533 sscanf (str.c_str(), "%" PRId32, &msecs);
534 return msecs * (sr / 1000);
537 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
538 return (secs * sr) + (msecs * (sr/1000));
541 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
542 return (secs * sr) + (msecs * (sr/1000));
545 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
546 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
549 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
550 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
553 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
554 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
557 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
558 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
568 AudioClock::parse_as_timecode_distance (const std::string& str)
570 double fps = _session->timecode_frames_per_second();
571 framecnt_t sr = _session->frame_rate();
577 switch (str.length()) {
582 sscanf (str.c_str(), "%" PRId32, &frames);
583 return lrint ((frames/(float)fps) * sr);
586 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
587 return (secs * sr) + lrint ((frames/(float)fps) * sr);
590 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
591 return (secs * sr) + lrint ((frames/(float)fps) * sr);
594 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
595 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
598 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
599 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
602 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
603 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
606 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
607 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
617 AudioClock::parse_as_bbt_distance (const std::string& str)
623 AudioClock::parse_as_distance (const std::string& instr)
627 /* the input string is in reverse order */
629 std::reverse (str.begin(), str.end());
633 return parse_as_timecode_distance (str);
636 return parse_as_frames_distance (str);
639 return parse_as_bbt_distance (str);
642 return parse_as_minsec_distance (str);
649 AudioClock::end_edit_relative (bool add)
651 framecnt_t frames = parse_as_distance (input_string);
656 _layout->set_attributes (normal_attributes);
660 set (current_time() + frames, true);
662 framepos_t c = current_time();
665 set (c - frames, true);
670 ValueChanged (); /* EMIT SIGNAL */
673 input_string.clear ();
679 AudioClock::session_configuration_changed (std::string p)
681 if (p == "sync-source" || p == "external-sync") {
682 set (current_time(), true);
686 if (p != "timecode-offset" && p != "timecode-offset-negative") {
695 current = current_duration ();
697 current = current_time ();
707 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
709 if ((!force && !is_visible()) || _session == 0) {
714 when = when - offset;
717 if (when == last_when && !force) {
725 set_timecode (when, force);
729 set_bbt (when, force);
733 set_minsec (when, force);
737 set_frames (when, force);
747 AudioClock::set_frames (framepos_t when, bool /*force*/)
750 bool negative = false;
753 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
756 _left_layout->set_text ("");
757 _right_layout->set_text ("");
769 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
771 snprintf (buf, sizeof (buf), " %10" PRId64, when);
774 _layout->set_text (buf);
777 framecnt_t rate = _session->frame_rate();
779 if (fmod (rate, 100.0) == 0.0) {
780 sprintf (buf, "SR %.1fkHz", rate/1000.0);
782 sprintf (buf, "SR %" PRId64, rate);
785 _left_layout->set_text (buf);
787 float vid_pullup = _session->config.get_video_pullup();
789 if (vid_pullup == 0.0) {
790 _right_layout->set_text (_("pullup: \u2012"));
792 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
793 _right_layout->set_text (buf);
799 AudioClock::set_minsec (framepos_t when, bool force)
807 bool negative = false;
810 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
813 _left_layout->set_text ("");
814 _right_layout->set_text ("");
826 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
827 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
828 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
829 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
830 secs = (int) floor (left / (float) _session->frame_rate());
831 left -= (framecnt_t) floor (secs * _session->frame_rate());
832 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
835 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
837 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
840 _layout->set_text (buf);
844 AudioClock::set_timecode (framepos_t when, bool force)
848 bool negative = false;
851 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
853 _left_layout->set_text ("");
854 _right_layout->set_text ("");
866 _session->timecode_duration (when, TC);
868 _session->timecode_time (when, TC);
871 if (TC.negative || negative) {
872 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
874 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
877 _layout->set_text (buf);
881 if (_session->config.get_external_sync()) {
882 switch (_session->config.get_sync_source()) {
884 _left_layout->set_text ("JACK");
887 _left_layout->set_text ("MTC");
890 _left_layout->set_text ("M-Clock");
894 _left_layout->set_text ("INT");
897 double timecode_frames = _session->timecode_frames_per_second();
899 if (fmod(timecode_frames, 1.0) == 0.0) {
900 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
902 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
905 _right_layout->set_text (buf);
910 AudioClock::set_bbt (framepos_t when, bool force)
913 Timecode::BBT_Time BBT;
914 bool negative = false;
917 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
919 _left_layout->set_text ("");
920 _right_layout->set_text ("");
930 /* handle a common case */
937 _session->tempo_map().bbt_time (when, BBT);
942 _session->tempo_map().bbt_time (when, BBT);
946 snprintf (buf, sizeof (buf), "-%03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
948 snprintf (buf, sizeof (buf), " %03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
951 _layout->set_text (buf);
956 if (bbt_reference_time < 0) {
959 pos = bbt_reference_time;
962 TempoMetric m (_session->tempo_map().metric_at (pos));
964 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
965 _left_layout->set_text (buf);
967 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
968 _right_layout->set_text (buf);
973 AudioClock::set_session (Session *s)
975 SessionHandlePtr::set_session (s);
979 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
981 const XMLProperty* prop;
982 XMLNode* node = _session->extra_xml (X_("ClockModes"));
983 AudioClock::Mode amode;
986 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
987 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
989 if ((prop = (*i)->property (X_("mode"))) != 0) {
990 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
993 if ((prop = (*i)->property (X_("on"))) != 0) {
994 set_off (!string_is_affirmative (prop->value()));
1001 set (last_when, true);
1006 AudioClock::on_key_press_event (GdkEventKey* ev)
1012 /* return true for keys that we MIGHT use
1015 switch (ev->keyval) {
1038 case GDK_KP_Decimal:
1050 AudioClock::on_key_release_event (GdkEventKey *ev)
1059 switch (ev->keyval) {
1102 case GDK_KP_Subtract:
1103 end_edit_relative (false);
1109 end_edit_relative (true);
1122 ChangeAborted(); /* EMIT SIGNAL */
1127 input_string = input_string.substr (1, input_string.length() - 1);
1128 goto use_input_string;
1134 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1135 /* eat the key event, but do no nothing with it */
1139 input_string.insert (input_string.begin(), new_char);
1143 string::reverse_iterator ri;
1144 vector<int> insert_at;
1145 int highlight_length = 0;
1149 /* merge with pre-edit-string into edit string */
1153 /* get this one in the right order, and to the right width */
1154 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1155 edit_string.push_back (new_char);
1157 edit_string = edit_string.substr (0, edit_string.length() - 1);
1159 if (!edit_string.empty()) {
1160 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1161 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1164 highlight_length = edit_string.length();
1168 edit_string = pre_edit_string;
1170 /* backup through the original string, till we have
1171 * enough digits locations to put all the digits from
1175 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1176 if (isdigit (*ri)) {
1177 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1178 if (insert_at.size() == input_string.length()) {
1184 if (insert_at.size() != input_string.length()) {
1185 error << "something went wrong " << endmsg;
1187 for (int i = input_string.length() - 1; i >= 0; --i) {
1188 edit_string[insert_at[i]] = input_string[i];
1191 highlight_length = edit_string.length() - insert_at.back();
1197 if (edit_string != _layout->get_text()) {
1198 show_edit_status (highlight_length);
1199 _layout->set_text (edit_string);
1207 AudioClock::index_to_field (int index) const
1212 return Timecode_Hours;
1213 } else if (index < 7) {
1214 return Timecode_Minutes;
1215 } else if (index < 10) {
1216 return Timecode_Seconds;
1218 return Timecode_Frames;
1224 } else if (index < 7) {
1232 return Timecode_Hours;
1233 } else if (index < 6) {
1235 } else if (index < 9) {
1238 return MS_Milliseconds;
1250 AudioClock::on_button_press_event (GdkEventButton *ev)
1252 switch (ev->button) {
1254 if (editable && !_off) {
1256 /* make absolutely sure that the pointer is grabbed */
1257 gdk_pointer_grab(ev->window,false ,
1258 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1259 NULL,NULL,ev->time);
1261 drag_start_y = ev->y;
1269 /* the text has been centered vertically, so adjust
1273 y = ev->y - ((upper_height - layout_height)/2);
1274 x = ev->x - x_leading_padding;
1276 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1277 drag_field = index_to_field (index);
1279 drag_field = Field (0);
1293 AudioClock::on_button_release_event (GdkEventButton *ev)
1295 if (editable && !_off) {
1297 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1299 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1300 // we actually dragged so return without
1301 // setting editing focus, or we shift clicked
1304 if (ev->button == 1) {
1312 if (Keyboard::is_context_menu_event (ev)) {
1313 if (ops_menu == 0) {
1316 ops_menu->popup (1, ev->time);
1324 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1326 bool ret = CairoWidget::on_focus_out_event (ev);
1336 AudioClock::on_scroll_event (GdkEventScroll *ev)
1341 if (editing || _session == 0 || !editable || _off) {
1348 /* the text has been centered vertically, so adjust
1352 y = ev->y - ((upper_height - layout_height)/2);
1353 x = ev->x - x_leading_padding;
1355 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1356 /* not in the main layout */
1360 Field f = index_to_field (index);
1361 framepos_t frames = 0;
1363 switch (ev->direction) {
1366 frames = get_frame_step (f);
1368 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1371 set (current_time() + frames, true);
1372 ValueChanged (); /* EMIT_SIGNAL */
1376 case GDK_SCROLL_DOWN:
1377 frames = get_frame_step (f);
1379 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1383 if ((double)current_time() - (double)frames < 0.0) {
1386 set (current_time() - frames, true);
1389 ValueChanged (); /* EMIT_SIGNAL */
1402 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1404 if (editing || _session == 0 || !dragging) {
1408 float pixel_frame_scale_factor = 0.2f;
1410 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1411 pixel_frame_scale_factor = 0.1f;
1415 if (Keyboard::modifier_state_contains (ev->state,
1416 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1418 pixel_frame_scale_factor = 0.025f;
1421 double y_delta = ev->y - drag_y;
1423 drag_accum += y_delta*pixel_frame_scale_factor;
1427 if (trunc (drag_accum) != 0) {
1432 dir = (drag_accum < 0 ? 1:-1);
1433 pos = current_time();
1434 frames = get_frame_step (drag_field,pos,dir);
1436 if (frames != 0 && frames * drag_accum < current_time()) {
1437 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1443 ValueChanged(); /* EMIT_SIGNAL */
1450 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1453 Timecode::BBT_Time BBT;
1455 case Timecode_Hours:
1456 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1458 case Timecode_Minutes:
1459 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1461 case Timecode_Seconds:
1462 f = _session->frame_rate();
1464 case Timecode_Frames:
1465 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1473 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1476 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1479 f = (framecnt_t) _session->frame_rate();
1481 case MS_Milliseconds:
1482 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1489 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1495 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1501 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1504 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1513 AudioClock::current_time (framepos_t pos) const
1519 AudioClock::current_duration (framepos_t pos) const
1528 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1544 AudioClock::bbt_validate_edit (const string& str)
1548 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1550 if (!is_duration && any.bbt.bars == 0) {
1554 if (!is_duration && any.bbt.beats == 0) {
1562 AudioClock::timecode_validate_edit (const string& str)
1566 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1567 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1571 if (TC.minutes > 59 || TC.seconds > 59) {
1575 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1579 if (_session->timecode_drop_frames()) {
1580 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1589 AudioClock::frames_from_timecode_string (const string& str) const
1591 if (_session == 0) {
1598 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1599 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1603 TC.rate = _session->timecode_frames_per_second();
1604 TC.drop= _session->timecode_drop_frames();
1606 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1608 // timecode_tester ();
1614 AudioClock::frames_from_minsec_string (const string& str) const
1616 if (_session == 0) {
1620 int hrs, mins, secs, millisecs;
1621 framecnt_t sr = _session->frame_rate();
1623 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1624 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1628 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1632 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1634 if (_session == 0) {
1635 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1640 any.type = AnyTime::BBT;
1642 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1647 return _session->any_duration_to_frames (pos, any);
1649 return _session->convert_to_frames (any);
1655 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1657 if (_session == 0) {
1658 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1662 Timecode::BBT_Time bbt;
1664 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1666 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1670 AudioClock::frames_from_audioframes_string (const string& str) const
1673 sscanf (str.c_str(), "%" PRId64, &f);
1678 AudioClock::build_ops_menu ()
1680 using namespace Menu_Helpers;
1681 ops_menu = new Menu;
1682 MenuList& ops_items = ops_menu->items();
1683 ops_menu->set_name ("ArdourContextMenu");
1685 if (!Profile->get_sae()) {
1686 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1688 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1689 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1690 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1692 if (editable && !_off && !is_duration && !_follows_playhead) {
1693 ops_items.push_back (SeparatorElem());
1694 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1695 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1700 AudioClock::set_from_playhead ()
1706 set (_session->transport_frame());
1711 AudioClock::locate ()
1713 if (!_session || is_duration) {
1717 _session->request_locate (current_time(), _session->transport_rolling ());
1721 AudioClock::set_mode (Mode m)
1731 _layout->set_text ("");
1734 _left_layout->set_text ("");
1735 _right_layout->set_text ("");
1740 mode_based_info_ratio = 0.5;
1741 insert_map.push_back (11);
1742 insert_map.push_back (10);
1743 insert_map.push_back (8);
1744 insert_map.push_back (7);
1745 insert_map.push_back (5);
1746 insert_map.push_back (4);
1747 insert_map.push_back (2);
1748 insert_map.push_back (1);
1752 mode_based_info_ratio = 0.5;
1753 insert_map.push_back (11);
1754 insert_map.push_back (10);
1755 insert_map.push_back (9);
1756 insert_map.push_back (8);
1757 insert_map.push_back (6);
1758 insert_map.push_back (5);
1759 insert_map.push_back (3);
1760 insert_map.push_back (2);
1761 insert_map.push_back (1);
1765 mode_based_info_ratio = 1.0;
1766 insert_map.push_back (12);
1767 insert_map.push_back (11);
1768 insert_map.push_back (10);
1769 insert_map.push_back (8);
1770 insert_map.push_back (7);
1771 insert_map.push_back (5);
1772 insert_map.push_back (4);
1773 insert_map.push_back (2);
1774 insert_map.push_back (1);
1778 mode_based_info_ratio = 0.5;
1782 set (last_when, true);
1784 if (!is_transient) {
1785 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1788 mode_changed (); /* EMIT SIGNAL (the member one) */
1792 AudioClock::set_bbt_reference (framepos_t pos)
1794 bbt_reference_time = pos;
1798 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1800 CairoWidget::on_style_changed (old_style);
1806 AudioClock::set_editable (bool yn)
1812 AudioClock::set_is_duration (bool yn)
1814 if (yn == is_duration) {
1819 set (last_when, true);
1823 AudioClock::set_off (bool yn)
1831 /* force a redraw. last_when will be preserved, but the clock text will
1835 set (last_when, true);
1839 AudioClock::focus ()