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