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_sample (-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
154 _phead_color = UIConfiguration::instance().color ("play head");
158 MiniTimeline::parameter_changed (std::string const& p)
160 if (p == "minitimeline-span") {
161 calculate_time_spacing ();
162 update_minitimeline ();
167 MiniTimeline::on_size_request (Gtk::Requisition* req)
169 req->width = req->height = 0;
170 CairoWidget::on_size_request (req);
172 req->width = std::max (req->width, 1);
173 req->height = std::max (req->height, 20);
177 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
179 CairoWidget::on_size_allocate (alloc);
180 calculate_time_spacing ();
184 MiniTimeline::set_span (samplecnt_t ts)
187 if (_session->config.get_minitimeline_span () == ts) {
191 _session->config.set_minitimeline_span (ts);
192 calculate_time_spacing ();
193 update_minitimeline ();
197 MiniTimeline::super_rapid_update ()
199 if (!_session || !_session->engine().running() || !is_mapped ()) {
202 samplepos_t const sample = PublicEditor::instance().playhead_cursor_sample ();
203 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
206 if (fabs ((_last_update_sample - sample) * _px_per_sample) >= 1.0) {
210 if (m != _clock_mode) {
212 calculate_time_width ();
216 if (_clock_mode == AudioClock::BBT) {
217 // TODO check if tempo-map changed
222 _last_update_sample = sample;
223 update_minitimeline ();
228 MiniTimeline::update_minitimeline ()
230 CairoWidget::set_dirty ();
234 MiniTimeline::calculate_time_width ()
236 switch (_clock_mode) {
237 case AudioClock::Timecode:
238 _layout->set_text (" 88:88:88,888 ");
240 case AudioClock::BBT:
241 _layout->set_text ("888|88|8888");
243 case AudioClock::MinSec:
244 _layout->set_text ("88:88:88,88");
246 case AudioClock::Seconds:
247 case AudioClock::Samples:
248 _layout->set_text ("8888888888");
251 _layout->get_pixel_size (_time_width, _time_height);
255 MiniTimeline::calculate_time_spacing ()
257 _n_labels = floor (get_width () / (_time_width * 1.15));
259 if (_n_labels == 0 || !_session) {
263 const samplecnt_t time_span = _session->config.get_minitimeline_span () / 2;
264 _time_span_samples = time_span * _session->nominal_sample_rate ();
265 _time_granularity = _session->nominal_sample_rate () * ceil (2. * time_span / _n_labels);
266 _px_per_sample = get_width () / (2. * _time_span_samples);
267 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
271 MiniTimeline::format_time (samplepos_t when)
273 switch (_clock_mode) {
274 case AudioClock::Timecode:
277 _session->timecode_time (when, TC);
278 // chop of leading space or minus.
279 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
282 case AudioClock::BBT:
285 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_sample (when);
286 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
287 BBT.bars, BBT.beats, BBT.ticks);
288 _layout->set_text (buf);
291 case AudioClock::MinSec:
294 AudioClock::print_minsec (when, buf, sizeof (buf), _session->sample_rate());
295 _layout->set_text (std::string(buf).substr(1));
298 case AudioClock::Seconds:
301 snprintf (buf, sizeof (buf), "%.1f", when / (float)_session->sample_rate());
302 _layout->set_text (buf);
305 case AudioClock::Samples:
308 snprintf (buf, sizeof (buf), "%" PRId64, when);
309 _layout->set_text (buf);
316 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, Gtkmm2ext::Color color)
318 if (left + 1 >= right) {
321 cairo_move_to (cr, left + .5, y + .5);
322 cairo_line_to (cr, right - .5, y + .5);
323 Gtkmm2ext::set_source_rgb_a(cr, color, 0.3);
324 const double dashes[] = { 0, 1 };
325 cairo_set_dash (cr, dashes, 2, 1);
326 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
327 cairo_set_line_width (cr, 1.0);
329 cairo_set_dash (cr, 0, 0, 0);
333 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
335 int h = _marker_height;
336 /* ArdourMarker shape
344 * (0,MH*.4) (6,MH*.4)
349 const int y = PADDING;
350 int w2 = (h - 1) / 4;
355 _layout->set_text (label);
356 _layout->get_pixel_size (lw, lh);
357 int rw = std::min (x1, x0 + w2 + lw + 2);
359 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 - w2 && _pointer_x <= rw) {
363 // TODO cache in set_colors()
364 uint32_t color = UIConfiguration::instance().color (
365 prelight ? "entered marker" : "location marker");
368 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
374 cairo_rectangle (cr, x0, y, rw - x0, h);
375 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
376 cairo_fill_preserve (cr);
380 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
381 cairo_set_source_rgb (cr, 0, 0, 0);
382 pango_cairo_show_layout (cr, _layout->gobj());
386 // draw marker on top
387 cairo_move_to (cr, x0 - .5, y + .5);
388 cairo_rel_line_to (cr, -w2 , 0);
389 cairo_rel_line_to (cr, 0, h0);
390 cairo_rel_line_to (cr, w2, h1);
391 cairo_rel_line_to (cr, w2, -h1);
392 cairo_rel_line_to (cr, 0, -h0);
393 cairo_close_path (cr);
394 cairo_set_source_rgba (cr, r, g, b, 1.0);
395 cairo_set_line_width (cr, 1.0);
396 cairo_stroke_preserve (cr);
403 MiniTimeline::draw_edge (cairo_t* cr, int x0, int x1, bool left, const std::string& label, bool& prelight)
405 int h = _marker_height;
406 int w2 = (h - 1) / 4;
408 const int y = PADDING;
409 const double yc = rint (h * .5);
410 const double dy = h * .4;
414 _layout->set_text (label);
415 _layout->get_pixel_size (lw, lh);
419 if (x0 + 2 * w2 + lw + 2 < x1) {
420 x1 = std::min (x1, x0 + 2 * w2 + lw + 2);
423 x1 = std::min (x1, x0 + 2 * w2);
430 if (x1 - 2 * w2 - lw - 2 > x0) {
431 x0 = std::max (x0, x1 - 2 * w2 - lw - 2);
434 x0 = std::max (x0, x1 - 2 * w2);
439 lx = x1 + dx - lw - 2;
442 if (x1 - x0 < 2 * w2) {
443 return left ? x0 : x1;
446 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= x1) {
450 // TODO cache in set_colors()
451 uint32_t color = UIConfiguration::instance().color (
452 prelight ? "entered marker" : "location marker");
455 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
458 const int y = PADDING;
460 cairo_rectangle (cr, lx, y, lw + 2, h);
461 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
462 cairo_fill_preserve (cr);
466 cairo_move_to (cr, lx + 1, y + .5 * (h - lh));
467 cairo_set_source_rgb (cr, 0, 0, 0);
468 pango_cairo_show_layout (cr, _layout->gobj());
473 cairo_move_to (cr, px - .5, PADDING + yc - .5);
474 cairo_rel_line_to (cr, dx , dy);
475 cairo_rel_line_to (cr, 0, -2. * dy);
476 cairo_close_path (cr);
477 cairo_set_source_rgba (cr, r, g, b, 1.0);
478 cairo_set_line_width (cr, 1.0);
479 cairo_stroke_preserve (cr);
482 return left ? x1 : x0;
486 struct LocationMarker {
487 LocationMarker (const std::string& l, samplepos_t w)
488 : label (l), when (w) {}
493 struct LocationMarkerSort {
494 bool operator() (const LocationMarker& a, const LocationMarker& b) {
495 return (a.when < b.when);
500 MiniTimeline::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
502 cairo_t* cr = ctx->cobj();
503 // TODO cache, set_colors()
504 Gtkmm2ext::Color base = UIConfiguration::instance().color ("ruler base");
505 Gtkmm2ext::Color text = UIConfiguration::instance().color ("ruler text");
507 if (_n_labels == 0) {
511 const int width = get_width ();
512 const int height = get_height ();
514 Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, 4);
515 Gtkmm2ext::set_source_rgba(cr, base);
518 Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, 4);
526 const samplepos_t p = _last_update_sample;
527 const samplepos_t lower = (std::max ((samplepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
529 int dot_left = width * .5 + (lower - p) * _px_per_sample;
530 for (int i = 0; i < 2 + _n_labels; ++i) {
531 samplepos_t when = lower + i * _time_granularity;
532 double xpos = width * .5 + (when - p) * _px_per_sample;
534 // TODO round to nearest display TC in +/- 1px
535 // prefer to display BBT |0 or .0
539 _layout->get_pixel_size (lw, lh);
541 int x0 = xpos - lw / 2.0;
542 int y0 = height - PADDING - _time_height;
544 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
546 cairo_move_to (cr, x0, y0);
547 Gtkmm2ext::set_source_rgba(cr, text);
548 pango_cairo_show_layout (cr, _layout->gobj());
551 draw_dots (cr, dot_left, width, height - PADDING - _time_height * .5, text);
554 samplepos_t lmin = std::max ((samplepos_t)0, (p - _time_span_samples));
555 samplepos_t lmax = p + _time_span_samples;
558 _layout->set_text (X_("Marker@"));
559 _layout->get_pixel_size (tw, th);
561 _marker_height = th + 2;
562 assert (_marker_height > 4);
563 const int mw = (_marker_height - 1) / 4;
565 lmin -= mw / _px_per_sample;
566 lmax += mw / _px_per_sample;
568 std::vector<LocationMarker> lm;
570 const Locations::LocationList& ll (_session->locations ()->list ());
571 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
572 if ((*l)->is_session_range ()) {
573 lm.push_back (LocationMarker(_("start"), (*l)->start ()));
574 lm.push_back (LocationMarker(_("end"), (*l)->end ()));
578 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
582 lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
587 LocationMarkerSort location_marker_sort;
588 std::sort (lm.begin(), lm.end(), location_marker_sort);
590 std::vector<LocationMarker>::const_iterator outside_left = lm.end();
591 std::vector<LocationMarker>::const_iterator outside_right = lm.end();
593 int right_limit = width * .5 + mw;
596 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
597 samplepos_t when = (*l).when;
600 if (++l != lm.end()) {
601 left_limit = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
603 left_limit = width * .5 - mw;
611 int x0 = floor (width * .5 + (when - p) * _px_per_sample);
613 const std::string& label = (*l).label;
614 if (++l != lm.end()) {
615 x1 = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
617 bool prelight = false;
618 x1 = draw_mark (cr, x0, x1, label, prelight);
619 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
620 right_limit = std::max (x1, right_limit);
623 if (outside_left != lm.end ()) {
624 if (left_limit > 3 * mw + PADDING) {
625 int x0 = PADDING + 1;
626 int x1 = left_limit - mw;
627 bool prelight = false;
628 x1 = draw_edge (cr, x0, x1, true, (*outside_left).label, prelight);
630 _jumplist.push_back (JumpRange (x0, x1, (*outside_left).when, prelight));
631 right_limit = std::max (x1, right_limit);
636 if (outside_right != lm.end ()) {
637 if (right_limit + PADDING < width - 3 * mw) {
638 int x0 = right_limit;
639 int x1 = width - PADDING;
640 bool prelight = false;
641 x0 = draw_edge (cr, x0, x1, false, (*outside_right).label, prelight);
643 _jumplist.push_back (JumpRange (x0, x1, (*outside_right).when, prelight));
649 /* playhead on top */
650 int xc = width * 0.5f;
651 cairo_set_line_width (cr, 1.0);
652 double r,g,b,a; Gtkmm2ext::color_to_rgba(_phead_color, r,g,b,a);
653 cairo_set_source_rgb (cr, r,g,b); // playhead color
654 cairo_move_to (cr, xc - .5, 0);
655 cairo_rel_line_to (cr, 0, height);
657 cairo_move_to (cr, xc - .5, height);
658 cairo_rel_line_to (cr, -3, 0);
659 cairo_rel_line_to (cr, 3, -4);
660 cairo_rel_line_to (cr, 3, 4);
661 cairo_close_path (cr);
666 MiniTimeline::build_minitl_context_menu ()
669 using namespace Gtk::Menu_Helpers;
673 const samplecnt_t time_span = _session->config.get_minitimeline_span ();
675 _minitl_context_menu = new Gtk::Menu();
676 MenuList& items = _minitl_context_menu->items();
678 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
679 std::map<samplecnt_t, std::string> spans;
680 spans[30] = _("30 sec");
681 spans[60] = _("1 min");
682 spans[120] = _("2 mins");
683 spans[300] = _("5 mins");
684 spans[600] = _("10 mins");
685 spans[1200] = _("20 mins");
687 RadioMenuItem::Group span_group;
688 for (std::map<samplecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
689 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
690 if (time_span == i->first) {
691 static_cast<RadioMenuItem*>(&items.back())->set_active ();
697 MiniTimeline::on_button_press_event (GdkEventButton *ev)
699 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
701 if (_minitl_context_menu == 0) {
702 build_minitl_context_menu ();
704 _minitl_context_menu->popup (ev->button, ev->time);
712 MiniTimeline::on_button_release_event (GdkEventButton *ev)
714 if (!_session) { return true; }
715 if (_session->actively_recording ()) { return true; }
716 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
720 if (ev->y <= PADDING + _marker_height) {
721 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
722 if (i->left <= ev->x && ev->x <= i->right) {
723 _session->request_locate (i->to, _session->transport_rolling ());
729 if (ev->button == 1) {
730 samplepos_t when = _last_update_sample + (ev->x - get_width() * .5) / _px_per_sample;
731 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
738 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
740 if (!_session) { return true; }
741 if (_session->actively_recording ()) { return true; }
746 bool need_expose = false;
748 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
749 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
750 if (!(*i).prelight) {
762 update_minitimeline ();
769 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
771 CairoWidget::on_leave_notify_event (ev);
772 _pointer_x = _pointer_y = -1;
773 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
775 update_minitimeline ();
783 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
785 if (!_session) { return true; }
786 if (_session->actively_recording ()) { return true; }
787 const samplecnt_t time_span = _session->config.get_minitimeline_span ();
788 samplepos_t when = _session->audible_sample ();
790 double scale = time_span / 60.0;
792 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
793 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
800 switch (ev->direction) {
802 case GDK_SCROLL_RIGHT:
803 when += scale * _session->nominal_sample_rate ();
805 case GDK_SCROLL_DOWN:
806 case GDK_SCROLL_LEFT:
807 when -= scale * _session->nominal_sample_rate ();
813 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());