/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : DrawableElement.cpp
**
** Author : Satish Hota
**
** Description:
**    This file provides implementation for CDrawableElement class.
**
** History:
** 15-Nov-01 Satish Hota
**     File Created
** 20-Nov-01 Satish Hota
**     Added new methods
** 24-Nov-01 Satish Hota
**     Added new methods and renamed and modified existing methods
** 25-Nov-01 Satish Hota
**     Temporarily commented the PostUpdate call in Select, SetFromLocation and
**     SetToLocation to enable these calls from the constructor of the document.
** 26-Nov-01 Satish Hota
**     Set the class static member c_nTextClearance to 5.  Initialized the
**     temporary text member to "testText".
** 26-Nov-01 Edwin Chan
**     Added method OnEditProperties, Intersects, and IsSelected
** 27-Nov-01 Edwin Chan
**     Modified OnEditProperties and UnSelect.
** 28-Nov-01 Satish Hota
**     Renamed this class from CDrawable to CDrawableElement.  Created a 
**     CDrawable super-class which has usurped some of the methods and member
**     variables of this class.
** 30-Nov-01 Satish Hota
**     Implemented the default GetText method.  Also added the ClipLine 
**     method.
** 02-Dec-01 Edwin Chan, Satish Hota
**     Moved implementation of OnEditProperties to CDrawableConcept.
**     Added DoJoin and DoDetach methods.
******************************************************************************/

#include "stdafx.h"
#include "ConGraT.h"
#include "DrawableElement.h"

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

/* Class level static variable declarations */
CMFCDevConHelper* CDrawableElement::c_pDevConHelper = NULL;
const int CDrawableElement::c_nTextClearance = 5;

/*******************************************************************
** Name: CDrawableElement::CDrawableElement
**
** Description:
** This is the default constructor.  It initializes the state of the
** variables defined in this class.
**
** Arguments:
**   None
**
** Returns:
**   None
**
** Exceptions:
**   None
********************************************************************/
CDrawableElement::CDrawableElement()
{
    m_bSelected = false;
    m_strText = "testText";
}

/*******************************************************************
** Name: CDrawableElement::~CDrawableElement
**
** Description:
** This is the default destructor.  It currently does nothing.
**
** Arguments:
**   None
**
** Returns:
**   None
**
** Exceptions:
**   None
********************************************************************/
CDrawableElement::~CDrawableElement()
{

}

/*******************************************************************
** Name: CDrawableElement::HitTest
**
** Description:
** This method is used to test if the input point is within its 
** graphic boundary.
**
** Arguments:
**   int nXCoor,  [in]  - Logical X coordinates
**   int nYCoor,  [in]  - Logical Y coordinates
**
** Returns:
**   CDrawableElement::RC_S_HitSucess - Point is within the boundary
**   CDrawableElement::RC_S_HitFailue - Point is not within the boundary
**   CG_E_FAIL                 - General failure
**
** Exceptions:
**   None
********************************************************************/
long CDrawableElement::HitTest(int nXCoor, int nYCoor)
{
    return CG_S_OK;
}


/*******************************************************************
** Name: CDrawableElement::Select
**
** Description:
** This sets the drawable objects selected state to true.
**
** Arguments:
**   None
**
** Returns:
**   None
**
** Exceptions:
**   None
********************************************************************/
void CDrawableElement::Select()
{
    m_bSelected = true;
    //c_pDevConHelper->PostUpdate();
}

/*******************************************************************
** Name: CDrawableElement::UnSelect
**
** Description:
** This sets the drawable objects selected state to false.
**
** Arguments:
**   None
**
** Returns:
**   None
**
** Exceptions:
**   None
********************************************************************/
void CDrawableElement::UnSelect()
{
    m_bSelected = false;
    c_pDevConHelper->PostUpdate();
}

/*******************************************************************
** Name: CDrawableElement::SetDevConHelper
**
** Description:
** This method is used to set the device context helper pointer.
**
** Arguments:
**   CMFCDevConHelper *pDevConHelper [in] - device context helper
**                                          pointer.
**
** Returns:
**   Nothing
**
** Exceptions:
**   None
********************************************************************/
void CDrawableElement::SetDevConHelper(CMFCDevConHelper *pDevConHelper)
{
    c_pDevConHelper = pDevConHelper;
}

/*******************************************************************
** Name: CDrawableElement::TranslateBy
**
** Description:
** This method is used to translate the drawable object by the
** vector specified
**
** Arguments:
**   int nDeltaX,  [in]  - Logical X coordinates
**   int nDeltaY,  [in]  - Logical Y coordinates
**
** Returns:
**   CG_S_OK        - Success
**
** Exceptions:
**   None
********************************************************************/
long CDrawableElement::TranslateBy(int nDeltaX, int nDeltaY)
{
    m_nFromX += nDeltaX;
    m_nFromY += nDeltaY;
    m_nToX += nDeltaX;
    m_nToY += nDeltaY;
    c_pDevConHelper->PostUpdate();

    return CG_S_OK;
}

void CDrawableElement::SetFromLocation(int nFromX, int nFromY)
{
    m_nFromX = nFromX;
    m_nFromY = nFromY;
    //c_pDevConHelper->PostUpdate();
}

void CDrawableElement::SetToLocation(int nToX, int nToY)
{
    m_nToX = nToX;
    m_nToY = nToY;
    //c_pDevConHelper->PostUpdate();
}

bool CDrawableElement::IsSelected()
{
    return m_bSelected;    
}

bool CDrawableElement::Intersects(const CRect &rect)
{
	CRect fixed(this->m_nFromX, this->m_nFromY, this->m_nToX, this->m_nToY);
	fixed.NormalizeRect();
	CRect rectT = rect;
	rectT.NormalizeRect();
	return !(rectT & fixed).IsRectEmpty();
}

void CDrawableElement::OnEditProperties(CmdType eCmd)
{

}

long CDrawableElement::GetText(std::string& strText)
{
    strText = m_strText;
    return CG_S_OK;
}

long CDrawableElement::ClipLine(int *pnFromX, int *pnFromY, int *pnToX, int *pnToY)
{
    return CG_S_OK;
}

void CDrawableElement::DoJoin(CDrawableElement *pInElement)
{
}

void CDrawableElement::DoDetach()
{

}
