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