Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Scribble / src / Mainfrm.cpp
1 ////////////////////////////////////////////////////\r
2 // Mainfrm.cpp  - definitions for the CMainFrame class\r
3 \r
4 #include "stdafx.h"\r
5 #include "mainfrm.h"\r
6 #include "resource.h"\r
7 \r
8 \r
9 CMainFrame::CMainFrame()\r
10 {\r
11         // Set m_View as the view window of the frame\r
12         SetView(m_View);\r
13 \r
14         // Set the registry key name, and load the initial window position\r
15         // Use a registry key name like "CompanyName\\Application"\r
16         LoadRegistrySettings(_T("Win32++\\Scribble Sample"));\r
17 \r
18         // Load the settings from the registry with 4 MRU entries\r
19         LoadRegistryMRUSettings(4);\r
20 }\r
21 \r
22 CMainFrame::~CMainFrame()\r
23 {\r
24 }\r
25 \r
26 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)\r
27 {\r
28         // Process the messages from the Menu and Tool Bar\r
29 \r
30         UNREFERENCED_PARAMETER(lParam);\r
31 \r
32         switch (LOWORD(wParam))\r
33         {\r
34         case IDM_FILE_NEW:\r
35                 m_View.ClearPoints();\r
36                 m_strPathName = _T("");\r
37                 return TRUE;\r
38         case IDM_FILE_OPEN:\r
39                 OnFileOpen();\r
40                 return TRUE;\r
41         case IDM_FILE_SAVE:\r
42                 OnFileSave();\r
43                 return TRUE;\r
44         case IDM_FILE_SAVEAS:\r
45                 OnFileSaveAs();\r
46                 return TRUE;\r
47         case IDM_FILE_PRINT:\r
48                 OnFilePrint();\r
49                 return TRUE;\r
50         case IDM_PEN_RED:\r
51                 TRACE(_T("Red pen selected\n"));\r
52                 m_View.SetPen(RGB(255,0,0));\r
53                 return TRUE;\r
54         case IDM_PEN_BLUE:\r
55                 TRACE(_T("Blue pen selected\n"));\r
56                 m_View.SetPen(RGB(0,0,255));\r
57                 return TRUE;\r
58         case IDM_PEN_GREEN:\r
59                 TRACE(_T("Green pen selected\n"));\r
60                 m_View.SetPen(RGB(0,196,0));\r
61                 return TRUE;\r
62         case IDM_PEN_BLACK:\r
63                 TRACE(_T("Black pen selected\n"));\r
64                 m_View.SetPen(RGB(0,0,0));\r
65                 return TRUE;\r
66         case IDW_VIEW_STATUSBAR:\r
67                 OnViewStatusBar();\r
68                 return TRUE;\r
69         case IDW_VIEW_TOOLBAR:\r
70                 OnViewToolBar();\r
71                 return TRUE;\r
72         case IDM_HELP_ABOUT:\r
73                 OnHelp();\r
74                 return TRUE;\r
75         case IDM_FILE_EXIT:\r
76                 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);\r
77                 return TRUE;\r
78         case IDW_FILE_MRU_FILE1:\r
79         case IDW_FILE_MRU_FILE2:\r
80         case IDW_FILE_MRU_FILE3:\r
81         case IDW_FILE_MRU_FILE4:\r
82         case IDW_FILE_MRU_FILE5:\r
83                 {\r
84                         UINT nMRUIndex = LOWORD(wParam) - IDW_FILE_MRU_FILE1;\r
85                         CString strMRUText = GetMRUEntry(nMRUIndex);\r
86 \r
87                         if (m_View.FileOpen(strMRUText))\r
88                                 m_strPathName = strMRUText;\r
89                         else\r
90                                 RemoveMRUEntry(strMRUText);\r
91 \r
92                         return TRUE;\r
93                 }\r
94         }\r
95 \r
96         return FALSE;\r
97 }\r
98 \r
99 void CMainFrame::OnFileOpen()\r
100 {\r
101         CFile File;\r
102         CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);\r
103 \r
104         if (!str.IsEmpty())\r
105         {\r
106                 // Retrieve the PlotPoint data\r
107                 if (m_View.FileOpen(str))\r
108                 {\r
109                         // Save the filename\r
110                         m_strPathName = str;\r
111                         AddMRUEntry(str);\r
112                 }\r
113                 else\r
114                         m_strPathName=_T("");\r
115         }\r
116 }\r
117 \r
118 void CMainFrame::OnFileSave()\r
119 {\r
120         if (m_strPathName == _T(""))\r
121                 OnFileSaveAs();\r
122         else\r
123                 m_View.FileSave(m_strPathName);\r
124 }\r
125 \r
126 void CMainFrame::OnFileSaveAs()\r
127 {\r
128         CFile File;\r
129         CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);\r
130 \r
131         // Store the PlotPoint data in the file\r
132         if (!str.IsEmpty())\r
133         {\r
134                 m_strPathName = str;\r
135 \r
136                 // Save the file name\r
137                 m_View.FileSave(str);\r
138                 AddMRUEntry(str);\r
139         }\r
140 }\r
141 \r
142 // Sends the bitmap extracted from the View window to a printer of your choice\r
143 // This function provides a useful reference for printing bitmaps in general\r
144 void CMainFrame::OnFilePrint()\r
145 {\r
146         // Get the dimensions of the View window\r
147         CRect rcView = m_View.GetClientRect();\r
148         int Width = rcView.Width();\r
149         int Height = rcView.Height();\r
150 \r
151         // Copy the bitmap from the View window\r
152         CClientDC ViewDC(&m_View);\r
153         CMemDC MemDC(&ViewDC);\r
154         CBitmap bmView;\r
155         bmView.CreateCompatibleBitmap(&ViewDC, Width, Height);\r
156         MemDC.SelectObject(&bmView);\r
157         BitBlt(MemDC, 0, 0, Width, Height, ViewDC, 0, 0, SRCCOPY);\r
158 \r
159         // Bring up a dialog to choose the printer\r
160         PRINTDLG pd = {0};\r
161         pd.lStructSize = sizeof( pd );\r
162         pd.Flags = PD_RETURNDC;\r
163         pd.hwndOwner = m_hWnd;\r
164 \r
165         // Retrieve the printer DC\r
166         if( !PrintDlg( &pd ) )\r
167         {\r
168                 TRACE(_T("PrintDlg canceled"));\r
169                 return;\r
170         }\r
171 \r
172         // Zero and then initialize the members of a DOCINFO structure.\r
173         DOCINFO di;\r
174         memset( &di, 0, sizeof(DOCINFO) );\r
175         di.cbSize = sizeof(DOCINFO);\r
176         di.lpszDocName = _T("Scribble Printout");\r
177         di.lpszOutput = (LPTSTR) NULL;\r
178         di.lpszDatatype = (LPTSTR) NULL;\r
179         di.fwType = 0;\r
180 \r
181         // Begin a print job by calling the StartDoc function.\r
182         if (SP_ERROR == StartDoc(pd.hDC, &di))\r
183                 throw CWinException(_T("Failed to start print job"));\r
184 \r
185         // Inform the driver that the application is about to begin sending data.\r
186         if (0 > StartPage(pd.hDC))\r
187                 throw CWinException(_T("StartPage failed"));\r
188 \r
189         BITMAPINFOHEADER bi = {0};\r
190         bi.biSize = sizeof(BITMAPINFOHEADER);\r
191         bi.biHeight = Height;\r
192         bi.biWidth = Width;\r
193         bi.biPlanes = 1;\r
194         bi.biBitCount =  24;\r
195         bi.biCompression = BI_RGB;\r
196 \r
197         // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps\r
198         // Get the size of the image data\r
199         MemDC.GetDIBits(&bmView, 0, Height, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
200 \r
201         // Retrieve the image data\r
202         std::vector<byte> vBits(bi.biSizeImage, 0);     // a vector to hold the byte array\r
203         byte* pByteArray = &vBits.front();\r
204         MemDC.GetDIBits(&bmView, 0, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
205 \r
206         // Determine the scaling factors required to print the bitmap and retain its original proportions.\r
207         float fLogPelsX1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSX);\r
208         float fLogPelsY1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSY);\r
209         float fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);\r
210         float fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);\r
211         float fScaleX = MAX(fLogPelsX1, fLogPelsX2) / MIN(fLogPelsX1, fLogPelsX2);\r
212         float fScaleY = MAX(fLogPelsY1, fLogPelsY2) / MIN(fLogPelsY1, fLogPelsY2);\r
213 \r
214     // Compute the coordinates of the upper left corner of the centered bitmap.\r
215         int cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);\r
216         int xLeft = ((cWidthPels / 2) - ((int) (((float) Width) * fScaleX)) / 2);\r
217         int cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);\r
218         int yTop = ((cHeightPels / 2) - ((int) (((float) Height) * fScaleY)) / 2);\r
219 \r
220     // Use StretchDIBits to scale the bitmap and maintain its original proportions\r
221         if (GDI_ERROR == (UINT)StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) Width * fScaleX),\r
222                 (int) ((float) Height * fScaleY), 0, 0, Width, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY))\r
223         {\r
224                 throw CWinException(_T("Failed to resize image for printing"));\r
225         }\r
226 \r
227         // Inform the driver that the page is finished.\r
228         if (0 > EndPage(pd.hDC))\r
229                 throw CWinException(_T("EndPage failed"));\r
230 \r
231         // Inform the driver that document has ended.\r
232         if(0 > EndDoc(pd.hDC))\r
233                 throw CWinException(_T("EndDoc failed"));\r
234 }\r
235 \r
236 void CMainFrame::OnInitialUpdate()\r
237 {\r
238         // Here we process the command line arguments, and automatically load a file if one is specified.\r
239         // GetCommandLineW retrieves our command line arguments.\r
240         // CommandLineToArgvW parses the command line arguements in to an array of strings\r
241         // The first string (lpArgv[0]) contains the name of our program\r
242         // The second string (lpArg[1]) contains an additional parameter (presumably a filename to load).\r
243         // CommandLineToArgvW is not supported in Win95, Win98 or WinME\r
244 \r
245 \r
246         // CommandLineToArgvW might not be supported, so use run-time dynamic linking to call the function\r
247         HMODULE hMod = LoadLibrary(_T("Shell32.dll"));\r
248         if (hMod)\r
249         {\r
250                 // Get a pointer to the CommandLineToArgvW function\r
251                 LPWSTR* (WINAPI* fpGetCommandLineW)(LPCWSTR, int*);\r
252                 fpGetCommandLineW = (LPWSTR* (WINAPI*)(LPCWSTR, int*))::GetProcAddress(hMod, "CommandLineToArgvW");\r
253 \r
254                 if (fpGetCommandLineW)\r
255                 {\r
256                         int argCount = 0;\r
257                         LPWSTR* lpArgv = (*fpGetCommandLineW)(::GetCommandLineW(), &argCount);\r
258 \r
259                         // The second argument (if any) contains our file name.\r
260                         if (argCount >= 2)\r
261                         {\r
262                                 m_View.FileOpen((W2T(lpArgv[1])));\r
263                         }\r
264 \r
265                         LocalFree(lpArgv);\r
266                 }\r
267 \r
268                 FreeLibrary(hMod);\r
269         } \r
270 \r
271 /*      \r
272         // This works on Win2000 and above\r
273         int argCount = 0;\r
274         LPWSTR* lpArgv = ::CommandLineToArgvW(::GetCommandLineW(), &argCount);\r
275 \r
276         // The second argument (if any) contains our file name.\r
277         if (argCount >= 2)\r
278         {\r
279                 m_View.FileOpen((W2T(lpArgv[1])));\r
280         }\r
281 */\r
282 }\r
283 \r
284 void CMainFrame::SetupToolBar()\r
285 {\r
286         // Define our toolbar\r
287         AddToolBarButton( IDM_FILE_NEW   );\r
288         AddToolBarButton( IDM_FILE_OPEN  );\r
289         AddToolBarButton( IDM_FILE_SAVE  );\r
290         AddToolBarButton( 0 );                          // Separator\r
291         AddToolBarButton( IDM_EDIT_CUT,   FALSE );\r
292         AddToolBarButton( IDM_EDIT_COPY,  FALSE );\r
293         AddToolBarButton( IDM_EDIT_PASTE, FALSE );\r
294         AddToolBarButton( IDM_FILE_PRINT );\r
295         AddToolBarButton( 0 );                          // Separator\r
296         AddToolBarButton( IDM_PEN_RED    );\r
297         AddToolBarButton( IDM_PEN_BLUE   );\r
298         AddToolBarButton( IDM_PEN_GREEN  );\r
299         AddToolBarButton( IDM_PEN_BLACK  );\r
300         AddToolBarButton( 0 );                          // Separator\r
301         AddToolBarButton( IDM_HELP_ABOUT );\r
302 }\r
303 \r
304 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
305 {\r
306 //      switch (uMsg)\r
307 //      {\r
308 \r
309 //      } // switch (uMsg)\r
310 \r
311         return WndProcDefault(uMsg, wParam, lParam);\r
312 } // LRESULT CMainFrame::WndProc(...)\r
313 \r

UCC git Repository :: git.ucc.asn.au