/*	XGGroupBox.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>

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

/*$	XGGroupBox::XGGroupBox
 *
 *		Initialize me
 */

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

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


/*$	XGGroupBox::~XGGroupBox
 *
 *		Delete me
 */

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

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

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

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

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

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

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

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

void XGGroupBox::DoDrawView(XRect)
{
	XRect r;
	XRect s;
	short i;
	
	r = GetContentRect();

	XGDraw draw(this);
	if (fFontPtr) {
		fFontPtr->Attach();
		draw.SetFont(fFontPtr);
	}
	
	// Calculate the rectangle things go into
	s.left = r.left + 8;
	s.right = r.right - 8;
	i = s.left + draw.StringWidthA(fString.Get());
	if (s.right > i) s.right = i;
	s.top = r.top;
	s.bottom = s.top + draw.GetFontHeight();
	r.top += draw.GetFontAscent() - 3;
	
	// Draw the box
	if (!IsEnabled()) {
		draw.MoveTo(s.left-3,r.top);
		draw.LineTo(r.left,r.top);
		draw.LineTo(r.left,r.bottom-1);
		draw.LineTo(r.right-1,r.bottom-1);
		draw.LineTo(r.right-1,r.top);
		draw.LineTo(s.right+3,r.top);
	} else {
		draw.SetForeColor(KXGColorBtnHilite);
		draw.MoveTo(s.left-3,r.top+1);
		draw.LineTo(r.left+1,r.top+1);
		draw.LineTo(r.left+1,r.bottom-1);
		draw.LineTo(r.right-1,r.bottom-1);
		draw.LineTo(r.right-1,r.top+1);
		draw.LineTo(s.right+4,r.top+1);

		draw.SetForeColor(KXGColorBtnShadow);
		draw.MoveTo(s.left-4,r.top);
		draw.LineTo(r.left,r.top);
		draw.LineTo(r.left,r.bottom-2);
		draw.LineTo(r.right-2,r.bottom-2);
		draw.LineTo(r.right-2,r.top);
		draw.LineTo(s.right+3,r.top);
	}

	if (!IsEnabled()) {
		draw.SetForeColor(KXGColorBtnShadow);
	} else {
		draw.SetForeColor(KXGColorBlack);
	}

	// Clip and draw the text
	r.right = s.right;
	r.top = s.top;
	draw.ClipRect(&r);
	draw.MoveTo(s.left,s.top+draw.GetFontAscent());
	draw.DrawStringA(fString.Get());
}

/************************************************************************/
/*																		*/
/*	Internal Initialization												*/
/*																		*/
/************************************************************************/

/*$	XGGroupBox::Init
 *
 *		Initialization central
 */

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

