www.pudn.com > 44757463.rar > GraphSoftDoc.cpp
// GraphSoftDoc.cpp : implementation of the CGraphSoftDoc class
//
#include "stdafx.h"
#include "GraphSoft.h"
#include "MainFrm.h"
#include "GraphSoftDoc.h"
#include "GraphSoftView.h"
#include "GraphDefines.h"
#include "FullViewBtn.h"
#include "UND_Create.h"
#include "UND_Move.h"
#include "UND_Rotate.h"
#include "UND_Magnify.h"
#include "UND_ShowPoints.h"
#include "UND_TopToBtm.h"
#include "UND_LeftToRight.h"
#include "UND_Delete.h"
#include "UND_Copy.h"
#include "UND_Edit.h"
#include "UND_Property.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraphSoftDoc
IMPLEMENT_DYNCREATE(CGraphSoftDoc, CDocument)
BEGIN_MESSAGE_MAP(CGraphSoftDoc, CDocument)
//{{AFX_MSG_MAP(CGraphSoftDoc)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_FILE_IMPORT, OnFileImport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGraphSoftDoc construction/destruction
CGraphSoftDoc::CGraphSoftDoc()
{
// TODO: add one-time construction code here
m_nCurrentShapeType=_shape_none;
m_pCore=new CCore(this,NULL);
m_sizeDoc = CSize(800*36,650*36);//36最大扩大倍数
}
CGraphSoftDoc::~CGraphSoftDoc()
{
delete m_pCore;
}
BOOL CGraphSoftDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CGraphSoftDoc serialization
void CGraphSoftDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
m_shapeList.Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CGraphSoftDoc diagnostics
#ifdef _DEBUG
void CGraphSoftDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CGraphSoftDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGraphSoftDoc commands
void CGraphSoftDoc::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
while(!m_shapeList.IsEmpty())
{
m_shapeList.RemoveHead();
}
CDocument::DeleteContents();
}
BOOL CGraphSoftDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
((CGraphSoftApp*)AfxGetApp())->m_strFileName = lpszPathName;
return CDocument::OnSaveDocument(lpszPathName);
}
void CGraphSoftDoc::OnCloseDocument()
{
// TODO: Add your specialized code here and/or call the base class
CDocument::OnCloseDocument();
}
void CGraphSoftDoc::OnFileNew()
{
// TODO: Add your command handler code here
if(m_pCore->IsAbleToUndo()||m_pCore->IsAbleToRedo()){
CString strTitle,strDetail;
strTitle.LoadString(IDS_FILE_SAVE_TITLE);
strDetail.LoadString(IDS_FILE_SAVE_STRING);
int nRtn=AfxGetApp()->GetMainWnd()->MessageBox(strDetail,strTitle,MB_YESNOCANCEL);
if(nRtn==IDYES){
SendMessage(AfxGetApp()->GetMainWnd()->GetSafeHwnd(),WM_COMMAND,WPARAM(ID_FILE_SAVE),NULL);
}else if(nRtn==IDCANCEL){
return ;
}
}
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
m_nCurrentShapeType=_shape_none;
if(pView!=NULL)
{
pView->m_graph_op=_graph_noneOp;
pView->m_bDrawFlag = FALSE;
}
delete m_pCore;
m_pCore=new CCore(this,pView);
CDocument::UpdateAllViews(NULL);
CDocument::OnNewDocument();
}
////////////////////////////////////////////////////////
bool CGraphSoftDoc::HasGraphSelected()
{
bool bRlt=FALSE;
POSITION pos;
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected()){
bRlt=TRUE;
break;
}
}
return bRlt;
}
//////////////////////////////////////////////////////////////////
CRect CGraphSoftDoc::GetSelectGraphBoundaryRect()
{
CRect rectRltVal(0,0,0,0);
CRect rectTemp;
int nflag=0;
if(HasGraphSelected()){
POSITION pos;
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected()){
if(nflag==0){
rectRltVal=pShape->GetBoundaryRect();
nflag=1;
}else{
rectTemp=pShape->GetBoundaryRect();
rectRltVal.left=min(rectRltVal.left,rectTemp.left);
rectRltVal.right=max(rectRltVal.right,rectTemp.right);
rectRltVal.top=min(rectRltVal.top,rectTemp.top);
rectRltVal.bottom=max(rectRltVal.bottom,rectTemp.bottom);
}
}
}
}
return rectRltVal;
}
///////////////////////////////////////////////////////
void CGraphSoftDoc::RefreshSelectGraph()
{
POSITION pos;
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected()){
pShape->RefreshData(TRUE);
}
}
}
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CGraphSoftDoc::SelectInRect(CRect* pRect,CDC* pDC,UINT nFlag,bool bDraw)
{
CRect rect;
POSITION pos;
rect.left=pRect->leftright?pRect->left:pRect->right;
rect.right=pRect->leftright?pRect->right:pRect->left;
rect.top=pRect->topbottom?pRect->top:pRect->bottom;
rect.bottom=pRect->topbottom?pRect->bottom:pRect->top;
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
// int old_rop = pDC->SetROP2(R2_XORPEN);
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(nFlag&MK_SHIFT){
if(pShape->IsInRect(rect)){
pShape->SetSelectFlag(!pShape->IsSelected());
if(pShape->IsSelected()){
pShape->DrawCutToRect(pDC,RGB(255,0,0),RGB(255,0,0),pView->m_rectTarget,pView->m_rectMaxWindow);
}else{
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
}else{
if(pShape->IsInRect(rect)){
if(bDraw)
pShape->DrawCutToRect(pDC,RGB(255,0,0),RGB(255,0,0),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->SetSelectFlag(TRUE);
}
else if(pShape->IsSelected())
{
pShape->SetSelectFlag(FALSE);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
}
// pDC->SetROP2(old_rop);
}
////////////
int CGraphSoftDoc::GetSelectNum()
{
POSITION pos;
int nRltVal=0;
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
nRltVal++;
}
}
return nRltVal;
}
void CGraphSoftDoc::SetGraphDrawPtFlag(int nFlag,CDC* pDC)
{
POSITION pos;
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
if(pShape->GetDrawPointsFlag()==1){
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
}
pShape->SetDrawPointsFlag(nFlag);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
CRect rect=GetSelectGraphBoundaryRect();
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::GraphLeftToRight(CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->LeftToRight(pDC,rect);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::GraphTopToBottom(CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->TopToBottom(pDC,rect);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
////////
void CGraphSoftDoc::SetEditStateCursor(float flx,float fly,float flRate)
{
POSITION pos;
CShape* pShape;
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
break;
}
}
UINT unID=0;
if(pShape->IsSelected()){
if(pShape->GetPtState(flx,fly,flRate)==1){
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_POINTSEL));
unID=IDC_GRAPH_POINTSEL;
}else if(pShape->GetPtState(flx,fly,flRate)==2){
float flRate=pShape->GetSelectLineRate();
if(flRate>=2){
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT_LINE0));
unID=IDC_GRAPH_EDIT_LINE0;
}else if(flRate>=0.5){
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT_LINE3));
unID=IDC_GRAPH_EDIT_LINE3;
}else if(flRate>=-0.5){
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT_LINE2));
unID=IDC_GRAPH_EDIT_LINE2;
}else if(flRate>=-2){
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT_LINE1));
unID=IDC_GRAPH_EDIT_LINE1;
}else {
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT_LINE0));
unID=IDC_GRAPH_EDIT_LINE0;
}
}else{
SetCursor(AfxGetApp()->LoadCursor(IDC_GRAPH_EDIT));
unID=IDC_GRAPH_EDIT;
}
}
((CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView())->m_unPrevCur=unID;
}
void CGraphSoftDoc::ExPort(FILE* outStream)
{
POSITION pos;
CShape* pShape;
int num=m_shapeList.GetCount();
fprintf(outStream,"%d\n",num);
pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
pShape=m_shapeList.GetNext(pos);
pShape->ExPort(outStream);
}
}
void CGraphSoftDoc::ImPort(FILE* inStream)
{
m_shapeList.RemoveAll();
int num;
// int nshapeType;
CShape* pShape;
fscanf(inStream,"%d",&num);
for(int i=0;iImPort(inStream);
m_shapeList.AddTail(pShape);
CUND_Base* pUndo = new CUND_Create(pShape);
m_pCore->AddUndoItem(pUndo);
}
CDocument::UpdateAllViews(NULL);
}
//////////////////////////////////////////////////////////////////
void CGraphSoftDoc::OnFileImport()
{
// TODO: Add your command handler code here
CFileDialog dlg(true,_T("lld"),_T("*.lld"),OFN_ENABLESIZING,_T("*.lld|*.lld"));
if (dlg.DoModal()==IDOK)
{
if(dlg.GetFileExt()=="lld"){
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
CString strTitle,strDetail;
strTitle.LoadString(IDS_FILE_SAVE_TITLE);
strDetail.LoadString(IDS_FILE_SAVE_STRING);
if(m_pCore->IsAbleToUndo()||m_pCore->IsAbleToRedo())
{
int nRtn=AfxGetApp()->GetMainWnd()->MessageBox(strDetail,strTitle,MB_YESNOCANCEL|MB_APPLMODAL);
if(nRtn==IDYES){
SendMessage(AfxGetApp()->GetMainWnd()->GetSafeHwnd(),WM_COMMAND,WPARAM(ID_FILE_SAVE),NULL);
}else if(nRtn==IDCANCEL){
return ;
}
}
m_nCurrentShapeType=_shape_none;
if(pView!=NULL)
{
pView->m_graph_op=_graph_noneOp;
pView->m_bDrawFlag = FALSE;
}
delete m_pCore;
m_pCore=new CCore(this,pView);
FILE *InStream;
InStream = fopen(dlg.GetPathName(), "r" );
ImPort(InStream);
fclose(InStream);
CMainFrame* pMainFrm = (CMainFrame*) AfxGetApp()->GetMainWnd();
if(pMainFrm && pView){
pMainFrm->m_wndFullView.m_pBtn->m_targetrect=pMainFrm->m_wndFullView.m_pBtn->m_MaxRect;
pMainFrm->m_wndFullView.m_pBtn->Invalidate(TRUE);
pView->m_rectMaxWindow = pView->m_rectTarget = CRect(0,0,800,650);
pView->SetScrollSizes(MM_TEXT,CSize(0,0));
pView->Invalidate(TRUE);
}
}
}
}
void CGraphSoftDoc::SetSelGraphColor(COLORREF color,CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->SetColor(color);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::SetSelGraphFillClr(COLORREF color,CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->SetFillStyle(_shape_solid_fill);
pShape->SetFillColor(color);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::SetSelGraphFillSty(SHAPE_FILLSTYLE nStyle,CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->SetFillStyle(nStyle);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::SetSelGraphPenWidth(int nWidth,CDC* pDC)
{
POSITION pos;
CRect rect=GetSelectGraphBoundaryRect();
pos=m_shapeList.GetHeadPosition();
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
pShape->DrawCutToRect(pDC,RGB(255,255,255),RGB(255,255,255),pView->m_rectTarget,pView->m_rectMaxWindow);
pShape->SetPenWidth(nWidth);
pShape->DrawCutToRect(pDC,pView->m_rectTarget,pView->m_rectMaxWindow);
}
}
rect.left/=1000;
rect.top/=1000;
rect.bottom/=1000;
rect.right/=1000;
pView->Redraw(pDC,&rect);
}
void CGraphSoftDoc::GetSelGraphIDArray(CArray& arrID)
{
POSITION pos;
pos=m_shapeList.GetHeadPosition();
arrID.RemoveAll();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
arrID.Add(pShape->GetID());
}
}
}
void CGraphSoftDoc::GetSelGraphArray(CArray& ArrShape)
{
POSITION pos;
pos=m_shapeList.GetHeadPosition();
ArrShape.RemoveAll();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
ArrShape.Add(pShape);
}
}
}
void CGraphSoftDoc::GetSelGraphPropertyArr(CArray& ArrProperty)
{
POSITION pos;
pos=m_shapeList.GetHeadPosition();
ArrProperty.RemoveAll();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
if(pShape->IsSelected())
{
SUNDPropertyUnit unitProperty;
unitProperty.nID=pShape->GetID();
unitProperty.color=pShape->GetColor();
unitProperty.colorFill=pShape->GetFillColor();
unitProperty.fillStyle=pShape->GetFillStyle();
unitProperty.nPenWidth=pShape->GetPenWidth();
unitProperty.nPtShow=pShape->GetDrawPointsFlag();
ArrProperty.Add(unitProperty);
}
}
}
////////////////////////////////////////////////////////////////////////////////
//End of File
BOOL CGraphSoftDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
CGraphSoftView* pView=(CGraphSoftView*)((CMainFrame*)AfxGetApp()->GetMainWnd())->GetActiveView();
CString strTitle,strDetail;
strTitle.LoadString(IDS_FILE_SAVE_TITLE);
strDetail.LoadString(IDS_FILE_SAVE_STRING);
if(m_pCore->IsAbleToUndo()||m_pCore->IsAbleToRedo())
{
int nRtn=AfxGetApp()->GetMainWnd()->MessageBox(strDetail,strTitle,MB_YESNOCANCEL|MB_APPLMODAL);
if(nRtn==IDYES){
SendMessage(AfxGetApp()->GetMainWnd()->GetSafeHwnd(),WM_COMMAND,WPARAM(ID_FILE_SAVE),NULL);
}else if(nRtn==IDCANCEL){
return FALSE;
}
}
m_nCurrentShapeType=_shape_none;
if(pView!=NULL)
{
pView->m_graph_op=_graph_noneOp;
pView->m_bDrawFlag = FALSE;
}
if(((CGraphSoftApp*)AfxGetApp())->m_strFileName.Compare(lpszPathName)!=0) {
delete m_pCore;//删除撤消/恢复对象指针,以及删除图形对象指针
m_shapeList.RemoveAll();
m_pCore=new CCore(this,pView);
}
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
if(((CGraphSoftApp*)AfxGetApp())->m_strFileName.Compare(lpszPathName)!=0) {
CUND_Base* pUNDBase;
POSITION pos=m_shapeList.GetHeadPosition();
while (pos!=NULL) {
CShape* pShape=m_shapeList.GetNext(pos);
pUNDBase = new CUND_Create(pShape);
m_pCore->AddUndoItem(pUNDBase);
}
}
((CGraphSoftApp*)AfxGetApp())->m_strFileName = lpszPathName;
CMainFrame* pMainFrm = (CMainFrame*) AfxGetApp()->GetMainWnd();
if(pMainFrm && pView){
pMainFrm->m_wndFullView.m_pBtn->m_targetrect=pMainFrm->m_wndFullView.m_pBtn->m_MaxRect;
pMainFrm->m_wndFullView.m_pBtn->Invalidate(TRUE);
pView->m_rectMaxWindow = pView->m_rectTarget = CRect(0,0,800,650);
pView->SetScrollSizes(MM_TEXT,CSize(0,0));
pView->Invalidate(TRUE);
}
return TRUE;
}