OSDN Git Service

Merge with stable
[winmerge-jp/winmerge-jp.git] / Src / GhostTextView.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //  File:       GhostTextView.h
3 //  Version:    1.0.0.0
4 //  Created:    31-Jul-2003
5 //
6 /////////////////////////////////////////////////////////////////////////////
7 //    WinMerge:  an interactive diff/merge utility
8 //    Copyright (C) 1997-2000  Thingamahoochie Software
9 //    Author: Dean Grimm
10 //
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.
15 //
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.
20 //
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.
24 //
25 /////////////////////////////////////////////////////////////////////////////
26
27
28 #include "stdafx.h"
29 #include "GhostTextView.h"
30 #include "GhostTextBuffer.h"
31
32 #ifdef _DEBUG
33 #define new DEBUG_NEW
34 #undef THIS_FILE
35 static char THIS_FILE[] = __FILE__;
36 #endif
37
38
39 IMPLEMENT_DYNCREATE (CGhostTextView, CCrystalEditViewEx)
40
41 /** 
42  * @brief Constructor, initializes members.
43  */
44 CGhostTextView::CGhostTextView()
45 : m_pGhostTextBuffer(NULL)
46 , m_nTopSubLinePushed(0)
47 {
48 }
49
50 void CGhostTextView::
51 ReAttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
52 {
53         if (pBuf == NULL)
54         {
55                 pBuf = LocateTextBuffer ();
56                 // ...
57         }
58         m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
59         CCrystalEditViewEx::ReAttachToBuffer(pBuf);
60 }
61
62 void CGhostTextView::
63 AttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
64 {
65         if (pBuf == NULL)
66         {
67                 pBuf = LocateTextBuffer ();
68                 // ...
69         }
70         m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
71         CCrystalEditViewEx::AttachToBuffer(pBuf);
72 }
73
74 void CGhostTextView::
75 DetachFromBuffer ()
76 {
77         if (m_pGhostTextBuffer != NULL)
78                 m_pGhostTextBuffer = NULL;
79         CCrystalEditViewEx::DetachFromBuffer();
80 }
81
82 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
83 {
84         pt.x = Ssrc.x;
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())
89         {
90                 pt.y = GetLineCount()-1;
91                 pt.x = GetLineLength(pt.y);
92         }
93 }
94
95 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
96 {
97         Sdest.x = pt.x;
98         Sdest.y = m_pGhostTextBuffer->ComputeRealLineAndGhostAdjustment(pt.y, Sdest.nToFirstReal);
99 }
100
101 void CGhostTextView::PopCursors ()
102 {
103         CPoint ptCursorLast = m_ptCursorLast;
104         popPosition(m_ptCursorPosPushed, ptCursorLast);
105
106         ASSERT_VALIDTEXTPOS (ptCursorLast);
107         SetCursorPos (ptCursorLast);
108
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);
115         // laoran 2003/09/03
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);
119
120         if (m_bDraggingText)
121         {
122                 popPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
123                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
124                 popPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
125                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
126         }
127         if (m_bDropPosVisible)
128         {
129                 popPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
130                 ASSERT_VALIDTEXTPOS(m_ptSavedCaretPos);
131         }
132         if (m_bSelectionPushed)
133         {
134                 popPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
135                 ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
136                 popPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
137                 ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
138         }
139
140         CPoint ptLastChange;
141         if (m_ptLastChangePushed.y == 0 && m_ptLastChangePushed.nToFirstReal > 0)
142                 ptLastChange = CPoint(-1,-1);
143         else 
144         {
145                 popPosition(m_ptLastChangePushed, ptLastChange);
146                 ASSERT_VALIDTEXTPOS(ptLastChange);
147         }
148         m_pGhostTextBuffer->RestoreLastChangePos(ptLastChange);
149
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)
155                 m_nTopSubLine = 0;
156         int nDummy;
157         GetLineBySubLine( m_nTopSubLine, m_nTopLine, nDummy );
158     RecalcVertScrollBar(true);
159 }
160
161 void CGhostTextView::PushCursors ()
162 {
163         pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
164         pushPosition(m_ptSelStartPushed, m_ptSelStart);
165         pushPosition(m_ptSelEndPushed, m_ptSelEnd);
166         pushPosition(m_ptAnchorPushed, m_ptAnchor);
167         if (m_bDraggingText)
168         {
169                 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
170                 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
171         }
172         if (m_bDropPosVisible)
173         {
174                 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
175         }
176         if (m_bSelectionPushed)
177         {
178                 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
179                 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
180         }
181
182         pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer->GetLastChangePos());
183
184         // and top line positions
185         m_nTopSubLinePushed = m_nTopSubLine;
186 }
187
188
189
190
191 int CGhostTextView::ComputeRealLine (int nApparentLine) const
192 {
193         if (!m_pGhostTextBuffer)
194                 return 0;
195         return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
196 }
197
198 int CGhostTextView::ComputeApparentLine (int nRealLine) const
199 {
200         return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
201 }
202
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*/)
207 {
208   if (m_pGhostTextBuffer != NULL)
209     m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle, bExcludeInvisibleLines);
210   else
211     text = _T ("");
212 }
213
214 void CGhostTextView::GetTextWithoutEmptysInColumnSelection (CString & text, bool bExcludeInvisibleLines/*=true*/)
215 {
216         if (m_pGhostTextBuffer == NULL)
217         {
218                 text = _T ("");
219                 return;
220         }
221
222         PrepareSelBounds ();
223
224         CString sEol = m_pGhostTextBuffer->GetStringEol (CRLF_STYLE_DOS);
225
226         int nBufSize = 1;
227         for (int L = m_ptDrawSelStart.y; L <= m_ptDrawSelEnd.y; L++)
228                 nBufSize += GetLineLength (L) + sEol.GetLength ();
229         LPTSTR pszBuf = text.GetBuffer (nBufSize);
230
231         for (int I = m_ptDrawSelStart.y; I <= m_ptDrawSelEnd.y; I++)
232         {
233                 // exclude ghost lines
234                 if ((GetLineFlags(I) & LF_GHOST) || (bExcludeInvisibleLines && (GetLineFlags(I) & LF_INVISIBLE)))
235                         continue;
236
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 ();
243         }
244         pszBuf[0] = 0;
245         text.ReleaseBuffer ();
246         text.FreeExtra ();
247 }
248
249 HGLOBAL CGhostTextView::PrepareDragData ()
250 {
251         PrepareSelBounds ();
252         if (m_ptDrawSelStart == m_ptDrawSelEnd)
253                 return NULL;
254
255         CString text;
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);
260         if (hData == NULL)
261                 return NULL;
262         ::GlobalReAlloc(hData, cbData, 0);
263         ASSERT(::GlobalSize(hData) == cbData);
264
265         LPTSTR pszData = (LPTSTR)::GlobalLock (hData);
266         if (pszData)
267                 memcpy (pszData, text, cbData);
268         ::GlobalUnlock (hData);
269
270         m_ptDraggedTextBegin = m_ptDrawSelStart;
271         m_ptDraggedTextEnd = m_ptDrawSelEnd;
272         return hData;
273 }
274
275 /**
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.
281  */
282 void CGhostTextView::DrawMargin (CDC * pdc, const CRect & rect, int nLineIndex, int nLineNumber)
283 {
284         int nRealLineNumber;
285         if (nLineIndex < 0 || GetLineFlags(nLineIndex) & LF_GHOST)
286                 nRealLineNumber = -1;
287         else
288                 nRealLineNumber = ComputeRealLine(nLineIndex) + 1;
289         CCrystalTextView::DrawMargin(pdc, rect, nLineIndex, nRealLineNumber);
290 }