AkelPad Forum Index AkelPad
Support forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Scripts collection
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 17, 18, 19  Next
 
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Instructor
Site Admin


Joined: 06 Jul 2006
Posts: 5373

PostPosted: Wed Sep 15, 2010 5:52 pm    Post subject: Reply with quote

Shift tabulation size at specified offset.
ShiftTabSize.js

Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9209#9209
// Version v1.7
//
//
//// Shift tabulation size at specified offset.
//
// How to use:
//
// -"Increase tab" Call("Scripts::Main", 1, "ShiftTabSize.js", `+2`)
// -"Decrease tab" Call("Scripts::Main", 1, "ShiftTabSize.js", `-2`)
// -"Set tab to 8" Call("Scripts::Main", 1, "ShiftTabSize.js", `8`)
// -"Set tab to 8 locally" Call("Scripts::Main", 1, "ShiftTabSize.js", `8 -Local=true`)
//
//
//// Изменить размер табуляции на указанную величину.
//
// Как использовать:
//
// -"Увеличить таб" Call("Scripts::Main", 1, "ShiftTabSize.js", `+2`)
// -"Уменьшить таб" Call("Scripts::Main", 1, "ShiftTabSize.js", `-2`)
// -"Установить таб размером 8" Call("Scripts::Main", 1, "ShiftTabSize.js", `8`)
// -"Установить таб размером 8 локально" Call("Scripts::Main", 1, "ShiftTabSize.js", `8 -Local=true`)


//Arguments
var chSign="";
var nTabMove=0;
var bLocal=AkelPad.GetArgValue("Local", false);
if (WScript.Arguments.length >= 1)
{
  chSign=WScript.Arguments(0).substr(0, 1);
  nTabMove=parseInt(WScript.Arguments(0));
}
else WScript.Quit();

//Variables
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var nTabSize;

if (hMainWnd)
{
  if (bLocal)
  {
    if (chSign == '+' || chSign == '-')
      nTabSize=AkelPad.SendMessage(hWndEdit, 3239 /*AEM_GETTABSTOP*/, 0, 0) + nTabMove;
    else
      nTabSize=nTabMove;
    if (nTabSize > 0)
      AkelPad.SendMessage(hWndEdit, 3240 /*AEM_SETTABSTOP*/, nTabSize, 0);
  }
  else
  {
    if (chSign == '+' || chSign == '-')
      nTabSize=AkelPad.SendMessage(hMainWnd, 1223 /*AKD_GETFRAMEINFO*/, 52 /*FI_TABSTOPSIZE*/, 0) + nTabMove;
    else
      nTabSize=nTabMove;
    if (nTabSize > 0)
      AkelPad.SetFrameInfo(0, 1 /*FIS_TABSTOPSIZE*/, nTabSize);
  }
}



Shift marker position at specified offset.
ShiftMarker.js

Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9209#9209
// Version v1.6
//
//
//// Shift marker position at specified offset.
//
// How to use:
//
// -"Increase marker" Call("Scripts::Main", 1, "ShiftMarker.js", `+10`)
// -"Decrease marker" Call("Scripts::Main", 1, "ShiftMarker.js", `-10`)
// -"Set marker to 80" Call("Scripts::Main", 1, "ShiftMarker.js", `80`)
// -"Set marker to 80 locally" Call("Scripts::Main", 1, "ShiftMarker.js", `80 -Local=true`)
//
//
//// Сместить позицию маркера на указанную величину.
//
// Как использовать:
//
// -"Увеличить маркер" Call("Scripts::Main", 1, "ShiftMarker.js", `+10`)
// -"Уменьшить маркер" Call("Scripts::Main", 1, "ShiftMarker.js", `-10`)
// -"Установить маркер на позиции 80" Call("Scripts::Main", 1, "ShiftMarker.js", `80`)
// -"Установить маркер на позиции 80 локально" Call("Scripts::Main", 1, "ShiftMarker.js", `80 -Local=true`)


//Arguments
var chSign="";
var nMarkerMove=0;
var bLocal=AkelPad.GetArgValue("Local", false);
if (WScript.Arguments.length >= 1)
{
  chSign=WScript.Arguments(0).substr(0, 1);
  nMarkerMove=parseInt(WScript.Arguments(0));
}
else WScript.Quit();

//Variables
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var nMarkerPos;

if (hMainWnd)
{
  if (bLocal)
  {
    if (chSign == '+' || chSign == '-')
      nMarkerPos=AkelPad.SendMessage(hWndEdit, 3257 /*AEM_GETMARKER*/, 0, 0) + nMarkerMove;
    else
      nMarkerPos=nMarkerMove;
    AkelPad.SendMessage(hWndEdit, 3258 /*AEM_SETMARKER*/, 1 /*AEMT_SYMBOL*/, nMarkerPos);
  }
  else
  {
    if (chSign == '+' || chSign == '-')
      nMarkerPos=AkelPad.SendMessage(hMainWnd, 1223 /*AKD_GETFRAMEINFO*/, 64 /*FI_MARKER*/, 0) + nMarkerMove;
    else
      nMarkerPos=nMarkerMove;
    AkelPad.SetFrameInfo(0, 12 /*FIS_MARKER*/, nMarkerPos);
  }
}



Change alternating lines size.
ShiftAltLines.js

Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9209#9209
// Version v1.0
//
//
//// Change alternating lines size.
//
// How to use:
//
// -"Increase alt line" Call("Scripts::Main", 1, "ShiftAltLines.js", `+1`)
// -"Decrease alt line" Call("Scripts::Main", 1, "ShiftAltLines.js", `-1`)
// -"Set alt line to 3" Call("Scripts::Main", 1, "ShiftAltLines.js", `3`)
// -"Set alt line to 3 locally" Call("Scripts::Main", 1, "ShiftAltLines.js", `3 -Local=true`)
//
//
//// Изменить размер зебры на указанную величину.
//
// Как использовать:
//
// -"Увеличить зебру" Call("Scripts::Main", 1, "ShiftAltLines.js", `+1`)
// -"Уменьшить зебру" Call("Scripts::Main", 1, "ShiftAltLines.js", `-1`)
// -"Установить 3 строки в зебре" Call("Scripts::Main", 1, "ShiftAltLines.js", `3`)
// -"Установить 3 строки в зебре локально" Call("Scripts::Main", 1, "ShiftAltLines.js", `3 -Local=true`)


//Arguments
var chSign="";
var nMove=0;
var bLocal=AkelPad.GetArgValue("Local", false);
if (WScript.Arguments.length >= 1)
{
  chSign=WScript.Arguments(0).substr(0, 1);
  nMove=parseInt(WScript.Arguments(0));
}
else WScript.Quit();

//Variables
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var dwAltLineSkip;

if (hMainWnd)
{
  if (bLocal)
  {
    if (chSign == '+' || chSign == '-')
      dwAltLineSkip=LOWORD(AkelPad.SendMessage(hWndEdit, 3264 /*AEM_GETALTLINE*/, 0, 0)) + nMove;
    else
      dwAltLineSkip=nMove;

    if (dwAltLineSkip >= 0)
      AkelPad.SendMessage(hWndEdit, 3265 /*AEM_SETALTLINE*/, MAKELONG(dwAltLineSkip, dwAltLineSkip), 0);
  }
  else
  {
    if (chSign == '+' || chSign == '-')
      dwAltLineSkip=AkelPad.SendMessage(hMainWnd, 1223 /*AKD_GETFRAMEINFO*/, 73 /*FI_ALTLINEFILL*/, 0) + nMove;
    else
      dwAltLineSkip=nMove;

    if (dwAltLineSkip >= 0)
      AkelPad.SetFrameInfo(0, 21 /*FIS_ALTLINES*/, MAKELONG(dwAltLineSkip, dwAltLineSkip));
  }
}

function LOWORD(dwNumber)
{
  return (dwNumber & 0xffff);
}

function HIWORD(dwNumber)
{
  return (dwNumber >> 16);
}

function MAKELONG(a, b)
{
  return (a & 0xffff) | ((b & 0xffff) << 16);
}



Last edited by Instructor on Sun Dec 04, 2011 4:29 pm; edited 13 times in total
Back to top
View user's profile Send private message Send e-mail
FeyFre



Joined: 07 Aug 2007
Posts: 2042
Location: Vinnitsa, Ukraine

PostPosted: Sun Sep 26, 2010 9:40 pm    Post subject: Reply with quote

autotag.js v1.4 AutoTag.js
Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9326#9326
// http://outstanding.hmarka.net/akelpad/scripts/autotag.js
//
// Version: 1.4 (2013.05.20) (by VladSh)
//    Исправлено неверное определение тэга при указании нескольких слов текста после открывающего тэга
// Version: 1.3 (2012.10.20) (by VladSh)
//   Исправлено:
//     - неправильное срабатывание после уже установленного закрывающего символа;
//     - раздвоение закрывающего символа в некоторых случаях
//   + Оптимизация кода
// Version: 1.2 (2011.06.24) (by Poma)
//     Now correctly closes tags with attributes
// Version: 1.1 (2010.09.27) (by VladSh)
//     Added handling BBCode-style tags
// Version: 1.0 (2010.09.26) (by FeyFre)
//     Initial release
//
// Examples:
//   Call("Scripts::Main", 1, "AutoTag.js", `"<" ">"`) or without arguments      ;for HTML   (Ctrl+.)
//   Call("Scripts::Main", 1, "AutoTag.js", `"[" "]"`)      ;for BBCode (Ctlr+])
// Usage:
//   For smooth integration into AkelPad you can assign hotkey for this Script, which equal keystroke correspondent closing tag style you using (i.e ] for BBCode and Shift+. for XML/HTML/SGML derived markups)

var hWndEdit = AkelPad.GetEditWnd();
if (hWndEdit)
{
   var qStart = "<";
   var qEnd = ">";
   if (WScript.Arguments.length >= 1) {
      qStart = WScript.Arguments(0) || qStart;
      if (WScript.Arguments.length >= 2) {
         qEnd = WScript.Arguments(1) || qEnd;
      }
   }

   var lEnd = qEnd.length;
   var nCaret = AkelPad.GetSelStart();
   var worker = nCaret - lEnd;
   var text = "";
   var tag = new Array();
   var ntag;
   while (worker >= 0)      // берём по одному символу от каретки до открывающего символа
   {
      ntag = worker + lEnd;   // текущая позиция
      text = AkelPad.GetTextRange(worker, ntag);
      if (text.match(/[^<(){}\[\]\\\/]/i)) {      // собираем тэг
         if (text != " " && text != ">")
            tag.push(text);
         else
            tag = new Array();
      }
      else if (text == qStart) {      // получаем тэг и добавляем
         tag = tag.reverse().join("");
         text = qStart + "/" + tag;

         ntag += tag.length;   // текущая позиция + длина тэга
         if (AkelPad.GetTextRange(ntag, ntag + lEnd) != qEnd &&      // закрывающий символ тэга перед значением
            AkelPad.GetTextRange(nCaret - lEnd, nCaret) != qEnd) {   // закрывающий символ тэга, когда нет текста
            text = qEnd + text;
            nCaret += lEnd;
         }
         if (tag.substr(tag.length - lEnd) != qEnd)
            text += qEnd;      // закрывающий символ закрывающего тэга

         AkelPad.ReplaceSel(text);
         AkelPad.SetSel(nCaret, nCaret);

         WScript.Quit();
      }
      else break;
      worker --;
   }

   AkelPad.ReplaceSel(qEnd);
}


Last edited by FeyFre on Mon May 20, 2013 1:27 pm; edited 3 times in total
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Sat Oct 02, 2010 9:28 pm    Post subject: Reply with quote

Changing selection mode (column on/off) - simple version.

SelectionSwitch.js

Code:
// Changing selection mode (column on/off) - simple version - 2010-10-02
//
// Call("Scripts::Main", 1, "SelectionSwitch.js")


var hEditWnd = AkelPad.GetEditWnd();
if (! hEditWnd)
  WScript.Quit();


var bColSel = AkelPad.SendMessage(hEditWnd, 3127 /*AEM_GETCOLUMNSEL*/, 0, 0);

if (bColSel)
  AkelPad.SendMessage(hEditWnd, 3128 /*AEM_UPDATESEL*/, 0x0 /*column off*/, 0);
else
  AkelPad.SendMessage(hEditWnd, 3128 /*AEM_UPDATESEL*/, 0x1 /*AESELT_COLUMNON*/, 0);



Last edited by KDJ on Sun Oct 10, 2010 12:14 pm; edited 2 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Thu Oct 07, 2010 5:34 pm    Post subject: Reply with quote

Changing selection mode (column on/off) - extended version.
Move caret in the selection.

SelectionSwitchCaretMove.js

Code:
// SelectionSwitchCaretMove.js - ver. 2014-04-14 (x86/x64)
//
// Switch selection mode (column on/off) or move caret in the selection.
//
// Usage:
// Call("Scripts::Main", 1, "SelectionSwitchCaretMove.js", "0") - switch selection mode
// Call("Scripts::Main", 1, "SelectionSwitchCaretMove.js", "1") - move caret - begin/end of selection
// Call("Scripts::Main", 1, "SelectionSwitchCaretMove.js", "2") - in the columnar selection, sequentially, move caret to the four vertices of the rectangle

var hEditWnd = AkelPad.GetEditWnd();
var nAction;

if (! hEditWnd)
  WScript.Quit();
if (WScript.Arguments.length)
  nAction = WScript.Arguments(0);
if (! ((nAction == 0) || (nAction == 1) || (nAction == 2)))
  WScript.Quit();

var DT_QWORD          = 2;
var DT_DWORD          = 3;
var AEGI_FIRSTSELCHAR = 3;
var AEGI_LASTSELCHAR  = 4;
var AESELT_COLUMNOFF  = 0x0;
var AESELT_COLUMNON   = 0x1;
var AEM_GETSEL        = 3125;
var AEM_SETSEL        = 3126;
var AEM_UPDATESEL     = 3128;
var AEM_GETCOLUMNSEL  = 3127;
var AEM_GETINDEX      = 3130;
var AEM_INDEXUPDATE   = 3132;
var AEM_INDEXCOMPARE  = 3133;

var bColSel   = SendMessage(hEditWnd, AEM_GETCOLUMNSEL, 0, 0);
var lpFirstC  = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpLastC   = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpCaret   = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpSelect  = AkelPad.MemAlloc(_X64 ? 56 : 32 /*sizeof(AESELECTION)*/);
var lpBegSel  = lpSelect;
var lpEndSel  = lpSelect + (_X64 ? 24 : 12);
var lpSelFlag = lpSelect + (_X64 ? 48 : 24);

SendMessage(hEditWnd, AEM_GETINDEX, AEGI_FIRSTSELCHAR, lpFirstC);
SendMessage(hEditWnd, AEM_GETINDEX, AEGI_LASTSELCHAR,  lpLastC);
SendMessage(hEditWnd, AEM_GETSEL, lpCaret, lpSelect);

if (nAction == 0)
{
  if (bColSel)
    SendMessage(hEditWnd, AEM_UPDATESEL, AESELT_COLUMNOFF, 0);

  else
  {
    if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpEndSel) == 1)
    {
      CopyCharIndex(lpEndSel, lpCaret);
    }
    else
      if ((SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpBegSel) == 1) &&
          (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpEndSel) == -1))
      {
        CopyCharIndex(lpBegSel, lpEndSel);
        CopyCharIndex(lpEndSel, lpCaret);
      }

    AkelPad.MemCopy(lpSelFlag, AESELT_COLUMNON, DT_DWORD);
    SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);
  }
}

else if (nAction == 1)
{
  if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpEndSel) == -1)
    CopyCharIndex(lpCaret, lpEndSel);
  else
    CopyCharIndex(lpCaret, lpBegSel);

  SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);
}

else if (bColSel)
{
  if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpBegSel, lpFirstC) == 0)
  {
    if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpBegSel) == 0)
    {
      CopyCharIndexRowCol(lpBegSel, lpLastC,  lpFirstC);
      CopyCharIndexRowCol(lpEndSel, lpFirstC, lpLastC);
    }
    else
    {
      CopyCharIndexRowCol(lpBegSel, lpFirstC, lpLastC);
      CopyCharIndexRowCol(lpEndSel, lpLastC,  lpFirstC);
    }
    CopyCharIndex(lpCaret, lpEndSel);
  }

  else
  {
    if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpBegSel) == 0)
    {
      CopyCharIndex(lpBegSel, lpFirstC);
      CopyCharIndex(lpEndSel, lpLastC);
      CopyCharIndex(lpCaret,  lpEndSel);
    }
    else
    {
      CopyCharIndex(lpBegSel, lpLastC);
      CopyCharIndex(lpEndSel, lpFirstC);
      CopyCharIndex(lpCaret,  lpBegSel);
    }
  }
  SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);
}

AkelPad.MemFree(lpFirstC);
AkelPad.MemFree(lpLastC);
AkelPad.MemFree(lpCaret);
AkelPad.MemFree(lpSelect);

function CopyCharIndex(lpToIndex, lpFromIndex)
{
  AkelPad.MemCopy(lpToIndex, AkelPad.MemRead(lpFromIndex, DT_DWORD), DT_DWORD);
  AkelPad.MemCopy(lpToIndex + (_X64 ?  8 : 4), AkelPad.MemRead(lpFromIndex + (_X64 ?  8 : 4), DT_QWORD), DT_QWORD);
  AkelPad.MemCopy(lpToIndex + (_X64 ? 16 : 8), AkelPad.MemRead(lpFromIndex + (_X64 ? 16 : 8), DT_DWORD), DT_DWORD);
}

function CopyCharIndexRowCol(lpToIndex, lpFromIndexRow, lpFromIndexCol)
{
  var nRow = AkelPad.MemRead(lpFromIndexRow, DT_DWORD);
  var nCol = AkelPad.MemRead(lpFromIndexCol + (_X64 ? 16 : 8), DT_DWORD);

  AkelPad.MemCopy(lpToIndex, nRow, DT_DWORD);
  AkelPad.MemCopy(lpToIndex + (_X64 ? 16 : 8), nCol, DT_DWORD);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpToIndex);
}

function SendMessage(hWnd, uMsg, wParam, lParam)
{
  return AkelPad.SystemFunction().Call("User32::SendMessage" + _TCHAR, hWnd, uMsg, wParam, lParam);
}


Last edited by KDJ on Mon Apr 14, 2014 8:14 pm; edited 2 times in total
Back to top
View user's profile Send private message
Instructor
Site Admin


Joined: 06 Jul 2006
Posts: 5373

PostPosted: Thu Oct 07, 2010 11:01 pm    Post subject: Reply with quote

ChmKeyword.js

Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9418#9418
// Version v2.1
//
//
//// Show help for keyword.
//
// Arguments:
// -Name="help_name.chm"  -Use "help_name.chm" to search keyword (default is predefined CHM names).
// -Path="C:\MyPath"      -Path to CHM files (default is "[AkelPad]\AkelFiles\Help").
// -Maximize=false        -Don't maximize CHM help dialog (default is true).
// -CatchEsc=false        -Don't close CHM help dialog after escape key pressed (default is true).
//
// Usage #1:
// Use predefined CHM names that depends on current file extension (lpExt2Chm variable).
// Call("Scripts::Main", 1, "ChmKeyword.js")
//
// Usage #2:
// Use "help_name.chm" to search keyword.
// Call("Scripts::Main", 1, "ChmKeyword.js", `-Name="help_name.chm"`)

//Options
var lpExt2Chm={"js"   : "wsh.chm",
               "vbs"  : "wsh.chm",
               "c"    : "cpp.chm",
               "cpp"  : "cpp.chm",
               "h"    : "cpp.chm",
               "nsi"  : "nsis.chm",
               "css"  : "css.chm",
               "php"  : "php.chm",
               "htm"  : "html.chm",
               "html" : "html.chm",
               ""     : "html.chm"};

//Arguments
var pChmName=AkelPad.GetArgValue("Name", "");
var pChmPath=AkelPad.GetArgValue("Path", AkelPad.GetAkelDir() + "\\AkelFiles\\Help");
var bMaximize=AkelPad.GetArgValue("Maximize", true);
var bCatchEsc=AkelPad.GetArgValue("CatchEsc", true);

//Variables
var fso=new ActiveXObject("Scripting.FileSystemObject");
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var oSys=AkelPad.SystemFunction();
var hWndHelpDlg=0;
var pFile;
var pExt;
var pKeyword="";
var nSelStart;
var nSelEnd;
var nWordBegin;
var nWordEnd;
var nTimeOut;

if (hMainWnd)
{
  //File
  if (!pChmName)
  {
    pExt=fso.GetExtensionName(AkelPad.GetEditFile(0));
    if (!lpExt2Chm[pExt])
    {
      AkelPad.MessageBox(hMainWnd, GetLangString(0).replace(/%s/, pExt), WScript.ScriptName, 16 /*MB_ICONERROR*/);
      WScript.Quit();
    }
    pFile=pChmPath + "\\" + lpExt2Chm[pExt];
  }
  else pFile=pChmPath + "\\" + pChmName;

  if (!fso.FileExists(pFile))
  {
    AkelPad.MessageBox(hMainWnd, GetLangString(1).replace(/%s/, pFile), WScript.ScriptName, 16 /*MB_ICONERROR*/);
    WScript.Quit();
  }

  //Keyword
  nSelStart=AkelPad.GetSelStart();
  nSelEnd=AkelPad.GetSelEnd();

  if (nSelStart == nSelEnd)
  {
    //Select word under caret
    nWordBegin=AkelPad.SendMessage(hWndEdit, 1100 /*EM_FINDWORDBREAK*/, 0 /*WB_LEFT*/, nSelStart);
    nWordEnd=AkelPad.SendMessage(hWndEdit, 1100 /*EM_FINDWORDBREAK*/, 7 /*WB_RIGHTBREAK*/, nWordBegin);
    if (nWordEnd > nSelStart)
      pKeyword=AkelPad.GetTextRange(nWordBegin, nWordEnd);
  }
  else pKeyword=AkelPad.GetTextRange(nSelStart, nSelEnd);

  //Show help
  ChmKeyword(pFile, pKeyword);
}

//Functions
function ChmKeyword(pFile, pKeyword)
{
  var hModule;
  var lpKeywordBuffer;
  var lpStructure;
  var hWndHelpList=0;
  var hWndHelpChild;
  var hWndHelpFocus;
  var dwCurThreadID;
  var dwHelpThreadID;
  var hHook;
  var hMutex;

  //Load library manually to prevent hhctrl.ocx unload after HtmlHelp call
  if (!(hModule=oSys.Call("kernel32::GetModuleHandle" + _TCHAR, "hhctrl.ocx")))
    hModule=oSys.Call("kernel32::LoadLibrary" + _TCHAR, "hhctrl.ocx");

  if (hModule)
  {
    if (lpKeywordBuffer=AkelPad.MemAlloc(256 * _TSIZE))
    {
      AkelPad.MemCopy(lpKeywordBuffer, pKeyword.substr(0, 255), _TSTR);

      if (lpStructure=AkelPad.MemAlloc(_X64?56:32 /*sizeof(HH_AKLINK)*/))
      {
        //Fill structure
        AkelPad.MemCopy(lpStructure, _X64?56:32, 3 /*DT_DWORD*/);                    //HH_AKLINK.cbStruct
        AkelPad.MemCopy(lpStructure + (_X64?8:8), lpKeywordBuffer, 2 /*DT_QWORD*/);  //HH_AKLINK.pszKeywords
        AkelPad.MemCopy(lpStructure + (_X64?48:28), true, 3 /*DT_DWORD*/);           //HH_AKLINK.fIndexOnFail

        hWndHelpDlg=oSys.Call("hhctrl.ocx::HtmlHelp" + _TCHAR, oSys.Call("user32::GetDesktopWindow"), pFile, 0xD /*HH_KEYWORD_LOOKUP*/, lpStructure);

        //Send VK_RETURN
        if (hWndHelpDlg)
        {
          dwCurThreadID=oSys.Call("kernel32::GetCurrentThreadId");
          dwHelpThreadID=oSys.Call("user32::GetWindowThreadProcessId", hWndHelpDlg, 0);

          if (pKeyword)
          {
            if (oSys.Call("user32::AttachThreadInput", dwCurThreadID, dwHelpThreadID, true))
            {
              hWndHelpFocus=oSys.Call("user32::GetFocus");

              if (oSys.Call("user32::GetWindowTextLength" + _TCHAR, hWndHelpFocus))
              {
                AkelPad.SendMessage(hWndHelpFocus, 0x0100 /*WM_KEYDOWN*/, 0x0D /*VK_RETURN*/, 0x011C001);
                AkelPad.SendMessage(hWndHelpFocus, 0x0101 /*WM_KEYUP*/, 0x0D /*VK_RETURN*/, 0xC11C001);
              }
              else oSys.Call("user32::SetWindowText" + _TCHAR, hWndHelpFocus, lpKeywordBuffer);

              oSys.Call("user32::AttachThreadInput", dwCurThreadID, dwHelpThreadID, false);
            }
          }

          if (bMaximize)
          {
            //Maximize CHM window
            oSys.Call("user32::ShowWindow", hWndHelpDlg, 3 /*SW_MAXIMIZE*/);
          }
          if (bCatchEsc)
          {
            if (hMutex=oSys.Call("kernel32::CreateEvent" + _TCHAR, 0, 0, 0, WScript.ScriptName))
            {
              //Allow WindowSubClass and ThreadHook only for single script
              if (oSys.GetLastError() != 183 /*ERROR_ALREADY_EXISTS*/)
              {
                //Catch WM_CLOSE
                if (AkelPad.WindowSubClass(hWndHelpDlg, SubClassCallback, 2 /*WM_DESTROY*/))
                {
                  //Catch VK_ESCAPE
                  if (hHook=AkelPad.ThreadHook(3 /*WH_GETMESSAGE*/, HookCallback, dwHelpThreadID))
                  {
                    //Allow other scripts running
                    AkelPad.ScriptNoMutex();

                    //Message loop
                    AkelPad.WindowGetMessage();

                    //Wait while CHM window complitelly destroyed
                    for (nTimeOut=5000; nTimeOut > 0 && oSys.Call("user32::IsWindow", hWndHelpDlg); nTimeOut-=100)
                      WScript.Sleep(100);

                    AkelPad.ThreadUnhook(hHook);
                  }
                  AkelPad.WindowUnsubClass(hWndHelpDlg);
                }
              }
              oSys.Call("kernel32::CloseHandle", hMutex);
            }
          }
        }
        AkelPad.MemFree(lpStructure);
      }
      AkelPad.MemFree(lpKeywordBuffer);
    }
  }
}

function SubClassCallback(hWnd, uMsg, wParam, lParam)
{
  if (uMsg == 2 /*WM_DESTROY*/)
  {
    //Exit message loop
    oSys.Call("user32::PostQuitMessage", 0);
  }
}

function HookCallback(nCode, wParam, lParam)
{
  var uMsg=AkelPad.MemRead(lParam + (_X64?8:4) /*MSG.message*/, 3 /*DT_DWORD*/);
  var wParam;

  if (uMsg == 0x100 /*WM_KEYDOWN*/)
  {
    wParam=AkelPad.MemRead(lParam + (_X64?16:8) /*MSG.wParam*/, 2 /*DT_QWORD*/);

    if (wParam == 0x1B /*VK_ESCAPE*/)
    {
      if (oSys.Call("user32::GetForegroundWindow") == hWndHelpDlg)
        AkelPad.SendMessage(hWndHelpDlg, 16 /*WM_CLOSE*/, 0, 0);
    }
  }
}

function GetLangString(nStringID)
{
  var nLangID=AkelPad.GetLangId(1 /*LANGID_PRIMARY*/);

  if (nLangID == 0x19) //LANG_RUSSIAN
  {
    if (nStringID == 0)
      return "\u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043D\u0438\u0435 \"%s\" \u043D\u0435\u0020\u0430\u0441\u0441\u043E\u0446\u0438\u0438\u0440\u043E\u0432\u0430\u043D\u043E\u0020\u043D\u0438\u0020\u0441\u0020\u043E\u0434\u043D\u0438\u043C\u0020\u0043\u0048\u004D\u0020\u0444\u0430\u0439\u043B\u043E\u043C\u002E";
    if (nStringID == 1)
      return "\u041D\u0435\u0020\u043D\u0430\u0439\u0434\u0435\u043D\u043E\u003A \"%s\"";
  }
  else
  {
    if (nStringID == 0)
      return "Extension \"%s\" is not associated with any CHM file.";
    if (nStringID == 1)
      return "Missing: \"%s\"";
  }
  return "";
}



- Набор файлов помощи CHM для C/C++, CSS, HTML, VBS, JS, NSIS: скачать (14.5 Mb).
- Файл помощи для PHP.


Last edited by Instructor on Wed Jan 08, 2014 3:12 pm; edited 20 times in total
Back to top
View user's profile Send private message Send e-mail
FeyFre



Joined: 07 Aug 2007
Posts: 2042
Location: Vinnitsa, Ukraine

PostPosted: Thu Oct 07, 2010 11:13 pm    Post subject: Reply with quote

Win32 Constants Library
Set of constants usable during writing scripts using Win32 system calls.
Includes constants related to such Win32 system libraries
1. Kernel32.js: most often used non-GUI system calls declared in Kernel32.dll and partially in AdvApi32.dll: File/Handle operations, Memory management, Process/Thread management, Registry operations, Resources, Code-page, Language constants
2. User32.js: GUI-related Syscalls: Windows, Controls, Keyboard and Mouse codes(user32.dll)
3. Gdi32.js: Device-Context painting operations(gdi32.dll)
4. Shell32.js: a few constants to interact with Shell (Windows Explorer in most cases) (shell32.dll)
5. Comdlg32.js: constants usable during calling Common Dialog Syscalls (Open/Save file dialog, Color chooser, Print dialog etc) (Comdlg32.dll)
6. Comctl32.js: Common Controls related constants: messages, notification codes, windows class names etc(comctl32.dll)
7. Wsock32.js: a few winsock related constants (wsock32.dll)
Since I have not enough power to test it You always should doublecheck if that or this constant defined correctly. Especially if You know constant You use is result of a series of bitwise operations(and, or, xor, not). Any fixes are welcomed.


Last edited by FeyFre on Sun Jun 29, 2014 10:26 pm; edited 2 times in total
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Sun Oct 10, 2010 12:17 pm    Post subject: Reply with quote

Open selected text in new tab.

SelectionOpenInTab.js

Code:
// SelectionOpenInTab.js - ver. 2012-04-14
//
// Open selected text in new tab.
//
// Call("Scripts::Main", 1, "SelectionOpenInTab.js")
// Can assign shortcut key, eg: Shift+Alt+N

var pSelTxt;
if (AkelPad.GetEditWnd() && (AkelPad.IsMDI() > 0) && (pSelTxt = AkelPad.GetSelText()))
{
  AkelPad.SendMessage(AkelPad.GetMainWnd(), 273 /*WM_COMMAND*/, 4101 /*wParam=MAKEWPARAM(0,IDM_FILE_NEW)*/, 1 /*lParam=TRUE*/);
  AkelPad.ReplaceSel(pSelTxt);
}


Last edited by KDJ on Sat Apr 14, 2012 1:51 pm; edited 4 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Wed Oct 13, 2010 6:27 pm    Post subject: Reply with quote

Select entire columns or duplicate selected columns.

ColumnsDuplicateSelect.js

Code:
// ColumnsDuplicateSelect.js - var. 2014-04-14 (x86/x64)
//
// Select entire columns or duplicate selected columns.
//
// Usage:
// Call("Scripts::Main", 1, "ColumnsDuplicateSelect.js"[, "Action"])
// Action:
//   0 - select entire columns (default)
//   2 - duplicate columns
//
// Can assign shortcut keys, eg: Ctrl+Shift+C, Ctrl+Shift+D

var hEditWnd = AkelPad.GetEditWnd();
var nAction;

if (! hEditWnd)
  WScript.Quit();
if (WScript.Arguments.length)
  nAction = WScript.Arguments(0);
if (nAction != 2)
  nAction = 0;

var DT_QWORD             = 2;
var DT_DWORD             = 3;
var AEGI_FIRSTSELCHAR    = 3;
var AEGI_LASTSELCHAR     = 4;
var AEGL_LINEUNWRAPCOUNT = 11;
var AESELT_COLUMNON      = 0x1;
var AEM_GETSEL           = 3125;
var AEM_SETSEL           = 3126;
var AEM_GETLINENUMBER    = 3129;
var AEM_GETINDEX         = 3130;
var AEM_INDEXUPDATE      = 3132;
var AEM_GETWORDWRAP      = 3241;
var IDM_VIEW_WORDWRAP    = 4209;

var oSys       = AkelPad.SystemFunction();
var nWordWrap  = SendMessage(hEditWnd, AEM_GETWORDWRAP, 0, 0);
var lpFirstC   = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpLastC    = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpCaret    = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpSelect   = AkelPad.MemAlloc(_X64 ? 56 : 32 /*sizeof(AESELECTION)*/);
var lpSelect1  = AkelPad.MemAlloc(_X64 ? 56 : 32 /*sizeof(AESELECTION)*/);
var lpBegSel   = lpSelect;
var lpEndSel   = lpSelect + (_X64 ? 24 : 12);
var lpSelFlag  = lpSelect + (_X64 ? 48 : 24);
var lpBegSel1  = lpSelect1;
var lpEndSel1  = lpSelect1 + (_X64 ? 24 : 12);
var lpSelFlag1 = lpSelect1 + (_X64 ? 48 : 24);
var nLastLine  = SendMessage(hEditWnd, AEM_GETLINENUMBER, AEGL_LINEUNWRAPCOUNT, 0) - 1;
var sSelTxt;

if (nWordWrap)
  AkelPad.Command(IDM_VIEW_WORDWRAP);

SendMessage(hEditWnd, AEM_GETINDEX, AEGI_FIRSTSELCHAR, lpFirstC);
SendMessage(hEditWnd, AEM_GETINDEX, AEGI_LASTSELCHAR,  lpLastC);
SendMessage(hEditWnd, AEM_GETSEL, lpCaret, lpSelect);

ReplaceCharIndex(lpBegSel, nLastLine, AkelPad.MemRead(lpFirstC + (_X64 ? 16 : 8), DT_DWORD));
ReplaceCharIndex(lpEndSel, 0,         AkelPad.MemRead(lpLastC  + (_X64 ? 16 : 8), DT_DWORD));
CopyCharIndex(lpCaret, lpEndSel);

AkelPad.MemCopy(lpSelFlag, AESELT_COLUMNON, DT_DWORD);

SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);

if (nAction == 2)
{
  sSelTxt = AkelPad.GetSelText(1 /*\r*/);

  ReplaceCharIndex(lpBegSel1, 0, AkelPad.MemRead(lpLastC + (_X64 ? 16 : 8), DT_DWORD));
  ReplaceCharIndex(lpEndSel1, 0, AkelPad.MemRead(lpLastC + (_X64 ? 16 : 8), DT_DWORD));
  AkelPad.MemCopy(lpSelFlag1, AESELT_COLUMNON, DT_DWORD);

  SetRedraw(hEditWnd, false);
  SendMessage(hEditWnd, AEM_SETSEL, 0, lpSelect1);
  AkelPad.ReplaceSel(sSelTxt);

  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpBegSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpEndSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpCaret);
  SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);
  SetRedraw(hEditWnd, true);
}

AkelPad.MemFree(lpFirstC);
AkelPad.MemFree(lpLastC);
AkelPad.MemFree(lpCaret);
AkelPad.MemFree(lpSelect);
AkelPad.MemFree(lpSelect1);

if (nWordWrap)
  AkelPad.Command(IDM_VIEW_WORDWRAP);

function SetRedraw(hWnd, bRedraw)
{
  SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
  bRedraw && oSys.Call("User32::InvalidateRect", hWnd, 0, true);
}

function CopyCharIndex(lpToIndex, lpFromIndex)
{
  AkelPad.MemCopy(lpToIndex, AkelPad.MemRead(lpFromIndex, DT_DWORD), DT_DWORD);
  AkelPad.MemCopy(lpToIndex + (_X64 ?  8 : 4), AkelPad.MemRead(lpFromIndex + (_X64 ?  8 : 4), DT_QWORD), DT_QWORD);
  AkelPad.MemCopy(lpToIndex + (_X64 ? 16 : 8), AkelPad.MemRead(lpFromIndex + (_X64 ? 16 : 8), DT_DWORD), DT_DWORD);
}

function ReplaceCharIndex(lpIndex, nRow, nCol) /*if nRow or nCol == -1, no change*/
{
  if (nRow > -1)
    AkelPad.MemCopy(lpIndex, nRow, DT_DWORD);
  if (nCol > -1)
    AkelPad.MemCopy(lpIndex + (_X64 ? 16 : 8), nCol, DT_DWORD);

  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpIndex);
}

function SendMessage(hWnd, uMsg, wParam, lParam)
{
  return oSys.Call("User32::SendMessage" + _TCHAR, hWnd, uMsg, wParam, lParam);
}


Last edited by KDJ on Mon Apr 14, 2014 8:10 pm; edited 2 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Sun Oct 17, 2010 4:37 pm    Post subject: Reply with quote

Replace text in selected column.

ColumnsReplace.js

Code:
// ColumnsReplace.js - ver. 2014-07-19 (x86/x64)
//
// Replace text in selected column.
//
// Usage:
// Call("Scripts::Main", 1, "ColumnsReplace.js")
//
// Can assign shortcut key, eg: Shift+Alt+Insert

if (AkelPad.GetLangId(0 /*LANGID_FULL*/) == 1045 /*Polish*/)
{
  var sTxtCaption  = 'Zamiana tekstu w kolumnie';
  var sTxtReadOnly = 'Ustawiony jest tryb "Tylko do odczytu".';
  var sTxtNoColSel = 'Brak zaznaczenia kolumnowego.';
  var sTxtLabel    = 'Podaj tekst do zamiany:';
}
else
{
  var sTxtCaption  = 'Replace text in selected column';
  var sTxtReadOnly = '"Read only" mode is set.';
  var sTxtNoColSel = 'There is no columnar selection.';
  var sTxtLabel    = 'Input text to replace:';
}

var DT_DWORD          = 3;
var AEGI_FIRSTSELCHAR = 3;
var AEGI_LASTSELCHAR  = 4;
var AEM_GETSEL        = 3125;
var AEM_SETSEL        = 3126;
var AEM_GETCOLUMNSEL  = 3127;
var AEM_GETINDEX      = 3130;
var AEM_INDEXUPDATE   = 3132;
var AEM_INDEXCOMPARE  = 3133;

var hEditWnd = AkelPad.GetEditWnd();

if (! hEditWnd)
  WScript.Quit();

if (AkelPad.GetEditReadOnly(hEditWnd))
{
  AkelPad.MessageBox(hEditWnd, sTxtReadOnly, sTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
  WScript.Quit();
}

if (! SendMessage(hEditWnd, AEM_GETCOLUMNSEL, 0, 0))
{
  AkelPad.MessageBox(hEditWnd, sTxtNoColSel, sTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
  WScript.Quit();
}

var lpFirstC = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpCaret  = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpSelect = AkelPad.MemAlloc(_X64 ? 56 : 32 /*sizeof(AESELECTION)*/);
var lpBegSel = lpSelect;
var lpEndSel = lpSelect + (_X64 ? 24 : 12);
var nCarPos;
var aSelText;
var sText;
var nShiftCol;
var i;

SendMessage(hEditWnd, AEM_GETINDEX, AEGI_FIRSTSELCHAR, lpFirstC);
SendMessage(hEditWnd, AEM_GETSEL, lpCaret, lpSelect);

//nCarPos   0   1
//          3   2
if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpBegSel, lpFirstC) == 0)
{
  if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpBegSel) == 0)
    nCarPos = 0;
  else
    nCarPos = 2;
}
else
{
  if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpEndSel) == 0)
    nCarPos = 3;
  else
    nCarPos = 1;
}

aSelText = AkelPad.GetSelText(1 /*\r*/).split("\r");

if (nCarPos < 2)
  sText = aSelText[0];
else
  sText = aSelText[aSelText.length - 1];

sText = AkelPad.InputBox(hEditWnd, sTxtCaption, sTxtLabel, sText);

if (sText)
{
  nShiftCol = sText.length - aSelText[0].length;

  for (i = 0; i < aSelText.length; ++i)
    aSelText[i] = sText;

  sText = aSelText.join("\r");
  AkelPad.ReplaceSel(sText);

  if (nCarPos == 0)
    ShiftCharIndex(lpEndSel, nShiftCol);
  else if (nCarPos == 1)
  {
    ShiftCharIndex(lpBegSel, nShiftCol);
    ShiftCharIndex(lpCaret, nShiftCol);
  }
  else if (nCarPos == 2)
  {
    ShiftCharIndex(lpEndSel, nShiftCol);
    ShiftCharIndex(lpCaret, nShiftCol);
  }
  else
    ShiftCharIndex(lpBegSel, nShiftCol);

  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpBegSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpEndSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpCaret);

  SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);
}

AkelPad.MemFree(lpFirstC);
AkelPad.MemFree(lpCaret);
AkelPad.MemFree(lpSelect);

function ShiftCharIndex(lpIndex, nCol)
{
  AkelPad.MemCopy(lpIndex + (_X64 ? 16 : 8), AkelPad.MemRead(lpIndex + (_X64 ? 16 : 8), DT_DWORD) + nCol, DT_DWORD);
}

function SendMessage(hWnd, uMsg, wParam, lParam)
{
  return AkelPad.SystemFunction().Call("User32::SendMessage" + _TCHAR, hWnd, uMsg, wParam, lParam);
}



Last edited by KDJ on Sat Jul 19, 2014 4:54 pm; edited 5 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Sun Oct 17, 2010 8:12 pm    Post subject: Reply with quote

Sum numbers (expressions) in selected column.

ColumnsSum.js

Code:
// Sum numbers (expressions) in selected column - 2011-03-08
//
// Call("Scripts::Main", 1, "ColumnsSum.js")
// Can assign shortcut key, eg: Shift+Alt+S
// Dialog box can be moved using keys: Shift+Alt+C, Right, Left, Down, Up, End, Home, PgDown, PgUp.

var oSys = AkelPad.SystemFunction();

if (oSys.Call("kernel32::GetUserDefaultLangID") == 0x0415) //Polish
{
  var pTxtCaption   = "Sumowanie w kolumnie";
  var pTxtNoColSel  = "Brak zaznaczenia pionowego.";
  var pTxtBadSep    = "Separator nie moe być literą ani cyfrą.";
  var pTxtEqualSep  = "Separatory nie mogą być takie same.";
  var pTxtBadRound  = "Zaokrąglenie musi być liczbą z zakresu od -9 do 20.";
  var pTxtSum       = "Suma:";
  var pTxtMin       = "Minimum:";
  var pTxtMax       = "Maksimum:";
  var pTxtAve       = "Średnia:";
  var pTxtCountA    = "Licznik razem:";
  var pTxtCountN    = "- liczby:";
  var pTxtCountT    = "- tekst:";
  var pTxtCountE    = "- puste:";
  var pTxtOptions   = "Opcje";
  var pTxtDecSep    = "Separator dziesiętny:";
  var pTxtGroSep    = "Separator tysięcy:";
  var pTxtRound     = "Zaokrąglij do:";
  var pTxtTextAs0   = "Traktuj tekst jak liczbę 0";
  var pTxtEmptyAs0  = "Traktuj puste jak liczbę 0";
  var pTxtCalculate = "Sumuj";
  var pTxtClose     = "Zamknij";
}
else
{
  var pTxtCaption   = "Sum in the column";
  var pTxtNoColSel  = "There is no columnar selection.";
  var pTxtBadSep    = "Separator can not be letter or digit.";
  var pTxtEqualSep  = "Separators can not be the same.";
  var pTxtBadRound  = "Round must be a number in the range from -9 to 20.";
  var pTxtSum       = "Sum:";
  var pTxtMin       = "Minimum:";
  var pTxtMax       = "Maximum:";
  var pTxtAve       = "Average:";
  var pTxtCountA    = "Count all:";
  var pTxtCountN    = "- numbers:";
  var pTxtCountT    = "- text:";
  var pTxtCountE    = "- empty:";
  var pTxtOptions   = "Options";
  var pTxtDecSep    = "Decimal separator:";
  var pTxtGroSep    = "Group separator:";
  var pTxtRound     = "Round to:";
  var pTxtTextAs0   = "Treat text as a zero";
  var pTxtEmptyAs0  = "Treat empty as a zero";
  var pTxtCalculate = "Calculate";
  var pTxtClose     = "Close";
}

var DT_DWORD = 3;

var hMainWnd     = AkelPad.GetMainWnd();
var hEditWnd     = AkelPad.GetEditWnd();
var hGuiFont     = oSys.Call("gdi32::GetStockObject", 17 /*DEFAULT_GUI_FONT*/);
var pScriptName  = WScript.ScriptName;
var hInstanceDLL = AkelPad.GetInstanceDll();
var bColSel      = AkelPad.SendMessage(hEditWnd, 3127 /*AEM_GETCOLUMNSEL*/, 0, 0);

var pDecSep   = ".";
var pGroSep   = "";
var pRound    = "2";
var bTextAs0  = 0;
var bEmptyAs0 = 0;
var pSum;
var pMin;
var pMax;
var pAve;
var pCountA;
var pCountN;
var pCountE;
var pCountT;
var hWndDlg;
var lpBuffer;
var nWndPosX;
var nWndPosY;

ReadWriteIni(0);

var lpWnd       = [];
var IDRESULT    = 1000;
var IDTXTSUM    = 1001;
var IDTXTMIN    = 1002;
var IDTXTMAX    = 1003;
var IDTXTAVE    = 1004;
var IDTXTCOUNTA = 1005;
var IDTXTCOUNTN = 1006;
var IDTXTCOUNTT = 1007;
var IDTXTCOUNTE = 1008;
var IDSUM       = 1009;
var IDMIN       = 1010;
var IDMAX       = 1011;
var IDAVE       = 1012;
var IDCOUNTA    = 1013;
var IDCOUNTN    = 1014;
var IDCOUNTT    = 1015;
var IDCOUNTE    = 1016;
var IDOPTIONS   = 1017;
var IDTXTDECSEP = 1018;
var IDTXTGROSEP = 1019;
var IDTXTROUND  = 1020;
var IDDECSEP    = 1021;
var IDGROSEP    = 1022;
var IDROUND     = 1023;
var IDTEXTAS0   = 1024;
var IDEMPTYAS0  = 1025;
var IDCALCULATE = 1026;
var IDCLOSE     = 1027;

var WNDTYPE  = 0;
var WND      = 1;
var WNDEXSTY = 2;
var WNDSTY   = 3;
var WNDX     = 4;
var WNDY     = 5;
var WNDW     = 6;
var WNDH     = 7;
var WNDTXT   = 8;

//0x50000000 - WS_VISIBLE|WS_CHILD
//0x50000007 - WS_VISIBLE|WS_CHILD|BS_GROUPBOX
//0x50010000 - WS_VISIBLE|WS_CHILD|WS_TABSTOP
//0x50010001 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_DEFPUSHBUTTON
//0x50010003 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_AUTOCHECKBOX
//0x50010080 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|ES_AUTOHSCROLL
//0x50010880 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|ES_AUTOHSCROLL|ES_READONLY
//Windows              WNDTYPE, WND,WNDEXSTY,     WNDSTY,WNDX,WNDY,WNDW,WNDH, WNDTXT
lpWnd[IDRESULT   ] = ["BUTTON",   0,       0, 0x50000007,  10,   5, 200, 220, ""];
lpWnd[IDTXTSUM   ] = ["STATIC",   0,       0, 0x50000000,  20,  20, 100,  13, pTxtSum];
lpWnd[IDTXTMIN   ] = ["STATIC",   0,       0, 0x50000000,  20,  45, 100,  13, pTxtMin];
lpWnd[IDTXTMAX   ] = ["STATIC",   0,       0, 0x50000000,  20,  70, 100,  13, pTxtMax];
lpWnd[IDTXTAVE   ] = ["STATIC",   0,       0, 0x50000000,  20,  95, 100,  13, pTxtAve];
lpWnd[IDTXTCOUNTA] = ["STATIC",   0,       0, 0x50000000,  20, 120, 100,  13, pTxtCountA];
lpWnd[IDTXTCOUNTN] = ["STATIC",   0,       0, 0x50000000,  40, 145, 100,  13, pTxtCountN];
lpWnd[IDTXTCOUNTT] = ["STATIC",   0,       0, 0x50000000,  40, 170, 100,  13, pTxtCountT];
lpWnd[IDTXTCOUNTE] = ["STATIC",   0,       0, 0x50000000,  40, 195, 100,  13, pTxtCountE];
lpWnd[IDSUM      ] = ["EDIT",     0,   0x200, 0x50010880,  90,  20, 110,  20, ""];
lpWnd[IDMIN      ] = ["EDIT",     0,   0x200, 0x50010880,  90,  45, 110,  20, ""];
lpWnd[IDMAX      ] = ["EDIT",     0,   0x200, 0x50010880,  90,  70, 110,  20, ""];
lpWnd[IDAVE      ] = ["EDIT",     0,   0x200, 0x50010880,  90,  95, 110,  20, ""];
lpWnd[IDCOUNTA   ] = ["EDIT",     0,   0x200, 0x50010880,  90, 120,  80,  20, ""];
lpWnd[IDCOUNTN   ] = ["EDIT",     0,   0x200, 0x50010880, 100, 145,  80,  20, ""];
lpWnd[IDCOUNTT   ] = ["EDIT",     0,   0x200, 0x50010880, 100, 170,  80,  20, ""];
lpWnd[IDCOUNTE   ] = ["EDIT",     0,   0x200, 0x50010880, 100, 195,  80,  20, ""];
lpWnd[IDOPTIONS  ] = ["BUTTON",   0,       0, 0x50000007, 220,  30, 153, 165, pTxtOptions];
lpWnd[IDTXTDECSEP] = ["STATIC",   0,       0, 0x50000000, 230,  50, 100,  13, pTxtDecSep];
lpWnd[IDTXTGROSEP] = ["STATIC",   0,       0, 0x50000000, 230,  75, 100,  13, pTxtGroSep];
lpWnd[IDTXTROUND ] = ["STATIC",   0,       0, 0x50000000, 230, 100, 100,  13, pTxtRound];
lpWnd[IDDECSEP   ] = ["EDIT",     0,   0x200, 0x50010080, 342,  50,  20,  20, pDecSep];
lpWnd[IDGROSEP   ] = ["EDIT",     0,   0x200, 0x50010080, 342,  75,  20,  20, pGroSep];
lpWnd[IDROUND    ] = ["EDIT",     0,   0x200, 0x50010080, 342, 100,  20,  20, pRound];
lpWnd[IDTEXTAS0  ] = ["BUTTON",   0,       0, 0x50010003, 230, 145, 140,  16, pTxtTextAs0];
lpWnd[IDEMPTYAS0 ] = ["BUTTON",   0,       0, 0x50010003, 230, 170, 140,  16, pTxtEmptyAs0];
lpWnd[IDCALCULATE] = ["BUTTON",   0,       0, 0x50010001, 220, 205,  70,  23, pTxtCalculate];
lpWnd[IDCLOSE    ] = ["BUTTON",   0,       0, 0x50010000, 305, 205,  70,  23, pTxtClose];


if (hEditWnd)
{
  if (! bColSel)
  {
    AkelPad.MessageBox(hEditWnd, pTxtNoColSel, pTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
    WScript.Quit();
  }

  if (AkelPad.WindowRegisterClass(pScriptName))
  {
    if (lpBuffer = AkelPad.MemAlloc(256 * _TSIZE))
    {
      //Create dialog
      AkelPad.MemCopy(lpBuffer, pScriptName, _TSTR);
      hWndDlg = oSys.Call("user32::CreateWindowEx" + _TCHAR,
                          0,               //dwExStyle
                          lpBuffer,        //lpClassName
                          0,               //lpWindowName
                          0x90CA0000,      //WS_VISIBLE|WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
                          0,               //x
                          0,               //y
                          390,             //nWidth
                          268,             //nHeight
                          hMainWnd,        //hWndParent
                          0,               //ID
                          hInstanceDLL,    //hInstance
                          DialogCallback); //Script function callback. To use it class must be registered by WindowRegisterClass.
      if (hWndDlg)
      {
        //Disable main window, to make dialog modal
        oSys.Call("user32::EnableWindow", hMainWnd, false);

        //Message loop
        AkelPad.WindowGetMessage();
      }
      AkelPad.MemFree(lpBuffer);
    }
    AkelPad.WindowUnregisterClass(pScriptName);
  }
}


//////////////
function DialogCallback(hWnd, uMsg, wParam, lParam)
{
  var i;
  var nLowParam;

  if (uMsg == 1) //WM_CREATE
  {
    //Dialog caption
    AkelPad.MemCopy(lpBuffer, pTxtCaption, _TSTR);
    oSys.Call("user32::SetWindowText" + _TCHAR, hWnd, lpBuffer);

    //Create windows
    for (i = 1000; i < lpWnd.length; ++i)
    {
      AkelPad.MemCopy(lpBuffer, lpWnd[i][WNDTYPE], _TSTR);
      lpWnd[i][WND] = oSys.Call("user32::CreateWindowEx" + _TCHAR,
                                lpWnd[i][WNDEXSTY],//dwExStyle
                                lpBuffer,          //lpClassName
                                0,                 //lpWindowName
                                lpWnd[i][WNDSTY],  //dwStyle
                                lpWnd[i][WNDX],    //x
                                lpWnd[i][WNDY],    //y
                                lpWnd[i][WNDW],    //nWidth
                                lpWnd[i][WNDH],    //nHeight
                                hWnd,              //hWndParent
                                i,                 //ID
                                hInstanceDLL,      //hInstance
                                0);                //lpParam
      //Set font and text
      SetWindowFontAndText(lpWnd[i][WND], hGuiFont, lpWnd[i][WNDTXT]);
    }

    //Check
    if (bTextAs0)
      AkelPad.SendMessage(lpWnd[IDTEXTAS0][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
    if (bEmptyAs0)
      AkelPad.SendMessage(lpWnd[IDEMPTYAS0][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);

    //Set limit edit text
    AkelPad.SendMessage(lpWnd[IDDECSEP][WND], 197 /*EM_LIMITTEXT*/, 1, 0);
    AkelPad.SendMessage(lpWnd[IDGROSEP][WND], 197 /*EM_LIMITTEXT*/, 1, 0);
    AkelPad.SendMessage(lpWnd[IDROUND][WND],  197 /*EM_LIMITTEXT*/, 2, 0);

    Calculate();

    //Select text
    AkelPad.SendMessage(lpWnd[IDSUM][WND], 177 /*EM_SETSEL*/, 0, -1);

    //Set window position
    if ((nWndPosX == undefined) || (nWndPosY == undefined))
      MoveWindow(hMainWnd, hWnd, "RT");
    else
      MoveWindow(hMainWnd, hWnd, [nWndPosX, nWndPosY]);
  }

  else if (uMsg == 7) //WM_SETFOCUS
    oSys.Call("user32::SetFocus", lpWnd[IDSUM][WND]);

  else if (uMsg == 256) //WM_KEYDOWN
  {
    if (wParam == 13)  //VK_RETURN
      oSys.Call("user32::PostMessage" + _TCHAR, hWndDlg, 273 /*WM_COMMAND*/, IDCALCULATE, 0);
    else if (wParam == 27)  //VK_ESCAPE
      oSys.Call("user32::PostMessage" + _TCHAR, hWndDlg, 273 /*WM_COMMAND*/, IDCLOSE, 0);
  }

  else if ((uMsg == 260) /*WM_SYSKEYDOWN*/ &&
           (oSys.Call("user32::GetAsyncKeyState", 0xA0 /*VK_LSHIFT*/)))
  {
    if (wParam == 0x27) //VK_RIGHT
      MoveWindow(hMainWnd, hWnd, "R");
    else if (wParam == 0x25) //VK_LEFT
      MoveWindow(hMainWnd, hWnd, "L");
    else if (wParam == 0x28) //VK_DOWN
      MoveWindow(hMainWnd, hWnd, "D");
    else if (wParam == 0x26) //VK_UP
      MoveWindow(hMainWnd, hWnd, "U");
    else if (wParam == 0x23) //VK_END
      MoveWindow(hMainWnd, hWnd, "E");
    else if (wParam == 0x24) //VK_HOME
      MoveWindow(hMainWnd, hWnd, "H");
    else if (wParam == 0x22) //VK_NEXT
      MoveWindow(hMainWnd, hWnd, "B");
    else if (wParam == 0x21) //VK_PRIOR
      MoveWindow(hMainWnd, hWnd, "T");
  }

  else if (uMsg == 273) //WM_COMMAND
  {
    nLowParam = LoWord(wParam);

    if (nLowParam == IDTEXTAS0)
      bTextAs0 = AkelPad.SendMessage(lpWnd[IDTEXTAS0][WND],  240 /*BM_GETCHECK*/, 0, 0);

    else if (nLowParam == IDEMPTYAS0)
      bEmptyAs0 = AkelPad.SendMessage(lpWnd[IDEMPTYAS0][WND], 240 /*BM_GETCHECK*/, 0, 0);

    else if (nLowParam == IDCALCULATE)
    {
      //pDecSep
      oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDDECSEP][WND], lpBuffer, 256);
      pDecSep = AkelPad.MemRead(lpBuffer, _TSTR);
      if (! pDecSep)
      {
        pDecSep = ".";
        SetWindowFontAndText(lpWnd[IDDECSEP][WND], hGuiFont, pDecSep);
      }

      //pGroSep
      oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDGROSEP][WND], lpBuffer, 256);
      pGroSep = AkelPad.MemRead(lpBuffer, _TSTR);

      //pRound
      oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDROUND][WND], lpBuffer, 256);
      pRound = AkelPad.MemRead(lpBuffer, _TSTR);

      if (/[0-9a-zA-Z]/.test(pDecSep))
      {
        AkelPad.MessageBox(hWnd, pTxtBadSep, pTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
        oSys.Call("user32::SetFocus", lpWnd[IDDECSEP][WND]);
      }
      else if (/[0-9a-zA-Z]/.test(pGroSep))
      {
        AkelPad.MessageBox(hWnd, pTxtBadSep, pTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
        oSys.Call("user32::SetFocus", lpWnd[IDGROSEP][WND]);
      }
      else if (pDecSep && (pDecSep == pGroSep))
      {
        AkelPad.MessageBox(hWnd, pTxtEqualSep, pTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
        oSys.Call("user32::SetFocus", lpWnd[IDDECSEP][WND]);
      }
      else if (pRound && (isNaN(pRound) || (pRound > 20)))
      {
        AkelPad.MessageBox(hWnd, pTxtBadRound, pTxtCaption, 48 /*MB_ICONEXCLAMATION*/);
        oSys.Call("user32::SetFocus", lpWnd[IDROUND][WND]);
      }
      else
        Calculate();
    }

    else if (nLowParam == IDCLOSE)
      oSys.Call("user32::PostMessage" + _TCHAR, hWndDlg, 16 /*WM_CLOSE*/, 0, 0);

  }
  else if (uMsg == 16) //WM_CLOSE
  {
    ReadWriteIni(1);

    //Enable main window
    oSys.Call("user32::EnableWindow", hMainWnd, true);
    //Destroy dialog
    oSys.Call("user32::DestroyWindow", hWnd);
  }

  else if (uMsg == 2) //WM_DESTROY
  {
    //Exit message loop
    oSys.Call("user32::PostQuitMessage", 0);
  }

  else
  {
    if (oSys.Call("user32::GetFocus") != lpWnd[IDCLOSE][WND])
      oSys.Call("user32::DefDlgProc" + _TCHAR, hWnd, 1025 /*DM_SETDEFID*/, IDCALCULATE, 0);
  }

  return 0;
}

function SetWindowFontAndText(hWnd, hFont, pText)
{
  var lpWindowText;

  AkelPad.SendMessage(hWnd, 48 /*WM_SETFONT*/, hFont, true);

  if (lpWindowText=AkelPad.MemAlloc(256 * _TSIZE))
  {
    AkelPad.MemCopy(lpWindowText, pText.substr(0, 255), _TSTR);
    oSys.Call("user32::SetWindowText" + _TCHAR, hWnd, lpWindowText);

    AkelPad.MemFree(lpWindowText);
  }
}

function MoveWindow(hWndParent, hWnd, Action)
{
  var rcWndParent;
  var rcWnd;
  var nX;
  var nY;

  if (! hWndParent)
    hWndParent=oSys.Call("user32::GetDesktopWindow");

  rcWndParent = GetWindowPos(hWndParent);
  rcWnd       = GetWindowPos(hWnd);

  nX = rcWnd.left;
  nY = rcWnd.top;

  if (Action == "R") //Move right
    nX = rcWnd.left + ((rcWnd.left < rcWndParent.right - 50) ? 20: 0);
  else if (Action == "L") //Move left
    nX = rcWnd.left - ((rcWnd.right > rcWndParent.left + 50) ? 20: 0);
  else if (Action == "D") //Move down
    nY = rcWnd.top + ((rcWnd.top < rcWndParent.bottom - 50) ? 20: 0);
  else if (Action == "U") //Move up
    nY = rcWnd.top - ((rcWnd.bottom > rcWndParent.top + 50) ? 20: 0);
  else if (Action == "E") //Move end (right)
    nX = rcWnd.left + (rcWndParent.right - rcWnd.right);
  else if (Action == "H") //Move home (left)
    nX = rcWnd.left + (rcWndParent.left - rcWnd.left);
  else if (Action == "B") //Move bottom
    nY = rcWnd.top + (rcWndParent.bottom - rcWnd.bottom);
  else if (Action == "T") //Move top
    nY = rcWnd.top + (rcWndParent.top - rcWnd.top);
  else if (Action == "C") //Center window
  {
    nX = rcWndParent.left + ((rcWndParent.right  - rcWndParent.left) / 2 - (rcWnd.right  - rcWnd.left) / 2);
    nY = rcWndParent.top  + ((rcWndParent.bottom - rcWndParent.top)  / 2 - (rcWnd.bottom - rcWnd.top)  / 2);
  }
  else if (Action == "LT") //Move left top
  {
    nX = rcWndParent.left;
    nY = rcWndParent.top;
  }
  else if (Action == "RT") //Move right top
  {
    nX = rcWnd.left + (rcWndParent.right - rcWnd.right);
    nY = rcWndParent.top;
  }
  else if (Action == "LB") //Move left bottom
  {
    nX = rcWndParent.left;
    nY = rcWnd.top + (rcWndParent.bottom - rcWnd.bottom);
  }
  else if (Action == "RB") //Move right bottom
  {
    nX = rcWnd.left + (rcWndParent.right - rcWnd.right);
    nY = rcWnd.top + (rcWndParent.bottom - rcWnd.bottom);
  }
  else
  {
    nX = Action[0];
    nY = Action[1];
  }

  oSys.Call("user32::SetWindowPos", hWnd, 0, nX, nY, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);
}

function GetWindowPos(hWnd)
{
  var lpRect = AkelPad.MemAlloc(16) //sizeof(RECT);
  var rcRect = [];

  oSys.Call("user32::GetWindowRect", hWnd, lpRect);

  rcRect.left   = AkelPad.MemRead(lpRect,     DT_DWORD);
  rcRect.top    = AkelPad.MemRead(lpRect + 4, DT_DWORD);
  rcRect.right  = AkelPad.MemRead(lpRect + 8, DT_DWORD);
  rcRect.bottom = AkelPad.MemRead(lpRect +12, DT_DWORD);
  AkelPad.MemFree(lpRect);

  return rcRect;
}

function LoWord(nParam)
{
  return (nParam & 0xffff);
}

function ReadWriteIni(bWrite)
{
  var oFSO     = new ActiveXObject("Scripting.FileSystemObject");
  var pIniName = WScript.ScriptFullName.substring(0, WScript.ScriptFullName.lastIndexOf(".")) + ".ini";
  var rcWnd;
  var nError;
  var oFile;
  var pTxt;

  if (bWrite)
  {
    rcWnd = GetWindowPos(hWndDlg);

    oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDDECSEP][WND], lpBuffer, 256);
    pDecSep = AkelPad.MemRead(lpBuffer, _TSTR);
    oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDGROSEP][WND], lpBuffer, 256);
    pGroSep = AkelPad.MemRead(lpBuffer, _TSTR);
    oSys.Call("user32::GetWindowText" + _TCHAR, lpWnd[IDROUND][WND], lpBuffer, 256);
    pRound = AkelPad.MemRead(lpBuffer, _TSTR);

    pTxt = 'nWndPosX='  + rcWnd.left + ';\r\n' +
           'nWndPosY='  + rcWnd.top  + ';\r\n' +
           'pDecSep="'  + pDecSep.replace(/[\\"]/g, "\\$&") + '";\r\n' +
           'pGroSep="'  + pGroSep.replace(/[\\"]/g, "\\$&") + '";\r\n' +
           'pRound="'   + pRound    + '";\r\n' +
           'bTextAs0='  + bTextAs0  + ';\r\n' +
           'bEmptyAs0=' + bEmptyAs0 + ';';

    oFile = oFSO.OpenTextFile(pIniName, 2, true, 0);
    oFile.Write(pTxt);
    oFile.Close();
  }

  else if ((! bWrite) && (oFSO.FileExists(pIniName)))
  {
    try
    {
      eval(AkelPad.ReadFile(pIniName));
    }
    catch (nError)
    {
    }
  }
}

function Calculate()
{
  var pSelTxt   = AkelPad.GetSelText(1 /*\r*/);
  var nCountN   = 0;
  var nCountE   = 0;
  var nCountT   = 0;
  var nSum      = 0;
  var nMin      = "";
  var nMax      = "";
  var nAve      = "";
  var nNum;
  var oPattern;
  var lpTxt;
  var nCountA;
  var nCount;
  var nRound;
  var nError;
  var i;

  if (pGroSep)
  {
    oPattern = new RegExp("\\" + pGroSep, "gm");
    pSelTxt  = pSelTxt.replace(oPattern, "");
  }

  oPattern = new RegExp("\\" + pDecSep, "gm");
  pSelTxt  = pSelTxt.replace(oPattern, ".");

  lpTxt   = pSelTxt.split("\r");
  nCountA = lpTxt.length;

  for (i = 0; i < nCountA; ++i)
  {
    if (/[^ \t]/.test(lpTxt[i])) /*no empty*/
    {
      try
      {
        with (Math)
        {
          nNum = eval(lpTxt[i]);
          if (isFinite(nNum))
          {
            ++ nCountN;
            if (nMin === "")
            {
              nMin = nNum;
              nMax = nNum;
            }
            else if (nNum < nMin)
              nMin = nNum;
            else if (nNum > nMax)
              nMax = nNum;
          }
          else
          {
            ++ nCountT;
            nNum = 0;
            if (bTextAs0)
            {
              if (nMin === "")
              {
                nMin = nNum;
                nMax = nNum;
              }
              else if (nNum < nMin)
                nMin = nNum;
              else if (nNum > nMax)
                nMax = nNum;
            }
          }
        }
      }
      catch (nError)
      {
        ++ nCountT;
        nNum = 0;
        if (bTextAs0)
        {
          if (nMin === "")
          {
            nMin = nNum;
            nMax = nNum;
          }
          else if (nNum < nMin)
            nMin = nNum;
          else if (nNum > nMax)
            nMax = nNum;
        }
      }
    }
    else
    {
      ++ nCountE;
      nNum = 0;
      if (bEmptyAs0)
      {
        if (nMin === "")
        {
          nMin = nNum;
          nMax = nNum;
        }
        else if (nNum < nMin)
          nMin = nNum;
        else if (nNum > nMax)
          nMax = nNum;
      }
    }
    nSum += nNum;
  }

  if ((nCountN == 0) && (! bTextAs0) && (! bEmptyAs0))
    nSum = "";

  if (nSum !== "")
  {
    nCount = nCountN;
    if (bTextAs0)
      nCount += nCountT;
    if (bEmptyAs0)
      nCount += nCountE;

    nAve = nSum / nCount;
  }

  if ((nSum !== "") && (pRound))
  {
    nRound = parseInt(pRound); 
    nSum   = round2(nSum, nRound);
    nMin   = round2(nMin, nRound);
    nMax   = round2(nMax, nRound);
    nAve   = round2(nAve, nRound);

    if (nRound >= 0)
    {
      nSum = nSum.toFixed(nRound);
      nMin = nMin.toFixed(nRound);
      nMax = nMax.toFixed(nRound);
      nAve = nAve.toFixed(nRound);
    }
  }

  nSum    = nSum.toString().replace(".", pDecSep);
  nMin    = nMin.toString().replace(".", pDecSep);
  nMax    = nMax.toString().replace(".", pDecSep);
  nAve    = nAve.toString().replace(".", pDecSep);
  nCountA = nCountA.toString();
  nCountN = nCountN.toString();
  nCountT = nCountT.toString();
  nCountE = nCountE.toString();

  if (pGroSep)
  {
    nSum    = InsertGroSep(nSum,    pGroSep, pDecSep);
    nMin    = InsertGroSep(nMin,    pGroSep, pDecSep);
    nMax    = InsertGroSep(nMax,    pGroSep, pDecSep);
    nAve    = InsertGroSep(nAve,    pGroSep, pDecSep);
    nCountA = InsertGroSep(nCountA, pGroSep, pDecSep);
    nCountN = InsertGroSep(nCountN, pGroSep, pDecSep);
    nCountT = InsertGroSep(nCountT, pGroSep, pDecSep);
    nCountE = InsertGroSep(nCountE, pGroSep, pDecSep);
  }

  SetWindowFontAndText(lpWnd[IDSUM][WND],    hGuiFont, nSum);
  SetWindowFontAndText(lpWnd[IDMIN][WND],    hGuiFont, nMin);
  SetWindowFontAndText(lpWnd[IDMAX][WND],    hGuiFont, nMax);
  SetWindowFontAndText(lpWnd[IDAVE][WND],    hGuiFont, nAve);
  SetWindowFontAndText(lpWnd[IDCOUNTA][WND], hGuiFont, nCountA);
  SetWindowFontAndText(lpWnd[IDCOUNTN][WND], hGuiFont, nCountN);
  SetWindowFontAndText(lpWnd[IDCOUNTT][WND], hGuiFont, nCountT);
  SetWindowFontAndText(lpWnd[IDCOUNTE][WND], hGuiFont, nCountE);

  oSys.Call("user32::SetFocus", lpWnd[IDSUM][WND]);
}

function round2(nNum, nDec)
{
  return Math.round(nNum * Math.pow(10, nDec))/Math.pow(10, nDec);
}

function InsertGroSep(pNum, pGroSep, pDecSep)
{
  var nInd = pNum.indexOf(pDecSep);
  var pStr = "";
  var pNum1;
  var pNum2;
  var nInt;
  var nRest;
  var i;

  if (nInd == -1)
  {
    pNum1 = pNum;
    pNum2 = "";
  }
  else
  {
    pNum1 = pNum.substr(0, nInd);
    pNum2 = pNum.substr(nInd);
  }

  if (pNum1.length > 3)
  {
    nInt  = Math.floor(pNum1.length / 3);
    nRest = pNum1.length % 3;

    if (nRest > 0)
      pStr = pNum1.substr(0, nRest) + pGroSep;

    for (i = 0; i < nInt; ++i)
      pStr = pStr + pNum1.substr(nRest + i * 3, 3) + (i < nInt - 1 ? pGroSep : "");

    pNum1 = pStr;
  }

  return pNum1 + pNum2;
}



Last edited by KDJ on Tue Mar 08, 2011 8:28 pm; edited 10 times in total
Back to top
View user's profile Send private message
Instructor
Site Admin


Joined: 06 Jul 2006
Posts: 5373

PostPosted: Fri Oct 22, 2010 7:00 pm    Post subject: Reply with quote

Call NSIS plugin example.

NsisCall.js

Code:
// http://akelpad.sourceforge.net/forum/viewtopic.php?p=9530#9530
// Version v1.1
//
//
//// Call NSIS plugin example.

var hMainWnd=AkelPad.GetMainWnd();
var oSys=AkelPad.SystemFunction();
var pMD5;

//NSIS
var NSIS_MAX_STRLEN=1024;
var nNsisUnicode=0 /*DT_ANSI*/;
var lpStack=0;

if (hMainWnd)
{
  //Get MD5 of file
  pushstring("c:\\_1.txt");
  nsiscall("md5dll::GetMD5File");
  pMD5=popstring();
  WScript.Echo(pMD5);
}


//Functions
function nsiscall(pFunction)
{
  var lppStack;

  if (lppStack=AkelPad.MemAlloc(4 /*DWORD*/))
  {
    AkelPad.MemCopy(lppStack, lpStack, 3 /*DT_DWORD*/);
    oSys.Call(pFunction, hMainWnd, NSIS_MAX_STRLEN, 0, lppStack);
    lpStack=AkelPad.MemRead(lppStack, 3 /*DT_DWORD*/);
    AkelPad.MemFree(lppStack);
  }
}

function pushstring(pString)
{
  var lpTop;

  if (lpTop=oSys.Call("kernel32::GlobalAlloc", 0x40 /*GPTR*/, (nNsisUnicode?NSIS_MAX_STRLEN * 2:NSIS_MAX_STRLEN) + 8 /*sizeof(stack_t)*/))
  {
    AkelPad.MemCopy(lpTop + 4 /*stack_t.text*/, pString.substr(0, NSIS_MAX_STRLEN), nNsisUnicode);
    AkelPad.MemCopy(lpTop + 0 /*stack_t.next*/, lpStack, 3 /*DT_DWORD*/);
    lpStack=lpTop;
  }
}

function popstring(pString)
{
  var lpTop;
  var pString="";

  if (lpStack)
  {
    lpTop=lpStack;
    pString=AkelPad.MemRead(lpTop + 4 /*stack_t.text*/, nNsisUnicode);
    lpStack=AkelPad.MemRead(lpTop + 0 /*stack_t.next*/, 3 /*DT_DWORD*/);
    oSys.Call("kernel32::GlobalFree", lpTop);
  }
  return pString;
}



Last edited by Instructor on Sat Jun 11, 2011 8:05 am; edited 3 times in total
Back to top
View user's profile Send private message Send e-mail
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Fri Oct 22, 2010 10:01 pm    Post subject: Reply with quote

Set initial font size from AkelPad.ini or registry.
This feature is built into version 4.5.6 AkelPad as command 4206.

FontIniSize.js

Code:
// Set initial font size from AkelPad.ini or registry - 2010-10-26
//
// Call("Scripts::Main", 1, "FontIniSize.js")
//
// Can assign shortcut key, eg: Ctrl+Num0 or Ctrl+Num/

var hEditWnd = AkelPad.GetEditWnd();
var oSys     = AkelPad.SystemFunction();
var pIniFile = AkelPad.GetAkelDir() + "\\AkelPad.ini";

var pTxtIni     = "";
var pTxtSaveSet = "";
var pTxtFont    = "";
var pTxt        = "";
var lfHeight;
var hDC;
var nDevCap;
var nFontSize;
var i;

if (! hEditWnd)
  WScript.Quit();

pTxtIni     = AkelPad.ReadFile(pIniFile);
pTxtSaveSet = pTxtIni.substr(pTxtIni.indexOf("SaveSettings=") + 13, 1);

//settings in .ini file
if (pTxtSaveSet == "2")
{
  pTxtFont = pTxtIni.substr(pTxtIni.indexOf("Font=") + 5, 8);
  for (i = 6; i >= 0; i -= 2)
    pTxt += pTxtFont.substr(i, 2);

  lfHeight = Number("0x" + pTxt);
}
//settings in registry
else
{
  lfHeight = ReadRegistry();
}

hDC       = oSys.Call("user32::GetDC", hEditWnd);
nDevCap   = oSys.Call("gdi32::GetDeviceCaps" , hDC, 90 /*LOGPIXELSY*/);
nFontSize = -oSys.Call("kernel32::MulDiv", lfHeight, 72, nDevCap);
oSys.Call("user32::ReleaseDC", hEditWnd, hDC);

if (nFontSize > 0)
  AkelPad.Font("", 0, nFontSize);

//////////////////
function ReadRegistry()
{
  var DT_DWORD          = 3;
  var HKEY_CURRENT_USER = 0x80000001;
  var KEY_QUERY_VALUE   = 0x0001;
  var REG_BINARY        = 3;
  var ERROR_SUCCES      = 0;
  var ERROR_MORE_DATA   = 234;

  var lpSubKey   = MakeString("SOFTWARE\\Akelsoft\\AkelPad\\Options");
  var lpValue    = MakeString("Font");
  var lpKey      = AkelPad.MemAlloc(4);
  var lpType     = AkelPad.MemAlloc(4);
  var lpData     = AkelPad.MemAlloc(28);
  var lpDataSize = AkelPad.MemAlloc(4);
  var lfHeight   = 0;
  var hKey;
  var nError;

  nError = oSys.Call("advapi32::RegOpenKeyEx" + _TCHAR,
             HKEY_CURRENT_USER, // Parent key
             lpSubKey,          // Subkey
             0,                 // Reserved. must be 0
             KEY_QUERY_VALUE,   // Desired access
             lpKey);            // Result key

  if(nError == ERROR_SUCCES)
  {
    hKey = AkelPad.MemRead(lpKey, DT_DWORD);
    AkelPad.MemCopy(lpType, REG_BINARY, DT_DWORD);
    AkelPad.MemCopy(lpDataSize, 28, DT_DWORD);

    nError = oSys.Call("advapi32::RegQueryValueEx" + _TCHAR,
               hKey,        // Parent key
               lpValue,     // Value name
               0,           // Must be NULL
               lpType,      // Value type receiver: will be REG_BINARY
               lpData,      // Buffer with data
               lpDataSize); // Buffer size
    if ((nError == ERROR_SUCCES || nError == ERROR_MORE_DATA) &&
        (AkelPad.MemRead(lpType, DT_DWORD) == REG_BINARY)     && //! Must be binary
        (AkelPad.MemRead(lpDataSize, DT_DWORD) >= 4)             //! Must be at least 4 bytes
       )
    {
      lfHeight = AkelPad.MemRead(lpData, DT_DWORD);
    }

    oSys.Call("advapi32::RegCloseKey", hKey);
  }

  AkelPad.MemFree(lpDataSize);
  AkelPad.MemFree(lpData);
  AkelPad.MemFree(lpType);
  AkelPad.MemFree(lpKey);
  AkelPad.MemFree(lpValue);
  AkelPad.MemFree(lpSubKey);
  return lfHeight;
}

function MakeString(pStr)
{
  var lpBuffer = AkelPad.MemAlloc((pStr.length + 1) * _TSIZE);
  AkelPad.MemCopy(lpBuffer, pStr, _TSTR);
  return lpBuffer;
}



A similar function in VBS (for AkelPad ver. 4.5).

FontIniSize.vbs

Code:
' Set the initial font size from AkelPad.ini or registry - 2011-01-08
'
' Call("Scripts::Main", 1, "FontIniSize.vbs")
'
' Can assign shortcut key, eg: Ctrl+Num/ or Ctrl+Num*

Option Explicit
On Error Resume Next

Const LOGPIXELSY = 90

Dim hEditWnd
Dim oSys
Dim pIniFile
Dim WshShell
Dim pTxtIni
Dim pTxtSaveSet
Dim pTxt
Dim lfHeight
Dim hDC
Dim nDevCap
Dim nFontSize
Dim i

hEditWnd = AkelPad.GetEditWnd()

If (hEditWnd = 0) Then
  WScript.Quit
End If

set oSys     = AkelPad.SystemFunction()
set WshShell = CreateObject("WScript.shell")

pIniFile    = (AkelPad.GetAkelDir & "\AkelPad.ini")
pTxtIni     = AkelPad.ReadFile(pIniFile)
pTxtSaveSet = Mid(pTxtIni, InStr(pTxtIni, "SaveSettings=") + 13, 1)
pTxt        = ""

'settings in .ini file
If pTxtSaveSet = "2" Then
  For i = 6 To 0 Step -2
    pTxt = pTxt & Mid(pTxtIni, InStr(pTxtIni, "Font=") + 5 + i, 2)
  Next

'settings in registry
Else
  pTxtIni = WshShell.RegRead("HKCU\Software\Akelsoft\AkelPad\Options\Font")

  If Err.Number Then
    WScript.Quit
  End If

  If IsArray(pTxtIni) Then
    For i = 3 To 0 Step -1
      pTxt = pTxt & PadL0(Hex(pTxtIni(i)))
    Next
  Else
    WScript.Quit
  End If
End If

lfHeight = Eval("&H" + pTxt)

If (lfHeight < 0) Then
  lfHeight = lfHeight + 4294967296
End If

hDC       = oSys.Call("user32::GetDC", hEditWnd)
nDevCap   = oSys.Call("gdi32::GetDeviceCaps", hDC, LOGPIXELSY)
nFontSize = -oSys.Call("kernel32::MulDiv", lfHeight, 72, nDevCap)
oSys.Call "user32::ReleaseDC", hEditWnd, hDC

If nFontSize > 0 Then
  AkelPad.Font "", 0, nFontSize
End If

''''''''''''''''''''
Function PadL0(pTxt)
  Do While len(pTxt) < 2
    pTxt = "0" & pTxt
  Loop

  PadL0 = pTxt
End Function



----
PS: Instructor and FeyFre, thanks for the lessons.


Last edited by KDJ on Sun Mar 27, 2011 1:07 pm; edited 2 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Tue Nov 02, 2010 6:45 am    Post subject: Reply with quote

Select range of text, change selection.

SelectRangeText.js

Code:
// SelectRangeText.js - ver. 2014-04-14 (x86/x64)
//
// Select range of text.
//
// Usage:
// Call("Scripts::Main", 1, "SelectRangeText.js")
//
// Shortcut keys in dialog box:
// Alt+End, Home, PgDn, PgUp - set caret position
// Enter - Apply
// Esc   - Close
// Shift+Alt+C, Right, Left, Down, Up, End, Home, PgDn, PgUp - move dialog box.

var oSys = AkelPad.SystemFunction();

if (oSys.Call("Kernel32::GetUserDefaultLangID") == 0x0415) //Polish
{
  var sTxtCaption = "Zaznacz zakres";
  var sTxtCoordin = "Współrzędne";
  var sTxtLine    = "Wiersz";
  var sTxtColumn  = "Kolumna";
  var sTxtBegin   = "Początek";
  var sTxtEnd     = "Koniec";
  var sTxtCaret   = "Kursor";
  var sTxtOffset1 = "Offset";
  var sTxtOffset2 = "&Offset";
  var sTxtColSel  = "&Zaznaczenie pionowe";
  var sTxtCarPos  = "Pozycja kursora";
  var sTxtCarPos0 = "L.góra";
  var sTxtCarPos1 = "P.góra";
  var sTxtCarPos2 = "P.dół";
  var sTxtCarPos3 = "L.dół";
  var sTxtApply   = "Zastosuj";
  var sTxtClose   = "Zamknij";
}
else
{
  var sTxtCaption = "Select range";
  var sTxtCoordin = "Coordinates";
  var sTxtLine    = "Line";
  var sTxtColumn  = "Column";
  var sTxtBegin   = "Begin";
  var sTxtEnd     = "End";
  var sTxtCaret   = "Caret";
  var sTxtOffset1 = "Offset";
  var sTxtOffset2 = "&Offset";
  var sTxtColSel  = "&Columnar selection";
  var sTxtCarPos  = "Caret position";
  var sTxtCarPos0 = "L.top";
  var sTxtCarPos1 = "R.top";
  var sTxtCarPos2 = "R.bottom";
  var sTxtCarPos3 = "L.bottom";
  var sTxtApply   = "Apply";
  var sTxtClose   = "Close";
}

var DT_DWORD              = 3;
var AEGI_FIRSTSELCHAR     = 3;
var AEM_GETSEL            = 3125;
var AEM_SETSEL            = 3126;
var AEM_GETCOLUMNSEL      = 3127;
var AEM_GETINDEX          = 3130;
var AEM_INDEXUPDATE       = 3132;
var AEM_INDEXCOMPARE      = 3133;
var AEM_INDEXTORICHOFFSET = 3136;
var AEM_RICHOFFSETTOINDEX = 3137;

var hMainWnd     = AkelPad.GetMainWnd();
var hEditWnd     = AkelPad.GetEditWnd();
var hGuiFont     = oSys.Call("Gdi32::GetStockObject", 17 /*DEFAULT_GUI_FONT*/);
var sScriptName  = WScript.ScriptName;
var hInstanceDLL = AkelPad.GetInstanceDll();

var bOffset   = 0;
var bColSel   = SendMessage(hEditWnd, AEM_GETCOLUMNSEL, 0, 0);
var lpFirstC  = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpCaret   = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
var lpSelect  = AkelPad.MemAlloc(_X64 ? 56 : 32 /*sizeof(AESELECTION)*/);
var lpBegSel  = lpSelect;
var lpEndSel  = lpSelect + (_X64 ? 24 : 12);
var lpSelFlag = lpSelect + (_X64 ? 48 : 24);
var lpBegL    = lpBegSel;
var lpBegC    = lpBegSel + (_X64 ? 16 : 8);
var lpEndL    = lpEndSel;
var lpEndC    = lpEndSel + (_X64 ? 16 : 8);
var lpCarL    = lpCaret;
var lpCarC    = lpCaret + (_X64 ? 16 : 8);
var nBegOf;
var nEndOf;
var nCarOf;
var hWndDlg;
var lpBuffer;

var aWnd      = [];
var IDCOORDIN = 1000;
var IDTXTBEG  = 1001;
var IDTXTEND  = 1002;
var IDTXTCAR  = 1003;
var IDTXTLIN  = 1004;
var IDTXTCOL  = 1005;
var IDBEGL    = 1006;
var IDBEGC    = 1007;
var IDENDL    = 1008;
var IDENDC    = 1009;
var IDCARL    = 1010;
var IDCARC    = 1011;
var IDTXTOF   = 1012;
var IDBEGOF   = 1013;
var IDENDOF   = 1014;
var IDCAROF   = 1015;
var IDOFFSET  = 1016;
var IDCOLSEL  = 1017;
var IDCARPOS  = 1018;
var IDCARPOSB = 1019;
var IDCARPOSE = 1020;
var IDCARPOS0 = 1021;
var IDCARPOS1 = 1022;
var IDCARPOS2 = 1023;
var IDCARPOS3 = 1024;
var IDAPPLY   = 1025;
var IDCLOSE   = 1026;

var WNDTYPE  = 0;
var WND      = 1;
var WNDEXSTY = 2;
var WNDSTY   = 3;
var WNDX     = 4;
var WNDY     = 5;
var WNDW     = 6;
var WNDH     = 7;
var WNDTXT   = 8;

//0x50000000 - WS_VISIBLE|WS_CHILD
//0x50000007 - WS_VISIBLE|WS_CHILD|BS_GROUPBOX
//0x50000009 - WS_VISIBLE|WS_CHILD|BS_AUTORADIOBUTTON
//0x50010000 - WS_VISIBLE|WS_CHILD|WS_TABSTOP
//0x50010001 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_DEFPUSHBUTTON
//0x50010003 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_AUTOCHECKBOX
//0x50012080 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|ES_AUTOHSCROLL|ES_NUMBER
//Windows            WNDTYPE, WND,WNDEXSTY,     WNDSTY,WNDX,WNDY,WNDW,WNDH, WNDTXT
aWnd[IDCOORDIN] = ["BUTTON",   0,       0, 0x50000007,  10,  10, 185, 135, sTxtCoordin];
aWnd[IDTXTBEG ] = ["STATIC",   0,       0, 0x50000000,  20,  45,  50,  13, sTxtBegin];
aWnd[IDTXTEND ] = ["STATIC",   0,       0, 0x50000000,  20,  70,  50,  13, sTxtEnd];
aWnd[IDTXTCAR ] = ["STATIC",   0,       0, 0x50000000,  20,  95,  50,  13, sTxtCaret];
aWnd[IDTXTLIN ] = ["STATIC",   0,       0, 0x50000000,  70,  30,  50,  13, sTxtLine];
aWnd[IDTXTCOL ] = ["STATIC",   0,       0, 0x50000000, 130,  30,  50,  13, sTxtColumn];
aWnd[IDBEGL   ] = ["EDIT",     0,   0x200, 0x50012080,  70,  45,  55,  20, ""];
aWnd[IDBEGC   ] = ["EDIT",     0,   0x200, 0x50012080, 130,  45,  55,  20, ""];
aWnd[IDENDL   ] = ["EDIT",     0,   0x200, 0x50012080,  70,  70,  55,  20, ""];
aWnd[IDENDC   ] = ["EDIT",     0,   0x200, 0x50012080, 130,  70,  55,  20, ""];
aWnd[IDCARL   ] = ["STATIC",   0,   0x200, 0x50000000,  70,  95,  55,  20, ""];
aWnd[IDCARC   ] = ["STATIC",   0,   0x200, 0x50000000, 130,  95,  55,  20, ""];
aWnd[IDTXTOF  ] = ["STATIC",   0,       0, 0x50000000,  70,  30,  50,  13, sTxtOffset1];
aWnd[IDBEGOF  ] = ["EDIT",     0,   0x200, 0x50012080,  70,  45, 115,  20, ""];
aWnd[IDENDOF  ] = ["EDIT",     0,   0x200, 0x50012080,  70,  70, 115,  20, ""];
aWnd[IDCAROF  ] = ["STATIC",   0,   0x200, 0x50000000,  70,  95, 115,  20, ""];
aWnd[IDOFFSET ] = ["BUTTON",   0,       0, 0x50010003,  70, 122, 120,  16, sTxtOffset2];
aWnd[IDCOLSEL ] = ["BUTTON",   0,       0, 0x50010003, 225,  15, 120,  16, sTxtColSel];
aWnd[IDCARPOS ] = ["BUTTON",   0,       0, 0x50000007, 205,  45, 140,  65, sTxtCarPos];
aWnd[IDCARPOSB] = ["BUTTON",   0,       0, 0x50000009, 215,  65,  60,  16, sTxtBegin];
aWnd[IDCARPOSE] = ["BUTTON",   0,       0, 0x50000009, 280,  85,  60,  16, sTxtEnd];
aWnd[IDCARPOS0] = ["BUTTON",   0,       0, 0x50000009, 215,  65,  60,  16, sTxtCarPos0];
aWnd[IDCARPOS1] = ["BUTTON",   0,       0, 0x50000009, 280,  65,  60,  16, sTxtCarPos1];
aWnd[IDCARPOS2] = ["BUTTON",   0,       0, 0x50000009, 280,  85,  60,  16, sTxtCarPos2];
aWnd[IDCARPOS3] = ["BUTTON",   0,       0, 0x50000009, 215,  85,  60,  16, sTxtCarPos3];
aWnd[IDAPPLY  ] = ["BUTTON",   0,       0, 0x50010001, 205, 123,  65,  23, sTxtApply];
aWnd[IDCLOSE  ] = ["BUTTON",   0,       0, 0x50010000, 280, 123,  65,  23, sTxtClose];

if (hEditWnd)
{
  if (AkelPad.WindowRegisterClass(sScriptName))
  {
    if (lpBuffer = AkelPad.MemAlloc(256 * _TSIZE))
    {
      //Create dialog
      AkelPad.MemCopy(lpBuffer, sScriptName, _TSTR);
      hWndDlg = oSys.Call("User32::CreateWindowEx" + _TCHAR,
                          0,               //dwExStyle
                          lpBuffer,        //lpClassName
                          0,               //lpWindowName
                          0x90CA0000,      //WS_VISIBLE|WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
                          0,               //x
                          0,               //y
                          360,             //nWidth
                          187,             //nHeight
                          hMainWnd,        //hWndParent
                          0,               //ID
                          hInstanceDLL,    //hInstance
                          DialogCallback); //Script function callback. To use it class must be registered by WindowRegisterClass.
      if (hWndDlg)
      {
        //Disable main window, to make dialog modal
        oSys.Call("User32::EnableWindow", hMainWnd, false);

        //Message loop
        AkelPad.WindowGetMessage();
      }
      AkelPad.MemFree(lpBuffer);
    }
    AkelPad.WindowUnregisterClass(sScriptName);
  }
}

AkelPad.MemFree(lpFirstC);
AkelPad.MemFree(lpCaret);
AkelPad.MemFree(lpSelect);

function DialogCallback(hWnd, uMsg, wParam, lParam)
{
  var i;
  var nLowParam;

  if (uMsg == 1) //WM_CREATE
  {
    //Dialog caption
    AkelPad.MemCopy(lpBuffer, sTxtCaption, _TSTR);
    oSys.Call("User32::SetWindowText" + _TCHAR, hWnd, lpBuffer);

    //Create windows
    for (i = 1000; i < aWnd.length; ++i)
    {
      AkelPad.MemCopy(lpBuffer, aWnd[i][WNDTYPE], _TSTR);
      aWnd[i][WND] = oSys.Call("User32::CreateWindowEx" + _TCHAR,
                               aWnd[i][WNDEXSTY],//dwExStyle
                               lpBuffer,         //lpClassName
                               0,                //lpWindowName
                               aWnd[i][WNDSTY],  //dwStyle
                               aWnd[i][WNDX],    //x
                               aWnd[i][WNDY],    //y
                               aWnd[i][WNDW],    //nWidth
                               aWnd[i][WNDH],    //nHeight
                               hWnd,             //hWndParent
                               i,                //ID
                               hInstanceDLL,     //hInstance
                               0);               //lpParam
      //Set font and text
      SetWindowFontAndText(aWnd[i][WND], hGuiFont, aWnd[i][WNDTXT]);
    }

    //Set limit edit text
    for (i = IDBEGL; i <= IDENDC; ++i)
      SendMessage(aWnd[i][WND], 197 /*EM_LIMITTEXT*/, 9, 0);
    for (i = IDBEGOF; i <= IDENDOF; ++i)
      SendMessage(aWnd[i][WND], 197 /*EM_LIMITTEXT*/, 9, 0);

    //Get selection and set text to coordinates
    GetSelection();

    //Check
    CheckButtons();
    ShowHideWindows();

    //Set window position
    MoveWindow(hMainWnd, hWnd, "RT");
  }

  else if (uMsg == 256) //WM_KEYDOWN
  {
    if (wParam == 27) //VK_ESCAPE
      PostMessage(hWndDlg, 273 /*WM_COMMAND*/, IDCLOSE, 0);
    else if (wParam == 13) //VK_RETURN
      PostMessage(hWndDlg, 273 /*WM_COMMAND*/, IDAPPLY, 0);
  }

  else if (uMsg == 260) //WM_SYSKEYDOWN
  {
    if (oSys.Call("User32::GetAsyncKeyState", 0xA0 /*VK_LSHIFT*/))
    {
      if (wParam == 0x27) //VK_RIGHT
        MoveWindow(hMainWnd, hWnd, "R");
      else if (wParam == 0x25) //VK_LEFT
        MoveWindow(hMainWnd, hWnd, "L");
      else if (wParam == 0x28) //VK_DOWN
        MoveWindow(hMainWnd, hWnd, "D");
      else if (wParam == 0x26) //VK_UP
        MoveWindow(hMainWnd, hWnd, "U");
      else if (wParam == 0x23) //VK_END
        MoveWindow(hMainWnd, hWnd, "E");
      else if (wParam == 0x24) //VK_HOME
        MoveWindow(hMainWnd, hWnd, "H");
      else if (wParam == 0x22) //VK_NEXT
        MoveWindow(hMainWnd, hWnd, "B");
      else if (wParam == 0x21) //VK_PRIOR
        MoveWindow(hMainWnd, hWnd, "T");
      else if (wParam == 0x43) //C
        MoveWindow(hMainWnd, hWnd, "C");
    }
    else
    {
      if (wParam == 0x24) //VK_HOME
        SetRadiobutton(0);
      else if (wParam == 0x21) //VK_PRIOR
        SetRadiobutton(bColSel ? 1 : 0);
      else if (wParam == 0x22) //VK_NEXT
        SetRadiobutton(bColSel ? 2 : 1);
      else if (wParam == 0x23) //VK_END
        SetRadiobutton(bColSel ? 3 : 1);
    }
  }

  else if (uMsg == 273) //WM_COMMAND
  {
    nLowParam = LoWord(wParam);

    if (nLowParam == IDOFFSET)
    {
      bOffset = SendMessage(aWnd[IDOFFSET][WND],  240 /*BM_GETCHECK*/, 0, 0);
      ShowHideWindows();
      SendMessage(aWnd[bOffset ? IDBEGOF : IDBEGL][WND], 177 /*EM_SETSEL*/, 0, -1);
      oSys.Call("User32::SetFocus", aWnd[bOffset ? IDBEGOF : IDBEGL][WND]);
    }

    else if (nLowParam == IDCOLSEL)
    {
      bColSel = SendMessage(aWnd[IDCOLSEL][WND],  240 /*BM_GETCHECK*/, 0, 0);
      ShowHideWindows();
    }

    else if (nLowParam == IDAPPLY)
    {
      SetSelection();
      GetSelection();
    }

    else if (nLowParam == IDCLOSE)
      PostMessage(hWndDlg, 16 /*WM_CLOSE*/, 0, 0);
  }

  else if (uMsg == 16) //WM_CLOSE
  {
    //Enable main window
    oSys.Call("User32::EnableWindow", hMainWnd, true);

    //Destroy dialog
    oSys.Call("User32::DestroyWindow", hWnd);
  }
  else if (uMsg == 2) //WM_DESTROY
  {
    //Exit message loop
    oSys.Call("User32::PostQuitMessage", 0);
  }

  else
  {
    if (oSys.Call("User32::GetFocus") != aWnd[IDCLOSE][WND])
      oSys.Call("User32::DefDlgProc" + _TCHAR, hWnd, 1025 /*DM_SETDEFID*/, IDAPPLY, 0);
  }

  return 0;
}

function PostMessage(hWnd, uMsg, wParam, lParam)
{
  return oSys.Call("User32::PostMessage" + _TCHAR, hWnd, uMsg, wParam, lParam);
}

function SendMessage(hWnd, uMsg, wParam, lParam)
{
  return oSys.Call("User32::SendMessage" + _TCHAR, hWnd, uMsg, wParam, lParam);
}

function LoWord(nParam)
{
  return nParam & 0xFFFF;
}

function SetWindowFontAndText(hWnd, hFont, pText)
{
  var lpWindowText;

  SendMessage(hWnd, 48 /*WM_SETFONT*/, hFont, true);

  if (lpWindowText = AkelPad.MemAlloc(256 * _TSIZE))
  {
    AkelPad.MemCopy(lpWindowText, pText.substr(0, 255), _TSTR);
    oSys.Call("User32::SetWindowText" + _TCHAR, hWnd, lpWindowText);

    AkelPad.MemFree(lpWindowText);
  }
}

function MoveWindow(hWndParent, hWnd, sAction)
{
  var oRectParent = {};
  var oRectWnd    = {};
  var lpRect;
  var nX;
  var nY;

  if (lpRect=AkelPad.MemAlloc(16)) //sizeof(RECT)
  {
    if (! hWndParent)
      hWndParent=oSys.Call("User32::GetDesktopWindow");

    oSys.Call("User32::GetWindowRect", hWndParent, lpRect);
    RectToObject(lpRect, oRectParent);

    oSys.Call("User32::GetWindowRect", hWnd, lpRect);
    RectToObject(lpRect, oRectWnd);

    nX = oRectWnd.left;
    nY = oRectWnd.top;

    if (sAction == "R") //Move right
      nX = oRectWnd.left + ((oRectWnd.right < oRectParent.right) ? 10: 0);
    else if (sAction == "L") //Move left
      nX = oRectWnd.left - ((oRectWnd.left > oRectParent.left) ? 10: 0);
    else if (sAction == "D") //Move down
      nY = oRectWnd.top + ((oRectWnd.bottom < oRectParent.bottom) ? 10: 0);
    else if (sAction == "U") //Move up
      nY = oRectWnd.top - ((oRectWnd.top > oRectParent.top) ? 10: 0);
    else if (sAction == "E") //Move end (right)
      nX = oRectWnd.left + (oRectParent.right - oRectWnd.right);
    else if (sAction == "H") //Move home (left)
      nX = oRectWnd.left + (oRectParent.left - oRectWnd.left);
    else if (sAction == "B") //Move bottom
      nY = oRectWnd.top + (oRectParent.bottom - oRectWnd.bottom);
    else if (sAction == "T") //Move top
      nY = oRectWnd.top + (oRectParent.top - oRectWnd.top);
    else if (sAction == "C") //Center window
    {
      nX = oRectParent.left + ((oRectParent.right  - oRectParent.left) / 2 - (oRectWnd.right  - oRectWnd.left) / 2);
      nY = oRectParent.top  + ((oRectParent.bottom - oRectParent.top)  / 2 - (oRectWnd.bottom - oRectWnd.top)  / 2);
    }
    else if (sAction == "LT") //Move left top
    {
      nX = oRectParent.left;
      nY = oRectParent.top;
    }
    else if (sAction == "RT") //Move right top
    {
      nX = oRectWnd.left + (oRectParent.right - oRectWnd.right);
      nY = oRectParent.top;
    }
    else if (sAction == "LB") //Move left bottom
    {
      nX = oRectParent.left;
      nY = oRectWnd.top + (oRectParent.bottom - oRectWnd.bottom);
    }
    else if (sAction == "RB") //Move right bottom
    {
      nX = oRectWnd.left + (oRectParent.right - oRectWnd.right);
      nY = oRectWnd.top + (oRectParent.bottom - oRectWnd.bottom);
    }

    oSys.Call("User32::SetWindowPos", hWnd, 0, nX, nY, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);
    AkelPad.MemFree(lpRect);
  }
}

function RectToObject(lpRect, oRect)
{
  oRect.left   = AkelPad.MemRead(lpRect,     DT_DWORD);
  oRect.top    = AkelPad.MemRead(lpRect + 4, DT_DWORD);
  oRect.right  = AkelPad.MemRead(lpRect + 8, DT_DWORD);
  oRect.bottom = AkelPad.MemRead(lpRect +12, DT_DWORD);
}

function SetRadiobutton(nButton)
{
  var i;

  if (bColSel)
  {
    for (i = IDCARPOS0; i <= IDCARPOS3; ++i)
      SendMessage(aWnd[i][WND], 241 /*BM_SETCHECK*/, 0 /*BST_UNCHECKED*/, 0);
    SendMessage(aWnd[IDCARPOS0 + nButton][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
  }
  else
  {
    for (i = IDCARPOSB; i <= IDCARPOSE; ++i)
      SendMessage(aWnd[i][WND], 241 /*BM_SETCHECK*/, 0 /*BST_UNCHECKED*/, 0);
    SendMessage(aWnd[IDCARPOSB + nButton][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
  }
}

function CheckButtons()
{
  var i;

  SendMessage(aWnd[IDOFFSET][WND],  241 /*BM_SETCHECK*/, bOffset, 0);
  SendMessage(aWnd[IDCOLSEL][WND],  241 /*BM_SETCHECK*/, bColSel, 0);

  for (i = IDCARPOSB; i <= IDCARPOS3; ++i)
    SendMessage(aWnd[i][WND], 241 /*BM_SETCHECK*/, 0 /*BST_UNCHECKED*/, 0);

  if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpCaret, lpEndSel) == -1)
  {
    SendMessage(aWnd[IDCARPOSB][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
    if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpBegSel, lpFirstC) == 0)
      SendMessage(aWnd[IDCARPOS0][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
    else
      SendMessage(aWnd[IDCARPOS1][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
  }
  else
  {
    SendMessage(aWnd[IDCARPOSE][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
    if (SendMessage(hEditWnd, AEM_INDEXCOMPARE, lpBegSel, lpFirstC) == 0)
      SendMessage(aWnd[IDCARPOS2][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
    else
      SendMessage(aWnd[IDCARPOS3][WND], 241 /*BM_SETCHECK*/, 1 /*BST_CHECKED*/, 0);
  }
}

function ShowHideWindows()
{
  var i;

  if (bOffset)
  {
    for (i = IDTXTLIN; i <= IDCARC; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 0 /*SW_HIDE*/);
    for (i = IDTXTOF; i <= IDCAROF; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 5 /*SW_SHOW*/);
  }
  else
  {
    for (i = IDTXTOF; i <= IDCAROF; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 0 /*SW_HIDE*/);
    for (i = IDTXTLIN; i <= IDCARC; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 5 /*SW_SHOW*/);
  }

  if (bColSel)
  {
    oSys.Call("User32::ShowWindow", aWnd[IDCARPOSB][WND], 0 /*SW_HIDE*/);
    oSys.Call("User32::ShowWindow", aWnd[IDCARPOSE][WND], 0 /*SW_HIDE*/);
    for (i = IDCARPOS0; i <= IDCARPOS3; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 5 /*SW_SHOW*/);
  }
  else
  {
    for (i = IDCARPOS0; i <= IDCARPOS3; ++i)
      oSys.Call("User32::ShowWindow", aWnd[i][WND], 0 /*SW_HIDE*/);
    oSys.Call("User32::ShowWindow", aWnd[IDCARPOSB][WND], 5 /*SW_SHOW*/);
    oSys.Call("User32::ShowWindow", aWnd[IDCARPOSE][WND], 5 /*SW_SHOW*/);
  }
}

function GetSelection()
{
  SendMessage(hEditWnd, AEM_GETINDEX, AEGI_FIRSTSELCHAR, lpFirstC);
  SendMessage(hEditWnd, AEM_GETSEL, lpCaret, lpSelect);

  nBegOf = SendMessage(hEditWnd, AEM_INDEXTORICHOFFSET, 0, lpBegSel);
  nEndOf = SendMessage(hEditWnd, AEM_INDEXTORICHOFFSET, 0, lpEndSel);
  nCarOf = SendMessage(hEditWnd, AEM_INDEXTORICHOFFSET, 0, lpEndSel);

  SetWindowFontAndText(aWnd[IDBEGL][WND], hGuiFont, String(AkelPad.MemRead(lpBegL, DT_DWORD) + 1));
  SetWindowFontAndText(aWnd[IDBEGC][WND], hGuiFont, String(AkelPad.MemRead(lpBegC, DT_DWORD) + 1));
  SetWindowFontAndText(aWnd[IDENDL][WND], hGuiFont, String(AkelPad.MemRead(lpEndL, DT_DWORD) + 1));
  SetWindowFontAndText(aWnd[IDENDC][WND], hGuiFont, String(AkelPad.MemRead(lpEndC, DT_DWORD) + 1));
  SetWindowFontAndText(aWnd[IDCARL][WND], hGuiFont, String(AkelPad.MemRead(lpCarL, DT_DWORD) + 1));
  SetWindowFontAndText(aWnd[IDCARC][WND], hGuiFont, String(AkelPad.MemRead(lpCarC, DT_DWORD) + 1));

  SetWindowFontAndText(aWnd[IDBEGOF][WND], hGuiFont, String(nBegOf));
  SetWindowFontAndText(aWnd[IDENDOF][WND], hGuiFont, String(nEndOf));
  SetWindowFontAndText(aWnd[IDCAROF][WND], hGuiFont, String(nCarOf));

  SendMessage(aWnd[bOffset ? IDBEGOF : IDBEGL][WND], 177 /*EM_SETSEL*/, 0, -1);
  oSys.Call("User32::SetFocus", aWnd[bOffset ? IDBEGOF : IDBEGL][WND]);
}

function SetSelection()
{
  var aCoord = new Array(4);
  var nBegL;
  var nBegC;
  var nEndL;
  var nEndC;
  var nOffset;
  var lpIndex;
  var i;

  if (bOffset)
  {
    lpIndex = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
    for (i = 0; i < 2; ++i)
    {
      oSys.Call("User32::GetWindowText" + _TCHAR, aWnd[i + IDBEGOF][WND], lpBuffer, 256);
      nOffset = parseInt(AkelPad.MemRead(lpBuffer, _TSTR));
      SendMessage(hEditWnd, AEM_RICHOFFSETTOINDEX, nOffset, lpIndex);
      aCoord[i*2]   = AkelPad.MemRead(lpIndex, DT_DWORD);
      aCoord[i*2+1] = AkelPad.MemRead(lpIndex + (_X64 ? 16 : 8), DT_DWORD);
    }
    AkelPad.MemFree(lpIndex);
  }
  else
  {
    for (i = 0; i < 4; ++i)
    {
      oSys.Call("User32::GetWindowText" + _TCHAR, aWnd[i + IDBEGL][WND], lpBuffer, 256);
      aCoord[i] = parseInt(AkelPad.MemRead(lpBuffer, _TSTR));
      if (aCoord[i] > 0) --aCoord[i];
    }
  }

  if ((aCoord[0] > aCoord[2]) || ((aCoord[0] == aCoord[2]) && (aCoord[1] > aCoord[3])))
  {
    nBegL = aCoord[2];
    nBegC = aCoord[3];
    nEndL = aCoord[0];
    nEndC = aCoord[1];
  }
  else
  {
    nBegL = aCoord[0];
    nBegC = aCoord[1];
    nEndL = aCoord[2];
    nEndC = aCoord[3];
  }

  if (bColSel)
  {
    if (SendMessage(aWnd[IDCARPOS0][WND], 240 /*BM_GETCHECK*/, 0, 0))
    {
      if (nBegC < nEndC)
        CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nBegL, nBegC);
      else
        CopyCoordinatesToIndex(nBegL, nEndC, nEndL, nBegC, nBegL, nEndC);
    }
    else if (SendMessage(aWnd[IDCARPOS1][WND], 240 /*BM_GETCHECK*/, 0, 0))
    {
      if (nBegC < nEndC)
        CopyCoordinatesToIndex(nBegL, nEndC, nEndL, nBegC, nBegL, nEndC);
      else
        CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nBegL, nBegC);
    }
    else if (SendMessage(aWnd[IDCARPOS2][WND], 240 /*BM_GETCHECK*/, 0, 0))
    {
      if (nBegC < nEndC)
        CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nEndL, nEndC);
      else
        CopyCoordinatesToIndex(nBegL, nEndC, nEndL, nBegC, nEndL, nBegC);
    }
    else
    {
      if (nBegC < nEndC)
        CopyCoordinatesToIndex(nBegL, nEndC, nEndL, nBegC, nEndL, nBegC);
      else
        CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nEndL, nEndC);
    }
  }
  else
  {
    if (SendMessage(aWnd[IDCARPOSB][WND], 240 /*BM_GETCHECK*/, 0, 0))
      CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nBegL, nBegC);
    else
      CopyCoordinatesToIndex(nBegL, nBegC, nEndL, nEndC, nEndL, nEndC);
  }

  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpBegSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpEndSel);
  SendMessage(hEditWnd, AEM_INDEXUPDATE, 0, lpCaret);

  AkelPad.MemCopy(lpSelFlag, bColSel, DT_DWORD);
  SendMessage(hEditWnd, AEM_SETSEL, lpCaret, lpSelect);

  oSys.Call("User32::InvalidateRect", hEditWnd, 0, true);
}

function CopyCoordinatesToIndex(n1, n2, n3, n4, n5, n6)
{
  AkelPad.MemCopy(lpBegL, n1, DT_DWORD);
  AkelPad.MemCopy(lpBegC, n2, DT_DWORD);
  AkelPad.MemCopy(lpEndL, n3, DT_DWORD);
  AkelPad.MemCopy(lpEndC, n4, DT_DWORD);
  AkelPad.MemCopy(lpCarL, n5, DT_DWORD);
  AkelPad.MemCopy(lpCarC, n6, DT_DWORD);
}


Last edited by KDJ on Mon Apr 14, 2014 8:16 pm; edited 8 times in total
Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Sat Nov 06, 2010 9:07 pm    Post subject: Reply with quote

Information about maximum line length.

LineMaxLength.js

Code:
// Maximum line length - 2010-11-06
//
// Call("Scripts::Main", 1, "LineMaxLength.js")

var hEditWnd = AkelPad.GetEditWnd();
var nMaxLenL = 0;
var nWordWrap;
var nLines;
var nBegLine;
var nLenLine;
var i;

if (! hEditWnd) WScript.Quit();

nWordWrap = AkelPad.SendMessage(hEditWnd, 3241 /*AEM_GETWORDWRAP*/, 0, 0);
if (nWordWrap > 0) AkelPad.Command(4209 /*IDM_VIEW_WORDWRAP*/);

nLines = AkelPad.SendMessage(hEditWnd, 3129 /*AEM_GETLINENUMBER*/, 0 /*AEGL_LINECOUNT*/, 0);

for (i = 0; i < nLines; ++i)
{
  nBegLine = AkelPad.SendMessage(hEditWnd, 187 /*EM_LINEINDEX*/, i, 0);
  nLenLine = AkelPad.SendMessage(hEditWnd, 193 /*EM_LINELENGTH*/, nBegLine, 0);
  if (nLenLine > nMaxLenL)
    nMaxLenL = nLenLine;
}

if (nWordWrap > 0) AkelPad.Command(4209 /*IDM_VIEW_WORDWRAP*/);

AkelPad.MessageBox(hEditWnd, "Maximum line length = " + nMaxLenL, "Line length", 64 /*MB_ICONINFORMATION*/);

Back to top
View user's profile Send private message
KDJ



Joined: 06 Mar 2010
Posts: 1361
Location: Poland

PostPosted: Mon Nov 08, 2010 10:08 pm    Post subject: Reply with quote

File info and text statistics.
Required to include: ChooseFont_function.js and FileAndStream_functions.js
All in one: KDJ.zip

FileInfo.js

Code:
// FileInfo.js - ver. 2013-09-14 (x86/x64)
//
// File info and text statistics.
//
// Required to include: ChooseFont_function.js and FileAndStream_functions.js
//
// Call("Scripts::Main", 1, "FileInfo.js")          - without arguments - shows dialog box
// Call("Scripts::Main", 1, "FileInfo.js", "0")     - general statistics all
// Call("Scripts::Main", 1, "FileInfo.js", "1")     - detailed statistics all
// Call("Scripts::Main", 1, "FileInfo.js", "1 CL")  - detailed statistics of chars and lines only
// Call("Scripts::Main", 1, "FileInfo.js", "0 W 2") - general statistics of words, outupt in log window
//
// First argument:
//   0 - general
//   1 - details
// Second argument (default is "CWL"):
//   C - chars
//   W - words
//   L - lines
// Third argument, output:
//  -1 - MDI mode - output in new tab, SDI - output in log window (default)
//   0 - output in new window
//   1 - output in new tab (MDI), SDI - output in new window
//   2 - output in log window

var DT_QWORD     = 2;
var DT_DWORD     = 3;
var DT_WORD      = 4;
var oSys         = AkelPad.SystemFunction();
var hMainWnd     = AkelPad.GetMainWnd();
var hEditWnd     = AkelPad.GetEditWnd();
var hInstanceDLL = AkelPad.GetInstanceDll();
var bChars       = 1;
var bWords       = 1;
var bLines       = 1;
var bCharsDet    = 0;
var bWordsDet    = 0;
var bLinesDet    = 0;
var nOutput      = -1;
var bFont        = 0;
var aFontOut;

if ((! hEditWnd) || (! AkelPad.Include("ChooseFont_function.js")) || (! AkelPad.Include("FileAndStream_functions.js")))
  WScript.Quit();

GetLangStrins();

if (WScript.Arguments.length)
{
  if (WScript.Arguments(0) == "1")
    {
      bCharsDet = 1;
      bWordsDet = 1;
      bLinesDet = 1;
    }
  if (WScript.Arguments.length > 1)
  {
    bChars = /C/i.test(WScript.Arguments(1));
    bWords = /W/i.test(WScript.Arguments(1));
    bLines = /L/i.test(WScript.Arguments(1));
  }
  if (WScript.Arguments.length > 2)
  {
    nOutput = parseInt(WScript.Arguments(2));
    if ((nOutput < 0) || (nOutput > 2))
      nOutput = -1;
  }
}
else
{
  var sClassName = "AkelPad::Scripts::" + WScript.ScriptName + "::" + hInstanceDLL;
  var bCancel    = 1;
  var hFocus;

  var CLASS = 0;
  var HWND  = 1;
  var STYLE = 2;
  var X     = 3;
  var Y     = 4;
  var W     = 5;
  var H     = 6;
  var TXT   = 7;

  var aWnd       = [];
  var IDSTATSG   = 2000;
  var IDALLG     = 2001;
  var IDCHARS    = 2002;
  var IDCHARSDET = 2003;
  var IDWORDS    = 2004;
  var IDWORDSDET = 2005;
  var IDLINES    = 2006;
  var IDLINESDET = 2007;
  var IDALL      = 2008;
  var IDALLDET   = 2009;
  var IDOUTPUTG  = 2010;
  var IDWND      = 2011;
  var IDTAB      = 2012;
  var IDLOG      = 2013;
  var IDFONT     = 2014;
  var IDFONTSET  = 2015;
  var IDOK       = 2016;
  var IDCANCEL   = 2017;

  ReadWriteIni(0);
  if (! aFontOut)
    aFontOut = ConvertFontFormat(AkelPad.SendMessage(hEditWnd, 0x0031 /*WM_GETFONT*/, 0, 0), 2, 3);

  //0x50000007 - WS_VISIBLE|WS_CHILD|BS_GROUPBOX
  //0x50000009 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_AUTORADIOBUTTON
  //0x50010000 - WS_VISIBLE|WS_CHILD|WS_TABSTOP
  //0x50010001 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_DEFPUSHBUTTON
  //0x50010003 - WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_AUTOCHECKBOX
  //Windows              CLASS,HWND,      STYLE,   X,   Y,   W,   H, TXT
  aWnd[IDSTATSG  ] = ["BUTTON",   0, 0x50000007,  10,  10, 180, 115, sTxtStats];
  aWnd[IDALLG    ] = ["BUTTON",   0, 0x50000007,  10,  85, 180,  40, ""];
  aWnd[IDCHARS   ] = ["BUTTON",   0, 0x50010003,  20,  30,  80,  16, sTxtChars];
  aWnd[IDCHARSDET] = ["BUTTON",   0, 0x50010003, 100,  30,  80,  16, sTxtDetails];
  aWnd[IDWORDS   ] = ["BUTTON",   0, 0x50010003,  20,  50,  80,  16, sTxtWords];
  aWnd[IDWORDSDET] = ["BUTTON",   0, 0x50010003, 100,  50,  80,  16, sTxtDetails];
  aWnd[IDLINES   ] = ["BUTTON",   0, 0x50010003,  20,  70,  80,  16, sTxtLines];
  aWnd[IDLINESDET] = ["BUTTON",   0, 0x50010003, 100,  70,  80,  16, sTxtDetails];
  aWnd[IDALL     ] = ["BUTTON",   0, 0x50010003,  20, 100,  80,  16, sTxtAll];
  aWnd[IDALLDET  ] = ["BUTTON",   0, 0x50010003, 100, 100,  80,  16, sTxtAllDet];
  aWnd[IDOUTPUTG ] = ["BUTTON",   0, 0x50000007,  10, 135, 180, 130, sTxtOutput];
  aWnd[IDWND     ] = ["BUTTON",   0, 0x50000009,  50, 155, 120,  16, sTxtNewWnd];
  aWnd[IDTAB     ] = ["BUTTON",   0, 0x50000009,  50, 175, 120,  16, sTxtNewTab];
  aWnd[IDLOG     ] = ["BUTTON",   0, 0x50000009,  50, 195, 120,  16, sTxtLogWnd];
  aWnd[IDFONT    ] = ["BUTTON",   0, 0x50010003,  20, 215, 120,  16, sTxtFont];
  aWnd[IDFONTSET ] = ["BUTTON",   0, 0x50010000,  20, 235, 160,  20, aFontOut.toString()];
  aWnd[IDOK      ] = ["BUTTON",   0, 0x50010001,  10, 275,  85,  23, sTxtOK];
  aWnd[IDCANCEL  ] = ["BUTTON",   0, 0x50010000, 105, 275,  85,  23, sTxtCancel];

  AkelPad.WindowRegisterClass(sClassName);

  oSys.Call("User32::CreateWindowEx" + _TCHAR,
            0,               //dwExStyle
            sClassName,      //lpClassName
            sTxtCaption,     //lpWindowName
            0x90C80000,      //dwStyle=WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU
            0,               //x
            0,               //y
            206,             //nWidth
            340,             //nHeight
            hMainWnd,        //hWndParent
            0,               //ID
            hInstanceDLL,    //hInstance
            DialogCallback); //Script function callback. To use it class must be registered by WindowRegisterClass.

  oSys.Call("User32::EnableWindow", hMainWnd, 0);

  AkelPad.WindowGetMessage();

  AkelPad.WindowUnregisterClass(sClassName);

  if (bCancel)
    WScript.Quit();
}

var hWndWait       = GetWaitWindow();
var sLineSep       = "----------------------------------------------";
var sFileName      = AkelPad.GetEditFile(0);
var aOutput        = [];
var aChars         = [];
var aWords         = [];
var aLines         = [];
var aLinesWhite    = [];
var aLinesWhiteEnd = [];
var aLinesBadNL    = [];
var aStreams       = [];
var bStream        = false;
var nMinLenL       = Infinity;
var nMaxLenL       = 0;
var nEmptyEntire   = 0;
var sStreamName;
var oFSO;
var oFile;
var nFileSize;
var dFileDate;
var sFileDateC;
var sFileDateM;
var sText;
var nChars;
var nLatins;
var nDigits;
var nUnders;
var nWhites;
var nSpaces;
var nHTabs;
var nVTabs;
var nFF;
var nCR;
var nLF;
var nOthers;
var aWordTDH;
var nWordTDH;
var nWordDH;
var nWordT;
var nWordD;
var nWordH;
var aTextLines;
var nFirstLine;
var nEmptyLines;
var nEmptyWhite;
var nWhiteEnd;
var reBadNL;
var sChar;
var nCode;
var sWord;
var aExec;
var i, n;

if (sFileName)
{
  i = sFileName.lastIndexOf(":");

  if (i > 3) //NTFS stream
  {
    bStream     = true;
    sStreamName = sFileName.substr(i + 1);
    sFileName   = sFileName.substr(0, i);
  }

  aStreams = EnumStreams(sFileName);

  if (bStream)
  {
    for (i = 1; i < aStreams.length; ++i)
    {
      if (aStreams[i][0] == sStreamName)
      {
        nFileSize  = aStreams[i][1];
        break;
      }
    }
    sFileName = sFileName + ":" + sStreamName;
  }
  else
  {
    oFSO       = new ActiveXObject("Scripting.FileSystemObject");
    oFile      = oFSO.GetFile(sFileName);
    nFileSize  = oFile.Size;
    dFileDate  = new Date(oFile.DateCreated);
    sFileDateC = DateToShortLocaleString(dFileDate);
    dFileDate  = new Date(oFile.DateLastModified);
    sFileDateM = DateToShortLocaleString(dFileDate);
  }
}
else
{
  sFileName  = sTxtNoFile;
  nFileSize  = "?";
  sFileDateC = "?";
  sFileDateM = "?";
}

aOutput.push(Pad(sTxtFile, 22) + sFileName);
aOutput.push(Pad(sTxtSize, 22) + nFileSize + (bStream ? " " + sTxtStream : ""));
if (! bStream)
{
  aOutput.push(Pad(sTxtCreated,  22) + sFileDateC);
  aOutput.push(Pad(sTxtModified, 22) + sFileDateM);

  for (i = 1; i < aStreams.length; ++i)
  {
    if (i == 1)
      aOutput.push(Pad(sTxtStreams, 22) + aStreams[i][0]);
    else
      aOutput.push(Pad(aStreams[i][0], aStreams[i][0].length + 22, "L"));
  }
}

if (bLines)
{
  if (AkelPad.GetSelStart() == AkelPad.GetSelEnd())
  {
    sText      = AkelPad.GetTextRange(0, -1, 1 /*\r*/);
    nFirstLine = 1;
  }
  else
  {
    sText      = AkelPad.GetSelText(1 /*\r*/);
    nFirstLine = AkelPad.SendMessage(hEditWnd, 3129 /*AEM_GETLINENUMBER*/, 1 /*AEGL_FIRSTSELLINE*/, 0);
    nFirstLine = AkelPad.SendMessage(hEditWnd, 3143 /*AEM_GETUNWRAPLINE*/, nFirstLine, 0) + 1;
  }
  aTextLines = sText.split("\r");
}

if (AkelPad.GetSelStart() == AkelPad.GetSelEnd())
{
  sText = AkelPad.GetTextRange(0, -1, 0);
  sTxtStatsTxt += sTxtEntire;
}
else
{
  sText = AkelPad.GetSelText(0);
  sTxtStatsTxt += sTxtSelect;
}

if (bChars || bWords || bLines)
{
  aOutput.push(sLineSep);
  aOutput.push(Pad(sTxtStatsTxt, 46, "C"));
  aOutput.push(sLineSep);
}

//chars
if (bChars)
{
  nChars  = sText.length;
  nLatins = Count(/[a-z]/gi);
  nDigits = Count(/\d/g);
  nUnders = Count(/_/g);
  nSpaces = Count(/ /g);
  nHTabs  = Count(/\t/g);
  nVTabs  = Count(/\v/g);
  nFF     = Count(/\f/g);
  nCR     = Count(/\r/g);
  nLF     = Count(/\n/g);
  nWhites = nSpaces + nHTabs + nVTabs + nFF + nCR + nLF;
  nOthers = nChars - nLatins - nDigits - nUnders - nWhites;

  aOutput.push(         Pad(sTxtChars + ":",  32) + nChars);
  aOutput.push("  "   + Pad(sTxtLatin,  32) + nLatins);
  aOutput.push("  "   + Pad(sTxtDigits, 32) + nDigits);
  aOutput.push("  "   + Pad(sTxtUnders, 32) + nUnders);
  aOutput.push("  "   + Pad(sTxtWhites, 32) + nWhites);
  aOutput.push("    " + Pad(sTxtSpaces, 32) + nSpaces);
  aOutput.push("    " + Pad(sTxtHTabs,  32) + nHTabs);
  aOutput.push("    " + Pad(sTxtVTabs,  32) + nVTabs);
  aOutput.push("    " + Pad(sTxtFF,     32) + nFF);
  aOutput.push("    " + Pad(sTxtCR,     32) + nCR);
  aOutput.push("    " + Pad(sTxtLF,     32) + nLF);
  aOutput.push("  "   + Pad(sTxtOther,  32) + nOthers);
  aOutput.push(sLineSep);
}

//words
if (bWords)
{
  aWordTDH = sText.match(/[^\s'`"\\\|\[\]\(\)\{\}<>,\.;:\+\-=~!@#\$%^&\*/\?]+/g);
  nWordTDH = aWordTDH ? aWordTDH.length : 0;
  nWordD   = Count(/(^|[\s'`"\\\|\[\]\(\)\{\}<>,\.;:\+\-=~!@#\$%^&\*/\?])\d+(?=($|[\s'`"\\\|\[\]\(\)\{\}<>,\.;:\+\-=~!@#\$%^&\*/\?]))/g);
  nWordH   = Count(/(^|[\s'`"\\\|\[\]\(\)\{\}<>,\.;:\+\-=~!@#\$%^&\*/\?])0x[\da-f]+(?=($|[\s'`"\\\|\[\]\(\)\{\}<>,\.;:\+\-=~!@#\$%^&\*/\?]))/gi);
  nWordT   = nWordTDH - nWordD - nWordH;
  nWordDH  = nWordD   + nWordH;

  aOutput.push(         Pad(sTxtWords + ":", 32) + nWordTDH);
  aOutput.push("  "   + Pad(sTxtText,  32) + nWordT);
  aOutput.push("  "   + Pad(sTxtInt,   32) + nWordDH);
  aOutput.push("    " + Pad(sTxtDec,   32) + nWordD);
  aOutput.push("    " + Pad(sTxtHex,   32) + nWordH);
  aOutput.push(sLineSep);
}

//lines
if (bLines)
{
  for (i = 0; i < aTextLines.length; ++i)
  {
    if (aTextLines[i].length < nMinLenL)
      nMinLenL = aTextLines[i].length;
    if (aTextLines[i].length > nMaxLenL)
      nMaxLenL = aTextLines[i].length;
    if (aTextLines[i].length == 0)
      ++nEmptyEntire;
  }

  nEmptyWhite = Count(/^[ \t\v\f]+$/gm);
  nWhiteEnd   = Count(/\S+[ \t\v\f]+$/gm);
  nEmptyLines = nEmptyEntire + nEmptyWhite;

  if (AkelPad.GetEditNewLine(hEditWnd) == 1) //Win
  {
    reBadNL = /\r\r\n|\r\n|\r|\n/g;
    while (aExec = reBadNL.exec(sText))
    {
      if (aExec[0] != "\r\n")
        aLinesBadNL.push(RegExp.index);
    }
  }
  else
  {
    if (AkelPad.GetEditNewLine(hEditWnd) == 2) //Unix
      reBadNL = /\r{1,2}\n|\r/g;
    else //Mac
      reBadNL = /\r{0,2}\n/g;
    while (reBadNL.test(sText))
      aLinesBadNL.push(RegExp.index);
  }

  aOutput.push(         Pad(sTxtLines + ":",    32) + aTextLines.length);
  aOutput.push("  "   + Pad(sTxtEmptyL,   32) + nEmptyLines);
  aOutput.push("    " + Pad(sTxtEnEmptyL, 32) + nEmptyEntire);
  aOutput.push("    " + Pad(sTxtWhiteL,   32) + nEmptyWhite);
  aOutput.push("  "   + Pad(sTxtWhiteEnd, 32) + nWhiteEnd);
  aOutput.push("  "   + Pad(sTxtBadNLF,   32) + aLinesBadNL.length);
  aOutput.push(sLineSep);
  aOutput.push(         Pad(sTxtMinLenL,  32) + nMinLenL);
  aOutput.push(         Pad(sTxtMaxLenL,  32) + nMaxLenL);
  aOutput.push(sLineSep);
}

//chars details
if (bChars && bCharsDet)
{
  for (i = 0; i < sText.length; ++i)
  {
    nCode = sText.charCodeAt(i);
    if (nCode > 31)
      sChar = sText.charAt(i);
    else
      sChar = " ";
    if (aChars[nCode])
      ++aChars[nCode][0];
    else
      aChars[nCode] = [1, sChar];
  }
  if (aChars.length)
  {
    aOutput.push(sTxtDetails + " " + sTxtChars + ":");
    aOutput.push(Pad(sTxtCount, 9, "L") + Pad(sTxtChar, 9, "L") + Pad(sTxtCode, 10, "L") + Pad("(hex)", 11, "L"));
    aOutput.push(sLineSep);
    for (i = 0; i < aChars.length; ++i)
    {
      if (aChars[i])
        aOutput.push(Pad(aChars[i][0].toString(), 9, "L") + "      " + aChars[i][1] + Pad("(" + i + ")", 12, "L") + "     (" + Pad(i.toString(16).toUpperCase(), 4, "L", "0") + ")");
    }
    aOutput.push(sLineSep);
  }
}

//words details
if (bWords && bWordsDet)
{
  if (nWordTDH)
  {
    aWordTDH.sort();
    i = 0;
    while (i < nWordTDH)
    {
      sWord = aWordTDH[i];
      n     = 1;
      ++i;
      while ((i < nWordTDH) && (sWord == aWordTDH[i]))
      {
        ++n;
        ++i;
      }
      aWords.push([n, sWord]);
    }
  }
  if (aWords.length)
  {
    aOutput.push(sTxtDetails + " " + sTxtWords + ":");
    aOutput.push(Pad(sTxtCount, 9, "L") + "      " + sTxtWord);
    aOutput.push(sLineSep);
    for (i = 0; i < aWords.length; ++i)
      aOutput.push(Pad(aWords[i][0].toString(), 9, "L") + "      " + aWords[i][1]);

    aOutput.push(sLineSep);
  }
}

//lines details
if (bLines && bLinesDet)
{
  for (i = 0; i < aTextLines.length; ++i)
  {
    if (/^\s+$/.test(aTextLines[i]))
      aLinesWhite.push(i + nFirstLine);
    else if (/\S+\s+$/.test(aTextLines[i]))
      aLinesWhiteEnd.push(i + nFirstLine);

    if (aLines[aTextLines[i].length])
    {
      ++aLines[aTextLines[i].length][0];
      aLines[aTextLines[i].length][1].push(i + nFirstLine);
    }
    else
      aLines[aTextLines[i].length] = [1, [i + nFirstLine]];
  }

  aOutput.push(sTxtDetails + " " + sTxtLines + ":");
  aOutput.push(Pad(sTxtCount, 9, "L") + Pad(sTxtLineLen, 21, "C") + sTxtLineNum);
  aOutput.push(sLineSep);
  for (i = 0; i < aLines.length; ++i)
  {
    if (aLines[i])
      aOutput.push(Pad(aLines[i][0].toString(), 9, "L") + Pad(i.toString(), 11, "L") + "          [" + aLines[i][1] + "]");
  }

  if (aLinesWhite.length)
    aOutput.push(Pad(sTxtWhiteL, 30) + "[" + aLinesWhite + "]");
  if (aLinesWhiteEnd.length)
    aOutput.push(Pad(sTxtWhiteEnd, 30) + "[" + aLinesWhiteEnd + "]");
  if (aLinesBadNL.length)
  {
    OffsetToLineNumber(aLinesBadNL);
    aOutput.push(Pad(sTxtBadNLF, 30) + "[" + aLinesBadNL + "]");
  }
  aOutput.push(sLineSep);
}

oSys.Call("User32::DestroyWindow", hWndWait);
AkelPad.SendMessage(GetOutputWindow(), 0x000C /*WM_SETTEXT*/, 0, aOutput.join("\r"));

function DialogCallback(hWnd, uMsg, wParam, lParam)
{
  if (uMsg == 1) //WM_CREATE
  {
    var hGuiFont = oSys.Call("Gdi32::GetStockObject", 17 /*DEFAULT_GUI_FONT*/);
    var i;

    for (i = 2000; i < aWnd.length; ++i)
    {
      aWnd[i][HWND] =
        oSys.Call("User32::CreateWindowEx" + _TCHAR,
                  0,                //dwExStyle
                  aWnd[i][CLASS],   //lpClassName
                  0,                //lpWindowName
                  aWnd[i][STYLE],   //dwStyle
                  aWnd[i][X],       //x
                  aWnd[i][Y],       //y
                  aWnd[i][W],       //nWidth
                  aWnd[i][H],       //nHeight
                  hWnd,             //hWndParent
                  i,                //ID
                  hInstanceDLL,     //hInstance
                  0);               //lpParam

      AkelPad.SendMessage(aWnd[i][HWND], 48 /*WM_SETFONT*/, hGuiFont, 0);
      oSys.Call("User32::SetWindowText" + _TCHAR, aWnd[i][HWND], aWnd[i][TXT]);
    }

    CheckButtons();
    CheckRadioButtons();
    CenterWindow(hWnd);
    hFocus = aWnd[IDCHARS][HWND];
  }

  else if ((uMsg == 6 /*WM_ACTIVATE*/) && (wParam == 0 /*WA_INACTIVE*/))
    hFocus = oSys.Call("user32::GetFocus");

  else if (uMsg == 7) //WM_SETFOCUS
    oSys.Call("User32::SetFocus", hFocus);

  else if (uMsg == 256) //WM_KEYDOWN
  {
    if ((wParam == 13 /*VK_RETURN*/) && (oSys.Call("User32::GetFocus") != aWnd[IDFONTSET][HWND]) && (oSys.Call("User32::GetFocus") != aWnd[IDCANCEL][HWND]))
      oSys.Call("User32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDOK, 0);
    else if (wParam == 27 /*VK_ESCAPE*/)
      oSys.Call("User32::PostMessage" + _TCHAR, hWnd, 16 /*WM_CLOSE*/, 0, 0);
  }

  else if (uMsg == 273) //WM_COMMAND
  {
    var nLowParam = wParam & 0xFFFF;

    if (((nLowParam >= IDCHARS) && (nLowParam <= IDALLDET)) || (nLowParam == IDFONT))
    {
      if (nLowParam == IDCHARS)
        bChars = ! bChars;
      else if (nLowParam == IDWORDS)
        bWords = ! bWords;
      else if (nLowParam == IDLINES)
        bLines = ! bLines;
      else if (nLowParam == IDCHARSDET)
        bCharsDet = ! bCharsDet;
      else if (nLowParam == IDWORDSDET)
        bWordsDet = ! bWordsDet;
      else if (nLowParam == IDLINESDET)
        bLinesDet = ! bLinesDet;
      else if (nLowParam == IDALL)
        bChars = bWords = bLines = AkelPad.SendMessage(lParam, 0x00F0 /*BM_GETCHECK*/, 0, 0);
      else if (nLowParam == IDALLDET)
        bCharsDet = bWordsDet = bLinesDet = AkelPad.SendMessage(lParam, 0x00F0 /*BM_GETCHECK*/, 0, 0);
      else if (nLowParam == IDFONT)
        bFont = ! bFont;

      CheckButtons();
    }
    else if ((nLowParam >= IDWND) && (nLowParam <= IDLOG))
      nOutput = nLowParam - IDWND;
    else if (nLowParam == IDFONTSET)
      MenuFont(hWnd);
    else if (nLowParam == IDOK)
    {
      bCancel = 0;
      oSys.Call("User32::PostMessage" + _TCHAR, hWnd, 16 /*WM_CLOSE*/, 0, 0);
    }
    else if (nLowParam == IDCANCEL)
      oSys.Call("User32::PostMessage" + _TCHAR, hWnd, 16 /*WM_CLOSE*/, 0, 0);

  }

  else if (uMsg == 16) //WM_CLOSE
  {
    ReadWriteIni(1);
    oSys.Call("User32::EnableWindow", hMainWnd, 1);
    oSys.Call("User32::DestroyWindow", hWnd);
  }

  else if (uMsg == 2) //WM_DESTROY
    oSys.Call("User32::PostQuitMessage", 0);

  return 0;
}

function CheckButtons()
{
  AkelPad.SendMessage(aWnd[IDCHARS][HWND], 0x00F1 /*BM_SETCHECK*/, bChars, 0);
  AkelPad.SendMessage(aWnd[IDWORDS][HWND], 0x00F1 /*BM_SETCHECK*/, bWords, 0);
  AkelPad.SendMessage(aWnd[IDLINES][HWND], 0x00F1 /*BM_SETCHECK*/, bLines, 0);

  AkelPad.SendMessage(aWnd[IDCHARSDET][HWND], 0x00F1 /*BM_SETCHECK*/, bCharsDet, 0);
  AkelPad.SendMessage(aWnd[IDWORDSDET][HWND], 0x00F1 /*BM_SETCHECK*/, bWordsDet, 0);
  AkelPad.SendMessage(aWnd[IDLINESDET][HWND], 0x00F1 /*BM_SETCHECK*/, bLinesDet, 0);

  AkelPad.SendMessage(aWnd[IDALL   ][HWND], 0x00F1 /*BM_SETCHECK*/, bChars && bWords && bLines, 0);
  AkelPad.SendMessage(aWnd[IDALLDET][HWND], 0x00F1 /*BM_SETCHECK*/, bCharsDet && bWordsDet && bLinesDet, 0);

  AkelPad.SendMessage(aWnd[IDFONT][HWND], 0x00F1 /*BM_SETCHECK*/, bFont, 0);

  oSys.Call("User32::EnableWindow", aWnd[IDCHARSDET][HWND], bChars);
  oSys.Call("User32::EnableWindow", aWnd[IDWORDSDET][HWND], bWords);
  oSys.Call("User32::EnableWindow", aWnd[IDLINESDET][HWND], bLines);
  oSys.Call("User32::EnableWindow", aWnd[IDFONTSET ][HWND], bFont);
}

function CheckRadioButtons()
{
  if (nOutput < 0)
  {
    if (AkelPad.IsMDI())
      nOutput = 1;
    else
      nOutput = 0;
  }

  if ((nOutput == 2) && (! IsLogPluginExists()))
    nOutput = 1;

  if ((nOutput == 1) && (! AkelPad.IsMDI()))
    nOutput = 0;

  AkelPad.SendMessage(aWnd[IDWND + nOutput][HWND], 0x00F1 /*BM_SETCHECK*/, 1, 0);

  oSys.Call("User32::EnableWindow", aWnd[IDTAB][HWND], AkelPad.IsMDI());
  oSys.Call("User32::EnableWindow", aWnd[IDLOG][HWND], IsLogPluginExists());
}

function IsLogPluginExists()
{
  return IsFileExists(AkelPad.GetAkelDir(4 /*ADTYPE_PLUGS*/) + "\\Log.dll");
}

function MenuFont(hWndOwn)
{
  var hMenu = oSys.Call("User32::CreatePopupMenu");
  var oRect = {};
  var nCmd;
  var vCF;

  GetWindowPos(aWnd[IDFONT][HWND], oRect);

  oSys.Call("User32::AppendMenu" + _TCHAR, hMenu, 0 /*MF_STRING*/, 1, sTxtMonoFonts);
  oSys.Call("User32::AppendMenu" + _TCHAR, hMenu, 0 /*MF_STRING*/, 2, sTxtAllFonts);

  nCmd = oSys.Call("User32::TrackPopupMenu", hMenu, 0x0180 /*TPM_RETURNCMD|TPM_NONOTIFY*/, oRect.X + 16, oRect.Y + 16, 0, hWndOwn, 0);
  oSys.Call("User32::DestroyMenu", hMenu);

  if (nCmd)
  {
    if (vCF = ChooseFont(hWndOwn, 3, aFontOut, 0, nCmd - 2, 3))
    {
      aFontOut = vCF;
      oSys.Call("User32::SetWindowText" + _TCHAR, aWnd[IDFONTSET][HWND], aFontOut.toString());
    }
  }
}

function GetWindowPos(hWnd, oRect)
{
  var lpRect = AkelPad.MemAlloc(16); //sizeof(RECT)

  oSys.Call("User32::GetWindowRect", hWnd, lpRect);

  oRect.X = AkelPad.MemRead(lpRect,      DT_DWORD);
  oRect.Y = AkelPad.MemRead(lpRect +  4, DT_DWORD);
  oRect.W = AkelPad.MemRead(lpRect +  8, DT_DWORD) - oRect.X;
  oRect.H = AkelPad.MemRead(lpRect + 12, DT_DWORD) - oRect.Y;

  AkelPad.MemFree(lpRect);
}

function GetWaitWindow()
{
  WScript.Sleep(20); //without it, doesn't show Wait window. I don't know why
  var hWnd =
        oSys.Call("User32::CreateWindowEx" + _TCHAR,
                  0,          //dwExStyle
                  "STATIC",   //lpClassName
                  0,          //lpWindowName
                  0x50400001, //dwStyle = WS_VISIBLE|WS_CHILD|WS_DLGFRAME|SS_CENTER
                  0,          //x
                  0,          //y
                  200,        //nWidth
                  90,         //nHeight
                  hMainWnd,   //hWndParent
                  0,          //ID
                  hInstanceDLL,
                  0);

  CenterWindow(hWnd);
  WScript.Sleep(20); //without it, doesn't show Wait window. I don't know why
  oSys.Call("User32::SetWindowText" + _TCHAR, hWnd, "\n\n" + sTxtWait);

  return hWnd;
}

function CenterWindow(hWnd)
{
  var oRectEdit = {};
  var oRect     = {};

  GetWindowPos(hEditWnd, oRectEdit);
  GetWindowPos(hWnd, oRect);

  oRect.X = oRectEdit.X + (oRectEdit.W - oRect.W) / 2;
  oRect.Y = oRectEdit.Y + (oRectEdit.H - oRect.H) / 2;

  oSys.Call("User32::SetWindowPos", hWnd, 0, oRect.X, oRect.Y, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);
}

function Count(oRE)
{
  var aMatch = sText.match(oRE);
  return aMatch ? aMatch.length : 0;
}

function Pad(sString, nLen, sType, sChar)
{
  var i = 0;

  if (! sType) sType = "R";
  if (! sChar) sChar = " ";

  if (sType == "R")
  {
    while (sString.length < nLen)
      sString += sChar;
  }
  else if (sType == "L")
  {
    while (sString.length < nLen)
      sString = sChar + sString;
  }
  else if (sType == "C")
  {
    while (sString.length < nLen)
    {
      if ((i % 2) == 0)
        sString += sChar;
      else
        sString = sChar + sString;
      ++ i;
    }
  }
  return sString;
}

function DateToShortLocaleString(dDate)
{
  var lpSysTime = AkelPad.MemAlloc(16 /*sizeof(SYSTEMTIME)*/);
  var lpString  = AkelPad.MemAlloc(256 * _TSIZE);
  var sDateTime;

  AkelPad.MemCopy(lpSysTime,      dDate.getFullYear(),     DT_WORD);
  AkelPad.MemCopy(lpSysTime +  2, dDate.getMonth() + 1,    DT_WORD);
  AkelPad.MemCopy(lpSysTime +  4, dDate.getDay(),          DT_WORD);
  AkelPad.MemCopy(lpSysTime +  6, dDate.getDate(),         DT_WORD);
  AkelPad.MemCopy(lpSysTime +  8, dDate.getHours(),        DT_WORD);
  AkelPad.MemCopy(lpSysTime + 10, dDate.getMinutes(),      DT_WORD);
  AkelPad.MemCopy(lpSysTime + 12, dDate.getSeconds(),      DT_WORD);
  AkelPad.MemCopy(lpSysTime + 14, dDate.getMilliseconds(), DT_WORD);

  oSys.Call("Kernel32::GetDateFormat" + _TCHAR,
            0x400, //LOCALE_USER_DEFAULT
            0x1,   //DATE_SHORTDATE
            lpSysTime,
            0,
            lpString,
            256);
  sDateTime = AkelPad.MemRead(lpString, _TSTR) + " ";

  oSys.Call("Kernel32::GetTimeFormat" + _TCHAR,
            0x400, //LOCALE_USER_DEFAULT
            0x8,   //TIME_FORCE24HOURFORMAT
            lpSysTime,
            0,
            lpString,
            256);
  sDateTime += AkelPad.MemRead(lpString, _TSTR);

  AkelPad.MemFree(lpSysTime);
  AkelPad.MemFree(lpString);

  return sDateTime;
}

function OffsetToLineNumber(aLinesBadNL)
{
  var lpCharIndex1  = AkelPad.MemAlloc(_X64 ? 24 : 12); //sizeof(AECHARINDEX)
  var lpCharIndex2  = AkelPad.MemAlloc(_X64 ? 24 : 12); //sizeof(AECHARINDEX)
  var lpIndexOffset = AkelPad.MemAlloc(_X64 ? 32 : 16); //sizeof(AEINDEXOFFSET)
  var i;

  AkelPad.MemCopy(lpIndexOffset, lpCharIndex1, DT_QWORD);
  AkelPad.MemCopy(lpIndexOffset + (_X64 ? 8 : 4), lpCharIndex2, DT_QWORD);
  AkelPad.MemCopy(lpIndexOffset + (_X64 ? 24 : 12), 3 /*AELB_ASIS*/, DT_DWORD);

  AkelPad.SendMessage(hEditWnd, 3130 /*AEM_GETINDEX*/, (AkelPad.GetSelStart() == AkelPad.GetSelEnd()) ? 1 /*AEGI_FIRSTCHAR*/ : 3 /*AEGI_FIRSTSELCHAR*/, lpCharIndex1);

  for (i = 0; i < aLinesBadNL.length; ++i)
  {
    AkelPad.MemCopy(lpIndexOffset + (_X64 ? 16 : 8), aLinesBadNL[i], DT_QWORD);
    AkelPad.SendMessage(hEditWnd, 3135 /*AEM_INDEXOFFSET*/, 0, lpIndexOffset);

    aLinesBadNL[i] = AkelPad.SendMessage(hEditWnd, 3136 /*AEM_INDEXTORICHOFFSET*/, 0, lpCharIndex2);
     aLinesBadNL[i] = AkelPad.SendMessage(hEditWnd, 1078 /*EM_EXLINEFROMCHAR*/, 0, aLinesBadNL[i]);
    aLinesBadNL[i] = AkelPad.SendMessage(hEditWnd, 3143 /*AEM_GETUNWRAPLINE*/, aLinesBadNL[i], 0) + 1;
  }

  AkelPad.MemFree(lpCharIndex1);
  AkelPad.MemFree(lpCharIndex2);
  AkelPad.MemFree(lpIndexOffset);
}

function GetOutputWindow()
{
  var hWnd;

  if (AkelPad.IsMDI())
  {
    if (nOutput == 2)
      hWnd = GetLogWindow();
    else
      hWnd = GetNewEditWindow(nOutput);
  }
  else
  {
    if ((nOutput == -1) || (nOutput == 2))
      hWnd = GetLogWindow();
    else
      hWnd = GetNewEditWindow(0);
  }

  return hWnd;
}

function GetNewEditWindow(bNewTab)
{
  var aFontEdit;

  if (bNewTab)
  {
    //To eliminate conflict with Templates plugin: lParam=1
    AkelPad.SendMessage(hMainWnd, 273 /*WM_COMMAND*/, 4101 /*wParam=MAKEWPARAM(0,IDM_FILE_NEW)*/, 1 /*lParam=TRUE*/);

    if (bFont)
    {
      WScript.Sleep(10);
      AkelPad.Font(aFontOut[0], aFontOut[1], aFontOut[2])
    }
  }
  else
  {
    if (bFont)
    {
      aFontEdit = ConvertFontFormat(AkelPad.SendMessage(hEditWnd, 49 /*WM_GETFONT*/, 0, 0), 2, 3);

      AkelPad.SendMessage(hEditWnd, 11 /*WM_SETREDRAW*/, 0, 0);
      AkelPad.Font(aFontOut[0], aFontOut[1], aFontOut[2])
    }

    //Force create new instance
    if (AkelPad.SendMessage(hMainWnd, 1222 /*AKD_GETMAININFO*/, 153 /*MI_SINGLEOPENPROGRAM*/, 0))
    {
      AkelPad.Command(4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/);
      hMainWnd = AkelPad.Command(4102 /*IDM_FILE_CREATENEW*/);
      AkelPad.Command(4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/);
      AkelPad.SendMessage(hMainWnd, 273 /*WM_COMMAND*/, 4256 /*IDM_OPTIONS_SINGLEOPEN_PROGRAM*/, 0);
    }
    else
      hMainWnd = AkelPad.Command(4102 /*IDM_FILE_CREATENEW*/);

    if (bFont)
    {
      AkelPad.Font(aFontEdit[0], aFontEdit[1], aFontEdit[2])
      AkelPad.SendMessage(hEditWnd, 11 /*WM_SETREDRAW*/, 1, 0);
    }
  }

  return AkelPad.SendMessage(hMainWnd, 1223 /*AKD_GETFRAMEINFO*/, 2 /*FI_WNDEDIT*/, 0);
}

function GetLogWindow()
{
  var lpWnd = AkelPad.MemAlloc(_X64 ? 8 : 4); //sizeof(HWND)
  var hWnd;

  if (! AkelPad.IsPluginRunning("Log::Output"))
    AkelPad.Call("Log::Output");

  AkelPad.Call("Log::Output", 2, lpWnd);
  hWnd = AkelPad.MemRead(lpWnd, DT_QWORD);
  AkelPad.MemFree(lpWnd);

  if (bFont)
    AkelPad.SendMessage(hWnd, 48 /*WM_SETFONT*/, ConvertFontFormat(aFontOut, 3, 2), 0);

  return hWnd;
}

function ReadWriteIni(bWrite)
{
  var sIniFile = WScript.ScriptFullName.substring(0, WScript.ScriptFullName.lastIndexOf(".")) + ".ini";
  var sIniTxt;
  var oError;

  if (bWrite)
  {
    sIniTxt = 'bChars='     + bChars    + ';\r\n' +
              'bWords='     + bWords    + ';\r\n' +
              'bLines='     + bLines    + ';\r\n' +
              'bCharsDet='  + bCharsDet + ';\r\n' +
              'bWordsDet='  + bWordsDet + ';\r\n' +
              'bLinesDet='  + bLinesDet + ';\r\n' +
              'nOutput='    + nOutput   + ';\r\n' +
              'bFont='      + bFont     + ';\r\n' +
              'aFontOut=["' + aFontOut[0] + '",' + aFontOut[1] + ',' + aFontOut[2] + '];';

    WriteFile(sIniFile, null, sIniTxt, 1);
  }

  else if (IsFileExists(sIniFile))
  {
    try
    {
      eval(AkelPad.ReadFile(sIniFile));
    }
    catch (oError)
    {
    }
  }
}

function GetLangStrins()
{
  if (AkelPad.GetLangId(0 /*LANGID_FULL*/) == 0x0415) //Polish
  {
    sTxtCaption   = "Informacja o pliku";
    sTxtWait      = "Proszę czekać...";
    sTxtNoFile    = "Brak nazwy, plik nie został zapisany";
    sTxtStatsTxt  = "Statystyka tekstu";
    sTxtEntire    = " - cały tekst";
    sTxtSelect    = " - obszar zaznaczenia";
    sTxtDetails   = "Szczegóły";
    sTxtCount     = "Licznik";
    sTxtWord      = "Słowo";
    sTxtChar      = "Znak";
    sTxtCode      = "(Kod)";
    sTxtLineLen   = "Długość wiersza";
    sTxtLineNum   = "[Numery wierszy]";
    sTxtFile      = "Plik, pełna nazwa:";
    sTxtSize      = "Rozmiar [Bajty]:";
    sTxtCreated   = "Utworzony:";
    sTxtModified  = "Ostatnio zapisany:";
    sTxtChars     = "Znaki";
    sTxtLatin     = "Litery łacińskie:";
    sTxtDigits    = "Cyfry:";
    sTxtUnders    = "Znaki podkreślenia:";
    sTxtWhites    = "Białe znaki:";
    sTxtSpaces    = "Spacje:";
    sTxtHTabs     = "Tabulatory (09h):";
    sTxtVTabs     = "Tabulatory pionowe (0Bh):";
    sTxtFF        = "Wysuw strony FF (0Ch):";
    sTxtCR        = "Powrót karetki CR (0Dh):";
    sTxtLF        = "Nowy wiersz LF (0Ah):";
    sTxtOther     = "Inne:";
    sTxtWords     = "Słowa";
    sTxtText      = "Teksty:";
    sTxtInt       = "Liczby całkowite:";
    sTxtDec       = "Dziesiętne:";
    sTxtHex       = "Hex (0x...):";
    sTxtLines     = "Wiersze";
    sTxtEmptyL    = "Puste wiersze:";
    sTxtEnEmptyL  = "Całkowicie puste:";
    sTxtWhiteL    = "Tylko z białymi znakami:";
    sTxtWhiteEnd  = "Zakończone białymi znakami:";
    sTxtBadNLF    = "Błędny format nowego wiersza:";
    sTxtMinLenL   = "Min. długość wiersza:";
    sTxtMaxLenL   = "Max. długość wiersza:";
    sTxtStream    = "(strumień NTFS)";
    sTxtStreams   = "Strumienie NTFS:";
    sTxtStats     = "Statystyki";
    sTxtAll       = "&Wszystko";
    sTxtAllDet    = "&Szczegółowo";
    sTxtOutput    = "Wyjście";
    sTxtNewWnd    = "Nowe okno";
    sTxtNewTab    = "Nowa karta (MDI)";
    sTxtLogWnd    = "Okno log";
    sTxtFont      = "Czcionka";
    sTxtMonoFonts = "Czcionki monospace";
    sTxtAllFonts  = "Wszystkie czcionki";
    sTxtOK        = "OK";
    sTxtCancel    = "Anuluj";
  }
  else
  {
    sTxtCaption   = "File info";
    sTxtWait      = "Please wait...";
    sTxtNoFile    = "No name, the file is not saved";
    sTxtStatsTxt  = "Text statistics";
    sTxtEntire    = " - entire text";
    sTxtSelect    = " - selected text";
    sTxtDetails   = "Details";
    sTxtCount     = "Count";
    sTxtWord      = "Word";
    sTxtChar      = "Char";
    sTxtCode      = "(Code)";
    sTxtLineLen   = "Line length";
    sTxtLineNum   = "[Line numbers]";
    sTxtFile      = "File, full name:";
    sTxtSize      = "Size [Bytes]:";
    sTxtCreated   = "Created time:";
    sTxtModified  = "Last modified:";
    sTxtChars     = "Chars";
    sTxtLatin     = "Latin letters:";
    sTxtDigits    = "Digits:";
    sTxtUnders    = "Underscore chars:";
    sTxtWhites    = "Whitespaces:";
    sTxtSpaces    = "Spaces:";
    sTxtHTabs     = "Tabs (09h):";
    sTxtVTabs     = "Vertical Tabs (0Bh):";
    sTxtFF        = "Form Feed (0Ch):";
    sTxtCR        = "Carriage Return (0Dh):";
    sTxtLF        = "Line Feed (0Ah):";
    sTxtOther     = "Other:";
    sTxtWords     = "Words";
    sTxtText      = "Text:";
    sTxtInt       = "Integers:";
    sTxtDec       = "Decimal:";
    sTxtHex       = "Hex (0x...):";
    sTxtLines     = "Lines";
    sTxtEmptyL    = "Empty lines:";
    sTxtEnEmptyL  = "Entirely empty:";
    sTxtWhiteL    = "Only with whitespaces:";
    sTxtWhiteEnd  = "Terminated by whitespaces:";
    sTxtBadNLF    = "Bad NewLine format:";
    sTxtMinLenL   = "Min line length:";
    sTxtMaxLenL   = "Max line length:";
    sTxtStream    = "(NTFS stream)";
    sTxtStreams   = "NTFS streams:";
    sTxtStats     = "Statistics";
    sTxtAll       = "&All";
    sTxtAllDet    = "With &details";
    sTxtOutput    = "Output";
    sTxtNewWnd    = "New window";
    sTxtNewTab    = "New tab (MDI)";
    sTxtLogWnd    = "Log window";
    sTxtFont      = "Font";
    sTxtMonoFonts = "Monospace fonts";
    sTxtAllFonts  = "All fonts";
    sTxtOK        = "OK";
    sTxtCancel    = "Cancel";
  }
}


Last edited by KDJ on Sat Sep 14, 2013 12:46 pm; edited 18 times in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins All times are GMT
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 17, 18, 19  Next
Page 7 of 19

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


SourceForge.net Logo Powered by phpBB © 2001, 2005 phpBB Group