0029589: Configuration - allow MFC samples to be built when OCCT is linked statically
[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
0553a8ea 36BOOL OCC_App::InitApplication()
5c1f974e 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 }
0553a8ea 51
52 return TRUE;
5c1f974e 53}
54
55void OCC_App::SetSamplePath(LPCTSTR aPath)
56{
576f8b11 57 wchar_t anAbsoluteExecutableFileName[MAX_PATH + 1];
58 HMODULE hModule = GetModuleHandleW (NULL);
59 GetModuleFileNameW (hModule, anAbsoluteExecutableFileName, MAX_PATH);
5c1f974e 60
576f8b11 61 SamplePath = CString (anAbsoluteExecutableFileName);
5c1f974e 62 int index = SamplePath.ReverseFind('\\');
63 SamplePath.Delete(index+1, SamplePath.GetLength() - index - 1);
64 if (aPath == NULL)
65 SamplePath += "..";
66 else{
67 CString aCInitialDir(aPath);
9e3ca93a 68 //SamplePath += "..\\" + aCInitialDir;
5c1f974e 69 }
70}
71/////////////////////////////////////////////////////////////////////////////
72// CAboutDlgStd dialog used for App About
73
74class CAboutDlgStd : public CDialog
75{
76public:
77 CAboutDlgStd();
78 BOOL OnInitDialog();
79
80 // Dialog Data
81 //{{AFX_DATA(CAboutDlgStd)
82 enum { IDD = IDD_OCC_ABOUTBOX };
83 //}}AFX_DATA
84
85 // ClassWizard generated virtual function overrides
86 //{{AFX_VIRTUAL(CAboutDlgStd)
87protected:
88 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
89 //}}AFX_VIRTUAL
90
91
92// Implementation
93protected:
94 //{{AFX_MSG(CAboutDlgStd)
95 // No message handlers
96 //}}AFX_MSG
97 DECLARE_MESSAGE_MAP()
98
99public:
100 CString ReadmeText;
101};
102
103CAboutDlgStd::CAboutDlgStd() : CDialog(CAboutDlgStd::IDD)
104, ReadmeText(_T(""))
105{
106 //{{AFX_DATA_INIT(CAboutDlgStd)
107 //}}AFX_DATA_INIT
108}
109
110void CAboutDlgStd::DoDataExchange(CDataExchange* pDX)
111{
112 CDialog::DoDataExchange(pDX);
113 //{{AFX_DATA_MAP(CAboutDlgStd)
114 //}}AFX_DATA_MAP
115 DDX_Text(pDX, IDC_README, ReadmeText);
116}
117
118BEGIN_MESSAGE_MAP(CAboutDlgStd, CDialog)
119 //{{AFX_MSG_MAP(CAboutDlgStd)
120 // No message handlers
121 //}}AFX_MSG_MAP
122END_MESSAGE_MAP()
123
124BOOL CAboutDlgStd::OnInitDialog(){
125 CWnd* Title = GetDlgItem(IDC_ABOUTBOX_TITLE);
126
127 CString About = "About ";
128 CString Sample = "Sample ";
129 CString SampleName = ((OCC_App*)AfxGetApp())->GetSampleName();
130 CString Cascade = ", Open CASCADE Technology ";
131 CString Version = OCC_VERSION_STRING;
132
133 CString strTitle = Sample + SampleName + Cascade + Version;
134 CString dlgTitle = About + SampleName;
135
136 Title->SetWindowText(strTitle);
137 SetWindowText(dlgTitle);
b3837d74 138
139 CWnd* aReadmeEdit = GetDlgItem(IDC_README);
140 CFile aFile;
47162471 141 CString aHelpFilePath = CString (((OCC_App*)AfxGetApp())->GetInitDataDir()) + L"\\README.txt";
b3837d74 142 if(aFile.Open (aHelpFilePath, CFile::modeRead))
5c1f974e 143 {
b3837d74 144 aReadmeEdit->ShowWindow(TRUE);
145 UINT aFileLength = (UINT)aFile.GetLength();
576f8b11 146 char* buffer = new char[aFileLength];
b3837d74 147 aFile.Read(buffer,aFileLength);
576f8b11 148 ReadmeText = buffer;
149 delete[] buffer;
576f8b11 150 ReadmeText.Replace (L"\n", L"\r\n");
47162471 151 UpdateData (FALSE);
5c1f974e 152 }
153 else
154 {
b3837d74 155 aReadmeEdit->ShowWindow(FALSE);
5c1f974e 156 }
157
158 CenterWindow();
159 return TRUE;
160}
161
162// App command to run the dialog
163void OCC_App::OnAppAbout()
164{
165 CAboutDlgStd aboutDlg;
166 aboutDlg.DoModal();
167}
168
576f8b11 169const wchar_t* OCC_App::GetSampleName() const
5c1f974e 170{
576f8b11 171 return (const wchar_t* )SampleName;
5c1f974e 172}
173
576f8b11 174const wchar_t* OCC_App::GetInitDataDir() const
5c1f974e 175{
576f8b11 176 return (const wchar_t* )SamplePath;
5c1f974e 177}
178
576f8b11 179void OCC_App::SetSampleName (const wchar_t* theName)
5c1f974e 180{
576f8b11 181 SampleName = theName;
5c1f974e 182}
b5ac8292 183
184//=============================================================================
185// function: OnStereo
186// purpose:
187//=============================================================================
188void OCC_App::OnStereo()
189{
190 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast (myGraphicDriver);
191
576f8b11 192 int anAnswer = MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd,
193 L"It is required to switch OpenGl context to turn on / off hardware stereo support. "
194 L"The document views need to be re-created to change \"GL\" context pixel format. "
195 L"This will close all current views and open new one (the model will be kept).\n"
196 L"Do you want to continue?", L"Enable/disable hardware stereo support", MB_OKCANCEL | MB_ICONQUESTION);
b5ac8292 197 if (anAnswer != IDOK)
198 {
199 return;
200 }
201
202 Standard_Boolean& aStereoMode = aDriver->ChangeOptions().contextStereo;
203
204 aStereoMode = !aStereoMode;
205
206 // reset document views
207 POSITION aTemplateIt = GetFirstDocTemplatePosition();
208
209 while (aTemplateIt != NULL)
210 {
211 CDocTemplate* aTemplate = (CDocTemplate*)GetNextDocTemplate (aTemplateIt);
212
213 POSITION aDocumentIt = aTemplate->GetFirstDocPosition();
214
215 while (aDocumentIt != NULL)
216 {
217 OCC_BaseDoc* aDocument = dynamic_cast<OCC_BaseDoc*> (aTemplate->GetNextDoc (aDocumentIt));
218 if (aDocument == NULL)
219 continue;
220
221 aDocument->ResetDocumentViews (aTemplate);
222 }
223 }
224}
225
226//=============================================================================
227// function: OnUpdateStereo
228// purpose:
229//=============================================================================
230void OCC_App::OnUpdateStereo (CCmdUI* theCmdUI)
231{
232 Handle(OpenGl_GraphicDriver) aDriver =
233 Handle(OpenGl_GraphicDriver)::DownCast (myGraphicDriver);
234
235 theCmdUI->SetCheck (!aDriver.IsNull() && aDriver->Options().contextStereo);
236}