{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{ version 2.2                                                               }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2007                                        }
{            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 IWTMSImgCtrls;

{$I TMSDEFS.INC}

interface

uses
  {$IFDEF Linux}
  QControls, QGraphics, QIWPicCntnr,
  {$ELSE}
  Controls, Graphics, Windows, IWPicCntnr,
  {$ENDIF}
  {$IFDEF DELPHI6_LVL}
  Types,
  {$ENDIF}
  Classes, 
  IWControl, IWTypes, SysUtils, 
  IWExtCtrls, IWTMSImg, JPEG, IWHTMLTag, IWTMSBase
  {$IFDEF TMSIW6}
  , IWRenderContext, IWColor
  {$ENDIF}
  {$IFDEF TMSIW9}
  , IWXMLTag
  {$ENDIF}
  ;

type
  TTIWAdvImage = class(TIWControl)
  private
    FIPicture: TIPicture;
    FPictureURL: string;
    FUseBorder: Boolean;
    FUseSize: Boolean;
    FAltText: string;
    FHotIPicture: TIPicture;
    FHotPictureURL: string;
    FOnMouseDown: TIWImageOnMouseDown;
    procedure SetIPicture(const Value: TIPicture);
    procedure SetHotIPicture(const Value: TIPicture);
    procedure SetUseSize(const Value: Boolean);
    function GetOnClick: TNotifyEvent;
    procedure SetOnClick(const Value: TNotifyEvent);
    procedure SetOnMouseDown(const Value: TIWImageOnMouseDown);
  protected
    procedure PictureChanged(Sender: TObject);
    {$IFDEF TMSIW6}
    function ImageHTML(const ASrc,BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    {$ELSE}
    function ImageHTML(const ASrc,BSrc: string): TIWHTMLTag;
    {$ENDIF}
    procedure Submit(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    procedure Paint; override;
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    {$IFDEF TMSIW9}
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    {$ENDIF}
  published
    property DoSubmitValidation;
    property ExtraTagParams;
    property ScriptEvents;
    property AltText: string read FAltText write FAltText;
    property Picture: TIPicture read FIPicture write SetIPicture;
    property PicturURL: string read FPictureURL write FPictureURL;
    property HotPicture: TIPicture read FHotIPicture write SetHotIPicture;
    property HotPictureURL: string read FHotPictureURL write FHotPictureURL;
    property UseBorder: Boolean read FUseBorder write FUseBorder;
    property UseSize: Boolean read FUseSize write SetUseSize;
    property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
    property OnMouseDown: TIWImageOnMouseDown read FOnMouseDown write SetOnMouseDown;
    {$IFDEF TMSIW9}
    property OnAsyncClick;
    {$ENDIF}              
  end;

  TIWFadeType = (ftFadeInOut, ftFadeIn, ftFadeOut);

  TTIWFadeImage = class(TIWControl)
  private
    FUseBorder: Boolean;
    FUseSize: Boolean;
    FAltText: string;
    FIPicture: TIPicture;
    FPictureURL: string;
    FSpeed: Integer;
    FOpacity: Integer;
    FFadeType: TIWFadeType;
    FOnMouseDown: TIWImageOnMouseDown;
    procedure SetIPicture(const Value: TIPicture);
    procedure SetUseSize(const Value: Boolean);
    function GetOnClick: TNotifyEvent;
    procedure SetOnClick(const Value: TNotifyEvent);
    procedure SetOnMouseDown(const Value: TIWImageOnMouseDown);
  protected
    procedure PictureChanged(Sender: TObject);
    {$IFDEF TMSIW6}
    function ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    {$ELSE}
    function ImageHTML(const ASrc: string): TIWHTMLTag;
    {$ENDIF}
    procedure Submit(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    {$IFDEF TMSIW6}
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    procedure Paint; override;
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
    {$IFDEF TMSIW9}
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
    {$ENDIF}
  published
    property DoSubmitValidation;
    property ExtraTagParams;
    property AltText: string read FAltText write FAltText;
    property FadeType: TIWFadeType read FFadeType write FFadeType;
    property Opacity: Integer read FOpacity write FOpacity;
    property Speed: Integer read FSpeed write FSpeed;
    property Picture: TIPicture read FIPicture write SetIPicture;
    property PictureURL: string read FPictureURL write FPictureURL;
    property UseBorder: Boolean read FUseBorder write FUseBorder;
    property UseSize: Boolean read FUseSize write SetUseSize;
    property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
    property OnMouseDown: TIWImageOnMouseDown read FOnMouseDown write SetOnMouseDown;
    {$IFDEF TMSIW9}
    property OnAsyncClick;
    {$ENDIF}
  end;

  TTIWPaintBox = class(TIWControl)
  private
    ABitmap: Graphics.TBitmap;
    FOnMouseDown: TIWImageOnMouseDown;
    function GetBMPCanvas: TCanvas;
    function GetOnClick: TNotifyEvent;
    procedure SetOnClick(const Value: TNotifyEvent);
    procedure SetOnMouseDown(const Value: TIWImageOnMouseDown);
  protected
    {$IFDEF TMSIW6}
    function ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    {$ELSE}
    function ImageHTML(const ASrc: string): TIWHTMLTag;
    {$ENDIF}
    procedure Submit(const AValue: string); override;
  public
    property Canvas: TCanvas read GetBMPCanvas;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    {$IFDEF TMSIW6}
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    {$ELSE}
    function RenderHTML: TIWHTMLTag; override;
    {$ENDIF}
  published
    property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
    property OnMouseDown: TIWImageOnMouseDown read FOnMouseDown write SetOnMouseDown;
  end;



implementation
uses
  IWAppForm, IWForm, IWServerControllerBase, SWSystem, SWStrings;

{ TTIWAdvImage }
{$IFDEF TMSIW9}
function TTIWAdvImage.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWADVTIMAGE');
    RenderAsyncCommonProperties( AContext, Result );
  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWAdvImage.Create(AOwner: TComponent);
begin
  inherited;
  FIPicture := TIPicture.Create;
  FIPicture.OnChange := PictureChanged;
  FHotIPicture := TIPicture.Create;
  {$IFDEF TMSIW6}
  {$ELSE}
  FSupportsSubmit := True;
  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  {$ENDIF}
  FUseSize := True;
  Width := 100;
  Height := 100;
end;

destructor TTIWAdvImage.Destroy;
begin
  FIPicture.Free;
  FHotIPicture.Free;
  inherited;
end;

{$IFDEF TMSIW6}
procedure TTIWAdvImage.IWPaint;
{$ELSE}
procedure TTIWAdvImage.Paint;
{$ENDIF}
begin
  if not Assigned(FIPicture) then
  begin
    inherited;
    Exit;
  end;

  if FIPicture.Empty then
  begin
    inherited;
    Exit;
  end;

  if UseSize then
    Canvas.StretchDraw(ClientRect,FIPicture)
  else
    Canvas.Draw(0,0,FIPicture)
end;

procedure TTIWAdvImage.PictureChanged(Sender: TObject);
begin
  Invalidate;
end;

{$IFDEF TMSIW6}
function TTIWAdvImage.ImageHTML(const ASrc, BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvImage.ImageHTML(const ASrc, BSrc: string): TIWHTMLTag;
{$ENDIF}
var
  LTag: TIWHTMLTag;
  ajaxcall, submitclick: string;
  hotpictureset : boolean;
begin
  hotpictureset := false;

  Result := TIWHTMLTag.CreateTag('IMG');
  Result.AddStringParam('SRC', ASrc);
  Result.AddStringParam('NAME', HTMLName);
  Result.AddIntegerParam('BORDER', iif(UseBorder, 1, 0));
  Result.AddStringParam('ALT', AltText);
  if UseSize then
  begin
    Result.AddIntegerParam('WIDTH', Width);
    Result.AddIntegerParam('HEIGHT', Height);
  end;

  if HotPictureURL <> EmptyStr then
    hotpictureset := true
  else if Assigned(FHotIPicture) then
  begin
    if not FHotIPicture.Empty then
      hotpictureset := true;
  end;

  if hotpictureset then
  begin
    Result.AddStringParam('OnMouseOver','this.src='#39+BSrc+#39';');
    Result.AddStringParam('OnMouseOut','this.src='#39+ASrc+#39';');
  end;

  if Assigned(OnMouseDown) then
  begin
    {$IFDEF TMSIW6}
    case AContext.WebApplication.Browser of    
    {$ELSE}
    case WebApplication.Browser of
    {$ENDIF}

      brIE:
      begin
        Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
          + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');');
      end;
      brNetscape6:
      begin
        LTag := TIWHTMLTag.CreateTag('A');
        LTag.AddStringParam('HREF', '#');
        LTag.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
          + ',' + iif(DoSubmitValidation, 'true', 'false')
          + ',' + '''' + Confirmation + ''''
          + ');"');
        LTag.Contents.AddTag(Result.Render);
        FreeAndNil(Result);
        Result := LTag;
      end;
    end;
  end;

      if Assigned(OnClick) then
        submitclick := ' return SubmitClick(''' + HTMLName + ''','''',''false'');';

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncClick) then
      begin
        ajaxcall := 'processAjaxEvent(''onClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

          submitclick := '';
       end;
      {$ENDIF}

  // OnMouseDown and OnClick are mutually exclusive, no need to check
  if Assigned(OnClick) then begin
    ScriptEvents.HookEvent('OnClick', ajaxcall + submitclick);
    Result := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
  end;
end;

{$IFDEF TMSIW6}
function TTIWAdvImage.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvImage.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  ImgName,ImgHot: string;
  htmlres: string;
begin
  htmlres := '';

  // Is nil if no picture. Ex: Drop a TIWImage on form, dont load an image, run.

  ImgName := '';
  ImgHot := '';

  if PicturURL <> EmptyStr then
       ImgName := PicturURL
  else if Assigned(FIPicture) then
  begin
    if not FIPicture.Empty then
    begin
      if FIPicture.IsGIF then
      {$IFDEF TMSIW71}
        ImgName := TIWServerControllerBase.NewCacheFile('gif',true)
      {$ELSE}
        ImgName := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ImgName := TIWServerControllerBase.NewCacheFile('jpg',true);
      {$ELSE}
        ImgName := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FIPicture.SaveToFile(ImgName);
    end;
  end;

  if HotPictureURL <> EmptyStr then
       ImgHot := HotPictureURL
  else if Assigned(FHotIPicture) then
  begin
    if not FHotIPicture.Empty then
    begin
      if FHotIPicture.IsGIF then
      {$IFDEF TMSIW71}
        ImgHot := TIWServerControllerBase.NewCacheFile('gif',true)
      {$ELSE}
        ImgHot := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ImgHot := TIWServerControllerBase.NewCacheFile('jpg',true);
      {$ELSE}
        ImgHot := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FHotIPicture.SaveToFile(ImgHot);
    end;
  end;


   if (PicturURL <> EmptyStr) or (HotPictureURL <> EmptyStr) then
   begin
      Result := ImageHTML(ImgName, ImgHot
      {$IFDEF TMSIW6}
        ,AContext);
      {$ELSE}
        );
      {$ENDIF}
   end
   else
   begin
      Result := ImageHTML(GetCacheDir(Self) + ExtractFilename(ImgName),
      GetCacheDir(Self) + ExtractFilename(ImgHot)
      {$IFDEF TMSIW6}
        ,AContext);
      {$ELSE}
        );
      {$ENDIF}
   end;

end;

procedure TTIWAdvImage.SetIPicture(const Value: TIPicture);
begin
  FIPicture.Assign(Value);
  Invalidate;
end;

procedure TTIWAdvImage.SetHotIPicture(const Value: TIPicture);
begin
  FHotIPicture.Assign(Value);
  Invalidate;
end;

procedure TTIWAdvImage.SetUseSize(const Value: Boolean);
begin
  FUseSize := Value;
  Invalidate;
end;

function TTIWAdvImage.GetOnClick: TNotifyEvent;
begin
  Result := (inherited OnClick);
end;

procedure TTIWAdvImage.SetOnClick(const Value: TNotifyEvent);
begin
  FOnMouseDown := nil;
  inherited OnClick := Value;
end;

procedure TTIWAdvImage.SetOnMouseDown(const Value: TIWImageOnMouseDown);
begin
  inherited OnClick := nil;
  FOnMouseDown := Value;
end;

procedure TTIWAdvImage.Submit(const AValue: string);
var
  s: string;
  LX: Integer;
  LY: Integer;
begin
  if Assigned(OnMouseDown) then
  begin
    s := AValue;
    LX := StrToInt(Fetch(s, ','));
    LY := StrToInt(s);
    OnMouseDown(Self, LX, LY);
  end
  else
  begin
    DoClick;
  end;
end;


{ TTIWFadeImage }
{$IFDEF TMSIW9}
function TTIWFadeImage.RenderAsync(
  AContext: TIWBaseHTMLComponentContext): TIWXMLTag;
begin
  Result := TIWXMLTag.CreateTag('control');
  try
    Result.AddStringParam('id', HTMLName);
    Result.AddStringParam('type', 'TTIWFADEIMAGE');
    RenderAsyncCommonProperties( AContext, Result );
  except
    FreeAndNil(Result);
  end;

end;
{$ENDIF}

constructor TTIWFadeImage.Create(AOwner: TComponent);
begin
  inherited;
 
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportsSubmit := True;
  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  {$ENDIF}

  FIPicture := TIPicture.Create;
  FIPicture.OnChange := PictureChanged;
  FOpacity := 40;
  FSpeed := 40;
  Width := 100;
  Height := 100;
end;

destructor TTIWFadeImage.Destroy;
begin
  FIPicture.Free;
  inherited;
end;

function TTIWFadeImage.GetOnClick: TNotifyEvent;
begin
  Result := (inherited OnClick);
end;

{$IFDEF TMSIW6}
function TTIWFadeImage.ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWFadeImage.ImageHTML(const ASrc: string): TIWHTMLTag;
{$ENDIF}
var
  LTag: TIWHTMLTag;
  ajaxcall, submitclick: string;
begin
  Result := TIWHTMLTag.CreateTag('IMG');
  Result.AddStringParam('SRC', ASrc);
//  Result.AddStringParam('NAME', HTMLName+'I');
  Result.AddStringParam('NAME', HTMLName);
  Result.AddIntegerParam('BORDER', iif(UseBorder, 1, 0));
  Result.AddStringParam('ALT', AltText);
  if UseSize then
  begin
    Result.AddIntegerParam('WIDTH', Width);
    Result.AddIntegerParam('HEIGHT', Height);
  end;

  Result.AddStringParam('style','filter:alpha(opacity='+IntToStr(Opacity)+')');
  Result.AddStringParam('OnMouseOver',HTMLName + 'high(this);');
  Result.AddStringParam('OnMouseOut',HTMLName + 'low(this);');

  if Assigned(OnMouseDown) then
  begin

    {$IFDEF TMSIW6}
    case AContext.WebApplication.Browser of
    {$ELSE}
    case WebApplication.Browser of
    {$ENDIF}
      brIE:
      begin
        Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
          + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');');
      end;
      brNetscape6:
      begin
        LTag := TIWHTMLTag.CreateTag('A');
        LTag.AddStringParam('HREF', '#');
        LTag.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
          + ',' + iif(DoSubmitValidation, 'true', 'false')
          + ',' + '''' + Confirmation + ''''
          + ');"');
        LTag.Contents.AddTag(Result.Render);
        FreeAndNil(Result);
        Result := LTag;
      end;
    end;

  end;

      if Assigned(OnClick) then
        submitclick := ' return SubmitClick(''' + HTMLName + ''','''',''false'');';

      {$IFDEF TMSIW9}
      if Assigned(FOnAsyncClick) then
      begin
        ajaxcall := 'processAjaxEvent(''onClick'', '
          + HTMLControlImplementation.IWCLName
          + ',''' + HTMLName + '.' + 'DoOnAsyncClick' + ''','
          + 'true' + ', null, '
          + 'true' + ');';

          submitclick := '';
       end;
      {$ENDIF}

  // OnMouseDown and OnClick are mutually exclusive, no need to check
  if Assigned(OnClick) then begin
    ScriptEvents.HookEvent('OnClick', ajaxcall + submitclick);
    Result := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
  end;

end;

{$IFDEF TMSIW6}
procedure TTIWFadeImage.IWPaint;
{$ELSE}
procedure TTIWFadeImage.Paint;
{$ENDIF}
begin
  if not Assigned(FIPicture) then
  begin
    inherited;
    Exit;
  end;

  if FIPicture.Empty then
  begin
    inherited;
    Exit;
  end;

  if UseSize then
    Canvas.StretchDraw(ClientRect,FIPicture)
  else
    Canvas.Draw(0,0,FIPicture)
end;

procedure TTIWFadeImage.PictureChanged(Sender: TObject);
begin
  Invalidate;
end;

{$IFDEF TMSIW6}
function TTIWFadeImage.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWFadeImage.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  ImgName: string;
  htmlres: string;
begin
  htmlres := '';

  ImgName := '';

  htmlres :=''+
    '<script language="JavaScript1.2">'#13+
    'var lowlighting = 0;'#13+
    'var highlighting = 0;'#13+
    'function '+HTMLName+'high(which2){'#13+
//    'theobject=document.getElementById("'+HTMLName+'I");'#13+
//    'theobject=FindElem("'+HTMLName+'");'#13+
//    'if (theobject.tagName.toUpperCase() != "IMG")'+
//    '  theobject=FindElem("'+HTMLName+'").firstChild;'#13+
    ' theobject = which2;'#13+
    iif(FFadeType in [ftFadeIn,ftFadeInOut],'highlighting=setInterval("'+HTMLName+'highlightit(theobject)",'+IntToStr(Speed)+')'#13,
      'theobject.filters.alpha.opacity=100'#13)+
    'if (lowlighting != undefined) clearInterval(lowlighting)'#13+
    '}'#13+
    'function '+HTMLName+'low(which2){'#13+
//    'theobject=document.getElementById("'+HTMLName+'I");'#13+
//    'theobject=FindElem("'+HTMLName+'");'#13+
//        'if (theobject.tagName.toUpperCase() != "IMG")'+
//    '  theobject=FindElem("'+HTMLName+'").firstChild;'#13+
    ' theobject = which2;'#13+
    iif(FFadeType in [ftFadeOut,ftFadeInOut],'lowlighting=setInterval("'+HTMLName+'lowlightit(theobject)",'+IntToStr(Speed)+')'#13,
      'theobject.filters.alpha.opacity='+IntToStr(Opacity)+#13) +
    'if (highlighting != undefined) clearInterval(highlighting)'#13+
    '}'#13+
    ''#13+
    'function '+HTMLName+'highlightit(cur2){'#13+
    'if (cur2.filters.alpha.opacity<100)'#13+
    'cur2.filters.alpha.opacity+=5'#13+
    'else if (window.highlighting)'#13+
    'clearInterval(highlighting)'#13+
    '}'#13+
    'function '+HTMLName+'lowlightit(cur2){'#13+
    'if (cur2.filters.alpha.opacity>'+IntToStr(Opacity)+')'#13+
    'cur2.filters.alpha.opacity-=5'#13+
    'else if (window.lowlighting)'#13+
    'clearInterval(lowlighting)'#13+
    '}'#13+

    ''#13+
    '</script>'#13;

  if PictureURL <> EmptyStr then
  begin
    {$IFDEF TMSIW6}
    Result := ImageHTML(PictureURL,AContext);
    {$ELSE}
    Result := ImageHTML(PictureURL);
    {$ENDIF}
  end
  else if Assigned(FIPicture) then
  begin
    if not FIPicture.Empty then
    begin
      if FIPicture.IsGIF then
      {$IFDEF TMSIW71}
        ImgName := TIWServerControllerBase.NewCacheFile('gif',true)
      {$ELSE}
        ImgName := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ImgName := TIWServerControllerBase.NewCacheFile('jpg',true);
      {$ELSE}
        ImgName := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}

      FIPicture.SaveToFile(ImgName);
    end;

  {$IFDEF TMSIW6}
  Result := ImageHTML(GetCacheDir(Self) + ExtractFilename(ImgName),AContext);
  {$ELSE}
  Result := ImageHTML(GetCacheDir(Self) + ExtractFilename(ImgName));
  {$ENDIF}
  end
  else
  begin
    {$IFDEF TMSIW6}
    Result := ImageHTML('',AContext);
    {$ELSE}
    Result := ImageHTML('');
    {$ENDIF}
  end;

  Result.Contents.AddText(htmlres);

end;

procedure TTIWFadeImage.SetIPicture(const Value: TIPicture);
begin
  FIPicture.Assign(Value);
  Invalidate;
end;

procedure TTIWFadeImage.SetOnClick(const Value: TNotifyEvent);
begin
  FOnMouseDown := nil;
  inherited OnClick := Value;
end;

procedure TTIWFadeImage.SetOnMouseDown(const Value: TIWImageOnMouseDown);
begin
  inherited OnClick := nil;
  FOnMouseDown := Value;
end;

procedure TTIWFadeImage.SetUseSize(const Value: Boolean);
begin
  FUseSize := Value;
  Invalidate;
end;

procedure TTIWFadeImage.Submit(const AValue: string);
var
  s: string;
  LX: Integer;
  LY: Integer;
begin
  if Assigned(OnMouseDown) then begin
    s := AValue;
    LX := StrToInt(Fetch(s, ','));
    LY := StrToInt(s);
    OnMouseDown(Self, LX, LY);
  end else begin
    DoClick;
  end;
end;

{ TTIWPaintBox }

constructor TTIWPaintBox.Create(AOwner: TComponent);
begin
  inherited;
  ABitmap := Graphics.TBitmap.Create;
  ABitmap.Width := 1024;
  ABitmap.Height := 1024;
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportsSubmit := True;
  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  {$ENDIF}
  Width := 100;
  Height := 100;
end;

destructor TTIWPaintBox.Destroy;
begin
  ABitmap.Free;
  inherited;
end;

function TTIWPaintBox.GetBMPCanvas: TCanvas;
begin
  Result := ABitmap.Canvas;
end;

function TTIWPaintBox.GetOnClick: TNotifyEvent;
begin
  Result := (inherited OnClick);
end;

{$IFDEF TMSIW6}
function TTIWPaintBox.ImageHTML(const ASrc: String; AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWPaintBox.ImageHTML(const ASrc: String): TIWHTMLTag;
{$ENDIF}
var
  LTag: TIWHTMLTag;
begin
  Result := TIWHTMLTag.CreateTag('IMG');
  Result.AddStringParam('SRC', ASrc);
  Result.AddStringParam('NAME', HTMLName);
  Result.AddIntegerParam('BORDER', 0);
  Result.AddIntegerParam('WIDTH', Width);
  Result.AddIntegerParam('HEIGHT', Height);

  if Assigned(OnMouseDown) then
  begin
    {$IFDEF TMSIW6}
    case AContext.WebApplication.Browser of
    {$ELSE}
    case WebApplication.Browser of
    {$ENDIF}
      brIE: begin
        Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
          + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');');
      end;
      brNetscape6: begin
        LTag := TIWHTMLTag.CreateTag('A');
        LTag.AddStringParam('HREF', '#');
        LTag.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
          + ',' + iif(DoSubmitValidation, 'true', 'false')
          + ',' + '''' + Confirmation + ''''
          + ');"');
        LTag.Contents.AddTag(Result.Render);
        FreeAndNil(Result);
        Result := LTag;
      end;
    end;
  end;
  // OnMouseDown and OnClick are mutually exclusive, no need to check
  if Assigned(OnClick) then begin
    ScriptEvents.HookEvent('OnClick', ' return SubmitClick(''' + HTMLName + ''','''',''false'');');
    Result := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
  end;
end;

{$IFDEF TMSIW6}
function TTIWPaintBox.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWPaintBox.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  Apicture:TJPEGImage;
  bmp: Graphics.TBitmap;
  ImgName: string;
begin
  bmp := Graphics.TBitmap.Create;
  bmp.Width := self.Width;
  bmp.Height := self.Height;
  bmp.Canvas.CopyRect(bmp.Canvas.ClipRect,ABitmap.Canvas,bmp.Canvas.ClipRect);

  Apicture := TJPEGImage.Create;
  Apicture.CompressionQuality := 100;
  Apicture.Assign(bmp);

  {$IFDEF TMSIW71}
  ImgName := TIWServerControllerBase.NewCacheFile('jpg',true);
  {$ELSE}
  ImgName := TIWServerControllerBase.NewCacheFile('jpg');
  {$ENDIF}

  Apicture.SaveToFile(ImgName);

  Apicture.Free;
  bmp.Free;

  {$IFDEF TMSIW6}
  Result := ImageHTML(GetCacheDir(Self) + ExtractFilename(ImgName),AContext);
  {$ELSE}
  Result := ImageHTML(GetCacheDir(Self) + ExtractFilename(ImgName));
  {$ENDIF}
end;

procedure TTIWPaintBox.SetOnClick(const Value: TNotifyEvent);
begin
  FOnMouseDown := nil;
  inherited OnClick := Value;
end;

procedure TTIWPaintBox.SetOnMouseDown(const Value: TIWImageOnMouseDown);
begin
  FOnMouseDown := Value;
end;

procedure TTIWPaintBox.Submit(const AValue: string);
var
  s: string;
  LX: Integer;
  LY: Integer;
begin
  if Assigned(OnMouseDown) then
  begin
    s := AValue;
    LX := StrToInt(Fetch(s, ','));
    LY := StrToInt(s);
    OnMouseDown(Self, LX, LY);
  end
  else
  begin
    DoClick;
  end;
end;

end.
