fix mini-timeline marker-prelight
[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 PADDING 3
39 #define BBT_BAR_CHAR "|"
40
41 using namespace ARDOUR;
42
43 MiniTimeline::MiniTimeline ()
44         : _last_update_frame (-1)
45         , _clock_mode (AudioClock::Timecode)
46         , _time_width (0)
47         , _time_height (0)
48         , _n_labels (0)
49         , _px_per_sample (0)
50         , _time_granularity (0)
51         , _time_span_samples (0)
52         , _marker_height (0)
53         , _pointer_x (-1)
54         , _pointer_y (-1)
55         , _minitl_context_menu (0)
56 {
57         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
59         _layout = Pango::Layout::create (get_pango_context());
60
61         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
62         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
63         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
64
65         set_name ("minitimeline");
66
67         Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
68         Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
69         Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
70         Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
71
72 }
73
74 MiniTimeline::~MiniTimeline ()
75 {
76         delete _minitl_context_menu;
77         _minitl_context_menu = 0;
78 }
79
80 void
81 MiniTimeline::session_going_away ()
82 {
83         super_rapid_connection.disconnect ();
84         session_connection.drop_connections ();
85         SessionHandlePtr::session_going_away ();
86         _jumplist.clear ();
87         delete _minitl_context_menu;
88         _minitl_context_menu = 0;
89 }
90
91 void
92 MiniTimeline::set_session (Session* s)
93 {
94         SessionHandlePtr::set_session (s);
95         if (!s) {
96                 return;
97         }
98
99         assert (!super_rapid_connection.connected ());
100         super_rapid_connection = Timers::super_rapid_connect (
101                         sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
102                         );
103
104         _session->config.ParameterChanged.connect (session_connection,
105                         invalidator (*this),
106                         boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
107                         );
108         _session->locations()->added.connect (session_connection,
109                         invalidator (*this),
110                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
111                         );
112         _session->locations()->removed.connect (session_connection,
113                         invalidator (*this),
114                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
115                         );
116         _session->locations()->changed.connect (session_connection,
117                         invalidator (*this),
118                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
119                         );
120
121         _jumplist.clear ();
122         calculate_time_spacing ();
123         update_minitimeline ();
124 }
125
126 void
127 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
128 {
129         CairoWidget::on_style_changed (old_style);
130         set_colors ();
131         calculate_time_width ();
132 }
133
134 void
135 MiniTimeline::on_name_changed ()
136 {
137         set_colors ();
138         calculate_time_width ();
139
140         if (is_realized()) {
141                 queue_resize ();
142         }
143 }
144
145 void
146 MiniTimeline::set_colors ()
147 {
148         // TODO  UIConfiguration::instance().color & font
149 }
150
151 void
152 MiniTimeline::parameter_changed (std::string const& p)
153 {
154         if (p == "minitimeline-span") {
155                 calculate_time_spacing ();
156                 update_minitimeline ();
157         }
158 }
159
160 void
161 MiniTimeline::on_size_request (Gtk::Requisition* req)
162 {
163         req->width = req->height = 0;
164         CairoWidget::on_size_request (req);
165
166         req->width = std::max (req->width, 1);
167         req->height = std::max (req->height, 20);
168 }
169
170 void
171 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
172 {
173         CairoWidget::on_size_allocate (alloc);
174         calculate_time_spacing ();
175 }
176
177 void
178 MiniTimeline::set_span (framecnt_t ts)
179 {
180         assert (_session);
181         if (_session->config.get_minitimeline_span () == ts) {
182                 return;
183         }
184
185         _session->config.set_minitimeline_span (ts);
186         calculate_time_spacing ();
187         update_minitimeline ();
188 }
189
190 void
191 MiniTimeline::super_rapid_update ()
192 {
193         if (!_session || !_session->engine().running()) {
194                 return;
195         }
196         framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
197         AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
198
199         bool change = false;
200         if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
201                 change = true;
202         }
203
204         if (m != _clock_mode) {
205                 _clock_mode = m;
206                 calculate_time_width ();
207                 change = true;
208         }
209
210         if (_clock_mode == AudioClock::BBT) {
211                 // TODO check if tempo-map changed
212                 change = true;
213         }
214
215         if (change) {
216                 _last_update_frame = frame;
217                 update_minitimeline ();
218         }
219 }
220
221 void
222 MiniTimeline::update_minitimeline ()
223 {
224         CairoWidget::set_dirty ();
225 }
226
227 void
228 MiniTimeline::calculate_time_width ()
229 {
230         switch (_clock_mode) {
231                 case AudioClock::Timecode:
232                         _layout->set_text (" 88:88:88,888 ");
233                         break;
234                 case AudioClock::BBT:
235                         _layout->set_text ("888|88|8888");
236                         break;
237                 case AudioClock::MinSec:
238                         _layout->set_text ("88:88:88,88");
239                         break;
240                 case AudioClock::Frames:
241                         _layout->set_text ("8888888888");
242                         break;
243         }
244         _layout->get_pixel_size (_time_width, _time_height);
245 }
246
247 void
248 MiniTimeline::calculate_time_spacing ()
249 {
250         _n_labels = floor (get_width () / (_time_width * 1.15));
251
252         if (_n_labels == 0 || !_session) {
253                 return;
254         }
255
256         const framecnt_t time_span = _session->config.get_minitimeline_span () / 2;
257         _time_span_samples = time_span * _session->nominal_frame_rate ();
258         _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
259         _px_per_sample = get_width () / (2. * _time_span_samples);
260         //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
261 }
262
263 void
264 MiniTimeline::format_time (framepos_t when)
265 {
266         switch (_clock_mode) {
267                 case AudioClock::Timecode:
268                         {
269                                 Timecode::Time TC;
270                                 _session->timecode_time (when, TC);
271                                 // chop of leading space or minus.
272                                 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
273                         }
274                         break;
275                 case AudioClock::BBT:
276                         {
277                                 char buf[64];
278                                 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
279                                 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
280                                                 BBT.bars, BBT.beats, BBT.ticks);
281                                 _layout->set_text (buf);
282                         }
283                         break;
284                 case AudioClock::MinSec:
285                         {
286                                 char buf[32];
287                                 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
288                                 _layout->set_text (std::string(buf).substr(1));
289                         }
290                         break;
291                 case AudioClock::Frames:
292                         {
293                                 char buf[32];
294                                 snprintf (buf, sizeof (buf), "%" PRId64, when);
295                                 _layout->set_text (buf);
296                         }
297                         break;
298         }
299 }
300
301 void
302 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
303 {
304         if (left + 1 >= right) {
305                 return;
306         }
307         cairo_move_to (cr, left + .5, y + .5);
308         cairo_line_to (cr, right - .5, y + .5);
309         ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
310         const double dashes[] = { 0, 1 };
311         cairo_set_dash (cr, dashes, 2, 1);
312         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
313         cairo_set_line_width (cr, 1.0);
314         cairo_stroke (cr);
315         cairo_set_dash (cr, 0, 0, 0);
316 }
317
318 int
319 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
320 {
321         int h = _marker_height;
322         /* ArdourMarker shape
323          * MH = 13
324          *
325          * Mark:
326          *
327          *  (0,0)   --  (6,0)
328          *    |           |
329          *    |           |
330          * (0,MH*.4)  (6,MH*.4)
331          *     \         /
332          *        (3,MH)
333          */
334
335         const int y = PADDING;
336         int w2 = (h - 1) / 4;
337         double h0 = h * .4;
338         double h1 = h - h0;
339
340         int lw, lh;
341         _layout->set_text (label);
342         _layout->get_pixel_size (lw, lh);
343         int rw = std::min (x1, x0 + w2 + lw + 2);
344
345         if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= rw) {
346                 prelight = true;
347         }
348
349         // TODO cache in set_colors()
350         uint32_t color = UIConfiguration::instance().color (
351                         prelight ? "entered marker" : "location marker");
352
353         double r, g, b, a;
354         ArdourCanvas::color_to_rgba (color, r, g, b, a);
355
356         if (rw < x0) {
357                 rw = x1;
358         } else {
359                 cairo_save (cr);
360                 cairo_rectangle (cr, x0, y, rw - x0, h);
361                 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
362                 cairo_fill_preserve (cr);
363                 cairo_clip (cr);
364
365                 // marker label
366                 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
367                 cairo_set_source_rgb (cr, 0, 0, 0);
368                 pango_cairo_show_layout (cr, _layout->gobj());
369                 cairo_restore (cr);
370         }
371
372         // draw marker on top
373         cairo_move_to (cr, x0 - .5, y + .5);
374         cairo_rel_line_to (cr, -w2 , 0 );
375         cairo_rel_line_to (cr, 0, h0);
376         cairo_rel_line_to (cr, w2, h1);
377         cairo_rel_line_to (cr, w2, -h1);
378         cairo_rel_line_to (cr, 0, -h0);
379         cairo_close_path (cr);
380         cairo_set_source_rgba (cr, r, g, b, 1.0);
381         cairo_set_line_width (cr, 1.0);
382         cairo_stroke_preserve (cr);
383         cairo_fill (cr);
384
385         return rw;
386 }
387
388 struct LocationMarker {
389         LocationMarker (const std::string& l, framepos_t w)
390                 : label (l), when (w) {}
391         std::string label;
392         framepos_t  when;
393 };
394
395 struct LocationMarkerSort {
396         bool operator() (const LocationMarker& a, const LocationMarker& b) {
397                 return (a.when < b.when);
398         }
399 };
400
401 void
402 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
403 {
404         // TODO cache, set_colors()
405         ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
406         ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
407
408         if (_n_labels == 0) {
409                 return;
410         }
411
412         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
413         ArdourCanvas::set_source_rgba(cr, base);
414         cairo_fill (cr);
415
416         Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, get_width() - PADDING - PADDING, get_height() - PADDING - PADDING, 4);
417         cairo_clip (cr);
418
419         if (_session == 0) {
420                 return;
421         }
422
423
424         /* time */
425         const framepos_t p = _last_update_frame;
426         const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
427
428         int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
429         for (int i = 0; i < 2 + _n_labels; ++i) {
430                 framepos_t when = lower + i * _time_granularity;
431                 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
432
433                 // TODO round to nearest display TC in +/- 1px
434                 // prefer to display BBT |0  or .0
435
436                 int lw, lh;
437                 format_time (when);
438                 _layout->get_pixel_size (lw, lh);
439
440                 int x0 = xpos - lw / 2.0;
441                 int y0 = get_height() - PADDING - _time_height;
442
443                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
444
445                 cairo_move_to (cr, x0, y0);
446                 ArdourCanvas::set_source_rgba(cr, text);
447                 pango_cairo_show_layout (cr, _layout->gobj());
448                 dot_left = x0 + lw;
449         }
450         draw_dots (cr, dot_left, get_width(), get_height() - PADDING - _time_height * .5, text);
451
452         /* locations */
453         framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
454         framepos_t lmax = p + _time_span_samples;
455
456         int tw, th;
457         _layout->set_text (X_("Marker@"));
458         _layout->get_pixel_size (tw, th);
459
460         _marker_height = th + 2;
461         assert (_marker_height > 4);
462         const int mw = (_marker_height - 1) / 4;
463
464         lmin -= mw / _px_per_sample;
465         lmax += mw / _px_per_sample;
466
467         std::vector<LocationMarker> lm;
468
469         const Locations::LocationList& ll (_session->locations ()->list ());
470         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
471                 if ((*l)->is_session_range ()) {
472                         framepos_t when = (*l)->start ();
473                         if (when >= lmin && when <= lmax) {
474                                 lm.push_back (LocationMarker(_("start"), when));
475                         }
476                         when = (*l)->end ();
477                         if (when >= lmin && when <= lmax) {
478                                 lm.push_back (LocationMarker(_("end"), when));
479                         }
480                         continue;
481                 }
482
483                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
484                         continue;
485                 }
486
487                 framepos_t when = (*l)->start ();
488                 if (when < lmin || when > lmax) {
489                         continue;
490                 }
491                 lm.push_back (LocationMarker((*l)->name(), when));
492         }
493
494         _jumplist.clear ();
495
496         LocationMarkerSort location_marker_sort;
497         std::sort (lm.begin(), lm.end(), location_marker_sort);
498
499         int id = 0;
500         for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
501                 framepos_t when = (*l).when;
502                 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
503                 int x1 = get_width();
504                 const std::string& label = (*l).label;
505                 if (++l != lm.end()) {
506                         x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
507                 }
508                 bool prelight = false;
509                 x1 = draw_mark (cr, x0, x1, label, prelight);
510                 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
511         }
512
513         /* playhead on top */
514         int xc = get_width () * 0.5f;
515         cairo_set_line_width (cr, 1.0);
516         cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
517         cairo_move_to (cr, xc - .5, 0);
518         cairo_rel_line_to (cr, 0, get_height ());
519         cairo_stroke (cr);
520         cairo_move_to (cr, xc - .5, get_height ());
521         cairo_rel_line_to (cr, -3,  0);
522         cairo_rel_line_to (cr,  3, -4);
523         cairo_rel_line_to (cr,  3,  4);
524         cairo_close_path (cr);
525         cairo_fill (cr);
526 }
527
528 void
529 MiniTimeline::build_minitl_context_menu ()
530 {
531         using namespace Gtk;
532         using namespace Gtk::Menu_Helpers;
533
534         assert (_session);
535
536         const framecnt_t time_span = _session->config.get_minitimeline_span ();
537
538         _minitl_context_menu = new Gtk::Menu();
539         MenuList& items = _minitl_context_menu->items();
540
541         // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
542         std::map<framecnt_t, std::string> spans;
543         spans[30]   = _("30 sec");
544         spans[60]   = _("1 min");
545         spans[120]  = _("2 mins");
546         spans[300]  = _("5 mins");
547         spans[600]  = _("10 mins");
548         spans[1200] = _("20 mins");
549
550         RadioMenuItem::Group span_group;
551         for (std::map<framecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
552                 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
553                 if (time_span == i->first) {
554                         static_cast<RadioMenuItem*>(&items.back())->set_active ();
555                 }
556         }
557 }
558
559 void
560 MiniTimeline::show_minitl_context_menu ()
561 {
562         if (_minitl_context_menu == 0) {
563                 build_minitl_context_menu ();
564         }
565         _minitl_context_menu->popup (1, gtk_get_current_event_time());
566 }
567
568 bool
569 MiniTimeline::on_button_press_event (GdkEventButton *ev)
570 {
571         if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
572                 if (_session) {
573                         show_minitl_context_menu ();
574                 }
575                 return true;
576         }
577         return true;
578 }
579
580 bool
581 MiniTimeline::on_button_release_event (GdkEventButton *ev)
582 {
583         if (!_session) { return true; }
584         if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
585                 return true;
586         }
587
588         if (ev->y <= PADDING + _marker_height) {
589                 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
590                         if (i->left < ev->x && ev->x < i->right) {
591                                 _session->request_locate (i->to, _session->transport_rolling ());
592                                 return true;
593                         }
594                 }
595         }
596
597         if (ev->button == 1) {
598                 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
599                 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
600         }
601
602         return true;
603 }
604
605 bool
606 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
607 {
608         if (!_session) { return true; }
609
610         _pointer_x = ev->x;
611         _pointer_y = ev->y;
612
613         bool need_expose = false;
614
615         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
616                 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
617                         if (!(*i).prelight) {
618                                 need_expose = true;
619                                 break;
620                         }
621                 } else {
622                         if ((*i).prelight) {
623                                 need_expose = true;
624                                 break;
625                         }
626                 }
627         }
628         if (need_expose) {
629                 update_minitimeline ();
630         }
631
632         return true;
633 }
634
635 bool
636 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
637 {
638         CairoWidget::on_leave_notify_event (ev);
639         _pointer_x = _pointer_y = -1;
640         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
641                 if ((*i).prelight) {
642                         update_minitimeline ();
643                         break;
644                 }
645         }
646         return true;
647 }
648
649 bool
650 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
651 {
652         if (!_session) { return true; }
653         const framecnt_t time_span = _session->config.get_minitimeline_span ();
654         framepos_t when = _session->audible_frame ();
655
656         double scale = time_span / 60.0;
657
658         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
659                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
660                         scale = 0.1;
661                 } else {
662                         scale = 0.5;
663                 }
664         }
665
666         switch (ev->direction) {
667                 case GDK_SCROLL_UP:
668                         when += scale * _session->nominal_frame_rate ();
669                         break;
670                 case GDK_SCROLL_DOWN:
671                         when -= scale * _session->nominal_frame_rate ();
672                         break;
673                 default:
674                         return true;
675                         break;
676         }
677         _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
678         return true;
679 }