{********************************************}
{ TeeChart Pro Metafile exporting            }
{ Copyright (c) 1995-2020 by Steema Software }
{         All Rights Reserved                }
{********************************************}
unit TeeEmfOptions;
{$I TeeDefs.inc}

interface

uses
  {$IFNDEF LINUX}
  Windows, Messages,
  {$ENDIF}
  SysUtils, Classes,

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,

  {$IFDEF D9}
  Types,
  {$ENDIF}
  TeeProcs, TeeExport;

type
  TEMFOptions=class;

  TEMFExportFormat=class(TTeeExportFormat)
  private
    FProperties: TEMFOptions;

    function GetEnhanced: Boolean;
    procedure SetEnhanced(const Value: Boolean);
  protected
    Procedure DoCopyToClipboard; override;
    function FileFilterIndex: Integer; override;
    procedure IncFileFilterIndex(var FilterIndex: Integer); override;
    function WantsFilterIndex(Index:Integer):Boolean; override;
  public
    function Description:String; override;
    function FileExtension:String; override;
    class function FileFilter:String; override;
    function GraphicClass:TGraphicClass; override;
    Function Metafile:TMetafile;
    Function Options(Check:Boolean=True):TForm; override;
    Procedure SaveToStream(Stream:TStream); override;

    property Enhanced:Boolean read GetEnhanced write SetEnhanced;
  end;

  TEMFOptions = class(TForm)
    Panel1: TPanel;
    CBEnhanced: TCheckBox;
    GroupBox1: TGroupBox;
    ImagePreview: TImage;
    procedure CBEnhancedClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    IFormat : TEMFExportFormat;
    procedure Preview;
  public
    { Public declarations }
  end;

implementation

{$IFNDEF LCL}
{$R *.DFM}
{$ELSE}
{$R *.lfm}
{$ENDIF}

Uses
  Clipbrd, TeCanvas, TeeConst;

function TEMFExportFormat.Description:String;
begin
  result:=TeeMsg_AsEMF;
end;

class function TEMFExportFormat.FileFilter:String;
begin
  result:=TeeMsg_EMFFilter;
end;

function TEMFExportFormat.FileExtension:String;
begin
  if Enhanced then
     result:='emf' // Do not localize
  else
     result:='wmf'; // Do not localize
end;

function TEMFExportFormat.FileFilterIndex: Integer;
begin
  if Enhanced then result:=FFilterIndex else result:=Succ(FFilterIndex);
end;

procedure TEMFExportFormat.IncFileFilterIndex(var FilterIndex: Integer);
begin
  inherited;
  Inc(FilterIndex);
end;

Function TEMFExportFormat.Metafile:TMetafile;
begin
  CheckSize;
  result:=Panel.TeeCreateMetafile(Enhanced,TeeRect(Width,Height));
end;

Function TEMFExportFormat.Options(Check:Boolean=True):TForm;
begin
  if Check and (not Assigned(FProperties)) then
  begin
    FProperties:=TEMFOptions.Create(nil);
    FProperties.IFormat:=Self;
  end;

  result:=FProperties;
end;

procedure TEMFExportFormat.DoCopyToClipboard;
var tmp : TMetafile;
begin
  tmp:=Metafile;
  try
    Clipboard.Assign(tmp);
  finally
    tmp.Free;
  end;
end;

procedure TEMFExportFormat.SaveToStream(Stream:TStream);
begin
  With Metafile do
  try
    SaveToStream(Stream);
  finally
    Free;
  end;
end;

function TEMFExportFormat.GetEnhanced: Boolean;
begin
  if Assigned(FProperties) then result:=FProperties.CBEnhanced.Checked
                           else result:=True;
end;

procedure TEMFExportFormat.SetEnhanced(const Value: Boolean);
begin
  Options;
  FProperties.CBEnhanced.Checked:=Value;
end;

// Special case only for Metafile export format, that
// supports both "emf" and "wmf" extensions.
function TEMFExportFormat.WantsFilterIndex(Index: Integer): Boolean;
begin
  result:=FFilterIndex=Index;

  if (not result) and (Succ(FFilterIndex)=Index) then
  begin
    Enhanced:=False;
    result:=True;
  end;
end;

{ TEMFOptions }

function TEMFExportFormat.GraphicClass: TGraphicClass;
begin
  result:=TMetafile;
end;

procedure TEMFOptions.Preview;
var tmp : TMetafile;
begin
  if Assigned(IFormat) then
  begin
    tmp:=IFormat.Metafile;
    try
      ImagePreview.Picture.Graphic:=tmp;
    finally
      tmp.Free;
    end;
  end;
end;

procedure TEMFOptions.CBEnhancedClick(Sender: TObject);
begin
  Preview;
end;

procedure TEMFOptions.FormShow(Sender: TObject);
begin
  Align:=alClient;
  Preview;
end;

initialization
  RegisterTeeExportFormat(TEMFExportFormat);
finalization
  UnRegisterTeeExportFormat(TEMFExportFormat);
end.
