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

interface

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

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls,
  {$IFDEF D9}
  Types,
  {$ENDIF}

  TeeProcs, TeeExport, TeCanvas, TeeFilters;

type
  TBMPExportFormat=class;

  TBMPOptions = class(TForm)
    CBMono: TCheckBox;
    Label1: TLabel;
    CBColors: TComboFlat;
    Button1: TButton;
    GroupBox1: TGroupBox;
    ImageFiltered1: TImageFiltered;
    UDDpi: TUpDown;
    EDpi: TEdit;
    Label2: TLabel;
    procedure CBMonoClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CBColorsChange(Sender: TObject);
  private
    { Private declarations }
    procedure Preview;
  protected
    IFormat : TBMPExportFormat;
  public
    { Public declarations }
  end;

  TBMPExportFormat=class(TTeeExportFormat)
  private
    FFilters   : TFilterItems;
    FProperties: TBMPOptions;

    Procedure CheckProperties;
    function InternalGetBitmap:TBitmap;
  protected
    Procedure DoCopyToClipboard; override;
  public
    Constructor Create; override;
    Destructor Destroy; override;

    Function Bitmap(const AOptions:TBMPOptions):TBitmap;
    function Description:String; override;
    function FileExtension:String; override;
    class function FileFilter:String; override;
    function GraphicClass:TGraphicClass; override;
    Function Options(Check:Boolean=True):TForm; override;
    Procedure SaveToStream(Stream:TStream); override;
    Procedure SaveToStream(Stream:TStream; const AOptions:TForm); override;
  end;

Procedure TeeSaveToBitmap( APanel:TCustomTeePanel;
                           Const FileName: WideString;
                           Const R:TRect;
                           Dpi: Integer = 0);

implementation

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

Uses
  Clipbrd, TeeConst, TeeFiltersEditor;

procedure TBMPOptions.CBMonoClick(Sender: TObject);
begin
  CBColors.Enabled:=not CBMono.Checked;
  Preview;
end;

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

class function TBMPExportFormat.FileFilter:String;
begin
  result:=TeeMsg_BMPFilter;
end;

function TBMPExportFormat.FileExtension:String;
begin
  result:='bmp'; // Do not localize
end;

Function TBMPExportFormat.Bitmap(const AOptions:TBMPOptions):TBitmap;
begin
  if Assigned(AOptions) then FProperties:=AOptions
                        else CheckProperties;

  result:=InternalGetBitmap;
  FFilters.ApplyTo(result);

  if Assigned(AOptions) then
     FProperties:=nil;  
end;

function TBMPExportFormat.InternalGetBitmap:TBitmap;
begin
  CheckSize;

  result:=GetBitmap(TPixelFormat(FProperties.CBColors.ItemIndex),
                    FProperties.UDDpi.Position);

  result.Monochrome:=FProperties.CBMono.Checked;
end;

Procedure TBMPExportFormat.CheckProperties;
begin
  if not Assigned(FProperties) then
  begin
    FProperties:=TBMPOptions.Create(nil);
    FProperties.IFormat:=Self;
  end;
end;

Function TBMPExportFormat.Options(Check:Boolean=True):TForm;
begin
  if Check then CheckProperties;
  result:=FProperties;
end;

procedure TBMPExportFormat.DoCopyToClipboard;
var tmp : TBitmap;
begin
  tmp:=Bitmap(nil);
  try
    Clipboard.Assign(tmp);
  finally
    tmp.Free;
  end;
end;

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

procedure TBMPExportFormat.SaveToStream(Stream: TStream; const AOptions: TForm);
//var dpi: Integer;
begin
  With Bitmap(TBMPOptions(AOptions)) do
  try
    SaveToStream(Stream);
//    dpi:=FProperties.UDDpi.Position;
//    if dpi > 0 then TeeSetBitmapDPI(Stream, dpi);
  finally
    Free;
  end;
end;

function TBMPExportFormat.GraphicClass: TGraphicClass;
begin
  result:=TBitmap;
end;

constructor TBMPExportFormat.Create;
begin
  inherited;
  FFilters:=TFilterItems.Create(nil,TTeeFilter);
end;

destructor TBMPExportFormat.Destroy;
begin
  FFilters.Free;
  inherited;
end;

Procedure TeeSaveToBitmap( APanel:TCustomTeePanel;
                           Const FileName: WideString;
                           Const R:TRect;
                           Dpi: Integer = 0);
var tmp         : String;
    tmpWidth,
    tmpHeight   : Integer;
    FProperties : TBMPOptions;
begin
    { verify filename extension }
  tmp:=FileName;

  if ExtractFileExt(tmp)='' then
     tmp:=tmp+'.bmp'; // Do not localize

  FProperties:=TBMPOptions.Create(nil);
  try
    FProperties.UDDpi.Position:=Dpi;

    tmpWidth:=R.Right-R.Left;
    tmpHeight:=R.Bottom-R.Top;

    if tmpWidth<=0 then tmpWidth:=APanel.Width;
    if tmpHeight<=0 then tmpHeight:=APanel.Height;

    TBMPExportFormat.SaveToFile(APanel, tmp, FProperties, tmpWidth, tmpHeight);
  finally
    FProperties.Free;
  end;
end;

procedure TBMPOptions.FormCreate(Sender: TObject);
begin
  CBColors.ItemIndex:=0;
end;

procedure TBMPOptions.FormShow(Sender: TObject);
begin
  CBColors.ItemIndex:=0;
  Preview;
end;

procedure TBMPOptions.Preview;
var tmp : TBitmap;
begin
  if Assigned(IFormat) then
  begin
    tmp:=IFormat.Bitmap(nil);

    try
      ImageFiltered1.Picture.Graphic:=tmp;
    finally
      tmp.Free;
    end;
  end;
end;

procedure TBMPOptions.Button1Click(Sender: TObject);
var tmp : TBitmap;
begin
  tmp:=IFormat.InternalGetBitmap;
  try
    if ShowFiltersEditor(Self, tmp, IFormat.FFilters) then
       Preview;
  finally
    tmp.Free;
  end;
end;

procedure TBMPOptions.CBColorsChange(Sender: TObject);
begin
  Preview;
end;

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

