d1b982d0be6335ae4caa04752ff2d8288084e0ed
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Scribble / src / View.cpp
1 //////////////////////////////////////////////\r
2 // View.cpp\r
3 //  Definitions for the CView class\r
4 \r
5 #include "stdafx.h"\r
6 #include "view.h"\r
7 #include "resource.h"\r
8 \r
9 \r
10 using namespace std;\r
11 \r
12 CView::CView() : m_PenColor(RGB(0,0,0))\r
13 {\r
14         m_Brush.CreateSolidBrush(RGB(255,255,230));\r
15 }\r
16 \r
17 CView::~CView()\r
18 {\r
19 }\r
20 \r
21 void CView::ClearPoints()\r
22 {\r
23         m_points.clear();\r
24         Invalidate();\r
25 }\r
26 \r
27 void CView::DrawLine(int x, int y)\r
28 {\r
29         CClientDC dcClient(this);\r
30         dcClient.CreatePen(PS_SOLID, 1, m_points.back().color);\r
31         dcClient.MoveTo(m_points.back().x, m_points.back().y);\r
32         dcClient.LineTo(x, y);\r
33 }\r
34 \r
35 void CView::OnDraw(CDC* pDC)\r
36 {\r
37 \r
38         // Here we use double buffering (drawing to a memory DC) for smoother rendering\r
39         // Set up our Memory DC and bitmap\r
40         CMemDC MemDC(pDC);\r
41         int Width = GetClientRect().Width();\r
42         int Height = GetClientRect().Height();\r
43         MemDC.CreateCompatibleBitmap(pDC, Width, Height);\r
44         MemDC.FillRect(GetClientRect(), &m_Brush);\r
45 \r
46         if (m_points.size() > 0)\r
47         {\r
48                 bool bDraw = false;  //Start with the pen up\r
49                 for (unsigned int i = 0 ; i < m_points.size(); i++)\r
50                 {\r
51 \r
52                         MemDC.CreatePen(PS_SOLID, 1, m_points[i].color);\r
53                         if (bDraw)\r
54                                 MemDC.LineTo(m_points[i].x, m_points[i].y);\r
55                         else\r
56                                 MemDC.MoveTo(m_points[i].x, m_points[i].y);\r
57 \r
58                         bDraw = m_points[i].PenDown;\r
59                 }\r
60         }\r
61 \r
62         // Copy from the memory DC to our painting dc\r
63         pDC->BitBlt(0, 0, Width, Height, &MemDC, 0, 0, SRCCOPY);\r
64 }\r
65 \r
66 void CView::PreCreate(CREATESTRUCT &cs)\r
67 {\r
68         // Set the extra style to provide a sunken effect\r
69         cs.dwExStyle = WS_EX_CLIENTEDGE;\r
70 }\r
71 \r
72 void CView::PreRegisterClass(WNDCLASS &wc)\r
73 {\r
74         // Set the background brush, class name and cursor\r
75         wc.hbrBackground = m_Brush;\r
76         wc.lpszClassName = _T("Scribble Window");\r
77         wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));\r
78 }\r
79 \r
80 BOOL CView::FileOpen(LPCTSTR szFilename)\r
81 {\r
82         // empty the PlotPoint vector\r
83         m_points.clear();\r
84         DWORD nBytesRead;\r
85         BOOL bResult = FALSE;\r
86 \r
87         // Create a handle to the file\r
88         CFile File;\r
89         if (File.Open(szFilename, OPEN_EXISTING))\r
90         {\r
91                 do\r
92                 {\r
93                         PlotPoint pp;\r
94                         nBytesRead = File.Read(&pp, sizeof(PlotPoint));\r
95                         if (nBytesRead == sizeof(PlotPoint))\r
96                                 m_points.push_back(pp);\r
97 \r
98                 } while (nBytesRead == sizeof(PlotPoint));\r
99 \r
100                 if ((0 != nBytesRead) || (m_points.empty()))\r
101                 {\r
102                         // Failed to read all of the file\r
103                         m_points.clear();\r
104                         ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
105                 }\r
106                 else\r
107                         bResult = TRUE;\r
108 \r
109         }\r
110         else\r
111         {\r
112                 CString strErrMsg = _T("Failed to open file ");\r
113                 strErrMsg += szFilename;\r
114                 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
115         }\r
116 \r
117         Invalidate();\r
118         return bResult;\r
119 }\r
120 \r
121 BOOL CView::FileSave(LPCTSTR szFilename)\r
122 {\r
123         BOOL bResult = TRUE;\r
124         CFile hFile;\r
125         if (!hFile.Open(szFilename, CREATE_ALWAYS))\r
126         {\r
127                 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
128                 bResult = FALSE;\r
129         }\r
130 \r
131         if (bResult)\r
132         {\r
133                 // Write the file\r
134                 for (size_t i = 0; i < m_points.size(); ++i)\r
135                 {\r
136                         if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))\r
137                         {\r
138                                 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
139                                 bResult = FALSE;\r
140                                 break;\r
141                         }\r
142                 }\r
143 \r
144                 // Verify file length\r
145                 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))\r
146                 {\r
147                         ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
148                         bResult = FALSE;\r
149                 }\r
150         }\r
151 \r
152         return bResult;\r
153 }\r
154 \r
155 void CView::SetPen(COLORREF color)\r
156 {\r
157         m_PenColor = color;\r
158 }\r
159 \r
160 void CView::StorePoint(int x, int y, bool PenDown)\r
161 {\r
162         PlotPoint P1;\r
163         P1.x = x;\r
164         P1.y = y;\r
165         P1.PenDown = PenDown;\r
166         P1.color = m_PenColor;\r
167 \r
168         m_points.push_back(P1); //Add the point to the vector\r
169 }\r
170 \r
171 void CView::OnLButtonDown(LPARAM lParam)\r
172 {\r
173         // Capture mouse input.\r
174         SetCapture();\r
175 \r
176         StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
177 }\r
178 \r
179 void CView::OnLButtonUp(LPARAM lParam)\r
180 {\r
181         {\r
182                 //Release the capture on the mouse\r
183                 ReleaseCapture();\r
184 \r
185                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);\r
186         }\r
187 }\r
188 \r
189 void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)\r
190 {\r
191         // hold down the left mouse button and move mouse to draw lines.\r
192         if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )\r
193         {\r
194                 CString str;\r
195                 str.Format( _T("Draw Point:  %hd, %hd\n"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) );\r
196                 TRACE(str);\r
197 \r
198                 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\r
199                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
200         }\r
201 }\r
202 \r
203 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
204 {\r
205         switch (uMsg)\r
206         {\r
207         case WM_LBUTTONDOWN:\r
208                 OnLButtonDown(lParam);\r
209                 break;\r
210 \r
211         case WM_MOUSEMOVE:\r
212                 OnMouseMove(wParam, lParam);\r
213         break;\r
214 \r
215     case WM_LBUTTONUP:\r
216                 OnLButtonUp(lParam);\r
217                 break;\r
218         }\r
219 \r
220         //Use the default message handling for remaining messages\r
221         return WndProcDefault(uMsg, wParam, lParam);\r
222 }\r
223 \r

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