Fix deletion of VCA with slaved controls.
[ardour.git] / gtk2_ardour / ghostregion.cc
1 /*
2     Copyright (C) 2000-2007 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 "ardour/parameter_descriptor.h"
21
22 #include "evoral/Note.hpp"
23 #include "canvas/container.h"
24 #include "canvas/polygon.h"
25 #include "canvas/rectangle.h"
26 #include "canvas/wave_view.h"
27 #include "canvas/debug.h"
28
29 #include "automation_time_axis.h"
30 #include "ghostregion.h"
31 #include "midi_streamview.h"
32 #include "midi_time_axis.h"
33 #include "region_view.h"
34 #include "midi_region_view.h"
35 #include "rgb_macros.h"
36 #include "note.h"
37 #include "hit.h"
38 #include "ui_config.h"
39
40 using namespace std;
41 using namespace Editing;
42 using namespace ArdourCanvas;
43 using namespace ARDOUR;
44
45 GhostRegion::GhostRegion(RegionView& rv,
46                          ArdourCanvas::Container* parent,
47                          TimeAxisView& tv,
48                          TimeAxisView& source_tv,
49                          double initial_pos)
50     : parent_rv(rv)
51     , trackview(tv)
52     , source_trackview(source_tv)
53 {
54         group = new ArdourCanvas::Container (parent);
55         CANVAS_DEBUG_NAME (group, "ghost region");
56         group->set_position (ArdourCanvas::Duple (initial_pos, 0));
57
58         base_rect = new ArdourCanvas::Rectangle (group);
59         CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
60         base_rect->set_x0 (0);
61         base_rect->set_y0 (1.0);
62         base_rect->set_y1 (trackview.current_height());
63         base_rect->set_outline (false);
64
65         if (!is_automation_ghost()) {
66                 base_rect->hide();
67         }
68
69         GhostRegion::set_colors();
70
71         /* the parent group of a ghostregion is a dedicated group for ghosts,
72            so the new ghost would want to get to the top of that group*/
73         group->raise_to_top ();
74 }
75
76 GhostRegion::~GhostRegion ()
77 {
78         parent_rv.remove_ghost(this);
79         trackview.erase_ghost(this);
80         delete base_rect;
81         delete group;
82 }
83
84 void
85 GhostRegion::set_duration (double units)
86 {
87         base_rect->set_x1 (units);
88 }
89
90 void
91 GhostRegion::set_height ()
92 {
93         base_rect->set_y1 (trackview.current_height());
94 }
95
96 void
97 GhostRegion::set_colors ()
98 {
99         if (is_automation_ghost()) {
100                 base_rect->set_fill_color (UIConfiguration::instance().color_mod ("ghost track base", "ghost track base"));
101         }
102 }
103
104 guint
105 GhostRegion::source_track_color(unsigned char alpha)
106 {
107         Gdk::Color color = source_trackview.color();
108         return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
109 }
110
111 bool
112 GhostRegion::is_automation_ghost()
113 {
114         return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
115 }
116
117 AudioGhostRegion::AudioGhostRegion(RegionView& rv,
118                                    TimeAxisView& tv,
119                                    TimeAxisView& source_tv,
120                                    double initial_unit_pos)
121     : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
122 {
123
124 }
125
126 void
127 AudioGhostRegion::set_samples_per_pixel (double fpp)
128 {
129         for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
130                 (*i)->set_samples_per_pixel (fpp);
131         }
132 }
133
134 void
135 AudioGhostRegion::set_height ()
136 {
137         vector<WaveView*>::iterator i;
138         uint32_t n;
139
140         GhostRegion::set_height();
141
142         double const ht = ((trackview.current_height()) / (double) waves.size());
143
144         for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
145                 (*i)->set_height (ht);
146                 (*i)->set_y_position (n * ht);
147         }
148 }
149
150 void
151 AudioGhostRegion::set_colors ()
152 {
153         GhostRegion::set_colors();
154         guint fill_color;
155
156         if (is_automation_ghost()) {
157                 fill_color = UIConfiguration::instance().color ("ghost track wave fill");
158         }
159         else {
160                 fill_color = source_track_color(200);
161         }
162
163         for (uint32_t n=0; n < waves.size(); ++n) {
164                 waves[n]->set_outline_color (UIConfiguration::instance().color ("ghost track wave"));
165                 waves[n]->set_fill_color (fill_color);
166                 waves[n]->set_clip_color (UIConfiguration::instance().color ("ghost track wave clip"));
167                 waves[n]->set_zero_color (UIConfiguration::instance().color ("ghost track zero line"));
168         }
169 }
170
171 /** The general constructor; called when the destination timeaxisview doesn't have
172  *  a midistreamview.
173  *
174  *  @param rv The parent RegionView that is being ghosted.
175  *  @param tv TimeAxisView that this ghost region is on.
176  *  @param source_tv TimeAxisView that we are the ghost for.
177  */
178 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
179                                  TimeAxisView& tv,
180                                  TimeAxisView& source_tv,
181                                  double initial_unit_pos)
182     : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
183     , _note_group (new ArdourCanvas::Container (group))
184     ,  parent_mrv (rv)
185     , _optimization_iterator(events.end())
186 {
187         _outline = UIConfiguration::instance().color ("ghost track midi outline");
188
189         base_rect->lower_to_bottom();
190 }
191
192 /**
193  *  @param rv The parent RegionView being ghosted.
194  *  @param msv MidiStreamView that this ghost region is on.
195  *  @param source_tv TimeAxisView that we are the ghost for.
196  */
197 MidiGhostRegion::MidiGhostRegion(MidiRegionView& rv,
198                                  MidiStreamView& msv,
199                                  TimeAxisView& source_tv,
200                                  double initial_unit_pos)
201     : GhostRegion(rv,
202                   msv.midi_underlay_group,
203                   msv.trackview(),
204                   source_tv,
205                   initial_unit_pos)
206     , _note_group (new ArdourCanvas::Container (group))
207     ,   parent_mrv (rv)
208     , _optimization_iterator(events.end())
209 {
210         _outline = UIConfiguration::instance().color ("ghost track midi outline");
211
212         base_rect->lower_to_bottom();
213 }
214
215 MidiGhostRegion::~MidiGhostRegion()
216 {
217         clear_events ();
218         delete _note_group;
219 }
220
221 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
222         : event (e)
223 {
224
225         if (dynamic_cast<Note*>(e)) {
226                 item = new ArdourCanvas::Rectangle(
227                         g, ArdourCanvas::Rect(e->x0(), e->y0(), e->x1(), e->y1()));
228                 is_hit = false;
229         } else {
230                 Hit* hit = dynamic_cast<Hit*>(e);
231                 if (!hit) {
232                         return;
233                 }
234                 ArdourCanvas::Polygon* poly = new ArdourCanvas::Polygon(g);
235                 poly->set(Hit::points(e->y1() - e->y0()));
236                 poly->set_position(hit->position());
237                 item = poly;
238                 is_hit = true;
239         }
240
241         CANVAS_DEBUG_NAME (item, "ghost note item");
242 }
243
244 MidiGhostRegion::GhostEvent::~GhostEvent ()
245 {
246         /* event is not ours to delete */
247         delete item;
248 }
249
250 void
251 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
252 {
253 }
254
255 /** @return MidiStreamView that we are providing a ghost for */
256 MidiStreamView*
257 MidiGhostRegion::midi_view ()
258 {
259         StreamView* sv = source_trackview.view ();
260         assert (sv);
261         MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
262         assert (msv);
263
264         return msv;
265 }
266
267 void
268 MidiGhostRegion::set_height ()
269 {
270         GhostRegion::set_height();
271         update_contents_height ();
272 }
273
274 void
275 MidiGhostRegion::set_colors()
276 {
277         GhostRegion::set_colors();
278         _outline = UIConfiguration::instance().color ("ghost track midi outline");
279
280         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
281                 it->second->item->set_fill_color (UIConfiguration::instance().color_mod((*it).second->event->base_color(), "ghost track midi fill"));
282                 it->second->item->set_outline_color (_outline);
283         }
284 }
285
286 static double
287 note_height(TimeAxisView& trackview, MidiStreamView* mv)
288 {
289         const double tv_height  = trackview.current_height();
290         const double note_range = mv->contents_note_range();
291
292         return std::max(1.0, floor(tv_height / note_range - 1.0));
293 }
294
295 static double
296 note_y(TimeAxisView& trackview, MidiStreamView* mv, uint8_t note_num)
297 {
298         const double tv_height  = trackview.current_height();
299         const double note_range = mv->contents_note_range();
300         const double s          = tv_height / note_range;
301
302         return tv_height - (note_num + 1 - mv->lowest_note()) * s;
303 }
304
305 void
306 MidiGhostRegion::update_contents_height ()
307 {
308         MidiStreamView* mv = midi_view();
309
310         if (!mv) {
311                 return;
312         }
313
314         double const h = note_height(trackview, mv);
315
316         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
317                 uint8_t const note_num = it->second->event->note()->note();
318
319                 double const y = note_y(trackview, mv, note_num);
320
321                 if (!it->second->is_hit) {
322                         _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(it->second->item);
323                         _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
324                 } else {
325                         _tmp_poly = static_cast<ArdourCanvas::Polygon*>(it->second->item);
326                         Duple position = _tmp_poly->position();
327                         position.y = y;
328                         _tmp_poly->set_position(position);
329                         _tmp_poly->set(Hit::points(h));
330                 }
331         }
332 }
333
334 void
335 MidiGhostRegion::add_note (NoteBase* n)
336 {
337         GhostEvent* event = new GhostEvent (n, _note_group);
338         events.insert (make_pair (n->note(), event));
339
340         event->item->set_fill_color (UIConfiguration::instance().color_mod(n->base_color(), "ghost track midi fill"));
341         event->item->set_outline_color (_outline);
342
343         MidiStreamView* mv = midi_view();
344
345         if (mv) {
346
347                 if (!n->item()->visible()) {
348                         event->item->hide();
349                 } else {
350                         uint8_t const note_num = n->note()->note();
351                         double const  h        = note_height(trackview, mv);
352                         double const  y        = note_y(trackview, mv, note_num);
353                         if (!event->is_hit) {
354                                 _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(event->item);
355                                 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
356                         } else {
357                                 _tmp_poly = static_cast<ArdourCanvas::Polygon*>(event->item);
358                                 Duple position = _tmp_poly->position();
359                                 position.y = y;
360                                 _tmp_poly->set_position(position);
361                                 _tmp_poly->set(Hit::points(h));
362                         }
363                 }
364         }
365 }
366
367 void
368 MidiGhostRegion::clear_events()
369 {
370         _note_group->clear (true);
371         events.clear ();
372         _optimization_iterator = events.end();
373 }
374
375 /** Update the  positions of our representation of a note.
376  *  @param ev The GhostEvent from the parent MidiRegionView.
377  */
378 void
379 MidiGhostRegion::update_note (GhostEvent* ev)
380 {
381         MidiStreamView* mv = midi_view();
382
383         if (!mv) {
384                 return;
385         }
386
387         _tmp_rect = static_cast<ArdourCanvas::Rectangle*>(ev->item);
388
389         uint8_t const note_num = ev->event->note()->note();
390         double const y = note_y(trackview, mv, note_num);
391         double const h = note_height(trackview, mv);
392
393         _tmp_rect->set (ArdourCanvas::Rect (ev->event->x0(), y, ev->event->x1(), y + h));
394 }
395
396 /** Update the positions of our representation of a parent's hit.
397  *  @param ev The GhostEvent from the parent MidiRegionView.
398  */
399 void
400 MidiGhostRegion::update_hit (GhostEvent* ev)
401 {
402         MidiStreamView* mv = midi_view();
403
404         if (!mv) {
405                 return;
406         }
407
408         _tmp_poly = static_cast<ArdourCanvas::Polygon*>(ev->item);
409
410         uint8_t const note_num = ev->event->note()->note();
411         double const h = note_height(trackview, mv);
412         double const y = note_y(trackview, mv, note_num);
413
414         ArdourCanvas::Duple ppos = ev->item->position();
415         ArdourCanvas::Duple gpos = _tmp_poly->position();
416         gpos.x = ppos.x;
417         gpos.y = y;
418
419         _tmp_poly->set_position(gpos);
420         _tmp_poly->set(Hit::points(h));
421 }
422
423 void
424 MidiGhostRegion::remove_note (NoteBase* note)
425 {
426         EventList::iterator f = events.find (note->note());
427         if (f == events.end()) {
428                 return;
429         }
430
431         delete f->second;
432         events.erase (f);
433
434         _optimization_iterator = events.end ();
435 }
436 void
437 MidiGhostRegion::redisplay_model ()
438 {
439         /* we rely on the parent MRV having removed notes not in the model */
440         for (EventList::iterator i = events.begin(); i != events.end(); ) {
441
442                 boost::shared_ptr<NoteType> note = i->first;
443                 GhostEvent* cne = i->second;
444                 const bool visible = (note->note() >= parent_mrv._current_range_min) &&
445                         (note->note() <= parent_mrv._current_range_max);
446
447                 if (visible) {
448                         if (cne->is_hit) {
449                                 update_hit (cne);
450                         } else {
451                                 update_note (cne);
452                         }
453                         cne->item->show ();
454                 } else {
455                         cne->item->hide ();
456                 }
457
458                 ++i;
459         }
460 }
461
462 /** Given a note in our parent region (ie the actual MidiRegionView), find our
463  *  representation of it.
464  *  @return Our Event, or 0 if not found.
465  */
466
467 MidiGhostRegion::GhostEvent *
468 MidiGhostRegion::find_event (boost::shared_ptr<NoteType> parent)
469 {
470         /* we are using _optimization_iterator to speed up the common case where a caller
471            is going through our notes in order.
472         */
473
474         if (_optimization_iterator != events.end()) {
475                 ++_optimization_iterator;
476                 if (_optimization_iterator != events.end() && _optimization_iterator->first == parent) {
477                         return _optimization_iterator->second;
478                 }
479         }
480
481         _optimization_iterator = events.find (parent);
482         if (_optimization_iterator != events.end()) {
483                 return _optimization_iterator->second;
484         }
485
486         return 0;
487 }