Remove now-deprecated non-zero page size in Adjustments used for SpinButtons;
[ardour.git] / gtk2_ardour / region_view.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
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27 #include <pbd/stacktrace.h>
28
29 #include <ardour/playlist.h>
30 #include <ardour/audioregion.h>
31 #include <ardour/audiosource.h>
32 #include <ardour/audio_diskstream.h>
33
34 #include "ardour_ui.h"
35 #include "streamview.h"
36 #include "region_view.h"
37 #include "automation_region_view.h"
38 #include "route_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
41 #include "waveview.h"
42 #include "public_editor.h"
43 #include "region_editor.h"
44 #include "ghostregion.h"
45 #include "route_time_axis.h"
46 #include "utils.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
49
50 #include "i18n.h"
51
52 using namespace sigc;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Editing;
56 using namespace ArdourCanvas;
57
58 static const int32_t sync_mark_width = 9;
59
60 sigc::signal<void,RegionView*> RegionView::RegionViewGoingAway;
61
62 RegionView::RegionView (ArdourCanvas::Group*              parent, 
63                         TimeAxisView&                     tv,
64                         boost::shared_ptr<ARDOUR::Region> r,
65                         double                            spu,
66                         Gdk::Color&                       basic_color)
67         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false,
68
69                             TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
70                                                           TimeAxisViewItem::ShowNameHighlight|
71                                                           TimeAxisViewItem::ShowFrame))
72           , _region (r)
73           , sync_mark(0)
74           , sync_line(0)
75           , editor(0)
76           , current_visible_sync_position(0.0)
77           , valid(false)
78           , _enable_display(false)
79           , _pixel_width(1.0)
80           , in_destructor(false)
81           , wait_for_data(false)
82 {
83 }
84
85 RegionView::RegionView (const RegionView& other)
86         : TimeAxisViewItem (other)
87 {
88         /* derived concrete type will call init () */
89
90         _region = other._region;
91         current_visible_sync_position = other.current_visible_sync_position;
92         valid = false;
93         _pixel_width = other._pixel_width;
94         _height = other._height;
95 }
96
97 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
98         : TimeAxisViewItem (other)
99 {
100         /* this is a pseudo-copy constructor used when dragging regions 
101            around on the canvas.
102         */
103
104         /* derived concrete type will call init () */
105
106         _region = other_region;
107         current_visible_sync_position = other.current_visible_sync_position;
108         valid = false;
109         _pixel_width = other._pixel_width;
110         _height = other._height;
111 }
112
113 RegionView::RegionView (ArdourCanvas::Group*         parent, 
114                         TimeAxisView&                tv,
115                         boost::shared_ptr<ARDOUR::Region> r,
116                         double                       spu,
117                         Gdk::Color&                  basic_color,
118                                                 bool recording,
119                         TimeAxisViewItem::Visibility visibility)
120         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, visibility)
121         , _region (r)
122         , sync_mark(0)
123         , sync_line(0)
124         , editor(0)
125         , current_visible_sync_position(0.0)
126         , valid(false)
127         , _enable_display(false)
128         , _pixel_width(1.0)
129         , in_destructor(false)
130         , wait_for_data(false)
131 {
132 }
133
134 void
135 RegionView::init (Gdk::Color& basic_color, bool wfd)
136 {
137         editor        = 0;
138         valid         = true;
139         in_destructor = false;
140         _height       = 0;
141         wait_for_data = wfd;
142         sync_mark     = 0;
143         sync_line     = 0;
144         sync_mark     = 0;
145         sync_line     = 0;
146
147         compute_colors (basic_color);
148
149         if (name_highlight) {
150                 name_highlight->set_data ("regionview", this);
151                 name_highlight->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
152         }
153
154         if (name_text) {
155                 name_text->set_data ("regionview", this);
156         }
157
158         if (wfd)
159                 _enable_display = true;
160
161         set_height (trackview.current_height());
162
163         _region->StateChanged.connect (mem_fun(*this, &RegionView::region_changed));
164
165         group->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
166
167         set_colors ();
168
169         ColorsChanged.connect (mem_fun (*this, &RegionView::color_handler));
170
171         /* XXX sync mark drag? */
172 }
173
174 RegionView::~RegionView ()
175 {
176         in_destructor = true;
177
178         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
179                 delete *g;
180         }
181
182         for (std::list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
183                 delete *i;
184         }
185
186         delete editor;
187 }
188
189 gint
190 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
191 {
192         switch (ev->type) {
193         case GDK_BUTTON_RELEASE:
194                 static_cast<RegionView*>(arg)->lock_toggle ();
195                 return TRUE;
196                 break;
197         default:
198                 break;
199         } 
200         return FALSE;
201 }
202
203 void
204 RegionView::lock_toggle ()
205 {
206         _region->set_locked (!_region->locked());
207 }
208
209 void
210 RegionView::region_changed (Change what_changed)
211 {
212         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
213
214         if (what_changed & BoundsChanged) {
215                 region_resized (what_changed);
216                 region_sync_changed ();
217         }
218         if (what_changed & Region::MuteChanged) {
219                 region_muted ();
220         }
221         if (what_changed & Region::OpacityChanged) {
222                 region_opacity ();
223         }
224         if (what_changed & ARDOUR::NameChanged) {
225                 region_renamed ();
226         }
227         if (what_changed & Region::SyncOffsetChanged) {
228                 region_sync_changed ();
229         }
230         /* 
231            this should not be needed now that only playlist can change layering
232         */
233         /*
234         if (what_changed & Region::LayerChanged) {
235                 // this is handled by the playlist i believe
236                 //region_layered ();
237         }
238         */
239         if (what_changed & Region::LockChanged) {
240                 region_locked ();
241         }
242 }
243
244 void
245 RegionView::region_locked ()
246 {
247         /* name will show locked status */
248         region_renamed ();
249 }
250
251 void
252 RegionView::region_resized (Change what_changed)
253 {
254         double unit_length;
255
256         if (what_changed & ARDOUR::PositionChanged) {
257                 set_position (_region->position(), 0);
258         }
259
260         if (what_changed & Change (StartChanged|LengthChanged)) {
261
262                 set_duration (_region->length(), 0);
263
264                 unit_length = _region->length() / samples_per_unit;
265                 
266                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
267
268                         (*i)->set_duration (unit_length);
269
270                 }
271         }
272 }
273
274 void
275 RegionView::reset_width_dependent_items (double pixel_width)
276 {
277         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
278         _pixel_width = pixel_width;
279
280         /*for (AutomationChildren::iterator i = _automation_children.begin();
281                         i != _automation_children.end(); ++i) {
282                 i->second->reset_width_dependent_items(pixel_width);
283         }*/
284 }
285
286 void
287 RegionView::region_layered ()
288 {
289         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
290         assert(rtv);
291         //rtv->view()->region_layered (this);
292 }
293         
294 void
295 RegionView::region_muted ()
296 {
297         set_frame_color ();
298         region_renamed ();
299 }
300
301 void
302 RegionView::region_opacity ()
303 {
304         set_frame_color ();
305 }
306
307 void
308 RegionView::raise_to_top ()
309 {
310         _region->raise_to_top ();
311 }
312
313 void
314 RegionView::lower_to_bottom ()
315 {
316         _region->lower_to_bottom ();
317 }
318
319 bool
320 RegionView::set_position (nframes_t pos, void* src, double* ignored)
321 {
322         double delta;
323         bool ret;
324
325         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
326                 return false;
327         }
328
329         if (ignored) {
330                 *ignored = delta;
331         }
332
333         if (delta) {
334                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
335                         (*i)->group->move (delta, 0.0);
336                 }
337         
338                 for (AutomationChildren::iterator i = _automation_children.begin();
339                                 i != _automation_children.end(); ++i) {
340                         i->second->get_canvas_group()->move(delta, 0.0);
341                 }
342         }
343
344         return ret;
345 }
346
347 void
348 RegionView::set_samples_per_unit (gdouble spu)
349 {
350         TimeAxisViewItem::set_samples_per_unit (spu);
351
352         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
353                 (*i)->set_samples_per_unit (spu);
354                 (*i)->set_duration (_region->length() / samples_per_unit);
355         }
356
357         region_sync_changed ();
358 }
359
360 bool
361 RegionView::set_duration (nframes_t frames, void *src)
362 {
363         if (!TimeAxisViewItem::set_duration (frames, src)) {
364                 return false;
365         }
366         
367         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
368                 (*i)->set_duration (_region->length() / samples_per_unit);
369         }
370
371         return true;
372 }
373
374 void
375 RegionView::compute_colors (Gdk::Color& basic_color)
376 {
377         TimeAxisViewItem::compute_colors (basic_color);
378 }
379
380 void
381 RegionView::set_colors ()
382 {
383         TimeAxisViewItem::set_colors ();
384         
385         if (sync_mark) {
386                 sync_mark->property_fill_color_rgba() = fill_color;
387                 sync_line->property_fill_color_rgba() = fill_color;
388         }
389 }
390
391 void
392 RegionView::set_frame_color ()
393 {
394         if (_region->opaque()) {
395                 fill_opacity = 130;
396         } else {
397                 fill_opacity = 60;
398         }
399
400         TimeAxisViewItem::set_frame_color ();
401 }
402
403 void
404 RegionView::fake_set_opaque (bool yn)
405 {
406        if (yn) {
407                fill_opacity = 130;
408        } else {
409                fill_opacity = 60;
410        }
411        
412        set_frame_color ();
413 }
414
415 void
416 RegionView::hide_region_editor()
417 {
418         if (editor) {
419                 editor->hide_all ();
420         }
421 }
422
423 Glib::ustring
424 RegionView::make_name () const
425 {
426         Glib::ustring str;
427
428         // XXX nice to have some good icons for this
429
430         if (_region->locked()) {
431                 str += '>';
432                 str += _region->name();
433                 str += '<';
434         } else if (_region->position_locked()) {
435                 str += '{';
436                 str += _region->name();
437                 str += '}';
438         } else {
439                 str = _region->name();
440         }
441
442         if (_region->muted()) {
443                 str = string ("!") + str;
444         }
445
446         return str;
447 }
448
449 void
450 RegionView::region_renamed ()
451 {
452         Glib::ustring str = make_name ();
453
454         set_item_name (str, this);
455         set_name_text (str);
456         reset_width_dependent_items (_pixel_width);
457 }
458
459 void
460 RegionView::region_sync_changed ()
461 {
462         int sync_dir;
463         nframes_t sync_offset;
464
465         sync_offset = _region->sync_offset (sync_dir);
466
467         if (sync_offset == 0) {
468                 /* no need for a sync mark */
469                 if (sync_mark) {
470                         sync_mark->hide();
471                         sync_line->hide ();
472                 }
473                 return;
474         }
475
476         if (!sync_mark) {
477
478                 /* points set below */
479                 
480                 sync_mark =  new ArdourCanvas::Polygon (*group);
481                 sync_mark->property_fill_color_rgba() = fill_color;
482
483                 sync_line = new ArdourCanvas::Line (*group);
484                 sync_line->property_fill_color_rgba() = fill_color;
485                 sync_line->property_width_pixels() = 1;
486         }
487
488         /* this has to handle both a genuine change of position, a change of samples_per_unit,
489            and a change in the bounds of the _region->
490          */
491
492         if (sync_offset == 0) {
493
494                 /* no sync mark - its the start of the region */
495                 
496                 sync_mark->hide();
497                 sync_line->hide ();
498
499         } else {
500
501                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
502
503                         /* no sync mark - its out of the bounds of the region */
504
505                         sync_mark->hide();
506                         sync_line->hide ();
507
508                 } else {
509
510                         /* lets do it */
511
512                         Points points;
513                         
514                         //points = sync_mark->property_points().get_value();
515                         
516                         double offset = sync_offset / samples_per_unit;
517                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
518                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
519                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
520                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
521                         sync_mark->property_points().set_value (points);
522                         sync_mark->show ();
523
524                         points.clear ();
525                         points.push_back (Gnome::Art::Point (offset, 0));
526                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
527
528                         sync_line->property_points().set_value (points);
529                         sync_line->show ();
530                 }
531         }
532 }
533
534 void
535 RegionView::move (double x_delta, double y_delta)
536 {
537         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
538                 return;
539         }
540
541         get_canvas_group()->move (x_delta, y_delta);
542
543         /* note: ghosts never leave their tracks so y_delta for them is always zero */
544
545         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
546                 (*i)->group->move (x_delta, 0.0);
547         }
548                 
549         for (AutomationChildren::iterator i = _automation_children.begin();
550                         i != _automation_children.end(); ++i) {
551                 i->second->get_canvas_group()->move(x_delta, 0.0);
552         }
553 }
554
555 void
556 RegionView::remove_ghost_in (TimeAxisView& tv)
557 {
558         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
559                 if (&(*i)->trackview == &tv) {
560                         delete *i;
561                         break;
562                 }
563         }
564 }
565
566 void
567 RegionView::remove_ghost (GhostRegion* ghost)
568 {
569         if (in_destructor) {
570                 return;
571         }
572
573         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
574                 if (*i == ghost) {
575                         ghosts.erase (i);
576                         break;
577                 }
578         }
579 }
580
581 uint32_t
582 RegionView::get_fill_color ()
583 {
584         return fill_color;
585 }
586
587 void
588 RegionView::set_height (double h)
589 {
590         TimeAxisViewItem::set_height(h);
591
592         if (sync_line) {
593                 Points points;
594                 int sync_dir;
595                 nframes_t sync_offset;
596                 sync_offset = _region->sync_offset (sync_dir);
597                 double offset = sync_offset / samples_per_unit;
598
599                 points.push_back (Gnome::Art::Point (offset, 0));
600                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
601                 sync_line->property_points().set_value (points);
602         }
603 }
604
605 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
606  *  which uses them. */
607 void
608 RegionView::update_coverage_frames (LayerDisplay d)
609 {
610         /* remove old coverage frames */
611         for (std::list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
612                 delete *i;
613         }
614
615         _coverage_frames.clear ();
616
617         if (d != Stacked) {
618                 /* don't do coverage frames unless we're in stacked mode */
619                 return;
620         }
621
622         boost::shared_ptr<Playlist> pl (_region->playlist ());
623         if (!pl) {
624                 return;
625         }
626
627         nframes_t const position = _region->first_frame ();
628         nframes_t t = position;
629         nframes_t const end = _region->last_frame ();
630
631         ArdourCanvas::SimpleRect* cr = 0;
632         bool me = false;
633
634         uint32_t const color = frame->property_fill_color_rgba ();
635         uint32_t const base_alpha = UINT_RGBA_A (color);
636         
637         while (t < end) {
638
639                 t++;
640
641                 /* is this region is on top at time t? */
642                 bool const new_me = (pl->top_region_at (t) == _region);
643
644                 /* finish off any old rect, if required */
645                 if (cr && me != new_me) {
646                         cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
647                 }
648
649                 /* start off any new rect, if required */
650                 if (cr == 0 || me != new_me) {
651                         cr = new ArdourCanvas::SimpleRect (*group);
652                         _coverage_frames.push_back (cr);
653                         cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
654                         cr->property_y1() = 1;
655                         cr->property_y2() = _height + 1;
656                         cr->property_outline_pixels() = 0;
657                         /* areas that will be played get a lower alpha */
658                         uint32_t alpha = base_alpha;
659                         if (new_me) {
660                                 alpha /= 2;
661                         }
662                         cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
663                 }
664
665                 t = pl->find_next_region_boundary (t, 1);
666                 me = new_me;
667         }
668
669         if (cr) {
670                 /* finish off the last rectangle */
671                 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
672         }
673 }