@@ -14,14 +14,15 @@ Copyright (C) 2000 Nick Gammon.
14
14
#include " sendvw.h"
15
15
#include " mainfrm.h"
16
16
#include " doc.h"
17
+ #include < ../src/afximpl.h>
17
18
18
19
#include " dialogs\GoToLineDlg.h"
19
20
#include " dialogs\LuaGsubDlg.h"
20
21
21
22
#pragma warning( disable : 4100) // unreferenced formal parameter
22
23
23
24
#ifdef _DEBUG
24
- // #define new DEBUG_NEW
25
+ #define new DEBUG_NEW
25
26
#undef THIS_FILE
26
27
static char THIS_FILE[] = __FILE__;
27
28
#endif
@@ -225,62 +226,6 @@ void CTextView::OnEscape()
225
226
}
226
227
227
228
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
-
284
229
void CTextView::OnViewMaximize ()
285
230
{
286
231
WINDOWPLACEMENT wp;
@@ -1760,3 +1705,136 @@ void CTextView::OnUpdateCompleteFunction(CCmdUI* pCmdUI)
1760
1705
{
1761
1706
pCmdUI->Enable ();
1762
1707
}
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
+
0 commit comments