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