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 (" 88|88:88:88,888");
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 /* move focus back to the default widget in the top level window */
492 Keyboard::magic_widget_drop_focus ();
493 Widget* top = get_toplevel();
494 if (top->is_toplevel ()) {
495 Window* win = dynamic_cast<Window*> (top);
501 AudioClock::parse_as_frames_distance (const std::string& str)
505 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
513 AudioClock::parse_as_minsec_distance (const std::string& str)
515 framecnt_t sr = _session->frame_rate();
521 switch (str.length()) {
528 sscanf (str.c_str(), "%" PRId32, &msecs);
529 return msecs * (sr / 1000);
532 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
533 return (secs * sr) + (msecs * (sr/1000));
536 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
537 return (secs * sr) + (msecs * (sr/1000));
540 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
541 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
544 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
545 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
548 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
549 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
552 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
553 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
563 AudioClock::parse_as_timecode_distance (const std::string& str)
565 double fps = _session->timecode_frames_per_second();
566 framecnt_t sr = _session->frame_rate();
572 switch (str.length()) {
577 sscanf (str.c_str(), "%" PRId32, &frames);
578 return lrint ((frames/(float)fps) * sr);
581 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
582 return (secs * sr) + lrint ((frames/(float)fps) * sr);
585 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
586 return (secs * sr) + lrint ((frames/(float)fps) * sr);
589 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
590 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
593 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
594 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
597 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
598 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
601 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
602 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
612 AudioClock::parse_as_bbt_distance (const std::string& str)
618 AudioClock::parse_as_distance (const std::string& instr)
622 /* the input string is in reverse order */
624 std::reverse (str.begin(), str.end());
628 return parse_as_timecode_distance (str);
631 return parse_as_frames_distance (str);
634 return parse_as_bbt_distance (str);
637 return parse_as_minsec_distance (str);
644 AudioClock::end_edit_relative (bool add)
646 framecnt_t frames = parse_as_distance (input_string);
651 _layout->set_attributes (normal_attributes);
655 set (current_time() + frames, true);
657 framepos_t c = current_time();
660 set (c - frames, true);
665 ValueChanged (); /* EMIT SIGNAL */
668 input_string.clear ();
674 AudioClock::session_configuration_changed (std::string p)
676 if (p == "sync-source" || p == "external-sync") {
677 set (current_time(), true);
681 if (p != "timecode-offset" && p != "timecode-offset-negative") {
690 current = current_duration ();
692 current = current_time ();
702 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
704 if ((!force && !is_visible()) || _session == 0) {
709 when = when - offset;
712 if (when == last_when && !force) {
720 set_timecode (when, force);
724 set_bbt (when, force);
728 set_minsec (when, force);
732 set_frames (when, force);
742 AudioClock::set_frames (framepos_t when, bool /*force*/)
745 bool negative = false;
748 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
751 _left_layout->set_text ("");
752 _right_layout->set_text ("");
764 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
766 snprintf (buf, sizeof (buf), " %10" PRId64, when);
769 _layout->set_text (buf);
772 framecnt_t rate = _session->frame_rate();
774 if (fmod (rate, 100.0) == 0.0) {
775 sprintf (buf, "SR %.1fkHz", rate/1000.0);
777 sprintf (buf, "SR %" PRId64, rate);
780 _left_layout->set_text (buf);
782 float vid_pullup = _session->config.get_video_pullup();
784 if (vid_pullup == 0.0) {
785 _right_layout->set_text (_("pullup: \u2012"));
787 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
788 _right_layout->set_text (buf);
794 AudioClock::set_minsec (framepos_t when, bool force)
802 bool negative = false;
805 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
808 _left_layout->set_text ("");
809 _right_layout->set_text ("");
821 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
822 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
823 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
824 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
825 secs = (int) floor (left / (float) _session->frame_rate());
826 left -= (framecnt_t) floor (secs * _session->frame_rate());
827 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
830 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
832 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
835 _layout->set_text (buf);
839 AudioClock::set_timecode (framepos_t when, bool force)
843 bool negative = false;
846 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
848 _left_layout->set_text ("");
849 _right_layout->set_text ("");
861 _session->timecode_duration (when, TC);
863 _session->timecode_time (when, TC);
866 if (TC.negative || negative) {
867 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
869 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
872 _layout->set_text (buf);
876 if (_session->config.get_external_sync()) {
877 switch (_session->config.get_sync_source()) {
879 _left_layout->set_text ("JACK");
882 _left_layout->set_text ("MTC");
885 _left_layout->set_text ("M-Clock");
889 _left_layout->set_text ("INT");
892 double timecode_frames = _session->timecode_frames_per_second();
894 if (fmod(timecode_frames, 1.0) == 0.0) {
895 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
897 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
900 _right_layout->set_text (buf);
905 AudioClock::set_bbt (framepos_t when, bool force)
908 Timecode::BBT_Time BBT;
909 bool negative = false;
912 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
914 _left_layout->set_text ("");
915 _right_layout->set_text ("");
925 /* handle a common case */
932 _session->tempo_map().bbt_time (when, BBT);
937 _session->tempo_map().bbt_time (when, BBT);
941 snprintf (buf, sizeof (buf), "-%03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
943 snprintf (buf, sizeof (buf), " %03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
946 _layout->set_text (buf);
951 if (bbt_reference_time < 0) {
954 pos = bbt_reference_time;
957 TempoMetric m (_session->tempo_map().metric_at (pos));
959 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
960 _left_layout->set_text (buf);
962 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
963 _right_layout->set_text (buf);
968 AudioClock::set_session (Session *s)
970 SessionHandlePtr::set_session (s);
974 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
976 const XMLProperty* prop;
977 XMLNode* node = _session->extra_xml (X_("ClockModes"));
978 AudioClock::Mode amode;
981 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
982 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
984 if ((prop = (*i)->property (X_("mode"))) != 0) {
985 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
988 if ((prop = (*i)->property (X_("on"))) != 0) {
989 set_off (!string_is_affirmative (prop->value()));
996 set (last_when, true);
1001 AudioClock::on_key_press_event (GdkEventKey* ev)
1007 /* return true for keys that we MIGHT use
1010 switch (ev->keyval) {
1033 case GDK_KP_Decimal:
1045 AudioClock::on_key_release_event (GdkEventKey *ev)
1054 switch (ev->keyval) {
1097 case GDK_KP_Subtract:
1098 end_edit_relative (false);
1104 end_edit_relative (true);
1117 ChangeAborted(); /* EMIT SIGNAL */
1122 input_string = input_string.substr (1, input_string.length() - 1);
1123 goto use_input_string;
1129 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1130 /* eat the key event, but do no nothing with it */
1134 input_string.insert (input_string.begin(), new_char);
1138 string::reverse_iterator ri;
1139 vector<int> insert_at;
1140 int highlight_length = 0;
1144 /* merge with pre-edit-string into edit string */
1148 /* get this one in the right order, and to the right width */
1149 if (ev->keyval != GDK_Delete && ev->keyval != GDK_BackSpace) {
1150 edit_string.push_back (new_char);
1152 edit_string = edit_string.substr (0, edit_string.length() - 1);
1154 if (!edit_string.empty()) {
1155 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1156 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1159 highlight_length = edit_string.length();
1163 edit_string = pre_edit_string;
1165 /* backup through the original string, till we have
1166 * enough digits locations to put all the digits from
1170 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1171 if (isdigit (*ri)) {
1172 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1173 if (insert_at.size() == input_string.length()) {
1179 if (insert_at.size() != input_string.length()) {
1180 error << "something went wrong " << endmsg;
1182 for (int i = input_string.length() - 1; i >= 0; --i) {
1183 edit_string[insert_at[i]] = input_string[i];
1186 highlight_length = edit_string.length() - insert_at.back();
1192 if (edit_string != _layout->get_text()) {
1193 show_edit_status (highlight_length);
1194 _layout->set_text (edit_string);
1202 AudioClock::index_to_field (int index) const
1207 return Timecode_Hours;
1208 } else if (index < 7) {
1209 return Timecode_Minutes;
1210 } else if (index < 10) {
1211 return Timecode_Seconds;
1212 } else if (index < 13) {
1213 return Timecode_Frames;
1219 } else if (index < 7) {
1221 } else if (index < 12) {
1227 return Timecode_Hours;
1228 } else if (index < 6) {
1230 } else if (index < 9) {
1232 } else if (index < 12) {
1233 return MS_Milliseconds;
1245 AudioClock::on_button_press_event (GdkEventButton *ev)
1247 switch (ev->button) {
1249 if (editable && !_off) {
1251 /* make absolutely sure that the pointer is grabbed */
1252 gdk_pointer_grab(ev->window,false ,
1253 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1254 NULL,NULL,ev->time);
1256 drag_start_y = ev->y;
1264 /* the text has been centered vertically, so adjust
1268 y = ev->y - ((upper_height - layout_height)/2);
1269 x = ev->x - x_leading_padding;
1271 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1272 drag_field = index_to_field (index);
1274 drag_field = Field (0);
1288 AudioClock::on_button_release_event (GdkEventButton *ev)
1290 if (editable && !_off) {
1292 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1294 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1295 // we actually dragged so return without
1296 // setting editing focus, or we shift clicked
1299 if (ev->button == 1) {
1307 if (Keyboard::is_context_menu_event (ev)) {
1308 if (ops_menu == 0) {
1311 ops_menu->popup (1, ev->time);
1319 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1321 bool ret = CairoWidget::on_focus_out_event (ev);
1331 AudioClock::on_scroll_event (GdkEventScroll *ev)
1336 if (editing || _session == 0 || !editable || _off) {
1343 /* the text has been centered vertically, so adjust
1347 y = ev->y - ((upper_height - layout_height)/2);
1348 x = ev->x - x_leading_padding;
1350 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1351 /* not in the main layout */
1355 Field f = index_to_field (index);
1356 framepos_t frames = 0;
1358 switch (ev->direction) {
1361 frames = get_frame_step (f);
1363 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1366 set (current_time() + frames, true);
1367 ValueChanged (); /* EMIT_SIGNAL */
1371 case GDK_SCROLL_DOWN:
1372 frames = get_frame_step (f);
1374 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1378 if ((double)current_time() - (double)frames < 0.0) {
1381 set (current_time() - frames, true);
1384 ValueChanged (); /* EMIT_SIGNAL */
1397 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1399 if (editing || _session == 0 || !dragging) {
1403 float pixel_frame_scale_factor = 0.2f;
1405 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1406 pixel_frame_scale_factor = 0.1f;
1410 if (Keyboard::modifier_state_contains (ev->state,
1411 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1413 pixel_frame_scale_factor = 0.025f;
1416 double y_delta = ev->y - drag_y;
1418 drag_accum += y_delta*pixel_frame_scale_factor;
1422 if (trunc (drag_accum) != 0) {
1427 dir = (drag_accum < 0 ? 1:-1);
1428 pos = current_time();
1429 frames = get_frame_step (drag_field,pos,dir);
1431 if (frames != 0 && frames * drag_accum < current_time()) {
1432 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1438 ValueChanged(); /* EMIT_SIGNAL */
1445 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1448 Timecode::BBT_Time BBT;
1450 case Timecode_Hours:
1451 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1453 case Timecode_Minutes:
1454 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1456 case Timecode_Seconds:
1457 f = _session->frame_rate();
1459 case Timecode_Frames:
1460 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1468 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1471 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1474 f = (framecnt_t) _session->frame_rate();
1476 case MS_Milliseconds:
1477 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1484 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1490 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1496 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1499 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1508 AudioClock::current_time (framepos_t pos) const
1514 AudioClock::current_duration (framepos_t pos) const
1523 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1539 AudioClock::bbt_validate_edit (const string& str)
1543 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1545 if (!is_duration && any.bbt.bars == 0) {
1549 if (!is_duration && any.bbt.beats == 0) {
1557 AudioClock::timecode_validate_edit (const string& str)
1561 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1562 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1566 if (TC.minutes > 59 || TC.seconds > 59) {
1570 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1574 if (_session->timecode_drop_frames()) {
1575 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1584 AudioClock::frames_from_timecode_string (const string& str) const
1586 if (_session == 0) {
1593 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1594 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1598 TC.rate = _session->timecode_frames_per_second();
1599 TC.drop= _session->timecode_drop_frames();
1601 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1603 // timecode_tester ();
1609 AudioClock::frames_from_minsec_string (const string& str) const
1611 if (_session == 0) {
1615 int hrs, mins, secs, millisecs;
1616 framecnt_t sr = _session->frame_rate();
1618 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1619 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1623 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1627 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1629 if (_session == 0) {
1630 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1635 any.type = AnyTime::BBT;
1637 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1642 return _session->any_duration_to_frames (pos, any);
1644 return _session->convert_to_frames (any);
1650 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1652 if (_session == 0) {
1653 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1657 Timecode::BBT_Time bbt;
1659 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1661 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1665 AudioClock::frames_from_audioframes_string (const string& str) const
1668 sscanf (str.c_str(), "%" PRId64, &f);
1673 AudioClock::build_ops_menu ()
1675 using namespace Menu_Helpers;
1676 ops_menu = new Menu;
1677 MenuList& ops_items = ops_menu->items();
1678 ops_menu->set_name ("ArdourContextMenu");
1680 if (!Profile->get_sae()) {
1681 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1683 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1684 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1685 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1687 if (editable && !_off && !is_duration && !_follows_playhead) {
1688 ops_items.push_back (SeparatorElem());
1689 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1690 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1695 AudioClock::set_from_playhead ()
1701 set (_session->transport_frame());
1706 AudioClock::locate ()
1708 if (!_session || is_duration) {
1712 _session->request_locate (current_time(), _session->transport_rolling ());
1716 AudioClock::set_mode (Mode m)
1726 _layout->set_text ("");
1729 _left_layout->set_text ("");
1730 _right_layout->set_text ("");
1735 mode_based_info_ratio = 0.5;
1736 insert_map.push_back (11);
1737 insert_map.push_back (10);
1738 insert_map.push_back (8);
1739 insert_map.push_back (7);
1740 insert_map.push_back (5);
1741 insert_map.push_back (4);
1742 insert_map.push_back (2);
1743 insert_map.push_back (1);
1747 mode_based_info_ratio = 0.5;
1748 insert_map.push_back (11);
1749 insert_map.push_back (10);
1750 insert_map.push_back (9);
1751 insert_map.push_back (8);
1752 insert_map.push_back (6);
1753 insert_map.push_back (5);
1754 insert_map.push_back (3);
1755 insert_map.push_back (2);
1756 insert_map.push_back (1);
1760 mode_based_info_ratio = 1.0;
1761 insert_map.push_back (12);
1762 insert_map.push_back (11);
1763 insert_map.push_back (10);
1764 insert_map.push_back (8);
1765 insert_map.push_back (7);
1766 insert_map.push_back (5);
1767 insert_map.push_back (4);
1768 insert_map.push_back (2);
1769 insert_map.push_back (1);
1773 mode_based_info_ratio = 0.5;
1777 set (last_when, true);
1779 if (!is_transient) {
1780 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1783 mode_changed (); /* EMIT SIGNAL (the member one) */
1787 AudioClock::set_bbt_reference (framepos_t pos)
1789 bbt_reference_time = pos;
1793 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1795 CairoWidget::on_style_changed (old_style);
1801 AudioClock::set_editable (bool yn)
1807 AudioClock::set_is_duration (bool yn)
1809 if (yn == is_duration) {
1814 set (last_when, true);
1818 AudioClock::set_off (bool yn)
1826 /* force a redraw. last_when will be preserved, but the clock text will
1830 set (last_when, true);
1834 AudioClock::focus ()