ImageViewer.cpp 13 KB

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