{**********************************************}
{  TTeeShadow Editor Dialog                    }
{  Copyright (c) 2002-2020 by Steema Software  }
{**********************************************}
unit TeeShadowEditor;
{$I TeeDefs.inc}

interface

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

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

     {$IFDEF LCL}
     Buttons,
     {$ENDIF}
     TeeProcs, TeCanvas;

type
  TTeeShadowEditor = class(TForm)
    PanelButtons: TPanel;
    Panel2: TPanel;
    ButtonOK: TButton;
    PageControl1: TPageControl;
    TabFormat: TTabSheet;
    TabOptions: TTabSheet;
    Label2: TLabel;
    LBlur: TLabel;
    CBSmooth: TCheckBox;
    TBBlur: TTrackBar;
    CBClip: TCheckBox;
    LTransp: TLabel;
    BShadowColor: TButtonColor;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label5: TLabel;
    Edit1: TEdit;
    UDShadowSize: TUpDown;
    EVertSize: TEdit;
    UDShaVert: TUpDown;
    TBSize: TTrackBar;
    CBVisible: TCheckBox;
    TBColor: TTrackBar;
    TBTransp: TTrackBar;
    Label3: TLabel;
    CBDirection: TComboFlat;
    procedure Edit1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure EVertSizeChange(Sender: TObject);
    procedure ButtonOKClick(Sender: TObject);
    procedure CBSmoothClick(Sender: TObject);
    procedure CBVisibleClick(Sender: TObject);
    procedure TBBlurChange(Sender: TObject);
    procedure TBColorChange(Sender: TObject);
    procedure BShadowColorClick(Sender: TObject);
    procedure CBClipClick(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure TBSizeChange(Sender: TObject);
    procedure CBDirectionChange(Sender: TObject);
  private
    { Private declarations }
    IChanging    : Boolean;
    CreatingForm : Boolean;
    H,L,S        : Word;

    Function CanChange:Boolean;
    procedure SetTrackBarSize;

  protected
     Shadow       : TTeeShadow;
  public
    { Public declarations }

    {$IFDEF LINUX}
    Constructor Create(AOwner: TComponent); override;
    Destructor Destroy; override;
    {$ENDIF}

    class function InsertForm(const AShadow:TTeeShadow; const AParent:TWinControl):TTeeShadowEditor;
    Procedure RefreshControls(const AShadow:TTeeShadow);
  end;

Function EditTeeShadow(AOwner:TComponent; AShadow:TTeeShadow):Boolean;

implementation

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

uses
  TeePenDlg, TeeFilters;

Function EditTeeShadow(AOwner:TComponent;
                       AShadow:TTeeShadow):Boolean;
begin
  with TeeCreateForm(TTeeShadowEditor,AOwner) as TTeeShadowEditor do
  try
    Shadow:=AShadow;

    PanelButtons.Visible:=True;
    
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

class function TTeeShadowEditor.InsertForm(const AShadow:TTeeShadow; const AParent:TWinControl):TTeeShadowEditor;
begin
  result:=TTeeShadowEditor.Create(AParent.Owner);
  result.RefreshControls(AShadow);
  result.Align:=alClient;
  AddFormTo(result,AParent);
end;

// TTeeShadowEditor

{$IFDEF LINUX}
Constructor TTeeShadowEditor.Create(AOwner:TComponent);
begin
  inherited;
end;

Destructor TTeeShadowEditor.Destroy;
begin
  inherited;
end;
{$ENDIF}

Procedure TTeeShadowEditor.RefreshControls(const AShadow:TTeeShadow);
begin
  Shadow:=AShadow;

  with Shadow do
  begin
    UDShadowSize.Position:=HorizSize;
    UDShaVert.Position:=VertSize;

    SetTrackBarSize;

    TBTransp.Position:=Transparency;
    CBSmooth.Checked:=Smooth;
    CBVisible.Checked:=Visible;
    Edit1.Enabled:=CBVisible.Checked;
    EVertSize.Enabled:=CBVisible.Checked;

    TBBlur.Position:=SmoothBlur;
    LBlur.Caption:=TeeStr(SmoothBlur);
    TBBlur.Enabled:=Smooth;

    ColorToHLS(Color,H,L,S);
    TBColor.Position:=L;

    CBClip.Checked:=Clip;
    CBClip.Enabled:=Smooth;

    CBDirection.ItemIndex:=Ord(Shadow.Direction);
  end;

  BShadowColor.LinkProperty(Shadow,'Color');
end;

Function TTeeShadowEditor.CanChange:Boolean;
begin
  result:=(not CreatingForm) and Assigned(Shadow);
end;

procedure TTeeShadowEditor.SetTrackBarSize;
var tmp : Integer;
begin
  tmp:=Abs(Shadow.HorizSize);

  if tmp>TBSize.Max then
  begin
    TBSize.Min:= -2*tmp;
    TBSize.Max:=  2*tmp;
    TBSize.Frequency:=(TBSize.Max-TBSize.Min) div 12;
  end;

  IChanging:=True;
  TBSize.Position:=Shadow.HorizSize;
  IChanging:=False;
end;

procedure TTeeShadowEditor.Edit1Change(Sender: TObject);
begin
  if CanChange then
  begin
    Shadow.HorizSize:=UDShadowSize.Position;
    SetTrackBarSize;
  end;
end;

procedure TTeeShadowEditor.FormCreate(Sender: TObject);
begin
  CreatingForm:=True;
  PageControl1.ActivePage:=TabFormat;
end;

procedure TTeeShadowEditor.FormShow(Sender: TObject);
begin
  if Tag<>0 then
     Shadow:=TTeeShadow(Tag);

  if Assigned(Shadow) then
     RefreshControls(Shadow);

  TeeTranslateControl(Self);
  CreatingForm:=False;
end;

procedure TTeeShadowEditor.EVertSizeChange(Sender: TObject);
begin
  if CanChange then
     Shadow.VertSize:=UDShaVert.Position;
end;

procedure TTeeShadowEditor.ButtonOKClick(Sender: TObject);
begin
  Close;
end;

procedure TTeeShadowEditor.CBSmoothClick(Sender: TObject);
begin
  Shadow.Smooth:=CBSmooth.Checked;
  TBBlur.Enabled:=Shadow.Smooth;
  CBClip.Enabled:=Shadow.Smooth;
end;

procedure TTeeShadowEditor.CBVisibleClick(Sender: TObject);
begin
  Shadow.Visible:=CBVisible.Checked;
  Edit1.Enabled:=CBVisible.Checked;
  EVertSize.Enabled:=CBVisible.Checked;
end;

procedure TTeeShadowEditor.TBBlurChange(Sender: TObject);
begin
  if CanChange then
  begin
    Shadow.SmoothBlur:=TBBlur.Position;
    LBlur.Caption:=TeeStr(Shadow.SmoothBlur);
  end;
end;

procedure TTeeShadowEditor.TBColorChange(Sender: TObject);
begin
  if CanChange then
  begin
    Shadow.Color:=HLSToColor(H,TBColor.Position,S);
    BShadowColor.Invalidate;
  end;
end;

procedure TTeeShadowEditor.BShadowColorClick(Sender: TObject);
begin
  ColorToHLS(Shadow.Color,H,L,S);
end;

procedure TTeeShadowEditor.CBClipClick(Sender: TObject);
begin
  Shadow.Clip:=CBClip.Checked;
end;

procedure TTeeShadowEditor.TBTranspChange(Sender: TObject);
begin
  if CanChange then
     Shadow.Transparency:=TBTransp.Position;
end;

procedure TTeeShadowEditor.TBSizeChange(Sender: TObject);
begin
  if (not IChanging) and CanChange then
  begin
    Shadow.Size:=TBSize.Position;

    UDShadowSize.Position:=Shadow.HorizSize;
    UDShaVert.Position:=Shadow.VertSize;
  end;
end;

procedure TTeeShadowEditor.CBDirectionChange(Sender: TObject);
begin
  if CanChange then
     Shadow.Direction:=TShadowDirection(CBDirection.ItemIndex);
end;

end.
