
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	FILE	: Splash.cpp
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

#include "stdinc.h"
#include "splash.h"


CSplashScreen::CSplashScreen(HINSTANCE hInstance, int SplashBmpID, UINT minTime, int FontHeight, RECT* TextRect, char* FontName, RGBQUAD* Clr  )
{
	CanDelete = 0;

	R = G = B = 0;
	if(Clr)
	{
		R = Clr->rgbRed;
		G = Clr->rgbGreen;
		B = Clr->rgbBlue;
	}
	m_TextHeight = FontHeight;
	szSplashString = NULL;
	if(FontName) strcpy(m_FontName = new char[ strlen(FontName) +1],FontName);
	else strcpy(m_FontName = new char[ 6 ], "Arial");

	HMODULE app = GetModuleHandle(NULL);
	hDib =LoadResource(app, FindResource( app, MAKEINTRESOURCE(SplashBmpID), RT_BITMAP));
	pDib = (BITMAPINFO*)LockResource(hDib);
	pal = CreatePaletteHandle(pDib->bmiColors);
	if(TextRect) r = *TextRect;
	else
	{
		r.left = r.top = 0;
		r.right = pDib->bmiHeader.biWidth;
		r.bottom = pDib->bmiHeader.biHeight;
	}
	char ptr[sizeof(DLGTEMPLATE) + 16];
	memset( ptr, 0, sizeof(DLGTEMPLATE) + 16);
	((DLGTEMPLATE*)ptr)->style =
#ifndef _DEBUG
		DS_SYSMODAL|
#endif
		WS_POPUP|WS_DLGFRAME|WS_VISIBLE|DS_SETFOREGROUND;
	StartDialog = CreateDialogIndirectParam( hInstance, (DLGTEMPLATE*)ptr, NULL,(DLGPROC)StartDlgProc, (LPARAM)this);
	SetTimer( StartDialog, (UINT) this, minTime, NULL);
}

CSplashScreen::~CSplashScreen()
{
	DestroyWindow(StartDialog);
	delete[] m_FontName;
	if(szSplashString)
		delete[] szSplashString;
	FreeResource(hDib);
	DeleteObject(pal);
}

BOOL CALLBACK CSplashScreen::StartDlgProc( HWND  hwnd, UINT msg, WPARAM  wp, LPARAM  lp )
{
  switch(msg)
  {
  case WM_TIMER:
	 {
		CSplashScreen* sp = (CSplashScreen*) wp;
		if(sp->CanDelete)
			delete sp;
		else
			sp->CanDelete = TRUE;
	 }
	 break;
  case WM_INITDIALOG:
    {
		SetProp( hwnd, "OWNER", (HANDLE)lp);
		CSplashScreen* S = (CSplashScreen*) lp;

		int xb = GetSystemMetrics(SM_CXFIXEDFRAME);
		int yb = GetSystemMetrics(SM_CYFIXEDFRAME);
		SetWindowPos
			(hwnd,HWND_TOP,//HWND_TOPMOST,
			(GetSystemMetrics(SM_CXSCREEN)-S->pDib->bmiHeader.biWidth)/2-xb,
			(GetSystemMetrics(SM_CYSCREEN)- S->pDib->bmiHeader.biHeight)/2-yb,
			S->pDib->bmiHeader.biWidth+2*xb,S->pDib->bmiHeader.biHeight+2*yb,0);

		HDC dc = GetDC( hwnd );
		S->hfont = CreateFont
			(
			-MulDiv( S->m_TextHeight, 72, GetDeviceCaps(dc, LOGPIXELSY)),
			0,
			0,
			0,
			FW_DONTCARE,
			FALSE,
			FALSE,
			FALSE,
			DEFAULT_CHARSET,
			OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			DEFAULT_QUALITY,
			DEFAULT_PITCH|FF_DONTCARE,
			S->m_FontName
			);
		ReleaseDC( hwnd, dc );
    }
    break;
  case WM_PAINT:
    {
      CSplashScreen* S = (CSplashScreen*)GetProp( hwnd, "OWNER");
      BITMAPINFOHEADER bih = S->pDib->bmiHeader;
      PAINTSTRUCT ps;
      HDC dc = BeginPaint(hwnd, &ps);
      HPALETTE oldpal = SelectPalette(dc,S->pal,FALSE);
      RealizePalette(dc);
      SetDIBitsToDevice(dc,0,0,bih.biWidth,bih.biHeight,0,0,0,bih.biHeight,S->pDib->bmiColors+256,S->pDib,DIB_RGB_COLORS);
	  if(S->szSplashString)
	  {
		  HFONT hOldF = SelectObject( dc, S->hfont);
		  SetBkMode( dc, TRANSPARENT );
		  SetTextColor( dc, PALETTERGB( S->R,S->G,S->B ));
		  DrawText( dc, S->szSplashString, strlen(S->szSplashString), &S->r, DT_WORDBREAK|DT_EXPANDTABS);
 		  SelectObject( dc, hOldF);
	  }
      SelectPalette(dc,oldpal,FALSE);
      EndPaint(hwnd,&ps);
    }
    break;
  case WM_DESTROY:
	  {
		  CSplashScreen* S = (CSplashScreen*)RemoveProp( hwnd, "OWNER");
		  DeleteObject(S->hfont);
	  }
    break;
  default: return 0;
  }
  return 1;
};	

void CSplashScreen::RGBQuadToPaletteEntry(RGBQUAD rgbq[256], PALETTEENTRY pe[256], int flag)
{	for(int i=0; i<256; i++)
	{	pe[i].peRed = rgbq[i].rgbRed;
		pe[i].peGreen = rgbq[i].rgbGreen;
		pe[i].peBlue = rgbq[i].rgbBlue;
		pe[i].peFlags = flag;
	}
}

HPALETTE CSplashScreen::CreatePaletteHandle(RGBQUAD rgbq[256], int flag )
{	
	if(!rgbq) return NULL;
	char dummy[sizeof(LOGPALETTE) + 256*sizeof(PALETTEENTRY)];
	((LOGPALETTE*)dummy)->palVersion = 0x300;
	((LOGPALETTE*)dummy)->palNumEntries = 256;
	RGBQuadToPaletteEntry(rgbq, ((LOGPALETTE*)dummy)->palPalEntry,	flag);
	return CreatePalette((LOGPALETTE*)dummy);
} 

void CSplashScreen::SetText( char* szText)
{
	if(szSplashString) delete[] szSplashString;
	if( szText ) strcpy( szSplashString = new char[lstrlen(szText)+1], szText);
	else szSplashString = NULL;
	InvalidateRect( StartDialog, &r, FALSE);
	UpdateWindow(StartDialog);
}

