Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 351.9K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// Stream.h
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_STREAM_H__5A2B7AC1_27AE_11D6_B6A6_F2A9931D1E45__INCLUDED_)
#define AFX_STREAM_H__5A2B7AC1_27AE_11D6_B6A6_F2A9931D1E45__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CStreamHeader
class CStreamHeader
{
public:
	// signature
	DWORD m_DaeDoe;
	// signature
	GUID m_Signature;
	// version
	DWORD m_Version;
	// reserved 0
	DWORD m_Reserved0;
	// reserved 1
	DWORD m_Reserved1;

	CStreamHeader();

	// reads the header from the passed stream
	void ReadFromStream(const CComPtr<IStream> &spStream);
	// writes the header to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
	// returns TRUE if the header is valid
	BOOL IsValid();
};

/////////////////////////////////////////////////////////////////////////////
// CStreamFooter
class CStreamFooter
{
public:
	// signature
	GUID m_Signature;
	// reserved 0
	DWORD m_Reserved0;
	// reserved 1
	DWORD m_Reserved1;

	CStreamFooter();

	// reads the footer from the passed stream
	void ReadFromStream(const CComPtr<IStream> &spStream);
	// writes the footer to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
	// returns TRUE if the footer is valid
	BOOL IsValid();
};

/////////////////////////////////////////////////////////////////////////////
// CStreamFormBlock
class CStreamFormBlock
{
public:
	// name
	std::wstring m_Name;
	// size
	CSize m_Size;
	// background color
	OLE_COLOR m_BackColor;
	// foreground color
	OLE_COLOR m_ForeColor;

	CStreamFormBlock();

	// reads the form block from the passed stream
	void ReadFromStream(
		const CComPtr<IStream> &spStream,DWORD StreamVersion);
	// writes the form block to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
};

/////////////////////////////////////////////////////////////////////////////
// CStreamItemListHeader
class CStreamItemListHeader
{
public:
	// count
	DWORD m_Count;
	// bounding rectangle
	CRect m_BoundingRect;

	CStreamItemListHeader();

	// reads the item list header from the passed stream
	void ReadFromStream(
		const CComPtr<IStream> &spStream,DWORD StreamVersion);
	// writes the item list header to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
};

/////////////////////////////////////////////////////////////////////////////
// CStreamItemBlock
class CStreamItemBlock
{
public:
	// name
	std::wstring m_Name;
	// tag
	std::wstring m_Tag;
	// position and size
	CRect m_Rect;
	// class id
	CLSID m_ClassId;
	// this will be set TRUE if the item has persistant data
	BOOL m_HasPersistantData;

	CStreamItemBlock();

	// reads the item block from the passed stream
	void ReadFromStream(
		const CComPtr<IStream> &spStream,DWORD StreamVersion);
	// writes the item block to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
};

/////////////////////////////////////////////////////////////////////////////
// CStreamItemPersistantDataHeader
class CStreamItemPersistantDataHeader
{
public:
	// length
	ULARGE_INTEGER m_Length;

	CStreamItemPersistantDataHeader();

	// reads the item persistant data header from the passed stream
	void ReadFromStream(
		const CComPtr<IStream> &spStream,DWORD StreamVersion);
	// writes the item persistant data header to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
};

/////////////////////////////////////////////////////////////////////////////
// CStreamScriptHeader
class CStreamScriptHeader
{
public:
	// line count
	DWORD m_LineCount;

	CStreamScriptHeader();

	// reads the script header from the passed stream
	void ReadFromStream(
		const CComPtr<IStream> &spStream,DWORD StreamVersion);
	// writes the script header to the passed stream
	void WriteToStream(const CComPtr<IStream> &spStream);
};

/////////////////////////////////////////////////////////////////////////////
// stream formats
//
// version 1
//		CStreamHeader
//			reserved0=zero
//			reserved1=zero
//		CStreamFormBlock (a)
//		CStreamItemListHeader
//		for all items
//			CStreamItemBlock
//			item persistant data
//		CStreamScriptHeader (a)(b)
//		script (a)(b)
//		CStreamFooter
//			reserved0=zero
//			reserved1=zero
//
//	(a) not present in the clipboard stream
//	(b) not present in the undo/redo stream
//
// version 2
//		added m_Tag to CStreamItemBlock
//
// version 3
//		CStreamHeader
//			reserved0=zero
//			reserved1=zero
//		CStreamFormBlock (a)
//		CStreamItemListHeader
//		for all items
//			CStreamItemBlock
//			CStreamItemPersistantDataHeader (new)
//			item persistant data
//		CStreamScriptHeader (a)(b)
//		script (a)(b)
//		CStreamFooter
//			reserved0=zero
//			reserved1=zero
//
//	(a) not present in the clipboard stream
//	(b) not present in the undo/redo stream
//
// todo : add additional stream version formats here

/////////////////////////////////////////////////////////////////////////////
// Stream helpers

// reads a simple data type from the passed stream
template <class T>
void ReadFromStream(const CComPtr<IStream> &spStream,T &Data)
{
	if(spStream==NULL)
	{
		throw std::exception();
	}
	DWORD BytesRead=0;
	if(!SUCCEEDED(spStream->Read(&Data,sizeof(T),&BytesRead)))
	{
		throw std::exception();
	}
	if(BytesRead!=sizeof(T))
	{
		throw std::exception();
	}
}

// writes a simple data type to the passed stream
template <class T>
void WriteToStream(const CComPtr<IStream> &spStream,const T &Data)
{
	if(spStream==NULL)
	{
		throw std::exception();
	}
	DWORD BytesWritten=0;
	if(!SUCCEEDED(spStream->Write(&Data,sizeof(T),&BytesWritten)))
	{
		throw std::exception();
	}
	if(BytesWritten!=sizeof(T))
	{
		throw std::exception();
	}
}

// reads a simple data type array from the passed stream
template <class T>
void ReadArrayFromStream(
	const CComPtr<IStream> &spStream,T *pArray,DWORD Length)
{
	if(spStream==NULL)
	{
		throw std::exception();
	}
	if(pArray==NULL)
	{
		throw std::exception();
	}
	DWORD BytesRead=0;
	if(!SUCCEEDED(spStream->Read(pArray,Length*sizeof(T),&BytesRead)))
	{
		throw std::exception();
	}
	if(BytesRead!=Length*sizeof(T))
	{
		throw std::exception();
	}
}

// writes a simple data type array to the passed stream
template <class T>
void WriteArrayToStream(
	const CComPtr<IStream> &spStream,const T *pArray,DWORD Length)
{
	if(spStream==NULL)
	{
		throw std::exception();
	}
	if(pArray==NULL)
	{
		throw std::exception();
	}
	DWORD BytesWritten=0;
	if(!SUCCEEDED(spStream->Write(pArray,Length*sizeof(T),&BytesWritten)))
	{
		throw std::exception();
	}
	if(BytesWritten!=Length*sizeof(T))
	{
		throw std::exception();
	}
}

// reads a simple data type sized array from the passed stream
template <class T>
void ReadSizedArrayFromStream(
	const CComPtr<IStream> &spStream,T *pArray)
{
	// length
	DWORD Length=0;
	ReadFromStream(spStream,Length);
	// data
	ReadArrayFromStream(spStream,pArray,Length);
}

// writes a simple data type sized array to the passed stream
template <class T>
void WriteSizedArrayToStream(
	const CComPtr<IStream> &spStream,const T *pArray,DWORD Length)
{
	// length
	WriteToStream(spStream,Length);
	// data
	WriteArrayToStream(spStream,pArray,Length);
}

// reads a std::wstring from the passed stream
template <>
void ReadFromStream(
	const CComPtr<IStream> &spStream,std::wstring &Data);
// writes a std::wstring to the passed stream
template <>
void WriteToStream(
	const CComPtr<IStream> &spStream,const std::wstring &Data);

#endif // !defined(AFX_STREAM_H__5A2B7AC1_27AE_11D6_B6A6_F2A9931D1E45__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions