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

interface

{$I TMSDEFS.INC}

uses
  SysUtils, Types, UITypes, Classes, Variants, FMX.TMSBaseControl,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Objects
  {$IFDEF DELPHIXE5_LVL}
  , FMX.Graphics
  {$ENDIF}
  ;

const
  MAJ_VER = 1; // Major version nr.
  MIN_VER = 5; // Minor version nr.
  REL_VER = 0; // Release nr.
  BLD_VER = 0; // Build nr.

  // version history
  // v1.0.0.0 : First Release
  // v1.1.0.3 : New : Designtime support
  //          : Reorganized packages
  // v1.1.1.0 : New : support for update 4
  // v1.1.1.1 : Improved : TMSIOS define
  // v1.1.1.2 : New : support for update 4 hotfix 1
  // v1.5.0.0 : New : support for XE4

type

  TTMSFMXBitmapItem = class(TCollectionItem)
  private
    FBitmap: TBitmap;
    FTag: Integer;
    FName: string;
    procedure SetBitmap(const Value: TBitmap);
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
    function GetDisplayName: string; override;
  published
    property Bitmap: TBitmap read FBitmap write SetBitmap;
    property Name: string read FName write FName;
    property Tag: Integer read FTag write FTag;
  end;

  TTMSFMXBitmapCollection = class(TTMSFMXBaseCollection)
  private
    FOwner: TComponent;
    function GetItem(Index: Integer): TTMSFMXBitmapItem;
    procedure SetItem(Index: Integer; Value: TTMSFMXBitmapItem);
  protected
    function GetOwner: TPersistent; override;
  public
    constructor Create(AOwner:TComponent);
    function Add: TTMSFMXBitmapItem;
    function Insert(index: Integer): TTMSFMXBitmapItem;
    property Items[Index: Integer]: TTMSFMXBitmapItem read GetItem write SetItem; default;
  end;

  [ComponentPlatformsAttribute(TMSPlatforms)]
  TTMSFMXBitmapContainer = class(TComponent)
  private
    FItems: TTMSFMXBitmapCollection;
    procedure SetItems(const Value: TTMSFMXBitmapCollection);
    function GetVersion: string;
    function GetVersionNr: Integer;
    procedure SetVersion(const Value: string);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function FindBitmap(s: string): TBitmap; virtual;
  published
    { Published declarations }
    property Items: TTMSFMXBitmapCollection read FItems write SetItems;
    property Version: string read GetVersion write SetVersion;
  end;


implementation


{ TTMSFMXBitmapItem }

procedure TTMSFMXBitmapItem.Assign(Source: TPersistent);
begin
  Name := (Source as TTMSFMXBitmapItem).Name;
  Tag := (Source as TTMSFMXBitmapItem).Tag;
  Bitmap.Assign((Source as TTMSFMXBitmapItem).Bitmap)
end;

constructor TTMSFMXBitmapItem.Create(Collection: TCollection);
begin
  inherited;
  FBitmap := TBitmap.Create(0,0);
  FName := 'Item'+ IntToStr(Collection.Count);
end;

destructor TTMSFMXBitmapItem.Destroy;
begin
  FBitmap.Free;
  inherited;
end;

function TTMSFMXBitmapItem.GetDisplayName: string;
begin
  if FName <> '' then
    Result := FName
  else
    Result := inherited;
end;

procedure TTMSFMXBitmapItem.SetBitmap(const Value: TBitmap);
begin
  FBitmap.Assign(Value);
end;

{ TTMSFMXBitmapCollection }

function TTMSFMXBitmapCollection.Add: TTMSFMXBitmapItem;
begin
  Result := TTMSFMXBitmapItem(inherited Add);
end;


constructor TTMSFMXBitmapCollection.Create(AOwner: TComponent);
begin
  inherited Create(TTMSFMXBitmapItem);
  FOwner := AOwner;
end;

function TTMSFMXBitmapCollection.GetItem(Index: Integer): TTMSFMXBitmapItem;
begin
  Result := TTMSFMXBitmapItem(inherited Items[Index]);
end;

function TTMSFMXBitmapCollection.GetOwner: TPersistent;
begin
  Result := FOwner;
end;

function TTMSFMXBitmapCollection.Insert(index: Integer): TTMSFMXBitmapItem;
begin
  Result := TTMSFMXBitmapItem(inherited Insert(Index));
end;

procedure TTMSFMXBitmapCollection.SetItem(Index: Integer;
  Value: TTMSFMXBitmapItem);
begin
  inherited SetItem(Index, Value);
end;

{ TTMSFMXBitmapContainer }

constructor TTMSFMXBitmapContainer.Create(AOwner: TComponent);
begin
  inherited;
  FItems := TTMSFMXBitmapCollection.Create(Self);
end;

destructor TTMSFMXBitmapContainer.Destroy;
begin
  FItems.Free;
  inherited;
end;

function TTMSFMXBitmapContainer.FindBitmap(s: string): TBitmap;
var
  i: Integer;
begin
  Result := nil;
  s := Uppercase(s);
  i := 1;
  while i <= Items.Count do
  begin
    if Uppercase(Items.Items[i - 1].Name) = s then
    begin
      Result := Items.Items[i - 1].Bitmap;
      Break;
    end;
    Inc(i);
  end;
end;

procedure TTMSFMXBitmapContainer.SetItems(const Value: TTMSFMXBitmapCollection);
begin
  FItems := Value;
end;

function TTMSFMXBitmapContainer.GetVersion: string;
var
  vn: Integer;
begin
  vn := GetVersionNr;
  Result := IntToStr(Hi(Hiword(vn)))+'.'+IntToStr(Lo(Hiword(vn)))+'.'+IntToStr(Hi(Loword(vn)))+'.'+IntToStr(Lo(Loword(vn)));
end;

function TTMSFMXBitmapContainer.GetVersionNr: integer;
begin
  Result := MakeLong(MakeWord(BLD_VER,REL_VER),MakeWord(MIN_VER,MAJ_VER));
end;

procedure TTMSFMXBitmapContainer.SetVersion(const Value: string);
begin

end;

end.
