{***************************************************************************}
{ 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 IWDBScrollPanel;

{PUBDIST}

interface

{$I TMSDEFS.INC}

uses
  {$IFDEF LINUX}
  QGraphics, QDBCtrls,
  {$ELSE}
  Windows, Graphics, DBCtrls,
  {$ENDIF}
  Classes,  SysUtils, DB, IWControl, IWFont,
  IWTypes, IWHTMLTag, IWScrollPanel, IWTMSBase;

type
  TTIWDBScrollPanel= class(TTIWScrollPanel)
  private
    FDataLink: TFieldDataLink;
    function GetDataField: string;
    function GetDataSource: TDataSource;
    procedure SetDataField(const Value: string);
    procedure SetDataSource(Value: TDataSource);
    procedure DataChange(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property DataField: string read GetDataField write SetDataField;
    property DataSource: TDataSource read GetDataSource write SetDataSource;
  end;

implementation

{ TTIWDBScrollPanel }

function TTIWDBScrollPanel.GetDataField: string;
begin
  Result := FDataLink.FieldName;
end;

procedure TTIWDBScrollPanel.SetDataField(const Value: string);
begin
  FDataLink.FieldName := Value;
end;

function TTIWDBScrollPanel.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;


procedure TTIWDBScrollPanel.SetDataSource(Value: TDataSource);
begin
  if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
    FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);

end;


constructor TTIWDBScrollPanel.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink := TFieldDataLink.Create;
  FDataLink.Control := Self;
  FDataLink.OnDataChange := DataChange;
end;

destructor TTIWDBScrollPanel.Destroy;
begin
  FDataLink.Free;
  FDataLink := nil;
  inherited Destroy;
end;

procedure TTIWDBScrollPanel.DataChange(Sender: TObject);
begin
  if FDataLink.Field <> nil
    then
    begin
    if FDataLink.Field.IsBlob
       then
        begin
          try
            Text.clear;
            Text.Add(FDataLink.Field.AsString);
            // Text.Add('BLOB');// only for debug
          except
            on E:EInvalidOperation do
               begin
               Text.clear;
               Text.Add( Format('(%s)', [E.Message]));
               end;
          end;
        end
        else begin
              Text.clear;
              text.add(FDataLink.Field.Text);
              // text.add('STRING');// only for debug
              end;
     end
        else
          begin
            if csDesigning in ComponentState then
                begin
                 Text.clear;
                 text.add(Name);
                end
                 else Text.clear;
          end;
end;

end.
