/*	XGCheckButton
 *
 *		This is the command button. This throws up a command button
 *	object.
 */

/*  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 <XDraw.h>
#include <XButtons.h>
#include <XWindow.h>

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

/*	@F"XGCheckButton::XGCheckButton" @I
 *
 *		Construct me
 */

XGCheckButton::XGCheckButton(XGOwner *v, XGArgStream &s) : XGButton(v,s)
{
#if OPT_XWIN
	fValue = s.ReadInteger();
	s.ReadString(fName);
#endif

#if OPT_MACOS || OPT_WINOS
	long value;
	char buffer[256];
	
	value = s.ReadInteger();
	s.ReadString(buffer);
	InitControl(buffer,value);
#endif
}

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

XGCheckButton::~XGCheckButton()
{
}

/************************************************************************/
/*																		*/
/*	Construction														*/
/*																		*/
/************************************************************************/

#if OPT_MACOS

/*	XGCheckButton::InitControl
 *
 *		Initialize the control
 */

void XGCheckButton::InitControl(char *name, long value)
{
	Rect r;
	char buffer[256];
	
	if (IsVisible()) {
		r = GetContentRect();
		ViewToGlobal(&r);
	} else {
		r.left = -20,
		r.top = -20,
		r.right = -10,
		r.bottom = -10;
	}
	
	fControl = ::NewControl(GetRootWindow(),
							&r,
							c2pstr(StripAmperstand(buffer,name)),
							false,
							0,
							0,
							1,
							9,			// check button
							0L);
	SetValue(value);
}

#endif

#if OPT_WINOS

/*	XGCheckButton::InitControl
 *
 *		Initialize the control
 */

void XGCheckButton::InitControl(char *buffer,long value)
{
	Rect r;
	
	r = GetContentRect();
	ViewToGlobal(&r);
	
	fControl = ::CreateWindow("BUTTON",
							  buffer,
							  WS_CHILD | BS_CHECKBOX,
							  r.left,
							  r.top,
							  r.right-r.left,
							  r.bottom-r.top,
							  GetRootWindow(),
							  NULL,
							  _GInstance,
							  NULL);
	SetValue(value);
	
	::SetProp(fControl,_GControlClass,(HANDLE)this);
	if (fFont) {
		::SendMessage(fControl,WM_SETFONT,(WPARAM)(fFont->GetFont()),0L);
	}
}

#endif

/************************************************************************/
/*																		*/
/*	X Windows drawing													*/
/*																		*/
/************************************************************************/

#if OPT_XWIN

/*	XGCheckButton::DrawControl
 *
 *		Draw this control. 'true' means draw down
 */

void XGCheckButton::DrawControl(bool down)
{
	Rect r;
	short x,y;
	Rect s;
	
	XGDraw draw(this);
	if (fFont) {
		fFont->Attach();
		draw.SetFont(fFont);
	}
	
	r = GetContentRect();
	x = r.bottom - r.top - 2;
	if (x > 14) x = 14;
	s = r;
	InsetRect(&s,1,1);
	s.right = s.left + x;
	
	draw.Draw3DRect(s,KXGEBackground);
	x = s.right + 5;
	y = (r.bottom + r.top + draw.GetFontAscent())/2;
	
	if (IsEnabled()) {
		/*
		 *	Draw enabled control
		 */

		if (down) {
			draw.Draw3DRect(s,KXGEInButton);
		} else {
			draw.Draw3DRect(s,KXGEButton);
		}
	} else {
		draw.Draw3DRect(s,KXGEGrayFrame);
	}
	if (fValue) {
		::InsetRect(&s,3,3);
		if (!IsEnabled()) {
			draw.SetForeColor(KXGColorBtnShadow);
		}
		draw.MoveTo(s.left,s.top);
		draw.LineTo(s.right-1,s.bottom-1);
		draw.MoveTo(s.left,s.bottom-1);
		draw.LineTo(s.right-1,s.top);
	}
	
	draw.MoveTo(x,y);
	draw.ClipRect(&r);
	draw.DrawStringA(fName);
}

#endif
