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