File: JCFDropTarget.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (556 lines) | stat: -rw-r--r-- 15,109 bytes parent folder | download | duplicates (8)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
unit JCFDropTarget;

{ AFS 16 May 2K
  Got this unit as freeware from www.undu.com October 1998 page
  code by Thorsten Engler - Thorsten.Engler@gmx.net
  Renamed to JCFDropTarget to avoid name conflicts (peter3)
}

{(*}
(*------------------------------------------------------------------------------
 Delphi Code formatter source code 

The Original Code is JCFDropTarget, released May 2003.
The Initial Developer of the Original Code is Anthony Steele. 
Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
All Rights Reserved. 
Contributor(s): Anthony Steele. 

The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"). you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.mozilla.org/NPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied.
See the License for the specific language governing rights and limitations 
under the License.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL") 
See http://www.gnu.org/licenses/gpl.html
------------------------------------------------------------------------------*)
{*)}

{$I JcfGlobal.inc}

interface

uses
  Windows,
  Classes,
  ActiveX,
  Controls;

type
  TTeDropTarget    = class;
  TTeDropInterface = class;

  TTeDropTargetLifeState = (lsStart, lsExists, lsLocked, lsRegd);

  TTeDragOperation = (doNothing, doCopy, doMove, doLink);

  TTeComDragObject = class(TDragObject)
  private
    FDropInterface: TTeDropInterface;
    function GetDataObject: IDataObject;
    function GetDragOperation: TTeDragOperation;
    procedure SetDragOperation(Value: TTeDragOperation);
    function GetShiftState: TShiftState;
  public
    constructor Create(ADropInterface: TTeDropInterface); virtual;
    property DataObject: IDataObject Read GetDataObject;
    property DragOperation: TTeDragOperation Read GetDragOperation
      Write SetDragOperation;
    property ShiftState: TShiftState Read GetShiftState;
  end;

  TComDragObjectClass = class of TTeComDragObject;

  TTeDropInterface = class
  private
    function DoDragOver(DragMsg: TDragMessage): boolean;
    function DragTo(const Pos: TPoint): boolean;
    function DragFindTarget(const Pos: TPoint; var Handle: HWND): Pointer;
  protected
    FDropTarget: TTeDropTarget;
    FWinControl: TWinControl;
    FDataObject: IDataObject;
    FDragOperation: TTeDragOperation;
    FShiftState: TShiftState;
    FDragObject: TTeComDragObject;
  public
    property CFDropTarget: TTeDropTarget Read FDropTarget;

    constructor Create(AWinControl: TWinControl); virtual;
    destructor Destroy; override;
    procedure BeforeDestruction; override;

    function DropTarget_Create: HResult;
    function DropTarget_Destroy: HResult;
    function DropTarget_Exists: boolean;
  protected
    procedure DropTarget_Forget;
  public
    function DropTarget_LifeState: TTeDropTargetLifeState;

    function DragEnter(const dataObj: IDataObject; grfKeyState: longint;
      pt: TPoint; var dwEffect: longint): HResult; virtual;
    function DragOver(grfKeyState: longint; pt: TPoint;
      var dwEffect: longint): HResult; virtual;
    function DragLeave: HResult; virtual;
    function Drop(const dataObj: IDataObject; grfKeyState: longint;
      pt: TPoint; var dwEffect: longint): HResult; virtual;

    property DataObject: IDataObject Read FDataObject;
    property DragOperation: TTeDragOperation Read FDragOperation Write FDragOperation;
    property ShiftState: TShiftState Read FShiftState;
  end;

  TTeDropTarget = class(TInterfacedObject, IDropTarget)
  private
    FDropHWND: HWND;
    FDropWinControl: TWinControl;

    FDropInterface: TTeDropInterface;
    FLifeState: TTeDropTargetLifeState;

    procedure SetLifeState(Value: TTeDropTargetLifeState);
  public
    property DropHWND: HWND Read FDropHWnd;
    property DropWinControl: TWinControl Read FDropWinControl;
    property LifeState: TTeDropTargetLifeState Read FLifeState Write SetLifeState;

    constructor Create(AWinControl: TWinControl;
      ADropInterface: TTeDropInterface); virtual;
    procedure BeforeDestruction; override;

    function ToState_Exists: HResult;
    function ToState_Locked: HResult;
    function ToState_Regd: HResult;
  public
    { IDropTarget }
    function DragEnter(const dataObj: IDataObject; grfKeyState: longint;
      pt: TPoint; var dwEffect: longint): HResult; stdcall;
    function DragOver(grfKeyState: longint; pt: TPoint;
      var dwEffect: longint): HResult; stdcall;
    function DragLeave: HResult; stdcall;
    function Drop(const dataObj: IDataObject; grfKeyState: longint;
      pt: TPoint; var dwEffect: longint): HResult; stdcall;
  end;

var
  ComDragObjectClass: TComDragObjectClass;

const
  Effects: array[TTeDragOperation] of integer =
    (DROPEFFECT_NONE, DROPEFFECT_COPY, DROPEFFECT_MOVE, DROPEFFECT_LINK);

implementation

function DragMessage(Handle: HWND; Msg: TDragMessage; Source: TDragObject;
  Target: Pointer; const Pos: TPoint): longint;
var
  DragRec: TDragRec;
begin
  Result := 0;
  if Handle <> 0 then
  begin
    DragRec.Pos := Pos;
    DragRec.Target := Target;
    DragRec.Source := Source;
    DragRec.Docking := False;
    Result := SendMessage(Handle, CM_DRAG, longint(Msg), longint( @DragRec));
  end;
end;

function DragFindWindow(const Pos: TPoint): HWND;
begin
  Result := WindowFromPoint(Pos);
  while Result <> 0 do
    if not Assigned(FindControl(Result)) then
      Result := GetParent(Result)
    else
      Exit;
end;

function TTeDropInterface.DragFindTarget(const Pos: TPoint; var Handle: HWND): Pointer;
begin
  Handle := DragFindWindow(Pos);
  Result := Pointer(DragMessage(Handle, dmFindTarget, FDragObject, nil, Pos));
end;

function TTeDropInterface.DoDragOver(DragMsg: TDragMessage): boolean;
begin
  Result := False;
  if FDragObject.DragTarget <> nil then
    Result := longbool(DragMessage(FDragObject.DragHandle, DragMsg, FDragObject,
      FDragObject.DragTarget, FDragObject.DragPos));
end;

function TTeDropInterface.DragTo(const Pos: TPoint): boolean;
var
  Target: TControl;
  TargetHandle: HWND;
begin
  Target := DragFindTarget(Pos, TargetHandle);
  if Target <> FDragObject.DragTarget then
  begin
    DoDragOver(dmDragLeave);
    FDragObject.DragTarget := Target;
    FDragObject.DragHandle := TargetHandle;
    FDragObject.DragPos    := Pos;
    DoDragOver(dmDragEnter);
  end;
  FDragObject.DragPos := Pos;
  if FDragObject.DragTarget <> nil then
    FDragObject.DragTargetPos := TControl(FDragObject.DragTarget).ScreenToClient(Pos);
  Result := DoDragOver(dmDragMove);
end;

constructor TTeDropInterface.Create(AWinControl: TWinControl);
begin
  inherited Create;
  FWinControl := AWinControl;
  FDropTarget := nil;
  FDragObject := ComDragObjectClass.Create(Self);
end;

procedure TTeDropInterface.BeforeDestruction;
begin
  inherited;
  if Assigned(FDragObject) then
    FDragObject.FDropInterface := nil;
  if Assigned(FDropTarget) then
    FDropTarget.Free;
end;

function TTeDropInterface.DropTarget_Create: HResult;
begin
  Result := E_UNEXPECTED;
  try
    if not Assigned(FDropTarget) then
      FDropTarget := TTeDropTarget.Create(FWinControl, Self);
    if Assigned(FDropTarget) then
      Result := CFDropTarget.ToState_Regd;
  except
    Result := E_UNEXPECTED;
  end;
end;

function TTeDropInterface.DropTarget_Destroy: HResult;
begin
  Result := S_OK;
  try
    if Assigned(FDropTarget) then
      Result := CFDropTarget.ToState_Locked;
  except
    Result := E_UNEXPECTED;
  end;
end;

function TTeDropInterface.DropTarget_Exists: boolean;
begin
  Result := Assigned(FDropTarget);
end;

procedure TTeDropInterface.DropTarget_Forget;
begin
  FDropTarget := nil;
end;

function TTeDropInterface.DropTarget_LifeState: TTeDropTargetLifeState;
begin
  if DropTarget_Exists then
    Result := CFDropTarget.LifeState
  else
    Result := lsStart;
end;

function CreateShiftState(grfKeyState: longint): TShiftState;
begin
  Result := [];
  if (grfKeyState and MK_CONTROL) = MK_CONTROL then
    Include(Result, ssCtrl);
  if (grfKeyState and MK_SHIFT) = MK_SHIFT then
    Include(Result, ssShift);
//  if (grfKeyState and MK_ALT)     = MK_ALT     then Include (Result, ssAlt);
  if (grfKeyState and MK_LBUTTON) = MK_LBUTTON then
    Include(Result, ssLeft);
  if (grfKeyState and MK_MBUTTON) = MK_MBUTTON then
    Include(Result, ssMiddle);
  if (grfKeyState and MK_RBUTTON) = MK_RBUTTON then
    Include(Result, ssRight);
end;

function CreateDragOperation(ShiftState: TShiftState): TTeDragOperation;
begin
  Result := doMove; // muss noch gendert werden;
  if ssCtrl in ShiftState then
    Result := doCopy;
  if ssShift in ShiftState then
    Result := doMove;
  if (ssCtrl in ShiftState) and (ssShift in ShiftState) then
    Result := doLink;
end;

function TTeDropInterface.DragEnter(const dataObj: IDataObject;
  grfKeyState: longint; pt: TPoint; var dwEffect: longint): HResult;
begin
  Result   := S_OK;
  dwEffect := DROPEFFECT_NONE;
  if not Assigned(FWinControl) then
    exit;
  if not Assigned(FDragObject) then
    exit;
  try
    FShiftState    := CreateShiftState(grfKeyState);
    FDragOperation := CreateDragOperation(FShiftState);
    FDataObject    := dataObj;
    if not DragTo(pt) then
      FDragOperation := doNothing;
    dwEffect := Effects[FDragOperation];
  except
    Result := E_UNEXPECTED;
  end;
end;

function TTeDropInterface.DragOver(grfKeyState: longint; pt: TPoint;
  var dwEffect: longint): HResult;
begin
  Result   := S_OK;
  dwEffect := DROPEFFECT_NONE;
  if not Assigned(FWinControl) then
    exit;
  if not Assigned(FDragObject) then
    exit;
  try
    FShiftState    := CreateShiftState(grfKeyState);
    FDragOperation := CreateDragOperation(FShiftState);
    if not DragTo(pt) then
      FDragOperation := doNothing;
    dwEffect := Effects[FDragOperation];
  except
    Result := E_UNEXPECTED;
  end;
end;

function TTeDropInterface.DragLeave: HResult;
begin
  Result := S_OK;
  if not Assigned(FWinControl) then
    exit;
  if not Assigned(FDragObject) then
    exit;
  try
    DoDragOver(dmDragLeave);
    FDragObject.DragTarget := nil;
    FDragObject.DragHandle := 0;
    FDataObject := nil;
  except
    Result := E_UNEXPECTED;
  end;
end;

function TTeDropInterface.Drop(const dataObj: IDataObject; grfKeyState: longint;
  pt: TPoint; var dwEffect: longint): HResult;
begin
  Result   := S_OK;
  dwEffect := DROPEFFECT_NONE;
  if not Assigned(FWinControl) then
    exit;
  if not Assigned(FDragObject) then
    exit;
  try
    FDataObject := dataObj;
    try
      FShiftState    := CreateShiftState(grfKeyState);
      FDragOperation := CreateDragOperation(FShiftState);
      if not DragTo(pt) then
        FDragOperation := doNothing;
      dwEffect := Effects[FDragOperation];
      if FDragOperation <> doNothing then
        DoDragOver(dmDragDrop);
    finally
      FDataObject := nil;
    end;
  except
    Result := E_UNEXPECTED;
  end;
end;

constructor TTeDropTarget.Create(AWinControl: TWinControl;
  ADropInterface: TTeDropInterface);
begin
  inherited Create;
  FDropWinControl := AWinControl;
  FDropInterface  := ADropInterface;
  FLifeState      := lsExists;
end;

procedure TTeDropTarget.BeforeDestruction;
begin
  if Assigned(FDropInterface) then
    FDropInterface.DropTarget_Forget;

  if FLifeState > lsLocked then
  begin
    while RefCount < 2 do
      _AddRef;
    ActiveX.RevokeDragDrop(FDropHWND);
    FDropHWND  := 0;
    FLifeState := lsLocked;
  end;

  if FLifeState > lsExists then
  begin
    while RefCount < 2 do
      _AddRef;
    ActiveX.CoLockObjectExternal(Self as IDropTarget, False, False);
    FLifeState := lsExists;
  end;
end;

function TTeDropTarget.ToState_Exists: HResult;
begin
  Result := S_OK;
  if LifeState = lsRegd then
    Result := ToState_Locked;

  if LifeState = lsLocked then
  begin
    LifeState := lsExists;
    Result    := ActiveX.CoLockObjectExternal(Self as IDropTarget, False, True);
  end;
end;

function TTeDropTarget.ToState_Locked: HResult;
begin
  Result := S_OK;

  if LifeState = lsExists then
  begin
    Result := ActiveX.CoLockObjectExternal(Self as IDropTarget, True, False);
    if Result = S_OK then
      LifeState := lsLocked;
  end;

  if LifeState = lsRegd then
  begin
    while RefCount < 2 do
      _AddRef;
    Result    := ActiveX.RevokeDragDrop(FDropHWND);
    FDropHWND := 0;
    if Result = S_OK then
      LifeState := lsLocked;
  end;
end;

function TTeDropTarget.ToState_Regd: HResult;
begin
  Result := S_OK;
  if LifeState = lsExists then
    Result := ToState_Locked;
  if LifeState = lsLocked then
  begin
    FDropHWND := FDropWinControl.Handle;
    Result    := ActiveX.RegisterDragDrop(FDropHWND, Self as IDropTarget);
    if Result = S_OK then
      LifeState := lsRegd;
  end;
end;

procedure TTeDropTarget.SetLifeState(Value: TTeDropTargetLifeState);
begin
  FLifeState := Value;
end;

function TTeDropTarget.DragEnter(const dataObj: IDataObject;
  grfKeyState: longint; pt: TPoint; var dwEffect: longint): HResult;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.DragEnter(dataObj, grfKeyState, pt, dwEffect)
  else
    Result := E_UNEXPECTED;
end;

function TTeDropTarget.DragOver(grfKeyState: longint; pt: TPoint;
  var dwEffect: longint): HResult;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.DragOver(grfKeyState, pt, dwEffect)
  else
    Result := E_UNEXPECTED;
end;

function TTeDropTarget.DragLeave: HResult;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.DragLeave
  else
    Result := E_UNEXPECTED;
end;

function TTeDropTarget.Drop(const dataObj: IDataObject; grfKeyState: longint;
  pt: TPoint; var dwEffect: longint): HResult;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.Drop(dataObj, grfKeyState, pt, dwEffect)
  else
    Result := E_UNEXPECTED;
end;

{ TTeComDragObject }

constructor TTeComDragObject.Create(ADropInterface: TTeDropInterface);
begin
  inherited Create;
  FDropInterface := ADropInterface;
end;

function TTeComDragObject.GetDataObject: IDataObject;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.DataObject
  else
    Result := nil;
end;

function TTeComDragObject.GetDragOperation: TTeDragOperation;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.DragOperation
  else
    Result := doNothing;
end;

function TTeComDragObject.GetShiftState: TShiftState;
begin
  if Assigned(FDropInterface) then
    Result := FDropInterface.ShiftState
  else
    Result := [];
end;

procedure TTeComDragObject.SetDragOperation(Value: TTeDragOperation);
begin
  if Assigned(FDropInterface) then
    FDropInterface.DragOperation := Value;
end;

destructor TTeDropInterface.Destroy;
begin
  if Assigned(FDragObject) then
  begin
    FDragObject.Free;
    FDragObject := nil;
  end;
  inherited;
end;

initialization
  ComDragObjectClass := TTeComDragObject;
  OleInitialize(nil);

finalization
  OleUninitialize;
end.