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