forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FIBCloneComponents.pas
321 lines (289 loc) · 7.68 KB
/
FIBCloneComponents.pas
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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit FIBCloneComponents ;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes;
function CreateClone(Src: TComponent): TComponent;
procedure SaveCmpToFile(Cmp: TComponent;FileName:string);
procedure SaveCmpToStream(Cmp: TComponent;ms:TStream);
function LoadCmpFromFile(FOwner,FParent: TComponent;FileName:string):TComponent;
function LoadCmpFromStream(FOwner,FParent: TComponent;ms:Tstream;
FromBegin:boolean =True
):TComponent;
procedure CopyProps(Src: TComponent; Dst: TComponent);
implementation
uses TypInfo;
type
PLongInt=^LongInt;
TClassRegistrator = class (TObject)
procedure RegisterClass(Instance: TComponent);
end;
TComponentHack = class (TComponent)
end;
TReaderEvents = class
public
OldName: String;
procedure SetName(Reader: TReader; Component: TComponent; var Name: string);
procedure OnFindMethodEvent(Reader: TReader; const MethodName: string;
var Address: Pointer; var Error: Boolean) ;
end;
function UniqueName(Instance: TComponent; Name: String; Owner: TComponent): String; forward;
procedure TClassRegistrator.RegisterClass(Instance: TComponent);
begin
Classes.RegisterClass(TComponentClass(Instance.ClassType));
TComponentHack(Instance).GetChildren(Self.RegisterClass , nil );
end;
procedure TReaderEvents.SetName(Reader: TReader; Component: TComponent; var Name: string);
begin
if Component.Name='' then
Name := UniqueName(Component, Name, Component.Owner)
else
Name:=Component.Name
end;
procedure TReaderEvents.OnFindMethodEvent(Reader: TReader; const MethodName: string;
var Address: Pointer; var Error: Boolean);
begin
Error:=false
end;
procedure RegisterComponent(Instance: TComponent);
begin
with TClassRegistrator.Create do
try
RegisterClass(Instance);
finally
Free;
end;
end;
procedure CopyProps(Src: TComponent; Dst: TComponent);
var
F: TMemoryStream;
Reader: TReader;
Writer: TWriter;
ReaderEvents: TReaderEvents;
FOwner: TComponent;
begin
F := TMemoryStream.Create;
try
Writer := TWriter.Create(F, 1024);
try
if Assigned(Src.Owner) then
FOwner := Src.Owner else
FOwner := Dst.Owner;
Writer.Root := FOwner;
Writer.WriteComponent(Src);
finally
Writer.Free;
end;
RegisterComponent(Src);
F.Position := 0;
Reader := TReader.Create(F, 1024);
try
ReaderEvents := TReaderEvents.Create;
try
ReaderEvents.OldName := Dst.Name;
Reader.Root := FOwner;
Reader.OnSetName := ReaderEvents.SetName;
Reader.BeginReferences;
try
Reader.ReadComponent(Dst);
Reader.FixupReferences;
finally
Reader.EndReferences;
end;
finally
ReaderEvents.Free;
end;
finally
Reader.Free;
end;
finally
F.Free;
end;
end;
procedure SaveCmpToStream(Cmp: TComponent;ms:TStream);
var
Writer: TWriter;
FOwner: TComponent;
begin
try
Writer := TWriter.Create(ms, 4096);
try
FOwner := Cmp.Owner;
Writer.Root := FOwner;
Writer.WriteSignature;
Writer.WriteComponent(cmp);
finally
Writer.Free;
end;
ms.Position := 0;
finally
end
end;
procedure SaveCmpToFile(Cmp: TComponent;FileName:string);
var
F: TMemoryStream;
begin
F := TMemoryStream.Create;
try
SaveCmpToStream(Cmp,F);
F.SaveToFile(FileName);
finally
F.Free
end
end;
function LoadCmpFromStream(FOwner,FParent: TComponent;ms:Tstream;
FromBegin:boolean =True
):TComponent;
var
Reader: TReader;
ReaderEvents: TReaderEvents;
Signature: Longint;
const
FilerSignature: array[1..4] of Char = 'TPF0';
begin
if FromBegin then
ms.Seek(0,soFromBeginning);
try
Reader := TReader.Create(ms, 4096);
Reader.Read(Signature, SizeOf(Signature));
if Signature <> PLongInt(@FilerSignature)^ then
begin
Reader.Free;
Reader := TReader.Create(ms, 4096);
ms.Position:=0;
end;
try
ReaderEvents := TReaderEvents.Create;
try
Reader.Root := FOwner;
Reader.Owner := FOwner;
Reader.Parent:= FParent;
Reader.OnSetName := ReaderEvents.SetName;
Reader.OnFindMethod:= ReaderEvents.OnFindMethodEvent;
Reader.BeginReferences;
try
Result :=Reader.ReadComponent(nil); // nil
try
Reader.FixupReferences;
except
end;
finally
Reader.EndReferences;
end;
finally
ReaderEvents.Free;
end;
finally
Reader.Free;
end;
except
Result :=nil
end;
end;
function LoadCmpFromFile(FOwner,FParent: TComponent;FileName:string):TComponent;
var
F: TMemoryStream;
begin
F := TMemoryStream.Create;
try
F.LoadFromFile(FileName);
Result:=LoadCmpFromStream(FOwner,FParent,F);
finally
F.Free;
end;
end;
function CreateCloneOwner(Src: TComponent; AOwner: TComponent): TComponent;
begin
Result := TComponentClass(Src.ClassType).Create(AOwner);
try
CopyProps(Src, Result);
except
Result.Free;
raise;
end;
end;
function CreateClone(Src: TComponent): TComponent;
begin
Result := CreateCloneOwner(Src, Src.Owner)
end;
{
procedure CopyMethodProps(Src, Dst: TObject);
var
I, Count: Integer;
PropList: PPropList;
PropInfo: PPropInfo;
Method: TMethod;
begin
if not Dst.InheritsFrom(Src.ClassType) then Exit;
PropList := nil;
Count := GetTypeData(Src.ClassInfo)^.PropCount;
try
ReAllocMem(PropList, Count * SizeOf(Pointer));
GetPropInfos(Src.ClassInfo, PropList);
for I := 0 to Count - 1 do
begin
PropInfo := PropList^[I];
if PropInfo^.PropType^.Kind = tkMethod then
begin
Method := GetMethodProp(Src, PropInfo);
SetMethodProp(Dst, PropInfo, Method);
end;
end;
finally
ReAllocMem(PropList, 0);
end;
end;
}
function UniqueName(Instance: TComponent; Name: String; Owner: TComponent): String;
var
I: Integer;
Tmp: TComponent;
begin
I := 1;
Result := Name;
if Assigned(Owner) then
begin
Tmp := Owner.FindComponent(Result);
if Assigned(Tmp) and (Tmp <> Instance) then
while (Tmp <> nil) do
begin
Result :=Copy(Instance.ClassName,2,255)+IntToStr(i);
Inc(I);
Tmp := Owner.FindComponent(Result);
end;
end else
begin
Result := '';
{$IFNDEF D6+}
if Assigned(FindGlobalComponent) then
{$ENDIF}
begin
Result := Name;
while FindGlobalComponent(Result) <> nil do
begin
Result :=Copy(Instance.ClassName,2,255)+IntToStr(i);
Inc(I);
end;
end;
end;
end;
end.