-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWDDT.Vcl.RTTI.pas
More file actions
75 lines (58 loc) · 1.86 KB
/
Copy pathWDDT.Vcl.RTTI.pas
File metadata and controls
75 lines (58 loc) · 1.86 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
unit WDDT.Vcl.RTTI;
interface
uses
System.SysUtils,
System.Classes,
System.TypInfo,
System.Rtti,
Vcl.Controls,
WDDT.RTTI;
type
TCloneProc = reference to procedure(Source, Target: TComponent);
function DeepCloneComponent(Component: TComponent; CustomParent: TWinControl = nil;
AcceptProc: TPropertyAcceptProc = nil; AfterClone: TCloneProc = nil): TComponent;
implementation
var
CloneComponentCallCounter: Integer;
function DeepCloneComponent(Component: TComponent; CustomParent: TWinControl;
AcceptProc: TPropertyAcceptProc; AfterClone: TCloneProc): TComponent;
var
OldName, NewName, TempName: string;
ResultControl: TControl absolute Result;
SourceWinControl: TWinControl absolute Component;
ResultWinControl: TWinControl absolute Result;
procedure AdjustControls;
begin
if not Assigned(CustomParent) then
ResultControl.Parent := SourceWinControl.Parent
else
ResultControl.Parent := CustomParent;
end;
procedure AdjustWinControls;
var
cc: Integer;
begin
for cc := 0 to SourceWinControl.ControlCount - 1 do
DeepCloneComponent(SourceWinControl.Controls[cc], ResultWinControl, AcceptProc, AfterClone);
end;
begin
try
OldName := Component.Name;
NewName := OldName + IntToStr(CloneComponentCallCounter);
TempName := OldName + IntToStr(CloneComponentCallCounter + 1);
Inc(CloneComponentCallCounter);
Result := TComponentClass(Component.ClassType).Create(Component.Owner);
Component.Name := TempName;
Result.Name := NewName;
CopyObject(Component, Result, AcceptProc);
if Component is TControl then
AdjustControls;
if Component is TWinControl then
AdjustWinControls;
finally
Component.Name := OldName;
end;
if Assigned(AfterClone) then
AfterClone(Component, Result);
end;
end.