Drop references held by any GUI Lua script after execution
[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         cairo_push_group (cr);
513
514         const int width = get_width ();
515         const int height = get_height ();
516
517         Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, 4);
518         Gtkmm2ext::set_source_rgba(cr, base);
519         cairo_fill (cr);
520
521         Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, 4);
522         cairo_clip (cr);
523
524         if (_session == 0) {
525                 cairo_pop_group_to_source (cr);
526                 cairo_paint (cr);
527                 return;
528         }
529
530         /* time */
531         const samplepos_t p = _last_update_sample;
532         const samplepos_t lower = (std::max ((samplepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
533
534         int dot_left = width * .5 + (lower - p) * _px_per_sample;
535         for (int i = 0; i < 2 + _n_labels; ++i) {
536                 samplepos_t when = lower + i * _time_granularity;
537                 double xpos = width * .5 + (when - p) * _px_per_sample;
538
539                 // TODO round to nearest display TC in +/- 1px
540                 // prefer to display BBT |0  or .0
541
542                 int lw, lh;
543                 format_time (when);
544                 _layout->get_pixel_size (lw, lh);
545
546                 int x0 = xpos - lw / 2.0;
547                 int y0 = height - PADDING - _time_height;
548
549                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
550
551                 cairo_move_to (cr, x0, y0);
552                 Gtkmm2ext::set_source_rgba(cr, text);
553                 pango_cairo_show_layout (cr, _layout->gobj());
554                 dot_left = x0 + lw;
555         }
556         draw_dots (cr, dot_left, width, height - PADDING - _time_height * .5, text);
557
558         /* locations */
559         samplepos_t lmin = std::max ((samplepos_t)0, (p - _time_span_samples));
560         samplepos_t lmax = p + _time_span_samples;
561
562         int tw, th;
563         _layout->set_text (X_("Marker@"));
564         _layout->get_pixel_size (tw, th);
565
566         _marker_height = th + 2;
567         assert (_marker_height > 4);
568         const int mw = (_marker_height - 1) / 4;
569
570         lmin -= mw / _px_per_sample;
571         lmax += mw / _px_per_sample;
572
573         std::vector<LocationMarker> lm;
574
575         const Locations::LocationList& ll (_session->locations ()->list ());
576         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
577                 if ((*l)->is_session_range ()) {
578                         lm.push_back (LocationMarker(_("start"), (*l)->start ()));
579                         lm.push_back (LocationMarker(_("end"), (*l)->end ()));
580                         continue;
581                 }
582
583                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
584                         continue;
585                 }
586
587                 lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
588         }
589
590         _jumplist.clear ();
591
592         LocationMarkerSort location_marker_sort;
593         std::sort (lm.begin(), lm.end(), location_marker_sort);
594
595         std::vector<LocationMarker>::const_iterator outside_left = lm.end();
596         std::vector<LocationMarker>::const_iterator outside_right = lm.end();
597         int left_limit = 0;
598         int right_limit = width * .5 + mw;
599         int id = 0;
600
601         for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end(); ++id) {
602                 samplepos_t when = (*l).when;
603                 if (when < lmin) {
604                         outside_left = l;
605                         if (++l != lm.end()) {
606                                 left_limit = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
607                         } else {
608                                 left_limit = width * .5 - mw;
609                         }
610                         continue;
611                 }
612                 if (when > lmax) {
613                         outside_right = l;
614                         break;
615                 }
616                 int x0 = floor (width * .5 + (when - p) * _px_per_sample);
617                 int x1 = width;
618                 const std::string& label = (*l).label;
619                 if (++l != lm.end()) {
620                         x1 = floor (width * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
621                 }
622                 bool prelight = false;
623                 x1 = draw_mark (cr, x0, x1, label, prelight);
624                 _jumplist.push_back (JumpRange (x0 - mw, x1, when, prelight));
625                 right_limit = std::max (x1, right_limit);
626         }
627
628         if (outside_left != lm.end ()) {
629                 if (left_limit > 3 * mw + PADDING) {
630                         int x0 = PADDING + 1;
631                         int x1 = left_limit - mw;
632                         bool prelight = false;
633                         x1 = draw_edge (cr, x0, x1, true, (*outside_left).label, prelight);
634                         if (x0 != x1) {
635                                 _jumplist.push_back (JumpRange (x0, x1, (*outside_left).when, prelight));
636                                 right_limit = std::max (x1, right_limit);
637                         }
638                 }
639         }
640
641         if (outside_right != lm.end ()) {
642                 if (right_limit + PADDING < width - 3 * mw) {
643                         int x0 = right_limit;
644                         int x1 = width - PADDING;
645                         bool prelight = false;
646                         x0 = draw_edge (cr, x0, x1, false, (*outside_right).label, prelight);
647                         if (x0 != x1) {
648                                 _jumplist.push_back (JumpRange (x0, x1, (*outside_right).when, prelight));
649                         }
650                 }
651         }
652
653
654         /* playhead on top */
655         int xc = width * 0.5f;
656         cairo_set_line_width (cr, 1.0);
657         double r,g,b,a;  Gtkmm2ext::color_to_rgba(_phead_color, r,g,b,a);
658         cairo_set_source_rgb (cr, r,g,b); // playhead color
659         cairo_move_to (cr, xc - .5, 0);
660         cairo_rel_line_to (cr, 0, height);
661         cairo_stroke (cr);
662         cairo_move_to (cr, xc - .5, height);
663         cairo_rel_line_to (cr, -3,  0);
664         cairo_rel_line_to (cr,  3, -4);
665         cairo_rel_line_to (cr,  3,  4);
666         cairo_close_path (cr);
667         cairo_fill (cr);
668
669         cairo_pop_group_to_source (cr);
670         cairo_paint (cr);
671 }
672
673 void
674 MiniTimeline::build_minitl_context_menu ()
675 {
676         using namespace Gtk;
677         using namespace Gtk::Menu_Helpers;
678
679         assert (_session);
680
681         const samplecnt_t time_span = _session->config.get_minitimeline_span ();
682
683         _minitl_context_menu = new Gtk::Menu();
684         MenuList& items = _minitl_context_menu->items();
685
686         // ideally this would have a heading (or rather be a sub-menu to "Visible Time")
687         std::map<samplecnt_t, std::string> spans;
688         spans[30]   = _("30 sec");
689         spans[60]   = _("1 min");
690         spans[120]  = _("2 mins");
691         spans[300]  = _("5 mins");
692         spans[600]  = _("10 mins");
693         spans[1200] = _("20 mins");
694
695         RadioMenuItem::Group span_group;
696         for (std::map<samplecnt_t, std::string>::const_iterator i = spans.begin (); i != spans.end (); ++i) {
697                 items.push_back (RadioMenuElem (span_group, i->second, sigc::bind (sigc::mem_fun (*this, &MiniTimeline::set_span), i->first)));
698                 if (time_span == i->first) {
699                         static_cast<RadioMenuItem*>(&items.back())->set_active ();
700                 }
701         }
702 }
703
704 bool
705 MiniTimeline::on_button_press_event (GdkEventButton *ev)
706 {
707         if (Gtkmm2ext::Keyboard::is_context_menu_event (ev)) {
708                 if (_session) {
709                         if (_minitl_context_menu == 0) {
710                                 build_minitl_context_menu ();
711                         }
712                         _minitl_context_menu->popup (ev->button, ev->time);
713                 }
714                 return true;
715         }
716         return true;
717 }
718
719 bool
720 MiniTimeline::on_button_release_event (GdkEventButton *ev)
721 {
722         if (!_session) { return true; }
723         if (_session->actively_recording ()) { return true; }
724         if (ev->y < 0 || ev->y > get_height () || ev->x < 0 || ev->x > get_width ()) {
725                 return true;
726         }
727
728         if (ev->y <= PADDING + _marker_height) {
729                 for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
730                         if (i->left <= ev->x && ev->x <= i->right) {
731                                 _session->request_locate (i->to, _session->transport_rolling ());
732                                 return true;
733                         }
734                 }
735         }
736
737         if (ev->button == 1) {
738                 samplepos_t when = _last_update_sample + (ev->x - get_width() * .5) / _px_per_sample;
739                 _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
740         }
741
742         return true;
743 }
744
745 bool
746 MiniTimeline::on_motion_notify_event (GdkEventMotion *ev)
747 {
748         if (!_session) { return true; }
749         if (_session->actively_recording ()) { return true; }
750
751         _pointer_x = ev->x;
752         _pointer_y = ev->y;
753
754         bool need_expose = false;
755
756         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
757                 if (i->left < ev->x && ev->x < i->right && ev->y <= PADDING + _marker_height) {
758                         if (!(*i).prelight) {
759                                 need_expose = true;
760                                 break;
761                         }
762                 } else {
763                         if ((*i).prelight) {
764                                 need_expose = true;
765                                 break;
766                         }
767                 }
768         }
769         if (need_expose) {
770                 update_minitimeline ();
771         }
772
773         return true;
774 }
775
776 bool
777 MiniTimeline::on_leave_notify_event (GdkEventCrossing *ev)
778 {
779         CairoWidget::on_leave_notify_event (ev);
780         _pointer_x = _pointer_y = -1;
781         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
782                 if ((*i).prelight) {
783                         update_minitimeline ();
784                         break;
785                 }
786         }
787         return true;
788 }
789
790 bool
791 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
792 {
793         if (!_session) { return true; }
794         if (_session->actively_recording ()) { return true; }
795         const samplecnt_t time_span = _session->config.get_minitimeline_span ();
796         samplepos_t when = _session->audible_sample ();
797
798         double scale = time_span / 60.0;
799
800         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
801                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
802                         scale = 0.1;
803                 } else {
804                         scale = 0.5;
805                 }
806         }
807
808         switch (ev->direction) {
809                 case GDK_SCROLL_UP:
810                 case GDK_SCROLL_RIGHT:
811                         when += scale * _session->nominal_sample_rate ();
812                         break;
813                 case GDK_SCROLL_DOWN:
814                 case GDK_SCROLL_LEFT:
815                         when -= scale * _session->nominal_sample_rate ();
816                         break;
817                 default:
818                         return true;
819                         break;
820         }
821         _session->request_locate (std::max ((samplepos_t)0, when), _session->transport_rolling ());
822         return true;
823 }