+
+void
+MidiRegionView::update_ghost_note (double x, double y)
+{
+ _last_ghost_x = x;
+ _last_ghost_y = y;
+
+ _note_group->w2i (x, y);
+ framepos_t f = trackview.editor().pixel_to_frame (x) + _region->position ();
+ trackview.editor().snap_to (f);
+ f -= _region->position ();
+
+ bool success;
+ Evoral::MusicalTime beats = trackview.editor().get_grid_type_as_beats (success, f);
+
+ if (!success) {
+ beats = 1;
+ }
+
+ double length = frames_to_beats (snap_frame_to_frame (f + beats_to_frames (beats)) - f);
+
+ _ghost_note->note()->set_time (frames_to_beats (f + _region->start()));
+ _ghost_note->note()->set_length (length);
+ _ghost_note->note()->set_note (midi_stream_view()->y_to_note (y));
+
+ /* the ghost note does not appear in ghost regions, so pass false in here */
+ update_note (_ghost_note, false);
+
+ show_verbose_canvas_cursor (_ghost_note->note ());
+}
+
+void
+MidiRegionView::create_ghost_note (double x, double y)
+{
+ delete _ghost_note;
+ _ghost_note = 0;
+
+ boost::shared_ptr<NoteType> g (new NoteType);
+ _ghost_note = new NoEventCanvasNote (*this, *_note_group, g);
+ update_ghost_note (x, y);
+ _ghost_note->show ();
+
+ _last_ghost_x = x;
+ _last_ghost_y = y;
+
+ show_verbose_canvas_cursor (_ghost_note->note ());
+}
+
+void
+MidiRegionView::snap_changed ()
+{
+ if (!_ghost_note) {
+ return;
+ }
+
+ create_ghost_note (_last_ghost_x, _last_ghost_y);
+}
+
+void
+MidiRegionView::show_verbose_canvas_cursor (boost::shared_ptr<NoteType> n) const
+{
+ char buf[24];
+ snprintf (buf, sizeof (buf), "%s (%d)\nVel %d",
+ Evoral::midi_note_name (n->note()).c_str(),
+ (int) n->note (),
+ (int) n->velocity());
+ trackview.editor().show_verbose_canvas_cursor_with (buf, 10, 20);
+}
+
+void
+MidiRegionView::drop_down_keys ()
+{
+ _mouse_state = None;
+}
+
+void
+MidiRegionView::maybe_select_by_position (GdkEventButton* ev, double /*x*/, double y)
+{
+ double note = midi_stream_view()->y_to_note(y);
+ Events e;
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+
+ uint16_t chn_mask = mtv->channel_selector().get_selected_channels();
+
+ if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
+ get_events (e, Evoral::Sequence<Evoral::MusicalTime>::PitchGreaterThanOrEqual, (uint8_t) floor (note), chn_mask);
+ } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
+ get_events (e, Evoral::Sequence<Evoral::MusicalTime>::PitchLessThanOrEqual, (uint8_t) floor (note), chn_mask);
+ } else {
+ return;
+ }
+
+ bool add_mrv_selection = false;
+
+ if (_selection.empty()) {
+ add_mrv_selection = true;
+ }
+
+ for (Events::iterator i = e.begin(); i != e.end(); ++i) {
+ if (_selection.insert (*i).second) {
+ (*i)->set_selected (true);
+ }
+ }
+
+ if (add_mrv_selection) {
+ PublicEditor& editor (trackview.editor());
+ editor.get_selection().add (this);
+ }
+}
+
+void
+MidiRegionView::color_handler ()
+{
+ RegionView::color_handler ();
+
+ for (Events::iterator i = _events.begin(); i != _events.end(); ++i) {
+ (*i)->set_selected ((*i)->selected()); // will change color
+ }
+
+ /* XXX probably more to do here */
+}
+
+void
+MidiRegionView::enable_display (bool yn)
+{
+ RegionView::enable_display (yn);
+ if (yn) {
+ redisplay_model ();
+ }
+}
+
+void
+MidiRegionView::show_step_edit_cursor (Evoral::MusicalTime pos)
+{
+ if (_step_edit_cursor == 0) {
+ ArdourCanvas::Group* const group = (ArdourCanvas::Group*)get_canvas_group();
+
+ _step_edit_cursor = new ArdourCanvas::SimpleRect (*group);
+ _step_edit_cursor->property_y1() = 0;
+ _step_edit_cursor->property_y2() = midi_stream_view()->contents_height();
+ _step_edit_cursor->property_fill_color_rgba() = RGBA_TO_UINT (45,0,0,90);
+ _step_edit_cursor->property_outline_color_rgba() = RGBA_TO_UINT (85,0,0,90);
+ }
+
+ move_step_edit_cursor (pos);
+ _step_edit_cursor->show ();
+}
+
+void
+MidiRegionView::move_step_edit_cursor (Evoral::MusicalTime pos)
+{
+ _step_edit_cursor_position = pos;
+
+ if (_step_edit_cursor) {
+ double pixel = trackview.editor().frame_to_pixel (beats_to_frames (pos));
+ _step_edit_cursor->property_x1() = pixel;
+ set_step_edit_cursor_width (_step_edit_cursor_width);
+ }
+}
+
+void
+MidiRegionView::hide_step_edit_cursor ()
+{
+ if (_step_edit_cursor) {
+ _step_edit_cursor->hide ();
+ }
+}
+
+void
+MidiRegionView::set_step_edit_cursor_width (Evoral::MusicalTime beats)
+{
+ _step_edit_cursor_width = beats;
+
+ if (_step_edit_cursor) {
+ _step_edit_cursor->property_x2() = _step_edit_cursor->property_x1() + trackview.editor().frame_to_pixel (beats_to_frames (beats));
+ }
+}
+
+/** Called when a diskstream on our track has received some data. Update the view, if applicable.
+ * @param buf Data that has been recorded.
+ * @param w Source that this data will end up in.
+ */
+void
+MidiRegionView::data_recorded (boost::shared_ptr<MidiBuffer> buf, boost::weak_ptr<MidiSource> w)
+{
+ if (!_active_notes) {
+ /* we aren't actively being recorded to */
+ return;
+ }
+
+ boost::shared_ptr<MidiSource> src = w.lock ();
+ if (!src || src != midi_region()->midi_source()) {
+ /* recorded data was not destined for our source */
+ return;
+ }
+
+ MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*> (&trackview);
+ BeatsFramesConverter converter (trackview.session()->tempo_map(), mtv->midi_track()->get_capture_start_frame (0));
+
+ framepos_t back = max_framepos;
+
+ for (MidiBuffer::iterator i = buf->begin(); i != buf->end(); ++i) {
+ Evoral::MIDIEvent<MidiBuffer::TimeType> const ev (*i, false);
+ assert (ev.buffer ());
+
+ Evoral::MusicalTime const time_beats = converter.from (ev.time () - converter.origin_b ());
+
+ if (ev.type() == MIDI_CMD_NOTE_ON) {
+
+ boost::shared_ptr<Evoral::Note<Evoral::MusicalTime> > note (
+ new Evoral::Note<Evoral::MusicalTime> (ev.channel(), time_beats, 0, ev.note(), ev.velocity())
+ );
+
+ add_note (note, true);
+
+ /* fix up our note range */
+ if (ev.note() < _current_range_min) {
+ midi_stream_view()->apply_note_range (ev.note(), _current_range_max, true);
+ } else if (ev.note() > _current_range_max) {
+ midi_stream_view()->apply_note_range (_current_range_min, ev.note(), true);
+ }
+
+ } else if (ev.type() == MIDI_CMD_NOTE_OFF) {
+ resolve_note (ev.note (), time_beats);
+ }
+
+ back = ev.time ();
+ }
+
+ midi_stream_view()->check_record_layers (region(), back);
+}
+
+void
+MidiRegionView::trim_front_starting ()
+{
+ /* Reparent the note group to the region view's parent, so that it doesn't change
+ when the region view is trimmed.
+ */
+ _temporary_note_group = new ArdourCanvas::Group (*group->property_parent ());
+ _temporary_note_group->move (group->property_x(), group->property_y());
+ _note_group->reparent (*_temporary_note_group);
+}
+
+void
+MidiRegionView::trim_front_ending ()
+{
+ _note_group->reparent (*group);
+ delete _temporary_note_group;
+ _temporary_note_group = 0;
+
+ if (_region->start() < 0) {
+ /* Trim drag made start time -ve; fix this */
+ midi_region()->fix_negative_start ();
+ }
+}
+
+/** @return channel (counted from 0) to add an event to, based on the current setting
+ * of the channel selector.
+ */
+uint8_t
+MidiRegionView::get_channel_for_add () const
+{
+ MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+ uint16_t const chn_mask = mtv->channel_selector().get_selected_channels();
+ int chn_cnt = 0;
+ uint8_t channel = 0;
+
+ /* pick the highest selected channel, unless all channels are selected,
+ which is interpreted to mean channel 1 (zero)
+ */
+
+ for (uint16_t i = 0; i < 16; ++i) {
+ if (chn_mask & (1<<i)) {
+ channel = i;
+ chn_cnt++;
+ }
+ }
+
+ if (chn_cnt == 16) {
+ channel = 0;
+ }
+
+ return channel;
+}
+
+void
+MidiRegionView::edit_patch_change (ArdourCanvas::CanvasPatchChange* pc)
+{
+ PatchChangeDialog d (&_time_converter, trackview.session(), *pc->patch (), Gtk::Stock::APPLY);
+ if (d.run () != Gtk::RESPONSE_ACCEPT) {
+ return;
+ }
+
+ change_patch_change (pc->patch(), d.patch ());
+}