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

interface

{$I TMSDEFS.INC}

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

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


implementation


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

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

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

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

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

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

procedure TTIWDBSmartPanel.DataChange(Sender: TObject);
begin
  if FDataLink.Field <> nil then
  begin
    if FDataLink.Field.IsBlob then
    begin
       try
         Lines.Clear;
         Lines.Text := (FDataLink.Field.AsString);
       except
         on E:EInvalidOperation do
         begin
           Lines.Clear;
           Lines.Add( Format('(%s)', [E.Message]));
         end;
       end;
     end
     else
     begin
       Lines.Clear;
       Lines.Text := FDataLink.Field.Text;
     end;
   end
   else
   begin
     if csDesigning in ComponentState then
     begin
       Lines.Clear;
       Lines.Add(Name);
     end
     else
       Lines.Clear;
  end;

  if FCaptionLink.Field <> nil then
  begin
    if FCaptionLink.Field.IsBlob then
    begin
       try
         Caption := (FCaptionLink.Field.AsString);
       except
         on E:EInvalidOperation do
           Caption := Format('(%s)', [E.Message]);
       end;
     end
     else
       Caption := FCaptionLink.Field.Text;
   end
   else
   begin
     if csDesigning in ComponentState then
     begin
       Caption := Name;
     end
     else
       Caption := '';
  end;

end;

function TTIWDBSmartPanel.GetCaptionField: string;
begin
  Result := FCaptionLink.FieldName;
end;

procedure TTIWDBSmartPanel.SetCaptionField(const Value: string);
begin
  FCaptionLink.FieldName := Value;
end;

end.
