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