OSDN Git Service

refactor
[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 #pragma once
7
8 #include <vector>
9 #include "ccrystaltextbuffer.h"
10
11
12 /////////////////////////////////////////////////////////////////////////////
13
14 /**
15  * We use the current ccrystalEditor flags 
16  *
17  * This flag must be cleared and set in GhostTextBuffer.cpp 
18  * and MergeDoc.cpp (Rescan) only.
19  *
20  * GetLineColors (in MergeEditView) reads it to choose the line color.
21  */
22 enum GHOST_LINEFLAGS : lineflags_t
23 {
24         LF_GHOST = 0x00400000UL, /**< Ghost line. */
25 };
26
27 /////////////////////////////////////////////////////////////////////////////
28 // CCrystalTextBuffer command target
29
30 /**
31  * @brief A class handling ghost lines.
32  * Features offered with this class : 
33  * <ul>
34  *  <li> apparent/real line conversion 
35  *  <li> insertText/deleteText working with ghost lines 
36  *  <li> AddUndoRecord/Undo/Redo working with ghost lines 
37  *  <li> insertGhostLine function 
38  * </ul>
39  */
40 class EDITPADC_CLASS CGhostTextBuffer : public CCrystalTextBuffer
41 {
42 private:
43         /**
44          * @brief A struct mapping real lines and apparent (screen) lines.
45          * This struct maps lines between real lines and apparent (screen) lines.
46          * The mapping records for each text block an apparent line and matching
47          * real line.
48          */
49         struct RealityBlock
50         {
51                 int nStartReal; /**< Start line of real block. */
52                 int nStartApparent; /**< Start line of apparent block. */
53                 int nCount; /**< Lines in the block. */
54         };
55         std::vector<RealityBlock> m_RealityBlocks; /**< Mapping of real and apparent lines. */
56
57         // Operations
58 private:
59 #if 0
60         bool InternalInsertGhostLine (CCrystalTextView * pSource, int nLine);
61 #endif
62         bool InternalDeleteGhostLine (CCrystalTextView * pSource, int nLine, int nCount);
63 public :
64         // Construction/destruction code
65         CGhostTextBuffer ();
66
67         /** 
68         This should work in base code as ghost lines are real empty lines
69         but maybe it doesn't (if there is an assert to check there is an EOL,
70         or if it adds the default EOL)
71         */
72         virtual void GetTextWithoutEmptys (int nStartLine, int nStartChar,
73                         int nEndLine, int nEndChar, String &text,
74                         CRLFSTYLE nCrlfStyle =CRLFSTYLE::AUTOMATIC,
75                         bool bExcludeInvisibleLines = true) const override;
76
77         virtual bool IsIndentableLine(int nLine) const override { return (GetLineFlags(nLine) & LF_GHOST) == 0; }
78
79         // Text modification functions
80         virtual bool InsertText (CCrystalTextView * pSource, int nLine, int nPos,
81                 const tchar_t* pszText, size_t cchText, int &nEndLine, int &nEndChar,
82                 int nAction = CE_ACTION_UNKNOWN, bool bHistory =true) override;
83         virtual bool DeleteText2 (CCrystalTextView * pSource, int nStartLine,
84                 int nStartPos, int nEndLine, int nEndPos,
85                 int nAction = CE_ACTION_UNKNOWN, bool bHistory =true) override;
86 #if 0
87         bool InsertGhostLine (CCrystalTextView * pSource, int nLine);
88 #endif
89
90         virtual void AddUndoRecord (bool bInsert, const CEPoint & ptStartPos, const CEPoint & ptEndPos,
91                                     const tchar_t* pszText, size_t cchText, int nActionType = CE_ACTION_UNKNOWN, std::vector<uint32_t> *paSavedRevisionNumbers = nullptr) override;
92         virtual UndoRecord GetUndoRecord(int nUndoPos) const override;
93         virtual bool UndoInsert(CCrystalTextView * pSource, CEPoint & ptCursorPos,
94                                                         const CEPoint apparent_ptStartPos, CEPoint const apparent_ptEndPos, const UndoRecord & ur) override;
95
96         virtual std::vector<uint32_t> *CopyRevisionNumbers(int nStartLine, int nEndLine) const override;
97         virtual void RestoreRevisionNumbers(int nStartLine, std::vector<uint32_t> *paSavedRevisionNumbers) override;
98
99 public:
100         //@{
101         /**
102          * @name Real/apparent line number conversion functions.
103          * These functions convert line numbers between file line numbers
104          * (real line numbers) and screen line numbers (apparent line numbers).
105          *
106          * This mapping is needed to handle ghost lines (ones with no text or
107          * EOL chars) which WinMerge uses for left-only or right-only lines.
108         */
109         int ApparentLastRealLine() const;
110         int ComputeRealLine(int nApparentLine) const;
111         int ComputeApparentLine(int nRealLine) const;
112         /** richer position information   yApparent = apparent(yReal) - yGhost */
113         int ComputeRealLineAndGhostAdjustment(int nApparentLine, int& decToReal) const;
114         /** richer position information   yApparent = apparent(yReal) - yGhost */
115         int ComputeApparentLine(int nRealLine, int decToReal) const;
116         //@}
117
118         /** for loading file */
119         void FinishLoading();
120         /** for saving file */ 
121         void RemoveAllGhostLines();
122
123
124 private:
125         void RecomputeRealityMapping();
126         void CountEolAndLastLineLength(const CEPoint& ptStartPos, const tchar_t* pszText, size_t cchText, int& nLastLineLength, int& nEol);
127         /** For debugging purpose */
128         void checkFlagsFromReality() const;
129
130 protected:
131         virtual void OnNotifyLineHasBeenEdited(int nLine);
132
133
134 protected:
135         // Overrides
136         // ClassWizard generated virtual function overrides
137         //{{AFX_VIRTUAL(CCrystalTextBuffer)
138         //}}AFX_VIRTUAL
139 };