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 "evoral/Note.hpp"
21 #include "canvas/container.h"
22 #include "canvas/polygon.h"
23 #include "canvas/rectangle.h"
24 #include "canvas/wave_view.h"
25 #include "canvas/debug.h"
27 #include "ardour_ui.h"
28 #include "automation_time_axis.h"
29 #include "ghostregion.h"
30 #include "midi_streamview.h"
31 #include "midi_time_axis.h"
32 #include "rgb_macros.h"
37 using namespace Editing;
38 using namespace ArdourCanvas;
39 using namespace ARDOUR;
41 PBD::Signal1<void,GhostRegion*> GhostRegion::CatchDeletion;
43 GhostRegion::GhostRegion (ArdourCanvas::Container* parent, TimeAxisView& tv, TimeAxisView& source_tv, double initial_pos)
45 , source_trackview (source_tv)
47 group = new ArdourCanvas::Container (parent);
48 CANVAS_DEBUG_NAME (group, "ghost region");
49 group->set_position (ArdourCanvas::Duple (initial_pos, 0));
51 base_rect = new ArdourCanvas::Rectangle (group);
52 CANVAS_DEBUG_NAME (base_rect, "ghost region rect");
53 base_rect->set_x0 (0);
54 base_rect->set_y0 (1.0);
55 base_rect->set_y1 (trackview.current_height());
56 base_rect->set_outline (false);
58 if (!is_automation_ghost()) {
62 GhostRegion::set_colors();
64 /* the parent group of a ghostregion is a dedicated group for ghosts,
65 so the new ghost would want to get to the top of that group*/
66 group->raise_to_top ();
69 GhostRegion::~GhostRegion ()
77 GhostRegion::set_duration (double units)
79 base_rect->set_x1 (units);
83 GhostRegion::set_height ()
85 base_rect->set_y1 (trackview.current_height());
89 GhostRegion::set_colors ()
91 if (is_automation_ghost()) {
92 base_rect->set_fill_color (ARDOUR_UI::config()->color_mod ("ghost track base", "ghost track base"));
97 GhostRegion::source_track_color(unsigned char alpha)
99 Gdk::Color color = source_trackview.color();
100 return RGBA_TO_UINT (color.get_red() / 256, color.get_green() / 256, color.get_blue() / 256, alpha);
104 GhostRegion::is_automation_ghost()
106 return (dynamic_cast<AutomationTimeAxisView*>(&trackview)) != 0;
109 AudioGhostRegion::AudioGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
110 : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
116 AudioGhostRegion::set_samples_per_pixel (double fpp)
118 for (vector<WaveView*>::iterator i = waves.begin(); i != waves.end(); ++i) {
119 (*i)->set_samples_per_pixel (fpp);
124 AudioGhostRegion::set_height ()
126 vector<WaveView*>::iterator i;
129 GhostRegion::set_height();
131 double const ht = ((trackview.current_height()) / (double) waves.size());
133 for (n = 0, i = waves.begin(); i != waves.end(); ++i, ++n) {
134 (*i)->set_height (ht);
135 (*i)->set_y_position (n * ht);
140 AudioGhostRegion::set_colors ()
142 GhostRegion::set_colors();
145 if (is_automation_ghost()) {
146 fill_color = ARDOUR_UI::config()->color ("ghost track wave fill");
149 fill_color = source_track_color(200);
152 for (uint32_t n=0; n < waves.size(); ++n) {
153 waves[n]->set_outline_color (ARDOUR_UI::config()->color ("ghost track wave"));
154 waves[n]->set_fill_color (fill_color);
155 waves[n]->set_clip_color (ARDOUR_UI::config()->color ("ghost track wave clip"));
156 waves[n]->set_zero_color (ARDOUR_UI::config()->color ("ghost track zero line"));
160 /** The general constructor; called when the destination timeaxisview doesn't have
163 * @param tv TimeAxisView that this ghost region is on.
164 * @param source_tv TimeAxisView that we are the ghost for.
166 MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
167 : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
168 , _optimization_iterator (events.end ())
170 base_rect->lower_to_bottom();
173 midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
177 * @param msv MidiStreamView that this ghost region is on.
178 * @param source_tv TimeAxisView that we are the ghost for.
180 MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
181 : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
182 , _optimization_iterator (events.end ())
184 base_rect->lower_to_bottom();
187 midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
190 MidiGhostRegion::~MidiGhostRegion()
195 MidiGhostRegion::GhostEvent::GhostEvent (NoteBase* e, ArdourCanvas::Container* g)
199 if (dynamic_cast<Note*>(e)) {
200 item = new ArdourCanvas::Rectangle(
201 g, ArdourCanvas::Rect(e->x0(), e->y0(), e->x1(), e->y1()));
202 } else if ((hit = dynamic_cast<Hit*>(e))) {
203 ArdourCanvas::Polygon* poly = new ArdourCanvas::Polygon(g);
204 poly->set(Hit::points(e->y1() - e->y0()));
205 poly->set_position(hit->position());
209 CANVAS_DEBUG_NAME (item, "ghost note item");
212 MidiGhostRegion::GhostEvent::~GhostEvent ()
214 /* event is not ours to delete */
219 MidiGhostRegion::set_samples_per_pixel (double /*spu*/)
223 /** @return MidiStreamView that we are providing a ghost for */
225 MidiGhostRegion::midi_view ()
227 StreamView* sv = source_trackview.view ();
229 MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
236 MidiGhostRegion::set_height ()
238 GhostRegion::set_height();
243 MidiGhostRegion::set_colors()
245 GhostRegion::set_colors();
247 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
248 (*it)->item->set_fill_color (ARDOUR_UI::config()->color_mod((*it)->event->base_color(), "ghost track midi fill"));
249 (*it)->item->set_outline_color (ARDOUR_UI::config()->color ("ghost track midi outline"));
254 note_height(TimeAxisView& trackview, MidiStreamView* mv)
256 const double tv_height = trackview.current_height();
257 const double note_range = mv->contents_note_range();
259 return std::max(1.0, floor(tv_height / note_range - 1.0));
263 note_y(TimeAxisView& trackview, MidiStreamView* mv, uint8_t note_num)
265 const double tv_height = trackview.current_height();
266 const double note_range = mv->contents_note_range();
267 const double s = tv_height / note_range;
269 return tv_height - (note_num + 1 - mv->lowest_note()) * s;
273 MidiGhostRegion::update_range ()
275 MidiStreamView* mv = midi_view();
281 double const h = note_height(trackview, mv);
283 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
284 uint8_t const note_num = (*it)->event->note()->note();
286 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
290 double const y = note_y(trackview, mv, note_num);
291 ArdourCanvas::Rectangle* rect = NULL;
292 ArdourCanvas::Polygon* poly = NULL;
293 if ((rect = dynamic_cast<ArdourCanvas::Rectangle*>((*it)->item))) {
295 rect->set_y1 (y + h);
296 } else if ((poly = dynamic_cast<ArdourCanvas::Polygon*>((*it)->item))) {
297 Duple position = poly->position();
299 poly->set_position(position);
300 poly->set(Hit::points(h));
307 MidiGhostRegion::add_note (NoteBase* n)
309 GhostEvent* event = new GhostEvent (n, group);
310 events.push_back (event);
312 event->item->set_fill_color (ARDOUR_UI::config()->color_mod(n->base_color(), "ghost track midi fill"));
313 event->item->set_outline_color (ARDOUR_UI::config()->color ("ghost track midi outline"));
315 MidiStreamView* mv = midi_view();
318 uint8_t const note_num = n->note()->note();
319 double const h = note_height(trackview, mv);
320 double const y = note_y(trackview, mv, note_num);
322 if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
325 ArdourCanvas::Rectangle* rect = NULL;
326 ArdourCanvas::Polygon* poly = NULL;
327 if ((rect = dynamic_cast<ArdourCanvas::Rectangle*>(event->item))) {
329 rect->set_y1 (y + h);
330 } else if ((poly = dynamic_cast<ArdourCanvas::Polygon*>(event->item))) {
331 Duple position = poly->position();
333 poly->set_position(position);
334 poly->set(Hit::points(h));
341 MidiGhostRegion::clear_events()
343 for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
348 _optimization_iterator = events.end ();
351 /** Update the x positions of our representation of a parent's note.
352 * @param parent The CanvasNote from the parent MidiRegionView.
355 MidiGhostRegion::update_note (NoteBase* parent)
357 GhostEvent* ev = find_event (parent);
363 ArdourCanvas::Rectangle* rect = NULL;
365 ArdourCanvas::Polygon* poly = NULL;
366 if ((note = dynamic_cast<Note*>(parent))) {
367 if ((rect = dynamic_cast<ArdourCanvas::Rectangle*>(ev->item))) {
368 double const x1 = parent->x0 ();
369 double const x2 = parent->x1 ();
373 } else if ((hit = dynamic_cast<Hit*>(parent))) {
374 if ((poly = dynamic_cast<ArdourCanvas::Polygon*>(ev->item))) {
375 ArdourCanvas::Duple ppos = hit->position();
376 ArdourCanvas::Duple gpos = poly->position();
378 poly->set_position(gpos);
384 MidiGhostRegion::remove_note (NoteBase* note)
386 GhostEvent* ev = find_event (note);
393 _optimization_iterator = events.end ();
396 /** Given a note in our parent region (ie the actual MidiRegionView), find our
397 * representation of it.
398 * @return Our Event, or 0 if not found.
401 MidiGhostRegion::GhostEvent *
402 MidiGhostRegion::find_event (NoteBase* parent)
404 /* we are using _optimization_iterator to speed up the common case where a caller
405 is going through our notes in order.
408 if (_optimization_iterator != events.end()) {
409 ++_optimization_iterator;
412 if (_optimization_iterator != events.end() && (*_optimization_iterator)->event == parent) {
413 return *_optimization_iterator;
416 for (_optimization_iterator = events.begin(); _optimization_iterator != events.end(); ++_optimization_iterator) {
417 if ((*_optimization_iterator)->event == parent) {
418 return *_optimization_iterator;