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