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