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