{***************************************************************************}
{ TMS Smooth Label for IntraWeb                                             }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2012 - 2013                                        }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}
unit iwLabel;

{$I TMSDEFS.INC}

interface

uses
  IWAdvSmoothLabel, IWGdipFill, IWAdvSmoothGDIP, IWAdvStyleIF,IWApplication,
  Controls, Graphics, Windows, IWCompLabel,
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  Classes,
  IWControl, IWTypes, SysUtils, JPEG, IWHTMLTag, IWTMSBase,
  IWRenderContext, IWColor, IWXMLTag
  {$IFDEF TMSIW121}
  ,IWCompExtCtrls
  {$ELSE}
  ,IWExtCtrls
  {$ENDIF}
  ;

const
  MAJ_VER = 1; // Major version nr.
  MIN_VER = 1; // Minor version nr.
  REL_VER = 0; // Release nr.
  BLD_VER = 0; // Build nr.
  DATE_VER = 'JAN, 2012'; // Month version

  //v1.0.0.1 : Fixed issue with transparency when rendering images

type
  TTIWSmoothLabel = class;

  TTIWSmoothLabelAppearance = class(TPersistent)
  private
    FIWOwner: TTIWSmoothLabel;
    FTransparent: Boolean;
    FCaption: TIWAdvSmoothLabelCaption;
    FFill: TIWGDIPFill;
    procedure SetCaption(const Value: TIWAdvSmoothLabelCaption);
    procedure SetFill(const Value: TIWGDIPFill);
    procedure SetTransparent(const Value: Boolean);
  protected
    procedure CaptionChanged(Sender: TObject);
    procedure FillChanged(Sender: TObject);
    procedure Changed;
  public
    constructor Create(IWOwner: TTIWSmoothLabel; AOwner: TIWAdvSmoothLabel);
    destructor Destroy; override;
  published
    property Caption: TIWAdvSmoothLabelCaption read FCaption write SetCaption;
    property Fill: TIWGDIPFill read FFill write SetFill;
    property Transparent: Boolean read FTransparent write SetTransparent;
  end;

  TTIWSmoothLabel = class(TIWCustomLabel)
  private
    FLabel: TIWAdvSmoothLabel;
    FNoResize: Boolean;
    FVersion: string;
    FChanged: boolean;
    FUpdateCount: integer;
    FImageType: TImageType;
    FImageQuality: Integer;
    FAppearance: TTIWSmoothLabelAppearance;
    function GetVersion: string;
    procedure SetImageType(const Value: TImageType);
    procedure SetImageQuality(const Value: Integer);
    procedure SetAppearance(const Value: TTIWSmoothLabelAppearance);
  protected
    function ImageHTML(const ASrc,BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    function OutputImage: string;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Resize; override;
    procedure ApplyAppearance;
    procedure LabelChanged(Sender: TObject);
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    function GetVersionNr: Integer; virtual;
    function GetVersionString: string; virtual;
  published
    property Appearance: TTIWSmoothLabelAppearance read FAppearance write SetAppearance;
    property Version: string read GetVersion;
    property ImageType: TImageType read FImageType write SetImageType default itPng;
    property ImageQuality: Integer read FImageQuality write SetImageQuality default 100;
  end;

  function GetControlHTML(Control: TIWCustomControl; AContext: TIWBaseComponentContext): string;

implementation
uses
  IWGlobal, IWAppForm, IWForm, IWServerControllerBase,
  TMSIWStrings, TMSIWSystem;

function GetControlHTML(Control: TIWCustomControl;
  AContext: TIWBaseComponentContext): string;
var
  it: TIWHTMLTag;
begin
  // !!!!!!!!!!!!!! IMPORTANT NOTICE !!!!!!!!!!!!!!!!!!
  //
  // If you encounter a compilation problem here,
  // make sure that you followed the installation steps
  // in INSTALL.TXT, in particular, that you're using the
  // correct TMSDEFS.INC file
  //
  // !!!!!!!!!!!!!! IMPORTANT NOTICE !!!!!!!!!!!!!!!!!!

  it := Control.RenderHTML(AContext);
  Result := it.Render;
  it.Free;
end;

{ TTIWAdvChart }

function TTIWSmoothLabel.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
var
  js, ImgName: string;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWSmoothLabel');
    RenderAsyncCommonProperties( AContext, Result );

    ImgName := OutputImage;

    js :=
      '<![CDATA['
      + 'document.getElementById("' + HTMLName + '").src = "' + GetCacheDir + ExtractFileName(ImgName) + '"'#13
      +']]>';

    GGetWebApplicationThreadVar.CallBackResponse.AddJavaScriptToExecute(js);
  except
    FreeAndNil(Result);
  end;
end;

function TTIWSmoothLabel.GetVersion: string;
begin
  Result := FVersion;
end;

function TTIWSmoothLabel.GetVersionNr: Integer;
begin
  Result := MakeLong(MakeWord(BLD_VER, REL_VER), MakeWord(MIN_VER, MAJ_VER));
end;

function TTIWSmoothLabel.GetVersionString: string;
var
  vn: Integer;
begin
  vn := GetVersionNr;
  Result := IntToStr(Hi(Hiword(vn))) + '.' + IntToStr(Lo(Hiword(vn))) + '.' + IntToStr(Hi(Loword(vn))) + '.' + IntToStr(Lo(Loword(vn))) + ' ' + DATE_VER;
end;

procedure TTIWSmoothLabel.ApplyAppearance;
begin
  FLabel.Width := Width;
  FLabel.Height := Height;
  FLabel.Transparent := Appearance.Transparent;
  FLabel.Caption.Assign(Appearance.Caption);
  FLabel.Fill.Assign(Appearance.Fill);
end;

constructor TTIWSmoothLabel.Create(AOwner: TComponent);
begin
  inherited;
  FLabel := TIWAdvSmoothLabel.Create(Self);
  FLabel.OnChange := LabelChanged;
  FAppearance := TTIWSmoothLabelAppearance.Create(Self, FLabel);
  if (csDesigning in ComponentState) and not
    ((csReading in Owner.ComponentState) or (csLoading in Owner.ComponentState)) then
    begin
      FAppearance.Caption.Text := ClassName;
      Fappearance.Transparent := true;
    end;
  FImageType := itPng;
  FImageQuality := 100;

  FNoResize := true;
  Width := FLabel.Width;
  Height := FLabel.Height;
  FNoResize := false;
  FUpdateCount := 0;
  FVersion := GetVersionString;
end;

destructor TTIWSmoothLabel.Destroy;
begin
  FAppearance.Free;
  FLabel.Free;
  inherited;
end;

procedure TTIWSmoothLabel.IWPaint;
begin
  if not (Assigned(FLabel)) then
  begin
    inherited;
    Exit;
  end;

  inc(FUpdateCount);

  if FChanged then
    ApplyAppearance;

  FChanged := false;

  FLabel.DrawLabel(Canvas);

  dec(FUpdateCount);
end;

function TTIWSmoothLabel.ImageHTML(const ASrc, BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
begin
  Result := TIWHTMLTag.CreateTag('IMG');
  Result.AddStringParam('SRC', ASrc);
  Result.AddStringParam('NAME', HTMLName);
  Result.AddIntegerParam('WIDTH', Width);
  Result.AddIntegerParam('HEIGHT', Height);
end;

procedure TTIWSmoothLabel.LabelChanged(Sender: TObject);
begin
  if FUpdateCount = 0 then
  begin
    FChanged := true;
    Invalidate;
  end;
end;

function TTIWSmoothLabel.OutputImage: string;
var
  ImgName, str: string;
  {$IFDEF TMSIW14}
  ms: TMemoryStream;
  imgtype: TIWImageType;
  {$ENDIF}
begin
  ImgName := '';

  if Assigned(FLabel) then
  begin
    case ImageType of
      itPNG: str := 'png';
      itBMP: str := 'bmp';
      itJPEG: str := 'jpg';
      itTIFF: str := 'tiff';
      itGIF: str := 'gif';
    end;
    {$IFNDEF TMSIW14}
    ImgName := GetNewCacheFile(str,true);
    {$ENDIF}

    ApplyAppearance;

    {$IFNDEF TMSIW14}
    FLabel.SaveToImage(ImgName, Width, Height, ImageType, ImageQuality);
    {$ENDIF}

    {$IFDEF TMSIW14}
    ms := TMemoryStream.Create;

    try
      FLabel.SaveToStream(ms, Width, Height, ImageType, ImageQuality);
      ms.Position := 0;

      case ImageType of
      itPNG: imgtype := imgPNG;
      itBMP: imgtype := imgBMP;
      itJPEG: imgtype := imgJPEG;
      itGIF: imgtype := imgGIF;
      else
        imgtype := imgPNG;
      end;

      ImgName := TIWAppCache.StreamToCacheFile(ms, imgtype);
    finally
      ms.Free;
    end;
    {$ENDIF}

  end;

  Result := ImgName;
end;

function TTIWSmoothLabel.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  ImgName,ImgHot: string;
  htmlres: string;
begin
  htmlres := '';
  ImgName := '';
  ImgHot := '';

  ImgName := OutputImage;

  Result := ImageHTML(GetCacheDir + ExtractFilename(ImgName),
  GetCacheDir + ExtractFilename(ImgHot), AContext);
end;

procedure TTIWSmoothLabel.Resize;
begin
  inherited;
  if not FNoResize and Assigned(FLabel) then
  begin
    FLabel.Width := Width;
    FLabel.Height := Height;
  end;
end;

procedure TTIWSmoothLabel.SetAppearance(
  const Value: TTIWSmoothLabelAppearance);
begin
  if FAppearance <> value then
  begin
    FAppearance.Assign(Value);
    LabelChanged(Self);
  end;
end;

procedure TTIWSmoothLabel.SetImageQuality(const Value: Integer);
begin
  if fImageQuality <> value then
  begin
    FImageQuality := Value;
    LabelChanged(Self);
  end;
end;

procedure TTIWSmoothLabel.SetImageType(const Value: TImageType);
begin
  if FImageType <> value then
  begin
    FImageType := Value;
    LabelChanged(Self);
  end;
end;

{ TTIWSmoothLabelAppearance }

procedure TTIWSmoothLabelAppearance.CaptionChanged(Sender: TObject);
begin
  Changed;
end;

procedure TTIWSmoothLabelAppearance.Changed;
begin
  FIWOwner.LabelChanged(Self);
end;

constructor TTIWSmoothLabelAppearance.Create(IWOwner: TTIWSmoothLabel; AOwner: TIWAdvSmoothLabel);
begin
  FIWOwner := IWOwner;
  FCaption := TIWAdvSmoothLabelCaption.Create(AOwner);
  FCaption.OnChange := CaptionChanged;
  FFill := TIWGDIPFill.Create;
  FFill.OnChange := FillChanged;
  FTransparent := false;
end;

destructor TTIWSmoothLabelAppearance.Destroy;
begin
  FCaption.Free;
  FFill.Free;
  inherited;
end;

procedure TTIWSmoothLabelAppearance.FillChanged(Sender: TObject);
begin
  Changed;
end;

procedure TTIWSmoothLabelAppearance.SetCaption(
  const Value: TIWAdvSmoothLabelCaption);
begin
  if FCaption <> Value then
  begin
    FCaption := Value;
    Changed;
  end;
end;

procedure TTIWSmoothLabelAppearance.SetFill(const Value: TIWGDIPFill);
begin
  if FFill <> value then
  begin
    FFill := Value;
    Changed;
  end;
end;

procedure TTIWSmoothLabelAppearance.SetTransparent(const Value: Boolean);
begin
  if FTransparent <> value then
  begin
    FTransparent := Value;
    Changed;
  end;
end;

end.
