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