#include "stdafx.h"

#ifdef ACTIVE_EDITOR
#include "acp_base.h"
#include "direct.h"
#include "itf/CPARes.h"
#include "errint.h"
#include "mmgint.h"
#include "itf/FRMBase.hpp"
#include "itf/FRMBsMn.hpp"
#include "itf/CPAProj.hpp"

IMPLEMENT_DYNCREATE(FRMBase, CFrameWnd)
BEGIN_MESSAGE_MAP(FRMBase, CFrameWnd)
    ON_WM_NCLBUTTONDOWN()
    ON_WM_SIZE()
    ON_WM_NCHITTEST()
    ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

// Constructor
//------------------------------------------------------------------------
FRMBase::FRMBase(void)
{
    m_bAlreadySize = FALSE;
    m_cLock = 0;
    m_uiHitTest = HTNOWHERE;
    m_p_oMainWorld = NULL;

    memset(&m_stWinTop, 0, sizeof(tdstFRMLink));
    memset(&m_stWinBottom, 0, sizeof(tdstFRMLink));
    memset(&m_stWinLeft, 0, sizeof(tdstFRMLink));
    memset(&m_stWinRight, 0, sizeof(tdstFRMLink));
    m_bKeepPos = TRUE;
    m_iInitWidth = 0;
    m_iInitHeight = 0;
    m_dwCaptionStyle = 0;//WS_CAPTION|WS_MAXIMIZEBOX|WS_MINIMIZEBOX|WS_SYSMENU;
    m_oLastPos.SetRectEmpty();
}

// Create frame
//------------------------------------------------------------------------
BOOL FRMBase::CreateBase
(
    LPCTSTR lpszWindowName, 
    int _iCol, 
    int _iRow, 
    CPA_MainWorld *_p_oMainWorld, 
    CCreateContext *pContext
)
{
    BOOL        bRes;
    CFrameWnd   *pParentWnd;
    DWORD       dwStyle;

    m_iCol = _iCol;
    m_iRow = _iRow;
    m_p_oMainWorld = _p_oMainWorld;

    // Style of window
	if((m_iCol!=2)||(m_iRow!=2))
		dwStyle = m_dwCaptionStyle|WS_DISABLED|WS_CLIPCHILDREN|WS_THICKFRAME|/*WS_OVERLAPPED|*/WS_CHILD|WS_CLIPSIBLINGS;
	else
		dwStyle = m_dwCaptionStyle|WS_DISABLED|WS_CLIPCHILDREN|WS_THICKFRAME;//WS_OVERLAPPED;

    // Parent window : The first created one
//    pParentWnd = g_oFrameGest.mp_oFirstWindow;
	pParentWnd = &g_oBaseFrame;

    // Create frame window
    bRes = CFrameWnd::Create
    (
        NULL,
        lpszWindowName,
        dwStyle,
        rectDefault,
        pParentWnd,
        NULL,
        0,
        pContext
    );

    ASSERT(bRes);

    // Add frame to list of frames
    g_oFrameGest.mfn_vAddOneFrame(this);

    return bRes;
}

// To set caption title
//------------------------------------------------------------------------
void FRMBase::mfn_vEnableCaption(BOOL _bVal)
{
	if(_bVal)
		ModifyStyle(0, WS_CAPTION|m_dwCaptionStyle, WS_VISIBLE);
	else
		ModifyStyle(WS_CAPTION|m_dwCaptionStyle, 0, WS_VISIBLE);

    if(g_oFrameGest.m_iCanRefresh >= 0)
    {
        RECT    stRect;
	    GetWindowRect(&stRect);
		g_oBaseFrame.ScreenToClient(&stRect);
        m_oCurPos.left--;
	    MoveWindow(&stRect);
        stRect.left++;
	    MoveWindow(&stRect);
    }
}

//*************************************************************
// MESSAGES
//*************************************************************

// Select window, but not move it
//-------------------------------------------------------------
void FRMBase::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
    if(nHitTest == HTCAPTION)
        SetFocus();
    else
        CFrameWnd::OnNcLButtonDown(nHitTest, point);
}

// To test point under mouse
//-------------------------------------------------------------
UINT FRMBase::OnNcHitTest(CPoint point)
{
    CSize   sizeFrame(GetSystemMetrics(SM_CXFRAME), GetSystemMetrics(SM_CYFRAME));
    UINT    uiReponse = CFrameWnd::OnNcHitTest(point);
    RECT    stRect;

    GetWindowRect(&stRect);

    //
    // Where is this ? : No corners
    //
    switch(uiReponse)
    {
    case HTTOPLEFT:
        if(point.x < stRect.left + sizeFrame.cx)
            uiReponse = HTLEFT;
        else
            uiReponse = HTTOP;
        break;

    case HTTOPRIGHT:
        if(point.x > stRect.right - sizeFrame.cx)
            uiReponse = HTRIGHT;
        else
            uiReponse = HTTOP;
        break;

    case HTBOTTOMRIGHT:
        if(point.x > stRect.right - sizeFrame.cx)
            uiReponse = HTRIGHT;
        else
            uiReponse = HTBOTTOM;
        break;

    case HTBOTTOMLEFT:
        if(point.x < stRect.left + sizeFrame.cx)
            uiReponse = HTLEFT;
        else
            uiReponse = HTBOTTOM;
        break;

    case HTSIZE:
        return HTNOWHERE;
    }

    m_uiHitTest = uiReponse;

    //
    // Post process to eventually disable hit test
    //
    switch(m_uiHitTest)
	{
    case HTTOP:
    case HTBOTTOM:  
    case HTLEFT:    
    case HTRIGHT:   
		if((m_iCol==2)&&(m_iRow==2))
		{
			if(M_bEditorsActive())
				uiReponse = HTNOWHERE;
		}
		break;
	}

    switch(m_uiHitTest)
    {
    case HTTOP:
        if(m_cLock & FRM_C_MoveTop) 
            uiReponse = HTNOWHERE;
        break;

    case HTBOTTOM:  
        if(m_cLock & FRM_C_MoveBottom) 
            uiReponse = HTNOWHERE;
        break;

    case HTLEFT:    
        if(m_cLock & FRM_C_MoveLeft) 
            uiReponse = HTNOWHERE;
        break;

    case HTRIGHT:   
        if(m_cLock & FRM_C_MoveRight) 
            uiReponse = HTNOWHERE;
        break;
    }
    return uiReponse;
}

// When window is resized
//-------------------------------------------------------------
void FRMBase::OnSize(UINT nType, int cx, int cy)
{
    char    cMove = 0;

    switch(m_uiHitTest)
    {
    case HTTOP:
        cMove = FRM_C_MoveTop; 
        break;
    case HTBOTTOM:  
        cMove = FRM_C_MoveBottom; 
        break;
    case HTLEFT:    
        cMove = FRM_C_MoveLeft; 
        break;
    case HTRIGHT:   
        cMove = FRM_C_MoveRight; 
        break;
    }

    CFrameWnd::OnSize(nType, cx, cy);

    if(cMove && m_bAlreadySize == FALSE)
    {
        GetWindowRect(m_oCurPos);
		g_oBaseFrame.ScreenToClient(m_oCurPos);
        g_oFrameGest.m_iBeginSize++;
        if(g_oFrameGest.m_iBeginSize == 1)
            g_oFrameGest.mfn_vUpdateLastPos();

        g_oFrameGest.mfn_vAWindowHasMoved(this, cMove);

        g_oFrameGest.m_iBeginSize--;
        if(g_oFrameGest.m_iBeginSize == 0)
            g_oFrameGest.mfn_vRefreshWindows(FALSE);
    }
}

void FRMBase::mfn_vComputeWidthWindow(int nWidth)
{
    m_uiHitTest = HTRIGHT;
    if(m_cLock & FRM_C_MoveRight) 
    {
        m_uiHitTest = HTLEFT;
        if(m_cLock & FRM_C_MoveLeft) 
            return;
    }
    if(m_uiHitTest == HTRIGHT)
        m_oCurPos.right = m_oCurPos.left + nWidth;
    else
        m_oCurPos.left = m_oCurPos.right - nWidth;
}
void FRMBase::mfn_vWidthWindow(int nWidth)
{
    CRect   stPos = m_oCurPos;

    mfn_vComputeWidthWindow(nWidth);
    if(!stPos.EqualRect(m_oCurPos))
        MoveWindow(m_oCurPos.left, m_oCurPos.top, m_oCurPos.right - m_oCurPos.left + 1, m_oCurPos.bottom - m_oCurPos.top + 1);
}

void FRMBase::mfn_vComputeHeightWindow(int nHeight)
{
    m_uiHitTest = HTBOTTOM;
    if(m_cLock & FRM_C_MoveBottom) 
    {
        m_uiHitTest = HTTOP;
        if(m_cLock & FRM_C_MoveTop) 
            return;
    }
    if(m_uiHitTest == HTBOTTOM)
        m_oCurPos.bottom = m_oCurPos.top + nHeight;
    else
        m_oCurPos.top = m_oCurPos.bottom - nHeight;
}
void FRMBase::mfn_vHeightWindow(int nHeight)
{
    CRect   stPos = m_oCurPos;

    mfn_vComputeHeightWindow(nHeight);
    if(!stPos.EqualRect(m_oCurPos))
        MoveWindow(m_oCurPos.left, m_oCurPos.top, m_oCurPos.right - m_oCurPos.left + 1, m_oCurPos.bottom - m_oCurPos.top + 1);
}
void FRMBase::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
    CSize		sizeScreen(GetSystemMetrics(SM_CXMAXIMIZED), GetSystemMetrics(SM_CYMAXIMIZED));
    CSize       sizeDlgFrame(GetSystemMetrics(SM_CXDLGFRAME), GetSystemMetrics(SM_CYDLGFRAME));

	sizeScreen.cx -= 2*sizeDlgFrame.cx;
	sizeScreen.cy -= ((2*sizeDlgFrame.cy)+GetSystemMetrics(SM_CYCAPTION));

    lpMMI->ptMinTrackSize.x = 100;
    lpMMI->ptMinTrackSize.y = 120;
    lpMMI->ptMaxTrackSize.x = sizeScreen.cx - 100;
    lpMMI->ptMaxTrackSize.y = sizeScreen.cy - 120;

    if((m_cLock & FRM_C_MoveTop) && (m_cLock & FRM_C_MoveBottom))
        lpMMI->ptMaxTrackSize.y = sizeScreen.cy;
    if((m_cLock & FRM_C_MoveLeft) && (m_cLock & FRM_C_MoveRight))
        lpMMI->ptMaxTrackSize.x = sizeScreen.cx;

    if(!(m_cLock & FRM_C_MoveTop) && !(m_cLock & FRM_C_MoveBottom))
        lpMMI->ptMaxTrackSize.y = sizeScreen.cy;
    if(!(m_cLock & FRM_C_MoveLeft) && !(m_cLock & FRM_C_MoveRight))
        lpMMI->ptMaxTrackSize.x = sizeScreen.cx;
}
#endif // ACTIVE_EDITOR