{***************************************************************************}
{ 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.                                      }
{***************************************************************************}

{$I TMSDEFS.INC}

unit IWDBTMSEdit;

interface

uses
  Classes, IWTMSEdit, DB, IWHTMLTag, IWTMSBase, SysUtils
  {$IFDEF TMSIW6}
  ,IWRenderContext
  {$ENDIF}
  ;


type
  TTIWDBAdvSpinEdit = class(TTIWAdvSpinEdit)
  private
    FAutoEditable: Boolean;
    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 AutoEditable: Boolean read FAutoEditable write FAutoEditable;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write FDataSource;
  end;

  TTIWDBAdvDateEdit = class(TTIWAdvDateEdit)
  private
    FDataField: string;
    FDataSource: TDataSource;
    FAutoEditable: boolean;
  protected
    procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
    procedure SetValue(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;  
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
  published
    property AutoEditable: boolean read FAutoEditable write FAutoEditable default true;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write FDataSource;
  end;

  TTIWDBAdvTimeEdit = class(TTIWAdvTimeEdit)
  private
    FDataField: string;
    FDataSource: TDataSource;
    FAutoEditable: boolean;
  protected
    procedure Notification(AComponent: TComponent; AOperation: TOperation); override;
    procedure SetValue(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
  published
    property AutoEditable: boolean read FAutoEditable write FAutoEditable default true;  
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write FDataSource;
  end;
  

  function CheckDataSource(ADataSource: TDataSource): Boolean; overload;
  function CheckDataSource(ADataSource: TDataSource; const AFieldName: string; var VField: TField): Boolean; overload;

implementation


function CheckDataSource(ADataSource: TDataSource): Boolean;
begin
  Result := False;
  if ADataSource <> nil then
  begin
    if ADataSource.Dataset <> nil then
    begin
      Result := ADataSource.Dataset.Active;
    end;
  end;
end;

function CheckDataSource(ADataSource: TDataSource; const AFieldName: string; var VField: TField)
 : boolean;
begin
  Result := CheckDataSource(ADataSource) and (Length(AFieldName) > 0);
  if Result then begin
    VField := ADataSource.Dataset.FieldByName(AFieldName);
  end;
end;

function InEditMode(ADataset: TDataset): Boolean;
begin
  Result := ADataset.State in [dsEdit, dsInsert];
end;


{ TTIWDBAdvSpinEdit }

{$IFDEF TMSIW6}
function TTIWDBAdvSpinEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWDBAdvSpinEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Text := LField.AsString;
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end
    else
      Editable := True;
  end
  else
  begin
    Text := '';
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  {$IFDEF TMSIW6}
  Result := inherited RenderHTML(AContext);  
  {$ELSE}
  Result := inherited RenderHTML;
  {$ENDIF}
end;

procedure TTIWDBAdvSpinEdit.SetValue(const AValue: string);
var
  LField: TField;
begin
  inherited;
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if LField.AsString <> Text then
    begin
      Datasource.Edit;
      LField.AsString := Text;
    end;
  end;
end;

procedure TTIWDBAdvSpinEdit.Notification(AComponent: TComponent; AOperation: TOperation);
begin
  if (AOperation = opRemove) and (AComponent = DataSource) then
    FDataSource := nil;
end;

constructor TTIWDBAdvTimeEdit.Create(AOwner: TComponent);
begin
  inherited;
  FAutoEditable := true;
end;

{$IFDEF TMSIW6}
function TTIWDBAdvTimeEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWDBAdvTimeEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Enabled := InEditMode(DataSource.Dataset) and AutoEditable;
    Time := LField.AsDateTime;
  end;

  {$IFDEF TMSIW6}
  Result := inherited RenderHTML(AContext);
  {$ELSE}
  Result := inherited RenderHTML;
  {$ENDIF}
end;

procedure TTIWDBAdvTimeEdit.Notification(AComponent: TComponent; AOperation: TOperation);
begin
  if (AOperation = opRemove) and (AComponent = DataSource) then
    FDataSource := nil;
end;


procedure TTIWDBAdvTimeEdit.SetValue(const AValue: string);
var
  LField: TField;
var Year, Month, Day: Word;
begin
  if Enabled then
  begin                                                               
    inherited SetValue(AValue);

    if CheckDataSource(DataSource, DataField, LField) then
    begin
      if LField.AsDateTime <> Time then
      begin
        Datasource.Edit;
        DecodeDate(LField.AsDateTime, Year, Month, Day);
        LField.AsDateTime := EncodeDate(Year, Month, Day) + Time;
      end;
    end;
  end;
end;

constructor TTIWDBAdvDateEdit.Create(AOwner: TComponent);
begin
  inherited;
  FAutoEditable := true;
end;

{$IFDEF TMSIW6}
function TTIWDBAdvDateEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWDBAdvDateEdit.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Enabled := InEditMode(DataSource.Dataset) and AutoEditable;
    Date := LField.AsDateTime;
  end;

  {$IFDEF TMSIW6}
  Result := inherited RenderHTML(AContext);
  {$ELSE}
  Result := inherited RenderHTML;
  {$ENDIF}
end;

procedure TTIWDBAdvDateEdit.SetValue(const AValue: string);
var
  LField: TField;
var  Hour, Min, Sec, MSec: Word;
begin
  if Enabled then
  begin
    inherited SetValue(AValue);

    if CheckDataSource(DataSource, DataField, LField) then
    begin
      if LField.AsDateTime <> Date then
      begin
        Datasource.Edit;
        DecodeTime(LField.AsDateTime, Hour, Min, Sec, MSec);
        LField.AsDateTime := Date + EncodeTime(Hour, Min, Sec, MSec);
      end;
    end;
  end;
end;

procedure TTIWDBAdvDateEdit.Notification(AComponent: TComponent; AOperation: TOperation);
begin
  if (AOperation = opRemove) and (AComponent = DataSource) then
    FDataSource := nil;
end;


end.
