/*
*********************************************************************
*
*	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 */
#include <comdef.h>


/*
*****************************************************************************
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:
		{   
			HRESULT hr ;

					                                       
/*
Set up a drop target.  Instantiate a CDropTarget object and CoLock it
to keep it in memory.  Register it with OLE.  
*/

			lpDropTarget = new CDropTarget ( ) ;     
           	hr = CoLockObjectExternal (lpDropTarget, TRUE, FALSE) ;     
            
			hr = RegisterDragDrop (hWnd, lpDropTarget) ;
					
			return 0 ;
		
		}
		
/*
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.
*/

		case WM_INITMENU:
       	{   

/*
Get a data object pointer to the current Clipboard contents
*/
			IDataObjectPtr lpd ;
       		HRESULT hr ;  

       		hr = OleGetClipboard (&lpd) ;  
		
/*
Fill out a FORMATETC structure, see if the Clipboard data object
contains text.  Gray out menu item if it doesn't, or if Clipboard
is empty (returned data object pointer is NULL).
*/

       		FORMATETC fe ; 

       		fe.cfFormat = CF_TEXT ;
    		fe.ptd = NULL ;
    		fe.tymed = TYMED_HGLOBAL ;  
    		fe.dwAspect = DVASPECT_CONTENT ; 
    		fe.lindex = -1 ;
    		
      		
			if (lpd == NULL ||  lpd->QueryGetData (&fe) != S_OK)
			{            
       	    	EnableMenuItem ((HMENU)wParam, ID_DEMO_PASTETEXT, MF_GRAYED) ;
       		}      
       		else
       		{   
       			EnableMenuItem ((HMENU)wParam, ID_DEMO_PASTETEXT, MF_ENABLED) ;
       		} 
       		
/*
Smart pointer releases Clipboard data object in its destructor
when it goes out of scope.
*/
       		
       		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 ;
				}
				
/*
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:
				{   
					IDataObjectPtr lpd ;
					STGMEDIUM stg ; FORMATETC fe ; HRESULT hr ;
					LPSTR cp ; 
									        
					OleGetClipboard (&lpd) ;  
							
    				fe.cfFormat = CF_TEXT ;
    				fe.ptd = NULL ;
		    		fe.tymed = TYMED_HGLOBAL ;  
    				fe.dwAspect = DVASPECT_CONTENT ; 
    				fe.lindex = -1 ;
                   
					hr = lpd->GetData (&fe, &stg) ;
					cp = (LPSTR) GlobalLock (stg.hGlobal) ;
					lstrcpyn (DisplayText, cp, 127) ;
					GlobalUnlock (stg.hGlobal) ;
					
					ReleaseStgMedium (&stg) ;
					
					xText = 0 ;
					yText = 0 ;	
					InvalidateRect (hMainWnd, NULL, TRUE) ;
			        		
/*
Smart pointer releases Clipboard data object in its destructor
when it goes out of scope.
*/
					
					return 0 ;
				
				}  
				
			}

			return 0 ;

		}




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

		case WM_CLOSE:
		{
			RevokeDragDrop (hWnd) ;
			CoLockObjectExternal (lpDropTarget, FALSE, TRUE) ;
			lpDropTarget->Release( ) ;
			 
			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) ;
	
}


