6a1561ab708882bce6773be4e42137a0f69fd8e9
[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/types.h"
29
30 #include "canvas.h"
31 #include "editor_items.h"
32
33 namespace ARDOUR {
34         class Location;
35 }
36
37 class Editor;
38 class EditorCursor;
39 class TimeAxisView;
40
41 /** Abstract base class for dragging of things within the editor */
42 class Drag
43 {
44         
45 public:
46         Drag (Editor *, ArdourCanvas::Item *);
47         virtual ~Drag () {}
48
49         /** @return the canvas item being dragged */
50         ArdourCanvas::Item* item () const {
51                 return _item;
52         }
53
54         void swap_grab (ArdourCanvas::Item *, Gdk::Cursor *, uint32_t);
55         void break_drag ();
56
57         bool motion_handler (GdkEvent*, bool);
58
59         /** @return true if an end drag is in progress */
60         bool ending () const {
61                 return _ending;
62         }
63
64         /** @return true if the first move (past any move threshold) has occurred */
65         bool first_move () const {
66                 return _first_move;
67         }
68
69         /** @return current pointer x position in item coordinates */
70         double current_pointer_x () const {
71                 return _current_pointer_x;
72         }
73
74         /** @return current pointer y position in item coordinates */
75         double current_pointer_y () const {
76                 return _current_pointer_y;
77         }
78
79         /** @return current pointer frame */
80         nframes64_t current_pointer_frame () const {
81                 return _current_pointer_frame;
82         }
83
84         /** Called to start a grab of an item.
85          *  @param e Event that caused the grab to start.
86          *  @param c Cursor to use, or 0.
87          */
88         virtual void start_grab (GdkEvent* e, Gdk::Cursor* c = 0);
89
90         virtual bool end_grab (GdkEvent *);
91
92         /** Called when a drag motion has occurred.
93          *  @param e Event describing the motion.
94          */
95         virtual void motion (GdkEvent* e) = 0;
96
97         /** Called when a drag has finished.
98          *  @param e Event describing the finish.
99          */
100         virtual void finished (GdkEvent *) = 0;
101
102         /** @param m Mouse mode.
103          *  @return true if this drag should happen in this mouse mode.
104          */
105         virtual bool active (Editing::MouseMode m) {
106                 return (m != Editing::MouseGain);
107         }
108
109         /** Called when a subclass should update the editor's selection following a drag */
110         virtual void update_selection () {}
111
112 protected:
113         nframes64_t adjusted_current_frame () const;
114         
115         Editor* _editor; ///< our editor
116         ArdourCanvas::Item* _item; ///< our item
117         nframes64_t _pointer_frame_offset; ///< offset from the mouse's position for the drag
118                                            ///< to the start of the thing that is being dragged
119         nframes64_t _last_frame_position; ///< last position of the thing being dragged
120         nframes64_t _grab_frame; ///< frame that the mouse was at when start_grab was called, or 0
121         nframes64_t _last_pointer_frame; ///< frame that the pointer was at last time a motion occurred
122         nframes64_t _current_pointer_frame; ///< frame that the pointer is now at
123         double _original_x; ///< original world x of the thing being dragged
124         double _original_y; ///< original world y of the thing being dragged
125         double _grab_x; ///< item x of the grab start position
126         double _grab_y; ///< item y of the grab start position
127         double _current_pointer_x; ///< item x of the current pointer
128         double _current_pointer_y; ///< item y of the current pointer
129         double _last_pointer_x; ///< item x of the pointer last time a motion occurred
130         double _last_pointer_y; ///< item y of the pointer last time a motion occurred
131         bool _x_constrained; ///< true if x motion is constrained, otherwise false
132         bool _y_constrained; ///< true if y motion is constrained, otherwise false
133         bool _copy; ///< true if we're copying the things that we're dragging
134         bool _was_rolling; ///< true if the session was rolling before the drag started, otherwise false
135         bool _first_move; ///< true if some movement has occurred, otherwise false
136         bool _move_threshold_passed; ///< true if the move threshold has been passed, otherwise false
137         bool _want_move_threshold; ///< true if a move threshold should be applied, otherwise false
138
139 private:
140         
141         bool _ending; ///< true if end_grab is in progress, otherwise false
142 };
143
144
145 /** Abstract base class for drags that involve region(s) */
146 class RegionDrag : public Drag, public sigc::trackable
147 {
148 public:
149         RegionDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
150         virtual ~RegionDrag () {}
151
152         void update_selection ();
153
154 protected:
155         
156         RegionView* _primary; ///< the view that was clicked on (or whatever) to start the drag
157         std::list<RegionView*> _views; ///< all views that are being dragged
158
159 private:
160         void region_going_away (RegionView *);
161 };
162
163
164 /** Drags to move regions */
165 class RegionMoveDrag : public RegionDrag
166 {
167 public:
168         RegionMoveDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &, bool, bool);
169         virtual ~RegionMoveDrag () {}
170
171         virtual void start_grab (GdkEvent *, Gdk::Cursor *);
172         virtual void motion (GdkEvent *);
173         virtual void finished (GdkEvent *);
174
175 protected:
176
177         bool check_possible (RouteTimeAxisView **, ARDOUR::layer_t *);
178
179         TimeAxisView* _source_trackview;
180         ARDOUR::layer_t _source_layer;
181         TimeAxisView* _dest_trackview;
182         ARDOUR::layer_t _dest_layer;
183
184 private:
185
186         void copy_regions (GdkEvent *);
187         bool y_movement_disallowed (int, int, int, int, int, std::bitset<512> const &, std::vector<int32_t> const &) const;
188
189         bool _brushing;
190 };
191
192 /** Region drag in splice mode */
193 class RegionSpliceDrag : public RegionMoveDrag
194 {
195 public:
196         RegionSpliceDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
197
198         void motion (GdkEvent *);
199         void finished (GdkEvent *);
200 };
201
202 /** Drags to create regions */
203 class RegionCreateDrag : public Drag
204 {
205 public:
206         RegionCreateDrag (Editor *, ArdourCanvas::Item *, TimeAxisView *);
207
208         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
209         void motion (GdkEvent *);
210         void finished (GdkEvent *);
211
212 private:
213         TimeAxisView* _view;
214
215         TimeAxisView* _source_trackview;
216         ARDOUR::layer_t _source_layer;
217         TimeAxisView* _dest_trackview;
218         ARDOUR::layer_t _dest_layer;
219 };
220
221 /** Drag of region gain */
222 class RegionGainDrag : public Drag
223 {
224 public:
225         RegionGainDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
226
227         void motion (GdkEvent *);
228         void finished (GdkEvent *);
229         bool active (Editing::MouseMode m) {
230                 return (m == Editing::MouseGain);
231         }
232 };
233
234 /** Drag to trim region(s) */
235 class TrimDrag : public RegionDrag
236 {
237 public:
238         enum Operation {
239                 StartTrim,
240                 EndTrim,
241                 ContentsTrim,
242         };
243
244         TrimDrag (Editor *, ArdourCanvas::Item *, RegionView*, std::list<RegionView*> const &);
245
246         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
247         void motion (GdkEvent *);
248         void finished (GdkEvent *);
249
250 private:
251
252         Operation _operation;
253 };
254
255 /** Meter marker drag */
256 class MeterMarkerDrag : public Drag
257 {
258 public:
259         MeterMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
260
261         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
262         void motion (GdkEvent *);
263         void finished (GdkEvent *);
264
265 private:
266         MeterMarker* _marker;
267 };
268
269 /** Tempo marker drag */
270 class TempoMarkerDrag : public Drag
271 {
272 public:
273         TempoMarkerDrag (Editor *, ArdourCanvas::Item *, bool);
274
275         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
276         void motion (GdkEvent *);
277         void finished (GdkEvent *);
278
279 private:
280         TempoMarker* _marker;
281 };
282
283
284 /** Drag of a cursor */
285 class CursorDrag : public Drag
286 {
287 public:
288         CursorDrag (Editor *, ArdourCanvas::Item *, bool);
289
290         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
291         void motion (GdkEvent *);
292         void finished (GdkEvent *);
293
294 private:
295         EditorCursor* _cursor; ///< cursor being dragged
296         bool _stop; ///< true to stop the transport on starting the drag, otherwise false
297         
298 };
299
300 /** Region fade-in drag */
301 class FadeInDrag : public RegionDrag
302 {
303 public:
304         FadeInDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
305
306         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
307         void motion (GdkEvent *);
308         void finished (GdkEvent *);
309 };
310
311 /** Region fade-out drag */
312 class FadeOutDrag : public RegionDrag
313 {
314 public:
315         FadeOutDrag (Editor *, ArdourCanvas::Item *, RegionView *, std::list<RegionView*> const &);
316
317         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
318         void motion (GdkEvent *);
319         void finished (GdkEvent *);
320 };
321
322 /** Marker drag */
323 class MarkerDrag : public Drag
324 {
325 public:
326         MarkerDrag (Editor *, ArdourCanvas::Item *);
327         ~MarkerDrag ();
328
329         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
330         void motion (GdkEvent *);
331         void finished (GdkEvent *);
332
333 private:
334         void update_item (ARDOUR::Location *);
335         
336         Marker* _marker; ///< marker being dragged
337         std::list<ARDOUR::Location*> _copied_locations;
338         ArdourCanvas::Line* _line;
339         ArdourCanvas::Points _points;
340 };
341
342 /** Control point drag */
343 class ControlPointDrag : public Drag
344 {
345 public:
346         ControlPointDrag (Editor *, ArdourCanvas::Item *);
347
348         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
349         void motion (GdkEvent *);
350         void finished (GdkEvent *);
351
352 private:
353         
354         ControlPoint* _point;
355         double _cumulative_x_drag;
356         double _cumulative_y_drag;
357         static double const _zero_gain_fraction;
358 };
359
360 /** Gain or automation line drag */
361 class LineDrag : public Drag
362 {
363 public:
364         LineDrag (Editor *e, ArdourCanvas::Item *i);
365
366         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
367         void motion (GdkEvent *);
368         void finished (GdkEvent *);
369
370 private:
371
372         AutomationLine* _line;
373         uint32_t _before;
374         uint32_t _after;
375         double _cumulative_y_drag;
376 };
377
378 /** Dragging of a rubberband rectangle for selecting things */
379 class RubberbandSelectDrag : public Drag
380 {
381 public:
382         RubberbandSelectDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
383
384         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
385         void motion (GdkEvent *);
386         void finished (GdkEvent *);
387 };
388
389 /** Region drag in time-FX mode */
390 class TimeFXDrag : public RegionDrag
391 {
392 public:
393         TimeFXDrag (Editor *e, ArdourCanvas::Item *i, RegionView* p, std::list<RegionView*> const & v) : RegionDrag (e, i, p, v) {}
394
395         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
396         void motion (GdkEvent *);
397         void finished (GdkEvent *);
398 };
399
400 /** Drag in range selection mode */
401 class SelectionDrag : public Drag
402 {
403 public:
404         enum Operation {
405                 CreateSelection,
406                 SelectionStartTrim,
407                 SelectionEndTrim,
408                 SelectionMove
409         };
410
411         SelectionDrag (Editor *, ArdourCanvas::Item *, Operation);
412
413         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
414         void motion (GdkEvent *);
415         void finished (GdkEvent *);
416
417 private:
418         Operation _operation;
419 };
420
421 /** Range marker drag */
422 class RangeMarkerBarDrag : public Drag
423 {
424 public:
425         enum Operation {
426                 CreateRangeMarker,
427                 CreateTransportMarker,
428                 CreateCDMarker
429         };
430
431         RangeMarkerBarDrag (Editor *, ArdourCanvas::Item *, Operation);
432
433         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
434         void motion (GdkEvent *);
435         void finished (GdkEvent *);
436
437 private:
438         void update_item (ARDOUR::Location *);
439
440         Operation _operation;
441         ArdourCanvas::SimpleRect* _drag_rect;
442 };
443
444 /* Drag of rectangle to set zoom */
445 class MouseZoomDrag : public Drag
446 {
447 public:
448         MouseZoomDrag (Editor *e, ArdourCanvas::Item *i) : Drag (e, i) {}
449
450         void start_grab (GdkEvent *, Gdk::Cursor* c = 0);
451         void motion (GdkEvent *);
452         void finished (GdkEvent *);
453 };
454
455 #endif /* __gtk2_ardour_editor_drag_h_ */
456