'http://masm32.com/board/index.php?topic=3079.0
'http://www.jose.it-berater.org/richedit/iframe/index.htm
'http://forums.codeguru.com/showthread.php?446242-rich-edit-control-line-numbering-number
'https://groups.google.com/forum/#!topic/borland.public.cppbuilder.vcl.components.using/4cEJOqvXs0U

'include \masm32\include\masm32rt.inc
uses dialogs

'macro DlgRichEdit(dstyle,tx,ty,wd,ht,ctlID)
'    align_4 edi
'    mov DWORD PTR [edi+0],  WS_VISIBLE or WS_CHILD or dstyle
'    mov WORD  PTR [edi+8],  tx
'    mov WORD  PTR [edi+10], ty
'    mov WORD  PTR [edi+12], wd
'    mov WORD  PTR [edi+14], ht
'    mov WORD  PTR [edi+16], ctlID
'    add edi, 18
'    ustring "RichEdit20A"
'    align_2 edi
'    add edi, 2
'end macro

% IDC_EDIT 100
% IDC_RICHEDIT	101
% MARGIN_X  30

'.data?
    sys g_hInstance     'HINSTANCE ?
    sys g_pOrgWndProc   'WNDPROC ?
'.code
'WndProc  proc uses esi edi ebx hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
'
function WndProc(sys hWnd,UMsg,wParam,lParam) as int callback
=============================================================
POINT  pt
CHAR sz[16]
DWORD lc
RECT crect
sys rgn
int dret
sys hDC
int line
int charidx

    dret=CallWindowProc(g_pOrgWndProc,hWnd,uMsg,wParam,lParam)

    if uMsg == WM_PAINT
        lc=SendMessage(hWnd,EM_GETLINECOUNT,0,0)
        if lc
            hDC = GetDC(hWnd)
            SaveDC(hDC)
            GetClientRect(hWnd,&crect)
            rgn = CreateRectRgn(crect.left,crect.top,crect.right,crect.bottom)
            SelectClipRgn(hDC,rgn)

             ;fnx br = SelectObject,ebx,rv(CreateSolidBrush,bkColor)
            % PATCOPY 0x00F00021
            BitBlt(hDC,0,0,MARGIN_X,crect.bottom, hDC,0,0,PATCOPY)
           RestoreDC(hDC,-1)           
           
            line=SendMessage(hWnd,EM_GETFIRSTVISIBLELINE,0,0)
            while line <= lc
                charidx = SendMessage(hWnd,EM_LINEINDEX,line,0)
                exit if charidx == -1
                SendMessage(hWnd,EM_POSFROMCHAR,&pt,charidx)
                exit if pt.y > crect.bottom
                'wide chars
                'set line number
                wsprintf(&sz,"%lu",line+1)
                SetTextColor(hDC,0xff0000) 'blue
                TextOut(hDC,4,pt.y,sz,len(sz))
                'set text hihlight
                'SetTextColor(hDC,0x0000ff) 'red
                'TextOut(hDC,pt.x,pt.y,sz,len(sz))
                line++
            wend
            'RestoreDC(hDC,-1)
            DeleteObject(rgn)
            ReleaseDC(hWnd,hDC)
        endif      
    endif
    return dret
end function 'wndproc


'DlgProc proc uses esi edi ebx hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
function DlgProc(sys hWnd,uMsg,wParam,lParam) as int callback
=============================================================
Rect crect

    if uMsg == WM_INITDIALOG
         % EC_LEFTMARGIN 1
         SendDlgItemMessage(hWnd,IDC_EDIT,EM_SETMARGINS,EC_LEFTMARGIN,MARGIN_X)
        g_pOrgWndProc = SetWindowLong(GetDlgItem(hWnd,IDC_EDIT),GWL_WNDPROC,@WndProc)
        return 0
    elseif uMsg == WM_CLOSE
        return EndDialog(hWnd,0)
    else
        return 0
    endif
end function 'DlgProc endp

'main proc
   
    g_hInstance = GetModuleHandle(0)
    LoadLibrary("Riched20.dll")
   init_common_controls
   
   sys hDlg

   Dialog( 0, 0, 200, 100, "RichEdit modal dialog with line numbers",
                WS_OVERLAPPEDWINDOW or DS_CENTER )
   char cr=chr(13)+chr(10)
   char txt[0x100]="RichEdit"+cr+"box"
   RichEdit( txt, IDC_EDIT, 0, 0, 200, 100, ES_AUTOHSCROLL or WS_HSCROLL )
   hDlg = CreateModalDialog( null, @DlgProc, 0 )
   
    'Dialog "test","MS Sans Serif",12,WS_OVERLAPPED or WS_SYSMENU or DS_CENTER,1,10,10,100,100,1024)
    'DlgRichEdit(ES_MULTILINE or ES_WANTRETURN or ES_AUTOHSCROLL or ES_AUTOVSCROLL ,0,0,90,50,IDC_EDIT)
    'CallModalDialog(g_hInstance,0,@DlgProc,0)

   
'exit
'main endp
'end main