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