Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / RibbonFrame / 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_hBrush.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         // Here we use double buffering (drawing to a memory DC) for smoother rendering\r
38         // Set up our Memory DC and bitmap\r
39         CMemDC MemDC(pDC);\r
40         int Width = GetClientRect().Width();\r
41         int Height = GetClientRect().Height();\r
42         MemDC.CreateCompatibleBitmap(pDC, Width, Height);\r
43         MemDC.FillRect(GetClientRect(), &m_hBrush);     \r
44 \r
45         if (m_points.size() > 0)\r
46         {\r
47                 bool bDraw = false;  //Start with the pen up\r
48                 for (unsigned int i = 0 ; i < m_points.size(); i++)\r
49                 {\r
50                 \r
51                         MemDC.CreatePen(PS_SOLID, 1, m_points[i].color);\r
52                         if (bDraw)\r
53                                 MemDC.LineTo(m_points[i].x, m_points[i].y);\r
54                         else \r
55                                 MemDC.MoveTo(m_points[i].x, m_points[i].y);\r
56                         \r
57                         bDraw = m_points[i].PenDown;                    \r
58                 }\r
59         }\r
60 \r
61         // Copy from the memory DC to our painting dc\r
62         pDC->BitBlt(0, 0, Width, Height, &MemDC, 0, 0, SRCCOPY);\r
63 }\r
64 \r
65 void CView::PreCreate(CREATESTRUCT &cs)\r
66 {\r
67         // Set the extra style to provide a sunken effect\r
68         cs.dwExStyle = WS_EX_CLIENTEDGE;\r
69 }\r
70 \r
71 void CView::PreRegisterClass(WNDCLASS &wc)\r
72 {\r
73         // Set the background brush, class name and cursor\r
74         wc.hbrBackground = m_hBrush;\r
75         wc.lpszClassName = _T("Scribble Window");\r
76         wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));\r
77 }\r
78 \r
79 BOOL CView::FileOpen(LPCTSTR szFilename)\r
80 {\r
81         // empty the PlotPoint vector\r
82         m_points.clear();\r
83         DWORD nBytesRead;\r
84         BOOL bResult = FALSE;\r
85 \r
86         // Create a handle to the file\r
87         CFile File;\r
88         if (File.Open(szFilename, OPEN_EXISTING))\r
89         {\r
90                 do\r
91                 {\r
92                         PlotPoint pp;\r
93                         nBytesRead = File.Read(&pp, sizeof(PlotPoint));\r
94                         if (nBytesRead == sizeof(PlotPoint))\r
95                                 m_points.push_back(pp); \r
96 \r
97                 } while (nBytesRead == sizeof(PlotPoint));\r
98 \r
99                 if ((0 != nBytesRead) || (m_points.empty()))\r
100                 {\r
101                         // Failed to read all of the file\r
102                         m_points.clear();\r
103                         ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
104                 }\r
105                 else\r
106                         bResult = TRUE;\r
107 \r
108         }\r
109         else\r
110         {\r
111                 CString strErrMsg = _T("Failed to open file ");\r
112                 strErrMsg += szFilename;\r
113                 ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
114         }\r
115 \r
116         Invalidate();\r
117         return bResult;\r
118 }\r
119 \r
120 BOOL CView::FileSave(LPCTSTR szFilename)\r
121 {\r
122         BOOL bResult = TRUE;\r
123         CFile hFile;\r
124         if (!hFile.Open(szFilename, CREATE_ALWAYS))\r
125         {\r
126                 ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
127                 bResult = FALSE;\r
128         }\r
129         \r
130         if (bResult)\r
131         {\r
132                 // Write the file\r
133                 for (size_t i = 0; i < m_points.size(); ++i)\r
134                 {\r
135                         if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))\r
136                         {\r
137                                 ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
138                                 bResult = FALSE;\r
139                                 break;\r
140                         }\r
141                 }\r
142 \r
143                 // Verify file length\r
144                 if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))\r
145                 {\r
146                         ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
147                         bResult = FALSE;\r
148                 }\r
149         }\r
150 \r
151         return bResult;\r
152 }\r
153 \r
154 void CView::SetPen(COLORREF color)\r
155 {\r
156         m_PenColor = color;\r
157 }\r
158 \r
159 void CView::StorePoint(int x, int y, bool PenDown)\r
160 {\r
161         PlotPoint P1;\r
162         P1.x = x;\r
163         P1.y = y;\r
164         P1.PenDown = PenDown;\r
165         P1.color = m_PenColor;\r
166 \r
167         m_points.push_back(P1); //Add the point to the vector\r
168 }\r
169 \r
170 void CView::OnLButtonDown(LPARAM lParam)\r
171 {\r
172         // Capture mouse input.\r
173         ::SetCapture(m_hWnd);\r
174 \r
175         StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
176 }\r
177 \r
178 void CView::OnLButtonUp(LPARAM lParam)\r
179 {\r
180         if (GetCapture() == this)\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                 if (GetCapture() == this)\r
195                 {\r
196                         CString str;\r
197                         str.Format( _T("Draw Point:  %hd, %hd\n"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) );\r
198                         TRACE(str);\r
199 \r
200                         DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\r
201                         StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
202                 }\r
203         }\r
204 }\r
205 \r
206 LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
207 {\r
208         switch (uMsg)\r
209         {\r
210         case WM_LBUTTONDOWN:\r
211                 OnLButtonDown(lParam);\r
212                 break;\r
213 \r
214         case WM_MOUSEMOVE:\r
215                 OnMouseMove(wParam, lParam);\r
216         break;\r
217 \r
218     case WM_LBUTTONUP:\r
219                 OnLButtonUp(lParam);\r
220                 break;\r
221         }\r
222 \r
223         //Use the default message handling for remaining messages\r
224         return WndProcDefault(uMsg, wParam, lParam);\r
225 }\r
226 \r

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