Make drag selection work properly with stacked region mode.
[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
139         HeightChanged ();
140         
141         return 0;
142 }
143
144 int 
145 StreamView::set_samples_per_unit (gdouble spp)
146 {
147         RegionViewList::iterator i;
148
149         if (spp < 1.0) {
150                 return -1;
151         }
152
153         _samples_per_unit = spp;
154
155         for (i = region_views.begin(); i != region_views.end(); ++i) {
156                 (*i)->set_samples_per_unit (spp);
157         }
158
159         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
160                 RecBoxInfo &recbox = (*xi);
161                 
162                 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
163                 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
164
165                 recbox.rectangle->property_x1() = xstart;
166                 recbox.rectangle->property_x2() = xend;
167         }
168
169         update_coverage_frames ();
170
171         return 0;
172 }
173
174 void
175 StreamView::add_region_view_weak (boost::weak_ptr<Region> r)
176 {
177         boost::shared_ptr<Region> sp (r.lock());
178         
179         if (sp) {
180                 add_region_view (sp);
181         }
182 }
183
184
185 void
186 StreamView::add_region_view (boost::shared_ptr<Region> r)
187 {
188         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::add_region_view), r));
189         
190         add_region_view_internal (r, true);
191         
192         if (_layer_display == Stacked) {
193                 update_contents_height ();
194         }
195 }
196
197 void
198 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
199 {
200         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_region_view), weak_r));
201
202         boost::shared_ptr<Region> r (weak_r.lock());
203
204         if (!r) {
205                 return;
206         }
207
208         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
209                 if (((*i)->region()) == r) {
210                         RegionView* rv = *i;
211                         region_views.erase (i);
212                         delete rv;
213                         break;
214                 }
215         }
216 }
217
218 void
219 StreamView::undisplay_diskstream ()
220 {
221         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
222                 RegionViewList::iterator next = i;
223                 ++next;
224                 delete *i;
225                 i = next;
226         }
227
228         region_views.clear();
229 }
230
231 void
232 StreamView::display_diskstream (boost::shared_ptr<Diskstream> ds)
233 {
234         playlist_change_connection.disconnect();
235         playlist_changed (ds);
236         playlist_change_connection = ds->PlaylistChanged.connect (bind (
237                         mem_fun (*this, &StreamView::playlist_changed_weak),
238                         boost::weak_ptr<Diskstream> (ds)));
239 }
240
241 void
242 StreamView::layer_regions()
243 {
244         // In one traversal of the region view list:
245         // - Build a list of region views sorted by layer
246         // - Remove invalid views from the actual region view list
247         RegionViewList copy;
248         list<RegionView*>::iterator i, tmp;
249         for (i = region_views.begin(); i != region_views.end(); ) {
250                 tmp = i;
251                 tmp++;
252
253                 if (!(*i)->is_valid()) {
254                         delete *i;
255                         region_views.erase (i);
256                         i = tmp;
257                         continue;
258                 } else {
259                         (*i)->enable_display(true);
260                 }
261
262                 if (copy.size() == 0) {
263                         copy.push_front((*i));
264                         i = tmp;
265                         continue;
266                 }
267
268                 RegionViewList::iterator k = copy.begin();
269                 RegionViewList::iterator l = copy.end();
270                 l--;
271
272                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
273                         copy.push_front((*i));
274                         i = tmp;
275                         continue;
276                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
277                         copy.push_back((*i));
278                         i = tmp;
279                         continue;
280                 }
281
282                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
283                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
284                                 copy.insert(j, (*i));
285                                 break;
286                         }
287                 }
288
289                 i = tmp;
290         }
291
292         // Fix canvas layering by raising each in the sorted list order
293         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
294                 region_layered (*i);
295         }
296 }
297
298 void
299 StreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
300 {
301         boost::shared_ptr<Diskstream> sp (ds.lock());
302         
303         if (sp) {
304                 playlist_modified (sp);
305         }
306 }
307
308 void
309 StreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
310 {
311         /* we do not allow shared_ptr<T> to be bound to slots */
312         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_modified_weak), ds));
313
314         /* update layers count and the y positions and heights of our regions */
315         if (ds->playlist()) {
316                 _layers = ds->playlist()->top_layer() + 1;
317         }
318
319         if (_layer_display == Stacked) {
320                 update_contents_height ();
321                 update_coverage_frames ();
322         }
323 }
324
325 void
326 StreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
327 {
328         boost::shared_ptr<Diskstream> sp (ds.lock());
329         if (sp) {
330                 playlist_changed (sp);
331         }
332 }
333
334 void
335 StreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
336 {
337         ENSURE_GUI_THREAD (bind (
338                         mem_fun (*this, &StreamView::playlist_changed_weak),
339                         boost::weak_ptr<Diskstream> (ds)));
340
341         /* disconnect from old playlist */
342
343         for (vector<sigc::connection>::iterator i = playlist_connections.begin();
344                         i != playlist_connections.end(); ++i) {
345                 (*i).disconnect();
346         }
347         
348         playlist_connections.clear();
349         undisplay_diskstream ();
350
351         /* update layers count and the y positions and heights of our regions */
352         _layers = ds->playlist()->top_layer() + 1;
353         update_contents_height ();
354         update_coverage_frames ();
355
356         ds->playlist()->set_explicit_relayering (_layer_display == Stacked);
357         
358         /* draw it */
359         
360         redisplay_diskstream ();
361
362         /* catch changes */
363
364         playlist_connections.push_back (ds->playlist()->Modified.connect (bind (
365                         mem_fun (*this, &StreamView::playlist_modified_weak), ds)));
366
367         playlist_connections.push_back (ds->playlist()->RegionAdded.connect (
368                         mem_fun (*this, &StreamView::add_region_view_weak)));
369
370         playlist_connections.push_back (ds->playlist()->RegionRemoved.connect (
371                         mem_fun (*this, &StreamView::remove_region_view)));
372 }
373
374 void
375 StreamView::diskstream_changed ()
376 {
377         boost::shared_ptr<Track> t;
378
379         if ((t = _trackview.track()) != 0) {
380                 Gtkmm2ext::UI::instance()->call_slot (bind (
381                                 mem_fun (*this, &StreamView::display_diskstream),
382                                 t->diskstream()));
383         } else {
384                 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
385         }
386 }
387
388 void
389 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
390
391 {
392         list<RegionView *>::iterator i;
393
394         switch (target) {
395         case RegionColor:
396                 region_color = color;
397                 for (i = region_views.begin(); i != region_views.end(); ++i) {
398                         (*i)->set_color (region_color);
399                 }
400                 break;
401                 
402         case StreamBaseColor:
403                 stream_base_color = RGBA_TO_UINT (
404                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
405                 canvas_rect->property_fill_color_rgba() = stream_base_color;
406                 break;
407         }
408 }
409
410 void
411 StreamView::region_layered (RegionView* rv)
412 {
413         /* don't ever leave it at the bottom, since then it doesn't
414            get events - the  parent group does instead ...
415         */
416         rv->get_canvas_group()->raise (rv->region()->layer());
417 }
418
419 void
420 StreamView::rec_enable_changed ()
421 {
422         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
423 }
424
425 void
426 StreamView::sess_rec_enable_changed ()
427 {
428         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
429 }
430
431 void
432 StreamView::transport_changed()
433 {
434         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
435 }
436
437 void
438 StreamView::transport_looped()
439 {
440         // to force a new rec region
441         rec_active = false;
442         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
443 }
444
445 void
446 StreamView::update_rec_box ()
447 {
448         if (rec_active && rec_rects.size() > 0) {
449                 /* only update the last box */
450                 RecBoxInfo & rect = rec_rects.back();
451                 nframes_t at = _trackview.get_diskstream()->current_capture_end();
452                 double xstart;
453                 double xend;
454                 
455                 switch (_trackview.track()->mode()) {
456                 
457                 case NonLayered:
458                 case Normal:
459                         rect.length = at - rect.start;
460                         xstart = _trackview.editor().frame_to_pixel (rect.start);
461                         xend = _trackview.editor().frame_to_pixel (at);
462                         break;
463                         
464                 case Destructive:
465                         rect.length = 2;
466                         xstart = _trackview.editor().frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
467                         xend = _trackview.editor().frame_to_pixel (at);
468                         break;
469                 }
470                 
471                 rect.rectangle->property_x1() = xstart;
472                 rect.rectangle->property_x2() = xend;
473         }
474 }
475         
476 RegionView*
477 StreamView::find_view (boost::shared_ptr<const Region> region)
478 {
479         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
480
481                 if ((*i)->region() == region) {
482                         return *i;
483                 }
484         }
485         return 0;
486 }
487         
488 void
489 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
490 {
491         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
492                 slot (*i);
493         }
494 }
495
496 void
497 StreamView::set_selected_regionviews (RegionSelection& regions)
498 {
499         bool selected;
500
501         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
502                 
503                 selected = false;
504                 
505                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
506                         if (*i == *ii) {
507                                 selected = true;
508                                 break;
509                         }
510                 }
511
512                 (*i)->set_selected (selected);
513         }
514 }
515
516 void
517 StreamView::get_selectables (nframes_t start, nframes_t end, double top, double bottom, list<Selectable*>& results)
518 {
519         layer_t min_layer = 0;
520         layer_t max_layer = 0;
521         
522         if (_layer_display == Stacked) {
523                 double const c = child_height ();
524                 min_layer = _layers - ((bottom - _trackview.y_position()) / c);
525                 max_layer = _layers - ((top - _trackview.y_position()) / c);
526         }
527         
528         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
529
530                 bool layer_ok = true;
531
532                 if (_layer_display == Stacked) {
533                         layer_t const l = (*i)->region()->layer ();
534                         layer_ok = (min_layer <= l && l <= max_layer);
535                 }
536                 
537                 if ((*i)->region()->coverage (start, end) != OverlapNone && layer_ok) {
538                         results.push_back (*i);
539                 }
540         }
541 }
542
543 void
544 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
545 {
546         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
547                 if (!sel.regions.contains (*i)) {
548                         results.push_back (*i);
549                 }
550         }
551 }
552
553 /** @return height of a child region view, depending on stacked / overlaid mode */
554 double
555 StreamView::child_height () const
556 {
557         if (_layer_display == Stacked) {
558                 return height / _layers;
559         }
560         
561         return height;
562 }
563
564 void
565 StreamView::update_contents_height ()
566 {
567         const double h = child_height ();
568
569         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
570                 switch (_layer_display) {
571                 case Overlaid:
572                         (*i)->set_y (0);
573                         break;
574                 case Stacked:
575                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
576                         break;
577                 }
578
579                 (*i)->set_height (h);
580         }
581
582         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
583                 i->rectangle->property_y2() = height - 1.0;
584         }
585 }
586
587 void
588 StreamView::set_layer_display (LayerDisplay d)
589 {
590         _layer_display = d;
591         update_contents_height ();
592         update_coverage_frames ();
593         _trackview.get_diskstream()->playlist()->set_explicit_relayering (_layer_display == Stacked);
594 }
595
596 void
597 StreamView::update_coverage_frames ()
598 {
599         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
600                 (*i)->update_coverage_frames (_layer_display);
601         }
602 }