Leaked source code of windows server 2003
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.

331 lines
11 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. if (ppsd == NULL)
  50. {
  51. break;
  52. }
  53. CPageSetupData *that = (CPageSetupData *) ppsd->lCustData;
  54. if (that == NULL)
  55. {
  56. break;
  57. }
  58. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR) ppsd);
  59. SendDlgItemMessage(hDlg, IDC_PERCENT_NORMAL_SIZE, EM_LIMITTEXT, 4, 0);
  60. SendDlgItemMessage(hDlg, IDC_PAGES_WIDE, EM_LIMITTEXT, 2, 0);
  61. SendDlgItemMessage(hDlg, IDC_PAGES_TALL, EM_LIMITTEXT, 2, 0);
  62. that->UpdateControls(hDlg);
  63. break;
  64. }
  65. case WM_COMMAND:
  66. {
  67. LPPAGESETUPDLG ppsd = (LPPAGESETUPDLG) GetWindowLongPtr(hDlg, GWLP_USERDATA);
  68. if (ppsd == NULL)
  69. {
  70. break;
  71. }
  72. CPageSetupData *that = (CPageSetupData *) ppsd->lCustData;
  73. if (that == NULL)
  74. {
  75. break;
  76. }
  77. switch (LOWORD(wParam))
  78. {
  79. case IDC_HORIZONTALLY:
  80. {
  81. if (HIWORD(wParam) == BN_CLICKED)
  82. {
  83. that->bCenterHorizontally = !that->bCenterHorizontally;
  84. that->UpdateControls(hDlg);
  85. }
  86. return TRUE;
  87. }
  88. case IDC_VERTICALLY:
  89. {
  90. if (HIWORD(wParam) == BN_CLICKED)
  91. {
  92. that->bCenterVertically = !that->bCenterVertically;
  93. that->UpdateControls(hDlg);
  94. }
  95. return TRUE;
  96. }
  97. case IDC_ADJUST_TO:
  98. {
  99. if (HIWORD(wParam) == BN_CLICKED)
  100. {
  101. that->bScaleFitTo = FALSE;
  102. that->UpdateControls(hDlg);
  103. }
  104. return TRUE;
  105. }
  106. case IDC_FIT_TO:
  107. {
  108. if (HIWORD(wParam) == BN_CLICKED)
  109. {
  110. that->bScaleFitTo = TRUE;
  111. that->UpdateControls(hDlg);
  112. }
  113. return TRUE;
  114. }
  115. case IDC_PERCENT_NORMAL_SIZE:
  116. {
  117. if (HIWORD(wParam) == EN_CHANGE)
  118. {
  119. UpdateValue(hDlg, IDC_PERCENT_NORMAL_SIZE, &that->nAdjustToPercent);
  120. }
  121. else if (HIWORD(wParam) == EN_KILLFOCUS)
  122. {
  123. SetDlgItemInt(hDlg, IDC_PERCENT_NORMAL_SIZE, that->nAdjustToPercent, FALSE);
  124. }
  125. return TRUE;
  126. }
  127. case IDC_PAGES_WIDE:
  128. {
  129. if (HIWORD(wParam) == EN_CHANGE)
  130. {
  131. UpdateValue(hDlg, IDC_PAGES_WIDE, &that->nFitToPagesWide);
  132. }
  133. else if (HIWORD(wParam) == EN_KILLFOCUS)
  134. {
  135. SetDlgItemInt(hDlg, IDC_PAGES_WIDE, that->nFitToPagesWide, FALSE);
  136. }
  137. return TRUE;
  138. }
  139. case IDC_PAGES_TALL:
  140. {
  141. if (HIWORD(wParam) == EN_CHANGE)
  142. {
  143. UpdateValue(hDlg, IDC_PAGES_TALL, &that->nFitToPagesTall);
  144. }
  145. else if (HIWORD(wParam) == EN_KILLFOCUS)
  146. {
  147. SetDlgItemInt(hDlg, IDC_PAGES_TALL, that->nFitToPagesTall, FALSE);
  148. }
  149. return TRUE;
  150. }
  151. }
  152. break;
  153. }
  154. }
  155. return FALSE;
  156. }
  157. /***************************************************************************/
  158. VOID
  159. CPageSetupData::CalculateImageRect(
  160. const CSize &PhysicalPageSize,
  161. CPoint &PhysicalOrigin,
  162. CSize &PhysicalScaledImageSize
  163. )
  164. {
  165. // Find the scaled image size and the total page size required to print that image
  166. LONG nPhysicalTotalPageWidth;
  167. LONG nPhysicalTotalPageHeight;
  168. if (bScaleFitTo)
  169. {
  170. nPhysicalTotalPageWidth = PhysicalPageSize.cx * nFitToPagesWide;
  171. nPhysicalTotalPageHeight = PhysicalPageSize.cy * nFitToPagesTall;
  172. // Keep the aspect ratio; try the match the width first and if it fails, match the height
  173. PhysicalScaledImageSize.cx = nPhysicalTotalPageWidth;
  174. PhysicalScaledImageSize.cy = (LONG) (fPhysicalImageHeight * nPhysicalTotalPageWidth / fPhysicalImageWidth);
  175. if (PhysicalScaledImageSize.cy > nPhysicalTotalPageHeight)
  176. {
  177. PhysicalScaledImageSize.cx = (LONG) (fPhysicalImageWidth * nPhysicalTotalPageHeight / fPhysicalImageHeight);
  178. PhysicalScaledImageSize.cy = nPhysicalTotalPageHeight;
  179. }
  180. }
  181. else
  182. {
  183. PhysicalScaledImageSize.cx = (LONG) (fPhysicalImageWidth * nAdjustToPercent / 100);
  184. PhysicalScaledImageSize.cy = (LONG) (fPhysicalImageHeight * nAdjustToPercent / 100);
  185. nFitToPagesWide = (PhysicalScaledImageSize.cx + PhysicalPageSize.cx - 1) / PhysicalPageSize.cx;
  186. nFitToPagesTall = (PhysicalScaledImageSize.cy + PhysicalPageSize.cy - 1) / PhysicalPageSize.cy;
  187. nPhysicalTotalPageWidth = PhysicalPageSize.cx * nFitToPagesWide;
  188. nPhysicalTotalPageHeight = PhysicalPageSize.cy * nFitToPagesTall;
  189. }
  190. PhysicalOrigin.x = bCenterHorizontally ? (nPhysicalTotalPageWidth - PhysicalScaledImageSize.cx) / 2 : 0;
  191. PhysicalOrigin.y = bCenterVertically ? (nPhysicalTotalPageHeight - PhysicalScaledImageSize.cy) / 2 : 0;
  192. }
  193. /***************************************************************************/
  194. UINT_PTR APIENTRY CPageSetupData::PagePaintHook(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  195. {
  196. switch (uMsg)
  197. {
  198. case WM_PSD_PAGESETUPDLG:
  199. {
  200. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR) lParam);
  201. break;
  202. }
  203. case WM_PSD_GREEKTEXTRECT:
  204. {
  205. LPPAGESETUPDLG ppsd = (LPPAGESETUPDLG) GetWindowLongPtr(hDlg, GWLP_USERDATA);
  206. if (ppsd == NULL)
  207. {
  208. break;
  209. }
  210. CPageSetupData *that = (CPageSetupData *) ppsd->lCustData;
  211. if (that == NULL)
  212. {
  213. break;
  214. }
  215. CRect *pOutputWindowRect = (CRect *) lParam;
  216. // Find the physical size of the page
  217. CSize PhysicalPageSize;
  218. PhysicalPageSize.cx = ppsd->ptPaperSize.x - ppsd->rtMargin.left - ppsd->rtMargin.right;
  219. PhysicalPageSize.cy = ppsd->ptPaperSize.y - ppsd->rtMargin.top - ppsd->rtMargin.bottom;
  220. CPoint PhysicalOrigin;
  221. CSize PhysicalScaledImageSize;
  222. that->CalculateImageRect(PhysicalPageSize, PhysicalOrigin, PhysicalScaledImageSize);
  223. // Find the scaling ratios for the preview window
  224. double fWidthRatio = (double) pOutputWindowRect->Width() / PhysicalPageSize.cx;
  225. double fHeightRatio = (double) pOutputWindowRect->Height() / PhysicalPageSize.cy;
  226. // Find the size of the image rectangle on the preview window
  227. CRect OutputImageRect;
  228. OutputImageRect.left = pOutputWindowRect->left + (int) (fWidthRatio * PhysicalOrigin.x);
  229. OutputImageRect.top = pOutputWindowRect->top + (int) (fHeightRatio * PhysicalOrigin.y);
  230. OutputImageRect.right = OutputImageRect.left + (int) (fWidthRatio * PhysicalScaledImageSize.cx);
  231. OutputImageRect.bottom = OutputImageRect.top + (int) (fHeightRatio * PhysicalScaledImageSize.cy);
  232. // Draw a rectangle with crossing lines
  233. CDC *pDC = CDC::FromHandle((HDC) wParam);
  234. CGdiObject *pOldPen = pDC->SelectStockObject(BLACK_PEN);
  235. CGdiObject *pOldBrush = pDC->SelectStockObject(LTGRAY_BRUSH);
  236. pDC->Rectangle(OutputImageRect);
  237. pDC->MoveTo(OutputImageRect.left, OutputImageRect.top);
  238. pDC->LineTo(OutputImageRect.right - 1, OutputImageRect.bottom - 1);
  239. pDC->MoveTo(OutputImageRect.left, OutputImageRect.bottom - 1);
  240. pDC->LineTo(OutputImageRect.right - 1, OutputImageRect.top);
  241. pDC->SelectObject(pOldPen);
  242. pDC->SelectObject(pOldBrush);
  243. return TRUE;
  244. }
  245. }
  246. return FALSE;
  247. }