{*************************************************************************}
{                                                                         }
{ written by TMS Software                                                 }
{           copyright © 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 FMX.TMSHotSpotImageDE;

interface

{$I TMSDEFS.INC}

uses
  Classes, FMX.TMSHotSpotImageEditor, FMX.Forms, FMX.TMSHotSpotImage, FMX.Controls, UITypes
  {$IFDEF WIN32}
  ,DesignIntf, DesignEditors
  {$ENDIF}
  ;


  {$IFDEF WIN32}
type
  TTMSFMXHotSpotImageEditor = class(TDefaultEditor)
    function GetVerbCount: Integer; override;
    function GetVerb(Index: Integer): String; override;
    procedure ExecuteVerb(Index: Integer); override;
    procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
  end;

  TTMSFMXHotSpotEditor = class(TPropertyEditor)
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: String; override;

    procedure TransferToHotSpotImage;
    procedure TransferFromHotSpotImage;
  end;
  {$ENDIF}


implementation


{$IFDEF WIN32}

uses
  SysUtils, ShlObj, ActiveX,  Windows;

procedure FreePidl( pidl: PItemIDList );
var
  allocator: IMalloc;
begin
  if Succeeded(SHGetMalloc(allocator)) then
    Allocator.Free(pidl);
end;

function GetMyDocuments: string;
var
  pidl: PItemIDList;
  Path: array [0..MAX_PATH-1] of char;
begin
  Result := '';

  if Succeeded(
       SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl)
     ) then
  begin
    if SHGetPathFromIDList(pidl, Path) then
      Result := StrPas(Path);
    FreePidl(pidl);
  end;
end;

procedure TTMSFMXHotSpotEditor.Edit;
var
  frmEditor: TTMSFMXHotSpotImageEditorForm;
  s:string;
begin
  TransferFromHotSpotImage;
  frmEditor := TTMSFMXHotSpotImageEditorForm.Create(nil);

  s := GetMyDocuments;
  s := IncludeTrailingBackslash(s);

  frmEditor.LoadSettings(s + 'hotspotedit.ini');
  try
    if frmEditor.ShowModal = mrOk then
    begin
      TransferToHotSpotImage;
      Modified;
    end;
    frmEditor.SaveSettings(s + 'hotspotedit.ini');
  finally
    frmEditor.Free;
  end;
end;

function TTMSFMXHotSpotEditor.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;

function TTMSFMXHotSpotEditor.GetValue: String;
begin
  Result := '(THotSpots)';
end;

{ TTMSFMXHotSpotImageEditor }

procedure TTMSFMXHotSpotImageEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
begin
  if PropertyEditor.GetName = 'HotSpots' then
  begin
    PropertyEditor.Edit;
    Continue := False;
  end
  else inherited;
end;

procedure TTMSFMXHotSpotImageEditor.ExecuteVerb(Index: Integer);
begin
  Edit;
end;

function TTMSFMXHotSpotImageEditor.GetVerb(Index: Integer): String;
begin
  Result := 'Edit hotspots...';
end;

function TTMSFMXHotSpotImageEditor.GetVerbCount: Integer;
begin
  Result := 1;
end;

procedure TTMSFMXHotSpotEditor.TransferFromHotSpotImage;
var
  Comp: TTMSFMXHotSpotImage;
begin
  if propCount = 0 then
    Exit;
  Comp := TTMSFMXHotSpotImage(GetComponent(0));
  if Comp = nil then
    Exit;
  if FHotSpots_org = nil then
    Exit;
  if FPicture = nil then
    Exit;
  if FHotSpots = nil then
    Exit;

  BitmapContainer := Comp.BitmapContainer;
  FHotSpots_org.Assign(Comp.HotSpots);
  FPicture.Assign(Comp.Bitmap);
  if Comp.Stretch then
    FHotSpots_org.ReScale(FPicture.Width,FPicture.Height);
  FHotSpots.Assign(FHotSpots_org);
  HotSpotsChanged:=false;
end;

procedure TTMSFMXHotSpotEditor.TransferToHotSpotImage;
var
  Comp: TTMSFMXHotSpotImage;
begin
  Comp := TTMSFMXHotSpotImage(GetComponent(0));
  Comp.HotSpots.Assign(FHotSpots);
  if Comp.Stretch then
    Comp.HotSpots.ReScale(Comp.Width,Comp.Height)
  else
    Comp.HotSpots.ReScale(Comp.Bitmap.Width,Comp.Bitmap.Height);
  Comp.Bitmap := FPicture;
  Comp.RePaint;
end;

{$ENDIF}

end.

