{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2007                                        }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}

unit IWDBHTMLEdit;

interface

{$I TMSDEFS.INC}

uses
  {$IFNDEF LINUX}
  Windows,
  {$ENDIF}
  IWHTMLEdit, IWHTMLTag, DB, IWDBStdCtrls, Classes, IWTMSBase
  {$IFDEF TMSIW51}
  , IWDBCommon
  {$ENDIF}
  {$IFDEF TMSIW6}
  , IWRenderContext
  {$ENDIF}
  ;

type

  TTIWDBHTMLEdit = class(TTIWCustomHTMLEdit)
  private
    FDataField: string;
    FDataSource: TDataSource;
  protected
    procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
    procedure SetValue(const AValue: string); override;
  public
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
  published
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write FDataSource;
    property BackgroundColor;
    property Font;
    property FontList;
    property Glyphs;
  end;

implementation


{ TTIWDBHTMLEdit }

function GetFieldText(AField: TField): String;
begin
  if AField.IsBlob then
  begin
    if Assigned(AField.OnGetText) then
    begin
      Result := AField.Text;
    end
    else
    begin
      Result := AField.AsString;
    end;
  end
  else
  begin
    Result := AField.Text;
  end;
end;

{$IFDEF TMSIW6}
function TTIWDBHTMLEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWDBHTMLEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Lines.Text := GetFieldText(LField);
    ContentEditable := InEditMode(DataSource.Dataset);
  end
  else
  begin
    Lines.Clear;
  end;
  {$IFDEF TMSIW6}
  Result := inherited RenderHTML(AContext);
  {$ELSE}
  Result := inherited RenderHTML;
  {$ENDIF}
end;

procedure TTIWDBHTMLEdit.Notification(AComponent: TComponent; AOperation: TOperation);
begin
  inherited Notification(AComponent, AOperation);
  if AOperation = opRemove then begin
    if FDatasource = AComponent then begin
      FDatasource := nil;
    end;
  end;
end;

procedure TTIWDBHTMLEdit.SetValue(const AValue: string);
var
  LField: TField;
begin
  inherited SetValue(AValue);
  if CheckDataSource(DataSource, DataField, LField) and ContentEditable then
  begin
    // After SetValue Text holds the real value
    // When AValue does not end with CR-LF FLisnes automatically adds CRLF
    Datasource.Edit;
    LField.AsString := AValue;
  end;
end;



end.
