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