Click here to Skip to main content
15,893,644 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 352K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// UndoRedoStorage.cpp: implementation of the CUndoRedoStorage class.
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UndoRedoStorage.h"

// the base element name
#define BASE_ELEMENT_NAME	L""

/////////////////////////////////////////////////////////////////////////////
// CUndoRedoStorage

CUndoRedoStorage::CUndoRedoStorage()
{
	// initialise everthing
	m_Count=0;
}

CUndoRedoStorage::~CUndoRedoStorage()
{
	// clean up
}

std::wstring CUndoRedoStorage::ElementName(DWORD Ix) const
{
	// create an element name given the index
	std::wstringstream strm;
	strm << BASE_ELEMENT_NAME;
	strm << Ix;
	// return the element name
	return strm.str().c_str();
}

void CUndoRedoStorage::Create()
{
	// create the storage
	if(!SUCCEEDED(StgCreateDocfile(NULL,
		STGM_DIRECT|STGM_READWRITE|
		STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_DELETEONRELEASE,
		0,&m_spStorage)))
	{
		throw std::exception();
	}
}

BOOL CUndoRedoStorage::IsCreated() const
{
	// determine if the storage has been created
	return (m_spStorage==NULL) ? FALSE : TRUE;
}

DWORD CUndoRedoStorage::GetElementCount() const
{
	// return the number of elements in the storage
	return m_Count;
}

CComPtr<IStream> CUndoRedoStorage::CreateNewElement()
{
	ATLASSERT(m_spStorage!=NULL);

	// create a new storage element
	CComPtr<IStream> spStream;
	if(!SUCCEEDED(m_spStorage->CreateStream(ElementName(m_Count).c_str(),
		STGM_DIRECT|STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
		0,0,&spStream)))
	{
		throw std::exception();
	}
	// update the element count
	m_Count++;
	// return the element stream
	return spStream;
}

CComPtr<IStream> CUndoRedoStorage::GetStreamOnLastElement()
{
	ATLASSERT(m_spStorage!=NULL);
	ATLASSERT(m_Count > 0);

	// return a stream on the last storage element
	CComPtr<IStream> spStream;
	if(!SUCCEEDED(m_spStorage->OpenStream(ElementName(m_Count-1).c_str(),
		NULL,STGM_DIRECT|STGM_READ|STGM_SHARE_EXCLUSIVE,
		0,&spStream)))
	{
		throw std::exception();
	}
	// return the element stream
	return spStream;
}

void CUndoRedoStorage::RemoveLastElement()
{
	ATLASSERT(m_spStorage!=NULL);
	ATLASSERT(m_Count > 0);

	// remove the last storage element
	if(!SUCCEEDED(
		m_spStorage->DestroyElement(ElementName(m_Count-1).c_str())))
	{
		throw std::exception();
	}
	// update the element count
	m_Count--;
}

void CUndoRedoStorage::RemoveAllElements()
{
	ATLASSERT(m_spStorage!=NULL);

	// remove all storage elements
	while(m_Count > 0)
	{
		RemoveLastElement();
	}
}

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