/** * @file XTPSyntaxEditDoc.cpp * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ #include "stdafx.h" #include "SyntaxEdit/Resource.h" // common includes #include "Common/XTPTypeId.h" #include "Common/XTPCasting.h" #include "Common/XTPFramework.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/XTPGdiObjects.h" #include "Common/XTPSmartPtrInternalT.h" #include "Common/XTPColorManager.h" #include "Common/XTPDrawHelpers.h" #include "Common/XTPResourceManager.h" // syntax editor includes #include "SyntaxEdit/XTPSyntaxEditDefines.h" #include "SyntaxEdit/XTPSyntaxEditStruct.h" #include "SyntaxEdit/XTPSyntaxEditBufferManager.h" #include "SyntaxEdit/XTPSyntaxEditCtrl.h" #include "SyntaxEdit/XTPSyntaxEditDoc.h" #include "SyntaxEdit/XTPSyntaxEditView.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CXTPSyntaxEditDoc IMPLEMENT_DYNCREATE(CXTPSyntaxEditDoc, CDocument) BEGIN_MESSAGE_MAP(CXTPSyntaxEditDoc, CDocument) //{{AFX_MSG_MAP(CXTPSyntaxEditDoc) //}}AFX_MSG_MAP END_MESSAGE_MAP() CXTPSyntaxEditDoc::CXTPSyntaxEditDoc() : m_bAutoReload(FALSE) , m_bOpened(FALSE) , m_bNewFile(TRUE) , m_bDocCreated(FALSE) , m_bReloading(FALSE) , m_iTopRow(1) , m_iHScrollPos(0) { ::ZeroMemory(&m_curFileData, sizeof(WIN32_FIND_DATA)); CWinApp* pWinApp = AfxGetApp(); if (pWinApp != NULL) { m_bAutoReload = (0 != (pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_AUTORELOAD, m_bAutoReload))); } m_ptrDataMan = NULL; } CXTPSyntaxEditDoc::~CXTPSyntaxEditDoc() { if (m_ptrDataMan) { CMDTARGET_RELEASE(m_ptrDataMan); } } BOOL CXTPSyntaxEditDoc::IsReadonly() { return ((m_curFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY); } BOOL CXTPSyntaxEditDoc::SaveFileData(LPCTSTR lpszPathName) { HANDLE hFind = ::FindFirstFile(lpszPathName, &m_curFileData); if (hFind == INVALID_HANDLE_VALUE) return FALSE; ::FindClose(hFind); // if (IsReadonly()) // { // TRACE(_T("Cannot write to a read only file")); // return FALSE; // } return TRUE; } BOOL CXTPSyntaxEditDoc::OnNewDocument() { CWaitCursor wait; if (!CDocument::OnNewDocument()) { return FALSE; } wait.Restore(); m_bOpened = FALSE; m_bNewFile = TRUE; m_bDocCreated = TRUE; ::ZeroMemory(&m_curFileData, sizeof(WIN32_FIND_DATA)); if (m_ptrDataMan) { m_ptrDataMan->InsertText(_T(""), 1, 1, FALSE); } UpdateAllViews(NULL, xtpEditHintRefreshView); return TRUE; } BOOL CXTPSyntaxEditDoc::OnOpenDocument(LPCTSTR lpszPathName) { CWaitCursor wait; m_bNewFile = FALSE; BOOL bRes = CDocument::OnOpenDocument(lpszPathName); wait.Restore(); if (bRes) { UpdateAllViews(NULL, xtpEditHintRefreshView); bRes = SaveFileData(lpszPathName); } if (!bRes && m_bAutoDelete) { delete this; } return bRes; } BOOL CXTPSyntaxEditDoc::OnSaveDocument(LPCTSTR lpszPathName) { CWaitCursor wait; if (!CDocument::OnSaveDocument(lpszPathName)) return FALSE; if (!SaveFileData(lpszPathName)) return FALSE; UpdateAllViews(NULL, xtpEditHintRefreshView); return TRUE; } BOOL CXTPSyntaxEditDoc::ReOpen() { return OnOpenDocument(GetPathName()); } void CXTPSyntaxEditDoc::OnCloseDocument() { CWaitCursor wait; if (m_ptrDataMan) { m_ptrDataMan->Close(); } CDocument::OnCloseDocument(); } ///////////////////////////////////////////////////////////////////////////// // CXTPSyntaxEditDoc serialization void CXTPSyntaxEditDoc::Serialize(CArchive& ar) { if (m_ptrDataMan) { CWaitCursor wait; m_ptrDataMan->Serialize(ar); } } ///////////////////////////////////////////////////////////////////////////// // CXTPSyntaxEditDoc diagnostics #ifdef _DEBUG void CXTPSyntaxEditDoc::AssertValid() const { CDocument::AssertValid(); } void CXTPSyntaxEditDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CXTPSyntaxEditDoc commands void CXTPSyntaxEditDoc::OnChangedViewList() { if (!m_bDocCreated) return; int iTopRow = 1; BOOL bFirst = TRUE; POSITION pos = GetFirstViewPosition(); while (pos) { CXTPSyntaxEditView* pView = (CXTPSyntaxEditView*)GetNextView(pos); if (!pView->IsKindOf(RUNTIME_CLASS(CXTPSyntaxEditView))) continue; if (bFirst) { iTopRow = pView->GetTopRow(); bFirst = FALSE; } else { pView->SetTopRow(iTopRow); } } CDocument::OnChangedViewList(); } void CXTPSyntaxEditDoc::SetDataManager(CXTPSyntaxEditBufferManager* pDataMan) { CMDTARGET_RELEASE(m_ptrDataMan); m_ptrDataMan = pDataMan; CMDTARGET_ADDREF(m_ptrDataMan); } void CXTPSyntaxEditDoc::SetConfigFile(LPCTSTR szPath) { if (m_ptrDataMan) { m_ptrDataMan->SetConfigFile(szPath); } } BOOL CXTPSyntaxEditDoc::CheckFileModified(LPCTSTR lpszPathName) { if (!GetAutoReload()) return FALSE; WIN32_FIND_DATA newFileData; HANDLE hFound = ::FindFirstFile(lpszPathName, &newFileData); if (hFound != INVALID_HANDLE_VALUE) { ::FindClose(hFound); // If file attributes have changed or file write time has changed, reload file. if (!m_bReloading && ((m_curFileData.ftLastWriteTime.dwLowDateTime != newFileData.ftLastWriteTime.dwLowDateTime) || (m_curFileData.ftLastWriteTime.dwHighDateTime != newFileData.ftLastWriteTime.dwHighDateTime) || (m_curFileData.dwFileAttributes != newFileData.dwFileAttributes))) { m_bReloading = TRUE; CString csWarning; CString csFormat = XTPResourceManager()->LoadString(XTP_IDS_EDIT_MSG_WARN_RELOAD); csWarning.Format(csFormat, lpszPathName); if (AfxMessageBox(csWarning, MB_YESNO | MB_ICONQUESTION) == IDNO) { m_bReloading = FALSE; return FALSE; } if (ReOpen()) { ::memcpy(&m_curFileData, &newFileData, sizeof(WIN32_FIND_DATA)); m_bReloading = FALSE; return TRUE; } } } return FALSE; } BOOL CXTPSyntaxEditDoc::SetAutoReload(BOOL bAutoReload, BOOL bUpdateReg /*=FALSE*/) { m_bAutoReload = bAutoReload; if (bUpdateReg) { CWinApp* pWinApp = AfxGetApp(); if (pWinApp != NULL) { if (pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_AUTORELOAD, (int)bAutoReload)) return TRUE; } return FALSE; } return TRUE; } CXTPSyntaxEditView* CXTPSyntaxEditDoc::GetFirstView() { POSITION pos = GetFirstViewPosition(); return DYNAMIC_DOWNCAST(CXTPSyntaxEditView, GetNextView(pos)); }