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