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