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