68676fe85ce0ae9ccbe1ea8180172668cdd9d12f
[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$
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                         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                         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_muted ();
137         region_sync_changed ();
138         region_resized (BoundsChanged);
139         region_locked ();
140
141         _region.StateChanged.connect (mem_fun(*this, &RegionView::region_changed));
142
143         group->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
144         name_highlight->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
145
146         set_colors ();
147
148         ColorChanged.connect (mem_fun (*this, &RegionView::color_handler));
149
150         /* XXX sync mark drag? */
151 }
152
153 RegionView::~RegionView ()
154 {
155         in_destructor = true;
156
157         RegionViewGoingAway (this); /* EMIT_SIGNAL */
158
159         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
160                 delete *g;
161         }
162
163         if (editor) {
164                 delete editor;
165         }
166 }
167
168 gint
169 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
170 {
171         switch (ev->type) {
172         case GDK_BUTTON_RELEASE:
173                 static_cast<RegionView*>(arg)->lock_toggle ();
174                 return TRUE;
175                 break;
176         default:
177                 break;
178         } 
179         return FALSE;
180 }
181
182 void
183 RegionView::lock_toggle ()
184 {
185         _region.set_locked (!_region.locked());
186 }
187
188 void
189 RegionView::region_changed (Change what_changed)
190 {
191         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
192
193         if (what_changed & BoundsChanged) {
194                 region_resized (what_changed);
195                 region_sync_changed ();
196         }
197         if (what_changed & Region::MuteChanged) {
198                 region_muted ();
199         }
200         if (what_changed & Region::OpacityChanged) {
201                 region_opacity ();
202         }
203         if (what_changed & ARDOUR::NameChanged) {
204                 region_renamed ();
205         }
206         if (what_changed & Region::SyncOffsetChanged) {
207                 region_sync_changed ();
208         }
209         if (what_changed & Region::LayerChanged) {
210                 region_layered ();
211         }
212         if (what_changed & Region::LockChanged) {
213                 region_locked ();
214         }
215 }
216
217 void
218 RegionView::region_locked ()
219 {
220         /* name will show locked status */
221         region_renamed ();
222 }
223
224 void
225 RegionView::region_resized (Change what_changed)
226 {
227         double unit_length;
228
229         if (what_changed & ARDOUR::PositionChanged) {
230                 set_position (_region.position(), 0);
231         }
232
233         if (what_changed & Change (StartChanged|LengthChanged)) {
234
235                 set_duration (_region.length(), 0);
236
237                 unit_length = _region.length() / samples_per_unit;
238                 
239                 reset_width_dependent_items (unit_length);
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::set_height (gdouble height)
258 {
259         TimeAxisViewItem::set_height (height - 2);
260         
261         _height = height;
262 }
263
264 void
265 RegionView::region_layered ()
266 {
267         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
268         assert(rtv);
269         rtv->view()->region_layered (this);
270 }
271         
272 void
273 RegionView::region_muted ()
274 {
275         set_frame_color ();
276         region_renamed ();
277 }
278
279 void
280 RegionView::region_opacity ()
281 {
282         set_frame_color ();
283 }
284
285 void
286 RegionView::raise ()
287 {
288         _region.raise ();
289 }
290
291 void
292 RegionView::raise_to_top ()
293 {
294         _region.raise_to_top ();
295 }
296
297 void
298 RegionView::lower ()
299 {
300         _region.lower ();
301 }
302
303 void
304 RegionView::lower_to_bottom ()
305 {
306         _region.lower_to_bottom ();
307 }
308
309 bool
310 RegionView::set_position (jack_nframes_t pos, void* src, double* ignored)
311 {
312         double delta;
313         bool ret;
314
315         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
316                 return false;
317         }
318
319         if (ignored) {
320                 *ignored = delta;
321         }
322
323         if (delta) {
324                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
325                         (*i)->group->move (delta, 0.0);
326                 }
327         }
328
329         return ret;
330 }
331
332 void
333 RegionView::set_samples_per_unit (gdouble spu)
334 {
335         TimeAxisViewItem::set_samples_per_unit (spu);
336
337         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
338                 (*i)->set_samples_per_unit (spu);
339                 (*i)->set_duration (_region.length() / samples_per_unit);
340         }
341
342         region_sync_changed ();
343 }
344
345 bool
346 RegionView::set_duration (jack_nframes_t frames, void *src)
347 {
348         if (!TimeAxisViewItem::set_duration (frames, src)) {
349                 return false;
350         }
351         
352         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
353                 (*i)->set_duration (_region.length() / samples_per_unit);
354         }
355
356         return true;
357 }
358
359 void
360 RegionView::compute_colors (Gdk::Color& basic_color)
361 {
362         TimeAxisViewItem::compute_colors (basic_color);
363 }
364
365 void
366 RegionView::set_colors ()
367 {
368         TimeAxisViewItem::set_colors ();
369         
370         if (sync_mark) {
371                 sync_mark->property_fill_color_rgba() = fill_color;
372         }
373 }
374
375 void
376 RegionView::set_frame_color ()
377 {
378         if (_region.opaque()) {
379                 fill_opacity = 180;
380         } else {
381                 fill_opacity = 100;
382         }
383
384         TimeAxisViewItem::set_frame_color ();
385 }
386
387 void
388 RegionView::hide_region_editor()
389 {
390         if (editor) {
391                 editor->hide_all ();
392         }
393 }
394
395 void
396 RegionView::region_renamed ()
397 {
398         string str;
399
400         if (_region.locked()) {
401                 str += '>';
402                 str += _region.name();
403                 str += '<';
404         } else {
405                 str = _region.name();
406         }
407
408         if (_region.muted()) {
409                 str = string ("!") + str;
410         }
411
412         set_item_name (str, this);
413         set_name_text (str);
414 }
415
416 void
417 RegionView::region_sync_changed ()
418 {
419         if (sync_mark == 0) {
420                 return;
421         }
422
423         int sync_dir;
424         jack_nframes_t sync_offset;
425
426         sync_offset = _region.sync_offset (sync_dir);
427
428         /* this has to handle both a genuine change of position, a change of samples_per_unit,
429            and a change in the bounds of the _region.
430          */
431
432         if (sync_offset == 0) {
433
434                 /* no sync mark - its the start of the region */
435
436                 sync_mark->hide();
437
438         } else {
439
440                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region.length()))) { 
441
442                         /* no sync mark - its out of the bounds of the region */
443
444                         sync_mark->hide();
445
446                 } else {
447
448                         /* lets do it */
449
450                         Points points;
451                         
452                         //points = sync_mark->property_points().get_value();
453                         
454                         double offset = sync_offset / samples_per_unit;
455                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
456                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
457                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
458                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
459                         sync_mark->property_points().set_value (points);
460                         sync_mark->show();
461
462                 }
463         }
464 }
465
466 void
467 RegionView::move (double x_delta, double y_delta)
468 {
469         if (_region.locked() || (x_delta == 0 && y_delta == 0)) {
470                 return;
471         }
472
473         get_canvas_group()->move (x_delta, y_delta);
474
475         /* note: ghosts never leave their tracks so y_delta for them is always zero */
476
477         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
478                 (*i)->group->move (x_delta, 0.0);
479         }
480 }
481
482 void
483 RegionView::remove_ghost (GhostRegion* ghost)
484 {
485         if (in_destructor) {
486                 return;
487         }
488
489         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
490                 if (*i == ghost) {
491                         ghosts.erase (i);
492                         break;
493                 }
494         }
495 }
496
497 uint32_t
498 RegionView::get_fill_color ()
499 {
500         return fill_color;
501 }
502