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

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

/*$	XGCommandButton::XGCommandButton
 *
 *		Construct me
 */

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

/*$	XGCommandButton::~XGCommandButton
 *
 *		Delete me
 */

XGCommandButton::~XGCommandButton()
{
}

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

#if OPT_MACOS

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

void XGCommandButton::DoDrawView(XRect)
{
	XRect 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);
			Rect mr = r.MacRect();
			::FrameRoundRect(&mr,16,16);
		}
	}
}

#endif

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

void XGCommandButton::DoMoveView()
{
#if OPT_MACOS || OPT_WINOS
	XRect 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
}

/*$	XGCommandButton::DoShowHide
 *
 *		This is called when the control is to be hidden or
 *	shown. This only needs to hide the control; the call to
 *	DoDrawView() will show the control if needed.
 */

void XGCommandButton::DoShowHide()
{
#if OPT_MACOS
	if (!IsVisible() && fControl) {
		XGDraw draw(this,false);
		if (fFont) {
			fFont->Attach();
			draw.SetFont(fFont);
		}
		::HideControl(fControl);
		::MoveControl(fControl,-20,-20);
		::SizeControl(fControl,10,10);
	} else {
		if (fControl) {
			XRect r;
			
			r = GetContentRect();
			::InsetRect(&r,4,4);
			ViewToGlobal(&r);
			::MoveControl(fControl,r.left,r.top);
			::SizeControl(fControl,r.right-r.left,r.bottom-r.top);
		}
	}
#endif

#if OPT_WINOS
	if (!IsVisible() && fControl) {
		::ShowWindow(fControl,SW_HIDE);
	}
#endif

	XGView::DoShowHide();
}

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

#if OPT_MACOS

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

void XGCommandButton::InitControl(char *name)
{
	XRect 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);
	Rect mr = r.MacRect();
	fControl = ::NewControl(GetRootWindow(),
							&mr,
							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)
{
	XRect 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)(XGView *)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)
{
	XRect 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
