Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 25 / lab22 / lab22View
.cpp// lab22View.cpp : implementation of the CLab22View class
//
#include "stdafx.h"
#include "lab22.h"
#include "SetDialog.h"
#include "ScaleDlg.h"
#include "lab22Doc.h"
#include "lab22View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLab22View
IMPLEMENT_DYNCREATE(CLab22View, CScrollView)
BEGIN_MESSAGE_MAP(CLab22View, CScrollView)
//{{AFX_MSG_MAP(CLab22View)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_CHAR()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_FIGURE_DELETE, OnFigureDelete)
ON_COMMAND(ID_SCALE, OnScale)
ON_COMMAND(ID_CONTEINRE_SETVIEW, OnConteinreSetview)
ON_COMMAND(ID_SETS_SET1, OnSetsSet1)
ON_UPDATE_COMMAND_UI(ID_SETS_SET1, OnUpdateSetsSet1)
ON_COMMAND(ID_SETS_SET2, OnSetsSet2)
ON_UPDATE_COMMAND_UI(ID_SETS_SET2, OnUpdateSetsSet2)
ON_COMMAND(ID_CONTEINER_SET2VIEW, OnConteinerSet2view)
ON_COMMAND(ID_SETS_UNOIN, OnSetsUnoin)
ON_COMMAND(ID_SETS_CROSS, OnSetsCross)
ON_COMMAND(ID_SETS_SUBTRACTION, OnSetsSubtraction)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CLab22View construction/destruction
CLab22View::CLab22View()
{
// TODO: add construction code here
m_FirstPoint = CPoint(0,0);
m_SecondPoint = CPoint(0,0);
m_MoveLastPoint = CPoint(0,0);
m_pTempElement = 0;
m_pSelected = 0;
StringData="";
CaretCreated=false;
doMove=false;
m_Scale=1;
SetScrollSizes(MM_TEXT, CSize(0,0));
SetNom=1;
}
CLab22View::~CLab22View()
{
}
BOOL CLab22View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLab22View drawing
void CLab22View::OnDraw(CDC* pDC)
{
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str;
if (SetNom==1)
{
if (pDoc->set1.count()!=0) //при перерисовка окна, перерисовываем все фигуры по итератору
{
ArSetIterator<Shape*> it(pDoc->set1);
while(!it.end())
{
if(pDC->RectVisible(it.currentItem()->GetBoundRect()))
{
str.Format("%d",it.currentPos());
pDC->TextOut(it.currentItem()->getX()-10,it.currentItem()->getY()-10,str);
it.currentItem()->Draw(pDC,m_pSelected);
}
it.next();
}
}
}
if (SetNom==2)
{
if (pDoc->set2.count()!=0) //при перерисовка окна, перерисовываем все фигуры по итератору
{
ArSetIterator<Shape*> it2(pDoc->set2);
while(!it2.end())
{
if(pDC->RectVisible(it2.currentItem()->GetBoundRect()))
{
str.Format("%d",it2.currentPos());
pDC->TextOut(it2.currentItem()->getX()-10,it2.currentItem()->getY()-10,str);
it2.currentItem()->Draw(pDC,m_pSelected);
}
it2.next();
}
}
}
if (SetNom==3)
{
if (pDoc->set3.count()!=0) //при перерисовка окна, перерисовываем все фигуры по итератору
{
ArSetIterator<Shape*> it3(pDoc->set3);
while(!it3.end())
{
if(pDC->RectVisible(it3.currentItem()->GetBoundRect()))
{
str.Format("%d",it3.currentPos());
pDC->TextOut(it3.currentItem()->getX()-10,it3.currentItem()->getY()-10,str);
it3.currentItem()->Draw(pDC,m_pSelected);
}
it3.next();
}
}
}
}
void CLab22View::OnInitialUpdate()
{
ResetScrollSizes();
CScrollView::OnInitialUpdate();
}
/////////////////////////////////////////////////////////////////////////////
// CLab22View printing
BOOL CLab22View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLab22View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLab22View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CLab22View diagnostics
#ifdef _DEBUG
void CLab22View::AssertValid() const
{
CScrollView::AssertValid();
}
void CLab22View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CLab22Doc* CLab22View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLab22Doc)));
return (CLab22Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLab22View message handlers
Shape* CLab22View::CreateElement() //создаем фигуру выбранного типа
{
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
switch(pDoc->GetElementType())
{
case TRIANGLE:
return new CTriangleEntity(m_SecondPoint.x-m_FirstPoint.x,m_SecondPoint.y-m_FirstPoint.y,m_FirstPoint.x,m_FirstPoint.y);
case TRAPEZ:
return new CTrapeziumEntity(10,m_SecondPoint.x-m_FirstPoint.x,m_SecondPoint.y-m_FirstPoint.y,m_FirstPoint.x,m_FirstPoint.y);
case TEXT:
return new CTextEntity(StringData,m_FirstPoint.x,m_FirstPoint.y);
case TRAPEZTEXT:
return new CTextTrapeziumEntity(StringData,10,m_SecondPoint.x-m_FirstPoint.x,m_SecondPoint.y-m_FirstPoint.y,m_FirstPoint.x,m_FirstPoint.y);
default:
AfxMessageBox("Bad Element code", MB_OK);
AfxAbort();
return 0;
}
}
Shape* CLab22View::SelectElement(CPoint aPoint)
{
CLab22Doc* pDoc = GetDocument(); //выделенный элемент
ASSERT_VALID(pDoc);
CClientDC aDC(this);
OnPrepareDC(&aDC);
aDC.DPtoLP(&aPoint);
Shape* element=0;
CRect aRect(0,0,0,0);
if (SetNom==1)
{
if (pDoc->set1.count()!=0)
{
ArSetIterator<Shape*> it(pDoc->set1);
while(!it.end())
{
element=it.currentItem();
aRect=it.currentItem()->GetBoundRect();
if(aRect.PtInRect(aPoint))
return element;
it.next();
}
}
}
if (SetNom==2)
{
if (pDoc->set2.count()!=0)
{
ArSetIterator<Shape*> it2(pDoc->set2);
while(!it2.end())
{
element=it2.currentItem();
aRect=it2.currentItem()->GetBoundRect();
if(aRect.PtInRect(aPoint))
return element;
it2.next();
}
}
}
return 0;
}
void CLab22View::ShowCursor(CDC* pDC)
{ if (!CaretCreated)
{
TEXTMETRIC textmetric;
pDC->GetTextMetrics(&textmetric);
CreateSolidCaret(textmetric.tmAveCharWidth/8,textmetric.tmHeight);
SetCaretPos(m_FirstPoint);
ShowCaret();
CaretCreated=true;
}
}
void CLab22View::ResetScrollSizes()
{
CClientDC aDC(this);
OnPrepareDC(&aDC);
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CSize DocSize = pDoc->GetDocSize();
aDC.LPtoDP(&DocSize);
SetScrollSizes(MM_TEXT, DocSize);
}
void CLab22View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_FirstPoint = point;
StringData.Empty();
SetCapture();
HideCaret();
CaretCreated=false;
CScrollView::OnLButtonDown(nFlags, point);
}
void CLab22View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC aDC(this);
if (pDoc->is_TextEnter)
{
if (!doMove)
{
m_SecondPoint = point;
ShowCursor(&aDC);
}
else
{
if(this == GetCapture())
ReleaseCapture();
}
}
else
{
if (m_pTempElement)
{
if (SetNom==1)
{
pDoc->set1.insertEl(pDoc->index1,m_pTempElement);
pDoc->index1++;
Invalidate();
}//добавление в контейнер
else
{
pDoc->set2.insertEl(pDoc->index2,m_pTempElement);
pDoc->index2++;
pDoc->index2++;
Invalidate();
}
}
if(this == GetCapture())
ReleaseCapture();
}
if (doMove==true){doMove=false;}
if(m_pTempElement)
{
m_pTempElement = 0;
}
CScrollView::OnLButtonUp(nFlags, point);
}
void CLab22View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC aDC(this);
OnPrepareDC(&aDC);
if((nFlags & MK_LBUTTON) && (this == GetCapture()))
{
if (!doMove)
{
aDC.DPtoLP(&point);
m_SecondPoint = point;
if(m_pTempElement)
{
aDC.SetROP2(R2_NOTXORPEN); //перерисовка фигуры
m_pTempElement->Draw(&aDC);
delete m_pTempElement;
m_pTempElement = 0;
}
m_pTempElement = CreateElement();
m_pTempElement->Draw(&aDC);
}
else
{
aDC.SetROP2(R2_NOTXORPEN); //перемещение фигуры
m_pSelected->Draw(&aDC,m_pSelected);
m_pSelected->move(point.x-m_MoveLastPoint.x,point.y-m_MoveLastPoint.y);
m_pSelected->Draw(&aDC,m_pSelected);
Invalidate();
}
}
else
{
CRect aRect;
Shape* pCurrentSelection = SelectElement(point); //выделение фигуры
if ((pCurrentSelection==m_pSelected)&&(m_pSelected)){doMove=true;}
if(pCurrentSelection!=m_pSelected)
{ doMove=false;
if(m_pSelected)
{
aRect = m_pSelected->GetBoundRect();
aDC.LPtoDP(aRect);
aRect.NormalizeRect();
InvalidateRect(aRect, FALSE);
}
m_pSelected = pCurrentSelection;
if(m_pSelected)
{
aRect = m_pSelected->GetBoundRect();
aDC.LPtoDP(aRect);
aRect.NormalizeRect();
InvalidateRect(aRect, FALSE);
}
}
}
m_MoveLastPoint=point;
CScrollView::OnMouseMove(nFlags, point);
}
void CLab22View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
HideCaret();
CClientDC aDC(this);
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (nChar==VK_SPACE)
{
if(m_pTempElement)
{
m_pTempElement->Draw(&aDC);
if (SetNom==1)
{
pDoc->set1.insertEl(pDoc->index1,m_pTempElement); //добавление эт-та в контейнер
pDoc->index1++;
Invalidate();
}
else
{
pDoc->set2.insertEl(pDoc->index2,m_pTempElement); //добавление эт-та в контейнер
pDoc->index2++;
pDoc->index2++;
Invalidate();
}
m_pTempElement = 0;
}
if(this == GetCapture())
{ ReleaseCapture(); }
HideCaret();CaretCreated=false;
StringData.Empty();
}
else
{
if(this == GetCapture()) //перерисовка элемента
{
StringData+=nChar;
if(m_pTempElement)
{
m_pTempElement->Draw(&aDC);
delete m_pTempElement;
m_pTempElement = 0;
}
m_pTempElement = CreateElement();
m_pTempElement->Draw(&aDC);
CSize size=aDC.GetTextExtent(StringData);
CPoint point(m_FirstPoint.x+size.cx,m_FirstPoint.y);
SetCaretPos(point);
ShowCaret();
}
}
CScrollView::OnChar(nChar, nRepCnt, nFlags);
}
void CLab22View::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu aMenu;
aMenu.LoadMenu(IDR_CONTEXT_MENU); //контекстное меню
ClientToScreen(&point);
if(m_pSelected)
{
aMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
point.x, point.y, this);
}
else
{
WORD ElementType = GetDocument()->GetElementType();
aMenu.CheckMenuItem(ID_ELEMENTS_TRIANGLE,
(TRIANGLE==ElementType?MF_CHECKED:MF_UNCHECKED)|MF_BYCOMMAND);
aMenu.CheckMenuItem(ID_ELEMENTS_TRAPEZIUM,
(TRAPEZ==ElementType?MF_CHECKED:MF_UNCHECKED)|MF_BYCOMMAND);
aMenu.CheckMenuItem(ID_ELEMENTS_TEXT,
(TEXT==ElementType?MF_CHECKED:MF_UNCHECKED)|MF_BYCOMMAND);
aMenu.CheckMenuItem(ID_ELEMENTS_TRAPEZIUMWITHTEXT,
(TRAPEZTEXT==ElementType?MF_CHECKED:MF_UNCHECKED)|MF_BYCOMMAND);
aMenu.GetSubMenu(1)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);
}
}
void CLab22View::OnFigureDelete()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc); //удаление фигуры из контейнера
bool deleted;
if (SetNom==1)
{
ArSetIterator<Shape*> it(pDoc->set1);
deleted=false;
while((!it.end())&&(!deleted))
{
if (it.currentItem()==m_pSelected)
{pDoc->set1.deleteEl(it.currentPos());
deleted=true;
}
it.next();
}
}
if (SetNom==2)
{
ArSetIterator<Shape*> it2(pDoc->set2);
deleted=false;
while((!it2.end())&&(!deleted))
{
if (it2.currentItem()==m_pSelected)
{pDoc->set2.deleteEl(it2.currentPos());
deleted=true;
}
it2.next();
}
}
Invalidate();
}
void CLab22View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
if(pHint) //при перерисовке раб области (ползунки)
{
CClientDC aDC(this);
OnPrepareDC(&aDC);
CRect aRect=((Shape*)pHint)->GetBoundRect();
aDC.LPtoDP(aRect);
aRect.NormalizeRect();
InvalidateRect(aRect);
}
else
InvalidateRect(0);
}
void CLab22View::OnScale()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc); //вызов диалогового окна при масштабировании
ScaleDlg dlg;
dlg.m_scale=m_Scale;
if (dlg.DoModal()==IDOK)
{
m_Scale = dlg.m_scale;
pDoc->m_DocSize = CSize(3000*m_Scale,3000*m_Scale);
ResetScrollSizes();
InvalidateRect(0);
}
}
void CLab22View::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnPrepareDC(pDC, pInfo); //метод для масштабирования
/* CLab22Doc* pDoc = GetDocument();
pDC->SetMapMode(MM_ANISOTROPIC); // Set the map mode
CSize DocSize = pDoc->GetDocSize(); // Get the document size
// y extent must be negative because we want MM_LOENGLISH
DocSize.cy = -DocSize.cy; // Change sign of y
pDC->SetWindowExt(DocSize); // Now set the window extent
// Get the number of pixels per inch in x and y
int xLogPixels = pDC->GetDeviceCaps(LOGPIXELSX);
int yLogPixels = pDC->GetDeviceCaps(LOGPIXELSY);
// Calculate the viewport extent in x and y
long xExtent = (long)DocSize.cx*m_Scale*xLogPixels/100L;
long yExtent = (long)DocSize.cy*m_Scale*yLogPixels/100L;
pDC->SetViewportExt((int)xExtent, (int)-yExtent); // Set viewport extent
*/
}
void CLab22View::OnConteinreSetview()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument(); //вызов диалогового окна просмотра контейнера
ASSERT_VALID(pDoc);
SetDialog dlg;
if (pDoc->set1.count()!=0)
{
dlg.set=pDoc->set1;
ArSetIterator<Shape*> it(pDoc->set1);
dlg.m_x=it.currentItem()->getX();
dlg.m_y=it.currentItem()->getY();
dlg.m_name=it.currentItem()->getName();
dlg.m_cnt=pDoc->set1.count();
dlg.m_pos=it.currentPos();
dlg.m_num=1;
}
if (dlg.DoModal()==IDOK)
{
InvalidateRect(0);
}
}
void CLab22View::OnSetsSet1()
{
// TODO: Add your command handler code here
SetNom=1;
Invalidate();
}
void CLab22View::OnUpdateSetsSet1(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(SetNom==1);
}
void CLab22View::OnSetsSet2()
{
// TODO: Add your command handler code here
SetNom=2;
Invalidate();
}
void CLab22View::OnUpdateSetsSet2(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(SetNom==2);
}
void CLab22View::OnConteinerSet2view()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument(); //вызов диалогового окна просмотра контейнера
ASSERT_VALID(pDoc);
SetDialog dlg;
if (pDoc->set2.count()!=0)
{
dlg.set=pDoc->set2;
ArSetIterator<Shape*> it(pDoc->set2);
dlg.m_x=it.currentItem()->getX();
dlg.m_y=it.currentItem()->getY();
dlg.m_name=it.currentItem()->getName();
dlg.m_cnt=pDoc->set2.count();
dlg.m_pos=it.currentPos();
dlg.m_num=1;
}
if (dlg.DoModal()==IDOK)
{
InvalidateRect(0);
}
}
void CLab22View::OnSetsUnoin()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument(); //вызов диалогового окна просмотра контейнера
ASSERT_VALID(pDoc);
pDoc->set3.clean();
ArSetIterator<Shape*> it1(pDoc->set1);
ArSetIterator<Shape*> it2(pDoc->set2);
while (!it1.end())
{
pDoc->set3.insertEl(it1.currentPos(),it1.currentItem());
it1.next();
}
while (!it2.end())
{
if (!pDoc->set3.checkPos(it2.currentPos()))
{
pDoc->set3.insertEl(it2.currentPos(),it2.currentItem());
}
it2.next();
}
SetNom=3;
Invalidate();
}
void CLab22View::OnSetsCross()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument(); //вызов диалогового окна просмотра контейнера
ASSERT_VALID(pDoc);
pDoc->set3.clean();
ArSetIterator<Shape*> it1(pDoc->set1);
while (!it1.end())
{
ArSetIterator<Shape*> it2(pDoc->set2);
while (!it2.end())
{
if (it2.currentPos()==it1.currentPos())
{
pDoc->set3.insertEl(it1.currentPos(),it1.currentItem());
}
it2.next();
}
it1.next();
}
SetNom=3;
Invalidate();
}
void CLab22View::OnSetsSubtraction()
{
// TODO: Add your command handler code here
CLab22Doc* pDoc = GetDocument(); //вызов диалогового окна просмотра контейнера
ASSERT_VALID(pDoc);
pDoc->set3.clean();
ArSetIterator<Shape*> it1(pDoc->set1);
bool pres;
while (!it1.end())
{
pres=false;
ArSetIterator<Shape*> it2(pDoc->set2);
while (!it2.end())
{
if (it2.currentPos()==it1.currentPos())
{
pres=true;
}
it2.next();
}
if (!pres)
{
pDoc->set3.insertEl(it1.currentPos(),it1.currentItem());
}
it1.next();
}
SetNom=3;
Invalidate();
}
Соседние файлы в папке lab22