-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPJDropFiles.pas
More file actions
1806 lines (1663 loc) · 60.9 KB
/
Copy pathPJDropFiles.pas
File metadata and controls
1806 lines (1663 loc) · 60.9 KB
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 1998-2014, Peter Johnson (www.delphidabbler.com).
*
* DelphiDabbler Drop Files Components run time code. Contains components that
* can "catch" files dragged and dropped from Windows Explorer along with
* secondary components used to filter dropped files.
}
unit PJDropFiles;
interface
// Set conditionally defined symbols and switch off certain warnings
{$UNDEF DELPHI4ANDUP}
{$UNDEF DELPHI6ANDUP}
{$UNDEF DELPHI2005ANDUP}
{$UNDEF DELPHIXE2ANDUP}
{$IFDEF VER120} // Delphi 4
{$DEFINE DELPHI4ANDUP}
{$ENDIF}
{$IFDEF VER125} // C++ Builder 4
{$DEFINE DELPHI4ANDUP}
{$ENDIF}
{$IFDEF VER130} // Delphi 5
{$DEFINE DELPHI4ANDUP}
{$ENDIF}
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 24.0} // Delphi XE3 and later
{$LEGACYIFEND ON} // NOTE: this must come before all $IFEND directives
{$IFEND}
{$IF CompilerVersion >= 23.0} // Delphi XE2 and later
{$DEFINE DELPHIXE2ANDUP}
{$IFEND}
{$IF CompilerVersion >= 17.0} // Delphi 2005 and later
{$DEFINE DELPHI2005ANDUP}
{$IFEND}
{$IF CompilerVersion >= 15.0} // Delphi 7 and later
{$WARN UNSAFE_CAST OFF}
{$WARN SYMBOL_PLATFORM OFF}
{$IFEND}
{$IF CompilerVersion >= 14.0} // Delphi 6 and later
{$DEFINE DELPHI4ANDUP}
{$DEFINE DELPHI6ANDUP}
{$IFEND}
{$ENDIF}
uses
// Delphi
{$IFNDEF DELPHIXE2ANDUP}
Windows, Messages, Classes, Controls;
{$ELSE}
Winapi.Windows, Winapi.Messages, System.Classes, Vcl.Controls;
{$ENDIF}
type
{
TPJFileFilter:
Abstract base class for components that filter file names according to
various criteria. These components are designed for use with the various
drop files components to filter the dropped files. This class defines an
abstract method that is called when files are dropped by the drop files
components when a filter component is referenced.
}
TPJFileFilter = class(TComponent)
public
function Accept(const FilePath: string; const IsFolder: Boolean): Boolean;
virtual; abstract;
{Returns true if the given file (or folder - per IsFolder) passes through
the filter and false if not}
end;
{
TPJExtFileFilterStyle:
Values of TPJExtFileFilter component's Style property.
}
TPJExtFileFilterStyle = (
fsFilterFilesOnly, // files are filtered by extension, folders pass thru
fsFilterFoldersOnly, // folders are filtered by extension, files pass thru
fsAll // filter is applied to files and folders
);
{
TPJExtFileFilter:
Filter component used to filter out files that do not have extensions that
match in a given list of extensions. The component is designed for use with
drop files components.
}
TPJExtFileFilter = class(TPJFileFilter)
private // properties
fExtensions: TStringList;
fStyle: TPJExtFileFilterStyle;
function GetExtensions: string;
procedure SetExtensions(const Value: string);
public
constructor Create(AOwner: TComponent); override;
{Class constructor: creates component and sets default property values}
destructor Destroy; override;
{Class destructor: frees owned objects}
function Accept(const FilePath: string; const IsFolder: Boolean): Boolean;
override;
{Check the given file or folder (per IsFolder) and return true if the
given file is of required type (per Style property) and has extension that
matches one of those specified in Extensions property}
published
property Extensions: string
read GetExtensions write SetExtensions;
{List of valid extensions (';' separated): only files with an extension in
the the list passes the filter unless Extensions = '', when all files
pass through}
property Style: TPJExtFileFilterStyle
read fStyle write fStyle default fsFilterFilesOnly;
{Style of filter applied by component. For explanation of values see
comments on TPJExtFileFilterStyle}
end;
{
TPJWildCardFileFilter:
Filter component used to filter out files that do not have a file name
matching a wild card.
}
TPJWildCardFileFilter = class(TPJFileFilter)
private // properties
fWildCard: string;
procedure SetWildCard(const Value: string);
private
fWildCardChanged: Boolean;
{Used to detect if wild card has changed since last call to Accept}
fMatchingFiles: TStringList;
{List of files in current folder that match wild-card}
fCurrentFolder: string;
{Current folder that list of matching files comes from}
public
constructor Create(AOwner: TComponent); override;
{Class constructor: creates component and owned object}
destructor Destroy; override;
{Class destructor: frees owned objects}
function Accept(const FilePath: string; const IsFolder: Boolean): Boolean;
override;
{Check the given file or folder (per IsFolder) and return true if the
given file matches wild card in current folder}
published
property WildCard: string read fWildCard write SetWildCard;
{Files in a folder that match the given wild-card pass through the filter,
while those that don't match the wild card do not. Wild-card is in usual
DOS format - '?' and '*' are supported}
end;
{
TPJDroppedFileFilter:
Type of method used to handle OnFileFilter events. FileName is name of a
file, IsFolder tells if FileName is a file or folder. Accept is set to true
on calling the event handler: resetting to False means the file is filtered
out and not included in list of dropped files.
}
TPJDroppedFileFilter = procedure(Sender: TObject; const FileName: string;
const IsFolder: Boolean; var Accept: Boolean) of object;
{
TPJDropFilesOption:
Enumeration containing all possible values for Options property.
}
TPJDropFilesOption = (
dfoIncFolders, // include folders in list of dropped files
dfoIncFiles, // include files in list of dropped filed
dfoRecurseFolders // recurse through all folder in list of dropped files
);
{
TPJDropFilesOptions:
Set containing possible values of Options property.
}
TPJDropFilesOptions = set of TPJDropFilesOption;
{
TPJAbstractDropFilesHelper:
Helper class that implements interface to windows drag drop events and other
common functionality for two drop files classes. This can't be done in
common base class for drop files classes since classes inherit from
different components.
}
TPJAbstractDropFilesHelper = class(TObject)
private
fComp: TComponent;
{Reference to the component the helper is working with}
protected
function GetHWnd: THandle; virtual;
{Returns window handle that related control receives messages from. This
is window handle of container returned by GetContainer method}
function GetContainer: TWinControl; virtual; abstract;
{Abstract method to return reference to the related windowed control that
the component works with}
procedure HandleFile(const FileName: string);
{Processes the given file and updates file list according to
RecurseFolders and ExcludeFolders properties. If ExcludeFolders is true
then any folder names encountered are excluded from file list. If
RecurseFolders is true then any folders and examined and the files in the
folder are added to list (taking note of ExcludeFolders). Any sub-folders
are also searched recursively}
public
fForeGroundOnDrop: Boolean;
{Value of related control's ForegroundOnDrop property: determines if
window is brought to foreground when files dropped}
fOnDropFiles: TNotifyEvent;
{Value of related control's OnDropFiles event handler: triggered when
files dropped and after they have been processed}
fOnBeforeDrop: TNotifyEvent;
{Value of related control's OnBeforeDrop property: triggered when files
dropped but before they are processed. Files property has no defined value
when this event is triggered}
fOnAfterDrop: procedure(hDrop: THandle) of object;
{Event triggered after drop is complete but before drop handle is
destroyed: this permits associated controls to perform additional
processing with drop handle if required}
fOnFileFilter: TPJDroppedFileFilter;
{Event triggered when files are dropped for each dropped file (after
checking required types per Options property. If Accept parameter of
event handler is set to False the file is not included in the list of
dropped files. This event handler is not called if a filter component
filters out the file}
fFilterComp: TPJFileFilter;
{Reference to component used to filter the list of dropped files. This is
used before the OnFileFilter event is triggered}
fDropPoint: TPoint;
{Point at which drop occurred: can be changed by related components if
necessary}
fFileList: TStringList;
{List of files dropped}
fOptions: TPJDropFilesOptions;
{Value related to control's Options property. The options apply to the
handling of files and folders: determines if files and/or folders are
listed and if folders are recursed}
function GetDropControl: TControl;
{Returns reference to control at position where cursor was released, or
nil if no control under drop point}
constructor Create(Comp: TComponent);
{Class constructor: records reference to related component and helper
object}
destructor Destroy; override;
{Class destructor: frees helper object}
procedure AcceptFiles(Flag: Boolean);
{Calls DragAcceptFiles with given value, if not designing and if related
window handle exists}
procedure FilesDropped(hDrop: THandle);
{Uses drop handle to get list of dropped files and to set drop point.
Triggers OnAfterDrop event before drop handle is destroyed}
procedure DropFiles; virtual;
{Called after dropped files are processed and drop handle has been freed.
Triggers OnDropFiles event and brings drop target to front if required}
procedure BeforeDrop; virtual;
{Called before dropped files are processed. Triggers OnBeforeDrop event}
end;
{
TPJDropFilesHelper:
Helper class customised to TPJDropFiles.
}
TPJDropFilesHelper = class(TPJAbstractDropFilesHelper)
protected
function GetContainer: TWinControl; override;
{Returns reference to the component's related windowed control: this is
the control itself in this class}
end;
{
TPJFormDropFilesHelper:
Helper class customised to TPJFormDropFiles.
}
TPJFormDropFilesHelper = class(TPJAbstractDropFilesHelper)
protected
function GetContainer: TWinControl; override;
{Returns reference to the component's related windowed control: this is
the form on which owns the component}
end;
{
TPJCtrlDropFilesHelper:
Helper class customised to TPJCtrlDropFiles
}
TPJCtrlDropFilesHelper = class(TPJAbstractDropFilesHelper)
protected
function GetContainer: TWinControl; override;
{Returns reference to the control managed by the related component}
end;
{
TPJDropFiles:
Component that provides a container window which catches files dropped on it
or any components parented by container.
}
TPJDropFiles = class(TCustomControl)
private // properties
fPassThrough: Boolean;
function GetCount: Integer;
function GetFile(Idx: Integer): string;
function GetDropControl: TControl;
function GetFileName: string;
function GetOnDropFiles: TNotifyEvent;
procedure SetOnDropFiles(const Value: TNotifyEvent);
function GetDropPoint: TPoint;
function GetForegroundOnDrop: Boolean;
procedure SetForegroundOnDrop(const Value: Boolean);
function GetOnBeforeDrop: TNotifyEvent;
procedure SetOnBeforeDrop(const Value: TNotifyEvent);
function GetOptions: TPJDropFilesOptions;
procedure SetOptions(const Value: TPJDropFilesOptions);
function GetIsFolder(Idx: Integer): Boolean;
function GetOnFileFilter: TPJDroppedFileFilter;
procedure SetOnFileFilter(const Value: TPJDroppedFileFilter);
function GetFilter: TPJFileFilter;
procedure SetFilter(const Value: TPJFileFilter);
private // other
fHelper: TPJDropFilesHelper;
{Helper object that undertakes most of drag drop handling}
protected
procedure CreateWnd; override;
{Window creation method - registers that window can accept dropped files}
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
{Sets Filter property to nil when referenced component is destroyed}
procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
{File drop message handler - used to record info about dropped files}
procedure CMEnabledChanged(var Msg: TMessage); message CM_ENABLEDCHANGED;
{Message triggered when Enabled property changes - used to toggle whether
files can be accepted or not}
procedure DoPassThrough(hDrop: THandle);
{Event handler called by helper class that passes a customised drop files
message on to owner control when PassThrough property is true}
public
constructor Create(AOwner: TComponent); override;
{Control constructor - sets default values of properties and creates owned
helper object}
destructor Destroy; override;
{Control destructor - frees owned helper object}
procedure Paint; override;
{Control paint handler - draws dashed outline to control only when
designing}
property Count: Integer read GetCount;
{The number of files dropped}
property Files[Idx: Integer]: string read GetFile;
{The names of the dropped files}
property FileName: string read GetFileName;
{The name of the first file dropped, if any, otherwise empty string}
property IsFolder[Idx: Integer]: Boolean read GetIsFolder;
{Whether the dropped files in Files property are folders (true) or files
(false)}
property DropPoint: TPoint read GetDropPoint;
{The mouse coordinates where the last files were dropped}
property DropControl: TControl read GetDropControl;
{The child control (if any) under the mouse when files are dropped -
returns nil if no control under mouse}
published
{ Inherited protected properties published }
property Enabled stored True;
{Enables/disables ability to receive dropped files}
property Align;
{$IFDEF DELPHI4ANDUP}
property Anchors;
property Constraints;
{$ENDIF}
property ParentShowHint;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
{ Inherited published properties given new defaults }
property Width default 80;
property Height default 60;
{ New properties }
property Filter: TPJFileFilter
read GetFilter write SetFilter;
{Reference to a file filter component used to automatically filter dropped
files. TPJFileFilter is an abstract class and concrete derived classes
that provide different filtering options can be assigned to this property}
property ForegroundOnDrop: Boolean
read GetForegroundOnDrop write SetForegroundOnDrop;
{When true the window containing the component is brought to front after
files have been dropped}
property Options: TPJDropFilesOptions
read GetOptions write SetOptions;
{Set of options that govern how dropped files are processed: whether
simple files or folders are included in Files property and whether any
folders are recursed}
property PassThrough: Boolean
read fPassThrough write fPassThrough;
{Passes drop information on to main window as a custom message: can be
intercepted by TPJFormDropFiles}
{ New events }
property OnBeforeDrop: TNotifyEvent
read GetOnBeforeDrop write SetOnBeforeDrop;
{Event triggered just as drop occurs - before files have been processed
(Files property is not valid at this point and shouldn't be accessed)}
property OnDropFiles: TNotifyEvent
read GetOnDropFiles write SetOnDropFiles;
{Event triggered when files have been dropped and have been processed (
Files property has been updated)}
property OnFileFilter: TPJDroppedFileFilter
read GetOnFileFilter write SetOnFileFilter;
{Event triggered for each file dropped to enable user to filter out
unwanted files or folders by setting event handler's Accept parameter to
false}
end;
{
TPJSubClassedDropFiles:
Base class for drop files components handle drop files for an associated
TWinControl by sub-classing the associated control's window.
}
TPJSubClassedDropFiles = class(TComponent)
private // properties
fHelper: TPJAbstractDropFilesHelper;
fEnabled: Boolean;
function GetCount: Integer;
function GetDropControl: TControl;
function GetDropPoint: TPoint;
function GetFile(Idx: Integer): string;
function GetFileName: string;
function GetFilter: TPJFileFilter;
function GetForegroundOnDrop: Boolean;
function GetIsFolder(Idx: Integer): Boolean;
function GetOnBeforeDrop: TNotifyEvent;
function GetOnDropFiles: TNotifyEvent;
function GetOnFileFilter: TPJDroppedFileFilter;
function GetOptions: TPJDropFilesOptions;
procedure SetEnabled(const Value: Boolean);
procedure SetFilter(const Value: TPJFileFilter);
procedure SetForegroundOnDrop(const Value: Boolean);
procedure SetOnBeforeDrop(const Value: TNotifyEvent);
procedure SetOnDropFiles(const Value: TNotifyEvent);
procedure SetOnFileFilter(const Value: TPJDroppedFileFilter);
procedure SetOptions(const Value: TPJDropFilesOptions);
protected
function CreateAndInitHelper: TPJAbstractDropFilesHelper; virtual; abstract;
{Creates and initializes helper object of appropriate type for component}
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
{Sets Filter property to nil when referenced component is destroyed}
property Helper: TPJAbstractDropFilesHelper
read fHelper write fHelper;
{Helper class that undertakes most of drag drop handling}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Creates and initialises component}
destructor Destroy; override;
{Class destructor. Tears down component}
{ Run time properties }
property Count: Integer read GetCount;
{The number of files dropped}
property DropControl : TControl read GetDropControl;
{The child control (if any) under the mouse when files are dropped -
returns nil if no control under mouse}
property DropPoint: TPoint read GetDropPoint;
{The mouse coordinates where the last files were dropped}
property FileName: string read GetFileName;
{The name of the first file dropped, if any, otherwise empty string}
property Files[Idx: Integer]: string read GetFile;
{The names of the dropped files}
property IsFolder[Idx: Integer]: Boolean read GetIsFolder;
{Whether the dropped files in Files property are folders (true) or files
(false)}
published
{ Properties }
property Enabled: Boolean
read fEnabled write SetEnabled default True;
{Enables/disables ability to receive dropped files}
property Filter: TPJFileFilter
read GetFilter write SetFilter;
{Reference to a file filter component used to automatically filter dropped
files. TPJFileFilter is an abstract class and concrete derived classes
that provide different filtering options can be assigned to this property}
property ForegroundOnDrop: Boolean
read GetForegroundOnDrop write SetForegroundOnDrop;
{When true the window containing the component is brought to front after
files have been dropped}
property Options: TPJDropFilesOptions
read GetOptions write SetOptions;
{Set of options that govern how dropped files are processed: whether
simple files or folders are included in Files property and whether any
folders are recursed}
{ Events }
property OnBeforeDrop: TNotifyEvent
read GetOnBeforeDrop write SetOnBeforeDrop;
{Event triggered just as drop occurs - before files have been processed
(Files property is not valid at this point and shouldn't be accessed)}
property OnDropFiles: TNotifyEvent
read GetOnDropFiles write SetOnDropFiles;
{Event triggered when files have been dropped and have been processed (
Files property has been updated)}
property OnFileFilter: TPJDroppedFileFilter
read GetOnFileFilter write SetOnFileFilter;
{Event triggered for each file dropped to enable user to filter out
unwanted files or folders by setting event handler's Accept parameter to
false}
end;
{
TPJFormDropFiles:
Component that subclasses owner form window to provide ability to catch
files dropped anywhere on form window.
}
TPJFormDropFiles = class(TPJSubClassedDropFiles)
private
fOldWndProc, fNewWndProc: Pointer;
{Reference to old window procedure and replacement used to trap file drop
messages}
protected
procedure NewWndProc(var Msg: TMessage); virtual;
{Window procedure used to sub-class owning form's window procedure to
enable drop-file events to be handled}
function FormHandle: THandle;
{Returns window handle of owner form, or 0 if there is no owner}
function CreateAndInitHelper: TPJAbstractDropFilesHelper; override;
{Creates associated helper object}
public
constructor Create(AOwner: TComponent); override;
{Component constructor. Creates component, ensuring that owner is a form
and that no more that one of these components is present on the form}
destructor Destroy; override;
{Component destructor. Tears down component}
end;
{
TPJCtrlDropFiles:
Component that sub classes an associated controls window to provide ability
to catch files dropped anywhere on associated control.
}
TPJCtrlDropFiles = class(TPJSubClassedDropFiles)
private
fManagedControl: TWinControl;
{Storage for ManagedControl property}
fPassThrough: Boolean;
{Storage for PassThrough property}
fOldWndProc, fNewWndProc: Pointer;
{Reference to old window procedure and replacement used to trap file drop
messages in handled control}
procedure SetManagedControl(const Value: TWinControl);
{Write access method for ManagedControl property}
procedure RemoveManagedControl;
{Removes currently managed control by switching off drop handling and
restoring original window procedure}
function ManagedControlHandle: THandle;
{Returns handle of managed control or 0 if there is no managed control}
function ClientToScreen(ClientPos: TPoint): TPoint;
{Converts co-ordinates in terms of managed control's client area to screen
co-ordinates}
protected
function CreateAndInitHelper: TPJAbstractDropFilesHelper; override;
{Creates associated drop files helper object and sets up its OnAfterDrop
event handler to enable messages to be passed through to the main form}
procedure DoPassThrough(hDrop: THandle);
{Event handler called by helper class that passes a customised drop files
message on to owner control when PassThrough property is true}
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
{Sets ManagedComponent property to nil when referenced component is
destroyed}
procedure NewWndProc(var Msg: TMessage); virtual;
{Window procedure used to sub-class managed control's window procedure to
enable drop-file events to be handled}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Creates in initializes component}
destructor Destroy; override;
{Class destructor. Tears down component}
published
{ Properties }
property ManagedControl: TWinControl
read fManagedControl write SetManagedControl;
{Reference to associated control that we are managing drag and drop
operations for}
property PassThrough: Boolean
read fPassThrough write fPassThrough default False;
{Passes drop information on to main window as a custom message: can be
intercepted by TPJFormDropFiles}
end;
implementation
uses
// Delphi
{$IFDEF DELPHI2005ANDUP}Types, {for inlining}{$ENDIF}
SysUtils, Graphics, ShellAPI, Forms;
resourcestring
// Error messages
sOwnerNotForm = 'TPJFormDropFiles owner must be a form';
sOnlyOneAllowed = 'Only one TPJFormDropFiles component is permitted on a '
+ 'form: %0:s is already present on %1:s';
var
// Id of private, unique, PJ_DROPFILES message
pvtPJDropFilesMsg: UINT;
function SetWndProc(HWnd: THandle; NewWndProc: Pointer): Pointer;
{Sets the window procedure of a given window in a a 32 and 64 bit safe way.
@param HWnd [in] Handle of window to receive new window procedure.
@param NewWndProc [in] Pointer to new window procedure.
@returns Pointer to former window procedure.
}
begin
{$IFDEF WIN64}
Result := Pointer(
SetWindowLongPtr(HWnd, GWLP_WNDPROC, LONG_PTR(NewWndProc))
);
{$ELSE}
Result := Pointer(
SetWindowLong(HWnd, GWL_WNDPROC, Integer(NewWndProc))
);
{$ENDIF}
end;
procedure SendPJDropFilesMsg(const HWnd: THandle; const HDrop: THandle;
const Pos: TPoint);
{Sends a PJDropFiles custom message in a 32 and 64 bit safe way.
@param HWnd [in] Handle of window receiving message.
@param HDrop [in] Drop file handle.
@param Pos [in] Position of file drop.
}
{$IFDEF WIN64}
begin
// LPARAM cast requires SizeOf(Pos) = SizeOf(LPARAM)
SendMessage(HWnd, pvtPJDropFilesMsg, WPARAM(HDrop), LPARAM(Pos));
end;
{$ELSE}
var
SmallPos: TSmallPoint; // Pos converted to a TSmallPoint
begin
SmallPos := PointToSmallPoint(Pos);
// LPARAM cast requires SizeOf(SmallPos) = SizeOf(LPARAM)
SendMessage(HWnd, pvtPJDropFilesMsg, WPARAM(HDrop), LPARAM(SmallPos));
end;
{$ENDIF}
procedure FreeObjectInst(AObjectInstance: Pointer);
{Wrapper function that frees the given object instance.
NOTE: Provided to centralise the conditional compilation needed to access
the VCL's FreeObjectInstance routine}
begin
{$IFDEF DELPHI6ANDUP}
{$IFDEF DELPHIXE2ANDUP}
System.
{$ENDIF}
Classes
{$ELSE}
Forms
{$ENDIF}
.FreeObjectInstance(AObjectInstance);
end;
function MakeObjectInst(AMethod: TWndMethod): Pointer;
{Wrapper function that allocates a block of memory to be used by an instance
of a class.
NOTE: Provided to centralise the conditional compilation needed to access
the VCL's MakeObjectInstance routine}
begin
Result :=
{$IFDEF DELPHI6ANDUP}
{$IFDEF DELPHIXE2ANDUP}
System.
{$ENDIF}
Classes
{$ELSE}
Forms
{$ENDIF}
.MakeObjectInstance(AMethod);
end;
{ TPJDropFiles }
procedure TPJDropFiles.CMEnabledChanged(var Msg : TMessage);
{Message triggered when Enabled property changes - used to toggle whether
files can be accepted or not}
begin
inherited;
fHelper.AcceptFiles(Enabled);
end;
constructor TPJDropFiles.Create(AOwner : TComponent);
{Control constructor - sets default values of properties and creates owned
helper object}
begin
inherited Create(AOwner);
// Make control accept child controls
ControlStyle := ControlStyle + [csAcceptsControls];
// Set default size
Width := 80;
Height := 60;
// Ensure we're not a tab-stop
TabStop := False;
// Create helper object and set pass through handler
fHelper := TPJDropFilesHelper.Create(Self);
fHelper.fOnAfterDrop := DoPassThrough;
end;
procedure TPJDropFiles.CreateWnd;
{Window creation method - registers that window can accept dropped files}
begin
inherited CreateWnd;
fHelper.AcceptFiles(Enabled);
end;
destructor TPJDropFiles.Destroy;
{Control destructor - frees owned helper object}
begin
fHelper.Free;
inherited Destroy;
end;
procedure TPJDropFiles.DoPassThrough(hDrop: THandle);
{Event handler called by helper class that passes a customised drop files
message on to owner control when PassThrough property is true}
var
OwnerCtrl: TWinControl; // control that owns this component
ScreenPos: TPoint; // screen position of drop point
OwnerPos: TPoint; // position of drop point relative to owner control
begin
if fPassThrough
and Visible
and Assigned(Owner)
and (Owner is TWinControl) then
begin
// Find ultimate owning form
OwnerCtrl := Owner as TWinControl;
while Assigned(OwnerCtrl) and Assigned(OwnerCtrl.Owner)
and (OwnerCtrl.Owner is TWinControl) do
OwnerCtrl := OwnerCtrl.Owner as TWinControl;
// Calculate drop position relative to owner control's window
ScreenPos := ClientToScreen(fHelper.fDropPoint);
OwnerPos := OwnerCtrl.ScreenToClient(ScreenPos);
// Send the custom message to owner form containing new drop position
SendPJDropFilesMsg(OwnerCtrl.Handle, hDrop, OwnerPos);
end;
end;
function TPJDropFiles.GetCount : integer;
{Read access method for Count property - gets number of files dropped}
begin
Result := fHelper.fFileList.Count;
end;
function TPJDropFiles.GetDropControl : TControl;
{Read access method for DropControl property - returns child control which was
under mouse when files were dropped, or nil if no child control was under
mouse}
begin
Result := fHelper.GetDropControl;
end;
function TPJDropFiles.GetDropPoint: TPoint;
{Read access method for DropPoint property}
begin
Result := fHelper.fDropPoint;
end;
function TPJDropFiles.GetFile(Idx: Integer): string;
{Read access method for Files array property - returns name of file at the
given array index}
begin
Result := fHelper.fFileList[Idx];
end;
function TPJDropFiles.GetFileName: string;
{Read access method for FileName property: returns name of first file dropped,
if any, otherwise returns empty string}
begin
if Count > 0 then
Result := GetFile(0)
else
Result := '';
end;
function TPJDropFiles.GetFilter: TPJFileFilter;
{Read access method for Filter property}
begin
// This property can be accessed (from Notification method) before helper
// object is created, so we test for that
if Assigned(fHelper) then
Result := fHelper.fFilterComp
else
Result := nil;
end;
function TPJDropFiles.GetForegroundOnDrop: Boolean;
{Read access method for ForegroundOnDrop property}
begin
Result := fHelper.fForegroundOnDrop;
end;
function TPJDropFiles.GetIsFolder(Idx: Integer): Boolean;
{Read access method for IsFolder property}
begin
Result := Boolean(fHelper.fFileList.Objects[Idx]);
end;
function TPJDropFiles.GetOnBeforeDrop: TNotifyEvent;
{Read access method for OnBeforeDrop event property}
begin
Result := fHelper.fOnBeforeDrop;
end;
function TPJDropFiles.GetOnDropFiles: TNotifyEvent;
{Read access method for OnDropFiles event property}
begin
Result := fHelper.fOnDropFiles;
end;
function TPJDropFiles.GetOnFileFilter: TPJDroppedFileFilter;
{Read access method for OnFileFilter method}
begin
Result := fHelper.fOnFileFilter;
end;
function TPJDropFiles.GetOptions: TPJDropFilesOptions;
{Read access method for Options property}
begin
Result := fHelper.fOptions;
end;
procedure TPJDropFiles.Notification(AComponent: TComponent;
Operation: TOperation);
{Sets Filter property to nil when referenced component is destroyed}
begin
inherited;
if (AComponent = Filter) and (Operation = opRemove) then
Filter := nil;
end;
procedure TPJDropFiles.Paint;
{Control paint handler - draws dashed outline to control only when designing}
begin
if csDesigning in ComponentState then
with Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Width, Height);
end;
end;
procedure TPJDropFiles.SetFilter(const Value: TPJFileFilter);
{Write access method for Filter property}
begin
// Check helper object is assigned before attempting to set it: Notification
// method may call this method before helper is created
if Assigned(fHelper) then
fHelper.fFilterComp := Value;
end;
procedure TPJDropFiles.SetForegroundOnDrop(const Value: Boolean);
{Write access method for ForegroundOnDrop property}
begin
fHelper.fForegroundOnDrop := Value;
end;
procedure TPJDropFiles.SetOnBeforeDrop(const Value: TNotifyEvent);
{Write access method for OnBeforeDrop event property}
begin
fHelper.fOnBeforeDrop := Value;
end;
procedure TPJDropFiles.SetOnDropFiles(const Value: TNotifyEvent);
{Write access method for OnDropFiles event property}
begin
fHelper.fOnDropFiles := Value;
end;
procedure TPJDropFiles.SetOnFileFilter(const Value: TPJDroppedFileFilter);
{Write access method for OnFileFilter event property}
begin
fHelper.fOnFileFilter := Value;
end;
procedure TPJDropFiles.SetOptions(const Value: TPJDropFilesOptions);
{Write access method for Options property}
begin
fHelper.fOptions := Value;
end;
procedure TPJDropFiles.WMDropFiles(var Msg: TMessage);
{File drop message handler - used to record info about dropped files}
begin
// Trigger event that indicates drop is about to start
fHelper.BeforeDrop;
// Collect dropped files
fHelper.FilesDropped(Msg.WParam);
// Trigger drop files event
fHelper.DropFiles;
// We handled message
Msg.Result := 0;
end;
{ TPJSubClassedDropFiles }
constructor TPJSubClassedDropFiles.Create(AOwner: TComponent);
{Class constructor. Creates and initialises component}
begin
inherited;
fHelper := CreateAndInitHelper;
fEnabled := True;
end;
destructor TPJSubClassedDropFiles.Destroy;
{Class destructor. Tears down component}
begin
fHelper.Free;
inherited;
end;
function TPJSubClassedDropFiles.GetCount: Integer;
{Read access method for Count property}
begin
Result := Helper.fFileList.Count;
end;
function TPJSubClassedDropFiles.GetDropControl: TControl;
{Read access method for DropControl property - returns child control of owner
form which was under mouse when files were dropped, or nil if if no child
control was under mouse}
begin
Result := Helper.GetDropControl;
end;
function TPJSubClassedDropFiles.GetDropPoint: TPoint;
{Read access method for DropPoint property}
begin
Result := Helper.fDropPoint;
end;
function TPJSubClassedDropFiles.GetFile(Idx: Integer): string;
{Read access method for Files property}
begin
Result := Helper.fFileList[Idx];
end;
function TPJSubClassedDropFiles.GetFileName: string;
{Read access method for FileName property: returns name of first file dropped,
if any, otherwise returns empty string}
begin
if Count > 0 then
Result := GetFile(0)
else
Result := '';
end;
function TPJSubClassedDropFiles.GetFilter: TPJFileFilter;
{Read access method for Filter property}
begin
// This property can be accessed (from Notification method) before helper
// object is created, so we test for that
if Assigned(Helper) then
Result := Helper.fFilterComp
else
Result := nil;
end;
function TPJSubClassedDropFiles.GetForegroundOnDrop: Boolean;
{Read access method for ForegroundOnDrop property}
begin
Result := Helper.fForegroundOnDrop;
end;
function TPJSubClassedDropFiles.GetIsFolder(Idx: Integer): Boolean;
{Read access method for IsFolder property}
begin
Result := Boolean(Helper.fFileList.Objects[Idx])
end;
function TPJSubClassedDropFiles.GetOnBeforeDrop: TNotifyEvent;
{Read access method for OnBeforeDrop event property}
begin
Result := Helper.fOnBeforeDrop;
end;
function TPJSubClassedDropFiles.GetOnDropFiles: TNotifyEvent;
{Read access method for OnDropFiles event property}
begin
Result := Helper.fOnDropFiles;
end;
function TPJSubClassedDropFiles.GetOnFileFilter: TPJDroppedFileFilter;
{Read access method for OnFileFilter event property}
begin
Result := Helper.fOnFileFilter;
end;
function TPJSubClassedDropFiles.GetOptions: TPJDropFilesOptions;
{Read access method for Options property}
begin