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