unit FormImage;

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  ExtDlgs, ContactImage, FormEx;

type

  { TFormImage }

  TFormImage = class(TFormEx)
    ButtonCancel: TButton;
    ButtonOk: TButton;
    ButtonLoad: TButton;
    ButtonSave: TButton;
    ButtonClear: TButton;
    EditUrl: TEdit;
    ImagePhoto: TImage;
    Label1: TLabel;
    OpenPictureDialog1: TOpenPictureDialog;
    SavePictureDialog1: TSavePictureDialog;
    procedure ButtonClearClick(Sender: TObject);
    procedure ButtonLoadClick(Sender: TObject);
    procedure ButtonSaveClick(Sender: TObject);
    procedure EditUrlChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    procedure ImageChange(Sender: TObject);
  public
    Image: TContactImage;
    procedure UpdateInterface;
  end;


implementation

{$R *.lfm}

uses
  Core;

{ TFormImage }

procedure TFormImage.FormCreate(Sender: TObject);
begin
  Image := TContactImage.Create(nil);
  Image.OnChange := ImageChange;
end;

procedure TFormImage.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Image);
end;

procedure TFormImage.ButtonLoadClick(Sender: TObject);
begin
  OpenPictureDialog1.InitialDir := ExtractFileDir(Core.Core.LastPhotoFileName);
  OpenPictureDialog1.FileName := ExtractFileName(Core.Core.LastPhotoFileName);
  if OpenPictureDialog1.Execute then begin
    Image.LoadFromFile(OpenPictureDialog1.FileName);
    Core.Core.LastPhotoFileName := OpenPictureDialog1.FileName;
  end;
end;

procedure TFormImage.ButtonClearClick(Sender: TObject);
begin
  Image.Clear;
end;

procedure TFormImage.ButtonSaveClick(Sender: TObject);
begin
  SavePictureDialog1.InitialDir := ExtractFileDir(Core.Core.LastPhotoFileName);
  SavePictureDialog1.FileName := ExtractFileName(Core.Core.LastPhotoFileName);
  if SavePictureDialog1.Execute then begin
    Image.SaveToFile(SavePictureDialog1.FileName);
    Core.Core.LastPhotoFileName := SavePictureDialog1.FileName;
  end;
end;

procedure TFormImage.EditUrlChange(Sender: TObject);
begin
  Image.Url := EditUrl.Text;
end;

procedure TFormImage.FormShow(Sender: TObject);
begin
  ImageChange(nil);
end;

procedure TFormImage.ImageChange(Sender: TObject);
begin
  if Image.Used and (Image.Url = '') then ImagePhoto.Picture.Bitmap.Assign(Image.Bitmap)
    else ImagePhoto.Picture.Assign(Core.Core.GetProfileImage.Picture);
  EditUrl.Text := Image.Url;
  UpdateInterface;
end;

procedure TFormImage.UpdateInterface;
begin
  ButtonSave.Enabled := Image.Used;
  ButtonClear.Enabled := Image.Used;
end;

end.

