// AtextCtl.cpp : Implementation of the CAtextCtrl OLE control class.

#include "stdafx.h"
#include "atext.h"
#include "asynchtext.h"
#include "AtextCtl.h"
#include "AtextPpg.h"


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


IMPLEMENT_DYNCREATE(CAtextCtrl, COleControl)


/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CAtextCtrl, COleControl)
	//{{AFX_MSG_MAP(CAtextCtrl)
	// NOTE - ClassWizard will add and remove message map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CAtextCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CAtextCtrl)
	DISP_PROPERTY_EX(CAtextCtrl, "DownloadPath", GetDownloadPath, SetDownloadPath, VT_BSTR)
	DISP_STOCKPROP_READYSTATE()
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CAtextCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CAtextCtrl, COleControl)
	//{{AFX_EVENT_MAP(CAtextCtrl)
	// NOTE - ClassWizard will add and remove event map entries
	//    DO NOT EDIT what you see in these blocks of generated code !
	EVENT_STOCK_READYSTATECHANGE()
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CAtextCtrl, 1)
	PROPPAGEID(CAtextPropPage::guid)
END_PROPPAGEIDS(CAtextCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CAtextCtrl, "ATEXT.AtextCtrl.1",
	0x722024f3, 0xfc5, 0x11d0, 0x8b, 0xc7, 0, 0x60, 0x8c, 0x87, 0x6f, 0xb3)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CAtextCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DAtext =
		{ 0x722024f1, 0xfc5, 0x11d0, { 0x8b, 0xc7, 0, 0x60, 0x8c, 0x87, 0x6f, 0xb3 } };
const IID BASED_CODE IID_DAtextEvents =
		{ 0x722024f2, 0xfc5, 0x11d0, { 0x8b, 0xc7, 0, 0x60, 0x8c, 0x87, 0x6f, 0xb3 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwAtextOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CAtextCtrl, IDS_ATEXT, _dwAtextOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::CAtextCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CAtextCtrl

BOOL CAtextCtrl::CAtextCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_ATEXT,
			IDB_ATEXT,
			afxRegApartmentThreading,
			_dwAtextOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::CAtextCtrl - Constructor

CAtextCtrl::CAtextCtrl()
{
	InitializeIIDs(&IID_DAtext, &IID_DAtextEvents);

	m_lReadyState = READYSTATE_LOADING;
	// TODO: Call InternalSetReadyState when the readystate changes.

	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::~CAtextCtrl - Destructor

CAtextCtrl::~CAtextCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::OnDraw - Drawing function

void CAtextCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	int length = m_AsynchText.m_Cache.GetLength ( ) ;
	char *cp, *maxcp ; 
	
	m_AsynchText.m_Cache.GetBufferPtr (
		CFile::bufferRead,
		0,
		(void **)&cp,
		(void **)&maxcp) ;

	if (length != 0)
	{
		pdc->DrawText (cp, length, (LPRECT) &rcBounds, DT_LEFT) ;
	}
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::DoPropExchange - Persistence support

void CAtextCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

/*
Load ot save download data path property.
*/

	PX_DataPath (pPX, "DownloadPath", m_AsynchText) ;

/*
If we re loading the property set, then start the binding process
for the asynchronous moniker.
*/

	if (pPX->IsLoading( ))
	{
		BOOL bOK = m_AsynchText.Open (this) ;

		if (!bOK) 
		{
			AfxMessageBox ("Couldn't start download") ;
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::OnResetState - Reset control to default state

void CAtextCtrl::OnResetState()
{
	m_AsynchText.ResetData ( ) ;

	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl::AboutBox - Display an "About" box to the user

void CAtextCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_ATEXT);
	dlgAbout.DoModal();
}


/////////////////////////////////////////////////////////////////////////////
// CAtextCtrl message handlers

OLE_DATAPATH CAtextCtrl::GetDownloadPath() 
{
	CString strResult;
	
	strResult = m_AsynchText.GetPath();

	return strResult.AllocSysString();
}

void CAtextCtrl::SetDownloadPath(LPCTSTR lpszNewValue) 
{
	Load(lpszNewValue, m_AsynchText);

	SetModifiedFlag();
}
