various minor fixes for crashes reported by nowhiskey
[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 "streamview.h"
35 #include "region_view.h"
36 #include "route_time_axis.h"
37 #include "simplerect.h"
38 #include "simpleline.h"
39 #include "waveview.h"
40 #include "public_editor.h"
41 #include "region_editor.h"
42 #include "ghostregion.h"
43 #include "route_time_axis.h"
44 #include "utils.h"
45 #include "rgb_macros.h"
46 #include "gui_thread.h"
47 #include "ardour_ui.h"
48
49 #include "i18n.h"
50
51 using namespace sigc;
52 using namespace ARDOUR;
53 using namespace PBD;
54 using namespace Editing;
55 using namespace ArdourCanvas;
56
57 static const int32_t sync_mark_width = 9;
58
59 sigc::signal<void,RegionView*> RegionView::RegionViewGoingAway;
60
61 RegionView::RegionView (ArdourCanvas::Group* parent, 
62                         TimeAxisView&        tv,
63                         boost::shared_ptr<ARDOUR::Region> r,
64                         double               spu,
65                         Gdk::Color&          basic_color
66                                                 )
67         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false,
68                             TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
69                                                           TimeAxisViewItem::ShowNameHighlight|
70                                                           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           , _pixel_width(1.0)
78           , _height(1.0)
79           , in_destructor(false)
80           , wait_for_data(false)
81 {
82 }
83
84 RegionView::RegionView (const RegionView& other)
85         : TimeAxisViewItem (other)
86 {
87         /* derived concrete type will call init () */
88
89         _region = other._region;
90         current_visible_sync_position = other.current_visible_sync_position;
91         valid = false;
92         _pixel_width = other._pixel_width;
93         _height = other._height;
94 }
95
96 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
97         : TimeAxisViewItem (other)
98 {
99         /* this is a pseudo-copy constructor used when dragging regions 
100            around on the canvas.
101         */
102
103         /* derived concrete type will call init () */
104
105         _region = other_region;
106         current_visible_sync_position = other.current_visible_sync_position;
107         valid = false;
108         _pixel_width = other._pixel_width;
109         _height = other._height;
110 }
111
112 RegionView::RegionView (ArdourCanvas::Group*         parent, 
113                         TimeAxisView&                tv,
114                         boost::shared_ptr<ARDOUR::Region> r,
115                         double                       spu,
116                         Gdk::Color&                  basic_color,
117                                                 bool recording,
118                         TimeAxisViewItem::Visibility visibility)
119         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, visibility)
120         , _region (r)
121         , sync_mark(0)
122         , sync_line(0)
123         , editor(0)
124         , current_visible_sync_position(0.0)
125         , valid(false)
126         , _pixel_width(1.0)
127         , _height(1.0)
128         , in_destructor(false)
129         , wait_for_data(false)
130 {
131 }
132
133 void
134 RegionView::init (Gdk::Color& basic_color, bool wfd)
135 {
136         editor        = 0;
137         valid         = true;
138         in_destructor = false;
139         _height       = 0;
140         wait_for_data = wfd;
141         sync_mark     = 0;
142         sync_line     = 0;
143
144         compute_colors (basic_color);
145
146         if (name_highlight) {
147                 name_highlight->set_data ("regionview", this);
148                 name_highlight->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
149         }
150
151         if (name_text) {
152                 name_text->set_data ("regionview", this);
153         }
154
155         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
156
157         set_height (trackview.current_height());
158
159         _region->StateChanged.connect (mem_fun(*this, &RegionView::region_changed));
160
161         group->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
162
163         set_colors ();
164
165         ColorsChanged.connect (mem_fun (*this, &RegionView::color_handler));
166         /* XXX sync mark drag? */
167 }
168
169 RegionView::~RegionView ()
170 {
171         in_destructor = true;
172
173         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
174                 delete *g;
175         }
176
177         if (editor) {
178                 delete editor;
179         }
180 }
181
182 gint
183 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
184 {
185         switch (ev->type) {
186         case GDK_BUTTON_RELEASE:
187                 static_cast<RegionView*>(arg)->lock_toggle ();
188                 return TRUE;
189                 break;
190         default:
191                 break;
192         } 
193         return FALSE;
194 }
195
196 void
197 RegionView::lock_toggle ()
198 {
199         _region->set_locked (!_region->locked());
200 }
201
202 void
203 RegionView::region_changed (Change what_changed)
204 {
205         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
206
207         if (what_changed & BoundsChanged) {
208                 region_resized (what_changed);
209                 region_sync_changed ();
210         }
211         if (what_changed & Region::MuteChanged) {
212                 region_muted ();
213         }
214         if (what_changed & Region::OpacityChanged) {
215                 region_opacity ();
216         }
217         if (what_changed & ARDOUR::NameChanged) {
218                 region_renamed ();
219         }
220         if (what_changed & Region::SyncOffsetChanged) {
221                 region_sync_changed ();
222         }
223         if (what_changed & Region::LayerChanged) {
224                 region_layered ();
225         }
226         if (what_changed & Region::LockChanged) {
227                 region_locked ();
228         }
229 }
230
231 void
232 RegionView::region_locked ()
233 {
234         /* name will show locked status */
235         region_renamed ();
236 }
237
238 void
239 RegionView::region_resized (Change what_changed)
240 {
241         double unit_length;
242
243         if (what_changed & ARDOUR::PositionChanged) {
244                 set_position (_region->position(), 0);
245         }
246
247         if (what_changed & Change (StartChanged|LengthChanged)) {
248
249                 set_duration (_region->length(), 0);
250
251                 unit_length = _region->length() / samples_per_unit;
252                 
253                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
254
255                         (*i)->set_duration (unit_length);
256
257                 }
258         }
259 }
260
261 void
262 RegionView::reset_width_dependent_items (double pixel_width)
263 {
264         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
265         _pixel_width = pixel_width;
266 }
267
268 void
269 RegionView::region_layered ()
270 {
271         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
272         assert(rtv);
273         rtv->view()->region_layered (this);
274 }
275         
276 void
277 RegionView::region_muted ()
278 {
279         set_frame_color ();
280         region_renamed ();
281 }
282
283 void
284 RegionView::region_opacity ()
285 {
286         set_frame_color ();
287 }
288
289 void
290 RegionView::raise ()
291 {
292         _region->raise ();
293 }
294
295 void
296 RegionView::raise_to_top ()
297 {
298         _region->raise_to_top ();
299 }
300
301 void
302 RegionView::lower ()
303 {
304         _region->lower ();
305 }
306
307 void
308 RegionView::lower_to_bottom ()
309 {
310         _region->lower_to_bottom ();
311 }
312
313 bool
314 RegionView::set_position (nframes_t pos, void* src, double* ignored)
315 {
316         double delta;
317         bool ret;
318
319         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
320                 return false;
321         }
322
323         if (ignored) {
324                 *ignored = delta;
325         }
326
327         if (delta) {
328                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
329                         (*i)->group->move (delta, 0.0);
330                 }
331         }
332
333         return ret;
334 }
335
336 void
337 RegionView::set_samples_per_unit (gdouble spu)
338 {
339         TimeAxisViewItem::set_samples_per_unit (spu);
340
341         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
342                 (*i)->set_samples_per_unit (spu);
343                 (*i)->set_duration (_region->length() / samples_per_unit);
344         }
345
346         region_sync_changed ();
347 }
348
349 bool
350 RegionView::set_duration (nframes_t frames, void *src)
351 {
352         if (!TimeAxisViewItem::set_duration (frames, src)) {
353                 return false;
354         }
355         
356         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
357                 (*i)->set_duration (_region->length() / samples_per_unit);
358         }
359
360         return true;
361 }
362
363 void
364 RegionView::compute_colors (Gdk::Color& basic_color)
365 {
366         TimeAxisViewItem::compute_colors (basic_color);
367 }
368
369 void
370 RegionView::set_colors ()
371 {
372         TimeAxisViewItem::set_colors ();
373         
374         if (sync_mark) {
375                 sync_mark->property_fill_color_rgba() = fill_color;
376                 sync_line->property_fill_color_rgba() = fill_color;
377         }
378 }
379
380 void
381 RegionView::set_frame_color ()
382 {
383         if (_region->opaque()) {
384                 fill_opacity = 130;
385         } else {
386                 fill_opacity = 0;
387         }
388
389         TimeAxisViewItem::set_frame_color ();
390 }
391
392 void
393 RegionView::fake_set_opaque (bool yn)
394 {
395        if (yn) {
396                fill_opacity = 130;
397        } else {
398                fill_opacity = 0;
399        }
400        
401        set_frame_color ();
402 }
403
404 void
405 RegionView::hide_region_editor()
406 {
407         if (editor) {
408                 editor->hide_all ();
409         }
410 }
411
412 void
413 RegionView::region_renamed ()
414 {
415         string str;
416
417         if (_region->locked()) {
418                 str += '>';
419                 str += _region->name();
420                 str += '<';
421         } else {
422                 str = _region->name();
423         }
424
425         if (_region->speed_mismatch (trackview.session().frame_rate())) {
426                 str = string ("*") + str;
427         }
428
429         if (_region->muted()) {
430                 str = string ("!") + str;
431         }
432
433         set_item_name (str, this);
434         set_name_text (str);
435         reset_width_dependent_items (_pixel_width);
436 }
437
438 void
439 RegionView::region_sync_changed ()
440 {
441         int sync_dir;
442         nframes_t sync_offset;
443
444         sync_offset = _region->sync_offset (sync_dir);
445
446         if (sync_offset == 0) {
447                 /* no need for a sync mark */
448                 if (sync_mark) {
449                         sync_mark->hide();
450                         sync_line->hide ();
451                 }
452                 return;
453         }
454
455         if (!sync_mark) {
456
457                 /* points set below */
458                 
459                 sync_mark =  new ArdourCanvas::Polygon (*group);
460                 sync_mark->property_fill_color_rgba() = fill_color;
461
462                 sync_line = new ArdourCanvas::Line (*group);
463                 sync_line->property_fill_color_rgba() = fill_color;
464                 sync_line->property_width_pixels() = 1;
465         }
466
467         /* this has to handle both a genuine change of position, a change of samples_per_unit,
468            and a change in the bounds of the _region->
469          */
470
471         if (sync_offset == 0) {
472
473                 /* no sync mark - its the start of the region */
474                 
475                 sync_mark->hide();
476                 sync_line->hide ();
477
478         } else {
479
480                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
481
482                         /* no sync mark - its out of the bounds of the region */
483
484                         sync_mark->hide();
485                         sync_line->hide ();
486
487                 } else {
488
489                         /* lets do it */
490
491                         Points points;
492                         
493                         //points = sync_mark->property_points().get_value();
494                         
495                         double offset = sync_offset / samples_per_unit;
496                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
497                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
498                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
499                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
500                         sync_mark->property_points().set_value (points);
501                         sync_mark->show ();
502
503                         points.clear ();
504                         points.push_back (Gnome::Art::Point (offset, 0));
505                         points.push_back (Gnome::Art::Point (offset, _height - NAME_HIGHLIGHT_SIZE));
506
507                         sync_line->property_points().set_value (points);
508                         sync_line->show ();
509                 }
510         }
511 }
512
513 void
514 RegionView::move (double x_delta, double y_delta)
515 {
516         if (_region->locked() || (x_delta == 0 && y_delta == 0)) {
517                 return;
518         }
519
520         get_canvas_group()->move (x_delta, y_delta);
521
522         /* note: ghosts never leave their tracks so y_delta for them is always zero */
523
524         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
525                 (*i)->group->move (x_delta, 0.0);
526         }
527 }
528
529 void
530 RegionView::remove_ghost (GhostRegion* ghost)
531 {
532         if (in_destructor) {
533                 return;
534         }
535
536         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
537                 if (*i == ghost) {
538                         ghosts.erase (i);
539                         break;
540                 }
541         }
542 }
543
544 uint32_t
545 RegionView::get_fill_color ()
546 {
547         return fill_color;
548 }
549
550 void
551 RegionView::set_height (double h)
552 {
553         if (sync_line) {
554                 Points points;
555                 int sync_dir;
556                 nframes_t sync_offset;
557                 sync_offset = _region->sync_offset (sync_dir);
558                 double offset = sync_offset / samples_per_unit;
559
560                 points.push_back (Gnome::Art::Point (offset, 0));
561                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
562                 sync_line->property_points().set_value (points);
563         }
564 }