{**********************************************}
{  TBackImage editor dialog                    }
{  Copyright (c) 2006-2020 by Steema Software  }
{**********************************************}
unit TeeBackImage;
{$I TeeDefs.inc}

interface

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

  Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ExtCtrls, Buttons,
  TeeProcs, TeCanvas, TeeFiltersEditor;

type
  TBackImageEditor = class(TForm)
    PanelButtons: TPanel;
    PageControl1: TPageControl;
    TabOptions: TTabSheet;
    TabFilters: TTabSheet;
    TabGallery: TTabSheet;
    Bevel1: TBevel;
    Image1: TImage;
    LabelSize: TLabel;
    LabelExt: TLabel;
    BBrowse: TButton;
    CBTransp: TCheckBox;
    Panel1: TPanel;
    Button2: TButton;
    Button3: TButton;
    TabBounds: TTabSheet;
    GBPosition: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    ELeft: TEdit;
    UDLeft: TUpDown;
    ETop: TEdit;
    UDTop: TUpDown;
    RGBitmap: TRadioGroup;
    Label1: TLabel;
    Label2: TLabel;
    ERight: TEdit;
    UDRight: TUpDown;
    EBottom: TEdit;
    UDBottom: TUpDown;
    LQuality: TLabel;
    CBQuality: TComboFlat;
    LabelFileSize: TLabel;
    procedure RGBitmapClick(Sender: TObject);
    procedure BBrowseClick(Sender: TObject);
    procedure CBTranspClick(Sender: TObject);
    procedure ELeftChange(Sender: TObject);
    procedure ETopChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure ERightChange(Sender: TObject);
    procedure EBottomChange(Sender: TObject);
    procedure CBQualityChange(Sender: TObject);
    procedure PageControl1GetImageIndex(Sender: TObject; TabIndex: Integer;
      var ImageIndex: Integer);
  private
    { Private declarations }
    Image : TTeePicture;
    IFilters : TFiltersEditor;

    procedure EnableImageControls;
    procedure FiltersChanged(Sender: TObject);
    procedure SetBrowseCaption;
    procedure TextureChanged(Sender: TObject);
  public
    { Public declarations }
    class Function InsertForm(const AParent:TWinControl; const APicture:TTeePicture):TBackImageEditor;
    Procedure RefreshControls(const AImage:TTeePicture);
  end;

function EditTeePicture(const AOwner:TComponent; const APicture:TTeePicture):Boolean;

implementation

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

uses
  TeePenDlg, TeeConst, TeeBrushDlg, TeeGalleryPanel;

function EditTeePicture(const AOwner:TComponent; const APicture:TTeePicture):Boolean;
begin
  with TBackImageEditor.Create(AOwner) do
  try
    PanelButtons.Show;
    RefreshControls(APicture);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

class Function TBackImageEditor.InsertForm(const AParent:TWinControl;
                       const APicture:TTeePicture):TBackImageEditor;
begin
  // Speed optimization, pass "nil" as owner.
  result:=TBackImageEditor.Create(nil {AParent.Owner});
  result.Align:=alClient;

  AddFormTo(result,AParent);

  result.RefreshControls(APicture);
end;

Procedure TBackImageEditor.RefreshControls(const AImage:TTeePicture);
begin
  Image:=AImage;

  if Image is TBackImage then
  begin
    RGBitmap.ItemIndex :=Ord(TBackImage(Image).Mode);
    UDLeft.Position    :=TBackImage(Image).Left;
    UDTop.Position     :=TBackImage(Image).Top;
    UDRight.Position   :=TBackImage(Image).Right;
    UDBottom.Position  :=TBackImage(Image).Bottom;
    CBQuality.ItemIndex:=Ord(TBackImage(Image).DrawQuality);
  end;

  EnableImageControls;

  CBTransp.Enabled:=Assigned(Image.Graphic);
  CBTransp.Checked:=CBTransp.Enabled and Image.Graphic.Transparent;
end;

procedure TBackImageEditor.RGBitmapClick(Sender: TObject);
begin
  TBackImage(Image).Mode:=TTeeBackImageMode(RGBitmap.ItemIndex);
  EnableImageControls;
end;

function GraphicSizeToStr(const Graphic:TGraphic):String;
var tmp : TMemoryStream;
begin
  tmp:=TMemoryStream.Create;
  try
    Graphic.SaveToStream(tmp);
    result:=FileSizeToStr(tmp.Size);
  finally
    tmp.Free;
  end;
end;

procedure TBackImageEditor.EnableImageControls;
begin
  RGBitmap.Enabled:=Assigned(Image.Graphic);

//  CBImageInside.Enabled:=RGBitmap.Enabled;

  if CBTransp.Visible then
  begin
    CBTransp.Enabled:=RGBitmap.Enabled;
    CBTransp.Checked:=CBTransp.Enabled and Image.Graphic.Transparent;
  end;

  if not (Image is TBackImage) then
  begin
    TabBounds.TabVisible:=False;
    TabBounds.Hide;

    LQuality.Hide;
    CBQuality.Hide;
  end
  else
    GBPosition.Enabled:=TBackImage(Image).Mode=pbmCustom;

  LabelExt.Visible:=RGBitmap.Enabled;
  LabelSize.Visible:=RGBitmap.Enabled;
  LabelFileSize.Visible:=RGBitmap.Enabled;

  if LabelSize.Visible then
  begin
    LabelSize.Caption:=IntToStr(Image.Width)+'x'+IntToStr(Image.Height); // Do not localize
    LabelExt.Caption:=GraphicExtension(TGraphicClass(Image.Graphic.ClassType));
    LabelFileSize.Caption:=GraphicSizeToStr(Image.Graphic);
  end;

  SetBrowseCaption;
end;

procedure TBackImageEditor.SetBrowseCaption;
begin
  Image1.Picture.Assign(Image.Filtered);

  if Assigned(Image.Graphic) then
     BBrowse.Caption:=TeeMsg_ClearImage
  else
     BBrowse.Caption:=TeeMsg_BrowseImage;
end;

procedure TBackImageEditor.BBrowseClick(Sender: TObject);
begin
  if Assigned(Image.Graphic) then
     Image.Graphic:=nil
  else
     TeeLoadClearImage(Self,Image);

  if Assigned(IFilters) then
     IFilters.SetPicture(Image.Graphic);

  SetBrowseCaption;
  EnableImageControls;
end;

procedure TBackImageEditor.CBTranspClick(Sender: TObject);
begin
  if CBTransp.Enabled then
     Image.Graphic.Transparent:=CBTransp.Checked;
end;

procedure TBackImageEditor.ELeftChange(Sender: TObject);
begin
  if Showing then
     TBackImage(Image).Left:=UDLeft.Position;
end;

procedure TBackImageEditor.ETopChange(Sender: TObject);
begin
  if Showing then
     TBackImage(Image).Top:=UDTop.Position;
end;

procedure TBackImageEditor.FormShow(Sender: TObject);
begin
  if Assigned(Image) then
     RefreshControls(Image);

  TeeTranslateControl(Self);
end;

procedure TBackImageEditor.FormCreate(Sender: TObject);
begin
  PageControl1.ActivePage:=TabOptions;
end;

procedure TBackImageEditor.TextureChanged(Sender: TObject);
begin
  Image.Graphic:=TChartGalleryPanel(Sender).SelectedChart.Walls.Back.Brush.Image.Graphic;

  if Assigned(IFilters) then
     IFilters.SetPicture(Image.Graphic);

  SetBrowseCaption;
  EnableImageControls;
end;

procedure TBackImageEditor.FiltersChanged(Sender: TObject);
begin
  Image.Repaint;
  SetBrowseCaption;
end;

procedure TBackImageEditor.PageControl1Change(Sender: TObject);
var tmp : TBrushDialog;
begin
  if (PageControl1.ActivePage=TabFilters) and (TabFilters.ControlCount=0) then
  begin
    IFilters:=TFiltersEditor.InsertForm(Image,TabFilters);
    IFilters.Changed:=FiltersChanged;
  end
  else
  if (PageControl1.ActivePage=TabGallery) and (TabGallery.ControlCount=0) then
  begin
    tmp:=TBrushDialog.Create(Self);
    tmp.PageControl1.ActivePage:=tmp.TabTexture;
    tmp.PageControl1Change(Self);

    tmp.TextureGallery.OnChangeChart:=TextureChanged;
    tmp.TextureGallery.Parent:=TabGallery;
  end;
end;

procedure TBackImageEditor.ERightChange(Sender: TObject);
begin
  if Showing then
     TBackImage(Image).Right:=UDRight.Position;
end;

procedure TBackImageEditor.EBottomChange(Sender: TObject);
begin
  if Showing then
     TBackImage(Image).Bottom:=UDBottom.Position;
end;

procedure TBackImageEditor.CBQualityChange(Sender: TObject);
begin
  if Showing then
     TBackImage(Image).DrawQuality:=TCanvasStretchQuality(CBQuality.ItemIndex);
end;

procedure TBackImageEditor.PageControl1GetImageIndex(Sender: TObject;
  TabIndex: Integer; var ImageIndex: Integer);
begin
  // VCL bug workaround, exception when deleting / hiding tabs, solved by using
  // this event:

  ImageIndex:=0;
end;

end.
