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"
37 IMPLEMENT_DYNCREATE (CGhostTextView, CCrystalEditViewEx)
40 * @brief Constructor, initializes members.
42 CGhostTextView::CGhostTextView()
43 : m_pGhostTextBuffer(NULL)
44 , m_nTopSubLinePushed(0)
49 ReAttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
53 pBuf = LocateTextBuffer ();
56 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
57 CCrystalEditViewEx::ReAttachToBuffer(pBuf);
61 AttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
65 pBuf = LocateTextBuffer ();
68 m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
69 CCrystalEditViewEx::AttachToBuffer(pBuf);
75 if (m_pGhostTextBuffer != NULL)
76 m_pGhostTextBuffer = NULL;
77 CCrystalEditViewEx::DetachFromBuffer();
80 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
83 pt.y = m_pGhostTextBuffer->ComputeApparentLine(Ssrc.y, Ssrc.nToFirstReal);
84 // if the cursor was in a trailing ghost line, and this disappeared,
85 // got at the end of the last line
86 if (pt.y >= GetLineCount())
88 pt.y = GetLineCount()-1;
89 pt.x = GetLineLength(pt.y);
93 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
96 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);
159 void CGhostTextView::PushCursors ()
161 pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
162 pushPosition(m_ptSelStartPushed, m_ptSelStart);
163 pushPosition(m_ptSelEndPushed, m_ptSelEnd);
164 pushPosition(m_ptAnchorPushed, m_ptAnchor);
167 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
168 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
170 if (m_bDropPosVisible)
172 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
174 if (m_bSelectionPushed)
176 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
177 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
180 pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer->GetLastChangePos());
182 // and top line positions
183 m_nTopSubLinePushed = m_nTopSubLine;
189 int CGhostTextView::ComputeRealLine (int nApparentLine) const
191 if (!m_pGhostTextBuffer)
193 return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
196 int CGhostTextView::ComputeApparentLine (int nRealLine) const
198 return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
201 void CGhostTextView::GetTextWithoutEmptys (int nStartLine, int nStartChar,
202 int nEndLine, int nEndChar, CString &text,
203 CRLFSTYLE nCrlfStyle /*=CRLF_STYLE_AUTOMATIC*/,
204 bool bExcludeInvisibleLines/*=false*/)
206 if (m_pGhostTextBuffer != NULL)
207 m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle, bExcludeInvisibleLines);
212 void CGhostTextView::GetTextWithoutEmptysInColumnSelection (CString & text, bool bExcludeInvisibleLines/*=true*/)
214 if (m_pGhostTextBuffer == NULL)
222 CString sEol = m_pGhostTextBuffer->GetStringEol (CRLF_STYLE_DOS);
225 for (int L = m_ptDrawSelStart.y; L <= m_ptDrawSelEnd.y; L++)
226 nBufSize += GetLineLength (L) + sEol.GetLength ();
227 LPTSTR pszBuf = text.GetBuffer (nBufSize);
229 for (int I = m_ptDrawSelStart.y; I <= m_ptDrawSelEnd.y; I++)
231 // exclude ghost lines
232 if ((GetLineFlags(I) & LF_GHOST) || (bExcludeInvisibleLines && (GetLineFlags(I) & LF_INVISIBLE)))
235 int nSelLeft, nSelRight;
236 GetColumnSelection (I, nSelLeft, nSelRight);
237 memcpy (pszBuf, GetLineChars (I) + nSelLeft, sizeof (TCHAR) * (nSelRight - nSelLeft));
238 pszBuf += (nSelRight - nSelLeft);
239 memcpy (pszBuf, sEol, sizeof (TCHAR) * sEol.GetLength ());
240 pszBuf += sEol.GetLength ();
243 text.ReleaseBuffer ();
247 HGLOBAL CGhostTextView::PrepareDragData ()
250 if (m_ptDrawSelStart == m_ptDrawSelEnd)
254 GetTextWithoutEmptys (m_ptDrawSelStart.y, m_ptDrawSelStart.x, m_ptDrawSelEnd.y, m_ptDrawSelEnd.x, text);
255 int cchText = text.GetLength();
256 SIZE_T cbData = (cchText + 1) * sizeof(TCHAR);
257 HGLOBAL hData =::GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, cbData);
261 LPTSTR pszData = (LPTSTR)::GlobalLock (hData);
263 memcpy (pszData, text, cbData);
264 ::GlobalUnlock (hData);
266 m_ptDraggedTextBegin = m_ptDrawSelStart;
267 m_ptDraggedTextEnd = m_ptDrawSelEnd;
272 * @brief Draw selection margin.
273 * @param [in] pdc Pointer to draw context.
274 * @param [in] rect The rectangle to draw.
275 * @param [in] nLineIndex Index of line in view.
276 * @param [in] nLineNumber Line number to display. if -1, it's not displayed.
278 void CGhostTextView::DrawMargin (CDC * pdc, const CRect & rect, int nLineIndex, int nLineNumber)
281 if (nLineIndex < 0 || GetLineFlags(nLineIndex) & LF_GHOST)
282 nRealLineNumber = -1;
284 nRealLineNumber = ComputeRealLine(nLineIndex) + 1;
285 CCrystalTextView::DrawMargin(pdc, rect, nLineIndex, nRealLineNumber);