?? mainview.cpp
字號:
// mainview.cpp : implementation of the CMainView class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "oclient.h"
#include "maindoc.h"
#include "mainview.h"
#include "rectitem.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainView
CBrush NEAR CMainView::m_brHatch;
CLIPFORMAT CMainView::m_cfObjectDescriptor=NULL;
IMPLEMENT_DYNCREATE(CMainView, CScrollView)
BEGIN_MESSAGE_MAP(CMainView, CScrollView)
//{{AFX_MSG_MAP(CMainView)
ON_COMMAND(ID_EDIT_PASTE, OnPaste)
ON_COMMAND(ID_EDIT_PASTE_LINK, OnPasteLink)
ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditMenu)
ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_RBUTTONDOWN()
ON_WM_CHAR()
ON_WM_SETFOCUS()
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(ID_OBJECT_DISPLAYCONTENT, OnObjectDisplayContent)
ON_UPDATE_COMMAND_UI(ID_OBJECT_DISPLAYCONTENT, OnUpdateObjectDisplayContent)
ON_COMMAND(ID_OBJECT_DISPLAYASICON, OnObjectDisplayAsIcon)
ON_UPDATE_COMMAND_UI(ID_OBJECT_DISPLAYASICON, OnUpdateObjectDisplayAsIcon)
ON_COMMAND(ID_EDIT_PASTE_SPECIAL, OnPasteSpecial)
ON_UPDATE_COMMAND_UI(ID_EDIT_CLONE, OnUpdateEditClone)
ON_COMMAND(ID_EDIT_CLONE, OnEditClone)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_SPECIAL, OnUpdateEditPaste)
ON_COMMAND(ID_OBJECT_RESETSIZE, OnObjectResetsize)
ON_COMMAND(ID_CANCEL_INPLACE, OnCancelInplace)
ON_WM_DESTROY()
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditMenu)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditMenu)
ON_UPDATE_COMMAND_UI(ID_OBJECT_RESETSIZE, OnUpdateEditMenu)
ON_COMMAND(ID_OLE_CHANGE_SOURCE, OnOleChangeSource)
ON_UPDATE_COMMAND_UI(ID_OLE_CHANGE_SOURCE, OnUpdateOleChangeSource)
ON_COMMAND(ID_OLE_EDIT_PROPERTIES, OnOleEditProperties)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_PROPERTIES, OnUpdateOleEditProperties)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMainView construction/destruction
CMainView::CMainView()
{
if (m_brHatch.m_hObject == NULL)
m_brHatch.CreateHatchBrush(HS_DIAGCROSS, RGB(0,0,0));
if (m_cfObjectDescriptor == NULL)
m_cfObjectDescriptor =
(CLIPFORMAT)::RegisterClipboardFormat(_T("Object Descriptor"));
m_pSelection = NULL;
m_prevDropEffect = DROPEFFECT_NONE;
m_bInDrag = FALSE;
}
CMainView::~CMainView()
{
}
void CMainView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// We can't pass MM_ANISOTROPIC to SetScrollSizes so we have to convert to MM_TEXT
CSize size = GetDocument()->GetDocumentSize();
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);
}
/////////////////////////////////////////////////////////////////////////////
// CMainView drawing
void CMainView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnPrepareDC(pDC, pInfo);
// set up a reasonable default context
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
// LOENGLISH units are based on physical inches
// We want logical inches so we have to do it differently
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetViewportExt(
pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
pDC->SetWindowExt(100,-100);
}
void CMainView::SetupTracker(CRectTracker* pTracker, CRectItem* pItem,
CRect* pTrueRect)
{
ASSERT(pTracker != NULL);
ASSERT(pItem != NULL);
pTracker->m_rect = pItem->GetRect();
DocToClient(pTracker->m_rect);
// set minimum size for our OLE items
pTracker->m_sizeMin.cx = 8;
pTracker->m_sizeMin.cy = 8;
pTracker->m_nStyle = 0;
// setup resize handles if item is selected
if (pItem == m_pSelection)
pTracker->m_nStyle |= CRectTracker::resizeInside;
// put correct border depending on item type
if (pItem->GetType() == OT_LINK)
pTracker->m_nStyle |= CRectTracker::dottedLine;
else
pTracker->m_nStyle |= CRectTracker::solidLine;
// put hatching over the item if it is currently open
if (pItem->GetItemState() == COleClientItem::openState ||
pItem->GetItemState() == COleClientItem::activeUIState)
{
pTracker->m_nStyle |= CRectTracker::hatchInside;
}
if (pTrueRect != NULL)
pTracker->GetTrueRect(pTrueRect);
}
void CMainView::OnDraw(CDC* pDC)
{
CMainDoc* pDoc = GetDocument();
ASSERT_VALID(pDC);
if (!pDC->IsPrinting())
{
m_brHatch.UnrealizeObject();
CPoint point(0, 0);
pDC->LPtoDP(&point);
pDC->SetBrushOrg(point.x % 8, point.y % 8);
CRect rcClip;
GetClientRect(&rcClip);
ClientToDoc(rcClip);
CSize docSize = pDoc->GetDocumentSize();
if (rcClip.right > docSize.cx)
{
CRect rcFill(rcClip);
rcFill.left = max(rcFill.left,docSize.cx);
pDC->FillRect(rcFill,&m_brHatch);
}
if (rcClip.bottom < -docSize.cy)
{
CRect rcFill(rcClip);
rcFill.top = min(rcFill.top, -docSize.cy);
pDC->FillRect(rcFill,&m_brHatch);
}
}
// Draw all the CRectItems
POSITION pos = pDoc->GetStartPosition();
while (pos != NULL)
{
CRectItem* pItem = DYNAMIC_DOWNCAST(CRectItem, pDoc->GetNextItem(pos));
if (pItem != NULL)
{
pItem->Draw(pDC, pItem->GetRect());
if (!pDC->IsPrinting())
{
// draw the tracker
CRectTracker tracker;
CRect rectTrue;
SetupTracker(&tracker, pItem, &rectTrue);
ClientToDoc(rectTrue);
if (pDC->RectVisible(&rectTrue))
tracker.Draw(pDC);
}
}
}
}
// pHint is the deleted item or NULL if deselect/delete all
void CMainView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
if (pHint == NULL && lHint == 0)
{
// some sort of clear all
m_pSelection = NULL;
}
if (pHint != NULL && pHint->IsKindOf(RUNTIME_CLASS(CRectItem)))
{
// just invalidate the one item
InvalidateItem((CRectItem*)pHint);
// clear selection if pointing to deleted item
if (lHint == 1 && pHint == m_pSelection)
{
// specific case of pHint being deleted
m_pSelection = NULL;
}
}
else if (lHint != 0)
{
// invalidate arbitrary rectangle
InvalidateRect((CRect*)lHint);
}
else
{
// complete update
CScrollView::OnUpdate(pSender, lHint, pHint);
}
}
void CMainView::InvalidateItem(CRectItem* pItem)
{
if (m_nMapMode != 0)
{
CRectTracker tracker;
CRect rect;
SetupTracker(&tracker, pItem, &rect);
InvalidateRect(&rect);
}
}
BOOL CMainView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
{
// remove drag/drop feedback before scrolling
if (bDoScroll && m_prevDropEffect != DROPEFFECT_NONE)
{
CClientDC dc(this);
dc.DrawFocusRect(CRect(m_dragPoint, m_dragSize));
// erase previous focus rect
m_prevDropEffect = DROPEFFECT_NONE;
}
// do the scroll
if (!CScrollView::OnScrollBy(sizeScroll, bDoScroll))
return FALSE;
// update the position of any in-place active item
if (bDoScroll)
{
UpdateActiveItem();
UpdateWindow();
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainView printing
BOOL CMainView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// Selection support
BOOL CMainView::IsSelected(const CObject* pDocItem) const
{
return (pDocItem == m_pSelection);
}
void CMainView::SetSelection(CRectItem* pNewSel, BOOL bSafeSelect)
{
if (pNewSel != NULL && pNewSel == m_pSelection)
return;
// deactivate any in-place active item on this view!
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pNewSel != pActiveItem)
{
if (bSafeSelect)
return;
// if we found one, deactivate it
pActiveItem->Close();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
if (m_pSelection != NULL) // invalidate the old item
InvalidateItem(m_pSelection);
if ((m_pSelection = pNewSel) != NULL) // invalidate the new item
InvalidateItem(m_pSelection);
}
/////////////////////////////////////////////////////////////////////////////
// CMainView diagnostics
#ifdef _DEBUG
void CMainView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMainView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// Main 'Edit' menu commands
void CMainView::OnUpdateEditMenu(CCmdUI* pCmdUI)
{
// most Edit menu commands are enabled only if we have a selection
// and there are no in-place activations for this view
pCmdUI->Enable(m_pSelection != NULL &&
GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
void CMainView::OnEditCut()
{
ASSERT(m_pSelection != NULL);
TRY
{
m_pSelection->CopyToClipboard(TRUE);
OnEditClear();
}
CATCH_ALL(e)
{
AfxMessageBox(IDP_CLIPBOARD_CUT_FAILED);
}
END_CATCH_ALL
}
void CMainView::OnEditCopy()
{
ASSERT(m_pSelection != NULL);
TRY
{
m_pSelection->CopyToClipboard(TRUE);
}
CATCH_ALL(e)
{
AfxMessageBox(IDP_CLIPBOARD_COPY_FAILED);
}
END_CATCH_ALL
}
void CMainView::OnEditClear()
{
if (m_pSelection != NULL)
GetDocument()->DeleteItem(m_pSelection);
}
void CMainView::OnPaste()
{
if (DoPasteItem(FALSE, NULL, NULL) == NULL)
AfxMessageBox(IDP_GET_FROM_CLIPBOARD_FAILED);
}
void CMainView::OnPasteLink()
{
if (DoPasteItem(TRUE, NULL, NULL) == NULL)
AfxMessageBox(IDP_GET_FROM_CLIPBOARD_FAILED);
}
void CMainView::DoPasteNative(
COleDataObject* pDataObject, CPoint* pPoint, CRectItem* pItem)
{
// get file refering to clipboard data
CFile* pFile = pDataObject->GetFileData(CMainDoc::m_cfPrivate);
if (pFile == NULL)
{
// if the file failed to open, throw an exception
// to force cleanup in DoPasteItem. the exact
// type of exception thrown here is unimportant...
AfxThrowFileException(CFileException::generic);
}
CArchive ar(pFile, CArchive::load);
TRY
{
// connect the file to an archive and read the data
ar.m_pDocument = GetDocument(); // for COleClientItem serialize
pItem->Serialize(ar);
}
CATCH_ALL(e)
{
ar.Close();
delete pFile;
THROW_LAST();
}
END_CATCH_ALL
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -