uglobal
  hLister dd ?
  hSplitterLister dd ?
endg


proc SetListerText, .hText
begin
        pushad
        invoke  SendMessageW, [hLister], LM_SHOW, TRUE, 0

        stdcall SetControlTextUtf8, [hLister], [.hText]
        popad
        return
endp


proc InitListerWindow, .hParent
begin
        xor     ebx, ebx
        invoke   CreateWindowExW, WS_EX_CLIENTEDGE, cAsmEditClassName, NULL,          \
                 WS_CHILD or WS_HSCROLL or WS_VSCROLL or WS_CLIPSIBLINGS or  \
                 ES_NOHIDESEL or AES_AUTOINDENT or AES_SMARTTABS or AES_CONSOLECARET ,      \
                 ebx, ebx , ebx, ebx,                                 \
                 [.hParent],                                           \
                 ebx, [hInstance],                                    \
                 NULL

        mov     [hLister], eax
        mov     ebx, eax

        stdcall ApplySettingsToAsmEdit, ebx, [CurrentSettings], fasm_syntax
        invoke  SendMessageW, ebx, AEM_SETBOOKMARKICON, [hBookmarkIcon], 0
        invoke  SendMessageW, ebx, AEM_SETPOPUPMENU, 0, [hEditorMenu]

        stdcall SubclassWindow, ebx, ListerWinProc
        stdcall AttachToCell, ebx, [.hParent], MainLayout.cellLister

        return
endp




proc ListerWinProc, .hwnd, .wmsg, .wparam, .lparam
.minaddr dd ?
.maxaddr dd ?
.line    dd ?
.beg     dd ?
.end     dd ?
begin
        cmp     [.wmsg], LM_SHOWLIST
        je      .lmshowlist

        cmp     [.wmsg], LM_SHOW
        je      .lmshow

        stdcall SourceEditProc, [.hwnd], [.wmsg], [.wparam], [.lparam]
        return


;-------- LM_SHOW ----------------------
.lmshow:
        stdcall ActionSetCheck, actViewLister, [.wparam]
        invoke  ShowWindow, [.hwnd], [.wparam]
        stdcall SetCellHidden, [hMainForm], MainLayout.cellLister, [.wparam]
        clc
        return

;-------- LM_SHOWLIST ------------------

.lmshowlist:
        mov     esi, [.wparam]
        mov     edi, [ptrDebugInfo]
        test    edi, edi
        jnz     .doprocess
        clc
        return

.doprocess:
        lea     eax, [edi+TArray.array]
        mov     ecx, [edi+TArray.count]

        dec     ecx
        mov     [.minaddr], eax
        imul    ecx, [edi+TArray.itemsize]
        add     eax, ecx
        mov     [.maxaddr], eax

        mov     [.line], 1

        mov     ecx, [esi+TDebugLine.ptrFile]
        mov     edx, [esi+TDebugLine.LineNum]

        stdcall StrNew
        mov     ebx, eax

.searchbegin:
        cmp     esi, [.minaddr]
        je      .begfound

        cmp     [esi+TDebugLine.ptrFile-sizeof.TDebugLine], ecx   ; check prev
        jne     .begfound

        cmp     [esi+TDebugLine.LineNum-sizeof.TDebugLine], edx
        jne     .begfound

        sub     esi, sizeof.TDebugLine
        inc     [.line]
        jmp     .searchbegin

.begfound:
        mov     [.beg], esi
        mov     esi, [.wparam]

.searchend:
        cmp     esi, [.maxaddr]
        je      .endfound
        cmp     dword [esi+TDebugLine.ptrFile+sizeof.TDebugLine], ecx   ; check next
        jne     .endfound
        cmp     [esi+TDebugLine.LineNum+sizeof.TDebugLine], edx
        jne     .endfound

        add     esi, sizeof.TDebugLine
        jmp     .searchend

.endfound:
        mov     [.end], esi
        mov     esi, [.beg]

.getloop:
        mov     eax, [esi+TDebugLine.strLine]
        add     eax, [ptrListing]

        stdcall StrCat, ebx, eax
        stdcall StrCharCat, ebx, $0d
        add     esi, sizeof.TDebugLine
        cmp     esi, [.end]
        jbe     .getloop

        stdcall SetControlTextUtf8, [hLister], ebx
        stdcall StrDel, ebx

        invoke  SendMessageW, [hLister], AEM_SETFOCUSLINE, [.line], 0

        clc
        return
endp