cont'd work on mini-timeline:
[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 (change) {
210                 _last_update_frame = frame;
211                 update_minitimeline ();
212         }
213 }
214
215 void
216 MiniTimeline::update_minitimeline ()
217 {
218         CairoWidget::set_dirty ();
219 }
220
221 void
222 MiniTimeline::calculate_time_width ()
223 {
224         switch (_clock_mode) {
225                 case AudioClock::Timecode:
226                         _layout->set_text (" 88:88:88,888 ");
227                         break;
228                 case AudioClock::BBT:
229                         _layout->set_text ("888|88|8888");
230                         break;
231                 case AudioClock::MinSec:
232                         _layout->set_text ("88:88:88,88");
233                         break;
234                 case AudioClock::Frames:
235                         _layout->set_text ("8888888888");
236                         break;
237         }
238         _layout->get_pixel_size (_time_width, _time_height);
239 }
240
241 void
242 MiniTimeline::calculate_time_spacing ()
243 {
244         _n_labels = floor (get_width () / (_time_width * 1.15));
245
246         if (_n_labels == 0 || !_session) {
247                 return;
248         }
249
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);
255 }
256
257 void
258 MiniTimeline::format_time (framepos_t when)
259 {
260         switch (_clock_mode) {
261                 case AudioClock::Timecode:
262                         {
263                                 Timecode::Time TC;
264                                 _session->timecode_time (when, TC);
265                                 // chop of leading space or minus.
266                                 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
267                         }
268                         break;
269                 case AudioClock::BBT:
270                         {
271                                 char buf[64];
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);
276                         }
277                         break;
278                 case AudioClock::MinSec:
279                         {
280                                 char buf[32];
281                                 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
282                                 _layout->set_text (std::string(buf).substr(1));
283                         }
284                         break;
285                 case AudioClock::Frames:
286                         {
287                                 char buf[32];
288                                 snprintf (buf, sizeof (buf), "%" PRId64, when);
289                                 _layout->set_text (buf);
290                         }
291                         break;
292         }
293 }
294
295 void
296 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
297 {
298         if (left + 1 >= right) {
299                 return;
300         }
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);
308         cairo_stroke (cr);
309         cairo_set_dash (cr, 0, 0, 0);
310 }
311
312 int
313 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
314 {
315         int h = _marker_height;
316         /* ArdourMarker shape
317          * MH = 13
318          *
319          * Mark:
320          *
321          *  (0,0)   --  (6,0)
322          *    |           |
323          *    |           |
324          * (0,MH*.4)  (6,MH*.4)
325          *     \         /
326          *        (3,MH)
327          */
328
329         int y = 3;
330         int w2 = (h - 1) / 4;
331         double h0 = h * .4;
332         double h1 = h - h0;
333
334         int lw, lh;
335         _layout->set_text (label);
336         _layout->get_pixel_size (lw, lh);
337         int rw = std::min (x1, x0 + w2 + lw + 2);
338
339         if (_pointer_y >= 0 && _pointer_y < h && _pointer_x >= x0 && _pointer_x <= rw) {
340                 prelight = true;
341         }
342
343         // TODO cache in set_colors()
344         uint32_t color = UIConfiguration::instance().color (
345                         prelight ? "entered marker" : "location marker");
346
347         double r, g, b, a;
348         ArdourCanvas::color_to_rgba (color, r, g, b, a);
349
350         if (rw < x0) {
351                 rw = x1;
352         } else {
353                 cairo_save (cr);
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);
357                 cairo_clip (cr);
358
359                 // marker label
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());
363                 cairo_restore (cr);
364         }
365
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);
377         cairo_fill (cr);
378
379         return rw;
380 }
381
382 struct LocationMarker {
383         LocationMarker (const std::string& l, framepos_t w)
384                 : label (l), when (w) {}
385         std::string label;
386         framepos_t  when;
387 };
388
389 struct LocationMarkerSort {
390         bool operator() (const LocationMarker& a, const LocationMarker& b) {
391                 return (a.when < b.when);
392         }
393 };
394
395 void
396 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
397 {
398         // TODO cache, set_colors()
399         ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
400         ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
401
402         if (_n_labels == 0) {
403                 return;
404         }
405
406         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
407         ArdourCanvas::set_source_rgba(cr, base);
408         cairo_fill (cr);
409
410         Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
411         cairo_clip (cr);
412
413         if (_session == 0) {
414                 return;
415         }
416
417
418         /* time */
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;
421
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;
426
427                 // TODO round to nearest display TC in +/- 1px
428                 // prefer to display BBT |0  or .0
429
430                 int lw, lh;
431                 format_time (when);
432                 _layout->get_pixel_size (lw, lh);
433
434                 int x0 = xpos - lw / 2.0;
435                 int y0 = get_height() - 3 - _time_height;
436
437                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
438
439                 cairo_move_to (cr, x0, y0);
440                 ArdourCanvas::set_source_rgba(cr, text);
441                 pango_cairo_show_layout (cr, _layout->gobj());
442                 dot_left = x0 + lw;
443         }
444         draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
445
446         /* locations */
447         framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
448         framepos_t lmax = p + _time_span_samples;
449
450         int tw, th;
451         _layout->set_text (X_("Marker@"));
452         _layout->get_pixel_size (tw, th);
453
454         _marker_height = th + 2;
455         assert (_marker_height > 4);
456         const int mw = (_marker_height - 1) / 4;
457
458         lmin -= mw / _px_per_sample;
459         lmax += mw / _px_per_sample;
460
461         std::vector<LocationMarker> lm;
462
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));
469                         }
470                         when = (*l)->end ();
471                         if (when >= lmin && when <= lmax) {
472                                 lm.push_back (LocationMarker(_("end"), when));
473                         }
474                         continue;
475                 }
476
477                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
478                         continue;
479                 }
480
481                 framepos_t when = (*l)->start ();
482                 if (when < lmin || when > lmax) {
483                         continue;
484                 }
485                 lm.push_back (LocationMarker((*l)->name(), when));
486         }
487
488         _jumplist.clear ();
489
490         LocationMarkerSort location_marker_sort;
491         std::sort (lm.begin(), lm.end(), location_marker_sort);
492
493         int id = 0;
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;
501                 }
502                 bool prelight = false;
503                 x1 = draw_mark (cr, x0, x1, label, prelight);
504                 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
505         }
506
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 ());
513         cairo_stroke (cr);
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);
519         cairo_fill (cr);
520 }
521
522 void
523 MiniTimeline::build_minitl_context_menu ()
524 {
525         using namespace Gtk;
526         using namespace Gtk::Menu_Helpers;
527
528         assert (_session);
529
530         const framecnt_t time_span = _session->config.get_minitimeline_span ();
531
532         _minitl_context_menu = new Gtk::Menu();
533         MenuList& items = _minitl_context_menu->items();
534
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");
543
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 ();
549                 }
550         }
551 }
552
553 void
554 MiniTimeline::show_minitl_context_menu ()
555 {
556         if (_minitl_context_menu == 0) {
557                 build_minitl_context_menu ();
558         }
559         _minitl_context_menu->popup (1, gtk_get_current_event_time());
560 }
561
562 bool
563 MiniTimeline::on_button_press_event (GdkEventButton *ev)
564 {
565         if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
566                 if (_session) {
567                         show_minitl_context_menu ();
568                 }
569                 return true;
570         }
571         return true;
572 }
573
574 bool
575 MiniTimeline::on_button_release_event (GdkEventButton *ev)
576 {
577         if (!_session) { return true; }
578         if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
579                 return true;
580         }
581
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 ());
586                                 return true;
587                         }
588                 }
589         }
590
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 ());
594         }
595
596         return true;
597 }
598
599 bool
600 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
601 {
602         if (!_session) { return true; }
603
604         _pointer_x = ev->x;
605         _pointer_y = ev->y;
606
607         bool need_expose = false;
608
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) {
612                                 need_expose = true;
613                                 break;
614                         }
615                 } else {
616                         if ((*i).prelight) {
617                                 need_expose = true;
618                                 break;
619                         }
620                 }
621         }
622         if (need_expose) {
623                 update_minitimeline ();
624         }
625
626         return true;
627 }
628
629 bool
630 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
631 {
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) {
635                 if ((*i).prelight) {
636                         update_minitimeline ();
637                         break;
638                 }
639         }
640         return true;
641 }
642
643 bool
644 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
645 {
646         if (!_session) { return true; }
647         const framecnt_t time_span = _session->config.get_minitimeline_span ();
648         framepos_t when = _session->audible_frame ();
649
650         double scale = time_span / 60.0;
651
652         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
653                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
654                         scale = 0.1;
655                 } else {
656                         scale = 0.5;
657                 }
658         }
659
660         switch (ev->direction) {
661                 case GDK_SCROLL_UP:
662                         when += scale * _session->nominal_frame_rate ();
663                         break;
664                 case GDK_SCROLL_DOWN:
665                         when -= scale * _session->nominal_frame_rate ();
666                         break;
667                 default:
668                         return true;
669                         break;
670         }
671         _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
672         return true;
673 }