OSDN Git Service

38cb527421a6e38f181a351788dcf9eb5a92cc79
[winmerge-jp/winmerge-jp.git] / Src / editlib / ccrystaltextbuffer.h
1 ////////////////////////////////////////////////////////////////////////////
2 //  File:       ccrystaltextbuffer.h
3 //  Version:    1.0.0.0
4 //  Created:    29-Dec-1998
5 //
6 //  Author:     Stcherbatchenko Andrei
7 //  E-mail:     windfall@gmx.de
8 //
9 //  Interface of the CCrystalTextBuffer class, a part of Crystal Edit -
10 //  syntax coloring text editor.
11 //
12 //  You are free to use or modify this code to the following restrictions:
13 //  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
14 //  will be enough. If you can't (or don't want to), contact me personally.
15 //  - LEAVE THIS HEADER INTACT
16 ////////////////////////////////////////////////////////////////////////////
17
18 ////////////////////////////////////////////////////////////////////////////
19 //  19-Jul-99
20 //      Ferdinand Prantl:
21 //  +   FEATURE: see cpps ...
22 //
23 //  ... it's being edited very rapidly so sorry for non-commented
24 //        and maybe "ugly" code ...
25 ////////////////////////////////////////////////////////////////////////////
26
27 #if !defined(AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_)
28 #define AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_
29
30 #if _MSC_VER >= 1000
31 #pragma once
32 #endif // _MSC_VER >= 1000
33
34 #include "ccrystaltextview.h"
35
36 #ifndef __AFXTEMPL_H__
37 #pragma message("Include <afxtempl.h> in your stdafx.h to avoid this message")
38 #include <afxtempl.h>
39 #endif
40
41 #define UNDO_DESCRIP_BUF        32
42
43 enum LINEFLAGS
44 {
45   LF_BOOKMARK_FIRST = 0x00000001L,
46   LF_EXECUTION = 0x00010000L,
47   LF_BREAKPOINT = 0x00020000L,
48   LF_COMPILATION_ERROR = 0x00040000L,
49   LF_BOOKMARKS = 0x00080000L,
50   LF_INVALID_BREAKPOINT = 0x00100000L
51 };
52
53 #define LF_BOOKMARK(id)     (LF_BOOKMARK_FIRST << id)
54
55 enum CRLFSTYLE
56 {
57   CRLF_STYLE_AUTOMATIC = -1,
58   CRLF_STYLE_DOS = 0,
59   CRLF_STYLE_UNIX = 1,
60   CRLF_STYLE_MAC = 2
61 };
62
63 enum
64 {
65   CE_ACTION_UNKNOWN = 0,
66   CE_ACTION_PASTE = 1,
67   CE_ACTION_DELSEL = 2,
68   CE_ACTION_CUT = 3,
69   CE_ACTION_TYPING = 4,
70   CE_ACTION_BACKSPACE = 5,
71   CE_ACTION_INDENT = 6,
72   CE_ACTION_DRAGDROP = 7,
73   CE_ACTION_REPLACE = 8,
74   CE_ACTION_DELETE = 9,
75   CE_ACTION_AUTOINDENT = 10,
76   CE_ACTION_AUTOCOMPLETE = 11,
77   CE_ACTION_AUTOEXPAND = 12,
78   CE_ACTION_LOWERCASE = 13,
79   CE_ACTION_UPPERCASE = 14,
80   CE_ACTION_SWAPCASE = 15,
81   CE_ACTION_CAPITALIZE = 16,
82   CE_ACTION_SENTENCIZE = 17,
83   CE_ACTION_RECODE  = 18, 
84   CE_ACTION_SPELL = 19
85                          //  ...
86                          //  Expandable: user actions allowed
87 };
88
89
90 /////////////////////////////////////////////////////////////////////////////
91 // CUpdateContext class
92
93 class EDITPADC_CLASS CUpdateContext
94   {
95 public :
96     virtual void RecalcPoint (CPoint & ptPoint) = 0;
97   };
98
99
100 /////////////////////////////////////////////////////////////////////////////
101 // CCrystalTextBuffer command target
102
103 class EDITPADC_CLASS CCrystalTextBuffer : public CCmdTarget
104   {
105 public:
106     DECLARE_DYNCREATE (CCrystalTextBuffer)
107
108     int m_nSourceEncoding;
109     static int m_nDefaultEncoding;
110     DWORD m_dwCurrentRevisionNumber;
111     DWORD m_dwRevisionNumberOnSave;
112     BOOL IsTextBufferInitialized () const { return m_bInit; }
113
114 protected :
115     BOOL m_bInit;
116     BOOL m_bReadOnly;
117     BOOL m_bModified;
118     int m_nCRLFMode;
119     BOOL m_IgnoreEol;
120     BOOL m_bCreateBackupFile;
121     int m_nUndoBufSize;
122     BOOL m_bInsertTabs;
123     int  m_nTabSize;
124     int FindLineWithFlag (DWORD dwFlag);
125
126 protected :
127 #pragma pack(push, 1)
128     //  Nested class declarations
129     struct SLineInfo
130       {
131         TCHAR *m_pcLine;
132         int m_nLength, m_nMax;
133         int m_nEolChars; // # of eolchars
134         DWORD m_dwFlags;
135         DWORD m_dwRevisionNumber;
136
137         int FullLength() const { return m_nLength+m_nEolChars; }
138         int Length() const { return m_nLength; }
139
140         SLineInfo ()
141         {
142           memset (this, 0, sizeof (SLineInfo));
143         };
144       };
145
146     enum
147     {
148       UNDO_INSERT = 0x0001,
149       UNDO_BEGINGROUP = 0x0100
150     };
151
152     //  [JRT] Support For Descriptions On Undo/Redo Actions
153     struct SUndoRecord
154       {
155         DWORD m_dwFlags;
156         
157         CPoint m_ptStartPos, m_ptEndPos;  //  Block of text participating
158         int m_nAction;            //  For information only: action type
159         CDWordArray *m_paSavedRevisonNumbers;
160
161 private :
162         //  TCHAR   *m_pcText;
163         //  Since in most cases we have 1 character here,
164         //  we should invent a better way. Note: 2 * sizeof(WORD) <= sizeof(TCHAR*)
165         //
166         //  Here we will use the following trick: on Win32 platforms high-order word
167         //  of any pointer will be != 0. So we can store 1 character strings without
168         //  allocating memory.
169         //
170
171         union
172           {
173             TCHAR *m_pszText;     //  For cases when we have > 1 character strings
174             TCHAR m_szText[2];    //  For single-character strings
175           };
176
177 public :
178         SUndoRecord () // default constructor
179         {
180           memset (this, 0, sizeof (SUndoRecord));
181         }
182         SUndoRecord (const SUndoRecord & src) // copy constructor
183         {
184           memset (this, 0, sizeof (SUndoRecord));
185           (*this)=src;
186         }
187         SUndoRecord & operator=(const SUndoRecord & src) // copy assignment
188         {
189           m_dwFlags = src.m_dwFlags;
190           m_ptStartPos = src.m_ptStartPos;
191           m_ptEndPos = src.m_ptEndPos;
192           m_nAction = src.m_nAction;
193           SetText(src.GetText());
194           INT_PTR size = src.m_paSavedRevisonNumbers->GetSize();
195           m_paSavedRevisonNumbers = new CDWordArray();
196           m_paSavedRevisonNumbers->SetSize(size);
197           INT_PTR i;
198           for (i = 0; i < size; i++)
199             (*m_paSavedRevisonNumbers)[i] = (*src.m_paSavedRevisonNumbers)[i];
200           return *this;
201         }
202         ~SUndoRecord () // destructor
203         {
204           FreeText();
205           if (m_paSavedRevisonNumbers)
206                 delete m_paSavedRevisonNumbers;
207         }
208
209         void SetText (LPCTSTR pszText);
210         void FreeText ();
211
212         LPCTSTR GetText () const
213         {
214           // see the m_szText/m_pszText definition about the use of HIWORD
215           if (HIWORD ((DWORD) m_pszText) != 0)
216             return m_pszText;
217           return m_szText;
218         };
219       };
220
221 #pragma pack(pop)
222
223 class EDITPADC_CLASS CInsertContext : public CUpdateContext
224       {
225 public :
226         CPoint m_ptStart, m_ptEnd;
227         virtual void RecalcPoint (CPoint & ptPoint);
228       };
229
230 class EDITPADC_CLASS CDeleteContext : public CUpdateContext
231       {
232 public :
233         CPoint m_ptStart, m_ptEnd;
234         virtual void RecalcPoint (CPoint & ptPoint);
235       };
236
237     //  Lines of text
238     CArray < SLineInfo, SLineInfo & >m_aLines;
239
240     //  Undo
241     CArray < SUndoRecord, SUndoRecord & >m_aUndoBuf;
242     int m_nUndoPosition;
243     int m_nSyncPosition;
244     BOOL m_bUndoGroup, m_bUndoBeginGroup;
245
246     //BEGIN SW
247     /** Position where the last change was made. */
248     CPoint m_ptLastChange;
249     //END SW
250
251     //  Connected views
252     CList < CCrystalTextView *, CCrystalTextView * >m_lpViews;
253
254     //  Helper methods
255     void InsertLine (LPCTSTR pszLine, int nLength = -1, int nPosition = -1, int nCount = 1);
256     void AppendLine (int nLineIndex, LPCTSTR pszChars, int nLength = -1);
257     void MoveLine(int line1, int line2, int newline1);
258     void SetEmptyLine(int nPosition, int nCount = 1);
259
260     //  Implementation
261     BOOL InternalInsertText (CCrystalTextView * pSource, int nLine, int nPos, LPCTSTR pszText, int &nEndLine, int &nEndChar);
262     BOOL InternalDeleteText (CCrystalTextView * pSource, int nStartLine, int nStartPos, int nEndLine, int nEndPos);
263     CString StripTail (int i, int bytes);
264
265     //  [JRT] Support For Descriptions On Undo/Redo Actions
266     virtual void AddUndoRecord (BOOL bInsert, const CPoint & ptStartPos, const CPoint & ptEndPos,
267                                 LPCTSTR pszText, int nActionType = CE_ACTION_UNKNOWN, CDWordArray *paSavedRevisonNumbers = NULL);
268
269     //  Overridable: provide action description
270     virtual BOOL GetActionDescription (int nAction, CString & desc);
271
272     // Operations
273 public :
274     //  Construction/destruction code
275     CCrystalTextBuffer ();
276     ~CCrystalTextBuffer ();
277
278     //  Basic functions
279     BOOL InitNew (int nCrlfStyle = CRLF_STYLE_DOS);
280
281 // WinMerge has own routines for loading and saving
282 #ifdef CRYSTALEDIT_ENABLELOADER
283     BOOL LoadFromFile (LPCTSTR pszFileName, int nCrlfStyle = CRLF_STYLE_AUTOMATIC);
284 #endif
285 #ifdef CRYSTALEDIT_ENABLESAVER
286     BOOL SaveToFile(LPCTSTR pszFileName, int nCrlfStyle = CRLF_STYLE_AUTOMATIC, 
287     BOOL bClearModifiedFlag = TRUE);
288 #endif
289
290     void FreeAll ();
291
292     void ResetInit() { FreeAll(); InitNew(); } // reset new or used buffer
293
294     //  'Dirty' flag
295     virtual void SetModified (BOOL bModified = TRUE);
296     BOOL IsModified () const;
297
298     //  Connect/disconnect views
299     void AddView (CCrystalTextView * pView);
300     void RemoveView (CCrystalTextView * pView);
301
302     //  Text access functions
303     int GetLineCount () const;
304     int GetLineLength (int nLine) const;
305     int GetFullLineLength (int nLine) const; // including EOLs
306     LPCTSTR GetLineEol (int nLine) const;
307     BOOL ChangeLineEol (int nLine, LPCTSTR lpEOL);
308     LPTSTR GetLineChars (int nLine) const;
309     DWORD GetLineFlags (int nLine) const;
310     DWORD GetLineRevisionNumber (int nLine) const;
311     int GetLineWithFlag (DWORD dwFlag);
312     void SetLineFlag (int nLine, DWORD dwFlag, BOOL bSet, BOOL bRemoveFromPreviousLine = TRUE, BOOL bUpdate=TRUE);
313     void GetText (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString & text, LPCTSTR pszCRLF = NULL);
314     virtual void GetTextWithoutEmptys (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString &text, int nCrlfStyle =CRLF_STYLE_AUTOMATIC );
315
316     //  Attributes
317     int GetCRLFMode ();
318     void SetCRLFMode (int nCRLFMode);
319     /// Adjust all the lines in the buffer to the buffer default EOL Mode
320     virtual BOOL applyEOLMode();
321     LPCTSTR CCrystalTextBuffer::GetDefaultEol() const;
322     LPCTSTR CCrystalTextBuffer::GetStringEol(int nCRLFMode) const;
323     BOOL GetReadOnly () const;
324     void SetReadOnly (BOOL bReadOnly = TRUE);
325
326     void SetIgnoreEol(BOOL IgnoreEol) { m_IgnoreEol = IgnoreEol; }
327
328     //  Text modification functions
329     virtual BOOL InsertText (CCrystalTextView * pSource, int nLine, int nPos, LPCTSTR pszText, int &nEndLine, int &nEndChar, int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
330     virtual BOOL DeleteText (CCrystalTextView * pSource, int nStartLine, int nStartPos, int nEndLine, int nEndPos, int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
331
332     //  Undo/Redo
333     BOOL CanUndo ();
334     BOOL CanRedo ();
335     virtual BOOL Undo (CCrystalTextView * pSource, CPoint & ptCursorPos);
336     virtual BOOL Redo (CCrystalTextView * pSource, CPoint & ptCursorPos);
337
338     //  Undo grouping
339     virtual void BeginUndoGroup (BOOL bMergeWithPrevious = FALSE);
340     virtual void FlushUndoGroup (CCrystalTextView * pSource);
341
342     //BEGIN SW
343     /**
344     Returns the position where the last changes where made.
345     */
346     CPoint GetLastChangePos() const;
347     //END SW
348     void RestoreLastChangePos(CPoint pt);
349     void DeleteLine(int line, int nCount = 1);
350
351
352     //  Browse undo sequence
353     POSITION GetUndoActionCode (int & nAction, POSITION pos = NULL);
354     POSITION GetRedoActionCode (int & nAction, POSITION pos = NULL);
355     POSITION GetUndoDescription (CString & desc, POSITION pos = NULL);
356     POSITION GetRedoDescription (CString & desc, POSITION pos = NULL);
357
358     //  Notify all connected views about changes in name of file
359     CCrystalTextView::TextDefinition *RetypeViews (LPCTSTR lpszFileName);
360     //  Notify all connected views about changes in text
361     void UpdateViews (CCrystalTextView * pSource, CUpdateContext * pContext,
362                       DWORD dwUpdateFlags, int nLineIndex = -1);
363
364     // Tabs/space inserting
365     BOOL GetInsertTabs() const;
366     void SetInsertTabs(BOOL bInsertTabs);
367
368     // Tabbing
369     int  GetTabSize();
370     void SetTabSize(int nTabSize);
371
372     // More bookmarks
373     int FindNextBookmarkLine (int nCurrentLine = 0);
374     int FindPrevBookmarkLine (int nCurrentLine = 0);
375
376     BOOL IsMBSLead (int nLine, int nCol);
377     BOOL IsMBSTrail (int nLine, int nCol);
378
379     // Overrides
380     // ClassWizard generated virtual function overrides
381     //{{AFX_VIRTUAL(CCrystalTextBuffer)
382     //}}AFX_VIRTUAL
383
384
385     // Generated message map functions
386     //{{AFX_MSG(CCrystalTextBuffer)
387     //}}AFX_MSG
388
389     DECLARE_MESSAGE_MAP ()
390   };
391
392 #if ! (defined(CE_FROM_DLL) || defined(CE_DLL_BUILD))
393 #include "ccrystaltextbuffer.inl"
394 #endif
395
396 /////////////////////////////////////////////////////////////////////////////
397
398 //{{AFX_INSERT_LOCATION}}
399 // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
400
401 #endif // !defined(AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_)