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