1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit UserInterface.DragAndDrop.Manager;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Event,
Geometry._2d.Integer32.Point,
UserInterface.Control,
UserInterface.DragAndDrop.Action,
UserInterface.Keyboard.Event,
UserInterface.Mouse.Event;
type
TDragPhase = (None, ButtonDown, Dragging);
TDragAndDropManager = class(TObject)
protected
StartingPoint: TInteger32Point;
Source: TDragSite;
Target: TDragSite;
Phase: TDragPhase;
Actions: TDragDropActions;
procedure ClearTarget();
procedure DragDropMove();
procedure DragDropSwap();
function ControlWithPointOnScreen(Point: TInteger32Point): TControl;
procedure AdjustCursorForDragDrop(Actions: TDragDropActions);
procedure HandleDragStart(Position: TInteger32Point); overload;
procedure HandleDragStart(Control: TControl); overload;
function HandleDragMove(Position: TInteger32Point): TBoolean;
function HandleDragEnd(): TBoolean;
function HandleMouseButtonEvent(RootControl: TControl; Event: TMouseButtonEvent): TBoolean;
function HandleMouseMoveEvent(RootControl: TControl; Event: TMouseMoveEvent): TBoolean;
function HandleMouseEvent(RootControl: TControl; Event: TMouseEvent): TBoolean;
function HandleKeyboardEvent(Event: TKeyboardEvent): TBoolean;
procedure Stop(Successful: TBoolean);
public
MouseManager: TMouseManager;
function HandleEvent(Control: TControl; Event: TEvent): TBoolean;
function IsActive(): TBoolean;
procedure StartDrag(Control: TControl);
end;
implementation
uses
UserInterface.Keyboard.Key,
Windows.Api.UserInterface,
Windows.Control,
Windows.Gdi.Cursor;
{ TDragAndDropManager }
procedure TDragAndDropManager.AdjustCursorForDragDrop(Actions: TDragDropActions);
begin
if TDragDropAction.Swap in Actions then
MouseManager.SetCursor(CursorManager.DropSwap)
else if TDragDropAction.Move in Actions then
MouseManager.SetCursor(CursorManager.DropMove)
else
MouseManager.SetCursor(CursorManager.No);
end;
procedure TDragAndDropManager.ClearTarget();
begin
if Target <> nil then
begin
Target.Free();
Target := nil;
end;
end;
function TDragAndDropManager.ControlWithPointOnScreen(Point: TInteger32Point): TControl;
var
Offset: TInteger32Point;
begin
Result := FindControlWithHandle(WindowFromPoint(Point));
if Result <> nil then
begin
Dec(Point.X, Result.Left);
Dec(Point.Y, Result.Top);
if Result is TWindowsControl then
begin
Offset := (Result as TWindowsControl).Control.Position;
Dec(Point.X, Offset.X);
Dec(Point.Y, Offset.Y);
end;
Result := Result.DescendantWithPosition(Point);
end;
end;
procedure TDragAndDropManager.DragDropMove();
begin
Source.Detach();
Target.Attach(Source.Control);
end;
procedure TDragAndDropManager.DragDropSwap();
begin
Source.Detach();
Target.Detach();
Source.Attach(Target.Control);
Target.Attach(Source.Control);
end;
function TDragAndDropManager.HandleDragEnd(): TBoolean;
begin
Result := False;
if Phase <> TDragPhase.None then
begin
if (Phase = TDragPhase.Dragging) and (Target <> nil) then
begin
Result := True;
if TDragDropAction.Swap in Actions then
DragDropSwap()
else if TDragDropAction.Move in Actions then
DragDropMove();
end;
Stop(True);
end;
end;
function TDragAndDropManager.HandleDragMove(Position: TInteger32Point): TBoolean;
var
Control: TControl;
begin
if (Phase = TDragPhase.ButtonDown) and (StartingPoint.SquaredDistanceTo(Position) > 9) then
Phase := TDragPhase.Dragging;
if Phase = TDragPhase.Dragging then
begin
ClearTarget();
Result := True;
Actions := [];
Control := ControlWithPointOnScreen(Position);
if Control <> nil then
begin
Position := Control.ScreenToRelative(Position);
Target := nil;
while (Target = nil) and (Control <> nil) do
begin
Target := Control.CreateDragTarget(Source, Position);
if Control.ParentControl <> nil then
Position := Control.ParentControl.ConvertPositionFromChildPosition(Position, Control);
Control := Control.ParentControl;
end;
if Target <> nil then
Actions := Source.Actions * Target.Actions;
end;
AdjustCursorForDragDrop(Actions);
end
else
begin
Result := False;
Target := nil;
end;
end;
procedure TDragAndDropManager.HandleDragStart(Control: TControl);
begin
Source := Control.ParentControl.CreateDragSource(Control);
if Source <> nil then
begin
if Source.Actions <> [] then
MouseManager.ControlWithCapture := Source.Container
else
Stop(False);
end
else
Stop(False);
end;
procedure TDragAndDropManager.HandleDragStart(Position: TInteger32Point);
var
DragControl: TControl;
Control: TControl;
begin
DragControl := nil;
Control := ControlWithPointOnScreen(Position);
Position := Control.ScreenToRelative(Position);
while (DragControl = nil) and (Control <> nil) do
begin
DragControl := Control.GetDragControl(Position);
if Control.ParentControl <> nil then
Position := Control.ParentControl.ConvertPositionFromChildPosition(Position, Control);
Control := Control.ParentControl;
end;
if DragControl <> nil then
begin
Phase := TDragPhase.ButtonDown;
HandleDragStart(DragControl);
end;
end;
function TDragAndDropManager.HandleEvent(Control: TControl; Event: TEvent): TBoolean;
begin
if Event is TMouseEvent then
Result := HandleMouseEvent(Control, Event as TMouseEvent)
else
begin
Result := Phase = TDragPhase.Dragging;
if Result then
begin
if Event is TKeyboardEvent then
HandleKeyboardEvent(Event as TKeyboardEvent);
end;
end;
end;
function TDragAndDropManager.HandleKeyboardEvent(Event: TKeyboardEvent): TBoolean;
begin
Result := (Event is TKeyDownEvent) and ((Event as TKeyDownEvent).Key = TKey.Escape);
if Result then
Stop(False);
end;
function TDragAndDropManager.HandleMouseButtonEvent(RootControl: TControl; Event: TMouseButtonEvent): TBoolean;
begin
Result := False;
if Event.Button = Event.Mouse.Left then
begin
if Event is TMouseButtonDownEvent then
begin
StartingPoint := RootControl.RelativeToScreen(Event.Position);
HandleDragStart(StartingPoint);
end
else if (Phase <> TDragPhase.None) and (Event is TMouseButtonUpEvent) then
Result := HandleDragEnd();
end;
end;
function TDragAndDropManager.HandleMouseEvent(RootControl: TControl; Event: TMouseEvent): TBoolean;
begin
if Event is TMouseButtonEvent then
Result := HandleMouseButtonEvent(RootControl, Event as TMouseButtonEvent)
else if Event is TMouseMoveEvent then
Result := HandleMouseMoveEvent(RootControl, Event as TMouseMoveEvent)
else
Result := False;
end;
function TDragAndDropManager.HandleMouseMoveEvent(RootControl: TControl; Event: TMouseMoveEvent): TBoolean;
begin
Result := (Phase <> TDragPhase.None) and HandleDragMove(RootControl.RelativeToScreen(Event.Position));
end;
function TDragAndDropManager.IsActive(): TBoolean;
begin
Result := Phase = TDragPhase.Dragging;
end;
procedure TDragAndDropManager.StartDrag(Control: TControl);
begin
Phase := TDragPhase.Dragging;
HandleDragStart(Control);
end;
procedure TDragAndDropManager.Stop(Successful: TBoolean);
begin
Phase := TDragPhase.None;
if Source <> nil then
begin
Source.Free();
Source := nil;
end;
ClearTarget();
MouseManager.ControlWithCapture := nil;
end;
end.
|