/*
*********************************************************************
*
*	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: MFCDATA1VIEW.CPP
*
*	Purpose: CView class MFCDATA1 sample app
*
*********************************************************************
*/

// mfcdata1View.cpp : implementation of the CMfcdata1View class
//

#include "stdafx.h"
#include "mfcdata1.h"

#include "mfcdata1Doc.h"
#include "mfcdata1View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View

IMPLEMENT_DYNCREATE(CMfcdata1View, CView)

BEGIN_MESSAGE_MAP(CMfcdata1View, CView)
	//{{AFX_MSG_MAP(CMfcdata1View)
	ON_WM_CREATE()
	ON_COMMAND(ID_DATAOBJECT_PASTETEXT, OnDataobjectPastetext)
	ON_UPDATE_COMMAND_UI(ID_DATAOBJECT_PASTETEXT, OnUpdateDataobjectPastetext)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View construction/destruction

CMfcdata1View::CMfcdata1View()
{
	m_DropPoint.x = 0 ;
	m_DropPoint.y = 0 ;
}

CMfcdata1View::~CMfcdata1View()
{
}

BOOL CMfcdata1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View drawing

void CMfcdata1View::OnDraw(CDC* pDC)
{
	CMfcdata1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	pDC->TextOut (m_DropPoint.x, m_DropPoint.y, pDoc->m_String) ;
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View printing

BOOL CMfcdata1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMfcdata1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMfcdata1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View diagnostics

#ifdef _DEBUG
void CMfcdata1View::AssertValid() const
{
	CView::AssertValid();
}

void CMfcdata1View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMfcdata1Doc* CMfcdata1View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMfcdata1Doc)));
	return (CMfcdata1Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMfcdata1View message handlers

int CMfcdata1View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_DropTarget.Register (this) ;
	
	return 0;
}

DROPEFFECT CMfcdata1View::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	if (pDataObject->IsDataAvailable(CF_TEXT))
	{
		m_bCanDrop = TRUE ;
	}
	else
	{
		m_bCanDrop = FALSE ;
	}

	return OnDragOver (pDataObject, dwKeyState, point) ;
}

DROPEFFECT CMfcdata1View::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point) 
{
	if (m_bCanDrop) 
	{
		if (!(dwKeyState & MK_CONTROL))
		{
			return DROPEFFECT_MOVE ;
		}
		else
		{
			return DROPEFFECT_COPY ;
		}
	}
	return DROPEFFECT_NONE ;
}

BOOL CMfcdata1View::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) 
{
	return PasteTextIntoMyDocument (pDataObject, dropEffect, point) ;
}

void CMfcdata1View::OnDataobjectPastetext() 
{
	COleDataObject dataobj ;

	dataobj.AttachClipboard ( ) ;

	PasteTextIntoMyDocument (&dataobj) ;
}

void CMfcdata1View::OnUpdateDataobjectPastetext(CCmdUI* pCmdUI) 
{
	COleDataObject dataobj ;

	dataobj.AttachClipboard ( ) ;

	pCmdUI->Enable (dataobj.IsDataAvailable (CF_TEXT)) ;
}

BOOL CMfcdata1View::PasteTextIntoMyDocument (COleDataObject *pData, DROPEFFECT de, CPoint point)
{
	STGMEDIUM stg ;

	FORMATETC fe ;
	fe.cfFormat	= CF_TEXT ;
	fe.tymed = TYMED_HGLOBAL ;
	fe.lindex = -1 ;
	fe.dwAspect = DVASPECT_CONTENT ;
	fe.ptd = NULL ;

	if (pData->GetData (CF_TEXT, &stg, &fe))
	{
		CMfcdata1Doc* pDoc = GetDocument();
		pDoc->m_String = (char *) GlobalLock (stg.hGlobal) ;
		GlobalUnlock (stg.hGlobal) ;
		m_DropPoint = point ;
		ReleaseStgMedium (&stg) ;
		Invalidate ( ) ;

		return TRUE ;
	}
	else
	{
		return FALSE ;
	}
}

