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

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

  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++

#if OPT_WINOS

#include <XApplication.h>
#include <XDraw.h>
#include <XWindow.h>
#include <XDataUtil.h>
#include <XStringUtil.h>
#include <XMisc.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. This does make
 *	sure the version information is drawn over the picture in the
 *	upper right corner in Arial 8
 */

static LRESULT CALLBACK CopyrightWindowProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
{
	char buffer[256];
	PAINTSTRUCT ps;
	RECT r;
	SIZE size;
	int len;
	HFONT font;
	HGDIOBJ old;
	
	switch (msg) {
		case WM_PAINT:
			::BeginPaint(w,&ps);
			::DrawResBitmap(ps.hdc,128,0,0);		// draw copyright
			if (::XGGetAppVersion(buffer,sizeof(buffer))) {
				font = ::CreateFont(-::MulDiv(8,GetDeviceCaps(ps.hdc,LOGPIXELSY),72),
									0,
									0,
									0,
									FW_NORMAL,
									false,
									false,
									false,
									DEFAULT_CHARSET,
									OUT_DEFAULT_PRECIS,
									CLIP_DEFAULT_PRECIS,
									DEFAULT_QUALITY,
									DEFAULT_PITCH | FF_SWISS,
									"Ariel");
				if (font) old = ::SelectObject(ps.hdc,(HGDIOBJ)font);
				len = ::strlen(buffer);
				::GetClientRect(w,&r);
				::GetTextExtentPoint(ps.hdc,buffer,len,&size);
				
				::SetBkMode(ps.hdc,TRANSPARENT);
				::TextOut(ps.hdc,r.right-5-size.cx,r.top,buffer,len);
				if (font) {
					::SelectObject(ps.hdc,old);
					::DeleteObject((HGDIOBJ)font);
				}
			}
			::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);
}

#endif // OPT_WINOS
