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

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

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

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

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

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

XGCommandButton::~XGCommandButton()
{
}

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

#if OPT_MACOS

/*	XGCommandButton::DoDrawView
 *
 *		Handle the drawing
 */

void XGCommandButton::DoDrawView(Rect)
{
	Rect r;
	
	if (fControl) {
		XGDraw draw(this,false);
		if (fFont) {
			fFont->Attach();
			draw.SetFont(fFont);
		}
		
		if (IsEnabled() && IsActive()) {
			/*
			 *	Enabled
			 */
			
			::HiliteControl(fControl,0);
		} else {
			::HiliteControl(fControl,255);
		}
		::ShowControl(fControl);
		::Draw1Control(fControl);
		
		if (fDefault) {
			r = (**fControl).contrlRect;
			::PenSize(3,3);
			::InsetRect(&r,-4,-4);
			::FrameRoundRect(&r,16,16);
		}
	}
}

#endif

/*	XGCommandButton::DoMoveView
 *
 *		Move the view.
 */

void XGCommandButton::DoMoveView()
{
#if OPT_MACOS || OPT_WINOS
	Rect r;
	
	if (!fControl) return;
	r = GetContentRect();
	ViewToGlobal(&r);
	::InsetRect(&r,4,4);
	
#if OPT_MACOS
	XGDraw draw(this,false);
	if (fFont) {
		fFont->Attach();
		draw.SetFont(fFont);
	}
	::HideControl(fControl);
	::MoveControl(fControl,r.left,r.top);
	::SizeControl(fControl,r.right-r.left,r.bottom-r.top);
#endif

#if OPT_WINOS
	::SetWindowPos(fControl,
				   NULL,
				   r.left,
				   r.top,
				   r.right-r.left,
				   r.bottom-r.top,
				   SWP_NOZORDER | SWP_HIDEWINDOW);
#endif
#endif
}

/*	XGCommandButton::SetDefault
 *
 *		Set the default button attribute for this thing
 */

void XGCommandButton::SetDefault(bool f)
{
	if (fDefault == f) return;
	
	fDefault = f;
	InvalView();
	
#if OPT_WINOS
	::SendMessage(fControl,
				  BM_SETSTYLE,
				  (WPARAM)(f ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON),
				  0);
#endif
}

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

#if OPT_MACOS

/*	XGCommandButton::InitControl
 *
 *		Initialize the control
 */

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

#endif

#if OPT_WINOS

/*	XGCommandButton::InitControl
 *
 *		Initialize the control
 */

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

#endif

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

#if OPT_XWIN

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

void XGCommandButton::DrawControl(bool down)
{
	Rect r;
	short x,y;
	
	XGDraw draw(this);
	if (fFont) {
		fFont->Attach();
		draw.SetFont(fFont);
	}
	
	r = GetContentRect();
	x = (r.left + r.right - draw.StringWidthA(fName))/2;
	y = (r.bottom + r.top + draw.GetFontAscent())/2;
	
	/* Draw default box */
	
	if (IsEnabled() && fDefault) {
		::InsetRect(&r,1,1);
		draw.Draw3DRect(r,KXGEFrame);
		::InsetRect(&r,2,2);
	} else {
		::InsetRect(&r,3,3);
	}
	
	/* Draw control */
	
	if (IsEnabled()) {

		/*
		 *	Draw enabled control
		 */
		
		if (down) {
			draw.Draw3DRect(r,KXGEInButton);
			::InsetRect(&r,2,2);
			draw.Draw3DRect(r,KXGEInset);
			x++;
			y++;
		} else {
			draw.Draw3DRect(r,KXGEButton);
			::InsetRect(&r,2,2);
			draw.Draw3DRect(r,KXGEOutset);
		}
		
		::InsetRect(&r,1,1);
		draw.Draw3DRect(r,KXGEBackground);
		draw.ClipRect(&r);
		draw.MoveTo(x,y);
		draw.DrawStringA(fName);
	} else {
		/*
		 *	Disabled control
		 */
		
		draw.Draw3DRect(r,KXGEGrayFrame);
		::InsetRect(&r,1,1);
		draw.Draw3DRect(r,KXGEBackground);
		draw.ClipRect(&r);
		draw.MoveTo(x,y);
		draw.SetForeColor(KXGColorBtnShadow);		// dimmed gray text
		draw.DrawStringA(fName);
	}
}

#endif
