void CSquaresView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
//
// Convert click coordinates to MM_LOENGLISH units.
//
CClientDC dc (this);
dc.SetMapMode (MM_LOENGLISH);
CPoint pos = point;
dc.DPtoLP (&pos);
//
// If a square was clicked, set its color to the current color.
//
if (pos.x >= 35 && pos.x <= 315 && pos.y <= -35 && pos.y >= -315) {
int i = (-pos.y - 35) / 70;
int j = (pos.x - 35) / 70;
CSquaresDoc* pDoc = GetDocument ();
COLORREF clrCurrentColor = pDoc->GetCurrentColor ();
pDoc->SetSquare (i, j, clrCurrentColor);
}
}
void CSquaresView::OnDraw(CDC* pDC)
{
CSquaresDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//
// Set the mapping mode to MM_LOENGLISH.
//
pDC->SetMapMode (MM_LOENGLISH);
//
// Draw the 16 squares.
//
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
COLORREF color = pDoc->GetSquare (i, j);
CBrush brush (color);
int x1 = (j * 70) + 35;
int y1 = (i * -70) - 35;
int x2 = x1 + 70;
int y2 = y1 - 70;
CRect rect (x1, y1, x2, y2);
pDC->FillRect (rect, &brush);
}
}
//
// Then draw the grid lines surrounding them.
//
for (int x=35; x<=315; x+=70) {
pDC->MoveTo (x, -35);
pDC->LineTo (x, -315);
}
for (int y=-35; y>=-315; y-=70) {
pDC->MoveTo (35, y);
pDC->LineTo (315, y);
}
}
BOOL CSquaresDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
m_clrGrid[i][j] = RGB (255, 255, 255);
m_clrCurrentColor = RGB (255, 0, 0);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CSquaresDoc serialization
void CSquaresDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
ar << m_clrGrid[i][j];
ar << m_clrCurrentColor;
}
else
{
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
ar >> m_clrGrid[i][j];
ar >> m_clrCurrentColor;
}
}
COLORREF CSquaresDoc::GetCurrentColor()
{
return m_clrCurrentColor;
}
COLORREF CSquaresDoc::GetSquare(int i, int j)
{
ASSERT (i >= 0 && i <= 3 && j >= 0 && j <= 3);
return m_clrGrid[i][j];
}
void CSquaresDoc::SetSquare(int i, int j, COLORREF color)
{
ASSERT (i >= 0 && i <= 3 && j >= 0 && j <= 3);
m_clrGrid[i][j] = color;
SetModifiedFlag (TRUE);
UpdateAllViews (NULL);
}
void CSquaresDoc::OnColorRed()
{
m_clrCurrentColor = RGB (255, 0, 0);
}
void CSquaresDoc::OnColorYellow()
{
m_clrCurrentColor = RGB (255, 255, 0);
}
void CSquaresDoc::OnColorGreen()
{
m_clrCurrentColor = RGB (0, 255, 0);
}
void CSquaresDoc::OnColorCyan()
{
m_clrCurrentColor = RGB (0, 255, 255);
}
void CSquaresDoc::OnColorBlue()
{
m_clrCurrentColor = RGB (0, 0, 255);
}
void CSquaresDoc::OnColorWhite()
{
m_clrCurrentColor = RGB (255, 255, 255);
}
void CSquaresDoc::OnUpdateColorRed(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (255, 0, 0));
}
void CSquaresDoc::OnUpdateColorYellow(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (255, 255, 0));
}
void CSquaresDoc::OnUpdateColorGreen(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (0, 255, 0));
}
void CSquaresDoc::OnUpdateColorCyan(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (0, 255, 255));
}
void CSquaresDoc::OnUpdateColorBlue(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (0, 0, 255));
}
void CSquaresDoc::OnUpdateColorWhite(CCmdUI* pCmdUI)
{
pCmdUI->SetRadio (m_clrCurrentColor == RGB (255, 255, 255));
}