6425b84c27ab4c83ee140b0c3272916d15c3b316
[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 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         , use_rec_regions (tv.editor().show_waveforms_recording ())
58         , region_color(_trackview.color())
59         , stream_base_color(0xFFFFFFFF)
60         , _layers (1)
61         , _layer_display (Overlaid)
62         , height(tv.height)
63         , last_rec_data_frame(0)
64 {
65         /* set_position() will position the group */
66
67         canvas_rect = new ArdourCanvas::SimpleRect (*_background_group);
68         canvas_rect->property_x1() = 0.0;
69         canvas_rect->property_y1() = 0.0;
70         canvas_rect->property_x2() = _trackview.editor().get_physical_screen_width ();
71         canvas_rect->property_y2() = (double) tv.current_height();
72         canvas_rect->raise(1); // raise above tempo lines
73
74         canvas_rect->property_outline_what() = (guint32) (0x2|0x8);  // outline RHS and bottom 
75
76         canvas_rect->signal_event().connect (bind (
77                         mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
78                         canvas_rect, &_trackview));
79
80         if (_trackview.is_track()) {
81                 _trackview.track()->DiskstreamChanged.connect (
82                                 mem_fun (*this, &StreamView::diskstream_changed));
83                 _trackview.session().TransportStateChange.connect (
84                                 mem_fun (*this, &StreamView::transport_changed));
85                 _trackview.session().TransportLooped.connect (
86                                 mem_fun (*this, &StreamView::transport_looped));
87                 _trackview.get_diskstream()->RecordEnableChanged.connect (
88                                 mem_fun (*this, &StreamView::rec_enable_changed));
89                 _trackview.session().RecordStateChanged.connect (
90                                 mem_fun (*this, &StreamView::sess_rec_enable_changed));
91         } 
92
93         ColorsChanged.connect (mem_fun (*this, &StreamView::color_handler));
94 }
95
96 StreamView::~StreamView ()
97 {
98         undisplay_diskstream ();
99         
100         delete canvas_rect;
101         
102         if (owns_canvas_group) {
103                 delete canvas_group;
104         }
105 }
106
107 void
108 StreamView::attach ()
109 {
110         if (_trackview.is_track()) {
111                 display_diskstream (_trackview.get_diskstream());
112         }
113 }
114
115 int
116 StreamView::set_position (gdouble x, gdouble y)
117 {
118         canvas_group->property_x() = x;
119         canvas_group->property_y() = y;
120         return 0;
121 }
122
123 int
124 StreamView::set_height (double h)
125 {
126         /* limit the values to something sane-ish */
127         if (h < 10.0 || h > 1000.0) {
128                 return -1;
129         }
130
131         if (canvas_rect->property_y2() == h) {
132                 return 0;
133         }
134
135         height = h;
136         canvas_rect->property_y2() = height;
137         update_contents_height ();
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::shared_ptr<Region> r)
173 {
174         // ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_region_view), r));
175
176         add_region_view_internal (r, true);
177         if (_layer_display == Stacked) {
178                 update_contents_height ();
179         }
180 }
181
182 void
183 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
184 {
185         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_region_view), weak_r));
186
187         boost::shared_ptr<Region> r (weak_r.lock());
188
189         if (!r) {
190                 return;
191         }
192
193         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
194                 if (((*i)->region()) == r) {
195                         RegionView* rv = *i;
196                         region_views.erase (i);
197                         delete rv;
198                         break;
199                 }
200         }
201 }
202
203 void
204 StreamView::undisplay_diskstream ()
205 {
206         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
207                 RegionViewList::iterator next = i;
208                 ++next;
209                 delete *i;
210                 i = next;
211         }
212
213         region_views.clear();
214 }
215
216 void
217 StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
218 {
219         playlist_change_connection.disconnect();
220         playlist_changed (ds);
221         playlist_change_connection = ds->PlaylistChanged.connect (bind (
222                         mem_fun (*this, &StreamView::playlist_changed_weak),
223                         boost::weak_ptr<Diskstream> (ds)));
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 in the sorted list order
278         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
279                 region_layered (*i);
280         }
281 }
282
283 void
284 StreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
285 {
286         boost::shared_ptr<Diskstream> sp (ds.lock());
287         if (sp) {
288                 playlist_modified (sp);
289         }
290 }
291
292 void
293 StreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
294 {
295         /* we do not allow shared_ptr<T> to be bound to slots */
296         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_modified_weak), ds));
297
298         /* update layers count and the y positions and heights of our regions */
299         if (ds->playlist()) {
300                 _layers = ds->playlist()->top_layer() + 1;
301                 update_contents_height ();
302                 update_coverage_frames ();
303                 redisplay_diskstream ();
304         }
305 }
306
307 void
308 StreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
309 {
310         boost::shared_ptr<Diskstream> sp (ds.lock());
311         if (sp) {
312                 playlist_changed (sp);
313         }
314 }
315
316 void
317 StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
318 {
319         ENSURE_GUI_THREAD (bind (
320                         mem_fun (*this, &StreamView::playlist_changed_weak),
321                         boost::weak_ptr<Diskstream> (ds)));
322
323         /* disconnect from old playlist */
324
325         for (vector<sigc::connection>::iterator i = playlist_connections.begin();
326                         i != playlist_connections.end(); ++i) {
327                 (*i).disconnect();
328         }
329         
330         playlist_connections.clear();
331         undisplay_diskstream ();
332
333         /* update layers count and the y positions and heights of our regions */
334         _layers = ds->playlist()->top_layer() + 1;
335         update_contents_height ();
336
337         update_coverage_frames ();
338         
339         /* draw it */
340         redisplay_diskstream ();
341
342         /* catch changes */
343
344         playlist_connections.push_back (ds->playlist()->Modified.connect (bind (
345                         mem_fun (*this, &StreamView::playlist_modified_weak),
346                         ds)));
347 }
348
349 void
350 StreamView::diskstream_changed ()
351 {
352         boost::shared_ptr<Track> t;
353
354         if ((t = _trackview.track()) != 0) {
355                 Gtkmm2ext::UI::instance()->call_slot (bind (
356                                 mem_fun (*this, &StreamView::display_diskstream),
357                                 t->diskstream()));
358         } else {
359                 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
360         }
361 }
362
363 void
364 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
365
366 {
367         list<RegionView *>::iterator i;
368
369         switch (target) {
370         case RegionColor:
371                 region_color = color;
372                 for (i = region_views.begin(); i != region_views.end(); ++i) {
373                         (*i)->set_color (region_color);
374                 }
375                 break;
376                 
377         case StreamBaseColor:
378                 stream_base_color = RGBA_TO_UINT (
379                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
380                 canvas_rect->property_fill_color_rgba() = stream_base_color;
381                 break;
382         }
383 }
384
385 void
386 StreamView::region_layered (RegionView* rv)
387 {
388         /* don't ever leave it at the bottom, since then it doesn't
389            get events - the  parent group does instead ...
390         */
391         rv->get_canvas_group()->raise (rv->region()->layer());
392 }
393
394 void
395 StreamView::rec_enable_changed ()
396 {
397         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
398 }
399
400 void
401 StreamView::sess_rec_enable_changed ()
402 {
403         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
404 }
405
406 void
407 StreamView::transport_changed()
408 {
409         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
410 }
411
412 void
413 StreamView::transport_looped()
414 {
415         // to force a new rec region
416         rec_active = false;
417         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
418 }
419
420 void
421 StreamView::update_rec_box ()
422 {
423         if (rec_active && rec_rects.size() > 0) {
424                 /* only update the last box */
425                 RecBoxInfo & rect = rec_rects.back();
426                 nframes_t at = _trackview.get_diskstream()->current_capture_end();
427                 double xstart;
428                 double xend;
429                 
430                 switch (_trackview.track()->mode()) {
431                 
432                 case NonLayered:
433                 case Normal:
434                         rect.length = at - rect.start;
435                         xstart = _trackview.editor().frame_to_pixel (rect.start);
436                         xend = _trackview.editor().frame_to_pixel (at);
437                         break;
438                         
439                 case Destructive:
440                         rect.length = 2;
441                         xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
442                         xend = _trackview.editor().frame_to_pixel (at);
443                         break;
444                 }
445                 
446                 rect.rectangle->property_x1() = xstart;
447                 rect.rectangle->property_x2() = xend;
448         }
449 }
450         
451 RegionView*
452 StreamView::find_view (boost::shared_ptr<const Region> region)
453 {
454         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
455
456                 if ((*i)->region() == region) {
457                         return *i;
458                 }
459         }
460         return 0;
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::set_selected_regionviews (RegionSelection& regions)
473 {
474         bool selected;
475
476         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
477                 
478                 selected = false;
479                 
480                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
481                         if (*i == *ii) {
482                                 selected = true;
483                                 break;
484                         }
485                 }
486
487                 (*i)->set_selected (selected);
488         }
489 }
490
491 void
492 StreamView::get_selectables (nframes_t start, nframes_t end, list<Selectable*>& results)
493 {
494         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
495                 if ((*i)->region()->coverage(start, end) != OverlapNone) {
496                         results.push_back (*i);
497                 }
498         }
499 }
500
501 void
502 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
503 {
504         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
505                 if (!sel.regions.contains (*i)) {
506                         results.push_back (*i);
507                 }
508         }
509 }
510
511 /** @return height of a child region view, depending on stacked / overlaid mode */
512 double
513 StreamView::child_height () const
514 {
515         if (_layer_display == Stacked) {
516                 return height / _layers;
517         }
518         
519         return height;
520 }
521
522 void
523 StreamView::update_contents_height ()
524 {
525
526         const double h = child_height ();
527
528         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
529                 switch (_layer_display) {
530                 case Overlaid:
531                         (*i)->set_y (0);
532                         break;
533                 case Stacked:
534                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
535                         break;
536                 }
537
538                 (*i)->set_height (h);
539         }
540
541         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
542                 i->rectangle->property_y2() = height - 1.0;
543         }
544 }
545
546 void
547 StreamView::set_layer_display (LayerDisplay d)
548 {
549         _layer_display = d;
550         update_contents_height ();
551         update_coverage_frames ();
552 }
553
554 void
555 StreamView::update_coverage_frames ()
556 {
557         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
558                 (*i)->update_coverage_frames (_layer_display);
559         }
560 }