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

	Filename:  MAINWIND.C

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


/*
*****************************************************************************
System header files
*****************************************************************************
*/

#include <windows.h>		    /* required for all Windows applications */


/*
*****************************************************************************
Application header files
*****************************************************************************
*/

#include "main.h"
#include "ctimedta.h"

/*
*****************************************************************************
Function prototype files
*****************************************************************************
*/

/*
*****************************************************************************
Global Variables defined here
*****************************************************************************
*/

int ObjectCount = 0, LockCount = 0 ;
SYSTEMTIME lt ;
CTimeData *lpFirstObj = NULL ;

HRESULT LastImpersonateResult ;
extern char LastImpersonatedName [], LastImpersonatedDomain[] ;  
DWORD g_AuthenticationLevel, g_ImpersonationLevel ;
                               
/*
*****************************************************************************
EXTERNS ofglobal variables defined in other files
*****************************************************************************
*/

extern HWND	hMainWnd ;
extern HINSTANCE hInst ;


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

    FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)

    PURPOSE:  Processes messages


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

long WINAPI MainWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


	switch (message)
	{

		case WM_CREATE:
		{   
			SetTimer (hWnd, 0, 1000, NULL) ;
   			return 0 ;	
		}

		case WM_TIMER:
		{
			GetLocalTime (&lt) ;
			InvalidateRect (hWnd, NULL, FALSE) ;

			if (lpFirstObj)
			{
				lpFirstObj->SendDataChangeToClient( )  ;
			}
			return 0 ;
		}
		

/*
Shutdown time.  
*/

		case WM_CLOSE:
		{
			DestroyWindow (hWnd) ;
			return 0 ;
		}

	
		case WM_DESTROY:
		{           
		  	PostQuitMessage (0) ;
		  	return 0 ;
		}
		
		case WM_PAINT:
		{
			PAINTSTRUCT ps ;
			HDC hDC ;
			char out [256] ;
			int length ;
			
			hDC = BeginPaint (hWnd, &ps) ;
			
			TextOut (hDC, 0, 0, "This window is displayed for demonstration purposes only.", 57) ;

			length = wsprintf (out, "Object Count == %d", ObjectCount) ;
			TextOut (hDC, 0, 20, out, length) ;
			length = wsprintf (out, "Server Lock Count == %d", LockCount) ;
			TextOut (hDC, 0, 40, out, length) ;

			length = wsprintf (out, "%2.2d:%2.2d:%2.2d", lt.wHour, lt.wMinute, lt.wSecond) ;
			TextOut (hDC, 0, 60, out, length) ;

			char UserName [256] ;
			DWORD size = sizeof (UserName) ;
			GetUserName (UserName, &size) ;

			length = wsprintf (out, "Main app running as user: %s", UserName) ;
			TextOut (hDC, 0, 90, out, length) ;
			
			length = wsprintf (out, "Last call to CoImpersonate client hr == 0x%x", LastImpersonateResult) ;
			TextOut (hDC, 0, 110, out, length) ;

			length = wsprintf (out, "Last GetData call processed as domain: %s user: %s", LastImpersonatedDomain, LastImpersonatedName) ;
			TextOut (hDC, 0, 130, out, length) ;

			length = wsprintf (out, "Last GetData call auth level = 0x%x imp level = 0x%x", g_AuthenticationLevel, g_ImpersonationLevel) ;
			TextOut (hDC, 0, 150, out, length) ;

			EndPaint (hWnd, &ps) ;
			return 0 ;
		}

	}
       
	return DefWindowProc(hWnd, message, wParam, lParam) ;
	
}


void SignalObjectDestroyed ( ) 
{
	ObjectCount -- ;

	if (ObjectCount == 0 && LockCount == 0) 
	{
		PostMessage (hMainWnd, WM_CLOSE, 0, 0) ;
	}
	else
	{
		InvalidateRect (hMainWnd, NULL, TRUE) ;
	}

}
