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
|
{
$Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
This file is part of the Free Component Library (FCL)
Copyright (c) 1999-2000 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit fpdatasetform;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fphtml, htmldefs, htmlwriter, db, htmlelements;
type
THTMLDatasetFormProducer = class;
TFormFieldItem = class;
TFormButtonItem = class;
TFieldCellEvent = procedure (Sender:THTMLDatasetFormProducer; FieldDef:TFormFieldItem;
IsLabel:boolean; Cell : THTMLCustomelement) of object;
TButtonEvent = procedure (Sender:THTMLDatasetFormProducer; ButtonDef:TFormButtonItem;
Button : THTMLAttrsElement) of object;
TProducerEvent = procedure (Sender:THTMLDatasetFormProducer; FieldDef:TFormFieldItem;
Producer:THTMLContentProducer) of object;
TProducerSetRecordEvent = procedure (Sender:THTMLDatasetFormProducer) of object;
THTMLElementEvent = procedure (Sender:THTMLDatasetFormProducer; element : THTMLCustomElement) of object;
TFieldCheckEvent = procedure (aField:TField; var check:boolean) of object;
TFieldItemEvent = procedure (Sender:TFormFieldItem; var aValue : string) of object;
TFormInputType = (fittext,fitpassword,fitcheckbox,fitradio,fitfile,fithidden,
fitproducer,fittextarea,fitrecordselection,fitlabel);
{ TTablePosition }
TTablePosition = class (TPersistent)
private
FAlignHor: THTMLalign;
FAlignVer: THTMLvalign;
FColSpan: integer;
FLeft: integer;
FRowSpan: integer;
FTop: integer;
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor create;
published
property Left : integer read FLeft write FLeft;
property Top : integer read FTop write FTop;
property ColSpan : integer read FColSpan write FColSpan default 1;
property RowSpan : integer read FRowSpan write FRowSpan default 1;
property AlignVertical : THTMLvalign read FAlignVer write FAlignVer default vaEmpty;
property AlignHorizontal : THTMLalign read FAlignHor write FAlignHor default alEmpty;
end;
{ TFormFieldItem }
TFormFieldItem = class (TCollectionItem)
private
FAction: string;
FField: TField;
FFieldName: string;
FInputType: TFormInputType;
FLabelCaption: string;
FLabelAbove : boolean;
FLabelPos: TTablePosition;
FProducer: THTMLContentProducer;
FValuePos: TTablePosition;
FName : String;
FOnGetValue: TFieldItemEvent;
FOnGetLabel: TFieldItemEvent;
FOnGetAction: TFieldItemEvent;
procedure SetLabelPos(const AValue: TTablePosition);
procedure SetValuePos(const AValue: TTablePosition);
protected
procedure AssignTo(Dest: TPersistent); override;
Function GetDisplayName : String; override;
Procedure SetDisplayName(const Value : String); override;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
function getValue : String; virtual;
function getLabel : String; virtual;
function getAction : String; virtual;
property Field : TField read FField;
published
Property Name : String Read GetDisplayName Write SetDisplayName;
property Fieldname : string read FFieldName write FFieldname;
// the field to show/edit
property LabelCaption : string read FLabelCaption write FLabelCaption;
// the text to show for the control
property LabelPos : TTablePosition read FLabelPos write SetLabelPos;
// place of the label in the table-grid
property LabelAbove : boolean read FLabelAbove write FLabelAbove default false;
// if not SeparateLabel then place a <BR> between label and edit/value
property ValuePos : TTablePosition read FValuePos write SetValuePos;
// place of the value in the table-grid
{ only when editting: }
property InputType : TFormInputType read FInputType write FInputType default fittext;
// the type of form control to use
property Producer : THTMLContentProducer read FProducer write FProducer;
// the producer to include when generating the value
{ only when showing: }
property Action : string read FAction write FAction;
// the link to include in the value
property OnGetValue : TFieldItemEvent read FOnGetValue write FOnGetValue;
property OnGetLabel : TFieldItemEvent read FOnGetLabel write FOnGetLabel;
property OnGetAction : TFieldItemEvent read FOnGetAction write FOnGetAction;
end;
{ TFormFieldCollection }
TFormFieldCollection = class (TCollection)
private
function GetItem(index : integer): TFormFieldItem;
procedure SetItem(index : integer; const AValue: TFormFieldItem);
public
constructor create;
function AddField (afieldname, acaption : string) : TFormFieldItem;
Function FindItem(AName : String): TFormFieldItem;
Function IndexofItem(AName : String) : Integer;
property Items [index : integer] : TFormFieldItem read GetItem write SetItem;
end;
TFormButtonType = (fbtSubmit, fbtReset, fbtPushbutton, fbtInputSubmit,fbtInputReset,fbtInputPushButton);
TImagePlace = (ipOnly, ipBefore, ipAfter, ipUnder, ipAbove);
{ TFormButtonItem }
TFormButtonItem = class (TCollectionItem)
private
FButtonType: TFormButtonType;
FCaption: string;
FImage: string;
FName: string;
FImagePlace: TImagePlace;
FValue: string;
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor create (ACollection : TCollection); override;
published
property Name : string read FName write FName;
property Value : string read FValue write FValue;
property Caption : string read FCaption write FCaption;
// Text on button, or as hint with image
property Image : string read FImage write FImage;
// Image to show on the button
property ImagePlace : TImagePlace read FImagePlace write FImagePlace;
// where the image is placed regarding from the caption.
// if ipOnly; the caption is placed in the alternate text of the image (hint)
property ButtonType : TFormButtonType read FButtonType write FButtonType default fbtPushButton;
// Where the button is used for
end;
{ TFormButtonCollection }
TFormButtonCollection = class (TCollection)
private
function GetItem(index : integer): TFormButtonItem;
procedure SetItem(index : integer; const AValue: TFormButtonItem);
public
constructor create;
function AddButton (aname, avalue, acaption : string) : TFormButtonItem;
function AddButton (aname, acaption : string) : TFormButtonItem;
function AddButton (acaption : string) : TFormButtonItem;
property Items [index : integer] : TFormButtonItem read GetItem write SetItem;
end;
TCellType = (ctEmpty, ctInput, ctLabel, ctProducer, ctSpanned);
{ TTableCell }
TTableCell = class (TCollectionItem)
private
FAlignHor: THTMLalign;
FAlignVer: THTMLvalign;
FCaption: string;
FCellType: TCellType;
FChecked: boolean;
FDisabled: boolean;
FColSpan: integer;
FEndRow: boolean;
FFormField: TFormFieldItem;
FIncludeBreak: boolean;
FInputType: TFormInputType;
FIsLabel: boolean;
FLink: string;
FMaxLength: integer;
FName: string;
FProducer: THTMLContentProducer;
FRowSpan: integer;
FSize: integer;
FSpanned: boolean;
FValue: string;
procedure WriteLabel(aWriter: THTMLWriter);
public
function WriteContent (aWriter : THTMLWriter) : THTMLCustomElement;
function WriteHeader (aWriter : THTMLWriter) : THTMLCustomElement;
property FormField : TFormFieldItem read FFormField write FFormField;
// field definition that origintated this cell
property IsLabel : boolean read FIsLabel write FIsLabel;
// Label or Value ?
property Caption : string read FCaption write FCaption;
// label to place with the edit/value if not separateLabel
property IncludeBreak : boolean read FIncludeBreak write FIncludeBreak;
// place <br> between label and edit/value if label is included in cell
property CellType : TCellType read FCellType write FCellType;
{ Cell properties: }
property ColSpan : integer read FColSpan write FColSpan;
property RowSpan : integer read FRowSpan write FRowSpan;
property AlignVertical : THTMLvalign read FAlignVer write FAlignVer default vaEmpty;
property AlignHorizontal : THTMLalign read FAlignHor write FAlignHor default alEmpty;
property Value : string read FValue write FValue;
// Contains the text for labels, or the value for input, or unused for producer and empty
{ properties to correctly generate the rows and the table ends }
property EndOfRow : boolean read FEndRow write FEndRow;
property SpannedOut : boolean read FSpanned write FSpanned;
{ only for input: }
property Name : string read FName write FName;
// name of the control
property InputType : TFormInputType read FInputType write FInputType;
// type of the input element
property Size : integer read FSize write FSize;
// size of text input element
property MaxLength : integer read FMaxLength write FMaxLength;
// MaxLength of text input element
property Checked : boolean read FChecked write FChecked;
// checked or not for radio,checkbox
property Disabled : boolean read FDisabled write FDisabled;
// disabled or not for radio,checkbox
{ only for labels: }
property Link : string read FLink write FLink;
// link to place around the text
{ only for producers: }
property Producer : THTMLContentProducer read FProducer write FProducer;
// producer to include
end;
{ TTableDef }
TTableDef = class (TCollection)
private
fCols, fRows : integer;
function GetCell(x, y : integer): TTableCell;
function GetItem(index: integer): TTableCell;
public
Constructor Create (acols, arows : integer);
function CopyTablePosition (position : TTablePosition) : TTableCell;
property Cells [x,y : integer] : TTableCell read GetCell; default;
property items [index:integer] : TTableCell read GetItem;
end;
TButtonVerPosition = (bvpTop, bvpBottom);
TButtonVerPositionSet = set of TButtonVerPosition;
TButtonHorPosition = (bhpLeft, bhpCenter, bhpJustify, bhpRight);
TFormMethod = (fmNone, fmGet, fmPost);
{ THTMLDatasetFormProducer }
THTMLDatasetFormProducer = class (THTMLContentProducer)
private
FAfterSetRecord: TProducerSetRecordEvent;
FOnInitializeProducer : TProducerEvent;
FOnFieldChecked : TFieldCheckEvent;
FAfterTBodyCreate,
FAfterTableCreate : THTMLElementEvent;
FAfterButtonCreate: TButtonEvent;
FAfterCellCreate: TFieldCellEvent;
Fbuttonrow: TFormButtonCollection;
FButtonsHor: TButtonHorPosition;
FButtonsVer: TButtonVerPositionSet;
FControls: TFormFieldCollection;
FDatasource: TDatasource;
FFormAction: string;
FFormMethod: TFormMethod;
FIncludeHeader: boolean;
FSeparateLabel: boolean;
FTableCols: integer;
FTableRows: integer;
FTableDef : TTableDef;
FPage: integer;
FRecordsPerPage: integer;
procedure SetIncludeHeader(const AValue: boolean);
procedure SetSeparateLabel(const AValue: boolean);
procedure WriteButtons (aWriter : THTMLWriter);
procedure WriteTableDef (aWriter : THTMLWriter);
procedure WriteHeaderTableDef (aWriter : THTMLWriter);
procedure CorrectCellSpans;
procedure SearchControlFields;
protected
procedure FillTableDef (IsHeader:boolean); virtual;
procedure PlaceFieldValue(aControldef : TFormFieldItem); virtual;
procedure ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean); virtual; abstract;
function StartForm (aWriter : THTMLWriter) : THTMLCustomElement; virtual;
procedure EndForm (aWriter : THTMLWriter); virtual;
property TableDef : TTableDef read FTableDef;
function SingleRecord : boolean; dynamic;
// generate form for 1 record or for the selected pages
property RecordsPerPage : integer read FRecordsPerPage write FRecordsPerPage default 20;
// number of records to show
property Page : integer read FPage write FPage default -1;
// page to show. -1 shows all records. zero based
property IncludeHeader : boolean read FIncludeHeader write SetIncludeHeader;
// create a header cell for each control
public
constructor create (aOwner : TComponent); override;
destructor destroy; override;
function WriteContent (aWriter : THTMLWriter) : THTMLCustomElement; override;
published
property FormAction : string read FFormAction write FFormAction;
// action of the form (link), if not given; don't use a form element
property FormMethod : TFormMethod read FFormMethod write FFormMethod;
// method of the form, Get or Post
Property DataSource : TDataSource read FDataSource write FDataSource;
// the data to use
property Controls : TFormFieldCollection read FControls write FControls;
// configuration of the fields and how to generate the html
property SeparateLabel : boolean read FSeparateLabel write SetSeparateLabel;
// place label and value/edit in same table cell
property buttonrow : TFormButtonCollection read Fbuttonrow write Fbuttonrow;
// buttons to place in the form
property TableCols : integer read FTableCols write FTableCols default 2;
// number columns in the grid for 1 record
property TableRows : integer read FTableRows write FTableRows;
// number of rows in the grid for 1 record
property ButtonsHorizontal : TButtonHorPosition read FButtonsHor write FButtonsHor default bhpleft;
// where to place the buttons horizontally
property ButtonsVertical : TButtonVerPositionSet read FButtonsVer write FButtonsVer default [bvpTop,bvpBottom];
// where to place the buttons vertically
property OnInitializeProducer : TProducerEvent read FOnInitializeProducer write FOnInitializeProducer;
// Called before the producer creates it's HTML code
property AfterCellCreate : TFieldCellEvent read FAfterCellCreate write FAfterCellCreate;
// Called after each creation of a cell in the table makeup in the form
property AfterButtonCreate : TButtonEvent read FAfterButtonCreate write FAfterButtonCreate;
// Called after each creation of a button
property AfterTableCreate : THTMLElementEvent read FAfterTableCreate write FAfterTableCreate;
// Called after the creation of the table
property AfterTBodyCreate : THTMLElementEvent read FAfterTBodyCreate write FAfterTBodyCreate;
// Called after finishing the tbody of each record
property AfterSetRecord : TProducerSetRecordEvent read FAfterSetRecord write FAfterSetRecord;
// Called after the dataset is scrolled to the next record
property OnFieldChecked : TFieldCheckEvent read FOnFieldChecked write FOnFieldChecked;
// return if the field is true or false if the false string differs from '0','false','-'
end;
{ THTMLDatasetFormEditProducer }
THTMLDatasetFormEditProducer = class (THTMLDatasetFormProducer)
procedure ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean); override;
end;
{ THTMLDatasetFormShowProducer }
THTMLDatasetFormShowProducer = class (THTMLDatasetFormProducer)
protected
procedure ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean); override;
end;
{ THTMLDatasetFormGridProducer }
THTMLDatasetFormGridProducer = class (THTMLDatasetFormProducer)
protected
procedure ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean); override;
function SingleRecord : boolean; override;
public
constructor Create (aOwner : TComponent); override;
published
property RecordsPerPage;
property Page;
property IncludeHeader;
property AfterSetRecord;
end;
implementation
{ TTableDef }
function TTableDef.GetItem(index: integer): TTableCell;
begin
result := TTableCell (inherited items[index]);
end;
function TTableDef.GetCell(x, y : integer): TTableCell;
var r : integer;
begin
r := x + (y * fcols);
result := getItem (r);
end;
constructor TTableDef.Create(acols, arows: integer);
var r, t : integer;
begin
inherited create (TTableCell);
fRows := aRows;
fCols := aCols;
for r := 1 to aRows do
begin
for t := 1 to aCols-1 do
Add;
TTableCell(Add).EndOfRow := True;
end;
end;
function TTableDef.CopyTablePosition(position: TTablePosition): TTableCell;
begin
result := Cells[position.left,position.top];
with result do
begin
AlignHorizontal := position.AlignHorizontal;
AlignVertical := position.FAlignVer;
ColSpan := position.ColSpan;
RowSpan := position.RowSpan;
end;
end;
{ TTablePosition }
procedure TTablePosition.AssignTo(Dest: TPersistent);
begin
inherited AssignTo(Dest);
if dest is TTablePosition then
with TTablePosition(Dest) do
begin
FTop := self.FTop;
FLeft := self.FLeft;
FColSpan := self.FColSpan;
FRowSpan := self.FRowSpan;
FAlignVer := self.FAlignVer;
FalignHor := self.FAlignHor;
end;
end;
constructor TTablePosition.create;
begin
inherited create;
FColSpan := 1;
FRowSpan := 1;
FAlignVer := vaEmpty;
FAlignHor := alEmpty;
end;
{ TFormFieldItem }
procedure TFormFieldItem.SetLabelPos(const AValue: TTablePosition);
begin
FLabelPos.assign(AValue);
end;
procedure TFormFieldItem.SetValuePos(const AValue: TTablePosition);
begin
FValuePos.assign(AValue);
end;
procedure TFormFieldItem.AssignTo(Dest: TPersistent);
begin
inherited AssignTo(Dest);
if dest is TFormFieldItem then
with TFormFIeldItem(Dest) do
begin
FAction := self.FAction;
FFieldName := self.FFieldName;
FInputType := self.FInputType;
FLabelCaption := self.FLabelCaption;
FLabelPos.assign (self.FLabelPos);
FProducer := self.FProducer;
FValuePos.assign(self.FValuePos);
end;
end;
function TFormFieldItem.GetDisplayName: String;
begin
If (FName='') then
FName:=ClassName+IntToStr(self.Index);
Result:=FName;
end;
procedure TFormFieldItem.SetDisplayName(const Value: String);
begin
Inherited;
FName:=Value;
end;
constructor TFormFieldItem.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FLabelPos := TTablePosition.Create;
FValuePos := TTablePosition.Create;
end;
destructor TFormFieldItem.Destroy;
begin
FLabelPos.Free;
FValuePos.Free;
inherited Destroy;
end;
function TFormFieldItem.getValue: String;
begin
if inputType in [fitcheckbox,fitradio] then
Result := 'T'
else
Result := FField.DisplayText;
if assigned (FOnGetValue) then
onGetValue(self,Result);
end;
function TFormFieldItem.getLabel: String;
begin
Result := LabelCaption;
if assigned(FOnGetLabel) then
onGetLabel(Self,Result);
end;
function TFormFieldItem.getAction: String;
begin
Result := Format(Action,[FField.asstring]);
if assigned(FOnGetAction) then
onGetAction(Self,Result);
end;
{ TFormFieldCollection }
function TFormFieldCollection.GetItem(index : integer): TFormFieldItem;
begin
result := TFormFieldItem(inherited items[index]);
end;
procedure TFormFieldCollection.SetItem(index : integer;
const AValue: TFormFieldItem);
begin
inherited items[index] := AValue;
end;
constructor TFormFieldCollection.create;
begin
inherited create (TFormFieldItem);
end;
function TFormFieldCollection.AddField(afieldname, acaption: string): TFormFieldItem;
begin
result := TFormFieldItem (Add);
result.fieldname := afieldname;
result.labelcaption := acaption;
end;
function TFormFieldCollection.FindItem(AName: String): TFormFieldItem;
Var
I : Integer;
begin
I:=IndexofItem(AName);
If (I=-1) then
Result:=Nil
else
Result:=Items[I];
end;
function TFormFieldCollection.IndexofItem(AName: String): Integer;
begin
Result:=Count-1;
While (Result>=0) and (CompareText(Items[Result].Name,AName)<>0) do
Dec(Result);
end;
{ TFormButtonItem }
procedure TFormButtonItem.AssignTo(Dest: TPersistent);
begin
inherited AssignTo(Dest);
if dest is TFormButtonItem then
with TFormButtonItem(Dest) do
begin
FButtonType := self.FButtonType;
FCaption := self.FCaption;
FImage := self.FImage;
FImagePlace := self.FImagePlace;
FName := self.FName;
FValue := self.FValue;
end;
end;
constructor TFormButtonItem.create(ACollection: TCollection);
begin
inherited create(ACollection);
ButtonType := fbtPushButton;
end;
{ TFormButtonCollection }
function TFormButtonCollection.GetItem(index: integer): TFormButtonItem;
begin
result := TFormButtonItem(inherited items[index]);
end;
procedure TFormButtonCollection.SetItem(index: integer;
const AValue: TFormButtonItem);
begin
inherited items[index] := AValue;
end;
constructor TFormButtonCollection.create;
begin
inherited create (TFormButtonItem);
end;
function TFormButtonCollection.AddButton(aname, avalue, acaption: string): TFormButtonItem;
begin
result := TFormButtonItem(Add);
with result do
begin
name := aname;
value := avalue;
caption := acaption;
end;
end;
function TFormButtonCollection.AddButton(aname, acaption: string): TFormButtonItem;
begin
result := AddButton (aName, aCaption, acaption);
end;
function TFormButtonCollection.AddButton(acaption: string): TFormButtonItem;
begin
result := AddButton (acaption, acaption, acaption);
end;
{ THTMLDatasetFormProducer }
procedure THTMLDatasetFormProducer.WriteButtons(aWriter: THTMLWriter);
procedure WriteButton (aButton : TFormButtonItem);
const ButtonTypes : array[TFormButtontype] of THTMLbuttontype = (btsubmit,btreset,btbutton,btreset,btreset,btreset);
const InputTypes : array[TFormButtontype] of THTMLinputtype = (itreset,itreset,itreset,itsubmit,itreset,itbutton);
var b : THTML_Button;
ib: THTML_input;
begin
with aWriter do
if aButton.ButtonType in [fbtInputPushButton,fbtInputReset,fbtInputSubmit] then
begin
ib := input;
with ib do
begin
Name := aButton.name;
Value := aButton.value;
TheType := InputTypes[aButton.ButtonType];
if assigned (FAfterButtonCreate) then
FAfterButtonCreate (self, aButton, ib);
end;
end
else
begin
b := Startbutton;
with b do
begin
Name := aButton.name;
Value := aButton.value;
TheType := ButtonTypes[aButton.ButtonType];
if aButton.Image = '' then
Text (aButton.Caption)
else
begin
if aButton.ImagePlace in [ipAfter, ipUnder] then
begin
Text (aButton.Caption);
if aButton.ImagePlace = ipUnder then
linebreak;
end;
with image do
begin
src := aButton.image;
if aButton.ImagePlace = ipOnly then
alt := aButton.Caption;
end;
if aButton.ImagePlace in [ipBefore, ipAbove] then
begin
if aButton.ImagePlace = ipAbove then
linebreak;
Text (aButton.Caption);
end;
end;
if assigned (FAfterButtonCreate) then
FAfterButtonCreate (self, aButton, b);
Endbutton;
end;
end;
end;
const ButHorAlign : array[TButtonHorPosition] of THTMLalign = (alleft,alcenter,aljustify,alright);
var r : integer;
begin
with aWriter do
begin
StartTableRow;
with StartTableCell do
begin
ColSpan := inttostr(FTableCols);
align := ButHorAlign[ButtonsHorizontal];
end;
for r := 0 to buttonrow.count-1 do
WriteButton (buttonrow.Items[r]);
EndTableCell;
EndTableRow;
end;
end;
procedure THTMLDatasetFormProducer.SetSeparateLabel(const AValue: boolean);
begin
if AValue <> FSeparateLabel then
begin
FSeparateLabel := AValue;
if AValue then
FIncludeHeader := false;
end;
end;
procedure THTMLDatasetFormProducer.SetIncludeHeader(const AValue: boolean);
begin
if FIncludeHeader <> AValue then
begin
FIncludeHeader := AValue;
if AValue then
SeparateLabel := false;
end;
end;
procedure THTMLDatasetFormProducer.WriteTableDef(aWriter: THTMLWriter);
var r : integer;
c : THTMLCustomelement;
begin
c := aWriter.Starttablebody;
if assigned (FAfterTBodyCreate) then
FAfterTBodyCreate (self, c);
aWriter.StartTableRow;
with tabledef do
begin
for r := 0 to count-1 do
with TTableCell (Items[r]) do
begin
if CellType <> ctSpanned then
begin
if (CellType = ctProducer) and assigned (FOnInitializeProducer) then
FOnInitializeProducer (self, FFormField, Producer);
c := WriteContent(aWriter);
if assigned (FAfterCellCreate) then
FAfterCellCreate(self, Items[r].FormField, IsLabel, c);
end;
if EndOfRow then
begin
aWriter.EndTableRow;
aWriter.StartTableRow;
end;
end;
end;
aWriter.EndTableRow;
aWriter.Endtablebody;
end;
procedure THTMLDatasetFormProducer.WriteHeaderTableDef(aWriter: THTMLWriter);
var r : integer;
c : THTMLCustomelement;
begin
aWriter.Starttablehead;
aWriter.StartTableRow;
with tabledef do
begin
for r := 0 to count-1 do
with TTableCell (Items[r]) do
begin
c := WriteHeader(aWriter);
if assigned (FAfterCellCreate) then
FAfterCellCreate(self, Items[r].FormField, true, c);
if EndOfRow then
begin
aWriter.EndTableRow;
aWriter.StartTableRow;
end;
end;
end;
aWriter.EndTableRow;
aWriter.Endtablehead;
end;
procedure THTMLDatasetFormProducer.CorrectCellSpans;
var r, s, t : integer;
c : TTableCell;
begin
for r := 0 to TableDef.count-1 do
with TableDef.items[r] do
if CellType <> ctSpanned then
begin
// CollSpan marking other cells as spanned
s := 1;
c := TableDef.Items[r];
while (s < ColSpan) and not c.EndOfRow do
begin
c := TableDef.Items[r+s];
c.celltype := ctSpanned;
inc (s);
end;
// the same for rowsapn
s := 1;
t := r + (s*tablecols);
while (s < rowspan) and (t < TableDef.count) do
begin
TableDef.items[t].CellType := ctSpanned;
inc (s);
inc (t, tablecols);
end;
end;
end;
procedure THTMLDatasetFormProducer.SearchControlFields;
var r : integer;
begin
for r := 0 to FControls.count-1 do
with FControls.items[r] do
FField := datasource.dataset.FindField(FFieldname);
end;
function THTMLDatasetFormProducer.StartForm(aWriter: THTMLWriter) : THTMLCustomElement;
const MethodAttribute : array[TFormMethod] of string = ('','GET','POST');
var t : THTMLCustomElement;
begin
if Self.FormMethod <> fmNone then
begin
result := aWriter.Startform;
with THTML_Form(result) do
begin
method := MethodAttribute[self.FormMethod];
action := FormAction;
end;
t := aWriter.Starttable;
end
else
begin
t := aWriter.Starttable;
result := t;
end;
if assigned (FAfterTableCreate) then
FAfterTableCreate (self, t);
end;
procedure THTMLDatasetFormProducer.EndForm(aWriter: THTMLWriter);
begin
with aWriter do
begin
EndTable;
if self.FormMethod <> fmNone then
Endform;
end;
end;
function THTMLDatasetFormProducer.WriteContent(aWriter: THTMLWriter): THTMLCustomElement;
var r : integer;
begin
if assigned (datasource) and assigned(datasource.dataset) then
begin
Ftabledef := TTableDef.Create (TableCols, TableRows);
try
SearchControlFields;
result := StartForm (aWriter);
if bvpTop in ButtonsVertical then
WriteButtons (aWriter);
if SingleRecord then
begin
FillTableDef (false);
CorrectCellSpans;
WriteTableDef (aWriter);
end
else
with datasource.dataset do
begin
if FIncludeHeader then
begin
FillTableDef (true);
CorrectCellSpans;
WriteHeaderTableDef (aWriter);
end;
if Page < 0 then
first
else
begin
try // Catch exception if the record doesn't exist.
RecNo := ((Page-1) * RecordsPerPage) + 1; // zero based? yes: + 1 has to be deleted
except
Last;
end;
end;
r := 0;
while not eof and (r < RecordsPerPage) do
begin
if assigned (FAfterSetRecord) then
FAfterSetRecord (self);
FillTableDef (false);
CorrectCellSpans;
WriteTableDef (aWriter);
Next;
inc (r);
end;
end;
if bvpBottom in ButtonsVertical then
WriteButtons (aWriter);
EndForm (aWriter)
finally
tabledef.Free;
end;
end;
end;
procedure THTMLDatasetFormProducer.FillTableDef (IsHeader:boolean);
var r : integer;
begin
for r := 0 to Controls.Count-1 do
ControlToTableDef (Controls.items[r], IsHeader);
end;
procedure THTMLDatasetFormProducer.PlaceFieldValue(aControldef : TFormFieldItem);
var check : boolean;
begin
with TableDef.CopyTablePosition(aControlDef.ValuePos) do
begin
FormField := aControldef;
case aControlDef.inputtype of
fitlabel,
fittext,
fitpassword,
fitcheckbox,
fitradio,
fitfile,
fithidden,
fittextarea :
begin
CellType := ctInput;
InputType := aControlDef.InputType;
Name := aControlDef.Field.FieldName;
Size := aControlDef.Field.DisplayWidth;
MaxLength := aControldef.Field.Size;
if aControlDef.inputType in [fitcheckbox,fitradio] then
begin
with aControlDef.Field do
Check := asBoolean;
if assigned (FOnFieldChecked) then
FOnFieldChecked (aControlDef.Field, check);
Checked := check;
end;
end;
fitproducer :
begin
CellType := ctProducer;
Producer := aControlDef.Producer;
// if Producer is THTMLSelectProducer then THTMLSelectProducer(Producer).PreSelected:=aControldef.getValue;
end;
fitrecordselection : ;
end;
IsLabel := false;
Value := aControlDef.getValue;
Link := aControldef.getAction;
if not FSeparateLabel and not FIncludeHeader then
begin
Caption := aControldef.getLabel;
IncludeBreak := aControldef.LabelAbove;
end;
end;
end;
function THTMLDatasetFormProducer.SingleRecord: boolean;
begin
result := true;
end;
constructor THTMLDatasetFormProducer.create(aOwner: TComponent);
begin
inherited create(aOwner);
FTableCols := 2;
FButtonsHor := bhpLeft;
FButtonsVer := [bvpTop, bvpBottom];
Fbuttonrow := TFormButtonCollection.create;
FControls := TFormFieldCollection.Create;
end;
destructor THTMLDatasetFormProducer.destroy;
begin
Fbuttonrow.Free;
FControls.Free;
inherited destroy;
end;
{ THTMLDatasetFormEditProducer }
procedure THTMLDatasetFormEditProducer.ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean);
procedure PlaceLabel;
begin
with TableDef.CopyTablePosition(aControlDef.LabelPos) do
begin
FormField := aControldef;
CellType := ctLabel;
IsLabel := true;
Value := aControldef.getLabel;
end;
end;
begin
if assigned (aControlDef.FField) then
PlaceFieldValue(aControldef);
if FSeparateLabel and (aControlDef.getLabel <> '') then
PlaceLabel;
end;
{ THTMLDatasetFormShowProducer }
(**** TTableCell *****
property IsLabel : boolean read FIsLabel write FIsLabel;
// Label or Value ?
property CellType : TCellType read FCellType write FCellType;
ctEmpty, ctInput, ctLabel, ctProducer, ctSpanned
{ Cell properties: }
property ColSpan : integer read FColSpan write FColSpan;
property RowSpan : integer read FRowSpan write FRowSpan;
property AlignVertical : THTMLvalign read FAlignVer write FAlignVer default vaEmpty;
property AlignHorizontal : THTMLalign read FAlignHor write FAlignHor default alEmpty;
property Value : string read FValue write FValue;
// Contains the text for labels, or the value for input, or unused for producer and empty
{ only for input: }
property Name : string read FName write FName;
// name of the control
property InputType : TFormInputType read FInputType write FInputType;
// type of the input element
property Size : integer read FSize write FSize;
// size of text input element
property MaxLength : integer read FMaxLength write FMaxLength;
// MaxLength of text input element
property Checked : boolean read FChecked write FChecked;
// checked or not for radio,checkbox
{ only for labels: }
property Link : string read FLink write FLink;
// link to place around the text
{ only for producers: }
property Producer : THTMLContentProducer read FProducer write FProducer;
// producer to include
***** TFormFieldItem *****
property Fieldname : string read FFieldName write FFieldname;
property Field : TField
// the field to show/edit
property LabelCaption : string read FLabelCaption write FLabelCaption;
// the text to show for the control
property InputType : TFormInputType read FInputType write FInputType default fittext;
// the type of form control to use
(fittext,fitpassword,fitcheckbox,fitradio,fitfile,fithidden,fitproducer,fittextarea,fitrecordselection)
property Producer : THTMLContentProducer read FProducer write FProducer;
// the producer to include when generating the value
property Action : string read FAction write FAction;
// when showing the link to include in the value
property LabelPos : TTablePosition read FLabelPos write SetLabelPos;
// place of the label in the table-grid
property ValuePos : TTablePosition read FValuePos write SetValuePos;
// place of the value in the table-grid *)
procedure THTMLDatasetFormShowProducer.ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean);
procedure PlaceFieldValue;
begin
with TableDef.CopyTablePosition(aControlDef.ValuePos) do
begin
CellType := ctLabel;
IsLabel := false;
FormField := aControldef;
Value := aControlDef.getValue;
if not FSeparateLabel and not FIncludeHeader then
begin
Caption := aControldef.getLabel;
IncludeBreak := aControldef.LabelAbove;
end;
end;
end;
procedure PlaceLabel;
begin
with TableDef.CopyTablePosition(aControlDef.LabelPos) do
begin
CellType := ctLabel;
FormField := aControldef;
IsLabel := true;
Value := aControldef.getLabel;
end;
end;
begin
if assigned (aControlDef.FField) then
PlaceFieldValue;
if FSeparateLabel and (aControlDef.getLabel <> '') then
PlaceLabel;
end;
{ THTMLDatasetFormGridProducer }
procedure THTMLDatasetFormGridProducer.ControlToTableDef (aControldef : TFormFieldItem; IsHeader:boolean);
procedure PlaceLabel;
begin
with TableDef.CopyTablePosition(aControlDef.LabelPos) do
begin
CellType := ctLabel;
IsLabel := true;
Value := aControldef.getLabel;
end;
end;
begin
if assigned (aControlDef.FField) and not IsHeader then
PlaceFieldValue(aControldef);
if (IsHeader or FSeparateLabel) and (aControlDef.getLabel <> '') then
PlaceLabel;
end;
function THTMLDatasetFormGridProducer.SingleRecord: boolean;
begin
Result := false;
end;
constructor THTMLDatasetFormGridProducer.Create(aOwner: TComponent);
begin
inherited create(aOwner);
RecordsPerPage := 20;
Page := -1;
end;
{ TTableCell }
procedure TTableCell.WriteLabel(aWriter: THTMLWriter);
var HasLink : boolean;
begin
HasLink := (Link <> '');
if HasLink then
aWriter.Anchor(Value).href := Link
else
aWriter.Text (Value);
end;
function TTableCell.WriteContent(aWriter: THTMLWriter) : THTMLCustomElement;
procedure WriteTextArea;
begin
aWriter.textarea(value).name := Name;
end;
procedure WriteInput;
var s, m : string;
begin
if size > 0 then
s := inttostr(size)
else
s := '';
if MaxLength > 0 then
m := inttostr(MaxLength)
else
m := '';
case InputType of
fittext :
with aWriter.FormText (Name, Value) do
begin
Size := s;
MaxLength := m;
end;
fitpassword :
with aWriter.FormPasswd (Name) do
begin
if self.Value <> '' then
Value := self.value;
Size := s;
MaxLength := m;
end;
fitcheckbox, fitrecordselection :
aWriter.FormCheckbox (Name, Value, checked).disabled := Disabled;
fitradio :
aWriter.FormRadio(Name, Value, checked);
fitfile :
aWriter.FormFile(Name, Value);
fithidden :
aWriter.FormHidden (Name, Value);
fitlabel :
if link <> '' then
aWriter.Anchor(Value).href := Link
else
aWriter.Text (Value);
end;
end;
procedure WriteProducer;
begin
if assigned(Producer) then with Producer do
begin
ParentElement := aWriter.CurrentElement;
HTMLDocument := aWriter.Document;
WriteContent (aWriter);
end;
end;
var c : THTML_td;
begin
if CellType <> ctSpanned then
with aWriter do
begin
c := Starttablecell;
with c do
begin
if self.ColSpan > 1 then
colspan := IntToStr(self.Colspan);
if self.RowSpan > 1 then
Rowspan := IntToStr(self.Rowspan);
align := AlignHorizontal;
valign := AlignVertical;
end;
if Self.Caption <> '' then
begin
span(self.caption);
if IncludeBreak then
linebreak;
end;
case CellType of
ctEmpty : ;
ctInput :
if InputType = fittextarea then
WriteTextArea
else
WriteInput;
ctLabel : WriteLabel(aWriter);
ctProducer : WriteProducer;
end;
Endtablecell;
result := c;
end
else
result := nil;
end;
function TTableCell.WriteHeader(aWriter: THTMLWriter) : THTMLCustomElement;
var c : THTML_th;
begin
with aWriter do
begin
c := Starttableheadcell;
with c do
begin
if self.ColSpan > 1 then
ColSpan := IntToStr(self.Colspan);
if self.RowSpan > 1 then
RowSpan := IntToStr(self.Rowspan);
align := AlignHorizontal;
valign := AlignVertical;
end;
case CellType of
ctEmpty : ;
ctLabel : WriteLabel(aWriter);
// ctProducer : WriteProducer;
end;
Endtableheadcell;
result := c;
end;
end;
end.
|