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
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /////////////////////////////////////////////////////////////////////////////
29 #include "GhostTextView.h"
30 #include "GhostTextBuffer.h"
35 static char THIS_FILE[] = __FILE__;
39 IMPLEMENT_DYNCREATE (CGhostTextView, CCrystalEditViewEx)
42 * @brief Constructor, initializes members.
44 CGhostTextView::CGhostTextView()
45 : m_pGhostTextBuffer(NULL)
46 , m_nTopSubLinePushed(0)
51 ReAttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
55 pBuf = LocateTextBuffer ();
58 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
59 CCrystalEditViewEx::ReAttachToBuffer(pBuf);
63 AttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
67 pBuf = LocateTextBuffer ();
70 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
71 CCrystalEditViewEx::AttachToBuffer(pBuf);
77 if (m_pGhostTextBuffer != NULL)
78 m_pGhostTextBuffer = NULL;
79 CCrystalEditViewEx::DetachFromBuffer();
82 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
85 pt.y = m_pGhostTextBuffer->ComputeApparentLine(Ssrc.y, Ssrc.nToFirstReal);
86 // if the cursor was in a trailing ghost line, and this disappeared,
87 // got at the end of the last line
88 if (pt.y >= GetLineCount())
90 pt.y = GetLineCount()-1;
91 pt.x = GetLineLength(pt.y);
95 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
98 Sdest.y = m_pGhostTextBuffer->ComputeRealLineAndGhostAdjustment(pt.y, Sdest.nToFirstReal);
101 void CGhostTextView::PopCursors ()
103 CPoint ptCursorLast = m_ptCursorLast;
104 popPosition(m_ptCursorPosPushed, ptCursorLast);
106 ASSERT_VALIDTEXTPOS (ptCursorLast);
107 SetCursorPos (ptCursorLast);
109 popPosition(m_ptSelStartPushed, m_ptSelStart);
110 ASSERT_VALIDTEXTPOS (m_ptSelStart);
111 popPosition(m_ptSelEndPushed, m_ptSelEnd);
112 ASSERT_VALIDTEXTPOS (m_ptSelEnd);
113 popPosition(m_ptAnchorPushed, m_ptAnchor);
114 ASSERT_VALIDTEXTPOS (m_ptAnchor);
116 // here is what we did before, maybe we have to do it, but test with pushed positions
117 // SetSelection (ptCursorLast, ptCursorLast);
118 // SetAnchor (ptCursorLast);
122 popPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
123 ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
124 popPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
125 ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
127 if (m_bDropPosVisible)
129 popPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
130 ASSERT_VALIDTEXTPOS(m_ptSavedCaretPos);
132 if (m_bSelectionPushed)
134 popPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
135 ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
136 popPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
137 ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
141 if (m_ptLastChangePushed.y == 0 && m_ptLastChangePushed.nToFirstReal > 0)
142 ptLastChange = CPoint(-1,-1);
145 popPosition(m_ptLastChangePushed, ptLastChange);
146 ASSERT_VALIDTEXTPOS(ptLastChange);
148 m_pGhostTextBuffer->RestoreLastChangePos(ptLastChange);
150 // restore the scrolling position
151 m_nTopSubLine = m_nTopSubLinePushed;
152 if (m_nTopSubLine >= GetSubLineCount())
153 m_nTopSubLine = GetSubLineCount() - 1;
154 if (m_nTopSubLine < 0)
157 GetLineBySubLine( m_nTopSubLine, m_nTopLine, nDummy );
158 RecalcVertScrollBar(true);
161 void CGhostTextView::PushCursors ()
163 pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
164 pushPosition(m_ptSelStartPushed, m_ptSelStart);
165 pushPosition(m_ptSelEndPushed, m_ptSelEnd);
166 pushPosition(m_ptAnchorPushed, m_ptAnchor);
169 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
170 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
172 if (m_bDropPosVisible)
174 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
176 if (m_bSelectionPushed)
178 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
179 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
182 pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer->GetLastChangePos());
184 // and top line positions
185 m_nTopSubLinePushed = m_nTopSubLine;
191 int CGhostTextView::ComputeRealLine (int nApparentLine) const
193 if (!m_pGhostTextBuffer)
195 return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
198 int CGhostTextView::ComputeApparentLine (int nRealLine) const
200 return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
203 void CGhostTextView::GetTextWithoutEmptys (int nStartLine, int nStartChar,
204 int nEndLine, int nEndChar, CString &text,
205 CRLFSTYLE nCrlfStyle /*=CRLF_STYLE_AUTOMATIC*/,
206 bool bExcludeInvisibleLines/*=false*/)
208 if (m_pGhostTextBuffer != NULL)
209 m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle, bExcludeInvisibleLines);
214 void CGhostTextView::GetTextWithoutEmptysInColumnSelection (CString & text, bool bExcludeInvisibleLines/*=true*/)
216 if (m_pGhostTextBuffer == NULL)
224 CString sEol = m_pGhostTextBuffer->GetStringEol (CRLF_STYLE_DOS);
227 for (int L = m_ptDrawSelStart.y; L <= m_ptDrawSelEnd.y; L++)
228 nBufSize += GetLineLength (L) + sEol.GetLength ();
229 LPTSTR pszBuf = text.GetBuffer (nBufSize);
231 for (int I = m_ptDrawSelStart.y; I <= m_ptDrawSelEnd.y; I++)
233 // exclude ghost lines
234 if ((GetLineFlags(I) & LF_GHOST) || (bExcludeInvisibleLines && (GetLineFlags(I) & LF_INVISIBLE)))
237 int nSelLeft, nSelRight;
238 GetColumnSelection (I, nSelLeft, nSelRight);
239 memcpy (pszBuf, GetLineChars (I) + nSelLeft, sizeof (TCHAR) * (nSelRight - nSelLeft));
240 pszBuf += (nSelRight - nSelLeft);
241 memcpy (pszBuf, sEol, sizeof (TCHAR) * sEol.GetLength ());
242 pszBuf += sEol.GetLength ();
245 text.ReleaseBuffer ();
249 HGLOBAL CGhostTextView::PrepareDragData ()
252 if (m_ptDrawSelStart == m_ptDrawSelEnd)
256 GetTextWithoutEmptys (m_ptDrawSelStart.y, m_ptDrawSelStart.x, m_ptDrawSelEnd.y, m_ptDrawSelEnd.x, text);
257 int cchText = text.GetLength();
258 SIZE_T cbData = (cchText + 1) * sizeof(TCHAR);
259 HGLOBAL hData =::GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, cbData);
262 ::GlobalReAlloc(hData, cbData, 0);
263 ASSERT(::GlobalSize(hData) == cbData);
265 LPTSTR pszData = (LPTSTR)::GlobalLock (hData);
267 memcpy (pszData, text, cbData);
268 ::GlobalUnlock (hData);
270 m_ptDraggedTextBegin = m_ptDrawSelStart;
271 m_ptDraggedTextEnd = m_ptDrawSelEnd;
276 * @brief Draw selection margin.
277 * @param [in] pdc Pointer to draw context.
278 * @param [in] rect The rectangle to draw.
279 * @param [in] nLineIndex Index of line in view.
280 * @param [in] nLineNumber Line number to display. if -1, it's not displayed.
282 void CGhostTextView::DrawMargin (CDC * pdc, const CRect & rect, int nLineIndex, int nLineNumber)
285 if (nLineIndex < 0 || GetLineFlags(nLineIndex) & LF_GHOST)
286 nRealLineNumber = -1;
288 nRealLineNumber = ComputeRealLine(nLineIndex) + 1;
289 CCrystalTextView::DrawMargin(pdc, rect, nLineIndex, nRealLineNumber);