fix drawing of zero-length notes
[ardour.git] / gtk2_ardour / editor_drag.h
1 /*
2  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2015 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
6  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
8  * Copyright (C) 2015-2018 Ben Loftis <ben@harrisonconsoles.com>
9  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifndef __gtk2_ardour_editor_drag_h_
27 #define __gtk2_ardour_editor_drag_h_
28
29 #include <list>
30
31 #include <gdk/gdk.h>
32 #include <stdint.h>
33
34 #include "ardour/tempo.h"
35 #include "ardour/types.h"
36
37 #include "canvas/types.h"
38
39 #include "cursor_context.h"
40 #include "editor_items.h"
41 #include "mouse_cursors.h"
42 #include "editing.h"
43 #include "track_selection.h"
44
45 namespace ARDOUR {
46         class Location;
47         class TempoSection;
48 }
49
50 namespace ArdourCanvas {
51         class Item;
52         class Line;
53         class Rectangle;
54 }
55
56 namespace PBD {
57         class StatefulDiffCommand;
58 }
59
60 class PatchChange;
61 class Editor;
62 class EditorCursor;
63 class TimeAxisView;
64 class MidiTimeAxisView;
65 class Drag;
66 class NoteBase;
67 class RegionView;
68 class TimeAxisView;
69 class RouteTimeAxisView;
70 class RegionSelection;
71 class MidiRegionView;
72 class MeterMarker;
73 class ArdourMarker;
74 class TempoMarker;
75 class ControlPoint;
76 class AudioRegionView;
77 class AutomationLine;
78 class AutomationTimeAxisView;
79
80 /** Class to manage current drags */
81 class DragManager
82 {
83 public:
84
85         DragManager (Editor* e);
86         ~DragManager ();
87
88         bool motion_handler (GdkEvent *, bool);
89
90         void abort ();
91         void add (Drag *);
92         void set (Drag *, GdkEvent *, Gdk::Cursor* c = MouseCursors::invalid_cursor());
93         void start_grab (GdkEvent *, Gdk::Cursor* c = MouseCursors::invalid_cursor());
94         bool end_grab (GdkEvent *);
95         bool have_item (ArdourCanvas::Item *) const;
96
97         void mark_double_click ();
98
99         /** @return true if an end drag or abort is in progress */
100         bool ending () const {
101                 return _ending;
102         }
103
104         bool active () const {
105                 return !_drags.empty ();
106         }
107
108         /** @return current pointer x position in canvas coordinates */
109         double current_pointer_x () const {
110                 return _current_pointer_x;
111         }
112
113         /** @return current pointer y position in canvas coordinates */
114         double current_pointer_y () const {
115                 return _current_pointer_y;
116         }
117
118         /** @return current pointer sample */
119         ARDOUR::samplepos_t current_pointer_sample () const {
120                 return _current_pointer_sample;
121         }
122
123         /** return drag-motion displays video-frame of drag-location */
124         bool preview_video () const;
125
126 private:
127         Editor* _editor;
128         std::list<Drag*> _drags;
129         bool _ending; ///< true if end_grab or abort is in progress, otherwise false
130         double _current_pointer_x; ///< canvas-coordinate space x of the current pointer
131         double _current_pointer_y; ///< canvas-coordinate space y of the current pointer
132         ARDOUR::samplepos_t _current_pointer_sample; ///< sample that the pointer is now at
133         bool _old_follow_playhead; ///< state of Editor::follow_playhead() before the drags started
134 };
135
136 /** Abstract base class for dragging of things within the editor */
137 class Drag
138 {
139 public:
140         Drag (Editor *, ArdourCanvas::Item *, bool trackview_only = true);
141         virtual ~Drag () {}
142
143         void set_manager (DragManager* m) {
144                 _drags = m;
145         }
146
147         /** @return the canvas item being dragged */
148         ArdourCanvas::Item* item () const {
149                 return _item;
150         }
151
152         void swap_grab (ArdourCanvas::Item *, Gdk::Cursor *, uint32_t);
153         bool motion_handler (GdkEvent*, bool);
154         void abort ();
155
156         ARDOUR::MusicSample adjusted_sample (ARDOUR::samplepos_t, GdkEvent const *, bool snap = true) const;
157         ARDOUR::samplepos_t adjusted_current_sample (GdkEvent const *, bool snap = true) const;
158
159         bool was_double_click() const { return _was_double_click; }
160         void set_double_click (bool yn) { _was_double_click = yn; }
161
162         /** Called to start a grab of an item.
163          *  @param e Event that caused the grab to start.
164          *  @param c Cursor to use, or 0.
165          */
166         virtual void start_grab (GdkEvent* e, Gdk::Cursor* c = 0);
167
168         virtual bool end_grab (GdkEvent *);
169
170         /** Called when a drag motion has occurred.
171          *  @param e Event describing the motion.
172          *  @param f true if this is the first movement, otherwise false.
173          */
174         virtual void motion (GdkEvent* e, bool f) = 0;
175
176         /** Called when a drag has finished.
177          *  @param e Event describing the finish.
178          *  @param m true if some movement occurred, otherwise false.
179          */
180         virtual void finished (GdkEvent* e, bool m) = 0;
181
182         /** Called to abort a drag and return things to how
183          *  they were before it started.
184          *  @param m true if some movement occurred, otherwise false.
185          */
186         virtual void aborted (bool m) = 0;
187
188         /** @param m Mouse mode.
189          *  @return true if this drag should happen in this mouse mode.
190          */
191         virtual bool active (Editing::MouseMode m) {
192                 return true;
193         }
194
195         bool preview_video () const {
196                 return _preview_video;
197         }
198
199         /** @return minimum number of samples (in x) and pixels (in y) that should be considered a movement */
200         virtual std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
201                 return std::make_pair (1, 1);
202         }
203
204         virtual bool allow_vertical_autoscroll () const {
205                 return true;
206         }
207
208         /** @return true if x movement matters to this drag */
209         virtual bool x_movement_matters () const {
210                 return true;
211         }
212
213         /** @return true if y movement matters to this drag */
214         virtual bool y_movement_matters () const {
215                 return true;
216         }
217
218         bool initially_vertical() const {
219                 return _initially_vertical;
220         }
221
222         /** Set up the _pointer_sample_offset */
223         virtual void setup_pointer_sample_offset () {
224                 _pointer_sample_offset = 0;
225         }
226
227         /** Set up the _video_sample_offset - relative to _current_pointer_sample */
228         virtual void setup_video_sample_offset () {
229                 _video_sample_offset = 0;
230                 _preview_video = false;
231         }
232
233 protected:
234
235         double grab_x () const {
236                 return _grab_x;
237         }
238
239         double grab_y () const {
240                 return _grab_y;
241         }
242
243         ARDOUR::samplepos_t raw_grab_sample () const {
244                 return _raw_grab_sample;
245         }
246
247         ARDOUR::samplepos_t grab_sample () const {
248                 return _grab_sample;
249         }
250
251         double last_pointer_x () const {
252                 return _last_pointer_x;
253         }
254
255         double last_pointer_y () const {
256                 return _last_pointer_y;
257         }
258
259         ARDOUR::samplepos_t last_pointer_sample () const {
260                 return _last_pointer_sample;
261         }
262
263         ARDOUR::sampleoffset_t snap_delta (guint const) const;
264         double  snap_delta_music (guint const) const;
265
266         double current_pointer_x () const;
267         double current_pointer_y () const;
268
269         /* sets snap delta from unsnapped pos */
270         void setup_snap_delta (ARDOUR::MusicSample pos);
271
272         boost::shared_ptr<ARDOUR::Region> add_midi_region (MidiTimeAxisView*, bool commit);
273
274         void show_verbose_cursor_time (samplepos_t);
275         void show_verbose_cursor_duration (samplepos_t, samplepos_t, double xoffset = 0);
276         void show_verbose_cursor_text (std::string const &);
277         void show_view_preview (samplepos_t);
278
279         Editor* _editor; ///< our editor
280         DragManager* _drags;
281         ArdourCanvas::Item* _item; ///< our item
282         /** Offset from the mouse's position for the drag to the start of the thing that is being dragged */
283         ARDOUR::samplecnt_t _pointer_sample_offset;
284         ARDOUR::samplecnt_t _video_sample_offset;
285         bool _preview_video;
286         bool _x_constrained; ///< true if x motion is constrained, otherwise false
287         bool _y_constrained; ///< true if y motion is constrained, otherwise false
288         bool _was_rolling; ///< true if the session was rolling before the drag started, otherwise false
289
290 private:
291         bool _trackview_only; ///< true if pointer y value should always be relative to the top of the trackview group
292         bool _move_threshold_passed; ///< true if the move threshold has been passed, otherwise false
293         bool _starting_point_passed; ///< true if we called move () with first_move flag, otherwise false
294         bool _initially_vertical; ///< true if after move threshold is passed we appear to be moving vertically; undefined before that
295         bool _was_double_click; ///< true if drag initiated by a double click event
296         double _grab_x; ///< trackview x of the grab start position
297         double _grab_y; ///< y of the grab start position, possibly adjusted if _trackview_only is true
298         double _last_pointer_x; ///< trackview x of the pointer last time a motion occurred
299         double _last_pointer_y; ///< trackview y of the pointer last time a motion occurred
300         ARDOUR::samplepos_t _raw_grab_sample; ///< unsnapped sample that the mouse was at when start_grab was called, or 0
301         ARDOUR::samplepos_t _grab_sample; ///< adjusted_sample that the mouse was at when start_grab was called, or 0
302         ARDOUR::samplepos_t _last_pointer_sample; ///< adjusted_sample the last time a motion occurred
303
304         /* difference between some key position's snapped and unsnapped
305          *  samplepos. used for relative snap.
306          */
307         samplepos_t _snap_delta;
308         double      _snap_delta_music;
309         CursorContext::Handle _cursor_ctx; ///< cursor change context
310         bool _constraint_pressed; ///< if the keyboard indicated constraint modifier was pressed on start_grab()
311 };
312
313 class RegionDrag;
314
315 /** Container for details about a region being dragged */
316 class DraggingView
317 {
318 public:
319         DraggingView (RegionView *, RegionDrag *, TimeAxisView* original_tav);
320
321         RegionView* view; ///< the view
322         /** index into RegionDrag::_time_axis_views of the view that this region is currently being displayed on,
323          *  or -1 if it is not visible.
324          */
325         int time_axis_view;
326         /** layer that this region is currently being displayed on.  This is a double
327             rather than a layer_t as we use fractional layers during drags to allow the user
328             to indicate a new layer to put a region on.
329         */
330         double layer;
331         double initial_y; ///< the initial y position of the view before any reparenting
332         samplepos_t initial_position; ///< initial position of the region
333         samplepos_t initial_end; ///< initial end position of the region
334         samplepos_t anchored_fade_length; ///< fade_length when anchored during drag
335         boost::shared_ptr<ARDOUR::Playlist> initial_playlist;
336         TimeAxisView* initial_time_axis_view;
337 };
338
339 /** Abstract base class for drags that involve region(s) */
340 class RegionDrag : public Drag, public sigc::trackable
341 {
342 public:
343         RegionDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
344         virtual ~RegionDrag () {}
345
346 protected:
347
348         RegionView* _primary; ///< the view that was clicked on (or whatever) to start the drag
349         std::list<DraggingView> _views; ///< information about all views that are being dragged
350
351         /** a list of the non-hidden TimeAxisViews sorted by editor order key */
352         std::vector<TimeAxisView*> _time_axis_views;
353         int find_time_axis_view (TimeAxisView *) const;
354         int apply_track_delta (const int start, const int delta, const int skip, const bool distance_only = false) const;
355
356         int _visible_y_low;
357         int _visible_y_high;
358         uint32_t _ntracks;
359
360         void setup_video_sample_offset ();
361
362         friend class DraggingView;
363
364 private:
365
366         void region_going_away (RegionView *);
367         PBD::ScopedConnection death_connection;
368 };
369
370
371 /** Drags involving region motion from somewhere */
372 class RegionMotionDrag : public RegionDrag
373 {
374 public:
375
376         RegionMotionDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &, bool);
377         virtual ~RegionMotionDrag () {}
378
379         virtual void start_grab (GdkEvent *, Gdk::Cursor *);
380         virtual void motion (GdkEvent *, bool);
381         virtual void finished (GdkEvent *, bool);
382         virtual void aborted (bool);
383
384         /** @return true if the regions being `moved' came from somewhere on the canvas;
385          *  false if they came from outside (e.g. from the region list).
386          */
387         virtual bool regions_came_from_canvas () const = 0;
388
389 protected:
390
391         double compute_x_delta (GdkEvent const *, ARDOUR::MusicSample *);
392         virtual bool y_movement_allowed (int, double, int skip_invisible = 0) const;
393
394         bool _brushing;
395         bool _ignore_video_lock;
396         ARDOUR::MusicSample _last_position; ///< last position of the thing being dragged
397         double _total_x_delta;
398         int _last_pointer_time_axis_view;
399         double _last_pointer_layer;
400 private:
401         uint32_t _ndropzone;
402         uint32_t _pdropzone;
403         uint32_t _ddropzone;
404 };
405
406
407 /** Drags to move (or copy) regions that are already shown in the GUI to
408  *  somewhere different.
409  */
410 class RegionMoveDrag : public RegionMotionDrag
411 {
412 public:
413         RegionMoveDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &, bool, bool);
414         virtual ~RegionMoveDrag () {}
415
416         void motion (GdkEvent *, bool);
417         void finished (GdkEvent *, bool);
418         void aborted (bool);
419
420         bool regions_came_from_canvas () const {
421                 return true;
422         }
423
424         std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
425                 return std::make_pair (4, 4);
426         }
427
428         void setup_pointer_sample_offset ();
429
430 protected:
431         typedef std::set<boost::shared_ptr<ARDOUR::Playlist> > PlaylistSet;
432         void add_stateful_diff_commands_for_playlists (PlaylistSet const &);
433
434 private:
435         void finished_no_copy (
436                 bool const,
437                 bool const,
438                 ARDOUR::MusicSample,
439                 int32_t const ev_state
440                 );
441
442         void finished_copy (
443                 bool const,
444                 bool const,
445                 ARDOUR::MusicSample,
446                 int32_t const ev_state
447                 );
448
449         RegionView* insert_region_into_playlist (
450                 boost::shared_ptr<ARDOUR::Region>,
451                 RouteTimeAxisView*,
452                 ARDOUR::layer_t,
453                 ARDOUR::MusicSample,
454                 double quarter_note,
455                 PlaylistSet&,
456                 bool for_music = false
457                 );
458
459         void remove_region_from_playlist (
460                 boost::shared_ptr<ARDOUR::Region>,
461                 boost::shared_ptr<ARDOUR::Playlist>,
462                 PlaylistSet& modified_playlists
463                 );
464
465
466         void collect_new_region_view (RegionView *);
467         RouteTimeAxisView* create_destination_time_axis (boost::shared_ptr<ARDOUR::Region>, TimeAxisView* original);
468
469         bool _copy;
470         RegionView* _new_region_view;
471 };
472
473 /** Drag to insert a region from somewhere */
474 class RegionInsertDrag : public RegionMotionDrag
475 {
476 public:
477         RegionInsertDrag (Editor *, boost::shared_ptr<ARDOUR::Region>, RouteTimeAxisView*, ARDOUR::samplepos_t);
478
479         void finished (GdkEvent *, bool);
480         void aborted (bool);
481
482         bool regions_came_from_canvas () const {
483                 return false;
484         }
485 };
486
487 /** Region drag in splice mode */
488 class RegionSpliceDrag : public RegionMoveDrag
489 {
490 public:
491         RegionSpliceDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
492
493         void motion (GdkEvent *, bool);
494         void finished (GdkEvent *, bool);
495         void aborted (bool);
496 };
497
498 /** Region drag in ripple mode */
499
500 class RegionRippleDrag : public RegionMoveDrag
501 {
502 public:
503         RegionRippleDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
504         ~RegionRippleDrag () { delete exclude; }
505
506         void motion (GdkEvent *, bool);
507         void finished (GdkEvent *, bool);
508         void aborted (bool);
509 protected:
510         bool y_movement_allowed (int delta_track, double delta_layer, int skip_invisible = 0) const;
511
512 private:
513         TimeAxisView *prev_tav; // where regions were most recently dragged from
514         TimeAxisView *orig_tav; // where drag started
515         ARDOUR::samplecnt_t prev_amount;
516         ARDOUR::samplepos_t prev_position;
517         ARDOUR::samplecnt_t selection_length;
518         bool allow_moves_across_tracks; // only if all selected regions are on one track
519         ARDOUR::RegionList *exclude;
520         void add_all_after_to_views (TimeAxisView *tav, ARDOUR::samplepos_t where, const RegionSelection &exclude, bool drag_in_progress);
521         void remove_unselected_from_views (ARDOUR::samplecnt_t amount, bool move_regions);
522
523 };
524
525 /** "Drag" to cut a region (action only on button release) */
526 class RegionCutDrag : public Drag
527 {
528 public:
529         RegionCutDrag (Editor*, ArdourCanvas::Item*, samplepos_t);
530         ~RegionCutDrag ();
531
532         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
533         void motion (GdkEvent*, bool);
534         void finished (GdkEvent*, bool);
535         void aborted (bool);
536
537 private:
538 };
539
540 /** Drags to create regions */
541 class RegionCreateDrag : public Drag
542 {
543 public:
544         RegionCreateDrag (Editor *, ArdourCanvas::Item *, TimeAxisView *);
545
546         void motion (GdkEvent *, bool);
547         void finished (GdkEvent *, bool);
548         void aborted (bool);
549
550 private:
551         MidiTimeAxisView* _view;
552         boost::shared_ptr<ARDOUR::Region> _region;
553 };
554
555 /** Drags to resize MIDI notes */
556 class NoteResizeDrag : public Drag
557 {
558 public:
559         NoteResizeDrag (Editor *, ArdourCanvas::Item *);
560
561         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
562         void motion (GdkEvent *, bool);
563         void finished (GdkEvent *, bool);
564         void aborted (bool);
565
566 private:
567         MidiRegionView*     region;
568         bool                relative;
569         bool                at_front;
570         bool                _was_selected;
571         double              _snap_delta;
572 };
573
574 /** Drags to move MIDI notes */
575 class NoteDrag : public Drag
576 {
577 public:
578         NoteDrag (Editor*, ArdourCanvas::Item*);
579
580         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
581         void motion (GdkEvent *, bool);
582         void finished (GdkEvent *, bool);
583         void aborted (bool);
584
585         void setup_pointer_sample_offset ();
586 private:
587
588         double total_dx (GdkEvent * event) const; // total movement in quarter notes
589         int8_t total_dy () const;
590
591         MidiRegionView* _region;
592         NoteBase* _primary;
593         double _cumulative_dx;
594         double _cumulative_dy;
595         double _earliest; // earliest quarter note in note selection
596         bool   _was_selected;
597         double _note_height;
598         bool   _copy;
599 };
600
601 class NoteCreateDrag : public Drag
602 {
603 public:
604         NoteCreateDrag (Editor *, ArdourCanvas::Item *, MidiRegionView *);
605         ~NoteCreateDrag ();
606
607         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
608         void motion (GdkEvent *, bool);
609         void finished (GdkEvent *, bool);
610         void aborted (bool);
611
612         bool active (Editing::MouseMode mode) {
613                 return mode == Editing::MouseDraw || mode == Editing::MouseContent;
614         }
615
616         bool y_movement_matters () const {
617                 return false;
618         }
619
620 private:
621         double y_to_region (double) const;
622         ARDOUR::samplecnt_t grid_samples (samplepos_t) const;
623
624         /** @return minimum number of samples (in x) and pixels (in y) that should be considered a movement */
625         virtual std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
626                 return std::make_pair (0, 0);
627         }
628
629         MidiRegionView* _region_view;
630         ArdourCanvas::Rectangle* _drag_rect;
631         samplepos_t _note[2];
632 };
633
634 class HitCreateDrag : public Drag
635 {
636 public:
637         HitCreateDrag (Editor *, ArdourCanvas::Item *, MidiRegionView *);
638         ~HitCreateDrag ();
639
640         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
641         void motion (GdkEvent *, bool);
642         void finished (GdkEvent *, bool);
643         void aborted (bool);
644
645         bool active (Editing::MouseMode mode) {
646                 return mode == Editing::MouseDraw || mode == Editing::MouseContent;
647         }
648
649         bool y_movement_matters () const {
650                 return false;
651         }
652
653 private:
654         double y_to_region (double) const;
655         ARDOUR::samplecnt_t grid_samples (samplepos_t) const;
656
657         /** @return minimum number of samples (in x) and pixels (in y) that should be considered a movement */
658         virtual std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
659                 return std::make_pair (0, 0);
660         }
661
662         MidiRegionView* _region_view;
663         samplepos_t     _last_pos;
664         double          _y;
665
666 };
667
668 /** Drag to move MIDI patch changes */
669 class PatchChangeDrag : public Drag
670 {
671 public:
672         PatchChangeDrag (Editor *, PatchChange *, MidiRegionView *);
673
674         void motion (GdkEvent *, bool);
675         void finished (GdkEvent *, bool);
676         void aborted (bool);
677
678         bool y_movement_matters () const {
679                 return false;
680         }
681
682         void setup_pointer_sample_offset ();
683
684 private:
685         MidiRegionView* _region_view;
686         PatchChange* _patch_change;
687         double _cumulative_dx;
688 };
689
690 /** Container for details about audio regions being dragged along with video */
691 class AVDraggingView
692 {
693 public:
694         AVDraggingView (RegionView *);
695
696         RegionView* view; ///< the view
697         samplepos_t initial_position; ///< initial position of the region
698 };
699
700 /** Drag of video offset */
701 class VideoTimeLineDrag : public Drag
702 {
703 public:
704         VideoTimeLineDrag (Editor *e, ArdourCanvas::Item *i);
705
706         void motion (GdkEvent *, bool);
707         void finished (GdkEvent *, bool);
708         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
709
710         bool y_movement_matters () const {
711                 return false;
712         }
713
714         bool allow_vertical_autoscroll () const {
715                 return false;
716         }
717
718         void aborted (bool);
719
720 protected:
721         std::list<AVDraggingView> _views; ///< information about all audio that are being dragged along
722
723 private:
724         ARDOUR::sampleoffset_t _startdrag_video_offset;
725         ARDOUR::sampleoffset_t _max_backwards_drag;
726         bool _stuck;
727 };
728
729 /** Drag to trim region(s) */
730 class TrimDrag : public RegionDrag
731 {
732 public:
733         enum Operation {
734                 StartTrim,
735                 EndTrim,
736                 ContentsTrim,
737         };
738
739         TrimDrag (Editor *, ArdourCanvas::Item *, RegionView*, std::list<RegionView*> const &, bool preserve_fade_anchor = false);
740
741         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
742         void motion (GdkEvent *, bool);
743         void finished (GdkEvent *, bool);
744         void aborted (bool);
745
746         bool y_movement_matters () const {
747                 return false;
748         }
749
750         void setup_pointer_sample_offset ();
751
752 private:
753
754         Operation _operation;
755
756         bool _preserve_fade_anchor;
757         bool _jump_position_when_done;
758 };
759
760 /** Meter marker drag */
761 class MeterMarkerDrag : public Drag
762 {
763 public:
764         MeterMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
765
766         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
767         void motion (GdkEvent *, bool);
768         void finished (GdkEvent *, bool);
769         void aborted (bool);
770
771         bool allow_vertical_autoscroll () const {
772                 return false;
773         }
774
775         bool y_movement_matters () const {
776                 return false;
777         }
778
779         void setup_pointer_sample_offset ();
780
781 private:
782         MeterMarker* _marker;
783         ARDOUR::MeterSection* _real_section;
784
785         bool _copy;
786         Editing::GridType _old_grid_type;
787         Editing::SnapMode _old_snap_mode;
788         XMLNode* before_state;
789 };
790
791 /** Tempo marker drag */
792 class TempoMarkerDrag : public Drag
793 {
794 public:
795         TempoMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
796
797         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
798         void motion (GdkEvent *, bool);
799         void finished (GdkEvent *, bool);
800         void aborted (bool);
801
802         bool allow_vertical_autoscroll () const {
803                 return false;
804         }
805
806         bool y_movement_matters () const {
807                 return true;
808         }
809
810         void setup_pointer_sample_offset ();
811
812 private:
813         TempoMarker* _marker;
814         ARDOUR::TempoSection* _real_section;
815
816         bool _copy;
817         bool _movable;
818         ARDOUR::Tempo _grab_bpm;
819         double _grab_qn;
820         XMLNode* _before_state;
821 };
822
823 /** BBT Ruler drag */
824 class BBTRulerDrag : public Drag
825 {
826 public:
827         BBTRulerDrag (Editor *, ArdourCanvas::Item *);
828
829         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
830         void motion (GdkEvent *, bool);
831         void finished (GdkEvent *, bool);
832         void aborted (bool);
833
834         bool allow_vertical_autoscroll () const {
835                 return false;
836         }
837
838         bool y_movement_matters () const {
839                 return false;
840         }
841
842         void setup_pointer_sample_offset ();
843
844 private:
845         double _grab_qn;
846         ARDOUR::TempoSection* _tempo;
847         XMLNode* _before_state;
848         bool     _drag_valid;
849 };
850
851 /** tempo curve twist drag */
852 class TempoTwistDrag : public Drag
853 {
854 public:
855         TempoTwistDrag (Editor *, ArdourCanvas::Item *);
856
857         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
858         void motion (GdkEvent *, bool);
859         void finished (GdkEvent *, bool);
860         void aborted (bool);
861
862         bool allow_vertical_autoscroll () const {
863                 return false;
864         }
865
866         bool y_movement_matters () const {
867                 return true;
868         }
869
870         void setup_pointer_sample_offset ();
871
872 private:
873         double _grab_qn;
874         ARDOUR::Tempo  _grab_tempo;
875         ARDOUR::TempoSection* _tempo;
876         ARDOUR::TempoSection* _next_tempo;
877         bool _drag_valid;
878         XMLNode* _before_state;
879 };
880
881
882 /** tempo curve twist drag */
883 class TempoEndDrag : public Drag
884 {
885 public:
886         TempoEndDrag (Editor *, ArdourCanvas::Item *);
887
888         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
889         void motion (GdkEvent *, bool);
890         void finished (GdkEvent *, bool);
891         void aborted (bool);
892
893         bool allow_vertical_autoscroll () const {
894                 return false;
895         }
896
897         bool y_movement_matters () const {
898                 return true;
899         }
900
901         void setup_pointer_sample_offset ();
902
903 private:
904         double _grab_qn;
905         ARDOUR::TempoSection* _tempo;
906         XMLNode* _before_state;
907         bool _drag_valid;
908 };
909
910 /** Drag of the playhead cursor */
911 class CursorDrag : public Drag
912 {
913 public:
914         CursorDrag (Editor *, EditorCursor&, bool);
915
916         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
917         void motion (GdkEvent *, bool);
918         void finished (GdkEvent *, bool);
919         void aborted (bool);
920
921         bool allow_vertical_autoscroll () const {
922                 return false;
923         }
924
925         bool y_movement_matters () const {
926                 return true;
927         }
928
929 private:
930         void fake_locate (samplepos_t);
931
932         EditorCursor& _cursor;
933         bool _stop; ///< true to stop the transport on starting the drag, otherwise false
934         double _grab_zoom; ///< editor samples per unit when our grab started
935
936         //used for zooming
937         int _last_mx;
938         int _last_my;
939         int _last_dx;
940         int _last_y_delta;
941 };
942
943 /** Region fade-in drag */
944 class FadeInDrag : public RegionDrag
945 {
946 public:
947         FadeInDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
948
949         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
950         void motion (GdkEvent *, bool);
951         void finished (GdkEvent *, bool);
952         void aborted (bool);
953
954         bool y_movement_matters () const {
955                 return false;
956         }
957
958         void setup_pointer_sample_offset ();
959 };
960
961 /** Region fade-out drag */
962 class FadeOutDrag : public RegionDrag
963 {
964 public:
965         FadeOutDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
966
967         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
968         void motion (GdkEvent *, bool);
969         void finished (GdkEvent *, bool);
970         void aborted (bool);
971
972         bool y_movement_matters () const {
973                 return false;
974         }
975
976         void setup_pointer_sample_offset ();
977 };
978
979 /** Marker drag */
980 class MarkerDrag : public Drag
981 {
982 public:
983         MarkerDrag (Editor *, ArdourCanvas::Item *);
984         ~MarkerDrag ();
985
986         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
987         void motion (GdkEvent *, bool);
988         void finished (GdkEvent *, bool);
989         void aborted (bool);
990
991         bool allow_vertical_autoscroll () const {
992                 return false;
993         }
994
995         bool y_movement_matters () const {
996                 return false;
997         }
998
999         void setup_pointer_sample_offset ();
1000         void setup_video_sample_offset ();
1001
1002 private:
1003         void update_item (ARDOUR::Location *);
1004
1005         ArdourMarker* _marker; ///< marker being dragged
1006         bool _selection_changed;
1007         struct CopiedLocationMarkerInfo {
1008                 ARDOUR::Location* location;
1009                 std::vector<ArdourMarker*> markers;
1010                 bool    move_both;
1011                 CopiedLocationMarkerInfo (ARDOUR::Location* l, ArdourMarker* m);
1012         };
1013
1014         typedef std::list<CopiedLocationMarkerInfo> CopiedLocationInfo;
1015         CopiedLocationInfo _copied_locations;
1016         ArdourCanvas::Points _points;
1017 };
1018
1019 /** Control point drag */
1020 class ControlPointDrag : public Drag
1021 {
1022 public:
1023         ControlPointDrag (Editor *, ArdourCanvas::Item *);
1024
1025         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1026         void motion (GdkEvent *, bool);
1027         void finished (GdkEvent *, bool);
1028         void aborted (bool);
1029
1030         bool active (Editing::MouseMode m);
1031
1032 private:
1033
1034         ControlPoint* _point;
1035         double _fixed_grab_x;
1036         double _fixed_grab_y;
1037         double _cumulative_x_drag;
1038         double _cumulative_y_drag;
1039         bool     _pushing;
1040         uint32_t _final_index;
1041         static double _zero_gain_fraction;
1042 };
1043
1044 /** Gain or automation line drag */
1045 class LineDrag : public Drag
1046 {
1047 public:
1048         LineDrag (Editor *e, ArdourCanvas::Item *i);
1049
1050         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1051         void motion (GdkEvent *, bool);
1052         void finished (GdkEvent *, bool);
1053         void aborted (bool);
1054
1055 private:
1056
1057         AutomationLine* _line;
1058         double _fixed_grab_x;
1059         double _fixed_grab_y;
1060         double _cumulative_y_drag;
1061         uint32_t _before;
1062         uint32_t _after;
1063 };
1064
1065 /** Transient feature line drags*/
1066 class FeatureLineDrag : public Drag
1067 {
1068 public:
1069         FeatureLineDrag (Editor *e, ArdourCanvas::Item *i);
1070
1071         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1072         void motion (GdkEvent *, bool);
1073         void finished (GdkEvent *, bool);
1074         void aborted (bool);
1075
1076 private:
1077
1078         ArdourCanvas::Line* _line;
1079         AudioRegionView* _arv;
1080
1081         double _region_view_grab_x;
1082         double _cumulative_x_drag;
1083
1084         float _before;
1085         uint32_t _max_x;
1086 };
1087
1088 /** Dragging of a rubberband rectangle for selecting things */
1089 class RubberbandSelectDrag : public Drag
1090 {
1091 public:
1092         RubberbandSelectDrag (Editor *, ArdourCanvas::Item *);
1093
1094         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1095         void motion (GdkEvent *, bool);
1096         void finished (GdkEvent *, bool);
1097         void aborted (bool);
1098
1099         std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
1100                 return std::make_pair (8, 1);
1101         }
1102
1103         void do_select_things (GdkEvent *, bool);
1104
1105         /** Select some things within a rectangle.
1106          *  @param button_state The button state from the GdkEvent.
1107          *  @param x1 The left-hand side of the rectangle in session samples.
1108          *  @param x2 The right-hand side of the rectangle in session samples.
1109          *  @param y1 The top of the rectangle in trackview coordinates.
1110          *  @param y2 The bottom of the rectangle in trackview coordinates.
1111          *  @param drag_in_progress true if the drag is currently happening.
1112          */
1113         virtual void select_things (int button_state, samplepos_t x1, samplepos_t x2, double y1, double y2, bool drag_in_progress) = 0;
1114
1115         virtual void deselect_things () = 0;
1116
1117   protected:
1118         bool _vertical_only;
1119 };
1120
1121 /** A general editor RubberbandSelectDrag (for regions, automation points etc.) */
1122 class EditorRubberbandSelectDrag : public RubberbandSelectDrag
1123 {
1124 public:
1125         EditorRubberbandSelectDrag (Editor *, ArdourCanvas::Item *);
1126
1127         void select_things (int, samplepos_t, samplepos_t, double, double, bool);
1128         void deselect_things ();
1129 };
1130
1131 /** A RubberbandSelectDrag for selecting MIDI notes */
1132 class MidiRubberbandSelectDrag : public RubberbandSelectDrag
1133 {
1134 public:
1135         MidiRubberbandSelectDrag (Editor *, MidiRegionView *);
1136
1137         void select_things (int, samplepos_t, samplepos_t, double, double, bool);
1138         void deselect_things ();
1139
1140 private:
1141         MidiRegionView* _region_view;
1142 };
1143
1144 /** A RubberbandSelectDrag for selecting MIDI notes but with no horizonal component */
1145 class MidiVerticalSelectDrag : public RubberbandSelectDrag
1146 {
1147 public:
1148         MidiVerticalSelectDrag (Editor *, MidiRegionView *);
1149
1150         void select_things (int, samplepos_t, samplepos_t, double, double, bool);
1151         void deselect_things ();
1152
1153 private:
1154         MidiRegionView* _region_view;
1155 };
1156
1157 /** Region drag in time-FX mode */
1158 class TimeFXDrag : public RegionDrag
1159 {
1160 public:
1161         TimeFXDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
1162
1163         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1164         void motion (GdkEvent *, bool);
1165         void finished (GdkEvent *, bool);
1166         void aborted (bool);
1167 };
1168
1169 /** Scrub drag in audition mode */
1170 class ScrubDrag : public Drag
1171 {
1172 public:
1173         ScrubDrag (Editor *, ArdourCanvas::Item *);
1174
1175         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1176         void motion (GdkEvent *, bool);
1177         void finished (GdkEvent *, bool);
1178         void aborted (bool);
1179 };
1180
1181 /** Drag in range select mode */
1182 class SelectionDrag : public Drag
1183 {
1184 public:
1185         enum Operation {
1186                 CreateSelection,
1187                 SelectionStartTrim,
1188                 SelectionEndTrim,
1189                 SelectionMove,
1190                 SelectionExtend
1191         };
1192
1193         SelectionDrag (Editor *, ArdourCanvas::Item *, Operation);
1194
1195         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1196         void motion (GdkEvent *, bool);
1197         void finished (GdkEvent *, bool);
1198         void aborted (bool);
1199
1200         void setup_pointer_sample_offset ();
1201
1202 private:
1203         Operation _operation;
1204         bool _add;
1205         TrackSelection _track_selection_at_start;
1206         bool _time_selection_at_start;
1207         samplepos_t start_at_start;
1208         samplepos_t end_at_start;
1209 };
1210
1211 /** Range marker drag */
1212 class RangeMarkerBarDrag : public Drag
1213 {
1214 public:
1215         enum Operation {
1216                 CreateSkipMarker,
1217                 CreateRangeMarker,
1218                 CreateTransportMarker,
1219                 CreateCDMarker
1220         };
1221
1222         RangeMarkerBarDrag (Editor *, ArdourCanvas::Item *, Operation);
1223         ~RangeMarkerBarDrag ();
1224
1225         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1226         void motion (GdkEvent *, bool);
1227         void finished (GdkEvent *, bool);
1228         void aborted (bool);
1229
1230         bool allow_vertical_autoscroll () const {
1231                 return false;
1232         }
1233
1234         bool y_movement_matters () const {
1235                 return false;
1236         }
1237
1238 private:
1239         void update_item (ARDOUR::Location *);
1240
1241         Operation _operation;
1242         ArdourCanvas::Rectangle* _drag_rect;
1243         bool _copy;
1244 };
1245
1246 /** Drag of rectangle to set zoom */
1247 class MouseZoomDrag : public Drag
1248 {
1249 public:
1250         MouseZoomDrag (Editor *, ArdourCanvas::Item *);
1251
1252         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1253         void motion (GdkEvent *, bool);
1254         void finished (GdkEvent *, bool);
1255         void aborted (bool);
1256
1257         std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
1258                 return std::make_pair (4, 4);
1259         }
1260
1261 private:
1262         bool _zoom_out;
1263 };
1264
1265 /** Drag of a range of automation data (either on an automation track or region gain),
1266  *  changing value but not position.
1267  */
1268 class AutomationRangeDrag : public Drag
1269 {
1270 public:
1271         AutomationRangeDrag (Editor *, AutomationTimeAxisView *, std::list<ARDOUR::AudioRange> const &);
1272         AutomationRangeDrag (Editor *, std::list<RegionView*> const &, std::list<ARDOUR::AudioRange> const &, double y_origin, double y_height);
1273
1274         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
1275         void motion (GdkEvent *, bool);
1276         void finished (GdkEvent *, bool);
1277         void aborted (bool);
1278
1279         bool x_movement_matters () const {
1280                 return false;
1281         }
1282
1283 private:
1284         void setup (std::list<boost::shared_ptr<AutomationLine> > const &);
1285         double y_fraction (double global_y_position) const;
1286         double value (boost::shared_ptr<ARDOUR::AutomationList> list, double x) const;
1287
1288         std::list<ARDOUR::AudioRange> _ranges;
1289
1290         /** A line that is part of the drag */
1291         struct Line {
1292                 boost::shared_ptr<AutomationLine> line; ///< the line
1293                 std::list<ControlPoint*> points; ///< points to drag on the line
1294                 std::pair<ARDOUR::samplepos_t, ARDOUR::samplepos_t> range; ///< the range of all points on the line, in session samples
1295                 XMLNode* state; ///< the XML state node before the drag
1296         };
1297
1298         std::list<Line> _lines;
1299         double          _y_origin;
1300         double          _y_height;
1301         bool            _nothing_to_drag;
1302         bool            _integral;
1303 };
1304
1305 /** Drag of one edge of an xfade
1306  */
1307 class CrossfadeEdgeDrag : public Drag
1308 {
1309 public:
1310         CrossfadeEdgeDrag (Editor*, AudioRegionView*, ArdourCanvas::Item*, bool start);
1311
1312         void start_grab (GdkEvent*, Gdk::Cursor* c = 0);
1313         void motion (GdkEvent*, bool);
1314         void finished (GdkEvent*, bool);
1315         void aborted (bool);
1316
1317         bool y_movement_matters () const {
1318                 return false;
1319         }
1320
1321         virtual std::pair<ARDOUR::samplecnt_t, int> move_threshold () const {
1322                 return std::make_pair (4, 4);
1323         }
1324
1325 private:
1326         AudioRegionView* arv;
1327         bool start;
1328 };
1329
1330 #endif /* __gtk2_ardour_editor_drag_h_ */
1331