Factor out region layering.
[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
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 ARDOUR;
45 using namespace PBD;
46 using namespace Editing;
47
48 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* group)
49         : _trackview (tv)
50         , owns_canvas_group(group == 0)
51         , _background_group (new ArdourCanvas::Group (*_trackview.canvas_background()))
52         , canvas_group(group ? group : new ArdourCanvas::Group(*_trackview.canvas_display()))
53         , _samples_per_unit (_trackview.editor().get_current_zoom ())
54         , rec_updating(false)
55         , rec_active(false)
56         , use_rec_regions (tv.editor().show_waveforms_recording ())
57         , region_color(_trackview.color())
58         , stream_base_color(0xFFFFFFFF)
59         , _layers (1)
60         , height(tv.height)
61         , _layer_display (Overlaid)
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 (bind (mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
76
77         if (_trackview.is_track()) {
78                 _trackview.track()->DiskstreamChanged.connect (mem_fun (*this, &StreamView::diskstream_changed));
79                 _trackview.session().TransportStateChange.connect (mem_fun (*this, &StreamView::transport_changed));
80                 _trackview.session().TransportLooped.connect (mem_fun (*this, &StreamView::transport_looped));
81                 _trackview.get_diskstream()->RecordEnableChanged.connect (mem_fun (*this, &StreamView::rec_enable_changed));
82                 _trackview.session().RecordStateChanged.connect (mem_fun (*this, &StreamView::sess_rec_enable_changed));
83         } 
84
85         ColorsChanged.connect (mem_fun (*this, &StreamView::color_handler));
86 }
87
88 StreamView::~StreamView ()
89 {
90         undisplay_diskstream ();
91         
92         delete canvas_rect;
93         
94         if (owns_canvas_group) {
95                 delete canvas_group;
96         }
97 }
98
99 void
100 StreamView::attach ()
101 {
102         if (_trackview.is_track()) {
103                 display_diskstream (_trackview.get_diskstream());
104         }
105 }
106
107 int
108 StreamView::set_position (gdouble x, gdouble y)
109 {
110         canvas_group->property_x() = x;
111         canvas_group->property_y() = 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->property_y2() == h) {
124                 return 0;
125         }
126
127         height = h;
128         update_contents_height ();
129         return 0;
130 }
131
132 int 
133 StreamView::set_samples_per_unit (gdouble spp)
134 {
135         RegionViewList::iterator i;
136
137         if (spp < 1.0) {
138                 return -1;
139         }
140
141         _samples_per_unit = spp;
142
143         for (i = region_views.begin(); i != region_views.end(); ++i) {
144                 (*i)->set_samples_per_unit (spp);
145         }
146
147         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
148                 RecBoxInfo &recbox = (*xi);
149                 
150                 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
151                 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
152
153                 recbox.rectangle->property_x1() = xstart;
154                 recbox.rectangle->property_x2() = xend;
155         }
156
157         update_coverage_frames ();
158
159         return 0;
160 }
161
162 void
163 StreamView::add_region_view (boost::shared_ptr<Region> r)
164 {
165         // ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_region_view), r));
166
167         add_region_view_internal (r, true);
168         update_contents_height ();
169 }
170
171 void
172 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
173 {
174         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_region_view), weak_r));
175
176         boost::shared_ptr<Region> r (weak_r.lock());
177
178         if (!r) {
179                 return;
180         }
181
182         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
183                 if (((*i)->region()) == r) {
184                         RegionView* rv = *i;
185                         region_views.erase (i);
186                         delete rv;
187                         break;
188                 }
189         }
190 }
191
192 void
193 StreamView::undisplay_diskstream ()
194 {
195         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
196                 RegionViewList::iterator next = i;
197                 ++next;
198                 delete *i;
199                 i = next;
200         }
201
202         region_views.clear();
203 }
204
205 void
206 StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
207 {
208         playlist_change_connection.disconnect();
209         playlist_changed (ds);
210         playlist_change_connection = ds->PlaylistChanged.connect (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
211 }
212
213 void
214 StreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
215 {
216         boost::shared_ptr<Diskstream> sp (ds.lock());
217         if (!sp) {
218                 return;
219         }
220
221         playlist_modified (sp);
222 }
223
224 void
225 StreamView::layer_regions()
226 {
227         // In one traversal of the region view list:
228         // - Build a list of region views sorted by layer
229         // - Remove invalid views from the actual region view list
230         RegionViewList copy;
231         list<RegionView*>::iterator i, tmp;
232         for (i = region_views.begin(); i != region_views.end(); ) {
233                 tmp = i;
234                 tmp++;
235
236                 if (!(*i)->is_valid()) {
237                         delete *i;
238                         region_views.erase (i);
239                         i = tmp;
240                         continue;
241                 } else {
242                         (*i)->enable_display(true);
243                 }
244
245                 if (copy.size() == 0) {
246                         copy.push_front((*i));
247                         i = tmp;
248                         continue;
249                 }
250
251                 RegionViewList::iterator k = copy.begin();
252                 RegionViewList::iterator l = copy.end();
253                 l--;
254
255                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
256                         copy.push_front((*i));
257                         i = tmp;
258                         continue;
259                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
260                         copy.push_back((*i));
261                         i = tmp;
262                         continue;
263                 }
264
265                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
266                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
267                                 copy.insert(j, (*i));
268                                 break;
269                         }
270                 }
271
272                 i = tmp;
273         }
274
275         // Fix canvas layering by raising each in the sorted list order
276         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
277                 region_layered (*i);
278         }
279 }
280
281 void
282 StreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
283 {
284         /* we do not allow shared_ptr<T> to be bound to slots */
285         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_modified_weak), ds));
286
287         /* update layers count and the y positions and heights of our regions */
288         if (ds->playlist()) {
289                 _layers = ds->playlist()->top_layer() + 1;
290                 update_contents_height ();
291                 update_coverage_frames ();
292                 redisplay_diskstream ();
293         }
294 }
295
296 void
297 StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
298 {
299         /* XXX: binding to a shared_ptr, is this ok? */
300         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
301
302         /* disconnect from old playlist */
303
304         for (vector<sigc::connection>::iterator i = playlist_connections.begin(); i != playlist_connections.end(); ++i) {
305                 (*i).disconnect();
306         }
307         
308         playlist_connections.clear();
309         undisplay_diskstream ();
310
311         /* update layers count and the y positions and heights of our regions */
312         _layers = ds->playlist()->top_layer() + 1;
313         update_contents_height ();
314
315         update_coverage_frames ();
316         
317         /* draw it */
318         redisplay_diskstream ();
319
320         /* catch changes */
321
322         playlist_connections.push_back (ds->playlist()->Modified.connect (bind (mem_fun (*this, &StreamView::playlist_modified_weak), ds)));
323 }
324
325 void
326 StreamView::diskstream_changed ()
327 {
328         boost::shared_ptr<Track> t;
329
330         if ((t = _trackview.track()) != 0) {
331                 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun (*this, &StreamView::display_diskstream), t->diskstream()));
332         } else {
333                 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
334         }
335 }
336
337 void
338 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
339
340 {
341         list<RegionView *>::iterator i;
342
343         switch (target) {
344         case RegionColor:
345                 region_color = color;
346                 for (i = region_views.begin(); i != region_views.end(); ++i) {
347                         (*i)->set_color (region_color);
348                 }
349                 break;
350                 
351         case StreamBaseColor:
352                 stream_base_color = RGBA_TO_UINT (
353                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
354                 canvas_rect->property_fill_color_rgba() = stream_base_color;
355                 break;
356         }
357 }
358
359 void
360 StreamView::region_layered (RegionView* rv)
361 {
362         /* don't ever leave it at the bottom, since then it doesn't
363            get events - the  parent group does instead ...
364         */
365         rv->get_canvas_group()->raise (rv->region()->layer());
366 }
367
368 void
369 StreamView::rec_enable_changed ()
370 {
371         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
372 }
373
374 void
375 StreamView::sess_rec_enable_changed ()
376 {
377         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
378 }
379
380 void
381 StreamView::transport_changed()
382 {
383         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
384 }
385
386 void
387 StreamView::transport_looped()
388 {
389         // to force a new rec region
390         rec_active = false;
391         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
392 }
393
394 void
395 StreamView::update_rec_box ()
396 {
397         if (rec_active && rec_rects.size() > 0) {
398                 /* only update the last box */
399                 RecBoxInfo & rect = rec_rects.back();
400                 nframes_t at = _trackview.get_diskstream()->current_capture_end();
401                 double xstart;
402                 double xend;
403                 
404                 switch (_trackview.track()->mode()) {
405                 case Normal:
406                         rect.length = at - rect.start;
407                         xstart = _trackview.editor().frame_to_pixel (rect.start);
408                         xend = _trackview.editor().frame_to_pixel (at);
409                         break;
410                         
411                 case Destructive:
412                         rect.length = 2;
413                         xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
414                         xend = _trackview.editor().frame_to_pixel (at);
415                         break;
416                 }
417                 
418                 rect.rectangle->property_x1() = xstart;
419                 rect.rectangle->property_x2() = xend;
420         }
421 }
422         
423 RegionView*
424 StreamView::find_view (boost::shared_ptr<const Region> region)
425 {
426         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
427
428                 if ((*i)->region() == region) {
429                         return *i;
430                 }
431         }
432         return 0;
433 }
434         
435 void
436 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
437 {
438         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
439                 slot (*i);
440         }
441 }
442
443 void
444 StreamView::set_selected_regionviews (RegionSelection& regions)
445 {
446         bool selected;
447
448         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
449                 
450                 selected = false;
451                 
452                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
453                         if (*i == *ii) {
454                                 selected = true;
455                                 break;
456                         }
457                 }
458
459                 (*i)->set_selected (selected);
460         }
461 }
462
463 void
464 StreamView::get_selectables (nframes_t start, nframes_t end, list<Selectable*>& results)
465 {
466         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
467                 if ((*i)->region()->coverage(start, end) != OverlapNone) {
468                         results.push_back (*i);
469                 }
470         }
471 }
472
473 void
474 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
475 {
476         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
477                 if (!sel.regions.contains (*i)) {
478                         results.push_back (*i);
479                 }
480         }
481 }
482
483 /** @return height of a child region view, depending on stacked / overlaid mode */
484 double
485 StreamView::child_height () const
486 {
487         if (_layer_display == Stacked) {
488                 return height / _layers;
489         }
490         
491         return height;
492 }
493
494 void
495 StreamView::update_contents_height ()
496 {
497         canvas_rect->property_y2() = height;
498
499         const double h = child_height ();
500
501         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
502                 switch (_layer_display) {
503                 case Overlaid:
504                         (*i)->set_y (0);
505                         break;
506                 case Stacked:
507                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
508                         break;
509                 }
510
511                 (*i)->set_height (h);
512         }
513
514         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
515                 i->rectangle->property_y2() = height - 1.0;
516         }
517 }
518
519 void
520 StreamView::set_layer_display (LayerDisplay d)
521 {
522         _layer_display = d;
523         update_contents_height ();
524         update_coverage_frames ();
525 }
526
527 void
528 StreamView::update_coverage_frames ()
529 {
530         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
531                 (*i)->update_coverage_frames (_layer_display);
532         }
533 }