Click here to Skip to main content
15,893,668 members
Articles / Desktop Programming / MFC

Another Enum Viewer

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
22 Oct 20015 min read 83K   1.3K   19  
An article on the usage and design of another Enum Viewer
// enum_viewer.cpp : Defines the class behaviors for the application.
//
// $Header$
//
// All of this source code is copyright (C) 2001 Tim Finer.
// You have permission to use this code in any way.  
// My only restriction is that you must not claim you wrote this code.
//

#include "stdafx.h"
#include "enum_viewer.h"

#include "main_frame.h"
#include "enum_doc.h"
#include "enum_view.h"

#include <afxadv.h>		// CRecentFileList usage of m_pRecentFileList
#include <process.h>	// spawn

#include "resource.h"


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


using namespace tfiner;


CEnumViewerApp theApp;


CEnumViewerApp::CEnumViewerApp(){}


BOOL CEnumViewerApp::InitInstance() {

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

	// Change the registry key under which our settings are stored.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register document templates

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CEnumDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CEnumView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
//	if ( CCommandLineInfo::FileNew == cmdInfo.m_nShellCommand )
//		cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


class CAboutDlg : public CDialog {
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}



BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CEnumViewerApp::OnAppAbout() {
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}


bool CEnumViewerApp::OpenHeaderFile( LPCTSTR HEADER ) {
	const TCHAR* pTEMP_FILE = _ttmpnam(0);
	if ( !pTEMP_FILE ) {
		AfxMessageBox( IDS_COULDNT_GENERATE_TEMPFILE );
		return false;
	}

	TCHAR modulePath[ _MAX_PATH ];
	if ( !GetModuleFileName(0, &modulePath[0], _MAX_PATH) ) {
		AfxMessageBox( IDS_COULDNT_GET_MOD_PATH );
		return false;
	}

	TCHAR viewerDrive[_MAX_DRIVE];
	TCHAR viewerPath[_MAX_DIR];
	_tsplitpath( modulePath, viewerDrive, viewerPath, 0, 0 );

	CString enumPath( viewerDrive );
	enumPath += viewerPath;
	enumPath += _T("enum_parser.exe");
 
	CWaitCursor pointer;

	bool processedHeader = false;
	int spawnRes = _spawnl( _P_WAIT, 
		(LPCTSTR)enumPath, _T("enum_parser"), 
		_T("-dump"), pTEMP_FILE, HEADER, NULL );

	if ( 0 == spawnRes ) {
		POSITION pos = GetFirstDocTemplatePosition();
		ASSERT( pos );
		CDocTemplate* pTempl = GetNextDocTemplate( pos );
		ASSERT( pTempl );

		CDocument* pDoc = pTempl->OpenDocumentFile( pTEMP_FILE );

		if ( pDoc ) {
			pDoc->SetTitle( HEADER );
			pDoc->UpdateFrameCounts();

			// remove temp file added, add header file
			m_pRecentFileList->Remove( 0 );
			m_pRecentFileList->Add( HEADER );

			processedHeader = true;
		}

	} else if ( ENOENT == errno )
		AfxMessageBox( IDS_COULDNT_FIND_PARSER );

	return processedHeader;
}


void CEnumViewerApp::OnFileOpen() {
	CString headerFilter;
	headerFilter.LoadString( IDS_HEADER_FILTER );
	CFileDialog openDlg( 
		TRUE, 0, 0, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST,
		(LPCTSTR)headerFilter );

	if ( IDOK != openDlg.DoModal() )
		return;

	OpenHeaderFile( openDlg.GetPathName() );
}


void CEnumViewerApp::OnFileMru( UINT mru ) {
	int index = mru - ID_FILE_MRU_FIRST;
	CString& header = (*m_pRecentFileList)[index];
	OpenHeaderFile( header );
}


BEGIN_MESSAGE_MAP(CEnumViewerApp, CWinApp)
	//{{AFX_MSG_MAP(CEnumViewerApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//	ON_COMMAND(ID_FILE_MRU_FILE1, OnFileMru)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)

	// Most Recently Used Files
	ON_COMMAND_RANGE( ID_FILE_MRU_FIRST, ID_FILE_MRU_LAST, OnFileMru )
END_MESSAGE_MAP()


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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions