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

A Simple Generic Wizard

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
12 May 20033 min read 66.9K   892   26  
A custom AppWizard application to create simple CTreePropSheet-based wizards.
// $$root$$.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "$$root$$.h"
#include "TreePropSheet\\TreePropSheet.h"
#include "WizDlg1.h"
#include "WizDlg2.h"
#include "WizDlg3.h"

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

/////////////////////////////////////////////////////////////////////////////
// C$$Safe_root$$App

BEGIN_MESSAGE_MAP(C$$Safe_root$$App, CWinApp)
	//{{AFX_MSG_MAP(C$$Safe_root$$App)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// C$$Safe_root$$App construction

C$$Safe_root$$App::C$$Safe_root$$App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	_tcscpy( wizInfo.sProjectName, _T("myproj") );
}

/////////////////////////////////////////////////////////////////////////////
// The one and only C$$Safe_root$$App object

C$$Safe_root$$App theApp;

BOOL C$$Safe_root$$App::MyEnumResNameProc( HANDLE hModule,
								  LPCTSTR lpszType,
								  LPTSTR lpszName,
								  LONG lParam )
{
	CFile f;
	if ( f.Open(lpszName,CFile::modeWrite|CFile::modeCreate) ) {
		HRSRC hResInfo;
		if ( (hResInfo=FindResource(NULL,lpszName,lpszType)) ) {
			HGLOBAL h;
			if ( (h=LoadResource(NULL,hResInfo)) ) {
				f.Write( h, SizeofResource(NULL,hResInfo) );
			}
		}
	}
	f.Close();
	return TRUE;
}

BOOL C$$Safe_root$$App::CreateDefaultTemplatesFromResources() {
	return EnumResourceNames( NULL,
					"TEMPLATE",
					(ENUMRESNAMEPROC)MyEnumResNameProc,
					(long)this );
}

BOOL C$$Safe_root$$App::AskCreateDefaultTemplates() {
	CString s;
	s.Format( _T("Important configuration file not found (%s). "
				"Do you want to create default templates?\n"
				"NOTE: any template with the same name as a default"
				" template will be overwritten."), WIZ_FILE_PROJECT );
	int iRet = AfxMessageBox( s, MB_YESNO|MB_ICONQUESTION );
	if ( IDYES == iRet )
		return TRUE;
	else
		return FALSE;
}

void C$$Safe_root$$App::InitTemplatesFolder() {
	DWORD dwSize = 80;
	char sUserName[80];
	GetUserName( sUserName, &dwSize );
	m_sTemplatesFolder.Empty();
	m_sTemplatesFolder.Format( "Templates_%s", sUserName );
	CreateDirectory( m_sTemplatesFolder, NULL );
	SetCurrentDirectory( m_sTemplatesFolder );
}

void C$$Safe_root$$App::GetConfirmText( CString& sText ) {
	sText = m_sConfirmBuf;
}

PTCHAR C$$Safe_root$$App::LoadTemplate( LPCTSTR lpszTemplateName ) {
	DWORD dwSize;
	CFile fo;
	PTCHAR Buf;
	if ( fo.Open(lpszTemplateName,CFile::modeRead) ) {
		dwSize = fo.GetLength();
		Buf = new char[dwSize+1];
		fo.Read( Buf, dwSize );
		fo.Close();
		Buf[dwSize] = NULL;
		return Buf;
	}
	else {
		return NULL;
	}
}

DWORD C$$Safe_root$$App::FileStatusInProject( CString& sFName ) {
	int i = 0;
	DWORD fstatus = FILE_STATUS_DEF;
	BOOL bContinue = TRUE;
	while ( bContinue ) {
		TCHAR c = sFName[i++];
		// TODO: modifiy according to the symbol supported by your wizard
		switch( c ) {
		case '!': // add code to manipulate this symbol
			sFName.Remove( c );
			fstatus |= FILE_STATUS_1;
		case '-': // add code to manipulate this symbol
			sFName.Remove( c );
			fstatus |= FILE_STATUS_2;
		case '*': // add code to manipulate this symbol
			sFName.Remove( c );
			fstatus |= FILE_STATUS_3;
		case '^': // add code to manipulate this symbol
			sFName.Remove( c );
			fstatus |= FILE_STATUS_4;
		default:
			bContinue = FALSE;
			break;
		}
	}
	return fstatus;
}

BOOL C$$Safe_root$$App::LoadProjectInfo() {
	CStdioFile f;
	PTCHAR Buf;
	CString sTemplateName;
	CString s;
	TCHAR seps[] = " ,\t\n";
	if ( f.Open(WIZ_FILE_PROJECT,CFile::modeRead) ) {
		while ( f.ReadString(s) ) {
			if ( s.Find("$$_//_$$")==-1 && !s.IsEmpty() ) {
				sTemplateName = _tcstok( (LPTSTR)(LPCTSTR)s, seps );
				DWORD dwFS = FileStatusInProject( sTemplateName );
				// TODO: add your code depending on how you want
				// to handle the different types of files in the
				// project
				if ( dwFS&FILE_STATUS_DEF ) {
					m_saTemplates.Add( _tcstok( NULL, seps) );
					Buf = LoadTemplate( sTemplateName );
					if ( Buf ) {
						m_saTemplatesBuf.Add( Buf );
						delete Buf;
					}
					else {
						return FALSE;
					}
				}
				else if ( dwFS&FILE_STATUS_1 ) {
				}
				else if ( dwFS&FILE_STATUS_2 ) {
				}
				else if ( dwFS&FILE_STATUS_3 ) {
				}
				else if ( dwFS&FILE_STATUS_4 ) {
				}
				else {
				}
			}
		}
		f.Close();
		Buf = LoadTemplate( WIZ_FILE_CONFIRM );
		if ( Buf ) {
			m_sConfirmBuf = Buf;
			delete Buf;
		}
		else {
			return FALSE;
		}
	}
	else {
		AfxMessageBox( "Error: Project info file was not found.", MB_ICONERROR );
		return FALSE;
	}
	return TRUE;
}

BOOL C$$Safe_root$$App::CreateProjectFiles() {
	CWaitCursor();
	CString s = "..\\";
	s += wizInfo.sProjectName;
	CreateDirectory( s, NULL );
	SetCurrentDirectory( s );
	CFile f;
	for ( int i=0;i<m_saTemplates.GetSize();i++ ) {
		if ( f.Open(m_saTemplates[i],CFile::modeCreate|CFile::modeWrite) ) {
			f.Write( m_saTemplatesBuf[i], m_saTemplatesBuf[i].GetLength() );
			f.Close();
		}
		else {
			return FALSE;
		}
	}
	return TRUE;
}

BOOL C$$Safe_root$$App::VerifyWizardConfigFile() {
	CFileFind ff;

	if ( !ff.FindFile(WIZ_FILE_PROJECT) ) {
		if ( !AskCreateDefaultTemplates() )
			return FALSE;
		else {
			if ( !CreateDefaultTemplatesFromResources() )
				return FALSE;
			else
				return VerifyWizardConfigFile();
		}
	}
	return TRUE;
}

void C$$Safe_root$$App::AddMacroFromDouble( LPCTSTR lpszMacro, double value ) {
	CString sMacro;

	sMacro.Format( "%f", value );
	m_Dictionary[lpszMacro] = sMacro;
}

void C$$Safe_root$$App::AddMacroFromInt( LPCTSTR lpszMacro, int value ) {
	CString sValue;

	sValue.Format( "%d", value );
	m_Dictionary[lpszMacro] = sValue;
}

void C$$Safe_root$$App::CreateAndFillMacros() {
	// TODO: create and assign values to your
	// macros.
	m_Dictionary["PROJNAME"] = wizInfo.sProjectName;
	if ( wizInfo.dwInputNodes != 0 ) {
		AddMacroFromInt( _T("INPUT_NODES"), wizInfo.dwInputNodes );
		m_Dictionary[_T("USE_INPUT_NODES")] = _T("YES");
	}
	if ( wizInfo.dwHiddenNodes != 0 ) {
		AddMacroFromInt( _T("HIDDEN_NODES"), wizInfo.dwHiddenNodes );
		m_Dictionary[_T("USE_HIDDEN_NODES")] = _T("YES");
	}
	if ( wizInfo.dwInputNodes != 0 ) {
		AddMacroFromInt( _T("OUTPUT_NODES"), wizInfo.dwOutputNodes );
		m_Dictionary[_T("USE_OUTPUT_NODES")] = _T("YES");
	}
}

void C$$Safe_root$$App::ProcessTemplates() {
	CString sMacro;
	CString sValue;
	CString sTmp;
	// Create the macros from the data collected through
	// the wizard's GUI
	CreateAndFillMacros();
	for ( POSITION pos=m_Dictionary.GetStartPosition();pos!=NULL; ) {
		m_Dictionary.GetNextAssoc( pos, sMacro, sValue );
		sTmp.Format( _T("$$s$$"), sMacro );
		sMacro = sTmp;
		for ( int i=0;i<m_saTemplatesBuf.GetSize();i++ ) {
			TRACE(_T("%s\n"), m_saTemplates[i]);
			int pos_if;
			while ( (pos_if=m_saTemplatesBuf[i].Find(_T("$$_IF_$$")))!= -1 ) {
				int pos_endif = pos_if;
				int if_counter = 0;
				int t_endif = 0;
				int t_if = pos_if;
				do {
					t_endif = m_saTemplatesBuf[i].Find( _T("$$_ENDIF_$$"), pos_endif+3 );
					t_if = m_saTemplatesBuf[i].Find( _T("$$_IF_$$"), t_if+3 );
					if_counter++;
					if ( t_endif != -1 )
						pos_endif = t_endif;
				}while( t_endif>t_if && t_endif!=-1 && t_if!=-1 );
				CString s;
				int p1 = m_saTemplatesBuf[i].Find( _T("("), pos_if );
				int p2 = m_saTemplatesBuf[i].Find( _T(")"), pos_if );
				if ( p1!=-1 && p2!=-1 ) {
					s = m_saTemplatesBuf[i].Mid( p1+1, p2-p1-1 );
					if ( pos_endif != -1 ) {
						if ( m_Dictionary.Lookup(s,sTmp) ) {
							m_saTemplatesBuf[i].Delete( pos_endif, 9 );
							m_saTemplatesBuf[i].Delete( pos_if, p2-pos_if+3 );
						}
						else {
							m_saTemplatesBuf[i].Delete( pos_if, pos_endif-pos_if+9 );
						}
					}
				}
				else { // error: open parenthesis
				}
			}
			int pos_beginloop;
			while ( (pos_beginloop=m_saTemplatesBuf[i].Find("$$_BEGINLOOP_$$"))!= -1 ) {
				CString s1;
				CString s2;
				int pos_endloop = m_saTemplatesBuf[i].Find( "$$_ENDLOOP_$$", pos_beginloop+9 );
				int p1 = m_saTemplatesBuf[i].Find( "(", pos_beginloop );
				int p2 = m_saTemplatesBuf[i].Find( ")", pos_beginloop );
				if ( p1!=-1 && p2!=-1 ) {
					sTmp = m_saTemplatesBuf[i].Mid( p1+1, p2-p1-1 );
					int pc = sTmp.Find( "," );
					if ( pc != -1 ) {
						s1 = sTmp.Mid( 0, pc );
						s2 = sTmp.Mid( pc+1, sTmp.GetLength()-pc );
					}
					else {
						s1 = sTmp;
					}
					if ( pos_endloop != -1 ) {
						if ( m_Dictionary.Lookup(s1,sTmp) ) {
							CString snew;
							CString sbetween = m_saTemplatesBuf[i].Mid( p2+3, pos_endloop-p2-3 );
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
							int n = atoi( sTmp );
							for ( int k=0;k<n;k++ ) {
								CString ss;
								ss.Format( _T("%s_%d"), s1, k );
								sTmp = sbetween;
								sTmp.Replace( s1, ss );
								if ( !s2.IsEmpty() ) {
									ss.Format( _T("%s_%d"), s2, k );
									sTmp.Replace( s2, ss );
								}
								snew += sTmp;
							}
							m_saTemplatesBuf[i].Insert( pos_beginloop, snew );
						}
						else {
							m_saTemplatesBuf[i].Delete( pos_beginloop, pos_endloop-pos_beginloop+11 );
						}
					}
				}
				else { // error: open parenthesis
				}
			}
			// replace macros in file names
			m_saTemplates[i].Replace( sMacro, sValue );
			// replace macros in file buffers
			m_saTemplatesBuf[i].Replace( sMacro, sValue );
		}
		// confirm text
		m_sConfirmBuf.Replace( sMacro, sValue );
	}
}

/////////////////////////////////////////////////////////////////////////////
// C$$Safe_root$$App initialization

BOOL C$$Safe_root$$App::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// set images
	CImageList	DefaultImages, Images;
	DefaultImages.Create(IDB_DEFAULT, 16, 0, RGB(0x00, 0x80, 0x80));
	Images.Create(IDB_IMAGES, 16, 0, RGB(0x00, 0x80, 0x80));

	// declare and create the only property (wizard) page
	TreePropSheet::CTreePropSheet wizardWnd( _T("$$root$$") );
	// declare and add the pages (wizard dialogs) to the wizard page
	// TODO: add as many are necessary to implement your wizard
	CWizDlg1 wndPage1;
	CWizDlg2 wndPage2;
	CWizDlg3 wndPage3;
	wizardWnd.AddPage( &wndPage1 );
	wizardWnd.AddPage( &wndPage2 );
	wizardWnd.AddPage( &wndPage3 );

	wizardWnd.SetEmptyPageText( _T("Please expand and select an item of '%s'.") );
	wizardWnd.SetTreeViewMode( TRUE, TRUE, TRUE );
	wizardWnd.SetTreeDefaultImages( &DefaultImages );
	wizardWnd.SetActivePage( &wndPage1 );

	// make sure the user's folder exists
	InitTemplatesFolder();
	// TODO: determine if the configuration files exist under
	// the user's folder.
	if ( !VerifyWizardConfigFile() ) {
		return FALSE;
	}
	m_pMainWnd = &wizardWnd;
	int iRes = wizardWnd.DoModal();
	if ( IDOK == iRes ) {
	}
	else {
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

/** < The global structure containing all of the wizard's parameters that
should be transfered to the macros.*/
WIZARDINFO wizInfo;

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Delphi
United States United States
Carlos Buelna works in the automotive industry.

Comments and Discussions