Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Scribble / src / View.cpp
diff --git a/research/transmission_spectroscopy/TOF/Win32++/samples/Scribble/src/View.cpp b/research/transmission_spectroscopy/TOF/Win32++/samples/Scribble/src/View.cpp
new file mode 100644 (file)
index 0000000..d1b982d
--- /dev/null
@@ -0,0 +1,223 @@
+//////////////////////////////////////////////\r
+// View.cpp\r
+//  Definitions for the CView class\r
+\r
+#include "stdafx.h"\r
+#include "view.h"\r
+#include "resource.h"\r
+\r
+\r
+using namespace std;\r
+\r
+CView::CView() : m_PenColor(RGB(0,0,0))\r
+{\r
+       m_Brush.CreateSolidBrush(RGB(255,255,230));\r
+}\r
+\r
+CView::~CView()\r
+{\r
+}\r
+\r
+void CView::ClearPoints()\r
+{\r
+       m_points.clear();\r
+       Invalidate();\r
+}\r
+\r
+void CView::DrawLine(int x, int y)\r
+{\r
+       CClientDC dcClient(this);\r
+       dcClient.CreatePen(PS_SOLID, 1, m_points.back().color);\r
+       dcClient.MoveTo(m_points.back().x, m_points.back().y);\r
+       dcClient.LineTo(x, y);\r
+}\r
+\r
+void CView::OnDraw(CDC* pDC)\r
+{\r
+\r
+       // Here we use double buffering (drawing to a memory DC) for smoother rendering\r
+       // Set up our Memory DC and bitmap\r
+       CMemDC MemDC(pDC);\r
+       int Width = GetClientRect().Width();\r
+       int Height = GetClientRect().Height();\r
+       MemDC.CreateCompatibleBitmap(pDC, Width, Height);\r
+       MemDC.FillRect(GetClientRect(), &m_Brush);\r
+\r
+       if (m_points.size() > 0)\r
+       {\r
+               bool bDraw = false;  //Start with the pen up\r
+               for (unsigned int i = 0 ; i < m_points.size(); i++)\r
+               {\r
+\r
+                       MemDC.CreatePen(PS_SOLID, 1, m_points[i].color);\r
+                       if (bDraw)\r
+                               MemDC.LineTo(m_points[i].x, m_points[i].y);\r
+                       else\r
+                               MemDC.MoveTo(m_points[i].x, m_points[i].y);\r
+\r
+                       bDraw = m_points[i].PenDown;\r
+               }\r
+       }\r
+\r
+       // Copy from the memory DC to our painting dc\r
+       pDC->BitBlt(0, 0, Width, Height, &MemDC, 0, 0, SRCCOPY);\r
+}\r
+\r
+void CView::PreCreate(CREATESTRUCT &cs)\r
+{\r
+       // Set the extra style to provide a sunken effect\r
+       cs.dwExStyle = WS_EX_CLIENTEDGE;\r
+}\r
+\r
+void CView::PreRegisterClass(WNDCLASS &wc)\r
+{\r
+       // Set the background brush, class name and cursor\r
+       wc.hbrBackground = m_Brush;\r
+       wc.lpszClassName = _T("Scribble Window");\r
+       wc.hCursor = ::LoadCursor(GetApp()->GetInstanceHandle(), MAKEINTRESOURCE(IDC_CURSOR1));\r
+}\r
+\r
+BOOL CView::FileOpen(LPCTSTR szFilename)\r
+{\r
+       // empty the PlotPoint vector\r
+       m_points.clear();\r
+       DWORD nBytesRead;\r
+       BOOL bResult = FALSE;\r
+\r
+       // Create a handle to the file\r
+       CFile File;\r
+       if (File.Open(szFilename, OPEN_EXISTING))\r
+       {\r
+               do\r
+               {\r
+                       PlotPoint pp;\r
+                       nBytesRead = File.Read(&pp, sizeof(PlotPoint));\r
+                       if (nBytesRead == sizeof(PlotPoint))\r
+                               m_points.push_back(pp);\r
+\r
+               } while (nBytesRead == sizeof(PlotPoint));\r
+\r
+               if ((0 != nBytesRead) || (m_points.empty()))\r
+               {\r
+                       // Failed to read all of the file\r
+                       m_points.clear();\r
+                       ::MessageBox (0, _T("Invalid data in file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
+               }\r
+               else\r
+                       bResult = TRUE;\r
+\r
+       }\r
+       else\r
+       {\r
+               CString strErrMsg = _T("Failed to open file ");\r
+               strErrMsg += szFilename;\r
+               ::MessageBox (0, strErrMsg, _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
+       }\r
+\r
+       Invalidate();\r
+       return bResult;\r
+}\r
+\r
+BOOL CView::FileSave(LPCTSTR szFilename)\r
+{\r
+       BOOL bResult = TRUE;\r
+       CFile hFile;\r
+       if (!hFile.Open(szFilename, CREATE_ALWAYS))\r
+       {\r
+               ::MessageBox (0, _T("Failed to open file for writing"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
+               bResult = FALSE;\r
+       }\r
+\r
+       if (bResult)\r
+       {\r
+               // Write the file\r
+               for (size_t i = 0; i < m_points.size(); ++i)\r
+               {\r
+                       if (!hFile.Write(&m_points[i], sizeof(PlotPoint)))\r
+                       {\r
+                               ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
+                               bResult = FALSE;\r
+                               break;\r
+                       }\r
+               }\r
+\r
+               // Verify file length\r
+               if (hFile.GetLength() != m_points.size() * sizeof(PlotPoint))\r
+               {\r
+                       ::MessageBox (0, _T("Error while writing to file"), _T("Error"), MB_ICONEXCLAMATION | MB_OK);\r
+                       bResult = FALSE;\r
+               }\r
+       }\r
+\r
+       return bResult;\r
+}\r
+\r
+void CView::SetPen(COLORREF color)\r
+{\r
+       m_PenColor = color;\r
+}\r
+\r
+void CView::StorePoint(int x, int y, bool PenDown)\r
+{\r
+       PlotPoint P1;\r
+       P1.x = x;\r
+       P1.y = y;\r
+       P1.PenDown = PenDown;\r
+       P1.color = m_PenColor;\r
+\r
+       m_points.push_back(P1); //Add the point to the vector\r
+}\r
+\r
+void CView::OnLButtonDown(LPARAM lParam)\r
+{\r
+       // Capture mouse input.\r
+       SetCapture();\r
+\r
+       StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
+}\r
+\r
+void CView::OnLButtonUp(LPARAM lParam)\r
+{\r
+       {\r
+               //Release the capture on the mouse\r
+               ReleaseCapture();\r
+\r
+               StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), false);\r
+       }\r
+}\r
+\r
+void CView::OnMouseMove(WPARAM wParam, LPARAM lParam)\r
+{\r
+       // hold down the left mouse button and move mouse to draw lines.\r
+       if ( (wParam & MK_LBUTTON) && (GetCapture() == this) )\r
+       {\r
+               CString str;\r
+               str.Format( _T("Draw Point:  %hd, %hd\n"), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) );\r
+               TRACE(str);\r
+\r
+               DrawLine(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\r
+               StorePoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), true);\r
+       }\r
+}\r
+\r
+LRESULT CView::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
+{\r
+       switch (uMsg)\r
+       {\r
+       case WM_LBUTTONDOWN:\r
+               OnLButtonDown(lParam);\r
+               break;\r
+\r
+       case WM_MOUSEMOVE:\r
+               OnMouseMove(wParam, lParam);\r
+        break;\r
+\r
+    case WM_LBUTTONUP:\r
+               OnLButtonUp(lParam);\r
+               break;\r
+       }\r
+\r
+       //Use the default message handling for remaining messages\r
+       return WndProcDefault(uMsg, wParam, lParam);\r
+}\r
+\r

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