OSDN Git Service

fix in PopCursors for ptLastChange
[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
43 void CGhostTextView::
44 AttachToBuffer (CCrystalTextBuffer * pBuf /*= NULL*/ )
45 {
46         if (pBuf == NULL)
47         {
48                 pBuf = LocateTextBuffer ();
49                 // ...
50         }
51         m_pGhostTextBuffer = dynamic_cast<CGhostTextBuffer*> (pBuf);
52         CCrystalEditViewEx::AttachToBuffer(pBuf);
53 }
54
55 void CGhostTextView::
56 DetachFromBuffer ()
57 {
58         if (m_pGhostTextBuffer != NULL)
59                 m_pGhostTextBuffer = NULL;
60         CCrystalEditViewEx::DetachFromBuffer();
61 }
62
63
64
65
66
67
68
69
70 void CGhostTextView::popPosition(SCursorPushed Ssrc, CPoint & pt)
71 {
72         pt.x = Ssrc.x;
73         pt.y = m_pGhostTextBuffer->ComputeApparentLine(Ssrc.y, Ssrc.nToFirstReal);
74         // if the cursor was in a trailing ghost line, and this disappeared,
75         // got at the end of the last line
76         if (pt.y >= GetLineCount())
77         {
78                 pt.y = GetLineCount()-1;
79                 pt.x = GetLineLength(pt.y);
80         }
81 }
82
83 void CGhostTextView::pushPosition(SCursorPushed & Sdest, CPoint pt)
84 {
85         Sdest.x = pt.x;
86         Sdest.y = m_pGhostTextBuffer->ComputeRealLineAndGhostAdjustment(pt.y, Sdest.nToFirstReal);
87 }
88
89 void CGhostTextView::
90 PopCursors ()
91 {
92         CPoint ptCursorLast = m_ptCursorLast;
93         popPosition(m_ptCursorPosPushed, ptCursorLast);
94
95         ASSERT_VALIDTEXTPOS (ptCursorLast);
96         SetCursorPos (ptCursorLast);
97
98         popPosition(m_ptSelStartPushed, m_ptSelStart);
99         ASSERT_VALIDTEXTPOS (m_ptSelStart);
100         popPosition(m_ptSelEndPushed, m_ptSelEnd);
101         ASSERT_VALIDTEXTPOS (m_ptSelEnd);
102         popPosition(m_ptAnchorPushed, m_ptAnchor);
103         ASSERT_VALIDTEXTPOS (m_ptAnchor);
104         // laoran 2003/09/03
105         // here is what we did before, maybe we have to do it, but test with pushed positions
106         // SetSelection (ptCursorLast, ptCursorLast);
107         // SetAnchor (ptCursorLast);
108
109         if (m_bDraggingText == TRUE)
110         {
111                 popPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
112                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
113                 popPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
114                 ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
115         }
116         if (m_bDropPosVisible == TRUE)
117         {
118                 popPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
119                 ASSERT_VALIDTEXTPOS(m_ptSavedCaretPos);
120         }
121         if (m_bSelectionPushed == TRUE)
122         {
123                 popPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
124                 ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
125                 popPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
126                 ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
127         }
128
129         CPoint ptLastChange;
130         if (m_ptLastChangePushed.y == 0 && m_ptLastChangePushed.nToFirstReal > 0)
131                 ptLastChange = CPoint(-1,-1);
132         else 
133         {
134                 popPosition(m_ptLastChangePushed, ptLastChange);
135                 ASSERT_VALIDTEXTPOS(ptLastChange);
136         }
137         m_pGhostTextBuffer->RestoreLastChangePos(ptLastChange);
138 }
139
140 void CGhostTextView::
141 PushCursors ()
142 {
143         pushPosition(m_ptCursorPosPushed, m_ptCursorPos);
144         pushPosition(m_ptSelStartPushed, m_ptSelStart);
145         pushPosition(m_ptSelEndPushed, m_ptSelEnd);
146         pushPosition(m_ptAnchorPushed, m_ptAnchor);
147         if (m_bDraggingText == TRUE)
148         {
149                 pushPosition(m_ptDraggedTextBeginPushed, m_ptDraggedTextBegin);
150                 pushPosition(m_ptDraggedTextEndPushed, m_ptDraggedTextEnd);
151         }
152         if (m_bDropPosVisible == TRUE)
153                 pushPosition(m_ptSavedCaretPosPushed, m_ptSavedCaretPos);
154         if (m_bSelectionPushed == TRUE)
155         {
156                 pushPosition(m_ptSavedSelStartPushed, m_ptSavedSelStart);
157                 pushPosition(m_ptSavedSelEndPushed, m_ptSavedSelEnd);
158         }
159
160         pushPosition(m_ptLastChangePushed, m_pGhostTextBuffer->GetLastChangePos());
161 }
162
163
164
165
166 int CGhostTextView::ComputeRealLine (int nApparentLine) const
167 {
168         return m_pGhostTextBuffer->ComputeRealLine(nApparentLine);
169 }
170
171 int CGhostTextView::ComputeApparentLine (int nRealLine) const
172 {
173         return m_pGhostTextBuffer->ComputeApparentLine(nRealLine);
174 }
175
176 void CGhostTextView::GetTextWithoutEmptys (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString &text, int nCrlfStyle /*=CRLF_STYLE_AUTOMATIC*/ )
177 {
178   if (m_pGhostTextBuffer != NULL)
179     m_pGhostTextBuffer->GetTextWithoutEmptys (nStartLine, nStartChar, nEndLine, nEndChar, text, nCrlfStyle);
180   else
181     text = _T ("");
182 }