fix blank mini-timeline
[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/gui_thread.h"
24 #include "gtkmm2ext/keyboard.h"
25
26 #include "canvas/colors.h"
27 #include "canvas/utils.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 BBT_BAR_CHAR "|"
39
40 using namespace ARDOUR;
41
42 MiniTimeline::MiniTimeline ()
43         : _last_update_frame (-1)
44         , _clock_mode (AudioClock::Timecode)
45         , _time_width (0)
46         , _time_height (0)
47         , _n_labels (0)
48         , _px_per_sample (0)
49         , _time_granularity (0)
50         , _time_span_samples (0)
51 {
52         _layout = Pango::Layout::create (get_pango_context());
53
54         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &MiniTimeline::set_colors));
55         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
56         UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &MiniTimeline::on_name_changed));
57
58         set_name ("minitimeline");
59
60         Location::name_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
61         Location::end_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
62         Location::start_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
63         Location::flags_changed.connect (marker_connection, invalidator (*this), boost::bind (&MiniTimeline::update_minitimeline, this), gui_context ());
64
65 }
66
67 MiniTimeline::~MiniTimeline ()
68 {
69 }
70
71 void
72 MiniTimeline::session_going_away ()
73 {
74         super_rapid_connection.disconnect ();
75         session_connection.drop_connections ();
76         SessionHandlePtr::session_going_away ();
77         _jumplist.clear ();
78 }
79
80 void
81 MiniTimeline::set_session (Session* s)
82 {
83         SessionHandlePtr::set_session (s);
84         if (!s) {
85                 return;
86         }
87
88         assert (!super_rapid_connection.connected ());
89         super_rapid_connection = Timers::super_rapid_connect (
90                         sigc::mem_fun (*this, &MiniTimeline::super_rapid_update)
91                         );
92
93         _session->config.ParameterChanged.connect (session_connection,
94                         invalidator (*this),
95                         boost::bind (&MiniTimeline::parameter_changed, this, _1), gui_context()
96                         );
97         _session->locations()->added.connect (session_connection,
98                         invalidator (*this),
99                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
100                         );
101         _session->locations()->removed.connect (session_connection,
102                         invalidator (*this),
103                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
104                         );
105         _session->locations()->changed.connect (session_connection,
106                         invalidator (*this),
107                         boost::bind (&MiniTimeline::update_minitimeline, this), gui_context()
108                         );
109
110         _jumplist.clear ();
111         calculate_time_spacing ();
112         update_minitimeline ();
113 }
114
115 void
116 MiniTimeline::on_style_changed (const Glib::RefPtr<Gtk::Style>& old_style)
117 {
118         CairoWidget::on_style_changed (old_style);
119         set_colors ();
120         calculate_time_width ();
121 }
122
123 void
124 MiniTimeline::on_name_changed ()
125 {
126         set_colors ();
127         calculate_time_width ();
128
129         if (is_realized()) {
130                 queue_resize ();
131         }
132 }
133
134 void
135 MiniTimeline::set_colors ()
136 {
137         // TODO  UIConfiguration::instance().color & font
138 }
139
140 void
141 MiniTimeline::parameter_changed (std::string const& p)
142 {
143         if (p == "minitimeline-span") {
144                 calculate_time_spacing ();
145                 update_minitimeline ();
146         }
147 }
148
149 void
150 MiniTimeline::on_size_request (Gtk::Requisition* req)
151 {
152         req->width = req->height = 0;
153         CairoWidget::on_size_request (req);
154
155         req->width = std::max (req->width, 1);
156         req->height = std::max (req->height, 20);
157 }
158
159 void
160 MiniTimeline::on_size_allocate (Gtk::Allocation& alloc)
161 {
162         CairoWidget::on_size_allocate (alloc);
163         calculate_time_spacing ();
164 }
165
166 void
167 MiniTimeline::super_rapid_update ()
168 {
169         if (!_session || !_session->engine().running()) {
170                 return;
171         }
172         framepos_t const frame = PublicEditor::instance().playhead_cursor_sample ();
173         AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
174
175         bool change = false;
176         if (fabs ((_last_update_frame - frame) * _px_per_sample) >= 1.0) {
177                 change = true;
178         }
179
180         if (m != _clock_mode) {
181                 _clock_mode = m;
182                 calculate_time_width ();
183                 change = true;
184         }
185
186         if (change) {
187                 _last_update_frame = frame;
188                 update_minitimeline ();
189         }
190 }
191
192 void
193 MiniTimeline::update_minitimeline ()
194 {
195         CairoWidget::set_dirty ();
196 }
197
198 void
199 MiniTimeline::calculate_time_width ()
200 {
201         switch (_clock_mode) {
202                 case AudioClock::Timecode:
203                         _layout->set_text (" 88:88:88,888 ");
204                         break;
205                 case AudioClock::BBT:
206                         _layout->set_text ("888|88|8888");
207                         break;
208                 case AudioClock::MinSec:
209                         _layout->set_text ("88:88:88,88");
210                         break;
211                 case AudioClock::Frames:
212                         _layout->set_text ("8888888888");
213                         break;
214         }
215         _layout->get_pixel_size (_time_width, _time_height);
216 }
217
218 void
219 MiniTimeline::calculate_time_spacing ()
220 {
221         _n_labels = floor (get_width () / (_time_width * 1.15));
222
223         if (_n_labels == 0 || !_session) {
224                 return;
225         }
226
227         const framepos_t time_span = _session->config.get_minitimeline_span () / 2;
228         _time_span_samples = time_span * _session->nominal_frame_rate ();
229         _time_granularity = _session->nominal_frame_rate () * ceil (2. * time_span / _n_labels);
230         _px_per_sample = get_width () / (2. * _time_span_samples);
231         //_px_per_sample = 1.0 / round (1.0 / _px_per_sample);
232 }
233
234 void
235 MiniTimeline::format_time (framepos_t when)
236 {
237         switch (_clock_mode) {
238                 case AudioClock::Timecode:
239                         {
240                                 Timecode::Time TC;
241                                 _session->timecode_time (when, TC);
242                                 // chop of leading space or minus.
243                                 _layout->set_text (Timecode::timecode_format_time (TC).substr(1));
244                         }
245                         break;
246                 case AudioClock::BBT:
247                         {
248                                 char buf[64];
249                                 Timecode::BBT_Time BBT = _session->tempo_map().bbt_at_frame (when);
250                                 snprintf (buf, sizeof (buf), "%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
251                                                 BBT.bars, BBT.beats, BBT.ticks);
252                                 _layout->set_text (buf);
253                         }
254                         break;
255                 case AudioClock::MinSec:
256                         {
257                                 char buf[32];
258                                 AudioClock::print_minsec (when, buf, sizeof (buf), _session->frame_rate());
259                                 _layout->set_text (std::string(buf).substr(1));
260                         }
261                         break;
262                 case AudioClock::Frames:
263                         {
264                                 char buf[32];
265                                 snprintf (buf, sizeof (buf), "%" PRId64, when);
266                                 _layout->set_text (buf);
267                         }
268                         break;
269         }
270 }
271
272 void
273 MiniTimeline::draw_dots (cairo_t* cr, int left, int right, int y, ArdourCanvas::Color color)
274 {
275         if (left + 1 >= right) {
276                 return;
277         }
278         cairo_move_to (cr, left + .5, y + .5);
279         cairo_line_to (cr, right - .5, y + .5);
280         ArdourCanvas::set_source_rgb_a(cr, color, 0.3);
281         const double dashes[] = { 0, 1 };
282         cairo_set_dash (cr, dashes, 2, 1);
283         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
284         cairo_set_line_width (cr, 1.0);
285         cairo_stroke (cr);
286         cairo_set_dash (cr, 0, 0, 0);
287 }
288
289 int
290 MiniTimeline::draw_mark (cairo_t* cr, int x0, int x1, int h, const std::string& label)
291 {
292         /* ArdourMarker shape
293          * MH = 13
294          *
295          * Mark:
296          *
297          *  (0,0)   --  (6,0)
298          *    |           |
299          *    |           |
300          * (0,MH*.4)  (6,MH*.4)
301          *     \         /
302          *        (3,MH)
303          */
304
305         int y = 3;
306         int w2 = (h - 1) / 4;
307         double h0 = h * .4;
308         double h1 = h - h0;
309
310         int lw, lh;
311         _layout->set_text (label);
312         _layout->get_pixel_size (lw, lh);
313
314         // TODO cache, set_colors()
315         uint32_t color = UIConfiguration::instance().color ("location marker");
316         double r, g, b, a;
317         ArdourCanvas::color_to_rgba (color, r, g, b, a);
318
319         int rw = std::min (x1, x0 + w2 + lw + 2);
320         if (rw < x0) {
321                 rw = x1;
322         } else {
323                 cairo_save (cr);
324                 cairo_rectangle (cr, x0, y, rw - x0, h);
325                 cairo_set_source_rgba (cr, r, g, b, 0.5); // this should use a shaded color
326                 cairo_fill_preserve (cr);
327                 cairo_clip (cr);
328
329                 // marker label
330                 cairo_move_to (cr, x0 + w2, y + .5 * (h - lh));
331                 cairo_set_source_rgb (cr, 0, 0, 0);
332                 pango_cairo_show_layout (cr, _layout->gobj());
333                 cairo_restore (cr);
334         }
335
336         // draw marker on top
337         cairo_move_to (cr, x0 - .5, y + .5);
338         cairo_rel_line_to (cr, -w2 , 0 );
339         cairo_rel_line_to (cr, 0, h0);
340         cairo_rel_line_to (cr, w2, h1);
341         cairo_rel_line_to (cr, w2, -h1);
342         cairo_rel_line_to (cr, 0, -h0);
343         cairo_close_path (cr);
344         cairo_set_source_rgba (cr, r, g, b, 1.0);
345         cairo_set_line_width (cr, 1.0);
346         cairo_stroke_preserve (cr);
347         cairo_fill (cr);
348
349         return rw;
350 }
351
352 struct LocationMarker {
353         LocationMarker (const std::string& l, framepos_t w)
354                 : label (l), when (w) {}
355         std::string label;
356         framepos_t  when;
357 };
358
359 struct LocationMarkerSort {
360         bool operator() (const LocationMarker& a, const LocationMarker& b) {
361                 return (a.when < b.when);
362         }
363 };
364
365 void
366 MiniTimeline::render (cairo_t* cr, cairo_rectangle_t*)
367 {
368         // TODO cache, set_colors()
369         ArdourCanvas::Color base = UIConfiguration::instance().color ("ruler base");
370         ArdourCanvas::Color text = UIConfiguration::instance().color ("ruler text");
371
372         if (_n_labels == 0) {
373                 return;
374         }
375
376         Gtkmm2ext::rounded_rectangle (cr, 0, 0, get_width(), get_height(), 4);
377         ArdourCanvas::set_source_rgba(cr, base);
378         cairo_fill (cr);
379
380         Gtkmm2ext::rounded_rectangle (cr, 3, 3, get_width()-6, get_height()-6, 4);
381         cairo_clip (cr);
382
383         if (_session == 0) {
384                 return;
385         }
386
387
388         /* time */
389         const framepos_t p = _last_update_frame;
390         const framepos_t lower = (std::max ((framepos_t)0, (p - _time_span_samples)) / _time_granularity) * _time_granularity;
391
392         int dot_left = get_width() * .5 + (lower - p) * _px_per_sample;
393         for (int i = 0; i < 2 + _n_labels; ++i) {
394                 framepos_t when = lower + i * _time_granularity;
395                 double xpos = get_width() * .5 + (when - p) * _px_per_sample;
396
397                 // TODO round to nearest display TC in +/- 1px
398                 // prefer to display BBT |0  or .0
399
400                 int lw, lh;
401                 format_time (when);
402                 _layout->get_pixel_size (lw, lh);
403
404                 int x0 = xpos - lw / 2.0;
405                 int y0 = get_height() - 3 - _time_height;
406
407                 draw_dots (cr, dot_left, x0, y0 + _time_height * .5, text);
408
409                 cairo_move_to (cr, x0, y0);
410                 ArdourCanvas::set_source_rgba(cr, text);
411                 pango_cairo_show_layout (cr, _layout->gobj());
412                 dot_left = x0 + lw;
413         }
414         draw_dots (cr, dot_left, get_width(), get_height() - 3 - _time_height * .5, text);
415
416         /* locations */
417         framepos_t lmin = std::max ((framepos_t)0, (p - _time_span_samples));
418         framepos_t lmax = p + _time_span_samples;
419
420         int tw, th;
421         _layout->set_text (X_("Marker@"));
422         _layout->get_pixel_size (tw, th);
423
424         const int mh = th + 2;
425         assert (mh > 4);
426         const int mw = (mh - 1) / 4;
427
428         lmin -= mw / _px_per_sample;
429         lmax += mw / _px_per_sample;
430
431         std::vector<LocationMarker> lm;
432
433         const Locations::LocationList& ll (_session->locations ()->list ());
434         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
435                 if ((*l)->is_session_range ()) {
436                         framepos_t when = (*l)->start ();
437                         if (when >= lmin && when <= lmax) {
438                                 lm.push_back (LocationMarker(_("start"), when));
439                         }
440                         when = (*l)->end ();
441                         if (when >= lmin && when <= lmax) {
442                                 lm.push_back (LocationMarker(_("end"), when));
443                         }
444                         continue;
445                 }
446
447                 if (!(*l)->is_mark () || (*l)->name().substr (0, 4) == "xrun") {
448                         continue;
449                 }
450
451                 framepos_t when = (*l)->start ();
452                 if (when < lmin || when > lmax) {
453                         continue;
454                 }
455                 lm.push_back (LocationMarker((*l)->name(), when));
456         }
457
458         _jumplist.clear ();
459
460         LocationMarkerSort location_marker_sort;
461         std::sort (lm.begin(), lm.end(), location_marker_sort);
462
463         for (std::vector<LocationMarker>::const_iterator l = lm.begin(); l != lm.end();) {
464                 framepos_t when = (*l).when;
465                 int x0 = floor (get_width() * .5 + (when - p) * _px_per_sample);
466                 int x1 = get_width();
467                 const std::string& label = (*l).label;
468                 if (++l != lm.end()) {
469                         x1 = floor (get_width() * .5 + ((*l).when - p) * _px_per_sample) - 1 - mw;
470                 }
471                 x1 = draw_mark (cr, x0, x1, mh, label);
472                 _jumplist.push_back (JumpRange (x0 - mw, x1, when));
473         }
474
475         /* playhead on top */
476         int xc = get_width () * 0.5f;
477         cairo_set_line_width (cr, 1.0);
478         cairo_set_source_rgb (cr, 1, 0, 0); // playhead color
479         cairo_move_to (cr, xc - .5, 0);
480         cairo_rel_line_to (cr, 0, get_height ());
481         cairo_stroke (cr);
482         cairo_move_to (cr, xc - .5, get_height ());
483         cairo_rel_line_to (cr, -3,  0);
484         cairo_rel_line_to (cr,  3, -4);
485         cairo_rel_line_to (cr,  3,  4);
486         cairo_close_path (cr);
487         cairo_fill (cr);
488 }
489
490 bool
491 MiniTimeline::on_button_release_event (GdkEventButton *ev)
492 {
493         if (!_session) { return true; }
494
495         for (JumpList::const_iterator i = _jumplist.begin (); i != _jumplist.end(); ++i) {
496                 if (i->left < ev->x && ev->x < i->right) {
497                         if (ev->button == 3) {
498                                 PublicEditor::instance().center_screen (i->to);
499                         } else if (ev->button == 1) {
500                                 _session->request_locate (i->to, _session->transport_rolling ());
501                         }
502                         return true;
503                 }
504         }
505
506         if (ev->button == 1) {
507                 framepos_t when = _last_update_frame + (ev->x - get_width() * .5) / _px_per_sample;
508                 _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
509         }
510
511         return true;
512 }
513
514 bool
515 MiniTimeline::on_scroll_event (GdkEventScroll *ev)
516 {
517         if (!_session) { return true; }
518         framepos_t when = _session->audible_frame ();
519         double scale = 2.0;
520
521         if (ev->state & Gtkmm2ext::Keyboard::GainFineScaleModifier) {
522                 if (ev->state & Gtkmm2ext::Keyboard::GainExtraFineScaleModifier) {
523                         scale = 0.1;
524                 } else {
525                         scale = 0.5;
526                 }
527         }
528
529         switch (ev->direction) {
530                 case GDK_SCROLL_UP:
531                         when += scale * _session->nominal_frame_rate ();
532                         break;
533                 case GDK_SCROLL_DOWN:
534                         when -= scale * _session->nominal_frame_rate ();
535                         break;
536                 default:
537                         return true;
538                         break;
539         }
540         _session->request_locate (std::max ((framepos_t)0, when), _session->transport_rolling ());
541         return true;
542 }