Preferences/Config changes for image-surface settings
[ardour.git] / gtk2_ardour / mini_timeline.cc
1 /*
2  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2016-2018 Ben Loftis <ben@harrisonconsoles.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "ardour/audioengine.h"
21 #include "ardour/session.h"
22 #include "ardour/tempo.h"
23
24 #include "gtkmm2ext/colors.h"
25 #include "gtkmm2ext/gui_thread.h"
26 #include "gtkmm2ext/keyboard.h"
27
28 #include "widgets/tooltips.h"
29
30 #include "ardour_ui.h"
31 #include "public_editor.h"
32 #include "main_clock.h"
33 #include "mini_timeline.h"
34 #include "timers.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_sample (-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         ArdourWidgets::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         _phead_color = UIConfiguration::instance().color ("play head");
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 (samplecnt_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         samplepos_t const sample = 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_sample - sample) * _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_sample = sample;
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::Seconds:
248                 case AudioClock::Samples:
249                         _layout->set_text ("8888888888");
250                         break;
251         }
252         _layout->get_pixel_size (_time_width, _time_height);
253 }
254
255 void
256 MiniTimeline::calculate_time_spacing ()
257 {
258         _n_labels = floor (get_width () / (_time_width * 1.15));
259
260         if (_n_labels == 0 || !_session) {
261                 return;
262         }
263
264         const samplecnt_t time_span = _session->config.get_minitimeline_span () / 2;
265         _time_span_samples = time_span * _session->nominal_sample_rate ();
266         _time_granularity = _session->nominal_sample_rate () * ceil (2. * time_span / _n_labels);
267         _px_per_sample = get_width () / (2. * _time_span_samples);
268         //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
269 }
270
271 void
272 MiniTimeline::format_time (samplepos_t when)
273 {
274         switch (_clock_mode) {
275                 case AudioClock::Timecode:
276                         {
277                                 Timecode::Time TC;
278                                 _session->timecode_time (when, TC);
279                                 // chop of leading space or minus.
280                                 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
281                         }
282                         break;
283                 case AudioClock::BBT:
284                         {
285                                 char buf[64];
286                                 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_sample (when);
287                                 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
288                                                 BBT.bars, BBT.beats, BBT.ticks);
289                                 _layout->set_text (buf);
290                         }
291                         break;
292                 case AudioClock::MinSec:
293                         {
294                                 char buf[32];
295                                 AudioClock::print_minsec (when, buf, sizeof (buf), _session->sample_rate());
296                                 _layout->set_text (std::string(buf).substr(1));
297                         }
298                         break;
299                 case AudioClock::Seconds:
300                         {
301                                 char buf[32];
302                                 snprintf (buf, sizeof (buf), "%.1f", when / (float)_session->sample_rate());
303                                 _layout->set_text (buf);
304                         }
305                         break;
306                 case AudioClock::Samples:
307                         {
308                                 char buf[32];
309                                 snprintf (buf, sizeof (buf), "%" PRId64, when);
310                                 _layout->set_text (buf);
311                         }
312                         break;
313         }
314 }
315
316 void
317 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, Gtkmm2ext::Color color)
318 {
319         if (left + 1 >= right) {
320                 return;
321         }
322         cairo_move_to (cr, left + .5, y + .5);
323         cairo_line_to (cr, right - .5, y + .5);
324         Gtkmm2ext::set_source_rgb_a(cr, color, 0.3);
325         const double dashes[] = { 0, 1 };
326         cairo_set_dash (cr, dashes, 2, 1);
327         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
328         cairo_set_line_width (cr, 1.0);
329         cairo_stroke (cr);
330         cairo_set_dash (cr, 0, 0, 0);
331 }
332
333 int
334 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, const std::string& label, bool& prelight)
335 {
336         int h = _marker_height;
337         /* ArdourMarker shape
338          * MH = 13
339          *
340          * Mark:
341          *
342          *  (0,0)   --  (6,0)
343          *    |           |
344          *    |           |
345          * (0,MH*.4)  (6,MH*.4)
346          *     \         /
347          *        (3,MH)
348          */
349
350         const int y = PADDING;
351         int w2 = (h - 1) / 4;
352         double h0 = h * .4;
353         double h1 = h - h0;
354
355         int lw, lh;
356         _layout->set_text (label);
357         _layout->get_pixel_size (lw, lh);
358         int rw = std::min (x1, x0 + w2 + lw + 2);
359
360         if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 - w2 && _pointer_x <= rw) {
361                 prelight = true;
362         }
363
364         // TODO cache in set_colors()
365         uint32_t color = UIConfiguration::instance().color (
366                         prelight ? "entered marker" : "location marker");
367
368         double r, g, b, a;
369         Gtkmm2ext::color_to_rgba (color, r, g, b, a);
370
371         if (rw < x0) {
372                 rw = x1;
373         } else {
374                 cairo_save (cr);
375                 cairo_rectangle (cr, x0, y, rw - x0, h);
376                 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
377                 cairo_fill_preserve (cr);
378                 cairo_clip (cr);
379
380                 // marker label
381                 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
382                 cairo_set_source_rgb (cr, 0, 0, 0);
383                 pango_cairo_show_layout (cr, _layout->gobj());
384                 cairo_restore (cr);
385         }
386
387         // draw marker on top
388         cairo_move_to (cr, x0 - .5, y + .5);
389         cairo_rel_line_to (cr, -w2 , 0);
390         cairo_rel_line_to (cr, 0, h0);
391         cairo_rel_line_to (cr, w2, h1);
392         cairo_rel_line_to (cr, w2, -h1);
393         cairo_rel_line_to (cr, 0, -h0);
394         cairo_close_path (cr);
395         cairo_set_source_rgba (cr, r, g, b, 1.0);
396         cairo_set_line_width (cr, 1.0);
397         cairo_stroke_preserve (cr);
398         cairo_fill (cr);
399
400         return rw;
401 }
402
403 int
404 MiniTimeline::draw_edge (cairo_t* cr, int x0, int x1, bool left, const std::string& label, bool& prelight)
405 {
406         int h = _marker_height;
407         int w2 = (h - 1) / 4;
408
409         const int y = PADDING;
410         const double yc = rint (h * .5);
411         const double dy = h * .4;
412
413         bool with_label;
414         int lw, lh, lx;
415         _layout->set_text (label);
416         _layout->get_pixel_size (lw, lh);
417
418         double px, dx;
419         if (left) {
420                 if (x0 + 2 * w2 + lw + 2 < x1) {
421                         x1 = std::min (x1, x0 + 2 * w2 + lw + 2);
422                         with_label = true;
423                 } else {
424                         x1 = std::min (x1, x0 + 2 * w2);
425                         with_label = false;
426                 }
427                 px = x0;
428                 dx = 2 * w2;
429                 lx = x0 + dx;
430         } else {
431                 if (x1 - 2 * w2 - lw - 2 > x0) {
432                         x0 = std::max (x0, x1 - 2 * w2 - lw - 2);
433                         with_label = true;
434                 } else {
435                         x0 = std::max (x0, x1 - 2 * w2);
436                         with_label = false;
437                 }
438                 px = x1;
439                 dx = -2 * w2;
440                 lx = x1 + dx - lw - 2;
441         }
442
443         if (x1 - x0 < 2 * w2) {
444                 return left ? x0 : x1;
445         }
446
447         if (_pointer_y >= 0 && _pointer_y <= y + h && _pointer_x >= x0 && _pointer_x <= x1) {
448                 prelight = true;
449         }
450
451         // TODO cache in set_colors()
452         uint32_t color = UIConfiguration::instance().color (
453                         prelight ? "entered marker" : "location marker");
454
455         double r, g, b, a;
456         Gtkmm2ext::color_to_rgba (color, r, g, b, a);
457
458         if (with_label) {
459                 const int y = PADDING;
460                 cairo_save (cr);
461                 cairo_rectangle (cr, lx, y, lw + 2, h);
462                 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
463                 cairo_fill_preserve (cr);
464                 cairo_clip (cr);
465
466                 // marker label
467                 cairo_move_to (cr, lx + 1, y + .5 * (h - lh));
468                 cairo_set_source_rgb (cr, 0, 0, 0);
469                 pango_cairo_show_layout (cr, _layout->gobj());
470                 cairo_restore (cr);
471         }
472
473         // draw arrow
474         cairo_move_to (cr, px - .5, PADDING + yc - .5);
475         cairo_rel_line_to (cr, dx , dy);
476         cairo_rel_line_to (cr, 0, -2. * dy);
477         cairo_close_path (cr);
478         cairo_set_source_rgba (cr, r, g, b, 1.0);
479         cairo_set_line_width (cr, 1.0);
480         cairo_stroke_preserve (cr);
481         cairo_fill (cr);
482
483         return left ? x1 : x0;
484 }
485
486
487 struct LocationMarker {
488         LocationMarker (const std::string& l, samplepos_t w)
489                 : label (l), when (w) {}
490         std::string label;
491         samplepos_t  when;
492 };
493
494 struct LocationMarkerSort {
495         bool operator() (const LocationMarker& a, const LocationMarker& b) {
496                 return (a.when < b.when);
497         }
498 };
499
500 void
501 MiniTimeline::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
502 {
503         cairo_t* cr = ctx->cobj();
504         // TODO cache, set_colors()
505         Gtkmm2ext::Color base = UIConfiguration::instance().color ("ruler base");
506         Gtkmm2ext::Color text = UIConfiguration::instance().color ("ruler text");
507
508         if (_n_labels == 0) {
509                 return;
510         }
511
512         const int width = get_width ();
513         const int height = get_height ();
514
515         Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, 4);
516         Gtkmm2ext::set_source_rgba(cr, base);
517         cairo_fill (cr);
518
519         Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, 4);
520         cairo_clip (cr);
521
522         if (_session == 0) {
523                 return;
524         }
525
526         /* time */
527         const samplepos_t p = _last_update_sample;
528         const samplepos_t lower = (std::max ((samplepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
529
530         int dot_left = width * .5 + (lower - p) * _px_per_sample;
531         for (int i = 0; i < 2 + _n_labels; ++i) {
532                 samplepos_t when = lower + i * _time_granularity;
533                 double xpos = width * .5 + (when - p) * _px_per_sample;
534
535                 // TODO round to nearest display TC in +/- 1px
536                 // prefer to display BBT |0  or .0
537
538                 int lw, lh;
539                 format_time (when);
540                 _layout->get_pixel_size (lw, lh);
541
542                 int x0 = xpos - lw / 2.0;
543                 int y0 = height - PADDING - _time_height;
544
545                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
546
547                 cairo_move_to (cr, x0, y0);
548                 Gtkmm2ext::set_source_rgba(cr, text);
549                 pango_cairo_show_layout (cr, _layout->gobj());
550                 dot_left = x0 + lw;
551         }
552         draw_dots (cr, dot_left, width, height - PADDING - _time_height * .5, text);
553
554         /* locations */
555         samplepos_t lmin = std::max ((samplepos_t)0, (p - _time_span_samples));
556         samplepos_t lmax = p + _time_span_samples;
557
558         int tw, th;
559         _layout->set_text (X_("Marker@"));
560         _layout->get_pixel_size (tw, th);
561
562         _marker_height = th + 2;
563         assert (_marker_height > 4);
564         const int mw = (_marker_height - 1) / 4;
565
566         lmin -= mw / _px_per_sample;
567         lmax += mw / _px_per_sample;
568
569         std::vector<LocationMarker> lm;
570
571         const Locations::LocationList& ll (_session->locations ()->list ());
572         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
573                 if ((*l)->is_session_range ()) {
574                         lm.push_back (LocationMarker(_("start"), (*l)->start ()));
575                         lm.push_back (LocationMarker(_("end"), (*l)->end ()));
576                         continue;
577                 }
578
579                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
580                         continue;
581                 }
582
583                 lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
584         }
585
586         _jumplist.clear ();
587
588         LocationMarkerSort location_marker_sort;
589         std::sort (lm.begin(), lm.end(), location_marker_sort);
590
591         std::vector<LocationMarker>::const_iterator outside_left = lm.end();
592         std::vector<LocationMarker>::const_iterator outside_right = lm.end();
593         int left_limit = 0;
594         int right_limit = width * .5 + mw;
595         int id = 0;
596
597         for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
598                 samplepos_t when = (*l).when;
599                 if (when < lmin) {
600                         outside_left = l;
601                         if (++l != lm.end()) {
602                                 left_limit = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
603                         } else {
604                                 left_limit = width * .5 - mw;
605                         }
606                         continue;
607                 }
608                 if (when > lmax) {
609                         outside_right = l;
610                         break;
611                 }
612                 int x0 = floor (width * .5 + (when - p) * _px_per_sample);
613                 int x1 = width;
614                 const std::string& label = (*l).label;
615                 if (++l != lm.end()) {
616                         x1 = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
617                 }
618                 bool prelight = false;
619                 x1 = draw_mark (cr, x0, x1, label, prelight);
620                 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
621                 right_limit = std::max (x1, right_limit);
622         }
623
624         if (outside_left != lm.end ()) {
625                 if (left_limit > 3 * mw + PADDING) {
626                         int x0 = PADDING + 1;
627                         int x1 = left_limit - mw;
628                         bool prelight = false;
629                         x1 = draw_edge (cr, x0, x1, true, (*outside_left).label, prelight);
630                         if (x0 != x1) {
631                                 _jumplist.push_back (JumpRange (x0, x1, (*outside_left).when, prelight));
632                                 right_limit = std::max (x1, right_limit);
633                         }
634                 }
635         }
636
637         if (outside_right != lm.end ()) {
638                 if (right_limit + PADDING < width - 3 * mw) {
639                         int x0 = right_limit;
640                         int x1 = width - PADDING;
641                         bool prelight = false;
642                         x0 = draw_edge (cr, x0, x1, false, (*outside_right).label, prelight);
643                         if (x0 != x1) {
644                                 _jumplist.push_back (JumpRange (x0, x1, (*outside_right).when, prelight));
645                         }
646                 }
647         }
648
649
650         /* playhead on top */
651         int xc = width * 0.5f;
652         cairo_set_line_width (cr, 1.0);
653         double r,g,b,a;  Gtkmm2ext::color_to_rgba(_phead_color, r,g,b,a);
654         cairo_set_source_rgb (cr, r,g,b); // playhead color
655         cairo_move_to (cr, xc - .5, 0);
656         cairo_rel_line_to (cr, 0, height);
657         cairo_stroke (cr);
658         cairo_move_to (cr, xc - .5, height);
659         cairo_rel_line_to (cr, -3,  0);
660         cairo_rel_line_to (cr,  3, -4);
661         cairo_rel_line_to (cr,  3,  4);
662         cairo_close_path (cr);
663         cairo_fill (cr);
664 }
665
666 void
667 MiniTimeline::build_minitl_context_menu ()
668 {
669         using namespace Gtk;
670         using namespace Gtk::Menu_Helpers;
671
672         assert (_session);
673
674         const samplecnt_t time_span = _session->config.get_minitimeline_span ();
675
676         _minitl_context_menu = new Gtk::Menu();
677         MenuList& items = _minitl_context_menu->items();
678
679         // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
680         std::map<samplecnt_t, std::string> spans;
681         spans[30]   = _("30 sec");
682         spans[60]   = _("1 min");
683         spans[120]  = _("2 mins");
684         spans[300]  = _("5 mins");
685         spans[600]  = _("10 mins");
686         spans[1200] = _("20 mins");
687
688         RadioMenuItem::Group span_group;
689         for (std::map<samplecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
690                 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
691                 if (time_span == i->first) {
692                         static_cast<RadioMenuItem*>(&items.back())->set_active ();
693                 }
694         }
695 }
696
697 bool
698 MiniTimeline::on_button_press_event (GdkEventButton *ev)
699 {
700         if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
701                 if (_session) {
702                         if (_minitl_context_menu == 0) {
703                                 build_minitl_context_menu ();
704                         }
705                         _minitl_context_menu->popup (ev->button, ev->time);
706                 }
707                 return true;
708         }
709         return true;
710 }
711
712 bool
713 MiniTimeline::on_button_release_event (GdkEventButton *ev)
714 {
715         if (!_session) { return true; }
716         if (_session->actively_recording ()) { return true; }
717         if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
718                 return true;
719         }
720
721         if (ev->y <= PADDING + _marker_height) {
722                 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
723                         if (i->left <= ev->x && ev->x <= i->right) {
724                                 _session->request_locate (i->to, _session->transport_rolling ());
725                                 return true;
726                         }
727                 }
728         }
729
730         if (ev->button == 1) {
731                 samplepos_t when = _last_update_sample + (ev->x - get_width() * .5) / _px_per_sample;
732                 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
733         }
734
735         return true;
736 }
737
738 bool
739 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
740 {
741         if (!_session) { return true; }
742         if (_session->actively_recording ()) { return true; }
743
744         _pointer_x = ev->x;
745         _pointer_y = ev->y;
746
747         bool need_expose = false;
748
749         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
750                 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
751                         if (!(*i).prelight) {
752                                 need_expose = true;
753                                 break;
754                         }
755                 } else {
756                         if ((*i).prelight) {
757                                 need_expose = true;
758                                 break;
759                         }
760                 }
761         }
762         if (need_expose) {
763                 update_minitimeline ();
764         }
765
766         return true;
767 }
768
769 bool
770 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
771 {
772         CairoWidget::on_leave_notify_event (ev);
773         _pointer_x = _pointer_y = -1;
774         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
775                 if ((*i).prelight) {
776                         update_minitimeline ();
777                         break;
778                 }
779         }
780         return true;
781 }
782
783 bool
784 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
785 {
786         if (!_session) { return true; }
787         if (_session->actively_recording ()) { return true; }
788         const samplecnt_t time_span = _session->config.get_minitimeline_span ();
789         samplepos_t when = _session->audible_sample ();
790
791         double scale = time_span / 60.0;
792
793         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
794                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
795                         scale = 0.1;
796                 } else {
797                         scale = 0.5;
798                 }
799         }
800
801         switch (ev->direction) {
802                 case GDK_SCROLL_UP:
803                 case GDK_SCROLL_RIGHT:
804                         when += scale * _session->nominal_sample_rate ();
805                         break;
806                 case GDK_SCROLL_DOWN:
807                 case GDK_SCROLL_LEFT:
808                         when -= scale * _session->nominal_sample_rate ();
809                         break;
810                 default:
811                         return true;
812                         break;
813         }
814         _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
815         return true;
816 }