;<ff
Window frmHelpFiles, 3, 0, 'TForm', 'Helps and manuals', $46000000, $10000, 0, 307, 194, 320, 240, HelpFilesOptionsProc;
Window NONE, 0, 5, 'SysListView32', 'ListView', $50000209, $200, 200, 0, 36, 312, 177, 0;
Window NONE, 3, 3, 'TForm', 'Form', $50800000, $10000, 0, 0, 0, 312, 36, 0;
Window NONE, 0, 0, 'STATIC', '&Help files:', $50000200, $0, 0, 8, 12, 64, 19, 0;
Window NONE, 0, 0, 'BUTTON', '&Add', $50018000, $0, 100, 88, 8, 56, 19, 0;
Window NONE, 0, 0, 'BUTTON', '&Remove', $50018000, $0, 101, 152, 8, 56, 19, 0;
Window NONE, 0, 0, 'BUTTON', '&Down', $50018000, $0, 103, 264, 8, 40, 19, 0;
Window NONE, 2, 0, 'BUTTON', '&Up', $50018000, $0, 102, 216, 8, 40, 19, 0;
;ff>
; this is options dialog for the help files creation.

iglobal
  cHelpTitle  db 'Select help file:',0
  cHelpFilter db 'Help files (*.hlp; *.chm; *.html; *.htm; *.idx; *.md)',0,'*.hlp;*.chm;*.html;*.htm;*.idx;*.md',0
              db 0
endg

;-----------------------------------------------------------------------
; Window procedure for the form.
; Arguments:
;   .hwnd - handle of the window
;   .wmsg - code of the message
;   .wparam \ Parameters of the message
;   .lparam /
;-----------------------------------------------------------------------
winproc HelpFilesOptionsProc
  .str rb 1024

  .idButtonAdd  = 100
  .idButtonDel  = 101
  .idButtonUp   = 102
  .idButtonDn   = 103
  .idListView   = 200

  .lvitem LVITEM

begin

ondefault
        stc
        return

onmessage WM_COMMAND
        mov     eax, [.wparam]
        cmp     eax, .idButtonAdd
        je      .addfiles

        cmp     eax, .idButtonDel
        je      .delfile

        cmp     eax, .idButtonUp
        je      .moveup

        cmp     eax, .idButtonDn
        je      .movedn

        stc
        return

.delfile:
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETNEXTITEM, -1, LVNI_ALL or LVNI_SELECTED
        cmp     eax, -1
        je      .enddel

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_DELETEITEM, eax, 0
        jmp     .delfile

.enddel:
        clc
        return


.addfiles:
        stdcall OpenFilesDialog, cHelpTitle, TRUE, cHelpFilter, _OnAddHelpCallback, 0, [.hwnd]
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE_USEHEADER
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE_USEHEADER
        clc
        return

.moveup:
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
        cmp     eax, -1
        je      .endmove

        test    eax, eax
        jz      .endmove

        lea     ebx, [.lvitem]
        mov     [ebx+LVITEM.iItem], eax
        mov     [ebx+LVITEM.mask], LVIF_TEXT or LVIF_STATE
        mov     [ebx+LVITEM.state], LVIS_FOCUSED or LVIS_SELECTED
        mov     [ebx+LVITEM.stateMask], LVIS_FOCUSED or LVIS_SELECTED
        mov     [ebx+LVITEM.iSubItem], 0
        lea     eax, [.str]
        mov     [ebx+LVITEM.pszText], eax
        mov     [ebx+LVITEM.cchTextMax], 1024
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEM, 0, ebx
        sub     [ebx+LVITEM.iItem], 1
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_INSERTITEM, 0, ebx

        add     [ebx+LVITEM.iItem], 2
        mov     [ebx+LVITEM.iSubItem], 1
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEM, 0, ebx
        sub     [ebx+LVITEM.iItem], 2
        mov     [ebx+LVITEM.mask], LVIF_TEXT
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETITEM, 0, ebx

        add     [ebx+LVITEM.iItem], 2
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_DELETEITEM, [ebx+LVITEM.iItem], 0
        jmp      .endmove


.movedn:
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
        cmp     eax, -1
        je      .endmove

        lea     ebx, [.lvitem]
        mov     [ebx+LVITEM.iItem], eax

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEMCOUNT, 0, 0
        dec     eax
        cmp     eax, [ebx+LVITEM.iItem]
        jbe     .endmove

        mov     [ebx+LVITEM.mask], LVIF_TEXT or LVIF_STATE
        mov     [ebx+LVITEM.state], LVIS_FOCUSED or LVIS_SELECTED
        mov     [ebx+LVITEM.stateMask], LVIS_FOCUSED or LVIS_SELECTED
        mov     [ebx+LVITEM.iSubItem], 0
        lea     eax, [.str]
        mov     [ebx+LVITEM.pszText], eax
        mov     [ebx+LVITEM.cchTextMax], 1024
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEM, 0, ebx
        add     [ebx+LVITEM.iItem], 2
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_INSERTITEM, 0, ebx

        sub     [ebx+LVITEM.iItem], 2
        mov     [ebx+LVITEM.iSubItem], 1
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEM, 0, ebx
        add     [ebx+LVITEM.iItem], 2
        mov     [ebx+LVITEM.mask], LVIF_TEXT
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETITEM, 0, ebx
        sub     [ebx+LVITEM.iItem], 2
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_DELETEITEM, [ebx+LVITEM.iItem], 0

.endmove:
        clc
        return

;----------------------------------------------------------------------

onmessage FM_AFTERCREATE
locals
  .lvc LVCOLUMN
endl

cTitle1 text 'Help file'
cTitle2 text 'Full path'

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_GRIDLINES or LVS_EX_FULLROWSELECT

        lea     ebx, [.lvc]

        mov     [.lvc.mask], LVCF_FMT or LVCF_TEXT or LVCF_SUBITEM
        mov     [.lvc.fmt], LVCFMT_RIGHT
        mov     [.lvc.pszText], cTitle1
        mov     [.lvc.iSubItem], 0
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_INSERTCOLUMN, 0, ebx

        mov     [.lvc.mask], LVCF_FMT or LVCF_TEXT or LVCF_SUBITEM
        mov     [.lvc.fmt], LVCFMT_LEFT
        mov     [.lvc.pszText], cTitle2
        mov     [.lvc.iSubItem], 1

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_INSERTCOLUMN, 1, ebx

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE_USEHEADER
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE_USEHEADER

        clc
        return

onmessage OM_LOADOPTIONS
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_DELETEALLITEMS, 0, 0
        stdcall ReadSectionFromIni, [hIniFileName], cHelpSection, _OnAddHelpCallback, [.hwnd]
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE_USEHEADER
        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE_USEHEADER
        clc
        return


onmessage OM_SAVEOPTIONS
locals
  .keys   rb 4
endl
        mov     dword [.keys], 'aaa='

        stdcall GetMem, 65536   ; should be enough
        mov     edi, eax
        push    eax

        mov     [.lvitem.mask], LVIF_TEXT
        mov     [.lvitem.iSubItem], 1
        mov     [.lvitem.cchTextMax], 1024

        invoke  SendDlgItemMessageA, [.hwnd], .idListView, LVM_GETITEMCOUNT, 0, 0
        mov     esi, eax
        xor     ebx, ebx

.createloop:
        cmp     ebx, esi
        je      .endcreate

        call    .insertkey

        mov     [.lvitem.iItem], ebx
        mov     [.lvitem.pszText], edi
        lea     eax, [.lvitem]
        invoke  SendDlgItemMessageA, [.hwnd], HelpFilesOptionsProc.idListView, LVM_GETITEM, 0, eax
        stdcall StrLen, edi
        lea     edi, [edi+eax+1]
        inc     ebx
        jmp     .createloop

.endcreate:
        xor     eax, eax
        stosd

        pop     edi

        stdcall StrPtr, [hIniFileName]
        invoke  WritePrivateProfileSectionA, cHelpSection, edi, eax

        stdcall FreeMem, edi
        clc
        return

.insertkey:
        push    dword [.keys]
        pop     dword [edi]
        add     edi,4
        inc     byte [.keys+2]
        cmp     [.keys+2], 'z'
        jbe     @f
        mov     [.keys+2], 'a'
        inc     [.keys+1]
        cmp     [.keys+1], 'z'
        jbe     @f
        mov     [.keys+1], 'a'
        inc     [.keys]
        cmp     [.keys], 'z'
        jbe     @f
        mov     [.keys], 'a'
@@:
        retn
endwp


proc _OnAddHelpCallback, .hFileName, .hApplet
.lvitem LVITEM
begin
        push    ebx
        stdcall ExtractFileName, [.hFileName]
        stdcall StrExtract, [.hFileName], eax, -1
        push    eax
        stdcall StrPtr, eax

        lea     ebx, [.lvitem]
        mov     [ebx+LVITEM.mask], LVIF_TEXT
        mov     [ebx+LVITEM.iItem], 10000
        mov     [ebx+LVITEM.iSubItem], 0
        mov     [ebx+LVITEM.pszText], eax
        invoke  SendDlgItemMessageA, [.hApplet], HelpFilesOptionsProc.idListView, LVM_INSERTITEM, 0, ebx
        stdcall StrDel ; from the stack.

        mov     [ebx+LVITEM.iItem], eax
        stdcall StrPtr, [.hFileName]
        mov     [ebx+LVITEM.pszText], eax
        mov     [ebx+LVITEM.iSubItem], 1
        invoke  SendDlgItemMessageA, [.hApplet], HelpFilesOptionsProc.idListView, LVM_SETITEM, 0, ebx

        clc
        pop     ebx
        return
endp

