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