1 ////////////////////////////////////////////////////////////////////////////
2 // File: GhostTextView.h
4 // Created: 31-Jul-2003
6 /////////////////////////////////////////////////////////////////////////////
7 // WinMerge: an interactive diff/merge utility
8 // Copyright (C) 1997-2000 Thingamahoochie Software
10 // SPDX-License-Identifier: GPL-2.0-or-later
11 /////////////////////////////////////////////////////////////////////////////
15 #include "GhostTextView.h"
16 #include "GhostTextBuffer.h"
23 IMPLEMENT_DYNCREATE (CGhostTextView, CCrystalEditViewEx)
26 * @brief Constructor, initializes members.
28 CGhostTextView::CGhostTextView()
29 : m_pGhostTextBuffer(nullptr)
30 , m_ptCursorPosPushed{}
31 , m_ptSelStartPushed{}
34 , m_ptDraggedTextBeginPushed{}
35 , m_ptDraggedTextEndPushed{}
36 , m_ptSavedCaretPosPushed{}
37 , m_ptSavedSelStartPushed{}
38 , m_ptSavedSelEndPushed{}
39 , m_ptLastChangePushed{}
40 , m_nTopSubLinePushed(0)
45 ReAttachToBuffer (CCrystalTextBuffer * pBuf /*= nullptr*/ )
49 pBuf = LocateTextBuffer ();
52 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
53 CCrystalEditViewEx::ReAttachToBuffer(pBuf);
57 AttachToBuffer (CCrystalTextBuffer * pBuf /*= nullptr*/ )
61 pBuf = LocateTextBuffer ();
64 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
65 CCrystalEditViewEx::AttachToBuffer(pBuf);
71 m_pGhostTextBuffer = nullptr;
72 CCrystalEditViewEx::DetachFromBuffer();
75 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
78 pt.y = m_pGhostTextBuffer->ComputeApparentLine(Ssrc.y, Ssrc.nToFirstReal);
79 // if the cursor was in a trailing ghost line, and this disappeared,
80 // got at the end of the last line
81 if (pt.y >= GetLineCount())
83 pt.y = GetLineCount()-1;
84 pt.x = GetLineLength(pt.y);
90 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
93 if (m_pGhostTextBuffer)
94 Sdest.y = m_pGhostTextBuffer->ComputeRealLineAndGhostAdjustment(pt.y, Sdest.nToFirstReal);
99 void CGhostTextView::PopCursors ()
101 CPoint ptCursorLast = m_ptCursorLast;
102 popPosition(m_ptCursorPosPushed, ptCursorLast);
104 ASSERT_VALIDTEXTPOS (ptCursorLast);
105 SetCursorPos (ptCursorLast);
107 popPosition(m_ptSelStartPushed, m_ptSelStart);
108 ASSERT_VALIDTEXTPOS (m_ptSelStart);
109 popPosition(m_ptSelEndPushed, m_ptSelEnd);
110 ASSERT_VALIDTEXTPOS (m_ptSelEnd);
111 popPosition(m_ptAnchorPushed, m_ptAnchor);
112 ASSERT_VALIDTEXTPOS (m_ptAnchor);
114 // here is what we did before, maybe we have to do it, but test with pushed positions
115 // SetSelection (ptCursorLast, ptCursorLast);
116 // SetAnchor (ptCursorLast);
120 popPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
121 ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
122 popPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
123 ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
125 if (m_bDropPosVisible)
127 popPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
128 ASSERT_VALIDTEXTPOS(m_ptSavedCaretPos);
130 if (m_bSelectionPushed)
132 popPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
133 ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
134 popPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
135 ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
139 if (m_ptLastChangePushed.y == 0 && m_ptLastChangePushed.nToFirstReal > 0)
140 ptLastChange = CPoint(-1,-1);
143 popPosition(m_ptLastChangePushed, ptLastChange);
144 ASSERT_VALIDTEXTPOS(ptLastChange);
146 m_pGhostTextBuffer->RestoreLastChangePos(ptLastChange);
148 // restore the scrolling position
149 m_nTopSubLine = m_nTopSubLinePushed;
150 if (m_nTopSubLine >= GetSubLineCount())
151 m_nTopSubLine = GetSubLineCount() - 1;
152 if (m_nTopSubLine < 0)
155 GetLineBySubLine( m_nTopSubLine, m_nTopLine, nDummy );
156 RecalcVertScrollBar(true);
157 RecalcHorzScrollBar();
160 void CGhostTextView::PushCursors ()
162 pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
163 pushPosition(m_ptSelStartPushed, m_ptSelStart);
164 pushPosition(m_ptSelEndPushed, m_ptSelEnd);
165 pushPosition(m_ptAnchorPushed, m_ptAnchor);
168 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
169 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
171 if (m_bDropPosVisible)
173 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
175 if (m_bSelectionPushed)
177 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
178 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
181 pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer ? m_pGhostTextBuffer->GetLastChangePos() : CPoint{0, 0});
183 // and top line positions
184 m_nTopSubLinePushed = m_nTopSubLine;
190 int CGhostTextView::ComputeRealLine (int nApparentLine) const
192 if (m_pGhostTextBuffer == nullptr)
194 return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
197 int CGhostTextView::ComputeApparentLine (int nRealLine) const
199 return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
202 void CGhostTextView::GetTextWithoutEmptys (int nStartLine, int nStartChar,
203 int nEndLine, int nEndChar, CString &text,
204 CRLFSTYLE nCrlfStyle /*= CRLF_STYLE_AUTOMATIC*/,
205 bool bExcludeInvisibleLines /*= true*/)
207 if (m_pGhostTextBuffer != nullptr)
208 m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle, bExcludeInvisibleLines);
213 void CGhostTextView::GetTextWithoutEmptysInColumnSelection (CString & text, bool bExcludeInvisibleLines /*= true*/)
215 if (m_pGhostTextBuffer == nullptr)
223 CString sEol = m_pGhostTextBuffer->GetStringEol (CRLF_STYLE_DOS);
226 for (int L = m_ptDrawSelStart.y; L <= m_ptDrawSelEnd.y; L++)
227 nBufSize += GetLineLength (L) + sEol.GetLength ();
228 LPTSTR pszBuf = text.GetBuffer (nBufSize);
230 for (int I = m_ptDrawSelStart.y; I <= m_ptDrawSelEnd.y; I++)
232 // exclude ghost lines
233 if ((GetLineFlags(I) & LF_GHOST) || (bExcludeInvisibleLines && (GetLineFlags(I) & LF_INVISIBLE)))
236 int nSelLeft, nSelRight;
237 GetColumnSelection (I, nSelLeft, nSelRight);
238 memcpy (pszBuf, GetLineChars (I) + nSelLeft, sizeof (TCHAR) * (nSelRight - nSelLeft));
239 pszBuf += (nSelRight - nSelLeft);
240 memcpy (pszBuf, sEol, sizeof (TCHAR) * sEol.GetLength ());
241 pszBuf += sEol.GetLength ();
244 text.ReleaseBuffer ();
248 HGLOBAL CGhostTextView::PrepareDragData ()
251 if (m_ptDrawSelStart == m_ptDrawSelEnd)
255 GetTextWithoutEmptys (m_ptDrawSelStart.y, m_ptDrawSelStart.x, m_ptDrawSelEnd.y, m_ptDrawSelEnd.x, text);
256 int cchText = text.GetLength();
257 SIZE_T cbData = (cchText + 1) * sizeof(TCHAR);
258 HGLOBAL hData =::GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, cbData);
259 if (hData == nullptr)
262 LPTSTR pszData = (LPTSTR)::GlobalLock (hData);
263 if (pszData != nullptr)
264 memcpy (pszData, text, cbData);
265 ::GlobalUnlock (hData);
267 m_ptDraggedTextBegin = m_ptDrawSelStart;
268 m_ptDraggedTextEnd = m_ptDrawSelEnd;
273 * @brief Draw selection margin.
274 * @param [in] pdc Pointer to draw context.
275 * @param [in] rect The rectangle to draw.
276 * @param [in] nLineIndex Index of line in view.
277 * @param [in] nLineNumber Line number to display. if -1, it's not displayed.
279 void CGhostTextView::DrawMargin (const CRect & rect, int nLineIndex, int nLineNumber)
282 if (nLineIndex < 0 || GetLineFlags(nLineIndex) & LF_GHOST)
283 nRealLineNumber = -1;
285 nRealLineNumber = ComputeRealLine(nLineIndex) + 1;
286 CCrystalTextView::DrawMargin(rect, nLineIndex, nRealLineNumber);