1 // TortoiseIDiff - an image diff viewer in TortoiseSVN
3 // Copyright (C) 2006-2010, 2012 - 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
39 #define TRACKBAR_ID 101
40 #define SLIDER_HEIGHT 30
41 #define SLIDER_WIDTH 30
45 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
48 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
52 * \ingroup TortoiseIDiff
53 * The image view window.
54 * Shows an image and provides methods to scale the image or alpha blend it
57 class CPicWindow
: public CWindow
60 CPicWindow() : CWindow(NULL
) {}
62 CPicWindow(HINSTANCE hInst
, const WNDCLASSEX
* wcx
= NULL
) : CWindow(hInst
, wcx
)
67 , transparentColor(::GetSysColor(COLOR_WINDOW
))
72 , nCurrentDimension(1)
77 , bLinkedPositions(true)
80 , m_blend(BLEND_ALPHA
)
83 , nVSecondScrollPos(0)
84 , nHSecondScrollPos(0)
87 , startVSecondScrollPos(0)
88 , startHSecondScrollPos(0)
93 , hwndAlphaToggleBtn(0)
100 SetWindowTitle(_T("Picture Window"));
114 /// Registers the window class and creates the window
115 bool RegisterAndCreateWindow(HWND hParent
);
117 /// Sets the image path and title to show
118 void SetPic(tstring path
, tstring title
, bool bFirst
);
119 /// Returns the CPicture image object. Used to get an already loaded image
120 /// object without having to load it again.
121 CPicture
* GetPic() {return &picture
;}
122 /// Sets the path and title of the second image which is alpha blended over the original
123 void SetSecondPic(CPicture
* pPicture
= NULL
, const tstring
& sectit
= _T(""), const tstring
& secpath
= _T(""), int hpos
= 0, int vpos
= 0)
125 pSecondPic
= pPicture
;
128 nVSecondScrollPos
= vpos
;
129 nHSecondScrollPos
= hpos
;
132 void StopTimer() {KillTimer(*this, ID_ANIMATIONTIMER
);}
134 /// Returns the currently used alpha blending value (0.0-1.0)
135 float GetBlendAlpha() const { return blendAlpha
; }
136 /// Sets the alpha blending value
137 void SetBlendAlpha(BlendType type
, float a
)
141 if (m_AlphaSlider
.IsValid())
142 SendMessage(m_AlphaSlider
.GetWindow(), TBM_SETPOS
, (WPARAM
)1, (LPARAM
)(a
*16.0f
));
144 InvalidateRect(*this, NULL
, FALSE
);
146 /// Toggle the alpha blending value
149 if( 0.0f
!= GetBlendAlpha() )
150 SetBlendAlpha(m_blend
, 0.0f
);
152 SetBlendAlpha(m_blend
, 1.0f
);
155 /// Set the color that this PicWindow will display behind transparent images.
156 void SetTransparentColor(COLORREF back
) { transparentColor
= back
; InvalidateRect(*this, NULL
, false); }
158 /// Resizes the image to fit into the window. Small images are not enlarged.
159 void FitImageInWindow();
160 /// center the image in the view
162 /// Makes both images the same size, fitting into the window
163 void FitSizes(bool bFit
);
164 /// Sets the zoom factor of the image
165 void SetZoom(double dZoom
, bool centermouse
);
166 /// Returns the currently used zoom factor in which the image is shown.
167 double GetZoom() {return picscale
;}
168 /// Zooms in (true) or out (false) in nice steps
169 void Zoom(bool in
, bool centermouse
);
170 /// Sets the 'Other' pic window
171 void SetOtherPicWindow(CPicWindow
* pWnd
) {pTheOtherPic
= pWnd
;}
172 /// Links/Unlinks the two pic windows
173 void LinkPositions(bool bLink
) {bLinkedPositions
= bLink
;}
174 /// Sets the overlay mode info
175 void SetOverlapMode(bool b
) {bOverlap
= b
;}
177 void ShowInfo(bool bShow
= true) {bShowInfo
= bShow
; InvalidateRect(*this, NULL
, false);}
178 /// Sets up the scrollbars as needed
179 void SetupScrollBars();
181 bool HasMultipleImages();
183 int GetHPos() {return nHScrollPos
;}
184 int GetVPos() {return nVScrollPos
;}
185 void SetZoomValue(double z
) {picscale
= z
; InvalidateRect(*this, NULL
, FALSE
);}
187 /// Handles the mouse wheel
188 void OnMouseWheel(short fwKeys
, short zDelta
);
190 /// the message handler for this window
191 LRESULT CALLBACK
WinMsgHandler(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
192 /// Draws the view title bar
193 void DrawViewTitle(HDC hDC
, RECT
* rect
);
194 /// Creates the image buttons
195 bool CreateButtons();
196 /// Handles vertical scrolling
197 void OnVScroll(UINT nSBCode
, UINT nPos
);
198 /// Handles horizontal scrolling
199 void OnHScroll(UINT nSBCode
, UINT nPos
);
200 /// Returns the client rectangle, without the scrollbars and the view title.
201 /// Basically the rectangle the image can use.
202 void GetClientRect(RECT
* pRect
);
203 /// Returns the client rectangle, without the view title but with the scrollbars
204 void GetClientRectWithScrollbars(RECT
* pRect
);
205 /// the WM_PAINT function
206 void Paint(HWND hwnd
);
207 /// Draw pic to hdc, with a border, scaled by scale.
208 void ShowPicWithBorder(HDC hdc
, const RECT
&bounds
, CPicture
&pic
, double scale
);
209 /// Positions the buttons
210 void PositionChildren();
211 /// Rounds a double to a given precision
212 double RoundDouble(double doValue
, int nPrecision
);
213 /// advance to the next image in the file
215 /// go back to the previous image in the file
217 /// starts/stops the animation
218 void Animate(bool bStart
);
219 /// Creates the trackbar (the alpha blending slider control)
220 void CreateTrackbar(HWND hwndParent
);
221 /// Moves the alpha slider trackbar to the correct position
222 void PositionTrackBar();
223 /// creates the info string used in the info box and the tooltips
224 void BuildInfoString(TCHAR
* buf
, int size
, bool bTooltip
);
226 tstring picpath
; ///< the path to the image we show
227 tstring pictitle
; ///< the string to show in the image view as a title
228 CPicture picture
; ///< the picture object of the image
229 bool bValid
; ///< true if the picture object is valid, i.e. if the image could be loaded and can be shown
230 double picscale
; ///< the scale factor of the image
231 COLORREF transparentColor
; ///< the color to draw under the images
232 bool bFirstpaint
; ///< true if the image is painted the first time. Used to initialize some stuff when the window is valid for sure.
233 CPicture
* pSecondPic
; ///< if set, this is the picture to draw transparently above the original
234 CPicWindow
* pTheOtherPic
; ///< pointer to the other picture window. Used for "linking" the two windows when scrolling/zooming/...
235 bool bMainPic
; ///< if true, this is the first image
236 bool bLinkedPositions
; ///< if true, the two image windows are linked together for scrolling/zooming/...
237 bool bFitSizes
; ///< if true, the two image windows are always zoomed so they match their size
238 bool bOverlap
; ///< true if the overlay mode is active
239 BlendType m_blend
; ///< type of blending to use
240 tstring pictitle2
; ///< the title of the second picture
241 tstring picpath2
; ///< the path of the second picture
242 float blendAlpha
; ///<the alpha value for transparency blending
243 bool bShowInfo
; ///< true if the info rectangle of the image should be shown
244 TCHAR m_wszTip
[8192];
249 int nVScrollPos
; ///< vertical scroll position
250 int nHScrollPos
; ///< horizontal scroll position
251 int nVSecondScrollPos
; ///< vertical scroll position of second pic at the moment of enabling overlap mode
252 int nHSecondScrollPos
; ///< horizontal scroll position of second pic at the moment of enabling overlap mode
253 POINT ptPanStart
; ///< the point of the last mouse click
254 int startVScrollPos
; ///< the vertical scroll position when panning starts
255 int startHScrollPos
; ///< the horizontal scroll position when panning starts
256 int startVSecondScrollPos
; ///< the vertical scroll position of the second pic when panning starts
257 int startHSecondScrollPos
; ///< the horizontal scroll position of the second pic when panning starts
258 // image frames/dimensions
260 UINT nCurrentDimension
;
268 CNiceTrackbar m_AlphaSlider
;
269 HWND hwndAlphaToggleBtn
;