summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fpgtk/src/def/objectdef.pp
blob: 21c200561805c2ef9fe0ea584433cfc0a672e63e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
{$mode delphi}{$h+}
unit ObjectDef;
{_$define writecreate}{_$define loaddebug}
interface

uses
  sysutils, Classes;

const
  VersionNumber = '1.08';

type

  TLukStepitProc = procedure of Object;
  TLukStepitMaxProc = procedure (Max : integer) of Object;

  TInterfaceSection = (isPrivate,isProtected,isPublic,isPublished);
  TPropType = (ptField,ptProperty,ptFunction,ptProcedure,ptSignal,
               ptHelperProc,ptHelperFunc,ptSignalType,ptDeclarations,ptTypeDecl,
               ptConstructor,ptDestructor,ptInitialization, ptFinalization);
  TpropFuncType = (pftGtkFunc,pftObjField,pftObjFunc,pftField,pftProc,pftNotImplemented,
                   pftGtkMacro,pftExistingProc);
  TParamType = (ptNone,ptVar,ptConst);
  TProcType = (ptOverride, ptVirtual, ptDynamic, ptAbstract, ptCdecl,
               ptOverload, ptReintroduce);
  TProcTypeSet = set of TProcType;

  TObjectDefs = class;
  TObjectItem = class;
  TPropertyItem = class;


  TParameterItem = class (TCollectionItem)
  private
    FName : string;
    FConvert: boolean;
    FpascalType: string;
    FParamType: TParamType;
  protected
    function GetDisplayName : string; override;
    procedure SetDisplayName(Const Value : string); override;
    procedure AssignTo(Dest: TPersistent); override;
  public
    constructor Create (ACollection : TCollection); override;
    destructor destroy; override;
  published
    property Name : string read FName write FName;
    { De naam van de parameter }
    property PascalType : string read FpascalType write FPascalType;
    { Zijn type }
    property Convert : boolean read FConvert write FConvert default false;
    { geeft aan of er een omzetting dient te gebeuren voor het gebruiken }
    property ParamType : TParamType read FParamType write FParamType default ptNone;
    { het type van parameter : var, const of niets }
  end;

  TParamCollection = class (TCollection)
  private
    FProcedure : TPropertyItem;
    function GetItem(Index: Integer): TParameterItem;
    procedure SetItem(Index: Integer; const Value: TParameterItem);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor create (AOwner : TPropertyItem);
    property Items[Index: Integer]: TParameterItem read GetItem write SetItem; default;
  end;


  TPropertyItem = class (TCollectionItem)
  private
    FPropType : TPropType;
    FName: string;
    FSection: TInterfaceSection;
    FPascalType: string;
    FParameters: TParamCollection;
    FGtkName: string;
    FWriteProcType: TpropFuncType;
    FReadFuncType: TPropFuncType;
    FWriteGtkName: string;
    FCode: TStringList;
    FWriteCode: TStringList;
    FProctypes: TProcTypeSet;
    FWriteConvert: boolean;
    FReadConvert: boolean;
    procedure SetCode(const Value: TStringList);
    procedure SetWriteCode(const Value: TStringList);
    procedure SetPropType(const Value: TPropType);
  protected
    function GetDisplayName: string; override;
    procedure SetDisplayName(const Value: string); override;
    procedure AssignTo(Dest: TPersistent); override;
  public
    constructor create (ACollection : TCollection); override;
    destructor destroy; override;
  published
    property PropType : TPropType read FPropType write SetPropType default ptProcedure;
    { wat voor iets het is } // Moet voor DisplayName staan voor goede inleesvolgorde
    property Name : string read FName write FName;
    { Naam van de property/functie/proc/veld/... }
    property Section : TInterfaceSection read FSection write FSection default isPublic;
    { waar het geplaats moet worden private, public, ... }
    property PascalType : string read FPascalType write FPascalType;
    { het type van property, functie, veld, signal (moet dan wel gedefinieerd zijn) }
    property Parameters : TParamCollection read FParameters write FParameters;
    { de parameters die doorgegeven moeten worden via de functie/procedure/signaltype }
    property GtkName : string read FGtkName write FGtkName;
    { de naam zoals GTK die gebruikt (waarschijnlijk met _ in) }
    property Code : TStringList read FCode write SetCode;
  { Property specifiek }
    // ReadGtkName wordt weggeschreven in GtkName
    // ReadCode wordt weggeschreven in Code
    // parameters worden gebruikt om indexen aan te geven
    property ReadFuncType : TPropFuncType read FReadFuncType write FReadFuncType default pftGtkFunc;
    { hoe de read functie moet werken : gtk-functie, object-veld, object-functie, eigen functie }
    property ReadConvert : boolean read FReadConvert write FReadConvert default false;
    { Geeft aan of de waarde voor toekenning aan result moet omgezet worden }
    property WriteProcType : TpropFuncType read FWriteProcType write FWriteProcType default pftGtkFunc;
    { hoe de write functie moet werken : gtk-proc, object-veld, object-proc, eigen proc }
    property WriteGtkName : string read FWriteGtkName write FWriteGtkName;
    { de naam zoals gtk of object die gebruikt. Gebruikt in write, voor read zie GtkName }
    property WriteConvert : boolean read FWriteConvert write FWriteConvert default false;
    { Geeft aan of de waarde moet omgezet worden voor het doorgeven }
    property WriteCode : TStringList read FWriteCode write SetWriteCode;
  { procedure specifiek } //gebruikt code
    property ProcTypes : TProcTypeSet read FProctypes write FProcTypes default [];
    { Duid het type procedure/functie aan : abstract, virtual, ... }
  end;

  TPropertyCollection = class (TCollection)
  private
    FObject : TobjectItem;
    function GetItem(Index: Integer): TPropertyItem;
    procedure SetItem(Index: Integer; const Value: TPropertyItem);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor create (AOwner : TObjectItem);
    property Items[Index: Integer]: TPropertyItem read GetItem write SetItem; default;
  end;


  TObjectItem = class (TCollectionItem)
  private
    FInherit: string;
    FName: string;
    FProps: TPropertyCollection;
    FGtkFuncName: string;
    FWithPointer: boolean;
    FCreateObject: boolean;
    FGtkName: string;
    FCreateParams: string;
    procedure SetProps(const Value: TPropertyCollection);
    procedure SetGtkFuncName(const Value: string);
  protected
    function GetDisplayName: string; override;
    procedure SetDisplayName(const Value: string); override;
    procedure AssignTo(Dest: TPersistent); override;
  public
    constructor create (ACollection : TCollection); override;
    destructor destroy; override;
  published
    property Name : string read FName write FName;
    { Naam van het object }
    property Inherit : string read FInherit write FInherit;
    { De naam van het object dat ancester is }
    property GtkFuncName : string read FGtkFuncName write SetGtkFuncName;
    { Naam van het object in gtk zoals het in de functies en procedures gebruikt wordt }
    property GtkName : string read FGtkName write FGtkName;
    { Naam van het objectrecord in gtk zoals gebruikt in typedeclaraties}
    property Props : TPropertyCollection read FProps write SetProps;
    { De verschillende properties, procedures, ... van en voor het object }
    property WithPointer : boolean read FWithPointer write FWithPointer default false;
    { duid aan of er ook een pointerdefinitie moet zijn }
    property CreateObject : boolean read FCreateObject write FCreateObject default false;
    { duid aan of er een CreateGtkObject procedure moet aangemaakt worden }
    property CreateParams : string read FCreateParams write FCreateParams;
    { Geeft de parameters die meegeven moeten worden aan de _New functie }
  end;

  TObjectCollection = class (TCollection)
  private
    FGtkDEf : TObjectDefs;
    function GetItem(Index: Integer): TObjectItem;
    procedure SetItem(Index: Integer; const Value: TObjectItem);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor create (AOwner : TObjectDefs);
    property Items[Index: Integer]: TObjectItem read GetItem write SetItem; default;
  end;


  TObjectDefs = class(TComponent)
  private
    FDefinition: TObjectCollection;
    FGtkPrefix,
    FUsesList,
    FUnitName: string;
    {$IFNDEF Delphi}
    FTop, FLeft : integer;
    {$ENDIF}
    procedure SetDefinition(const Value: TObjectCollection);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor create (AOwner : TComponent); override;
    destructor destroy; override;
    procedure Write (TheUnit : TStrings; StepIt : TLukStepItProc; StepItMax : TLukStepItMaxProc);
    procedure Save (List : TStrings);
    procedure Load (List : TStrings);
  published
    { Published declarations }
    property Definition : TObjectCollection read FDefinition write SetDefinition;
    property GtkPrefix : string read FGtkPrefix write FGtkPrefix;
    property UnitName : string read FUnitName write FUnitName;
    property UsesList : string read FUsesList write FUsesList;
    {$IFNDEF delphi}
    // Compatibiliteit met Delphi
    property Left : integer read FLeft write FLeft;
    property Top : integer read FTop write FTop;
    {$ENDIF}
  end;

var
  GtkPrefix : string = 'gtk';
  ObjectsPrefix : string = 'FPgtk';

procedure Register;

implementation

//uses dsgnIntf;

const
  SectPublic = [isPublic,isPublished];
  SectPriv = [isPrivate,isProtected];
  CRLF = #13#10;
  PropUsesGtkName = [pftProc, pftExistingProc];

var
  lowerObjectsPrefix : string;
  ObjectsPrefixLength : integer;

procedure Register;
begin
  RegisterComponents('Luk', [TObjectDefs]);
end;

{ TParamCollection }

constructor TParamCollection.create(AOwner: TPropertyItem);
begin
  inherited Create (TParameterItem);
  FProcedure := AOwner;
end;

function TParamCollection.GetItem(Index: Integer): TParameterItem;
begin
  result := TParameterItem (inherited Items[index]);
end;

function TParamCollection.GetOwner: TPersistent;
begin
  result := FProcedure;
end;

procedure TParamCollection.SetItem(Index: Integer;
  const Value: TParameterItem);
begin
  inherited Items[Index] := Value;
end;

{ TParameterItem }

procedure TParameterItem.AssignTo(Dest: TPersistent);
begin
  if Dest is TParameterItem then
    with TParameterItem(Dest) do
      begin
      FName := Self.FName;
      FConvert := Self.FConvert;
      FpascalType := Self.FpascalType;
      FParamType := Self.FParamType;
      end
  else
    inherited;
end;

constructor TParameterItem.Create(ACollection: TCollection);
begin
  inherited;
  FConvert := False;
  FParamType := ptNone;
end;

destructor TParameterItem.destroy;
begin
  inherited;
end;

function TParameterItem.GetDisplayName: string;
begin
  result := FName;
end;

procedure TParameterItem.SetDisplayName(const Value: string);
begin
  FName := Value;
end;


{ TPropertyItem }

procedure TPropertyItem.AssignTo(Dest: TPersistent);
var r : integer;
begin
  if Dest is TPropertyItem then
    with TPropertyItem(Dest) do
      begin
      FPropType := Self.FPropType;
      FName := Self.FName;
      FSection := Self.FSection;
      FPascalType := Self.FPascalType;
      FParameters.clear;
      for r := 0 to pred(self.FParameters.count) do
        FParameters.Add.assign (self.FParameters[r]);
      FGtkName := Self.FGtkName;
      FWriteProcType := Self.FWriteProcType;
      FReadFuncType := Self.FReadFuncType;
      FWriteGtkName := Self.FWriteGtkName;
      FCode.Assign(Self.FCode);
      FWriteCode.assign(Self.FWriteCode);
      FProctypes := Self.FProctypes;
      FWriteConvert := Self.FWriteConvert;
      FReadConvert := Self.FReadConvert;
      end
  else
    inherited;
end;

constructor TPropertyItem.create(ACollection: TCollection);
begin
  inherited;
  FParameters := TParamCollection.Create (Self);
  FPropType := ptProcedure;
  FSection := isPublic;
  FCode := TStringList.Create;
  FWriteCode := TStringList.Create;
  {$IFDEF writecreate}
  writeln ('Property Item created');
  {$ENDIF}
end;

destructor TPropertyItem.destroy;
begin
  FParameters.Free;
  inherited;
end;

const
  DispPropType : array [TPropType] of string =
    ('Field','Property','Function','Procedure', 'Signal',
     'HelperProc','HelperFunc','SignalType','Declarations', 'TypeDeclaration',
     'Constructor','Destructor','Initialization','Finilization');

function TPropertyItem.GetDisplayName: string;
begin
  if FPropType = ptDeclarations then
    if Section = ispublished then
      result := 'Interface code before'
    else if Section = ispublic then
      result := 'Interface code after'
    else
      result := 'Implementation code'
  else
    begin
    result := DispProptype[FPropType];
    if FPropType in [ptInitialization, ptFinalization] then
      result := result + ' code'
    else
      result := FName + ' (' + result + ')';
    end;
end;

procedure TPropertyItem.SetCode(const Value: TStringList);
begin
  FCode.assign (Value);
end;

procedure TPropertyItem.SetDisplayName(const Value: string);
begin
  FName := Value;
end;

procedure TPropertyItem.SetPropType(const Value: TPropType);
begin
  FPropType := Value;
end;

procedure TPropertyItem.SetWriteCode(const Value: TStringList);
begin
  FWriteCode.assign (Value);
end;

{ TPropertyCollection }

constructor TPropertyCollection.create (AOwner : TObjectItem);
begin
  inherited create (TPropertyItem);
  FObject := AOwner;
end;

function TPropertyCollection.GetItem(Index: Integer): TPropertyItem;
begin
  result := TPropertyItem(inherited items[index]);
end;

function TPropertyCollection.GetOwner: TPersistent;
begin
  result := FObject;
end;

procedure TPropertyCollection.SetItem(Index: Integer;
  const Value: TPropertyItem);
begin
  Inherited Items[index] := Value;
end;


{ TObjectItem }

procedure TObjectItem.AssignTo(Dest: TPersistent);
var r : integer;
begin
  if Dest is TObjectItem then
    with TObjectItem(Dest) do
      begin
      FName := self.FName;
      FProps.clear;
      for r := 0 to pred(Self.FProps.count) do
        FProps.Add.assign (self.FProps[r]);
      FInherit := Self.FInherit;
      FGtkFuncName := Self.FGtkFuncName;
      FWithPointer := Self.FWithPointer;
      FCreateObject := Self.FCreateObject;
      FGtkName := Self.FGtkName;
      FCreateParams := Self.FCreateParams;
      end
  else
    inherited;
end;

constructor TObjectItem.create(ACollection: TCollection);
begin
  inherited create (ACollection);
  FProps := TpropertyCollection.Create (Self);
end;

destructor TObjectItem.destroy;
begin
  FProps.Free;
  inherited;
end;

function TObjectItem.GetDisplayName: string;
begin
  result := FName;
end;

procedure TObjectItem.SetDisplayName(const Value: string);
begin
  FName := Value;
end;

procedure TObjectItem.SetGtkFuncName(const Value: string);
begin
  FGtkFuncName := Value;
  {$IFDEF writecreate}
  writeln ('GtkFuncname = ', Value);
  {$ENDIF}
end;

procedure TObjectItem.SetProps(const Value: TPropertyCollection);
begin
  FProps.assign(Value);
end;

{ TObjectCollection }

constructor TObjectCollection.create (AOwner : TObjectDefs);
begin
  inherited create (TObjectItem);
  FGtkDef := AOwner;
end;

function TObjectCollection.GetItem(Index: Integer): TObjectItem;
begin
  result := TObjectItem(inherited Items[index]);
end;

function TObjectCollection.GetOwner: TPersistent;
begin
  result := FGtkDef;
end;

procedure TObjectCollection.SetItem(Index: Integer;
  const Value: TObjectItem);
begin
  inherited items[index] := Value;
end;


{ TObjectDefs }

constructor TObjectDefs.create (AOwner : TComponent);
begin
  inherited create (AOwner);
  FDefinition := TObjectCollection.Create (self);
  FgtkPrefix := 'gtk';
end;

destructor TObjectDefs.destroy;
begin
  FDefinition.Free;
  inherited;
end;

procedure TObjectDefs.SetDefinition(const Value: TObjectCollection);
begin
  FDefinition.assign(Value);
end;

const
  DispPropFuncType : array [TPropFuncType] of string = ('GtkFunc','ObjField',
      'ObjFunc','Field','Proc','NotImplemented','GtkMacro','ExistingProc');
  DispProcType : array [TProcType] of string = ('Override', 'Virtual', 'Dynamic',
      'Abstract', 'Cdecl', 'Overload', 'Reintroduce');

procedure TObjectDefs.Save (List : TStrings);

  procedure WriteParameter (AParameter : TParameterItem);
  begin
    with AParameter do
      begin
      List.Add ('      Param=' + FName);
      if FConvert then
        List.Add ('        Convert');
      if FpascalType <> '' then
        List.Add ('        PascalType=' + FpascalType);
      if FParamType = ptVar then
        List.Add ('        ParamType=Var')
      else if FParamType = ptConst then
        List.Add ('        ParamType=Const');
      end;
  end;

  procedure WriteProperty (AProperty : TPropertyItem);
  var r : integer;
      pt : TProcType;
  begin
    with AProperty do
      begin
      List.Add ('    Prop=' + FName);
      List.Add ('      PropType='+DispPropType[FPropType]);
      if FSection = isprivate then
        List.Add ('      Section=Private')
      else if FSection = isprotected then
        List.Add ('      Section=Protected')
      else if FSection = isPublished then
        List.Add ('      Section=Published');
      if FPascalType <> '' then
        List.Add ('      PascalType=' + FPascalType);
      if FGtkName <> '' then
        List.Add ('      GtkName=' + FGtkName);
      if Fcode.count > 0 then
        List.Add ('      Code='+FCode.Commatext);
      if FReadConvert then
        List.Add ('      ReadConvert');
      if FReadFuncType <> pftGtkFunc then
        List.Add ('      ReadFuncType='+ DispPropFuncType[FReadFuncType]);
      if FWriteProcType <> pftGtkFunc then
        List.Add ('      WriteProcType='+ DispPropFuncType[FWriteProcType]);
      if FWriteGtkName <> '' then
        List.Add ('      WriteGtkName=' + FWriteGtkName);
      if FWritecode.count > 0 then
        List.Add ('      WriteCode='+FWriteCode.Commatext);
      if FWriteConvert then
        List.Add ('      WriteConvert');
      if FProcTypes <> [] then
        for pt := low(TProcType) to high(TProcType) do
          if pt in FProcTypes then
            List.Add ('      '+DispProcType[pt]);
      with FParameters do
        begin
        List.Add ('      Count='+inttostr(Count));
        for r := 0 to count-1 do
          WriteParameter (Items[r]);
        end;
      end;
  end;

  procedure WriteObject (AnObject : TObjectItem);
  var r : integer;
  begin
    with AnObject do
      begin
      List.Add ('  Object=' + FName);
      if FInherit <> '' then
        List.Add ('    Inherit=' + FInherit);
      if FGtkFuncName <> '' then
        List.Add ('    GtkFuncName=' + FGtkFuncName);
      if FGtkName <> '' then
        List.Add ('    GtkName=' + FGtkName);
      if FCreateParams <> '' then
        List.Add ('    CreateParams=' + FCreateParams);
      if FWithPointer then
        List.Add ('    WithPointer');
      if FCreateObject then
        List.Add ('    CreateObject');
      with FProps do
        begin
        List.Add ('    Count='+inttostr(count));
        for r := 0 to count-1 do
          WriteProperty (Items[r]);
        end;
      end;
  end;

var r : integer;
begin
  List.Add ('definition');
  if FGtkPrefix <> '' then
    List.Add ('  GtkPrefix=' + FGtkPrefix);
  if FUsesList <> '' then
    List.Add ('  UsesList=' + FUsesList);
  if FUnitName <> '' then
    List.Add ('  UnitName=' + FUnitName);
  with Definition do
    begin
    List.Add ('  Count=' + inttostr(count));
    for r := 0 to count-1 do
      WriteObject (Items[r])
    end;
end;

resourcestring
  sErrWrongFirstLine = 'Error: First line doesn''t contain correct word';
  sErrCountExpected = 'Error: "Count" expected on line %d';
  sErrObjectExpected = 'Error: "Object" expected on line %d';
  sErrPropertyExpected = 'Error: "Prop" expected on line %d';
  sErrProptypeExpected = 'Error: "PropType" expected on line %d';
  sErrParameterExpected = 'Error: "Param" expected on line %d';

procedure TObjectDefs.Load (List : TStrings);

var line : integer;
    item, value : string;
    HasLine : boolean;

  procedure SplitNext;
  var p : integer;
  begin
    inc (line);
    HasLine := (line < List.Count);
    if HasLine then
      begin
      item := List[Line];
      p := pos ('=', item);
      if p = 0 then
        value := ''
      else
        begin
        value := copy(item, p+1, maxint);
        item := copy(item, 1, p-1);
        end;
      end
    else
      begin
      Item := '';
      value := '';
      end;
  end;

  procedure ReadParameter (AParameter : TParameterItem);
  begin
    with AParameter do
      begin
      if HasLine and (item = '      Param') then
        begin
        FName := value;
        {$ifdef LoadDebug}writeln ('    Parameter Name ', FName);{$endif}
        SplitNext;
        end
      else
        raise exception.CreateFmt (sErrParameterExpected, [line]);
      if HasLine then
        begin
        FConvert := (item = '        Convert');
        {$ifdef LoadDebug}writeln ('              Convert ', FConvert);{$endif}
        if FConvert then
          SplitNext;
        end;
      if HasLine and (item = '        PascalType') then
        begin
        FPascalType := value;
        {$ifdef LoadDebug}writeln ('              PascalType ', FPascalType);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '        ParamType') then
        begin
        if Value = 'Var' then
          FParamType := ptVar
        else if Value = 'Const' then
          FParamType := ptConst;
        {$ifdef LoadDebug}writeln ('              ParamType ', ord(FParamtype));{$endif}
        SplitNext;
        end;
      end;
  end;

  procedure ReadProperty (AProperty : TPropertyItem);
  var RProcType : TProcType;
      Rproptype : TPropType;
      RpropFuncType : TpropFuncType;
      counter : integer;
      s : string;
  begin
    with AProperty do
      begin
      if HasLine and (item = '    Prop') then
        begin
        FName := value;
        {$ifdef LoadDebug}writeln ('  Property Name ', FName);{$endif}
        SplitNext;
        end
      else
        raise exception.CreateFmt (sErrPropertyExpected, [line]);
      if HasLine and (item = '      PropType') then
        begin
        RProptype := high(TPropType);
        while (RPropType > low(TPropType)) and (DispPropType[RPropType] <> value) do
          dec (RPropType);
        FPropType := RPropType;
        {$ifdef LoadDebug}writeln ('           PropType ', ord(FPropType));{$endif}
        SplitNext;
        end
      else
        raise exception.CreateFmt (sErrPropTypeExpected, [Line]);
      Section := isPublic;
      if HasLine and (item = '      Section') then
        begin
        if value = 'Private' then
          Section := isPrivate
        else if value = 'Protected' then
          FSection := isprotected
        else if value = 'Published' then
          FSection := isPublished;
        SplitNext;
        {$ifdef LoadDebug}writeln ('           Section ', ord(FSection));{$endif}
        end;
      if HasLine and (item = '      PascalType') then
        begin
        FPascalType := value;
        {$ifdef LoadDebug}writeln ('           PascalType ', FPascalType);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '      GtkName') then
        begin
        FGtkName := value;
        {$ifdef LoadDebug}writeln ('           GtkName ', FGtkName);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '      Code') then
        begin
        FCode.Commatext := value;
        {$ifdef LoadDebug}writeln ('           Code set');{$endif}
        SplitNext;
        end;
      if HasLine then
        begin
        FReadConvert := (item = '      ReadConvert');
        {$ifdef LoadDebug}writeln ('           ReadConvert ', FReadConvert);{$endif}
        if FReadConvert then
          SplitNext;
        end;
      if HasLine and (item = '      ReadFuncType') then
        begin
        RpropFuncType := high(TpropFuncType);
        while (RpropFuncType > low(TpropFuncType)) and
              (value <> DispPropFuncType[RpropFuncType]) do
          dec (RpropFuncType);
        FReadFuncType := RpropFuncType;
        {$ifdef LoadDebug}writeln ('           ReadFuncType ', ord(FReadFunctype));{$endif}
        if RpropFuncType > low(TpropFuncType) then
          Splitnext;
        end;
      if HasLine and (item = '      WriteProcType') then
        begin
        RpropFuncType := high(TpropFuncType);
        while (RpropFuncType > low(TpropFuncType)) and
              (value <> DispPropFuncType[RpropFuncType]) do
          dec (RpropFuncType);
        FWriteProcType := RpropFuncType;
        {$ifdef LoadDebug}writeln ('           WriteProcType ', ord(FWriteProcType));{$endif}
        if RpropFuncType > low(TpropFuncType) then
          Splitnext;
        end;
      if HasLine and (item = '      WriteGtkName') then
        begin
        FWriteGtkName := value;
        {$ifdef LoadDebug}writeln ('           WriteGtkName ', FWriteGtkName);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '      WriteCode') then
        begin
        FWriteCode.Commatext := value;
        {$ifdef LoadDebug}writeln ('           WriteCode set');{$endif}
        SplitNext;
        end;
      if HasLine then
        begin
        FWriteConvert := (item = '      WriteConvert');
        {$ifdef LoadDebug}writeln ('           WriteConvert ', FWriteConvert);{$endif}
        if FWriteConvert then
          SplitNext;
        end;
      FProcTypes := [];
      if HasLine then
        begin
        s := copy(item, 7, 35);
        for RProcType := low(TProcType) to high(TProcType) do
          if s = DispProcType[RProcType] then
            begin
            FProcTypes := FProcTypes + [RProcType];
            {$ifdef LoadDebug}writeln ('           ProcType added ', s);{$endif}
            SplitNext;
            s := copy(item, 7, 35);
            end;
        end;
      if HasLine and (Item = '      Count') then
        with FParameters do
          begin
          counter := strtoint(value);
          {$ifdef LoadDebug}writeln ('           Counter ', Counter);{$endif}
          SplitNext;
          while (Counter > 0) do
            begin
            ReadParameter (Add as TParameterItem);
            dec (counter);
            end;
          end
      else
        raise exception.CreateFmt (sErrCountExpected, [line]);
      end;
  end;

  procedure ReadObject (AnObject : TObjectItem);
  var counter : integer;
  begin
    with AnObject do
      begin
      if HasLine and (item = '  Object') then
        begin
        FName := value;
        {$ifdef LoadDebug}writeln ('Object name ', FName);{$endif}
        SplitNext;
        end
      else
        raise exception.CreateFmt (sErrObjectExpected, [line]);
      if HasLine and (item = '    Inherit') then
        begin
        FInherit := value;
        {$ifdef LoadDebug}writeln ('       Inherit ', FInherit);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '    GtkFuncName') then
        begin
        FGtkFuncName := value;
        {$ifdef LoadDebug}writeln ('       GtkFuncName ', FGtkFuncName);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '    GtkName') then
        begin
        FGtkName := value;
        {$ifdef LoadDebug}writeln ('       GtkName ', FGtkName);{$endif}
        SplitNext;
        end;
      if HasLine and (item = '    CreateParams') then
        begin
        FCreateParams := value;
        {$ifdef LoadDebug}writeln ('       CreateParams ', FCreateParams);{$endif}
        SplitNext;
        end;
      if HasLine then
        begin
        FWithPointer := (item = '    WithPointer');
        {$ifdef LoadDebug}writeln ('       WithPointer ', FWithPointer);{$endif}
        if FWithPointer then
          SplitNext;
        end;
      if HasLine then
        begin
        FCreateObject := (item = '    CreateObject');
        {$ifdef LoadDebug}writeln ('       CreateObject ', FCreateObject);{$endif}
        if FCreateObject then
          SplitNext;
        end;
      if HasLine and (Item = '    Count') then
        with FProps do
          begin
          counter := strtoint(value);
          {$ifdef LoadDebug}writeln ('       Counter ', counter);{$endif}
          SplitNext;
          while (Counter > 0) do
            begin
            ReadProperty (Add as TPropertyItem);
            dec (counter);
            end;
          end
      else
        raise exception.CreateFmt (sErrCountExpected, [line]);
      end;
  end;

var counter : integer;
begin
  {$ifdef LoadDebug}writeln ('Start load');{$endif}
  if List[0] <> 'definition' then
    raise Exception.Create (sErrWrongFirstLine);
  {$ifdef LoadDebug}writeln ('Correct startline');{$endif}
  line := 0;
  {$ifdef LoadDebug}writeln ('Calling SplitNext');{$endif}
  SplitNext;
  if HasLine and (Item = '  GtkPrefix') then
    begin
    {$ifdef LoadDebug}writeln ('GtkPrefix=',value);{$endif}
    FGtkPrefix := value;
    SplitNext;
    end
  else
    FGtkPrefix := '';
  if HasLine and (Item = '  UsesList') then
    begin
    {$ifdef LoadDebug}writeln ('UsesList=',value);{$endif}
    FUsesList := value;
    SplitNext;
    end
  else
    FUsesList := '';
  if HasLine and (Item = '  UnitName') then
    begin
    {$ifdef LoadDebug}writeln ('UnitName=',value);{$endif}
    FUnitName := value;
    SplitNext;
    end
  else
    FUnitName := '';
  if HasLine and (Item = '  Count') then
    begin
    counter := strtoint(value);
    {$ifdef LoadDebug}writeln ('Counter ', counter);{$endif}
    if assigned(FDefinition) then
      begin
      {$ifdef LoadDebug}writeln ('Clearing ObjectDefinitions');{$endif}
      FDefinition.Clear;
      end
    else
      begin
      {$ifdef LoadDebug}writeln ('Creating ObjectDefinitions');{$endif}
      FDefinition := TObjectCollection.Create (self);
      end;
    SplitNext;
    while (Counter > 0) do
      begin
      ReadObject (Definition.Add as TObjectItem);
      dec (counter);
      end;
    end
  else
    raise exception.CreateFmt (sErrCountExpected, [line]);
end;

procedure TObjectDefs.Write(TheUnit : TStrings; StepIt : TLukStepItProc; StepItMax : TLukStepItMaxProc);

  procedure DoStepIt;
  begin
    if assigned (StepIt) then
      StepIt;
  end;

  procedure DoStepItMax (Max : integer);
  begin
    if assigned (StepItMax) then
      StepItMax (Max);
  end;

  procedure WriteObjectForward (Obj : TObjectItem);
  begin
    with obj do
      TheUnit.add ('  T'+ObjectsPrefix+Name+' = class;');
  end;

  function CalcProcTypes (ProcTypes : TProcTypeSet; InImplementation:boolean) : string; overload;
  begin
    if not InImplementation then
      begin
      if ptOverride in ProcTypes then
        result := ' Override;'
      else
        begin
        if ptVirtual in ProcTypes then
          result := ' Virtual;'
        else if ptDynamic in ProcTypes then
          result := ' Dynamic;'
        else
          result := '';
        if (result <> '') and (ptAbstract in ProcTypes) then
          result := result + ' Abstract;';
        end;
      if ptreintroduce in ProcTypes then
        result := result + ' Reintroduce;';
      end;
    if ptCDecl in ProcTypes then
      result := result + ' Cdecl;';
    if ptOverload in ProcTypes then
      result := result + ' Overload;';
  end;

  function CalcProcTypes (ProcTypes : TProcTypeSet) : string; overload;
  begin
    result := CalcProcTypes (ProcTypes, False);
  end;

  type
    TConvType = (ToGtk, ToLuk, ToFPgtk);

  function ConvertType (PascalType : string; ConvType : TConvType) : string;
  begin
    PascalType := lowercase (PascalType);
    if ConvType = ToGtk then
      begin
      if PascalType = 'string' then
        result := 'pgChar'
      else if copy(PascalType,1,ObjectsPrefixLength+1) = 't'+LowerObjectsPrefix then
        result := 'PGtk' + copy (PascalType, ObjectsPrefixLength+2, maxint)
      else if PascalType = 'longbool' then
        result := 'gint'
      else
        result := PascalType;
      end
    else
      begin
      if PascalType = 'pgChar' then
        result := 'string'
      else if copy(PascalType,1,4) = 'pgtk' then
        result := 'T'+ObjectsPrefix + copy (PascalType, 5, maxint)
      else if PascalType = 'gint' then
        result := 'longbool'
      else
        result := PascalType;
      end;
  end;

  function DoConvert (Variable, PascalType : string; ConvType : TConvType) : string;
  var s : string;
  begin
    result := variable;
    PascalType := lowercase (PascalType);
    if PascalType = 'string' then
      begin
      if ConvType <> ToLuk then
        result := 'ConvertToPgchar('+result+')'
      end
    else if copy(PascalType,1,4)='pgtk' then
      begin
      if ConvType = ToLuk then
        begin
        s := 'T'+ObjectsPrefix + copy(PascalType, 5, maxint);
        result := 'GetPascalInstance(PGtkObject('+result+'),'+s+') as '+ s
        end
      else
        result := PascalType+'(ConvertToGtkObject('+result+'))'
      end
    else if Copy(PascalType,1,ObjectsPrefixLength+1)='t'+LowerObjectsPrefix then
      begin
      if ConvType = ToLuk then
        result := 'GetPascalInstance(PGtkObject('+result+'),'+PascalType+') as '+PascalType
      else
        result := 'PGtk'+copy(PascalType,ObjectsPrefixLength+2,maxint)+'(ConvertToGtkObject('+result+'))'
      end
    else if PascalType = 'boolean' then
      begin
      if (copy(variable,1,4)='gtk.') and
              (ConvType = ToLuk) then
        result := 'boolean('+variable+')'
      else if  ConvType = ToFPGtk then
        result := 'guint('+variable+')'
      end
    else if PascalType = 'longbool' then
      begin
      if (copy(variable,1,4)='gtk.') and
              (ConvType = ToLuk) then
        result := 'longbool('+variable+')'
      else if ConvType in [ToFPGtk,ToGtk] then
        result := 'gint('+variable+')';
      end;
  end;

  function CalcParam (param : TParameterItem; Declaration : boolean; ConvType : TConvType) : string;
  begin
    with Param do
      begin
      if Declaration then
        begin
        case param.ParamType of
          ptVar   : result := 'var ';
          ptconst : result := 'const ';
          else      result := '';
        end;
        result := result + Name + ':' + PascalType;
        end
      else
        if Convert then
          result := DoConvert (Name, PascalType, convType)
        else
          result := name;
      end;
  end;

  type
    TParamListType = (plDecl, plImpl, plImplCl, plImplLukCl);

  function CalcParameterList (params : TParamCollection; PLType : TParamListType) : string; overload;
  var r : integer;
      Sep : string[2];
      ct : TConvType;
  begin
    if PLType = plDecl then
      Sep := '; '
    else
      Sep := ', ';
    if PLType = plImplLukCl then
      ct := ToLuk
    else
      ct := ToGtk;
    with params do
      if count = 0 then
        result := ''
      else
        begin
        result := CalcParam (Items[0], (PLType=plDecl), ct);
        for r := 1 to count-1 do
          result := result + Sep + CalcParam (items[r], (PLType=plDecl), ct);
        if PLType <> plImpl then
          result := ' (' + result + ')';
        end;
  end;

  function CalcParameterList (params : TParamCollection) : string; overload;
  var r : integer;
  begin
    with params do
      if count = 0 then
        result := ''
      else
        begin
        with Items[0] do
          result := Name + ':' + PascalType;
        for r := 1 to count-1 do
          with Items[r] do
            result := result + '; ' + Name + ':' + PascalType;
        end;
  end;

  var  Lpublic, LProt, LPriv, LPublish : TStrings;

  procedure WriteObjectInterface (Obj : TObjectItem);
  var r : integer;
      TheList : TStrings;
      I, N, s : string;
  begin
    Lpublic.Clear;
    LProt.Clear;
    LPriv.Clear;
    LPublish.clear;
    with obj do
      begin
      // Signal declarations
      with props do
        begin
        for r := 0 to count-1 do
          with Items[r] do
            begin
            if (PropType = ptSignalType) then
              if PascalType = '' then
                TheUnit.add ('  T'+ObjectsPrefix+Name+'Function = procedure' +
                            CalcParameterList(parameters,plDecl)+' of Object;')
              else
                TheUnit.add ('  T'+ObjectsPrefix+Name+'Function = function' +
                            CalcParameterList(parameters,plDecl)+': '+PascalType+' of Object;')
            else if (PropType = ptTypeDecl) then
              TheUnit.AddStrings (Code);
            end;
        end;
      TheUnit.Add ('');
      // Class definition
      if WithPointer then
        TheUnit.Add ('  P'+ObjectsPrefix+Name+' = ^T'+ObjectsPrefix+Name+';');
      if Inherit = '' then
        TheUnit.add ('  T'+ObjectsPrefix+Name+' = class')
      else
        begin
        if inherit[1] = '*' then
          s := copy(inherit, 2, maxint)
        else
          s := ObjectsPrefix + Inherit;
        TheUnit.add ('  T'+ObjectsPrefix+Name+' = class (T'+s+')');
        end;
      { Filling the 4 sections with the properties }
      for r := 0 to props.count-1 do
        with Props[r] do
          begin
          case Section of
            isPrivate : TheList := LPriv;
            isProtected : TheList := LProt;
            isPublic : TheList := LPublic;
            else TheList := LPublish;
          end;
          case PropType of
            ptField :
              TheList.Insert(0,'    ' + Name + ':' + PascalType + ';');
            ptProperty :
              begin
              s := '    property ' + Name;
              if (ReadFuncType <> pftNotImplemented) or
                 (WriteProcType <> pftNotImplemented) then
                begin
                if Parameters.Count > 0 then
                  begin
                  I := CalcParameterlist(parameters);
                  s := s + ' ['+I+'] ';
                  end;
                s := s + ' : ' + PascalType;
                if (ReadFuncType <> pftNotImplemented) then
                  begin
                  s := s + ' read ';
                  if ReadFuncType = pftField then
                    begin
                    if GtkName <> '' then
                      N := GtkName
                    else
                      N := 'F' + Name;
                    LPriv.insert (0, '    ' + N + ' : ' + PascalType + ';');
                    end
                  else
                    begin
                    if (ReadFuncType in PropUsesGtkName) and (GtkName <> '') then
                      N := GtkName
                    else
                      N := 'Get' + Name;
                    if (ReadFuncType <> pftExistingProc) then
                      begin
                      if parameters.count > 0 then
                        LPriv.Add ('    function '+N+'('+I+') : '+PascalType+';')
                      else
                        LPriv.Add ('    function '+N+' : '+PascalType+';');
                      end;
                    end;
                  s := s + N;
                  end;
                if (WriteProcType <> pftNotImplemented) then
                  begin
                  s := s + ' write ';
                  if WriteProcType = pftField then
                    begin
                    if GtkName <> '' then
                      N := GtkName
                    else
                      N := 'F' + Name;
                    if (ReadFuncType <> pftField) then
                      LPriv.insert (0, '    ' + N + ' : ' + PascalType + ';');
                    end
                  else
                    begin
                    if (WriteProcType in PropUsesGtkName) and (WriteGtkName <> '') then
                      N := WriteGtkName
                    else
                      N := 'Set' + Name;
                    if (WriteProcType <> pftExistingProc) then
                      begin
                      if parameters.count > 0 then
                        LPriv.Add ('    procedure '+N+' ('+I+'; TheValue : '+PascalType+');')
                      else
                        LPriv.Add ('    procedure '+N+' (TheValue : '+PascalType+');');
                      end;
                    end;
                  s := s + N;
                  end;
                end;
              TheList.Add (s+';');
              end;
            ptFunction :
              Thelist.Add ('    function ' + Name + CalcParameterList(Parameters, plDecl)
                         + ' : ' + PascalType+';' + CalcProcTypes(ProcTypes));
            ptProcedure :
              TheList.Add ('    procedure ' + Name + CalcParameterList(Parameters, plDecl)
                         + ';' + CalcProcTypes(ProcTypes));
            ptSignal :
              begin
              TheList.Add ('    function Connect'+Name+' (proc:T'+ObjectsPrefix+PascalType+'Function; data:pointer) : guint;');
              TheList.Add ('    function ConnectAfter'+Name+' (proc:T'+ObjectsPrefix+PascalType+'Function; data:pointer) : guint;');
              end;
            ptSignalType :
              begin
              TheList.Add ('    function ' + Name + 'Connect (Signal:string; Proc:T'+ObjectsPrefix+Name+'Function; data:pointer) : guint;');
              TheList.Add ('    function ' + Name + 'ConnectAfter (Signal:string; Proc:T'+ObjectsPrefix+Name+'Function; data:pointer) : guint;');
              end;
            ptConstructor :
              TheList.Add ('    constructor ' + Name + CalcParameterList(Parameters, plDecl)
                         + ';' + CalcProcTypes(ProcTypes));
            ptDestructor :
              TheList.Add ('    destructor ' + Name + CalcParameterList(Parameters, plDecl)
                         + ';' + CalcProcTypes(ProcTypes));
          end;
          end;
      { Adding the sections }
      if LPriv.count > 0 then
        begin
        TheUnit.add ('  Private');
        TheUnit.AddStrings (Lpriv);
        end;
      if (LProt.count > 0) or CreateObject then
        begin
        TheUnit.add ('  Protected');
        if CreateObject then
          TheUnit.add ('    procedure CreateGtkObject; override;');
        if LProt.Count >= 0 then
          TheUnit.AddStrings (Lprot);
        end;
      if (GtkFuncName <> '') or (LPublic.count >= 0) then
        begin
        TheUnit.add ('  Public');
        if (GtkFuncName <> '') then
          TheUnit.add ('    function TheGtkObject : PGtk'+Name+';');
        if LPublic.count >= 0 then
          TheUnit.AddStrings (Lpublic);
        end;
      if LPublish.count > 0 then
        begin
        TheUnit.add ('  Publish');
        TheUnit.AddStrings (Lpublish);
        end;
      end;
    TheUnit.Add ('  end;');
    TheUnit.add ('');
    DoStepIt;
  end;

  procedure WriteObjectImplementation (Obj : TObjectItem);
  var gn, n, s, start, midden, eind, res : string;
      r, l, p : integer;
  begin
    with Obj, TheUnit do
      begin
      n := Name;
      gn := GtkFuncName;
      add (' { T'+ObjectsPrefix+N+' }'+CRLF);
      if gn <> '' then
        // Functie voor alle objecten en header
        add ('function T'+ObjectsPrefix+N+'.TheGtkObject : PGtk'+N+';'+CRLF+
             'begin'+CRLF+
             '  result := P'+GtkPrefix+N+'(FGtkObject);'+CRLF+
             'end;'+CRLF);
      if CreateObject then
        begin
        eind := CreateParams;
        if eind <> '' then
          eind := ' (' + eind + ')';
        add ('procedure T'+ObjectsPrefix+N+'.CreateGtkObject;'+CRLF+
             'begin'+CRLF+
             '  FGtkObject := PGtkObject(gtk_'+gn+'_new'+eind+');'+CRLF+
             'end;'+CRLF);
        end;
      // Declarations toevoegen
      for r := 0 to Props.count-1 do
        with Props[r] do
          if (PropType = ptDeclarations) and (Section in sectPriv) then
            AddStrings (Code);
      // Properties toevoegen
      add ('');
      for r := 0 to props.count-1 do
        with Props[r] do
          begin
          case PropType of
            ptFunction :
              if not (ptAbstract in ProcTypes) then
                begin
                Add ('function T'+ObjectsPrefix + N + '.' + Name +
                     CalcParameterList(Parameters, plDecl) +
                     ' : ' + PascalType+';' + CalcProcTypes(ProcTypes,true));
                if GtkName = '' then
                  AddStrings (Code)
                else
                  begin
                  s := CalcParameterList (Parameters, plImpl);
                  if s <> '' then
                    s := ', ' + s;
                  Add ('begin' + CRLF +
                       '  result := ' + GtkPrefix + '_' + GN + '_' + GtkName +
                           ' (TheGtkObject' + s + ');' + CRLF +
                       'end;');
                  end;
                add ('');
                end;
            ptHelperFunc :
              begin
              Add ('function ' + Name + CalcParameterList(Parameters, plDecl) +
                ' : ' + PascalType+';'+CalcProcTypes(ProcTypes)+CRLF+Code.Text+CRLF);
              end;
            ptProcedure :
              if not (ptAbstract in ProcTypes) then
                begin
                Add ('procedure T'+ObjectsPrefix + N + '.' + Name+
                     CalcParameterList(Parameters,plDecl) + ';' +
                     CalcProcTypes(ProcTypes, True));
                if GtkName = '' then
                  AddStrings (Code)
                else
                  begin
                  s := CalcParameterList (Parameters, plImpl);
                  if s <> '' then
                    s := ', ' + s;
                  Add ('begin' + CRLF +
                       '  ' + GtkPrefix + '_' + GN + '_' + GtkName +
                                ' (TheGtkObject' + s + ');' + CRLF +
                       'end;');
                  end;
                add ('');
                end;
            ptHelperProc :
              Add ('procedure ' + Name + CalcParameterList(Parameters, plDecl) +
                   ';'+CalcProcTypes(ProcTypes)+CRLF+Code.Text+CRLF);
            ptConstructor :
              Add ('constructor T'+ObjectsPrefix + N + '.' + Name+
                   CalcParameterList(Parameters,plDecl) + ';'+CRLF+Code.Text+CRLF);
            ptDestructor :
              Add ('destructor T'+ObjectsPrefix + N + '.' + Name+
                   CalcParameterList(Parameters,plDecl) + ';'+CRLF+Code.Text+CRLF);
            ptSignal :
              begin
              start := 'function T'+ObjectsPrefix + N + '.Connect';
              midden := Name + ' (proc:T'+ObjectsPrefix + PascalType + 'Function; data:pointer) : guint;'+CRLF+
                        'begin' + CRLF +
                        '  result := ' + PascalType + 'Connect';
              eind := ' (sg' + Name + ', proc, data);' + CRLF +
                      'end;'+CRLF;
              Add (start+midden+eind);
              Add (start+'After'+midden+'After'+eind);
              end;
            ptSignalType :
              begin
              midden := '';
              with parameters do
                begin
                if count > 0 then
                  begin
                  {if lowercase(Items[0].Name) = 'sender' then
                    l := 1
                  else
                    l := 0;
                  p := count - 1;
                  if lowercase(Items[p].name) = 'data' then
                    dec (p);
                  }
                  // s = ParameterList for call; midden = parameter for declaration
                  //s := DoConvert ('TheWidget',ConvertType(Items[0].PascalType,ToGtk),ToLuk);
                  s := 'TheWidget as ' + Items[0].PascalType;
                  midden := Items[0].Name+':'+ConvertType(Items[0].PascalType,ToGtk);
                  for l := 1 to count-2 do
                    begin
                    case Items[l].ParamType of
                      ptVar : start := 'var ';
                      ptconst : start := 'const ';
                      else  start := '';
                    end;
                    with Items[l] do
                      if Convert then
                        begin
                        midden := midden+'; '+start+Name+':'+ConvertType(PascalType, ToGtk);
                        s := s+', '+DoConvert (Name,ConvertType(PascalType,ToGtk),ToLuk);
                        end
                      else
                        begin
                        midden := midden+'; '+start+Name+':'+PascalType;
                        s := s+', '+Name;
                        end
                    end;
                  p := count - 1;
                  midden := midden+'; '+Items[p].Name+':'+ConvertType(Items[p].PascalType, ToGtk);
                  s := s+', TheData';
                  end
                else
                  begin
                  s := '';
                  midden := '';
                  end;
                end;
              if PascalType = '' then
                begin
                start := 'procedure';
                eind := '';
                res := '';
                end
              else
                begin
                start := 'function';
                eind := 'result := ';
                res := ' : '+PascalType;
                end;
              Add (start+' '+Name+copy(start,1,4)+' ('+midden+')'+res+'; cdecl;'+CRLF+
                   'var p : T'+ObjectsPrefix+Name+'Function;'+CRLF+
                   'begin'+CRLF+
                   'with PSignalData(data)^ do'+CRLF+
                   '  begin'+CRLF+
                   '  p := T'+ObjectsPrefix+Name+'Function (TheSignalProc);'+CRLF+
                   '  '+eind+'p ('+s+')'+CRLF+
                   '  end;'+CRLF+
                   'end;'+CRLF);
              midden := ' (signal:string; proc:T'+ObjectsPrefix+Name+
                                              'Function; data:pointer) : guint;'+CRLF+
                   'begin'+CRLF+
                   '  result := '+GtkPrefix+'_signal_connect';
              eind:= ' (FGtkObject, pgChar(signal), '+GtkPrefix+'_signal_func(@'+Name+copy(start,1,4)+'), '+
                           'ConvertSignalData(T'+ObjectsPrefix+'SignalFunction(proc), data, true));'+CRLF+

                   'end;'+CRLF;
              start := 'function T'+ObjectsPrefix+N+'.'+Name+'Connect';
              Add (start+midden+eind);
              Add (start+'After'+midden+'_After'+eind);
              end;
            ptProperty :
              begin
              midden := Name;
              if parameters.count > 0 then
                start := ','+CalcParameterList (parameters, plImpl)
              else
                start := '';
              if parameters.count > 0 then
                eind := CalcParameterList (parameters)
              else
                eind := '';
              // Read Function
              if ReadFuncType = pftProc then
                begin
                s := Code.Text;
                if GtkName <> '' then
                  midden := GtkName
                else
                  midden := 'Get' + midden;
                end
              else if ReadFuncType in [pftGtkFunc, pftObjField, pftObjFunc, pftGtkMacro] then
                begin
                midden := 'Get'+midden;
                case ReadFuncType of
                  pftGtkFunc : s := GtkPrefix+'_'+gn+'_get_'+GtkName+'(TheGtkObject'+start+')';
                  pftObjField: s := 'TheGtkObject^.'+GtkName;
                  pftObjFunc : s := 'gtk.'+GtkName+'(TheGtkObject^'+start+')';
                  pftGtkMacro: s := GtkPrefix+'_'+gn+'_'+GtkName+'(TheGtkObject'+start+')';
                end;
                  if ReadConvert then
                    s := DoConvert (s, PascalType, ToLuk);
                  s := 'begin'+CRLF+'  result := '+s+';'+CRLF+'end;'+CRLF;
                end
              else
                s := '';
              if s <> '' then
                begin
                if eind = '' then
                  Add ('function T'+ObjectsPrefix+N+'.'+midden+' : '+PascalType+';'+CRLF+s)
                else
                  Add ('function T'+ObjectsPrefix+N+'.'+midden+' ('+eind+') : '+PascalType+';'+CRLF+s);
                end;
              // Write procedure
              midden := Name;
              if (WriteProcType in [pftGtkFunc,pftObjField,pftObjFunc,pftGtkMacro]) then
                begin
                midden := 'Set' + midden;
                if WriteConvert then
                  if WriteProcType in [pftObjField, pftObjFunc] then
                    s := DoConvert ('TheValue', PascalType, ToFPGtk)
                  else
                    s := DoConvert ('TheValue', PascalType, ToGtk)
                else
                  s := 'TheValue';
                case WriteProcType of
                  pftGtkFunc : s := GtkPrefix+'_'+gn+'_set_'+writeGtkName+'(TheGtkObject'+start+','+s+');';
                  pftGtkMacro: s := GtkPrefix+'_'+gn+'_'+writeGtkName+'(TheGtkObject'+start+','+s+');';
                  pftObjField: s := 'TheGtkObject^.'+writeGtkName+' := '+s+';';
                  pftObjFunc : s := 'gtk.'+'Set_'+WriteGtkName+'(TheGtkObject^'+start+','+s+')';
                end;
                s := 'begin'+CRLF+'  '+s+CRLF+'end;'+CRLF;
                end
              else if WriteProcType = pftProc then
                begin
                s := WriteCode.Text;
                if writegtkname <> '' then
                  midden := writegtkname
                else
                  midden := 'Set' + midden;
                end
              else
                s := '';
              if s <> '' then
                begin
                if eind = '' then
                  Add ('procedure T'+ObjectsPrefix+N+'.'+midden+' ('+'TheValue:' + PascalType+');'+CRLF+s)
                else
                  Add ('procedure T'+ObjectsPrefix+N+'.'+midden+' ('+eind+'; TheValue:' + PascalType+');'+CRLF+s);
                end;
              end;
          end;
          end;
      end;
    DoStepIt;
  end;

var r, t : integer;
    Need : boolean;
    UsedSignals : TStringList;

begin
  LPublic := TStringList.Create;
  LPublish := TStringList.Create;
  LPriv := TStringList.Create;
  LProt := TStringList.Create;
  UsedSignals := TStringList.Create;
  UsedSignals.Sorted := True;
  lowerObjectsPrefix := lowercase (ObjectsPrefix);
  ObjectsPrefixLength := length(lowerObjectsPrefix);
  with TheUnit do
    try
      DoStepItMax (FDefinition.Count * 2 + 4);
      clear;
      capacity := 70 * FDefinition.Count;
      add ('{$mode objfpc}{$h+} {$ifdef win32}{$define gtkwin}{$endif}'+CRLF+
           'UNIT '+UnitName+';'+CRLF+CRLF+
           '// Generated with GtkWrite by Luk Vandelaer (version '+versionnumber+')'+CRLF+CRLF+
           'INTERFACE'+CRLF+CRLF+
           'USES '+UsesList+';');
      // public declarations before classtypes
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          Need := True;
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (PropType = ptDeclarations) and (Section = ispublished) then
                begin
                if Need then
                  begin
                  add ('{ T'+ObjectsPrefix + FDefinition[r].Name + ' }');
                  Need := False;
                  end;
                AddStrings (Code);
                end;
          end;
      DoStepIt;
      Add (CRLF+'TYPE'+CRLF);
      //Forward en implementation moeten in dezelfde Type block zitten
        // Forward declarations
        for r := 0 to pred(FDefinition.count) do
          WriteObjectForward (FDefinition[r]);
        // class declaration
        add ('');
        DoStepIt;
        for r := 0 to pred(FDefinition.count) do
          WriteObjectInterface (FDefinition[r]);
      // public declarations after classtypes
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          Need := True;
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (PropType = ptDeclarations) and (Section = ispublic) then
                begin
                if Need then
                  begin
                  add ('{ T'+ObjectsPrefix + FDefinition[r].Name + ' }');
                  Need := False;
                  end;
                AddStrings (Code);
                end;
          end;
      // declaration of signal constants
      Add (CRLF+'Const');
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          Need := True;
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (Section <> isPrivate) and
                 (PropType = ptsignal) and
                 (UsedSignals.indexof (Name) < 0) then
                begin
                if Need then
                  begin
                  add ('// T'+ObjectsPrefix + FDefinition[r].Name);
                  Need := False;
                  end;
                Add ('  sg' + Name + ' = ''' + lowercase(GtkName)+ ''';');
                UsedSignals.Add (Name);
                end;
          end;
      Add ('');
      // public helper functions en procedures
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          Need := True;
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (Section in sectPublic) then
                if (PropType = ptHelperFunc) then
                  begin
                  if Need then
                    begin
                    add ('// T'+ObjectsPrefix + FDefinition[r].Name);
                    Need := False;
                    end;
                  Add ('function ' + Name + CalcParameterList(Parameters, plDecl)
                           + ' : ' + PascalType+';' + CalcProcTypes(ProcTypes));
                  end
                else if (PropType = ptHelperProc) then
                  begin
                  if Need then
                    begin
                    add ('// T'+ObjectsPrefix + FDefinition[r].Name);
                    Need := False;
                    end;
                  Add ('procedure ' + Name + CalcParameterList(Parameters, plDecl)
                         + ';' + CalcProcTypes(ProcTypes));
                  end;
          end;
      // Start implementation
      add (CRLF+'IMPLEMENTATION'+CRLF);
      // Object implementations
      for r := 0 to pred(FDefinition.count) do
        WriteObjectImplementation (FDefinition[r]);
      // Initializations
      Add ('INITIALIZATION');
      DoStepIt;
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (PropType = ptInitialization) then
                AddStrings (Code);
          end;
      // Finalizations
      Add (CRLF+'FINALIZATION');
      DoStepIt;
      for r := 0 to pred(FDefinition.count) do
        with FDefinition[r] do
          begin
          for t := 0 to Props.count-1 do
            with Props[t] do
              if (PropType = ptFinalization) then
                AddStrings (Code);
          end;
      add (CRLF+'End.');
    finally
      LPublic.Free;
      LPublish.Free;
      LPriv.Free;
      LProt.Free;
      UsedSignals.Free;
    end;
end;

end.