{**********************************************}
{    TeeChart Extended Series Editors          }
{   Copyright (c) 1996-2020 by Steema Software }
{**********************************************}
unit TeeEditPro;
{$I TeeDefs.inc}

interface

Uses {$IFNDEF LINUX}
     Windows,
     {$ENDIF}

     Controls, Graphics, Classes,
     TeCanvas, TeEngine, Chart;

type
  // Custom Graphic class to support loading *.tee files into TImage components, etc.
  TChartImage = class(TGraphic)
  private
    FChart  : TChart;
    FCustom : Boolean;
    function GetChart: TChart;
    procedure SetChart(const Value: TChart);
  protected
    function GetEmpty: Boolean; override;
    function GetHeight: Integer; override;

    {$IFDEF MSWINDOWS}
    function GetPalette: HPALETTE; override;
    {$ENDIF}

    function GetWidth: Integer; override;
    procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
    procedure ReadData(Stream: TStream); override;
    procedure SetHeight(Value: Integer); override;
    procedure SetTransparent(Value: Boolean); override;
    procedure SetWidth(Value: Integer); override;
    procedure WriteData(Stream: TStream); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Clear; {$IFDEF LCL}override;{$ENDIF}
    procedure LoadFromStream(Stream: TStream); override;
    procedure SaveToFile(const Filename: String); override;
    procedure SaveToStream(Stream: TStream); override;
    {$IFNDEF LCL}
    procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
      APalette: HPALETTE); override;
    procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
      var APalette: HPALETTE); override;
    {$ENDIF}
    procedure Assign(Source: TPersistent); override;
  published
    property Chart:TChart read GetChart write SetChart;
  end;

{ Show the user the dialog associated to the Series }
Procedure EditOneSeries(AOwner:TControl; ASeries:TChartSeries);

{ Show the user a "Save to..." dialog to save it to disk. }
Function SaveChartDialog(AChart:TCustomChart):String;

implementation

{ This unit should be "used" by your applications if showing the
  Chart Editor with "Extended" Series and Functions. }

uses SysUtils, Forms, Dialogs,

     EditChar, TeePenDlg, TeeEdiSeri, TeExport, TeeExport, TeeStore, TeeConst,
     TeeProCo,

     StatChar, TeeCandlEdi, TeeVolEd, TeeSurfEdit, TeePolarEditor,
     TeeErrBarEd, CurvFitt, TeePolarGridEditor,

     TeeContourEdit,
     TeeBezie, TeePo3DEdit, TeeProcs,
     TeeCount, TeeCumu, TeeDonutEdit,

     TeeCursorEdit, TeeTriSurfEdit,

     MyPoint, Bar3D, BigCandl, ImaPoint, ImageBar, TeeImaEd, TeeRose,
     TeeBoxPlot, TeeHistoEdit, TeeTools, TeeColorLineEditor, TeeColorBandEdit,
     TeeAxisArrowEdit, TeeDrawLineEditor, TeeImageToolEdit, TeeAnnToolEdit,
     TeeBarJoinEditor, TeeLinePointEditor, TeeRotateToolEditor,
     TeeHighLowEdit, TeeClockEditor, TeeBoxPlotEditor, TeeWindRoseEditor,
     TeeColorGridEditor, TeeWaterFallEdit, TeeSmith, TeePyramid, TeePyramidEdit,
	 
	 TeeViolinEdit, TeeKDEEdit,

     TeeNearestToolEditor, TeeSmithEdit, TeeCalendarEditor, TeePageNumToolEditor,
     TeeMapSeries, TeeMapSeriesEdit, TeeVolumePipeEditor,
     TeeMarksTipToolEdit, TeeDragMarksEdit, TeeDragPoint,
     TeeFunnel, TeeFunnelEditor, TeeADXFuncEditor, TeeMovAveFuncEdit,
     TeeSpline, TeeSmoothFuncEdit, TeeTransposeToolEditor,
     TeeTransposeSeriesEditor,
     TeeCompressOHLC, TeeMACDFuncEdit, TeeMoneyFlowFuncEdit, TeeRVIFuncEditor,
     TeeAlligatorFuncEditor, TeeBollingerEditor, TeeRMSFuncEdit,
     TeeExpAveFuncEdit, TeeGanttToolEditor, TeeGridBandToolEdit, TeeCLVFunction,
     TeeOBVFunction, TeeCCIFunction, TeePVOFunction, TeeDownSampling,
     TeePointFigure, TeeGaugeEditor, TeeTowerEdit, TeeVectorEdit,
     TeeSeriesAnimEdit, TeePieToolEditor, TeeSeriesBandToolEditor, TeeExtraLegendToolEditor,
     TeeSelectorToolEditor, TeeLighting, TeeLegendScrollBar, TeeSurfaceToolEditor,
     TeeDataTableEditor, TeeScroB, TeeIsoSurface, TeeSeriesStats, TeeDragPointEditor,
     TeeTransposeSeries, TeeHistogram, TeeSubsetEditor, TeeAnimations,
     TeeExpandAnimationEdit, TeeEditAnimations,
     TeeCustomLegendToolEditor,
     TeeSeriesStatsEditor, TeeSeriesRegionEditor, TeeLegendPaletteEditor,
     TeeFibonacciEditor, TeeLegendScrollBarEditor, TeeAxisBreaksEditor,
     TeeSubChartEditor,

     {$IFNDEF LCL}
     TeeVideo,
     TeeVideoPlayer,
     TeeExcelSource,
     {$ENDIF}

     TeeGally, TeeGalleryPanel, CandleCh, Series, TeeSurfa,

     TeeAntiAlias, TeeHighLowLine, TeeSeriesRegion, TeeFrameToolEditor, TeeSAR,
     TeeLegendPalette, TeePolarGrid, TeeBannerToolEditor, TeeSubChart,
     TeeChartBook, TeeMagnifyTool, TeeFaderTool, TeeCurveFittingEditor,
     TeeAxisScrollEdit, TeeOrgSeries, TeeTagCloud, TeeRenkoSeries,
     TeeKagiSeries, TeeNumericGauge, TeeLinearGauge, TeeLinkToolEditor,
     TeeFibonacci, TeeCircularGauge, TeeCircularGaugeEditor, TeeTagCloudEditor,
     TeeHighLowLineEditor, TeeKnobGaugeEditor,

     // v9
     TeeArrowToolEditor, TeeTernary, TeeKnobGauge, TeeTernaryEdit, TeeRoseEditor,
     TeeTreeMapSeries, TeeTreeMapSeriesEditor, TeeAxisBreaks, TeeErrorPoint,
     TeeErrorPointEdit, TeeErrorPoint3DEdit,

     TeeClusteringToolEditor,
     
     TeeScrollPagerEditor, TeeKagiSeriesEditor, TeeRenkoSeriesEditor, TeeEquiVolume,
     TeeEquiVolEdi, TeeToolsGalleryDemos, TeeAnimationsGalleryDemos,

     {$IFDEF MSWINDOWS}
     TeeGDIPlusEditor, TeeText3D,
     {$ENDIF}

     // v10
     TeeThemeEditor, TeeDoubleHorizBarEditor, TeePolarContourEditor,
     TeeBubbleCloudEdit, TeeAxisScrollBarEdit, TeeComboEditor,
     TeeRepaintMonitorEditor, TeeChartLayout,
     TeeDownSamplingEditor, TeeActivityGaugeEdit, TeeEqualizerEdit;


{$R TeeProBm.res}

type
  TTeeExportFormatAccess=class(TTeeExportFormat);

{ This method shows the "Save As..." dialog to save a Chart into a
  native *.tee file or a picture file, depending if exporting formats
  are registered (units like TeeBMP used in the application)
}
Function SaveChartDialog(AChart:TCustomChart):String;
var tmpDialog : TSaveDialog;
    tmp       : TStringList;
    t         : Integer;
begin
  result:='';

  tmpDialog:=TSaveDialog.Create(nil);
  With tmpDialog do
  try
    Filter:=TeeMsg_TeeFiles+' (*.'+TeeMsg_TeeExtension+')'; // Do not localize

    Filter:=Filter+'|*.'+TeeMsg_TeeExtension; // Do not localize
    Options:=[ofHideReadOnly,ofOverwritePrompt];

    tmp:=TStringList.Create;
    try
      TeeFillPictureDialog(tmpDialog,AChart,tmp);

      DefaultExt:=TeeMsg_TeeExtension;
      if Execute then
      begin
        if FilterIndex=1 then // native save to *.tee file
           SaveChartToFile(AChart,FileName)
        else
        { search the selected exporting picture format and save to file }
        for t:=0 to tmp.Count-1 do
        with TTeeExportFormatAccess(tmp.Objects[t]) do
        if {$IFDEF LCL}TTeeExportFormatAccess(tmp.Objects[t]).{$ENDIF}WantsFilterIndex(tmpDialog.FilterIndex) then
        begin
          SaveToFile(FileName);
          break;
        end;

        result:=FileName;
      end;
    finally
      with tmp do
      for t:=0 to Count-1 do
          TTeeExportFormat(Objects[t]).Free;

      tmp.Free;
    end;
  finally
    Free;
  end;
end;

{ This method shows the Series editor dialog as a stand-alone window,
  just to edit one series. }
Procedure EditOneSeries(AOwner:TControl; ASeries:TChartSeries);
begin
  with TeeCreateForm(SeriesEditorForm(ASeries),AOwner) do
  try
    BorderIcons:=[biSystemMenu];

    {$IFNDEF LCL}
    Scaled:=False;
    {$ENDIF}
    
    Caption:=Format(TeeMsg_Editing,[ASeries.Name]);
    Tag:=ObjectToTag(ASeries);

    {$IFDEF MSWINDOWS}
    Height:=Height+GetSystemMetrics(SM_CYDLGFRAME)+GetSystemMetrics(SM_CYCAPTION);
    {$ENDIF}

    ShowModal;
  finally
    Free;
  end;
end;

{ TChartImage }

procedure TChartImage.Assign(Source: TPersistent);
begin
  if Assigned(Source) then
  if (Source is TChartImage) then
  begin
     if Assigned(TChartImage(Source).FChart) then
     Chart.Assign(TChartImage(Source).FChart)
  end
  else
     inherited;
end;

procedure TChartImage.Clear;
begin
  if FCustom then FChart.Free;
end;

constructor TChartImage.Create;
begin
  inherited;
  FCustom:=True;
end;

destructor TChartImage.Destroy;
begin
  if FCustom then FChart.Free;
  inherited;
end;

procedure TChartImage.Draw(ACanvas: TCanvas; const Rect: TRect);
begin
  with ACanvas do
  begin
    Brush.Style:=bsSolid;
    Brush.Color:=clWhite;
    FillRect(Rect); // TV52010023
  end;

  Chart.Draw(ACanvas,Rect);
end;

function TChartImage.GetChart: TChart;
begin
  if not Assigned(FChart) then
  begin
    FChart:=TChart.Create(nil);
    FChart.BufferedDisplay:=False;
    FCustom:=True;
  end;

  result:=FChart
end;

function TChartImage.GetEmpty: Boolean;
begin
  result:=not Assigned(FChart);
end;

function TChartImage.GetHeight: Integer;
begin
  result:=Chart.Height;
end;

{$IFDEF MSWINDOWS}
function TChartImage.GetPalette: HPALETTE;
begin
  result:=0;
end;
{$ENDIF}

function TChartImage.GetWidth: Integer;
begin
  result:=Chart.Width;
end;

{$IFNDEF LCL}
procedure TChartImage.LoadFromClipboardFormat(AFormat: Word;
  AData: THandle; APalette: HPALETTE);
begin
end;

procedure TChartImage.SaveToClipboardFormat(var AFormat: Word;
  var AData: THandle; var APalette: HPALETTE);
begin
end;
{$ENDIF}

procedure TChartImage.LoadFromStream(Stream: TStream);
begin
  ReadData(Stream);
end;

procedure TChartImage.ReadData(Stream: TStream);
begin
  Chart;
  LoadChartFromStream(TCustomChart(FChart),Stream);

  if Assigned(FChart) then
     FChart.BufferedDisplay:=False;
end;

procedure TChartImage.SaveToFile(const Filename: String);
begin
  SaveChartToFile(Chart,FileName);
end;

procedure TChartImage.SaveToStream(Stream: TStream);
begin
  WriteData(Stream);
end;

procedure TChartImage.SetChart(const Value: TChart);
begin
  FChart:=Value;
  FCustom:=False;
  Changed(Self);
end;

procedure TChartImage.SetHeight(Value: Integer);
begin
  Chart.Height:=Value;
end;

procedure TChartImage.SetTransparent(Value: Boolean);
begin
  Chart.Color:=clNone;
end;

procedure TChartImage.SetWidth(Value: Integer);
begin
  Chart.Width:=Value;
end;

procedure TChartImage.WriteData(Stream: TStream);
begin
  SaveChartToStream(Chart,Stream);
end;

{$IFNDEF TEEOCX}
type
  TTeeProTemplate=class(TTeeTemplate)
  public
    procedure Add; override;
  end;

{ TTeeProTemplate }

procedure TTeeProTemplate.Add;
var c : TGalleryChart;
begin
  inherited;

  c:=NewTemplate(TeeMsg_TemplateFinancialStocks);
  c.AddSeries(TCandleSeries).FillSampleValues(40);
  c.AddSeries(TVolumeSeries).FillSampleValues(40);
  c.AddSeries(TFastLineSeries).DataSource:=c[0];
  c.LeftAxis.EndPosition:=70;
  c[2].FunctionType:=TMovingAverageFunction.Create(Owner);
  c[2].FunctionType.Period:=6;
  c[1].VertAxis:=aRightAxis;
  c.RightAxis.StartPosition:=70;

  c[0].Title:=TeeMsg_GalleryCandle;
  c[1].Title:=TeeMsg_GalleryVolume;
  c[2].Title:=TeeMsg_FunctionMovingAverage;

  c:=NewTemplate(TeeMsg_TemplateXYZData);
  c.AddSeries(TContourSeries);
  c.AddSeries(TSurfaceSeries).FillSampleValues(20);
  c[0].DataSource:=c[1];
  TContourSeries(c[0]).YPosition:=c[1].MandatoryValueList.MinValue+0.1;
  TContourSeries(c[0]).Brush.Style:=bsSolid;
end;

initialization
  TeeTemplateClass:=TTeeProTemplate;
  TPicture.RegisterFileFormat(TeeMsg_TeeExtension,'TeeChart',TChartImage); // Do not localize
finalization
  TPicture.UnRegisterGraphicClass(TChartImage);
  TeeTemplateClass:=TTeeTemplate;
{$ENDIF}
end.
