OSDN Git Service

Merge from rev.7448
[winmerge-jp/winmerge-jp.git] / Src / GhostTextBuffer.h
1 /**
2  * @file  GhostTextBuffer.h
3  *
4  * @brief Declaration of CGhostTextBuffer (subclasses CCrystalTextBuffer to handle ghost lines)
5  */
6 // ID line follows -- this is updated by SVN
7 // $Id: GhostTextBuffer.h 5762 2008-08-08 05:32:15Z kimmov $
8
9 #ifndef __GHOSTTEXTBUFFER_H__
10 #define __GHOSTTEXTBUFFER_H__
11
12 #include <vector>
13 #include "ccrystaltextbuffer.h"
14 #include "GhostUndoRecord.h"
15
16
17 /////////////////////////////////////////////////////////////////////////////
18
19 /**
20  * We use the current ccrystalEditor flags 
21  *
22  * This flag must be cleared and set in GhostTextBuffer.cpp 
23  * and MergeDoc.cpp (Rescan) only.
24  *
25  * GetLineColors (in MergeEditView) reads it to choose the line color.
26  */
27 enum GHOST_LINEFLAGS
28 {
29         LF_GHOST = 0x00400000L, /**< Ghost line. */
30 };
31
32 /////////////////////////////////////////////////////////////////////////////
33 // CCrystalTextBuffer command target
34
35 /**
36  * @brief A class handling ghost lines.
37  * Features offered with this class : 
38  * <ul>
39  *  <li> apparent/real line conversion 
40  *  <li> insertText/deleteText working with ghost lines 
41  *  <li> AddUndoRecord/Undo/Redo working with ghost lines 
42  *  <li> insertGhostLine function 
43  * </ul>
44  */
45 class EDITPADC_CLASS CGhostTextBuffer : public CCrystalTextBuffer
46 {
47 public:
48         DECLARE_DYNCREATE (CGhostTextBuffer)
49
50 protected :
51 #pragma pack(push, 1)
52         //  Nested class declarations
53         enum
54         {
55                 UNDO_INSERT = 0x0001,
56                 UNDO_BEGINGROUP = 0x0100
57         };
58
59
60
61 #pragma pack(pop)
62
63 protected:
64         /** 
65         We need another array with our richer structure.
66
67         We share the positions with the CCrystalTextBuffer object. 
68         We share m_bUndoGroup, its utility is to check we opened the UndoBeginGroup.
69         We share m_nUndoBufSize which is the max buffer size.
70         */
71         std::vector<GhostUndoRecord> m_aUndoBuf;
72         /** 
73         This one must be duplicated because the flag UNDO_BEGINGROUP needs to be set in both 
74         CGhostTextBuffer::m_aUndoBuf and CCrystalTextBuffer::m_aUndoBuf CArrays 
75         */
76         BOOL m_bUndoBeginGroup;
77
78         // [JRT] Support For Descriptions On Undo/Redo Actions
79         virtual void AddUndoRecord (BOOL bInsert, const CPoint & ptStartPos, const CPoint & ptEndPos,
80                               LPCTSTR pszText, int cchText, int nRealLinesChanged, int nActionType = CE_ACTION_UNKNOWN, CDWordArray *paSavedRevisonNumbers = NULL);
81
82 private:
83         /**
84          * @brief A struct mapping real lines and apparent (screen) lines.
85          * This struct maps lines between real lines and apparent (screen) lines.
86          * The mapping records for each text block an apparent line and matching
87          * real line.
88          */
89         struct RealityBlock
90         {
91                 int nStartReal; /**< Start line of real block. */
92                 int nStartApparent; /**< Start line of apparent block. */
93                 int nCount; /**< Lines in the block. */
94         };
95         std::vector<RealityBlock> m_RealityBlocks; /**< Mapping of real and apparent lines. */
96
97         // Operations
98 private:
99         BOOL InternalInsertGhostLine (CCrystalTextView * pSource, int nLine);
100         BOOL InternalDeleteGhostLine (CCrystalTextView * pSource, int nLine, int nCount);
101 public :
102         // Construction/destruction code
103         CGhostTextBuffer ();
104         virtual BOOL InitNew (CRLFSTYLE nCrlfStyle = CRLF_STYLE_DOS);
105
106         /** 
107         This should work in base code as ghost lines are real empty lines
108         but maybe it doesn't (if there is an assert to check there is an EOL,
109         or if it adds the default EOL)
110         */
111         virtual void GetTextWithoutEmptys (int nStartLine, int nStartChar,
112                         int nEndLine, int nEndChar, CString &text,
113                         CRLFSTYLE nCrlfStyle =CRLF_STYLE_AUTOMATIC,
114                         BOOL bExcludeInvisibleLines = TRUE);
115
116
117         // Text modification functions
118         virtual BOOL InsertText (CCrystalTextView * pSource, int nLine, int nPos,
119                 LPCTSTR pszText, int cchText, int &nEndLine, int &nEndChar,
120                 int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
121         virtual BOOL DeleteText (CCrystalTextView * pSource, int nStartLine,
122                 int nStartPos, int nEndLine, int nEndPos,
123                 int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE, BOOL bExcludeInvisibleLines = TRUE);
124         virtual BOOL DeleteText2 (CCrystalTextView * pSource, int nStartLine,
125                 int nStartPos, int nEndLine, int nEndPos,
126                 int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
127         BOOL InsertGhostLine (CCrystalTextView * pSource, int nLine);
128
129         // Undo/Redo
130         virtual BOOL Undo (CCrystalTextView * pSource, CPoint & ptCursorPos);
131         virtual BOOL Redo (CCrystalTextView * pSource, CPoint & ptCursorPos);
132
133         // Undo grouping
134         virtual void BeginUndoGroup (BOOL bMergeWithPrevious = FALSE);
135         virtual void FlushUndoGroup (CCrystalTextView * pSource);
136
137 public:
138         //@{
139         /**
140          * @name Real/apparent line number conversion functions.
141          * These functions convert line numbers between file line numbers
142          * (real line numbers) and screen line numbers (apparent line numbers).
143          *
144          * This mapping is needed to handle ghost lines (ones with no text or
145          * EOL chars) which WinMerge uses for left-only or right-only lines.
146         */
147         int ApparentLastRealLine() const;
148         int ComputeRealLine(int nApparentLine) const;
149         int ComputeApparentLine(int nRealLine) const;
150         /** richer position information   yApparent = apparent(yReal) - yGhost */
151         int ComputeRealLineAndGhostAdjustment(int nApparentLine, int& decToReal) const;
152         /** richer position information   yApparent = apparent(yReal) - yGhost */
153         int ComputeApparentLine(int nRealLine, int decToReal) const;
154         //@}
155
156         /** for loading file */
157         void FinishLoading();
158         /** for saving file */ 
159         void RemoveAllGhostLines();
160
161
162 private:
163         void RecomputeRealityMapping();
164         /** 
165         Code to set EOL, if the status ghost/real of the line changes 
166
167         We should call a CCrystalTextBuffer function to add the correct EOL
168         (if CCrystalTextBuffer keeps the default EOL for the file)
169         */
170         void RecomputeEOL(CCrystalTextView * pSource, int nStartLine, int nEndLine);
171         /** For debugging purpose */
172         void checkFlagsFromReality(BOOL bFlag) const;
173
174 protected:
175         virtual void OnNotifyLineHasBeenEdited(int nLine);
176
177
178 protected:
179         // Overrides
180         // ClassWizard generated virtual function overrides
181         //{{AFX_VIRTUAL(CCrystalTextBuffer)
182         //}}AFX_VIRTUAL
183
184         // Generated message map functions
185         //{{AFX_MSG(CCrystalTextBuffer)
186         //}}AFX_MSG
187
188         DECLARE_MESSAGE_MAP ()
189 };
190
191 /////////////////////////////////////////////////////////////////////////////
192
193 //{{AFX_INSERT_LOCATION}}
194 // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
195
196
197 #endif //__GHOSTTEXTBUFFER_H__