/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : DrawableRoundedRectangle.cpp
**
** Author : Satish Hota
**
** Description:
**    This file provides implementation for CDrawableRoundedRectangle class.
**
** History:
** 21-Nov-01 Satish Hota
**     File Created
** 25-Nov-01 Satish Hota
**     Added methods for accessing Roundness.
** 26-Nov-01 Satish Hota
**     Changed Draw implementation.
** 30-Nov-01 Satish Hota
**     Modified implementation of Draw method.  It now uses draws with 
**     different pen types.  Also added and implemented the ClipLine method.
**     This method uses the CDrawableRectangle::ClipLine implementation for 
**     now.
******************************************************************************/

#include "stdafx.h"
#include "ConGraT.h"
#include "DrawableRoundedRectangle.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDrawableRoundedRectangle::CDrawableRoundedRectangle()
{
    m_nRoundX = 0;
    m_nRoundY = 0;
}

CDrawableRoundedRectangle::~CDrawableRoundedRectangle()
{

}

long CDrawableRoundedRectangle::Draw()
{
    int nWidth, nHeight;
    std::string strText;

    GetText(strText);
    c_pDevConHelper->GetTextExtents(strText, &nWidth, &nHeight);

    m_nToY = m_nFromY + nHeight + 2*c_nTextClearance;
    m_nToX = m_nFromX + nWidth + 2*c_nTextClearance + (m_nToY - m_nFromY);
    m_nRoundX = (m_nToY - m_nFromY);
    m_nRoundY = (m_nToY - m_nFromY);

    CMFCDevConHelper::PenType ePenType;
    if(m_bSelected)
    {
        ePenType = CMFCDevConHelper::PenType_Thick;
    }
    else
    {
        ePenType = CMFCDevConHelper::PenType_Solid;
    }

    c_pDevConHelper->DrawRoundedRect(m_nFromX, m_nFromY, m_nToX, m_nToY, 
                                            m_nRoundX, m_nRoundY, ePenType);
    int nXCoor, nYCoor;
    nXCoor = m_nFromX+c_nTextClearance + (m_nToY - m_nFromY)/2;
    nYCoor = m_nFromY+c_nTextClearance;
    c_pDevConHelper->TextOut(nXCoor, nYCoor, strText);
    return CG_S_OK;
}

CDrawableRoundedRectangle::CDrawableRoundedRectangle(int l, int t, int r, int b, int x, int y)
{
    m_nFromX = l;
    m_nFromY = t;
    m_nToX = r;
    m_nToY = b;
    m_nRoundX = x;
    m_nRoundY = y;
}

long CDrawableRoundedRectangle::HitTest(int nXCoor, int nYCoor)
{
    CRgn region;

    if(!region.CreateRoundRectRgn(m_nFromX, m_nFromY, m_nToX, m_nToY, m_nRoundX, m_nRoundY))
        return CG_E_FAIL;

	if(region.PtInRegion(nXCoor, nYCoor))
    {
        return RC_S_HitSucess;
    }
    else
    {
        return RC_S_HitFailue;
    }

    // Should not be coming here
    return CG_E_FAIL;
}

long CDrawableRoundedRectangle::GetRoundness(int *pnRoundX, int *pnRoundY)
{
    *pnRoundX = m_nRoundX;
    *pnRoundY = m_nRoundY;

    return CG_S_OK;
}

long CDrawableRoundedRectangle::SetRoundness(int nRoundX, int nRoundY)
{
    m_nRoundX = nRoundX;
    m_nRoundY = nRoundY;

    return CG_S_OK;
}

long CDrawableRoundedRectangle::ClipLine(int *pnFromX, int *pnFromY, int *pnToX, int *pnToY)
{
    CDrawableRectangle oRectangle(m_nFromX, m_nFromY, m_nToX, m_nToY);
    return oRectangle.ClipLine(pnFromX, pnFromY, pnToX, pnToY);
}
