////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//////////////////////////////////////////////////////////////////////////////
//
// TXT.CPP
// 
// History:
//		08/07/96 JMI	Started.
//
//		08/12/96	JMI	Now utilizes CGuiItem::DrawText to draw text.
//
//		09/24/96	JMI	Changed all BLU_MB?_* macros to RSP_MB?_* macros.
//
//		10/31/96	JMI	Changed:
//							Old label:		New label:
//							=========		=========
//							CTxt				RTxt
//							CImage			RImage
//							CGuiItem			RGuiItem
//							CENTERED			RGuiItem::Centered
//
//		11/27/96	JMI	Added initialization of m_type to identify this type
//							of GUI item.
//
//		01/01/96	JMI	Compose() no longer sets hot area (now done by base 
//							class).
//
//		01/04/96	JMI	Upgraded HotCall() to new CursorEvent().  This upgrade
//							is in response to RGuiItem now using relative RHots.
//							Now m_hot.m_sX/Y is parent item relative just like
//							m_sX/Y.  This should simplify a lot of stuff and even
//							fix some odd features like being able to click a GUI
//							item that exceeds the boundary of its parent.  This fix
//							will be essential for the not-yet-existent RListBox since
//							it will most likely scroll many children through a small
//							client area.
//							Now there are two regions associated with cursor events.
//							The first is the 'hot' area.  This is the area that m_hot
//							is set to include.  Child items can only receive cursor
//							events through this area.  The second is the 'event' area.
//							This is the area where the item really is actually con-
//							cerned with cursor events.  Example:  For a Dlg, the
//							entire window is the 'hot' area and the title bar is the
//							'event' area.
//
//////////////////////////////////////////////////////////////////////////////
//
// This a GUI item that is based on the basic RGuiItem. 
// This overrides HotCall() to get information about where a click in its RHot
// occurred.
// This overrides Compose() to add text.
//
// Enhancements/Uses:
// To change the look of text when pressed, you may want to override the
// Compose() or DrawBorder() in a derived class.
// To change the background of the text item, see RGuiItem.
// To get a callback on a click/release pair in the text item, set m_tcUser.
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Headers.
//////////////////////////////////////////////////////////////////////////////
#include "Blue.h"

#ifdef PATHS_IN_INCLUDES
	#include "ORANGE/GUI/txt.h"
#else
	#include "txt.h"
#endif // PATHS_IN_INCLUDES

//////////////////////////////////////////////////////////////////////////////
// Module specific macros.
//////////////////////////////////////////////////////////////////////////////

// Sets val to def if val is -1.
#define DEF(val, def)	((val == -1) ? def : val)

//////////////////////////////////////////////////////////////////////////////
// Module specific typedefs.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Module specific (static) variables.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Construction/Destruction.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// Default constructor.
//
//////////////////////////////////////////////////////////////////////////////
RTxt::RTxt()
	{
	m_type					= Txt;	// Indicates type of GUI item.
	m_pGuiTextString = new PVR::cGuiTextString();	
	}

//////////////////////////////////////////////////////////////////////////////
//
// Destructor.
//
//////////////////////////////////////////////////////////////////////////////
RTxt::~RTxt()
	{		
		if (m_pGuiTextString != nullptr)
		{
			m_pprint->ResetDestGuiTextString();
			delete m_pGuiTextString;
		}
	}

////////////////////////////////////////////////////////////////////////
// Methods.
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
// Draw text in m_szText in m_u32TextColor with transparent
// background at sX, sY with sW and m_sJustification.
// Does nothing if m_szText is empty.
//
////////////////////////////////////////////////////////////////////////
int16_t RTxt::DrawTextPVR( // Returns 0 on success.
	int16_t sX,				   // X position in image.
	int16_t sY,				   // Y position in image.
	int16_t sW /*= 0*/,		   // Width of text area.
	int16_t sH /*= 0*/		   // Height of test area.
	)
{
	//return RGuiItem::DrawTextPVR(sX,sY,sW,sH);
	int16_t sRes = 0; // Assume success.	
	// Draw text.
	if (m_szText[0] != '\0')
	{		
		if (sW == 0)
		{
			GetClient(NULL, NULL, &sW, NULL);
		}

		if (sH == 0)
		{
			GetClient(NULL, NULL, NULL, &sH);
		}

	
		m_pprint->SetColor(m_u32TextColor, (int16_t)0, m_u32TextShadowColor);

		// Set size.  Hopefully this won't do too much scaling for
		// caching purposes but I'm not sure.
		m_pprint->SetFont(m_sFontCellHeight);
	
		SetJustification();
		SetTextEffects();

		m_pprint->SetDestGuiTextString(m_pGuiTextString);
		//m_pprint->offsetX = offsetPvrX;
		//m_pprint->offsetY = offsetPvrY;	
		m_pGuiTextString->Flush();	
		// m_pprint->SetColumn(sX, sY, sW, sH);
		m_pprint->SetColumn(0, 0, sW, sH);
		m_pprint->printPVR(m_sOffsetX, m_sOffsetY, m_szText);	
		// todo: greasy
		m_pprint->ResetDestGuiTextString();
	}

	return sRes;
}

////////////////////////////////////////////////////////////////////////
//
// TODO
//
////////////////////////////////////////////////////////////////////////
int16_t RTxt::DrawPVR( // Returns 0 on success.
	int16_t sDstX /*= 0*/, // X position in destination.
	int16_t sDstY /*= 0*/, // Y position in destination.
	int16_t sSrcX /*= 0*/, // X position in source.
	int16_t sSrcY /*= 0*/, // Y position in source.
	int16_t sW /*= 0*/,	   // Amount to draw.
	int16_t sH /*= 0*/,	   // Amount to draw.
	RRect *prc /*= NULL*/) // Clip to.
{	
	int16_t sRes = RGuiItem::DrawPVR(sDstX, sDstY, sSrcX, sSrcY, sW, sH, prc);
	if (m_sVisible != FALSE)
	{
		sDstX += m_sX;
		sDstY += m_sY;	
		m_pGuiTextString->Print(sDstX, sDstY);
	}
	
	return sRes;

}
////////////////////////////////////////////////////////////////////////
//
// Compose item.
//
////////////////////////////////////////////////////////////////////////
void RTxt::Compose(			// Returns nothing.
	)	// Dest image, uses m_im if NULL.
	{
		
	// Call base (draws border and background).
	RGuiItem::Compose();

	// Draw txt stuff.
	int16_t	sX, sY, sW, sH;
	
	// Get client relative to border so we know where to put the text.
	GetClient(&sX, &sY, &sW, &sH);

	// Draw text.			
	DrawTextPVR(sX, sY, sW, sH);

	}

////////////////////////////////////////////////////////////////////////
// Querries.
////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// EOF
//////////////////////////////////////////////////////////////////////////////
