Fixed issue #2495: "Show Reflog" dialog shows empty action for "push" entries
[TortoiseGit.git] / src / TortoiseIDiff / PicWindow.cpp
blobd91c563c7399b0a16db5e92434093718b4f04e61
1 // TortoiseIDiff - an image diff viewer in TortoiseSVN
3 // Copyright (C) 2006-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.
19 #include "stdafx.h"
20 #include <shellapi.h>
21 #include <CommCtrl.h>
22 #include "PicWindow.h"
23 #include <math.h>
24 #include "SysInfo.h"
25 #include <memory>
27 #pragma comment(lib, "Msimg32.lib")
28 #pragma comment(lib, "shell32.lib")
30 bool CPicWindow::RegisterAndCreateWindow(HWND hParent)
32 WNDCLASSEX wcx;
34 // Fill in the window class structure with default parameters
35 wcx.cbSize = sizeof(WNDCLASSEX);
36 wcx.style = CS_HREDRAW | CS_VREDRAW;
37 wcx.lpfnWndProc = CWindow::stWinMsgHandler;
38 wcx.cbClsExtra = 0;
39 wcx.cbWndExtra = 0;
40 wcx.hInstance = hResource;
41 wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
42 wcx.lpszClassName = _T("TortoiseGitIDiffPicWindow");
43 wcx.hIcon = LoadIcon(hResource, MAKEINTRESOURCE(IDI_TORTOISEIDIFF));
44 wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
45 wcx.lpszMenuName = MAKEINTRESOURCE(IDC_TORTOISEIDIFF);
46 wcx.hIconSm = LoadIcon(wcx.hInstance, MAKEINTRESOURCE(IDI_TORTOISEIDIFF));
47 RegisterWindow(&wcx);
48 if (CreateEx(WS_EX_ACCEPTFILES | WS_EX_CLIENTEDGE, WS_CHILD | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE, hParent))
50 ShowWindow(m_hwnd, SW_SHOW);
51 UpdateWindow(m_hwnd);
52 CreateButtons();
53 return true;
55 return false;
58 void CPicWindow::PositionTrackBar()
60 RECT rc;
61 GetClientRect(&rc);
62 HWND slider = m_AlphaSlider.GetWindow();
63 if ((pSecondPic)&&(m_blend == BLEND_ALPHA))
65 MoveWindow(slider, 0, rc.top-4+SLIDER_WIDTH, SLIDER_WIDTH, rc.bottom-rc.top-SLIDER_WIDTH+8, true);
66 ShowWindow(slider, SW_SHOW);
67 MoveWindow(hwndAlphaToggleBtn, 0, rc.top-4, SLIDER_WIDTH, SLIDER_WIDTH, true);
68 ShowWindow(hwndAlphaToggleBtn, SW_SHOW);
70 else
72 ShowWindow(slider, SW_HIDE);
73 ShowWindow(hwndAlphaToggleBtn, SW_HIDE);
77 LRESULT CALLBACK CPicWindow::WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
79 TRACKMOUSEEVENT mevt;
80 switch (uMsg)
82 case WM_CREATE:
84 // create a slider control
85 CreateTrackbar(hwnd);
86 ShowWindow(m_AlphaSlider.GetWindow(), SW_HIDE);
87 //Create the tooltips
88 TOOLINFO ti;
89 RECT rect; // for client area coordinates
91 hwndTT = CreateWindowEx(WS_EX_TOPMOST,
92 TOOLTIPS_CLASS,
93 NULL,
94 WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
95 CW_USEDEFAULT,
96 CW_USEDEFAULT,
97 CW_USEDEFAULT,
98 CW_USEDEFAULT,
99 hwnd,
100 NULL,
101 hResource,
102 NULL
105 SetWindowPos(hwndTT,
106 HWND_TOPMOST,
111 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
113 ::GetClientRect(hwnd, &rect);
115 ti.cbSize = sizeof(TOOLINFO);
116 ti.uFlags = TTF_TRACK | TTF_ABSOLUTE;
117 ti.hwnd = hwnd;
118 ti.hinst = hResource;
119 ti.uId = 0;
120 ti.lpszText = LPSTR_TEXTCALLBACK;
121 // ToolTip control will cover the whole window
122 ti.rect.left = rect.left;
123 ti.rect.top = rect.top;
124 ti.rect.right = rect.right;
125 ti.rect.bottom = rect.bottom;
127 SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
128 SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 600);
129 nHSecondScrollPos = 0;
130 nVSecondScrollPos = 0;
132 break;
133 case WM_SETFOCUS:
134 case WM_KILLFOCUS:
135 InvalidateRect(*this, NULL, FALSE);
136 break;
137 case WM_ERASEBKGND:
138 return 1;
139 break;
140 case WM_PAINT:
141 Paint(hwnd);
142 break;
143 case WM_SIZE:
144 PositionTrackBar();
145 SetupScrollBars();
146 break;
147 case WM_VSCROLL:
148 if ((pSecondPic)&&((HWND)lParam == m_AlphaSlider.GetWindow()))
150 if (LOWORD(wParam) == TB_THUMBTRACK)
152 // while tracking, only redraw after 50 milliseconds
153 ::SetTimer(*this, TIMER_ALPHASLIDER, 50, NULL);
155 else
156 SetBlendAlpha(m_blend, SendMessage(m_AlphaSlider.GetWindow(), TBM_GETPOS, 0, 0) / 16.0f);
158 else
160 UINT nPos = HIWORD(wParam);
161 bool bForceUpdate = false;
162 if (LOWORD(wParam) == SB_THUMBTRACK || LOWORD(wParam) == SB_THUMBPOSITION)
164 // Get true 32-bit scroll position
165 SCROLLINFO si;
166 si.cbSize = sizeof(SCROLLINFO);
167 si.fMask = SIF_TRACKPOS;
168 GetScrollInfo(*this, SB_VERT, &si);
169 nPos = si.nTrackPos;
170 bForceUpdate = true;
173 OnVScroll(LOWORD(wParam), nPos);
174 if (bLinkedPositions && pTheOtherPic)
176 pTheOtherPic->OnVScroll(LOWORD(wParam), nPos);
177 if (bForceUpdate)
178 ::UpdateWindow(*pTheOtherPic);
181 break;
182 case WM_HSCROLL:
184 UINT nPos = HIWORD(wParam);
185 bool bForceUpdate = false;
186 if (LOWORD(wParam) == SB_THUMBTRACK || LOWORD(wParam) == SB_THUMBPOSITION)
188 // Get true 32-bit scroll position
189 SCROLLINFO si;
190 si.cbSize = sizeof(SCROLLINFO);
191 si.fMask = SIF_TRACKPOS;
192 GetScrollInfo(*this, SB_VERT, &si);
193 nPos = si.nTrackPos;
194 bForceUpdate = true;
197 OnHScroll(LOWORD(wParam), nPos);
198 if (bLinkedPositions && pTheOtherPic)
200 pTheOtherPic->OnHScroll(LOWORD(wParam), nPos);
201 if (bForceUpdate)
202 ::UpdateWindow(*pTheOtherPic);
205 break;
206 case WM_MOUSEWHEEL:
208 OnMouseWheel(GET_KEYSTATE_WPARAM(wParam), GET_WHEEL_DELTA_WPARAM(wParam));
210 break;
211 case WM_MOUSEHWHEEL:
213 OnMouseWheel(GET_KEYSTATE_WPARAM(wParam)|MK_SHIFT, GET_WHEEL_DELTA_WPARAM(wParam));
215 break;
216 case WM_LBUTTONDOWN:
217 SetFocus(*this);
218 ptPanStart.x = GET_X_LPARAM(lParam);
219 ptPanStart.y = GET_Y_LPARAM(lParam);
220 startVScrollPos = nVScrollPos;
221 startHScrollPos = nHScrollPos;
222 startVSecondScrollPos = nVSecondScrollPos;
223 startHSecondScrollPos = nHSecondScrollPos;
224 bDragging = true;
225 SetCapture(*this);
226 break;
227 case WM_LBUTTONUP:
228 bDragging = false;
229 ReleaseCapture();
230 InvalidateRect(*this, NULL, FALSE);
231 break;
232 case WM_MOUSELEAVE:
233 ptPanStart.x = -1;
234 ptPanStart.y = -1;
235 m_lastTTPos.x = 0;
236 m_lastTTPos.y = 0;
237 SendMessage(hwndTT, TTM_TRACKACTIVATE, FALSE, 0);
238 break;
239 case WM_MOUSEMOVE:
241 mevt.cbSize = sizeof(TRACKMOUSEEVENT);
242 mevt.dwFlags = TME_LEAVE;
243 mevt.dwHoverTime = HOVER_DEFAULT;
244 mevt.hwndTrack = *this;
245 ::TrackMouseEvent(&mevt);
246 POINT pt = {((int)(short)LOWORD(lParam)), ((int)(short)HIWORD(lParam))};
247 if (pt.y < HEADER_HEIGHT)
249 ClientToScreen(*this, &pt);
250 if ((abs(m_lastTTPos.x - pt.x) > 20)||(abs(m_lastTTPos.y - pt.y) > 10))
252 m_lastTTPos = pt;
253 pt.x += 15;
254 pt.y += 15;
255 SendMessage(hwndTT, TTM_TRACKPOSITION, 0, MAKELONG(pt.x, pt.y));
256 TOOLINFO ti = {0};
257 ti.cbSize = sizeof(TOOLINFO);
258 ti.hwnd = *this;
259 ti.uId = 0;
260 SendMessage(hwndTT, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
263 else
265 SendMessage(hwndTT, TTM_TRACKACTIVATE, FALSE, 0);
266 m_lastTTPos.x = 0;
267 m_lastTTPos.y = 0;
269 if ((wParam & MK_LBUTTON) &&
270 (ptPanStart.x >= 0) &&
271 (ptPanStart.y >= 0))
273 // pan the image
274 int xPos = GET_X_LPARAM(lParam);
275 int yPos = GET_Y_LPARAM(lParam);
277 if (wParam & MK_CONTROL)
279 nHSecondScrollPos = startHSecondScrollPos + (ptPanStart.x - xPos);
280 nVSecondScrollPos = startVSecondScrollPos + (ptPanStart.y - yPos);
282 else if (wParam & MK_SHIFT)
284 nHScrollPos = startHScrollPos + (ptPanStart.x - xPos);
285 nVScrollPos = startVScrollPos + (ptPanStart.y - yPos);
287 else
289 nHSecondScrollPos = startHSecondScrollPos + (ptPanStart.x - xPos);
290 nVSecondScrollPos = startVSecondScrollPos + (ptPanStart.y - yPos);
291 nHScrollPos = startHScrollPos + (ptPanStart.x - xPos);
292 nVScrollPos = startVScrollPos + (ptPanStart.y - yPos);
293 if (!bLinkedPositions && pTheOtherPic)
295 // snap to the other picture borders
296 if (abs(nVScrollPos-pTheOtherPic->nVScrollPos) < 10)
297 nVScrollPos = pTheOtherPic->nVScrollPos;
298 if (abs(nHScrollPos-pTheOtherPic->nHScrollPos) < 10)
299 nHScrollPos = pTheOtherPic->nHScrollPos;
302 SetupScrollBars();
303 InvalidateRect(*this, NULL, TRUE);
304 UpdateWindow(*this);
305 if (pTheOtherPic && (bLinkedPositions) && ((wParam & MK_SHIFT)==0))
307 pTheOtherPic->nHScrollPos = nHScrollPos;
308 pTheOtherPic->nVScrollPos = nVScrollPos;
309 pTheOtherPic->SetupScrollBars();
310 InvalidateRect(*pTheOtherPic, NULL, TRUE);
311 UpdateWindow(*pTheOtherPic);
315 break;
316 case WM_SETCURSOR:
318 // we show a hand cursor if the image can be dragged,
319 // and a hand-down cursor if the image is currently dragged
320 if ((*this == (HWND)wParam)&&(LOWORD(lParam)==HTCLIENT))
322 RECT rect;
323 GetClientRect(&rect);
324 LONG width = picture.m_Width;
325 LONG height = picture.m_Height;
326 if (pSecondPic)
328 width = max(width, pSecondPic->m_Width);
329 height = max(height, pSecondPic->m_Height);
332 if ((GetKeyState(VK_LBUTTON)&0x8000)||(HIWORD(lParam) == WM_LBUTTONDOWN))
334 SetCursor(curHandDown);
336 else
338 SetCursor(curHand);
340 return TRUE;
342 return DefWindowProc(hwnd, uMsg, wParam, lParam);
344 break;
345 case WM_DROPFILES:
347 HDROP hDrop = (HDROP)wParam;
348 TCHAR szFileName[MAX_PATH] = {0};
349 // we only use the first file dropped (if multiple files are dropped)
350 if (DragQueryFile(hDrop, 0, szFileName, _countof(szFileName)))
352 SetPic(szFileName, _T(""), bMainPic);
353 FitImageInWindow();
354 InvalidateRect(*this, NULL, TRUE);
357 break;
358 case WM_COMMAND:
360 switch (LOWORD(wParam))
362 case LEFTBUTTON_ID:
364 PrevImage();
365 if (bLinkedPositions && pTheOtherPic)
366 pTheOtherPic->PrevImage();
367 return 0;
369 break;
370 case RIGHTBUTTON_ID:
372 NextImage();
373 if (bLinkedPositions && pTheOtherPic)
374 pTheOtherPic->NextImage();
375 return 0;
377 break;
378 case PLAYBUTTON_ID:
380 bPlaying = !bPlaying;
381 Animate(bPlaying);
382 if (bLinkedPositions && pTheOtherPic)
383 pTheOtherPic->Animate(bPlaying);
384 return 0;
386 break;
387 case ALPHATOGGLEBUTTON_ID:
389 WORD msg = HIWORD(wParam);
390 switch (msg)
392 case BN_DOUBLECLICKED:
394 SendMessage(hwndAlphaToggleBtn, BM_SETSTATE, 1, 0);
395 SetTimer(*this, ID_ALPHATOGGLETIMER, 1000, NULL);
397 break;
398 case BN_CLICKED:
399 KillTimer(*this, ID_ALPHATOGGLETIMER);
400 ToggleAlpha();
401 break;
403 return 0;
405 break;
406 case BLENDALPHA_ID:
408 m_blend = BLEND_ALPHA;
409 PositionTrackBar();
410 InvalidateRect(*this, NULL, TRUE);
412 break;
413 case BLENDXOR_ID:
415 m_blend = BLEND_XOR;
416 PositionTrackBar();
417 InvalidateRect(*this, NULL, TRUE);
419 break;
420 case SELECTBUTTON_ID:
422 SendMessage(GetParent(m_hwnd), WM_COMMAND, MAKEWPARAM(SELECTBUTTON_ID, SELECTBUTTON_ID), (LPARAM)m_hwnd);
424 break;
427 break;
428 case WM_TIMER:
430 switch (wParam)
432 case ID_ANIMATIONTIMER:
434 nCurrentFrame++;
435 if (nCurrentFrame > picture.GetNumberOfFrames(0))
436 nCurrentFrame = 1;
437 long delay = picture.SetActiveFrame(nCurrentFrame);
438 delay = max(100, delay);
439 SetTimer(*this, ID_ANIMATIONTIMER, delay, NULL);
440 InvalidateRect(*this, NULL, FALSE);
442 break;
443 case TIMER_ALPHASLIDER:
445 SetBlendAlpha(m_blend, SendMessage(m_AlphaSlider.GetWindow(), TBM_GETPOS, 0, 0)/16.0f);
446 KillTimer(*this, TIMER_ALPHASLIDER);
448 break;
449 case ID_ALPHATOGGLETIMER:
451 ToggleAlpha();
453 break;
456 break;
457 case WM_NOTIFY:
459 LPNMHDR pNMHDR = (LPNMHDR)lParam;
460 if (pNMHDR->code == TTN_GETDISPINFO)
462 if (pNMHDR->hwndFrom == m_AlphaSlider.GetWindow())
464 LPTOOLTIPTEXT lpttt = (LPTOOLTIPTEXT) lParam;
465 lpttt->hinst = hResource;
466 TCHAR stringbuf[MAX_PATH] = {0};
467 _stprintf_s(stringbuf, _T("%i%% alpha"), (int)(SendMessage(m_AlphaSlider.GetWindow(),TBM_GETPOS,0,0)/16.0f*100.0f));
468 _tcscpy_s(lpttt->lpszText, 80, stringbuf);
470 else if (pNMHDR->idFrom == (UINT_PTR)hwndAlphaToggleBtn)
472 _stprintf_s(m_wszTip, (TCHAR const *)ResString(hResource, IDS_ALPHABUTTONTT), (int)(SendMessage(m_AlphaSlider.GetWindow(),TBM_GETPOS,0,0)/16.0f*100.0f));
473 if (pNMHDR->code == TTN_NEEDTEXTW)
475 NMTTDISPINFOW* pTTTW = (NMTTDISPINFOW*)pNMHDR;
476 pTTTW->lpszText = m_wszTip;
478 else
480 NMTTDISPINFOA* pTTTA = (NMTTDISPINFOA*)pNMHDR;
481 pTTTA->lpszText = m_szTip;
482 ::WideCharToMultiByte(CP_ACP, 0, m_wszTip, -1, m_szTip, 8192, NULL, NULL);
485 else
487 BuildInfoString(m_wszTip, _countof(m_wszTip), true);
488 if (pNMHDR->code == TTN_NEEDTEXTW)
490 NMTTDISPINFOW* pTTTW = (NMTTDISPINFOW*)pNMHDR;
491 pTTTW->lpszText = m_wszTip;
493 else
495 NMTTDISPINFOA* pTTTA = (NMTTDISPINFOA*)pNMHDR;
496 pTTTA->lpszText = m_szTip;
497 ::WideCharToMultiByte(CP_ACP, 0, m_wszTip, -1, m_szTip, 8192, NULL, NULL);
502 break;
503 case WM_DESTROY:
504 DestroyIcon(hLeft);
505 DestroyIcon(hRight);
506 DestroyIcon(hPlay);
507 DestroyIcon(hStop);
508 bWindowClosed = TRUE;
509 break;
510 default:
511 return DefWindowProc(hwnd, uMsg, wParam, lParam);
514 return 0;
517 void CPicWindow::NextImage()
519 nCurrentDimension++;
520 if (nCurrentDimension > picture.GetNumberOfDimensions())
521 nCurrentDimension = picture.GetNumberOfDimensions();
522 nCurrentFrame++;
523 if (nCurrentFrame > picture.GetNumberOfFrames(0))
524 nCurrentFrame = picture.GetNumberOfFrames(0);
525 picture.SetActiveFrame(nCurrentFrame >= nCurrentDimension ? nCurrentFrame : nCurrentDimension);
526 InvalidateRect(*this, NULL, FALSE);
527 PositionChildren();
530 void CPicWindow::PrevImage()
532 nCurrentDimension--;
533 if (nCurrentDimension < 1)
534 nCurrentDimension = 1;
535 nCurrentFrame--;
536 if (nCurrentFrame < 1)
537 nCurrentFrame = 1;
538 picture.SetActiveFrame(nCurrentFrame >= nCurrentDimension ? nCurrentFrame : nCurrentDimension);
539 InvalidateRect(*this, NULL, FALSE);
540 PositionChildren();
543 void CPicWindow::Animate(bool bStart)
545 if (bStart)
547 SendMessage(hwndPlayBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hStop);
548 SetTimer(*this, ID_ANIMATIONTIMER, 0, NULL);
550 else
552 SendMessage(hwndPlayBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hPlay);
553 KillTimer(*this, ID_ANIMATIONTIMER);
557 void CPicWindow::SetPic(tstring path, tstring title, bool bFirst)
559 bMainPic = bFirst;
560 picpath=path;pictitle=title;
561 picture.SetInterpolationMode(InterpolationModeHighQualityBicubic);
562 bValid = picture.Load(picpath);
563 nDimensions = picture.GetNumberOfDimensions();
564 if (nDimensions)
565 nFrames = picture.GetNumberOfFrames(0);
566 if (bValid)
568 picscale = 100;
569 PositionChildren();
570 InvalidateRect(*this, NULL, FALSE);
574 void CPicWindow::DrawViewTitle(HDC hDC, RECT * rect)
576 HFONT hFont = NULL;
577 hFont = CreateFont(-MulDiv(pSecondPic ? 8 : 10, GetDeviceCaps(hDC, LOGPIXELSY), 72), 0, 0, 0, FW_DONTCARE, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH, _T("MS Shell Dlg"));
578 HFONT hFontOld = (HFONT)SelectObject(hDC, (HGDIOBJ)hFont);
580 RECT textrect;
581 textrect.left = rect->left;
582 textrect.top = rect->top;
583 textrect.right = rect->right;
584 textrect.bottom = rect->top + HEADER_HEIGHT;
585 if (HasMultipleImages())
586 textrect.bottom += HEADER_HEIGHT;
588 COLORREF crBk, crFg;
589 crBk = ::GetSysColor(COLOR_SCROLLBAR);
590 crFg = ::GetSysColor(COLOR_WINDOWTEXT);
591 SetBkColor(hDC, crBk);
592 ::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &textrect, NULL, 0, NULL);
594 if (GetFocus() == *this)
595 DrawEdge(hDC, &textrect, EDGE_BUMP, BF_RECT);
596 else
597 DrawEdge(hDC, &textrect, EDGE_ETCHED, BF_RECT);
599 SetTextColor(hDC, crFg);
601 // use the path if no title is set.
602 tstring * title = pictitle.empty() ? &picpath : &pictitle;
604 tstring realtitle = *title;
605 tstring imgnumstring;
607 if (HasMultipleImages())
609 TCHAR buf[MAX_PATH] = {0};
610 if (nFrames > 1)
611 _stprintf_s(buf, (const TCHAR *)ResString(hResource, IDS_DIMENSIONSANDFRAMES), nCurrentFrame, nFrames);
612 else
613 _stprintf_s(buf, (const TCHAR *)ResString(hResource, IDS_DIMENSIONSANDFRAMES), nCurrentDimension, nDimensions);
614 imgnumstring = buf;
617 SIZE stringsize;
618 if (GetTextExtentPoint32(hDC, realtitle.c_str(), (int)realtitle.size(), &stringsize))
620 int nStringLength = stringsize.cx;
621 int texttop = pSecondPic ? textrect.top + (HEADER_HEIGHT/2) - stringsize.cy : textrect.top + (HEADER_HEIGHT/2) - stringsize.cy/2;
622 ExtTextOut(hDC,
623 max(textrect.left + ((textrect.right-textrect.left)-nStringLength)/2, 1),
624 texttop,
625 ETO_CLIPPED,
626 &textrect,
627 realtitle.c_str(),
628 (UINT)realtitle.size(),
629 NULL);
630 if (pSecondPic)
632 realtitle = (pictitle2.empty() ? picpath2 : pictitle2);
633 ExtTextOut(hDC,
634 max(textrect.left + ((textrect.right-textrect.left)-nStringLength)/2, 1),
635 texttop + stringsize.cy,
636 ETO_CLIPPED,
637 &textrect,
638 realtitle.c_str(),
639 (UINT)realtitle.size(),
640 NULL);
643 if (HasMultipleImages())
645 if (GetTextExtentPoint32(hDC, imgnumstring.c_str(), (int)imgnumstring.size(), &stringsize))
647 int nStringLength = stringsize.cx;
649 ExtTextOut(hDC,
650 max(textrect.left + ((textrect.right-textrect.left)-nStringLength)/2, 1),
651 textrect.top + HEADER_HEIGHT + (HEADER_HEIGHT/2) - stringsize.cy/2,
652 ETO_CLIPPED,
653 &textrect,
654 imgnumstring.c_str(),
655 (UINT)imgnumstring.size(),
656 NULL);
659 SelectObject(hDC, (HGDIOBJ)hFontOld);
660 DeleteObject(hFont);
663 void CPicWindow::SetupScrollBars()
665 RECT rect;
666 GetClientRect(&rect);
668 SCROLLINFO si = {sizeof(si)};
670 si.fMask = SIF_POS | SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL;
672 long width = picture.m_Width*picscale/100;
673 long height = picture.m_Height*picscale/100;
674 if (pSecondPic && pTheOtherPic)
676 width = max(width, pSecondPic->m_Width*pTheOtherPic->GetZoom()/100);
677 height = max(height, pSecondPic->m_Height*pTheOtherPic->GetZoom()/100);
680 bool bShowHScrollBar = (nHScrollPos > 0); // left of pic is left of window
681 bShowHScrollBar = bShowHScrollBar || (width-nHScrollPos > rect.right); // right of pic is outside right of window
682 bShowHScrollBar = bShowHScrollBar || (width+nHScrollPos > rect.right); // right of pic is outside right of window
683 bool bShowVScrollBar = (nVScrollPos > 0); // top of pic is above window
684 bShowVScrollBar = bShowVScrollBar || (height-nVScrollPos+rect.top > rect.bottom); // bottom of pic is below window
685 bShowVScrollBar = bShowVScrollBar || (height+nVScrollPos+rect.top > rect.bottom); // bottom of pic is below window
687 // if the image is smaller than the window, we don't need the scrollbars
688 ShowScrollBar(*this, SB_HORZ, bShowHScrollBar);
689 ShowScrollBar(*this, SB_VERT, bShowVScrollBar);
691 if (bShowVScrollBar)
693 si.nPos = nVScrollPos;
694 si.nPage = rect.bottom-rect.top;
695 if (height < rect.bottom-rect.top)
697 if (nVScrollPos > 0)
699 si.nMin = 0;
700 si.nMax = rect.bottom+nVScrollPos-rect.top;
702 else
704 si.nMin = nVScrollPos;
705 si.nMax = int(height);
708 else
710 if (nVScrollPos > 0)
712 si.nMin = 0;
713 si.nMax = int(max(height, rect.bottom+nVScrollPos-rect.top));
715 else
717 si.nMin = 0;
718 si.nMax = int(height-nVScrollPos);
721 SetScrollInfo(*this, SB_VERT, &si, TRUE);
724 if (bShowHScrollBar)
726 si.nPos = nHScrollPos;
727 si.nPage = rect.right-rect.left;
728 if (width < rect.right-rect.left)
730 if (nHScrollPos > 0)
732 si.nMin = 0;
733 si.nMax = rect.right+nHScrollPos-rect.left;
735 else
737 si.nMin = nHScrollPos;
738 si.nMax = int(width);
741 else
743 if (nHScrollPos > 0)
745 si.nMin = 0;
746 si.nMax = int(max(width, rect.right+nHScrollPos-rect.left));
748 else
750 si.nMin = 0;
751 si.nMax = int(width-nHScrollPos);
754 SetScrollInfo(*this, SB_HORZ, &si, TRUE);
757 PositionChildren();
760 void CPicWindow::OnVScroll(UINT nSBCode, UINT nPos)
762 RECT rect;
763 GetClientRect(&rect);
765 switch (nSBCode)
767 case SB_BOTTOM:
768 nVScrollPos = LONG(picture.GetHeight()*picscale/100);
769 break;
770 case SB_TOP:
771 nVScrollPos = 0;
772 break;
773 case SB_LINEDOWN:
774 nVScrollPos++;
775 break;
776 case SB_LINEUP:
777 nVScrollPos--;
778 break;
779 case SB_PAGEDOWN:
780 nVScrollPos += (rect.bottom-rect.top);
781 break;
782 case SB_PAGEUP:
783 nVScrollPos -= (rect.bottom-rect.top);
784 break;
785 case SB_THUMBPOSITION:
786 nVScrollPos = nPos;
787 break;
788 case SB_THUMBTRACK:
789 nVScrollPos = nPos;
790 break;
791 default:
792 return;
794 LONG height = LONG(picture.GetHeight()*picscale/100);
795 if (pSecondPic)
797 height = max(height, LONG(pSecondPic->GetHeight()*picscale/100));
798 nVSecondScrollPos = nVScrollPos;
800 SetupScrollBars();
801 PositionChildren();
802 InvalidateRect(*this, NULL, TRUE);
805 void CPicWindow::OnHScroll(UINT nSBCode, UINT nPos)
807 RECT rect;
808 GetClientRect(&rect);
810 switch (nSBCode)
812 case SB_RIGHT:
813 nHScrollPos = LONG(picture.GetWidth()*picscale/100);
814 break;
815 case SB_LEFT:
816 nHScrollPos = 0;
817 break;
818 case SB_LINERIGHT:
819 nHScrollPos++;
820 break;
821 case SB_LINELEFT:
822 nHScrollPos--;
823 break;
824 case SB_PAGERIGHT:
825 nHScrollPos += (rect.right-rect.left);
826 break;
827 case SB_PAGELEFT:
828 nHScrollPos -= (rect.right-rect.left);
829 break;
830 case SB_THUMBPOSITION:
831 nHScrollPos = nPos;
832 break;
833 case SB_THUMBTRACK:
834 nHScrollPos = nPos;
835 break;
836 default:
837 return;
839 LONG width = LONG(picture.GetWidth()*picscale/100);
840 if (pSecondPic)
842 width = max(width, LONG(pSecondPic->GetWidth()*picscale/100));
843 nHSecondScrollPos = nHScrollPos;
845 SetupScrollBars();
846 PositionChildren();
847 InvalidateRect(*this, NULL, TRUE);
850 void CPicWindow::OnMouseWheel(short fwKeys, short zDelta)
852 RECT rect;
853 GetClientRect(&rect);
854 LONG width = long(picture.m_Width*picscale/100);
855 LONG height = long(picture.m_Height*picscale/100);
856 if (pSecondPic)
858 width = max(width, long(pSecondPic->m_Width*picscale/100));
859 height = max(height, long(pSecondPic->m_Height*picscale/100));
861 if ((fwKeys & MK_SHIFT)&&(fwKeys & MK_CONTROL)&&(pSecondPic))
863 // ctrl+shift+wheel: change the alpha channel
864 float a = blendAlpha;
865 a -= float(zDelta)/120.0f/4.0f;
866 if (a < 0.0f)
867 a = 0.0f;
868 else if (a > 1.0f)
869 a = 1.0f;
870 SetBlendAlpha(m_blend, a);
872 else if (fwKeys & MK_SHIFT)
874 // shift means scrolling sideways
875 OnHScroll(SB_THUMBPOSITION, GetHPos()-zDelta);
876 if ((bLinkedPositions)&&(pTheOtherPic))
878 pTheOtherPic->OnHScroll(SB_THUMBPOSITION, pTheOtherPic->GetHPos()-zDelta);
881 else if (fwKeys & MK_CONTROL)
883 // control means adjusting the scale factor
884 Zoom(zDelta>0, true);
885 PositionChildren();
886 InvalidateRect(*this, NULL, FALSE);
887 SetWindowPos(*this, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOREPOSITION|SWP_NOMOVE);
888 UpdateWindow(*this);
889 if (bLinkedPositions && pTheOtherPic)
891 pTheOtherPic->nHScrollPos = nHScrollPos;
892 pTheOtherPic->nVScrollPos = nVScrollPos;
893 pTheOtherPic->SetupScrollBars();
894 InvalidateRect(*pTheOtherPic, NULL, TRUE);
895 UpdateWindow(*pTheOtherPic);
898 else
900 OnVScroll(SB_THUMBPOSITION, GetVPos()-zDelta);
901 if ((bLinkedPositions)&&(pTheOtherPic))
903 pTheOtherPic->OnVScroll(SB_THUMBPOSITION, pTheOtherPic->GetVPos()-zDelta);
908 void CPicWindow::GetClientRect(RECT * pRect)
910 ::GetClientRect(*this, pRect);
911 pRect->top += HEADER_HEIGHT;
912 if (HasMultipleImages())
914 pRect->top += HEADER_HEIGHT;
916 if (pSecondPic)
917 pRect->left += SLIDER_WIDTH;
920 void CPicWindow::GetClientRectWithScrollbars(RECT * pRect)
922 GetClientRect(pRect);
923 ::GetWindowRect(*this, pRect);
924 pRect->right = pRect->right-pRect->left;
925 pRect->bottom = pRect->bottom-pRect->top;
926 pRect->top = 0;
927 pRect->left = 0;
928 pRect->top += HEADER_HEIGHT;
929 if (HasMultipleImages())
931 pRect->top += HEADER_HEIGHT;
933 if (pSecondPic)
934 pRect->left += SLIDER_WIDTH;
938 void CPicWindow::SetZoom(int Zoom, bool centermouse, bool inzoom)
940 // Set the interpolation mode depending on zoom
941 int oldPicscale = picscale;
942 int oldOtherPicscale = picscale;
944 picture.SetInterpolationMode(InterpolationModeNearestNeighbor);
945 if (pSecondPic)
946 pSecondPic->SetInterpolationMode(InterpolationModeNearestNeighbor);
948 if ((oldPicscale == 0) || (Zoom == 0))
949 return;
951 picscale = Zoom;
953 if (pTheOtherPic && !inzoom)
955 if (bFitHeights)
957 m_linkedHeight = 0;
958 pTheOtherPic->SetZoomToHeight(picture.m_Height*Zoom/100);
960 if (bFitWidths)
962 m_linkedWidth = 0;
963 pTheOtherPic->SetZoomToWidth(picture.m_Width*Zoom/100);
967 // adjust the scrollbar positions according to the new zoom and the
968 // mouse position: if possible, keep the pixel where the mouse pointer
969 // is at the same position after the zoom
970 if (!inzoom)
972 POINT cpos;
973 DWORD ptW = GetMessagePos();
974 cpos.x = GET_X_LPARAM(ptW);
975 cpos.y = GET_Y_LPARAM(ptW);
976 ScreenToClient(*this, &cpos);
977 RECT clientrect;
978 GetClientRect(&clientrect);
979 if ((PtInRect(&clientrect, cpos))&&(centermouse))
981 // the mouse pointer is over our window
982 nHScrollPos = (nHScrollPos + cpos.x)*Zoom/oldPicscale-cpos.x;
983 nVScrollPos = (nVScrollPos + cpos.y)*Zoom/oldPicscale-cpos.y;
984 if (pTheOtherPic && bMainPic)
986 int otherzoom = pTheOtherPic->GetZoom();
987 nHSecondScrollPos = (nHSecondScrollPos + cpos.x)*otherzoom/oldOtherPicscale-cpos.x;
988 nVSecondScrollPos = (nVSecondScrollPos + cpos.y)*otherzoom/oldOtherPicscale-cpos.y;
991 else
993 nHScrollPos = (nHScrollPos + ((clientrect.right-clientrect.left)/2))*Zoom/oldPicscale-((clientrect.right-clientrect.left)/2);
994 nVScrollPos = (nVScrollPos + ((clientrect.bottom-clientrect.top)/2))*Zoom/oldPicscale-((clientrect.bottom-clientrect.top)/2);
995 if (pTheOtherPic && bMainPic)
997 int otherzoom = pTheOtherPic->GetZoom();
998 nHSecondScrollPos = (nHSecondScrollPos + ((clientrect.right-clientrect.left)/2))*otherzoom/oldOtherPicscale-((clientrect.right-clientrect.left)/2);
999 nVSecondScrollPos = (nVSecondScrollPos + ((clientrect.bottom-clientrect.top)/2))*otherzoom/oldOtherPicscale-((clientrect.bottom-clientrect.top)/2);
1004 SetupScrollBars();
1005 PositionChildren();
1006 InvalidateRect(*this, NULL, TRUE);
1009 void CPicWindow::Zoom(bool in, bool centermouse)
1011 int zoomFactor;
1013 // Find correct zoom factor and quantize picscale
1014 if (picscale % 10)
1016 picscale /= 10;
1017 picscale *= 10;
1018 if (!in)
1019 picscale += 10;
1022 if (!in && picscale <= 20)
1024 picscale = 10;
1025 zoomFactor = 0;
1027 else if ((in && picscale < 100) || (!in && picscale <= 100))
1029 zoomFactor = 10;
1031 else if ((in && picscale < 200) || (!in && picscale <= 200))
1033 zoomFactor = 20;
1035 else
1037 zoomFactor = 10;
1040 // Set zoom
1041 if (in)
1043 SetZoom(picscale+zoomFactor, centermouse);
1045 else
1047 SetZoom(picscale-zoomFactor, centermouse);
1051 void CPicWindow::FitImageInWindow()
1053 RECT rect;
1055 GetClientRectWithScrollbars(&rect);
1057 if (rect.right-rect.left)
1059 int Zoom = 100;
1060 if (((rect.right - rect.left) > picture.m_Width+2)&&((rect.bottom - rect.top)> picture.m_Height+2))
1062 // image is smaller than the window
1063 Zoom = 100;
1065 else
1067 // image is bigger than the window
1068 int xscale = (rect.right-rect.left-2)*100/picture.m_Width;
1069 int yscale = (rect.bottom-rect.top-2)*100/picture.m_Height;
1070 Zoom = min(yscale, xscale);
1072 if (pSecondPic)
1074 if (((rect.right - rect.left) > pSecondPic->m_Width+2)&&((rect.bottom - rect.top)> pSecondPic->m_Height+2))
1076 // image is smaller than the window
1077 if (pTheOtherPic)
1078 pTheOtherPic->SetZoom(min(100, Zoom), false);
1080 else
1082 // image is bigger than the window
1083 int xscale = (rect.right-rect.left-2)*100/pSecondPic->m_Width;
1084 int yscale = (rect.bottom-rect.top-2)*100/pSecondPic->m_Height;
1085 if (pTheOtherPic)
1086 pTheOtherPic->SetZoom(min(yscale, xscale), false);
1088 nHSecondScrollPos = 0;
1089 nVSecondScrollPos = 0;
1091 SetZoom(Zoom, false);
1093 CenterImage();
1094 PositionChildren();
1095 InvalidateRect(*this, NULL, TRUE);
1098 void CPicWindow::CenterImage()
1100 RECT rect;
1101 GetClientRectWithScrollbars(&rect);
1102 long width = picture.m_Width*picscale/100 + 2;
1103 long height = picture.m_Height*picscale/100 + 2;
1104 if (pSecondPic && pTheOtherPic)
1106 width = max(width, pSecondPic->m_Width*pTheOtherPic->GetZoom()/100 + 2);
1107 height = max(height, pSecondPic->m_Height*pTheOtherPic->GetZoom()/100 + 2);
1110 bool bPicWidthBigger = (int(width) > (rect.right-rect.left));
1111 bool bPicHeightBigger = (int(height) > (rect.bottom-rect.top));
1112 // set the scroll position so that the image is drawn centered in the window
1113 // if the window is bigger than the image
1114 if (!bPicWidthBigger)
1116 nHScrollPos = -((rect.right-rect.left+4)-int(width))/2;
1117 nHSecondScrollPos = nHScrollPos;
1119 if (!bPicHeightBigger)
1121 nVScrollPos = -((rect.bottom-rect.top+4)-int(height))/2;
1122 nVSecondScrollPos = nVScrollPos;
1124 SetupScrollBars();
1127 void CPicWindow::FitWidths(bool bFit)
1129 bFitWidths = bFit;
1131 SetZoom(GetZoom(), false);
1134 void CPicWindow::FitHeights(bool bFit)
1136 bFitHeights = bFit;
1138 SetZoom(GetZoom(), false);
1141 void CPicWindow::ShowPicWithBorder(HDC hdc, const RECT &bounds, CPicture &pic, int scale)
1143 ::SetBkColor(hdc, transparentColor);
1144 ::ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &bounds, NULL, 0, NULL);
1146 RECT picrect;
1147 picrect.left = bounds.left - nHScrollPos;
1148 picrect.top = bounds.top - nVScrollPos;
1149 if ((!bLinkedPositions || bOverlap) && (pTheOtherPic) && (&pic != &picture))
1151 picrect.left = bounds.left - nHSecondScrollPos;
1152 picrect.top = bounds.top - nVSecondScrollPos;
1154 picrect.right = (picrect.left + pic.m_Width * scale / 100);
1155 picrect.bottom = (picrect.top + pic.m_Height * scale / 100);
1157 if (bFitWidths && m_linkedWidth)
1158 picrect.right = picrect.left + m_linkedWidth;
1159 if (bFitHeights && m_linkedHeight)
1160 picrect.bottom = picrect.top + m_linkedHeight;
1162 pic.Show(hdc, picrect);
1164 RECT border;
1165 border.left = picrect.left-1;
1166 border.top = picrect.top-1;
1167 border.right = picrect.right+1;
1168 border.bottom = picrect.bottom+1;
1170 HPEN hPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW));
1171 HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
1172 MoveToEx(hdc, border.left, border.top, NULL);
1173 LineTo(hdc, border.left, border.bottom);
1174 LineTo(hdc, border.right, border.bottom);
1175 LineTo(hdc, border.right, border.top);
1176 LineTo(hdc, border.left, border.top);
1177 SelectObject(hdc, hOldPen);
1178 DeleteObject(hPen);
1181 void CPicWindow::Paint(HWND hwnd)
1183 PAINTSTRUCT ps;
1184 HDC hdc;
1185 RECT rect, fullrect;
1187 GetUpdateRect(hwnd, &rect, FALSE);
1188 if (IsRectEmpty(&rect))
1189 return;
1191 ::GetClientRect(*this, &fullrect);
1192 hdc = BeginPaint(hwnd, &ps);
1194 // Exclude the alpha control and button
1195 if ((pSecondPic)&&(m_blend == BLEND_ALPHA))
1196 ExcludeClipRect(hdc, 0, m_inforect.top-4, SLIDER_WIDTH, m_inforect.bottom+4);
1198 CMyMemDC memDC(hdc);
1199 if ((pSecondPic)&&(m_blend != BLEND_ALPHA))
1201 // erase the place where the alpha slider would be
1202 ::SetBkColor(memDC, transparentColor);
1203 RECT bounds = {0, m_inforect.top-4, SLIDER_WIDTH, m_inforect.bottom+4};
1204 ::ExtTextOut(memDC, 0, 0, ETO_OPAQUE, &bounds, NULL, 0, NULL);
1207 GetClientRect(&rect);
1208 if (bValid)
1210 ShowPicWithBorder(memDC, rect, picture, picscale);
1211 if (pSecondPic)
1213 HDC secondhdc = CreateCompatibleDC(hdc);
1214 HBITMAP hBitmap = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);
1215 HBITMAP hOldBitmap = (HBITMAP)SelectObject(secondhdc, hBitmap);
1216 SetWindowOrgEx(secondhdc, rect.left, rect.top, NULL);
1218 if ((pSecondPic)&&(m_blend != BLEND_ALPHA))
1220 // erase the place where the alpha slider would be
1221 ::SetBkColor(secondhdc, transparentColor);
1222 RECT bounds = {0, m_inforect.top-4, SLIDER_WIDTH, m_inforect.bottom+4};
1223 ::ExtTextOut(secondhdc, 0, 0, ETO_OPAQUE, &bounds, NULL, 0, NULL);
1225 if (pTheOtherPic)
1226 ShowPicWithBorder(secondhdc, rect, *pSecondPic, pTheOtherPic->GetZoom());
1228 if (m_blend == BLEND_ALPHA)
1230 BLENDFUNCTION blender;
1231 blender.AlphaFormat = 0;
1232 blender.BlendFlags = 0;
1233 blender.BlendOp = AC_SRC_OVER;
1234 blender.SourceConstantAlpha = (BYTE)(blendAlpha*255);
1235 AlphaBlend(memDC,
1236 rect.left,
1237 rect.top,
1238 rect.right-rect.left,
1239 rect.bottom-rect.top,
1240 secondhdc,
1241 rect.left,
1242 rect.top,
1243 rect.right-rect.left,
1244 rect.bottom-rect.top,
1245 blender);
1247 else if (m_blend == BLEND_XOR)
1249 BitBlt(memDC,
1250 rect.left,
1251 rect.top,
1252 rect.right-rect.left,
1253 rect.bottom-rect.top,
1254 secondhdc,
1255 rect.left,
1256 rect.top,
1257 //rect.right-rect.left,
1258 //rect.bottom-rect.top,
1259 SRCINVERT);
1260 InvertRect(memDC, &rect);
1262 SelectObject(secondhdc, hOldBitmap);
1263 DeleteObject(hBitmap);
1264 DeleteDC(secondhdc);
1266 else if (bDragging && pTheOtherPic && !bLinkedPositions)
1268 // when dragging, show lines indicating the position of the other image
1269 HPEN hPen = CreatePen(PS_SOLID, 1, GetSysColor(/*COLOR_ACTIVEBORDER*/COLOR_HIGHLIGHT));
1270 HPEN hOldPen = (HPEN)SelectObject(memDC, hPen);
1271 int xpos = rect.left - pTheOtherPic->nHScrollPos - 1;
1272 MoveToEx(memDC, xpos, rect.top, NULL);
1273 LineTo(memDC, xpos, rect.bottom);
1274 xpos = rect.left - pTheOtherPic->nHScrollPos + pTheOtherPic->picture.m_Width*pTheOtherPic->GetZoom()/100 + 1;
1275 if (bFitWidths && m_linkedWidth)
1276 xpos = rect.left + pTheOtherPic->m_linkedWidth + 1;
1277 MoveToEx(memDC, xpos, rect.top, NULL);
1278 LineTo(memDC, xpos, rect.bottom);
1280 int ypos = rect.top - pTheOtherPic->nVScrollPos - 1;
1281 MoveToEx(memDC, rect.left, ypos, NULL);
1282 LineTo(memDC, rect.right, ypos);
1283 ypos = rect.top - pTheOtherPic->nVScrollPos + pTheOtherPic->picture.m_Height*pTheOtherPic->GetZoom()/100 + 1;
1284 if (bFitHeights && m_linkedHeight)
1285 ypos = rect.top - pTheOtherPic->m_linkedHeight + 1;
1286 MoveToEx(memDC, rect.left, ypos, NULL);
1287 LineTo(memDC, rect.right, ypos);
1289 SelectObject(memDC, hOldPen);
1290 DeleteObject(hPen);
1293 int sliderwidth = 0;
1294 if ((pSecondPic)&&(m_blend == BLEND_ALPHA))
1295 sliderwidth = SLIDER_WIDTH;
1296 m_inforect.left = rect.left+4+sliderwidth;
1297 m_inforect.top = rect.top;
1298 m_inforect.right = rect.right+sliderwidth;
1299 m_inforect.bottom = rect.bottom;
1301 SetBkColor(memDC, transparentColor);
1302 if (bShowInfo)
1304 std::unique_ptr<TCHAR[]> infostring(new TCHAR[8192]);
1305 BuildInfoString(infostring.get(), 8192, false);
1306 // set the font
1307 NONCLIENTMETRICS metrics = {0};
1308 metrics.cbSize = sizeof(NONCLIENTMETRICS);
1310 if (!SysInfo::Instance().IsVistaOrLater())
1312 metrics.cbSize -= sizeof(int); // subtract the size of the iPaddedBorderWidth member which is not available on XP
1315 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, FALSE);
1316 HFONT hFont = CreateFontIndirect(&metrics.lfStatusFont);
1317 HFONT hFontOld = (HFONT)SelectObject(memDC, (HGDIOBJ)hFont);
1318 // find out how big the rectangle for the text has to be
1319 DrawText(memDC, infostring.get(), -1, &m_inforect, DT_EDITCONTROL | DT_EXPANDTABS | DT_LEFT | DT_VCENTER | DT_CALCRECT);
1321 // the text should be drawn with a four pixel offset to the window borders
1322 m_inforect.top = rect.bottom - (m_inforect.bottom-m_inforect.top) - 4;
1323 m_inforect.bottom = rect.bottom-4;
1325 // first draw an edge rectangle
1326 RECT edgerect;
1327 edgerect.left = m_inforect.left-4;
1328 edgerect.top = m_inforect.top-4;
1329 edgerect.right = m_inforect.right+4;
1330 edgerect.bottom = m_inforect.bottom+4;
1331 ::ExtTextOut(memDC, 0, 0, ETO_OPAQUE, &edgerect, NULL, 0, NULL);
1332 DrawEdge(memDC, &edgerect, EDGE_BUMP, BF_RECT | BF_SOFT);
1334 SetTextColor(memDC, GetSysColor(COLOR_WINDOWTEXT));
1335 DrawText(memDC, infostring.get(), -1, &m_inforect, DT_EDITCONTROL | DT_EXPANDTABS | DT_LEFT | DT_VCENTER);
1336 SelectObject(memDC, (HGDIOBJ)hFontOld);
1337 DeleteObject(hFont);
1340 else
1342 SetBkColor(memDC, ::GetSysColor(COLOR_WINDOW));
1343 ::ExtTextOut(memDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
1344 SIZE stringsize;
1345 ResString str = ResString(hResource, IDS_INVALIDIMAGEINFO);
1347 // set the font
1348 NONCLIENTMETRICS metrics = {0};
1349 metrics.cbSize = sizeof(NONCLIENTMETRICS);
1350 if (!SysInfo::Instance().IsVistaOrLater())
1352 metrics.cbSize -= sizeof(int); // subtract the size of the iPaddedBorderWidth member which is not available on XP
1354 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &metrics, FALSE);
1355 HFONT hFont = CreateFontIndirect(&metrics.lfStatusFont);
1356 HFONT hFontOld = (HFONT)SelectObject(memDC, (HGDIOBJ)hFont);
1358 if (GetTextExtentPoint32(memDC, str, (int)_tcslen(str), &stringsize))
1360 int nStringLength = stringsize.cx;
1362 ExtTextOut(memDC,
1363 max(rect.left + ((rect.right-rect.left)-nStringLength)/2, 1),
1364 rect.top + ((rect.bottom-rect.top) - stringsize.cy)/2,
1365 ETO_CLIPPED,
1366 &rect,
1367 str,
1368 (UINT)_tcslen(str),
1369 NULL);
1371 SelectObject(memDC, (HGDIOBJ)hFontOld);
1372 DeleteObject(hFont);
1374 DrawViewTitle(memDC, &fullrect);
1376 EndPaint(hwnd, &ps);
1379 bool CPicWindow::CreateButtons()
1381 // Ensure that the common control DLL is loaded.
1382 INITCOMMONCONTROLSEX icex;
1383 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1384 icex.dwICC = ICC_BAR_CLASSES | ICC_WIN95_CLASSES;
1385 InitCommonControlsEx(&icex);
1387 hwndLeftBtn = CreateWindowEx(0,
1388 _T("BUTTON"),
1389 (LPCTSTR)NULL,
1390 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON | BS_FLAT,
1391 0, 0, 0, 0,
1392 (HWND)*this,
1393 (HMENU)LEFTBUTTON_ID,
1394 hResource,
1395 NULL);
1396 if (hwndLeftBtn == INVALID_HANDLE_VALUE)
1397 return false;
1398 hLeft = (HICON)LoadImage(hResource, MAKEINTRESOURCE(IDI_BACKWARD), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
1399 SendMessage(hwndLeftBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hLeft);
1400 hwndRightBtn = CreateWindowEx(0,
1401 _T("BUTTON"),
1402 (LPCTSTR)NULL,
1403 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON | BS_FLAT,
1404 0, 0, 0, 0,
1405 *this,
1406 (HMENU)RIGHTBUTTON_ID,
1407 hResource,
1408 NULL);
1409 if (hwndRightBtn == INVALID_HANDLE_VALUE)
1410 return false;
1411 hRight = (HICON)LoadImage(hResource, MAKEINTRESOURCE(IDI_FORWARD), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
1412 SendMessage(hwndRightBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hRight);
1413 hwndPlayBtn = CreateWindowEx(0,
1414 _T("BUTTON"),
1415 (LPCTSTR)NULL,
1416 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON | BS_FLAT,
1417 0, 0, 0, 0,
1418 *this,
1419 (HMENU)PLAYBUTTON_ID,
1420 hResource,
1421 NULL);
1422 if (hwndPlayBtn == INVALID_HANDLE_VALUE)
1423 return false;
1424 hPlay = (HICON)LoadImage(hResource, MAKEINTRESOURCE(IDI_START), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
1425 hStop = (HICON)LoadImage(hResource, MAKEINTRESOURCE(IDI_STOP), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
1426 SendMessage(hwndPlayBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hPlay);
1427 hwndAlphaToggleBtn = CreateWindowEx(0,
1428 _T("BUTTON"),
1429 (LPCTSTR)NULL,
1430 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_ICON | BS_FLAT | BS_NOTIFY | BS_PUSHLIKE,
1431 0, 0, 0, 0,
1432 (HWND)*this,
1433 (HMENU)ALPHATOGGLEBUTTON_ID,
1434 hResource,
1435 NULL);
1436 if (hwndAlphaToggleBtn == INVALID_HANDLE_VALUE)
1437 return false;
1438 hAlphaToggle = (HICON)LoadImage(hResource, MAKEINTRESOURCE(IDI_ALPHATOGGLE), IMAGE_ICON, 16, 16, LR_LOADTRANSPARENT);
1439 SendMessage(hwndAlphaToggleBtn, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hAlphaToggle);
1441 TOOLINFO ti = {0};
1442 ti.cbSize = sizeof(TOOLINFO);
1443 ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS;
1444 ti.hwnd = *this;
1445 ti.hinst = hResource;
1446 ti.uId = (UINT_PTR)hwndAlphaToggleBtn;
1447 ti.lpszText = LPSTR_TEXTCALLBACK;
1448 // ToolTip control will cover the whole window
1449 ti.rect.left = 0;
1450 ti.rect.top = 0;
1451 ti.rect.right = 0;
1452 ti.rect.bottom = 0;
1453 SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
1454 ResString sSelect(hResource, IDS_SELECT);
1455 hwndSelectBtn = CreateWindowEx(0,
1456 _T("BUTTON"),
1457 sSelect,
1458 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
1459 0, 0, 0, 0,
1460 *this,
1461 (HMENU)SELECTBUTTON_ID,
1462 hResource,
1463 NULL);
1464 if (hwndPlayBtn == INVALID_HANDLE_VALUE)
1465 return false;
1467 return true;
1470 void CPicWindow::PositionChildren()
1472 RECT rect;
1473 ::GetClientRect(*this, &rect);
1474 if (HasMultipleImages())
1476 SetWindowPos(hwndLeftBtn, HWND_TOP, rect.left+3, rect.top + HEADER_HEIGHT + (HEADER_HEIGHT-16)/2, 16, 16, SWP_FRAMECHANGED|SWP_SHOWWINDOW);
1477 SetWindowPos(hwndRightBtn, HWND_TOP, rect.left+23, rect.top + HEADER_HEIGHT + (HEADER_HEIGHT-16)/2, 16, 16, SWP_FRAMECHANGED|SWP_SHOWWINDOW);
1478 if (nFrames > 1)
1479 SetWindowPos(hwndPlayBtn, HWND_TOP, rect.left+43, rect.top + HEADER_HEIGHT + (HEADER_HEIGHT-16)/2, 16, 16, SWP_FRAMECHANGED|SWP_SHOWWINDOW);
1480 else
1481 ShowWindow(hwndPlayBtn, SW_HIDE);
1483 else
1485 ShowWindow(hwndLeftBtn, SW_HIDE);
1486 ShowWindow(hwndRightBtn, SW_HIDE);
1487 ShowWindow(hwndPlayBtn, SW_HIDE);
1489 if (bSelectionMode)
1490 SetWindowPos(hwndSelectBtn, HWND_TOP, rect.right-100, rect.bottom-HEADER_HEIGHT, 100, HEADER_HEIGHT, SWP_FRAMECHANGED|SWP_SHOWWINDOW);
1491 else
1492 ShowWindow(hwndSelectBtn, SW_HIDE);
1493 PositionTrackBar();
1496 bool CPicWindow::HasMultipleImages()
1498 return (((nDimensions > 1)||(nFrames > 1))&&(pSecondPic == NULL));
1501 void CPicWindow::CreateTrackbar(HWND hwndParent)
1503 HWND hwndTrack = CreateWindowEx(
1504 0, // no extended styles
1505 TRACKBAR_CLASS, // class name
1506 _T("Trackbar Control"), // title (caption)
1507 WS_CHILD | WS_VISIBLE | TBS_VERT | TBS_TOOLTIPS | TBS_AUTOTICKS, // style
1508 10, 10, // position
1509 200, 30, // size
1510 hwndParent, // parent window
1511 (HMENU)TRACKBAR_ID, // control identifier
1512 hInst, // instance
1513 NULL // no WM_CREATE parameter
1516 SendMessage(hwndTrack, TBM_SETRANGE,
1517 (WPARAM) TRUE, // redraw flag
1518 (LPARAM) MAKELONG(0, 16)); // min. & max. positions
1519 SendMessage(hwndTrack, TBM_SETTIPSIDE,
1520 (WPARAM) TBTS_TOP,
1521 (LPARAM) 0);
1523 m_AlphaSlider.ConvertTrackbarToNice(hwndTrack);
1526 void CPicWindow::BuildInfoString(TCHAR * buf, int size, bool bTooltip)
1528 // Unfortunately, we need two different strings for the tooltip
1529 // and the info box. Because the tooltips use a different tab size
1530 // than ExtTextOut(), and to keep the output aligned we therefore
1531 // need two different strings.
1532 // Note: some translations could end up with two identical strings, but
1533 // in English we need two - even if we wouldn't need two in English, some
1534 // translation might then need two again.
1535 if (pSecondPic && pTheOtherPic)
1537 _stprintf_s(buf, size,
1538 (TCHAR const *)ResString(hResource, bTooltip ? IDS_DUALIMAGEINFOTT : IDS_DUALIMAGEINFO),
1539 picture.GetFileSizeAsText().c_str(), picture.GetFileSizeAsText(false).c_str(),
1540 picture.m_Width, picture.m_Height,
1541 picture.GetHorizontalResolution(), picture.GetVerticalResolution(),
1542 picture.m_ColorDepth,
1543 (UINT)GetZoom(),
1544 pSecondPic->GetFileSizeAsText().c_str(), pSecondPic->GetFileSizeAsText(false).c_str(),
1545 pSecondPic->m_Width, pSecondPic->m_Height,
1546 pSecondPic->GetHorizontalResolution(), pSecondPic->GetVerticalResolution(),
1547 pSecondPic->m_ColorDepth,
1548 (UINT)pTheOtherPic->GetZoom());
1550 else
1552 _stprintf_s(buf, size,
1553 (TCHAR const *)ResString(hResource, bTooltip ? IDS_IMAGEINFOTT : IDS_IMAGEINFO),
1554 picture.GetFileSizeAsText().c_str(), picture.GetFileSizeAsText(false).c_str(),
1555 picture.m_Width, picture.m_Height,
1556 picture.GetHorizontalResolution(), picture.GetVerticalResolution(),
1557 picture.m_ColorDepth,
1558 (UINT)GetZoom());
1562 void CPicWindow::SetZoomToWidth( long width )
1564 m_linkedWidth = width;
1565 if (picture.m_Width)
1567 int zoom = width*100/picture.m_Width;
1568 SetZoom(zoom, false, true);
1572 void CPicWindow::SetZoomToHeight( long height )
1574 m_linkedHeight = height;
1575 if (picture.m_Height)
1577 int zoom = height*100/picture.m_Height;
1578 SetZoom(zoom, false, true);