// MatrixGridDoc.cpp : implementation of the CMatrixGridDoc class
//
#include "stdafx.h"
#include "MatrixGrid.h"
#include "MatrixGridDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc
IMPLEMENT_DYNCREATE(CMatrixGridDoc, CDocument)
BEGIN_MESSAGE_MAP(CMatrixGridDoc, CDocument)
//{{AFX_MSG_MAP(CMatrixGridDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc construction/destruction
CMatrixGridDoc::CMatrixGridDoc()
{
// TODO: add one-time construction code here
m_iColoums=COL;
m_iHeight=HEIGHT ;
m_iRow=ROW;
m_iWidth=WIDTH;
m_iXoffset=XOFFSET;
m_iYoffset=YOFFSET;
}
CMatrixGridDoc::~CMatrixGridDoc()
{
}
BOOL CMatrixGridDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc serialization
void CMatrixGridDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc diagnostics
#ifdef _DEBUG
void CMatrixGridDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMatrixGridDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridDoc commands
MatrixGridView.cpp
// MatrixGridView.cpp : implementation of the CMatrixGridView class
//
#include "stdafx.h"
#include "MatrixGrid.h"
#include "MatrixGridDoc.h"
#include "MatrixGridView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView
IMPLEMENT_DYNCREATE(CMatrixGridView, CScrollView)
BEGIN_MESSAGE_MAP(CMatrixGridView, CScrollView)
//{{AFX_MSG_MAP(CMatrixGridView)
ON_COMMAND(ID_ZOOM_25, OnZoom25)
ON_COMMAND(ID_ZOOM_50, OnZoom50)
ON_COMMAND(ID_ZOOM_200, OnZoom200)
ON_COMMAND(ID_ZOOM_100, OnZoom100)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView construction/destruction
CMatrixGridView::CMatrixGridView()
{
// TODO: add construction code here
m_iColIndex=1;
m_iRowIndex=1;
}
CMatrixGridView::~CMatrixGridView()
{
}
BOOL CMatrixGridView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView drawing
void CMatrixGridView::OnDraw(CDC* pDC)
{
CMatrixGridDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
InitializeCell();
FillColor(pDC);
SetScrollPosition();
DrawGrid(pDC);
}
void CMatrixGridView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView printing
BOOL CMatrixGridView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMatrixGridView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMatrixGridView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView diagnostics
#ifdef _DEBUG
void CMatrixGridView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMatrixGridView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMatrixGridDoc* CMatrixGridView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatrixGridDoc)));
return (CMatrixGridDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView message handlers
void CMatrixGridView::DrawGrid(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
int iIndex;
for(iIndex=0;iIndex<=pDoc->m_iColoums;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset);
pDC->LineTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset+(pDoc->m_iHeight*pDoc->m_iRow));
}
for(iIndex=0;iIndex<=pDoc->m_iRow;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset,pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
pDC->LineTo(pDoc->m_iXoffset+(pDoc->m_iWidth*pDoc->m_iColoums),pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
}
}
void CMatrixGridView::OnZoom25()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.25;
pDoc->m_iWidth=WIDTH*.25;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom50()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.50;
pDoc->m_iWidth=WIDTH*.50;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom200()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*2;
pDoc->m_iWidth=WIDTH*2;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom100()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*1;
pDoc->m_iWidth=WIDTH*1;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnEditUndo()
{
AfxMessageBox("Add code for Undo Operation here");
}
void CMatrixGridView::OnEditPaste()
{
AfxMessageBox("Add code for Paste Operation here");
}
void CMatrixGridView::OnEditCut()
{
AfxMessageBox("Add code for Cut Operation here");
}
void CMatrixGridView::OnEditCopy()
{
AfxMessageBox("Add code for Copy Operation here");
}
/*-------------------------------------------- Function Header ---------------------------------------------*
Function Name : CMatrixGridView::SetScrollPosition
Description : Set the scroll sizes here depending on the zoom Factor
Return Type : void
Warning :
------------------------------------------------------------------------------------------------------------*/
void CMatrixGridView::SetScrollPosition()
{
CMatrixGridDoc *pDoc=GetDocument();
int iHorizontalPosition=pDoc->m_iXoffset+pDoc->m_iColoums*pDoc->m_iWidth+100;
int iVerticalPosition=pDoc->m_iYoffset+pDoc->m_iRow*pDoc->m_iHeight+100;
SetScrollSizes(MM_TEXT,CSize(iHorizontalPosition,iVerticalPosition));
}
void CMatrixGridView::InitializeCell()
{
CMatrixGridDoc *pDoc=GetDocument();
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.SetRect(0,0,pDoc->m_iWidth,pDoc->m_iHeight);
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.OffsetRect(pDoc->m_iXoffset+(pDoc->m_iWidth*iColIndex),pDoc->m_iYoffset+(pDoc->m_iHeight*iRowIndex));
}
}
}
void CMatrixGridView::FillColor(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CBrush WhiteBrush(RGB(255,255,255));
CBrush RedBrush(RGB(255,0,0));
CBrush BlueBrush(RGB(0,0,255));
CBrush GreenBrush(RGB(0,255,0));
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
switch(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor)
{
case WHITE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&WhiteBrush);
break;
}
case RED:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&RedBrush);
break;
}
case GREEN:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&GreenBrush);
break;
}
case BLUE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&BlueBrush);
break;
}
case CUSTOM:
{
CBrush CustomBrush(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_cColor);
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&CustomBrush);
break;
}
}
pDC->DrawText(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_strText,pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,DT_LEFT);
}
}
void CMatrixGridView::OnLButtonDown(UINT nFlags, CPoint point)
{
CMatrixGridDoc *pDoc=GetDocument();
CPoint point1;
point1=GetScrollPosition();
point+=point1;
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.PtInRect(point))
{
m_iColIndex=iColIndex;
m_iRowIndex=iRowIndex;
if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor>BLUE )
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor=WHITE;
}
else
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor++;
}
}
}
}
Invalidate();
CScrollView::OnLButtonDown(nFlags, point);
}
Cell.cpp
// Cell.cpp: implementation of the CCell class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MatrixGrid.h"
#include "Cell.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCell::CCell()
{
m_iColor=1;
m_strText="";
}
CCell::~CCell()
{
}
MatrixGridView.cpp
// MatrixGridView.cpp : implementation of the CMatrixGridView class
//
#include "stdafx.h"
#include "MatrixGrid.h"
#include "MatrixGridDoc.h"
#include "MatrixGridView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView
IMPLEMENT_DYNCREATE(CMatrixGridView, CScrollView)
BEGIN_MESSAGE_MAP(CMatrixGridView, CScrollView)
//{{AFX_MSG_MAP(CMatrixGridView)
ON_COMMAND(ID_ZOOM_25, OnZoom25)
ON_COMMAND(ID_ZOOM_50, OnZoom50)
ON_COMMAND(ID_ZOOM_200, OnZoom200)
ON_COMMAND(ID_ZOOM_100, OnZoom100)
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView construction/destruction
CMatrixGridView::CMatrixGridView()
{
// TODO: add construction code here
m_iColIndex=1;
m_iRowIndex=1;
}
CMatrixGridView::~CMatrixGridView()
{
}
BOOL CMatrixGridView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView drawing
void CMatrixGridView::OnDraw(CDC* pDC)
{
CMatrixGridDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
InitializeCell();
FillColor(pDC);
SetScrollPosition();
DrawGrid(pDC);
}
void CMatrixGridView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView printing
BOOL CMatrixGridView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMatrixGridView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMatrixGridView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView diagnostics
#ifdef _DEBUG
void CMatrixGridView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMatrixGridView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMatrixGridDoc* CMatrixGridView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatrixGridDoc)));
return (CMatrixGridDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMatrixGridView message handlers
void CMatrixGridView::DrawGrid(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
int iIndex;
for(iIndex=0;iIndex<=pDoc->m_iColoums;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset);
pDC->LineTo(pDoc->m_iXoffset+(iIndex*pDoc->m_iWidth),pDoc->m_iYoffset+(pDoc->m_iHeight*pDoc->m_iRow));
}
for(iIndex=0;iIndex<=pDoc->m_iRow;iIndex++)
{
pDC->MoveTo(pDoc->m_iXoffset,pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
pDC->LineTo(pDoc->m_iXoffset+(pDoc->m_iWidth*pDoc->m_iColoums),pDoc->m_iYoffset+(iIndex*pDoc->m_iHeight));
}
}
void CMatrixGridView::OnZoom25()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.25;
pDoc->m_iWidth=WIDTH*.25;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom50()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*.50;
pDoc->m_iWidth=WIDTH*.50;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom200()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*2;
pDoc->m_iWidth=WIDTH*2;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnZoom100()
{
CMatrixGridDoc *pDoc=GetDocument();
pDoc->m_iHeight=HEIGHT*1;
pDoc->m_iWidth=WIDTH*1;
SetScrollPosition();
Invalidate();
}
void CMatrixGridView::OnEditUndo()
{
AfxMessageBox("Add code for Undo Operation here");
}
void CMatrixGridView::OnEditPaste()
{
AfxMessageBox("Add code for Paste Operation here");
}
void CMatrixGridView::OnEditCut()
{
AfxMessageBox("Add code for Cut Operation here");
}
void CMatrixGridView::OnEditCopy()
{
AfxMessageBox("Add code for Copy Operation here");
}
/*-------------------------------------------- Function Header ---------------------------------------------*
Function Name : CMatrixGridView::SetScrollPosition
Description : Set the scroll sizes here depending on the zoom Factor
Return Type : void
Warning :
------------------------------------------------------------------------------------------------------------*/
void CMatrixGridView::SetScrollPosition()
{
CMatrixGridDoc *pDoc=GetDocument();
int iHorizontalPosition=pDoc->m_iXoffset+pDoc->m_iColoums*pDoc->m_iWidth+100;
int iVerticalPosition=pDoc->m_iYoffset+pDoc->m_iRow*pDoc->m_iHeight+100;
SetScrollSizes(MM_TEXT,CSize(iHorizontalPosition,iVerticalPosition));
}
void CMatrixGridView::InitializeCell()
{
CMatrixGridDoc *pDoc=GetDocument();
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.SetRect(0,0,pDoc->m_iWidth,pDoc->m_iHeight);
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.OffsetRect(pDoc->m_iXoffset+(pDoc->m_iWidth*iColIndex),pDoc->m_iYoffset+(pDoc->m_iHeight*iRowIndex));
}
}
}
void CMatrixGridView::FillColor(CDC *pDC)
{
CMatrixGridDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CBrush WhiteBrush(RGB(255,255,255));
CBrush RedBrush(RGB(255,0,0));
CBrush BlueBrush(RGB(0,0,255));
CBrush GreenBrush(RGB(0,255,0));
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
switch(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor)
{
case WHITE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&WhiteBrush);
break;
}
case RED:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&RedBrush);
break;
}
case GREEN:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&GreenBrush);
break;
}
case BLUE:
{
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&BlueBrush);
break;
}
case CUSTOM:
{
CBrush CustomBrush(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_cColor);
pDC->FillRect(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,&CustomBrush);
break;
}
}
pDC->DrawText(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_strText,pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect,DT_LEFT);
}
}
void CMatrixGridView::OnLButtonDown(UINT nFlags, CPoint point)
{
CMatrixGridDoc *pDoc=GetDocument();
CPoint point1;
point1=GetScrollPosition();
point+=point1;
for(int iRowIndex=0;iRowIndex<pDoc->m_iRow;iRowIndex++)
{
for(int iColIndex=0;iColIndex<pDoc->m_iColoums;iColIndex++)
{
if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_Rect.PtInRect(point))
{
m_iColIndex=iColIndex;
m_iRowIndex=iRowIndex;
if(pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor>BLUE )
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor=WHITE;
}
else
{
pDoc->m_arrCellArray[iRowIndex][iColIndex].m_iColor++;
}
}
}
}
Invalidate();
CScrollView::OnLButtonDown(nFlags, point);
}