don't double-call methods via UICallback::call_slot() when they are already connected...
[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, boost::bind (&StreamView::diskstream_changed, this), gui_context());
83                 _trackview.get_diskstream()->RecordEnableChanged.connect (*this, boost::bind (&StreamView::rec_enable_changed, this), gui_context());
84
85                 _trackview.session()->TransportStateChange.connect (*this, boost::bind (&StreamView::transport_changed, this), gui_context());
86                 _trackview.session()->TransportLooped.connect (*this, boost::bind (&StreamView::transport_looped, this), gui_context());
87                 _trackview.session()->RecordStateChanged.connect (*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_weak (boost::weak_ptr<Region> r)
173 {
174         boost::shared_ptr<Region> sp (r.lock());
175
176         if (sp) {
177                 add_region_view (sp);
178         }
179 }
180
181
182 void
183 StreamView::add_region_view (boost::shared_ptr<Region> r)
184 {
185         ENSURE_GUI_THREAD (*this, &StreamView::add_region_view_weak, boost::weak_ptr<Region>(r));
186
187         add_region_view_internal (r, true);
188
189         if (_layer_display == Stacked) {
190                 update_contents_height ();
191         }
192 }
193
194 void
195 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
196 {
197         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
198
199         boost::shared_ptr<Region> r (weak_r.lock());
200
201         if (!r) {
202                 return;
203         }
204
205         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
206                 if (((*i)->region()) == r) {
207                         RegionView* rv = *i;
208                         region_views.erase (i);
209                         cerr << "Deleting RV for " << r->name() << " @ " << r << endl;
210                         delete rv;
211                         break;
212                 }
213         }
214 }
215
216 void
217 StreamView::undisplay_diskstream ()
218 {
219         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
220                 RegionViewList::iterator next = i;
221                 ++next;
222                 delete *i;
223                 i = next;
224         }
225
226         region_views.clear();
227 }
228
229 void
230 StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
231 {
232         playlist_change_connection.disconnect();
233         playlist_changed (ds);
234         ds->PlaylistChanged.connect (playlist_change_connection, boost::bind (&StreamView::playlist_changed_weak, this, boost::weak_ptr<Diskstream> (ds)), gui_context());
235 }
236
237 void
238 StreamView::layer_regions()
239 {
240         // In one traversal of the region view list:
241         // - Build a list of region views sorted by layer
242         // - Remove invalid views from the actual region view list
243         RegionViewList copy;
244         list<RegionView*>::iterator i, tmp;
245         for (i = region_views.begin(); i != region_views.end(); ) {
246                 tmp = i;
247                 tmp++;
248
249                 if (!(*i)->is_valid()) {
250                         delete *i;
251                         region_views.erase (i);
252                         i = tmp;
253                         continue;
254                 } else {
255                         (*i)->enable_display(true);
256                 }
257
258                 if (copy.size() == 0) {
259                         copy.push_front((*i));
260                         i = tmp;
261                         continue;
262                 }
263
264                 RegionViewList::iterator k = copy.begin();
265                 RegionViewList::iterator l = copy.end();
266                 l--;
267
268                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
269                         copy.push_front((*i));
270                         i = tmp;
271                         continue;
272                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
273                         copy.push_back((*i));
274                         i = tmp;
275                         continue;
276                 }
277
278                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
279                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
280                                 copy.insert(j, (*i));
281                                 break;
282                         }
283                 }
284
285                 i = tmp;
286         }
287
288         // Fix canvas layering by raising each to the top in the sorted order.
289         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
290                 (*i)->get_canvas_group()->raise_to_top ();
291         }
292 }
293
294 void
295 StreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
296 {
297         boost::shared_ptr<Diskstream> sp (ds.lock());
298
299         if (sp) {
300                 playlist_modified (sp);
301         }
302 }
303
304 void
305 StreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
306 {
307         /* we do not allow shared_ptr<T> to be bound to slots */
308         ENSURE_GUI_THREAD (*this, &StreamView::playlist_modified_weak, ds)
309
310         /* update layers count and the y positions and heights of our regions */
311         if (ds->playlist()) {
312                 _layers = ds->playlist()->top_layer() + 1;
313         }
314
315         if (_layer_display == Stacked) {
316                 update_contents_height ();
317                 update_coverage_frames ();
318         } else {
319                 /* layering has probably been modified. reflect this in the canvas. */
320                 layer_regions();
321         } 
322 }
323
324 void
325 StreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
326 {
327         boost::shared_ptr<Diskstream> sp (ds.lock());
328         if (sp) {
329                 playlist_changed (sp);
330         }
331 }
332
333 void
334 StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
335 {
336         ENSURE_GUI_THREAD (*this, &StreamView::playlist_changed_weak, boost::weak_ptr<Diskstream> (ds));
337
338         /* disconnect from old playlist */
339
340         playlist_connections.drop_connections ();
341         undisplay_diskstream ();
342
343         /* update layers count and the y positions and heights of our regions */
344         _layers = ds->playlist()->top_layer() + 1;
345         update_contents_height ();
346         update_coverage_frames ();
347
348         ds->playlist()->set_explicit_relayering (_layer_display == Stacked);
349
350         /* draw it */
351
352         redisplay_diskstream ();
353
354         /* catch changes */
355
356         ds->playlist()->Modified.connect (playlist_connections, boost::bind (&StreamView::playlist_modified_weak, this, ds), gui_context());
357         ds->playlist()->RegionAdded.connect (playlist_connections, ui_bind (&StreamView::add_region_view_weak, this, _1), gui_context());
358         ds->playlist()->RegionRemoved.connect (playlist_connections, ui_bind (&StreamView::remove_region_view, this, _1), gui_context());
359 }
360
361 void
362 StreamView::diskstream_changed ()
363 {
364         boost::shared_ptr<Track> t;
365
366         if ((t = _trackview.track()) != 0) {
367                 Gtkmm2ext::UI::instance()->call_slot (boost::bind (&StreamView::display_diskstream, this, t->diskstream()));
368         } else {
369                 Gtkmm2ext::UI::instance()->call_slot (boost::bind (&StreamView::undisplay_diskstream, this));
370         }
371 }
372
373 void
374 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
375
376 {
377         list<RegionView *>::iterator i;
378
379         switch (target) {
380         case RegionColor:
381                 region_color = color;
382                 for (i = region_views.begin(); i != region_views.end(); ++i) {
383                         (*i)->set_color (region_color);
384                 }
385                 break;
386
387         case StreamBaseColor:
388                 stream_base_color = RGBA_TO_UINT (
389                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
390                 canvas_rect->property_fill_color_rgba() = stream_base_color;
391                 break;
392         }
393 }
394
395 void
396 StreamView::region_layered (RegionView* rv)
397 {
398         /* don't ever leave it at the bottom, since then it doesn't
399            get events - the  parent group does instead ...
400         */
401         rv->get_canvas_group()->raise (rv->region()->layer());
402 }
403
404 void
405 StreamView::rec_enable_changed ()
406 {
407         setup_rec_box ();
408 }
409
410 void
411 StreamView::sess_rec_enable_changed ()
412 {
413         setup_rec_box ();
414 }
415
416 void
417 StreamView::transport_changed()
418 {
419         setup_rec_box ();
420 }
421
422 void
423 StreamView::transport_looped()
424 {
425         // to force a new rec region
426         rec_active = false;
427         Gtkmm2ext::UI::instance()->call_slot (boost::bind (&StreamView::setup_rec_box, this));
428 }
429
430 void
431 StreamView::update_rec_box ()
432 {
433         if (rec_active && rec_rects.size() > 0) {
434                 /* only update the last box */
435                 RecBoxInfo & rect = rec_rects.back();
436                 nframes_t at = _trackview.get_diskstream()->current_capture_end();
437                 double xstart;
438                 double xend;
439
440                 switch (_trackview.track()->mode()) {
441
442                 case NonLayered:
443                 case Normal:
444                         rect.length = at - rect.start;
445                         xstart = _trackview.editor().frame_to_pixel (rect.start);
446                         xend = _trackview.editor().frame_to_pixel (at);
447                         break;
448
449                 case Destructive:
450                         rect.length = 2;
451                         xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
452                         xend = _trackview.editor().frame_to_pixel (at);
453                         break;
454                 }
455
456                 rect.rectangle->property_x1() = xstart;
457                 rect.rectangle->property_x2() = xend;
458         }
459 }
460
461 RegionView*
462 StreamView::find_view (boost::shared_ptr<const Region> region)
463 {
464         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
465
466                 if ((*i)->region() == region) {
467                         return *i;
468                 }
469         }
470         return 0;
471 }
472
473 void
474 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
475 {
476         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
477                 slot (*i);
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.get_diskstream()->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 }