unit PartsFrames;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls,convImages,shellAPI, StdCtrls, ImgList,ExtAllowedForms;

type

  //PPathctrl=^TPathCtrl;


  TDirListFrame = class(TFrame)
   cellListView: TListView;
   folderSelectCB: TComboBox;
   procedure folderSelectCBChange(Sender: TObject);
   procedure cellListViewDblClick(Sender: TObject);
   procedure cellListViewSelectItem(Sender: TObject; Item: TListItem;
     Selected: Boolean);
  private
    { Private 宣言 }

   oldItem:TListItem;
   FNotifyNameEvent:TNotifyNameEvent;
   procedure setCellHint(hint:string);
   procedure setPathName(pathName:string);
   procedure setFolderIcon();
   procedure setImageList(list:TImageList);
   function setIcon(fileName:string):Integer;
  public
    { Public 宣言 }
   extAllowedDlg:TExtAllowedForm;
   eventlock:boolean;
   depth,dircount:Integer;
   pathctrl:TPathCtrl;
   FAfilter:TFileAllowedFilter;
   procedure exeExtAllowedDlg();
   function getFullFileName():string;
   function isDir():boolean;
   function getDirtype():shortInt;
   function getFileName():string;
   function getSelectedItemName:string;
   constructor create(Owner:TComponent);override;
   procedure setPathctrl(newCtrl:TPathctrl);
   procedure reset();
   procedure setImageFileList(folderName:string);
   destructor destroy();override;
   property cellHint:string write setCellHint;
   property IconImageList:TImageList write setImageList;
   property NotifyNameEvent:TNotifyNameEvent
    read FNotifyNameEvent write FNotifyNameEvent;
  end;

const
  sjScenario=4;
  sjCurrent=1;
  sjTable=2;
  sjScheme=3;

implementation
{$R *.dfm}
var
 wavINum,jpdcINum,jpmpINum,jpyINum,folderINum,bmpINum,jpegINum,jptxINum:Integer;
// PIconImageList:^TImageList;

constructor TDirListFrame.create(Owner:TComponent);
begin
inherited;
 FAfilter:=TFileAllowedFilter.create;
 pathctrl:=TPathctrl.Create();
 extAllowedDlg:=TExtAllowedForm.Create(Owner);
end;

destructor TDirListFrame.destroy();
begin
 oldItem:=nil;
 eventlock:=true;
 cellListView.SmallImages.Clear;
 pathctrl.Free();
 FAfilter.Free();
inherited;
end;

procedure TDirListFrame.reset();
begin
 cellListView.ItemIndex:=-1;
 self.dircount:=-1;
 self.depth:=0;
 oldItem:=nil;
end;

procedure TDirListFrame.setPathctrl(newCtrl:TPathctrl);
begin
 self.eventlock:=true;
 pathctrl.Assign(newCtrl);
 pathctrl.middlePath:='';

 folderSelectCB.Items.Strings[2]:=ExtractFileName(pathctrl.scenarioName);
 folderSelectCB.ItemIndex:=2;
 self.eventlock:=false;
end;

procedure TDirListFrame.setImageList(list:TImageList);
begin
 cellListView.SmallImages:=list;
 setFolderIcon();
end;

(*利用拡張子セット*)
procedure TDirListFrame.exeExtAllowedDlg();
begin

 if(extAllowedDlg.ShowModal()=mrOK)then
 begin
  FAFilter.Free();
  FAFilter:=nil;
  FAFilter:=extAllowedDlg.createAllowedFilter();
  folderSelectCBChange(self);
 end;

end;

(*リスト用のミニアイコン作成*)
function TDirListFrame.setIcon(fileName:string):Integer;
var
 icon:TIcon;
 FileInfo: TSHFileInfo;
begin

 icon:=TIcon.Create();
 icon.Width:=16;
 icon.Height:=16;
 SHGetFileInfo(PChar(fileName)
 ,0,FileInfo,sizeof(FileInfo),SHGFI_ICON or SHGFI_SMALLICON);

 icon.Handle:=fileInfo.hIcon;
 result:=cellListView.SmallImages.AddIcon(icon);

end;

procedure TDirListFrame.setImageFileList(folderName:string);
var
 SR:TSearchRec;
 ExtName:string;
 Found:Integer;
 listItem:TListItem;
begin
 oldItem:=nil;
 eventlock:=true;

 with self.cellListView.Items do
 begin

  Found:=FindFirst(folderName+'\*.*',faDirectory,SR);
  BeginUpdate();
  Clear();
  dircount:=-1;
  ExtName:='';

 (*フォルダと画像ファイルを分割して表示*)
  try
   while Found = 0 do
   begin

   if((SR.Attr and faDirectory)<>0) and(SR.Name<>'.')then
   begin
    if(SR.Name='..')and(depth<1)then
    begin
     Found:=FindNext(SR);
     continue;
    end;

    inc(dircount);
    listItem:=Insert(dircount);
    listItem.ImageIndex:=folderINum;
    listItem.Caption:=SR.Name;
   end
   else
   begin
    ExtName:=AnsiLowerCase(ExtractFileExt(SR.Name));
    if(ExtName='.bmp')and(FAfilter.getAllowed(aebmp))then
    begin

     if(bmpINum=-1)then
      bmpINum:=self.setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=bmpINum;
     listItem.Caption:=SR.Name;
    end
    else if((ExtName='.jpg')or(ExtName='.jpeg'))and(FAfilter.getAllowed(aejpg))then
    begin
     if(jpegINum=-1)then
      jpegINum:=self.setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=jpegINum;
     listItem.Caption:=SR.Name;
    end
    else if(ExtName='.jptx')and(FAfilter.getAllowed(aejptx))then
    begin
     if(jptxINum=-1)then
      jptxINum:=setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=jptxINum;
     listItem.Caption:=SR.Name;
    end
    else if(ExtName='.jpy1')and(FAfilter.getAllowed(aejpy1))then
    begin
    (*同名のファイルは開けない*)
     if(folderName+'\'+SR.Name<>pathctrl.openFileName)then
     begin
      if(jpyINum=-1)then
       jpyINum:=setIcon(folderName+'\'+SR.Name);
      listItem:=Add();
      listItem.ImageIndex:=jpyINum;
      listItem.Caption:=SR.Name;
     end;
    end
    else if(ExtName='.jpmp')then
    begin
     if(jpmpINum=-1)then
      jpmpINum:=setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=jpmpINum;
     listItem.Caption:=SR.Name;
    end
    else if(ExtName='.jpdc')and(FAfilter.getAllowed(aejpdc))then
    begin
     if(jpdcINum=-1)then
      jpdcINum:=setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=jpdcINum;
     listItem.Caption:=SR.Name;
    end
    else if(ExtName='.wav')and(FAfilter.getAllowed(aewave))then
    begin
     if(wavINum=-1)then
      wavINum:=setIcon(folderName+'\'+SR.Name);

     listItem:=Add();
     listItem.ImageIndex:=wavINum;
     listItem.Caption:=SR.Name;
    end;
   end;

   Found:=FindNext(SR); 
  end;
  finally
   sysUtils.FindClose(SR);
  end;

   EndUpdate();   
 end;

  if(cellListView.Items.Count>1)then
   cellListView.ItemIndex:=0;

  eventlock:=false
end;


(*フォルダ用のアイコンをセット*)
procedure TDirListFrame.setFolderIcon();
var
  c:array[0..255] of char;
begin
 if(folderINum=-1)then
 begin
  windows.GetSystemDirectory(c,255);
  folderINum:=setIcon(c);
 end;
end;


(*選択した項目に応じて画像を表示*)
procedure TDirListFrame.setPathName(pathName:string);
begin
 pathctrl.pathName:=pathName;
 self.depth:=0;
 pathctrl.middlePath:='';
 setImageFileList(pathctrl.pathName);
end;

procedure TDirListFrame.folderSelectCBChange(Sender: TObject);
begin

 case self.folderSelectCB.ItemIndex of
 0:setPathName(pathctrl.baseName+'\data\table');
 1:setPathName(pathctrl.baseName+'\Scheme');
 2:
  if(pathctrl.scenarioName<>'')then
  begin
   setPathName(pathctrl.scenarioName);
  end;
 end;

end;

(*リスト内のフォルダを開く*)
procedure TDirListFrame.cellListViewDblClick(Sender: TObject);
var
 dirName:string;
begin
 if(cellListView.ItemIndex>-1)and(cellListView.ItemIndex<=self.dircount)then
 begin
  dirName:=cellListView.Items.Item[cellListView.ItemIndex].Caption;

  if(dirName='..')then
  begin
   dec(depth);
   pathctrl.middlePath:=ExtractFileDir(pathctrl.middlePath);
   pathctrl.PathName:=ExtractFileDir(pathctrl.pathName);
  end
  else
  begin
   if(pathctrl.middlePath='')then
    pathctrl.middlePath:=dirName
   else
    pathctrl.middlePath:=pathctrl.middlePath+'\'+dirName;

   pathctrl.pathName:=pathctrl.pathName+'\'+dirName;
   inc(depth);
  end;

  eventlock:=true;
  setImageFileList(pathctrl.pathName);
  eventlock:=false;
//  if(cellListView.Items.Count>-1)then
//   cellListView.ItemIndex:=0;
 end;
end;


function TDirListFrame.isDir():boolean;
begin
 result:=cellListView.ItemIndex>dircount;
end;

function TDirListFrame.getSelectedItemName:string;
begin
 result:=cellListView.Items.Item[cellListView.ItemIndex].Caption;
end;

function TDirListFrame.getDirtype():shortInt;
begin
  case folderSelectCB.ItemIndex of
   0:result:=sjTable;
   1:result:=sjScheme;
   2:if(pathctrl.hasSummary)then result:=sjScenario
     else result:=sjCurrent;
   else result:=-1;
  end;
end;

function TDirListFrame.getFileName():string;
begin
  if(cellListView.Selected<>nil)then
  begin
   if(pathctrl.middlePath='')then
    result:=getSelectedItemName()
   else
    result:=pathctrl.middlePath+'\'+getSelectedItemName();
  end
  else result:='';
end;

procedure TDirListFrame.cellListViewSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
 if(assigned(FNotifyNameEvent))and not(eventlock)then
 begin
  if(olditem<>item)then
  begin
   olditem:=item;
   self.eventlock:=true;
   FNotifyNameEvent(pathctrl.pathName+'\'+item.Caption);
   self.eventlock:=false;
  end;
 end;
end;

procedure TDirListFrame.setCellHint(hint:string);
begin
 cellListView.Hint:=hint;
end;

function TDirListFrame.getFullFileName():string;
begin
 result:=pathctrl.pathName+'\'+self.cellListView.Selected.Caption;
end;



initialization
jpmpINum:=-1;
jpyINum:=-1;
folderINum:=-1;
bmpINum:=-1;
jpegINum:=-1;
jptxINum:=-1;
jpdcINum:=-1;
wavINum:=-1;



end.
