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