1 // TortoiseIDiff - an image diff viewer in TortoiseSVN
3 // Copyright (C) 2006-2010, 2012-2013 - 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 "BaseWindow.h"
22 #include "TortoiseIDiff.h"
24 #include "NiceTrackbar.h"
26 #define HEADER_HEIGHT 30
28 #define ID_ANIMATIONTIMER 100
29 #define TIMER_ALPHASLIDER 101
30 #define ID_ALPHATOGGLETIMER 102
32 #define LEFTBUTTON_ID 101
33 #define RIGHTBUTTON_ID 102
34 #define PLAYBUTTON_ID 103
35 #define ALPHATOGGLEBUTTON_ID 104
36 #define BLENDALPHA_ID 105
37 #define BLENDXOR_ID 106
38 #define SELECTBUTTON_ID 107
40 #define TRACKBAR_ID 101
41 #define SLIDER_HEIGHT 30
42 #define SLIDER_WIDTH 30
46 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
49 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
53 * \ingroup TortoiseIDiff
54 * The image view window.
55 * Shows an image and provides methods to scale the image or alpha blend it
58 class CPicWindow
: public CWindow
61 CPicWindow() : CWindow(NULL
) {}
63 CPicWindow(HINSTANCE hInst
, const WNDCLASSEX
* wcx
= NULL
) : CWindow(hInst
, wcx
)
68 , transparentColor(::GetSysColor(COLOR_WINDOW
))
73 , nCurrentDimension(1)
78 , bLinkedPositions(true)
82 , m_blend(BLEND_ALPHA
)
85 , nVSecondScrollPos(0)
86 , nHSecondScrollPos(0)
89 , startVSecondScrollPos(0)
90 , startHSecondScrollPos(0)
96 , hwndAlphaToggleBtn(0)
105 , bSelectionMode(false)
107 SetWindowTitle(_T("Picture Window"));
121 /// Registers the window class and creates the window
122 bool RegisterAndCreateWindow(HWND hParent
);
124 /// Sets the image path and title to show
125 void SetPic(tstring path
, tstring title
, bool bFirst
);
126 /// Returns the CPicture image object. Used to get an already loaded image
127 /// object without having to load it again.
128 CPicture
* GetPic() {return &picture
;}
129 /// Sets the path and title of the second image which is alpha blended over the original
130 void SetSecondPic(CPicture
* pPicture
= NULL
, const tstring
& sectit
= _T(""), const tstring
& secpath
= _T(""), int hpos
= 0, int vpos
= 0)
132 pSecondPic
= pPicture
;
135 nVSecondScrollPos
= vpos
;
136 nHSecondScrollPos
= hpos
;
139 void StopTimer() {KillTimer(*this, ID_ANIMATIONTIMER
);}
141 /// Returns the currently used alpha blending value (0.0-1.0)
142 float GetBlendAlpha() const { return blendAlpha
; }
143 /// Sets the alpha blending value
144 void SetBlendAlpha(BlendType type
, float a
)
148 if (m_AlphaSlider
.IsValid())
149 SendMessage(m_AlphaSlider
.GetWindow(), TBM_SETPOS
, (WPARAM
)1, (LPARAM
)(a
*16.0f
));
151 InvalidateRect(*this, NULL
, FALSE
);
153 /// Toggle the alpha blending value
156 if( 0.0f
!= GetBlendAlpha() )
157 SetBlendAlpha(m_blend
, 0.0f
);
159 SetBlendAlpha(m_blend
, 1.0f
);
162 /// Set the color that this PicWindow will display behind transparent images.
163 void SetTransparentColor(COLORREF back
) { transparentColor
= back
; InvalidateRect(*this, NULL
, false); }
165 /// Resizes the image to fit into the window. Small images are not enlarged.
166 void FitImageInWindow();
167 /// center the image in the view
169 /// forces the widths of the images to be the same
170 void FitWidths(bool bFit
);
171 /// forces the heights of the images to be the same
172 void FitHeights(bool bFit
);
173 /// Sets the zoom factor of the image
174 void SetZoom(int Zoom
, bool centermouse
, bool inzoom
= false);
175 /// Returns the currently used zoom factor in which the image is shown.
176 int GetZoom() {return picscale
;}
177 /// Zooms in (true) or out (false) in nice steps
178 void Zoom(bool in
, bool centermouse
);
179 /// Sets the 'Other' pic window
180 void SetOtherPicWindow(CPicWindow
* pWnd
) {pTheOtherPic
= pWnd
;}
181 /// Links/Unlinks the two pic windows
182 void LinkPositions(bool bLink
) {bLinkedPositions
= bLink
;}
183 /// Sets the overlay mode info
184 void SetOverlapMode(bool b
) {bOverlap
= b
;}
186 void ShowInfo(bool bShow
= true) {bShowInfo
= bShow
; InvalidateRect(*this, NULL
, false);}
187 /// Sets up the scrollbars as needed
188 void SetupScrollBars();
190 bool HasMultipleImages();
192 int GetHPos() {return nHScrollPos
;}
193 int GetVPos() {return nVScrollPos
;}
194 void SetZoomValue(int z
) {picscale
= z
; InvalidateRect(*this, NULL
, FALSE
);}
196 void SetSelectionMode(bool bSelect
= true) { bSelectionMode
= bSelect
; }
197 /// Handles the mouse wheel
198 void OnMouseWheel(short fwKeys
, short zDelta
);
200 /// the message handler for this window
201 LRESULT CALLBACK
WinMsgHandler(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
202 /// Draws the view title bar
203 void DrawViewTitle(HDC hDC
, RECT
* rect
);
204 /// Creates the image buttons
205 bool CreateButtons();
206 /// Handles vertical scrolling
207 void OnVScroll(UINT nSBCode
, UINT nPos
);
208 /// Handles horizontal scrolling
209 void OnHScroll(UINT nSBCode
, UINT nPos
);
210 /// Returns the client rectangle, without the scrollbars and the view title.
211 /// Basically the rectangle the image can use.
212 void GetClientRect(RECT
* pRect
);
213 /// Returns the client rectangle, without the view title but with the scrollbars
214 void GetClientRectWithScrollbars(RECT
* pRect
);
215 /// the WM_PAINT function
216 void Paint(HWND hwnd
);
217 /// Draw pic to hdc, with a border, scaled by scale.
218 void ShowPicWithBorder(HDC hdc
, const RECT
&bounds
, CPicture
&pic
, int scale
);
219 /// Positions the buttons
220 void PositionChildren();
221 /// advance to the next image in the file
223 /// go back to the previous image in the file
225 /// starts/stops the animation
226 void Animate(bool bStart
);
227 /// Creates the trackbar (the alpha blending slider control)
228 void CreateTrackbar(HWND hwndParent
);
229 /// Moves the alpha slider trackbar to the correct position
230 void PositionTrackBar();
231 /// creates the info string used in the info box and the tooltips
232 void BuildInfoString(TCHAR
* buf
, int size
, bool bTooltip
);
233 /// adjusts the zoom to fit the specified width
234 void SetZoomToWidth(long width
);
235 /// adjusts the zoom to fit the specified height
236 void SetZoomToHeight(long height
);
239 tstring picpath
; ///< the path to the image we show
240 tstring pictitle
; ///< the string to show in the image view as a title
241 CPicture picture
; ///< the picture object of the image
242 bool bValid
; ///< true if the picture object is valid, i.e. if the image could be loaded and can be shown
243 int picscale
; ///< the scale factor of the image in percent
244 COLORREF transparentColor
; ///< the color to draw under the images
245 bool bFirstpaint
; ///< true if the image is painted the first time. Used to initialize some stuff when the window is valid for sure.
246 CPicture
* pSecondPic
; ///< if set, this is the picture to draw transparently above the original
247 CPicWindow
* pTheOtherPic
; ///< pointer to the other picture window. Used for "linking" the two windows when scrolling/zooming/...
248 bool bMainPic
; ///< if true, this is the first image
249 bool bLinkedPositions
; ///< if true, the two image windows are linked together for scrolling/zooming/...
250 bool bFitWidths
; ///< if true, the two image windows are shown with the same width
251 bool bFitHeights
; ///< if true, the two image windows are shown with the same height
252 bool bOverlap
; ///< true if the overlay mode is active
253 bool bDragging
; ///< indicates an ongoing dragging operation
254 BlendType m_blend
; ///< type of blending to use
255 tstring pictitle2
; ///< the title of the second picture
256 tstring picpath2
; ///< the path of the second picture
257 float blendAlpha
; ///<the alpha value for transparency blending
258 bool bShowInfo
; ///< true if the info rectangle of the image should be shown
259 TCHAR m_wszTip
[8192];
263 bool bSelectionMode
; ///< true if TortoiseIDiff is in selection mode, used to resolve conflicts
265 int nVScrollPos
; ///< vertical scroll position
266 int nHScrollPos
; ///< horizontal scroll position
267 int nVSecondScrollPos
; ///< vertical scroll position of second pic at the moment of enabling overlap mode
268 int nHSecondScrollPos
; ///< horizontal scroll position of second pic at the moment of enabling overlap mode
269 POINT ptPanStart
; ///< the point of the last mouse click
270 int startVScrollPos
; ///< the vertical scroll position when panning starts
271 int startHScrollPos
; ///< the horizontal scroll position when panning starts
272 int startVSecondScrollPos
; ///< the vertical scroll position of the second pic when panning starts
273 int startHSecondScrollPos
; ///< the horizontal scroll position of the second pic when panning starts
274 // image frames/dimensions
276 UINT nCurrentDimension
;
285 CNiceTrackbar m_AlphaSlider
;
286 HWND hwndAlphaToggleBtn
;
295 // linked image sizes/positions