#ifndef _COMMON_DDEMESSAGE_HPP_
#define _COMMON_DDEMESSAGE_HPP_
#ifndef _COMMON_WINDOWS_HPP_
#include <common/windows.hpp>
#endif
#ifndef _COMMON_STRING_HPP_
#include <common/string.hpp>
#endif
#ifndef _COMMON_DDE_HPP_
#include <common/dde.hpp>
#endif
#ifndef _COMMON_DDEACK_HPP_
#include <common/ddeack.hpp>
#endif
#ifndef _COMMON_DDEDATA_HPP_
#include <common/ddedata.hpp>
#endif
#ifndef _COMMON_CALLBACK_HPP_
#include <common/callback.hpp>
#endif

class DDEMessage
{
public:
	DDEMessage(void);
	DDEMessage(HWND hClientWnd,HWND hServerWnd);
	~DDEMessage();
	void setClient(HWND hClientWnd);
	void setServer(HWND hServerWnd);
	WORD postTerminate(void);
	WORD receiveTerminate(CallbackData &someCallbackData);
	WORD sendInitiate(String applicationString,String topicString);
	WORD acceptInitiate(String applicationString,String topicString);
	WORD postRequest(String requestString);
	WORD receiveRequest(CallbackData &someCallbackData,String &requestString);
	WORD postAcknowledge(void);
	WORD postAcknowledge(DDEAck someDDEAck,String ackDescription);
	WORD receiveAcknowledge(CallbackData &someCallbackData);
	WORD postData(HGLOBAL hGlobalData,String stringDescription=String());
	HGLOBAL receiveData(CallbackData &someCallbackData);
	DWORD lastDDEMessage(void)const;
	CallbackData lastCallbackData(void)const;
private:
	enum {MaxLength=250};
	enum Format{TextFormat=CF_TEXT};
	enum Mode{Receive,Send,Post};
	DDEMessage(const DDEMessage &someDDEMessage);
	void cleanup(void);
	WORD isValid(void)const;
	HGLOBAL buildDDEDataHandle(HGLOBAL hGlobalData);
	HGLOBAL copyDDEDataHandle(HGLOBAL hGlobalDDEData);
	void examineDDEData(HGLOBAL hGlobalDDEData);

  String mStringUnset;
	HWND mhServerWnd;
	HWND mhClientWnd;
	DWORD mLastDDEMessage;
	HGLOBAL mhGlobalLastDDEData;
	CallbackData mLastCallbackData;
};

inline
DDEMessage::DDEMessage(void)
: mhServerWnd(0), mhClientWnd(0), mLastDDEMessage(0L), mhGlobalLastDDEData(0),
	mStringUnset("UNSET")
{
}

inline
DDEMessage::DDEMessage(HWND hServerWnd,HWND hClientWnd)
: mhServerWnd(hServerWnd), mhClientWnd(hClientWnd), mLastDDEMessage(0L),
	mhGlobalLastDDEData(0), mStringUnset("UNSET")
{
}

inline
DDEMessage::DDEMessage(const DDEMessage &/*someDDEMessage*/)
{
}

inline
DDEMessage::~DDEMessage()
{
	cleanup();
}

inline
WORD DDEMessage::isValid(void)const
{
	if(!::IsWindow(mhClientWnd))return FALSE;
	if(!::IsWindow(mhServerWnd))return FALSE;
  return TRUE;
}

inline
void DDEMessage::setClient(HWND hClientWnd)
{
	mhClientWnd=hClientWnd;
}

inline
void DDEMessage::setServer(HWND hServerWnd)
{
  mhServerWnd=hServerWnd;
}

inline
DWORD DDEMessage::lastDDEMessage(void)const
{
  return mLastDDEMessage;
}

inline
CallbackData DDEMessage::lastCallbackData(void)const
{
  return mLastCallbackData;
}

inline
WORD DDEMessage::postTerminate(void)
{
	mLastDDEMessage=WM_DDE_TERMINATE;
	return ::PostMessage(mhServerWnd,WM_DDE_TERMINATE,(WPARAM)mhClientWnd,0L);
}

inline
WORD DDEMessage::receiveTerminate(CallbackData &/*someCallbackData*/)
{
	return TRUE;
}
#endif
