/*	Copyright_mac.cpp
 *
 *		This handles the default copyright mechanism
 */

/*  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 <XApplication.h>
#include <XDataUtil.h>

/************************************************************************/
/*																		*/
/*	Copyright Globals													*/
/*																		*/
/************************************************************************/

static long			GOpenTime;				// When was this opened?
static WindowPtr	GWindow;				// Copyright window
static PicHandle	GPicture;				// window picture

/************************************************************************/
/*																		*/
/*	Internal Structures													*/
/*																		*/
/************************************************************************/

/*	VersRecord
 *
 *		The 'vers' record
 */

struct VersRecord {
	long			version;
	short			region;
	unsigned char	verstr[2];
};


/************************************************************************/
/*																		*/
/*	Internal Routines													*/
/*																		*/
/************************************************************************/

/*	DrawVersion
 *
 *		Get the version string (if it exists) and draw it. Also look for
 *	the build count (if it exists) and draw that appended to the version
 *	string
 */

static void DrawVersion(void)
{
#if optional_version
	Rect r;
	VersRecord **v;
	unsigned char buffer[256];
	unsigned char buffer2[32];
	long **l;
	
	/*
	 *	Get version string
	 */
	
	v = (VersRecord **)Get1Resource('vers',1);
	if (v == NULL) return;
	HLock((Handle)v);
	pstrcpy(buffer,(**v).verstr);
	HUnlock((Handle)v);
	ReleaseResource((Handle)v);
	
	/*
	 *	Get the BCtr count
	 */
	
	l = (long **)Get1Resource('BCtr',128);
	if (l) {
		pstrcat(buffer,"\p (");
		NumToString(**l,buffer2);
		pstrcat(buffer,buffer2);
		pstrcat(buffer,"\p)");
	}
	
#ifdef __powerc
	pstrcat(buffer,"\p ppc");
#else
	pstrcat(buffer,"\p 68k");
#endif
	
	/*
	 *	Now draw it
	 */	
	
	r = GWindow->portRect;
	TextFont(1);
	TextSize(9);
	MoveTo(r.right-5-StringWidth(buffer),12);
	DrawString(buffer);
#endif // optional_version
}

/*	RedrawWindow
 *
 *		Redraw the window contents
 */

static void RedrawWindow(void)
{
	Rect r;
	
	BeginUpdate(GWindow);
	SetPort(GWindow);
	r = GWindow->portRect;
	r.right -= r.left;
	r.bottom -= r.top;
	r.left = 0;
	r.top = 0;
	ClipRect(&r);
	DrawPicture(GPicture,&r);
	DrawVersion();
	EndUpdate(GWindow);
}

/*	RunEventLoop
 *
 *		Run the event loop until the mouse or keyboard is clicked
 */

void XGAppCore::RunEventLoop(void)
{
	EventRecord e;
	bool done = false;
	WindowPtr w;
	
	while (!done) {
		WaitNextEvent(everyEvent,&e,1,NULL);
		switch (e.what) {
			case updateEvt:
				w = (WindowPtr)(e.message);
				if (w == GWindow) RedrawWindow();
				else DoUpdate(w);
				break;
			case activateEvt:
//				w = (WindowPtr)(e.message);
//				if (w != GWindow) {
//					DoActivate(w,(e.modifiers & 1) ? true : false);
//				}
				break;
			case keyDown:
			case mouseDown:
				done = true;
				break;
		}
	}
}

/*	OpenCopyrightWindow
 *
 *		Open the copyright window, loading the 'PICT' #128 resource
 */

static bool OpenCopyrightWindow(void)
{
	Rect r;
	GrafPtr screen;
	Rect s;
	
	if (GWindow) return false;
	
	/*
	 *	Get picture
	 */
	
	GPicture = GetPicture(128);
	if (GPicture == NULL) return false;
	
	/*
	 *	Calculate the centered window location
	 */
	
	r = (**GPicture).picFrame;
	GetWMgrPort(&screen);
	s = screen->portRect;
	
	s.top = (s.top + s.bottom - (r.bottom - r.top))/2;
	s.bottom = s.top + (r.bottom - r.top);
	s.left = (s.left + s.right - (r.right - r.left))/2;
	s.right = s.left + (r.right - r.left);
	
	
	/*
	 *	Open window
	 */
	
	GWindow = NewCWindow(NULL,
					     &s,
					     "\p",
					     true,
					     plainDBox,
					     (WindowPtr)-1,
					     false,
					     0);
	if (GWindow == NULL) {
		ReleaseResource((Handle)GPicture);
		GPicture = NULL;
		return false;
	}
	
	/*
	 *	And draw into it the picture
	 */
	
	SetPort(GWindow);
	r = GWindow->portRect;
	r.right -= r.left;
	r.bottom -= r.top;
	r.left = 0;
	r.top = 0;
	ClipRect(&r);
	DrawPicture(GPicture,&r);
	DrawVersion();
	return true;
}

/*	CloseCopyrightWindow
 *
 *		Close the copyright window
 */

static void CloseCopyrightWindow(void)
{
	if (!GWindow) return;
	DisposeWindow(GWindow);
	ReleaseResource((Handle)GPicture);
	GWindow = NULL;
	GPicture = NULL;
}


/************************************************************************/
/*																		*/
/*	Temporary copyright window											*/
/*																		*/
/************************************************************************/

/*	XGAppCore::OpenCopyright
 *
 *		Open the copyright window
 */

void XGAppCore::OpenCopyright(void)
{
	if (!OpenCopyrightWindow()) return;
	GOpenTime = TickCount() + 120;			// 2 seconds
}

/*	XGAppCore::CloseCopyright
 *
 *		Delay and then close. This guarentees a minimum 2 second delay
 */

void XGAppCore::CloseCopyright(void)
{
	while (GOpenTime > TickCount()) ;
	CloseCopyrightWindow();
}

/*	XGAppCore::ShowCopyright
 *
 *		Show the copyright and wait until mouse or keyboard events
 */

void XGAppCore::ShowCopyright(void)
{
	if (!OpenCopyrightWindow()) return;
	RunEventLoop();
	CloseCopyrightWindow();
}
