
uglobal
  hEditorsHost    dd 0                        ; handle of editors host window
  hEditorsHostTooltips dd 0
  hStatus         dd 0                        ; handle of the Statusbar of the editor host.
                                              ; This statusbar is for common use from all
                                              ; hosted windows.

  ptrMRUFiles     dd 0

  hTabMenu        dd 0                        ; popup menu for the tab control.
  hTabIml         dd 0
endg

iglobal
  cEmptyEditorTitle du 'Fresh editor:', 0, 0

  cStatusBarParts dd    64, 128, 192, 208, 264, 320, -1

  CoolMenu TabPopup,                      \
      mfNormal, actFileClose,             \
      mfSeparator, NONE,                  \
      mfNormal, actNew,                   \
      mfNormal, actOpen,                  \
      mfNormal, actSave,                  \
      mfSeparator, NONE,                  \
      mfNormal, actSetCurrentMain,        \
      mfNormal, actAddToProject

  SplitStart EditorLayout
      Cell cellEditor
  SplitEnd

endg





proc TooltipMessages, .hwnd, .wmsg, .wparam, .lparam
begin
        cmp     [.wmsg], TTM_RELAYEVENT
        jne     .ondefault

        mov     eax, [.lparam]

        mov     [eax+MSG.message], WM_MOUSEMOVE

        OutputValue "Tooltip message: ", [eax+MSG.message], 16, 8
        OutputValue "Tooltip wparam: ", [eax+ MSG.wParam], 16, 8
        OutputValue "Tooltip lparam: ", [eax+MSG.lParam], 16, 8


.ondefault:
        stc
        return
endp




;----------------------------------------
; Creates editorhost window.
; Call this only once.
;----------------------------------------
proc InitEditorHost, .hParent
.ti TOOLINFO
.str rw 1024
begin

        stdcall CreateCoolMenu, TabPopup, TRUE
        mov     [hTabMenu], eax

        xor     ebx, ebx

        invoke  CreateWindowExW, WS_EX_CONTROLPARENT or WS_EX_ACCEPTFILES, cTabClassName, NULL,                                                 \
                WS_VISIBLE or WS_CHILD or WS_CLIPSIBLINGS or WS_CLIPCHILDREN or TCS_TABS or TCS_FOCUSNEVER or TCS_TOOLTIPS or WS_TABSTOP,       \
                0, 100, 640, 480,                                                                                                               \
                [.hParent], 0, [hInstance], ebx

        mov     [hEditorsHost], eax

;        OutputValue "Editors host control: ", eax, 16, 8

        stdcall SubclassWindow, [hEditorsHost], EditorHostProc

        stdcall SetSplitGrid, [hEditorsHost], EditorLayout
        stdcall AttachToCell, [hEditorsHost], [.hParent], MainLayout.cellEditorHost


        invoke  SendMessageW, [hEditorsHost], TCM_GETTOOLTIPS, 0, 0
        mov     esi, eax
        mov     [hEditorsHostTooltips], eax

;        OutputValue "Editors host tooltips control: ", esi, 16, 8

; Here, manually adding the tab control to the tooltip, as a workaround for WINE.

        mov     [.ti.cbSize], sizeof.TOOLINFO
        mov     [.ti.uFlags], TTF_SUBCLASS or TTF_IDISHWND
        mov     eax, [hEditorsHost]
        mov     [.ti.uId], eax
        mov     eax, [.hParent]
        mov     [.ti.hwnd], eax
        mov     [.ti.lpszText], LPSTR_TEXTCALLBACK

        lea     eax, [.ti]
        invoke  SendMessageW, esi, TTM_ADDTOOLW, 0, eax

;        OutputValue "TTM_ADDTOOLW returned ", eax, 10, -1

; Set some additional parameters for the tooltip control. It is needed for Windows too for faster
; display of the tooltips.

        invoke  GetWindowLongW, esi, GWL_STYLE
        or      eax, TTS_ALWAYSTIP or TTS_NOPREFIX
        invoke  SetWindowLongW, esi, GWL_STYLE, eax
        invoke  SendMessageW, esi, TTM_SETDELAYTIME, TTDT_AUTOMATIC, 600

; Create status bar

        invoke  CreateWindowExW, 0, cStatusBarClassName, NULL,                                                          \
                WS_CHILD or WS_VISIBLE or CCS_NODIVIDER or CCS_NORESIZE or CCS_NOPARENTALIGN or WS_CLIPSIBLINGS,        \
                ebx,ebx, ebx, ebx,                                                                                      \
                [.hParent], NULL, [hInstance], NULL
        mov     [hStatus], eax

        stdcall SubclassWindow, eax, StatusWinProc

        stdcall AttachToCell, [hStatus], [.hParent], MainLayout.cellStatus
        invoke  SendMessageW, [hStatus], SB_SETPARTS, 7, cStatusBarParts

        return
endp



proc StatusWinProc, .hwnd, .wmsg, .wparam, .lparam
.rect RECT
begin
        dispatch [.wmsg]

.ondefault:
        stc
        return

oncase WM_ERASEBKGND

; in order to remove graphical artifacts on resizing in WINE!!!

        invoke  InvalidateRect, [.hwnd], 0, 0

        mov     eax, 1
        clc
        return

        enddispatch

endp


;-----------------------------------------------------
; Dialog proc for Source editor
;-----------------------------------------------------
proc EditorHostProc, .hwnd, .wmsg, .wparam, .lparam
.nmh    NMHDR
.tci    TEditorTab
.str    rw      1024
.rect    RECT
.rectc   RECT
.recte   RECT
.ps      PAINTSTRUCT

begin
        dispatch [.wmsg]

.default:
        stc
        return

;oncase WM_NOTIFY
;
;        mov     eax, [.lparam]
;
;        OutputValue "WM_NOTIFY in editors host: ", [eax+NMHDR.code], 16, 8
;
;        stc
;        return


oncase WM_PAINT

        invoke  SendMessageW, [.hwnd], TCM_GETITEMCOUNT, 0, 0
        test    eax, eax
        jz      .paint_empty

; paint focus line on the tab:

        invoke  GetPropW, [.hwnd], [propOldWinProc]
        invoke  CallWindowProcW, eax, [.hwnd], [.wmsg], [.wparam], [.lparam]

        invoke  SendMessageW, [.hwnd], TCM_GETCURSEL, 0, 0
        test    eax, eax
        js      .end_paint

        lea     edi, [.rect]
        invoke  SendMessageW, [.hwnd], TCM_GETITEMRECT, eax, edi

        lea     eax, [.rectc]
        stdcall RectCopy, eax, edi

        invoke  CreateSolidBrush, $0080ff
        mov     ebx, eax

        mov     eax, [.rect.top]
        dec     eax
        mov     [.rect.bottom], eax
        dec     eax
        mov     [.rect.top], eax

        invoke  GetDC, [.hwnd]
        mov     esi, eax

        invoke  FillRect, esi, edi, ebx

        inc     [.rect.top]
        inc     [.rect.bottom]
        dec     [.rect.left]
        inc     [.rect.right]

        invoke  FillRect, esi, edi, ebx

        mov     eax, [.rectc.left]
        inc     eax
        mov     [.rectc.right], eax
        dec     [.rectc.left]

        lea     eax, [.rectc]
        invoke  FillRect, esi, eax, ebx


        invoke  ReleaseDC, esi
        invoke  DeleteObject, ebx

.end_paint:
        xor     eax, eax
        clc
        return


.paint_empty:
        lea     esi, [.ps]
        invoke  BeginPaint, [.hwnd], esi

        mov     eax, $56f0ff
        invoke  CreateSolidBrush, eax
        mov     ebx, eax

        lea     edi, [.ps.rcPaint]

        invoke  DrawEdge, [.ps.hdc], edi, EDGE_SUNKEN, BF_RECT or BF_ADJUST

        invoke  FillRect, [.ps.hdc], edi, ebx
        invoke  DeleteObject, ebx

        invoke  EndPaint, [.hwnd], esi

        xor     eax, eax
        inc     eax
        clc
        return



oncase WM_ERASEBKGND

        xor     edi, edi

        invoke  SendMessageW, [.hwnd], TCM_GETITEMCOUNT, 0, 0
        test    eax, eax
        jz      .final_invalidate

        invoke  GetSysColor, COLOR_BTNFACE
        invoke  CreateSolidBrush, eax
        mov     ebx, eax

        lea     esi, [.rect]
        invoke  GetClientRect, [.hwnd], esi

        lea     edi, [.rectc]
        stdcall RectCopy, edi, esi

        invoke  SendMessageW, [.hwnd], TCM_ADJUSTRECT, FALSE, esi

        xor     eax, eax
        mov     [.recte.left], eax
        mov     [.recte.top], eax
        mov     eax, [.rectc.right]
        mov     ecx, [.rect.top]
        mov     [.recte.right], eax
        mov     [.recte.bottom], ecx

        lea     edi, [.recte]
        invoke  FillRect, [.wparam], edi, ebx
        invoke  InvalidateRect, [.hwnd], edi, 0

        mov     eax, [.rect.right]
        mov     ecx, [.rectc.bottom]
        mov     [.recte.left], eax
        mov     [.recte.bottom], ecx

        invoke  FillRect, [.wparam], edi, ebx
        invoke  InvalidateRect, [.hwnd], edi, 0

        xor     eax, eax
        mov     ecx, [.rect.left]
        mov     [.recte.left], eax
        mov     [.recte.right], ecx

        invoke  FillRect, [.wparam], edi, ebx
        invoke  InvalidateRect, [.hwnd], edi, 0

        mov     eax, [.rect.bottom]
        mov     ecx, [.rectc.bottom]
        mov     edx, [.rectc.right]
        mov     [.recte.top], eax
        mov     [.recte.bottom], ecx
        mov     [.recte.right], edx

        invoke  FillRect, [.wparam], edi, ebx
        invoke  DeleteObject, ebx

.final_invalidate:

        invoke  InvalidateRect, [.hwnd], edi, 0

        xor     eax, eax
        inc     eax
        clc
        return


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

oncase WM_MEASUREITEM
        stdcall OnMeasureItem,[.hwnd],[.lparam]
        xor     eax, eax
        inc     eax
        clc
        return

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

oncase WM_DRAWITEM
        stdcall OnDrawItem, [.lparam]
        xor     eax, eax
        inc     eax
        clc
        return

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

oncase WM_MENUCHAR
        stdcall OnMenuChar, [.wparam], [.lparam]
        jc      .default

        clc
        return

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

oncase SGM_REALIGN

        invoke  GetPropW, [.hwnd], [propSplitGrid]
        test    eax, eax
        jz      .default

        lea     edi, [eax+TArray.array]
        lea     esi, [.rect]
        invoke  GetClientRect, [.hwnd], esi
        invoke  SendMessageW, [.hwnd], TCM_ADJUSTRECT, FALSE, esi

        stdcall RealignGrid, [.hwnd], edi, esi

        clc
        return


;------------ EHM_FOCUSWINDOW ----------------------

oncase EHM_FOCUSWINDOW

        stdcall IndexByWindow, [.wparam]
        cmp     eax, -1
        je      .endfocus

        invoke  SendMessageW, [.hwnd], _EHM_SETACTIVETAB, eax, FALSE

.endfocus:
        clc
        return

;------------ EHM_FILECHANGED ----------------------
oncase EHM_FILECHANGED

        mov     eax, [.wparam]
        stdcall IndexByWindow, [eax+TOpenFile.hEditor]
        cmp     eax, -1
        je      .endchanged

        mov     edi, eax

        mov     [.tci.mask], TCIF_TEXT or TCIF_IMAGE

        stdcall GetScreenProperties, [.wparam]
        push    eax

        mov     [.tci.iImage], edx

        stdcall utf8ToWideChar, eax
        stdcall StrDel ; from the stack
        push    eax

        mov     [.tci.pszText], eax
        lea     ecx, [.tci]
        invoke  SendMessageW, [.hwnd], TCM_SETITEMW, edi, ecx

        stdcall FreeMem ; from the stack...

; Now it will not have header.
;        mov     eax, [.wparam]
;        stdcall SetControlTextUtf8, [.hwnd], [eax+TOpenFile.hFileName]

.endchanged:
        clc
        return

;----------- EHM_DOCKWINDOW ------------------------
oncase EHM_DOCKWINDOW

;        invoke  ShowWindow, [.wparam], SW_HIDE
        invoke  SetParent, [.wparam], [.hwnd]

        xor     ebx, ebx
        mov     [.tci.mask], TCIF_TEXT or TCIF_IMAGE or TCIF_PARAM

        stdcall GetEditorFile, [.wparam]
        stdcall GetScreenProperties, eax

        mov     [.tci.iImage], edx
        push    eax

        stdcall utf8ToWideChar, eax
        stdcall StrDel ; from the stack

        mov     [.tci.pszText], eax

        invoke  SendMessageW, [.hwnd], TCM_GETCURSEL, ebx, ebx
        lea     edi, [eax+1]

        mov     eax, [.wparam]
        mov     [.tci.hEditor], eax

        lea     ecx, [.tci]
        invoke  SendMessageW, [.hwnd], TCM_INSERTITEMW, edi, ecx
        mov     edi, eax

        stdcall FreeMem, [.tci.pszText]

;        invoke  SendMessageW, [.hwnd], _EHM_SETACTIVETAB, edi, ebx
        return

;----------- EHM_UNDOCKWINDOW ----------------------
oncase EHM_UNDOCKWINDOW

        return

;----------- EHM_GETCURRENTASMEDIT -----------------
oncase EHM_GETCURRENTASMEDIT

; Loads the [.tci] structure with tab info depending of
; TCommonFile structure pointed by esi.
        invoke  SendMessageW, [.hwnd], TCM_GETCURSEL, 0, 0

        mov     [.tci.mask], TCIF_PARAM

        lea     ecx, [.tci]
        invoke  SendMessageW, [.hwnd], TCM_GETITEMW, eax, ecx

        xor     eax, eax
        stdcall GetAsmEdit, [.tci.hEditor]
        clc
        return

;----------- WM_DROPFILES -------------------------
oncase WM_DROPFILES

        DebugMsg "WM_DROPFILES on the editor hosts"

        lea     esi, [.str]

        invoke  DragQueryFileW, [.wparam], -1, esi, 1024
        mov     ebx, eax
        xor     edi, edi

.opendroploop:
        invoke  DragQueryFileW, [.wparam], edi, esi, 1024

        stdcall WideCharToUtf8, esi
        push    eax

        stdcall OpenFileByName, eax, NULL, TRUE
        stdcall StrDel ; from the stack

        inc     edi
        dec     ebx
        jnz     .opendroploop

        invoke  DragFinish, [.wparam]
        xor     eax, eax
        clc
        return

;-----------WM_COMMAND-----------------------------
oncase WM_COMMAND

        movzx   eax, word [.wparam+2]

        cmp     eax, AEN_POSCHANGE
        je      .updatestatusbar

        cmp     eax, AEN_TEXTCHANGE
        je      .updatestatusbar

        cmp     eax, AEN_SETFOCUS
        je      .editor_focused

        cmp     eax, AEN_MODECHANGE
        jne     .default

.updatestatusbar:

        stdcall UpdateStatusbar, [.lparam]
        stdcall UpdatePAHint
        stdcall UpdateEHelp
        jmp     .default

.editor_focused:

locals
  .nm NMHDR
endl
        stdcall GetEditorFile, [.lparam]
        stdcall SetCurrentFile, eax

        invoke  GetAncestor, [.hwnd], GA_PARENT
        test    eax, eax
        jz      .updatestatusbar

        mov     ebx, eax

        mov     eax, [.hwnd]
        mov     [.nm.hwndFrom], eax

        invoke  GetWindowLongW, [.hwnd], GWL_ID
        mov     [.nm.idFrom], eax
        mov     [.nm.code], NM_SETFOCUS

        lea     eax, [.nm]
        invoke  SendMessageW, ebx, WM_NOTIFY, [.nm.idFrom], eax

        jmp     .updatestatusbar



;;----------- WM_SETFOCUS ---------------------------

oncase WM_SETFOCUS

        invoke  SendMessageW, [hEditorsHost], TCM_GETCURSEL, 0, 0
        cmp     eax, -1
        je      .default

; focus active asmedit
        lea     ecx, [.tci]
        mov     [.tci.mask], TCIF_PARAM
        invoke  SendMessageW, [hEditorsHost], TCM_GETITEMW, eax, ecx

        stdcall GetEditorFile, [.tci.hEditor]
        stdcall FocusFile, eax
        jmp     .qfalse


;----------- _EHM_SETACTIVETAB -----------------------
oncase _EHM_SETACTIVETAB

        lea     ebx, [.nmh]

        cmp     [.lparam], FALSE
        je      .doset

        invoke  SendMessageW, [.hwnd], TCM_GETITEMCOUNT, 0, 0
        mov     esi, eax
        dec     esi
        js      .qfalse

        invoke  SendMessageW, [.hwnd], TCM_GETCURSEL, 0, 0
        test    eax, eax
        js      .qfalse

        add     eax, [.wparam]
        jns     @f
        mov     eax, esi        ; Set Max
@@:
        cmp     eax, esi
        jle     @f
        xor     eax, eax
@@:
        mov     [.wparam], eax

.doset:
;        DebugMsg "Set active tab!"

        mov     [.nmh.code], TCN_SELCHANGING
        mov     eax, [.hwnd]
        mov     [.nmh.hwndFrom], eax

        invoke  SendMessageW, [hMainForm], WM_NOTIFY, 0, ebx

        invoke  SendMessageW, [.hwnd], TCM_SETCURSEL, [.wparam], 0

        mov     [.nmh.code], TCN_SELCHANGE
        invoke  SendMessageW, [hMainForm], WM_NOTIFY, 0, ebx

        jmp     .qfalse

;------------------ WM_PARENTNOTIFY --------------------

oncase WM_PARENTNOTIFY

        movzx   eax, word [.wparam]
        cmp     eax, WM_DESTROY
        jne     .default

        stdcall IndexByWindow, [.lparam]
        cmp     eax, -1
        je      .default

        mov     ebx, eax
        invoke  SendMessageW, [.hwnd], TCM_DELETEITEM, eax, 0
        invoke  SendMessageW, [.hwnd], TCM_GETITEMCOUNT, 0, 0
        test    eax, eax
        jz      .hidetabs

        test    ebx, ebx
        jz      .setactive
        dec     ebx

.setactive:
        invoke  SendMessageW, [.hwnd], _EHM_SETACTIVETAB, ebx, FALSE
        jmp     .qfalse


.hidetabs:
        cmp     [ptrProjectFile], 0
        je      .qfalse

        stdcall FocusFile, [ptrProjectFile]
        jmp     .qfalse


;----------- WM_DESTROY -------------------------
oncase WM_DESTROY

        stdcall SaveMRUToIni, [ptrMRUFiles], [hCfgFileName], cMRUFilesSection
        stdcall DestroyMRUList, [ptrMRUFiles]

        invoke  SendMessageW, [.hwnd], WM_GETICON, FALSE, 0
        invoke  DestroyIcon, eax

        invoke DestroyMenu, [hTabMenu]

        invoke  ImageList_Destroy, [hTabIml]
        jmp     .qfalse

cMRUFilesSection text 'MRU_files'

;-----------AM_INITWINDOW----------------------------
oncase AM_INITWINDOW

        xor     ebx,ebx

        invoke  SendMessageW, [.hwnd], TCM_SETITEMEXTRA, sizeof.TEditorTab - sizeof.TCITEMHEADER
        invoke  SendMessageW, [.hwnd], TCM_SETPADDING, 0, $00040004

; Set image list.
        stdcall ImageList_LoadGif, [hInstance], resBmpTab, 16, FALSE
        mov     [hTabIml], eax
        invoke  SendMessageW, [.hwnd], TCM_SETIMAGELIST, 0, [hTabIml]


        stdcall LoadMRUFromIni, 16, [hCfgFileName], cMRUFilesSection
        mov     [ptrMRUFiles], eax

;        invoke  DragAcceptFiles, [.hwnd], TRUE

.qfalse:
        xor     eax,eax
        clc
        return

.qtrue:
        xor     eax, eax
        inc     eax
        clc
        return

        enddispatch
endp



  cStatusLine text 'Line:'
  cStatusPos  text 'Pos:'

  cStatusCompiled      text 'Total: '
  cStatusCompiledBin   text '  Binary: '
  cStatusCompiledLines text ' lines.'

iglobal
  cStatusOverwrite  du 'Overwrite', 0
  cStatusInsert     du 'Insert', 0
  cStatusVertical   du '||', 0
  cStatusHorizontal du '=', 0
  cStatusModified   du 'Modified', 0
endg


proc UpdateStatusbar, .hEditor
.pos  AEPOS
begin
        push    esi edi ebx
        mov     ebx, [.hEditor]   ; handle of the AsmEdit window

        lea     eax, [.pos]
        invoke  SendMessageW, ebx, AEM_GETPOS, eax, 0

        stdcall NumToStr, [.pos.caretLine], ntsDec or ntsUnsigned
        push    eax
        stdcall StrInsert, eax, cStatusLine, 0
        stdcall utf8ToWideChar, eax
        stdcall StrDel ; from the stack
        push    eax
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 0, eax
        stdcall FreeMem ; from the stack


        stdcall NumToStr, [.pos.caretPosition], ntsDec or ntsUnsigned
        push    eax
        stdcall StrInsert, eax, cStatusPos, 0
        stdcall utf8ToWideChar, eax
        stdcall StrDel ; from the stack
        push    eax
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 1, eax
        stdcall FreeMem ; from the stack

        invoke  SendMessageW, ebx, AEM_GETMODE, 0, 0
        push    eax
        mov     ecx, cStatusOverwrite
        test    eax, AEMODE_OVERWRITE
        jnz     @f
        mov     ecx, cStatusInsert
@@:
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 2, ecx
        pop     eax  ; mode again
        mov     ecx, cStatusVertical
        test    eax, AEMODE_VERTICALSEL
        jnz     @f
        mov     ecx, cStatusHorizontal
@@:
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 3, ecx

        invoke  SendMessageW, ebx, AEM_GETMODIFIED, 0, 0
        mov     ecx, cStatusModified
        test    eax, eax
        jnz     @f
        xor     ecx, ecx
@@:
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 4, ecx

        cmp     [hDebugThread], 0
        jne     @f

        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 5, NULL

        cmp     [fSetCurrent], 0
        jne     @f

        invoke  SendMessageW, ebx, AEM_SETFOCUSLINE, 0, 0
@@:

;        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 6, NULL

        pop     ebx edi esi
        return
endp




proc UpdateStatusbarCompiler
begin
        push    esi

; update source lines display.
        xor     esi, esi
        mov     eax, [TotalLines]
        or      eax, [AsmLines]
        jz      .disp_lines

        stdcall StrDup, cStatusCompiled
        mov     esi, eax

        stdcall NumToStr, [TotalLines], ntsUnsigned or ntsDec
        stdcall StrCat, esi, eax
        stdcall StrDel, eax
        stdcall StrCat, esi, cStatusCompiledLines
        stdcall StrCat, esi, cStatusCompiledBin

        stdcall NumToStr, [AsmLines], ntsUnsigned or ntsDec
        stdcall StrCat, esi, eax
        stdcall StrDel, eax
        stdcall StrCat, esi, cStatusCompiledLines

        stdcall StrPtr, esi

        stdcall utf8ToWideChar, esi
        stdcall StrDel, esi
        mov     esi, eax

.disp_lines:
        invoke  SendMessageW, [hStatus], SB_SETTEXTW, 6, esi
        stdcall FreeMem, esi

        pop     esi
        return
endp


;-------------------------------------------------------------
; Returns index of the tab containing handle of given window
; or -1 if such tab item is not found.
;-------------------------------------------------------------
proc IndexByWindow, .hChildWindow
.tci TEditorTab
begin
        push    ebx

        invoke  SendMessageW, [hEditorsHost], TCM_GETITEMCOUNT, 0, 0
        mov     ebx, eax

.while:
        dec     ebx
        js      .notfound

        mov     [.tci.mask], TCIF_PARAM
        lea     eax, [.tci]
        invoke  SendMessageW, [hEditorsHost], TCM_GETITEMW, ebx, eax
        test    eax, eax
        jz      .while

        mov     eax, [.hChildWindow]
        cmp     [.tci.hEditor], eax
        jne     .while

.notfound:
        mov     eax, ebx
        pop     ebx
        return
endp



;-----------------------------------------------------------
; Adds current active in editor file to current category in
; the project.
; If the file belongs to the project, remove it from the
; project and then adds it in the current category.
;-----------------------------------------------------------
proc OnAddToProject, .wparam, .lparam
begin
        mov     esi, [ptrCurrentFile]
        test    esi, esi
        jz      .finish

        invoke  SendMessageW, [hProjManager], PMM_ADDFILE, esi, -1

.finish:
        return
endp


proc OnSetCurrentAsMain, .wparam, .lparam
begin
        mov     esi, [ptrCurrentFile]
        test    esi, esi
        jz      .finish

        invoke  SendMessageW, [hProjManager], PMM_ADDFILE, esi, 0

.finish:
        return
endp



proc GetCurrentAsmEdit
.str rb 512
begin
        pushad
; first try to get the focused window

        invoke  GetFocus
        mov     ebx, eax

        lea     esi, [.str]
        invoke  GetClassNameW, ebx, esi, 512

        mov     ecx, cAsmEditClassName.length
        cmp     eax, ecx
        jne     .get_current_edit

        mov     edi, cAsmEditClassName

        repe cmpsw
        jne   .get_current_edit

.edit_ok:
        mov     [esp+4*regEAX], ebx
        clc
        popad
        return

.get_current_edit:
        invoke  SendMessageW, [hEditorsHost], EHM_GETCURRENTASMEDIT, 0, 0
        mov     ebx, eax
        test    eax, eax
        jnz     .edit_ok

        stc
        popad
        return
endp