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

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

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

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

/*$	XGCheckButton::XGCheckButton
 *
 *		Construct me
 */

XGCheckButton::XGCheckButton(XGView *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
}

/*$	XGCheckButton::~XGCheckButton
 *
 *		Delete me
 */

XGCheckButton::~XGCheckButton()
{
}

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

#if OPT_MACOS

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

void XGCheckButton::InitControl(char *name, long value)
{
	XRect r;
	char buffer[256];
	
	if (IsVisible()) {
		r = GetContentRect();
		ViewToGlobal(&r);
	} else {
		r.left = -20,
		r.top = -20,
		r.right = -10,
		r.bottom = -10;
	}
	
	Rect mr = r.MacRect();
	fControl = ::NewControl(GetRootWindow(),
							&mr,
							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)
{
	XRect 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)(XGView *)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)
{
	XRect r;
	short x,y;
	XRect 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
