Update ghost MIDI regions in automation tracks when zoom changes. Fixes #3803.
[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 "evoral/Note.hpp"
21 #include "ardour_ui.h"
22 #include "automation_time_axis.h"
23 #include "canvas-hit.h"
24 #include "canvas-note.h"
25 #include "ghostregion.h"
26 #include "midi_streamview.h"
27 #include "midi_time_axis.h"
28 #include "rgb_macros.h"
29 #include "simplerect.h"
30 #include "waveview.h"
31
32 using namespace std;
33 using namespace Editing;
34 using namespace ArdourCanvas;
35 using namespace ARDOUR;
36
37 PBD::Signal1<void,GhostRegion*> GhostRegion::CatchDeletion;
38
39 GhostRegion::GhostRegion (ArdourCanvas::Group* parent, TimeAxisView& tv, TimeAxisView& source_tv, double initial_pos)
40         : trackview (tv)
41         , source_trackview (source_tv)
42 {
43         group = new ArdourCanvas::Group (*parent);
44         group->property_x() = initial_pos;
45         group->property_y() = 0.0;
46
47         base_rect = new ArdourCanvas::SimpleRect (*group);
48         base_rect->property_x1() = (double) 0.0;
49         base_rect->property_y1() = (double) 0.0;
50         base_rect->property_y2() = (double) trackview.current_height();
51         base_rect->property_outline_what() = (guint32) 0;
52
53         if (!is_automation_ghost()) {
54                 base_rect->hide();
55         }
56
57         GhostRegion::set_colors();
58
59         /* the parent group of a ghostregion is a dedicated group for ghosts,
60            so the new ghost would want to get to the top of that group*/
61         group->raise_to_top ();
62 }
63
64 GhostRegion::~GhostRegion ()
65 {
66         CatchDeletion (this);
67         delete base_rect;
68         delete group;
69 }
70
71 void
72 GhostRegion::set_duration (double units)
73 {
74         base_rect->property_x2() = units;
75 }
76
77 void
78 GhostRegion::set_height ()
79 {
80         base_rect->property_y2() = (double) trackview.current_height();
81 }
82
83 void
84 GhostRegion::set_colors ()
85 {
86         if (is_automation_ghost()) {
87                 base_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackBase.get();
88                 base_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackBase.get();
89         }
90 }
91
92 guint
93 GhostRegion::source_track_color(unsigned char alpha)
94 {
95         Gdk::Color color = source_trackview.color();
96         return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
97 }
98
99 bool
100 GhostRegion::is_automation_ghost()
101 {
102         return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
103 }
104
105 AudioGhostRegion::AudioGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
106         : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
107 {
108         
109 }
110
111 void
112 AudioGhostRegion::set_samples_per_unit (double spu)
113 {
114         for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
115                 (*i)->property_samples_per_unit() = spu;
116         }
117 }
118
119 void
120 AudioGhostRegion::set_height ()
121 {
122         gdouble ht;
123         vector<WaveView*>::iterator i;
124         uint32_t n;
125
126         GhostRegion::set_height();
127
128         ht = ((trackview.current_height()) / (double) waves.size());
129
130         for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
131                 gdouble yoff = n * ht;
132                 (*i)->property_height() = ht;
133                 (*i)->property_y() = yoff;
134         }
135 }
136
137 void
138 AudioGhostRegion::set_colors ()
139 {
140         GhostRegion::set_colors();
141         guint fill_color;
142
143         if (is_automation_ghost()) {
144                 fill_color = ARDOUR_UI::config()->canvasvar_GhostTrackWaveFill.get();
145         }
146         else {
147                 fill_color = source_track_color(200);
148         }
149
150         for (uint32_t n=0; n < waves.size(); ++n) {
151                 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
152                 waves[n]->property_fill_color() = fill_color;
153                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWaveClip.get();
154                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_GhostTrackZeroLine.get();
155         }
156 }
157
158 /** The general constructor; called when the destination timeaxisview doesn't have
159  *  a midistreamview.
160  *
161  *  @param tv TimeAxisView that this ghost region is on.
162  *  @param source_tv TimeAxisView that we are the ghost for.
163  */
164 MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
165         : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
166         , _optimization_iterator (events.end ())
167 {
168         base_rect->lower_to_bottom();
169         update_range ();
170
171         midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
172 }
173
174 /**
175  *  @param msv MidiStreamView that this ghost region is on.
176  *  @param source_tv TimeAxisView that we are the ghost for.
177  */
178 MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
179         : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
180         , _optimization_iterator (events.end ())
181 {
182         base_rect->lower_to_bottom();
183         update_range ();
184
185         midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
186 }
187
188 MidiGhostRegion::~MidiGhostRegion()
189 {
190         //clear_events();
191 }
192
193 MidiGhostRegion::Event::Event(ArdourCanvas::CanvasNoteEvent* e)
194         : event(e)
195 {
196 }
197
198 MidiGhostRegion::Note::Note(ArdourCanvas::CanvasNote* n, ArdourCanvas::Group* g)
199         : Event(n)
200 {
201         rect = new ArdourCanvas::SimpleRect(*g, n->x1(), n->y1(), n->x2(), n->y2());
202 }
203
204 MidiGhostRegion::Note::~Note()
205 {
206         //delete rect;
207 }
208
209 MidiGhostRegion::Hit::Hit(ArdourCanvas::CanvasHit* h, ArdourCanvas::Group*)
210         : Event(h)
211 {
212         cerr << "Hit ghost item does not work yet" << endl;
213 }
214
215 MidiGhostRegion::Hit::~Hit()
216 {
217 }
218
219 void
220 MidiGhostRegion::set_samples_per_unit (double /*spu*/)
221 {
222 }
223
224 /** @return MidiStreamView that we are providing a ghost for */
225 MidiStreamView*
226 MidiGhostRegion::midi_view ()
227 {
228         StreamView* sv = source_trackview.view ();
229         assert (sv);
230         MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
231         assert (msv);
232
233         return msv;
234 }
235
236 void
237 MidiGhostRegion::set_height ()
238 {
239         GhostRegion::set_height();
240         update_range();
241 }
242
243 void
244 MidiGhostRegion::set_colors()
245 {
246         MidiGhostRegion::Note* note;
247         guint fill = source_track_color(200);
248
249         GhostRegion::set_colors();
250
251         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
252                 if ((note = dynamic_cast<MidiGhostRegion::Note*>(*it)) != 0) {
253                         note->rect->property_fill_color_rgba() = fill;
254                         note->rect->property_outline_color_rgba() =  ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
255                 }
256         }
257 }
258
259 void
260 MidiGhostRegion::update_range ()
261 {
262         MidiStreamView* mv = midi_view();
263
264         if (!mv) {
265                 return;
266         }
267
268         MidiGhostRegion::Note* note;
269         double const h = trackview.current_height() / double (mv->contents_note_range ());
270
271         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
272                 if ((note = dynamic_cast<MidiGhostRegion::Note*>(*it)) != 0) {
273                         uint8_t const note_num = note->event->note()->note();
274
275                         if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
276                                 note->rect->hide();
277                         } else {
278                                 note->rect->show();
279                                 double const y = trackview.current_height() - (note_num + 1 - mv->lowest_note()) * h + 1;
280                                 note->rect->property_y1() = y;
281                                 note->rect->property_y2() = y + h;
282                         }
283                 }
284         }
285 }
286
287 void
288 MidiGhostRegion::add_note(ArdourCanvas::CanvasNote* n)
289 {
290         Note* note = new Note(n, group);
291         events.push_back(note);
292
293         note->rect->property_fill_color_rgba() = source_track_color(200);
294         note->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
295
296         MidiStreamView* mv = midi_view();
297
298         if (mv) {
299                 const uint8_t note_num = n->note()->note();
300
301                 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
302                         note->rect->hide();
303                 } else {
304                         const double y = mv->note_to_y(note_num);
305                         note->rect->property_y1() = y;
306                         note->rect->property_y2() = y + mv->note_height();
307                 }
308         }
309 }
310
311 void
312 MidiGhostRegion::add_hit(ArdourCanvas::CanvasHit* /*h*/)
313 {
314         //events.push_back(new Hit(h, group));
315 }
316
317 void
318 MidiGhostRegion::clear_events()
319 {
320         for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
321                 delete *it;
322         }
323
324         events.clear();
325 }
326
327 /** Update the x positions of our representation of a parent's note.
328  *  @param parent The CanvasNote from the parent MidiRegionView.
329  */
330 void
331 MidiGhostRegion::update_note (ArdourCanvas::CanvasNote* parent)
332 {
333         Event* ev = find_event (parent);
334         if (!ev) {
335                 return;
336         }
337
338         Note* note = dynamic_cast<Note *> (ev);
339         if (note) {
340                 double const x1 = parent->property_x1 ();
341                 double const x2 = parent->property_x2 ();
342                 note->rect->property_x1 () = x1;
343                 note->rect->property_x2 () = x2;
344         }
345 }
346
347 /** Given a note in our parent region (ie the actual MidiRegionView), find our
348  *  representation of it.
349  *  @return Our Event, or 0 if not found.
350  */
351
352 MidiGhostRegion::Event *
353 MidiGhostRegion::find_event (ArdourCanvas::CanvasNote* parent)
354 {
355         /* we are using _optimization_iterator to speed up the common case where a caller
356            is going through our notes in order.
357         */
358         
359         if (_optimization_iterator != events.end()) {
360                 ++_optimization_iterator;
361         }
362
363         if (_optimization_iterator != events.end() && (*_optimization_iterator)->event == parent) {
364                 return *_optimization_iterator;
365         }
366
367         for (_optimization_iterator = events.begin(); _optimization_iterator != events.end(); ++_optimization_iterator) {
368                 if ((*_optimization_iterator)->event == parent) {
369                         return *_optimization_iterator;
370                 }
371         }
372
373         return 0;
374 }