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

unit IWGallery;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, INIFiles,
  IWGIFPreview, IWPropPersist;

type
  TGallery = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Label3: TLabel;
    TIWGIFPreview1: TTIWGIFPreview;
    IWPropertyPersister1: TIWPropertyPersister;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    FComponent: TComponent;
    FFilePath: string;
  public
    { Public declarations }
    procedure FillList;
    property Component: TComponent read FComponent write FComponent;
    property FilePath: string read FFilePath write FFilePath;

  end;

var
  Gallery: TGallery;

implementation

{$R *.dfm}

procedure TGallery.Button1Click(Sender: TObject);
var
  AINIFile: TINIFile;
begin
  if (Edit1.Text = '') or (Listbox1.Items.IndexOf(Edit1.Text) <> -1) then
  begin
    ShowMessage('No valid or already existing preference name choosen');
    Exit;
  end;

  AINIFile := TINIFile.Create(FFilePath + Edit1.Text+'.INI');

  IWPropertyPersister1.IniSaveRTTIComponent(AINIFile,Component,false);

  AINIFile.Free;

  listbox1.Items.Add(Edit1.Text);
end;

procedure TGallery.Button2Click(Sender: TObject);
begin
  if listbox1.ItemIndex >= 0 then
  begin
    if MessageDlg('Are you sure to delete this gallery item ?',mtConfirmation,[mbYes,mbNo],0) = mrYes then
    begin
      if fileExists(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.INI') then
        DeleteFile(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.INI');
      if fileExists(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.GIF') then
        DeleteFile(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.GIF');
      listbox1.Items.Delete(listbox1.ItemIndex);
    end;
  end;
end;

{$WARNINGS OFF}
procedure TGallery.FillList;
var
  sr: TSearchRec;
  FileAttrs: Integer;
  s:string;
begin
  FileAttrs := faArchive;
  if FindFirst(FFilePath + '*.INI', FileAttrs, sr) = 0 then
  begin
    repeat
      if (sr.Attr and FileAttrs) <> 0 then
      begin
        s := ExtractFileName(sr.Name);
        delete(s,length(s) - 3,4);
        listbox1.Items.Add(s);
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;
{$WARNINGS ON}

procedure TGallery.ListBox1Click(Sender: TObject);
begin
  if listbox1.ItemIndex >= 0 then
  begin
    if FileExists(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.GIF') then
      TIWGIFPreview1.Picture.LoadFromFile(FFilePath + ListBox1.Items[ListBox1.ItemIndex] + '.GIF')
    else
    begin
      TIWGIFPreview1.Picture := nil;
    end;
    TIWGIFPreview1.Invalidate;
  end;
end;

procedure TGallery.Button3Click(Sender: TObject);
var
  AIniFile: TIniFile;

begin
  if listbox1.ItemIndex >= 0 then
  begin

    AINIFile := TINIFile.Create(FFilePath + listbox1.Items[listbox1.ItemIndex]+'.INI');

    IWPropertyPersister1.IniLoadRTTIComponent(AINIFile,Component,false);

    AINIFile.Free;
  end;
end;

end.
