Highlight note in piano roll - #6510
[ardour.git] / gtk2_ardour / midi_region_view.cc
index fda5aec72532f4b346a17ec895022295be49a704..917999405c9b52d4bf8859293f7166647c0edf6c 100644 (file)
@@ -485,6 +485,11 @@ MidiRegionView::leave_internal()
        if (frame_handle_end) {
                frame_handle_end->raise_to_top();
        }
+
+       MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+       if (mtv) {
+               mtv->set_note_highlight (NO_MIDI_NOTE);
+       }
 }
 
 bool
@@ -2624,22 +2629,25 @@ MidiRegionView::note_dropped(NoteBase *, frameoffset_t dt, int8_t dnote)
 }
 
 /** @param x Pixel relative to the region position.
+ *  @param ensure_snap defaults to false. true = snap always, ignoring snap mode and magnetic snap.
+ *  Used for inverting the snap logic with key modifiers and snap delta calculation.
  *  @return Snapped frame relative to the region position.
  */
 framepos_t
-MidiRegionView::snap_pixel_to_sample(double x, bool explicitly)
+MidiRegionView::snap_pixel_to_sample(double x, bool ensure_snap)
 {
        PublicEditor& editor (trackview.editor());
-       return snap_frame_to_frame (editor.pixel_to_sample (x), explicitly);
+       return snap_frame_to_frame (editor.pixel_to_sample (x), ensure_snap);
 }
 
 /** @param x Pixel relative to the region position.
+ *  @param ensure_snap defaults to false. true = ignore magnetic snap and snap mode (used for snap delta calculation).
  *  @return Snapped pixel relative to the region position.
  */
 double
-MidiRegionView::snap_to_pixel(double x, bool explicitly)
+MidiRegionView::snap_to_pixel(double x, bool ensure_snap)
 {
-       return (double) trackview.editor().sample_to_pixel(snap_pixel_to_sample(x, explicitly));
+       return (double) trackview.editor().sample_to_pixel(snap_pixel_to_sample(x, ensure_snap));
 }
 
 double
@@ -2745,9 +2753,10 @@ MidiRegionView::begin_resizing (bool /*at_front*/)
  * amount of the drag.  In non-relative mode, all selected notes are set to have the same start or end point
  * as the \a primary note.
  * @param snap_delta snap offset of the primary note in pixels. used in SnapRelative SnapDelta mode.
+ * @param with_snap true if snap is to be used to determine the position, false if no snap is to be used.
  */
 void
-MidiRegionView::update_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, guint key_state)
+MidiRegionView::update_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, bool with_snap)
 {
        bool cursor_set = false;
 
@@ -2780,46 +2789,47 @@ MidiRegionView::update_resizing (NoteBase* primary, bool at_front, double delta_
                }
 
                if (at_front) {
-                       if (ArdourKeyboard::indicates_snap (key_state)) {
-                               resize_rect->set_x0 (current_x - snap_delta);
-                       } else {
+                       if (with_snap) {
                                resize_rect->set_x0 (snap_to_pixel(current_x) - snap_delta);
+                       } else {
+                               resize_rect->set_x0 (current_x - snap_delta);
                        }
                        resize_rect->set_x1 (canvas_note->x1());
                } else {
-                       if (ArdourKeyboard::indicates_snap (key_state)) {
-                               resize_rect->set_x1 (current_x - snap_delta);
-                       } else {
+                       if (with_snap) {
                                resize_rect->set_x1 (snap_to_pixel(current_x) - snap_delta);
+                       } else {
+                               resize_rect->set_x1 (current_x - snap_delta);
                        }
                        resize_rect->set_x0 (canvas_note->x0());
                }
 
                if (!cursor_set) {
-                       /* snap delta is in pixels (sigh) */
-                       framepos_t delta_samps = trackview.editor().pixel_to_sample (snap_delta);
-                       double delta_beats;
+                       /* Convert snap delta from pixels to beats. */
+                       framepos_t snap_delta_samps = trackview.editor().pixel_to_sample (snap_delta);
+                       double snap_delta_beats = 0.0;
                        int sign = 1;
+
                        /* negative beat offsets aren't allowed */
-                       if (delta_samps > 0) {
-                               delta_beats = region_frames_to_region_beats_double (delta_samps);
-                       } else if (delta_samps < 0) {
-                               delta_beats = region_frames_to_region_beats_double ( - delta_samps);
+                       if (snap_delta_samps > 0) {
+                               snap_delta_beats = region_frames_to_region_beats_double (snap_delta_samps);
+                       } else if (snap_delta_samps < 0) {
+                               snap_delta_beats = region_frames_to_region_beats_double ( - snap_delta_samps);
                                sign = -1;
                        }
 
-                       const double  snapped_x = snap_pixel_to_sample (current_x);
+                       const double  snapped_x = (with_snap ? snap_pixel_to_sample (current_x) : trackview.editor ().pixel_to_sample (current_x));
                        Evoral::Beats beats     = region_frames_to_region_beats (snapped_x);
                        Evoral::Beats len       = Evoral::Beats();
 
                        if (at_front) {
                                if (beats < canvas_note->note()->end_time()) {
-                                       len = canvas_note->note()->time() - beats + (sign * delta_beats);
+                                       len = canvas_note->note()->time() - beats + (sign * snap_delta_beats);
                                        len += canvas_note->note()->length();
                                }
                        } else {
                                if (beats >= canvas_note->note()->time()) {
-                                       len = beats - canvas_note->note()->time() - (sign * delta_beats);
+                                       len = beats - canvas_note->note()->time() - (sign * snap_delta_beats);
                                }
                        }
 
@@ -2840,10 +2850,8 @@ MidiRegionView::update_resizing (NoteBase* primary, bool at_front, double delta_
  *  Parameters the same as for \a update_resizing().
  */
 void
-MidiRegionView::commit_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, guint key_state)
+MidiRegionView::commit_resizing (NoteBase* primary, bool at_front, double delta_x, bool relative, double snap_delta, bool with_snap)
 {
-       bool snap_keys = ArdourKeyboard::indicates_snap (key_state);
-
        _note_diff_command = _model->new_note_diff_command (_("resize notes"));
 
        for (std::vector<NoteResizeData *>::iterator i = _resize_data.begin(); i != _resize_data.end(); ++i) {
@@ -2876,9 +2884,12 @@ MidiRegionView::commit_resizing (NoteBase* primary, bool at_front, double delta_
                if (current_x > trackview.editor().sample_to_pixel(_region->length())) {
                        current_x = trackview.editor().sample_to_pixel(_region->length());
                }
+
+               /* Convert snap delta from pixels to beats with sign. */
                framepos_t snap_delta_samps = trackview.editor().pixel_to_sample (snap_delta);
-               double snap_delta_beats;
+               double snap_delta_beats = 0.0;
                int sign = 1;
+
                if (snap_delta_samps > 0) {
                        snap_delta_beats = region_frames_to_region_beats_double (snap_delta_samps);
                } else if (snap_delta_samps < 0) {
@@ -2886,13 +2897,12 @@ MidiRegionView::commit_resizing (NoteBase* primary, bool at_front, double delta_
                        sign = -1;
                }
 
-               /* Convert that to a frame within the source */
+               /* Convert the new x position to a frame within the source */
                framepos_t current_fr;
-
-               if (snap_keys) {
-                       current_fr = trackview.editor().pixel_to_sample (current_x) + _region->start ();
-               } else {
+               if (with_snap) {
                        current_fr = snap_pixel_to_sample (current_x) + _region->start ();
+               } else {
+                       current_fr = trackview.editor().pixel_to_sample (current_x) + _region->start ();
                }
 
                /* and then to beats */
@@ -4001,7 +4011,7 @@ MidiRegionView::show_verbose_cursor (boost::shared_ptr<NoteType> n) const
 
        std::string name;
 
-       MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
+       MidiTimeAxisView* mtv = dynamic_cast<MidiTimeAxisView*>(&trackview);
        if (mtv) {
                boost::shared_ptr<MasterDeviceNames> device_names(mtv->get_device_names());
                if (device_names) {
@@ -4013,6 +4023,7 @@ MidiRegionView::show_verbose_cursor (boost::shared_ptr<NoteType> n) const
                                                       patch_key.program(),
                                                       n->note());
                }
+               mtv->set_note_highlight (n->note());
        }
 
        char buf[128];