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