fixed warning C4748
[TortoiseGit.git] / src / TortoiseIDiff / PicWindow.h
blob885b07d9f55f7404b152e46e2c8fb360fb0df3de
1 // TortoiseIDiff - an image diff viewer in TortoiseSVN
3 // Copyright (C) 2006-2010 - 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.
19 #pragma once
20 #include "commctrl.h"
21 #include "BaseWindow.h"
22 #include "TortoiseIDiff.h"
23 #include "Picture.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
44 #ifndef GET_X_LPARAM
45 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
46 #endif
47 #ifndef GET_Y_LPARAM
48 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
49 #endif
51 /**
52 * \ingroup TortoiseIDiff
53 * The image view window.
54 * Shows an image and provides methods to scale the image or alpha blend it
55 * over another image.
57 class CPicWindow : public CWindow
59 public:
60 CPicWindow(HINSTANCE hInst, const WNDCLASSEX* wcx = NULL) : CWindow(hInst, wcx)
61 , bValid(false)
62 , nHScrollPos(0)
63 , nVScrollPos(0)
64 , picscale(1.0)
65 , transparentColor(::GetSysColor(COLOR_WINDOW))
66 , pSecondPic(NULL)
67 , blendAlpha(0.5f)
68 , bShowInfo(false)
69 , nDimensions(0)
70 , nCurrentDimension(1)
71 , nFrames(0)
72 , nCurrentFrame(1)
73 , bPlaying(false)
74 , pTheOtherPic(NULL)
75 , bLinkedPositions(true)
76 , bFitSizes(false)
77 , bOverlap(false)
78 , m_blend(BLEND_ALPHA)
79 , bMainPic(false)
80 , bFirstpaint(false)
81 , nVSecondScrollPos(0)
82 , nHSecondScrollPos(0)
83 , startVScrollPos(0)
84 , startHScrollPos(0)
85 , startVSecondScrollPos(0)
86 , startHSecondScrollPos(0)
88 SetWindowTitle(_T("Picture Window"));
89 m_lastTTPos.x = 0;
90 m_lastTTPos.y = 0;
93 enum BlendType
95 BLEND_ALPHA,
96 BLEND_XOR,
98 /// Registers the window class and creates the window
99 bool RegisterAndCreateWindow(HWND hParent);
101 /// Sets the image path and title to show
102 void SetPic(tstring path, tstring title, bool bFirst);
103 /// Returns the CPicture image object. Used to get an already loaded image
104 /// object without having to load it again.
105 CPicture * GetPic() {return &picture;}
106 /// Sets the path and title of the second image which is alpha blended over the original
107 void SetSecondPic(CPicture * pPicture = NULL, const tstring& sectit = _T(""), const tstring& secpath = _T(""), int hpos = 0, int vpos = 0)
109 pSecondPic = pPicture;
110 pictitle2 = sectit;
111 picpath2 = secpath;
112 nVSecondScrollPos = vpos;
113 nHSecondScrollPos = hpos;
116 void StopTimer() {KillTimer(*this, ID_ANIMATIONTIMER);}
118 /// Returns the currently used alpha blending value (0.0-1.0)
119 float GetBlendAlpha() const { return blendAlpha; }
120 /// Sets the alpha blending value
121 void SetBlendAlpha(BlendType type, float a)
123 m_blend = type;
124 blendAlpha = a;
125 if (m_AlphaSlider.IsValid())
126 SendMessage(m_AlphaSlider.GetWindow(), TBM_SETPOS, (WPARAM)1, (LPARAM)(a*16.0f));
127 PositionTrackBar();
128 InvalidateRect(*this, NULL, FALSE);
130 /// Toggle the alpha blending value
131 void ToggleAlpha()
133 if( 0.0f != GetBlendAlpha() )
134 SetBlendAlpha(m_blend, 0.0f);
135 else
136 SetBlendAlpha(m_blend, 1.0f);
139 /// Set the color that this PicWindow will display behind transparent images.
140 void SetTransparentColor(COLORREF back) { transparentColor = back; InvalidateRect(*this, NULL, false); }
142 /// Resizes the image to fit into the window. Small images are not enlarged.
143 void FitImageInWindow();
144 /// center the image in the view
145 void CenterImage();
146 /// Makes both images the same size, fitting into the window
147 void FitSizes(bool bFit);
148 /// Sets the zoom factor of the image
149 void SetZoom(double dZoom, bool centermouse);
150 /// Returns the currently used zoom factor in which the image is shown.
151 double GetZoom() {return picscale;}
152 /// Zooms in (true) or out (false) in nice steps
153 void Zoom(bool in, bool centermouse);
154 /// Sets the 'Other' pic window
155 void SetOtherPicWindow(CPicWindow * pWnd) {pTheOtherPic = pWnd;}
156 /// Links/Unlinks the two pic windows
157 void LinkPositions(bool bLink) {bLinkedPositions = bLink;}
158 /// Sets the overlay mode info
159 void SetOverlapMode(bool b) {bOverlap = b;}
161 void ShowInfo(bool bShow = true) {bShowInfo = bShow; InvalidateRect(*this, NULL, false);}
162 /// Sets up the scrollbars as needed
163 void SetupScrollBars();
165 bool HasMultipleImages();
167 int GetHPos() {return nHScrollPos;}
168 int GetVPos() {return nVScrollPos;}
169 void SetZoomValue(double z) {picscale = z; InvalidateRect(*this, NULL, FALSE);}
171 /// Handles the mouse wheel
172 void OnMouseWheel(short fwKeys, short zDelta);
173 protected:
174 /// the message handler for this window
175 LRESULT CALLBACK WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
176 /// Draws the view title bar
177 void DrawViewTitle(HDC hDC, RECT * rect);
178 /// Creates the image buttons
179 bool CreateButtons();
180 /// Handles vertical scrolling
181 void OnVScroll(UINT nSBCode, UINT nPos);
182 /// Handles horizontal scrolling
183 void OnHScroll(UINT nSBCode, UINT nPos);
184 /// Returns the client rectangle, without the scrollbars and the view title.
185 /// Basically the rectangle the image can use.
186 void GetClientRect(RECT * pRect);
187 /// Returns the client rectangle, without the view title but with the scrollbars
188 void GetClientRectWithScrollbars(RECT * pRect);
189 /// the WM_PAINT function
190 void Paint(HWND hwnd);
191 /// Draw pic to hdc, with a border, scaled by scale.
192 void ShowPicWithBorder(HDC hdc, const RECT &bounds, CPicture &pic, double scale);
193 /// Positions the buttons
194 void PositionChildren();
195 /// Rounds a double to a given precision
196 double RoundDouble(double doValue, int nPrecision);
197 /// advance to the next image in the file
198 void NextImage();
199 /// go back to the previous image in the file
200 void PrevImage();
201 /// starts/stops the animation
202 void Animate(bool bStart);
203 /// Creates the trackbar (the alpha blending slider control)
204 void CreateTrackbar(HWND hwndParent);
205 /// Moves the alpha slider trackbar to the correct position
206 void PositionTrackBar();
207 /// creates the info string used in the info box and the tooltips
208 void BuildInfoString(TCHAR * buf, int size, bool bTooltip);
210 tstring picpath; ///< the path to the image we show
211 tstring pictitle; ///< the string to show in the image view as a title
212 CPicture picture; ///< the picture object of the image
213 bool bValid; ///< true if the picture object is valid, i.e. if the image could be loaded and can be shown
214 double picscale; ///< the scale factor of the image
215 COLORREF transparentColor; ///< the color to draw under the images
216 bool bFirstpaint; ///< true if the image is painted the first time. Used to initialize some stuff when the window is valid for sure.
217 CPicture * pSecondPic; ///< if set, this is the picture to draw transparently above the original
218 CPicWindow * pTheOtherPic; ///< pointer to the other picture window. Used for "linking" the two windows when scrolling/zooming/...
219 bool bMainPic; ///< if true, this is the first image
220 bool bLinkedPositions; ///< if true, the two image windows are linked together for scrolling/zooming/...
221 bool bFitSizes; ///< if true, the two image windows are always zoomed so they match their size
222 bool bOverlap; ///< true if the overlay mode is active
223 BlendType m_blend; ///< type of blending to use
224 tstring pictitle2; ///< the title of the second picture
225 tstring picpath2; ///< the path of the second picture
226 float blendAlpha; ///<the alpha value for transparency blending
227 bool bShowInfo; ///< true if the info rectangle of the image should be shown
228 TCHAR m_wszTip[8192];
229 char m_szTip[8192];
230 POINT m_lastTTPos;
231 HWND hwndTT;
232 // scrollbar info
233 int nVScrollPos; ///< vertical scroll position
234 int nHScrollPos; ///< horizontal scroll position
235 int nVSecondScrollPos; ///< vertical scroll position of second pic at the moment of enabling overlap mode
236 int nHSecondScrollPos; ///< horizontal scroll position of second pic at the moment of enabling overlap mode
237 POINT ptPanStart; ///< the point of the last mouse click
238 int startVScrollPos; ///< the vertical scroll position when panning starts
239 int startHScrollPos; ///< the horizontal scroll position when panning starts
240 int startVSecondScrollPos; ///< the vertical scroll position of the second pic when panning starts
241 int startHSecondScrollPos; ///< the horizontal scroll position of the second pic when panning starts
242 // image frames/dimensions
243 UINT nDimensions;
244 UINT nCurrentDimension;
245 UINT nFrames;
246 UINT nCurrentFrame;
248 // controls
249 HWND hwndLeftBtn;
250 HWND hwndRightBtn;
251 HWND hwndPlayBtn;
252 CNiceTrackbar m_AlphaSlider;
253 HWND hwndAlphaToggleBtn;
254 HICON hLeft;
255 HICON hRight;
256 HICON hPlay;
257 HICON hStop;
258 HICON hAlphaToggle;
259 bool bPlaying;
260 RECT m_inforect;