{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012 by Steema Software      }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Picture;
{$I TeeDefs.inc}

interface

uses
  SysUtils, Types, Classes, Variants, FMX.Types, FMX.Controls, FMX.Forms,
  FMX.Edit, FMX.Dialogs, FMX.Objects, FMXTee.Canvas, FMXTee.RadioGroup,
  FMX.ListBox;

type
  TBackImageEditor = class(TForm)
    Bevel1: TRectangle;
    Image1: TImage;
    LabelSize: TLabel;
    LabelExt: TLabel;
    BBrowse: TButton;
    Button1: TButton;
    GBPosition: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    UDLeft: TSpinBox;
    UDTop: TSpinBox;
    PanelButtons: TPanel;
    Button2: TButton;
    Button3: TButton;
    RGBitmap: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    LQuality: TLabel;
    CBQuality: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    procedure BBrowseClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure UDLeftChange(Sender: TObject);
    procedure RGBitmapChange(Sender: TObject);
    procedure UDTopChange(Sender: TObject);
    procedure CBQualityChange(Sender: TObject);
  private
   { Private declarations }
    FPicture : TPicture;

    procedure EnableImageControls;
    procedure SetBrowseCaption;
  public
   { Public declarations }

    class function BitmapFileFilter:String;
    class function CreateEmbedd(AOwner: TComponent;
                           APicture: TPicture; AParent: TFmxObject): TForm;
    class function GetPictureFileName(AOwner:TComponent):String;
    procedure RefreshPicture(const APicture:TPicture);
  end;

function StripHotKey(const S:String):String;

implementation

{$R *.fmx}

uses
  FMXTee.Constants, FMXTee.Procs, System.StrUtils, FMXTee.Editor.Stroke;

procedure TBackImageEditor.CBQualityChange(Sender: TObject);
begin
  TBackImage(FPicture).DrawQuality:=TCanvasStretchQuality(CBQuality.ItemIndex);
end;

class function TBackImageEditor.CreateEmbedd(AOwner: TComponent;
                          APicture: TPicture; AParent: TFmxObject): TForm;
begin
  result:=TBackImageEditor.Create(AOwner);
  TBackImageEditor(result).RefreshPicture(APicture);

  TBackImageEditor(result).PanelButtons.Visible:=False;
  EmbeddForm(result,AParent);
end;

procedure TBackImageEditor.RefreshPicture(const APicture: TPicture);
begin
  FPicture:=APicture;

  Image1.Bitmap:=FPicture.Graphic;
  LabelSize.Text:=IntToStr(FPicture.Width)+'x'+IntToStr(FPicture.Height);

  if FPicture is TBackImage then
  begin
    UDLeft.Value:=TBackImage(FPicture).Left;
    UDTop.Value:=TBackImage(FPicture).Top;
    RGBitmap.ItemIndex:=Ord(TBackImage(FPicture).Mode);
    CBQuality.ItemIndex:=Ord(TBackImage(FPicture).DrawQuality);
  end;

  EnableImageControls;
end;

procedure TBackImageEditor.RGBitmapChange(Sender: TObject);
begin
  TBackImage(FPicture).Mode:=TTeeBackImageMode(RGBitmap.ItemIndex);
  EnableImageControls;
end;

procedure TBackImageEditor.EnableImageControls;
begin
  RGBitmap.Enabled:=Assigned(FPicture.Graphic);

  if not (FPicture is TBackImage) then
  begin
    RGBitmap.Visible:=False;
    GBPosition.Visible:=False;
    CBQuality.Visible:=False;
    LQuality.Visible:=False;
  end
  else
    EnableControls(TBackImage(FPicture).Mode=pbmCustom, [UDLeft,UDTop]);

  LabelExt.Visible:=RGBitmap.Enabled;
  LabelSize.Visible:=RGBitmap.Enabled;

  if LabelSize.Visible then
  begin
    LabelSize.Text:=IntToStr(FPicture.Width)+'x'+IntToStr(FPicture.Height); // Do not localize
//    LabelExt.Text:=GraphicExtension(TGraphicClass(FPicture.Graphic.ClassType));
  end;

  SetBrowseCaption;
end;

class function TBackImageEditor.GetPictureFileName(AOwner:TComponent): String;
begin
  result:='';

  With TOpenDialog.Create(AOwner) do
  try
    Filter:=TBackImageEditor.BitmapFileFilter;
    if Execute then result:=FileName;
  finally
    Free;
  end;
end;

function StripHotKey(const S:String):String;
begin
  result:=ReplaceChar(S,'&');
end;

procedure TBackImageEditor.SetBrowseCaption;
begin
  Image1.Bitmap.Assign(FPicture.Graphic);

  if not FPicture.IsEmpty then
     BBrowse.Text:=StripHotKey(TeeMsg_ClearImage)
  else
     BBrowse.Text:=StripHotKey(TeeMsg_BrowseImage);
end;

procedure TBackImageEditor.UDLeftChange(Sender: TObject);
begin
  TBackImage(FPicture).Left:=Round(UDLeft.Value);
end;

procedure TBackImageEditor.UDTopChange(Sender: TObject);
begin
  TBackImage(FPicture).Top:=Round(UDTop.Value);
end;

class function TBackImageEditor.BitmapFileFilter:String;
{$IFNDEF D17}
var S, tmp : String;
    {$IFDEF FPC}
    t : Integer;
    {$ENDIF}
{$ENDIF}
begin
  {$IFDEF D17}
  result:=TBitmapCodecManager.GetFilterString;
  {$ELSE}

  result:='';

  tmp:={$IFDEF D17}TBitmapCodecManager{$ELSE}DefaultBitmapCodecClass{$ENDIF}.GetFileTypes;

  {$IFDEF FPC}
  for t := 1 to TeeNumFields(tmp,';') do
  begin
    S:=TeeExtractField(tmp,t,';');

    if result<>'' then
       result:=result+'|';

    result:=result+S+' files ('+S+')|'+S;
  end;
  {$ELSE}
  for S in SplitString(tmp,';') do
  begin
    if result<>'' then
       result:=result+'|';

    result:=result+S+' files ('+S+')|'+S;
  end;
  {$ENDIF}

  {$ENDIF}
end;

procedure TeeLoadClearImage(AOwner:TComponent; AImage:TPicture);
var tmp : String;
begin
  if not AImage.IsEmpty then
  begin
    AImage.Width:=0;
    AImage.Height:=0;
  end
  else
  begin
    tmp:=TBackImageEditor.GetPictureFileName(AOwner);

    if tmp<>'' then
    begin
      // FMX does not raise exception at LoadFromFile if "tmp" FileName does not exist
      if not FileExists(tmp) then
         Raise Exception.Create('Image file does not exist: '+tmp);

      AImage.LoadFromFile(tmp);
    end;
  end;
end;

procedure TBackImageEditor.BBrowseClick(Sender: TObject);
begin
  TeeLoadClearImage(Self,FPicture);

  SetBrowseCaption;

  EnableImageControls;
end;

procedure TBackImageEditor.Button1Click(Sender: TObject);
begin
// TODO
end;

end.
