/*
**************************************************************************
	Copyright (C) 1995

	Rolling Thunder Computing, Inc.
	25 Heartbreak Rd
	Ipswich MA 01938

  e-mail 73540.3430@compuserve.com
*****************************************************************************
*/


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

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


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

#include "main.h"
#include "resource.h"
#include "cadvsink.h"

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

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

LPDATAOBJECT lpd = NULL ;
DWORD dwAdviseConnection ;
DWORD LoadTime = 0, TransferTime = 0 ;
					
SYSTEMTIME st ;

CAdviseSink *pAptAdvSink = NULL, *pFreeAdvSink = NULL ;
IDataObject *pAptObj = NULL, *pFreeObj = NULL ;

                               
/*
*****************************************************************************
EXTERNS ofglobal variables defined in other files
*****************************************************************************
*/

extern HWND	hMainWnd ;
extern HINSTANCE hInst ;
extern HWND  hMdiClient ;


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

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

    PURPOSE:  Processes messages


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

static const GUID GUID_TimeData = 
{ 0xe7767a70, 0xa409, 0x11ce, { 0xad, 0xf, 0x0, 0x60, 0x8c, 0x86, 0xb8, 0x9c } };

UINT cfTimeData, cfThreadId ;

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


	switch (message)
	{

		case WM_CREATE:
		{
/*
Register private clipboard formats
*/

			cfTimeData = RegisterClipboardFormat ("TimeDataObject") ;
			cfThreadId = RegisterClipboardFormat ("ThreadId") ;
			return 0 ;
		}

/*
WM_COMMAND is a command from the application menu
*/

		case WM_COMMAND:
		{
                    
			switch (wParam)
			{
				case ID_WINDOW_TILEHORIZONTALLY:
				{
					return SendMessage (hMdiClient, WM_MDITILE, MDITILE_HORIZONTAL, 0) ;
				}
				case ID_WINDOW_TILEVERTICALLY:
				{
					return SendMessage (hMdiClient, WM_MDITILE, MDITILE_VERTICAL, 0) ;
				}
				case ID_WINDOW_NEWCASCADE:
				{
					return SendMessage (hMdiClient, WM_MDICASCADE, 0, 0) ;
				}
				case ID_DATAOBJECT_EXIT:
				{
					return SendMessage (hWnd, WM_CLOSE, 0, 0L) ;        
				}

				case ID_DATAOBJECT_CREATENONTHREADEDINPROC:
				case ID_DATAOBJECT_CREATEAPARTMENTTHREADEDINPORC:
				case ID_DATAOBJECT_CREATEFREETHREADEDINPROC:
				case ID_DATAOBJECT_CREATEBOTHINPROC:
				case ID_DATAOBJECT_CREATEAPARTMENTTHREADEDLOCAL:
				case ID_DATAOBJECT_CREATEFREETHREADEDLOCAL:
				case ID_DATAOBJECT_GETDATA:
				case ID_DATAOBJECT_RELEASE:
				case ID_DATAOBJECT_TIMETEST:
				case ID_DATAOBJECT_DADVISE:
				case ID_DATAOBJECT_DUNADVISE:
				{
					HWND hActive = (HWND) SendMessage (hMdiClient, WM_MDIGETACTIVE, 0, 0) ;
					return PostMessage (hActive, message, wParam, lParam) ;
				}
				default:
				{
					return DefFrameProc (hWnd, hMdiClient, message, wParam, lParam) ;
				}
			}
		}


		case WM_INITMENU:
		{
/*
Get active window
*/
			HMENU hMenu = GetMenu (hWnd) ;
			HWND hActive = (HWND) SendMessage (hMdiClient, WM_MDIGETACTIVE, 0, 0) ;

/*
Figure out if it's the apartment window or one of the free windows.
*/
			
			BOOL bTurnOn ;

			if (GetWindowLong (hActive, 0) == TRUE)
			{
				if (pFreeObj != NULL)
				{
					bTurnOn = TRUE ;
				}
				else
				{
					bTurnOn = FALSE ;
				}
			}
			else
			{
				if (pAptObj != NULL)
				{
					bTurnOn = TRUE ;
				}
				else
				{
					bTurnOn = FALSE ;
				}
			}

/*
Turn menu items on or off as appropriate
*/
			if (bTurnOn == TRUE)
			{
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATENONTHREADEDINPROC, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEAPARTMENTTHREADEDINPORC, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEFREETHREADEDINPROC, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEBOTHINPROC, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEAPARTMENTTHREADEDLOCAL, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEFREETHREADEDLOCAL, MF_GRAYED) ;
				
				EnableMenuItem (hMenu, ID_DATAOBJECT_GETDATA, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_TIMETEST, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_RELEASE, MF_ENABLED) ;

				if (GetWindowLong (hActive, 4) == 0)
				{
					EnableMenuItem (hMenu, ID_DATAOBJECT_DADVISE, MF_ENABLED) ;
					EnableMenuItem (hMenu, ID_DATAOBJECT_DUNADVISE, MF_GRAYED) ;
				}
				else
				{
 					EnableMenuItem (hMenu, ID_DATAOBJECT_DADVISE, MF_GRAYED) ;
					EnableMenuItem (hMenu, ID_DATAOBJECT_DUNADVISE, MF_ENABLED) ;
				}
			}
			else
			{
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATENONTHREADEDINPROC, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEAPARTMENTTHREADEDINPORC, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEFREETHREADEDINPROC, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEBOTHINPROC, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEAPARTMENTTHREADEDLOCAL, MF_ENABLED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_CREATEFREETHREADEDLOCAL, MF_ENABLED) ;
		
				EnableMenuItem (hMenu, ID_DATAOBJECT_GETDATA, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_TIMETEST, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_DADVISE, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_DUNADVISE, MF_GRAYED) ;
				EnableMenuItem (hMenu, ID_DATAOBJECT_RELEASE, MF_GRAYED) ;
			}

			return 0 ;
		}


/*
Shutdown time.  
*/

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

	
		case WM_DESTROY:
		{           
		  	PostQuitMessage (0) ;
		  	return 0 ;
		}
	}
       
	return DefFrameProc(hWnd, hMdiClient, message, wParam, lParam) ;
	
}


