/*	XGStdStaticText.cpp
 *
 *		this is my internal implementation of a static text object.
 *	This is something I simply roll myself
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <string.h>
#include <XError.h>
#include <XTextControls.h>
#include <XDataUtil.h>
#include <XDraw.h>
#include <ctype.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGStaticText::XGStaticText
 *
 *		Initialize me
 */

XGStaticText::XGStaticText(XGView *v, XGArgStream &s) : XGView(v,s)
{
	char buffer[256];
	s.ReadString(buffer);
	Init(buffer,s.ReadInteger());
}

XGStaticText::XGStaticText(XGView *v, XGSTextInitRecord &s) : XGView(v,s.v)
{
	Init(s.text,s.font);
}

/*$	XGStaticText::~XGStaticText
 *
 *		Delete me
 */

XGStaticText::~XGStaticText()
{
	fFontPtr->Detach();
}

/************************************************************************/
/*																		*/
/*	Text Drawing														*/
/*																		*/
/************************************************************************/

/*$	XGStaticText::GetTitle
 *
 *		Get the text contents of this
 */

void XGStaticText::GetTitle(char *text) const
{
	strcpy(text,fString.Get());
}

/*$	XGStaticText::SetTitle
 *
 *		Set the text and force a redraw
 */

void XGStaticText::SetTitle(const char *text)
{
	fString = text;
	InvalView();
}

/************************************************************************/
/*																		*/
/*	Static Control Drawing												*/
/*																		*/
/************************************************************************/

/*$	XGStaticText::DoDrawView
 *
 *		Draw this thing
 */

void XGStaticText::DoDrawView(XRect)
{
	XGDraw draw(this);
	XRect r;
	const char *text = fString.Get();
	
	r = GetContentRect();
	
	if (!IsEnabled()) draw.SetForeColor(KXGColorBtnShadow);
	fFontPtr->Attach();		
	draw.SetFont(fFontPtr);
	DrawLines(draw,text,r.left + 1,r.top,r.right-r.left - 2);
}

/************************************************************************/
/*																		*/
/*	Static Control Drawing												*/
/*																		*/
/************************************************************************/

/*$	XGStaticText::Init
 *
 *		This handles the initialization
 */

void XGStaticText::Init(char *buffer, short font)
{
	fFontPtr = XGFont::LoadFont(font);
	fString = buffer;
}

/************************************************************************/
/*																		*/
/*	Line Wrap Support													*/
/*																		*/
/************************************************************************/

/*$	XGStaticText::SkipSpace
 *
 *		Skip spaces
 */

short XGStaticText::SkipSpace(const char *str, short x)
{
	str += x;
	while (isspace(*str) && (*str != '\n')) x++, str++;
	return x;
}

/*$	XGStaticText::NextWord
 *
 *		Width of a word
 */

short XGStaticText::NextWord(const char *str, short x)
{
	str += x;
	while (!isspace(*str) && (*str)) x++, str++;
	return x;
}

/*$	XGStaticText::CharWidth
 *
 *		Given the pixel width to keep the string wrapped at, and the
 *	character pointer to the start of the string, this returns the
 *	number of characters that will fit into that width, adjusted for
 *	line breaks.
 */

short XGStaticText::CharWidth(XGDraw &draw, const char *str, short pwidth)
{
	short w;
	short n;
	
	w = SkipSpace(str,0);
	w = NextWord(str,w);				/* Draw at least one word */
	for (;;) {
		if (str[w] == 0) return w;
		if (str[w] == '\n') return w;
		n = SkipSpace(str,w);
		if (str[n] == 0) return n;
		if (str[n] == '\n') return n;
		n = NextWord(str,n);
		if (draw.StringWidthA(str,n) > pwidth) return w;
		w = n;
	}
}

/*$	XGStaticText::DrawLines
 *
 *		Draw the lines that are in this string
 */

void XGStaticText::DrawLines(XGDraw &draw, const char *str, 
							 short x, short y, short pwidth)
{
	short h;
	short w;
	
	h = draw.GetFontHeight();
	y += draw.GetFontAscent();
	for (;;) {
		w = CharWidth(draw,str,pwidth);
		draw.MoveTo(x,y);
		draw.DrawStringA(str,w);
		str += w;
		while (isspace(*str)) str++;
		if (*str == '\0') return;
		y += h;
	}
}

