// http://akelpad.sourceforge.net/en/plugins.php#Scripts
// Version: 1.1
// Author: Shengalts Aleksander aka Instructor
//
// Description(1033): Insert contents of a file.
// Description(2052): 插入文件的内容。

//Options
var pFilter="文本文件 (*.txt)\x00*.txt\x00所有文件 (*.*)\x00*.*\x00\x00";
var nFilterIndex=2;

//变量
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var pInitialFile=AkelPad.GetEditFile(0);
var pFile;
var pText;

if (hWndEdit)
{
  if (pFile=FileDialog(true, hMainWnd, pInitialFile, pFilter, nFilterIndex))
  {
    if (pText=AkelPad.ReadFile(pFile))
    {
      AkelPad.ReplaceSel(pText, -2);
    }
  }
}


//函数
function FileDialog(bOpenTrueSaveFalse, hWnd, pInitialFile, pFilter, nFilterIndex)
{
  var nFlags=0x880804; //OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_EXPLORER|OFN_ENABLESIZING
  var pDefaultExt="txt";
  var lpStructure;
  var lpFilterBuffer;
  var lpFileBuffer;
  var lpExtBuffer;
  var oSys;
  var pResultFile="";
  var nCallResult;

  if (lpFilterBuffer=AkelPad.MemAlloc(256 * _TSIZE))
  {
    AkelPad.MemCopy(lpFilterBuffer, pFilter.substr(0, 255), _TSTR);

    if (lpFileBuffer=AkelPad.MemAlloc(256 * _TSIZE))
    {
      AkelPad.MemCopy(lpFileBuffer, pInitialFile.substr(0, 255), _TSTR);

      if (lpExtBuffer=AkelPad.MemAlloc(256 * _TSIZE))
      {
        AkelPad.MemCopy(lpExtBuffer, pDefaultExt.substr(0, 255), _TSTR);

        if (lpStructure=AkelPad.MemAlloc(_X64?136:76 /*sizeof(OPENFILENAME)*/))
        {
          //填充结构
          AkelPad.MemCopy(_PtrAdd(lpStructure, 0) /*offsetof(OPENFILENAME, lStructSize)*/, _X64?136:76, 3 /*DT_DWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?8:4) /*offsetof(OPENFILENAME, hwndOwner)*/, hWnd, 2 /*DT_QWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?24:12) /*offsetof(OPENFILENAME, lpstrFilter)*/, lpFilterBuffer, 2 /*DT_QWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?44:24) /*offsetof(OPENFILENAME, nFilterIndex)*/, nFilterIndex, 3 /*DT_DWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?48:28) /*offsetof(OPENFILENAME, lpstrFile)*/, lpFileBuffer, 2 /*DT_QWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?56:32) /*offsetof(OPENFILENAME, nMaxFile)*/, 256, 3 /*DT_DWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?96:52) /*offsetof(OPENFILENAME, Flags)*/, nFlags, 3 /*DT_DWORD*/);
          AkelPad.MemCopy(_PtrAdd(lpStructure, _X64?104:60) /*offsetof(OPENFILENAME, lpstrDefExt)*/, lpExtBuffer, 2 /*DT_QWORD*/);

          if (oSys=AkelPad.SystemFunction())
          {
            //Call dialog
            if (bOpenTrueSaveFalse == true)
              nCallResult=oSys.Call("comdlg32::GetOpenFileName" + _TCHAR, lpStructure);
            else
              nCallResult=oSys.Call("comdlg32::GetSaveFileName" + _TCHAR, lpStructure);

            //Result file
            if (nCallResult) pResultFile=AkelPad.MemRead(lpFileBuffer, _TSTR);
          }
          AkelPad.MemFree(lpStructure);
        }
        AkelPad.MemFree(lpExtBuffer);
      }
      AkelPad.MemFree(lpFileBuffer);
    }
    AkelPad.MemFree(lpFilterBuffer);
  }
  return pResultFile;
}
