/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540.3430
*	Web: http://www.rollthunder.com
*
*	File Name: MAINWIND.CPP
*
*	Purpose: Main window response function
*
*********************************************************************
*/


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

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


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

#include "resource.h"
#include "cdroptgt.h"   


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

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

LPCDROPTARGET lpDropTarget ;
char DisplayText [128] = {0x00};          
int xText, yText ;

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

extern HWND	hMainWnd ;



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

    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:
		{   


/*
STEP 4
Set up a drop target.  Instantiate a CDropTarget object and AddRef( ) it
to keep it in memory.  Lock it. Register it with OLE.  
*/

								
			return 0 ;
		
		}
		

/*
STEP 2  Fetch Clipboard contents as data object and find out if it contains
text.  Enable or gray menu item accordingly.

Menu is starting to be displayed. Ask OLE for the clipboard contents packaged
into a data object.  Then use the object's QueryGetData( ) method to see
if it contains data that can be rendered as CF_TEXT in and HGLOBAL medium.
If so, enable the menu item, otherwise gray it out.	Remember to release the object
when you are finished with it.
*/

		case WM_INITMENU:
       	{   
       	    		
			if (1/* DATA NOT AVAILABLE AS TEXT */)
			{            
       	    	EnableMenuItem ((HMENU)wParam, ID_DEMO_PASTETEXT, MF_GRAYED) ;
       		}      
       		else
       		{   
       			EnableMenuItem ((HMENU)wParam, ID_DEMO_PASTETEXT, MF_ENABLED) ;
       		} 
       		
       	       		
       		return 0 ;
       	}

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

		case WM_COMMAND:
		{
                    
			switch (wParam)
			{
				case ID_DEMO_EXIT:
				{
					SendMessage (hWnd, WM_CLOSE, 0, 0L) ;        
					return 0 ;
				}
				
/*

STEP 3 Fetch Clipboard contents as data object and get the text out of it.

User wants to paste clipboard text into the app.  Get the clipboard contents
packaged as a data object.  Use the data object's GetData( ) method to 
retrieve the data from the object. Release the storage medium in which the
data was transferred, and also the data object itself.
*/
				case ID_DEMO_PASTETEXT:
				{   
					
					
					xText = 0 ;
					yText = 0 ;	
					InvalidateRect (hMainWnd, NULL, TRUE) ;
			        					
					return 0 ;
				
				}  
				
				
			}

			return 0 ;

		}




/*
STEP 4 Revoke, unlock, and release drop target

Shutdown time.  Revoke the drag and drop operation and Release( ) the drop
target object.  
*/

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


	
		case WM_DESTROY:
		{           
		  	PostQuitMessage (0) ;
		  	return 0 ;
		}
		
		case WM_PAINT:
		{
			PAINTSTRUCT ps ;
			HDC hDC ;
			
			hDC = BeginPaint (hWnd, &ps) ;
			
			TextOut (hDC, xText, yText, DisplayText, lstrlen(DisplayText)) ;

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

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


