enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 #include <gtkmm2ext/utils.h>
25
26 #include "ardour/playlist.h"
27 #include "ardour/region.h"
28 #include "ardour/track.h"
29 #include "ardour/session.h"
30
31 #include "pbd/compose.h"
32
33 #include "canvas/rectangle.h"
34 #include "canvas/debug.h"
35
36 #include "streamview.h"
37 #include "region_view.h"
38 #include "route_time_axis.h"
39 #include "region_selection.h"
40 #include "selection.h"
41 #include "public_editor.h"
42 #include "timers.h"
43 #include "rgb_macros.h"
44 #include "gui_thread.h"
45 #include "ui_config.h"
46 #include "utils.h"
47
48 #include "pbd/i18n.h"
49
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace ARDOUR_UI_UTILS;
53 using namespace PBD;
54 using namespace Editing;
55
56 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Container* canvas_group)
57         : _trackview (tv)
58         , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Container (_trackview.canvas_display()))
59         , _samples_per_pixel (_trackview.editor().get_current_zoom ())
60         , rec_updating(false)
61         , rec_active(false)
62         , stream_base_color(0xFFFFFFFF)
63         , _layers (1)
64         , _layer_display (Overlaid)
65         , height (tv.height)
66         , last_rec_data_frame(0)
67 {
68         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
69
70         /* set_position() will position the group */
71
72         canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
73         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
74         canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
75         canvas_rect->set_outline (false);
76         canvas_rect->set_fill (true);
77         canvas_rect->Event.connect (sigc::bind (sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
78
79         if (_trackview.is_track()) {
80                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
81                 _trackview.track()->rec_enable_control()->Changed.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
82
83                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
84                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
85                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
86         }
87
88         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
89 }
90
91 StreamView::~StreamView ()
92 {
93         undisplay_track ();
94
95         delete canvas_rect;
96 }
97
98 void
99 StreamView::attach ()
100 {
101         if (_trackview.is_track()) {
102                 display_track (_trackview.track ());
103         }
104 }
105
106 int
107 StreamView::set_position (gdouble x, gdouble y)
108 {
109         _canvas_group->set_position (ArdourCanvas::Duple (x, y));
110         return 0;
111 }
112
113 int
114 StreamView::set_height (double h)
115 {
116         /* limit the values to something sane-ish */
117
118         if (h < 10.0 || h > 1000.0) {
119                 return -1;
120         }
121
122         if (height == h) {
123                 return 0;
124         }
125
126         height = h;
127         canvas_rect->set_y1 (height);
128         update_contents_height ();
129
130         return 0;
131 }
132
133 int
134 StreamView::set_samples_per_pixel (double fpp)
135 {
136         RegionViewList::iterator i;
137
138         if (fpp < 1.0) {
139                 return -1;
140         }
141
142         if (fpp == _samples_per_pixel) {
143                 return 0;
144         }
145
146         _samples_per_pixel = fpp;
147
148         for (i = region_views.begin(); i != region_views.end(); ++i) {
149                 (*i)->set_samples_per_pixel (fpp);
150         }
151
152         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
153                 RecBoxInfo &recbox = (*xi);
154
155                 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
156                 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
157
158                 recbox.rectangle->set_x0 (xstart);
159                 recbox.rectangle->set_x1 (xend);
160         }
161
162         update_coverage_frames ();
163
164         return 0;
165 }
166
167 void
168 StreamView::add_region_view (boost::weak_ptr<Region> wr)
169 {
170         boost::shared_ptr<Region> r (wr.lock());
171         if (!r) {
172                 return;
173         }
174
175         add_region_view_internal (r, true);
176
177         if (_layer_display == Stacked || _layer_display == Expanded) {
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 (*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         RegionViewRemoved (); /* EMIT SIGNAL */
203 }
204
205 void
206 StreamView::undisplay_track ()
207 {
208         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
209                 RegionViewList::iterator next = i;
210                 ++next;
211                 delete *i;
212                 i = next;
213         }
214
215         region_views.clear();
216 }
217
218 void
219 StreamView::display_track (boost::shared_ptr<Track> tr)
220 {
221         playlist_switched_connection.disconnect();
222         playlist_switched (tr);
223         tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
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 to the top in the sorted order.
278         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
279                 (*i)->get_canvas_group()->raise_to_top ();
280         }
281 }
282
283 void
284 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
285 {
286         boost::shared_ptr<Track> tr (wtr.lock());
287
288         if (!tr) {
289                 return;
290         }
291
292         /* update layers count and the y positions and heights of our regions */
293         if (tr->playlist()) {
294                 _layers = tr->playlist()->top_layer() + 1;
295         }
296
297         if (_layer_display == Stacked) {
298                 update_contents_height ();
299                 /* tricky. playlist_changed() does this as well, and its really inefficient. */
300                 update_coverage_frames ();
301         } else {
302                 /* layering has probably been modified. reflect this in the canvas. */
303                 layer_regions();
304         }
305 }
306
307 void
308 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
309 {
310         boost::shared_ptr<Track> tr (wtr.lock());
311
312         if (!tr) {
313                 return;
314         }
315
316         /* disconnect from old playlist */
317
318         playlist_connections.drop_connections ();
319         //undisplay_track ();
320
321         /* draw it */
322         tr->playlist()->freeze();
323         redisplay_track ();
324         tr->playlist()->thaw();
325         /* update layers count and the y positions and heights of our regions */
326         _layers = tr->playlist()->top_layer() + 1;
327         update_contents_height ();
328         update_coverage_frames ();
329
330         /* catch changes */
331
332         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
333         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
334         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
335         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
336 }
337
338
339 void
340 StreamView::diskstream_changed ()
341 {
342         boost::shared_ptr<Track> t;
343
344         if ((t = _trackview.track()) != 0) {
345                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
346         } else {
347                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
348         }
349 }
350
351 void
352 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
353 {
354         return apply_color (gdk_color_to_rgba (c), target);
355 }
356
357 void
358 StreamView::apply_color (uint32_t color, ColorTarget target)
359 {
360         list<RegionView *>::iterator i;
361
362         switch (target) {
363         case RegionColor:
364                 region_color = color;
365                 for (i = region_views.begin(); i != region_views.end(); ++i) {
366                         (*i)->set_color (region_color);
367                 }
368                 break;
369
370         case StreamBaseColor:
371                 stream_base_color = color;
372                 canvas_rect->set_fill_color (stream_base_color);
373                 break;
374         }
375 }
376
377 void
378 StreamView::region_layered (RegionView* rv)
379 {
380         /* don't ever leave it at the bottom, since then it doesn't
381            get events - the  parent group does instead ...
382         */
383         rv->get_canvas_group()->raise (rv->region()->layer());
384 }
385
386 void
387 StreamView::rec_enable_changed ()
388 {
389         setup_rec_box ();
390 }
391
392 void
393 StreamView::sess_rec_enable_changed ()
394 {
395         setup_rec_box ();
396 }
397
398 void
399 StreamView::transport_changed()
400 {
401         setup_rec_box ();
402 }
403
404 void
405 StreamView::transport_looped()
406 {
407         // to force a new rec region
408         rec_active = false;
409         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
410 }
411
412 void
413 StreamView::create_rec_box(framepos_t frame_pos, double width)
414 {
415         const double   xstart     = _trackview.editor().sample_to_pixel(frame_pos);
416         const double   xend       = xstart + width;
417         const uint32_t fill_color = UIConfiguration::instance().color_mod("recording rect", "recording_rect");
418
419         ArdourCanvas::Rectangle* rec_rect = new ArdourCanvas::Rectangle(_canvas_group);
420         rec_rect->set_x0(xstart);
421         rec_rect->set_y0(0);
422         rec_rect->set_x1(xend);
423         rec_rect->set_y1(child_height ());
424         rec_rect->set_outline_what(ArdourCanvas::Rectangle::What(0));
425         rec_rect->set_outline_color(UIConfiguration::instance().color("recording rect"));
426         rec_rect->set_fill_color(fill_color);
427         rec_rect->lower_to_bottom();
428
429         RecBoxInfo recbox;
430         recbox.rectangle = rec_rect;
431         recbox.length    = 0;
432
433         if (rec_rects.empty()) {
434                 recbox.start = _trackview.session()->record_location ();
435         } else {
436                 recbox.start = _trackview.session()->transport_frame ();
437         }
438
439         rec_rects.push_back (recbox);
440
441         screen_update_connection.disconnect();
442         screen_update_connection = Timers::rapid_connect (sigc::mem_fun(*this, &StreamView::update_rec_box));
443
444         rec_updating = true;
445         rec_active = true;
446 }
447
448 void
449 StreamView::update_rec_box ()
450 {
451         if (rec_active && rec_rects.size() > 0) {
452                 /* only update the last box */
453                 RecBoxInfo & rect = rec_rects.back();
454                 framepos_t const at = _trackview.track()->current_capture_end ();
455                 double xstart;
456                 double xend;
457
458                 switch (_trackview.track()->mode()) {
459
460                 case NonLayered:
461                 case Normal:
462                         rect.length = at - rect.start;
463                         xstart = _trackview.editor().sample_to_pixel (rect.start);
464                         xend = _trackview.editor().sample_to_pixel (at);
465                         break;
466
467                 case Destructive:
468                         rect.length = 2;
469                         xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
470                         xend = _trackview.editor().sample_to_pixel (at);
471                         break;
472
473                 default:
474                         fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
475                         abort(); /*NOTREACHED*/
476                         return;
477                 }
478
479                 rect.rectangle->set_x0 (xstart);
480                 rect.rectangle->set_x1 (xend);
481         }
482 }
483
484 RegionView*
485 StreamView::find_view (boost::shared_ptr<const Region> region)
486 {
487         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
488
489                 if ((*i)->region() == region) {
490                         return *i;
491                 }
492         }
493         return 0;
494 }
495
496 uint32_t
497 StreamView::num_selected_regionviews () const
498 {
499         uint32_t cnt = 0;
500
501         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
502                 if ((*i)->selected()) {
503                         ++cnt;
504                 }
505         }
506         return cnt;
507 }
508
509 void
510 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
511 {
512         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
513                 slot (*i);
514         }
515 }
516
517 void
518 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
519 {
520         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
521                 if ((*i)->selected()) {
522                         slot (*i);
523                 }
524         }
525 }
526
527 void
528 StreamView::set_selected_regionviews (RegionSelection& regions)
529 {
530         bool selected;
531
532         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
533
534                 selected = false;
535
536                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
537                         if (*i == *ii) {
538                                 selected = true;
539                                 break;
540                         }
541                 }
542
543                 (*i)->set_selected (selected);
544         }
545 }
546
547 /** Get selectable things within a given range.
548  *  @param start Start time in session frames.
549  *  @param end End time in session frames.
550  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
551  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
552  *  @param result Filled in with selectable things.
553  */
554 void
555 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results, bool within)
556 {
557         if (_trackview.editor().internal_editing()) {
558                 return;  // Don't select regions with an internal tool
559         }
560
561         layer_t min_layer = 0;
562         layer_t max_layer = 0;
563
564         if (_layer_display == Stacked) {
565                 double const c = child_height ();
566
567                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
568                 if (mi < 0) {
569                         min_layer = 0;
570                 } else {
571                         min_layer = mi;
572                 }
573
574                 int const ma = _layers - ((top - _trackview.y_position()) / c);
575                 if (ma > (int) _layers) {
576                         max_layer = _layers - 1;
577                 } else {
578                         max_layer = ma;
579                 }
580
581         }
582
583         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
584
585                 bool layer_ok = true;
586
587                 if (_layer_display == Stacked) {
588                         layer_t const l = (*i)->region()->layer ();
589                         layer_ok = (min_layer <= l && l <= max_layer);
590                 }
591
592                 if (within) {
593                         if ((*i)->region()->coverage (start, end) == Evoral::OverlapExternal && layer_ok) {
594                                 results.push_back (*i);
595                         }
596                 } else {
597                         if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
598                                 results.push_back (*i);
599                         }
600                 }
601
602         }
603 }
604
605 void
606 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
607 {
608         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
609                 if (!sel.regions.contains (*i)) {
610                         results.push_back (*i);
611                 }
612         }
613 }
614
615 /** @return height of a child region view, depending on stacked / overlaid mode */
616 double
617 StreamView::child_height () const
618 {
619         switch (_layer_display) {
620         case Overlaid:
621                 return height;
622         case Stacked:
623                 return height / _layers;
624         case Expanded:
625                 return height / (_layers * 2 + 1);
626         }
627
628         abort(); /* NOTREACHED */
629         return height;
630 }
631
632 void
633 StreamView::update_contents_height ()
634 {
635         const double h = child_height ();
636
637         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
638                 switch (_layer_display) {
639                 case Overlaid:
640                         (*i)->set_y (0);
641                         break;
642                 case Stacked:
643                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
644                         break;
645                 case Expanded:
646                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
647                         break;
648                 }
649
650                 (*i)->set_height (h);
651         }
652
653         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
654                 switch (_layer_display) {
655                 case Overlaid:
656                         i->rectangle->set_y1 (height);
657                         break;
658                 case Stacked:
659                 case Expanded:
660                         /* In stacked displays, the recregion is always at the top */
661                         i->rectangle->set_y0 (0);
662                         i->rectangle->set_y1 (h);
663                         break;
664                 }
665         }
666
667         ContentsHeightChanged (); /* EMIT SIGNAL */
668 }
669
670 void
671 StreamView::set_layer_display (LayerDisplay d)
672 {
673         _layer_display = d;
674
675         if (_layer_display == Overlaid) {
676                 layer_regions ();
677         }
678
679         update_contents_height ();
680         update_coverage_frames ();
681 }
682
683 void
684 StreamView::update_coverage_frames ()
685 {
686         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
687                 (*i)->update_coverage_frames (_layer_display);
688         }
689 }
690
691 void
692 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
693 {
694         if (_new_rec_layer_time < to) {
695                 /* The region being recorded has overlapped the start of a top-layered region, so
696                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
697                    as the proper layering will get sorted out when the recorded region is added to
698                    its playlist.
699                 */
700
701                 /* Stop this happening again */
702                 _new_rec_layer_time = max_framepos;
703
704                 /* Make space in the view for the new layer */
705                 ++_layers;
706
707                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
708                 region->set_layer (_layers - 1);
709
710                 /* and reset the view */
711                 update_contents_height ();
712         }
713 }
714
715 void
716 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
717 {
718         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
719            recorded region in.  To work out where this needs to happen, find the start of the next
720            top-layered region after the start of the region we are recording and make a note of it.
721         */
722         if (_layer_display == Stacked) {
723                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
724         } else {
725                 _new_rec_layer_time = max_framepos;
726         }
727 }