24e476704ab2036e6e91ea04fc070fecf7c36a84
[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::set_height (gdouble height)
259 {
260         TimeAxisViewItem::set_height (height - 2);
261         
262         _height = height;
263 }
264
265 void
266 RegionView::region_layered ()
267 {
268         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
269         assert(rtv);
270         rtv->view()->region_layered (this);
271 }
272         
273 void
274 RegionView::region_muted ()
275 {
276         set_frame_color ();
277         region_renamed ();
278 }
279
280 void
281 RegionView::region_opacity ()
282 {
283         set_frame_color ();
284 }
285
286 void
287 RegionView::raise ()
288 {
289         _region->raise ();
290 }
291
292 void
293 RegionView::raise_to_top ()
294 {
295         _region->raise_to_top ();
296 }
297
298 void
299 RegionView::lower ()
300 {
301         _region->lower ();
302 }
303
304 void
305 RegionView::lower_to_bottom ()
306 {
307         _region->lower_to_bottom ();
308 }
309
310 bool
311 RegionView::set_position (nframes_t pos, void* src, double* ignored)
312 {
313         double delta;
314         bool ret;
315
316         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
317                 return false;
318         }
319
320         if (ignored) {
321                 *ignored = delta;
322         }
323
324         if (delta) {
325                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
326                         (*i)->group->move (delta, 0.0);
327                 }
328         }
329
330         return ret;
331 }
332
333 void
334 RegionView::set_samples_per_unit (gdouble spu)
335 {
336         TimeAxisViewItem::set_samples_per_unit (spu);
337
338         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
339                 (*i)->set_samples_per_unit (spu);
340                 (*i)->set_duration (_region->length() / samples_per_unit);
341         }
342
343         region_sync_changed ();
344 }
345
346 bool
347 RegionView::set_duration (nframes_t frames, void *src)
348 {
349         if (!TimeAxisViewItem::set_duration (frames, src)) {
350                 return false;
351         }
352         
353         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
354                 (*i)->set_duration (_region->length() / samples_per_unit);
355         }
356
357         return true;
358 }
359
360 void
361 RegionView::compute_colors (Gdk::Color& basic_color)
362 {
363         TimeAxisViewItem::compute_colors (basic_color);
364 }
365
366 void
367 RegionView::set_colors ()
368 {
369         TimeAxisViewItem::set_colors ();
370         
371         if (sync_mark) {
372                 sync_mark->property_fill_color_rgba() = fill_color;
373         }
374 }
375
376 void
377 RegionView::set_frame_color ()
378 {
379         if (_region->opaque()) {
380                 fill_opacity = 130;
381         } else {
382                 fill_opacity = 60;
383         }
384
385         TimeAxisViewItem::set_frame_color ();
386 }
387
388 void
389 RegionView::fake_set_opaque (bool yn)
390 {
391        if (yn) {
392                fill_opacity = 130;
393        } else {
394                fill_opacity = 60;
395        }
396
397        TimeAxisViewItem::set_frame_color ();
398 }
399
400 void
401 RegionView::hide_region_editor()
402 {
403         if (editor) {
404                 editor->hide_all ();
405         }
406 }
407
408 void
409 RegionView::region_renamed ()
410 {
411         string str;
412
413         if (_region->locked()) {
414                 str += '>';
415                 str += _region->name();
416                 str += '<';
417         } else {
418                 str = _region->name();
419         }
420
421         // speed mismatch handled in audio_region_view.cc
422         // FIXME: come up with more elegant solution for this
423         
424         if (_region->muted()) {
425                 str = string ("!") + str;
426         }
427
428         set_item_name (str, this);
429         set_name_text (str);
430         reset_width_dependent_items (_pixel_width);
431 }
432
433 void
434 RegionView::region_sync_changed ()
435 {
436         if (sync_mark == 0) {
437                 return;
438         }
439
440         int sync_dir;
441         nframes_t sync_offset;
442
443         sync_offset = _region->sync_offset (sync_dir);
444
445         /* this has to handle both a genuine change of position, a change of samples_per_unit,
446            and a change in the bounds of the _region->
447          */
448
449         if (sync_offset == 0) {
450
451                 /* no sync mark - its the start of the region */
452
453                 sync_mark->hide();
454
455         } else {
456
457                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
458
459                         /* no sync mark - its out of the bounds of the region */
460
461                         sync_mark->hide();
462
463                 } else {
464
465                         /* lets do it */
466
467                         Points points;
468                         
469                         //points = sync_mark->property_points().get_value();
470                         
471                         double offset = sync_offset / samples_per_unit;
472                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
473                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
474                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
475                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
476                         sync_mark->property_points().set_value (points);
477                         sync_mark->show();
478
479                 }
480         }
481 }
482
483 void
484 RegionView::move (double x_delta, double y_delta)
485 {
486         if (_region->locked() || (x_delta == 0 && y_delta == 0)) {
487                 return;
488         }
489
490         get_canvas_group()->move (x_delta, y_delta);
491
492         /* note: ghosts never leave their tracks so y_delta for them is always zero */
493
494         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
495                 (*i)->group->move (x_delta, 0.0);
496         }
497 }
498
499 void
500 RegionView::remove_ghost (GhostRegion* ghost)
501 {
502         if (in_destructor) {
503                 return;
504         }
505
506         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
507                 if (*i == ghost) {
508                         ghosts.erase (i);
509                         break;
510                 }
511         }
512 }
513
514 uint32_t
515 RegionView::get_fill_color ()
516 {
517         return fill_color;
518 }
519