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