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 ();
209 if (_clock_mode == AudioClock::BBT) {
210 // TODO check if tempo-map changed
215 _last_update_frame = frame;
216 update_minitimeline ();
221 MiniTimeline::update_minitimeline ()
223 CairoWidget::set_dirty ();
227 MiniTimeline::calculate_time_width ()
229 switch (_clock_mode) {
230 case AudioClock::Timecode:
231 _layout->set_text (" 88:88:88,888 ");
233 case AudioClock::BBT:
234 _layout->set_text ("888|88|8888");
236 case AudioClock::MinSec:
237 _layout->set_text ("88:88:88,88");
239 case AudioClock::Frames:
240 _layout->set_text ("8888888888");
243 _layout->get_pixel_size (_time_width, _time_height);
247 MiniTimeline::calculate_time_spacing ()
249 _n_labels = floor (get_width () / (_time_width * 1.15));
251 if (_n_labels == 0 || !_session) {
255 const framecnt_t time_span = _session->config.get_minitimeline_span () / 2;
256 _time_span_samples = time_span * _session->nominal_frame_rate ();
257 _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
258 _px_per_sample = get_width () / (2. * _time_span_samples);
259 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
263 MiniTimeline::format_time (framepos_t when)
265 switch (_clock_mode) {
266 case AudioClock::Timecode:
269 _session->timecode_time (when, TC);
270 // chop of leading space or minus.
271 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
274 case AudioClock::BBT:
277 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
278 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
279 BBT.bars, BBT.beats, BBT.ticks);
280 _layout->set_text (buf);
283 case AudioClock::MinSec:
286 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
287 _layout->set_text (std::string(buf).substr(1));
290 case AudioClock::Frames:
293 snprintf (buf, sizeof (buf), "%" PRId64, when);
294 _layout->set_text (buf);
301 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
303 if (left + 1 >= right) {
306 cairo_move_to (cr, left + .5, y + .5);
307 cairo_line_to (cr, right - .5, y + .5);
308 ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
309 const double dashes[] = { 0, 1 };
310 cairo_set_dash (cr, dashes, 2, 1);
311 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
312 cairo_set_line_width (cr, 1.0);
314 cairo_set_dash (cr, 0, 0, 0);
318 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
320 int h = _marker_height;
321 /* ArdourMarker shape
329 * (0,MH*.4) (6,MH*.4)
335 int w2 = (h - 1) / 4;
340 _layout->set_text (label);
341 _layout->get_pixel_size (lw, lh);
342 int rw = std::min (x1, x0 + w2 + lw + 2);
344 if (_pointer_y >= 0 && _pointer_y < h && _pointer_x >= x0 && _pointer_x <= rw) {
348 // TODO cache in set_colors()
349 uint32_t color = UIConfiguration::instance().color (
350 prelight ? "entered marker" : "location marker");
353 ArdourCanvas::color_to_rgba (color, r, g, b, a);
359 cairo_rectangle (cr, x0, y, rw - x0, h);
360 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
361 cairo_fill_preserve (cr);
365 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
366 cairo_set_source_rgb (cr, 0, 0, 0);
367 pango_cairo_show_layout (cr, _layout->gobj());
371 // draw marker on top
372 cairo_move_to (cr, x0 - .5, y + .5);
373 cairo_rel_line_to (cr, -w2 , 0 );
374 cairo_rel_line_to (cr, 0, h0);
375 cairo_rel_line_to (cr, w2, h1);
376 cairo_rel_line_to (cr, w2, -h1);
377 cairo_rel_line_to (cr, 0, -h0);
378 cairo_close_path (cr);
379 cairo_set_source_rgba (cr, r, g, b, 1.0);
380 cairo_set_line_width (cr, 1.0);
381 cairo_stroke_preserve (cr);
387 struct LocationMarker {
388 LocationMarker (const std::string& l, framepos_t w)
389 : label (l), when (w) {}
394 struct LocationMarkerSort {
395 bool operator() (const LocationMarker& a, const LocationMarker& b) {
396 return (a.when < b.when);
401 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
403 // TODO cache, set_colors()
404 ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
405 ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
407 if (_n_labels == 0) {
411 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
412 ArdourCanvas::set_source_rgba(cr, base);
415 Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
424 const framepos_t p = _last_update_frame;
425 const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
427 int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
428 for (int i = 0; i < 2 + _n_labels; ++i) {
429 framepos_t when = lower + i * _time_granularity;
430 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
432 // TODO round to nearest display TC in +/- 1px
433 // prefer to display BBT |0 or .0
437 _layout->get_pixel_size (lw, lh);
439 int x0 = xpos - lw / 2.0;
440 int y0 = get_height() - 3 - _time_height;
442 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
444 cairo_move_to (cr, x0, y0);
445 ArdourCanvas::set_source_rgba(cr, text);
446 pango_cairo_show_layout (cr, _layout->gobj());
449 draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
452 framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
453 framepos_t lmax = p + _time_span_samples;
456 _layout->set_text (X_("Marker@"));
457 _layout->get_pixel_size (tw, th);
459 _marker_height = th + 2;
460 assert (_marker_height > 4);
461 const int mw = (_marker_height - 1) / 4;
463 lmin -= mw / _px_per_sample;
464 lmax += mw / _px_per_sample;
466 std::vector<LocationMarker> lm;
468 const Locations::LocationList& ll (_session->locations ()->list ());
469 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
470 if ((*l)->is_session_range ()) {
471 framepos_t when = (*l)->start ();
472 if (when >= lmin && when <= lmax) {
473 lm.push_back (LocationMarker(_("start"), when));
476 if (when >= lmin && when <= lmax) {
477 lm.push_back (LocationMarker(_("end"), when));
482 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
486 framepos_t when = (*l)->start ();
487 if (when < lmin || when > lmax) {
490 lm.push_back (LocationMarker((*l)->name(), when));
495 LocationMarkerSort location_marker_sort;
496 std::sort (lm.begin(), lm.end(), location_marker_sort);
499 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
500 framepos_t when = (*l).when;
501 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
502 int x1 = get_width();
503 const std::string& label = (*l).label;
504 if (++l != lm.end()) {
505 x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
507 bool prelight = false;
508 x1 = draw_mark (cr, x0, x1, label, prelight);
509 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
512 /* playhead on top */
513 int xc = get_width () * 0.5f;
514 cairo_set_line_width (cr, 1.0);
515 cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
516 cairo_move_to (cr, xc - .5, 0);
517 cairo_rel_line_to (cr, 0, get_height ());
519 cairo_move_to (cr, xc - .5, get_height ());
520 cairo_rel_line_to (cr, -3, 0);
521 cairo_rel_line_to (cr, 3, -4);
522 cairo_rel_line_to (cr, 3, 4);
523 cairo_close_path (cr);
528 MiniTimeline::build_minitl_context_menu ()
531 using namespace Gtk::Menu_Helpers;
535 const framecnt_t time_span = _session->config.get_minitimeline_span ();
537 _minitl_context_menu = new Gtk::Menu();
538 MenuList& items = _minitl_context_menu->items();
540 // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
541 std::map<framecnt_t, std::string> spans;
542 spans[30] = _("30 sec");
543 spans[60] = _("1 min");
544 spans[120] = _("2 mins");
545 spans[300] = _("5 mins");
546 spans[600] = _("10 mins");
547 spans[1200] = _("20 mins");
549 RadioMenuItem::Group span_group;
550 for (std::map<framecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
551 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
552 if (time_span == i->first) {
553 static_cast<RadioMenuItem*>(&items.back())->set_active ();
559 MiniTimeline::show_minitl_context_menu ()
561 if (_minitl_context_menu == 0) {
562 build_minitl_context_menu ();
564 _minitl_context_menu->popup (1, gtk_get_current_event_time());
568 MiniTimeline::on_button_press_event (GdkEventButton *ev)
570 if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
572 show_minitl_context_menu ();
580 MiniTimeline::on_button_release_event (GdkEventButton *ev)
582 if (!_session) { return true; }
583 if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
587 if (ev->y <= _marker_height) {
588 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
589 if (i->left < ev->x && ev->x < i->right) {
590 _session->request_locate (i->to, _session->transport_rolling ());
596 if (ev->button == 1) {
597 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
598 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
605 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
607 if (!_session) { return true; }
612 bool need_expose = false;
614 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
615 if (i->left < ev->x && ev->x < i->right && ev->y <= _marker_height) {
616 if (!(*i).prelight) {
628 update_minitimeline ();
635 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
637 CairoWidget::on_leave_notify_event (ev);
638 _pointer_x = _pointer_y = -1;
639 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
641 update_minitimeline ();
649 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
651 if (!_session) { return true; }
652 const framecnt_t time_span = _session->config.get_minitimeline_span ();
653 framepos_t when = _session->audible_frame ();
655 double scale = time_span / 60.0;
657 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
658 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
665 switch (ev->direction) {
667 when += scale * _session->nominal_frame_rate ();
669 case GDK_SCROLL_DOWN:
670 when -= scale * _session->nominal_frame_rate ();
676 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());