/*	XGRadioButton
 *
 *		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"XGRadioButton::XGRadioButton" @I
 *
 *		Construct me
 */

XGRadioButton::XGRadioButton(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"XGRadioButton::~XGRadioButton" @I
 *
 *		Delete me
 */

XGRadioButton::~XGRadioButton()
{
}

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

#if OPT_MACOS

/*	@F"XGRadioButton::InitControl" @I
 *
 *		Initialize the control
 */

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

#endif

#if OPT_WINOS

/*	@F"XGRadioButton::InitControl" @I
 *
 *		Initialize the control
 */

void XGRadioButton::InitControl(char *buffer,long value)
{
	Rect 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)this);
	if (fFont) {
		::SendMessage(fControl,WM_SETFONT,(WPARAM)(fFont->GetFont()),0L);
	}
}

#endif

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

#if OPT_XWIN

/*	@F"XGRadioButton::DrawControl" @I
 *
 *		Draw this control. 'true' means draw down
 */

void XGRadioButton::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.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
