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

/**********************************************************************

  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>

#if OPT_MACOS

#include <XApplication.h>
#include <XDataUtil.h>
#include <XStringUtil.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)
{
	XRect 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);
}

/*	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) {
		if (_GHasWNETrap) {
			WaitNextEvent(everyEvent,&e,1,NULL);
		} else {
			SystemTask();
			GetNextEvent(everyEvent,&e);
		}
		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;
	}
	
	/*
	 *	Done.
	 */
	
	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;
	RedrawWindow();
	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();
}

#endif // OPT_MACOS