1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2003-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "SVNLineDiff.h"
22 #include "ScrollTool.h"
24 #include "LocatorBar.h"
27 * \ingroup TortoiseMerge
29 * View class providing the basic functionality for
30 * showing diffs. Has three parent classes which inherit
31 * from this base class: CLeftView, CRightView and CBottomView.
33 class CBaseView
: public CView
35 DECLARE_DYNCREATE(CBaseView
)
36 friend class CLineDiffBar
;
43 * Indicates that the underlying document has been updated. Reloads all
44 * data and redraws the view.
46 virtual void DocumentUpdated();
48 * Returns the number of lines visible on the view.
52 * Scrolls the view to the given line.
53 * \param nNewTopLine The new top line to scroll the view to
54 * \param bTrackScrollBar If TRUE, then the scrollbars are affected too.
56 void ScrollToLine(int nNewTopLine
, BOOL bTrackScrollBar
= TRUE
);
57 void ScrollAllToLine(int nNewTopLine
, BOOL bTrackScrollBar
= TRUE
);
58 void ScrollSide(int delta
);
59 void GoToLine(int nNewLine
, BOOL bAll
= TRUE
);
60 void ScrollToChar(int nNewOffsetChar
, BOOL bTrackScrollBar
= TRUE
);
61 void UseCaret(bool bUse
= true) {m_bCaretHidden
= !bUse
;}
62 bool HasCaret() {return !m_bCaretHidden
;}
63 void SetCaretPosition(POINT pt
) {m_ptCaretPos
= pt
; m_nCaretGoalPos
= pt
.x
; UpdateCaret();}
64 void EnsureCaretVisible();
66 void ClearSelection();
69 void SelectLines(int nLine1
, int nLine2
= -1);
70 void HiglightLines(int start
, int end
= -1);
71 inline BOOL
IsHidden() const {return m_bIsHidden
;}
72 inline void SetHidden(BOOL bHidden
) {m_bIsHidden
= bHidden
;}
73 inline BOOL
IsModified() const {return m_bModified
;}
74 void SetModified(BOOL bModified
= TRUE
) {m_bModified
= bModified
;}
75 BOOL
HasSelection() {return (!((m_nSelBlockEnd
< 0)||(m_nSelBlockStart
< 0)||(m_nSelBlockStart
> m_nSelBlockEnd
)));}
76 BOOL
HasTextSelection() {return ((m_ptSelectionStartPos
.x
!= m_ptSelectionEndPos
.x
)||(m_ptSelectionStartPos
.y
!= m_ptSelectionEndPos
.y
));}
77 BOOL
GetSelection(int& start
, int& end
) {start
=m_nSelBlockStart
; end
=m_nSelBlockEnd
; return HasSelection();}
78 void SetInlineWordDiff(bool bWord
) {m_bInlineWordDiff
= bWord
;}
80 BOOL
IsLineRemoved(int nLineIndex
);
81 bool IsBlockWhitespaceOnly(int nLineIndex
, bool& bIdentical
);
82 bool IsLineConflicted(int nLineIndex
);
84 CViewData
* m_pViewData
;
85 CViewData
* m_pOtherViewData
;
87 CString m_sWindowName
; ///< The name of the view which is shown as a window title to the user
88 CString m_sFullFilePath
; ///< The full path of the file shown
89 CFileTextLines::UnicodeType texttype
; ///< the text encoding this view uses
90 EOL lineendings
; ///< the line endings the view uses
92 BOOL m_bViewWhitespace
; ///< If TRUE, then SPACE and TAB are shown as special characters
93 BOOL m_bShowInlineDiff
; ///< If TRUE, diffs in lines are marked colored
94 bool m_bShowSelection
; ///< If true, selection bars are shown and selected text darkened
95 int m_nTopLine
; ///< The topmost text line in the view
97 static CLocatorBar
* m_pwndLocator
; ///< Pointer to the locator bar on the left
98 static CLineDiffBar
* m_pwndLineDiffBar
; ///< Pointer to the line diff bar at the bottom
99 static CMFCStatusBar
* m_pwndStatusBar
;///< Pointer to the status bar
100 static CMainFrame
* m_pMainFrame
; ///< Pointer to the mainframe
102 void GoToFirstDifference();
103 void AddEmptyLine(int nLineIndex
);
105 virtual BOOL
PreCreateWindow(CREATESTRUCT
& cs
);
106 virtual void OnDraw(CDC
* pDC
);
107 virtual INT_PTR
OnToolHitTest(CPoint point
, TOOLINFO
* pTI
) const;
108 BOOL
OnToolTipNotify(UINT id
, NMHDR
*pNMHDR
, LRESULT
*pResult
);
109 afx_msg
void OnHScroll(UINT nSBCode
, UINT nPos
, CScrollBar
* pScrollBar
);
110 afx_msg
void OnVScroll(UINT nSBCode
, UINT nPos
, CScrollBar
* pScrollBar
);
111 afx_msg BOOL
OnEraseBkgnd(CDC
* pDC
);
112 afx_msg
int OnCreate(LPCREATESTRUCT lpCreateStruct
);
113 afx_msg
void OnDestroy();
114 afx_msg
void OnSize(UINT nType
, int cx
, int cy
);
115 afx_msg BOOL
OnMouseWheel(UINT nFlags
, short zDelta
, CPoint pt
);
116 afx_msg BOOL
OnSetCursor(CWnd
* pWnd
, UINT nHitTest
, UINT message
);
117 afx_msg
void OnKillFocus(CWnd
* pNewWnd
);
118 afx_msg
void OnSetFocus(CWnd
* pOldWnd
);
119 afx_msg
void OnContextMenu(CWnd
* pWnd
, CPoint point
);
120 afx_msg
void OnMergeNextdifference();
121 afx_msg
void OnMergePreviousdifference();
122 afx_msg
void OnMergePreviousconflict();
123 afx_msg
void OnMergeNextconflict();
124 afx_msg
void OnKeyDown(UINT nChar
, UINT nRepCnt
, UINT nFlags
);
125 afx_msg
void OnLButtonDown(UINT nFlags
, CPoint point
);
126 afx_msg
void OnEditCopy();
127 afx_msg
void OnMouseMove(UINT nFlags
, CPoint point
);
128 afx_msg
void OnTimer(UINT_PTR nIDEvent
);
129 afx_msg
void OnMouseLeave();
130 afx_msg
void OnChar(UINT nChar
, UINT nRepCnt
, UINT nFlags
);
131 afx_msg
void OnCaretDown();
132 afx_msg
void OnCaretLeft();
133 afx_msg
void OnCaretRight();
134 afx_msg
void OnCaretUp();
135 afx_msg
void OnCaretWordleft();
136 afx_msg
void OnCaretWordright();
137 afx_msg
void OnEditCut();
138 afx_msg
void OnEditPaste();
139 afx_msg
void OnEditSelectall();
141 DECLARE_MESSAGE_MAP()
144 void DrawHeader(CDC
*pdc
, const CRect
&rect
);
145 void DrawMargin(CDC
*pdc
, const CRect
&rect
, int nLineIndex
);
146 void DrawSingleLine(CDC
*pDC
, const CRect
&rc
, int nLineIndex
);
147 bool DrawInlineDiff(CDC
*pDC
, const CRect
&rc
, int nLineIndex
, const CString
&line
, CPoint
&origin
);
149 * Draws the horizontal lines around current diff block or selection block.
151 void DrawBlockLine(CDC
*pDC
, const CRect
&rc
, int nLineIndex
);
153 * Draws the line ending 'char'.
155 void DrawLineEnding(CDC
*pDC
, const CRect
&rc
, int nLineIndex
, const CPoint
& origin
);
156 void ExpandChars(LPCTSTR pszChars
, int nOffset
, int nCount
, CString
&line
);
158 void RecalcVertScrollBar(BOOL bPositionOnly
= FALSE
);
159 void RecalcAllVertScrollBars(BOOL bPositionOnly
= FALSE
);
160 void RecalcHorzScrollBar(BOOL bPositionOnly
= FALSE
);
161 void RecalcAllHorzScrollBars(BOOL bPositionOnly
= FALSE
);
163 void OnDoMouseWheel(UINT nFlags
, short zDelta
, CPoint pt
);
164 void OnDoHScroll(UINT nSBCode
, UINT nPos
, CScrollBar
* pScrollBar
, CBaseView
* master
);
165 void OnDoVScroll(UINT nSBCode
, UINT nPos
, CScrollBar
* pScrollBar
, CBaseView
* master
);
167 void SetupSelection(int start
, int end
);
168 void ShowDiffLines(int nLine
);
170 int GetTabSize() const {return m_nTabSize
;}
172 int GetLineActualLength(int index
) const;
173 int GetLineCount() const;
174 void CalcLineCharDim();
177 int GetMaxLineLength();
178 int GetLineLength(int index
) const;
179 int GetScreenChars();
180 int GetAllMinScreenChars() const;
181 int GetAllMaxLineLength() const;
182 int GetAllLineCount() const;
183 int GetAllMinScreenLines() const;
184 LPCTSTR
GetLineChars(int index
) const;
185 int GetLineNumber(int index
) const;
186 CFont
* GetFont(BOOL bItalic
= FALSE
, BOOL bBold
= FALSE
, BOOL bStrikeOut
= FALSE
);
187 int GetLineFromPoint(CPoint point
);
188 int GetMarginWidth();
189 COLORREF
IntenseColor(long scale
, COLORREF col
);
190 COLORREF
InlineDiffColor(int nLineIndex
);
191 void CheckOtherView();
192 static CString
GetWhitespaceBlock(CViewData
*viewData
, int nLineIndex
);
194 /// Returns true if selection should be kept
195 virtual bool OnContextMenu(CPoint point
, int nLine
, DiffStates state
);
197 * Updates the status bar pane. Call this if the document changed.
199 void UpdateStatusBar();
201 void UseTheirAndYourBlock(viewstate
&rightstate
, viewstate
&bottomstate
, viewstate
&leftstate
);
202 void UseYourAndTheirBlock(viewstate
&rightstate
, viewstate
&bottomstate
, viewstate
&leftstate
);
203 void UseBothLeftFirst(viewstate
&rightstate
, viewstate
&leftstate
);
204 void UseBothRightFirst(viewstate
&rightstate
, viewstate
&leftstate
);
206 bool IsLeftViewGood() const {return ((m_pwndLeft
)&&(m_pwndLeft
->IsWindowVisible()));}
207 bool IsRightViewGood() const {return ((m_pwndRight
)&&(m_pwndRight
->IsWindowVisible()));}
208 bool IsBottomViewGood() const {return ((m_pwndBottom
)&&(m_pwndBottom
->IsWindowVisible()));}
210 int CalculateActualOffset(int nLineIndex
, int nCharIndex
) const;
211 int CalculateCharIndex(int nLineIndex
, int nActualOffset
) const;
212 POINT
TextToClient(const POINT
& point
);
213 void DrawText(CDC
* pDC
, const CRect
&rc
, LPCTSTR text
, int textlength
, int nLineIndex
, POINT coords
, bool bModified
, bool bInlineDiff
);
214 void ClearCurrentSelection();
215 void AdjustSelection();
216 void SelectNextBlock(int nDirection
, bool bConflict
, bool bSkipEndOfCurrentBlock
= true);
218 void RemoveLine(int nLineIndex
);
219 void RemoveSelectedText();
221 void AddUndoLine(int nLine
, bool bAddEmptyLine
= false);
223 bool MoveCaretLeft();
224 bool MoveCaretRight();
225 void UpdateGoalPos();
227 bool IsWordSeparator(wchar_t ch
) const;
228 bool IsCaretAtWordBoundary() const;
231 COLORREF m_InlineRemovedBk
;
232 COLORREF m_InlineAddedBk
;
233 COLORREF m_ModifiedBk
;
234 COLORREF m_WhiteSpaceFg
;
235 UINT m_nStatusBarID
; ///< The ID of the status bar pane used by this view. Must be set by the parent class.
237 SVNLineDiff m_svnlinediff
;
238 BOOL m_bOtherDiffChecked
;
241 BOOL m_bViewLinenumbers
;
247 int m_nMaxLineLength
;
253 bool m_bInlineWordDiff
;
255 int m_nSelBlockStart
;
263 POINT m_ptSelectionStartPos
;
264 POINT m_ptSelectionEndPos
;
265 POINT m_ptSelectionOrigin
;
269 HICON m_hRemovedIcon
;
270 HICON m_hConflictedIcon
;
271 HICON m_hConflictedIgnoredIcon
;
272 HICON m_hWhitespaceBlockIcon
;
276 HICON m_hLineEndingCR
;
277 HICON m_hLineEndingCRLF
;
278 HICON m_hLineEndingLF
;
280 LOGFONT m_lfBaseFont
;
281 CFont
* m_apFonts
[8];
282 CString m_sConflictedText
;
285 CBitmap
* m_pCacheBitmap
;
287 CScrollTool m_ScrollTool
;
288 CString m_sWordSeparators
;
290 char m_szTip
[MAX_PATH
*2+1];
291 wchar_t m_wszTip
[MAX_PATH
*2+1];
292 // These three pointers lead to the three parent
293 // classes CLeftView, CRightView and CBottomView
294 // and are used for the communication between
295 // the views (e.g. synchronized scrolling, ...)
296 // To find out which parent class this object
297 // is made of just compare e.g. (m_pwndLeft==this).
298 static CBaseView
* m_pwndLeft
; ///< Pointer to the left view. Must be set by the CLeftView parent class.
299 static CBaseView
* m_pwndRight
; ///< Pointer to the right view. Must be set by the CRightView parent class.
300 static CBaseView
* m_pwndBottom
; ///< Pointer to the bottom view. Must be set by the CBottomView parent class.