;This function was written by vkim
;vkim@aport2000.ru
;-----------------------------------------------------------------------------
;This one has been optimized by Ewayne
;-----------------------------------------------------------------------------

         title   Debug

         .586
         .model flat, stdcall
         option casemap:none   ; Case sensitive

            include  Windows.inc
            include  user32.inc
            include  kernel32.inc

         includelib  user32.lib
         includelib  kernel32.lib

.const
DB_NUM        equ  1 
DB_TEXT       equ  2 
IDM_WORK      equ  6222 ;6071

.data
ClassName      db  'DebugWin',0
ClassNameWA    db  'WorkArea',0
szWinCaption   db  'Debug Window for AsmEdit',0
szCurDir       db  'DebugWin.exe',0
;szCommandLine  db  '\MASM32V1\PROGRAMS\AsmEdit\bin\DebugWin.exe',0
szCommandLine  db  '\AsmEdit\bin\DebugWin.exe',0
szNum          db  20 dup(0)
szCRLF         db  0dh,0ah,0 
szEdit         db  'RichEdit20A',0
szEdit44       db  'RichEdit44',0

szError        db  'You need to change the CommandLine Path in Debug.asm',0dh,0ah
               db  'and reassemble it.',0dh,0ah
               db  'Or copy the DebugWin.exe to this applications directory.',0
NoGo           dd  0

.data?
CommandLine    dd  ?
Done           dd  ?

.code

DebugPrint proc DebugData:DWORD, fType:DWORD
LOCAL    hwnd:DWORD, hParent, hWork, hActive, dwLen, sw
LOCAL    szBuff[256]:BYTE, RootPath[256]:BYTE

      INVOKE     GetCurrentDirectory, sizeof RootPath, addr RootPath
         and     sw, 0
      INVOKE     FindWindow, addr ClassName, NULL 
      .if !eax
         INVOKE     FindWindow, addr ClassNameWA, NULL
      .endif
      .if eax                  ; AsmEdit's Work Area window
            inc     sw
            mov     hWork, eax
           push     eax
         INVOKE     GetForegroundWindow
            mov     hActive, eax
         INVOKE     GetParent, hWork
            mov     hParent, eax
            pop     eax
      .endif
      .if !eax
         INVOKE     WinExec, addr szCommandLine, SW_SHOWNORMAL
         INVOKE     FindWindow, NULL, addr szWinCaption 
      .endif
      .if !eax
         INVOKE     WinExec, addr szCurDir, SW_SHOWNORMAL
         INVOKE     FindWindow, NULL, addr szWinCaption 
      .endif
      .if !eax
         .if !NoGo
            INVOKE     MessageBox, NULL, addr szError, addr szWinCaption, MB_OK
               mov     NoGo, 1
         .endif
            ret
      .endif

      .if eax
            mov     hwnd, eax
         INVOKE     FindWindowEx, hwnd, NULL, addr szEdit, NULL
         .if !eax
            INVOKE     FindWindowEx, hwnd, NULL, addr szEdit44, NULL
         .endif
            mov     hwnd, eax
         INVOKE     SendMessage, hwnd, WM_GETTEXTLENGTH, 0, 0
            mov     dwLen, eax
         .if fType == DB_TEXT
            INVOKE     SendMessage, hwnd, EM_SETSEL, -1, -1
            .if dwLen
               INVOKE     SendMessage, hwnd, EM_REPLACESEL, FALSE, addr szCRLF
            .endif
            INVOKE     SendMessage, hwnd, EM_REPLACESEL, FALSE, DebugData
            INVOKE     SendMessage, hwnd, EM_SCROLLCARET, 0, 0
         .elseif fType == DB_NUM
            INVOKE     SendMessage, hwnd, EM_SETSEL, -1, -1
            .if dwLen
               INVOKE     SendMessage, hwnd, EM_REPLACESEL, FALSE, addr szCRLF
            .endif
            INVOKE     SendMessage, hwnd, EM_REPLACESEL, FALSE, DebugData
            INVOKE     SendMessage, hwnd, EM_SCROLLCARET, 0, 0
         .endif

         .if sw                ; AsmEdit's Work Area window
            INVOKE     IsWindowVisible, hWork
            .if !eax           ; Show the Work Area window & keep the application on top
               INVOKE     SendMessage, hParent, WM_COMMAND, IDM_WORK, 4
               INVOKE     SetWindowPos, hActive, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE
               INVOKE     SetFocus, hActive
            .endif
         .endif

      .endif
      INVOKE     SetCurrentDirectory, addr RootPath

         ret
DebugPrint endp

end


end start
