{**********************************************}
{  TTeeFont (or derived) Editor Dialog         }
{  Copyright (c) 1999-2020 by Steema Software  }
{**********************************************}
unit TeeEdiFont;
{$I TeeDefs.inc}

interface

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

     {$IFDEF D17}
     System.UITypes,
     {$ENDIF}
     
     Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, ExtCtrls,

     {$IFDEF LCL}
     Buttons,
     {$ENDIF}
     TeeProcs, TeCanvas, TeePenDlg, TeeEdiGrad, TeeShadowEditor, TeeBackImage;

type
  TTeeFontEditor = class(TForm)
    PanelButtons: TPanel;
    Button1: TButton;
    Button2: TButton;
    PageControl1: TPageControl;
    TabFont: TTabSheet;
    TabOptions: TTabSheet;
    Button3: TButton;
    Label1: TLabel;
    GroupBox1: TGroupBox;
    CBItalic: TCheckBox;
    CBBold: TCheckBox;
    CBStrike: TCheckBox;
    CBUnder: TCheckBox;
    BColor: TButtonColor;
    Label2: TLabel;
    EInter: TEdit;
    UDInter: TUpDown;
    TabShadow: TTabSheet;
    TabGradient: TTabSheet;
    Label3: TLabel;
    CBName: TComboFlat;
    TabPicture: TTabSheet;
    TabEmboss: TTabSheet;
    Label4: TLabel;
    CBQuality: TComboFlat;
    Label5: TLabel;
    EDepth: TEdit;
    UDDepth: TUpDown;
    TabOutline: TTabSheet;
    TBSize: TTrackBar;
    LSize: TLabel;
    Label6: TLabel;
    TBTransp: TTrackBar;
    procedure BFontClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure EInterChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBItalicClick(Sender: TObject);
    procedure CBBoldClick(Sender: TObject);
    procedure CBStrikeClick(Sender: TObject);
    procedure CBUnderClick(Sender: TObject);
    procedure CBNameChange(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure CBQualityChange(Sender: TObject);
    procedure EDepthChange(Sender: TObject);
    procedure TBSizeChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure BColorClick(Sender: TObject);
  private
    { Private declarations }
    Backup,
    TheFont : TTeeFont;

    FEmbossEditor   : TTeeShadowEditor;
    FGradientEditor : TTeeGradientEditor;
    FPicEditor      : TBackImageEditor;
    FShadowEditor   : TTeeShadowEditor;
    FOutlineEditor  : TPenDialog;

    procedure CheckStyle(const Check:TCheckBox; const Value:TFontStyle);
    procedure RefreshBasicProps;
  public
    { Public declarations }
    procedure RefreshControls(const AFont:TTeeFont);
  end;

Function InsertTeeFontEditor(const AParent:TWinControl):TTeeFontEditor;
function EditTeeFontEx(const AOwner:TComponent; const AFont:TTeeFont):Boolean;

implementation

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

uses
  TeeConst, TeeBrushDlg;

Function InsertTeeFontEditor(const AParent:TWinControl):TTeeFontEditor;
begin
  result:=TTeeFontEditor.Create(nil);
  result.Align:=alClient;
  AddFormTo(result,AParent);
end;

function EditTeeFontEx(const AOwner:TComponent; const AFont:TTeeFont):Boolean;
begin
  with TTeeFontEditor.Create(AOwner) do
  try
    BorderStyle:=TeeBorderStyle;
    TheFont:=AFont;

    PanelButtons.Visible:=True;
    Height:=Height+PanelButtons.Height;

    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

procedure TTeeFontEditor.BFontClick(Sender: TObject);
begin
  EditTeeFont(Self,TheFont);

  BColor.Repaint;
  RefreshBasicProps;
end;

procedure TTeeFontEditor.RefreshBasicProps;
begin
  with TheFont do
  begin
    TBSize.Position:=Size;
    LSize.Caption:=IntToStr(Size);
    
    CBItalic.Checked:=fsItalic in Style;
    CBBold.Checked:=fsBold in Style;
    CBStrike.Checked:=fsStrikeOut in Style;
    CBUnder.Checked:=fsUnderline in Style;

    CBName.ItemIndex:=CBName.Items.IndexOf(Name);

    if CBName.ItemIndex=-1 then
       CBName.ItemIndex:=CBName.Items.Add(Name);
  end;
end;

procedure TTeeFontEditor.RefreshControls(const AFont:TTeeFont);
begin
  if CBName.Items.Count=0 then
  begin
    CBName.Items:=Screen.Fonts;

    if Assigned(TeeAddFontNames) then  // used by TeeNumericGauge unit
       TeeAddFontNames(CBName.Items);
  end;

  TheFont:=AFont;

  BColor.LinkProperty(TheFont,'Color'); // Do not localize
  RefreshBasicProps;

  With TheFont do
  begin
    UDInter.Position:=InterCharSize;

    CBQuality.ItemIndex:=Ord(Quality);
    UDDepth.Position:=Round(Depth);

    TBTransp.Position:=Round(TTeeCanvas.ColorAlpha(Color)/2.55);
  end;

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

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

  if Assigned(FEmbossEditor) then
     FEmbossEditor.RefreshControls(TheFont.Emboss);

  if Assigned(FPicEditor) then
     FPicEditor.RefreshControls(TheFont.Picture);

  if Assigned(FOutlineEditor) then
     FOutlineEditor.RefreshPen(TheFont.OutLine);
end;

procedure TTeeFontEditor.FormShow(Sender: TObject);
var tmpFirstTime : Boolean;
begin
  tmpFirstTime:=not Assigned(Backup);

  if tmpFirstTime then
     TeeTranslateControl(Self);

  if Assigned(TheFont) then
  begin
    RefreshControls(TheFont);

    if PanelButtons.Visible then
    begin
      if tmpFirstTime then
         Backup:=TTeeFont.Create(nil);

      Backup.Assign(TheFont);
    end;
  end;
end;

procedure TTeeFontEditor.EInterChange(Sender: TObject);
begin
  if Showing and Assigned(TheFont) then
     TheFont.InterCharSize:=UDInter.Position;
end;

procedure TTeeFontEditor.FormCreate(Sender: TObject);
begin
  {$IFDEF D7}
  PageControl1.DoubleBuffered:=True;
  {$ENDIF}

  PanelButtons.Hide;
  Height:=Height-PanelButtons.Height;
  PageControl1.ActivePage:=TabFont;
end;

procedure TTeeFontEditor.Button2Click(Sender: TObject);
begin
  if Assigned(Backup) then
     TheFont.Assign(Backup);
end;

procedure TTeeFontEditor.FormDestroy(Sender: TObject);
begin
  Backup.Free;
end;

procedure TTeeFontEditor.CheckStyle(const Check:TCheckBox; const Value:TFontStyle);
begin
  if Check.Checked then
     TheFont.Style:=TheFont.Style+[Value]
  else
     TheFont.Style:=TheFont.Style-[Value];
end;

procedure TTeeFontEditor.CBItalicClick(Sender: TObject);
begin
  CheckStyle(CBItalic,fsItalic);
end;

procedure TTeeFontEditor.CBBoldClick(Sender: TObject);
begin
  CheckStyle(CBBold,fsBold);
end;

procedure TTeeFontEditor.CBStrikeClick(Sender: TObject);
begin
  CheckStyle(CBStrike,fsStrikeOut);
end;

procedure TTeeFontEditor.CBUnderClick(Sender: TObject);
begin
  CheckStyle(CBUnder,fsUnderline);
end;

procedure TTeeFontEditor.CBNameChange(Sender: TObject);
begin
  TheFont.Name:=CBName.CurrentItem;
end;

procedure TTeeFontEditor.PageControl1Change(Sender: TObject);
begin
  if Assigned(TheFont) then
    if (PageControl1.ActivePage=TabShadow) and (not Assigned(FShadowEditor)) then
       FShadowEditor:=TTeeShadowEditor.InsertForm(TheFont.Shadow,TabShadow)
    else
    if (PageControl1.ActivePage=TabEmboss) and (not Assigned(FEmbossEditor)) then
       FEmbossEditor:=TTeeShadowEditor.InsertForm(TheFont.Emboss,TabEmboss)
    else
    if (PageControl1.ActivePage=TabGradient) and (not Assigned(FGradientEditor)) then
       FGradientEditor:=TTeeGradientEditor.InsertForm(TheFont.Gradient,TabGradient)
    else
    if (PageControl1.ActivePage=TabPicture) and (not Assigned(FPicEditor)) then
    begin
      FPicEditor:=TBackImageEditor.InsertForm(TabPicture,TheFont.Picture);
      FPicEditor.RGBitmap.Hide;
      FPicEditor.GBPosition.Hide;
    end
    else
    if (PageControl1.ActivePage=TabOutline) and (not Assigned(FOutlineEditor)) then
       FOutlineEditor:=TPenDialog.InsertForm(TheFont.OutLine,TabOutline);
end;

procedure TTeeFontEditor.CBQualityChange(Sender: TObject);
begin
  TheFont.Quality:=TFontQuality(CBQuality.ItemIndex);
end;

procedure TTeeFontEditor.EDepthChange(Sender: TObject);
begin
  if Showing and Assigned(TheFont) then
     TheFont.Depth:=UDDepth.Position;
end;

procedure TTeeFontEditor.TBSizeChange(Sender: TObject);
begin
  if Showing then
  begin
    TheFont.Size:=TBSize.Position;
    LSize.Caption:=IntToStr(TheFont.Size);
  end;
end;

procedure TTeeFontEditor.TBTranspChange(Sender: TObject);
begin
  if Showing then
  begin
    TheFont.Color:=TTeeCanvas.ColorFrom(TheFont.Color,Round(TBTransp.Position*2.55));
  end;
end;

procedure TTeeFontEditor.BColorClick(Sender: TObject);
begin
  TBTranspChange(Self);
end;

end.
