Per-region MIDI CC "automation".
[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 "route_time_axis.h"
38 #include "simplerect.h"
39 #include "simpleline.h"
40 #include "waveview.h"
41 #include "public_editor.h"
42 #include "region_editor.h"
43 #include "ghostregion.h"
44 #include "route_time_axis.h"
45 #include "utils.h"
46 #include "rgb_macros.h"
47 #include "gui_thread.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           , editor(0)
73           , current_visible_sync_position(0.0)
74           , valid(false)
75           , _enable_display(false)
76           , _pixel_width(1.0)
77           , in_destructor(false)
78           , wait_for_data(false)
79 {
80 }
81
82 RegionView::RegionView (const RegionView& other)
83         : TimeAxisViewItem (other)
84 {
85         /* derived concrete type will call init () */
86
87         _region = other._region;
88         editor = other.editor;
89         current_visible_sync_position = other.current_visible_sync_position;
90         valid = false;
91         _enable_display = false;
92         _pixel_width = other._pixel_width;
93 }
94
95 RegionView::RegionView (ArdourCanvas::Group*         parent, 
96                         TimeAxisView&                tv,
97                         boost::shared_ptr<ARDOUR::Region> r,
98                         double                       spu,
99                         Gdk::Color&                  basic_color,
100                         TimeAxisViewItem::Visibility visibility)
101         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), visibility)
102         , _region (r)
103         , sync_mark(0)
104         , editor(0)
105         , current_visible_sync_position(0.0)
106         , valid(false)
107         , _enable_display(false)
108         , _pixel_width(1.0)
109         , in_destructor(false)
110         , wait_for_data(false)
111 {
112 }
113
114 void
115 RegionView::init (Gdk::Color& basic_color, bool wfd)
116 {
117         valid           = true;
118         _enable_display = false;
119         in_destructor   = false;
120         wait_for_data   = wfd;
121
122         compute_colors (basic_color);
123
124         name_highlight->set_data ("regionview", this);
125
126         if (name_text) {
127                 name_text->set_data ("regionview", this);
128         }
129
130         /* an equilateral triangle */
131
132         ArdourCanvas::Points shape;
133         shape.push_back (Gnome::Art::Point (-((sync_mark_width-1)/2), 1));
134         shape.push_back (Gnome::Art::Point ((sync_mark_width - 1)/2, 1));
135         shape.push_back (Gnome::Art::Point (0, sync_mark_width - 1));
136         shape.push_back (Gnome::Art::Point (-((sync_mark_width-1)/2), 1));
137
138         sync_mark =  new ArdourCanvas::Polygon (*group);
139         sync_mark->property_points() = shape;
140         sync_mark->property_fill_color_rgba() = fill_color;
141         sync_mark->hide();
142
143         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
144
145         if (wfd)
146                 _enable_display = true;
147
148         set_y_position_and_height (0, trackview.height - 2);
149
150         _region->StateChanged.connect (mem_fun(*this, &RegionView::region_changed));
151
152         group->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
153         name_highlight->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
154
155         set_colors ();
156
157         ColorsChanged.connect (mem_fun (*this, &RegionView::color_handler));
158
159         /* XXX sync mark drag? */
160 }
161
162 RegionView::~RegionView ()
163 {
164         in_destructor = true;
165
166         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
167                 delete *g;
168         }
169
170         if (editor) {
171                 delete editor;
172         }
173 }
174
175 gint
176 RegionView::_lock_toggle (ArdourCanvas::Item* item, GdkEvent* ev, void* arg)
177 {
178         switch (ev->type) {
179         case GDK_BUTTON_RELEASE:
180                 static_cast<RegionView*>(arg)->lock_toggle ();
181                 return TRUE;
182                 break;
183         default:
184                 break;
185         } 
186         return FALSE;
187 }
188
189 void
190 RegionView::lock_toggle ()
191 {
192         _region->set_locked (!_region->locked());
193 }
194
195 void
196 RegionView::region_changed (Change what_changed)
197 {
198         ENSURE_GUI_THREAD (bind (mem_fun(*this, &RegionView::region_changed), what_changed));
199
200         if (what_changed & BoundsChanged) {
201                 region_resized (what_changed);
202                 region_sync_changed ();
203         }
204         if (what_changed & Region::MuteChanged) {
205                 region_muted ();
206         }
207         if (what_changed & Region::OpacityChanged) {
208                 region_opacity ();
209         }
210         if (what_changed & ARDOUR::NameChanged) {
211                 region_renamed ();
212         }
213         if (what_changed & Region::SyncOffsetChanged) {
214                 region_sync_changed ();
215         }
216         if (what_changed & Region::LayerChanged) {
217                 region_layered ();
218         }
219         if (what_changed & Region::LockChanged) {
220                 region_locked ();
221         }
222 }
223
224 void
225 RegionView::region_locked ()
226 {
227         /* name will show locked status */
228         region_renamed ();
229 }
230
231 void
232 RegionView::region_resized (Change what_changed)
233 {
234         double unit_length;
235
236         if (what_changed & ARDOUR::PositionChanged) {
237                 set_position (_region->position(), 0);
238         }
239
240         if (what_changed & Change (StartChanged|LengthChanged)) {
241
242                 set_duration (_region->length(), 0);
243
244                 unit_length = _region->length() / samples_per_unit;
245                 
246                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
247
248                         (*i)->set_duration (unit_length);
249
250                 }
251         }
252 }
253
254 void
255 RegionView::reset_width_dependent_items (double pixel_width)
256 {
257         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
258         _pixel_width = pixel_width;
259 }
260
261 void
262 RegionView::region_layered ()
263 {
264         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*>(&get_time_axis_view());
265         assert(rtv);
266         rtv->view()->region_layered (this);
267 }
268         
269 void
270 RegionView::region_muted ()
271 {
272         set_frame_color ();
273         region_renamed ();
274 }
275
276 void
277 RegionView::region_opacity ()
278 {
279         set_frame_color ();
280 }
281
282 void
283 RegionView::raise_to_top ()
284 {
285         _region->raise_to_top ();
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         }
358 }
359
360 void
361 RegionView::set_frame_color ()
362 {
363         if (_region->opaque()) {
364                 fill_opacity = 130;
365         } else {
366                 fill_opacity = 60;
367         }
368
369         TimeAxisViewItem::set_frame_color ();
370 }
371
372 void
373 RegionView::fake_set_opaque (bool yn)
374 {
375        if (yn) {
376                fill_opacity = 130;
377        } else {
378                fill_opacity = 60;
379        }
380
381        TimeAxisViewItem::set_frame_color ();
382 }
383
384 void
385 RegionView::hide_region_editor()
386 {
387         if (editor) {
388                 editor->hide_all ();
389         }
390 }
391
392 Glib::ustring
393 RegionView::make_name () const
394 {
395         Glib::ustring str;
396
397         // XXX nice to have some good icons for this
398
399         if (_region->locked()) {
400                 str += '>';
401                 str += _region->name();
402                 str += '<';
403         } else if (_region->position_locked()) {
404                 str += '{';
405                 str += _region->name();
406                 str += '}';
407         } else {
408                 str = _region->name();
409         }
410
411         if (_region->muted()) {
412                 str = string ("!") + str;
413         }
414
415         return str;
416 }
417
418 void
419 RegionView::region_renamed ()
420 {
421         Glib::ustring str = make_name ();
422
423         set_item_name (str, this);
424         set_name_text (str);
425         reset_width_dependent_items (_pixel_width);
426 }
427
428 void
429 RegionView::region_sync_changed ()
430 {
431         if (sync_mark == 0) {
432                 return;
433         }
434
435         int sync_dir;
436         nframes_t sync_offset;
437
438         sync_offset = _region->sync_offset (sync_dir);
439
440         /* this has to handle both a genuine change of position, a change of samples_per_unit,
441            and a change in the bounds of the _region->
442          */
443
444         if (sync_offset == 0) {
445
446                 /* no sync mark - its the start of the region */
447
448                 sync_mark->hide();
449
450         } else {
451
452                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) { 
453
454                         /* no sync mark - its out of the bounds of the region */
455
456                         sync_mark->hide();
457
458                 } else {
459
460                         /* lets do it */
461
462                         Points points;
463                         
464                         //points = sync_mark->property_points().get_value();
465                         
466                         double offset = sync_offset / samples_per_unit;
467                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
468                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
469                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
470                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));     
471                         sync_mark->property_points().set_value (points);
472                         sync_mark->show();
473
474                 }
475         }
476 }
477
478 void
479 RegionView::move (double x_delta, double y_delta)
480 {
481         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
482                 return;
483         }
484
485         get_canvas_group()->move (x_delta, y_delta);
486
487         /* note: ghosts never leave their tracks so y_delta for them is always zero */
488
489         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
490                 (*i)->group->move (x_delta, 0.0);
491         }
492 }
493
494 void
495 RegionView::remove_ghost (GhostRegion* ghost)
496 {
497         if (in_destructor) {
498                 return;
499         }
500
501         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
502                 if (*i == ghost) {
503                         ghosts.erase (i);
504                         break;
505                 }
506         }
507 }
508
509 uint32_t
510 RegionView::get_fill_color ()
511 {
512         return fill_color;
513 }
514