www.pudn.com > RMS2000_C.rar > DrawTool.cpp
// DrawTool.cpp: implementation of the CDrawTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DrawCli.h"
#include "DrawTool.h"
#include "drawdoc.h"
#include "drawvw.h"
#include "drawobj.h"
#include "rectdlg.h"
#include "textdlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CDrawTool implementation
CPtrList CDrawTool::c_tools;
static CSelectTool selectTool;
static CRectTool lineTool(line);
static CRectTool rectTool(rect);
static CRectTool roundRectTool(roundRect);
static CRectTool ellipseTool(ellipse);
static CRectTool circleTool(circle);
static CRectTool hpipeTool(hpipe);
static CRectTool vpipeTool(vpipe);
static CRectTool warnbmpTool(warnbmp);
static CPencilTool pencilTool;
//static CPolyTool polyTool;
static CTextTool textTool;
static CRectTool liquidTool(liquid);
static CRectTool pumpTool(pump);
static CRectTool ladderTool(ladder);
static CRectTool sposTool(spos);
CPoint CDrawTool::c_down;
UINT CDrawTool::c_nDownFlags;
CPoint CDrawTool::c_last;
DrawShape CDrawTool::c_drawShape = selection;
CDrawTool::CDrawTool(DrawShape drawShape)
{
m_drawShape = drawShape;
c_tools.AddTail(this);
}
CDrawTool* CDrawTool::FindTool(DrawShape drawShape)
{
POSITION pos = c_tools.GetHeadPosition();
while (pos != NULL)
{
CDrawTool* pTool = (CDrawTool*)c_tools.GetNext(pos);
if (pTool->m_drawShape == drawShape)
return pTool;
}
return NULL;
}
void CDrawTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
{
// deactivate any in-place active item on this view!
COleClientItem* pActiveItem = pView->GetDocument()->GetInPlaceActiveItem(pView);
if (pActiveItem != NULL)
{
pActiveItem->Close();
ASSERT(pView->GetDocument()->GetInPlaceActiveItem(pView) == NULL);
}
pView->SetCapture();
c_nDownFlags = nFlags;
c_down = point;
c_last = point;
}
void CDrawTool::OnLButtonDblClk(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& /*point*/)
{
}
void CDrawTool::OnLButtonUp(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& point)
{
ReleaseCapture();
if (point == c_down)
c_drawShape = selection;
}
void CDrawTool::OnMouseMove(CDrawView* /*pView*/, UINT /*nFlags*/, const CPoint& point)
{
c_last = point;
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
}
void CDrawTool::OnEditProperties(CDrawView* /*pView*/)
{
}
void CDrawTool::OnCancel()
{
c_drawShape = selection;
}
////////////////////////////////////////////////////////////////////////////
// CResizeTool
enum SelectMode
{
none,
netSelect,
move,
size
};
SelectMode selectMode = none;
int nDragHandle;
CPoint lastPoint;
CSelectTool::CSelectTool()
: CDrawTool(selection)
{
}
void CSelectTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CPoint local = point;
pView->ClientToDoc(local);
CDrawObj* pObj;
selectMode = none;
// Check for resizing (only allowed on single selections)
if (pView->m_selection.GetCount() == 1)
{
pObj = pView->m_selection.GetHead();
nDragHandle = pObj->HitTest(local, pView, TRUE);
if (nDragHandle != 0)
selectMode = size;
}
// See if the click was on an object, select and start move if so
if (selectMode == none)
{
pObj = pView->GetDocument()->ObjectAt(local);
if (pObj != NULL)
{
selectMode = move;
if (!pView->IsSelected(pObj))
pView->Select(pObj, (nFlags & MK_SHIFT) != 0);
// Ctrl+Click clones the selection...
#ifndef _MAC
if ((nFlags & MK_CONTROL) != 0)
#else
if ((nFlags & MK_OPTION) != 0)
#endif
pView->CloneSelection();
}
}
// Click on background, start a net-selection
if (selectMode == none)
{
if ((nFlags & MK_SHIFT) == 0)
pView->Select(NULL);
selectMode = netSelect;
CClientDC dc(pView);
CRect rect(point.x, point.y, point.x, point.y);
rect.NormalizeRect();
dc.DrawFocusRect(rect);
}
lastPoint = local;
CDrawTool::OnLButtonDown(pView, nFlags, point);
}
void CSelectTool::OnLButtonDblClk(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if ((nFlags & MK_SHIFT) != 0)
{
// Shift+DblClk deselects object...
CPoint local = point;
pView->ClientToDoc(local);
CDrawObj* pObj = pView->GetDocument()->ObjectAt(local);
if (pObj != NULL)
pView->Deselect(pObj);
}
else
{
// "Normal" DblClk opens properties, or OLE server...
if (pView->m_selection.GetCount() == 1)
pView->m_selection.GetHead()->OnOpen(pView); //编辑属性
}
CDrawTool::OnLButtonDblClk(pView, nFlags, point);
}
void CSelectTool::OnEditProperties(CDrawView* pView)
{
if (pView->m_selection.GetCount() == 1)
pView->m_selection.GetHead()->OnEditProperties();
}
void CSelectTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if (pView->GetCapture() == pView)
{
if (selectMode == netSelect)
{
CClientDC dc(pView);
CRect rect(c_down.x, c_down.y, c_last.x, c_last.y);
rect.NormalizeRect();
dc.DrawFocusRect(rect);
pView->SelectWithinRect(rect, TRUE);
}
else if (selectMode != none)
{
pView->GetDocument()->UpdateAllViews(pView);
}
}
CDrawTool::OnLButtonUp(pView, nFlags, point);
}
void CSelectTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if (pView->GetCapture() != pView)
{
if (c_drawShape == selection && pView->m_selection.GetCount() == 1)
{
CDrawObj* pObj = pView->m_selection.GetHead();
CPoint local = point;
pView->ClientToDoc(local);
int nHandle = pObj->HitTest(local, pView, TRUE);
if (nHandle != 0)
{
SetCursor(pObj->GetHandleCursor(nHandle));
return; // bypass CDrawTool
}
}
if (c_drawShape == selection)
CDrawTool::OnMouseMove(pView, nFlags, point);
return;
}
if (selectMode == netSelect)
{
CClientDC dc(pView);
CRect rect(c_down.x, c_down.y, c_last.x, c_last.y);
rect.NormalizeRect();
dc.DrawFocusRect(rect);
rect.SetRect(c_down.x, c_down.y, point.x, point.y);
rect.NormalizeRect();
dc.DrawFocusRect(rect);
CDrawTool::OnMouseMove(pView, nFlags, point);
return;
}
CPoint local = point;
pView->ClientToDoc(local);
CPoint delta = (CPoint)(local - lastPoint);
POSITION pos = pView->m_selection.GetHeadPosition();
while (pos != NULL)
{
CDrawObj* pObj = pView->m_selection.GetNext(pos);
CRect position = pObj->m_position;
if (selectMode == move)
{
position += delta;
pObj->MoveTo(position, pView);
}
else if (nDragHandle != 0)
{
pObj->MoveHandleTo(nDragHandle, local, pView);
}
}
lastPoint = local;
if (selectMode == size && c_drawShape == selection)
{
c_last = point;
SetCursor(pView->m_selection.GetHead()->GetHandleCursor(nDragHandle));
return; // bypass CDrawTool
}
c_last = point;
if (c_drawShape == selection)
CDrawTool::OnMouseMove(pView, nFlags, point);
}
////////////////////////////////////////////////////////////////////////////
// CRectTool (does rectangles, round-rectangles, and ellipses)
CRectTool::CRectTool(DrawShape drawShape)
: CDrawTool(drawShape)
{
}
void CRectTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CDrawTool::OnLButtonDown(pView, nFlags, point);
CPoint local = point;
pView->ClientToDoc(local);
CDrawRect* pObj = new CDrawRect(CRect(local, CSize(0, 0)));
switch (m_drawShape)
{
default:
ASSERT(FALSE); // unsuported shape!
case rect:
pObj->m_nShape = CDrawRect::rectangle;
break;
case roundRect:
pObj->m_nShape = CDrawRect::roundRectangle;
break;
case ellipse:
pObj->m_nShape = CDrawRect::ellipse;
break;
case line:
pObj->m_nShape = CDrawRect::line;
break;
case circle:
pObj->m_nShape = CDrawRect::circle;
break;
case hpipe:
pObj->m_nShape = CDrawRect::hpipe;
break;
case vpipe:
pObj->m_nShape = CDrawRect::vpipe;
break;
case warnbmp:
pObj->m_nShape = CDrawRect::warnbmp;
break;
case liquid:
pObj->m_nShape = CDrawRect::liquid;
break;
case pump:
pObj->m_nShape = CDrawRect::pump;
break;
case spos:
pObj->m_nShape = CDrawRect::spos;
break;
case ladder:
pObj->m_nShape = CDrawRect::ladder;
break;
}
pView->GetDocument()->Add(pObj);
pView->Select(pObj);
selectMode = size;
nDragHandle = 1;
lastPoint = local;
}
void CRectTool::OnLButtonDblClk(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CDrawTool::OnLButtonDblClk(pView, nFlags, point);
}
void CRectTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if (point == c_down)
{
// Don't create empty objects...
CDrawObj *pObj = pView->m_selection.GetTail();
pView->GetDocument()->Remove(pObj);
pObj->Remove();
selectTool.OnLButtonDown(pView, nFlags, point); // try a select!
}
selectTool.OnLButtonUp(pView, nFlags, point);
}
void CRectTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
selectTool.OnMouseMove(pView, nFlags, point);
}
////////////////////////////////////////////////////////////////////////////
// CTextTool
CTextTool::CTextTool(): CRectTool(text)
{
}
void CTextTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
{
//when mouse is down, an Object is newed,
CDrawTool::OnLButtonDown(pView, nFlags, point);
//get the point that cursor is down
CPoint local = point;
pView->ClientToDoc(local);
CDrawText* pObj = new CDrawText(CRect(local, CSize(5, 5)));
CPropertySheet sheet( _T("对象显示属性") );
CRectDlg RectPage;
RectPage.m_bNoFill = !pObj->m_bBrush;
RectPage.m_bLock = pObj->mv_bLock;
RectPage.m_nRtu = pObj->mv_nRtu;
RectPage.m_nPar = pObj->mv_nPar;
RectPage.m_nType = pObj->mv_nType;
RectPage.m_nInt = pObj->mv_nInt;
RectPage.m_nDec = pObj->mv_nDec;
RectPage.m_nWard = pObj->mv_nWard;
RectPage.m_bInvalid = pObj->mv_bInvalid;
RectPage.m_penSize = pObj->m_bPen ? pObj->m_logpen.lopnWidth.x : 0;
RectPage.m_LineColor = pObj->m_logpen.lopnColor; /*xxx*/
RectPage.m_FillColor = pObj->m_logbrush.lbColor; /*xxx*/
CTextDlg textPage;
textPage.m_font.CreateFontIndirect(&(pObj->m_logfont));
textPage.m_color=pObj->m_color;
textPage.m_borderType=0; //
textPage.m_allignment=0;
sheet.AddPage( &textPage );
sheet.AddPage( &RectPage );
if (sheet.DoModal() != IDOK){
delete pObj; //delete the CDrawText object
return;
}
//get format of line, background,
pObj->m_bBrush = !RectPage.m_bNoFill;
pObj->mv_bLock = RectPage.m_bLock;
pObj->mv_nRtu=RectPage.m_nRtu;
pObj->mv_nPar=RectPage.m_nPar;
pObj->mv_nType=RectPage.m_nType;
pObj->mv_nInt=RectPage.m_nInt;
pObj->mv_nDec=RectPage.m_nDec;
pObj->mv_nWard=RectPage.m_nWard;
pObj->mv_bInvalid=RectPage.m_bInvalid;
pObj->m_bPen = RectPage.m_penSize > 0;
if (pObj->m_bPen)
{
pObj->m_logpen.lopnWidth.x = RectPage.m_penSize;
pObj->m_logpen.lopnWidth.y = RectPage.m_penSize;
}
pObj->m_logbrush.lbColor=RectPage.m_FillColor;
pObj->m_logpen.lopnColor=RectPage.m_LineColor;
//get format of font
if(textPage.m_bFont)
textPage.m_font.GetLogFont(&(pObj->m_logfont));
pObj->m_color=textPage.m_color;
pObj->m_nShape=(CDrawRect::Shape)textPage.m_borderType; //
pObj->m_allignment=textPage.m_allignment;
textPage.m_allignment=0;
//get the text
pObj->m_text=textPage.m_text;
pObj->m_GS=textPage.m_GS;
pObj->Resize(pView);
pView->GetDocument()->Add(pObj);//add the CDrawText to domcument
pView->Select(pObj); //select it
pObj->Invalidate(); //redraw it
pObj->m_pDocument->SetModifiedFlag();
//
c_drawShape = selection;
}
void CTextTool::OnLButtonDblClk(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CDrawTool::OnLButtonDblClk(pView, nFlags, point);
}
void CTextTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if (point == c_down)
{
//create empty text box for input
selectTool.OnLButtonDown(pView, nFlags, point); // try a select!
}
selectTool.OnLButtonUp(pView, nFlags, point);
}
void CTextTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
// selectTool.OnMouseMove(pView, nFlags, point);
}
////////////////////////////////////////////////////////////////////////////
// CPencilTool
CPencilTool::CPencilTool()
: CDrawTool(pencil)
{
m_pDrawObj = NULL;
}
void CPencilTool::OnLButtonDown(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CDrawTool::OnLButtonDown(pView, nFlags, point);
CPoint local = point;
pView->ClientToDoc(local);
if (m_pDrawObj == NULL)
{
pView->SetCapture();
m_pDrawObj = new CDrawPencil(CRect(local, CSize(0, 0)));
pView->GetDocument()->Add(m_pDrawObj);
pView->Select(m_pDrawObj);
m_pDrawObj->m_pointArray.Add(local);
}
selectMode = size;
nDragHandle = m_pDrawObj->GetHandleCount();
lastPoint = local;
return;
}
void CPencilTool::OnLButtonUp(CDrawView* pView, UINT nFlags, const CPoint& point)
{
CDrawTool::OnLButtonUp(pView, nFlags, point);
ReleaseCapture();
CPoint local = point;
pView->ClientToDoc(local);
m_pDrawObj->m_pointArray.Add(local);
m_pDrawObj=NULL;
selectTool.OnLButtonUp(pView, nFlags, point);
}
void CPencilTool::OnMouseMove(CDrawView* pView, UINT nFlags, const CPoint& point)
{
if (m_pDrawObj != NULL && (nFlags & MK_LBUTTON) != 0)
{
CPoint local = point;
pView->ClientToDoc(local);
m_pDrawObj->m_pointArray.Add(local);
nDragHandle = m_pDrawObj->GetHandleCount();
lastPoint = local;
c_last = point;
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
selectTool.OnMouseMove(pView, nFlags, point);
}
}
void CPencilTool::OnCancel()
{
CDrawTool::OnCancel();
m_pDrawObj = NULL;
}