/*
*********************************************************************
*
*	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: CELIZA.CPP
*
*	Purpose: Implmentation of IEliza interface for local server
*
*********************************************************************
*/

#include <windows.h>

#include "celiza.h"

extern "C" void Eliza (TCHAR *InStr, TCHAR *OutStr) ;

CEliza::CEliza (void)
{
    m_RefCount = 1 ;
    return;
}


CEliza::~CEliza(void)
{
	SendMessage (m_hWnd, WM_CLOSE, 0, 0) ;
    return;
}



STDMETHODIMP CEliza::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (IsEqualIID(riid, IID_IUnknown)
        || IsEqualIID(riid, IID_IEliza))
	{
        *ppv = (LPVOID)this;
		AddRef( ) ;
		return NOERROR;
	}
	else
	{
    	*ppv = NULL ;
    	return ResultFromScode(E_NOINTERFACE);
	}
        
}


STDMETHODIMP_(ULONG) CEliza::AddRef(void)
{
    return ++m_RefCount;
}


STDMETHODIMP_(ULONG) CEliza::Release(void)
{
	m_RefCount -- ;

    if (m_RefCount == 0)
	{
        delete this;
		return 0 ;
	}

    return m_RefCount ;
}


STDMETHODIMP CEliza::Complain (LPOLESTR pIn, LPOLESTR *pOut)
{
	if (*pIn != 0x00)
	{
 		TCHAR out [256] ;
		wsprintf (out, TEXT("Client: %s%c%c"), pIn, 0x0D, 0x0A) ;
		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) out) ;

		WCHAR *pStr = (WCHAR *) CoTaskMemAlloc (512) ;

		Eliza (pIn, pStr) ;
		*pOut = pStr ;

		SendMessage (m_hEdit, EM_SETSEL, (WPARAM) -1, (LPARAM)-1) ;
		SendMessage (m_hEdit, EM_REPLACESEL, 0, (LPARAM) pStr) ;
	}

	return NOERROR ;
}
