Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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         delete editor;
183 }
184
185 gint
186 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
187 {
188         switch (ev->type) {
189         case GDK_BUTTON_RELEASE:
190                 static_cast<RegionView*>(arg)->lock_toggle ();
191                 return TRUE;
192                 break;
193         default:
194                 break;
195         } 
196         return FALSE;
197 }
198
199 void
200 RegionView::lock_toggle ()
201 {
202         _region->set_locked (!_region->locked());
203 }
204
205 void
206 RegionView::region_changed (Change what_changed)
207 {
208         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
209
210         if (what_changed & BoundsChanged) {
211                 region_resized (what_changed);
212                 region_sync_changed ();
213         }
214         if (what_changed & Region::MuteChanged) {
215                 region_muted ();
216         }
217         if (what_changed & Region::OpacityChanged) {
218                 region_opacity ();
219         }
220         if (what_changed & ARDOUR::NameChanged) {
221                 region_renamed ();
222         }
223         if (what_changed & Region::SyncOffsetChanged) {
224                 region_sync_changed ();
225         }
226         /* 
227            this should not be needed now that only playlist can change layering
228         */
229         /*
230         if (what_changed & Region::LayerChanged) {
231                 // this is handled by the playlist i believe
232                 //region_layered ();
233         }
234         */
235         if (what_changed & Region::LockChanged) {
236                 region_locked ();
237         }
238 }
239
240 void
241 RegionView::region_locked ()
242 {
243         /* name will show locked status */
244         region_renamed ();
245 }
246
247 void
248 RegionView::region_resized (Change what_changed)
249 {
250         double unit_length;
251
252         if (what_changed & ARDOUR::PositionChanged) {
253                 set_position (_region->position(), 0);
254         }
255
256         if (what_changed & Change (StartChanged|LengthChanged)) {
257
258                 set_duration (_region->length(), 0);
259
260                 unit_length = _region->length() / samples_per_unit;
261                 
262                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
263
264                         (*i)->set_duration (unit_length);
265
266                 }
267         }
268 }
269
270 void
271 RegionView::reset_width_dependent_items (double pixel_width)
272 {
273         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
274         _pixel_width = pixel_width;
275
276         /*for (AutomationChildren::iterator i = _automation_children.begin();
277                         i != _automation_children.end(); ++i) {
278                 i->second->reset_width_dependent_items(pixel_width);
279         }*/
280 }
281
282 void
283 RegionView::region_layered ()
284 {
285         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
286         assert(rtv);
287         //rtv->view()->region_layered (this);
288 }
289         
290 void
291 RegionView::region_muted ()
292 {
293         set_frame_color ();
294         region_renamed ();
295 }
296
297 void
298 RegionView::region_opacity ()
299 {
300         set_frame_color ();
301 }
302
303 void
304 RegionView::raise_to_top ()
305 {
306         _region->raise_to_top ();
307 }
308
309 void
310 RegionView::lower_to_bottom ()
311 {
312         _region->lower_to_bottom ();
313 }
314
315 bool
316 RegionView::set_position (nframes_t pos, void* src, double* ignored)
317 {
318         double delta;
319         bool ret;
320
321         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
322                 return false;
323         }
324
325         if (ignored) {
326                 *ignored = delta;
327         }
328
329         if (delta) {
330                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
331                         (*i)->group->move (delta, 0.0);
332                 }
333         
334                 for (AutomationChildren::iterator i = _automation_children.begin();
335                                 i != _automation_children.end(); ++i) {
336                         i->second->get_canvas_group()->move(delta, 0.0);
337                 }
338         }
339
340         return ret;
341 }
342
343 void
344 RegionView::set_samples_per_unit (gdouble spu)
345 {
346         TimeAxisViewItem::set_samples_per_unit (spu);
347
348         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
349                 (*i)->set_samples_per_unit (spu);
350                 (*i)->set_duration (_region->length() / samples_per_unit);
351         }
352
353         region_sync_changed ();
354 }
355
356 bool
357 RegionView::set_duration (nframes_t frames, void *src)
358 {
359         if (!TimeAxisViewItem::set_duration (frames, src)) {
360                 return false;
361         }
362         
363         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
364                 (*i)->set_duration (_region->length() / samples_per_unit);
365         }
366
367         return true;
368 }
369
370 void
371 RegionView::compute_colors (Gdk::Color& basic_color)
372 {
373         TimeAxisViewItem::compute_colors (basic_color);
374 }
375
376 void
377 RegionView::set_colors ()
378 {
379         TimeAxisViewItem::set_colors ();
380         
381         if (sync_mark) {
382                 sync_mark->property_fill_color_rgba() = fill_color;
383                 sync_line->property_fill_color_rgba() = fill_color;
384         }
385 }
386
387 void
388 RegionView::set_frame_color ()
389 {
390         if (_region->opaque()) {
391                 fill_opacity = 130;
392         } else {
393                 fill_opacity = 60;
394         }
395
396         TimeAxisViewItem::set_frame_color ();
397 }
398
399 void
400 RegionView::fake_set_opaque (bool yn)
401 {
402        if (yn) {
403                fill_opacity = 130;
404        } else {
405                fill_opacity = 60;
406        }
407        
408        set_frame_color ();
409 }
410
411 void
412 RegionView::hide_region_editor()
413 {
414         if (editor) {
415                 editor->hide_all ();
416         }
417 }
418
419 Glib::ustring
420 RegionView::make_name () const
421 {
422         Glib::ustring str;
423
424         // XXX nice to have some good icons for this
425
426         if (_region->locked()) {
427                 str += '>';
428                 str += _region->name();
429                 str += '<';
430         } else if (_region->position_locked()) {
431                 str += '{';
432                 str += _region->name();
433                 str += '}';
434         } else {
435                 str = _region->name();
436         }
437
438         if (_region->muted()) {
439                 str = string ("!") + str;
440         }
441
442         return str;
443 }
444
445 void
446 RegionView::region_renamed ()
447 {
448         Glib::ustring str = make_name ();
449
450         set_item_name (str, this);
451         set_name_text (str);
452         reset_width_dependent_items (_pixel_width);
453 }
454
455 void
456 RegionView::region_sync_changed ()
457 {
458         int sync_dir;
459         nframes_t sync_offset;
460
461         sync_offset = _region->sync_offset (sync_dir);
462
463         if (sync_offset == 0) {
464                 /* no need for a sync mark */
465                 if (sync_mark) {
466                         sync_mark->hide();
467                         sync_line->hide ();
468                 }
469                 return;
470         }
471
472         if (!sync_mark) {
473
474                 /* points set below */
475                 
476                 sync_mark =  new ArdourCanvas::Polygon (*group);
477                 sync_mark->property_fill_color_rgba() = fill_color;
478
479                 sync_line = new ArdourCanvas::Line (*group);
480                 sync_line->property_fill_color_rgba() = fill_color;
481                 sync_line->property_width_pixels() = 1;
482         }
483
484         /* this has to handle both a genuine change of position, a change of samples_per_unit,
485            and a change in the bounds of the _region->
486          */
487
488         if (sync_offset == 0) {
489
490                 /* no sync mark - its the start of the region */
491                 
492                 sync_mark->hide();
493                 sync_line->hide ();
494
495         } else {
496
497                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
498
499                         /* no sync mark - its out of the bounds of the region */
500
501                         sync_mark->hide();
502                         sync_line->hide ();
503
504                 } else {
505
506                         /* lets do it */
507
508                         Points points;
509                         
510                         //points = sync_mark->property_points().get_value();
511                         
512                         double offset = sync_offset / samples_per_unit;
513                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
514                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
515                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
516                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
517                         sync_mark->property_points().set_value (points);
518                         sync_mark->show ();
519
520                         points.clear ();
521                         points.push_back (Gnome::Art::Point (offset, 0));
522                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
523
524                         sync_line->property_points().set_value (points);
525                         sync_line->show ();
526                 }
527         }
528 }
529
530 void
531 RegionView::move (double x_delta, double y_delta)
532 {
533         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
534                 return;
535         }
536
537         get_canvas_group()->move (x_delta, y_delta);
538
539         /* note: ghosts never leave their tracks so y_delta for them is always zero */
540
541         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
542                 (*i)->group->move (x_delta, 0.0);
543         }
544                 
545         for (AutomationChildren::iterator i = _automation_children.begin();
546                         i != _automation_children.end(); ++i) {
547                 i->second->get_canvas_group()->move(x_delta, 0.0);
548         }
549 }
550
551 void
552 RegionView::remove_ghost_in (TimeAxisView& tv)
553 {
554         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
555                 if (&(*i)->trackview == &tv) {
556                         delete *i;
557                         break;
558                 }
559         }
560 }
561
562 void
563 RegionView::remove_ghost (GhostRegion* ghost)
564 {
565         if (in_destructor) {
566                 return;
567         }
568
569         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
570                 if (*i == ghost) {
571                         ghosts.erase (i);
572                         break;
573                 }
574         }
575 }
576
577 uint32_t
578 RegionView::get_fill_color ()
579 {
580         return fill_color;
581 }
582
583 void
584 RegionView::set_height (double h)
585 {
586         TimeAxisViewItem::set_height(h);
587
588         if (sync_line) {
589                 Points points;
590                 int sync_dir;
591                 nframes_t sync_offset;
592                 sync_offset = _region->sync_offset (sync_dir);
593                 double offset = sync_offset / samples_per_unit;
594
595                 points.push_back (Gnome::Art::Point (offset, 0));
596                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
597                 sync_line->property_points().set_value (points);
598         }
599 }
600