proc PseudoAlign, .hwnd, .ptrRect, .winalign
.bounds TBounds
begin
        lea     eax, [.bounds]
        stdcall AlignWindow, [.hwnd], [.ptrRect], eax, [.winalign]
        test    eax, eax
        jz      .finish
        invoke  SetWindowPos, [.hwnd], NULL,                                                     \
                              [.bounds.x], [.bounds.y], [.bounds.width], [.bounds.height],      \
                              SWP_NOZORDER or SWP_NOCOPYBITS
.finish:
        return
endp



proc AutoArrangeWindows, .fToCursor
.info MONITORINFO

.rect1    RECT
.rect2    RECT
.curxy    POINT
begin
        push    ebx

;        invoke  ShowWindow, [hMainWindow], SW_HIDE

        lea     ebx, [.info.rcWork]
        invoke  GetClientRect, [hMainWindow], ebx
        mov     [ebx+RECT.bottom], 80        ; set it to 24 in order to hide component palette.

        cmp     [hToolPalette], 0
        je      .palette_ok

        invoke  IsWindowVisible, [hToolPalette]
        test    eax,eax
        jnz     .palette_ok

        mov     [ebx+RECT.bottom], 24

.palette_ok:
        invoke  AdjustWindowRectEx, ebx, [MainForm+TWinTemplate.style], TRUE, [MainForm+TWinTemplate.styleEx]

        mov     eax, [ebx+RECT.bottom]
        mov     ecx, [ebx+RECT.right]
        sub     eax, [ebx+RECT.top]
        sub     ecx, [ebx+RECT.left]
        invoke  SetWindowPos, [hMainWindow], 0, 0, 0, ecx, eax, SWP_NOMOVE or SWP_NOZORDER

        cmp     [.fToCursor], 0
        je      .by_window

        lea     eax, [.curxy]
        invoke  GetCursorPos, eax

        lea     eax, [.curxy]
        invoke  MonitorFromPoint, [.curxy.x], [.curxy.y], 0
        test    eax, eax
        jnz     .monitor_ok

.by_window:

        invoke  MonitorFromWindow, [hMainWindow], 2

.monitor_ok:

        mov     [.info.cbSize], sizeof.MONITORINFO
        lea     ebx, [.info]
        invoke  GetMonitorInfoW, eax, ebx

        invoke  SetWindowPos, [hApplication], 0, [.info.rcWork.left], [.info.rcWork.top], 0, 0, SWP_NOSIZE or SWP_NOZORDER or SWP_NOREDRAW

        lea     ebx, [.info.rcWork]
        stdcall PseudoAlign, [hMainWindow], ebx, waTop


        invoke  IsWindowVisible, [hProjManager]
        mov     esi, eax
        invoke  IsWindowVisible, [hPropEditor]
        test    esi, eax
        jnz     .both

        mov     ecx, [hProjManager]
        test    esi, esi
        jnz     .leftpane
        mov     ecx, [hPropEditor]
        test    eax, eax
        jz      .client

.leftpane:
        stdcall PseudoAlign, ecx, ebx, waLeft
        jmp     .client

.both:
        stdcall PseudoAlign, [hPropEditor], ebx, waLeft

        lea     eax, [.rect1]
        invoke  GetWindowRect, [hPropEditor], eax

        mov     eax, [.rect1.left]
        mov     ecx, [.rect1.right]
        sub     ecx, eax
        mov     [.rect2.left], eax
        mov     [.rect2.right], ecx
        mov     [.rect1.right], ecx

        mov     ecx, [.rect1.top]
        mov     eax, [.rect1.bottom]
        sub     eax, ecx

        push    eax

        xor     edx, edx
        mov     ecx, 3
        div     ecx          ; eax = height /3

        pop     ecx
        sub     ecx, eax

        mov     [.rect1.bottom], eax
        mov     [.rect2.bottom], ecx
        add     eax, [.rect1.top]
        mov     [.rect2.top], eax


        invoke  SetWindowPos, [hPropEditor], NULL, [.rect1.left], [.rect1.top], [.rect1.right], [.rect1.bottom], SWP_NOZORDER or SWP_NOACTIVATE
        invoke  SetWindowPos, [hProjManager], NULL, [.rect2.left], [.rect2.top], [.rect2.right], [.rect2.bottom], SWP_NOZORDER or SWP_NOACTIVATE

.client:
        stdcall PseudoAlign, [hEditorsHost], ebx, waClient

;        invoke  ShowWindow, [hMainWindow], SW_SHOWNORMAL
        invoke  SetFocus, [hEditorsHost]
        pop     ebx
        return
endp