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)
52 _layout = Pango::Layout::create (get_pango_context());
54 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
55 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
56 UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
58 set_name ("minitimeline");
60 Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
61 Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
62 Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
63 Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
67 MiniTimeline::~MiniTimeline ()
72 MiniTimeline::session_going_away ()
74 super_rapid_connection.disconnect ();
75 session_connection.drop_connections ();
76 SessionHandlePtr::session_going_away ();
81 MiniTimeline::set_session (Session* s)
83 SessionHandlePtr::set_session (s);
88 assert (!super_rapid_connection.connected ());
89 super_rapid_connection = Timers::super_rapid_connect (
90 sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
93 _session->config.ParameterChanged.connect (session_connection,
95 boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
97 _session->locations()->added.connect (session_connection,
99 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
101 _session->locations()->removed.connect (session_connection,
103 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
105 _session->locations()->changed.connect (session_connection,
107 boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
114 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
116 CairoWidget::on_style_changed (old_style);
118 calculate_time_width ();
122 MiniTimeline::on_name_changed ()
125 calculate_time_width ();
133 MiniTimeline::set_colors ()
135 // TODO UIConfiguration::instance().color & font
139 MiniTimeline::parameter_changed (std::string const& p)
141 if (p == "minitimeline-span") {
142 calculate_time_spacing ();
143 update_minitimeline ();
148 MiniTimeline::on_size_request (Gtk::Requisition* req)
150 req->width = req->height = 0;
151 CairoWidget::on_size_request (req);
153 req->width = std::max (req->width, 1);
154 req->height = std::max (req->height, 20);
158 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
160 CairoWidget::on_size_allocate (alloc);
161 calculate_time_spacing ();
165 MiniTimeline::super_rapid_update ()
167 if (!_session || !_session->engine().running()) {
170 framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
171 AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
174 if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
178 if (m != _clock_mode) {
180 calculate_time_width ();
185 _last_update_frame = frame;
186 update_minitimeline ();
191 MiniTimeline::update_minitimeline ()
193 CairoWidget::set_dirty ();
197 MiniTimeline::calculate_time_width ()
199 switch (_clock_mode) {
200 case AudioClock::Timecode:
201 _layout->set_text (" 88:88:88,888 ");
203 case AudioClock::BBT:
204 _layout->set_text ("888|88|8888");
206 case AudioClock::MinSec:
207 _layout->set_text ("88:88:88,88");
209 case AudioClock::Frames:
210 _layout->set_text ("8888888888");
213 _layout->get_pixel_size (_time_width, _time_height);
217 MiniTimeline::calculate_time_spacing ()
219 _n_labels = floor (get_width () / (_time_width * 1.15));
221 if (_n_labels == 0 || !_session) {
225 const framepos_t time_span = _session->config.get_minitimeline_span () / 2;
226 _time_span_samples = time_span * _session->nominal_frame_rate ();
227 _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
228 _px_per_sample = get_width () / (2. * _time_span_samples);
229 //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
233 MiniTimeline::format_time (framepos_t when)
235 switch (_clock_mode) {
236 case AudioClock::Timecode:
239 _session->timecode_time (when, TC);
240 // chop of leading space or minus.
241 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
244 case AudioClock::BBT:
247 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
248 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
249 BBT.bars, BBT.beats, BBT.ticks);
250 _layout->set_text (buf);
253 case AudioClock::MinSec:
256 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
257 _layout->set_text (std::string(buf).substr(1));
260 case AudioClock::Frames:
263 snprintf (buf, sizeof (buf), "%" PRId64, when);
264 _layout->set_text (buf);
271 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
273 if (left + 1 >= right) {
276 cairo_move_to (cr, left + .5, y + .5);
277 cairo_line_to (cr, right - .5, y + .5);
278 ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
279 const double dashes[] = { 0, 1 };
280 cairo_set_dash (cr, dashes, 2, 1);
281 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
282 cairo_set_line_width (cr, 1.0);
284 cairo_set_dash (cr, 0, 0, 0);
288 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, int h, const std::string& label)
290 /* ArdourMarker shape
298 * (0,MH*.4) (6,MH*.4)
304 int w2 = (h - 1) / 4;
309 _layout->set_text (label);
310 _layout->get_pixel_size (lw, lh);
312 // TODO cache, set_colors()
313 uint32_t color = UIConfiguration::instance().color ("location marker");
315 ArdourCanvas::color_to_rgba (color, r, g, b, a);
317 int rw = std::min (x1, x0 + w2 + lw + 2);
322 cairo_rectangle (cr, x0, y, rw - x0, h);
323 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
324 cairo_fill_preserve (cr);
328 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
329 cairo_set_source_rgb (cr, 0, 0, 0);
330 pango_cairo_show_layout (cr, _layout->gobj());
334 // draw marker on top
335 cairo_move_to (cr, x0 - .5, y + .5);
336 cairo_rel_line_to (cr, -w2 , 0 );
337 cairo_rel_line_to (cr, 0, h0);
338 cairo_rel_line_to (cr, w2, h1);
339 cairo_rel_line_to (cr, w2, -h1);
340 cairo_rel_line_to (cr, 0, -h0);
341 cairo_close_path (cr);
342 cairo_set_source_rgba (cr, r, g, b, 1.0);
343 cairo_set_line_width (cr, 1.0);
344 cairo_stroke_preserve (cr);
350 struct LocationMarker {
351 LocationMarker (const std::string& l, framepos_t w)
352 : label (l), when (w) {}
357 struct LocationMarkerSort {
358 bool operator() (const LocationMarker& a, const LocationMarker& b) {
359 return (a.when < b.when);
364 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
366 // TODO cache, set_colors()
367 ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
368 ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
370 if (_n_labels == 0) {
374 Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
375 ArdourCanvas::set_source_rgba(cr, base);
378 Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
387 const framepos_t p = _last_update_frame;
388 const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
390 int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
391 for (int i = 0; i < 2 + _n_labels; ++i) {
392 framepos_t when = lower + i * _time_granularity;
393 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
395 // TODO round to nearest display TC in +/- 1px
396 // prefer to display BBT |0 or .0
400 _layout->get_pixel_size (lw, lh);
402 int x0 = xpos - lw / 2.0;
403 int y0 = get_height() - 3 - _time_height;
405 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
407 cairo_move_to (cr, x0, y0);
408 ArdourCanvas::set_source_rgba(cr, text);
409 pango_cairo_show_layout (cr, _layout->gobj());
412 draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
415 framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
416 framepos_t lmax = p + _time_span_samples;
419 _layout->set_text (X_("Marker@"));
420 _layout->get_pixel_size (tw, th);
422 const int mh = th + 2;
424 const int mw = (mh - 1) / 4;
426 lmin -= mw / _px_per_sample;
427 lmax += mw / _px_per_sample;
429 std::vector<LocationMarker> lm;
431 const Locations::LocationList& ll (_session->locations ()->list ());
432 for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
433 if ((*l)->is_session_range ()) {
434 framepos_t when = (*l)->start ();
435 if (when >= lmin && when <= lmax) {
436 lm.push_back (LocationMarker(_("start"), when));
439 if (when >= lmin && when <= lmax) {
440 lm.push_back (LocationMarker(_("end"), when));
445 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
449 framepos_t when = (*l)->start ();
450 if (when < lmin || when > lmax) {
453 lm.push_back (LocationMarker((*l)->name(), when));
458 LocationMarkerSort location_marker_sort;
459 std::sort (lm.begin(), lm.end(), location_marker_sort);
461 for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end();) {
462 framepos_t when = (*l).when;
463 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
464 int x1 = get_width();
465 const std::string& label = (*l).label;
466 if (++l != lm.end()) {
467 x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
469 x1 = draw_mark (cr, x0, x1, mh, label);
470 _jumplist.push_back (JumpRange (x0 - mw, x1, when));
473 /* playhead on top */
474 int xc = get_width () * 0.5f;
475 cairo_set_line_width (cr, 1.0);
476 cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
477 cairo_move_to (cr, xc - .5, 0);
478 cairo_rel_line_to (cr, 0, get_height ());
480 cairo_move_to (cr, xc - .5, get_height ());
481 cairo_rel_line_to (cr, -3, 0);
482 cairo_rel_line_to (cr, 3, -4);
483 cairo_rel_line_to (cr, 3, 4);
484 cairo_close_path (cr);
489 MiniTimeline::on_button_release_event (GdkEventButton *ev)
491 if (!_session) { return true; }
493 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
494 if (i->left < ev->x && ev->x < i->right) {
495 if (ev->button == 3) {
496 PublicEditor::instance().center_screen (i->to);
497 } else if (ev->button == 1) {
498 _session->request_locate (i->to, _session->transport_rolling ());
504 if (ev->button == 1) {
505 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
506 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
513 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
515 if (!_session) { return true; }
516 framepos_t when = _session->audible_frame ();
519 if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
520 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
527 switch (ev->direction) {
529 when += scale * _session->nominal_frame_rate ();
531 case GDK_SCROLL_DOWN:
532 when -= scale * _session->nominal_frame_rate ();
538 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());