2 * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2016-2018 Ben Loftis <ben@harrisonconsoles.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "ardour/audioengine.h"
21 #include "ardour/session.h"
22 #include "ardour/tempo.h"
24 #include "gtkmm2ext/colors.h"
25 #include "gtkmm2ext/gui_thread.h"
26 #include "gtkmm2ext/keyboard.h"
28 #include "widgets/tooltips.h"
30 #include "ardour_ui.h"
31 #include "public_editor.h"
32 #include "main_clock.h"
33 #include "mini_timeline.h"
35 #include "ui_config.h"
40 #define BBT_BAR_CHAR "|"
42 using namespace ARDOUR;
44 MiniTimeline::MiniTimeline ()
45 : _last_update_sample (-1)
46 , _clock_mode (AudioClock::Timecode)
51 , _time_granularity (0)
52 , _time_span_samples (0)
56 , _minitl_context_menu (0)
58 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);
60 _layout = Pango::Layout::create (get_pango_context());
62 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
63 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
64 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
66 set_name ("minitimeline");
68 Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
69 Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
70 Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
71 Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
73 ArdourWidgets::set_tooltip (*this,
74 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."),
75 Gtkmm2ext::Keyboard::primary_modifier_name(),
76 Gtkmm2ext::Keyboard::primary_modifier_name (),
77 Gtkmm2ext::Keyboard::secondary_modifier_name ()));
80 MiniTimeline::~MiniTimeline ()
82 delete _minitl_context_menu;
83 _minitl_context_menu = 0;
87 MiniTimeline::session_going_away ()
89 super_rapid_connection.disconnect ();
90 session_connection.drop_connections ();
91 SessionHandlePtr::session_going_away ();
93 delete _minitl_context_menu;
94 _minitl_context_menu = 0;
98 MiniTimeline::set_session (Session* s)
100 SessionHandlePtr::set_session (s);
105 assert (!super_rapid_connection.connected ());
106 super_rapid_connection = Timers::super_rapid_connect (
107 sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
110 _session->config.ParameterChanged.connect (session_connection,
112 boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
114 _session->locations()->added.connect (session_connection,
116 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
118 _session->locations()->removed.connect (session_connection,
120 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
122 _session->locations()->changed.connect (session_connection,
124 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
128 calculate_time_spacing ();
129 update_minitimeline ();
133 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
135 CairoWidget::on_style_changed (old_style);
137 calculate_time_width ();
141 MiniTimeline::on_name_changed ()
144 calculate_time_width ();
152 MiniTimeline::set_colors ()
154 // TODO UIConfiguration::instance().color & font
155 _phead_color = UIConfiguration::instance().color ("play head");
159 MiniTimeline::parameter_changed (std::string const& p)
161 if (p == "minitimeline-span") {
162 calculate_time_spacing ();
163 update_minitimeline ();
168 MiniTimeline::on_size_request (Gtk::Requisition* req)
170 req->width = req->height = 0;
171 CairoWidget::on_size_request (req);
173 req->width = std::max (req->width, 1);
174 req->height = std::max (req->height, 20);
178 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
180 CairoWidget::on_size_allocate (alloc);
181 calculate_time_spacing ();
185 MiniTimeline::set_span (samplecnt_t ts)
188 if (_session->config.get_minitimeline_span () == ts) {
192 _session->config.set_minitimeline_span (ts);
193 calculate_time_spacing ();
194 update_minitimeline ();
198 MiniTimeline::super_rapid_update ()
200 if (!_session || !_session->engine().running() || !is_mapped ()) {
203 samplepos_t const sample = PublicEditor::instance().playhead_cursor_sample ();
204 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
207 if (fabs ((_last_update_sample - sample) * _px_per_sample) >= 1.0) {
211 if (m != _clock_mode) {
213 calculate_time_width ();
217 if (_clock_mode == AudioClock::BBT) {
218 // TODO check if tempo-map changed
223 _last_update_sample = sample;
224 update_minitimeline ();
229 MiniTimeline::update_minitimeline ()
231 CairoWidget::set_dirty ();
235 MiniTimeline::calculate_time_width ()
237 switch (_clock_mode) {
238 case AudioClock::Timecode:
239 _layout->set_text (" 88:88:88,888 ");
241 case AudioClock::BBT:
242 _layout->set_text ("888|88|8888");
244 case AudioClock::MinSec:
245 _layout->set_text ("88:88:88,88");
247 case AudioClock::Seconds:
248 case AudioClock::Samples:
249 _layout->set_text ("8888888888");
252 _layout->get_pixel_size (_time_width, _time_height);
256 MiniTimeline::calculate_time_spacing ()
258 _n_labels = floor (get_width () / (_time_width * 1.15));
260 if (_n_labels == 0 || !_session) {
264 const samplecnt_t time_span = _session->config.get_minitimeline_span () / 2;
265 _time_span_samples = time_span * _session->nominal_sample_rate ();
266 _time_granularity = _session->nominal_sample_rate () * ceil (2. * time_span / _n_labels);
267 _px_per_sample = get_width () / (2. * _time_span_samples);
268 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
272 MiniTimeline::format_time (samplepos_t when)
274 switch (_clock_mode) {
275 case AudioClock::Timecode:
278 _session->timecode_time (when, TC);
279 // chop of leading space or minus.
280 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
283 case AudioClock::BBT:
286 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_sample (when);
287 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
288 BBT.bars, BBT.beats, BBT.ticks);
289 _layout->set_text (buf);
292 case AudioClock::MinSec:
295 AudioClock::print_minsec (when, buf, sizeof (buf), _session->sample_rate());
296 _layout->set_text (std::string(buf).substr(1));
299 case AudioClock::Seconds:
302 snprintf (buf, sizeof (buf), "%.1f", when / (float)_session->sample_rate());
303 _layout->set_text (buf);
306 case AudioClock::Samples:
309 snprintf (buf, sizeof (buf), "%" PRId64, when);
310 _layout->set_text (buf);
317 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, Gtkmm2ext::Color color)
319 if (left + 1 >= right) {
322 cairo_move_to (cr, left + .5, y + .5);
323 cairo_line_to (cr, right - .5, y + .5);
324 Gtkmm2ext::set_source_rgb_a(cr, color, 0.3);
325 const double dashes[] = { 0, 1 };
326 cairo_set_dash (cr, dashes, 2, 1);
327 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
328 cairo_set_line_width (cr, 1.0);
330 cairo_set_dash (cr, 0, 0, 0);
334 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
336 int h = _marker_height;
337 /* ArdourMarker shape
345 * (0,MH*.4) (6,MH*.4)
350 const int y = PADDING;
351 int w2 = (h - 1) / 4;
356 _layout->set_text (label);
357 _layout->get_pixel_size (lw, lh);
358 int rw = std::min (x1, x0 + w2 + lw + 2);
360 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 - w2 && _pointer_x <= rw) {
364 // TODO cache in set_colors()
365 uint32_t color = UIConfiguration::instance().color (
366 prelight ? "entered marker" : "location marker");
369 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
375 cairo_rectangle (cr, x0, y, rw - x0, h);
376 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
377 cairo_fill_preserve (cr);
381 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
382 cairo_set_source_rgb (cr, 0, 0, 0);
383 pango_cairo_show_layout (cr, _layout->gobj());
387 // draw marker on top
388 cairo_move_to (cr, x0 - .5, y + .5);
389 cairo_rel_line_to (cr, -w2 , 0);
390 cairo_rel_line_to (cr, 0, h0);
391 cairo_rel_line_to (cr, w2, h1);
392 cairo_rel_line_to (cr, w2, -h1);
393 cairo_rel_line_to (cr, 0, -h0);
394 cairo_close_path (cr);
395 cairo_set_source_rgba (cr, r, g, b, 1.0);
396 cairo_set_line_width (cr, 1.0);
397 cairo_stroke_preserve (cr);
404 MiniTimeline::draw_edge (cairo_t* cr, int x0, int x1, bool left, const std::string& label, bool& prelight)
406 int h = _marker_height;
407 int w2 = (h - 1) / 4;
409 const int y = PADDING;
410 const double yc = rint (h * .5);
411 const double dy = h * .4;
415 _layout->set_text (label);
416 _layout->get_pixel_size (lw, lh);
420 if (x0 + 2 * w2 + lw + 2 < x1) {
421 x1 = std::min (x1, x0 + 2 * w2 + lw + 2);
424 x1 = std::min (x1, x0 + 2 * w2);
431 if (x1 - 2 * w2 - lw - 2 > x0) {
432 x0 = std::max (x0, x1 - 2 * w2 - lw - 2);
435 x0 = std::max (x0, x1 - 2 * w2);
440 lx = x1 + dx - lw - 2;
443 if (x1 - x0 < 2 * w2) {
444 return left ? x0 : x1;
447 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= x1) {
451 // TODO cache in set_colors()
452 uint32_t color = UIConfiguration::instance().color (
453 prelight ? "entered marker" : "location marker");
456 Gtkmm2ext::color_to_rgba (color, r, g, b, a);
459 const int y = PADDING;
461 cairo_rectangle (cr, lx, y, lw + 2, h);
462 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
463 cairo_fill_preserve (cr);
467 cairo_move_to (cr, lx + 1, y + .5 * (h - lh));
468 cairo_set_source_rgb (cr, 0, 0, 0);
469 pango_cairo_show_layout (cr, _layout->gobj());
474 cairo_move_to (cr, px - .5, PADDING + yc - .5);
475 cairo_rel_line_to (cr, dx , dy);
476 cairo_rel_line_to (cr, 0, -2. * dy);
477 cairo_close_path (cr);
478 cairo_set_source_rgba (cr, r, g, b, 1.0);
479 cairo_set_line_width (cr, 1.0);
480 cairo_stroke_preserve (cr);
483 return left ? x1 : x0;
487 struct LocationMarker {
488 LocationMarker (const std::string& l, samplepos_t w)
489 : label (l), when (w) {}
494 struct LocationMarkerSort {
495 bool operator() (const LocationMarker& a, const LocationMarker& b) {
496 return (a.when < b.when);
501 MiniTimeline::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
503 cairo_t* cr = ctx->cobj();
504 // TODO cache, set_colors()
505 Gtkmm2ext::Color base = UIConfiguration::instance().color ("ruler base");
506 Gtkmm2ext::Color text = UIConfiguration::instance().color ("ruler text");
508 if (_n_labels == 0) {
512 const int width = get_width ();
513 const int height = get_height ();
515 Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, 4);
516 Gtkmm2ext::set_source_rgba(cr, base);
519 Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, 4);
527 const samplepos_t p = _last_update_sample;
528 const samplepos_t lower = (std::max ((samplepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
530 int dot_left = width * .5 + (lower - p) * _px_per_sample;
531 for (int i = 0; i < 2 + _n_labels; ++i) {
532 samplepos_t when = lower + i * _time_granularity;
533 double xpos = width * .5 + (when - p) * _px_per_sample;
535 // TODO round to nearest display TC in +/- 1px
536 // prefer to display BBT |0 or .0
540 _layout->get_pixel_size (lw, lh);
542 int x0 = xpos - lw / 2.0;
543 int y0 = height - PADDING - _time_height;
545 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
547 cairo_move_to (cr, x0, y0);
548 Gtkmm2ext::set_source_rgba(cr, text);
549 pango_cairo_show_layout (cr, _layout->gobj());
552 draw_dots (cr, dot_left, width, height - PADDING - _time_height * .5, text);
555 samplepos_t lmin = std::max ((samplepos_t)0, (p - _time_span_samples));
556 samplepos_t lmax = p + _time_span_samples;
559 _layout->set_text (X_("Marker@"));
560 _layout->get_pixel_size (tw, th);
562 _marker_height = th + 2;
563 assert (_marker_height > 4);
564 const int mw = (_marker_height - 1) / 4;
566 lmin -= mw / _px_per_sample;
567 lmax += mw / _px_per_sample;
569 std::vector<LocationMarker> lm;
571 const Locations::LocationList& ll (_session->locations ()->list ());
572 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
573 if ((*l)->is_session_range ()) {
574 lm.push_back (LocationMarker(_("start"), (*l)->start ()));
575 lm.push_back (LocationMarker(_("end"), (*l)->end ()));
579 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
583 lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
588 LocationMarkerSort location_marker_sort;
589 std::sort (lm.begin(), lm.end(), location_marker_sort);
591 std::vector<LocationMarker>::const_iterator outside_left = lm.end();
592 std::vector<LocationMarker>::const_iterator outside_right = lm.end();
594 int right_limit = width * .5 + mw;
597 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
598 samplepos_t when = (*l).when;
601 if (++l != lm.end()) {
602 left_limit = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
604 left_limit = width * .5 - mw;
612 int x0 = floor (width * .5 + (when - p) * _px_per_sample);
614 const std::string& label = (*l).label;
615 if (++l != lm.end()) {
616 x1 = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
618 bool prelight = false;
619 x1 = draw_mark (cr, x0, x1, label, prelight);
620 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
621 right_limit = std::max (x1, right_limit);
624 if (outside_left != lm.end ()) {
625 if (left_limit > 3 * mw + PADDING) {
626 int x0 = PADDING + 1;
627 int x1 = left_limit - mw;
628 bool prelight = false;
629 x1 = draw_edge (cr, x0, x1, true, (*outside_left).label, prelight);
631 _jumplist.push_back (JumpRange (x0, x1, (*outside_left).when, prelight));
632 right_limit = std::max (x1, right_limit);
637 if (outside_right != lm.end ()) {
638 if (right_limit + PADDING < width - 3 * mw) {
639 int x0 = right_limit;
640 int x1 = width - PADDING;
641 bool prelight = false;
642 x0 = draw_edge (cr, x0, x1, false, (*outside_right).label, prelight);
644 _jumplist.push_back (JumpRange (x0, x1, (*outside_right).when, prelight));
650 /* playhead on top */
651 int xc = width * 0.5f;
652 cairo_set_line_width (cr, 1.0);
653 double r,g,b,a; Gtkmm2ext::color_to_rgba(_phead_color, r,g,b,a);
654 cairo_set_source_rgb (cr, r,g,b); // playhead color
655 cairo_move_to (cr, xc - .5, 0);
656 cairo_rel_line_to (cr, 0, height);
658 cairo_move_to (cr, xc - .5, height);
659 cairo_rel_line_to (cr, -3, 0);
660 cairo_rel_line_to (cr, 3, -4);
661 cairo_rel_line_to (cr, 3, 4);
662 cairo_close_path (cr);
667 MiniTimeline::build_minitl_context_menu ()
670 using namespace Gtk::Menu_Helpers;
674 const samplecnt_t time_span = _session->config.get_minitimeline_span ();
676 _minitl_context_menu = new Gtk::Menu();
677 MenuList& items = _minitl_context_menu->items();
679 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
680 std::map<samplecnt_t, std::string> spans;
681 spans[30] = _("30 sec");
682 spans[60] = _("1 min");
683 spans[120] = _("2 mins");
684 spans[300] = _("5 mins");
685 spans[600] = _("10 mins");
686 spans[1200] = _("20 mins");
688 RadioMenuItem::Group span_group;
689 for (std::map<samplecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
690 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
691 if (time_span == i->first) {
692 static_cast<RadioMenuItem*>(&items.back())->set_active ();
698 MiniTimeline::on_button_press_event (GdkEventButton *ev)
700 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
702 if (_minitl_context_menu == 0) {
703 build_minitl_context_menu ();
705 _minitl_context_menu->popup (ev->button, ev->time);
713 MiniTimeline::on_button_release_event (GdkEventButton *ev)
715 if (!_session) { return true; }
716 if (_session->actively_recording ()) { return true; }
717 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
721 if (ev->y <= PADDING + _marker_height) {
722 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
723 if (i->left <= ev->x && ev->x <= i->right) {
724 _session->request_locate (i->to, _session->transport_rolling ());
730 if (ev->button == 1) {
731 samplepos_t when = _last_update_sample + (ev->x - get_width() * .5) / _px_per_sample;
732 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
739 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
741 if (!_session) { return true; }
742 if (_session->actively_recording ()) { return true; }
747 bool need_expose = false;
749 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
750 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
751 if (!(*i).prelight) {
763 update_minitimeline ();
770 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
772 CairoWidget::on_leave_notify_event (ev);
773 _pointer_x = _pointer_y = -1;
774 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
776 update_minitimeline ();
784 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
786 if (!_session) { return true; }
787 if (_session->actively_recording ()) { return true; }
788 const samplecnt_t time_span = _session->config.get_minitimeline_span ();
789 samplepos_t when = _session->audible_sample ();
791 double scale = time_span / 60.0;
793 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
794 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
801 switch (ev->direction) {
803 case GDK_SCROLL_RIGHT:
804 when += scale * _session->nominal_sample_rate ();
806 case GDK_SCROLL_DOWN:
807 case GDK_SCROLL_LEFT:
808 when -= scale * _session->nominal_sample_rate ();
814 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());