{**********************************************}
{   TeeChart Export Dialog - Inherited         }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeExport;
{$I TeeDefs.inc}

interface

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

  {$IFDEF D17}
  System.Types,
  {$ENDIF}

  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons,

  TeeExport, TeeProcs, TeCanvas, TeeStore;

type
  TTeeExportForm = class(TTeeExportFormBase)
    TabDataFormat: TTabSheet;
    CBUseSeriesFormat: TCheckBox;
    Label5: TLabel;
    SeriesValueFormat: TComboFlat;
    BFormat: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure CBUseSeriesFormatClick(Sender: TObject);
    procedure BFormatClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

    {$IFNDEF LCL}
    procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;
    {$ENDIF}
  protected
    Function CreateData:TTeeExportData; override;
    Function CreateNativeStream:TStream; override;
    Procedure DoSaveNativeToFile( Const FileName:String;
                                  IncludeData:Boolean); override;
    Function ExistData:Boolean; override;
    Procedure PrepareOnShow; override;
  public
    { Public declarations }
  end;

{ Show the Export dialog }
{ example: TeeExport(Self,Chart1); }
Procedure TeeExport(const AOwner:TComponent; const APanel:TCustomTeePanel; InitialDir:String='');

{ Show the Save dialog and save to AFormat export format }
{ example: TeeSavePanel(TGIFExportFormat,Chart1); }
Procedure TeeSavePanel(AFormat:TTeeExportFormatClass; const APanel:TCustomTeePanel);

{ starts the MAPI (eg: Outlook) application with an empty new email
  message with the attachment file "FileName" }
Procedure TeeEmailFile(Const FileName:String; Const Subject:String='TeeChart'); // Do not localize

var
  TeeExcelDataObject:TSeriesDataClass;

implementation

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

{$IFNDEF TEELITE}
{$IFDEF D16}
{$DEFINE TEEHASODS}
{$ENDIF}
{$ENDIF}

uses
  {$IFDEF TEEHASODS}
  TeeODS,
  {$ENDIF}
  TeeConst, TeEngine, Chart, TeePenDlg, TeeFormatting;

Procedure TeeExport(const AOwner:TComponent; const APanel:TCustomTeePanel; InitialDir:String='');
begin
  With TTeeExportForm.Create(AOwner) do
  try
    ExportPanel:=APanel;
    InitDir:=InitialDir;
    ShowModal;
  finally
    Free;
  end;
end;

Procedure TeeSavePanel(AFormat:TTeeExportFormatClass; const APanel:TCustomTeePanel);
begin
  TeeExportSavePanel(AFormat,APanel);
end;

Procedure TeeEmailFile(Const FileName:String; Const Subject:String='TeeChart'); // Do not localize
begin
  InternalTeeEmailFile(FileName,Subject);
end;

{ TTeeExportForm }

function TTeeExportForm.CreateData: TTeeExportData;

  Function SelectedSeries:TChartSeries;
  begin
    result:=TChartSeries(CBSeries.SelectedObject);
  end;

  function Chart:TCustomChart;
  begin
    result:=TCustomChart(ExportPanel);
  end;

  function ExcelDataClass(const OldFormat:Boolean):TSeriesDataClass;
  begin
    if OldFormat or (TeeExcelDataObject=nil) then
       result:=TSeriesDataXLS
    else
       result:=TeeExcelDataObject;
  end;

begin
  case RGText.ItemIndex of
    0: begin
         result:=TSeriesDataText.Create(Chart,SelectedSeries);

         TSeriesDataText(result).TextDelimiter:=GetSeparator;
         TSeriesDataText(result).TextQuotes:=EQuotes.Text;

         {$IFDEF D12}
         TSeriesDataText(result).FileEncoding:=SelectedEncoding;
         {$ENDIF}
       end;

    1: begin
         result:=TSeriesDataXML.Create(Chart,SelectedSeries);

         TSeriesDataXML(result).Encoding:=CBXMLEncoding.Text;

         {$IFDEF D12}
         TSeriesDataXML(result).FileEncoding:=SelectedEncoding;
         {$ENDIF}
       end;

    2: begin
         result:=TSeriesDataHTML.Create(Chart,SelectedSeries);
         {$IFDEF D12}
         TSeriesDataText(result).FileEncoding:=SelectedEncoding;
         {$ENDIF}
       end;

    3: result:=ExcelDataClass(RGExcelVers.ItemIndex=0).Create(Chart,SelectedSeries);

    4: begin
         result:=TSeriesDataJSON.Create(Chart,SelectedSeries);
         TSeriesDataJSON(result).DateTimeFormat:=TJSONDateTimeFormat(RGJsonDate.ItemIndex);
       end;
  else
    {$IFDEF TEEHASODS}
    result:=TSeriesDataODS.Create(Chart,SelectedSeries);
    {$ELSE}
    result:=nil; // just to skip a hint
    {$ENDIF}
  end;

  // Configure options
  with TSeriesData(result) do
  begin
    IncludeLabels:=CBLabels.Checked;
    IncludeIndex :=CBIndex.Checked;
    IncludeHeader:=CBHeader.Checked;
    IncludeColors:=CBColors.Checked;

    UseSeriesFormat:=CBUseSeriesFormat.Checked;

    if not UseSeriesFormat then
       ValueFormat:=LocalToDelphiFormat(SeriesValueFormat.Text);
  end;
end;

function TTeeExportForm.ExistData: Boolean;
begin
  result:=ExportPanel is TCustomAxisPanel;
end;

Procedure TTeeExportForm.DoSaveNativeToFile( Const FileName:String;
                                             IncludeData:Boolean);
begin
  case CBNativeFormat.ItemIndex of
    0: SaveChartToFile(TCustomChart(ExportPanel),FileName,IncludeData,False);
    1: SaveChartToFile(TCustomChart(ExportPanel),FileName,IncludeData,True);
    2: SaveChartToXMLFile(TCustomChart(ExportPanel),FileName,IncludeData);
  end;
end;

Procedure TTeeExportForm.PrepareOnShow;
begin
  if ExportPanel is TCustomAxisPanel then
  begin
    CBSeries.Clear;

    FillSeriesItems(CBSeries.Items,TCustomAxisPanel(ExportPanel).SeriesList);

    CBSeries.Items.Insert(0,TeeMsg_All);
    CBSeries.ItemIndex:=0;
  end
  else
  begin
    CBNativeData.Visible:=False;
    TabData.TabVisible:=False;
  end;
end;

Function TTeeExportForm.CreateNativeStream:TStream;
begin
  if ExportPanel is TCustomChart then
  begin
    result:=TMemoryStream.Create;

    case CBNativeFormat.ItemIndex of
      0: SaveChartToStream(TCustomChart(ExportPanel),result,
                           CBNativeData.Checked,False);
      1: SaveChartToStream(TCustomChart(ExportPanel),result,
                           CBNativeData.Checked,True);
      2: SaveChartToXMLStream(TCustomChart(ExportPanel),result,
                              CBNativeData.Checked);
    end;
  end
  else
    result:=inherited CreateNativeStream;
end;

{$IFNDEF LCL}
procedure TTeeExportForm.CMShowingChanged(var Message: TMessage);
begin
  inherited;
  if Assigned(Parent) then
     Parent.UpdateControlState;
end;
{$ENDIF}

procedure TTeeExportForm.FormShow(Sender: TObject);
begin
  inherited;

  TFormatEditor.AddDefaultFormats(SeriesValueFormat.Items);
  SeriesValueFormat.Enabled:=not CBUseSeriesFormat.Checked;
  BFormat.Enabled:=SeriesValueFormat.Enabled;
end;

procedure TTeeExportForm.CBUseSeriesFormatClick(Sender: TObject);
begin
  SeriesValueFormat.Enabled:=not CBUseSeriesFormat.Checked;
  BFormat.Enabled:=SeriesValueFormat.Enabled;

  TryPreviewData;
end;

procedure TTeeExportForm.BFormatClick(Sender: TObject);
begin
  SeriesValueFormat.Text:=TFormatEditor.Change(Self,SeriesValueFormat.Text);
  TryPreviewData;
end;

procedure TTeeExportForm.FormCreate(Sender: TObject);
begin
  inherited;

  {$IFDEF TEEHASODS}
  RGText.Items.Add('OpenDocument'); // Do not translate
  DataFilter:=DataFilter+'|'+TeeMsg_ODSFilter;
  {$ENDIF}
end;

end.
