Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.9 KiB

  1. //
  2. // pgsetup.cpp
  3. //
  4. // implementation of the CPageSetupData class
  5. //
  6. #include "stdafx.h"
  7. #include "resource.h"
  8. #include "dlgs.h"
  9. #include "pgsetup.h"
  10. /***************************************************************************/
  11. #define FIXED_FLOATPT_MULTDIV 100000
  12. /***************************************************************************/
  13. VOID CPageSetupData::UpdateControls(HWND hDlg)
  14. {
  15. CheckDlgButton(hDlg, IDC_HORIZONTALLY, bCenterHorizontally);
  16. CheckDlgButton(hDlg, IDC_VERTICALLY, bCenterVertically);
  17. CheckDlgButton(hDlg, IDC_ADJUST_TO, !bScaleFitTo);
  18. SetDlgItemInt(hDlg, IDC_PERCENT_NORMAL_SIZE, nAdjustToPercent, FALSE);
  19. EnableWindow(GetDlgItem(hDlg, IDC_PERCENT_NORMAL_SIZE), !bScaleFitTo);
  20. EnableWindow(GetDlgItem(hDlg, IDC_STR_PERCENT_NORMAL_SIZE), !bScaleFitTo);
  21. CheckDlgButton(hDlg, IDC_FIT_TO, bScaleFitTo);
  22. SetDlgItemInt(hDlg, IDC_PAGES_WIDE, nFitToPagesWide, FALSE);
  23. EnableWindow(GetDlgItem(hDlg, IDC_PAGES_WIDE), bScaleFitTo);
  24. EnableWindow(GetDlgItem(hDlg, IDC_STR_PAGES_WIDE), bScaleFitTo);
  25. SetDlgItemInt(hDlg, IDC_PAGES_TALL, nFitToPagesTall, FALSE);
  26. EnableWindow(GetDlgItem(hDlg, IDC_PAGES_TALL), bScaleFitTo);
  27. EnableWindow(GetDlgItem(hDlg, IDC_STR_PAGES_TALL), bScaleFitTo);
  28. InvalidateRect(GetDlgItem(hDlg, rct1), 0, TRUE);
  29. }
  30. /***************************************************************************/
  31. VOID CPageSetupData::UpdateValue(HWND hDlg, int nIDDlgItem, UINT *pnResult)
  32. {
  33. BOOL bTranslated;
  34. UINT nResult = GetDlgItemInt(hDlg, nIDDlgItem, &bTranslated, FALSE);
  35. if (bTranslated && nResult != 0)
  36. {
  37. *pnResult = nResult;
  38. InvalidateRect(GetDlgItem(hDlg, rct1), 0, TRUE);
  39. }
  40. }
  41. /***************************************************************************/
  42. UINT_PTR APIENTRY CPageSetupData::PageSetupHook(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  43. {
  44. switch (uMsg)
  45. {
  46. case WM_INITDIALOG:
  47. {
  48. LPPAGESETUPDLG ppsd = (LPPAGESETUPDLG) lParam;
  49. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR) lParam);
  50. CPageSetupData *that = (CPageSetupData *) ppsd->lCustData;
  51. SendDlgItemMessage(hDlg, IDC_PERCENT_NORMAL_SIZE, EM_LIMITTEXT, 4, 0);
  52. SendDlgItemMessage(hDlg, IDC_PAGES_WIDE, EM_LIMITTEXT, 2, 0);
  53. SendDlgItemMessage(hDlg, IDC_PAGES_TALL, EM_LIMITTEXT, 2, 0);
  54. that->UpdateControls(hDlg);
  55. break;
  56. }
  57. case WM_COMMAND:
  58. {
  59. LPPAGESETUPDLG ppsd = (LPPAGESETUPDLG) GetWindowLongPtr(hDlg, GWLP_USERDATA);
  60. CPageSetupData *that = ppsd ? (CPageSetupData *) ppsd->lCustData : 0;
  61. switch (LOWORD(wParam))
  62. {
  63. case IDC_HORIZONTALLY:
  64. {
  65. if (HIWORD(wParam) == BN_CLICKED)
  66. {
  67. that->bCenterHorizontally = !that->bCenterHorizontally;
  68. that->UpdateControls(hDlg);
  69. }
  70. return TRUE;
  71. }
  72. case IDC_VERTICALLY:
  73. {
  74. if (HIWORD(wParam) == BN_CLICKED)
  75. {
  76. that->bCenterVertically = !that->bCenterVertically;
  77. that->UpdateControls(hDlg);
  78. }
  79. return TRUE;
  80. }
  81. case IDC_ADJUST_TO:
  82. {
  83. if (HIWORD(wParam) == BN_CLICKED)
  84. {
  85. that->bScaleFitTo = FALSE;
  86. that->UpdateControls(hDlg);
  87. }
  88. return TRUE;
  89. }
  90. case IDC_FIT_TO:
  91. {
  92. if (HIWORD(wParam) == BN_CLICKED)
  93. {
  94. that->bScaleFitTo = TRUE;
  95. that->UpdateControls(hDlg);
  96. }
  97. return TRUE;
  98. }
  99. case IDC_PERCENT_NORMAL_SIZE:
  100. {
  101. if (HIWORD(wParam) == EN_CHANGE)
  102. {
  103. UpdateValue(hDlg, IDC_PERCENT_NORMAL_SIZE, &that->nAdjustToPercent);
  104. }
  105. else if (HIWORD(wParam) == EN_KILLFOCUS)
  106. {
  107. SetDlgItemInt(hDlg, IDC_PERCENT_NORMAL_SIZE, that->nAdjustToPercent, FALSE);
  108. }
  109. return TRUE;
  110. }
  111. case IDC_PAGES_WIDE:
  112. {
  113. if (HIWORD(wParam) == EN_CHANGE)
  114. {
  115. UpdateValue(hDlg, IDC_PAGES_WIDE, &that->nFitToPagesWide);
  116. }
  117. else if (HIWORD(wParam) == EN_KILLFOCUS)
  118. {
  119. SetDlgItemInt(hDlg, IDC_PAGES_WIDE, that->nFitToPagesWide, FALSE);
  120. }
  121. return TRUE;
  122. }
  123. case IDC_PAGES_TALL:
  124. {
  125. if (HIWORD(wParam) == EN_CHANGE)
  126. {
  127. UpdateValue(hDlg, IDC_PAGES_TALL, &that->nFitToPagesTall);
  128. }
  129. else if (HIWORD(wParam) == EN_KILLFOCUS)
  130. {
  131. SetDlgItemInt(hDlg, IDC_PAGES_TALL, that->nFitToPagesTall, FALSE);
  132. }
  133. return TRUE;
  134. }
  135. }
  136. break;
  137. }
  138. }
  139. return FALSE;
  140. }
  141. /***************************************************************************/
  142. VOID
  143. CPageSetupData::CalculateImageRect(
  144. const CSize &PhysicalPageSize,
  145. CPoint &PhysicalOrigin,
  146. CSize &PhysicalScaledImageSize
  147. )
  148. {
  149. // Find the scaled image size and the total page size required to print that image
  150. LONG nPhysicalTotalPageWidth;
  151. LONG nPhysicalTotalPageHeight;
  152. if (bScaleFitTo)
  153. {
  154. nPhysicalTotalPageWidth = PhysicalPageSize.cx * nFitToPagesWide;
  155. nPhysicalTotalPageHeight = PhysicalPageSize.cy * nFitToPagesTall;
  156. // Keep the aspect ratio; try the match the width first and if it fails, match the height
  157. PhysicalScaledImageSize.cx = nPhysicalTotalPageWidth;
  158. PhysicalScaledImageSize.cy = (LONG) (fPhysicalImageHeight * nPhysicalTotalPageWidth / fPhysicalImageWidth);
  159. if (PhysicalScaledImageSize.cy > nPhysicalTotalPageHeight)
  160. {
  161. PhysicalScaledImageSize.cx = (LONG) (fPhysicalImageWidth * nPhysicalTotalPageHeight / fPhysicalImageHeight);
  162. PhysicalScaledImageSize.cy = nPhysicalTotalPageHeight;
  163. }
  164. }
  165. else
  166. {
  167. PhysicalScaledImageSize.cx = (LONG) (fPhysicalImageWidth * nAdjustToPercent / 100);
  168. PhysicalScaledImageSize.cy = (LONG) (fPhysicalImageHeight * nAdjustToPercent / 100);
  169. nFitToPagesWide = (PhysicalScaledImageSize.cx + PhysicalPageSize.cx - 1) / PhysicalPageSize.cx;
  170. nFitToPagesTall = (PhysicalScaledImageSize.cy + PhysicalPageSize.cy - 1) / PhysicalPageSize.cy;
  171. nPhysicalTotalPageWidth = PhysicalPageSize.cx * nFitToPagesWide;
  172. nPhysicalTotalPageHeight = PhysicalPageSize.cy * nFitToPagesTall;
  173. }
  174. PhysicalOrigin.x = bCenterHorizontally ? (nPhysicalTotalPageWidth - PhysicalScaledImageSize.cx) / 2 : 0;
  175. PhysicalOrigin.y = bCenterVertically ? (nPhysicalTotalPageHeight - PhysicalScaledImageSize.cy) / 2 : 0;
  176. }
  177. /***************************************************************************/
  178. UINT_PTR APIENTRY CPageSetupData::PagePaintHook(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  179. {
  180. switch (uMsg)
  181. {
  182. case WM_PSD_PAGESETUPDLG:
  183. {
  184. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR) lParam);
  185. break;
  186. }
  187. case WM_PSD_GREEKTEXTRECT:
  188. {
  189. LPPAGESETUPDLG ppsd = (LPPAGESETUPDLG) GetWindowLongPtr(hDlg, GWLP_USERDATA);
  190. CPageSetupData *that = (CPageSetupData *) ppsd->lCustData;
  191. CRect *pOutputWindowRect = (CRect *) lParam;
  192. // Find the physical size of the page
  193. CSize PhysicalPageSize;
  194. PhysicalPageSize.cx = ppsd->ptPaperSize.x - ppsd->rtMargin.left - ppsd->rtMargin.right;
  195. PhysicalPageSize.cy = ppsd->ptPaperSize.y - ppsd->rtMargin.top - ppsd->rtMargin.bottom;
  196. CPoint PhysicalOrigin;
  197. CSize PhysicalScaledImageSize;
  198. that->CalculateImageRect(PhysicalPageSize, PhysicalOrigin, PhysicalScaledImageSize);
  199. // Find the scaling ratios for the preview window
  200. double fWidthRatio = (double) pOutputWindowRect->Width() / PhysicalPageSize.cx;
  201. double fHeightRatio = (double) pOutputWindowRect->Height() / PhysicalPageSize.cy;
  202. // Find the size of the image rectangle on the preview window
  203. CRect OutputImageRect;
  204. OutputImageRect.left = pOutputWindowRect->left + (int) (fWidthRatio * PhysicalOrigin.x);
  205. OutputImageRect.top = pOutputWindowRect->top + (int) (fHeightRatio * PhysicalOrigin.y);
  206. OutputImageRect.right = OutputImageRect.left + (int) (fWidthRatio * PhysicalScaledImageSize.cx);
  207. OutputImageRect.bottom = OutputImageRect.top + (int) (fHeightRatio * PhysicalScaledImageSize.cy);
  208. // Draw a rectangle with crossing lines
  209. CDC *pDC = CDC::FromHandle((HDC) wParam);
  210. CGdiObject *pOldPen = pDC->SelectStockObject(BLACK_PEN);
  211. CGdiObject *pOldBrush = pDC->SelectStockObject(LTGRAY_BRUSH);
  212. pDC->Rectangle(OutputImageRect);
  213. pDC->MoveTo(OutputImageRect.left, OutputImageRect.top);
  214. pDC->LineTo(OutputImageRect.right - 1, OutputImageRect.bottom - 1);
  215. pDC->MoveTo(OutputImageRect.left, OutputImageRect.bottom - 1);
  216. pDC->LineTo(OutputImageRect.right - 1, OutputImageRect.top);
  217. pDC->SelectObject(pOldPen);
  218. pDC->SelectObject(pOldBrush);
  219. return TRUE;
  220. }
  221. }
  222. return FALSE;
  223. }