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

{$I TMSDEFS.INC}

interface

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

type
  TTIWAdvImage = class(TIWControl)
  private
    FIPicture: TIPicture;
    FPictureURL: string;
    FUseBorder: Boolean;
    FUseSize: Boolean;
    FAltText: string;
    FHotIPicture: TIPicture;
    FHotPictureURL: string;
    FOnMouseDown: TIWImageOnMouseDown;
    FTMSTabOrder: integer;

    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);
    procedure SetTMSTabOrder(const Value: integer);
  protected
    procedure PictureChanged(Sender: TObject);
    function ImageHTML(const ASrc,BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    procedure Submit(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
  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;
    //Default IW TabOrder can not be set to -1 (to avoid that the control gets focus)
    property TabOrder: integer read FTMSTabOrder write SetTMSTabOrder default -1;
    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;
    property OnAsyncClick;
  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);
    function ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    procedure Submit(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure IWPaint; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
    function RenderAsync(AContext: TIWBaseHTMLComponentContext): TIWXMLTag; override;
  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;
    property OnAsyncClick;
  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
    function ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
    procedure Submit(const AValue: string); override;
  public
    property Canvas: TCanvas read GetBMPCanvas;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag; override;
  published
    property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
    property OnMouseDown: TIWImageOnMouseDown read FOnMouseDown write SetOnMouseDown;
  end;



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

{ TTIWAdvImage }

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;

constructor TTIWAdvImage.Create(AOwner: TComponent);
begin
  inherited;
  FIPicture := TIPicture.Create;
  FIPicture.OnChange := PictureChanged;
  FHotIPicture := TIPicture.Create;
//  FSupportsSubmit := True;
//  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  FTMSTabOrder := -1;
  FUseSize := True;
  Width := 100;
  Height := 100;
end;

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

procedure TTIWAdvImage.IWPaint;
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;

function TTIWAdvImage.ImageHTML(const ASrc, BSrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
var
//  LTag: TIWHTMLTag;
  ajaxcall, submitclick: string;
  hotpictureset : boolean;
begin
  hotpictureset := false;

  Result := TIWHTMLTag.CreateTag('IMG');
  try
    Result.AddStringParam('SRC', ASrc);
    Result.AddStringParam('NAME', HTMLName);
    Result.AddIntegerParam('BORDER', iif(UseBorder, 1, 0));
    Result.AddStringParam('ALT', AltText);

    if TabOrder >= 0 then
      Result.AddIntegerParam('TABINDEX', TabOrder);

    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
  //    case AContext.WebApplication.Browser of brIE:
        if IsBrowserIe(AContext) then
        begin
          Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
            + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');"');
        end
        else //brNetscape6:
        //Fixed, enable for all non IE browsers
        begin

          Result.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
            + ',' + iif(DoSubmitValidation, 'true', 'false')
            + ',' + '''' + Confirmation + ''''
            + ');"');
        end;
    end;

    if Assigned(OnClick) then
    begin
      submitclick := ' return SubmitClick(''' + HTMLName + ''','''',''false'');';
  //      submitclick := ' return SubmitClickConfirm(''' + HTMLName + ''','', true, '');';
  //      submitclick := submitclick + 'alert(''here'');'
    end;

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

        submitclick := '';
     end;

    // OnMouseDown and OnClick are mutually exclusive, no need to check
    if Assigned(OnClick) or Assigned(OnAsyncClick) then
    begin
      Result.AddStringParam('STYLE', 'cursor:pointer;');

      //fix: differentiate between sync and async events
      if ajaxcall = EmptyStr then
        ScriptEvents.HookEvent('OnClick', submitclick);
      //else
         //ScriptEvents.HookEvent('OnClick', ajaxcall);

      //Memory leak fix
      //Result := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
      //LTag := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);

//    FreeAndNil(Result);
//      Result := LTag;
    end;
  except FreeAndNil(Result); raise; end;
end;

function TTIWAdvImage.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
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
      ImgName := TIWAppCache.GraphicToCacheFile(FIPicture, FIPicture.GetImageType);
  end;

  if HotPictureURL <> EmptyStr then
       ImgHot := HotPictureURL
  else if Assigned(FHotIPicture) then
  begin
    if not FHotIPicture.Empty then
      ImgHot := TIWAppCache.GraphicToCacheFile(FHotIPicture, FHotIPicture.GetImageType);
  end;

  Result := ImageHTML(ImgName,ImgHot, AContext);
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.SetTMSTabOrder(const Value: integer);
begin
  if (Value <> FTMSTabOrder) and (Value >= -1) then
    FTMSTabOrder := 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 }
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;

constructor TTIWFadeImage.Create(AOwner: TComponent);
begin
  inherited;

//  FSupportsSubmit := True;
//  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';

  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;

function TTIWFadeImage.ImageHTML(const ASrc: string; AContext: TIWBaseComponentContext): TIWHTMLTag;
var
  LTag: TIWHTMLTag;
  ajaxcall, submitclick: string;
begin
  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;

  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

//      case AContext.WebApplication.Browser of brIE:
      if IsBrowserIe(AContext) then
      begin
        Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
          + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');');
      end
      else //brNetscape6:
      //Fixed, enable for all non IE browsers
      begin

        Result.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
          + ',' + iif(DoSubmitValidation, 'true', 'false')
          + ',' + '''' + Confirmation + ''''
          + ');');

        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;

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

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

          submitclick := '';
       end;

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

end;

procedure TTIWFadeImage.IWPaint;
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;

function TTIWFadeImage.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
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;

  ImgName := '';

  if PictureURL <> EmptyStr then
    ImgName := PictureURL
  else if Assigned(FIPicture) then
  begin
    if not FIPicture.Empty then
      ImgName := TIWAppCache.GraphicToCacheFile(FIPicture, FIPicture.GetImageType);
  end;

  Result := ImageHTML(ImgName,AContext);
  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;
//  FSupportsSubmit := True;
//  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  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;

function TTIWPaintBox.ImageHTML(const ASrc: String; AContext: TIWBaseComponentContext): TIWHTMLTag;
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
//    case AContext.WebApplication.Browser of brIE:
      if IsBrowserIe(AContext) then
      begin

        Result.AddStringParam('OnMouseDown', 'return ImageClick(this.name,event,'
          + iif(DoSubmitValidation, 'true', 'false') + ', ''' + Confirmation + ''');');

      end
      else //brNetscape6:
      //Fixed, enable for all non IE browsers
      begin

        Result.AddStringParam('OnMouseOver', 'return ImageSetEvent(this,''' + HTMLName + ''''
          + ',' + iif(DoSubmitValidation, 'true', 'false')
          + ',' + '''' + Confirmation + ''''
          + ');');

//        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;

  // OnMouseDown and OnClick are mutually exclusive, no need to check
  if Assigned(OnClick) then begin
    ScriptEvents.HookEvent('OnClick', ' return SubmitClick(''' + HTMLName + ''','''',''false'');');
    //Memory leak fix
    //Result := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
    LTag := SubmitLink(Result.Render, '', Length(ScriptEvents.Values['OnClick']) > 0);
    FreeAndNil(Result);
    Result := LTag;
  end;
end;

function TTIWPaintBox.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
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);

  ImgName := TIWAppCache.GraphicToCacheFile(Apicture, imgJPEG);

  Apicture.Free;
  bmp.Free;

  Result := ImageHTML(ImgName, AContext);
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.
