{------------------------------------------------------------------------------}
{
   Unit Name: EditDoubleIni
   Purpose  : An DoubleEdit component with save to Ini-file
   Author   : Vesa Lappalainen
   Date     : 10.09.00
   Changed  :

   ToDo     :
    - maybe a problem with docking forms that have EditDoubleIni
      (gets message WM_DESTROY, but does no get Destroy =>
       maybe not saved after docking)
}
{------------------------------------------------------------------------------}

unit EditDoubleIni;

interface
uses classes,EditDouble,
{$ifdef CLX}
{$else}
  Messages,
{$endif}
  kIniSave;


type
  TEditDoubleIni = class(TEditDouble)
  private
    FIni : TIniSave;
    FNoSave : boolean;
    function GetAsString: string;
    procedure SetAsString(const Value: string);
{$ifdef CLX}

{$else}
    procedure WMDestroy(var Message: TWMDestroy); message WM_DESTROY;
    procedure WMCreate(var Message: TWMDestroy); message WM_CREATE;
    function GetAsInteger: integer;
    procedure SetAsInteger(const Value: integer);
{$endif}
  public
    constructor Create(AOwner:TComponent); override;
    destructor  Destroy;                   override;
    procedure   Change;                    override;
    procedure   Loaded;                    override;
    property AsInteger : integer read GetAsInteger write SetAsInteger;
  published
    property Ini : TIniSave read FIni write FIni;
    property Align;
    property AsString : string read GetAsString write SetAsString stored false;
  end;

  procedure Register;

implementation

uses SysUtils;

procedure Register;
begin
  RegisterComponents('KaveOptions', [TEditDoubleIni]);
end;

{ TEditDoubleIni }

procedure TEditDoubleIni.Change;
begin
  inherited Change;
  if ( FNoSave ) then exit;
  if ( csLoading in ComponentState ) then exit;
  Ini.DoAutoSave;
end;

constructor TEditDoubleIni.Create(AOwner: TComponent);
begin
  FNoSave := true;
  FIni          := TIniSave.Create(self);
  Ini.AutoSave  := true;
  Ini.PropName  := 'AsString';
  inherited Create(AOwner);
  FNoSave := false;
end;

destructor TEditDoubleIni.Destroy;
begin
  Ini.DoLastSave;
  Ini.Free;
  Ini := nil;
  inherited Destroy;
end;

{$ifdef CLX}
{$else}
procedure TEditDoubleIni.WMDestroy(var Message: TWMDestroy);
begin
  if ( ini <> nil ) then Ini.DoLastSave;
  inherited;
end;

procedure TEditDoubleIni.WMCreate(var Message: TWMDestroy);
begin
  inherited;
  if ( ini <> nil ) then Ini.ReleaseLastSave;
end;
{$endif}

function TEditDoubleIni.GetAsString: string;
begin
  Result := FloatToStr(Value);
  Result := StringReplace(Result,',','.',[rfReplaceAll]);
end;

procedure TEditDoubleIni.Loaded;
begin
  inherited;
  Ini.Load;
end;

procedure TEditDoubleIni.SetAsString(const Value: string);
begin
  Text := Value;
end;

function TEditDoubleIni.GetAsInteger: integer;
begin
  Result := Round(Value);
end;

procedure TEditDoubleIni.SetAsInteger(const Value: integer);
begin
  self.Value := Value;
end;

end.
