{*************************************************************************}
{                                                                         }
{ 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.TMSBitmapContainerDE;

interface

{$I TMSDEFS.INC}

uses
  Classes, Types, FMX.TMSBitmapContainer, FMX.Types
  {$IFDEF WIN32}
  , DesignIntf, DesignEditors, rtti
  {$ENDIF}
  ;

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

  TTMSFMXBitmapNameProperty = class(TStringProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure GetValues(Proc:TGetStrProc); override;
  end;
{$ENDIF}

implementation

{$IFDEF WIN32}
uses
  FMX.Dialogs, UITypes, SysUtils;
{$ENDIF}


{ TTMSFMXBitmapContainerEditor }

{$IFDEF WIN32}

procedure TTMSFMXBitmapContainerEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
var
  PropName: string;
begin
  PropName := PropertyEditor.GetName;
  if (CompareText(PropName, 'ITEMS') = 0) then
  begin
    PropertyEditor.Edit;
    Continue := False;
  end;
end;

procedure TTMSFMXBitmapContainerEditor.ExecuteVerb(Index: Integer);
var
  ofd: TOpenDialog;
  bmp: TTMSFMXBitmapContainer;
  i: integer;
  str: String;
begin
  inherited;
  bmp := nil;
  if Component is TTMSFMXBitmapContainer then
    bmp := Component as TTMSFMXBitmapContainer;

  if Assigned(bmp) then
  begin
    case Index of
      0, 1:
      begin
        ofd := TOpenDialog.Create(bmp);
        ofd.Options := [TOpenOption.ofAllowMultiSelect, TOpenOption.ofFileMustExist];
        ofd.Filter := 'All (*.gif;*.png;*.jpg;*.jpeg;*.bmp;*.tif;*.tiff;*.ico;*.emf;*.wmf)|*.gif;'+
        '*.png;*.jpg;*.jpeg;*.bmp;*.tif;*.tiff;*.ico;*.emf;*.wmf|GIF Image (*.gif)|*.gif|Portable Network Graphics (*.png)|*.png|'+
        'JPEG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg|Bitmaps (*.bmp)|*.bmp|TIFF Images (*.tif)|*.tif|TIFF Images'+
        '(*.tiff)|*.tiff|Icons (*.ico)|*.ico|Enhanced Metafiles (*.emf)|*.emf|Metafiles (*.wmf)|*.wmf';
        if ofd.Execute then
        begin
          if Index = 0 then
            bmp.Items.Clear;

          for I := 0 to ofd.Files.Count - 1 do
          begin
            with bmp.Items.Add do
            begin
              str := ofd.Files[I];
              Bitmap.LoadFromFile(str);
              Name := ExtractFileName(str)
            end;
          end;
        end;
        ofd.Free;
      end;
      2: bmp.Items.Clear;
    end;
  end;
end;

function TTMSFMXBitmapContainerEditor.GetVerb(Index: Integer): string;
begin
  case Index of
  0: Result := 'Load files from folder...';
  1: Result := 'Add files from folder...';
  2: Result := 'Clear files';
  end;
end;

function TTMSFMXBitmapContainerEditor.GetVerbCount: Integer;
begin
  Result := 3;
end;

{ TTMSFMXBitmapNameProperty }

function TTMSFMXBitmapNameProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paValueList, paSortList, paValueEditable];
end;

procedure TTMSFMXBitmapNameProperty.GetValues(Proc: TGetStrProc);
var
  p: TPersistent;
  b: TTMSFMXBitmapContainer;
  c : TRttiContext;
  t : TRttiInstanceType;
  i: Integer;
  val: TValue;
  obj: TObject;
  rt: TRttiProperty;
begin
  p := GetComponent(0);
  if Assigned(p) then
  begin
    c := TRttiContext.Create;
    try
      t := (c.GetType(p.ClassType) as TRttiInstanceType);
      rt := t.GetProperty('BitmapContainer');
      if Assigned(rt) then
      begin
        val := rt.GetValue(p);
        if not val.IsEmpty then
        begin
          obj := val.AsObject;
          if Assigned(obj) and (obj is TTMSFMXBitmapContainer) then
          begin
            b := obj as TTMSFMXBitmapcontainer;
            for I := 0 to b.Items.Count - 1 do
              Proc(b.Items[I].Name);
          end;
        end;
      end;
    finally
      c.Free;
    end;
  end;
end;


{$ENDIF}

end.
