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