{********************************************}
{   TeeChart Pro Charting Library            }
{   Frame Tool (Decorating Frames)           }
{                                            }
{ Copyright (c) 2006-2020 by Steema Software }
{ All Rights Reserved                        }
{********************************************}
unit TeeFrameToolEditor;
{$I TeeDefs.inc}

interface

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

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

  {$IFDEF D6}
  Types,
  {$ENDIF}

  TeeProcs, TeeFilters, TeEngine, Chart, TeCanvas, TeePenDlg, TeeFrameTool,
  TeeFiltersEditor;

type
  TFrameToolEditor = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    SBSize: TScrollBar;
    BBrowse: TButton;
    CBResize: TCheckBox;
    TabPaspartou: TTabSheet;
    LSize: TLabel;
    CBStyle: TComboFlat;
    Label3: TLabel;
    TabSheet3: TTabSheet;
    CBChangeColor: TCheckBox;
    GroupColors: TGroupBox;
    SBRed: TScrollBar;
    SBGreen: TScrollBar;
    SBBlue: TScrollBar;
    LRed: TLabel;
    LGreen: TLabel;
    LBlue: TLabel;
    Panel1: TPanel;
    Label2: TLabel;
    SBPasSize: TScrollBar;
    LPasSize: TLabel;
    PagePaspartou: TPageControl;
    TabBorder: TTabSheet;
    TabPattern: TTabSheet;
    TabFilters: TTabSheet;
    procedure FormShow(Sender: TObject);
    procedure SBSizeChange(Sender: TObject);
    procedure BBrowseClick(Sender: TObject);
    procedure CBResizeClick(Sender: TObject);
    procedure SBPasSizeChange(Sender: TObject);
    procedure CBStyleChange(Sender: TObject);
    procedure SBRedChange(Sender: TObject);
    procedure CBChangeColorClick(Sender: TObject);
    procedure SBGreenChange(Sender: TObject);
    procedure SBBlueChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure PagePaspartouChange(Sender: TObject);
  private
    { Private declarations }
    Tool : TFrameTool;

    IFilters : TFiltersEditor;

    procedure ChangedFilters(Sender: TObject);
    procedure CheckColorFilter;
    function ColorFilter:TColorFilter;
  public
    { Public declarations }
  end;

implementation

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

uses
  ImageBar, TeeBrushDlg, TeeProCo;

procedure TFrameToolEditor.FormShow(Sender: TObject);
begin
  Tool:=TFrameTool(Tag);

  if Assigned(Tool) then
  with Tool do
  begin
    CBStyle.ItemIndex:=Style;
    BBrowse.Enabled:=Style=0;

    SBSize.Position:=Size;
    LSize.Caption:=IntToStr(Size);

    SBPasSize.Position:=Paspartou;
    LPasSize.Caption:=IntToStr(Paspartou);

    CheckColorFilter;
  end;
end;

procedure TFrameToolEditor.SBSizeChange(Sender: TObject);
begin
  Tool.Size:=SBSize.Position;
  LSize.Caption:=IntToStr(Tool.Size);
end;

procedure TFrameToolEditor.BBrowseClick(Sender: TObject);
var tmp : String;
begin
  tmp:=TeeGetPictureFileName(Self);

  if tmp<>'' then
  begin
    Tool.Frame.LoadFromFile(tmp);
    Tool.Style:=0;
    Tool.Repaint;
  end;
end;

procedure TFrameToolEditor.CBResizeClick(Sender: TObject);
begin
  Tool.ResizeChart:=CBResize.Checked;
end;

procedure TFrameToolEditor.SBPasSizeChange(Sender: TObject);
begin
  Tool.Paspartou:=SBPasSize.Position;
  LPasSize.Caption:=IntToStr(Tool.Paspartou);
end;

procedure TFrameToolEditor.CBStyleChange(Sender: TObject);
begin
  BBrowse.Enabled:=CBStyle.ItemIndex=0;
  Tool.Style:=CBStyle.ItemIndex;
end;

procedure TFrameToolEditor.SBRedChange(Sender: TObject);
begin
  ColorFilter.Red:=SBRed.Position;
  LRed.Caption:=IntToStr(SBRed.Position);
  Tool.Repaint;
end;

procedure TFrameToolEditor.CBChangeColorClick(Sender: TObject);
begin
  SBRed.Enabled:=CBChangeColor.Checked;
  SBGreen.Enabled:=CBChangeColor.Checked;
  SBBlue.Enabled:=CBChangeColor.Checked;

  if CBChangeColor.Checked and (ColorFilter=nil) then
  begin
    Tool.Frame.Filters.Add(TColorFilter);
    CheckColorFilter;
  end;

  if ColorFilter<>nil then
  begin
    ColorFilter.Enabled:=CBChangeColor.Checked;
    Tool.Repaint;
  end;
end;

procedure TFrameToolEditor.SBGreenChange(Sender: TObject);
begin
  ColorFilter.Green:=SBGreen.Position;
  LGreen.Caption:=IntToStr(SBGreen.Position);
  Tool.Repaint;
end;

procedure TFrameToolEditor.SBBlueChange(Sender: TObject);
begin
  ColorFilter.Blue:=SBBlue.Position;
  LBlue.Caption:=IntToStr(SBBlue.Position);
  Tool.Repaint;
end;

function TFrameToolEditor.ColorFilter: TColorFilter;
var t : Integer;
begin
  result:=nil;

  with Tool.Frame.Filters do
  for t:=0 to Count-1 do
      if Items[t] is TColorFilter then
      begin
        result:=Items[t] as TColorFilter;
        break;
      end;
end;

procedure TFrameToolEditor.CheckColorFilter;

  procedure SetColors(R,G,B:Integer);
  begin
    SBRed.Position:=R;
    LRed.Caption:=IntToStr(R);

    SBGreen.Position:=G;
    LGreen.Caption:=IntToStr(G);

    SBBlue.Position:=B;
    LBlue.Caption:=IntToStr(B);
  end;

var tmp : TColorFilter;
begin
  tmp:=ColorFilter;

  CBChangeColor.Checked:=Assigned(tmp) and tmp.Enabled;

  SBRed.Enabled:=CBChangeColor.Checked;
  SBGreen.Enabled:=CBChangeColor.Checked;
  SBBlue.Enabled:=CBChangeColor.Checked;

  if CBChangeColor.Checked then
     SetColors(tmp.Red,tmp.Green,tmp.Blue)
  else
     SetColors(0,0,0);
end;

procedure TFrameToolEditor.ChangedFilters(Sender: TObject);
begin
  Tool.Repaint;
  CheckColorFilter;
end;

procedure TFrameToolEditor.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePage=TabPaspartou then
     PagePaspartouChange(Self)
  else
  if (PageControl1.ActivePage=TabFilters) and (TabFilters.ControlCount=0) then
  begin
    IFilters:=TFiltersEditor.InsertForm(Tool.Frame,TabFilters);
    IFilters.Changed:=ChangedFilters;
  end;
end;

procedure TFrameToolEditor.PagePaspartouChange(Sender: TObject);
begin
  if (PagePaspartou.ActivePage=TabBorder) and (TabBorder.ControlCount=0) then
     TPenDialog.InsertForm(Tool.Pen,TabBorder)
  else
  if (PagePaspartou.ActivePage=TabPattern) and (TabPattern.ControlCount=0) then
     TBrushDialog.AddDialog(Tool.Brush,TabPattern);
end;

initialization
  RegisterClass(TFrameToolEditor);
end.
