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