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"
34 #include "ui_config.h"
38 #define BBT_BAR_CHAR "|"
40 using namespace ARDOUR;
42 MiniTimeline::MiniTimeline ()
43 : _last_update_frame (-1)
44 , _clock_mode (AudioClock::Timecode)
49 , _time_granularity (0)
50 , _time_span_samples (0)
54 , _minitl_context_menu (0)
56 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);
58 _layout = Pango::Layout::create (get_pango_context());
60 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
61 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
62 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
64 set_name ("minitimeline");
66 Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
67 Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
68 Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
69 Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
73 MiniTimeline::~MiniTimeline ()
75 delete _minitl_context_menu;
76 _minitl_context_menu = 0;
80 MiniTimeline::session_going_away ()
82 super_rapid_connection.disconnect ();
83 session_connection.drop_connections ();
84 SessionHandlePtr::session_going_away ();
86 delete _minitl_context_menu;
87 _minitl_context_menu = 0;
91 MiniTimeline::set_session (Session* s)
93 SessionHandlePtr::set_session (s);
98 assert (!super_rapid_connection.connected ());
99 super_rapid_connection = Timers::super_rapid_connect (
100 sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
103 _session->config.ParameterChanged.connect (session_connection,
105 boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
107 _session->locations()->added.connect (session_connection,
109 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
111 _session->locations()->removed.connect (session_connection,
113 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
115 _session->locations()->changed.connect (session_connection,
117 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
121 calculate_time_spacing ();
122 update_minitimeline ();
126 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
128 CairoWidget::on_style_changed (old_style);
130 calculate_time_width ();
134 MiniTimeline::on_name_changed ()
137 calculate_time_width ();
145 MiniTimeline::set_colors ()
147 // TODO UIConfiguration::instance().color & font
151 MiniTimeline::parameter_changed (std::string const& p)
153 if (p == "minitimeline-span") {
154 calculate_time_spacing ();
155 update_minitimeline ();
160 MiniTimeline::on_size_request (Gtk::Requisition* req)
162 req->width = req->height = 0;
163 CairoWidget::on_size_request (req);
165 req->width = std::max (req->width, 1);
166 req->height = std::max (req->height, 20);
170 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
172 CairoWidget::on_size_allocate (alloc);
173 calculate_time_spacing ();
177 MiniTimeline::set_span (framecnt_t ts)
180 if (_session->config.get_minitimeline_span () == ts) {
184 _session->config.set_minitimeline_span (ts);
185 calculate_time_spacing ();
186 update_minitimeline ();
190 MiniTimeline::super_rapid_update ()
192 if (!_session || !_session->engine().running()) {
195 framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
196 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
199 if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
203 if (m != _clock_mode) {
205 calculate_time_width ();
210 _last_update_frame = frame;
211 update_minitimeline ();
216 MiniTimeline::update_minitimeline ()
218 CairoWidget::set_dirty ();
222 MiniTimeline::calculate_time_width ()
224 switch (_clock_mode) {
225 case AudioClock::Timecode:
226 _layout->set_text (" 88:88:88,888 ");
228 case AudioClock::BBT:
229 _layout->set_text ("888|88|8888");
231 case AudioClock::MinSec:
232 _layout->set_text ("88:88:88,88");
234 case AudioClock::Frames:
235 _layout->set_text ("8888888888");
238 _layout->get_pixel_size (_time_width, _time_height);
242 MiniTimeline::calculate_time_spacing ()
244 _n_labels = floor (get_width () / (_time_width * 1.15));
246 if (_n_labels == 0 || !_session) {
250 const framecnt_t time_span = _session->config.get_minitimeline_span () / 2;
251 _time_span_samples = time_span * _session->nominal_frame_rate ();
252 _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
253 _px_per_sample = get_width () / (2. * _time_span_samples);
254 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
258 MiniTimeline::format_time (framepos_t when)
260 switch (_clock_mode) {
261 case AudioClock::Timecode:
264 _session->timecode_time (when, TC);
265 // chop of leading space or minus.
266 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
269 case AudioClock::BBT:
272 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
273 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
274 BBT.bars, BBT.beats, BBT.ticks);
275 _layout->set_text (buf);
278 case AudioClock::MinSec:
281 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
282 _layout->set_text (std::string(buf).substr(1));
285 case AudioClock::Frames:
288 snprintf (buf, sizeof (buf), "%" PRId64, when);
289 _layout->set_text (buf);
296 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
298 if (left + 1 >= right) {
301 cairo_move_to (cr, left + .5, y + .5);
302 cairo_line_to (cr, right - .5, y + .5);
303 ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
304 const double dashes[] = { 0, 1 };
305 cairo_set_dash (cr, dashes, 2, 1);
306 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
307 cairo_set_line_width (cr, 1.0);
309 cairo_set_dash (cr, 0, 0, 0);
313 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
315 int h = _marker_height;
316 /* ArdourMarker shape
324 * (0,MH*.4) (6,MH*.4)
330 int w2 = (h - 1) / 4;
335 _layout->set_text (label);
336 _layout->get_pixel_size (lw, lh);
337 int rw = std::min (x1, x0 + w2 + lw + 2);
339 if (_pointer_y >= 0 && _pointer_y < h && _pointer_x >= x0 && _pointer_x <= rw) {
343 // TODO cache in set_colors()
344 uint32_t color = UIConfiguration::instance().color (
345 prelight ? "entered marker" : "location marker");
348 ArdourCanvas::color_to_rgba (color, r, g, b, a);
354 cairo_rectangle (cr, x0, y, rw - x0, h);
355 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
356 cairo_fill_preserve (cr);
360 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
361 cairo_set_source_rgb (cr, 0, 0, 0);
362 pango_cairo_show_layout (cr, _layout->gobj());
366 // draw marker on top
367 cairo_move_to (cr, x0 - .5, y + .5);
368 cairo_rel_line_to (cr, -w2 , 0 );
369 cairo_rel_line_to (cr, 0, h0);
370 cairo_rel_line_to (cr, w2, h1);
371 cairo_rel_line_to (cr, w2, -h1);
372 cairo_rel_line_to (cr, 0, -h0);
373 cairo_close_path (cr);
374 cairo_set_source_rgba (cr, r, g, b, 1.0);
375 cairo_set_line_width (cr, 1.0);
376 cairo_stroke_preserve (cr);
382 struct LocationMarker {
383 LocationMarker (const std::string& l, framepos_t w)
384 : label (l), when (w) {}
389 struct LocationMarkerSort {
390 bool operator() (const LocationMarker& a, const LocationMarker& b) {
391 return (a.when < b.when);
396 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
398 // TODO cache, set_colors()
399 ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
400 ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
402 if (_n_labels == 0) {
406 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
407 ArdourCanvas::set_source_rgba(cr, base);
410 Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
419 const framepos_t p = _last_update_frame;
420 const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
422 int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
423 for (int i = 0; i < 2 + _n_labels; ++i) {
424 framepos_t when = lower + i * _time_granularity;
425 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
427 // TODO round to nearest display TC in +/- 1px
428 // prefer to display BBT |0 or .0
432 _layout->get_pixel_size (lw, lh);
434 int x0 = xpos - lw / 2.0;
435 int y0 = get_height() - 3 - _time_height;
437 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
439 cairo_move_to (cr, x0, y0);
440 ArdourCanvas::set_source_rgba(cr, text);
441 pango_cairo_show_layout (cr, _layout->gobj());
444 draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
447 framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
448 framepos_t lmax = p + _time_span_samples;
451 _layout->set_text (X_("Marker@"));
452 _layout->get_pixel_size (tw, th);
454 _marker_height = th + 2;
455 assert (_marker_height > 4);
456 const int mw = (_marker_height - 1) / 4;
458 lmin -= mw / _px_per_sample;
459 lmax += mw / _px_per_sample;
461 std::vector<LocationMarker> lm;
463 const Locations::LocationList& ll (_session->locations ()->list ());
464 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
465 if ((*l)->is_session_range ()) {
466 framepos_t when = (*l)->start ();
467 if (when >= lmin && when <= lmax) {
468 lm.push_back (LocationMarker(_("start"), when));
471 if (when >= lmin && when <= lmax) {
472 lm.push_back (LocationMarker(_("end"), when));
477 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
481 framepos_t when = (*l)->start ();
482 if (when < lmin || when > lmax) {
485 lm.push_back (LocationMarker((*l)->name(), when));
490 LocationMarkerSort location_marker_sort;
491 std::sort (lm.begin(), lm.end(), location_marker_sort);
494 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
495 framepos_t when = (*l).when;
496 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
497 int x1 = get_width();
498 const std::string& label = (*l).label;
499 if (++l != lm.end()) {
500 x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
502 bool prelight = false;
503 x1 = draw_mark (cr, x0, x1, label, prelight);
504 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
507 /* playhead on top */
508 int xc = get_width () * 0.5f;
509 cairo_set_line_width (cr, 1.0);
510 cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
511 cairo_move_to (cr, xc - .5, 0);
512 cairo_rel_line_to (cr, 0, get_height ());
514 cairo_move_to (cr, xc - .5, get_height ());
515 cairo_rel_line_to (cr, -3, 0);
516 cairo_rel_line_to (cr, 3, -4);
517 cairo_rel_line_to (cr, 3, 4);
518 cairo_close_path (cr);
523 MiniTimeline::build_minitl_context_menu ()
526 using namespace Gtk::Menu_Helpers;
530 const framecnt_t time_span = _session->config.get_minitimeline_span ();
532 _minitl_context_menu = new Gtk::Menu();
533 MenuList& items = _minitl_context_menu->items();
535 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
536 std::map<framecnt_t, std::string> spans;
537 spans[30] = _("30 sec");
538 spans[60] = _("1 min");
539 spans[120] = _("2 mins");
540 spans[300] = _("5 mins");
541 spans[600] = _("10 mins");
542 spans[1200] = _("20 mins");
544 RadioMenuItem::Group span_group;
545 for (std::map<framecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
546 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
547 if (time_span == i->first) {
548 static_cast<RadioMenuItem*>(&items.back())->set_active ();
554 MiniTimeline::show_minitl_context_menu ()
556 if (_minitl_context_menu == 0) {
557 build_minitl_context_menu ();
559 _minitl_context_menu->popup (1, gtk_get_current_event_time());
563 MiniTimeline::on_button_press_event (GdkEventButton *ev)
565 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
567 show_minitl_context_menu ();
575 MiniTimeline::on_button_release_event (GdkEventButton *ev)
577 if (!_session) { return true; }
578 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
582 if (ev->y <= _marker_height) {
583 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
584 if (i->left < ev->x && ev->x < i->right) {
585 _session->request_locate (i->to, _session->transport_rolling ());
591 if (ev->button == 1) {
592 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
593 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
600 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
602 if (!_session) { return true; }
607 bool need_expose = false;
609 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
610 if (i->left < ev->x && ev->x < i->right && ev->y <= _marker_height) {
611 if (!(*i).prelight) {
623 update_minitimeline ();
630 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
632 CairoWidget::on_leave_notify_event (ev);
633 _pointer_x = _pointer_y = -1;
634 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
636 update_minitimeline ();
644 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
646 if (!_session) { return true; }
647 const framecnt_t time_span = _session->config.get_minitimeline_span ();
648 framepos_t when = _session->audible_frame ();
650 double scale = time_span / 60.0;
652 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
653 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
660 switch (ev->direction) {
662 when += scale * _session->nominal_frame_rate ();
664 case GDK_SCROLL_DOWN:
665 when -= scale * _session->nominal_frame_rate ();
671 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());