//////////////////////////////////////////////////////////////////////
//  JMSAgentObj.h   - a NeatTools module that controls 
//                    a Microsoft Agent character.
//                    You can control its location and 
//                    the speech it synthesizes.  
//                    Voice-recognition has not been implemented yet.
//	ver. 99 July 28
//
//	REQUIRED COMPONENTS: (also available on the MSDN CDs)
//
//	1.	http://activex.microsoft.com/activex/controls/agent2/MSagent.exe
//	2.	http://activex.microsoft.com/activex/controls/agent2/tv_enua.exe
//	3.	http://msdn.microsoft.com/workshop/imedia/agent/agentdl.asp#character
//
//	more information is available at
//		http://msdn.microsoft.com/msagent/default.asp
//		http://msdn.microsoft.com/workshop/imedia/agent/documentation.asp
//	** available languages: Arabic, French, German, Hebrew,
//                          Italian, Japanese, Korean, Spanish, and Chinese
//
//
//  Language:   Microsoft Visual C++, ver 6.0
//  Authors:    Tav Hawkins (taviare@physics.syr.edu)
//              Rob Salgado (salgado@physics.syr.edu)
//
//  Credits:	Special thanks to Yuh-Jye Chang for help
//              with threads.
//
//	            This module was partially generated by
//              Rob Salgado's ModuleMaker utility.
//
//////////////////////////////////////////////////////////////////////

#if !defined( _JMSAgentObj_h )
#define _JMSAgentObj_h

//include the header files of the base class (here, JAddObj)
//
#include "JAddObj.h"

//MICROSOFT AGENT SUPPORT
#include <windows.h>
#include <tchar.h>
#include "AgtSvr.h"
#include "AgtSvr_i.c"
static const LPTSTR kpszAppTitle = _T("NeatTools / Microsoft Agent");

//THREAD SUPPORT
#include "JCriticalSection.h"
#include "JThread.h"

//FILENAME SUPPORT
static const JString type=JString("*.acs");

static int AgentCount=0;	//not yet implemented

static int MINX=0, MAXX=1280;		//restrict the positions
static int MINY=0, MAXY=1024;		//of the characters


//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
//
//  class JMSAgentObj
//

class
#if defined(_EXTERNAL_DLL)
  DllExport
#else
  DllImport 
#endif
JMSAgentObj : public JAddObj { //derives from base class

  protected:

    //----------------------------------------------
    // PERSISTENCY-RELATED METHODS
    virtual void writeContent(class JOutputStream& os);
    virtual void readContent(class JDictionary& dict);

  public:
    enum { TRIGGER_INPUT, X_INPUT, Y_INPUT, STRING_INPUT , NUMBER_OF_INPUTS};     //convenient labels for 2 inputs
    enum { TRIGGER_OUTPUT,NICKNAME_OUTPUT, X_OUTPUT, Y_OUTPUT, STRING_OUTPUT, NUMBER_OF_OUTPUTS };  //convenient labels for 2 outputs
    //----------------------------------------------
    // BASIC METHODS  [ Register() is declared in JObject ]
    JMSAgentObj();	//constructor
    JMSAgentObj(const JMSAgentObj & src);  //if you define your own copy constructor
	~JMSAgentObj();	//if you define your own destructor

    virtual const char* className() const;
    virtual JObject* clone() const;

    //----------------------------------------------
    // EVENT-BROADCAST-RELATED METHODS
    virtual void engine(int n, JLinkObj& link);
    virtual void access(int n, JLinkObj& link, const JDataType& data);
    virtual JArray getProperties();
    virtual boolean updateProperty(JProperty& prop);
    
    //----------------------------------------------
    // DISPLAY-RELATED METHODS
    virtual void paint(JGraphics g, double dx, double dy, 
                    JRegion& rgn, double scale);
    //----------------------------------------------
    // INFORMATION-RELATED METHODS
    virtual int inputType(int n);
    virtual int outputType(int n);
    virtual boolean inputAllowed(int n);
    virtual boolean outputAllowed(int n);
    virtual JString inputTag(int n);
    virtual JString outputTag(int n);
    virtual JFRect inputArea(int n);
    virtual JFRect outputArea(int n);
    virtual int inputFace(int n);
    virtual int outputFace(int n);
    virtual JFPoint inputPoint(int n, JLinkObj& link);
    virtual JFPoint outputPoint(int n, JLinkObj& link);

	//FILENAME SUPPORT
	virtual JString composit(JString prefix, int sequence);
	virtual int parseName(JString filename, JString& prefix);

	//THREAD SUPPORT
    virtual void run();
    virtual void reset();
    //virtual void open();
    virtual void close();
	virtual void startup();
    JThread thread;
	int valid;
    JCriticalSection cs;  // Mutex for concurency

	//MICROSOFT AGENT SUPPORT

	int InitializeMSAgentServer(void); 
	int InitializeMSAgentChar(void); 

	int AgentCharSpeak(void);
	int AgentCharSetPosition(void);

	int AgentCharGetPosition(void);

	void ShutdownMSAgentServer(void);
	void ShutdownMSAgentChar(void);

  protected:

    //----------------------------------------------
    //  DATA
    boolean doRepaint;  // flag: repaint needed
                        // if something has changed

	JString prefix;	//for filename support
    JString speechText;
    
    //----------------------------------------------
    //  PERSISTENT DATA
    int     posX;
	int     posY;
	int     startState;

	JString filename;
	JString preText, nickname;

    //----------------------------------------------
    //  MSAgent
	HRESULT				hRes;
	_TCHAR				szError[256];
	VARIANT				vPath;
	VARIANT				vPathDefault;
	VARIANT				vPathDefault2;
	BSTR				bszSpeak;
	long				lCharID;
	long				lRequestID;
	IAgentEx		   *pAgentEx;
	IAgentCharacterEx  *pCharacterEx;

	int MSAgentServerLoaded;
	int MSAgentCharLoaded;
	int textUpdated;
	int positionUpdated;
    JString theText;


};


/*	partial attempt to convert JStrings to BSTRs
class JWString : public JString {
public:

	BSTR* toBSTR(JString & JS);
	~JWString();
private:
	BSTR *pbstr;
};

JWString::~JWString()
{
}

BSTR* JWString::toBSTR(JString & JS)
{
	BSTR BS=SysAllocStringByteLen( (char *)this, 2*(length()) );
	SysFreeString(*pbstr);
	*pbstr=SysAllocStringByteLen( (char *)this, 2*(length()) );
	for (int i=0; i<length(); i++)
	{
		*pbstr[i]=this[i];
	}
	return pbstr;
}
*/


#endif

