/*	Copyright_win.cpp
 *
 *		Copyright display routines (stubs)
 */

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

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

extern char _GCopyrightClass[];
static bool GRegFlag = false;
static HWND GCopyright;
static DWORD GTimer;

/************************************************************************/
/*																		*/
/*	Copyright window procedure											*/
/*																		*/
/************************************************************************/

/*	CopyrightWindowProc
 *
 *		This is a very brain dead callback routine
 */

static LRESULT CALLBACK CopyrightWindowProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
{
	PAINTSTRUCT ps;
	
	switch (msg) {
		case WM_PAINT:
			::BeginPaint(w,&ps);
			::TextOut(ps.hdc,10,20,"Test",4);
			::DrawResBitmap(ps.hdc,128,0,0);		// draw copyright
			::EndPaint(w,&ps);
			break;
		default:
			return ::DefWindowProc(w,msg,wp,lp);
	}
	return 0;
}

/*	RegisterCopyrightWindow
 *
 *		Register the copyright window. If it already is, skip
 */

static void RegisterCopyrightWindow(void)
{
	WNDCLASS wc;
	
	if (GRegFlag) return;
	GRegFlag = true;
	
	wc.style = 0;
	wc.lpfnWndProc = CopyrightWindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = _GInstance;
	wc.hIcon = NULL;
	wc.hCursor = NULL;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = _GCopyrightClass;
	RegisterClass(&wc);
}


/************************************************************************/
/*																		*/
/*	Copyright window opening											*/
/*																		*/
/************************************************************************/

/*	CreateCopyrightWindow
 *
 *		This creates the copyright window and copies the copyright
 *	bitmap into it. If no bitmap exists, this does nothing
 */

static HWND CreateCopyrightWindow(void)
{
	HRSRC find;
	HGLOBAL h;
	LPBITMAPINFO header;
	int sx,sy;
	int x,y;
	HWND w;
	
	/*
	 *	Make sure copyright window was registered
	 */
	
	::RegisterCopyrightWindow();
	
	/*
	 *	Explicitly load DIB bitmap and note the size of this thing
	 */
	
	find = ::FindResource(_GInstance,MAKEINTRESOURCE(128),RT_BITMAP);
	if (find == NULL) return NULL;
	h = ::LoadResource(_GInstance,find);
	if (h == NULL) return NULL;
	header = (LPBITMAPINFO)::LockResource(h);
	
	sx = header->bmiHeader.biWidth;
	sy = header->bmiHeader.biHeight;
	
	UnlockResource(h);
	FreeResource(h);
	
	/*
	 *	Now create a window that is topmost to contain the copyright
	 *	notice
	 */
	
	sx += ::GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
	sy += ::GetSystemMetrics(SM_CYFIXEDFRAME) * 2;
	x = (::GetSystemMetrics(SM_CXSCREEN) - sx)/2;
	y = (::GetSystemMetrics(SM_CYSCREEN) - sy)/2;
	w = ::CreateWindowEx(WS_EX_TOPMOST | WS_EX_DLGMODALFRAME,
					     _GCopyrightClass,
					     "Copyright",
					     WS_POPUP | WS_VISIBLE,
					     x,y,
					     sx,sy,
					     NULL,
					     NULL,
					     _GInstance,
					     NULL);

	if (w) {
		::ShowWindow(w,SW_SHOW);
		::UpdateWindow(w);
	}
	return w;
}

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

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

void XGAppCore::OpenCopyright(void)
{
	GCopyright = CreateCopyrightWindow();
	GTimer = GetTickCount() + 3000;				// 3 second delay
}

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

void XGAppCore::CloseCopyright(void)
{
	if (GCopyright) {
		while (GTimer > GetTickCount()) ;
		if (GCopyright) ::DestroyWindow(GCopyright);
	}
}

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

void XGAppCore::ShowCopyright(void)
{
	MSG msg;
	
	GCopyright = CreateCopyrightWindow();
	if (!GCopyright) return;

	/*
	 *	Now spin until something interesting happens
	 */
	
	for (;;) {
		::GetMessage(&msg,GCopyright,0,0);
		if ((msg.message == WM_LBUTTONDOWN) || 
				(msg.message == WM_RBUTTONDOWN) ||
				(msg.message == WM_MBUTTONDOWN) || 
				(msg.message == WM_KEYDOWN)) break;
		::DispatchMessage(&msg);
	}
	
	::DestroyWindow(GCopyright);
}
