+/** The general constructor; called when the destination timeaxisview doesn't have
+ * a midistreamview.
+ *
+ * @param tv TimeAxisView that this ghost region is on.
+ * @param source_tv TimeAxisView that we are the ghost for.
+ */
+MidiGhostRegion::MidiGhostRegion(TimeAxisView& tv, TimeAxisView& source_tv, double initial_unit_pos)
+ : GhostRegion(tv.ghost_group(), tv, source_tv, initial_unit_pos)
+ , _optimization_iterator (events.end ())
+{
+ base_rect->lower_to_bottom();
+ update_range ();
+
+ midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
+}
+
+/**
+ * @param msv MidiStreamView that this ghost region is on.
+ * @param source_tv TimeAxisView that we are the ghost for.
+ */
+MidiGhostRegion::MidiGhostRegion(MidiStreamView& msv, TimeAxisView& source_tv, double initial_unit_pos)
+ : GhostRegion(msv.midi_underlay_group, msv.trackview(), source_tv, initial_unit_pos)
+ , _optimization_iterator (events.end ())
+{
+ base_rect->lower_to_bottom();
+ update_range ();
+
+ midi_view()->NoteRangeChanged.connect (sigc::mem_fun (*this, &MidiGhostRegion::update_range));
+}
+
+MidiGhostRegion::~MidiGhostRegion()
+{
+ clear_events ();
+}
+
+MidiGhostRegion::Event::Event (ArdourCanvas::CanvasNoteEvent* e, ArdourCanvas::Group* g)
+ : event (e)
+{
+ rect = new ArdourCanvas::SimpleRect (*g, e->x1(), e->y1(), e->x2(), e->y2());
+}
+
+MidiGhostRegion::Event::~Event ()
+{
+ /* event is not ours to delete */
+ delete rect;
+}
+
+void
+MidiGhostRegion::set_samples_per_unit (double /*spu*/)
+{
+}
+
+/** @return MidiStreamView that we are providing a ghost for */
+MidiStreamView*
+MidiGhostRegion::midi_view ()
+{
+ StreamView* sv = source_trackview.view ();
+ assert (sv);
+ MidiStreamView* msv = dynamic_cast<MidiStreamView*> (sv);
+ assert (msv);
+
+ return msv;
+}
+
+void
+MidiGhostRegion::set_height ()
+{
+ GhostRegion::set_height();
+ update_range();
+}
+
+void
+MidiGhostRegion::set_colors()
+{
+ guint fill = source_track_color(200);
+
+ GhostRegion::set_colors();
+
+ for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
+ (*it)->rect->property_fill_color_rgba() = fill;
+ (*it)->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
+ }
+}
+
+void
+MidiGhostRegion::update_range ()
+{
+ MidiStreamView* mv = midi_view();
+
+ if (!mv) {
+ return;
+ }
+
+ double const h = trackview.current_height() / double (mv->contents_note_range ());
+
+ for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
+ uint8_t const note_num = (*it)->event->note()->note();
+
+ if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
+ (*it)->rect->hide();
+ } else {
+ (*it)->rect->show();
+ double const y = trackview.current_height() - (note_num + 1 - mv->lowest_note()) * h + 1;
+ (*it)->rect->property_y1() = y;
+ (*it)->rect->property_y2() = y + h;
+ }
+ }
+}
+
+void
+MidiGhostRegion::add_note(ArdourCanvas::CanvasNote* n)
+{
+ Event* event = new Event (n, group);
+ events.push_back (event);
+
+ event->rect->property_fill_color_rgba() = source_track_color(200);
+ event->rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_GhostTrackMidiOutline.get();
+
+ MidiStreamView* mv = midi_view();
+
+ if (mv) {
+ const uint8_t note_num = n->note()->note();
+
+ if (note_num < mv->lowest_note() || note_num > mv->highest_note()) {
+ event->rect->hide();
+ } else {
+ const double y = mv->note_to_y(note_num);
+ event->rect->property_y1() = y;
+ event->rect->property_y2() = y + mv->note_height();
+ }
+ }
+}
+
+void
+MidiGhostRegion::clear_events()
+{
+ for (EventList::iterator it = events.begin(); it != events.end(); ++it) {
+ delete *it;
+ }
+
+ events.clear();
+ _optimization_iterator = events.end ();
+}
+
+/** Update the x positions of our representation of a parent's note.
+ * @param parent The CanvasNote from the parent MidiRegionView.
+ */
+void
+MidiGhostRegion::update_note (ArdourCanvas::CanvasNote* parent)
+{
+ Event* ev = find_event (parent);
+ if (!ev) {
+ return;
+ }
+
+ double const x1 = parent->property_x1 ();
+ double const x2 = parent->property_x2 ();
+ ev->rect->property_x1 () = x1;
+ ev->rect->property_x2 () = x2;
+}
+
+void
+MidiGhostRegion::remove_note (ArdourCanvas::CanvasNoteEvent* note)
+{
+ Event* ev = find_event (note);
+ if (!ev) {
+ return;
+ }
+
+ events.remove (ev);
+ delete ev;
+ _optimization_iterator = events.end ();
+}
+
+/** Given a note in our parent region (ie the actual MidiRegionView), find our
+ * representation of it.
+ * @return Our Event, or 0 if not found.
+ */
+
+MidiGhostRegion::Event *
+MidiGhostRegion::find_event (ArdourCanvas::CanvasNoteEvent* parent)
+{
+ /* we are using _optimization_iterator to speed up the common case where a caller
+ is going through our notes in order.
+ */
+
+ if (_optimization_iterator != events.end()) {
+ ++_optimization_iterator;
+ }
+
+ if (_optimization_iterator != events.end() && (*_optimization_iterator)->event == parent) {
+ return *_optimization_iterator;
+ }
+
+ for (_optimization_iterator = events.begin(); _optimization_iterator != events.end(); ++_optimization_iterator) {
+ if ((*_optimization_iterator)->event == parent) {
+ return *_optimization_iterator;
+ }
+ }