f8527ce3f3a17a9bb6543e93316f0a85b78aa82c
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / tutorials / Tutorial9 / Mainfrm.cpp
1 ////////////////////////////////////////////////////\r
2 // Mainfrm.cpp  - definitions for the CMainFrame class\r
3 \r
4 #include "mainfrm.h"\r
5 #include "resource.h"\r
6 \r
7 \r
8 CMainFrame::CMainFrame()\r
9 {\r
10         // Set m_View as the view window of the frame\r
11         SetView(m_View);\r
12 }\r
13 \r
14 CMainFrame::~CMainFrame()\r
15 {\r
16 }\r
17 \r
18 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)\r
19 {\r
20         // Process the messages from the Menu and Tool Bar\r
21         switch (LOWORD(wParam))\r
22         {\r
23         case IDM_FILE_NEW:\r
24                 m_View.ClearPoints();\r
25                 m_PathName = _T("");\r
26                 return TRUE;\r
27         case IDM_FILE_OPEN:\r
28                 OnFileOpen();\r
29                 return TRUE;\r
30         case IDM_FILE_SAVE:\r
31                 OnFileSave();\r
32                 return TRUE;\r
33         case IDM_FILE_SAVEAS:\r
34                 OnFileSaveAs();\r
35                 return TRUE;\r
36         case IDM_FILE_PRINT:\r
37                 OnFilePrint();\r
38                 return TRUE;\r
39         case IDM_PEN_RED:\r
40                 m_View.SetPen(RGB(255,0,0));\r
41                 return TRUE;\r
42         case IDM_PEN_BLUE:\r
43                 m_View.SetPen(RGB(0,0,255));\r
44                 return TRUE;\r
45         case IDM_PEN_GREEN:\r
46                 m_View.SetPen(RGB(0,196,0));\r
47                 return TRUE;\r
48         case IDM_PEN_BLACK:\r
49                 m_View.SetPen(RGB(0,0,0));\r
50                 return TRUE;\r
51         case IDM_HELP_ABOUT:\r
52                 OnHelp();\r
53                 return TRUE;\r
54         case IDM_FILE_EXIT:\r
55                 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);\r
56                 return TRUE;\r
57         }\r
58 \r
59         return FALSE;\r
60 }\r
61 \r
62 void CMainFrame::OnFileOpen()\r
63 {\r
64         CFile File;\r
65         CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);\r
66 \r
67         if (!str.IsEmpty())\r
68         {\r
69                 // Retrieve the PlotPoint data\r
70                 if (m_View.FileOpen(str))\r
71                 {\r
72                         // Save the filename\r
73                         m_PathName = str;\r
74                         AddMRUEntry(str);\r
75                 }\r
76                 else\r
77                         m_PathName=_T("");\r
78         }\r
79 }\r
80 \r
81 void CMainFrame::OnFileSave()\r
82 {\r
83         if (m_PathName == _T(""))\r
84                 OnFileSaveAs();\r
85         else\r
86                 m_View.FileSave(m_PathName);\r
87 }\r
88 \r
89 void CMainFrame::OnFileSaveAs()\r
90 {\r
91         CFile File;\r
92         CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);\r
93 \r
94         // Store the PlotPoint data in the file\r
95         if (!str.IsEmpty())\r
96         {\r
97                 m_PathName = str;\r
98 \r
99                 // Save the file name\r
100                 m_View.FileSave(str);\r
101                 AddMRUEntry(str);\r
102         }\r
103 }\r
104 \r
105 // Sends the bitmap extracted from the View window to a printer of your choice\r
106 // This function provides a useful reference for printing bitmaps in general\r
107 void CMainFrame::OnFilePrint()\r
108 {\r
109         // Get the dimensions of the View window\r
110         CRect rcView = m_View.GetClientRect();\r
111         int Width = rcView.Width();\r
112         int Height = rcView.Height();\r
113 \r
114         // Copy the bitmap from the View window\r
115         CClientDC dcView(&m_View);\r
116         CMemDC MemDC(&dcView);\r
117         CBitmap bmView;\r
118         bmView.CreateCompatibleBitmap(&dcView, Width, Height);\r
119         MemDC.SelectObject(&bmView);\r
120         MemDC.BitBlt(0, 0, Width, Height, &dcView, 0, 0, SRCCOPY);\r
121 \r
122         // Bring up a dialog to choose the printer\r
123         PRINTDLG pd = {0};\r
124         pd.lStructSize = sizeof( pd );\r
125         pd.Flags = PD_RETURNDC;\r
126         pd.hwndOwner = m_hWnd;\r
127 \r
128         // Retrieve the printer DC\r
129         if( !PrintDlg( &pd ) )\r
130         {\r
131                 TRACE(_T("PrintDlg canceled"));\r
132                 return;\r
133         }\r
134 \r
135         // Zero and then initialize the members of a DOCINFO structure.\r
136         DOCINFO di;\r
137         memset( &di, 0, sizeof(DOCINFO) );\r
138         di.cbSize = sizeof(DOCINFO);\r
139         di.lpszDocName = _T("Scribble Printout");\r
140         di.lpszOutput = (LPTSTR) NULL;\r
141         di.lpszDatatype = (LPTSTR) NULL;\r
142         di.fwType = 0;\r
143 \r
144         // Begin a print job by calling the StartDoc function.\r
145         if (SP_ERROR == StartDoc(pd.hDC, &di))\r
146                 throw CWinException(_T("Failed to start print job"));\r
147 \r
148         // Inform the driver that the application is about to begin sending data.\r
149         if (0 > StartPage(pd.hDC))\r
150                 throw CWinException(_T("StartPage failed"));\r
151 \r
152         BITMAPINFOHEADER bi = {0};\r
153         bi.biSize = sizeof(BITMAPINFOHEADER);\r
154         bi.biHeight = Height;\r
155         bi.biWidth = Width;\r
156         bi.biPlanes = 1;\r
157         bi.biBitCount =  24;\r
158         bi.biCompression = BI_RGB;\r
159 \r
160         // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps\r
161         // Get the size of the image data\r
162         MemDC.GetDIBits(&bmView, 0, Height, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
163 \r
164         // Retrieve the image data\r
165         std::vector<byte> vBits(bi.biSizeImage, 0);     // a vector to hold the byte array\r
166         byte* pByteArray = &vBits.front();\r
167         MemDC.GetDIBits(&bmView, 0, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
168 \r
169         // Determine the scaling factors required to print the bitmap and retain its original proportions.\r
170         float fLogPelsX1 = (float) dcView.GetDeviceCaps(LOGPIXELSX);\r
171         float fLogPelsY1 = (float) dcView.GetDeviceCaps(LOGPIXELSY);\r
172         float fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);\r
173         float fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);\r
174         float fScaleX = MAX(fLogPelsX1, fLogPelsX2) / MIN(fLogPelsX1, fLogPelsX2);\r
175         float fScaleY = MAX(fLogPelsY1, fLogPelsY2) / MIN(fLogPelsY1, fLogPelsY2);\r
176 \r
177     // Compute the coordinates of the upper left corner of the centered bitmap.\r
178         int cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);\r
179         int xLeft = ((cWidthPels / 2) - ((int) (((float) Width) * fScaleX)) / 2);\r
180         int cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);\r
181         int yTop = ((cHeightPels / 2) - ((int) (((float) Height) * fScaleY)) / 2);\r
182 \r
183     // Use StretchDIBits to scale the bitmap and maintain its original proportions\r
184         if (GDI_ERROR == (UINT)StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) Width * fScaleX),\r
185                 (int) ((float) Height * fScaleY), 0, 0, Width, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY))\r
186         {\r
187                 throw CWinException(_T("Failed to resize image for printing"));\r
188         }\r
189 \r
190         // Inform the driver that the page is finished.\r
191         if (0 > EndPage(pd.hDC))\r
192                 throw CWinException(_T("EndPage failed"));\r
193 \r
194         // Inform the driver that document has ended.\r
195         if(0 > EndDoc(pd.hDC))\r
196                 throw CWinException(_T("EndDoc failed"));\r
197 }\r
198 \r
199 void CMainFrame::SetupToolBar()\r
200 {\r
201         // Set the Resource IDs for the toolbar buttons\r
202         AddToolBarButton( IDM_FILE_NEW   );\r
203         AddToolBarButton( IDM_FILE_OPEN  );\r
204         AddToolBarButton( IDM_FILE_SAVE  );\r
205         \r
206         AddToolBarButton( 0 );                          // Separator\r
207         AddToolBarButton( IDM_EDIT_CUT );\r
208         AddToolBarButton( IDM_EDIT_COPY );\r
209         AddToolBarButton( IDM_EDIT_PASTE );\r
210         \r
211         AddToolBarButton( 0 );                          // Separator\r
212         AddToolBarButton( IDM_FILE_PRINT );\r
213         \r
214         AddToolBarButton( 0 );                          // Separator\r
215         AddToolBarButton ( IDM_PEN_RED    );    \r
216         AddToolBarButton ( IDM_PEN_BLUE   );\r
217         AddToolBarButton ( IDM_PEN_GREEN  );\r
218         AddToolBarButton ( IDM_PEN_BLACK  );\r
219         AddToolBarButton ( IDM_HELP_ABOUT );\r
220 }\r
221 \r
222 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
223 {\r
224 //      switch (uMsg)\r
225 //      {\r
226 \r
227 //      } // switch (uMsg)\r
228 \r
229         return WndProcDefault(uMsg, wParam, lParam);\r
230 } // LRESULT CMainFrame::WndProc(...)\r
231 \r

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