unit JPYTemplateForms;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Contnrs,
  Dialogs, PartsFrames, StdCtrls, Buttons, ExtCtrls,convImages, ImgList,FileCtrl,
  iniFiles,shellAPI;

type
  TBlankInf = class(TObject)
   fileName,sectionName:string;
   dirType:Integer;
   constructor create(sectionName,fileName:string;dirType:Integer);overload;
  end;

  TBlankLoader = class(TObject)
   comment:string;
   function loadBlank(fileName:string):TObjectList;
  end;

  TBlankWriter = class(TObject)
   InFileName:string;
   outFileName:string;
   dirDepth:Integer;
   procedure copyFile(inFileName,outFileName:string);
   procedure saveBlank(list:TObjectList);
  end;

  TJPYTmpForm = class(TForm)
    DirListFrame1: TDirListFrame;
    Image1: TImage;
    saveBitBtn: TBitBtn;
    SectionLabel: TLabel;
    resetBitBtn: TBitBtn;
    ImageList1: TImageList;
    prevBitBtn: TBitBtn;
    nextBitBtn: TBitBtn;
    entryBitBtn: TBitBtn;
    FileNameEdit: TEdit;
    BitBtn3: TBitBtn;
    Panel1: TPanel;
    pageCountLabel: TLabel;
    BitBtn1: TBitBtn;
    sourceBitBtn: TBitBtn;
    extBB: TBitBtn;
    SaveDialog1: TSaveDialog;
    Bevel1: TBevel;
    Bevel2: TBevel;
    procedure resetBitBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure entryBitBtnClick(Sender: TObject);
    procedure nextBitBtnClick(Sender: TObject);
    procedure prevBitBtnClick(Sender: TObject);
    procedure saveBitBtnClick(Sender: TObject);
    procedure FileNameEditChange(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure sourceBitBtnClick(Sender: TObject);
    procedure extBBClick(Sender: TObject);
  private
    { Private 宣言 }
    pageCount:Integer;
    changeList:TObjectList;
    director:TEDDirector;
    loadFileName:string;
    nowFileName:string;
    saveFileName:string;
    function checkDepth(const fileDir:string):Integer;
 //   function countSeparator(fileName:string):Integer;
    function loadTemplate(fileName:string):boolean;
    procedure setPageCount(count:Integer);
    procedure loadImage(fileName:string);
    procedure setOutDir();
  public
    { Public 宣言 }
    pathctrl:TPathCtrl;
    procedure openWizard(fileName:string);
  end;

var
  JPYTmpForm: TJPYTmpForm;

implementation

{$R *.dfm}

(*TBlankInf*)
constructor TBlankInf.create(sectionName,fileName:string;dirType:Integer);
begin
 inherited create();
 self.sectionName:=sectionName;
 self.fileName:=fileName;
 self.dirType:=dirType;
end;

(*TBlankLoader*)
function TBlankLoader.loadBlank(fileName:string):TObjectList;
var
 ini:TIniFile;
 i:Integer;
 sList:TStringList;
 secfileName:string;
begin

 ini:=TIniFile.Create(fileName);
 result:=TObjectList.Create();
 sList:=TStringList.Create();

 try
  ini.ReadSections(sList);
  self.comment:=ini.ReadString('init','comment','コメントは特にありません');
  for i:=0 to sList.Count-1 do
  begin
   secfileName:=ini.ReadString(sList.Strings[i],'fileName','');
   if(secfileName='?')then
    result.Add(TBlankInf.create(sList.Strings[i],secfileName,0))
  end;

 finally
  ini.Free();
  sList.Free();
 end;

end;
(*writer*)
procedure TBlankWriter.copyFile(inFileName,outFileName:string);
//var
// stream:TMemoryStream;
begin
// stream:=TMemoryStream.Create;
 try
 // stream.LoadFromFile(inFileName);
  if not(DirectoryExists(ExtractFileDir(outFileName)))then
   ForceDirectories(ExtractFileDir(outFileName));
 // copyFile
 // stream.Position:=0;
 // stream.SaveToFile(outFileName);
 finally
 // stream.Free();
 end;
end;


procedure TBlankWriter.saveBlank(list:TObjectList);
var
 sList:TStringList;
 ini:TIniFile;
 i:Integer;
 inf:TBlankInf;
 fileName:string;
 outDir,inDir:string;
begin
 sList:=TStringList.Create();
 try
  sList.LoadFromFile(InFileName);
  if not(DirectoryExists(ExtractFileDir(outFileName)))then
   ForceDirectories(ExtractFileDir(outFileName));
  sList.SaveToFile(outFileName);
  ini:=TIniFile.Create(outFileName);

  ini.WriteInteger('init','dirdepth',dirDepth);

  for i:=0 to list.Count-1 do
  begin
   inf:=TBlankInf(list[i]);
   ini.WriteString(inf.sectionName,'fileName',inf.fileName);
   ini.WriteInteger(inf.sectionName,'dirType',inf.dirType);
  end;

  ini.ReadSections(sList);
  outDir:=ExtractFileDir(outFileName);
  inDir:=ExtractFileDir(inFileName);

  for i:=0 to sList.Count-1 do
  begin
   if(ini.ReadInteger(sList.Strings[i],'dirType',1)=1)then
   begin
    fileName:=ini.ReadString(sList.Strings[i],'fileName','?');
    if(fileName<>'?')then
    begin
     if not(DirectoryExists(ExtractFileDir(outDir+'\'+FileName)))then
     ForceDirectories(ExtractFileDir(outDir+'\'+FileName));
     windows.CopyFile(PChar(inDir+'\'+FileName),
     PChar(outDir+'\'+FileName),true);
    end;
    //and
  {    (not fileExists(outDir+'\'+FileName))and
      (fileExists(inDir+'\'+FileName))then
      self.copyFile(inDir+'\'+FileName,outDir+'\'+FileName);  }
   end;
  end;

 finally
  sList.Free();
 end;

end;



(*TJPYForm*)

procedure TJPYTmpForm.setOutDir();
begin
 DirListFrame1.setPathctrl(pathctrl);
 DirListFrame1.setImageFileList(pathctrl.scenarioName);
end;


function TJPYTmpForm.loadTemplate(fileName:string):boolean;
var
 loader:TBlankLoader;
begin
 setOutDir();
 result:=fileExists(fileName);
 if(result)then
 begin

  changeList.Free();
  loader:=TBlankLoader.Create();
  changeList:=loader.loadBlank(fileName);
  self.loadFileName:=fileName;

  self.SaveDialog1.FileName:=pathctrl.scenarioName+'\*.jpy1';

  SectionLabel.hint:=loader.comment;


  loader.Free();
 end;
 
end;

procedure TJPYTmpForm.resetBitBtnClick(Sender: TObject);
begin
 if(loadTemplate(nowFileName))then
 begin
  self.saveFileName:='';
  setPageCount(0);
 end;
end;

procedure TJPYTmpForm.FormCreate(Sender: TObject);
begin
 director:=TEDDirector.create;
 changeList:=TObjectList.Create;
 self.saveFileName:='.jpy1';
 DirListFrame1.NotifyNameEvent:=loadImage;
 Image1.Canvas.Brush.Color:=0;
 Image1.Canvas.FillRect(Image1.ClientRect);
 setPageCount(0);
end;

procedure TJPYTmpForm.loadImage(fileName:string);
var
 bitmap:Tbitmap;
begin
 bitmap:=TBitmap.Create();
 director.basedir:=fileName;
 director.loadImage(fileName,Bitmap);

 if(ExtractFileExt(fileName)='.jpy1')then
  DirListFrame1.cellHint:=director.getIniComment(fileName)
 else
  DirListFrame1.cellHint:='';

 bitmap.Transparent:=false;
 Image1.Canvas.StretchDraw(Image1.ClientRect,bitmap);
 bitmap.Free();
end;

procedure TJPYTmpForm.setPageCount(count:Integer);

begin
 if(changeList.Count<1)then
 begin
  pageCount:=0;
  prevBitBtn.Enabled:=false;
  nextBitBtn.Enabled:=false;
  entryBitBtn.Enabled:=false;
  FileNameEdit.Visible:=false;
  pageCountLabel.Caption:='page 0/'+IntToStr(changeList.Count);
  exit;
 end;

 entryBitBtn.Enabled:=true;
 if(count<1)then
 begin
  FileNameEdit.Visible:=false;
  pageCount:=0;
  prevBitBtn.Enabled:=false;
  nextBitBtn.Enabled:=true;
  SectionLabel.Caption:=SectionLabel.Hint;
  pageCountLabel.Caption:='page 0/'+IntToStr(changeList.Count);
  fileNameEdit.Text:=saveFileName;
  exit;
 end
 else
 begin
  FileNameEdit.Visible:=true;
  if(count>=changeList.Count)then
  begin
   pageCount:=changeList.Count;
   prevBitBtn.Enabled:=true;
   nextBitBtn.Enabled:=false;
  end
  else
  begin
   pageCount:=count;
   prevBitBtn.Enabled:=true;
   nextBitBtn.Enabled:=true;
  end;

  pageCountLabel.Caption:='page '+IntToStr(pageCount)+'/'+IntToStr(changeList.Count);
  fileNameEdit.Text:=TBlankInf(changeList[pageCount-1]).fileName;
  SectionLabel.Caption:=TBlankInf(changeList[pageCount-1]).sectionName;

 end;

end;

procedure TJPYTmpForm.FormDestroy(Sender: TObject);
begin
 self.DirListFrame1.eventlock:=true;
 changeList.Free();
 self.DirListFrame1.NotifyNameEvent:=nil;
 director.Free();
 //pathctrl.Free();
end;

procedure TJPYTmpForm.entryBitBtnClick(Sender: TObject);
begin
 fileNameEdit.Text:=DirListFrame1.getFileName();
 if(pageCount>0)then
 begin
  TBlankInf(changeList[pageCount-1]).fileName:=fileNameEdit.Text;
  TBlankInf(changeList[pageCount-1]).dirType:=DirListFrame1.getDirtype();
 end
 else saveFileName:=fileNameEdit.Text;
end;

procedure TJPYTmpForm.nextBitBtnClick(Sender: TObject);
begin
 setPageCount(pageCount+1);
end;

procedure TJPYTmpForm.prevBitBtnClick(Sender: TObject);
begin
 setPageCount(pageCount-1);
end;

procedure TJPYTmpForm.saveBitBtnClick(Sender: TObject);
var
 writer:TBlankWriter;
 fileName:string;
begin

 if(self.SaveDialog1.Execute)then
 begin
  fileName:=self.SaveDialog1.FileName;
  if(ExtractFileExt(fileName)='')then
   ChangeFileExt(fileName,'.jpy1');

   writer:=TBlankWriter.Create();
   try
    writer.InFileName:=nowFileName;
    writer.outFileName:=fileName;
    writer.dirDepth:=self.checkDepth(extractFileDir(fileName));
    writer.saveBlank(changeList);
   finally
    writer.Free();
   end;

 end;

end;

procedure TJPYTmpForm.FileNameEditChange(Sender: TObject);
begin
 if(self.pageCount=0)then
  saveFileName:=fileNameEdit.Text;
end;

(*
function TJPYTmpForm.countSeparator(fileName:string):Integer;
var
 i:Integer;
 w:widestring;
begin
 result:=0;
 w:=widestring(fileName);
 for i:=1 to Length(w)-1 do
 begin
  if(w[i]='\')then inc(result);
 end;

end;
*)

function TJPYTmpForm.checkDepth(const fileDir:string):Integer;
var
 driveName,s,s1:string;
begin
 result:=0;
 s:=filedir;
 driveName:=ExtractFileDrive(fileDir);


 while(not AnsiSameText(s,pathctrl.scenarioName))do
 begin
  if(AnsiSameText(s,driveName+'\'))then
  begin
   result:=0;
   break;
  end
  else
  begin
   inc(result);
   s:=ExtractFileDir(s);
   if(s=s1)then break;
  end;
 end;

end;

procedure TJPYTmpForm.openWizard(fileName:string);
begin

 if(loadTemplate(fileName))then
 begin
  nowFileName:=fileName;
  setPageCount(0);
  Caption:='JPYTmpForm - '+self.nowFileName;
  self.SaveDialog1.FileName:=pathctrl.scenarioName+'\*.jpy1';
  ShowModal();
 end;

end;

procedure TJPYTmpForm.BitBtn3Click(Sender: TObject);
begin
 ModalResult := mrCancel;
end;

procedure TJPYTmpForm.BitBtn1Click(Sender: TObject);
var
 s:string;
begin
 s:=pathctrl.pathName+'\'+DirListFrame1.getFileName();
 ShellExecute(self.Handle,'open',
 PChar(s),'','',SW_SHOWNORMAL);
end;

procedure TJPYTmpForm.sourceBitBtnClick(Sender: TObject);
begin
 ShellExecute(self.Handle,'open',
 PChar(self.loadFileName),'','',SW_SHOWNORMAL);
end;

procedure TJPYTmpForm.extBBClick(Sender: TObject);
begin
 DirListFrame1.exeExtAllowedDlg();
end;

end.
