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/gui_thread.h"
24 #include "gtkmm2ext/keyboard.h"
26 #include "canvas/colors.h"
27 #include "canvas/utils.h"
29 #include "ardour_ui.h"
30 #include "public_editor.h"
31 #include "main_clock.h"
32 #include "mini_timeline.h"
35 #include "ui_config.h"
40 #define BBT_BAR_CHAR "|"
42 using namespace ARDOUR;
44 MiniTimeline::MiniTimeline ()
45 : _last_update_frame (-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 ARDOUR_UI_UTILS::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 gained 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
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 (framecnt_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 framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
203 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
206 if (fabs ((_last_update_frame - frame) * _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_frame = frame;
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::Frames:
247 _layout->set_text ("8888888888");
250 _layout->get_pixel_size (_time_width, _time_height);
254 MiniTimeline::calculate_time_spacing ()
256 _n_labels = floor (get_width () / (_time_width * 1.15));
258 if (_n_labels == 0 || !_session) {
262 const framecnt_t time_span = _session->config.get_minitimeline_span () / 2;
263 _time_span_samples = time_span * _session->nominal_frame_rate ();
264 _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
265 _px_per_sample = get_width () / (2. * _time_span_samples);
266 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
270 MiniTimeline::format_time (framepos_t when)
272 switch (_clock_mode) {
273 case AudioClock::Timecode:
276 _session->timecode_time (when, TC);
277 // chop of leading space or minus.
278 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
281 case AudioClock::BBT:
284 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
285 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
286 BBT.bars, BBT.beats, BBT.ticks);
287 _layout->set_text (buf);
290 case AudioClock::MinSec:
293 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
294 _layout->set_text (std::string(buf).substr(1));
297 case AudioClock::Frames:
300 snprintf (buf, sizeof (buf), "%" PRId64, when);
301 _layout->set_text (buf);
308 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
310 if (left + 1 >= right) {
313 cairo_move_to (cr, left + .5, y + .5);
314 cairo_line_to (cr, right - .5, y + .5);
315 ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
316 const double dashes[] = { 0, 1 };
317 cairo_set_dash (cr, dashes, 2, 1);
318 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
319 cairo_set_line_width (cr, 1.0);
321 cairo_set_dash (cr, 0, 0, 0);
325 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
327 int h = _marker_height;
328 /* ArdourMarker shape
336 * (0,MH*.4) (6,MH*.4)
341 const int y = PADDING;
342 int w2 = (h - 1) / 4;
347 _layout->set_text (label);
348 _layout->get_pixel_size (lw, lh);
349 int rw = std::min (x1, x0 + w2 + lw + 2);
351 if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= rw) {
355 // TODO cache in set_colors()
356 uint32_t color = UIConfiguration::instance().color (
357 prelight ? "entered marker" : "location marker");
360 ArdourCanvas::color_to_rgba (color, r, g, b, a);
366 cairo_rectangle (cr, x0, y, rw - x0, h);
367 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
368 cairo_fill_preserve (cr);
372 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
373 cairo_set_source_rgb (cr, 0, 0, 0);
374 pango_cairo_show_layout (cr, _layout->gobj());
378 // draw marker on top
379 cairo_move_to (cr, x0 - .5, y + .5);
380 cairo_rel_line_to (cr, -w2 , 0 );
381 cairo_rel_line_to (cr, 0, h0);
382 cairo_rel_line_to (cr, w2, h1);
383 cairo_rel_line_to (cr, w2, -h1);
384 cairo_rel_line_to (cr, 0, -h0);
385 cairo_close_path (cr);
386 cairo_set_source_rgba (cr, r, g, b, 1.0);
387 cairo_set_line_width (cr, 1.0);
388 cairo_stroke_preserve (cr);
394 struct LocationMarker {
395 LocationMarker (const std::string& l, framepos_t w)
396 : label (l), when (w) {}
401 struct LocationMarkerSort {
402 bool operator() (const LocationMarker& a, const LocationMarker& b) {
403 return (a.when < b.when);
408 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
410 // TODO cache, set_colors()
411 ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
412 ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
414 if (_n_labels == 0) {
418 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
419 ArdourCanvas::set_source_rgba(cr, base);
422 Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, get_width() - PADDING - PADDING, get_height() - PADDING - PADDING, 4);
431 const framepos_t p = _last_update_frame;
432 const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
434 int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
435 for (int i = 0; i < 2 + _n_labels; ++i) {
436 framepos_t when = lower + i * _time_granularity;
437 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
439 // TODO round to nearest display TC in +/- 1px
440 // prefer to display BBT |0 or .0
444 _layout->get_pixel_size (lw, lh);
446 int x0 = xpos - lw / 2.0;
447 int y0 = get_height() - PADDING - _time_height;
449 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
451 cairo_move_to (cr, x0, y0);
452 ArdourCanvas::set_source_rgba(cr, text);
453 pango_cairo_show_layout (cr, _layout->gobj());
456 draw_dots (cr, dot_left, get_width(), get_height() - PADDING - _time_height * .5, text);
459 framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
460 framepos_t lmax = p + _time_span_samples;
463 _layout->set_text (X_("Marker@"));
464 _layout->get_pixel_size (tw, th);
466 _marker_height = th + 2;
467 assert (_marker_height > 4);
468 const int mw = (_marker_height - 1) / 4;
470 lmin -= mw / _px_per_sample;
471 lmax += mw / _px_per_sample;
473 std::vector<LocationMarker> lm;
475 const Locations::LocationList& ll (_session->locations ()->list ());
476 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
477 if ((*l)->is_session_range ()) {
478 framepos_t when = (*l)->start ();
479 if (when >= lmin && when <= lmax) {
480 lm.push_back (LocationMarker(_("start"), when));
483 if (when >= lmin && when <= lmax) {
484 lm.push_back (LocationMarker(_("end"), when));
489 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
493 framepos_t when = (*l)->start ();
494 if (when < lmin || when > lmax) {
497 lm.push_back (LocationMarker((*l)->name(), when));
502 LocationMarkerSort location_marker_sort;
503 std::sort (lm.begin(), lm.end(), location_marker_sort);
506 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
507 framepos_t when = (*l).when;
508 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
509 int x1 = get_width();
510 const std::string& label = (*l).label;
511 if (++l != lm.end()) {
512 x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
514 bool prelight = false;
515 x1 = draw_mark (cr, x0, x1, label, prelight);
516 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
519 /* playhead on top */
520 int xc = get_width () * 0.5f;
521 cairo_set_line_width (cr, 1.0);
522 cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
523 cairo_move_to (cr, xc - .5, 0);
524 cairo_rel_line_to (cr, 0, get_height ());
526 cairo_move_to (cr, xc - .5, get_height ());
527 cairo_rel_line_to (cr, -3, 0);
528 cairo_rel_line_to (cr, 3, -4);
529 cairo_rel_line_to (cr, 3, 4);
530 cairo_close_path (cr);
535 MiniTimeline::build_minitl_context_menu ()
538 using namespace Gtk::Menu_Helpers;
542 const framecnt_t time_span = _session->config.get_minitimeline_span ();
544 _minitl_context_menu = new Gtk::Menu();
545 MenuList& items = _minitl_context_menu->items();
547 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
548 std::map<framecnt_t, std::string> spans;
549 spans[30] = _("30 sec");
550 spans[60] = _("1 min");
551 spans[120] = _("2 mins");
552 spans[300] = _("5 mins");
553 spans[600] = _("10 mins");
554 spans[1200] = _("20 mins");
556 RadioMenuItem::Group span_group;
557 for (std::map<framecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
558 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
559 if (time_span == i->first) {
560 static_cast<RadioMenuItem*>(&items.back())->set_active ();
566 MiniTimeline::show_minitl_context_menu ()
568 if (_minitl_context_menu == 0) {
569 build_minitl_context_menu ();
571 _minitl_context_menu->popup (1, gtk_get_current_event_time());
575 MiniTimeline::on_button_press_event (GdkEventButton *ev)
577 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
579 show_minitl_context_menu ();
587 MiniTimeline::on_button_release_event (GdkEventButton *ev)
589 if (!_session) { return true; }
590 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
594 if (ev->y <= PADDING + _marker_height) {
595 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
596 if (i->left < ev->x && ev->x < i->right) {
597 _session->request_locate (i->to, _session->transport_rolling ());
603 if (ev->button == 1) {
604 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
605 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
612 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
614 if (!_session) { return true; }
619 bool need_expose = false;
621 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
622 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
623 if (!(*i).prelight) {
635 update_minitimeline ();
642 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
644 CairoWidget::on_leave_notify_event (ev);
645 _pointer_x = _pointer_y = -1;
646 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
648 update_minitimeline ();
656 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
658 if (!_session) { return true; }
659 const framecnt_t time_span = _session->config.get_minitimeline_span ();
660 framepos_t when = _session->audible_frame ();
662 double scale = time_span / 60.0;
664 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
665 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
672 switch (ev->direction) {
674 case GDK_SCROLL_RIGHT:
675 when += scale * _session->nominal_frame_rate ();
677 case GDK_SCROLL_DOWN:
678 case GDK_SCROLL_LEFT:
679 when -= scale * _session->nominal_frame_rate ();
685 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());