{ 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. **********************************************************************} {$mode objfpc} {$H+} unit dbwhtml; Interface uses sysutils,classes,db,whtml; Type THTMLAlign = (haDefault,haLeft,haRight,haCenter); // Compatible with Delphi. THTMLVAlign = (haVDefault,haTop,haMiddle,haBottom,haBaseLine); // Compatible with Delphi. TGetCellContentsEvent = Procedure (Sender : TObject; Var CellData : String) of object; TCellAttributesEvent = Procedure (Sender : TObject; Var BGColor : String; Var Align : THTMLAlign; Var VAlign : THTMLValign; Var CustomAttr : String) of Object; TRowAttributesEvent = Procedure (Sender : TObject; Var BGColor : String; Var Align : THTMLAlign; Var VAlign : THTMLValign; Var CustomAttr : String) of Object; TRowAttributes = Class(TPersistent) Private FAlign : THTMLAlign; FVAlign : THTMLVAlign; FBGColor : String; FCustom : String; Public Procedure Assign(Source : TPersistent); Override; Property Align : THTMLAlign Read FAlign Write FAlign; Property BGColor : String Read FBGColor Write FBGColor; Property Custom : String Read FCustom Write FCustom; Property VAlign : THTMLVAlign Read FVAlign Write FVAlign; end; TTableColumn = Class(TCollectionItem) private FActionUrl: String; FAlign: THTMLAlign; FVAlign : THTMLVAlign; FBGColor: String; FCaptionURL: String; FFieldName : String; FCaption : String; FGetColumn: String; FGetCellContent : TGetCellContentsEvent; FImgUrl: String; Protected FField : TField; // Filled. Published Property FieldName : String Read FFieldName Write FFieldName; Property Caption : String Read FCaption Write FCaption; Property ImgUrl : String Read FImgUrl Write FImgUrl; Property ActionUrl : String Read FActionUrl Write FActionUrl; Property CaptionURL : String Read FCaptionURL Write FCaptionURL; Property BGColor : String Read FBGColor Write FBGColor; Property Align : THTMLAlign read FAlign Write Falign; Property VAlign : THTMLVAlign Read FValign Write FVAlign; Property OnGetCellContents : TGetCellContentsEvent Read FGetCellContent Write FGetCellContent; end; TTableColumns = Class(TCollection) Constructor Create; private function GetColumn(Index : Integer): TTableColumn; procedure SetColumn(Index : Integer; const AValue: TTableColumn); Public Function FindColumn(ColumnName : String) : TTableColumn; Function ColumnByName(ColumnName : String) : TTableColumn; Property Items[Index : Integer] : TTableColumn Read GetColumn Write SetColumn; end; THTMLProducer = Class(TComponent) Private FDataset : TDataset; FContents: TMemorySTream; Function GetContent : String; Protected Procedure CheckContents; Procedure WriteString(S : TStream; Const Value : String); Procedure WriteString(S : TStream; Const Fmt : String; Args : Array Of Const); Public Destructor Destroy; override; Procedure ClearContent; Procedure CreateContent; virtual; Abstract; Property Content : String Read GetContent; Published Property Dataset : TDataset Read FDataset Write FDataset; end; TTableProducer = Class(THTMLProducer) Private FGetRowAttrs: TRowAttributesEvent; FRowAttributes: TRowAttributes; FTableColumns : TTableColumns; FBorder : Boolean; FBGColor : String; FCurrentRow : Integer; FCurrentCol : Integer; FGetCellAttrs : TCellAttributesEvent; procedure SetRowAttributes(const AValue: TRowAttributes); Procedure SetTableColumns(Value : TTableColumns); Protected Procedure BindColumns; Procedure CreateTableColumns; Virtual; Procedure CreateTableHeader(Stream : TStream); Procedure CreateHeaderCell(C : TTableColumn; Stream : TStream); virtual; Procedure CreateTableRow(Stream : TStream);virtual; Procedure StartTable(Stream : TStream); virtual; Procedure EndTable(Stream : TStream); virtual; Procedure EmitFieldCell(C : TTableColumn; Stream : TStream); virtual; Public Constructor Create(AOwner : TComponent); override; Destructor Destroy; virtual; Function CreateAttr(Const ABGColor : String; A : THTMLAlign; VA : THTMLVAlign; CustomAttr : String) : String; Procedure Clear; Procedure CreateColumns(FieldList : TStrings); Procedure CreateColumns(FieldList : String); Procedure CreateTable(Stream : TStream); Procedure CreateTable; Procedure CreateContent; override; Property CurrentRow : Integer Read FCurrentRow; Property CurrentCol : Integer Read FCurrentCol; Published Property BGColor : String Read FBGColor Write FBGColor; Property Border : Boolean Read FBorder Write FBorder; Property RowAttributes : TRowAttributes Read FRowAttributes Write SetRowAttributes; Property TableColumns : TTableColumns Read FTableColumns Write SetTableColumns; Property OnGetCellAttributes : TCellAttributesEvent Read FGetCellAttrs write FGetCellAttrs; Property OnGetRowAttributes : TRowAttributesEvent Read FGetRowAttrs write FGetRowAttrs; end; TComboBoxProducer = Class(THTMLProducer) private FDatafield: String; FInputName: String; FValue: String; FValueField: String; function GetInputName: String; protected procedure CreateItem(Stream : TStream; VF,DF : TField; Selected : Boolean); virtual; Public Constructor Create(AOwner : TComponent); override; Destructor Destroy; virtual; Procedure CreateComboBox(Stream : TStream); Procedure CreateComboBox; Procedure CreateContent; override; Published Property ValueField : String Read FValueField Write FValueField; Property DataField : String Read FDatafield Write FDataField; Property Value : String Read FValue Write FValue; Property InputName : String Read GetInputName Write FInputName; end; TDBHtmlWriter = Class(THTMLWriter) Protected Function CreateTableProducer: TTableProducer; virtual; Public Procedure CreateTable(Dataset : TDataset); Procedure CreateTable(Dataset : TDataset; Producer : TTableProducer); end; EDBWriter = Class(Exception); Implementation uses dbconst; { TTableColumns } constructor TTableColumns.Create; begin inherited Create(TTableColumn); end; function TTableColumns.GetColumn(Index : Integer): TTableColumn; begin Result:=TTableColumn(Inherited Items[Index]); end; procedure TTableColumns.SetColumn(Index : Integer; const AValue: TTableColumn); begin Inherited Items[Index]:=AValue; end; function TTableColumns.FindColumn(ColumnName: String): TTableColumn; Var I : Integer; begin Result:=Nil; I:=Count-1; While (I>=0) and (CompareText(Items[i].FieldName,ColumnName)<>0) do Dec(I); If (I>=0) then Result:=Items[I]; end; function TTableColumns.ColumnByName(ColumnName: String): TTableColumn; begin Result:=FindColumn(ColumnName); If (Result=Nil) then Raise EDBWriter.CreateFmt(SErrColumnNotFound,[ColumnName]); end; { TTableProducer } procedure TTableProducer.BindColumns; Var I : Integer; begin With FTableColumns do For I:=0 to Count-1 do With TTableColumn(Items[I]) do If (FieldName<>'') then FField:=FDataset.FieldByName(FieldName) else FField:=Nil; end; procedure TTableProducer.CreateTableColumns; begin FTableColumns:=TTableColumns.Create; end; procedure TTableProducer.CreateTableHeader(Stream : TStream); Var I : Integer; begin WriteString(Stream,'