Skip to content

Commit c038158

Browse files
committed
Fixed bug with garbled text in the notepad window
1 parent 7b35548 commit c038158

File tree

2 files changed

+140
-57
lines changed

2 files changed

+140
-57
lines changed

TextView.cpp

Lines changed: 135 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ Copyright (C) 2000 Nick Gammon.
1414
#include "sendvw.h"
1515
#include "mainfrm.h"
1616
#include "doc.h"
17+
#include <../src/afximpl.h>
1718

1819
#include "dialogs\GoToLineDlg.h"
1920
#include "dialogs\LuaGsubDlg.h"
2021

2122
#pragma warning( disable : 4100) // unreferenced formal parameter
2223

2324
#ifdef _DEBUG
24-
//#define new DEBUG_NEW
25+
#define new DEBUG_NEW
2526
#undef THIS_FILE
2627
static char THIS_FILE[] = __FILE__;
2728
#endif
@@ -225,62 +226,6 @@ void CTextView::OnEscape()
225226
}
226227

227228

228-
void CTextView::SerializeRaw(CArchive& ar)
229-
// Read/Write object as stand-alone file.
230-
{
231-
ASSERT_VALID(this);
232-
233-
if (ar.IsStoring())
234-
{
235-
CString strContents;
236-
GetEditCtrl().GetWindowText (strContents);
237-
ar.Write(strContents, strContents.GetLength ());
238-
}
239-
else
240-
{
241-
CFile* pFile = ar.GetFile();
242-
ASSERT(pFile->GetPosition() == 0);
243-
DWORD nLen = pFile->GetLength();
244-
// ReadFromArchive takes the number of characters as argument
245-
246-
// REPLACING: ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
247-
248-
// with this (we got garbage at times):
249-
250-
LPVOID hText = LocalAlloc(LMEM_MOVEABLE, (nLen+1)*sizeof(TCHAR));
251-
if (hText == NULL)
252-
AfxThrowMemoryException();
253-
254-
LPTSTR lpszText = (LPTSTR)LocalLock(hText);
255-
ASSERT(lpszText != NULL);
256-
if (ar.Read(lpszText, nLen*sizeof(TCHAR)) != nLen*sizeof(TCHAR))
257-
{
258-
LocalUnlock(hText);
259-
LocalFree(hText);
260-
AfxThrowArchiveException(CArchiveException::endOfFile);
261-
}
262-
// Replace the editing edit buffer with the newly loaded data
263-
lpszText[nLen] = '\0';
264-
265-
// set the text with SetWindowText, then free
266-
BOOL bResult = ::SetWindowText(m_hWnd, lpszText);
267-
LocalUnlock(hText);
268-
LocalFree(hText);
269-
270-
// make sure that SetWindowText was successful
271-
if (!bResult || ::GetWindowTextLength(m_hWnd) < (int)nLen)
272-
AfxThrowMemoryException();
273-
274-
// remove old shadow buffer
275-
delete[] m_pShadowBuffer;
276-
m_pShadowBuffer = NULL;
277-
m_nShadowSize = 0;
278-
279-
}
280-
ASSERT_VALID(this);
281-
} /* end of CTextView::SerializeRaw */
282-
283-
284229
void CTextView::OnViewMaximize()
285230
{
286231
WINDOWPLACEMENT wp;
@@ -1760,3 +1705,136 @@ void CTextView::OnUpdateCompleteFunction(CCmdUI* pCmdUI)
17601705
{
17611706
pCmdUI->Enable ();
17621707
}
1708+
1709+
1710+
#define VERSION_6 MAKELONG(0, 6)
1711+
1712+
// from Worstje
1713+
1714+
void CTextView::Serialize(CArchive& ar)
1715+
// Read and write CTextView object to archive, with length prefix.
1716+
{
1717+
ASSERT_VALID(this);
1718+
ASSERT(m_hWnd != NULL);
1719+
if (ar.IsStoring())
1720+
{
1721+
UINT nLen = GetBufferLength();
1722+
ar << (DWORD)nLen;
1723+
WriteToArchive(ar);
1724+
}
1725+
else
1726+
{
1727+
DWORD dwLen;
1728+
ar >> dwLen;
1729+
if (dwLen > GetEditCtrl().GetLimitText())
1730+
// Larger than edit control limit. Call SetLimitText() to set your own max size.
1731+
// Refer to documentation for EM_LIMITTEXT for max sizes for your target OS.
1732+
AfxThrowArchiveException(CArchiveException::badIndex);
1733+
UINT nLen = (UINT)dwLen;
1734+
ReadFromArchive(ar, nLen);
1735+
}
1736+
ASSERT_VALID(this);
1737+
}
1738+
1739+
void CTextView::ReadFromArchive(CArchive& ar, UINT nLen)
1740+
// Read certain amount of text from the file, assume at least nLen
1741+
// characters (not bytes) are in the file.
1742+
{
1743+
ASSERT_VALID(this);
1744+
1745+
LPVOID hText = LocalAlloc(LMEM_MOVEABLE, (nLen+1)*sizeof(TCHAR));
1746+
1747+
//LPVOID hText = LocalAlloc(LMEM_MOVEABLE, static_cast<UINT>(::ATL::AtlMultiplyThrow(static_cast<UINT>(nLen+1),static_cast<UINT>(sizeof(TCHAR)))));
1748+
if (hText == NULL)
1749+
AfxThrowMemoryException();
1750+
1751+
LPTSTR lpszText = (LPTSTR)LocalLock(hText);
1752+
ASSERT(lpszText != NULL);
1753+
if (ar.Read(lpszText, nLen*sizeof(TCHAR)) != nLen*sizeof(TCHAR))
1754+
{
1755+
LocalUnlock(hText);
1756+
LocalFree(hText);
1757+
AfxThrowArchiveException(CArchiveException::endOfFile);
1758+
}
1759+
// Replace the editing edit buffer with the newly loaded data
1760+
lpszText[nLen] = '\0';
1761+
1762+
1763+
#ifndef _UNICODE
1764+
if (_AfxGetComCtlVersion() >= VERSION_6)
1765+
{
1766+
// set the text with SetWindowText, then free
1767+
BOOL bResult = ::SetWindowText(m_hWnd, lpszText);
1768+
LocalUnlock(hText);
1769+
LocalFree(hText);
1770+
1771+
// make sure that SetWindowText was successful
1772+
if (!bResult || ::GetWindowTextLength(m_hWnd) < (int)nLen)
1773+
AfxThrowMemoryException();
1774+
1775+
// remove old shadow buffer
1776+
delete[] m_pShadowBuffer;
1777+
m_pShadowBuffer = NULL;
1778+
m_nShadowSize = 0;
1779+
1780+
ASSERT_VALID(this);
1781+
return;
1782+
}
1783+
#endif
1784+
1785+
LocalUnlock(hText);
1786+
HLOCAL hOldText = GetEditCtrl().GetHandle();
1787+
ASSERT(hOldText != NULL);
1788+
LocalFree(hOldText);
1789+
GetEditCtrl().SetHandle((HLOCAL)hText);
1790+
Invalidate();
1791+
ASSERT_VALID(this);
1792+
}
1793+
1794+
void CTextView::WriteToArchive(CArchive& ar)
1795+
// Write just the text to an archive, no length prefix.
1796+
{
1797+
ASSERT_VALID(this);
1798+
LPCTSTR lpszText = LockBuffer();
1799+
ASSERT(lpszText != NULL);
1800+
UINT nLen = GetBufferLength();
1801+
TRY
1802+
{
1803+
ar.Write(lpszText, nLen*sizeof(TCHAR));
1804+
}
1805+
CATCH_ALL(e)
1806+
{
1807+
UnlockBuffer();
1808+
THROW_LAST();
1809+
}
1810+
END_CATCH_ALL
1811+
UnlockBuffer();
1812+
ASSERT_VALID(this);
1813+
}
1814+
1815+
void CTextView::SerializeRaw(CArchive& ar)
1816+
// Read/Write object as stand-alone file.
1817+
{
1818+
ASSERT_VALID(this);
1819+
if (ar.IsStoring())
1820+
{
1821+
WriteToArchive(ar);
1822+
}
1823+
else
1824+
{
1825+
CFile* pFile = ar.GetFile();
1826+
ASSERT(pFile->GetPosition() == 0);
1827+
ULONGLONG nFileSize = pFile->GetLength();
1828+
if (nFileSize/sizeof(TCHAR) > GetEditCtrl().GetLimitText())
1829+
{
1830+
// Larger than edit control limit. Call SetLimitText() to set your own max size.
1831+
// Refer to documentation for EM_LIMITTEXT for max sizes for your target OS.
1832+
AfxMessageBox(AFX_IDP_FILE_TOO_LARGE);
1833+
AfxThrowUserException();
1834+
}
1835+
// ReadFromArchive takes the number of characters as argument
1836+
ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
1837+
}
1838+
ASSERT_VALID(this);
1839+
}
1840+

TextView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class CTextView : public CEditView
5252
CString & result,
5353
int & count);
5454

55+
56+
virtual void Serialize(CArchive& ar);
57+
virtual void ReadFromArchive(CArchive& ar, UINT nLen);
58+
virtual void WriteToArchive(CArchive& ar);
59+
5560
// ClassWizard generated virtual function overrides
5661
//{{AFX_VIRTUAL(CTextView)
5762
public:

0 commit comments

Comments
 (0)