d710e3a639261e0dd80dbca52d7e1691194c6ff9
[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 #include <bitset>
28
29 #include "ardour/types.h"
30
31 #include "canvas.h"
32 #include "editor_items.h"
33
34 namespace ARDOUR {
35         class Location;
36 }
37
38 namespace Gnome {
39         namespace Canvas {
40                 class CanvasNoteEvent;
41         }
42 }
43
44 class Editor;
45 class EditorCursor;
46 class TimeAxisView;
47 class MidiTimeAxisView;
48 class Drag;
49
50 /** Class to manage current drags */
51 class DragManager
52 {
53 public:
54
55         DragManager (Editor* e);
56         ~DragManager ();
57
58         bool motion_handler (GdkEvent *, bool);
59
60         void abort ();
61         void add (Drag *);
62         void set (Drag *, GdkEvent *, Gdk::Cursor* c = 0);
63         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
64         bool end_grab (GdkEvent *);
65         bool have_item (ArdourCanvas::Item *) const;
66
67         /** @return true if an end drag or abort is in progress */
68         bool ending () const {
69                 return _ending;
70         }
71
72         bool active () const {
73                 return !_drags.empty ();
74         }
75
76         /** @return current pointer x position in trackview coordinates */
77         double current_pointer_x () const {
78                 return _current_pointer_x;
79         }
80
81         /** @return current pointer y position in trackview coordinates */
82         double current_pointer_y () const {
83                 return _current_pointer_y;
84         }
85
86         /** @return current pointer frame */
87         nframes64_t current_pointer_frame () const {
88                 return _current_pointer_frame;
89         }
90
91 private:
92         Editor* _editor;
93         std::list<Drag*> _drags;
94         bool _ending; ///< true if end_grab or abort is in progress, otherwise false
95         double _current_pointer_x; ///< trackview x of the current pointer
96         double _current_pointer_y; ///< trackview y of the current pointer
97         nframes64_t _current_pointer_frame; ///< frame that the pointer is now at
98 };
99
100 /** Abstract base class for dragging of things within the editor */
101 class Drag
102 {
103 public:
104         Drag (Editor *, ArdourCanvas::Item *);
105         virtual ~Drag () {}
106
107         void set_manager (DragManager* m) {
108                 _drags = m;
109         }
110
111         /** @return the canvas item being dragged */
112         ArdourCanvas::Item* item () const {
113                 return _item;
114         }
115
116         void swap_grab (ArdourCanvas::Item *, Gdk::Cursor *, uint32_t);
117         bool motion_handler (GdkEvent*, bool);
118         void abort ();
119
120         nframes64_t adjusted_frame (nframes64_t, GdkEvent const *, bool snap = true) const;
121         nframes64_t adjusted_current_frame (GdkEvent const *, bool snap = true) const;
122         
123         /** Called to start a grab of an item.
124          *  @param e Event that caused the grab to start.
125          *  @param c Cursor to use, or 0.
126          */
127         virtual void start_grab (GdkEvent* e, Gdk::Cursor* c = 0);
128
129         virtual bool end_grab (GdkEvent *);
130
131         /** Called when a drag motion has occurred.
132          *  @param e Event describing the motion.
133          *  @param f true if this is the first movement, otherwise false.
134          */
135         virtual void motion (GdkEvent* e, bool f) = 0;
136
137         /** Called when a drag has finished.
138          *  @param e Event describing the finish.
139          *  @param m true if some movement occurred, otherwise false.
140          */
141         virtual void finished (GdkEvent* e, bool m) = 0;
142
143         /** Called to abort a drag and return things to how
144          *  they were before it started.
145          */
146         virtual void aborted () = 0;
147
148         /** @param m Mouse mode.
149          *  @return true if this drag should happen in this mouse mode.
150          */
151         virtual bool active (Editing::MouseMode m) {
152                 return (m != Editing::MouseGain);
153         }
154
155         /** @return minimum number of frames (in x) and pixels (in y) that should be considered a movement */
156         virtual std::pair<nframes64_t, int> move_threshold () const {
157                 return std::make_pair (1, 1);
158         }
159
160         virtual bool allow_vertical_autoscroll () const {
161                 return true;
162         }
163
164         /** @return true if x movement matters to this drag */
165         virtual bool x_movement_matters () const {
166                 return true;
167         }
168
169         /** @return true if y movement matters to this drag */
170         virtual bool y_movement_matters () const {
171                 return true;
172         }
173
174 protected:
175
176         double grab_x () const {
177                 return _grab_x;
178         }
179
180         double grab_y () const {
181                 return _grab_y;
182         }
183
184         ARDOUR::framepos_t grab_frame () const {
185                 return _grab_frame;
186         }
187
188         double last_pointer_x () const {
189                 return _last_pointer_x;
190         }
191
192         double last_pointer_y () const {
193                 return _last_pointer_y;
194         }
195
196         double last_pointer_frame () const {
197                 return _last_pointer_frame;
198         }
199
200         Editor* _editor; ///< our editor
201         DragManager* _drags;
202         ArdourCanvas::Item* _item; ///< our item
203         /** Offset from the mouse's position for the drag to the start of the thing that is being dragged */
204         nframes64_t _pointer_frame_offset;
205         bool _x_constrained; ///< true if x motion is constrained, otherwise false
206         bool _y_constrained; ///< true if y motion is constrained, otherwise false
207         bool _was_rolling; ///< true if the session was rolling before the drag started, otherwise false
208
209 private:
210
211         bool _move_threshold_passed; ///< true if the move threshold has been passed, otherwise false
212         double _grab_x; ///< trackview x of the grab start position
213         double _grab_y; ///< trackview y of the grab start position
214         double _last_pointer_x; ///< trackview x of the pointer last time a motion occurred
215         double _last_pointer_y; ///< trackview y of the pointer last time a motion occurred
216         nframes64_t _grab_frame; ///< adjusted_frame that the mouse was at when start_grab was called, or 0
217         nframes64_t _last_pointer_frame; ///< adjusted_frame the last time a motion occurred
218 };
219
220 struct DraggingView
221 {
222         DraggingView (RegionView* v);
223
224         RegionView* view; ///< the view
225         double initial_y; ///< the initial y position of the view before any reparenting
226 };
227
228 /** Abstract base class for drags that involve region(s) */
229 class RegionDrag : public Drag, public sigc::trackable
230 {
231 public:
232         RegionDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
233         virtual ~RegionDrag () {}
234
235 protected:
236
237         RegionView* _primary; ///< the view that was clicked on (or whatever) to start the drag
238         std::list<DraggingView> _views; ///< information about all views that are being dragged
239
240 private:
241         void region_going_away (RegionView *);
242         PBD::ScopedConnection death_connection;
243 };
244
245
246 /** Drags involving region motion from somewhere */
247 class RegionMotionDrag : public RegionDrag
248 {
249 public:
250
251         RegionMotionDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &, bool);
252         virtual ~RegionMotionDrag () {}
253
254         virtual void start_grab (GdkEvent *, Gdk::Cursor *);
255         virtual void motion (GdkEvent *, bool);
256         virtual void finished (GdkEvent *, bool) = 0;
257         virtual void aborted ();
258
259         /** @return true if the regions being `moved' came from somewhere on the canvas;
260          *  false if they came from outside (e.g. from the region list).
261          */
262         virtual bool regions_came_from_canvas () const = 0;
263
264 protected:
265         struct TimeAxisViewSummary {
266                 TimeAxisViewSummary () : height_list(512) {}
267
268                 std::bitset<512> tracks;
269                 std::vector<int32_t> height_list;
270                 int visible_y_low;
271                 int visible_y_high;
272         };
273
274         void copy_regions (GdkEvent *);
275         bool y_movement_disallowed (int, int, int, TimeAxisViewSummary const &) const;
276         std::map<RegionView*, std::pair<RouteTimeAxisView*, int> > find_time_axis_views_and_layers ();
277         double compute_x_delta (GdkEvent const *, nframes64_t *);
278         bool compute_y_delta (
279                 TimeAxisView const *, TimeAxisView*, int32_t, int32_t, TimeAxisViewSummary const &,
280                 int32_t *, int32_t *, int32_t *
281                 );
282
283         TimeAxisViewSummary get_time_axis_view_summary ();
284         bool x_move_allowed () const;
285
286         TimeAxisView* _dest_trackview;
287         ARDOUR::layer_t _dest_layer;
288         bool check_possible (RouteTimeAxisView **, ARDOUR::layer_t *);
289         bool _brushing;
290         nframes64_t _last_frame_position; ///< last position of the thing being dragged
291         double _total_x_delta;
292 };
293
294
295 /** Drags to move (or copy) regions that are already shown in the GUI to
296  *  somewhere different.
297  */
298 class RegionMoveDrag : public RegionMotionDrag
299 {
300 public:
301         RegionMoveDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &, bool, bool);
302         virtual ~RegionMoveDrag () {}
303
304         virtual void start_grab (GdkEvent *, Gdk::Cursor *);
305         void motion (GdkEvent *, bool);
306         void finished (GdkEvent *, bool);
307         void aborted ();
308
309         bool regions_came_from_canvas () const {
310                 return true;
311         }
312
313         std::pair<nframes64_t, int> move_threshold () const {
314                 return std::make_pair (4, 4);
315         }
316
317 private:
318         bool _copy;
319 };
320
321 /** Drag to insert a region from somewhere */
322 class RegionInsertDrag : public RegionMotionDrag
323 {
324 public:
325         RegionInsertDrag (Editor *, boost::shared_ptr<ARDOUR::Region>, RouteTimeAxisView*, nframes64_t);
326
327         void finished (GdkEvent *, bool);
328         void aborted ();
329
330         bool regions_came_from_canvas () const {
331                 return false;
332         }
333 };
334
335 /** Region drag in splice mode */
336 class RegionSpliceDrag : public RegionMoveDrag
337 {
338 public:
339         RegionSpliceDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
340
341         void motion (GdkEvent *, bool);
342         void finished (GdkEvent *, bool);
343         void aborted ();
344 };
345
346 /** Drags to create regions */
347 class RegionCreateDrag : public Drag
348 {
349 public:
350         RegionCreateDrag (Editor *, ArdourCanvas::Item *, TimeAxisView *);
351
352         void motion (GdkEvent *, bool);
353         void finished (GdkEvent *, bool);
354         void aborted ();
355
356 private:
357         MidiTimeAxisView* _view;
358         boost::shared_ptr<ARDOUR::Region> _region;
359 };
360
361 /** Drags to resize MIDI notes */
362 class NoteResizeDrag : public Drag
363 {
364 public:
365         NoteResizeDrag (Editor *, ArdourCanvas::Item *);
366
367         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
368         void motion (GdkEvent *, bool);
369         void finished (GdkEvent *, bool);
370         void aborted ();
371
372 private:
373         MidiRegionView*     region;
374         bool                relative;
375         bool                at_front;
376 };
377
378 /** Drags to move MIDI notes */
379 class NoteDrag : public Drag
380 {
381   public:
382         NoteDrag (Editor*, ArdourCanvas::Item*);
383
384         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
385         void motion (GdkEvent *, bool);
386         void finished (GdkEvent *, bool);
387         void aborted ();
388
389   private:
390
391         ARDOUR::frameoffset_t total_dx () const;
392         int8_t total_dy () const;
393         
394         MidiRegionView* _region;
395         Gnome::Canvas::CanvasNoteEvent* _primary;
396         double _cumulative_dx;
397         double _cumulative_dy;
398         bool _was_selected;
399         double _note_height;
400 };
401
402 /** Drag of region gain */
403 class RegionGainDrag : public Drag
404 {
405 public:
406         RegionGainDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
407
408         void motion (GdkEvent *, bool);
409         void finished (GdkEvent *, bool);
410         bool active (Editing::MouseMode m) {
411                 return (m == Editing::MouseGain);
412         }
413
414         void aborted ();
415 };
416
417 /** Drag to trim region(s) */
418 class TrimDrag : public RegionDrag
419 {
420 public:
421         enum Operation {
422                 StartTrim,
423                 EndTrim,
424                 ContentsTrim,
425         };
426
427         TrimDrag (Editor *, ArdourCanvas::Item *, RegionView*, std::list<RegionView*> const &);
428
429         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
430         void motion (GdkEvent *, bool);
431         void finished (GdkEvent *, bool);
432         void aborted ();
433
434         bool y_movement_matters () const {
435                 return false;
436         }
437
438 private:
439
440         Operation _operation;
441         bool _have_transaction; ///< true if a transaction has been started, false otherwise. Must be set true by derived class.
442 };
443
444 /** Meter marker drag */
445 class MeterMarkerDrag : public Drag
446 {
447 public:
448         MeterMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
449
450         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
451         void motion (GdkEvent *, bool);
452         void finished (GdkEvent *, bool);
453         void aborted ();
454
455         bool allow_vertical_autoscroll () const {
456                 return false;
457         }
458
459         bool y_movement_matters () const {
460                 return false;
461         }
462         
463 private:
464         MeterMarker* _marker;
465         bool _copy;
466 };
467
468 /** Tempo marker drag */
469 class TempoMarkerDrag : public Drag
470 {
471 public:
472         TempoMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
473
474         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
475         void motion (GdkEvent *, bool);
476         void finished (GdkEvent *, bool);
477         void aborted ();
478
479         bool allow_vertical_autoscroll () const {
480                 return false;
481         }
482
483         bool y_movement_matters () const {
484                 return false;
485         }
486
487 private:
488         TempoMarker* _marker;
489         bool _copy;
490 };
491
492
493 /** Drag of a cursor */
494 class CursorDrag : public Drag
495 {
496 public:
497         CursorDrag (Editor *, ArdourCanvas::Item *, bool);
498
499         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
500         void motion (GdkEvent *, bool);
501         void finished (GdkEvent *, bool);
502         void aborted ();
503
504         bool active (Editing::MouseMode) {
505                 return true;
506         }
507
508         bool allow_vertical_autoscroll () const {
509                 return false;
510         }
511
512         bool y_movement_matters () const {
513                 return false;
514         }
515
516 private:
517         EditorCursor* _cursor; ///< cursor being dragged
518         bool _stop; ///< true to stop the transport on starting the drag, otherwise false
519
520 };
521
522 /** Region fade-in drag */
523 class FadeInDrag : public RegionDrag
524 {
525 public:
526         FadeInDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
527
528         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
529         void motion (GdkEvent *, bool);
530         void finished (GdkEvent *, bool);
531         void aborted ();
532
533         bool y_movement_matters () const {
534                 return false;
535         }
536 };
537
538 /** Region fade-out drag */
539 class FadeOutDrag : public RegionDrag
540 {
541 public:
542         FadeOutDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
543
544         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
545         void motion (GdkEvent *, bool);
546         void finished (GdkEvent *, bool);
547         void aborted ();
548
549         bool y_movement_matters () const {
550                 return false;
551         }
552 };
553
554 /** Marker drag */
555 class MarkerDrag : public Drag
556 {
557 public:
558         MarkerDrag (Editor *, ArdourCanvas::Item *);
559         ~MarkerDrag ();
560
561         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
562         void motion (GdkEvent *, bool);
563         void finished (GdkEvent *, bool);
564         void aborted ();
565
566         bool allow_vertical_autoscroll () const {
567                 return false;
568         }
569
570         bool y_movement_matters () const {
571                 return false;
572         }
573         
574 private:
575         void update_item (ARDOUR::Location *);
576
577         Marker* _marker; ///< marker being dragged
578         std::list<ARDOUR::Location*> _copied_locations;
579         ArdourCanvas::Line* _line;
580         ArdourCanvas::Points _points;
581 };
582
583 /** Control point drag */
584 class ControlPointDrag : public Drag
585 {
586 public:
587         ControlPointDrag (Editor *, ArdourCanvas::Item *);
588
589         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
590         void motion (GdkEvent *, bool);
591         void finished (GdkEvent *, bool);
592         void aborted ();
593
594         bool active (Editing::MouseMode m);
595
596 private:
597
598         ControlPoint* _point;
599         double _time_axis_view_grab_x;
600         double _time_axis_view_grab_y;
601         double _cumulative_x_drag;
602         double _cumulative_y_drag;
603         static double const _zero_gain_fraction;
604 };
605
606 /** Gain or automation line drag */
607 class LineDrag : public Drag
608 {
609 public:
610         LineDrag (Editor *e, ArdourCanvas::Item *i);
611
612         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
613         void motion (GdkEvent *, bool);
614         void finished (GdkEvent *, bool);
615         void aborted ();
616
617         bool active (Editing::MouseMode) {
618                 return true;
619         }
620
621 private:
622
623         AutomationLine* _line;
624         double _time_axis_view_grab_x;
625         double _time_axis_view_grab_y;
626         uint32_t _before;
627         uint32_t _after;
628         double _cumulative_y_drag;
629 };
630
631 /** Transient feature line drags*/
632 class FeatureLineDrag : public Drag
633 {
634 public:
635         FeatureLineDrag (Editor *e, ArdourCanvas::Item *i);
636
637         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
638         void motion (GdkEvent *, bool);
639         void finished (GdkEvent *, bool);
640         void aborted ();
641
642         bool active (Editing::MouseMode) {
643                 return true;
644         }
645
646 private:
647
648         ArdourCanvas::SimpleLine* _line;
649         AudioRegionView* _arv;
650         
651         double _region_view_grab_x;
652         double _cumulative_x_drag;
653         
654         uint32_t _before;
655         uint32_t _max_x;
656 };
657
658 /** Dragging of a rubberband rectangle for selecting things */
659 class RubberbandSelectDrag : public Drag
660 {
661 public:
662         RubberbandSelectDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
663
664         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
665         void motion (GdkEvent *, bool);
666         void finished (GdkEvent *, bool);
667         void aborted ();
668
669         std::pair<nframes64_t, int> move_threshold () const {
670                 return std::make_pair (8, 1);
671         }
672 };
673
674 /** Region drag in time-FX mode */
675 class TimeFXDrag : public RegionDrag
676 {
677 public:
678         TimeFXDrag (Editor *e, ArdourCanvas::Item *i, RegionView* p, std::list<RegionView*> const & v) : RegionDrag (e, i, p, v) {}
679
680         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
681         void motion (GdkEvent *, bool);
682         void finished (GdkEvent *, bool);
683         void aborted ();
684 };
685
686 /** Scrub drag in audition mode */
687 class ScrubDrag : public Drag
688 {
689 public:
690         ScrubDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
691
692         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
693         void motion (GdkEvent *, bool);
694         void finished (GdkEvent *, bool);
695         void aborted ();
696 };
697
698 /** Drag in range select mode */
699 class SelectionDrag : public Drag
700 {
701 public:
702         enum Operation {
703                 CreateSelection,
704                 SelectionStartTrim,
705                 SelectionEndTrim,
706                 SelectionMove
707         };
708
709         SelectionDrag (Editor *, ArdourCanvas::Item *, Operation);
710
711         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
712         void motion (GdkEvent *, bool);
713         void finished (GdkEvent *, bool);
714         void aborted ();
715
716 private:
717         Operation _operation;
718         bool _copy;
719         int _original_pointer_time_axis;
720         int _last_pointer_time_axis;
721         std::list<TimeAxisView*> _added_time_axes;
722 };
723
724 /** Range marker drag */
725 class RangeMarkerBarDrag : public Drag
726 {
727 public:
728         enum Operation {
729                 CreateRangeMarker,
730                 CreateTransportMarker,
731                 CreateCDMarker
732         };
733
734         RangeMarkerBarDrag (Editor *, ArdourCanvas::Item *, Operation);
735
736         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
737         void motion (GdkEvent *, bool);
738         void finished (GdkEvent *, bool);
739         void aborted ();
740
741         bool allow_vertical_autoscroll () const {
742                 return false;
743         }
744
745         bool y_movement_matters () const {
746                 return false;
747         }
748
749 private:
750         void update_item (ARDOUR::Location *);
751
752         Operation _operation;
753         ArdourCanvas::SimpleRect* _drag_rect;
754         bool _copy;
755 };
756
757 /** Drag of rectangle to set zoom */
758 class MouseZoomDrag : public Drag
759 {
760 public:
761         MouseZoomDrag (Editor* e, ArdourCanvas::Item *i) : Drag (e, i) {}
762
763         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
764         void motion (GdkEvent *, bool);
765         void finished (GdkEvent *, bool);
766         void aborted ();
767 };
768
769 /** Drag of a range of automation data, changing value but not position */
770 class AutomationRangeDrag : public Drag
771 {
772 public:
773         AutomationRangeDrag (Editor *, ArdourCanvas::Item *, std::list<ARDOUR::AudioRange> const &);
774
775         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
776         void motion (GdkEvent *, bool);
777         void finished (GdkEvent *, bool);
778         void aborted ();
779
780         bool x_movement_matters () const {
781                 return false;
782         }
783
784 private:
785         std::list<ARDOUR::AudioRange> _ranges;
786         AutomationTimeAxisView* _atav;
787         boost::shared_ptr<AutomationLine> _line;
788         bool _nothing_to_drag;
789 };
790
791 #endif /* __gtk2_ardour_editor_drag_h_ */
792