update mini-timeline to immediately display tempo-map changes
[ardour.git] / gtk2_ardour / mini_timeline.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "ardour/audioengine.h"
20 #include "ardour/session.h"
21 #include "ardour/tempo.h"
22
23 #include "gtkmm2ext/gui_thread.h"
24 #include "gtkmm2ext/keyboard.h"
25
26 #include "canvas/colors.h"
27 #include "canvas/utils.h"
28
29 #include "ardour_ui.h"
30 #include "public_editor.h"
31 #include "main_clock.h"
32 #include "mini_timeline.h"
33 #include "timers.h"
34 #include "ui_config.h"
35
36 #include "pbd/i18n.h"
37
38 #define BBT_BAR_CHAR "|"
39
40 using namespace ARDOUR;
41
42 MiniTimeline::MiniTimeline ()
43         : _last_update_frame (-1)
44         , _clock_mode (AudioClock::Timecode)
45         , _time_width (0)
46         , _time_height (0)
47         , _n_labels (0)
48         , _px_per_sample (0)
49         , _time_granularity (0)
50         , _time_span_samples (0)
51         , _marker_height (0)
52         , _pointer_x (-1)
53         , _pointer_y (-1)
54         , _minitl_context_menu (0)
55 {
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);
57
58         _layout = Pango::Layout::create (get_pango_context());
59
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));
63
64         set_name ("minitimeline");
65
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 ());
70
71 }
72
73 MiniTimeline::~MiniTimeline ()
74 {
75         delete _minitl_context_menu;
76         _minitl_context_menu = 0;
77 }
78
79 void
80 MiniTimeline::session_going_away ()
81 {
82         super_rapid_connection.disconnect ();
83         session_connection.drop_connections ();
84         SessionHandlePtr::session_going_away ();
85         _jumplist.clear ();
86         delete _minitl_context_menu;
87         _minitl_context_menu = 0;
88 }
89
90 void
91 MiniTimeline::set_session (Session* s)
92 {
93         SessionHandlePtr::set_session (s);
94         if (!s) {
95                 return;
96         }
97
98         assert (!super_rapid_connection.connected ());
99         super_rapid_connection = Timers::super_rapid_connect (
100                         sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
101                         );
102
103         _session->config.ParameterChanged.connect (session_connection,
104                         invalidator (*this),
105                         boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
106                         );
107         _session->locations()->added.connect (session_connection,
108                         invalidator (*this),
109                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
110                         );
111         _session->locations()->removed.connect (session_connection,
112                         invalidator (*this),
113                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
114                         );
115         _session->locations()->changed.connect (session_connection,
116                         invalidator (*this),
117                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
118                         );
119
120         _jumplist.clear ();
121         calculate_time_spacing ();
122         update_minitimeline ();
123 }
124
125 void
126 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
127 {
128         CairoWidget::on_style_changed (old_style);
129         set_colors ();
130         calculate_time_width ();
131 }
132
133 void
134 MiniTimeline::on_name_changed ()
135 {
136         set_colors ();
137         calculate_time_width ();
138
139         if (is_realized()) {
140                 queue_resize ();
141         }
142 }
143
144 void
145 MiniTimeline::set_colors ()
146 {
147         // TODO  UIConfiguration::instance().color & font
148 }
149
150 void
151 MiniTimeline::parameter_changed (std::string const& p)
152 {
153         if (p == "minitimeline-span") {
154                 calculate_time_spacing ();
155                 update_minitimeline ();
156         }
157 }
158
159 void
160 MiniTimeline::on_size_request (Gtk::Requisition* req)
161 {
162         req->width = req->height = 0;
163         CairoWidget::on_size_request (req);
164
165         req->width = std::max (req->width, 1);
166         req->height = std::max (req->height, 20);
167 }
168
169 void
170 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
171 {
172         CairoWidget::on_size_allocate (alloc);
173         calculate_time_spacing ();
174 }
175
176 void
177 MiniTimeline::set_span (framecnt_t ts)
178 {
179         assert (_session);
180         if (_session->config.get_minitimeline_span () == ts) {
181                 return;
182         }
183
184         _session->config.set_minitimeline_span (ts);
185         calculate_time_spacing ();
186         update_minitimeline ();
187 }
188
189 void
190 MiniTimeline::super_rapid_update ()
191 {
192         if (!_session || !_session->engine().running()) {
193                 return;
194         }
195         framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
196         AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
197
198         bool change = false;
199         if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
200                 change = true;
201         }
202
203         if (m != _clock_mode) {
204                 _clock_mode = m;
205                 calculate_time_width ();
206                 change = true;
207         }
208
209         if (_clock_mode == AudioClock::BBT) {
210                 // TODO check if tempo-map changed
211                 change = true;
212         }
213
214         if (change) {
215                 _last_update_frame = frame;
216                 update_minitimeline ();
217         }
218 }
219
220 void
221 MiniTimeline::update_minitimeline ()
222 {
223         CairoWidget::set_dirty ();
224 }
225
226 void
227 MiniTimeline::calculate_time_width ()
228 {
229         switch (_clock_mode) {
230                 case AudioClock::Timecode:
231                         _layout->set_text (" 88:88:88,888 ");
232                         break;
233                 case AudioClock::BBT:
234                         _layout->set_text ("888|88|8888");
235                         break;
236                 case AudioClock::MinSec:
237                         _layout->set_text ("88:88:88,88");
238                         break;
239                 case AudioClock::Frames:
240                         _layout->set_text ("8888888888");
241                         break;
242         }
243         _layout->get_pixel_size (_time_width, _time_height);
244 }
245
246 void
247 MiniTimeline::calculate_time_spacing ()
248 {
249         _n_labels = floor (get_width () / (_time_width * 1.15));
250
251         if (_n_labels == 0 || !_session) {
252                 return;
253         }
254
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);
260 }
261
262 void
263 MiniTimeline::format_time (framepos_t when)
264 {
265         switch (_clock_mode) {
266                 case AudioClock::Timecode:
267                         {
268                                 Timecode::Time TC;
269                                 _session->timecode_time (when, TC);
270                                 // chop of leading space or minus.
271                                 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
272                         }
273                         break;
274                 case AudioClock::BBT:
275                         {
276                                 char buf[64];
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);
281                         }
282                         break;
283                 case AudioClock::MinSec:
284                         {
285                                 char buf[32];
286                                 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
287                                 _layout->set_text (std::string(buf).substr(1));
288                         }
289                         break;
290                 case AudioClock::Frames:
291                         {
292                                 char buf[32];
293                                 snprintf (buf, sizeof (buf), "%" PRId64, when);
294                                 _layout->set_text (buf);
295                         }
296                         break;
297         }
298 }
299
300 void
301 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
302 {
303         if (left + 1 >= right) {
304                 return;
305         }
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);
313         cairo_stroke (cr);
314         cairo_set_dash (cr, 0, 0, 0);
315 }
316
317 int
318 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
319 {
320         int h = _marker_height;
321         /* ArdourMarker shape
322          * MH = 13
323          *
324          * Mark:
325          *
326          *  (0,0)   --  (6,0)
327          *    |           |
328          *    |           |
329          * (0,MH*.4)  (6,MH*.4)
330          *     \         /
331          *        (3,MH)
332          */
333
334         int y = 3;
335         int w2 = (h - 1) / 4;
336         double h0 = h * .4;
337         double h1 = h - h0;
338
339         int lw, lh;
340         _layout->set_text (label);
341         _layout->get_pixel_size (lw, lh);
342         int rw = std::min (x1, x0 + w2 + lw + 2);
343
344         if (_pointer_y >= 0 && _pointer_y < h && _pointer_x >= x0 && _pointer_x <= rw) {
345                 prelight = true;
346         }
347
348         // TODO cache in set_colors()
349         uint32_t color = UIConfiguration::instance().color (
350                         prelight ? "entered marker" : "location marker");
351
352         double r, g, b, a;
353         ArdourCanvas::color_to_rgba (color, r, g, b, a);
354
355         if (rw < x0) {
356                 rw = x1;
357         } else {
358                 cairo_save (cr);
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);
362                 cairo_clip (cr);
363
364                 // marker label
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());
368                 cairo_restore (cr);
369         }
370
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);
382         cairo_fill (cr);
383
384         return rw;
385 }
386
387 struct LocationMarker {
388         LocationMarker (const std::string& l, framepos_t w)
389                 : label (l), when (w) {}
390         std::string label;
391         framepos_t  when;
392 };
393
394 struct LocationMarkerSort {
395         bool operator() (const LocationMarker& a, const LocationMarker& b) {
396                 return (a.when < b.when);
397         }
398 };
399
400 void
401 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
402 {
403         // TODO cache, set_colors()
404         ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
405         ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
406
407         if (_n_labels == 0) {
408                 return;
409         }
410
411         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
412         ArdourCanvas::set_source_rgba(cr, base);
413         cairo_fill (cr);
414
415         Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
416         cairo_clip (cr);
417
418         if (_session == 0) {
419                 return;
420         }
421
422
423         /* time */
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;
426
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;
431
432                 // TODO round to nearest display TC in +/- 1px
433                 // prefer to display BBT |0  or .0
434
435                 int lw, lh;
436                 format_time (when);
437                 _layout->get_pixel_size (lw, lh);
438
439                 int x0 = xpos - lw / 2.0;
440                 int y0 = get_height() - 3 - _time_height;
441
442                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
443
444                 cairo_move_to (cr, x0, y0);
445                 ArdourCanvas::set_source_rgba(cr, text);
446                 pango_cairo_show_layout (cr, _layout->gobj());
447                 dot_left = x0 + lw;
448         }
449         draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
450
451         /* locations */
452         framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
453         framepos_t lmax = p + _time_span_samples;
454
455         int tw, th;
456         _layout->set_text (X_("Marker@"));
457         _layout->get_pixel_size (tw, th);
458
459         _marker_height = th + 2;
460         assert (_marker_height > 4);
461         const int mw = (_marker_height - 1) / 4;
462
463         lmin -= mw / _px_per_sample;
464         lmax += mw / _px_per_sample;
465
466         std::vector<LocationMarker> lm;
467
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));
474                         }
475                         when = (*l)->end ();
476                         if (when >= lmin && when <= lmax) {
477                                 lm.push_back (LocationMarker(_("end"), when));
478                         }
479                         continue;
480                 }
481
482                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
483                         continue;
484                 }
485
486                 framepos_t when = (*l)->start ();
487                 if (when < lmin || when > lmax) {
488                         continue;
489                 }
490                 lm.push_back (LocationMarker((*l)->name(), when));
491         }
492
493         _jumplist.clear ();
494
495         LocationMarkerSort location_marker_sort;
496         std::sort (lm.begin(), lm.end(), location_marker_sort);
497
498         int id = 0;
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;
506                 }
507                 bool prelight = false;
508                 x1 = draw_mark (cr, x0, x1, label, prelight);
509                 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
510         }
511
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 ());
518         cairo_stroke (cr);
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);
524         cairo_fill (cr);
525 }
526
527 void
528 MiniTimeline::build_minitl_context_menu ()
529 {
530         using namespace Gtk;
531         using namespace Gtk::Menu_Helpers;
532
533         assert (_session);
534
535         const framecnt_t time_span = _session->config.get_minitimeline_span ();
536
537         _minitl_context_menu = new Gtk::Menu();
538         MenuList& items = _minitl_context_menu->items();
539
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");
548
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 ();
554                 }
555         }
556 }
557
558 void
559 MiniTimeline::show_minitl_context_menu ()
560 {
561         if (_minitl_context_menu == 0) {
562                 build_minitl_context_menu ();
563         }
564         _minitl_context_menu->popup (1, gtk_get_current_event_time());
565 }
566
567 bool
568 MiniTimeline::on_button_press_event (GdkEventButton *ev)
569 {
570         if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
571                 if (_session) {
572                         show_minitl_context_menu ();
573                 }
574                 return true;
575         }
576         return true;
577 }
578
579 bool
580 MiniTimeline::on_button_release_event (GdkEventButton *ev)
581 {
582         if (!_session) { return true; }
583         if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
584                 return true;
585         }
586
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 ());
591                                 return true;
592                         }
593                 }
594         }
595
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 ());
599         }
600
601         return true;
602 }
603
604 bool
605 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
606 {
607         if (!_session) { return true; }
608
609         _pointer_x = ev->x;
610         _pointer_y = ev->y;
611
612         bool need_expose = false;
613
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) {
617                                 need_expose = true;
618                                 break;
619                         }
620                 } else {
621                         if ((*i).prelight) {
622                                 need_expose = true;
623                                 break;
624                         }
625                 }
626         }
627         if (need_expose) {
628                 update_minitimeline ();
629         }
630
631         return true;
632 }
633
634 bool
635 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
636 {
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) {
640                 if ((*i).prelight) {
641                         update_minitimeline ();
642                         break;
643                 }
644         }
645         return true;
646 }
647
648 bool
649 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
650 {
651         if (!_session) { return true; }
652         const framecnt_t time_span = _session->config.get_minitimeline_span ();
653         framepos_t when = _session->audible_frame ();
654
655         double scale = time_span / 60.0;
656
657         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
658                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
659                         scale = 0.1;
660                 } else {
661                         scale = 0.5;
662                 }
663         }
664
665         switch (ev->direction) {
666                 case GDK_SCROLL_UP:
667                         when += scale * _session->nominal_frame_rate ();
668                         break;
669                 case GDK_SCROLL_DOWN:
670                         when -= scale * _session->nominal_frame_rate ();
671                         break;
672                 default:
673                         return true;
674                         break;
675         }
676         _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
677         return true;
678 }