-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bcrtti.pas
340 lines (302 loc) · 9.17 KB
/
bcrtti.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ Useful tools for RTTI. Functions are used expecialy for save/load styles.
Styles has construction similar to INI files:
[Header]
Author=Krzysztof Dibowski
Description=My test style
ControlClass=TBCButton
[Properties]
State.Border.Width=2
.....
But instead of IniFiles unit, we have own functions for read and write styles.
------------------------------------------------------------------------------
originally written in 2012 by Krzysztof Dibowski dibowski at interia.pl
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BCRTTI;
{$I bgracontrols.inc}
interface
uses
Classes;
type
PBCStyleHeader = ^TBCStyleHeader;
TBCStyleHeader = record
Author: String;
ControlClass: String;
Description: String;
end;
// Function return data of specified section (header, properties, etc).
// This is smart function, because it doesn't read whole file but read file
// line by line and return only needed section. So it should fastest for reading
// header info instead of TIniFile object which read, parse and index all file.
function GetSectionData(const AFileName, ASectionName: String): TStrings;
// Methods which read header from list or file and parse it into pascal record
procedure GetStyleHeader(const AFileName: String; AOutHeader: PBCStyleHeader);
// Function check if specified name is on ignored list
function IsPropIgnored(const AName: String): Boolean;
// Method load style saved by SaveStyle method
procedure LoadStyle(AControl: TObject; const AFileName: String; ALogs: TStrings = nil);
// Method save all (which are not on ignored list or readonly) public propertys to
// the output string list. This method have support for property
// tree (Propert1.Subpropert1.Color = 543467). Values are represented as "human readable"
// (e.g. Align = alClient). Header info is save too.
procedure SaveStyle(AControl: TObject; const AAuthor, ADescription: String;
ATargetList: TStrings);
implementation
uses typinfo, variants, sysutils, {%H-}strutils;
const
tIGNORED_PROPS: array[0..5] of string =
('name','caption','left','top','height','width');
sSECTION_HEADER_NAME = 'HEADER';
sSECTION_PROP_NAME = 'PROPERTIES';
sSECTION_HEADER = '['+sSECTION_HEADER_NAME+']';
sSECTION_PROP = '['+sSECTION_PROP_NAME+']';
procedure RemovePadChars(var S: String; const CSet: TSysCharset);
var
I,J,K: LONGINT;
begin
I:=Length(S);
IF (I>0) Then
Begin
J:=I;
While (j>0) and (S[J] IN CSet) DO DEC(J);
if j=0 Then
begin
s:='';
exit;
end;
k:=1;
While (k<=I) And (S[k] IN CSet) DO
INC(k);
IF k>1 Then
begin
move(s[k],s[1],j-k+1);
setlength(s,j-k+1);
end
else
setlength(s,j);
end;
end;
function TrimSet(const S: String;const CSet:TSysCharSet): String;
begin
result:=s;
RemovePadChars(result,cset);
end;
function IsPropIgnored(const AName: String): Boolean;
var
i: Integer;
begin
Result := False;
for i := Low(tIGNORED_PROPS) to High(tIGNORED_PROPS) do
if SameText(tIGNORED_PROPS[i],Trim(AName)) then
Exit(True);
end;
procedure LoadStyle(AControl: TObject; const AFileName: String;
ALogs: TStrings = nil);
var
i, iDot: Integer;
sPath, sVal: String;
obj: TObject;
sl: TStrings;
const
sLOG_NO_PROP = 'Can not find property "%s"';
sLOG_SET_ERR = 'Can not set value "%s" to property "%s"';
sLOG_READ_ONLY = 'Property "%s" is read-only';
procedure _AddLog(const AText: String);
begin
if ALogs<>nil then
ALogs.Add(AText);
end;
function _ValidateProp(AObj: TObject; const APropName: String): Boolean;
begin
Result := True;
// If can't find property
if not IsPublishedProp(AObj,APropName) then
begin
_AddLog(Format(sLOG_NO_PROP,[APropName]));
Exit(False);
end;
// If read-only property
if (GetPropInfo(AObj,APropName)^.SetProc=nil) then
begin
_AddLog(Format(sLOG_READ_ONLY,[APropName]));
Exit(False);
end;
end;
begin
if not FileExists(AFileName) then
Exit;
if ALogs<>nil then
ALogs.Clear;
sl := GetSectionData(AFileName, sSECTION_PROP_NAME);
try
for i:=0 to Pred(sl.Count) do
begin
// Full path with hierarchy tree
sPath := Trim(sl.Names[i]);
// "Human readable" value
sVal := Trim(sl.ValueFromIndex[i]);
iDot := Pos('.', sPath);
// If simple property then write it value
if iDot=0 then
begin
if not _ValidateProp(AControl,sPath) then
Continue;
// Writting property value
try
SetPropValue(AControl,sPath,sVal)
except
_AddLog(Format(sLOG_SET_ERR,[sVal, sPath]));
end
end
else
begin
//... else we must go down in hierarchy tree to the last
// object and then write value to property
obj := AControl;
while iDot>0 do
begin
if not _ValidateProp(obj,Copy(sPath,1,iDot-1)) then
begin
obj := nil;
Break;
end;
obj := GetObjectProp(obj,Copy(sPath,1,iDot-1));
Delete(sPath,1,iDot);
iDot := Pos('.', sPath);
end;
// If no dots, then this word is property name
if (obj<>nil) and (sPath<>'') and _ValidateProp(obj,sPath) then
begin
try
SetPropValue(obj,sPath,sVal)
except
_AddLog(Format(sLOG_SET_ERR,[sVal, sPath]));
end
end;
end;
end;
finally
sl.Free;
end;
end;
procedure SaveStyle(AControl: TObject; const AAuthor, ADescription: String;
ATargetList: TStrings);
procedure _SaveProp(AObj: TObject; APath: String = '');
var
iCount, i: Integer;
lst: TPropList;
s: String;
begin
if AObj=nil then Exit;
iCount := GetPropList(PTypeInfo(AObj.ClassInfo), tkProperties, @lst);
for i := 0 to Pred(iCount) do
{ Notice:
- IsPublishedProp return true for ALL public properties, not only
for properties in Published section. For saving styles, we don't need
all public properties, but only published (visible in object inspector).
I don't know if this is a bug, I leave it. Maybe it will start
working in future ;)
- Second argument check if property should be ignored (but only from root tree),
because we can't save basic properties of control like Name, Top, Left etc.
- SetProc<>nil mean "not read only"
}
if IsPublishedProp(AObj,lst[i]^.Name) and
((AControl<>AObj) or (not IsPropIgnored(lst[i]^.Name))) and
(lst[i]^.SetProc<>nil)
then
begin
// Building property tree
if APath=''
then s := lst[i]^.Name
else s := APath+'.'+lst[i]^.Name;
// If property has subproperty, then we start recurrence to
// build hierarchy tree.
if (lst[i]^.PropType^.Kind = tkClass) then
_SaveProp(GetObjectProp(AObj,lst[i]),s)
else
begin
// We are in bottom node, so we can save final property with value
s := s + ' = ' + String(GetPropValue(AObj,lst[i]^.Name,True));
ATargetList.Add(s);
end;
end;
end;
begin
if ATargetList=nil then
Exit;
ATargetList.Clear;
ATargetList.Add(sSECTION_HEADER);
ATargetList.Add('Author='+AAuthor);
ATargetList.Add('Description='+ADescription);
ATargetList.Add('ControlClass='+AControl.ClassName);
ATargetList.Add('');
ATargetList.Add(sSECTION_PROP);
_SaveProp(AControl);
end;
function GetSectionData(const AFileName, ASectionName: String): TStrings;
var
f: TextFile;
s: String;
bReading: Boolean;
begin
Result := TStringList.Create;
Result.Clear;
if (not FileExists(AFileName)) or (ASectionName='') then
Exit;
AssignFile(f,AFileName);
try
Reset(f);
bReading := False;
while not EOF(f) do
begin
ReadLn(f,s);
s := Trim(s);
if s='' then
Continue;
// If current line is section tag
if s[1]='[' then
begin
// If we currently reading section then we read it all and we must
// break because another section occur
if bReading then
begin
bReading := False;
Break;
end
else
// Otherwise if this is section we are looking for, then set flag
// to "start reading"
if SameText(ASectionName,TrimSet(s,['[',']'])) then
bReading := True;
end else
// Read section line
if bReading then
Result.Add(s);
end;
finally
CloseFile(f);
end;
end;
procedure GetStyleHeader(const AFileName: String; AOutHeader: PBCStyleHeader);
var sl: TStrings;
begin
if (AOutHeader=nil) or (not FileExists(AFileName)) then
Exit;
sl := GetSectionData(AFileName,sSECTION_HEADER_NAME);
try
// Header info (with format Author=Foo) should be at the top of file
with AOutHeader^ do
begin
Author := sl.Values['Author'];
Description := sl.Values['Description'];
ControlClass := sl.Values['ControlClass'];
end;
finally
sl.Free;
end;
end;
end.