OSDN Git Service

BUG: [ 1496066 ] WinMerge crashed with those files
[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 {
47 }
48
49 void CGhostTextView::
50 ReAttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
51 {
52         if (pBuf == NULL)
53         {
54                 pBuf = LocateTextBuffer ();
55                 // ...
56         }
57         m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
58         CCrystalEditViewEx::ReAttachToBuffer(pBuf);
59 }
60
61 void CGhostTextView::
62 AttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
63 {
64         if (pBuf == NULL)
65         {
66                 pBuf = LocateTextBuffer ();
67                 // ...
68         }
69         m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
70         CCrystalEditViewEx::AttachToBuffer(pBuf);
71 }
72
73 void CGhostTextView::
74 DetachFromBuffer ()
75 {
76         if (m_pGhostTextBuffer != NULL)
77                 m_pGhostTextBuffer = NULL;
78         CCrystalEditViewEx::DetachFromBuffer();
79 }
80
81 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
82 {
83         pt.x = Ssrc.x;
84         pt.y = m_pGhostTextBuffer->ComputeApparentLine(Ssrc.y, Ssrc.nToFirstReal);
85         // if the cursor was in a trailing ghost line, and this disappeared,
86         // got at the end of the last line
87         if (pt.y >= GetLineCount())
88         {
89                 pt.y = GetLineCount()-1;
90                 pt.x = GetLineLength(pt.y);
91         }
92 }
93
94 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
95 {
96         Sdest.x = pt.x;
97         Sdest.y = m_pGhostTextBuffer->ComputeRealLineAndGhostAdjustment(pt.y, Sdest.nToFirstReal);
98 }
99
100 void CGhostTextView::PopCursors ()
101 {
102         CPoint ptCursorLast = m_ptCursorLast;
103         popPosition(m_ptCursorPosPushed, ptCursorLast);
104
105         ASSERT_VALIDTEXTPOS (ptCursorLast);
106         SetCursorPos (ptCursorLast);
107
108         popPosition(m_ptSelStartPushed, m_ptSelStart);
109         ASSERT_VALIDTEXTPOS (m_ptSelStart);
110         popPosition(m_ptSelEndPushed, m_ptSelEnd);
111         ASSERT_VALIDTEXTPOS (m_ptSelEnd);
112         popPosition(m_ptAnchorPushed, m_ptAnchor);
113         ASSERT_VALIDTEXTPOS (m_ptAnchor);
114         // laoran 2003/09/03
115         // here is what we did before, maybe we have to do it, but test with pushed positions
116         // SetSelection (ptCursorLast, ptCursorLast);
117         // SetAnchor (ptCursorLast);
118
119         if (m_bDraggingText == TRUE)
120         {
121                 popPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
122                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
123                 popPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
124                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
125         }
126         if (m_bDropPosVisible == TRUE)
127         {
128                 popPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
129                 ASSERT_VALIDTEXTPOS(m_ptSavedCaretPos);
130         }
131         if (m_bSelectionPushed == TRUE)
132         {
133                 popPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
134                 ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
135                 popPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
136                 ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
137         }
138
139         CPoint ptLastChange;
140         if (m_ptLastChangePushed.y == 0 && m_ptLastChangePushed.nToFirstReal > 0)
141                 ptLastChange = CPoint(-1,-1);
142         else 
143         {
144                 popPosition(m_ptLastChangePushed, ptLastChange);
145                 ASSERT_VALIDTEXTPOS(ptLastChange);
146         }
147         m_pGhostTextBuffer->RestoreLastChangePos(ptLastChange);
148
149         // restore the scrolling position
150         CPoint temp;
151         popPosition(m_nTopLinePushed, temp);
152         ASSERT_VALIDTEXTPOS(temp);
153         ScrollToLine(temp.y);
154 }
155
156 void CGhostTextView::PushCursors ()
157 {
158         pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
159         pushPosition(m_ptSelStartPushed, m_ptSelStart);
160         pushPosition(m_ptSelEndPushed, m_ptSelEnd);
161         pushPosition(m_ptAnchorPushed, m_ptAnchor);
162         if (m_bDraggingText == TRUE)
163         {
164                 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
165                 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
166         }
167         if (m_bDropPosVisible == TRUE)
168         {
169                 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
170         }
171         if (m_bSelectionPushed == TRUE)
172         {
173                 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
174                 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
175         }
176
177         pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer->GetLastChangePos());
178
179         // and top line positions
180         pushPosition(m_nTopLinePushed, CPoint(0, m_nTopLine));
181 }
182
183
184
185
186 int CGhostTextView::ComputeRealLine (int nApparentLine) const
187 {
188         if (!m_pGhostTextBuffer)
189                 return 0;
190         return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
191 }
192
193 int CGhostTextView::ComputeApparentLine (int nRealLine) const
194 {
195         return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
196 }
197
198 void CGhostTextView::GetTextWithoutEmptys (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString &text, int nCrlfStyle /*=CRLF_STYLE_AUTOMATIC*/ )
199 {
200   if (m_pGhostTextBuffer != NULL)
201     m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle);
202   else
203     text = _T ("");
204 }
205
206 HGLOBAL CGhostTextView::PrepareDragData ()
207 {
208         PrepareSelBounds ();
209         if (m_ptDrawSelStart == m_ptDrawSelEnd)
210                 return NULL;
211
212         CString text;
213         GetTextWithoutEmptys (m_ptDrawSelStart.y, m_ptDrawSelStart.x, m_ptDrawSelEnd.y, m_ptDrawSelEnd.x, text);
214         HGLOBAL hData =::GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, (_tcslen (text)+1)*sizeof(TCHAR));
215         if (hData == NULL)
216                 return NULL;
217
218         LPTSTR pszData = (LPTSTR)::GlobalLock (hData);
219         _tcscpy (pszData, text.GetBuffer (0));
220         text.ReleaseBuffer ();
221         ::GlobalUnlock (hData);
222
223         m_ptDraggedTextBegin = m_ptDrawSelStart;
224         m_ptDraggedTextEnd = m_ptDrawSelEnd;
225         return hData;
226 }
227
228 /**
229  * @brief Draw selection margin. 
230  * @param [in] pdc         Pointer to draw context.
231  * @param [in] rect        The rectangle to draw.
232  * @param [in] nLineIndex  Index of line in view.
233  * @param [in] nLineNumber Line number to display. if -1, it's not displayed.
234  */
235 void CGhostTextView::DrawMargin (CDC * pdc, const CRect & rect, int nLineIndex, int nLineNumber)
236 {
237         int nRealLineNumber;
238         if (nLineIndex < 0 || GetLineFlags(nLineIndex) & LF_GHOST)
239                 nRealLineNumber = -1;
240         else
241                 nRealLineNumber = ComputeRealLine(nLineIndex) + 1;
242         CCrystalTextView::DrawMargin(pdc, rect, nLineIndex, nRealLineNumber);
243 }