unit SDIMAIN;

interface

uses Windows, Classes, Graphics, Forms, Controls, Menus, SySutils,cwcells,
  StdCtrls, ComCtrls, ShellCtrls, Dialogs, ImgList, StdActns,JPYInterfaced,
  Buttons, ExtCtrls,shellAPI,ShlObj, initFrames, ToolWin, ExtDlgs, ActnList,
 ViewForms,convImages,FileCtrl,iniFiles,TextImages,messages,partsForms,JPYTemplateForms;
                                                         //          ,jpxForms
type
  TJPYMaker = class(TForm)
    OpenDialog: TOpenDialog;
    SaveDialog: TSaveDialog;
    ToolBar1: TToolBar;
    openTB: TToolButton;
    saveTB: TToolButton;
    split1: TToolButton;
    cutTB: TToolButton;
    copyTB: TToolButton;
    pasteTB: TToolButton;
    StatusBar: TStatusBar;
    ImageList1: TImageList;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    FileNewItem: TMenuItem;
    FileOpenItem: TMenuItem;
    FileSaveItem: TMenuItem;
    FileSaveAsItem: TMenuItem;
    N1: TMenuItem;
    FileExitItem: TMenuItem;
    Edit1: TMenuItem;
    CutItem: TMenuItem;
    CopyItem: TMenuItem;
    PasteItem: TMenuItem;
    Help1: TMenuItem;
    HelpAboutItem: TMenuItem;
    Show1: TMenuItem;
    split2: TToolButton;
    ImageTB: TToolButton;
    I1: TMenuItem;
    contentTB: TToolButton;
    N2: TMenuItem;
    N3: TMenuItem;
    ActionList1: TActionList;
    FileOpen1: TAction;
    FileSave1: TAction;
    FileSaveAs1: TAction;
    FileExit1: TAction;
    EditCut1: TAction;
    normalCopy: TAction;
    HelpAbout1: TAction;
    partsPaste: TAction;
    EditShow: TAction;
    ImageCopy: TAction;
    ContentCopy: TAction;
    showParts: TAction;
    showView: TAction;
    viewTB: TToolButton;
    ViewCopy: TAction;
    N4: TMenuItem;
    ViewCopy1: TMenuItem;
    Panel1: TPanel;
    tmpOpenItem: TMenuItem;
    partsTB: TToolButton;
    partsCopy: TAction;
    optionItem: TMenuItem;
    frontN: TMenuItem;
    N5: TMenuItem;
    copyModeN1: TMenuItem;
    copyModeN2: TMenuItem;
    copyModeN3: TMenuItem;
    copyModeN4: TMenuItem;
    partsCopy1: TMenuItem;
    showViewTB: TToolButton;
    showPartsTB: TToolButton;
    procedure FileOpen1Execute(Sender: TObject);
    procedure FileSave1Execute(Sender: TObject);
    procedure FileExit1Execute(Sender: TObject);
    procedure HelpAbout1Execute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure copyTBClick(Sender: TObject);
    procedure FileSaveFirstExecute(Sender: TObject);
    procedure EditShowExecute(Sender: TObject);
    procedure PasteItemExecute(Sender: TObject);
    procedure ImageCopyExecute(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ContentCopyExecute(Sender: TObject);
    procedure viewTBClick(Sender: TObject);
    procedure tmpOpenItemClick(Sender: TObject);
    procedure partsCopyExecute(Sender: TObject);
    procedure frontNClick(Sender: TObject);
    procedure copyModeNClick(Sender: TObject);
  private
    { Private 宣言 }
    nomalCopyFormat:TCWFormat;
    dealer:IClipBrdDealer;
    director:TEDDirector;
    procedure clearCopyCheckN(n:TMenuItem);
    procedure setActiveClipDealer(sender:TObject);
    procedure WMDROPFILES(var msg:TWMDROPFILES);message WM_DROPFILES;
    procedure saveJPY(fileName:string);
    procedure loadJPY(FileName:string;check:boolean);
    procedure allEnabled(flag:boolean);
    procedure changeFile(fileName:string;flag:boolean);
    function checkDepth(const fileDir:string):Integer;
    procedure openSummary(fileName:string);
  public
    { Public 宣言 }
    pathctrl:TPathCtrl;
 //   jpxForm:TJPXForm;
    ViewForm:TViewForm;
    jpyForm:TJPYTmpForm;
    partsForm:TPartsForm;
    baseBitmap:TBitmap;
    procedure reset();

  end;

var
  JPYMaker: TJPYMaker;




implementation

uses about,SummaryLoaders;

{$R *.dfm}

procedure TJPYMaker.reset();
begin
 ViewForm.reset();
 partsForm.reset();
 baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
end;

(*ファイルを選択*)
procedure TJPYMaker.FileOpen1Execute(Sender: TObject);
var
 fileName,dirName,oldName:string;
begin
 oldName:=OpenDialog.FileName;
// OpenDialog.Filter:='シナリオファイル(Summary.wsm)|Summary.wsm|設定テンプレートファイル（*.tpy)|*.tpy';
 OpenDialog.Filter:='シナリオファイル(Summary.wsm)|Summary.wsm';
 dirName:=ExtractFileDir(OpenDialog.FileName);

 if(dirName<>'')then OpenDialog.FileName:=dirName+'\Summary.wsm'
 else OpenDialog.FileName:='Summary.wsm';

 if(OpenDialog.Execute)then
 begin
  FileName:=ansiLowerCase(OpenDialog.FileName);
  if(extractFileName(FileName)='summary.wsm')then
  begin
   openSummary(fileName);
   allEnabled(true);
   Caption:='JPYMaker - "'+ExtractFileDir(FileName)+'"';
  end;
 end;

 OpenDialog.FileName:=oldName;
 SaveDialog.FileName:=oldName;

end;

(*設定ファイルに保存*)
procedure TJPYMaker.saveJPY(fileName:string);
var
 init:TIniFile;
 secList,srcList:TStringList;
 inf:TImageInfo;
 i,v:Integer;
begin
  init:=TIniFile.Create(fileName);
  secList:=TStringList.Create();


 try

  init.ReadSections(secList);

  v:=secList.IndexOf('init');
  if(v>-1)then
   secList.Delete(v);

  inf:=ViewForm.getInitInf();
  director.basedir:=ExtractFileDir(fileName);
  inf.depth:=checkDepth(director.basedir);
  inc(inf.delCount);

  director.writeInitSection(TCustomIniFile(init),inf);
  inf.FreeEx();

  for i:=0 to ViewForm.PartsListBox.Items.Count-1 do
  begin
   director.writeSection
   (TCustomIniFile(init),ViewForm.getSectionInf(i));
  end;
  self.allEnabled(true);
  init.UpdateFile();
 finally
  init.Free();
  secList.Clear();
 end;

 (*余計なセクションを消去*)

 srcList:=TStringList.Create();
 try
  secList.Assign(ViewForm.PartsListBox.Items);
  secList.Insert(0,'init');//initセクションの追加
  srcList.LoadFromFile(fileName);
  director.replaceIni(srcList,secList);
  srcList.SaveToFile(fileName);
  Caption:='JPYMaker - "'+FileName+'"';  
 finally
  srcList.Free();
  secList.Free();
 end;
    
end;


(*設定ファイルを保存*)
procedure TJPYMaker.FileSaveFirstExecute(Sender: TObject);
begin
 if(SaveDialog.Execute)then
 begin
  case saveDialog.FilterIndex of
   1:
   begin
    SaveDialog.FileName:=ChangeFileExt(SaveDialog.FileName,'.jpy1');
    saveJPY(SaveDialog.FileName);
    pathctrl.nowFileName:=SaveDialog.FileName;
   end;
   2:
   begin
    SaveDialog.FileName:=ChangeFileExt(SaveDialog.FileName,'.bmp');
    ViewForm.saveImage(SaveDialog.FileName);
   end;
  end;

 end;
end;

procedure TJPYMaker.FileSave1Execute(Sender: TObject);
begin
 if(AnsiLowerCase(ExtractFileExt(pathctrl.nowFileName))='.jpy1')
 and(FileExists(pathctrl.nowFileName))then
 begin
  try
   saveJPY(pathctrl.nowFileName);
  except
   windows.MessageBox(0,'',PCHar(pathctrl.nowFileName),MB_OK);
  end;
 end
 else
  FileSaveFirstExecute(Sender);
end;

procedure TJPYMaker.FileExit1Execute(Sender: TObject);
begin
  Close;
end;

procedure TJPYMaker.HelpAbout1Execute(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

procedure TJPYMaker.FormCreate(Sender: TObject);

begin
  baseBitmap:=TBitmap.Create();
  baseBitmap.PixelFormat:=pf32bit;
  DragAcceptFiles(self.Handle, True);
  nomalCopyFormat:=cfCell;

  ViewForm:=TViewForm.Create(self);

  director:=TEDDirector.create();

  partsForm:=TPartsForm.create(self);


  jpyForm:=TJPYTmpForm.create(self);

  ViewForm.NotifySeekEvent:=partsForm.receiveParts;

  PartsForm.DirListFrame1.IconImageList:=self.ImageList1;
  PartsForm.pathctrl:=self.pathctrl;
  jpyForm.DirListFrame1.IconImageList:=self.ImageList1;
  jpyForm.pathctrl:=self.pathctrl;


  pathctrl:=TPathCtrl.Create();
  pathctrl.baseName:=ExtractFileDir(Application.ExeName);

  if not(DirectoryExists(pathctrl.baseName+'\Scenario'))then
   CreateDir(pathctrl.baseName+'\Scenario');
  if not(DirectoryExists(pathctrl.baseName+'\Scheme'))then
   CreateDir(pathctrl.baseName+'\Scheme');

  pathctrl.hasSummary:=false;
  partsForm.pathctrl:=pathctrl;
  viewForm.pathctrl:=pathctrl;

  PartsForm.addingForm:=ViewForm;
  PartsForm.cellCreateEvent:=changeFile;

  ViewForm.OnActivate:=self.setActiveClipDealer;
  PartsForm.OnActivate:=self.setActiveClipDealer;
  partsForm.hasInfo:=False;

  if(ParamStr(1)='/mdi')then
  begin
   self.Hide;
   self.AutoSize:=false;
   self.Top:=50;
   self.Left:=50;
   self.Width:=900;
   self.Height:=700;
   jpyForm.Top:=20;
   jpyForm.Left:=20;
   partsForm.Top:=0;
   partsForm.Left:=0;
   ViewForm.Top:=20;
   ViewForm.Left:=20;
   windows.SetParent(partsForm.Handle,Panel1.Handle);
   windows.SetParent(viewForm.Handle,Panel1.Handle);
   windows.SetParent(jpyForm.Handle,Panel1.Handle);
   self.Show;
  end
  else
  begin
   jpyForm.Top:=200;
   jpyForm.Left:=200;
   partsForm.Top:=self.Top+50;
   partsForm.Left:=self.Left;
   ViewForm.Top:=self.Top+70;
   ViewForm.Left:=self.Left+20;
  end;
   // SHGetFileInfo(c,0,FileInfo,sizeof(FileInfo),
 //  SHGFI_ICON or SHGFI_SMALLICON or SHGFI_OPENICON);
end;


procedure TJPYMaker.FormDestroy(Sender: TObject);
begin
 windows.SetParent(partsForm.Handle,0);
 windows.SetParent(viewForm.Handle,0);
 windows.SetParent(jpyForm.Handle,0);

 director.Free();
 pathctrl.Free();
 baseBitmap.Free();
 partsForm.Release();
 ViewForm.Release();
 DragAcceptFiles(self.Handle, false);
end;



procedure TJPYMaker.loadJPY(FileName:string;check:boolean);
var
 openName,loadFileName:string;
 ini:TMemIniFile;
 sList:TStringList;
 dinf:TDirinf;
 inf:TImageInfo;
 i:Integer;
 oldcolor:TColor;
begin

  openName:=ExtractFileDir(FileName);

  pathctrl.middlePath:='';
  pathctrl.openFileName:=FileName;
  pathctrl.pathName:=pathctrl.scenarioName;
  partsForm.openFolder(pathctrl.scenarioName);

  partsForm.hasInfo:=false;
  director.basedir:=openName;

  ini:=TMemIniFile.Create(FileName);
  sList:=TStringList.Create();

  try
   ini.ReadSections(sList);
   inf:=director.readInitSection(TCustomIniFile(ini));

   dinf.fileName:=inf.fileName;
   dinf.mode:=inf.dirtype;
   dinf.currentFile:=openName;
   dinf.firstDir:=pathctrl.scenarioName;
   dinf.exeDir:=pathctrl.baseName;
   loadFileName:=TTagText.serachFileName(dinf);

   inc(inf.delCount);
   ViewForm.setInitInf(inf);

   ViewForm.lock:=true;
   director.basedir:=director.getBaseName(fileName);

   if(fileExists(loadfileName))then
   begin
    director.loadImage(loadFileName,baseBitmap,1);
    if(inf.posInf.w=-1)then inf.posInf.w:=baseBitmap.Width;
    if(inf.posInf.h=-1)then inf.posInf.w:=baseBitmap.Height;

    ViewForm.setInitImage(inf.dirtype,inf.fileName,basebitmap);
   end
   else
   begin
    if(inf.posInf.w=-1)then inf.posInf.w:=632;
    if(inf.posInf.y=-1)then inf.posInf.y:=420;
    ViewForm.setInitImage(inf.dirtype,inf.fileName,nil);
   end;

   if(sList.Count>0)then
    sList.Delete(sList.IndexOf('init'));

   for i:=0 to sList.Count-1 do
   begin
    inf:=director.readSection(TCustomIniFile(ini),sList.Strings[i]);
    dinf.fileName:=inf.fileName;
    dinf.mode:=inf.dirtype;
    dinf.currentFile:=pathctrl.scenarioName;
    dinf.firstDir:=pathctrl.scenarioName;
    dinf.exeDir:=pathctrl.baseName;
    loadFileName:=TTagText.serachFileName(dinf);

    if(fileExists(loadFileName))then
    begin
     director.loadImage(loadFileName,baseBitmap,1);
    end
    else if(inf.posInf.w>0)and(inf.posInf.h>0)then
    begin
     oldcolor:=baseBitmap.Canvas.Brush.Color;
     baseBitmap.Width:=inf.posInf.w;
     baseBitmap.Height:=inf.posInf.h;
     baseBitmap.Canvas.Brush.Color:=inf.color;
     baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
     baseBitmap.Canvas.Brush.Color:=oldcolor;
    end;

    if(inf.posInf.w=-1)then
     inf.posInf.w:=baseBitmap.Width;

    if(inf.posInf.h=-1)then
     inf.posInf.h:=baseBitmap.Height;

    inc(inf.delCount);
    customizeBitmap(inf,baseBitmap);
    inc(inf.delCount);
    ViewForm.setSectionInf(inf,baseBitmap);
    inf.FreeEx();
   end;

   ViewForm.lock:=false;
   ViewForm.viewTabControlChange(self);
   self.allEnabled(true);
   pathctrl.nowFileName:=OpenDialog.FileName;
   ViewForm.Show();
   Caption:='JPYMaker - "'+FileName+'"';
  finally
   sList.Free();
   ini.Free();
  end;

end;

function TJPYMaker.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 TJPYMaker.PasteItemExecute(Sender: TObject);
begin
 if(dealer<>nil)and(pasteTB.Enabled)then
  dealer.loadFromClipBoard();
end;

procedure TJPYMaker.setActiveClipDealer(sender:TObject);
var
 subDealer:IClipBrdDealer;
begin
 if(Supports(Sender,IClipBrdDealer,subDealer))then
  dealer:=subDealer;
end;


procedure TJPYMaker.ImageCopyExecute(Sender: TObject);
begin
 if(dealer<>nil)then
  dealer.saveToClipBoard(cfImage);
end;


procedure TJPYMaker.allEnabled(flag:boolean);
begin
 self.partsPaste.Enabled:=flag;
 self.FileSave1.Enabled:=flag;
 self.FileSaveAs1.Enabled:=flag;
 self.normalCopy.Enabled:=flag;
 self.ImageCopy.Enabled:=flag;
 self.ViewCopy.Enabled:=flag;
 self.ContentCopy.Enabled:=flag;
 self.showParts.Enabled:=flag;
 self.showView.Enabled:=flag;
 self.tmpOpenItem.Enabled:=flag;
 self.partsCopy.Enabled:=flag;

 if(flag)then
 begin
  ViewForm.Show();
  partsForm.Show();
 end
 else
 begin
  ViewForm.Hide();
  partsForm.Hide();
 end;
end;

procedure TJPYMaker.ContentCopyExecute(Sender: TObject);
begin
 if(dealer<>nil)then
  dealer.saveToClipBoard(cfContent);
end;

procedure TJPYMaker.viewTBClick(Sender: TObject);
begin
 if(dealer<>nil)then
  dealer.saveToClipBoard(cfView);
end;

procedure TJPYMaker.changeFile(fileName:string;flag:boolean);
begin
 pathctrl.middlePath:='';
 pathctrl.openFileName:=ExtractFileDir(fileName);
 self.SaveDialog.FileName:=fileName;

 if(flag)then
 begin
  partsForm.reset();
  ViewForm.reset();
 end;

 //pathctrl.nowFileName:='';
 self.loadJPY(fileName,false);
end;

procedure TJPYMaker.OpenSummary(fileName:string);
var
 reader:TSummaryReader;
 dirName:string;
 items:TStrings;
begin
  reader:=TSummaryReader.Create;
  dirName:=ExtractFileDir(fileName);
  pathctrl.scenarioName:=dirName;
  pathctrl.middlePath:='';
  pathctrl.openFileName:=dirName;
  partsForm.reset();
  ViewForm.reset();
  pathctrl.pathName:=pathctrl.scenarioName;
  pathctrl.hasSummary:=true;
  pathctrl.nowFileName:='';
  partsForm.hasInfo:=false;
  partsForm.openFolder(dirName);
  self.allEnabled(true);
  items:=reader.loadFlagLists(fileName);

  if(items<>nil)then
  begin
   self.partsForm.setFlagItems(items);
  end;

  items.Free();
  reader.Free();
end;

procedure TJPYMaker.copyTBClick(Sender: TObject);
begin
 if(dealer<>nil)then
  dealer.saveToClipBoard(nomalCopyFormat);
end;


procedure TJPYMaker.EditShowExecute(Sender: TObject);
begin
 ViewForm.Visible:=not ViewForm.Visible;
end;

procedure TJPYMaker.WMDROPFILES(var msg:TWMDROPFILES);
var
  FileName: array [0..255] of Char;
begin
  DragQueryFile(Msg.Drop, 0, FileName, SizeOf(FileName)-1);

  if(ExtractFileName(AnsiLowerCase(FileName))='summary.wsm')then
    self.openSummary(FileName);

  DragFinish(Msg.Drop);
end;

procedure TJPYMaker.N2Click(Sender: TObject);
begin
 partsForm.Show()
end;

procedure TJPYMaker.N3Click(Sender: TObject);
begin
 viewForm.Show();
end;

procedure TJPYMaker.tmpOpenItemClick(Sender: TObject);
var
 fileName,oldDir:string;
begin
 OpenDialog.Filter:='設定テンプレートファイル（*.tpy)|*.tpy';
 if not(AnsiSameText(ExtractFileExt(openDialog.FileName),'.tpy'))then
 begin
  oldDir:=OpenDialog.FileName;
  OpenDialog.FileName:=self.pathctrl.baseName+'\Scheme\*.tpy';
  //ChangeFileExt(openDialog.FileName,'.tpy');
  if(OpenDialog.Execute)then
  begin
   FileName:=ansiLowerCase(OpenDialog.FileName);
   jpyForm.pathctrl:=self.pathctrl;
   if(extractFileExt(FileName)='.tpy')then
    jpyForm.openWizard(FileName);
  end;
  OpenDialog.FileName:=oldDir;
 end;

end;

procedure TJPYMaker.partsCopyExecute(Sender: TObject);
begin
 if(dealer<>nil)then
  dealer.saveToClipBoard(cfCell);
end;

procedure TJPYMaker.frontNClick(Sender: TObject);
begin
 if(frontN.Checked)then
  self.FormStyle:=fsStayOnTop
 else
  self.FormStyle:=fsNormal;
 // SetWindowPos(self.Handle,HWND_TOPMOST,0,0,0,0,or SWP_NOSIZE or SWP_NOMOVE)

end;

procedure TJPYMaker.copyModeNClick(Sender: TObject);
begin

 if(Sender=copyModeN1)then
  nomalCopyFormat:=cfCell
 else if(Sender=copyModeN2)then
  nomalCopyFormat:=cfContent
 else if(Sender=copyModeN3)then
  nomalCopyFormat:=cfView
 else if(Sender=copyModeN4)then
  nomalCopyFormat:=cfImage;

 if(sender is TMenuItem)then
  self.clearCopyCheckN(TMenuItem(sender));

end;

procedure TJPYMaker.clearCopyCheckN(n:TMenuItem);
begin
 copyModeN1.Checked:=false;
 copyModeN2.Checked:=false;
 copyModeN3.Checked:=false;
 copyModeN4.Checked:=false;
 if(n<>nil)then n.Checked:=true;
end;

end.
