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

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D19}
  FMX.Graphics,
  {$ENDIF}
  FMX.Dialogs, FMXTee.Procs, FMXTee.Editor.Export, FMX.ListBox, FMX.Layouts;

type
  TBitmapExportEditor = class(TForm)
    ImageControl1: TImageControl;
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    function PreviewBitmap:TBitmap;
  public
    { Public declarations }
  end;

  TBitmapExportFormat=class(TTeeExportFormat)
  protected
    IFormat : String;

    class procedure AddSubFormats(const APanel:TCustomTeePanel;
                       const AList:TListBox; var FilterIndex:Integer); override;
    Function Bitmap(const AOptions:TBitmapExportEditor):TBitmap;
    Procedure DoCopyToClipboard; override;
  public
    function Description:String; override;
    function Editor(AOwner:TComponent):TForm; override;
    function FileExtension:String; override;
    class function FileFilter:String; override;
    class procedure SaveToFile(APanel:TCustomTeePanel; const FileName:String; const AOptions:TForm; AWidth, AHeight:Integer); override;
    Procedure SaveToStream(Stream:TStream); override;
    Procedure SaveToStream(Stream:TStream; const AOptions:TForm); override;

    property Format:String read IFormat write IFormat;
  end;

implementation

{$R *.fmx}

uses
  System.StrUtils,
  {$IFDEF D18}
  FMX.Surfaces,
  {$ENDIF}
  {$IFDEF D23}
  FMX.Platform,
  {$ENDIF}
  FMXTee.Constants, FMXTee.Editor.Picture;

{ TBitmapExportFormat }

function TBitmapExportFormat.Bitmap(
  const AOptions: TBitmapExportEditor): TBitmap;
var R : TRectF;
begin
  R.Left:=0;
  R.Top:=0;

  if Width>0 then R.Width:=Width else R.Width:=Panel.Width;
  if Height>0 then R.Height:=Height else R.Height:=Panel.Height;

  result:=Panel.TeeCreateBitmap(TAlphaColors.Null, R);
end;

function TBitmapExportFormat.Description: String;
begin
  result:=TeeMsg_AsBMP;
end;

procedure TBitmapExportFormat.DoCopyToClipboard;
{$IFDEF D23}
var ClipService : IFMXClipboardService;
    tmp : TBitmap;
{$ENDIF}
begin
  {$IFDEF D23}
  if TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, IInterface(ClipService)) then
  begin
    tmp:=Bitmap(nil);
    try
      ClipService.SetClipboard(tmp);
    finally
      tmp.Free;
    end;
  end;
  {$ENDIF}
end;

class procedure TBitmapExportFormat.AddSubFormats(const APanel:TCustomTeePanel;
                     const AList:TListBox; var FilterIndex:Integer);

  procedure AddExport(const S:String);
  var tmpExport : TBitmapExportFormat;
      S2 : String;
  begin
    tmpExport:=TBitmapExportFormat.Create;
    tmpExport.Panel:=APanel;

    if Copy(S,1,2)='*.' then
       S2:=Copy(S,3,Length(S)-2)
    else
       S2:=S;

    tmpExport.IFormat:=S2;
    tmpExport.IncFileFilterIndex(FilterIndex);

    tmpExport.AddItem(AList,S2,tmpExport);
  end;

var tmp : String;
    {$IFDEF FPC}
    t : Integer;
    {$ELSE}
    S : String;
    {$ENDIF}
begin
  tmp:={$IFDEF D17}TBitmapCodecManager{$ELSE}DefaultBitmapCodecClass{$ENDIF}.GetFileTypes;

  // SplitString not available in FPC
  {$IFDEF FPC}
  for t := 1 to TeeNumFields(tmp,';') do
      AddExport(TeeExtractField(tmp,t,';'));
  {$ELSE}

  for S in SplitString(tmp,';') do
      AddExport(S);
  {$ENDIF}
end;

function TBitmapExportFormat.Editor(AOwner: TComponent): TForm;
begin
  result:=TBitmapExportEditor.Create(AOwner);
  result.TagObject:=Self;
  TBitmapExportEditor(result).FormActivate(Self);
end;

function TBitmapExportFormat.FileExtension: String;
begin
  result:=IFormat;
end;

class function TBitmapExportFormat.FileFilter: String;
begin
  result:=TBackImageEditor.BitmapFileFilter;
end;

class procedure TBitmapExportFormat.SaveToFile(APanel:TCustomTeePanel; const FileName:String; const AOptions:TForm; AWidth, AHeight:Integer);
begin
  with TBitmapExportFormat.Create do
  try
    Panel:=APanel;
    CheckSize(AWidth, AHeight);

    IFormat:=ExtractFileExt(FileName);
    if Copy(IFormat,1,1)='.' then
       Delete(IFormat,1,1);

    SaveToFile(FileName, AOptions);
  finally
    Free;
  end;
end;

procedure TBitmapExportFormat.SaveToStream(Stream: TStream);
begin
  SaveToStream(Stream, nil);
end;

procedure TBitmapExportFormat.SaveToStream(Stream: TStream;
  const AOptions: TForm);
var {$IFNDEF D17}
    tmpCodec : TBitmapCodec;
    {$ENDIF}
    tmpBitmap : TBitmap;

    {$IFDEF D18}
    //SaveParams : TBitmapCodecSaveParams;
    tmpSurface : TBitmapSurface;
    {$ENDIF}
begin
  tmpBitmap:=Bitmap(TBitmapExportEditor(AOptions));
  try
    {$IFDEF D18}
    //SaveParams.Quality:=
    tmpSurface:=TBitmapSurface.Create;
    try
      tmpSurface.Assign(tmpBitmap);
      TBitmapCodecManager.SaveToStream(Stream, tmpSurface, IFormat, nil {SaveParams});
    finally
      tmpSurface.Free;
    end;

    Stream.Position:=0;
    {$ELSE}
    {$IFDEF D17}
    // SaveParams:PBitmapCodecSaveParams; (ie: JPEG Quality)
    TBitmapCodecManager.SaveToStream(Stream, tmpBitmap, IFormat, nil {SaveParams});
    Stream.Position:=0;
    {$ELSE}

    tmpCodec:=DefaultBitmapCodecClass.Create;
    try
      tmpCodec.SaveToStream(Stream, tmpBitmap, IFormat);
      Stream.Position:=0;

//    dpi:=FProperties.UDDpi.Position;
//    if dpi > 0 then TeeSetBitmapDPI(Stream, dpi);
    finally
      tmpCodec.Free;
    end;
    {$ENDIF}
    {$ENDIF}
  finally
    tmpBitmap.Free;
  end;
end;

procedure TBitmapExportEditor.FormActivate(Sender: TObject);
var tmp : TBitmap;
begin
  tmp:=PreviewBitmap;
  try
    ImageControl1.Bitmap:=tmp;
  finally
    tmp.Free;
  end;
end;

function TBitmapExportEditor.PreviewBitmap: TBitmap;
begin
  result:=TBitmapExportFormat(TagObject).Bitmap(Self);
end;

initialization
  TTeeExportFormats.RegisterFormat(TBitmapExportFormat);
finalization
  TTeeExportFormats.UnRegisterFormat(TBitmapExportFormat);
end.
