2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include "ardour/parameter_descriptor.h"
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"
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"
37 #include "ui_config.h"
40 using namespace Editing;
41 using namespace ArdourCanvas;
42 using namespace ARDOUR;
44 GhostRegion::GhostRegion(RegionView& rv,
45 ArdourCanvas::Container* parent,
47 TimeAxisView& source_tv,
51 , source_trackview(source_tv)
53 group = new ArdourCanvas::Container (parent);
54 CANVAS_DEBUG_NAME (group, "ghost region");
55 group->set_position (ArdourCanvas::Duple (initial_pos, 0));
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);
64 if (!is_automation_ghost()) {
68 GhostRegion::set_colors();
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 ();
75 GhostRegion::~GhostRegion ()
77 parent_rv.remove_ghost(this);
78 trackview.erase_ghost(this);
84 GhostRegion::set_duration (double units)
86 base_rect->set_x1 (units);
90 GhostRegion::set_height ()
92 base_rect->set_y1 (trackview.current_height());
96 GhostRegion::set_colors ()
98 if (is_automation_ghost()) {
99 base_rect->set_fill_color (UIConfiguration::instance().color_mod ("ghost track base", "ghost track base"));
104 GhostRegion::source_track_color(unsigned char alpha)
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);
111 GhostRegion::is_automation_ghost()
113 return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
116 AudioGhostRegion::AudioGhostRegion(RegionView& rv,
118 TimeAxisView& source_tv,
119 double initial_unit_pos)
120 : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
126 AudioGhostRegion::set_samples_per_pixel (double fpp)
128 for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
129 (*i)->set_samples_per_pixel (fpp);
134 AudioGhostRegion::set_height ()
136 vector<WaveView*>::iterator i;
139 GhostRegion::set_height();
141 double const ht = ((trackview.current_height()) / (double) waves.size());
143 for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
144 (*i)->set_height (ht);
145 (*i)->set_y_position (n * ht);
150 AudioGhostRegion::set_colors ()
152 GhostRegion::set_colors();
155 if (is_automation_ghost()) {
156 fill_color = UIConfiguration::instance().color ("ghost track wave fill");
159 fill_color = source_track_color(200);
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"));
170 /** The general constructor; called when the destination timeaxisview doesn't have
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.
177 MidiGhostRegion::MidiGhostRegion(RegionView& rv,
179 TimeAxisView& source_tv,
180 double initial_unit_pos)
181 : GhostRegion(rv, tv.ghost_group(), tv, source_tv, initial_unit_pos)
182 , _note_group (new ArdourCanvas::Container (group))
183 , _optimization_iterator(events.end())
185 _outline = UIConfiguration::instance().color ("ghost track midi outline");
187 base_rect->lower_to_bottom();
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.
195 MidiGhostRegion::MidiGhostRegion(RegionView& rv,
197 TimeAxisView& source_tv,
198 double initial_unit_pos)
200 msv.midi_underlay_group,
204 , _note_group (new ArdourCanvas::Container (group))
205 , _optimization_iterator(events.end())
207 _outline = UIConfiguration::instance().color ("ghost track midi outline");
209 base_rect->lower_to_bottom();
212 MidiGhostRegion::~MidiGhostRegion()
218 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
222 if (dynamic_cast<Note*>(e)) {
223 item = new ArdourCanvas::Rectangle(
224 g, ArdourCanvas::Rect(e->x0(), e->y0(), e->x1(), e->y1()));
226 Hit* hit = dynamic_cast<Hit*>(e);
230 ArdourCanvas::Polygon* poly = new ArdourCanvas::Polygon(g);
231 poly->set(Hit::points(e->y1() - e->y0()));
232 poly->set_position(hit->position());
236 CANVAS_DEBUG_NAME (item, "ghost note item");
239 MidiGhostRegion::GhostEvent::~GhostEvent ()
241 /* event is not ours to delete */
246 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
250 /** @return MidiStreamView that we are providing a ghost for */
252 MidiGhostRegion::midi_view ()
254 StreamView* sv = source_trackview.view ();
256 MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
263 MidiGhostRegion::set_height ()
265 GhostRegion::set_height();
266 update_contents_height ();
270 MidiGhostRegion::set_colors()
272 GhostRegion::set_colors();
273 _outline = UIConfiguration::instance().color ("ghost track midi outline");
275 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
276 (*it).second->item->set_fill_color (UIConfiguration::instance().color_mod((*it).second->event->base_color(), "ghost track midi fill"));
277 (*it).second->item->set_outline_color (_outline);
282 note_height(TimeAxisView& trackview, MidiStreamView* mv)
284 const double tv_height = trackview.current_height();
285 const double note_range = mv->contents_note_range();
287 return std::max(1.0, floor(tv_height / note_range - 1.0));
291 note_y(TimeAxisView& trackview, MidiStreamView* mv, uint8_t note_num)
293 const double tv_height = trackview.current_height();
294 const double note_range = mv->contents_note_range();
295 const double s = tv_height / note_range;
297 return tv_height - (note_num + 1 - mv->lowest_note()) * s;
301 MidiGhostRegion::update_contents_height ()
303 MidiStreamView* mv = midi_view();
309 double const h = note_height(trackview, mv);
311 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
312 uint8_t const note_num = (*it).second->event->note()->note();
314 double const y = note_y(trackview, mv, note_num);
316 if ((_tmp_rect = dynamic_cast<ArdourCanvas::Rectangle*>((*it).second->item))) {
317 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
318 } else if ((_tmp_poly = dynamic_cast<ArdourCanvas::Polygon*>((*it).second->item))) {
319 Duple position = _tmp_poly->position();
321 _tmp_poly->set_position(position);
322 _tmp_poly->set(Hit::points(h));
328 MidiGhostRegion::add_note (NoteBase* n)
330 GhostEvent* event = new GhostEvent (n, _note_group);
331 events.insert (make_pair (n->note(), event));
333 event->item->set_fill_color (UIConfiguration::instance().color_mod(n->base_color(), "ghost track midi fill"));
334 event->item->set_outline_color (_outline);
336 MidiStreamView* mv = midi_view();
339 uint8_t const note_num = n->note()->note();
340 double const h = note_height(trackview, mv);
341 double const y = note_y(trackview, mv, note_num);
342 if (n->x0() < base_rect->x0() || n->x1() > base_rect->x1()) {
344 } else if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
347 if ((_tmp_rect = dynamic_cast<ArdourCanvas::Rectangle*>(event->item))) {
348 _tmp_rect->set (ArdourCanvas::Rect (_tmp_rect->x0(), y, _tmp_rect->x1(), y + h));
349 } else if ((_tmp_poly = dynamic_cast<ArdourCanvas::Polygon*>(event->item))) {
350 Duple position = _tmp_poly->position();
352 _tmp_poly->set_position(position);
353 _tmp_poly->set(Hit::points(h));
360 MidiGhostRegion::clear_events()
362 _note_group->clear (true);
364 _optimization_iterator = events.end();
367 /** Update the x positions of our representation of a parent's note.
368 * @param parent The CanvasNote from the parent MidiRegionView.
371 MidiGhostRegion::update_note (Note* note, bool hide)
373 MidiStreamView* mv = midi_view();
379 GhostEvent* ev = find_event (note);
385 uint8_t const note_num = note->note()->note();
387 double const y = note_y(trackview, mv, note_num);
388 double const h = note_height(trackview, mv);
393 if ((_tmp_rect = dynamic_cast<ArdourCanvas::Rectangle*>(ev->item))) {
394 _tmp_rect->set (ArdourCanvas::Rect (note->x0(), y, note->x1(), y + h));
400 /** Update the x positions of our representation of a parent's hit.
401 * @param hit The CanvasHit from the parent MidiRegionView.
404 MidiGhostRegion::update_hit (Hit* hit, bool hide)
406 MidiStreamView* mv = midi_view();
412 GhostEvent* ev = find_event (hit);
418 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);
425 if ((_tmp_poly = dynamic_cast<ArdourCanvas::Polygon*>(ev->item))) {
426 ArdourCanvas::Duple ppos = hit->position();
427 ArdourCanvas::Duple gpos = _tmp_poly->position();
430 _tmp_poly->set_position(gpos);
431 _tmp_poly->set(Hit::points(h));
438 MidiGhostRegion::remove_note (NoteBase* note)
440 EventList::iterator f = events.find (note->note());
441 if (f == events.end()) {
448 _optimization_iterator = events.end ();
451 /** Given a note in our parent region (ie the actual MidiRegionView), find our
452 * representation of it.
453 * @return Our Event, or 0 if not found.
456 MidiGhostRegion::GhostEvent *
457 MidiGhostRegion::find_event (NoteBase* parent)
459 /* we are using _optimization_iterator to speed up the common case where a caller
460 is going through our notes in order.
463 if (_optimization_iterator != events.end()) {
464 ++_optimization_iterator;
465 if (_optimization_iterator != events.end() && (*_optimization_iterator).second->event == parent) {
466 return (*_optimization_iterator).second;
470 _optimization_iterator = events.find (parent->note());
471 if (_optimization_iterator != events.end()) {
472 return (*_optimization_iterator).second;