Click here to Skip to main content
15,896,437 members
Articles / Desktop Programming / MFC

CDiagramEditor - DIY vector and dialog editor

Rate me:
Please Sign up or sign in to vote.
4.96/5 (165 votes)
23 Jun 2006Public Domain11 min read 513.1K   36.8K   302  
A feature rich vector editor skeleton.
// DialogEditorDemoDoc.cpp : implementation of the CDialogEditorDemoDoc class
//

#include "stdafx.h"
#include "DialogEditorDemo.h"

#include "DialogEditorDemoDoc.h"

// --- DiagramEditor ---
#include "DiagramControlFactory.h"
#include "TextFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma warning( disable : 4706 )

/////////////////////////////////////////////////////////////////////////////
// CDialogEditorDemoDoc

IMPLEMENT_DYNCREATE(CDialogEditorDemoDoc, CDocument)

BEGIN_MESSAGE_MAP(CDialogEditorDemoDoc, CDocument)
	//{{AFX_MSG_MAP(CDialogEditorDemoDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogEditorDemoDoc construction/destruction

CDialogEditorDemoDoc::CDialogEditorDemoDoc()
{
}

CDialogEditorDemoDoc::~CDialogEditorDemoDoc()
{
}

BOOL CDialogEditorDemoDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// --- DiagramEditor ---
	// Removing the current data
	m_objs.Clear();

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CDialogEditorDemoDoc serialization

void CDialogEditorDemoDoc::Serialize(CArchive& ar)
{
	// --- DiagramEditor ---
	// Saving and loading to/from a text file
	if (ar.IsStoring())
	{
		ar.WriteString( m_objs.GetString() + _T( "\r\n" ) );
		int count = 0;
		CDiagramEntity* obj;
		while( ( obj = m_objs.GetAt( count++ ) ) )
			ar.WriteString( obj->GetString() + _T( "\r\n" ) );

		m_objs.SetModified( FALSE );
	}
	else
	{

		m_objs.Clear();
		CString str;
		while(ar.ReadString( str ) )
		{

			if( !m_objs.FromString( str ) )
			{
				CDiagramEntity* obj = CDiagramControlFactory::CreateFromString( str );
				if( obj )
					m_objs.Add( obj );
			}
		}
		m_objs.SetModified( FALSE );
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDialogEditorDemoDoc diagnostics

#ifdef _DEBUG
void CDialogEditorDemoDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDialogEditorDemoDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDialogEditorDemoDoc commands

// --- DiagramEditor ---
CDiagramEntityContainer* CDialogEditorDemoDoc::GetData()
{
	// Data accessor
	return &m_objs;
}

BOOL CDialogEditorDemoDoc::SaveModified() 
{

	// We override this to get the dirty-
	// handling right
	SetModifiedFlag( m_objs.IsModified() );
	return CDocument::SaveModified();

}

void CDialogEditorDemoDoc::Export()
{
	// An example of exporting

	CString filename;
	CStringArray stra;

	// Header. I don't want this in the CDiagramEntityContainer class
	// where it would normally belong, as it should not be necessary 
	// to derive a class from CDiagramEntityContainer.

	stra.Add( _T( "<html>" ) );
	stra.Add( _T( "<head>" ) );
	stra.Add( _T( "<style>" ) );
	stra.Add( _T( "\t.controls { font-family:MS Sans Serif;font-size:12; }" ) );
	stra.Add( _T( "\tbody { background-color:#c0c0c0; }" ) );
	stra.Add( _T( "</style>" ) );
	stra.Add( _T( "<script>" ) );
	stra.Add( _T( "function buttonHandler( obj )" ) );
	stra.Add( _T( "{" ) );
	stra.Add( _T( "\talert( obj.name )" ) );
	stra.Add( _T( "}" ) );
	stra.Add( _T( "function checkboxHandler( obj )" ) );
	stra.Add( _T( "{" ) );
	stra.Add( _T( "\talert( obj.name )" ) );
	stra.Add( _T( "}" ) );
	stra.Add( _T( "function radiobuttonHandler( obj )" ) );
	stra.Add( _T( "{" ) );
	stra.Add( _T( "\talert( obj.name )" ) );
	stra.Add( _T( "}" ) );
	stra.Add( _T( "function listboxHandler( obj )" ) );
	stra.Add( _T( "{" ) );
	stra.Add( _T( "\talert( obj.name )" ) );
	stra.Add( _T( "}" ) );
	stra.Add( _T( "function comboboxHandler( obj )" ) );
	stra.Add( _T( "{" ) );
	stra.Add( _T( "\talert( obj.name )" ) );
	stra.Add( _T( "}" ) );
	stra.Add( _T( "</script>" ) );
	stra.Add( _T( "</head>" ) );
	stra.Add( _T( "<body topmargin=0 leftmargin=0>" ) );

	CRect rect( 0, 0, m_objs.GetVirtualSize().cx + 1, m_objs.GetVirtualSize().cy + 1 );
	CString input1( _T( "<div style='position:absolute;left:%i;top:%i;width:%i;height:%i;border:1 solid black;'>" ) );
	CString input2( _T( "<div style='position:absolute;left:0;top:0;width:%i;height:%i;border-top:1 solid white;border-left:1 solid white;border-right:1 solid gray;border-bottom:1 solid gray;'>" ) );
	CString str;

	str.Format( input1, rect.left, rect.top, rect.Width(), rect.Height() );
	stra.Add( str );

	rect.InflateRect( -1, -1 );
	str.Format( input2, rect.Width(), rect.Height() );
	stra.Add( str );

	// The export itself
	m_objs.Export( stra );

	// The footer
	stra.Add( _T( "</div>" ) );
	stra.Add( _T( "</div>" ) );

	stra.Add( _T( "</body>" ) );
	stra.Add( _T( "</html>" ) );

	// Writing it to a file
	CTextFile tf( _T( "html" ), _T( "\n" ) );
	if( !tf.WriteTextFile( filename, stra ) )
		if( filename.GetLength() )
			AfxMessageBox( tf.GetErrorMessage() );

}

#pragma warning( default : 4706 )

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 A Public Domain dedication


Written By
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

Comments and Discussions