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