{**********************************************}
{  TCustomChart (or derived) Editor Dialog     }
{  Copyright (c) 1996-2020 by Steema Software  }
{**********************************************}
unit TeeEdiPane;
{$I TeeDefs.inc}

interface

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

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

     TeeProcs, TeeEdiGrad, TeCanvas, TeePenDlg, TeeBackImage, TeeShadowEditor;

type
  TFormTeePanel = class(TForm)
    PageControl1: TPageControl;
    TabBack: TTabSheet;
    TabBorders: TTabSheet;
    TabGradient: TTabSheet;
    BPanelColor: TButtonColor;
    TabShadow: TTabSheet;
    CBColorDef: TCheckBox;
    TabImage: TTabSheet;
    TabMargins: TTabSheet;
    GBMargins: TGroupBox;
    Label5: TLabel;
    SETopMa: TEdit;
    SELeftMa: TEdit;
    SEBotMa: TEdit;
    SERightMa: TEdit;
    UDTopMa: TUpDown;
    UDRightMa: TUpDown;
    UDLeftMa: TUpDown;
    UDBotMa: TUpDown;
    CBMarUnits: TComboFlat;
    TabEmboss: TTabSheet;
    PanelBackImage: TPanel;
    CBImageInside: TCheckBox;
    PageControl2: TPageControl;
    TabBorder: TTabSheet;
    TabSheet2: TTabSheet;
    L19: TLabel;
    L2: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SEPanelBor: TEdit;
    SEPanelWi: TEdit;
    UDPanelWi: TUpDown;
    UDPanelBor: TUpDown;
    CBInner: TComboFlat;
    CBOuter: TComboFlat;
    Panel1: TPanel;
    Label1: TLabel;
    TBRound: TTrackBar;
    LRound: TLabel;
    Button1: TButton;
    procedure SEPanelWiChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CBImageInsideClick(Sender: TObject);
    procedure SEPanelBorChange(Sender: TObject);
    procedure BPanelColorClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBColorDefClick(Sender: TObject);
    procedure CBInnerChange(Sender: TObject);
    procedure CBOuterChange(Sender: TObject);
    procedure CBMarUnitsChange(Sender: TObject);
    procedure SERightMaChange(Sender: TObject);
    procedure SETopMaChange(Sender: TObject);
    procedure SEBotMaChange(Sender: TObject);
    procedure SELeftMaChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure TBRoundChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Changing : Boolean;

    FEmbossEditor   : TTeeShadowEditor;
    FGradientEditor : TTeeGradientEditor;
    FImageEditor    : TBackImageEditor;
    FShadowEditor   : TTeeShadowEditor;
    IBorderEditor   : TPenDialog;

    procedure AdjustMarginMinMax;
    Function ChangeMargin(UpDown:TUpDown; APos,OtherSide:Integer):Integer;
    Procedure CheckColorDef;
  public
    { Public declarations }
    ThePanel : TCustomTeePanelExtended;

    Constructor CreatePanel(Owner:TComponent; APanel:TCustomTeePanelExtended);
    class function InsertAt(AOwner:TComponent; AParent:TWinControl;
                            APanel:TCustomTeePanelExtended):TFormTeePanel;
    procedure RefreshPanel(APanel:TCustomTeePanelExtended);
  end;

implementation

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

Uses
  TeeBrushDlg, TeeConst, TeeFiltersEditor;

class function TFormTeePanel.InsertAt(AOwner:TComponent; AParent:TWinControl;
                                      APanel:TCustomTeePanelExtended):TFormTeePanel;
begin
  result:=TFormTeePanel.CreatePanel(AOwner,APanel);
  result.Align:=alClient;
  TeeTranslateControl(result);
  AddFormTo(result,AParent);
end;

{ TFormTeePanel }
Constructor TFormTeePanel.CreatePanel(Owner:TComponent; APanel:TCustomTeePanelExtended);
begin
  inherited Create(Owner);
  ThePanel:=APanel;
  Changing:=False;
  PageControl1.ActivePage:=TabBack;
end;

procedure TFormTeePanel.SEPanelWiChange(Sender: TObject);
begin
  if Showing then
  With ThePanel do
  if BevelWidth<>UDPanelWi.Position then
     BevelWidth:=UDPanelWi.Position;
end;

procedure TFormTeePanel.RefreshPanel(APanel:TCustomTeePanelExtended);
begin
  ThePanel:=APanel;

  with ThePanel do
  begin
    CBInner.ItemIndex      :=Ord(BevelInner);
    CBOuter.ItemIndex      :=Ord(BevelOuter);

    TBRound.Position       :=BorderRound;
    LRound.Caption:=IntToStr(ThePanel.BorderRound);

    UDPanelWi.Position     :=BevelWidth;
    UDPanelBor.Position    :=BorderWidth;
    CBImageInside.Checked  :=BackImage.Inside;

    UDTopMa.Position       :=MarginTop;
    UDLeftMa.Position      :=MarginLeft;
    UDBotMa.Position       :=MarginBottom;
    UDRightMa.Position     :=MarginRight;
    CBMarUnits.ItemIndex   :=Ord(MarginUnits);

    AdjustMarginMinMax;

    CheckColorDef;
  end;

  BPanelColor.LinkProperty(ThePanel,'Color'); // Do not localize

  if Assigned(FGradientEditor) then
     FGradientEditor.RefreshGradient(ThePanel.Gradient);

  if Assigned(FImageEditor) then
     FImageEditor.RefreshControls(ThePanel.BackImage);

  if Assigned(FShadowEditor) then
     FShadowEditor.RefreshControls(ThePanel.Shadow);

  if Assigned(IBorderEditor) then
     IBorderEditor.RefreshPen(ThePanel.Border);
end;

procedure TFormTeePanel.FormShow(Sender: TObject);
begin
  if Assigned(ThePanel) then
  begin
    RefreshPanel(ThePanel);

    if Assigned(FGradientEditor) then
       FGradientEditor.Visible:=True;

    PageControl1.ActivePage:=TabBack;
  end;
end;

procedure TFormTeePanel.CBImageInsideClick(Sender: TObject);
begin
  ThePanel.BackImage.Inside:=CBImageInside.Checked;
end;

procedure TFormTeePanel.SEPanelBorChange(Sender: TObject);
begin
  if Showing then
  With ThePanel do
  if BorderWidth<>UDPanelBor.Position then BorderWidth:=UDPanelBor.Position;
end;

procedure TFormTeePanel.BPanelColorClick(Sender: TObject);
begin
  ThePanel.Repaint;
  CheckColorDef;
end;

procedure TFormTeePanel.FormCreate(Sender: TObject);
begin
  Align:=alClient;
end;

Procedure TFormTeePanel.CheckColorDef;
begin
  Changing:=True;
  CBColorDef.Checked:=ThePanel.Color=clBtnFace;
  Changing:=False;
  CBColorDef.Enabled:=not CBColorDef.Checked;
end;

procedure TFormTeePanel.CBColorDefClick(Sender: TObject);
begin
  if not Changing then
  begin
    ThePanel.Color:=clBtnFace; // StyleServices.GetSystemColor( clBtnFace )
    CheckColorDef;
    BPanelColor.Invalidate;
  end;
end;

procedure TFormTeePanel.CBInnerChange(Sender: TObject);
begin
  if ThePanel.BevelInner<>TPanelBevel(CBInner.ItemIndex) then
     ThePanel.BevelInner:=TPanelBevel(CBInner.ItemIndex);
end;

procedure TFormTeePanel.CBOuterChange(Sender: TObject);
begin
  if ThePanel.BevelOuter<>TPanelBevel(CBOuter.ItemIndex) then
     ThePanel.BevelOuter:=TPanelBevel(CBOuter.ItemIndex);
end;

procedure TFormTeePanel.AdjustMarginMinMax;

  Procedure SetMinMax(Up:TUpDown);
  begin
    if ThePanel.MarginUnits=muPixels then
    begin
      Up.Min:=-2000;
      Up.Max:=2000;
    end
    else
    begin
      Up.Min:=0;
      Up.Max:=100;
    end;
  end;

begin
  SetMinMax(UDTopMa);
  SetMinMax(UDLeftMa);
  SetMinMax(UDRightMa);
  SetMinMax(UDBotMa);
end;

procedure TFormTeePanel.CBMarUnitsChange(Sender: TObject);
begin
  ThePanel.MarginUnits:=TTeeUnits(CBMarUnits.ItemIndex);
  AdjustMarginMinMax;
end;

procedure TFormTeePanel.SERightMaChange(Sender: TObject);
begin
  if Showing then
  with ThePanel do
       MarginRight:=ChangeMargin(UDRightMa,MarginRight,MarginLeft);
end;

Function TFormTeePanel.ChangeMargin(UpDown:TUpDown; APos,OtherSide:Integer):Integer;
begin
  result:=APos;

  if Showing then
  with UpDown do
  if (ThePanel.MarginUnits=muPixels) or (Position+OtherSide<100) then
     result:=Position
  else
     Position:=APos;
end;

procedure TFormTeePanel.SETopMaChange(Sender: TObject);
begin
  if Showing then
  with ThePanel do
       MarginTop:=ChangeMargin(UDTopMa,MarginTop,MarginBottom);
end;

procedure TFormTeePanel.SEBotMaChange(Sender: TObject);
begin
  if Showing then
  with ThePanel do
       MarginBottom:=ChangeMargin(UDBotMa,MarginBottom,MarginTop);
end;

procedure TFormTeePanel.SELeftMaChange(Sender: TObject);
begin
  if Showing then
  with ThePanel do
       MarginLeft:=ChangeMargin(UDLeftMa,MarginLeft,MarginRight);
end;

procedure TFormTeePanel.PageControl1Change(Sender: TObject);
begin
   if (PageControl1.ActivePage=TabGradient) and not Assigned(FGradientEditor) then
      FGradientEditor:=TTeeGradientEditor.InsertForm(ThePanel.Gradient,TabGradient)
   else
   if (PageControl1.ActivePage=TabShadow) and not Assigned(FShadowEditor) then
      FShadowEditor:=TTeeShadowEditor.InsertForm(ThePanel.Shadow,TabShadow)
   else
   if (PageControl1.ActivePage=TabEmboss) and not Assigned(FEmbossEditor) then
      FEmbossEditor:=TTeeShadowEditor.InsertForm(ThePanel.Emboss,TabEmboss)
   else
   if (PageControl1.ActivePage=TabImage) and not Assigned(FImageEditor) then
   begin
     FImageEditor:=TBackImageEditor.InsertForm(TabImage,ThePanel.BackImage);
     FImageEditor.Top:=PanelBackImage.Top + PanelBackImage.Height+1;
   end
   else
   if (PageControl1.ActivePage=TabBorders) and not Assigned(IBorderEditor) then
      IBorderEditor:=TPenDialog.InsertForm(ThePanel.Border, TabBorder);
end;

procedure TFormTeePanel.TBRoundChange(Sender: TObject);
begin
  if Showing then
  begin
    ThePanel.BorderRound:=TBRound.Position;
    LRound.Caption:=IntToStr(ThePanel.BorderRound);
  end;
end;

type
  TPanelAccess=class(TCustomTeePanelExtended);

procedure TFormTeePanel.Button1Click(Sender: TObject);
var tmp : TBitmap;
begin
  if ThePanel.Canvas is TTeeCanvas3D then
  begin
    // Pending:
    //  Redraw panel without filters, to obtain a non-filtered Bitmap:

    // TPanelAccess(ThePanel).Filters.InternalEnabled:=False;
    // tmp:=ThePanel.TeeCreateBitmap; ...
    // TPanelAccess(ThePanel).Filters.InternalEnabled:=True;

    tmp:=TTeeCanvas3D(ThePanel.Canvas).Bitmap;
  end
  else
     tmp:=nil;

  if ShowFiltersEditor(Self,tmp,TPanelAccess(ThePanel).Filters) then
     ThePanel.Invalidate;
end;

end.

