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