📄 editpaddoc.cpp
字号:
///////////////////////////////////////////////////////////////////////////
// File: editpaddoc.cpp
// Version: 1.2.0.5
// Updated: 16-Aug-1999
//
// Document core
//
// Copyright: Ferdinand Prantl, portions by Stcherbatchenko Andrei
// E-mail: prantl@ff.cuni.cz
//
// You are free to use or modify this code to the following restrictions:
// - Acknowledge me somewhere in your about box, simple "Parts of code by.."
// will be enough. If you can't (or don't want to), contact me personally.
// - LEAVE THIS HEADER INTACT
////////////////////////////////////////////////////////////////////////////
// editpaddoc.cpp : implementation of the CEditPadDoc class
//
#include "stdafx.h"
#include "editpad.h"
#include "editpaddoc.h"
#include "editcmd.h"
#include "filesup.h"
#include "mainfrm.h"
#include "editpadview.h"
#include "editreg.h"
#include "registry.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEditPadDoc
IMPLEMENT_DYNCREATE (CEditPadDoc, CDocument)
BEGIN_MESSAGE_MAP (CEditPadDoc, CDocument)
//{{AFX_MSG_MAP(CEditPadDoc)
ON_COMMAND (ID_READ_ONLY, OnReadOnly)
ON_UPDATE_COMMAND_UI (ID_READ_ONLY, OnUpdateReadOnly)
//}}AFX_MSG_MAP
END_MESSAGE_MAP ()
/////////////////////////////////////////////////////////////////////////////
// CEditPadDoc construction/destruction
#pragma warning(disable:4355)
CEditPadDoc::CEditPadDoc ():m_xTextBuffer (this)
{
}
#pragma warning(default:4355)
CEditPadDoc:: ~ CEditPadDoc ()
{
}
/////////////////////////////////////////////////////////////////////////////
// CEditPadDoc serialization
void CEditPadDoc::
Serialize (CArchive & ar)
{
if (ar.IsStoring ())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CEditPadDoc diagnostics
#ifdef _DEBUG
void CEditPadDoc::
AssertValid ()
const
{
CDocument::AssertValid ();
}
void CEditPadDoc::Dump (CDumpContext & dc)
const
{
CDocument::Dump (dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEditPadDoc commands
void CEditPadDoc::DeleteContents ()
{
CDocument::DeleteContents ();
m_xTextBuffer.FreeAll ();
}
void CEditPadDoc::CloseUnused ()
{
////////////////////////////////////////////////////////////////////
// loop through all of the documents
// and close the ones that match the criteria of:
// 1) NOT previously saved ( CDocument::GetPathName() is empty)
// and
// 2) NOT modified ( CDocument::IsModified () is zero )
// and
// 3) NOT the document we are in the process of opening (this)
////////////////////////////////////////////////////////////////////
CWinApp *pApp = AfxGetApp ();
CDocTemplate *pDocTemplate;
CDocument *pDoc;
POSITION tpos = pApp->GetFirstDocTemplatePosition (), dpos;
while(tpos)
{
pDocTemplate = pApp->GetNextDocTemplate (tpos);
if(pDocTemplate)
{
loop:
dpos = pDocTemplate->GetFirstDocPosition();
while(dpos)
{
pDoc = pDocTemplate->GetNextDoc (dpos);
if(this != pDoc && // if not self-referred
pDoc->GetPathName () == _T ("") && // and not saved
!pDoc->IsModified ()) // and not modified
{
pDoc->OnCloseDocument (); // then close it
goto loop; // and loop the doclist again
}
}
}
}
}
void CEditPadDoc::OnFileEvent (WPARAM wEvent, LPCTSTR pszPathName)
{
if (!(theApp.m_dwFlags & EP_NOTIFY_CHANGES))
return;
MessageBeep (MB_ICONEXCLAMATION);
CFrameWnd *pwndMain= (CFrameWnd*) theApp.GetMainWnd ();
ASSERT (pwndMain);
if (!pwndMain->IsWindowVisible ())
if(pwndMain->IsKindOf (RUNTIME_CLASS (CMainFrame)))
((CMainFrame*) pwndMain)->FlashUntilFocus ();
else
((CMainFrame2*) pwndMain)->FlashUntilFocus ();
CView *pView = (CView*) m_viewList.GetHead ();
if(pwndMain->IsKindOf (RUNTIME_CLASS (CMainFrame)))
{
((CMainFrame*) pwndMain)->MDIActivate (pView->GetParent ());
((CMainFrame*) pwndMain)->FlashUntilFocus ();
}
else
{
((CMainFrame2*) pwndMain)->MDIActivate (pView->GetParent ());
((CMainFrame2*) pwndMain)->FlashUntilFocus ();
}
if (wEvent & FE_MODIFIED)
{
bool bReload = (theApp.m_dwFlags & EP_AUTO_RELOAD) != 0;
if (!bReload)
{
CString sMsg;
sMsg.Format (IDS_FILE_CHANGED, pszPathName);
bReload = AfxMessageBox (sMsg, MB_YESNO|MB_ICONQUESTION) == IDYES;
}
if (bReload)
{
POSITION pos = GetFirstViewPosition ();
ASSERT (pos);
CEditPadView *pView;
do
{
pView = (CEditPadView*) GetNextView (pos);
// pView->PushCursor ();
}
while (pos);
m_xTextBuffer.FreeAll ();
m_xTextBuffer.LoadFromFile (pszPathName);
pos = GetFirstViewPosition ();
ASSERT (pos);
do
{
pView = (CEditPadView*) GetNextView (pos);
// pView->PopCursor ();
HWND hWnd = pView->GetSafeHwnd ();
::RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE|RDW_INTERNALPAINT|RDW_ERASE|RDW_ERASENOW|RDW_UPDATENOW|RDW_NOFRAME);
}
while (pos);
}
}
else if (wEvent & FE_DELETED)
{
if (!(theApp.m_dwFlags & EP_AUTO_RELOAD))
{
CString sMsg;
sMsg.Format (IDS_FILE_DELETED, pszPathName);
AfxMessageBox (sMsg, MB_OK|MB_ICONINFORMATION);
}
}
}
BOOL CEditPadDoc::
OnNewDocument ()
{
if (!CDocument::OnNewDocument ())
return FALSE;
m_xTextBuffer.InitNew ();
return TRUE;
}
BOOL CEditPadDoc::
OnOpenDocument (LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument (lpszPathName))
{
if (!OnNewDocument ())
{
return FALSE;
}
SetPathName (lpszPathName);
CloseUnused ();
return TRUE;
}
CloseUnused ();
if (m_xTextBuffer.LoadFromFile (lpszPathName))
{
if (theApp.m_dwFlags & EP_NOTIFY_CHANGES)
{
AddFile (lpszPathName);
StartWatching ();
}
return TRUE;
}
return FALSE;
}
BOOL CEditPadDoc::
OnSaveDocument (LPCTSTR lpszPathName)
{
CString sOld = GetExt (GetPathName ()), sNew = GetExt (lpszPathName);
if (_tcsicmp (sOld, sNew))
{
m_xTextBuffer.RetypeViews (lpszPathName);
UpdateAllViews (NULL);
}
if (theApp.m_dwFlags & EP_NOTIFY_CHANGES)
{
StopWatching ();
RemoveFile (lpszPathName);
}
BOOL bSuccess = m_xTextBuffer.SaveToFile (lpszPathName);
if (theApp.m_dwFlags & EP_NOTIFY_CHANGES)
{
AddFile (lpszPathName);
StartWatching ();
}
if (!bSuccess)
{
CString sFormat, sMsg;
sFormat.LoadString (IDS_FILE_CANNOT_BE_SAVED);
sMsg.Format(sFormat, lpszPathName);
AfxMessageBox (sMsg);
}
return bSuccess; // Note - we didn't call inherited member!
}
void CEditPadDoc::
OnReadOnly ()
{
if (!m_xTextBuffer.GetReadOnly ())
{
m_xTextBuffer.SetReadOnly (TRUE);
AfxMessageBox (_T ("Document now read-only!"));
}
else
m_xTextBuffer.SetReadOnly (FALSE);
}
void CEditPadDoc::
OnUpdateReadOnly (CCmdUI * pCmdUI)
{
pCmdUI->SetCheck (m_xTextBuffer.GetReadOnly ());
}
void CEditPadDoc::OnCloseDocument()
{
CString sDoc = GetPathName ();
if (!sDoc.IsEmpty () && (theApp.m_dwFlags & EP_NOTIFY_CHANGES))
{
StopWatching ();
RemoveFile (sDoc);
}
CDocTemplate *pDocTemplate = GetDocTemplate ();
POSITION pos = pDocTemplate->GetFirstDocPosition ();
ASSERT (pos);
pDocTemplate->GetNextDoc (pos);
if (!pos)
{
CWnd *pWnd = AfxGetMainWnd ();
if(pWnd->IsKindOf (RUNTIME_CLASS (CMainFrame)))
((CMainFrame*) pWnd)->ChooseStatusBar (FALSE);
else
((CMainFrame2*) pWnd)->ChooseStatusBar (FALSE);
}
if (theApp.m_dwFlags & EP_REMEMBER_LASTPOS && !sDoc.IsEmpty ())
{
pos = GetFirstViewPosition ();
ASSERT (pos);
CEditPadView *pView = (CEditPadView*) GetNextView (pos);
CPoint ptCursor = pView->GetCursorPos ();
time_t stamp;
DWORD dwLastPos[3] = { time (&stamp), ptCursor.x, ptCursor.y };
CString sKey = REG_EDITPAD;
sKey += _T ("\\Remembered");
CReg reg;
if (reg.Create (HKEY_CURRENT_USER, sKey, KEY_WRITE))
{
VERIFY (reg.SaveBinary (sDoc, (LPBYTE) dwLastPos, sizeof (dwLastPos)));
}
}
CDocument::OnCloseDocument();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -