// DrawVw.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include <afxpriv.h>
#include "DrawCli.h"
#include "DrawDoc.h"
#include "CntrItem.h"
#include "DrawVw.h"
#include "resource.h"
#include "DrawObj.h"
#include "drawtool.h"
#include "CntrItem.h"
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern Data_Bus Demo_Data;
// private clipboard format (list of Draw objects)
CLIPFORMAT CDrawView::m_cfDraw = (CLIPFORMAT)
#ifdef _MAC
::RegisterClipboardFormat(_T("DCLI"));
#else
::RegisterClipboardFormat(_T("MFC Draw Sample"));
#endif
CLIPFORMAT CDrawView::m_cfObjectDescriptor = NULL;
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CDrawView)
ON_WM_DESTROY()
ON_WM_SETFOCUS()
ON_WM_SIZE()
ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
ON_COMMAND(ID_DRAW_SELECT, OnDrawSelect)
ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
ON_UPDATE_COMMAND_UI(ID_DRAW_SELECT, OnUpdateDrawSelect)
ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
ON_COMMAND(ID_DRAW_ROUNDRECT, OnDrawRoundrect)
ON_COMMAND(ID_EDIT_PROPERTIES, OnEditProperties)
ON_COMMAND(ID_OBJECT_MOVEBACK, OnObjectMoveback)
ON_COMMAND(ID_OBJECT_MOVEFORWARD, OnObjectMoveforward)
ON_COMMAND(ID_OBJECT_MOVETOBACK, OnObjectMovetoback)
ON_COMMAND(ID_OBJECT_MOVETOFRONT, OnObjectMovetofront)
ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEBACK, OnUpdateObjectMoveback)
ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEFORWARD, OnUpdateObjectMoveforward)
ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOBACK, OnUpdateObjectMovetoback)
ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOFRONT, OnUpdateObjectMovetofront)
ON_COMMAND(ID_OBJECT_FILLCOLOR, OnObjectFillcolor)
ON_COMMAND(ID_OBJECT_LINECOLOR, OnObjectLinecolor)
ON_UPDATE_COMMAND_UI(ID_OBJECT_FILLCOLOR, OnUpdateObjectFillcolor)
ON_UPDATE_COMMAND_UI(ID_OBJECT_LINECOLOR, OnUpdateObjectLinecolor)
ON_COMMAND(ID_VIEW_GRID, OnViewGrid)
ON_UPDATE_COMMAND_UI(ID_VIEW_GRID, OnUpdateViewGrid)
ON_COMMAND(ID_VIEW_SHOWOBJECTS, OnViewShowobjects)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWOBJECTS, OnUpdateViewShowobjects)
ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditClear)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
ON_WM_CREATE()
//}}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)
ON_MESSAGE(WM_DEMO_DATA,DemoData)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// m_pSelection = NULL;
m_bGrid = FALSE;
m_gridColor = RGB(0, 0, 128);
m_bActive = FALSE;
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDC dc;
CDC* pDrawDC = pDC;
CBitmap bitmap;
CBitmap* pOldBitmap;
// only paint the rect that needs repainting
CRect client;
pDC->GetClipBox(client);
CRect rect = client;
DocToClient(rect);
if (!pDC->IsPrinting())
{
// draw to offscreen bitmap for fast looking repaints
if (dc.CreateCompatibleDC(pDC))
{
if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
{
OnPrepareDC(&dc, NULL);
pDrawDC = &dc;
// offset origin more because bitmap is just piece of the whole drawing
dc.OffsetViewportOrg(-rect.left, -rect.top);
pOldBitmap = dc.SelectObject(&bitmap);
dc.SetBrushOrg(rect.left % 8, rect.top % 8);
// might as well clip to the same rectangle
dc.IntersectClipRect(client);
}
}
}
// paint background
CBrush brush;
if (!brush.CreateSolidBrush(pDoc->GetPaperColor()))
return;
brush.UnrealizeObject();
pDrawDC->FillRect(client, &brush);
if (!pDC->IsPrinting() && m_bGrid)
DrawGrid(pDrawDC);
pDoc->Draw(pDrawDC, this);
if (pDrawDC != pDC)
{
pDC->SetViewportOrg(0, 0);
pDC->SetWindowOrg(0,0);
pDC->SetMapMode(MM_TEXT);
dc.SetViewportOrg(0, 0);
dc.SetWindowOrg(0,0);
dc.SetMapMode(MM_TEXT);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
&dc, 0, 0, SRCCOPY);
dc.SelectObject(pOldBitmap);
}
}
void CDrawView::OnInitialUpdate()
{
// CScrollView::OnInitialUpdate();
CSize size = GetDocument()->GetSize();
CClientDC dc(NULL);
size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
SetScrollSizes(MM_TEXT, size);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CDrawView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
CScrollView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
}
/////////////////////////////////////////////////////////////////////////////
// OLE Client support and commands
BOOL CDrawView::IsSelected(const CObject* pDocItem) const
{
/*
// The implementation below is adequate if your selection consists of
// only CDrawItem objects. To handle different selection
// mechanisms, the implementation here should be replaced.
// TODO: implement this function that tests for a selected OLE client item
return pDocItem == m_pSelection;
*/
CDrawObj* pDrawObj = (CDrawObj*)pDocItem;
if (pDocItem->IsKindOf(RUNTIME_CLASS(CDrawItem)))
pDrawObj = ((CDrawItem*)pDocItem)->m_pDrawObj;
return m_selection.Find(pDrawObj) != NULL;
}
void CDrawView::OnInsertObject()
{
// Invoke the standard Insert Object dialog box to obtain information
// for new CDrawItem object.
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
// First create the C++ object
CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
ASSERT_VALID(pObj);
CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
ASSERT_VALID(pItem);
pObj->m_pClientItem = pItem;
// Now create the OLE object/item
TRY
{
if (!dlg.CreateItem(pObj->m_pClientItem))
AfxThrowMemoryException();
// add the object to the document
GetDocument()->Add(pObj);
// try to get