.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

include REdit.inc

.CODE
start:

    mov hInstance, $invoke (GetModuleHandle, NULL) 
    mov CommandLine, $invoke (GetCommandLine) 
    call Main
    invoke ExitProcess,eax

; #########################################################################
	
Main proc

    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG

    mov wc.cbSize,sizeof WNDCLASSEX
    mov wc.style, CS_HREDRAW or CS_VREDRAW
    mov wc.lpfnWndProc, offset WndProc
    mov wc.cbClsExtra,NULL
    mov wc.cbWndExtra,NULL
    m2m wc.hInstance,      hInstance
    mov wc.hbrBackground,COLOR_WINDOW+1
    mov wc.lpszMenuName,NULL
    mov wc.lpszClassName,offset szClassName
    mov wc.hIcon, $invoke (LoadIcon,NULL,IDI_APPLICATION)
    mov wc.hIconSm, NULL
    mov wc.hCursor, $invoke (LoadCursor,NULL,IDC_ARROW)
	
    invoke RegisterClassEx, addr wc
	
    mov hMainMenu, $invoke (LoadMenu, hInstance, MAINMENU)
 
    invoke CreateWindowEx,NULL,
                          ADDR szClassName,
                          ADDR szDisplayName,
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,CW_USEDEFAULT,600,500,
                          NULL,hMainMenu,
                          hInstance,NULL
	mov hWnd,eax
	
    invoke ShowWindow,hWnd, SW_SHOWMAXIMIZED
    invoke UpdateWindow,hWnd
    
    invoke LoadLibrary, ADDR szRichLib
    invoke CreateWindowEx,WS_EX_CLIENTEDGE,ADDR szEditClass,NULL,
                          WS_MAXIMIZE or WS_CHILDWINDOW or ES_MULTILINE or \
		      ES_SAVESEL or WS_VSCROLL or WS_HSCROLL or ES_AUTOHSCROLL or \
		      ES_AUTOVSCROLL or ES_NOHIDESEL,
		      0, 0, CW_USEDEFAULT, CW_USEDEFAULT,hWnd,NULL,hInstance,NULL	
    mov	hWndEdit, eax	    
    invoke SetFocus, hWndEdit
    invoke ShowWindow, hWndEdit, SW_SHOWMAXIMIZED	
	
   .WHILE	TRUE
    invoke GetMessage, ADDR msg, NULL, 0, 0
   .BREAK	.IF (!eax)
    invoke TranslateMessage, ADDR msg
    invoke DispatchMessage, ADDR msg
   .ENDW

    mov eax, msg.wParam
    ret
	
Main endp

; #########################################################################

WndProc PROC hWin:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

LOCAL rct :RECT
LOCAL hDC :HDC

 .if uMsg==WM_CREATE
	
 .elseif uMsg==WM_COMMAND

   mov eax,wParam
		
    .if ax==IDM_OPEN

      ; --------------------------------------
      ; szFileName is defined in Filedlgs.asm
      ; --------------------------------------
        mov szFileName[0],0     ; set 1st byte to zero
        invoke GetFileName,hWin,SADD("Open A File"), offset szFilter
        cmp szFileName[0],0     ; zero if cancel pressed in dlgbox
        je @F

        invoke CreateFile, ADDR szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
         		OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL

        mov hFile, eax
        mov editstream.dwCookie,eax
        mov editstream.pfnCallback,offset StreamInProc
        invoke SendMessage,hWndEdit,EM_STREAMIN, SF_RTF,addr editstream
        invoke SendMessage,hWndEdit,EM_SETMODIFY,FALSE,0
        invoke CloseHandle,hFile
@@:
		
    .elseif ax == IDM_PAGE_SETUP
    
        invoke CreatePageSetup, hWin    
		
    .elseif ax==IDM_PRINT_PREVIEW
    
        invoke CreatePreView, hWin, hWndEdit

    .elseif ax==IDM_PRINT
		
        invoke PrintREdit, hWin, hWndEdit
		
    .elseif ax==IDM_ABOUT 		; Î ÏÐÎÃÐÀÌÌÅ				

        invoke CreateXXAboutDlg, hWin

    .elseif ax==IDM_EXIT  		; ÂÛÕÎÄ
				
        invoke SendMessage,hWin,WM_SYSCOMMAND,SC_CLOSE,NULL
						
    .endif

  .elseif uMsg == WM_SIZE

    .if hWndEdit != NULL
        mov eax,lParam
        mov ecx,eax
        and eax,0FFFFh
        shr ecx,16
        invoke MoveWindow, hWndEdit, 0, 0, eax, ecx, TRUE
    .endif
  
  .elseif uMsg == WM_ERASEBKGND
  
    .if hWndEdit != NULL
        return 1
    .endif
    
 .elseif uMsg == WM_SETFOCUS
    .if hWndEdit != NULL
        invoke SetFocus, hWndEdit
    .endif
		
 .elseif uMsg == WM_CLOSE

 .elseif uMsg == WM_DESTROY
    
   invoke PostQuitMessage,NULL
   return 0
    
 .endif	
	
   invoke DefWindowProc,hWin,uMsg,wParam,lParam
   ret
WndProc ENDP

; #########################################################################

StreamInProc PROC HFile:DWORD,pBuffer:DWORD, NumBytes:DWORD, pBytesRead:DWORD

  invoke ReadFile,HFile,pBuffer,NumBytes,pBytesRead,0
  xor eax,1
  ret
StreamInProc ENDP

; ########################################################################

end start