handle deletion of UI objects between the time that a callback is queued with the...
[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
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/source.h"
28 #include "ardour/diskstream.h"
29 #include "ardour/track.h"
30 #include "ardour/session.h"
31
32 #include "streamview.h"
33 #include "region_view.h"
34 #include "route_time_axis.h"
35 #include "canvas-waveview.h"
36 #include "canvas-simplerect.h"
37 #include "region_selection.h"
38 #include "selection.h"
39 #include "public_editor.h"
40 #include "ardour_ui.h"
41 #include "rgb_macros.h"
42 #include "gui_thread.h"
43 #include "utils.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47 using namespace PBD;
48 using namespace Editing;
49
50 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* group)
51         : _trackview (tv)
52         , owns_canvas_group(group == 0)
53         , _background_group (new ArdourCanvas::Group (*_trackview.canvas_background()))
54         , canvas_group(group ? group : new ArdourCanvas::Group(*_trackview.canvas_display()))
55         , _samples_per_unit (_trackview.editor().get_current_zoom ())
56         , rec_updating(false)
57         , rec_active(false)
58         , use_rec_regions (tv.editor().show_waveforms_recording ())
59         , region_color(_trackview.color())
60         , stream_base_color(0xFFFFFFFF)
61         , _layers (1)
62         , _layer_display (Overlaid)
63         , height(tv.height)
64         , last_rec_data_frame(0)
65 {
66         /* set_position() will position the group */
67
68         canvas_rect = new ArdourCanvas::SimpleRect (*_background_group);
69         canvas_rect->property_x1() = 0.0;
70         canvas_rect->property_y1() = 0.0;
71         canvas_rect->property_x2() = _trackview.editor().get_physical_screen_width ();
72         canvas_rect->property_y2() = (double) tv.current_height();
73         canvas_rect->raise(1); // raise above tempo lines
74
75         canvas_rect->property_outline_what() = (guint32) (0x2|0x8);  // outline RHS and bottom
76
77         canvas_rect->signal_event().connect (sigc::bind (
78                         sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
79                         canvas_rect, &_trackview));
80
81         if (_trackview.is_track()) {
82                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
83                 _trackview.get_diskstream()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
84
85                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
86                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
87                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
88         }
89
90         ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
91 }
92
93 StreamView::~StreamView ()
94 {
95         undisplay_diskstream ();
96
97         delete canvas_rect;
98
99         if (owns_canvas_group) {
100                 delete canvas_group;
101         }
102 }
103
104 void
105 StreamView::attach ()
106 {
107         if (_trackview.is_track()) {
108                 display_diskstream (_trackview.get_diskstream());
109         }
110 }
111
112 int
113 StreamView::set_position (gdouble x, gdouble y)
114 {
115         canvas_group->property_x() = x;
116         canvas_group->property_y() = y;
117         return 0;
118 }
119
120 int
121 StreamView::set_height (double h)
122 {
123         /* limit the values to something sane-ish */
124         if (h < 10.0 || h > 1000.0) {
125                 return -1;
126         }
127
128         if (canvas_rect->property_y2() == h) {
129                 return 0;
130         }
131
132         height = h;
133         canvas_rect->property_y2() = height;
134         update_contents_height ();
135
136         HeightChanged ();
137
138         return 0;
139 }
140
141 int
142 StreamView::set_samples_per_unit (gdouble spp)
143 {
144         RegionViewList::iterator i;
145
146         if (spp < 1.0) {
147                 return -1;
148         }
149
150         _samples_per_unit = spp;
151
152         for (i = region_views.begin(); i != region_views.end(); ++i) {
153                 (*i)->set_samples_per_unit (spp);
154         }
155
156         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
157                 RecBoxInfo &recbox = (*xi);
158
159                 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
160                 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
161
162                 recbox.rectangle->property_x1() = xstart;
163                 recbox.rectangle->property_x2() = xend;
164         }
165
166         update_coverage_frames ();
167
168         return 0;
169 }
170
171 void
172 StreamView::add_region_view (boost::weak_ptr<Region> wr)
173 {
174         boost::shared_ptr<Region> r (wr.lock());
175         if (!r) {
176                 return;
177         }
178
179         add_region_view_internal (r, true);
180
181         if (_layer_display == Stacked) {
182                 update_contents_height ();
183         }
184 }
185
186 void
187 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
188 {
189         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
190
191         boost::shared_ptr<Region> r (weak_r.lock());
192
193         if (!r) {
194                 return;
195         }
196
197         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
198                 if (((*i)->region()) == r) {
199                         RegionView* rv = *i;
200                         region_views.erase (i);
201                         delete rv;
202                         break;
203                 }
204         }
205 }
206
207 void
208 StreamView::undisplay_diskstream ()
209 {
210         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
211                 RegionViewList::iterator next = i;
212                 ++next;
213                 delete *i;
214                 i = next;
215         }
216
217         region_views.clear();
218 }
219
220 void
221 StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
222 {
223         playlist_switched_connection.disconnect();
224         playlist_switched (ds);
225         ds->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Diskstream> (ds)), gui_context());
226 }
227
228 void
229 StreamView::layer_regions()
230 {
231         // In one traversal of the region view list:
232         // - Build a list of region views sorted by layer
233         // - Remove invalid views from the actual region view list
234         RegionViewList copy;
235         list<RegionView*>::iterator i, tmp;
236         for (i = region_views.begin(); i != region_views.end(); ) {
237                 tmp = i;
238                 tmp++;
239
240                 if (!(*i)->is_valid()) {
241                         delete *i;
242                         region_views.erase (i);
243                         i = tmp;
244                         continue;
245                 } else {
246                         (*i)->enable_display(true);
247                 }
248
249                 if (copy.size() == 0) {
250                         copy.push_front((*i));
251                         i = tmp;
252                         continue;
253                 }
254
255                 RegionViewList::iterator k = copy.begin();
256                 RegionViewList::iterator l = copy.end();
257                 l--;
258
259                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
260                         copy.push_front((*i));
261                         i = tmp;
262                         continue;
263                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
264                         copy.push_back((*i));
265                         i = tmp;
266                         continue;
267                 }
268
269                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
270                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
271                                 copy.insert(j, (*i));
272                                 break;
273                         }
274                 }
275
276                 i = tmp;
277         }
278
279         // Fix canvas layering by raising each to the top in the sorted order.
280         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
281                 (*i)->get_canvas_group()->raise_to_top ();
282         }
283 }
284
285 void
286 StreamView::playlist_layered (boost::weak_ptr<Diskstream> wds)
287 {
288         boost::shared_ptr<Diskstream> ds (wds.lock());
289
290         if (!ds) {
291                 return;
292         }
293
294         /* update layers count and the y positions and heights of our regions */
295         if (ds->playlist()) {
296                 _layers = ds->playlist()->top_layer() + 1;
297         }
298
299         if (_layer_display == Stacked) {
300                 update_contents_height ();
301                 update_coverage_frames ();
302         } else {
303                 /* layering has probably been modified. reflect this in the canvas. */
304                 layer_regions();
305         } 
306 }
307
308 void
309 StreamView::playlist_switched (boost::weak_ptr<Diskstream> wds)
310 {
311         boost::shared_ptr<Diskstream> ds (wds.lock());
312
313         if (!ds) {
314                 return;
315         }
316
317         /* disconnect from old playlist */
318
319         playlist_connections.drop_connections ();
320         undisplay_diskstream ();
321
322         /* update layers count and the y positions and heights of our regions */
323         _layers = ds->playlist()->top_layer() + 1;
324         update_contents_height ();
325         update_coverage_frames ();
326
327         ds->playlist()->set_explicit_relayering (_layer_display == Stacked);
328
329         /* draw it */
330
331         redisplay_diskstream ();
332
333         /* catch changes */
334
335         ds->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Diskstream>(ds)), gui_context());
336         ds->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::add_region_view, this, _1), gui_context());
337         ds->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::remove_region_view, this, _1), gui_context());
338         // ds->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::redisplay_diskstream, this), gui_context());
339 }
340
341 void
342 StreamView::diskstream_changed ()
343 {
344         boost::shared_ptr<Track> t;
345         
346         if ((t = _trackview.track()) != 0) {
347                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_diskstream, this, t->diskstream()));
348         } else {
349                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_diskstream, this));
350         }
351 }
352
353 void
354 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
355
356 {
357         list<RegionView *>::iterator i;
358
359         switch (target) {
360         case RegionColor:
361                 region_color = color;
362                 for (i = region_views.begin(); i != region_views.end(); ++i) {
363                         (*i)->set_color (region_color);
364                 }
365                 break;
366
367         case StreamBaseColor:
368                 stream_base_color = RGBA_TO_UINT (
369                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
370                 canvas_rect->property_fill_color_rgba() = stream_base_color;
371                 break;
372         }
373 }
374
375 void
376 StreamView::region_layered (RegionView* rv)
377 {
378         /* don't ever leave it at the bottom, since then it doesn't
379            get events - the  parent group does instead ...
380         */
381         rv->get_canvas_group()->raise (rv->region()->layer());
382 }
383
384 void
385 StreamView::rec_enable_changed ()
386 {
387         setup_rec_box ();
388 }
389
390 void
391 StreamView::sess_rec_enable_changed ()
392 {
393         setup_rec_box ();
394 }
395
396 void
397 StreamView::transport_changed()
398 {
399         setup_rec_box ();
400 }
401
402 void
403 StreamView::transport_looped()
404 {
405         // to force a new rec region
406         rec_active = false;
407         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
408 }
409
410 void
411 StreamView::update_rec_box ()
412 {
413         if (rec_active && rec_rects.size() > 0) {
414                 /* only update the last box */
415                 RecBoxInfo & rect = rec_rects.back();
416                 nframes_t at = _trackview.get_diskstream()->current_capture_end();
417                 double xstart;
418                 double xend;
419
420                 switch (_trackview.track()->mode()) {
421
422                 case NonLayered:
423                 case Normal:
424                         rect.length = at - rect.start;
425                         xstart = _trackview.editor().frame_to_pixel (rect.start);
426                         xend = _trackview.editor().frame_to_pixel (at);
427                         break;
428
429                 case Destructive:
430                         rect.length = 2;
431                         xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
432                         xend = _trackview.editor().frame_to_pixel (at);
433                         break;
434                 }
435
436                 rect.rectangle->property_x1() = xstart;
437                 rect.rectangle->property_x2() = xend;
438         }
439 }
440
441 RegionView*
442 StreamView::find_view (boost::shared_ptr<const Region> region)
443 {
444         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
445
446                 if ((*i)->region() == region) {
447                         return *i;
448                 }
449         }
450         return 0;
451 }
452
453 uint32_t
454 StreamView::num_selected_regionviews () const
455 {
456         uint32_t cnt = 0;
457
458         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
459                 if ((*i)->get_selected()) {
460                         ++cnt;
461                 }
462         }
463         return cnt;
464 }
465
466 void
467 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
468 {
469         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
470                 slot (*i);
471         }
472 }
473
474 void
475 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
476 {
477         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
478                 if ((*i)->get_selected()) {
479                         slot (*i);
480                 }
481         }
482 }
483
484 void
485 StreamView::set_selected_regionviews (RegionSelection& regions)
486 {
487         bool selected;
488
489         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
490
491                 selected = false;
492
493                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
494                         if (*i == *ii) {
495                                 selected = true;
496                                 break;
497                         }
498                 }
499
500                 (*i)->set_selected (selected);
501         }
502 }
503
504 void
505 StreamView::get_selectables (nframes_t start, nframes_t end, double top, double bottom, list<Selectable*>& results)
506 {
507         layer_t min_layer = 0;
508         layer_t max_layer = 0;
509
510         if (_layer_display == Stacked) {
511                 double const c = child_height ();
512                 min_layer = _layers - ((bottom - _trackview.y_position()) / c);
513                 max_layer = _layers - ((top - _trackview.y_position()) / c);
514         }
515
516         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
517
518                 bool layer_ok = true;
519
520                 if (_layer_display == Stacked) {
521                         layer_t const l = (*i)->region()->layer ();
522                         layer_ok = (min_layer <= l && l <= max_layer);
523                 }
524
525                 if ((*i)->region()->coverage (start, end) != OverlapNone && layer_ok) {
526                         results.push_back (*i);
527                 }
528         }
529 }
530
531 void
532 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
533 {
534         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
535                 if (!sel.regions.contains (*i)) {
536                         results.push_back (*i);
537                 }
538         }
539 }
540
541 /** @return height of a child region view, depending on stacked / overlaid mode */
542 double
543 StreamView::child_height () const
544 {
545         if (_layer_display == Stacked) {
546                 return height / _layers;
547         }
548
549         return height;
550 }
551
552 void
553 StreamView::update_contents_height ()
554 {
555         const double h = child_height ();
556
557         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
558                 switch (_layer_display) {
559                 case Overlaid:
560                         (*i)->set_y (0);
561                         break;
562                 case Stacked:
563                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
564                         break;
565                 }
566
567                 (*i)->set_height (h);
568         }
569
570         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
571                 i->rectangle->property_y2() = height - 1.0;
572         }
573 }
574
575 void
576 StreamView::set_layer_display (LayerDisplay d)
577 {
578         _layer_display = d;
579         update_contents_height ();
580         update_coverage_frames ();
581         _trackview.get_diskstream()->playlist()->set_explicit_relayering (_layer_display == Stacked);
582 }
583
584 void
585 StreamView::update_coverage_frames ()
586 {
587         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
588                 (*i)->update_coverage_frames (_layer_display);
589         }
590 }