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

/*$	XGRadioButton::XGRadioButton
 *
 *		Construct me
 */

XGRadioButton::XGRadioButton(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
}

/*$	XGRadioButton::~XGRadioButton
 *
 *		Delete me
 */

XGRadioButton::~XGRadioButton()
{
}

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

#if OPT_MACOS

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

void XGRadioButton::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,
							10,			// push button
							0L);
	SetValue(value);
}

#endif

#if OPT_WINOS

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

void XGRadioButton::InitControl(char *buffer,long value)
{
	XRect r;
	
	r = GetContentRect();
	ViewToGlobal(&r);
	
	fControl = ::CreateWindow("BUTTON",
							  buffer,
							  WS_CHILD | BS_RADIOBUTTON,
							  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

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

void XGRadioButton::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.SetForeColor(KXGColorBtnFace);
	draw.PaintOval(s);					// fill with background
	
	x = s.right + 5;
	y = (r.bottom + r.top + draw.GetFontAscent())/2;
	
	if (IsEnabled()) {
		/*
		 *	Draw enabled control
		 */

		::InsetRect(&s,1,1);
		if (down) {
			draw.SetForeColor(KXGColorBtnShadow);
			draw.FrameArc(s,225,180);
			draw.SetForeColor(KXGColorWhite);
			draw.FrameArc(s,45,180);
		} else {
			draw.SetForeColor(KXGColorWhite);
			draw.FrameArc(s,225,180);
			draw.SetForeColor(KXGColorBtnShadow);
			draw.FrameArc(s,45,180);
		}
		::InsetRect(&s,-1,-1);
		draw.SetForeColor(KXGColorBlack);
	} else {
		draw.SetForeColor(KXGColorBtnShadow);
	}
	draw.FrameOval(s);
	if (fValue) {
		::InsetRect(&s,3,3);
		draw.PaintOval(s);
	}
	
	draw.MoveTo(x,y);
	draw.ClipRect(&r);
	draw.DrawStringA(fName);
}

#endif
