{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2012                                        }
{            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 IWDBTMSCtrls;

interface

{$I TMSDEFS.INC}

uses
  Controls, Windows, Graphics,
  Classes, IWTMSCtrls, IWTMSCal, DB, SysUtils, IWCompListBox,
  IWHTMLTag, IWTMSBase, IWRenderContext,IWXMLTag,
  IWCompEdit, IWCompLabel, IWControl;

type
  TTIWDBHTMLLabel = class;

  TTIWTMSDatePickerDataLink = class(TDataLink)
  private
    FIWControl: TTIWCustomDatePicker;
  protected
    procedure DataSetScrolled(Distance: Integer); override;
    procedure RecordChanged(Field: TField); override;
  public
    constructor Create(AControl: TTIWCustomDatePicker);
  end;

  TTIWTMSLabelDataLink = class(TDataLink)
  private
    FIWControl: TIWCustomLabel;
  protected
    procedure DataSetScrolled(Distance: Integer); override;
    procedure RecordChanged(Field: TField); override;
  public
    constructor Create(AControl: TIWCustomLabel);
  end;

  TTIWTMSComboBoxDataLink = class(TDataLink)
  private
    FIWControl: TIWCustomComboBox;
  protected
    procedure DataSetScrolled(Distance: Integer); override;
    procedure RecordChanged(Field: TField); override;
  public
    constructor Create(AControl: TIWCustomComboBox);
  end;

  TTIWDBHTMLLabel = class(TTIWHTMLLabel)
  private
    FDataSource: TDataSource;
    FDBHTMLText:string;
    FDataLink: TTIWTMSLabelDataLink;
    FClipRegion: Boolean;
    FShowScrollBars: Boolean;
    function GetDataSource: TDataSource;
    procedure SetDataSource(const Value: TDataSource);
    procedure SetClipRegion(const Value: Boolean);
    procedure SetShowScrollBars(const Value: Boolean);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    function HTMLDBReplace(s:string; DataSet: TDataSet):string;
    procedure DBUpdate;
  public
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure UpdateHTML;
    function GetDisplText: string; override;
  published
    property Datasource:  TDataSource read GetDataSource write SetDataSource;
    property ClipRegion: Boolean read FClipRegion write SetClipRegion;
    property ShowScrollBars: Boolean read FShowScrollBars write SetShowScrollBars;
  end;

  TTIWDBAdvEdit = class(TTIWAdvEdit)
  private
    FAutoEditable: Boolean;
    FDataField: string;
    FDataSource: TDataSource;
    FDataLink: TTIWTMSDataLink;
    procedure SetDataSource(const Value: TDataSource);
  protected
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    procedure SetValue(const AValue: string); override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable default true;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
  end;

  TTIWDBAdvLUEdit = class(TTIWAdvLUEdit)
  private
    FAutoEditable: Boolean;
    FDataField: string;
    FDataSource: TDataSource;
    FDataLink: TTIWTMSDataLink;
    procedure SetDataSource(const Value: TDataSource);
  protected
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    procedure SetValue(const AValue: string); override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
  end;

  TTIWDBCountryComboBox = class(TTIWCountryComboBox)
  private
    FAutoEditable: Boolean;
    FCountryField : string;
    FCountrySource : TDataSource;
    FDataField: string;
    FDataLink: TTIWTMSComboBoxDataLink;
    FDataSource: TDataSource;
    procedure SetDataSource(const Value: TDataSource);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    procedure SetValue(const AValue: string); override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable;
    property CountryField : string read FCountryField write FCountryField;
    property CountrySource : TDataSource read FCountrySource write FCountrySource;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
  end;

  TTIWDBStateComboBox = class(TTIWStateComboBox)
  private
    FAutoEditable: Boolean;
    FStateField : string;
    FStateSource : TDataSource;
    FDataField: string;
    FDataLink: TTIWTMSComboBoxDataLink;
    FDataSource: TDataSource;
    procedure SetDataSource(const Value: TDataSource);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    procedure SetValue(const AValue: string); override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
    property StateField : string read FStateField write FStateField;
    property StateSource : TDataSource read FStateSource write FStateSource;
  end;

  TTIWDBCalendar = class(TTIWCalendar)
  private
    FAutoEditable: Boolean;
    FDataField: string;
    FDataSource: TDataSource;
    FDataLink: TTIWTMSControlDataLink;
    procedure SetDataSource(const Value: TDataSource);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    procedure SetValue(const AValue: String); override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
  end;

  TTIWDBDatePicker = class(TTIWDatePicker)
  private
    FAutoEditable: Boolean;
    FDataField: string;
    FDataSource: TDataSource;
    FFullTime: TDateTime;
    FDataLink: TTIWTMSDatePickerDataLink;
    procedure SetDataSource(const Value: TDataSource);
  protected
  public
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure SetValue(const AValue: string); override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
  published
    property AutoEditable: Boolean read FAutoEditable write FAutoEditable default true;
    property DataField: string read FDataField write FDataField;
    property DataSource: TDataSource read FDataSource write SetDataSource;
    property BorderColor;
    property BorderWidth;
    property CalendarFont;
    property CalendarWidth;
    property CalendarHeight;
    property ClientEvents;
    property Color;
    property DateSep;
    property DisabledAppearance;
    property DropDownImage;
    property EditorEnabled;
    property Flat;
    property FocusColor;
    property Font;
    property HoverColor;
    property TodayColor;
    property MonthColor;
    property MonthTextColor;
    property DateFormat;
    property Day;
    property Enabled;
    property Month;
    property Year;
    property NameOfMonths;
    property NameOfDays;
    property NullIsToday;
    property TabOrder;
    property BorderCollapse;
    property ShowNextMonth;
    property ShowPrevMonth;
    property ShowNextYear;
    property ShowPrevYear;
    property HintPrevYear;
    property HintNextYear;
    property HintNextMonth;
    property HintPrevMonth;
    property OnClick;
    property BackgroundImage;
    property Glyphs;
    property GradientStartColor;
    property GradientEndColor;
    property GradientDirection;
    property GridLineWidth;
    property GridLineColor;
    property MonthGradientStartColor;
    property MonthGradientEndColor;
    property MonthGradientDirection;
    property StartDay;
    property Transparency;
    property WeekendColor;
    property WeekendTextColor;
  end;

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

uses
  IWApplication, IWServerControllerBase;

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

function CheckDataSource(ADataSource: TDataSource): boolean; overload;
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 TTIWDBHTMLLabel.HTMLDBReplace(s:string; DataSet: TDataSet):string;
var
  BeforeTag,AfterTag,Fld,DBFld:string;
  i,j: Integer;
  DBField: TField;
  LPathName: string;
  bmp: TBitmap;

  function VarPos(su,s: string; var Res: Integer): Integer;
  begin
    Res := Pos(su,s);
    Result := Res;
  end;

begin
  i := 1;

  BeforeTag := '';

  while VarPos('(#',s,i) > 0 do
  begin
    BeforeTag := BeforeTag + Copy(s,1,i - 1); //part prior to the tag
    AfterTag := Copy(s,i,Length(s)); //part after the tag

    j := Pos(')',AfterTag);
    if j = 0 then
      Break;
       
    Fld := Copy(AfterTag,1,j - 1);

    Delete(Fld,1,2);
    Delete(s,1,i + j - 1);

    DBFld := '';

    if Assigned(DataSet) and (DBFld = '') then
    begin
      if DataSet.Active then
      begin
        if Assigned(DataSet.FindField(Fld)) then
        begin
          DBField := DataSet.FieldByName(fld);
          if DBField.DataType = ftMemo then
            DBFld := DBField.Text
          else
          if DBField.DataType <> ftGraphic then
            DBFld := DBField.DisplayText
          else
          begin
            if not (csDesigning in ComponentState) then
            begin
              bmp := TBitmap.Create;
              bmp.Assign(DBField);
              LPathname :=  TIWAppCache.GraphicToCacheFile(bmp, imgBMP);
              DBFld := LPathname;
              bmp.Free;
            end
            else
            begin
              bmp := TBitmap.Create;
              bmp.Assign(DBField);
              bmp.SaveToFile('tmp.bmp');
              DBFld := 'file://'+GetCurrentDir + '/tmp.bmp';
              bmp.Free;
            end;
          end;
        end;
      end
      else
        DBfld := '['+fld+']';
    end
    else
     if DBFld='' then DBfld := '['+fld+']';

    BeforeTag := BeforeTag + DBFld;
  end;

  Result := Beforetag + s;
end;


{ TDBHTMLabel }

constructor TTIWDBHTMLLabel.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink :=  TTIWTMSLabelDataLink.Create(self);
end;

procedure TTIWDBHTMLLabel.DBUpdate;
begin
  if Assigned(Datasource) then
  begin
    if Assigned(DataSource.DataSet) then
    begin
      if DataSource.DataSet.Active then
      begin
        FDBHTMLText := HTMLDBReplace(HTMLText,Datasource.Dataset);
      end
      else
        FDBHTMLText := HTMLDBReplace(HTMLText,nil);
    end
    else
      FDBHTMLText := HTMLDBReplace(HTMLText,nil);
  end
  else
  begin
    FDBHTMLText := HTMLDBReplace(HTMLText,nil);
  end;

  //FDBHTMLText := HTMLText;
end;

destructor TTIWDBHTMLLabel.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBHTMLLabel.GetDataSource: TDataSource;
begin
  Result := FDataSource;
end;


procedure TTIWDBHTMLLabel.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = DataSource) then
    DataSource := nil;
end;

procedure TTIWDBHTMLLabel.SetClipRegion(const Value: Boolean);
begin
  FClipRegion := Value;
  Invalidate;
end;

procedure TTIWDBHTMLLabel.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
  DBUpdate;
end;

procedure TTIWDBHTMLLabel.SetShowScrollBars(const Value: Boolean);
begin
  FShowScrollBars := Value;
  Invalidate;
end;

procedure TTIWDBHTMLLabel.UpdateHTML;
begin
  DBUpdate;
end;

function TTIWDBHTMLLabel.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  t_OldTxt, t_NewTxt:String;
begin
  t_OldTxt := GetDisplText;
  DBUpdate;
  t_NewTxt := GetDisplText;
  if( t_OldTxt <> t_NewTxt ) then
  begin
    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute('<![CDATA[document.getElementById("'
      + HTMLName + '").innerHTML = "' + GetDisplText + '";]]>');
  end;
  if( FClipRegion ) then
  begin
    if( FShowScrollBars ) then
      GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute('<![CDATA[document.getElementById("'
        + HTMLName + '").style.overflow = "auto";]]>')
    else
      GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute('<![CDATA[document.getElementById("'
        + HTMLName + '").style.overflow = "hidden";]]>')
  end
  else
    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute('<![CDATA[document.getElementById("'
      + HTMLName + '").style.overflow = "visible";]]>');

 
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWHTMLLABEL');

    RenderAsyncCommonProperties( AContext, Result );

  except
    FreeAndNil(Result);
  end;
end;

function TTIWDBHTMLLabel.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
begin
  Result := TIWHTMLTag.CreateTag('DIV');
  if( FClipRegion ) then
  begin
    if( FShowScrollBars ) then
      result.Add( 'style=overflow: auto')
    else
      result.Add( 'style=overflow: hidden');
  end;
  Result.Contents.AddText(GetDisplText);
end;

function TTIWDBHTMLLabel.GetDisplText: string;
begin
  DBUpdate;
  Result := FDBHTMLText;
end;

{ TTIWDBAdvEdit }

destructor TTIWDBAdvEdit.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBAdvEdit.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin

  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;

    if (DataSource.DataSet.State in [dsInsert,dsEdit]) then
      Text := LField.Text
    else
      Text := LField.DisplayText;
  end;

  Result := inherited RenderAsync(AContext);
end;

constructor TTIWDBAdvEdit.Create(AOwner: TComponent);
begin
  inherited;
  FAutoEditable := true;
  FDataLink :=  TTIWTMSDataLink.Create(self);
end;


function TTIWDBAdvEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;
    if (DataSource.DataSet.State in [dsInsert,dsEdit]) then
//      Text := LField.AsString
//Fix: make sure the OnGetText event is called
      Text := LField.Text
    else
      Text := LField.DisplayText;
  end
  else
  begin
    Text := '';
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  Result := inherited RenderHTML(AContext);
end;


procedure TTIWDBAdvEdit.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

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


{ TTIWDBAdvLUEdit }

constructor TTIWDBAdvLUEdit.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink :=  TTIWTMSDataLink.Create(self);
end;

destructor TTIWDBAdvLUEdit.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBAdvLUEdit.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin

  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;

    if (DataSource.DataSet.State in [dsInsert,dsEdit]) then
      Text := LField.Text
    else
      Text := LField.DisplayText;
  end;

  Result := inherited RenderAsync(AContext);
end;

function TTIWDBAdvLUEdit.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;

    if (DataSource.DataSet.State in [dsInsert,dsEdit]) then
      Text := LField.Text
    else
      Text := LField.DisplayText;
  end
  else
  begin
    Text := '';
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  Result := inherited RenderHTML(AContext);
end;

procedure TTIWDBAdvLUEdit.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

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

{ TTIWDBCalendar }

constructor TTIWDBCalendar.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink :=  TTIWTMSControlDataLink.Create(self);
end;

destructor TTIWDBCalendar.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBCalendar.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin

  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Date := LField.AsDateTime;
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;
  end;

  Result := inherited RenderAsync(AContext);
end;

function TTIWDBCalendar.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Date := LField.AsDateTime;
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end;
  end
  else
  begin
    Date := Now;
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  Result := inherited RenderHTML(AContext);
end;

procedure TTIWDBCalendar.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

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

end;

{ TTIWDBDatePicker }

destructor TTIWDBDatePicker.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBDatePicker.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin

  FFullTime := 0;
  if CheckDataSource(DataSource, DataField, LField) then
  begin
//    if AutoEditable then
//      Enabled := (DataSource.Dataset.State in [dsEdit,dsInsert]) and AutoEditable;

    if (LField.IsNull) then
    begin
      if NullIsToday then
        Date := Now
      else
        Date := 0;
    end
    else
    begin
      Date := LField.AsDateTime;
      FFullTime := LField.AsDateTime;
    end;

    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end
    else
    begin
      Editable := Enabled;
    end;
  end
  else
  begin
    Enabled := False;
    Date := Now;
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  Result := inherited RenderAsync(AContext);
end;

constructor TTIWDBDatePicker.Create(AOwner: TComponent);
begin
  inherited;
//  FSupportsInput := True;
//  FSupportsSubmit := True;
  FAutoEditable := True;
  FDataLink := TTIWTMSDatePickerDataLink.Create(self);
end;

function TTIWDBDatePicker.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  FFullTime := 0;
  if CheckDataSource(DataSource, DataField, LField) then
  begin
//    if AutoEditable then
//      Enabled := (DataSource.Dataset.State in [dsEdit,dsInsert]) and AutoEditable;

    if (LField.IsNull) then
    begin
      if NullIsToday then
        Date := Now
      else
        Date := 0;
    end
    else
    begin
      Date := LField.AsDateTime;
      FFullTime := LField.AsDateTime;
    end;

    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
    end
    else
    begin
        Editable := Enabled;
    end;
  end
  else
  begin
    Enabled := False;
    Date := Now;
    if AutoEditable then
    begin
      Editable := False;
    end;
  end;
  Result := inherited RenderHTML(AContext);
end;

procedure TTIWDBDatePicker.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

procedure TTIWDBDatePicker.SetValue(const AValue: string);
var
  LField: TField;
begin
  inherited;

  if not Enabled or not Editable then
    Exit;

  if CheckDataSource(DataSource, DataField, LField) then
  begin
    if (DataSource.AutoEdit) or ((not DataSource.AutoEdit) and (InEditMode(DataSource.DataSet))) then
    begin
      if (LField.AsDateTime <> Date) or (LField.Text = '') then
      begin
        Datasource.Edit;
        if Date = 0 then
        {$IFDEF DELPHIXE3_LVL}
          LField.SetData(TValueBuffer(nil),true)
        {$ENDIF}
        {$IFNDEF DELPHIXE3_LVL}
          LField.SetData(nil)
        {$ENDIF}
        else
          LField.AsDateTime := Date + Frac(FFullTime);
      end;
    end;
  end;
end;


{ TTIWDBCountryComboBox }

constructor TTIWDBCountryComboBox.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink := TTIWTMSComboBoxDataLink.Create(self);
end;

destructor TTIWDBCountryComboBox.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBCountryComboBox.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin
  if CheckDataSource(CountrySource, CountryField, LField) then
  begin
    if Assigned(CountrySource) then
    begin
      if AutoEditable then
      begin
        Editable := InEditMode(DataSource.Dataset);
        Enabled := InEditMode(DataSource.Dataset);
      end;

      Items.Clear;
      CountrySource.DataSet.First;
      while not (CountrySource.DataSet.Eof) do
      begin
        Items.Add(LField.Text);
        CountrySource.DataSet.Next;
      end;
    end;
  end;

  if CheckDataSource(DataSource,DataField,LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
      Enabled := InEditMode(DataSource.Dataset);
    end;

    ItemIndex := Items.IndexOf(LField.Text);
  end;

  Result := inherited RenderAsync(AContext);
end;

function TTIWDBCountryComboBox.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  if CheckDataSource(CountrySource, CountryField, LField) then
  begin
    if Assigned(CountrySource) then
    begin
      Items.Clear;
      CountrySource.DataSet.First;
      while not (CountrySource.DataSet.Eof) do
      begin
        Items.Add(LField.Text);
        CountrySource.DataSet.Next;
      end;
    end;
  end;

  if CheckDataSource(DataSource,DataField,LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
      Enabled := InEditMode(DataSource.Dataset);
    end;

    ItemIndex := Items.IndexOf(LField.Text);
  end;

  Result := inherited RenderHTML(AContext);
end;

procedure TTIWDBCountryComboBox.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

procedure TTIWDBCountryComboBox.SetValue(const AValue: string);
var
  LField: TField;
begin
  inherited;
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Datasource.Edit;
    if (StrToInt(Avalue) = -1) then
       LField.Text := Items[0]
    else
    LField.Text := Items[StrToInt(AValue)];
  end;
end;

{ TTIWDBStateComboBox }
constructor TTIWDBStateComboBox.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink := TTIWTMSComboBoxDataLink.Create(self);
end;

destructor TTIWDBStateComboBox.Destroy;
begin
  FDataLink.Free;
  inherited;
end;

function TTIWDBStateComboBox.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  LField: TField;
begin
  if CheckDataSource(StateSource, StateField, LField) then
  begin
    if Assigned(StateSource) then
    begin
      Items.Clear;
      StateSource.DataSet.First;
      while not (StateSource.DataSet.Eof) do
      begin
        Items.Add(LField.Text);
        StateSource.DataSet.Next;
      end;
    end;
  end;
  if CheckDataSource(DataSource,DataField,LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
      Enabled := InEditMode(DataSource.Dataset);
    end;

    ItemIndex := Items.IndexOf(LField.Text);
  end;

  Result := inherited RenderAsync(AContext);
end;

function TTIWDBStateComboBox.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LField: TField;
begin
  if CheckDataSource(StateSource, StateField, LField) then
  begin
    if Assigned(StateSource) then
    begin
      Items.Clear;
      StateSource.DataSet.First;
      while not (StateSource.DataSet.Eof) do
      begin
        Items.Add(LField.Text);
        StateSource.DataSet.Next;
      end;
    end;
  end;

  if CheckDataSource(DataSource,DataField,LField) then
  begin
    if AutoEditable then
    begin
      Editable := InEditMode(DataSource.Dataset);
      Enabled := InEditMode(DataSource.Dataset);
    end;

    ItemIndex := Items.IndexOf(LField.Text);
  end;
  Result := inherited RenderHTML(AContext);
end;

procedure TTIWDBStateComboBox.SetDataSource(const Value: TDataSource);
begin
  FDataSource := Value;
  FDataLink.DataSource := FDataSource;
end;

procedure TTIWDBStateComboBox.SetValue(const AValue: string);
var
  LField: TField;
begin
  inherited;
  if CheckDataSource(DataSource, DataField, LField) then
  begin
    Datasource.Edit;
    if (StrToInt(Avalue) = -1) then
       LField.Text := Items[0]
    else
    LField.Text := Items[StrToInt(AValue)];
  end;
end;


{ TTIWTMSDatePickerDataLink }

constructor TTIWTMSDatePickerDataLink.Create(AControl: TTIWCustomDatePicker);
begin
  inherited Create;
  FIWControl := AControl;
end;

procedure TTIWTMSDatePickerDataLink.DataSetScrolled(Distance: Integer);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

procedure TTIWTMSDatePickerDataLink.RecordChanged(Field: TField);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

{ TTIWTMSLabelDataLink }

constructor TTIWTMSLabelDataLink.Create(AControl: TIWCustomLabel);
begin
  inherited Create;
  FIWControl := AControl;
end;

procedure TTIWTMSLabelDataLink.DataSetScrolled(Distance: Integer);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

procedure TTIWTMSLabelDataLink.RecordChanged(Field: TField);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

{ TTIWTMSComboBoxDataLink }

constructor TTIWTMSComboBoxDataLink.Create(AControl: TIWCustomComboBox);
begin
  inherited Create;
  FIWControl := AControl;
end;

procedure TTIWTMSComboBoxDataLink.DataSetScrolled(Distance: Integer);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

procedure TTIWTMSComboBoxDataLink.RecordChanged(Field: TField);
begin
  inherited;
  if Assigned(FIWControl) then
    FIWControl.Invalidate;
end;

end.
