Click here to Skip to main content
15,891,938 members
Articles / Desktop Programming / MFC

The Diffraction Grating Calculator

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
30 Oct 2010GPL38 min read 41.5K   678   6  
An MFC Windows program related to Concave Diffraction Gratings using VTK libraries.
// GratingCalculator.cpp : Defines the class behaviors for the application.

#include "stdafx.h"
#include "afxwinappex.h"
#include "GratingCalculator.h"
#include "MainFrm.h"

#include "GratingCalculatorDoc.h"
#include "GratingCalculatorView.h"

#include "vtkGarbageCollector.h"

#include "SettingSingleton.h"
#include "Directory.h"
#include "LoggerMacro.h"
#include "GenericObj.h"

#include "SimRunEngineSingleton.h"
#include "Sim3DFEngineSingleton.h"
#include "DataHolderSingleton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CGratingCalculatorApp

BEGIN_MESSAGE_MAP(CGratingCalculatorApp, CWinAppEx)
	ON_COMMAND(ID_APP_ABOUT, &CGratingCalculatorApp::OnAppAbout)
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinAppEx::OnFilePrintSetup)
END_MESSAGE_MAP()

// CGratingCalculatorApp construction

CGratingCalculatorApp::CGratingCalculatorApp()
{
	m_bHiColorIcons = TRUE;

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

// The one and only CGratingCalculatorApp object

CGratingCalculatorApp theApp;

// CGratingCalculatorApp initialization

BOOL CGratingCalculatorApp::InitInstance()
{
   // vtk garbage collector ->
   ::vtkGarbageCollector::DeferredCollectionPush();
   // vtk garbage collector <-

   // InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinAppEx::InitInstance();

	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}

	// 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
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("SOPRA GROUP"));
	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)

   // Grating Calculator Specific Init ->

   // Application-wide settings
   CSettingSingleton::GetInstance()->LoadSettings();
   CSettingSingleton::GetInstance()->SaveSettings();

   // Ensure the creation of default directories
   CString strRootDir;
   CSettingSingleton::GetInstance()->GetAttribute(SS_APPLICATION_PATH)->Get_String(strRootDir);
   CDirectory::CreateFileOrDirFullPath(strRootDir.GetBuffer(MAX_PATH),true);
   strRootDir.ReleaseBuffer();

   // The Logger file
   
   CString strLogFile;
   CSettingSingleton::GetInstance()->GetAttribute(SS_LOGFILE_PATH)->Get_String(strLogFile);
   CDirectory::DeleteGivenFile(strLogFile.GetBuffer(MAX_PATH));
   strLogFile.ReleaseBuffer();

   // The Logger
   CSingleLogger::GetInstance()->LoadDefaults();
   CSingleLogger::GetInstance()->CheckAndSetTraceFileName(strLogFile.GetBuffer(MAX_PATH));

   // Start logging
   LTRACE(LOG_TRLVL_ERROR,_T("Program Starts"));

   // Set the timezone to UTC
   if( !CGenericObj::SetTimezoneToUTC() )
   {
      AfxMessageBox(_T("Warning: time_t 64bits values may be corrected for localtime."));
   }
   // Grating Calculator Specific Init <-

	InitContextMenuManager();

	InitKeyboardManager();

	InitTooltipManager();
	CMFCToolTipInfo ttParams;
	ttParams.m_bVislManagerTheme = TRUE;
	theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
		RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CGratingCalculatorDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CGratingCalculatorView));
	if (!pDocTemplate)
		return FALSE;
	AddDocTemplate(pDocTemplate);


	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);


	// Dispatch commands specified on the command line.  Will return FALSE if
	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();
	// call DragAcceptFiles only if there's a suffix
	//  In an SDI app, this should occur after ProcessShellCommand
	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();
	return TRUE;
}

// CAboutDlg dialog used for App About

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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

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

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

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

// CGratingCalculatorApp customization load/save methods

void CGratingCalculatorApp::PreLoadState()
{
	BOOL bNameValid;
	CString strName;
	bNameValid = strName.LoadString(IDS_EDIT_MENU);
	ASSERT(bNameValid);
	GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
}

void CGratingCalculatorApp::LoadCustomState()
{
}

void CGratingCalculatorApp::SaveCustomState()
{
}

// CGratingCalculatorApp message handlers

int CGratingCalculatorApp::ExitInstance()
{
   // Stop logging
   LTRACE(LOG_TRLVL_ERROR,_T("Program Ends!"));

   // Save settings in INI
   CSettingSingleton::GetInstance()->SaveSettings();
   CSettingSingleton::DestroySingleton();

   CSimRunEngineSingleton::DestroySingleton();
   CSim3DFEngineSingleton::DestroySingleton();
   CDataHolderSingleton::DestroySingleton();

   // vtk garbage collector ->
   ::vtkGarbageCollector::DeferredCollectionPop();
   ::vtkGarbageCollector::Collect();
   // vtk garbage collector <-

   return CWinAppEx::ExitInstance();
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions