ImageViewer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // ImageViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "CP_Main.h"
  5. #include "ImageViewer.h"
  6. #include "BitmapHelper.h"
  7. #include "memdc.h"
  8. // CImageViewer
  9. IMPLEMENT_DYNAMIC(CImageViewer, CWnd)
  10. CImageViewer::CImageViewer()
  11. {
  12. m_scrollHelper.AttachWnd(this);
  13. m_hoveringOverImage = false;
  14. m_pGdiplusBitmap = NULL;
  15. m_scale = 1;
  16. }
  17. CImageViewer::~CImageViewer()
  18. {
  19. delete m_pGdiplusBitmap;
  20. }
  21. BEGIN_MESSAGE_MAP(CImageViewer, CWnd)
  22. ON_WM_HSCROLL()
  23. ON_WM_VSCROLL()
  24. ON_WM_MOUSEWHEEL()
  25. ON_WM_PAINT()
  26. ON_WM_SIZE()
  27. ON_WM_SETCURSOR()
  28. ON_WM_LBUTTONUP()
  29. ON_WM_MOUSEHWHEEL()
  30. ON_WM_ERASEBKGND()
  31. ON_MESSAGE(WM_GESTURE, &CImageViewer::OnGesture)
  32. ON_MESSAGE(WM_GESTURENOTIFY, &CImageViewer::OnGestureNotify)
  33. END_MESSAGE_MAP()
  34. BOOL CImageViewer::Create(CWnd* pParent)
  35. {
  36. BOOL bSuccess;
  37. // Register window class
  38. CString csClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
  39. LoadCursor(NULL, IDC_ARROW),
  40. CBrush(::GetSysColor(COLOR_BTNFACE)));
  41. // If no parent supplied then try and get a pointer to it anyway
  42. if (!pParent)
  43. pParent = AfxGetMainWnd();
  44. // Create popup window
  45. //bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, // Extended style
  46. bSuccess = CreateEx(0,
  47. csClassName, // Classname
  48. _T(""), // Title
  49. WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, // style
  50. 0, 0, // position - updated soon.
  51. 390, 130, // Size - updated soon
  52. pParent->GetSafeHwnd(), // handle to parent
  53. 0, // No menu
  54. NULL);
  55. if (!bSuccess) return FALSE;
  56. return TRUE;
  57. }
  58. void CImageViewer::UpdateBitmapSize(bool setScale)
  59. {
  60. if (m_pGdiplusBitmap != NULL)
  61. {
  62. if (setScale)
  63. {
  64. BOOL newScaleImage = CGetSetOptions::GetScaleImagesToDescWindow();
  65. if (newScaleImage)
  66. {
  67. CRect rect;
  68. GetClientRect(rect);
  69. SCROLLINFO si;
  70. if (this->GetScrollInfo(SB_HORZ, &si) && si.nPage > 0)
  71. {
  72. int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
  73. rect.right += cxSB;
  74. int cySB = ::GetSystemMetrics(SM_CYHSCROLL);
  75. rect.bottom += cySB;
  76. }
  77. double w = m_pGdiplusBitmap->GetWidth();
  78. if (w > 0)
  79. {
  80. m_scale = rect.Width() / w;
  81. }
  82. }
  83. else
  84. {
  85. m_scale = 1;
  86. }
  87. m_scrollHelper.ScrollToOrigin(true, true);
  88. }
  89. m_scrollHelper.AttachWnd(this);
  90. m_scrollHelper.SetDisplaySize(m_pGdiplusBitmap->GetWidth(), m_pGdiplusBitmap->GetHeight(), m_scale);
  91. this->GetParent()->PostMessage(WM_REFRESH_FOOTER, 0, 0);
  92. }
  93. }
  94. void CImageViewer::OnPaint()
  95. {
  96. CPaintDC dc(this); // device context for painting
  97. CMemDCEx memDC(&dc);
  98. CRect rect;
  99. GetClientRect(rect);
  100. CBrush Brush, *pOldBrush;
  101. Brush.CreateSolidBrush(g_Opt.m_Theme.DescriptionWindowBG());
  102. pOldBrush = memDC.SelectObject(&Brush);
  103. memDC.FillRect(&rect, &Brush);
  104. if (m_pGdiplusBitmap)
  105. {
  106. int width = m_pGdiplusBitmap->GetWidth();
  107. int height = m_pGdiplusBitmap->GetHeight();
  108. Gdiplus::ImageAttributes attrs;
  109. CSize s = m_scrollHelper.GetScrollPos();
  110. Gdiplus::Graphics graphics(memDC);
  111. graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
  112. graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
  113. Gdiplus::Rect dest((int)0, (int)0, (int)rect.Width(), (int)rect.Height());
  114. double nW = rect.Width() * (1 / m_scale);
  115. double nH = rect.Height() * (1 / m_scale);
  116. graphics.DrawImage(m_pGdiplusBitmap, dest, s.cx, s.cy, nW, nH, Gdiplus::UnitPixel, &attrs);
  117. //OutputDebugString(StrF(_T("OnPaint, Width: %d, New Width: %d\r\n"), rect.Width(), (int)nW));
  118. }
  119. memDC.SelectObject(pOldBrush);
  120. }
  121. void CImageViewer::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  122. {
  123. //OutputDebugString(_T("OnHScroll\r\n"));
  124. m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
  125. }
  126. void CImageViewer::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  127. {
  128. //OutputDebugString(_T("OnVScroll\r\n"));
  129. m_scrollHelper.OnVScroll(nSBCode, nPos, pScrollBar);
  130. }
  131. BOOL CImageViewer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  132. {
  133. OutputDebugString(_T("OnMouseWheel\r\n"));
  134. if (nFlags == MK_CONTROL)
  135. {
  136. this->LockWindowUpdate();
  137. CPoint delta;
  138. double oldScale = m_scale;
  139. if (zDelta > 0)
  140. {
  141. m_scale += .1;
  142. }
  143. else
  144. {
  145. m_scale -= .1;
  146. }
  147. ::ScreenToClient(m_hWnd, &pt);
  148. UpdateBitmapSize(false);
  149. delta.x = round((pt.x * (1 / oldScale)) - (pt.x * (1 / m_scale)));
  150. delta.y = round((pt.y * (1 / oldScale)) - (pt.y * (1 / m_scale)));
  151. m_scrollHelper.Update(delta);
  152. this->Invalidate();
  153. this->UnlockWindowUpdate();
  154. }
  155. else
  156. {
  157. BOOL wasScrolled = m_scrollHelper.OnMouseWheel(nFlags, zDelta, pt);
  158. return wasScrolled;
  159. }
  160. return TRUE;
  161. }
  162. void CImageViewer::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
  163. {
  164. OutputDebugString(_T("OnMouseHWheel\r\n"));
  165. BOOL wasScrolled = m_scrollHelper.OnMouseHWheel(nFlags, -zDelta, pt);
  166. CWnd::OnMouseHWheel(nFlags, zDelta, pt);
  167. }
  168. void CImageViewer::OnSize(UINT nType, int cx, int cy)
  169. {
  170. CWnd::OnSize(nType, cx, cy);
  171. m_scrollHelper.OnSize(nType, cx, cy);
  172. }
  173. BOOL CImageViewer::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  174. {
  175. if (this->m_pGdiplusBitmap &&
  176. pWnd->m_hWnd == this->m_hWnd &&
  177. nHitTest == HTCLIENT)
  178. {
  179. if (CGetSetOptions::GetScaleImagesToDescWindow())
  180. {
  181. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_IN));
  182. }
  183. else
  184. {
  185. ::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR_ZOOM_OUT));
  186. }
  187. m_hoveringOverImage = true;
  188. return TRUE;
  189. }
  190. m_hoveringOverImage = false;
  191. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  192. }
  193. void CImageViewer::OnLButtonUp(UINT nFlags, CPoint point)
  194. {
  195. if (this->m_pGdiplusBitmap &&
  196. m_hoveringOverImage)
  197. {
  198. CGetSetOptions::SetScaleImagesToDescWindow(!CGetSetOptions::GetScaleImagesToDescWindow());
  199. UpdateBitmapSize(true);
  200. Invalidate();
  201. return;
  202. }
  203. CWnd::OnLButtonUp(nFlags, point);
  204. }
  205. BOOL CImageViewer::OnEraseBkgnd(CDC* pDC)
  206. {
  207. //OutputDebugString(_T("image viewer OnEraseBkgnd\r\n"));
  208. //return CWnd::OnEraseBkgnd(pDC);
  209. return FALSE;
  210. }
  211. LRESULT CImageViewer::OnGesture(WPARAM wParam, LPARAM lParam)
  212. {
  213. CPoint ptZoomCenter;
  214. double k;
  215. GESTUREINFO gi;
  216. ZeroMemory(&gi, sizeof(GESTUREINFO));
  217. gi.cbSize = sizeof(GESTUREINFO);
  218. BOOL bResult = GetGestureInfo((HGESTUREINFO)lParam, &gi);
  219. BOOL bHandled = FALSE;
  220. if (bResult) {
  221. // now interpret the gesture
  222. switch (gi.dwID) {
  223. case GID_ZOOM:
  224. //OutputDebugString(_T("zoom\r\n"));
  225. // Code for zooming goes here
  226. bHandled = TRUE;
  227. switch (gi.dwFlags)
  228. {
  229. case GF_BEGIN:
  230. m_dwArguments = LODWORD(gi.ullArguments);
  231. m_ptFirst.x = gi.ptsLocation.x;
  232. m_ptFirst.y = gi.ptsLocation.y;
  233. ::ScreenToClient(m_hWnd, &m_ptFirst);
  234. OutputDebugString(_T("zoom start\r\n"));
  235. break;
  236. case GF_END:
  237. OutputDebugString(_T("zoom end\r\n"));
  238. break;
  239. default:
  240. // We read here the second point of the gesture. This is middle point between
  241. // fingers in this new position.
  242. m_ptSecond.x = gi.ptsLocation.x;
  243. m_ptSecond.y = gi.ptsLocation.y;
  244. ::ScreenToClient(m_hWnd, &m_ptSecond);
  245. // We have to calculate zoom center point
  246. ptZoomCenter.x = (m_ptFirst.x + m_ptSecond.x) / 2;
  247. ptZoomCenter.y = (m_ptFirst.y + m_ptSecond.y) / 2;
  248. // The zoom factor is the ratio between the new and the old distance.
  249. // The new distance between two fingers is stored in gi.ullArguments
  250. // (lower DWORD) and the old distance is stored in _dwArguments.
  251. k = (double)(LODWORD(gi.ullArguments)) / (double)(m_dwArguments);
  252. // Now we process zooming in/out of the object
  253. //ProcessZoom(k, ptZoomCenter.x, ptZoomCenter.y);
  254. //m_scrollHelper.Update(ptZoomCenter);
  255. CString cs;
  256. cs.Format(_T("ZOOM k: %f, x: %d, y: %d\r\n"), k, ptZoomCenter.x, ptZoomCenter.y);
  257. OutputDebugString(cs);
  258. //InvalidateRect(hWnd, NULL, TRUE);
  259. // Now we have to store new information as a starting information
  260. // for the next step in this gesture.
  261. m_ptFirst = m_ptSecond;
  262. //m_dwArguments = LODWORD(gi.ullArguments);
  263. break;
  264. }
  265. break;
  266. case GID_PAN:
  267. //OutputDebugString(_T("pan\r\n"));
  268. // Code for panning goes here
  269. bHandled = TRUE;
  270. switch (gi.dwFlags)
  271. {
  272. case GF_BEGIN:
  273. m_ptFirst.x = gi.ptsLocation.x;
  274. m_ptFirst.y = gi.ptsLocation.y;
  275. ::ScreenToClient(m_hWnd, &m_ptFirst);
  276. break;
  277. default:
  278. // We read the second point of this gesture. It is a middle point
  279. // between fingers in this new position
  280. m_ptSecond.x = gi.ptsLocation.x;
  281. m_ptSecond.y = gi.ptsLocation.y;
  282. ::ScreenToClient(m_hWnd, &m_ptSecond);
  283. int xDiff = m_ptSecond.x - m_ptFirst.x;
  284. int yDiff = m_ptSecond.y - m_ptFirst.y;
  285. m_scrollHelper.Update(CPoint(-xDiff, -yDiff));
  286. //CString cs;
  287. //cs.Format(_T("x: %d, y: %d\r\n"), xDiff, yDiff);
  288. //OutputDebugString(cs);
  289. // We apply move operation of the object
  290. //ProcessMove(_ptSecond.x - _ptFirst.x, _ptSecond.y - _ptFirst.y);
  291. //InvalidateRect(hWnd, NULL, TRUE);
  292. // We have to copy second point into first one to prepare
  293. // for the next step of this gesture.
  294. m_ptFirst = m_ptSecond;
  295. break;
  296. }
  297. break;
  298. break;
  299. case GID_ROTATE:
  300. OutputDebugString(_T("rotate\r\n"));
  301. // Code for rotation goes here
  302. bHandled = TRUE;
  303. break;
  304. case GID_TWOFINGERTAP:
  305. OutputDebugString(_T("two finger\r\n"));
  306. // Code for two-finger tap goes here
  307. bHandled = TRUE;
  308. break;
  309. case GID_PRESSANDTAP:
  310. OutputDebugString(_T("press and tap\r\n"));
  311. // Code for roll over goes here
  312. bHandled = TRUE;
  313. break;
  314. default:
  315. OutputDebugString(_T("default\r\n"));
  316. // A gesture was not recognized
  317. break;
  318. }
  319. }
  320. else {
  321. DWORD dwErr = GetLastError();
  322. if (dwErr > 0) {
  323. OutputDebugString(_T("error\r\n"));
  324. //MessageBoxW(hWnd, L"Error!", L"Could not retrieve a GESTUREINFO structure.", MB_OK);
  325. }
  326. }
  327. return FALSE;
  328. }
  329. LRESULT CImageViewer::OnGestureNotify(WPARAM wParam, LPARAM lParam)
  330. {
  331. // This is the right place to define the list of gestures that this
  332. // application will support. By populating GESTURECONFIG structure
  333. // and calling SetGestureConfig function. We can choose gestures
  334. // that we want to handle in our application. In this app we
  335. // decide to handle all gestures.
  336. GESTURECONFIG gc = {
  337. 0, // gesture ID
  338. GC_ALLGESTURES,
  339. //GC_PAN | GC_PAN_WITH_SINGLE_FINGER_VERTICALLY | GC_PAN_WITH_SINGLE_FINGER_HORIZONTALLY | GC_PAN_WITH_GUTTER | GC_PAN_WITH_INERTIA, // settings related to gesture ID that are to be
  340. // turned on
  341. 0 // settings related to gesture ID that are to be
  342. // turned off
  343. };
  344. BOOL bResult = ::SetGestureConfig(
  345. m_hWnd, // window for which configuration is specified
  346. 0, // reserved, must be 0
  347. 1, // count of GESTURECONFIG structures
  348. &gc, // array of GESTURECONFIG structures, dwIDs will be processed in the
  349. // order specified and repeated occurances will overwrite previous ones
  350. sizeof(GESTURECONFIG) // sizeof(GESTURECONFIG)
  351. );
  352. return TRUE;
  353. }