substantial changes in color management, involving a reduction in the use of Gdk...
[ardour.git] / gtk2_ardour / streamview.cc
1 /*
2     Copyright (C) 2001, 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20
21 #include <gtkmm.h>
22
23 #include <gtkmm2ext/gtk_ui.h>
24 #include <gtkmm2ext/utils.h>
25
26 #include "ardour/playlist.h"
27 #include "ardour/region.h"
28 #include "ardour/track.h"
29 #include "ardour/session.h"
30
31 #include "pbd/compose.h"
32
33 #include "canvas/rectangle.h"
34 #include "canvas/debug.h"
35
36 #include "streamview.h"
37 #include "global_signals.h"
38 #include "region_view.h"
39 #include "route_time_axis.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
46 #include "utils.h"
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53 using namespace Editing;
54
55 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* canvas_group)
56         : _trackview (tv)
57         , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Group (_trackview.canvas_display()))
58         , _samples_per_pixel (_trackview.editor().get_current_zoom ())
59         , rec_updating(false)
60         , rec_active(false)
61         , stream_base_color(0xFFFFFFFF)
62         , _layers (1)
63         , _layer_display (Overlaid)
64         , height (tv.height)
65         , last_rec_data_frame(0)
66 {
67         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
68         
69         /* set_position() will position the group */
70
71         canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
72         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
73         canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
74         canvas_rect->set_outline_what (ArdourCanvas::Rectangle::BOTTOM);
75         canvas_rect->set_outline_color (RGBA_TO_UINT (0, 0, 0, 255));
76         canvas_rect->set_fill (true);
77         canvas_rect->Event.connect (sigc::bind (sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
78
79         if (_trackview.is_track()) {
80                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
81                 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
82
83                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
84                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
85                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
86         }
87
88         ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
89 }
90
91 StreamView::~StreamView ()
92 {
93         undisplay_track ();
94
95         delete canvas_rect;
96 }
97
98 void
99 StreamView::attach ()
100 {
101         if (_trackview.is_track()) {
102                 display_track (_trackview.track ());
103         }
104 }
105
106 int
107 StreamView::set_position (gdouble x, gdouble y)
108 {
109         _canvas_group->set_position (ArdourCanvas::Duple (x, y));
110         return 0;
111 }
112
113 int
114 StreamView::set_height (double h)
115 {
116         /* limit the values to something sane-ish */
117         if (h < 10.0 || h > 1000.0) {
118                 return -1;
119         }
120
121         if (canvas_rect->y1() == h) {
122                 return 0;
123         }
124
125         height = h;
126         canvas_rect->set_y1 (height);
127         update_contents_height ();
128
129         return 0;
130 }
131
132 int
133 StreamView::set_samples_per_pixel (double fpp)
134 {
135         RegionViewList::iterator i;
136
137         if (fpp < 1.0) {
138                 return -1;
139         }
140
141         _samples_per_pixel = fpp;
142
143         for (i = region_views.begin(); i != region_views.end(); ++i) {
144                 (*i)->set_samples_per_pixel (fpp);
145         }
146
147         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
148                 RecBoxInfo &recbox = (*xi);
149
150                 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
151                 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
152
153                 recbox.rectangle->set_x0 (xstart);
154                 recbox.rectangle->set_x1 (xend);
155         }
156
157         update_coverage_frames ();
158
159         return 0;
160 }
161
162 void
163 StreamView::add_region_view (boost::weak_ptr<Region> wr)
164 {
165         boost::shared_ptr<Region> r (wr.lock());
166         if (!r) {
167                 return;
168         }
169
170         add_region_view_internal (r, true);
171
172         if (_layer_display == Stacked || _layer_display == Expanded) {
173                 update_contents_height ();
174         }
175 }
176
177 void
178 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
179 {
180         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
181
182         boost::shared_ptr<Region> r (weak_r.lock());
183
184         if (!r) {
185                 return;
186         }
187
188         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
189                 if (((*i)->region()) == r) {
190                         RegionView* rv = *i;
191                         region_views.erase (i);
192                         delete rv;
193                         break;
194                 }
195         }
196
197         RegionViewRemoved (); /* EMIT SIGNAL */
198 }
199
200 void
201 StreamView::undisplay_track ()
202 {
203         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
204                 RegionViewList::iterator next = i;
205                 ++next;
206                 delete *i;
207                 i = next;
208         }
209
210         region_views.clear();
211 }
212
213 void
214 StreamView::display_track (boost::shared_ptr<Track> tr)
215 {
216         playlist_switched_connection.disconnect();
217         playlist_switched (tr);
218         tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
219 }
220
221 void
222 StreamView::layer_regions()
223 {
224         // In one traversal of the region view list:
225         // - Build a list of region views sorted by layer
226         // - Remove invalid views from the actual region view list
227         RegionViewList copy;
228         list<RegionView*>::iterator i, tmp;
229         for (i = region_views.begin(); i != region_views.end(); ) {
230                 tmp = i;
231                 tmp++;
232
233                 if (!(*i)->is_valid()) {
234                         delete *i;
235                         region_views.erase (i);
236                         i = tmp;
237                         continue;
238                 } else {
239                         (*i)->enable_display(true);
240                 }
241
242                 if (copy.size() == 0) {
243                         copy.push_front((*i));
244                         i = tmp;
245                         continue;
246                 }
247
248                 RegionViewList::iterator k = copy.begin();
249                 RegionViewList::iterator l = copy.end();
250                 l--;
251
252                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
253                         copy.push_front((*i));
254                         i = tmp;
255                         continue;
256                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
257                         copy.push_back((*i));
258                         i = tmp;
259                         continue;
260                 }
261
262                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
263                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
264                                 copy.insert(j, (*i));
265                                 break;
266                         }
267                 }
268
269                 i = tmp;
270         }
271
272         // Fix canvas layering by raising each to the top in the sorted order.
273         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
274                 (*i)->get_canvas_group()->raise_to_top ();
275         }
276 }
277
278 void
279 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
280 {
281         boost::shared_ptr<Track> tr (wtr.lock());
282
283         if (!tr) {
284                 return;
285         }
286
287         /* update layers count and the y positions and heights of our regions */
288         if (tr->playlist()) {
289                 _layers = tr->playlist()->top_layer() + 1;
290         }
291
292         if (_layer_display == Stacked) {
293                 update_contents_height ();
294                 update_coverage_frames ();
295         } else {
296                 /* layering has probably been modified. reflect this in the canvas. */
297                 layer_regions();
298         }
299 }
300
301 void
302 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
303 {
304         boost::shared_ptr<Track> tr (wtr.lock());
305
306         if (!tr) {
307                 return;
308         }
309
310         /* disconnect from old playlist */
311
312         playlist_connections.drop_connections ();
313         undisplay_track ();
314
315         /* draw it */
316
317         redisplay_track ();
318
319         /* update layers count and the y positions and heights of our regions */
320         _layers = tr->playlist()->top_layer() + 1;
321         update_contents_height ();
322         update_coverage_frames ();
323
324         /* catch changes */
325
326         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
327         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
328         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
329         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
330 }
331
332
333 void
334 StreamView::diskstream_changed ()
335 {
336         boost::shared_ptr<Track> t;
337
338         if ((t = _trackview.track()) != 0) {
339                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
340         } else {
341                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
342         }
343 }
344
345 void
346 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
347 {
348         return apply_color (gdk_color_to_rgba (c), target);
349 }
350
351 void
352 StreamView::apply_color (uint32_t color, ColorTarget target)
353 {
354         list<RegionView *>::iterator i;
355
356         switch (target) {
357         case RegionColor:
358                 region_color = color;
359                 for (i = region_views.begin(); i != region_views.end(); ++i) {
360                         (*i)->set_color (region_color);
361                 }
362                 break;
363
364         case StreamBaseColor:
365                 stream_base_color = color;
366                 canvas_rect->set_fill_color (stream_base_color);
367                 break;
368         }
369 }
370
371 void
372 StreamView::region_layered (RegionView* rv)
373 {
374         /* don't ever leave it at the bottom, since then it doesn't
375            get events - the  parent group does instead ...
376         */
377         rv->get_canvas_group()->raise (rv->region()->layer());
378 }
379
380 void
381 StreamView::rec_enable_changed ()
382 {
383         setup_rec_box ();
384 }
385
386 void
387 StreamView::sess_rec_enable_changed ()
388 {
389         setup_rec_box ();
390 }
391
392 void
393 StreamView::transport_changed()
394 {
395         setup_rec_box ();
396 }
397
398 void
399 StreamView::transport_looped()
400 {
401         // to force a new rec region
402         rec_active = false;
403         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
404 }
405
406 void
407 StreamView::update_rec_box ()
408 {
409         if (rec_active && rec_rects.size() > 0) {
410                 /* only update the last box */
411                 RecBoxInfo & rect = rec_rects.back();
412                 framepos_t const at = _trackview.track()->current_capture_end ();
413                 double xstart;
414                 double xend;
415
416                 switch (_trackview.track()->mode()) {
417
418                 case NonLayered:
419                 case Normal:
420                         rect.length = at - rect.start;
421                         xstart = _trackview.editor().sample_to_pixel (rect.start);
422                         xend = _trackview.editor().sample_to_pixel (at);
423                         break;
424
425                 case Destructive:
426                         rect.length = 2;
427                         xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
428                         xend = _trackview.editor().sample_to_pixel (at);
429                         break;
430
431                 default:
432                         fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
433                         /*NOTREACHED*/
434                         return;
435                 }
436
437                 rect.rectangle->set_x0 (xstart);
438                 rect.rectangle->set_x1 (xend);
439         }
440 }
441
442 RegionView*
443 StreamView::find_view (boost::shared_ptr<const Region> region)
444 {
445         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
446
447                 if ((*i)->region() == region) {
448                         return *i;
449                 }
450         }
451         return 0;
452 }
453
454 uint32_t
455 StreamView::num_selected_regionviews () const
456 {
457         uint32_t cnt = 0;
458
459         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
460                 if ((*i)->get_selected()) {
461                         ++cnt;
462                 }
463         }
464         return cnt;
465 }
466
467 void
468 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
469 {
470         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
471                 slot (*i);
472         }
473 }
474
475 void
476 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
477 {
478         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
479                 if ((*i)->get_selected()) {
480                         slot (*i);
481                 }
482         }
483 }
484
485 void
486 StreamView::set_selected_regionviews (RegionSelection& regions)
487 {
488         bool selected;
489
490         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
491
492                 selected = false;
493
494                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
495                         if (*i == *ii) {
496                                 selected = true;
497                                 break;
498                         }
499                 }
500
501                 (*i)->set_selected (selected);
502         }
503 }
504
505
506 /** Get selectable things within a given range.
507  *  @param start Start time in session frames.
508  *  @param end End time in session frames.
509  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
510  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
511  *  @param result Filled in with selectable things.
512  */
513
514 void
515 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
516 {
517         layer_t min_layer = 0;
518         layer_t max_layer = 0;
519
520         if (_layer_display == Stacked) {
521                 double const c = child_height ();
522
523                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
524                 if (mi < 0) {
525                         min_layer = 0;
526                 } else {
527                         min_layer = mi;
528                 }
529
530                 int const ma = _layers - ((top - _trackview.y_position()) / c);
531                 if (ma > (int) _layers) {
532                         max_layer = _layers - 1;
533                 } else {
534                         max_layer = ma;
535                 }
536
537         }
538
539         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
540
541                 bool layer_ok = true;
542
543                 if (_layer_display == Stacked) {
544                         layer_t const l = (*i)->region()->layer ();
545                         layer_ok = (min_layer <= l && l <= max_layer);
546                 }
547
548                 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
549                         results.push_back (*i);
550                 }
551         }
552 }
553
554 void
555 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
556 {
557         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
558                 if (!sel.regions.contains (*i)) {
559                         results.push_back (*i);
560                 }
561         }
562 }
563
564 /** @return height of a child region view, depending on stacked / overlaid mode */
565 double
566 StreamView::child_height () const
567 {
568         switch (_layer_display) {
569         case Overlaid:
570                 return height;
571         case Stacked:
572                 return height / _layers;
573         case Expanded:
574                 return height / (_layers * 2 + 1);
575         }
576         
577         /* NOTREACHED */
578         return height;
579 }
580
581 void
582 StreamView::update_contents_height ()
583 {
584         const double h = child_height ();
585
586         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
587                 switch (_layer_display) {
588                 case Overlaid:
589                         (*i)->set_y (0);
590                         break;
591                 case Stacked:
592                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
593                         break;
594                 case Expanded:
595                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
596                         break;
597                 }
598
599                 (*i)->set_height (h);
600         }
601
602         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
603                 switch (_layer_display) {
604                 case Overlaid:
605                         i->rectangle->set_y1 (height);
606                         break;
607                 case Stacked:
608                 case Expanded:
609                         /* In stacked displays, the recregion is always at the top */
610                         i->rectangle->set_y0 (0);
611                         i->rectangle->set_y1 (h);
612                         break;
613                 }
614         }
615
616         ContentsHeightChanged (); /* EMIT SIGNAL */
617 }
618
619 void
620 StreamView::set_layer_display (LayerDisplay d)
621 {
622         _layer_display = d;
623
624         if (_layer_display == Overlaid) {
625                 layer_regions ();
626         }
627         
628         update_contents_height ();
629         update_coverage_frames ();
630 }
631
632 void
633 StreamView::update_coverage_frames ()
634 {
635         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
636                 (*i)->update_coverage_frames (_layer_display);
637         }
638 }
639
640 void
641 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
642 {
643         if (_new_rec_layer_time < to) {
644                 /* The region being recorded has overlapped the start of a top-layered region, so
645                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
646                    as the proper layering will get sorted out when the recorded region is added to
647                    its playlist.
648                 */
649
650                 /* Stop this happening again */
651                 _new_rec_layer_time = max_framepos;
652
653                 /* Make space in the view for the new layer */
654                 ++_layers;
655
656                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
657                 region->set_layer (_layers - 1);
658
659                 /* and reset the view */
660                 update_contents_height ();
661         }
662 }
663
664 void
665 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
666 {
667         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
668            recorded region in.  To work out where this needs to happen, find the start of the next
669            top-layered region after the start of the region we are recording and make a note of it.
670         */
671         if (_layer_display == Stacked) {
672                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
673         } else {
674                 _new_rec_layer_time = max_framepos;
675         }
676 }
677
678 void
679 StreamView::enter_internal_edit_mode ()
680 {
681         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
682                 (*i)->hide_rect ();
683         }
684 }
685
686 void
687 StreamView::leave_internal_edit_mode ()
688 {
689         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
690                 (*i)->show_rect ();
691         }
692 }