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