0024943: Port MFC samples to UNICODE for compatibility with VS2013
[occt.git] / samples / mfc / standard / Common / OCC_App.cpp
CommitLineData
5c1f974e 1// OCC_App.cpp: implementation of the OCC_App class.
2//
3//////////////////////////////////////////////////////////////////////
4
5#include <stdafx.h>
6
7#include "OCC_App.h"
b5ac8292 8#include "OCC_BaseDoc.h"
5c1f974e 9#include <res\OCC_Resource.h>
10
11#include <Standard_Version.hxx>
b5ac8292 12#include <OpenGl_GraphicDriver.hxx>
5c1f974e 13#include <OSD.hxx>
b5ac8292 14
5c1f974e 15#include "afxwin.h"
16
17/////////////////////////////////////////////////////////////////////////////
18// OCC_App
19
20BEGIN_MESSAGE_MAP(OCC_App, CWinApp)
21 //{{AFX_MSG_MAP(OCC_App)
22 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
23 // NOTE - the ClassWizard will add and remove mapping macros here.
24 // DO NOT EDIT what you see in these blocks of generated code!
25 //}}AFX_MSG_MAP
26 // Standard file based document commands
27 ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
28 ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
b5ac8292 29 ON_COMMAND(ID_BUTTON_STEREO, &OCC_App::OnStereo)
30 ON_UPDATE_COMMAND_UI(ID_BUTTON_STEREO, &OCC_App::OnUpdateStereo)
5c1f974e 31END_MESSAGE_MAP()
32
33/////////////////////////////////////////////////////////////////////////////
34// OCC_App construction
35
36OCC_App::OCC_App() : CWinApp()
37{
38 OSD::SetSignal (Standard_True);
39 SampleName = "";
40 SetSamplePath (NULL);
41 try
42 {
43 Handle(Aspect_DisplayConnection) aDisplayConnection;
65993a95 44 myGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
5c1f974e 45 }
46 catch(Standard_Failure)
47 {
576f8b11 48 AfxMessageBox (L"Fatal error during graphic initialization", MB_ICONSTOP);
5c1f974e 49 ExitProcess (1);
50 }
51}
52
53void OCC_App::SetSamplePath(LPCTSTR aPath)
54{
576f8b11 55 wchar_t anAbsoluteExecutableFileName[MAX_PATH + 1];
56 HMODULE hModule = GetModuleHandleW (NULL);
57 GetModuleFileNameW (hModule, anAbsoluteExecutableFileName, MAX_PATH);
5c1f974e 58
576f8b11 59 SamplePath = CString (anAbsoluteExecutableFileName);
5c1f974e 60 int index = SamplePath.ReverseFind('\\');
61 SamplePath.Delete(index+1, SamplePath.GetLength() - index - 1);
62 if (aPath == NULL)
63 SamplePath += "..";
64 else{
65 CString aCInitialDir(aPath);
66 SamplePath += "..\\" + aCInitialDir;
67 }
68}
69/////////////////////////////////////////////////////////////////////////////
70// CAboutDlgStd dialog used for App About
71
72class CAboutDlgStd : public CDialog
73{
74public:
75 CAboutDlgStd();
76 BOOL OnInitDialog();
77
78 // Dialog Data
79 //{{AFX_DATA(CAboutDlgStd)
80 enum { IDD = IDD_OCC_ABOUTBOX };
81 //}}AFX_DATA
82
83 // ClassWizard generated virtual function overrides
84 //{{AFX_VIRTUAL(CAboutDlgStd)
85protected:
86 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
87 //}}AFX_VIRTUAL
88
89
90// Implementation
91protected:
92 //{{AFX_MSG(CAboutDlgStd)
93 // No message handlers
94 //}}AFX_MSG
95 DECLARE_MESSAGE_MAP()
96
97public:
98 CString ReadmeText;
99};
100
101CAboutDlgStd::CAboutDlgStd() : CDialog(CAboutDlgStd::IDD)
102, ReadmeText(_T(""))
103{
104 //{{AFX_DATA_INIT(CAboutDlgStd)
105 //}}AFX_DATA_INIT
106}
107
108void CAboutDlgStd::DoDataExchange(CDataExchange* pDX)
109{
110 CDialog::DoDataExchange(pDX);
111 //{{AFX_DATA_MAP(CAboutDlgStd)
112 //}}AFX_DATA_MAP
113 DDX_Text(pDX, IDC_README, ReadmeText);
114}
115
116BEGIN_MESSAGE_MAP(CAboutDlgStd, CDialog)
117 //{{AFX_MSG_MAP(CAboutDlgStd)
118 // No message handlers
119 //}}AFX_MSG_MAP
120END_MESSAGE_MAP()
121
122BOOL CAboutDlgStd::OnInitDialog(){
123 CWnd* Title = GetDlgItem(IDC_ABOUTBOX_TITLE);
124
125 CString About = "About ";
126 CString Sample = "Sample ";
127 CString SampleName = ((OCC_App*)AfxGetApp())->GetSampleName();
128 CString Cascade = ", Open CASCADE Technology ";
129 CString Version = OCC_VERSION_STRING;
130
131 CString strTitle = Sample + SampleName + Cascade + Version;
132 CString dlgTitle = About + SampleName;
133
134 Title->SetWindowText(strTitle);
135 SetWindowText(dlgTitle);
b3837d74 136
137 CWnd* aReadmeEdit = GetDlgItem(IDC_README);
138 CFile aFile;
139 CString aHelpFilePath = CString (((OCC_App*)AfxGetApp())->GetInitDataDir()) + "\\README.txt";
140 if(aFile.Open (aHelpFilePath, CFile::modeRead))
5c1f974e 141 {
b3837d74 142 aReadmeEdit->ShowWindow(TRUE);
143 UINT aFileLength = (UINT)aFile.GetLength();
576f8b11 144 char* buffer = new char[aFileLength];
b3837d74 145 aFile.Read(buffer,aFileLength);
576f8b11 146 ReadmeText = buffer;
147 delete[] buffer;
148 ReadmeText.SetAt (aFileLength, '\0');
149 ReadmeText.Replace (L"\n", L"\r\n");
b3837d74 150 UpdateData(FALSE);
5c1f974e 151 }
152 else
153 {
b3837d74 154 aReadmeEdit->ShowWindow(FALSE);
5c1f974e 155 }
156
157 CenterWindow();
158 return TRUE;
159}
160
161// App command to run the dialog
162void OCC_App::OnAppAbout()
163{
164 CAboutDlgStd aboutDlg;
165 aboutDlg.DoModal();
166}
167
576f8b11 168const wchar_t* OCC_App::GetSampleName() const
5c1f974e 169{
576f8b11 170 return (const wchar_t* )SampleName;
5c1f974e 171}
172
576f8b11 173const wchar_t* OCC_App::GetInitDataDir() const
5c1f974e 174{
576f8b11 175 return (const wchar_t* )SamplePath;
5c1f974e 176}
177
576f8b11 178void OCC_App::SetSampleName (const wchar_t* theName)
5c1f974e 179{
576f8b11 180 SampleName = theName;
5c1f974e 181}
b5ac8292 182
183//=============================================================================
184// function: OnStereo
185// purpose:
186//=============================================================================
187void OCC_App::OnStereo()
188{
189 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (myGraphicDriver);
190
576f8b11 191 int anAnswer = MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd,
192 L"It is required to switch OpenGl context to turn on / off hardware stereo support. "
193 L"The document views need to be re-created to change \"GL\" context pixel format. "
194 L"This will close all current views and open new one (the model will be kept).\n"
195 L"Do you want to continue?", L"Enable/disable hardware stereo support", MB_OKCANCEL | MB_ICONQUESTION);
b5ac8292 196 if (anAnswer != IDOK)
197 {
198 return;
199 }
200
201 Standard_Boolean& aStereoMode = aDriver->ChangeOptions().contextStereo;
202
203 aStereoMode = !aStereoMode;
204
205 // reset document views
206 POSITION aTemplateIt = GetFirstDocTemplatePosition();
207
208 while (aTemplateIt != NULL)
209 {
210 CDocTemplate* aTemplate = (CDocTemplate*)GetNextDocTemplate (aTemplateIt);
211
212 POSITION aDocumentIt = aTemplate->GetFirstDocPosition();
213
214 while (aDocumentIt != NULL)
215 {
216 OCC_BaseDoc* aDocument = dynamic_cast<OCC_BaseDoc*> (aTemplate->GetNextDoc (aDocumentIt));
217 if (aDocument == NULL)
218 continue;
219
220 aDocument->ResetDocumentViews (aTemplate);
221 }
222 }
223}
224
225//=============================================================================
226// function: OnUpdateStereo
227// purpose:
228//=============================================================================
229void OCC_App::OnUpdateStereo (CCmdUI* theCmdUI)
230{
231 Handle(OpenGl_GraphicDriver) aDriver =
232 Handle(OpenGl_GraphicDriver)::DownCast (myGraphicDriver);
233
234 theCmdUI->SetCheck (!aDriver.IsNull() && aDriver->Options().contextStereo);
235}