2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "ardour/audioengine.h"
20 #include "ardour/session.h"
21 #include "ardour/tempo.h"
23 #include "gtkmm2ext/colors.h"
24 #include "gtkmm2ext/gui_thread.h"
25 #include "gtkmm2ext/keyboard.h"
27 #include "widgets/tooltips.h"
29 #include "ardour_ui.h"
30 #include "public_editor.h"
31 #include "main_clock.h"
32 #include "mini_timeline.h"
34 #include "ui_config.h"
39 #define BBT_BAR_CHAR "|"
41 using namespace ARDOUR;
43 MiniTimeline::MiniTimeline ()
44 : _last_update_frame (-1)
45 , _clock_mode (AudioClock::Timecode)
50 , _time_granularity (0)
51 , _time_span_samples (0)
55 , _minitl_context_menu (0)
57 add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK);
59 _layout = Pango::Layout::create (get_pango_context());
61 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
62 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
63 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
65 set_name ("minitimeline");
67 Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
68 Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
69 Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
70 Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
72 ArdourWidgets::set_tooltip (*this,
73 string_compose (_("<b>Navigation Timeline</b>. Use left-click to locate to time position or marker; scroll-wheel to jump, hold %1 for fine grained and %2 + %3 for extra-fine grained control. Right-click to set display range. The display unit is defined by the primary clock."),
74 Gtkmm2ext::Keyboard::primary_modifier_name(),
75 Gtkmm2ext::Keyboard::primary_modifier_name (),
76 Gtkmm2ext::Keyboard::secondary_modifier_name ()));
79 MiniTimeline::~MiniTimeline ()
81 delete _minitl_context_menu;
82 _minitl_context_menu = 0;
86 MiniTimeline::session_going_away ()
88 super_rapid_connection.disconnect ();
89 session_connection.drop_connections ();
90 SessionHandlePtr::session_going_away ();
92 delete _minitl_context_menu;
93 _minitl_context_menu = 0;
97 MiniTimeline::set_session (Session* s)
99 SessionHandlePtr::set_session (s);
104 assert (!super_rapid_connection.connected ());
105 super_rapid_connection = Timers::super_rapid_connect (
106 sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
109 _session->config.ParameterChanged.connect (session_connection,
111 boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
113 _session->locations()->added.connect (session_connection,
115 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
117 _session->locations()->removed.connect (session_connection,
119 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
121 _session->locations()->changed.connect (session_connection,
123 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
127 calculate_time_spacing ();
128 update_minitimeline ();
132 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
134 CairoWidget::on_style_changed (old_style);
136 calculate_time_width ();
140 MiniTimeline::on_name_changed ()
143 calculate_time_width ();
151 MiniTimeline::set_colors ()
153 // TODO UIConfiguration::instance().color & font
157 MiniTimeline::parameter_changed (std::string const& p)
159 if (p == "minitimeline-span") {
160 calculate_time_spacing ();
161 update_minitimeline ();
166 MiniTimeline::on_size_request (Gtk::Requisition* req)
168 req->width = req->height = 0;
169 CairoWidget::on_size_request (req);
171 req->width = std::max (req->width, 1);
172 req->height = std::max (req->height, 20);
176 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
178 CairoWidget::on_size_allocate (alloc);
179 calculate_time_spacing ();
183 MiniTimeline::set_span (framecnt_t ts)
186 if (_session->config.get_minitimeline_span () == ts) {
190 _session->config.set_minitimeline_span (ts);
191 calculate_time_spacing ();
192 update_minitimeline ();
196 MiniTimeline::super_rapid_update ()
198 if (!_session || !_session->engine().running() || !is_mapped ()) {
201 framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
202 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
205 if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
209 if (m != _clock_mode) {
211 calculate_time_width ();
215 if (_clock_mode == AudioClock::BBT) {
216 // TODO check if tempo-map changed
221 _last_update_frame = frame;
222 update_minitimeline ();
227 MiniTimeline::update_minitimeline ()
229 CairoWidget::set_dirty ();
233 MiniTimeline::calculate_time_width ()
235 switch (_clock_mode) {
236 case AudioClock::Timecode:
237 _layout->set_text (" 88:88:88,888 ");
239 case AudioClock::BBT:
240 _layout->set_text ("888|88|8888");
242 case AudioClock::MinSec:
243 _layout->set_text ("88:88:88,88");
245 case AudioClock::Frames:
246 _layout->set_text ("8888888888");
249 _layout->get_pixel_size (_time_width, _time_height);
253 MiniTimeline::calculate_time_spacing ()
255 _n_labels = floor (get_width () / (_time_width * 1.15));
257 if (_n_labels == 0 || !_session) {
261 const framecnt_t time_span = _session->config.get_minitimeline_span () / 2;
262 _time_span_samples = time_span * _session->nominal_frame_rate ();
263 _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
264 _px_per_sample = get_width () / (2. * _time_span_samples);
265 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
269 MiniTimeline::format_time (framepos_t when)
271 switch (_clock_mode) {
272 case AudioClock::Timecode:
275 _session->timecode_time (when, TC);
276 // chop of leading space or minus.
277 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
280 case AudioClock::BBT:
283 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
284 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
285 BBT.bars, BBT.beats, BBT.ticks);
286 _layout->set_text (buf);
289 case AudioClock::MinSec:
292 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
293 _layout->set_text (std::string(buf).substr(1));
296 case AudioClock::Frames:
299 snprintf (buf, sizeof (buf), "%" PRId64, when);
300 _layout->set_text (buf);
307 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, Gtkmm2ext::Color color)
309 if (left + 1 >= right) {
312 cairo_move_to (cr, left + .5, y + .5);
313 cairo_line_to (cr, right - .5, y + .5);
314 Gtkmm2ext::set_source_rgb_a(cr, color, 0.3);
315 const double dashes[] = { 0, 1 };
316 cairo_set_dash (cr, dashes, 2, 1);
317 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
318 cairo_set_line_width (cr, 1.0);
320 cairo_set_dash (cr, 0, 0, 0);
324 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
326 int h = _marker_height;
327 /* ArdourMarker shape
335 * (0,MH*.4) (6,MH*.4)
340 const int y = PADDING;
341 int w2 = (h - 1) / 4;
346 _layout->set_text (label);
347 _layout->get_pixel_size (lw, lh);
348 int rw = std::min (x1, x0 + w2 + lw + 2);
350 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 - w2 && _pointer_x <= rw) {
354 // TODO cache in set_colors()
355 uint32_t color = UIConfiguration::instance().color (
356 prelight ? "entered marker" : "location marker");
359 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
365 cairo_rectangle (cr, x0, y, rw - x0, h);
366 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
367 cairo_fill_preserve (cr);
371 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
372 cairo_set_source_rgb (cr, 0, 0, 0);
373 pango_cairo_show_layout (cr, _layout->gobj());
377 // draw marker on top
378 cairo_move_to (cr, x0 - .5, y + .5);
379 cairo_rel_line_to (cr, -w2 , 0);
380 cairo_rel_line_to (cr, 0, h0);
381 cairo_rel_line_to (cr, w2, h1);
382 cairo_rel_line_to (cr, w2, -h1);
383 cairo_rel_line_to (cr, 0, -h0);
384 cairo_close_path (cr);
385 cairo_set_source_rgba (cr, r, g, b, 1.0);
386 cairo_set_line_width (cr, 1.0);
387 cairo_stroke_preserve (cr);
394 MiniTimeline::draw_edge (cairo_t* cr, int x0, int x1, bool left, const std::string& label, bool& prelight)
396 int h = _marker_height;
397 int w2 = (h - 1) / 4;
399 const int y = PADDING;
400 const double yc = rint (h * .5);
401 const double dy = h * .4;
405 _layout->set_text (label);
406 _layout->get_pixel_size (lw, lh);
410 if (x0 + 2 * w2 + lw + 2 < x1) {
411 x1 = std::min (x1, x0 + 2 * w2 + lw + 2);
414 x1 = std::min (x1, x0 + 2 * w2);
421 if (x1 - 2 * w2 - lw - 2 > x0) {
422 x0 = std::max (x0, x1 - 2 * w2 - lw - 2);
425 x0 = std::max (x0, x1 - 2 * w2);
430 lx = x1 + dx - lw - 2;
433 if (x1 - x0 < 2 * w2) {
434 return left ? x0 : x1;
437 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= x1) {
441 // TODO cache in set_colors()
442 uint32_t color = UIConfiguration::instance().color (
443 prelight ? "entered marker" : "location marker");
446 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
449 const int y = PADDING;
451 cairo_rectangle (cr, lx, y, lw + 2, h);
452 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
453 cairo_fill_preserve (cr);
457 cairo_move_to (cr, lx + 1, y + .5 * (h - lh));
458 cairo_set_source_rgb (cr, 0, 0, 0);
459 pango_cairo_show_layout (cr, _layout->gobj());
464 cairo_move_to (cr, px - .5, PADDING + yc - .5);
465 cairo_rel_line_to (cr, dx , dy);
466 cairo_rel_line_to (cr, 0, -2. * dy);
467 cairo_close_path (cr);
468 cairo_set_source_rgba (cr, r, g, b, 1.0);
469 cairo_set_line_width (cr, 1.0);
470 cairo_stroke_preserve (cr);
473 return left ? x1 : x0;
477 struct LocationMarker {
478 LocationMarker (const std::string& l, framepos_t w)
479 : label (l), when (w) {}
484 struct LocationMarkerSort {
485 bool operator() (const LocationMarker& a, const LocationMarker& b) {
486 return (a.when < b.when);
491 MiniTimeline::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
493 cairo_t* cr = ctx->cobj();
494 // TODO cache, set_colors()
495 Gtkmm2ext::Color base = UIConfiguration::instance().color ("ruler base");
496 Gtkmm2ext::Color text = UIConfiguration::instance().color ("ruler text");
498 if (_n_labels == 0) {
502 const int width = get_width ();
503 const int height = get_height ();
505 Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, 4);
506 Gtkmm2ext::set_source_rgba(cr, base);
509 Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, 4);
517 const framepos_t p = _last_update_frame;
518 const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
520 int dot_left = width * .5 + (lower - p) * _px_per_sample;
521 for (int i = 0; i < 2 + _n_labels; ++i) {
522 framepos_t when = lower + i * _time_granularity;
523 double xpos = width * .5 + (when - p) * _px_per_sample;
525 // TODO round to nearest display TC in +/- 1px
526 // prefer to display BBT |0 or .0
530 _layout->get_pixel_size (lw, lh);
532 int x0 = xpos - lw / 2.0;
533 int y0 = height - PADDING - _time_height;
535 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
537 cairo_move_to (cr, x0, y0);
538 Gtkmm2ext::set_source_rgba(cr, text);
539 pango_cairo_show_layout (cr, _layout->gobj());
542 draw_dots (cr, dot_left, width, height - PADDING - _time_height * .5, text);
545 framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
546 framepos_t lmax = p + _time_span_samples;
549 _layout->set_text (X_("Marker@"));
550 _layout->get_pixel_size (tw, th);
552 _marker_height = th + 2;
553 assert (_marker_height > 4);
554 const int mw = (_marker_height - 1) / 4;
556 lmin -= mw / _px_per_sample;
557 lmax += mw / _px_per_sample;
559 std::vector<LocationMarker> lm;
561 const Locations::LocationList& ll (_session->locations ()->list ());
562 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
563 if ((*l)->is_session_range ()) {
564 lm.push_back (LocationMarker(_("start"), (*l)->start ()));
565 lm.push_back (LocationMarker(_("end"), (*l)->end ()));
569 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
573 lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
578 LocationMarkerSort location_marker_sort;
579 std::sort (lm.begin(), lm.end(), location_marker_sort);
581 std::vector<LocationMarker>::const_iterator outside_left = lm.end();
582 std::vector<LocationMarker>::const_iterator outside_right = lm.end();
584 int right_limit = width * .5 + mw;
587 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
588 framepos_t when = (*l).when;
591 if (++l != lm.end()) {
592 left_limit = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
594 left_limit = width * .5 - mw;
602 int x0 = floor (width * .5 + (when - p) * _px_per_sample);
604 const std::string& label = (*l).label;
605 if (++l != lm.end()) {
606 x1 = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
608 bool prelight = false;
609 x1 = draw_mark (cr, x0, x1, label, prelight);
610 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
611 right_limit = std::max (x1, right_limit);
614 if (outside_left != lm.end ()) {
615 if (left_limit > 3 * mw + PADDING) {
616 int x0 = PADDING + 1;
617 int x1 = left_limit - mw;
618 bool prelight = false;
619 x1 = draw_edge (cr, x0, x1, true, (*outside_left).label, prelight);
621 _jumplist.push_back (JumpRange (x0, x1, (*outside_left).when, prelight));
622 right_limit = std::max (x1, right_limit);
627 if (outside_right != lm.end ()) {
628 if (right_limit + PADDING < width - 3 * mw) {
629 int x0 = right_limit;
630 int x1 = width - PADDING;
631 bool prelight = false;
632 x0 = draw_edge (cr, x0, x1, false, (*outside_right).label, prelight);
634 _jumplist.push_back (JumpRange (x0, x1, (*outside_right).when, prelight));
640 /* playhead on top */
641 int xc = width * 0.5f;
642 cairo_set_line_width (cr, 1.0);
643 cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
644 cairo_move_to (cr, xc - .5, 0);
645 cairo_rel_line_to (cr, 0, height);
647 cairo_move_to (cr, xc - .5, height);
648 cairo_rel_line_to (cr, -3, 0);
649 cairo_rel_line_to (cr, 3, -4);
650 cairo_rel_line_to (cr, 3, 4);
651 cairo_close_path (cr);
656 MiniTimeline::build_minitl_context_menu ()
659 using namespace Gtk::Menu_Helpers;
663 const framecnt_t time_span = _session->config.get_minitimeline_span ();
665 _minitl_context_menu = new Gtk::Menu();
666 MenuList& items = _minitl_context_menu->items();
668 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
669 std::map<framecnt_t, std::string> spans;
670 spans[30] = _("30 sec");
671 spans[60] = _("1 min");
672 spans[120] = _("2 mins");
673 spans[300] = _("5 mins");
674 spans[600] = _("10 mins");
675 spans[1200] = _("20 mins");
677 RadioMenuItem::Group span_group;
678 for (std::map<framecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
679 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
680 if (time_span == i->first) {
681 static_cast<RadioMenuItem*>(&items.back())->set_active ();
687 MiniTimeline::on_button_press_event (GdkEventButton *ev)
689 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
691 if (_minitl_context_menu == 0) {
692 build_minitl_context_menu ();
694 _minitl_context_menu->popup (ev->button, ev->time);
702 MiniTimeline::on_button_release_event (GdkEventButton *ev)
704 if (!_session) { return true; }
705 if (_session->actively_recording ()) { return true; }
706 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
710 if (ev->y <= PADDING + _marker_height) {
711 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
712 if (i->left <= ev->x && ev->x <= i->right) {
713 _session->request_locate (i->to, _session->transport_rolling ());
719 if (ev->button == 1) {
720 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
721 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
728 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
730 if (!_session) { return true; }
731 if (_session->actively_recording ()) { return true; }
736 bool need_expose = false;
738 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
739 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
740 if (!(*i).prelight) {
752 update_minitimeline ();
759 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
761 CairoWidget::on_leave_notify_event (ev);
762 _pointer_x = _pointer_y = -1;
763 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
765 update_minitimeline ();
773 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
775 if (!_session) { return true; }
776 if (_session->actively_recording ()) { return true; }
777 const framecnt_t time_span = _session->config.get_minitimeline_span ();
778 framepos_t when = _session->audible_frame ();
780 double scale = time_span / 60.0;
782 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
783 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
790 switch (ev->direction) {
792 case GDK_SCROLL_RIGHT:
793 when += scale * _session->nominal_frame_rate ();
795 case GDK_SCROLL_DOWN:
796 case GDK_SCROLL_LEFT:
797 when -= scale * _session->nominal_frame_rate ();
803 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());