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