{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ 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.                                      }
{***************************************************************************}

{$I TMSDEFS.INC}

unit IWTMSButton;

interface

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

type
  TTIWAdvImageButton = class(TIWControl)
  private
    FIPicture: TIPicture;
    FUseBorder: Boolean;
    FUseSize: Boolean;
    FAltText: string;
    FOnClickEvent : string;
    FHotIPicture: TIPicture;
    FDownIPicture: TIPicture;
    FDisabledIPicture: TIPicture;
    FEnabled: Boolean;
    FFilePicture: string;
    FFileHotPicture: string;
    FFileDownPicture: string;
    FFileDisabledPicture: string;
    procedure SetIPicture(const Value: TIPicture);
    procedure SetHotIPicture(const Value: TIPicture);
    procedure SetUseSize(const Value: Boolean);
    function GetOnClick: TNotifyEvent;
    procedure SetOnClick(const Value: TNotifyEvent);
    procedure SetDownIPicture(const Value: TIPicture);
    procedure SetDisabledIPicture(const Value: TIPicture);
  protected
    procedure PictureChanged(Sender: TObject);
    function ImageHTML(const ASrc,BSrc,CSrc,DSrc: string;Enabled: boolean): TIWHTMLTag;
    procedure Submit(const AValue: string); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Loaded; override;
    procedure MakeScript;
    {$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 HotPicture: TIPicture read FHotIPicture write SetHotIPicture;
    property DownPicture: TIPicture read FDownIPicture write SetDownIPicture;
    property DisabledPicture: TIPicture read FDisabledIPicture write SetDisabledIPicture;
    property FilePicture: string read FFilePicture write FFilePicture;
    property FileHotPicture: string read FFileHotPicture write FFileHotPicture;
    property FileDownPicture: string read FFileDownPicture write FFileDownPicture;
    property FileDisabledPicture: string read FFileDisabledPicture write FFileDisabledPicture;
    property UseBorder: Boolean read FUseBorder write FUseBorder;
    property UseSize: Boolean read FUseSize write SetUseSize;
    property Enabled: Boolean read FEnabled write FEnabled default true;
    property TabOrder;
    property OnClick: TNotifyEvent read GetOnClick write SetOnClick;
    {$IFDEF TMSIW9}
    property OnAsyncClick;
    {$ENDIF}
  end;


implementation

uses
  IWAppForm, IWForm, IWServerControllerBase, SWSystem, SWStrings;

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

end;
{$ENDIF}

constructor TTIWAdvImageButton.Create(AOwner: TComponent);
begin
  inherited;
  FIPicture := TIPicture.Create;
  FIPicture.OnChange := PictureChanged;
  FHotIPicture := TIPicture.Create;
  FDownIPicture := TIPicture.Create;
  FDisabledIPicture := TIPicture.Create;
  FEnabled := true;
  {$IFDEF TMSIW72_NEW}
  FTabOrder := 0;
  {$ENDIF}
  {$IFDEF TMSIW6}
  {TODO}
  {$ELSE}
  FSupportsSubmit := True;
  FSupportedScriptEvents := 'OnAbort,OnError,OnKeyDown,OnKeyPress,OnKeyUp,OnLoad';
  {$ENDIF}
  FUseSize := True;
  Width := 100;
  Height := 100;
  FOnClickEvent := '';
end;

destructor TTIWAdvImageButton.Destroy;
begin
  FIPicture.Free;
  FHotIPicture.Free;
  FDownIPicture.Free;
  FDisabledIPicture.Free;
  inherited;
end;

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

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

  {$IFNDEF LINUX}
  if Enabled or FDisabledIPicture.Empty then
  begin
    if UseSize then
      Canvas.StretchDraw(ClientRect,FIPicture)
    else
      Canvas.Draw(0,0,FIPicture)
  end
  else
  begin
    if UseSize then
      Canvas.StretchDraw(ClientRect,FDisabledIPicture)
    else
      Canvas.Draw(0,0,FDisabledIPicture)
  end;
  {$ENDIF}
end;

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

function TTIWAdvImageButton.ImageHTML(const ASrc, BSrc, CSrc, DSrc: string; Enabled: boolean): TIWHTMLTag;
begin
  if Enabled then
    Result := TIWHTMLTag.CreateTag('A')
  else
    Result := TIWHTMLTag.CreateTag('DIV');

  Result.AddStringParam('NAME', HTMLName);
  Result.AddStringParam('HREF', '#');

  if not Enabled then
    Result.Contents.AddText('<IMG SRC="' + DSrc + '" style="border:'+ iif(UseBorder, '1px solid black', '0') +'" ')
  else
  {$IFDEF TMSIW6}
    Result.Contents.AddText('<IMG SRC="' + ASrc + '" style="border:'+ iif(UseBorder, '1px solid black', '0') +'" ');
  {$ELSE}
    Result.Contents.AddText('<IMG onclick="'+HTMLName+'click();" SRC="' + ASrc + '" style="border:'+ iif(UseBorder, '1px solid black', '0') +'" ');
  {$ENDIF}

  if UseSize then
  begin
    Result.Contents.AddText('width="' + IntToStr(Width) + '" ');
    Result.Contents.AddText('height="' + IntToStr(Height)+ '" ');
  end;

  if Enabled then
  begin
    Result.Contents.AddText('OnMouseOut="this.src='#39+ASrc+#39';" ');

    if Assigned(FHotIPicture) or (BSrc <> '') then
    begin
      if not FHotIPicture.Empty or (BSrc <> '') then
      begin
        Result.Contents.AddText('OnMouseOver="this.src='#39+BSrc+#39';" ');
      end;
    end;

    if Assigned(FDownIPicture) or (CSrc <> '') then
    begin
      if not FDownIPicture.Empty or (CSrc <> '') then
      begin
        Result.Contents.AddText('OnMouseDown="this.src='#39+CSrc+#39';" ');

        if (BSrc <> '') then
          Result.Contents.AddText('OnMouseUp="this.src='#39+BSrc+#39';" ')
        else
          Result.Contents.AddText('OnMouseUp="this.src='#39+ASrc+#39';" ');
      end;
    end;
  end;

  Result.Contents.AddText('>');

  //changed : double closing tags
  {*
  if Enabled then
    Result.Contents.AddText('</A>')
  else
    Result.Contents.AddText('</DIV>');
    *}
end;

{$IFDEF TMSIW6}
function TTIWAdvImageButton.RenderHTML(AContext: TIWBaseComponentContext): TIWHTMLTag;
{$ELSE}
function TTIWAdvImageButton.RenderHTML: TIWHTMLTag;
{$ENDIF}
var
  ImgName,ImgHot,ImgDown,ImgDis: string;
  htmlres, submitclick: string;
  scriptOnClick: TStrings;
begin
  htmlres := '';
  scriptOnClick := TStringList.Create;
  try
    with scriptOnClick do
    begin
      Add('function '+ HTMLName + 'click() {');
      if (Length(FOnClickEvent) > 0) then
        Add(FOnClickEvent + #13);                     

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

          {$IFDEF TMSIW9}
          //no need to assign the asyncclick
          //it's automatically added because asyncclick is a default property
          {
          if Assigned(FOnAsyncClick) then
          begin
            submitclick := 'processAjaxEvent(''onClick'', '
              + HTMLControlImplementation.IWCLName
              + ',''' + HTMLName + '.' + 'DoOnAsyncClick' + ''','
              + 'true' + ', null, '
              + 'true' + ');';
           end;
           }
          {$ENDIF}

          Add(submitclick);   
        end;


      Add('}'+#13);
    end;
    {$IFDEF TMSIW6}
    TIWComponent40Context(AContext).AddToJavaScriptOnce(scriptOnClick.Text);
    {$ELSE}
    AddToJavaScriptOnce(scriptOnClick.Text);
    {$ENDIF}
  finally
    FreeAndNil(scriptOnClick);
  end;

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

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

  if Assigned(FIPicture) and (FFilePicture = '') 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 Assigned(FHotIPicture) and (FFileHotPicture = '') 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 Assigned(FDownIPicture) and (FFileDownPicture = '') then
  begin
    if not FDownIPicture.Empty then
    begin
      if FDownIPicture.IsGIF then
      {$IFDEF TMSIW71}
        ImgDown := TIWServerControllerBase.NewCacheFile('gif',true)
      {$ELSE}
        ImgDown := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ImgDown := TIWServerControllerBase.NewCacheFile('jpg',true);
      {$ELSE}
        ImgDown := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}
      FDownIPicture.SaveToFile(ImgDown);
    end;
  end;


  if Assigned(FDisabledIPicture) and (FFileDisabledPicture = '') then
  begin
    if not FDisabledIPicture.Empty then
    begin
      if FDisabledIPicture.IsGIF then
      {$IFDEF TMSIW71}
        ImgDis := TIWServerControllerBase.NewCacheFile('gif',true)
      {$ELSE}
        ImgDis := TIWServerControllerBase.NewCacheFile('gif')
      {$ENDIF}
      else
      {$IFDEF TMSIW71}
        ImgDis := TIWServerControllerBase.NewCacheFile('jpg',true);
      {$ELSE}
        ImgDis := TIWServerControllerBase.NewCacheFile('jpg');
      {$ENDIF}
      FDisabledIPicture.SaveToFile(ImgDis);
    end;
  end;

  if FFilePicture <> '' then
    ImgName := FFilePicture
  else
    ImgName := GetCacheDir(Self) + ExtractFilename(ImgName);

  if FFileHotPicture <> '' then
    ImgHot := FFileHotPicture
  else
    if not FHotIPicture.Empty then
      ImgHot := GetCacheDir(Self) + ExtractFilename(ImgHot);

  if FFileDownPicture <> '' then
    ImgDown := FFileDownPicture
  else
    if not FDownIPicture.Empty then
      ImgDown := GetCacheDir(Self) + ExtractFilename(ImgDown);

  if FFileDisabledPicture <> '' then
    ImgDis := FFileDisabledPicture
  else
    ImgDis := GetCacheDir(Self) + ExtractFilename(ImgDis);

  Result := ImageHTML(ImgName, ImgHot, ImgDown, ImgDis, Enabled);
end;

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

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

procedure TTIWAdvImageButton.SetDownIPicture(const Value: TIPicture);
begin
  FDownIPicture.Assign(Value);
  Invalidate;
end;

procedure TTIWAdvImageButton.SetDisabledIPicture(const Value: TIPicture);
begin
  FDisabledIPicture.Assign(Value);
  Invalidate;
end;

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

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

procedure TTIWAdvImageButton.SetOnClick(const Value: TNotifyEvent);
begin
  inherited OnClick := Value;
end;

procedure TTIWAdvImageButton.Submit(const AValue: string);
begin
  DoClick;
end;

procedure TTIWAdvImageButton.Loaded;
begin
  inherited;
  if not (csDesigning in ComponentState) then
  begin
    MakeScript;
  end;
end;

procedure TTIWAdvImageButton.MakeScript;
var
  i,indexOnClick: integer;
begin
  if (Length(ScriptEvents.Values['OnClick']) > 0) then
    FOnClickEvent := ScriptEvents.Values['OnClick'];

  indexOnClick := 0;
  if (ScriptEvents.Count > 0) then
  begin
    for i :=0 to ScriptEvents.Count -1 do
    begin
      if (ScriptEvents.Items[i].GetNamePath = 'onClick') then
        indexOnClick := i;
    end;
    ScriptEvents.Delete(indexOnClick);
  end;

  ScriptEvents.Add('onClick');
  ScriptEvents.HookEvent('onClick',HTMLName+'click()');
end;


end.

