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);
284 const double cursor_width = 16.0; /* need em width here, not 16 */
286 if (!insert_map.empty()) {
287 Pango::Rectangle cursor = _layout->get_cursor_strong_pos (insert_map[input_string.length()]);
289 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.4);
291 x_leading_padding + cursor.get_x()/PANGO_SCALE,
292 (upper_height - layout_height)/2.0,
293 cursor_width, cursor.get_height()/PANGO_SCALE);
296 if (input_string.empty()) {
297 cairo_set_source_rgba (cr, 0.9, 0.1, 0.1, 0.4);
299 (get_width()/2.0) - cursor_width,
300 (upper_height - layout_height)/2.0,
301 cursor_width, upper_height);
309 AudioClock::on_size_allocate (Gtk::Allocation& alloc)
311 CairoWidget::on_size_allocate (alloc);
314 upper_height = (get_height()/2.0) - 1.0;
316 upper_height = get_height();
321 AudioClock::on_size_request (Gtk::Requisition* req)
323 Glib::RefPtr<Pango::Layout> tmp;
324 Glib::RefPtr<Gtk::Style> style = get_style ();
325 Pango::FontDescription font;
327 tmp = Pango::Layout::create (get_pango_context());
329 if (!is_realized()) {
330 font = get_font_for_style (get_name());
332 font = style->get_font();
335 tmp->set_font_description (font);
337 /* this string is the longest thing we will ever display,
338 and also includes the BBT "|" that may descends below
339 the baseline a bit, and a comma for the minsecs mode
340 where we printf a fractional value (XXX or should)
343 tmp->set_text (" 88|88:88:88,888");
345 tmp->get_pixel_size (req->width, req->height);
347 layout_height = req->height;
348 layout_width = req->width;
350 /* now tackle height, for which we need to know the height of the lower
358 font.set_size ((int) lrint (font.get_size() * info_font_scale_factor));
359 font.set_weight (Pango::WEIGHT_NORMAL);
360 tmp->set_font_description (font);
362 /* we only care about height, so put as much stuff in here
363 as possible that might change the height.
365 tmp->set_text ("qyhH|"); /* one ascender, one descender */
367 tmp->get_pixel_size (w, info_height);
369 /* silly extra padding that seems necessary to correct the info
370 * that pango just gave us. I have no idea why.
375 req->height += info_height;
376 req->height += separator_height;
381 AudioClock::show_edit_status (int length)
383 editing_attr->set_start_index (edit_string.length() - length);
384 editing_attr->set_end_index (edit_string.length());
386 editing_attributes.change (*foreground_attr);
387 editing_attributes.change (*editing_attr);
389 _layout->set_attributes (editing_attributes);
393 AudioClock::start_edit ()
395 pre_edit_string = _layout->get_text ();
396 if (!insert_map.empty()) {
397 edit_string = pre_edit_string;
399 edit_string.clear ();
400 _layout->set_text ("");
402 input_string.clear ();
407 Keyboard::magic_widget_grab_focus ();
412 AudioClock::end_edit (bool modify)
420 ok = timecode_validate_edit (edit_string);
424 ok = bbt_validate_edit (edit_string);
435 edit_string = pre_edit_string;
436 input_string.clear ();
437 _layout->set_text (edit_string);
438 show_edit_status (1);
439 /* edit attributes remain in use */
447 pos = frames_from_timecode_string (edit_string);
452 pos = frame_duration_from_bbt_string (0, edit_string);
454 pos = frames_from_bbt_string (0, edit_string);
459 pos = frames_from_minsec_string (edit_string);
463 pos = frames_from_audioframes_string (edit_string);
468 _layout->set_attributes (normal_attributes);
469 ValueChanged(); /* EMIT_SIGNAL */
475 _layout->set_attributes (normal_attributes);
476 _layout->set_text (pre_edit_string);
487 AudioClock::drop_focus ()
489 /* move focus back to the default widget in the top level window */
491 Keyboard::magic_widget_drop_focus ();
492 Widget* top = get_toplevel();
493 if (top->is_toplevel ()) {
494 Window* win = dynamic_cast<Window*> (top);
500 AudioClock::parse_as_frames_distance (const std::string& str)
504 if (sscanf (str.c_str(), "%" PRId64, &f) == 1) {
512 AudioClock::parse_as_minsec_distance (const std::string& str)
514 framecnt_t sr = _session->frame_rate();
520 switch (str.length()) {
527 sscanf (str.c_str(), "%" PRId32, &msecs);
528 return msecs * (sr / 1000);
531 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &msecs);
532 return (secs * sr) + (msecs * (sr/1000));
535 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &msecs);
536 return (secs * sr) + (msecs * (sr/1000));
539 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
540 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
543 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &msecs);
544 return (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
547 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
548 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
551 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &msecs);
552 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + (msecs * (sr/1000));
562 AudioClock::parse_as_timecode_distance (const std::string& str)
564 double fps = _session->timecode_frames_per_second();
565 framecnt_t sr = _session->frame_rate();
571 switch (str.length()) {
576 sscanf (str.c_str(), "%" PRId32, &frames);
577 return lrint ((frames/(float)fps) * sr);
580 sscanf (str.c_str(), "%1" PRId32 "%" PRId32, &secs, &frames);
581 return (secs * sr) + lrint ((frames/(float)fps) * sr);
584 sscanf (str.c_str(), "%2" PRId32 "%" PRId32, &secs, &frames);
585 return (secs * sr) + lrint ((frames/(float)fps) * sr);
588 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
589 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
592 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%" PRId32, &mins, &secs, &frames);
593 return (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
596 sscanf (str.c_str(), "%1" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
597 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
600 sscanf (str.c_str(), "%2" PRId32 "%2" PRId32 "%2" PRId32 "%" PRId32, &hrs, &mins, &secs, &frames);
601 return (hrs * 3600 * sr) + (mins * 60 * sr) + (secs * sr) + lrint ((frames/(float)fps) * sr);
611 AudioClock::parse_as_bbt_distance (const std::string& str)
617 AudioClock::parse_as_distance (const std::string& instr)
621 /* the input string is in reverse order */
623 std::reverse (str.begin(), str.end());
627 return parse_as_timecode_distance (str);
630 return parse_as_frames_distance (str);
633 return parse_as_bbt_distance (str);
636 return parse_as_minsec_distance (str);
643 AudioClock::end_edit_relative (bool add)
645 framecnt_t frames = parse_as_distance (input_string);
650 _layout->set_attributes (normal_attributes);
654 set (current_time() + frames, true);
656 framepos_t c = current_time();
659 set (c - frames, true);
664 ValueChanged (); /* EMIT SIGNAL */
667 input_string.clear ();
673 AudioClock::session_configuration_changed (std::string p)
675 if (p != "timecode-offset" && p != "timecode-offset-negative") {
684 current = current_duration ();
686 current = current_time ();
696 AudioClock::set (framepos_t when, bool force, framecnt_t offset)
698 if ((!force && !is_visible()) || _session == 0) {
703 when = when - offset;
706 if (when == last_when && !force) {
714 set_timecode (when, force);
718 set_bbt (when, force);
722 set_minsec (when, force);
726 set_frames (when, force);
731 if (when != last_when || force) {
739 AudioClock::set_frames (framepos_t when, bool /*force*/)
742 bool negative = false;
745 _layout->set_text ("\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012\u2012");
748 _left_layout->set_text ("");
749 _right_layout->set_text ("");
761 snprintf (buf, sizeof (buf), "-%10" PRId64, when);
763 snprintf (buf, sizeof (buf), " %10" PRId64, when);
766 _layout->set_text (buf);
769 framecnt_t rate = _session->frame_rate();
771 if (fmod (rate, 100.0) == 0.0) {
772 sprintf (buf, "SR %.1fkHz", rate/1000.0);
774 sprintf (buf, "SR %" PRId64, rate);
777 _left_layout->set_text (buf);
779 float vid_pullup = _session->config.get_video_pullup();
781 if (vid_pullup == 0.0) {
782 _right_layout->set_text (_("pullup: \u2012"));
784 sprintf (buf, _("pullup %-6.4f"), vid_pullup);
785 _right_layout->set_text (buf);
791 AudioClock::set_minsec (framepos_t when, bool force)
799 bool negative = false;
802 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012.\u2012\u2012\u2012");
805 _left_layout->set_text ("");
806 _right_layout->set_text ("");
818 hrs = (int) floor (left / (_session->frame_rate() * 60.0f * 60.0f));
819 left -= (framecnt_t) floor (hrs * _session->frame_rate() * 60.0f * 60.0f);
820 mins = (int) floor (left / (_session->frame_rate() * 60.0f));
821 left -= (framecnt_t) floor (mins * _session->frame_rate() * 60.0f);
822 secs = (int) floor (left / (float) _session->frame_rate());
823 left -= (framecnt_t) floor (secs * _session->frame_rate());
824 millisecs = floor (left * 1000.0 / (float) _session->frame_rate());
827 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
829 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
832 _layout->set_text (buf);
836 AudioClock::set_timecode (framepos_t when, bool force)
840 bool negative = false;
843 _layout->set_text ("\u2012\u2012:\u2012\u2012:\u2012\u2012:\u2012\u2012");
845 _left_layout->set_text ("");
846 _right_layout->set_text ("");
858 _session->timecode_duration (when, TC);
860 _session->timecode_time (when, TC);
863 if (TC.negative || negative) {
864 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
866 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
869 _layout->set_text (buf);
872 double timecode_frames = _session->timecode_frames_per_second();
874 if (fmod(timecode_frames, 1.0) == 0.0) {
875 sprintf (buf, "FPS %u %s", int (timecode_frames), (_session->timecode_drop_frames() ? "D" : ""));
877 sprintf (buf, "%.2f %s", timecode_frames, (_session->timecode_drop_frames() ? "D" : ""));
880 _right_layout->set_text (buf);
885 AudioClock::set_bbt (framepos_t when, bool force)
888 Timecode::BBT_Time BBT;
889 bool negative = false;
892 _layout->set_text ("\u2012\u2012\u2012|\u2012\u2012|\u2012\u2012\u2012\u2012");
894 _left_layout->set_text ("");
895 _right_layout->set_text ("");
905 /* handle a common case */
912 _session->tempo_map().bbt_time (when, BBT);
917 _session->tempo_map().bbt_time (when, BBT);
921 snprintf (buf, sizeof (buf), "-%03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
923 snprintf (buf, sizeof (buf), " %03" PRIu32 "|%02" PRIu32 "|%04" PRIu32, BBT.bars, BBT.beats, BBT.ticks);
926 _layout->set_text (buf);
931 if (bbt_reference_time < 0) {
934 pos = bbt_reference_time;
937 TempoMetric m (_session->tempo_map().metric_at (pos));
939 sprintf (buf, "%-5.2f", m.tempo().beats_per_minute());
940 _left_layout->set_text (buf);
942 sprintf (buf, "%g|%g", m.meter().beats_per_bar(), m.meter().note_divisor());
943 _right_layout->set_text (buf);
948 AudioClock::set_session (Session *s)
950 SessionHandlePtr::set_session (s);
954 _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&AudioClock::session_configuration_changed, this, _1), gui_context());
956 const XMLProperty* prop;
957 XMLNode* node = _session->extra_xml (X_("ClockModes"));
958 AudioClock::Mode amode;
961 for (XMLNodeList::const_iterator i = node->children().begin(); i != node->children().end(); ++i) {
962 if ((prop = (*i)->property (X_("name"))) && prop->value() == _name) {
964 if ((prop = (*i)->property (X_("mode"))) != 0) {
965 amode = AudioClock::Mode (string_2_enum (prop->value(), amode));
968 if ((prop = (*i)->property (X_("on"))) != 0) {
969 set_off (!string_is_affirmative (prop->value()));
976 set (last_when, true);
981 AudioClock::on_key_press_event (GdkEventKey* ev)
987 /* return true for keys that we MIGHT use
990 switch (ev->keyval) {
1013 case GDK_KP_Decimal:
1025 AudioClock::on_key_release_event (GdkEventKey *ev)
1034 switch (ev->keyval) {
1077 case GDK_KP_Subtract:
1078 end_edit_relative (false);
1084 end_edit_relative (true);
1097 ChangeAborted(); /* EMIT SIGNAL */
1104 if (!insert_map.empty() && (input_string.length() >= insert_map.size())) {
1105 /* eat the key event, but do no nothing with it */
1109 input_string.insert (input_string.begin(), new_char);
1111 string::reverse_iterator ri;
1112 vector<int> insert_at;
1113 int highlight_length;
1117 /* merge with pre-edit-string into edit string */
1121 /* get this one in the right order, and to the right width */
1122 edit_string.push_back (new_char);
1123 sscanf (edit_string.c_str(), "%" PRId64, &pos);
1124 snprintf (buf, sizeof (buf), " %10" PRId64, pos);
1126 highlight_length = edit_string.length();
1130 edit_string = pre_edit_string;
1132 /* backup through the original string, till we have
1133 * enough digits locations to put all the digits from
1137 for (ri = edit_string.rbegin(); ri != edit_string.rend(); ++ri) {
1138 if (isdigit (*ri)) {
1139 insert_at.push_back (edit_string.length() - (ri - edit_string.rbegin()) - 1);
1140 if (insert_at.size() == input_string.length()) {
1146 if (insert_at.size() != input_string.length()) {
1147 error << "something went wrong " << endmsg;
1149 for (int i = input_string.length() - 1; i >= 0; --i) {
1150 edit_string[insert_at[i]] = input_string[i];
1153 highlight_length = edit_string.length() - insert_at.back();
1159 if (edit_string != _layout->get_text()) {
1160 show_edit_status (highlight_length);
1161 _layout->set_text (edit_string);
1169 AudioClock::index_to_field (int index) const
1174 return Timecode_Hours;
1175 } else if (index < 7) {
1176 return Timecode_Minutes;
1177 } else if (index < 10) {
1178 return Timecode_Seconds;
1179 } else if (index < 13) {
1180 return Timecode_Frames;
1186 } else if (index < 7) {
1188 } else if (index < 12) {
1194 return Timecode_Hours;
1195 } else if (index < 6) {
1197 } else if (index < 9) {
1199 } else if (index < 12) {
1200 return MS_Milliseconds;
1212 AudioClock::on_button_press_event (GdkEventButton *ev)
1214 switch (ev->button) {
1216 if (editable && !_off) {
1218 /* make absolutely sure that the pointer is grabbed */
1219 gdk_pointer_grab(ev->window,false ,
1220 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
1221 NULL,NULL,ev->time);
1223 drag_start_y = ev->y;
1231 /* the text has been centered vertically, so adjust
1235 y = ev->y - ((upper_height - layout_height)/2);
1236 x = ev->x - x_leading_padding;
1238 if (_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1239 drag_field = index_to_field (index);
1241 drag_field = Field (0);
1255 AudioClock::on_button_release_event (GdkEventButton *ev)
1257 if (editable && !_off) {
1259 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1261 if (ev->y > drag_start_y+1 || ev->y < drag_start_y-1 || Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)){
1262 // we actually dragged so return without
1263 // setting editing focus, or we shift clicked
1266 if (ev->button == 1) {
1274 if (Keyboard::is_context_menu_event (ev)) {
1275 if (ops_menu == 0) {
1278 ops_menu->popup (1, ev->time);
1286 AudioClock::on_focus_out_event (GdkEventFocus* ev)
1288 bool ret = CairoWidget::on_focus_out_event (ev);
1298 AudioClock::on_scroll_event (GdkEventScroll *ev)
1303 if (_session == 0 || !editable || _off) {
1310 /* the text has been centered vertically, so adjust
1314 y = ev->y - ((upper_height - layout_height)/2);
1315 x = ev->x - x_leading_padding;
1317 if (!_layout->xy_to_index (x * PANGO_SCALE, y * PANGO_SCALE, index, trailing)) {
1318 /* not in the main layout */
1322 Field f = index_to_field (index);
1323 framepos_t frames = 0;
1325 switch (ev->direction) {
1328 frames = get_frame_step (f);
1330 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1333 set (current_time() + frames, true);
1334 ValueChanged (); /* EMIT_SIGNAL */
1338 case GDK_SCROLL_DOWN:
1339 frames = get_frame_step (f);
1341 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1345 if ((double)current_time() - (double)frames < 0.0) {
1348 set (current_time() - frames, true);
1351 ValueChanged (); /* EMIT_SIGNAL */
1364 AudioClock::on_motion_notify_event (GdkEventMotion *ev)
1366 if (_session == 0 || !dragging) {
1370 float pixel_frame_scale_factor = 0.2f;
1372 if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1373 pixel_frame_scale_factor = 0.1f;
1377 if (Keyboard::modifier_state_contains (ev->state,
1378 Keyboard::PrimaryModifier|Keyboard::SecondaryModifier)) {
1380 pixel_frame_scale_factor = 0.025f;
1383 double y_delta = ev->y - drag_y;
1385 drag_accum += y_delta*pixel_frame_scale_factor;
1389 if (trunc (drag_accum) != 0) {
1394 dir = (drag_accum < 0 ? 1:-1);
1395 pos = current_time();
1396 frames = get_frame_step (drag_field,pos,dir);
1398 if (frames != 0 && frames * drag_accum < current_time()) {
1399 set ((framepos_t) floor (pos - drag_accum * frames), false); // minus because up is negative in GTK
1405 ValueChanged(); /* EMIT_SIGNAL */
1412 AudioClock::get_frame_step (Field field, framepos_t pos, int dir)
1415 Timecode::BBT_Time BBT;
1417 case Timecode_Hours:
1418 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1420 case Timecode_Minutes:
1421 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1423 case Timecode_Seconds:
1424 f = _session->frame_rate();
1426 case Timecode_Frames:
1427 f = (framecnt_t) floor (_session->frame_rate() / _session->timecode_frames_per_second());
1435 f = (framecnt_t) floor (3600.0 * _session->frame_rate());
1438 f = (framecnt_t) floor (60.0 * _session->frame_rate());
1441 f = (framecnt_t) _session->frame_rate();
1443 case MS_Milliseconds:
1444 f = (framecnt_t) floor (_session->frame_rate() / 1000.0);
1451 f = _session->tempo_map().bbt_duration_at (pos,BBT,dir);
1457 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1463 f = _session->tempo_map().bbt_duration_at(pos,BBT,dir);
1466 error << string_compose (_("programming error: %1"), "attempt to get frames from non-text field!") << endmsg;
1475 AudioClock::current_time (framepos_t pos) const
1481 AudioClock::current_duration (framepos_t pos) const
1490 ret = frame_duration_from_bbt_string (pos, _layout->get_text());
1506 AudioClock::bbt_validate_edit (const string& str)
1510 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1512 if (!is_duration && any.bbt.bars == 0) {
1516 if (!is_duration && any.bbt.beats == 0) {
1524 AudioClock::timecode_validate_edit (const string& str)
1528 if (sscanf (_layout->get_text().c_str(), "%" PRId32 ":%" PRId32 ":%" PRId32 ":%" PRId32,
1529 &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1533 if (TC.minutes > 59 || TC.seconds > 59) {
1537 if (TC.frames > (long)rint(_session->timecode_frames_per_second()) - 1) {
1541 if (_session->timecode_drop_frames()) {
1542 if (TC.minutes % 10 && TC.seconds == 0 && TC.frames < 2) {
1551 AudioClock::frames_from_timecode_string (const string& str) const
1553 if (_session == 0) {
1560 if (sscanf (str.c_str(), "%d:%d:%d:%d", &TC.hours, &TC.minutes, &TC.seconds, &TC.frames) != 4) {
1561 error << string_compose (_("programming error: %1 %2"), "badly formatted timecode clock string", str) << endmsg;
1565 TC.rate = _session->timecode_frames_per_second();
1566 TC.drop= _session->timecode_drop_frames();
1568 _session->timecode_to_sample (TC, sample, false /* use_offset */, false /* use_subframes */ );
1570 // timecode_tester ();
1576 AudioClock::frames_from_minsec_string (const string& str) const
1578 if (_session == 0) {
1582 int hrs, mins, secs, millisecs;
1583 framecnt_t sr = _session->frame_rate();
1585 if (sscanf (str.c_str(), "%d:%d:%d.%d", &hrs, &mins, &secs, &millisecs) != 4) {
1586 error << string_compose (_("programming error: %1 %2"), "badly formatted minsec clock string", str) << endmsg;
1590 return (framepos_t) floor ((hrs * 60.0f * 60.0f * sr) + (mins * 60.0f * sr) + (secs * sr) + (millisecs * sr / 1000.0));
1594 AudioClock::frames_from_bbt_string (framepos_t pos, const string& str) const
1596 if (_session == 0) {
1597 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1602 any.type = AnyTime::BBT;
1604 sscanf (str.c_str(), "%" PRId32 "|%" PRId32 "|%" PRId32, &any.bbt.bars, &any.bbt.beats, &any.bbt.ticks);
1609 return _session->any_duration_to_frames (pos, any);
1611 return _session->convert_to_frames (any);
1617 AudioClock::frame_duration_from_bbt_string (framepos_t pos, const string& str) const
1619 if (_session == 0) {
1620 error << "AudioClock::current_time() called with BBT mode but without session!" << endmsg;
1624 Timecode::BBT_Time bbt;
1626 sscanf (str.c_str(), "%" PRIu32 "|%" PRIu32 "|%" PRIu32, &bbt.bars, &bbt.beats, &bbt.ticks);
1628 return _session->tempo_map().bbt_duration_at(pos,bbt,1);
1632 AudioClock::frames_from_audioframes_string (const string& str) const
1635 sscanf (str.c_str(), "%" PRId64, &f);
1640 AudioClock::build_ops_menu ()
1642 using namespace Menu_Helpers;
1643 ops_menu = new Menu;
1644 MenuList& ops_items = ops_menu->items();
1645 ops_menu->set_name ("ArdourContextMenu");
1647 if (!Profile->get_sae()) {
1648 ops_items.push_back (MenuElem (_("Timecode"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Timecode)));
1650 ops_items.push_back (MenuElem (_("Bars:Beats"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), BBT)));
1651 ops_items.push_back (MenuElem (_("Minutes:Seconds"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), MinSec)));
1652 ops_items.push_back (MenuElem (_("Samples"), sigc::bind (sigc::mem_fun(*this, &AudioClock::set_mode), Frames)));
1654 if (editable && !_off && !is_duration && !_follows_playhead) {
1655 ops_items.push_back (SeparatorElem());
1656 ops_items.push_back (MenuElem (_("Set From Playhead"), sigc::mem_fun(*this, &AudioClock::set_from_playhead)));
1657 ops_items.push_back (MenuElem (_("Locate to This Time"), sigc::mem_fun(*this, &AudioClock::locate)));
1662 AudioClock::set_from_playhead ()
1668 set (_session->transport_frame());
1673 AudioClock::locate ()
1675 if (!_session || is_duration) {
1679 _session->request_locate (current_time(), _session->transport_rolling ());
1683 AudioClock::set_mode (Mode m)
1693 _layout->set_text ("");
1696 _left_layout->set_text ("");
1697 _right_layout->set_text ("");
1702 mode_based_info_ratio = 0.5;
1703 insert_map.push_back (11);
1704 insert_map.push_back (10);
1705 insert_map.push_back (8);
1706 insert_map.push_back (7);
1707 insert_map.push_back (5);
1708 insert_map.push_back (4);
1709 insert_map.push_back (2);
1710 insert_map.push_back (1);
1714 mode_based_info_ratio = 0.5;
1715 insert_map.push_back (11);
1716 insert_map.push_back (10);
1717 insert_map.push_back (9);
1718 insert_map.push_back (8);
1719 insert_map.push_back (6);
1720 insert_map.push_back (5);
1721 insert_map.push_back (3);
1722 insert_map.push_back (2);
1723 insert_map.push_back (1);
1727 mode_based_info_ratio = 1.0;
1728 insert_map.push_back (12);
1729 insert_map.push_back (11);
1730 insert_map.push_back (10);
1731 insert_map.push_back (8);
1732 insert_map.push_back (7);
1733 insert_map.push_back (5);
1734 insert_map.push_back (4);
1735 insert_map.push_back (2);
1736 insert_map.push_back (1);
1740 mode_based_info_ratio = 0.5;
1744 set (last_when, true);
1746 if (!is_transient) {
1747 ModeChanged (); /* EMIT SIGNAL (the static one)*/
1750 mode_changed (); /* EMIT SIGNAL (the member one) */
1754 AudioClock::set_bbt_reference (framepos_t pos)
1756 bbt_reference_time = pos;
1760 AudioClock::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
1762 CairoWidget::on_style_changed (old_style);
1768 AudioClock::set_editable (bool yn)
1774 AudioClock::set_is_duration (bool yn)
1776 if (yn == is_duration) {
1781 set (last_when, true);
1785 AudioClock::set_off (bool yn)
1793 /* force a redraw. last_when will be preserved, but the clock text will
1797 set (last_when, true);
1801 AudioClock::focus ()