forked from e-delphi/VCL2FMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCvtrObject.pas
1332 lines (1197 loc) · 39 KB
/
CvtrObject.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
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
unit CvtrObject;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.IniFiles, Vcl.Imaging.PngImage, CvtrProp;
type
IDfmToFmxRoot = interface
procedure AddGridColumns(AObjName: String; AProp: TFmxProperty);
procedure AddGridLink(AObjName: String; AProp: TDfmProperty);
procedure AddFieldLink(AObjName: String; AProp: TDfmProperty);
function AddImageItem(APng: TPngImage): Integer;
procedure PushProgress;
function GetIniFile: TMemIniFile;
property IniFile: TMemIniFile read GetIniFile;
end;
TRule = record
NewName: String;
LineFound: Boolean;
Action: String;
Parameter: String;
end;
TCodeReplacement = record
NewCode: String;
IsEvent: Boolean;
end;
TCodeReplacements = class(TDictionary<String, TCodeReplacement>)
public
procedure AddProperty(const AOldCode, ANewCode: String);
procedure AddEvent(const AEvent, AParams: String);
end;
TDfmToFmxObject = class;
TOwnedObjects = class(TObjectList<TDfmToFmxObject>);
TEnumList = class(TObjectDictionary<String, TStringList>)
private
FIniFile: TMemIniFile;
public
constructor Create(AIniFile: TMemIniFile);
function GetEnum(const AName: String; var AItems: TStringList): Boolean;
end;
TDfmToFmxObject = class
private
FFMXFileText: String;
protected
FRoot: IDfmToFmxRoot;
FParent: TDfmToFmxObject;
FClassName: String;
FOldClassName: String;
FObjName: String;
FDfmProps: TDfmProperties;
FFmxProps: TFmxProperties;
FOwnedObjs: TOwnedObjects;
FGenerated: Boolean;
FEnumList: TEnumList;
FIniAddProperties: TStringlist;
FIniDefaultValueProperties: TStringlist;
FIniIncludeValues: TStringlist;
FIniSectionValues: TStringlist;
FCodeReplacements: TCodeReplacements;
procedure InitObjects; virtual;
function FMXProperties(APad: String): String;
function FMXSubObjects(APad: String): String;
function ReadContents(AStm: TStreamReader): String;
function GetRule(const AName: string; AList: TStringList = nil): TRule;
function TransformProperty(AProp: TDfmProperty): TFmxProperty;
procedure LoadCommonProperties(AParamName: String);
procedure GenerateObject(AProp: TDfmProperty; AObjectType: string);
procedure GenerateStyle(const APropName, APropValue, AObjectType: String);
procedure SetStyle(const AType, AParam, AValue: String);
procedure InternalProcessBody(var ABody: String);
procedure UpdateUsesStringList(AUsesList: TStrings); virtual;
function GetObjHeader: string; virtual;
public
property Root: IDfmToFmxRoot read FRoot;
property Parent: TDfmToFmxObject read FParent;
property ObjName: String read FObjName;
property NewClassName: String read FClassName;
property DfmProps: TDfmProperties read FDfmProps;
constructor Create(AParent: TDfmToFmxObject; ACreateText: String; AStm: TStreamReader);
constructor CreateGenerated(AParent: TDfmToFmxObject; AObjName, AClassName: String);
destructor Destroy; override;
function CountSubObjects: Integer;
procedure IniFileLoad; virtual;
function FMXFile(APad: String = ''): String; virtual;
end;
TDfmToFmxItem = class(TDfmToFmxObject)
protected
function GetObjHeader: String; override;
public
constructor CreateItem(AParent: TDfmToFmxObject; AClassName: String; AStm: TStreamReader; out ListEndFound: Boolean);
end;
TDfmToFmxItems = class(TObjectList<TDfmToFmxItem>);
TDfmItemsProp = class(TDfmProperty)
protected
FItems: TDfmToFmxItems;
public
property Items: TDfmToFmxItems read FItems;
constructor Create(const AName, AValue: string); override;
destructor Destroy; override;
procedure ReadItems(AParent: TDfmToFmxObject; AClassName: String; AStm: TStreamReader);
procedure Transform(AItemStrings: TStrings);
end;
implementation
uses
System.Generics.Defaults, Image, PatchLib, ReflexiveMasks, VCL2FMXStyleGen;
{ TDfmToFmxObject }
function TDfmToFmxObject.CountSubObjects: Integer;
var
i: Integer;
begin
Result := FOwnedObjs.Count;
for i := 0 to FOwnedObjs.Count - 1 do
Result := Result + FOwnedObjs.Items[i].CountSubObjects;
end;
constructor TDfmToFmxObject.Create(AParent: TDfmToFmxObject; ACreateText: String; AStm: TStreamReader);
var
InputArray: TArray<String>;
begin
FParent := AParent;
if Assigned(AParent) then
FRoot := FParent.Root;
InitObjects;
if Pos('object', Trim(ACreateText)) = 1 then
begin
InputArray := ACreateText.Split([' ']);
if Length(InputArray) > 2 then
begin
FObjName := InputArray[1].TrimRight([':']);
FClassName := InputArray[2];
end
else
begin
FObjName := '';
FClassName := InputArray[1];
end;
IniFileLoad;
ReadContents(AStm);
end
else
raise Exception.Create('Bad Start::' + ACreateText);
end;
constructor TDfmToFmxObject.CreateGenerated(AParent: TDfmToFmxObject; AObjName, AClassName: String);
begin
FParent := AParent;
FRoot := FParent.FRoot;
FObjName := AObjName;
FClassName := AClassName;
FGenerated := True;
InitObjects;
IniFileLoad;
end;
destructor TDfmToFmxObject.Destroy;
begin
FFmxProps.Free;
FDfmProps.Free;
FOwnedObjs.Free;
FCodeReplacements.Free;
FIniIncludeValues.Free;
FIniSectionValues.Free;
FEnumList.Free;
FIniAddProperties.Free;
FIniDefaultValueProperties.Free;
inherited;
end;
function TDfmToFmxObject.FMXFile(APad: String): String;
var
Properties: String;
begin
if FFMXFileText <> '' then
Exit(FFMXFileText);
Properties := FMXProperties(APad); // This can change FClassName, see FMXProperties.CalcShapeClass
FFMXFileText := APad + GetObjHeader;
FFMXFileText := FFMXFileText + Properties + FMXSubObjects(APad + ' ');
FFMXFileText := FFMXFileText + APad +'end' + CRLF;
Result := FFMXFileText;
end;
function TDfmToFmxObject.FMXProperties(APad: String): String;
var
i, j: Integer;
Rule: TRule;
Mask: TReflexiveMask;
DfmProp: TDfmProperty;
ExistingProp, NewProp: TFmxProperty;
procedure HandleStyledSettings(AExcludeElement: String);
var
Prop: TFmxProperty;
Item: Integer;
begin
Prop := FFmxProps.FindByName('StyledSettings');
if not Assigned(Prop) then
begin
Prop := TFmxSetProp.Create('StyledSettings');
TFmxSetProp(Prop).Items.AddStrings(['Family', 'Size', 'Style', 'FontColor', 'Other']);
FFmxProps.AddProp(Prop);
end;
Item := (Prop as TFmxSetProp).Items.IndexOf(AExcludeElement);
if Item >= 0 then
(Prop as TFmxSetProp).Items.Delete(Item);
end;
procedure CalcImageWrapMode;
var
Center, Proportional, Stretch: Boolean;
PropLine: TDfmProperty;
FmxProp: TFmxProperty;
Value: String;
begin
FmxProp := FFmxProps.FindByName('WrapMode');
if Assigned(FmxProp) then
Exit;
Center := False;
Proportional := False;
Stretch := False;
for PropLine in FDfmProps do
begin
if PropLine.Name = 'Center' then
Center := True;
if PropLine.Name = 'Proportional' then
Proportional := True;
if PropLine.Name = 'Stretch' then
Stretch := True;
end;
if Proportional and Stretch then
Value := 'Fit'
else
if Stretch then
Value := 'Stretch'
else
if Center then
Value := 'Center'
else
Value := 'Original';
FFmxProps.AddProp(TFmxProperty.Create('WrapMode', Value));
end;
procedure CalcShapeClass;
var
Shape: String;
Prop: TDfmProperty;
begin
Prop := FDfmProps.FindByName('Shape');
if Assigned(Prop) then
Shape := Prop.Value;
FOldClassName := FClassName;
if (Shape = '') or (Shape = 'stRectangle') or (Shape = 'stSquare') then
FClassName := 'TRectangle';
if Shape = 'stCircle' then
FClassName := 'TCircle';
if Shape = 'stEllipse' then
FClassName := 'TEllipse';
if (Shape = 'stRoundRect') or (Shape = 'stRoundSquare') then
FClassName := 'TRoundRect';
end;
procedure CopyFromParent(ACopyProp: String);
var
ParentProp: TDfmProperty;
NewProp: TFmxProperty;
Mask: TReflexiveMask;
Found: Boolean;
Parent: TDfmToFmxObject;
begin
Mask := TReflexiveMask.Create(ACopyProp);
try
Found := False;
Parent := FParent;
repeat
for ParentProp in Parent.DfmProps do
if Mask.Matches(ParentProp.Name) then
begin
NewProp := TransformProperty(ParentProp);
if Assigned(NewProp) and not Assigned(FFmxProps.FindByName(NewProp.Name)) then
FFmxProps.AddProp(NewProp)
else
NewProp.Free;
Found := True;
end;
Parent := Parent.Parent;
until Found or not Assigned(Parent);
finally
Mask.Free;
end;
end;
procedure ReconsiderAfterRemovingRule(ARemoveRule: String);
var
Line: TDfmProperty;
Mask: TReflexiveMask;
i: Integer;
begin
Mask := TReflexiveMask.Create(ARemoveRule);
try
for i := Pred(FIniSectionValues.Count) downto 0 do
if Mask.Matches(FIniSectionValues.Names[i]) then
FIniSectionValues.Delete(i);
LoadCommonProperties(ARemoveRule);
for Line in FDfmProps do
if Mask.Matches(Line.Name) then
FFmxProps.AddProp(TransformProperty(Line));
finally
Mask.Free;
end;
end;
begin
Result := EmptyStr;
for i := 0 to FDfmProps.Count - 1 do
FFmxProps.AddProp(TransformProperty(FDfmProps[i]));
for i := 0 to Pred(FIniDefaultValueProperties.Count) do
begin
DfmProp := FDfmProps.FindByName(FIniDefaultValueProperties.Names[i]);
if (not Assigned(DfmProp)) and (TReflexiveMask.ContainsWildcards(FIniDefaultValueProperties.Names[i])) then
begin
Mask := TReflexiveMask.Create(FIniDefaultValueProperties.Names[i]);
try
for j := 0 to FDfmProps.Count - 1 do
if Mask.Matches(FDfmProps[j].Name) then
begin
DfmProp := FDfmProps[j];
Break;
end;
finally
Mask.Free;
end;
end;
if Assigned(DfmProp) then
Continue;
Rule := GetRule(FIniDefaultValueProperties.Names[i], FIniDefaultValueProperties);
if Rule.Action = '#CopyFromParent#' then
begin
CopyFromParent(Rule.Parameter);
Continue;
end;
if Rule.Action = '#CalcImageWrapMode#' then
begin
CalcImageWrapMode;
Continue;
end;
if Rule.Action = '#CalcShapeClass#' then
begin
CalcShapeClass;
Continue;
end;
if Rule.Action = '#GenerateStyle#' then
begin
GenerateStyle(Rule.NewName, '', Rule.Parameter);
Continue;
end;
if Rule.Action = '#ReconsiderAfterRemovingRule#' then
begin
ReconsiderAfterRemovingRule(Rule.Parameter);
Continue;
end;
NewProp := TFmxProperty.CreateFromLine(Rule.Parameter);
if Assigned(NewProp) and not Assigned(FFmxProps.FindByName(NewProp.Name)) then
FFmxProps.AddProp(NewProp)
else
NewProp.Free;
end;
for i := 0 to Pred(FIniAddProperties.Count) do
begin
ExistingProp := FFmxProps.FindByName(FIniAddProperties.Names[i]);
if Assigned(ExistingProp) then
begin
Rule := GetRule(FIniAddProperties.Names[i], FIniAddProperties);
if Rule.Action = '#RemoveFromStyledSettings#' then
begin
HandleStyledSettings(Rule.Parameter);
Continue;
end;
NewProp := TFmxProperty.CreateFromLine(Rule.Parameter);
if Assigned(NewProp) and not Assigned(FFmxProps.FindByName(NewProp.Name)) then
begin
if Rule.Action = '#PutToTheTop#' then
FFmxProps.Insert(0, NewProp)
else
FFmxProps.AddProp(NewProp);
end
else
NewProp.Free;
end;
end;
for i := 0 to FFmxProps.Count - 1 do
Result := Result + FFmxProps[i].ToString(APad);
end;
function TDfmToFmxObject.FMXSubObjects(APad: String): String;
var
i: integer;
begin
for i := 0 to Pred(FOwnedObjs.Count) do
Result := Result + FOwnedObjs[i].FMXFile(APad +' ');
end;
procedure TDfmToFmxObject.GenerateObject(AProp: TDfmProperty; AObjectType: string);
procedure AddFmxProperty(AObj: TDfmToFmxObject; const APropName, APropValue: String);
var
Prop: TFmxProperty;
begin
Prop := AObj.FFmxProps.FindByName(APropName);
if Assigned(Prop) then
Prop.Value := APropValue
else
begin
Prop := TFmxProperty.Create(APropName, APropValue);
AObj.FFmxProps.Add(Prop);
end;
end;
procedure GenerateProperty(AObj: TDfmToFmxObject; const APropName, APropValue: String);
var
Prop: TDfmProperty;
begin
Prop := AObj.FDfmProps.FindByName(APropName);
if Assigned(Prop) then
Prop.Value := APropValue
else
begin
Prop := TDfmProperty.Create(APropName, APropValue);
AObj.FDfmProps.Add(Prop);
end;
end;
type
TProp = record
Name, Value: String;
end;
function GetObject(const AObjName, ADFMClass: String; const AInitProps, AReplacements: array of TProp;
APosition: Integer = 0): TDfmToFmxObject;
var
Obj: TDfmToFmxObject;
Prop: TProp;
begin
for Obj in FOwnedObjs do
begin
if not Obj.FGenerated then
Break;
if (Obj.FClassName = ADFMClass) and (Obj.FObjName = AObjName) then
Exit(Obj);
end;
Result := TDfmToFmxObject.CreateGenerated(Self, AObjName, ADFMClass);
FOwnedObjs.Insert(APosition, Result);
for Prop in AInitProps do
AddFmxProperty(Result, Prop.Name, Prop.Value);
for Prop in AReplacements do
FCodeReplacements.AddProperty(Prop.Name, Prop.Value);
end;
procedure ConvertGlyph;
var
Png: TPngImage;
NumGlyphs, Index: Integer;
begin
NumGlyphs := FDfmProps.GetIntValueDef('NumGlyphs', 1);
Png := CreateGlyphPng(AProp.Value, NumGlyphs);
Index := FRoot.AddImageItem(Png);
AddFmxProperty(Self, 'Images', 'SingletoneImageList');
AddFmxProperty(Self, 'ImageIndex', Index.ToString);
if FClassName.ToLower = 'tbutton' then
SetStyle(CButtonStyle, CGlyphSize, Png.Height.ToString);
if FClassName.ToLower = 'tspeedbutton' then
SetStyle(CSpeedButtonStyle, CGlyphSize, Png.Height.ToString);
end;
procedure InitChildImage(AObj: TDfmToFmxObject);
var
Png: TPngImage;
NumGlyphs, Width, Height: Integer;
begin
NumGlyphs := FDfmProps.GetIntValueDef('NumGlyphs', 1);
Width := FDfmProps.GetIntValueDef('Width', 23);
Height := FDfmProps.GetIntValueDef('Height', 22);
Png := CreateGlyphPng(AProp.Value, NumGlyphs);
Width := (Width - Png.Width) div 2;
Height := (Height - Png.Height) div 2;
AddFmxProperty(AObj, 'Margins.Left', Width.ToString);
AddFmxProperty(AObj, 'Margins.Right', Width.ToString);
AddFmxProperty(AObj, 'Margins.Top', Height.ToString);
AddFmxProperty(AObj, 'Margins.Bottom', Height.ToString);
AObj.FFmxProps.AddProp(TFmxImageProp.Create('Picture.Data', Png));
end;
const
ChildImageInitParams: array [0..2] of TProp = ((Name: 'Align'; Value: 'Client'), (Name: 'HitTest'; Value: 'False'),
(Name: 'WrapMode'; Value: 'Fit'));
ChildImageReplacements: array [0..0] of TProp = ((Name: 'Picture'; Value: '_Glyph.Bitmap'));
ColoredRectInitParams: array [0..5] of TProp = ((Name: 'Align'; Value: 'Client'), (Name: 'Margins.Left'; Value: '1'),
(Name: 'Margins.Top'; Value: '1'), (Name: 'Margins.Right'; Value: '1'), (Name: 'Margins.Bottom'; Value: '1'),
(Name: 'Stroke.Kind'; Value: 'None'));
ColoredRectReplacements: array [0..0] of TProp = ((Name: 'Color'; Value: '_Color.Fill.Color'));
RadioButtonInitParams: array [0..2] of TProp = ((Name: 'Size.Height'; Value: '19'), (Name: 'Position.X'; Value: '8'),
(Name: 'Size.Width'; Value: '50'));
SeparateCaptionInitParams: array [0..1] of TProp = ((Name: 'Align'; Value: 'Client'),
(Name: 'TabStop'; Value: 'False'));
SeparateCaptionReplacements: array [0..7] of TProp = ((Name: 'ShowCaption'; Value: '_Caption.Visible'),
(Name: 'VerticalAlignment'; Value: '_Caption.TextSettings.VertAlign'),
(Name: 'Alignment'; Value: '_Caption.TextSettings.HorzAlign'), (Name: 'Caption'; Value: '_Caption.Text'),
(Name: 'Font.Color'; Value: '_Caption.TextSettings.FontColor'),
(Name: 'Font.Height'; Value: '_Caption.TextSettings.Font.Size'),
(Name: 'Font.Name'; Value: '_Caption.TextSettings.Font.Family'),
(Name: 'Font.Style'; Value: '_Caption.TextSettings.Font.Style'));
var
Obj: TDfmToFmxObject;
Num, i: Integer;
Caption: String;
begin
if AObjectType = 'ChildImage' then
begin
Obj := GetObject(FObjName + '_Glyph', 'TImage', ChildImageInitParams, ChildImageReplacements);
InitChildImage(Obj);
end;
if AObjectType = 'ColoredRect' then
begin
Obj := GetObject(FObjName + '_Color', 'TShape', ColoredRectInitParams, ColoredRectReplacements);
if AProp.Name = 'Color' then
GenerateProperty(Obj, 'Brush.Color', AProp.Value)
else
GenerateProperty(Obj, AProp.Name, AProp.Value);
end;
if AObjectType = 'FieldLink' then
FRoot.AddFieldLink(FObjName, AProp);
if AObjectType = 'ImageItem' then
ConvertGlyph;
if AObjectType = 'GridLink' then
FRoot.AddGridLink(FObjName, AProp);
if AObjectType = 'MultipleRadioButtons' then
begin
Num := 0;
for Caption in (AProp as TDfmStringsProp).Strings do
begin
Obj := GetObject(FObjName + '_RadioButton' + (Num + 1).ToString, 'TRadioButton', RadioButtonInitParams, [], Num);
AddFmxProperty(Obj, 'Text', Caption);
AddFmxProperty(Obj, 'TabOrder', Num.ToString);
AddFmxProperty(Obj, 'Position.Y', (16 + Num * 20).ToString);
Inc(Num);
end;
end;
if AObjectType = 'MultipleTabs' then
begin
Num := 1;
for Caption in (AProp as TDfmStringsProp).Strings do
begin
Obj := GetObject(FObjName + 'Tab' + Num.ToString, 'TTabSheet', [], [], Num - 1);
AddFmxProperty(Obj, 'Text', Caption);
Inc(Num);
end;
end;
if AObjectType = 'SelectRadioButton' then
begin
Num := AProp.Value.ToInteger;
Obj := nil;
for i := 0 to Num do
Obj := GetObject(FObjName + '_RadioButton' + (i + 1).ToString, 'TRadioButton', RadioButtonInitParams, [], i);
AddFmxProperty(Obj, 'IsChecked', 'True');
end;
if AObjectType = 'SeparateCaption' then
begin
Obj := GetObject(FObjName + '_Caption', 'TLabel', SeparateCaptionInitParams, SeparateCaptionReplacements);
if Obj.FDfmProps.Count = 0 then
begin
GenerateProperty(Obj, 'Alignment', 'taCenter');
GenerateProperty(Obj, 'Layout', 'tlCenter');
end;
if AProp.Name = 'ShowCaption' then
AddFmxProperty(Obj, 'Visible', AProp.Value)
else
if AProp.Name = 'VerticalAlignment' then
begin
if AProp.Value = 'taAlignBottom' then
GenerateProperty(Obj, 'Layout', 'tlBottom'); //Center is default for panel and top - for label
end
else
GenerateProperty(Obj, AProp.Name, AProp.Value);
end;
end;
procedure TDfmToFmxObject.GenerateStyle(const APropName, APropValue, AObjectType: String);
function IsParamSet(const AType, AParam: String): Boolean;
var
Prop: TFmxProperty;
begin
Result := False;
Prop := FFmxProps.FindByName('StyleLookup');
if Assigned(Prop) and (StyleGenerator.ReadParam(Prop.Value.DeQuotedString, AType, AParam) <> '') then
Result := True;
end;
begin
FIniIncludeValues.Add('VCL2FMXStyleGen');
if AObjectType = 'Button' then
begin
if (APropName = 'ImageAlignment') and (APropValue <> 'iaCenter') then
SetStyle(CButtonStyle, CGlyphPosition, APropValue.Substring(2));
if APropName = 'Layout' then
SetStyle(CButtonStyle, CGlyphPosition, APropValue.Substring(7));
end
else
if (AObjectType = 'CheckBox') and (APropName = 'Color') then
SetStyle(CCheckBoxStyle, CBackgroundColor, ColorToAlphaColor(APropValue))
else
if (AObjectType = 'Edit') and (APropName = 'Color') then
SetStyle(CEditStyle, CBackgroundColor, ColorToAlphaColor(APropValue))
else
if AObjectType = 'GroupBox' then
begin
if APropName = 'Color' then
SetStyle(CGroupBoxStyle, CBackgroundColor, ColorToAlphaColor(APropValue));
if APropName = 'ShowFrame' then
SetStyle(CGroupBoxStyle, CShowFrame, APropValue);
if APropName = 'ParentBackground' then
begin
if StrToBoolDef(APropValue, True) then
SetStyle(CGroupBoxStyle, CBackgroundColor, 'claNull')
else
if not IsParamSet(CGroupBoxStyle, CBackgroundColor) then
SetStyle(CGroupBoxStyle, CBackgroundColor, CColorBtnFace);
end;
end
else
if (AObjectType = 'Label') and (APropName = 'Color') then
SetStyle(CLabelStyle, CBackgroundColor, ColorToAlphaColor(APropValue))
else
if (AObjectType = 'Memo') and (APropName = 'Color') then
SetStyle(CMemoStyle, CBackgroundColor, ColorToAlphaColor(APropValue))
else
if AObjectType = 'Panel' then
begin
if APropName = 'Color' then
SetStyle(CPanelStyle, CBackgroundColor, ColorToAlphaColor(APropValue));
if APropName = 'ParentBackground' then
begin
if StrToBoolDef(APropValue, True) then
SetStyle(CPanelStyle, CBackgroundColor, 'claNull')
else
if not IsParamSet(CPanelStyle, CBackgroundColor) then
SetStyle(CPanelStyle, CBackgroundColor, CColorBtnFace);
end;
end
else
if (AObjectType = 'RadioButton') and (APropName = 'Color') then
SetStyle(CRadioButtonStyle, CBackgroundColor, ColorToAlphaColor(APropValue))
else
if AObjectType = 'ScrollBox' then
begin
if APropName = 'Color' then
SetStyle(CScrollBoxStyle, CBackgroundColor, ColorToAlphaColor(APropValue));
if APropName = 'ParentBackground' then
begin
if StrToBoolDef(APropValue, False) then
SetStyle(CScrollBoxStyle, CBackgroundColor, 'claNull')
else
if not IsParamSet(CScrollBoxStyle, CBackgroundColor) then
SetStyle(CScrollBoxStyle, CBackgroundColor, CColorBtnFace);
end;
end
else
if (AObjectType = 'SpeedButton') and (APropName = 'Layout') then
SetStyle(CSpeedButtonStyle, CGlyphPosition, APropValue.Substring(7));
end;
function TDfmToFmxObject.GetObjHeader: string;
begin
if FObjName <> '' then
Result := 'object ' + FObjName + ': ' + FClassName + CRLF
else
Result := 'object ' + FClassName + CRLF;
end;
function TDfmToFmxObject.GetRule(const AName: string; AList: TStringList = nil): TRule;
var
RuleLine: String;
Mask: TReflexiveMask;
ActionNameStart, ActionNameEnd: Integer;
begin
Mask := nil;
try
if not Assigned(AList) then
begin
RuleLine := FIniSectionValues.Values[AName].Trim;
if RuleLine = '' then
for var i := 0 to FIniSectionValues.Count - 1 do
begin
if not TReflexiveMask.ContainsWildcards(FIniSectionValues.Names[i]) then
Continue;
Mask.Free;
Mask := TReflexiveMask.Create(FIniSectionValues.Names[i]);
if Mask.Matches(AName) then
begin
RuleLine := FIniSectionValues.ValueFromIndex[i];
Break;
end;
end;
end
else
RuleLine := AList.Values[AName].Trim;
if RuleLine = '' then
begin
Result.NewName := AName;
Result.LineFound := False;
Result.Action := '';
Result.Parameter := Result.NewName;
end
else
begin
Result.LineFound := True;
ActionNameStart := RuleLine.IndexOf('#');
case ActionNameStart of
-1: Result.NewName := RuleLine;
0: Result.NewName := AName;
else
Result.NewName := RuleLine.Substring(0, ActionNameStart).Trim;
end;
if Assigned(Mask) and Mask.ContainsWildcards(Result.NewName) then
Result.NewName := Mask.RestoreText(Result.NewName);
Result.Action := '';
if ActionNameStart > -1 then
begin
ActionNameEnd := RuleLine.IndexOf('#', ActionNameStart + 1);
if ActionNameEnd > -1 then
begin
Result.Action := RuleLine.Substring(ActionNameStart, ActionNameEnd - ActionNameStart + 1);
Result.Parameter := RuleLine.Substring(ActionNameEnd + 1);
end
else
Result.Parameter := RuleLine.Substring(ActionNameStart + 1);
end
else
Result.Parameter := RuleLine;
end;
finally
Mask.Free;
end;
end;
procedure TDfmToFmxObject.IniFileLoad;
var
NewClassName: String;
begin
if FClassName <> '' then
begin
NewClassName := FRoot.IniFile.ReadString('ObjectChanges', FClassName, EmptyStr);
if NewClassName <> EmptyStr then
begin
FOldClassName := FClassName;
FClassName := NewClassName;
end;
FRoot.IniFile.ReadSectionValues(FClassName, FIniSectionValues);
FRoot.IniFile.ReadSection(FClassName + '#Include', FIniIncludeValues);
FRoot.IniFile.ReadSectionValues(FClassName + '#AddIfPresent', FIniAddProperties);
FRoot.IniFile.ReadSectionValues(FClassName + '#DefaultValueProperty', FIniDefaultValueProperties);
end;
LoadCommonProperties('*');
end;
procedure TDfmToFmxObject.InitObjects;
begin
FDfmProps := TDfmProperties.Create({AOwnsObjects} True);
FFmxProps := TFmxProperties.Create({AOwnsObjects} True);
FEnumList := TEnumList.Create(FRoot.IniFile);
FIniAddProperties := TStringlist.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
FIniDefaultValueProperties := TStringlist.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
FIniIncludeValues := TStringlist.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
FIniSectionValues := TStringlist.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
FCodeReplacements := TCodeReplacements.Create;
FOwnedObjs := TOwnedObjects.Create({AOwnsObjects} True);
end;
procedure TDfmToFmxObject.InternalProcessBody(var ABody: String);
type
TReplacementPair = TPair<String, TCodeReplacement>;
var
i: Integer;
Replacement: TReplacementPair;
RuleName: String;
Rule: TRule;
procedure ReplaceEventParams(AReplacement: TReplacementPair);
var
Name, Opening, Closing: Integer;
begin
Name := PosNoCase(AReplacement.Key, ABody);
while Name > 0 do
begin
Opening := Pos('(', ABody, Name + AReplacement.Key.Length);
Closing := Pos(')', ABody, Opening + 1);
ABody := ABody.Substring(0, Opening) + AReplacement.Value.NewCode + ABody.Substring(Closing - 1);
Name := PosNoCase(AReplacement.Key, ABody, Closing + 1);
end;
end;
begin
if FGenerated then
ABody := StringReplaceSkipChars(ABody, FParent.ObjName + ':' + FParent.NewClassName + ';', FParent.ObjName + ': ' +
FParent.NewClassName + ';' + CRLF + ' ' + FObjName + ': ' + FClassName + ';')
else
if (FOldClassName <> '') and (FObjName <> '') then
ABody := StringReplaceSkipChars(ABody, FObjName + ':' + FOldClassName, FObjName + ': ' + FClassName);
if (FObjName <> '') and not FGenerated then
begin
for i := 0 to FIniSectionValues.Count - 1 do
begin
RuleName := FIniSectionValues.Names[i];
if not TReflexiveMask.ContainsWildcards(RuleName) then
begin
Rule := GetRule(RuleName, FIniSectionValues);
if (RuleName <> Rule.NewName) and not FCodeReplacements.ContainsKey('.' + RuleName) then
FCodeReplacements.AddProperty(RuleName, '.' + Rule.NewName)
end;
end;
for Replacement in FCodeReplacements do
if not Replacement.Value.IsEvent then
ABody := StringReplaceSkipChars(ABody, FObjName + Replacement.Key, FObjName + Replacement.Value.NewCode)
else
ReplaceEventParams(Replacement);
end;
FRoot.PushProgress;
for i := 0 to Pred(FOwnedObjs.Count) do
FOwnedObjs[i].InternalProcessBody(ABody);
end;
procedure TDfmToFmxObject.LoadCommonProperties(AParamName: String);
var
i, j: integer;
Found: Boolean;
CommonProps, Candidates: TStringList;
ParamMask, CommonPropMask, ExistingPropMask: TReflexiveMask;
begin
CommonProps := nil;
Candidates := nil;
ParamMask := nil;
try
CommonProps := TStringList.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
Candidates := TStringList.Create(dupIgnore, {Sorted} True, {CaseSensitive} False);
ParamMask := TReflexiveMask.Create(AParamName);
FRoot.IniFile.ReadSectionValues('CommonProperties', CommonProps);
for i := 0 to Pred(CommonProps.Count) do
if ParamMask.Matches(CommonProps.Names[i]) and
(FIniSectionValues.IndexOfName(CommonProps.Names[i]) = -1) then
begin
Found := False;
if TReflexiveMask.ContainsWildcards(CommonProps.Names[i]) then
begin
CommonPropMask := TReflexiveMask.Create(CommonProps.Names[i]);
try
for j := 0 to Pred(FIniSectionValues.Count) do
if CommonPropMask.Matches(FIniSectionValues.Names[j]) then
begin
Found := True;
Break;
end;
finally
CommonPropMask.Free;
end;
end;
if not Found then
Candidates.Add(CommonProps[i]);
end;
for i := 0 to Pred(FIniSectionValues.Count) do
if TReflexiveMask.ContainsWildcards(FIniSectionValues.Names[i]) then
begin
ExistingPropMask := TReflexiveMask.Create(FIniSectionValues.Names[i]);
try
for j := Pred(Candidates.Count) downto 0 do
if ExistingPropMask.Matches(Candidates.Names[j]) then
Candidates.Delete(j);
finally
ExistingPropMask.Free;
end;
end;
for i := 0 to Pred(Candidates.Count) do
FIniSectionValues.Add(Candidates[i]);
CommonProps.Clear;
FRoot.IniFile.ReadSectionValues('CommonProperties#AddIfPresent', CommonProps);
for i := 0 to Pred(CommonProps.Count) do
if FIniAddProperties.IndexOfName(CommonProps.Names[i]) = -1 then
FIniAddProperties.Add(CommonProps[i]);
finally
ParamMask.Free;
CommonProps.Free;
Candidates.Free;
end;
end;
function TDfmToFmxObject.ReadContents(AStm: TStreamReader): String;
var
PropEqSign: Integer;
Name, Value: String;
Prop: TDfmProperty;
Data: String;
function GetItemsClass: String;
var
Rule: TRule;
begin
Result := '';
Rule := GetRule(Name);
if (Rule.Action = '#ItemClass#') or (Rule.Action = '#Delete#') or (Rule.Action = '#GenerateLinkColumns#') then
Result := Rule.Parameter;
end;
begin
Data := Trim(AStm.ReadLine);
while not Data.StartsWith('end') do
begin
if Pos('object', Data) = 1 then
FOwnedObjs.Add(TDfmToFmxObject.Create(Self, Data, AStm))
else
begin
PropEqSign := Data.IndexOf('=');
Name := Data.Substring(0, PropEqSign).Trim;
Value := Data.Substring(PropEqSign + 1).Trim;
if Value = '' then
begin
Prop := TDfmProperty.Create(Name, '');
Prop.ReadMultiline(AStm);
end
else
if Value[1] = '(' then
begin
Prop := TDfmStringsProp.Create(Name, Value);
TDfmStringsProp(Prop).ReadLines(AStm);
end
else
if Value[1] = '<' then