unit TeeGDIPlusEditor;
{$I TeeDefs.inc}

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs, StdCtrls,
  TeeGDIPlus, TeeProcs, TeCanvas, ComCtrls;

type
  TGDIPlusEditor = class(TForm)
    CBAntiAlias: TCheckBox;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    XDPI: TLabel;
    YDPI: TLabel;
    CBBuffered: TCheckBox;
    Label3: TLabel;
    CBTextQuality: TComboFlat;
    CBActive: TCheckBox;
    Label4: TLabel;
    TBScale: TTrackBar;
    LScale: TLabel;
    procedure FormShow(Sender: TObject);
    procedure CBAntiAliasClick(Sender: TObject);
    procedure CBBufferedClick(Sender: TObject);
    procedure CBTextQualityChange(Sender: TObject);
    procedure CBActiveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBScaleChange(Sender: TObject);
  private
    { Private declarations }

    ICreating : Boolean;

    GDIPlus : TTeeGDIPlus;
    GDIPlusCanvas : TGDIPlusCanvas;

    procedure ForceGDIPLUSComponent;
  public
    { Public declarations }
    class function ModalShow(AOwner:TComponent; const AGDIPlus:TTeeGDIPlus):Boolean;
  end;

implementation

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

uses
  {$IFDEF D16}
  GDIPAPI
  {$ELSE}
  TeeGDIPAPI
  {$ENDIF}
  ;

type
  TCanvasAccess=class(TTeeCanvas3D);

procedure TGDIPlusEditor.FormShow(Sender: TObject);
var tmp : TObject;
begin
  tmp:=TObject(Tag);

  GDIPlusCanvas:=nil;

  if tmp is TTeeGDIPlus then
     GDIPlus:=TTeeGDIPlus(tmp)
  else
  if tmp is TGDIPlusCanvas then
  begin
    GDIPlusCanvas:=TGDIPlusCanvas(Tag);
    GDIPlus:=TTeeGDIPlus(TCanvasAccess(GDIPlusCanvas).Tag)
  end
  else
    GDIPlus:=nil;

  if Assigned(GDIPlus) then
  begin
    CBActive.Checked:=GDIPlus.Active;
    CBAntiAlias.Checked:=GDIPlus.AntiAlias;
    CBTextQuality.ItemIndex:=Ord(GDIPlus.AntiAliasText);
    TBScale.Position:=Round(100*(GDIPlus.Scale-1));

    if Assigned(Parent) then
       Parent.Enabled:=True;
  end
  else
  begin
    CBActive.Hide;

    if Assigned(GDIPlusCanvas) then
    begin
      CBAntiAlias.Checked:=GDIPlusCanvas.AntiAlias;
      CBTextQuality.ItemIndex:=Ord(GDIPlusCanvas.AntiAliasText);
      TBScale.Position:=Round(100*(GDIPlusCanvas.Scale-1));
    end;
  end;

  if Assigned(GDIPlusCanvas) then
  begin
    CBBuffered.Checked:=GDIPlusCanvas.UseBuffer;

    XDPI.Caption:=FloatToStr(GDIPlusCanvas.Graphics.GetDpiX);
    YDPI.Caption:=FloatToStr(GDIPlusCanvas.Graphics.GetDpiY);
  end
  else
    EnableControls(False,[XDPI,YDPI,CBBuffered]);

  TeeTranslateControl(Self);

  ICreating:=False;
end;

procedure TGDIPlusEditor.CBAntiAliasClick(Sender: TObject);
begin
  ForceGDIPLUSComponent;

  if Assigned(GDIPlus) then
     GDIPlus.AntiAlias:=CBAntiAlias.Checked
  else
  if Assigned(GDIPlusCanvas) then
     GDIPlusCanvas.AntiAlias:=CBAntiAlias.Checked;
end;

class function TGDIPlusEditor.ModalShow(AOwner:TComponent; const AGDIPlus: TTeeGDIPlus): Boolean;
begin
  with TGDIPlusEditor.Create(AOwner) do
  try
    BorderStyle:=bsDialog;

    Tag:=ObjectToTag(AGDIPlus);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

procedure TGDIPlusEditor.ForceGDIPLUSComponent;

  function ValidScope:Boolean;
  begin
    result:=(not Assigned(GDIPlus)) and
            Assigned(GDIPlusCanvas) and Assigned(GDIPlusCanvas.View3DOptions) and
            Assigned(GDIPlusCanvas.View3DOptions.Parent) and
            (csDesigning in GDIPlusCanvas.View3DOptions.Parent.ComponentState);
  end;

begin
  if (not ICreating) and ValidScope then
  begin
    GDIPlus:=TTeeGDIPlus.Create(GDIPlusCanvas.View3DOptions.Parent.Owner);
    GDIPlus.Name:=TeeGetUniqueName(GDIPlus.Owner,'TeeGDIPlus'); // Do not localize
    GDIPlus.TeePanel:=GDIPlusCanvas.View3DOptions.Parent as TCustomTeePanel;

    GDIPlus.Antialias:=CBAntiAlias.Checked;
    GDIPlus.AntiAliasText:=TGDIPlusFontQuality(CBTextQuality.ItemIndex);
    GDIPlus.Scale:=1+TBScale.Position*0.01;

    GDIPlus.Active:=True;

    GDIPlusCanvas:=GDIPlus.Canvas;
  end;
end;

procedure TGDIPlusEditor.CBBufferedClick(Sender: TObject);
begin
  if Assigned(GDIPlusCanvas) and (GDIPlusCanvas.UseBuffer<>CBBuffered.Checked) then
  begin
    GDIPlusCanvas.UseBuffer:=CBBuffered.Checked;

    with GDIPlusCanvas.View3DOptions do
    if Assigned(Parent) then
       Parent.Invalidate;
  end;
end;

procedure TGDIPlusEditor.CBTextQualityChange(Sender: TObject);
var tmp : TGDIPlusFontQuality;
begin
  ForceGDIPLUSComponent;

  tmp:=TGDIPlusFontQuality(CBTextQuality.ItemIndex);

  if Assigned(GDIPlus) then
     GDIPlus.AntiAliasText:=tmp
  else
  if Assigned(GDIPlusCanvas) then
     GDIPlusCanvas.AntiAliasText:=tmp;
end;

procedure TGDIPlusEditor.CBActiveClick(Sender: TObject);
begin
  GDIPlus.Active:=CBActive.Checked;

  if GDIPlus.Active then
     GDIPlusCanvas:=GDIPlus.Canvas
  else
     GDIPlusCanvas:=nil;
end;

procedure TGDIPlusEditor.FormCreate(Sender: TObject);
begin
  ICreating:=True;
end;

procedure TGDIPlusEditor.TBScaleChange(Sender: TObject);
var tmp : Single;
begin
  ForceGDIPLUSComponent;

  tmp:=1+(TBScale.Position*0.01);

  if Assigned(GDIPlus) then
     GDIPlus.Scale:=tmp
  else
  if Assigned(GDIPlusCanvas) then
     GDIPlusCanvas.Scale:=tmp;

  LScale.Caption:=IntToStr(100+TBScale.Position)+'%';
end;

initialization
  RegisterClass(TGDIPlusEditor);
end.
