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

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#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											*/
/*																		*/
/************************************************************************/

/*	@F"XGGroupBox::XGGroupBox" @I
 *
 *		Initialize me
 */

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

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


/*	@F"XGGroupBox::~XGGroupBox" @I
 *
 *		Delete me
 */

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

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

/*	@F"XGGroupBox::GetTitle" @I
 *
 *		Get the text contents of this
 */

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

/*	@F"XGGroupBox::SetTitle" @I
 *
 *		Set the text and force a redraw
 */

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

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

/*	@F"XGGroupBox::DoDrawView" @I
 *
 *		Draw this thing
 */

void XGGroupBox::DoDrawView(Rect)
{
	Rect r;
	Rect 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;
}

