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

// mfcdata2View.cpp : implementation of the CMfcdata2View class
//

#include "stdafx.h"
#include "mfcdata2.h"
#include "textdlg.h"
#include "mfcdata2Doc.h"
#include "mfcdata2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View

IMPLEMENT_DYNCREATE(CMfcdata2View, CView)

BEGIN_MESSAGE_MAP(CMfcdata2View, CView)
	//{{AFX_MSG_MAP(CMfcdata2View)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_DATAOBJECT_EDITTEXT, OnDataobjectEdittext)
	ON_COMMAND(ID_DATAOBJECT_COPYTOCLIPBOARD, OnDataobjectCopytoclipboard)
	ON_COMMAND(ID_DATAOBJECT_PASTETEXT, OnDataobjectPastetext)
	ON_UPDATE_COMMAND_UI(ID_DATAOBJECT_PASTETEXT, OnUpdateDataobjectPastetext)
	ON_WM_CREATE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View construction/destruction

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

CMfcdata2View::~CMfcdata2View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View drawing

void CMfcdata2View::OnDraw(CDC* pDC)
{
	CMfcdata2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

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

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMfcdata2View message handlers

void CMfcdata2View::OnDataobjectEdittext() 
{
	CTextDlg dlg ;
 	CMfcdata2Doc* pDoc = GetDocument();

	dlg.m_String = pDoc->m_String ;

	if (dlg.DoModal( ) == IDOK)
	{
		pDoc->m_String = dlg.m_String ;
		Invalidate( ) ;
	}
}


DROPEFFECT CMfcdata2View::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 CMfcdata2View::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 CMfcdata2View::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) 
{
	return PasteTextIntoMyDocument (pDataObject, dropEffect, point) ;
}

void CMfcdata2View::OnDataobjectPastetext() 
{
	COleDataObject dataobj ;

	dataobj.AttachClipboard ( ) ;

	PasteTextIntoMyDocument (&dataobj) ;
}

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

	dataobj.AttachClipboard ( ) ;

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

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

BOOL CMfcdata2View::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))
	{
		CMfcdata2Doc* pDoc = GetDocument();
		pDoc->m_String = (char *) GlobalLock (stg.hGlobal) ;
		GlobalUnlock (stg.hGlobal) ;
		m_DropPoint = point ;
		Invalidate ( ) ;

		return TRUE ;
	}
	else
	{
		return FALSE ;
	}
}



void CMfcdata2View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	COleDataSource ds  ;
	CMfcdata2Doc* pDoc = GetDocument();

	char *cp = (char *) GlobalAlloc (GMEM_FIXED, pDoc->m_String.GetLength( )+1) ;
	lstrcpy (cp, (LPCTSTR)pDoc->m_String) ;
	ds.CacheGlobalData (CF_TEXT, cp) ;

	if (ds.DoDragDrop () == DROPEFFECT_MOVE)
	{
		pDoc->m_String = "" ;	
		Invalidate ( ) ;
	}
}


void CMfcdata2View::OnDataobjectCopytoclipboard() 
{
	COleDataSource *pds = new COleDataSource ( ) ;
	CMfcdata2Doc* pDoc = GetDocument();

	char *cp = (char *) GlobalAlloc (GMEM_FIXED, pDoc->m_String.GetLength( )+1) ;
	lstrcpy (cp, (LPCTSTR)pDoc->m_String) ;

	pds->CacheGlobalData (CF_TEXT, cp) ;

	pds->SetClipboard ( ) ;
}
