Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / tutorials / Tutorial8 / View.cpp
1 //////////////////////////////////////////////\r
2 // View.cpp\r
3 //  Definitions for the CView class\r
4 \r
5 #include "view.h"\r
6 #include "GDI.h"\r
7 #include "file.h"\r
8 #include "resource.h"\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         if (m_points.size() > 0)\r
38         {\r
39                 bool bDraw = false;  //Start with the pen up\r
40                 for (unsigned int i = 0 ; i < m_points.size(); i++)\r
41                 {\r
42                         pDC->CreatePen(PS_SOLID, 1, m_points[i].color);\r
43                         if (bDraw)\r
44                                 pDC->LineTo(m_points[i].x, m_points[i].y);\r
45                         else\r
46                                 pDC->MoveTo(m_points[i].x, m_points[i].y);\r
47                         \r
48                         bDraw = m_points[i].PenDown;\r
49                 }\r
50         }\r
51 }\r
52 \r
53 void CView::PreCreate(CREATESTRUCT &cs)\r
54 {\r
55         // Set the extra style to provide a sunken effect\r
56         cs.dwExStyle = WS_EX_CLIENTEDGE;\r
57 }\r
58 \r
59 void CView::PreRegisterClass(WNDCLASS &wc)\r
60 {\r
61         // Set the background brush, class name and cursor\r
62         wc.hbrBackground = m_Brush;\r
63         wc.lpszClassName = _T("Scribble Window");\r
64         wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));\r
65 }\r
66 \r
67 BOOL CView::FileOpen(LPCTSTR szFilename)\r
68 {\r
69         // empty the PlotPoint vector\r
70         m_points.clear();\r
71         DWORD nBytesRead;\r
72         BOOL bResult = FALSE;\r
73 \r
74         // Create a handle to the file\r
75         CFile File;\r
76         if (File.Open(szFilename, OPEN_EXISTING))\r
77         {\r
78                 do\r
79                 {\r
80                         PlotPoint pp;\r
81                         nBytesRead = File.Read(&pp, sizeof(PlotPoint));\r
82                         if (nBytesRead == sizeof(PlotPoint))\r
83                                 m_points.push_back(pp); \r
84 \r
85                 } while (nBytesRead == sizeof(PlotPoint));\r
86 \r
87                 if ((0 != nBytesRead) || (m_points.empty()))\r
88                 {\r
89                         // Failed to read all of the file\r
90                         m_points.clear();\r
91                         ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
92                 }\r
93                 else\r
94                         bResult = TRUE;\r
95 \r
96         }\r
97         else\r
98         {\r
99                 CString strErrMsg = _T("Failed to open file ");\r
100                 strErrMsg += szFilename;\r
101                 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
102         }\r
103 \r
104         Invalidate();\r
105         return bResult;\r
106 }\r
107 \r
108 BOOL CView::FileSave(LPCTSTR szFilename)\r
109 {\r
110         BOOL bResult = TRUE;\r
111         CFile hFile;\r
112         if (!hFile.Open(szFilename, CREATE_ALWAYS))\r
113         {\r
114                 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
115                 bResult = FALSE;\r
116         }\r
117         \r
118         if (bResult)\r
119         {\r
120                 // Write the file\r
121                 for (size_t i = 0; i < m_points.size(); ++i)\r
122                 {\r
123                         if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))\r
124                         {\r
125                                 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
126                                 bResult = FALSE;\r
127                                 break;\r
128                         }\r
129                 }\r
130 \r
131                 // Verify file length\r
132                 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))\r
133                 {\r
134                         ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
135                         bResult = FALSE;\r
136                 }\r
137         }\r
138 \r
139         return bResult;\r
140 }\r
141 \r
142 void CView::SetPen(COLORREF color)\r
143 {\r
144         m_PenColor = color;\r
145 }\r
146 \r
147 void CView::StorePoint(int x, int y, bool PenDown)\r
148 {\r
149         PlotPoint P1;\r
150         P1.x = x;\r
151         P1.y = y;\r
152         P1.PenDown = PenDown;\r
153         P1.color = m_PenColor;\r
154 \r
155         m_points.push_back(P1); //Add the point to the vector\r
156 }\r
157 \r
158 void CView::OnLButtonDown(LPARAM lParam)\r
159 {\r
160         // Capture mouse input.\r
161         SetCapture();\r
162 \r
163         StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
164 }\r
165 \r
166 void CView::OnLButtonUp(LPARAM lParam)\r
167 {\r
168         {\r
169                 //Release the capture on the mouse\r
170                 ReleaseCapture();\r
171 \r
172                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);\r
173         }\r
174 }\r
175 \r
176 void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)\r
177 {\r
178         // hold down the left mouse button and move mouse to draw lines.\r
179         if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )\r
180         {\r
181                 DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\r
182                 StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
183         }\r
184 }\r
185 \r
186 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
187 {\r
188         switch (uMsg)\r
189         {\r
190         case WM_LBUTTONDOWN:\r
191                 OnLButtonDown(lParam);\r
192                 break;\r
193 \r
194         case WM_MOUSEMOVE:\r
195                 OnMouseMove(wParam, lParam);\r
196         break;\r
197 \r
198     case WM_LBUTTONUP:\r
199                 OnLButtonUp(lParam);\r
200                 break;\r
201         }\r
202 \r
203         //Use the default message handling for remaining messages\r
204         return WndProcDefault(uMsg, wParam, lParam);\r
205 }\r
206 \r
207 \r

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