PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap13/PopPad/PopPrnt.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 217 lines | 160 code | 40 blank | 17 comment | 24 complexity | 96e164f23fc6ae3ccde22854362384e9 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module PopPrnt;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. pragma(lib, "winmm.lib");
  21. import core.sys.windows.windef;
  22. import core.sys.windows.winuser;
  23. import core.sys.windows.wingdi;
  24. import core.sys.windows.winbase;
  25. import core.sys.windows.commdlg;
  26. import core.sys.windows.mmsystem;
  27. import resource;
  28. BOOL bUserAbort;
  29. HWND hDlgPrint;
  30. extern (Windows)
  31. BOOL PrintDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  32. {
  33. switch (msg)
  34. {
  35. case WM_INITDIALOG:
  36. EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  37. return TRUE;
  38. case WM_COMMAND:
  39. bUserAbort = TRUE;
  40. EnableWindow(GetParent(hDlg), TRUE);
  41. DestroyWindow(hDlg);
  42. hDlgPrint = NULL;
  43. return TRUE;
  44. default:
  45. }
  46. return FALSE;
  47. }
  48. extern (Windows)
  49. BOOL AbortProc(HDC hPrinterDC, int iCode)
  50. {
  51. MSG msg;
  52. while (!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  53. {
  54. if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
  55. {
  56. TranslateMessage(&msg);
  57. DispatchMessage(&msg);
  58. }
  59. }
  60. return !bUserAbort;
  61. }
  62. BOOL PopPrntPrintFile(HINSTANCE hInst, HWND hwnd, HWND hwndEdit, PTSTR szTitleName)
  63. {
  64. static DOCINFO di = DOCINFO(DOCINFO.sizeof);
  65. static PRINTDLG pd;
  66. BOOL bSuccess;
  67. int yChar, iCharsPerLine, iLinesPerPage, iTotalLines, iTotalPages, iPage, iLine, iLineNum;
  68. PTSTR pstrBuffer;
  69. TCHAR[64 + MAX_PATH] szJobName;
  70. TEXTMETRIC tm;
  71. WORD iColCopy, iNoiColCopy;
  72. // Invoke Print common dialog box
  73. pd.hwndOwner = hwnd;
  74. pd.hDevMode = NULL;
  75. pd.hDevNames = NULL;
  76. pd.hDC = NULL;
  77. pd.Flags = PD_ALLPAGES | PD_COLLATE |
  78. PD_RETURNDC | PD_NOSELECTION;
  79. pd.nFromPage = 0;
  80. pd.nToPage = 0;
  81. pd.nMinPage = 0;
  82. pd.nMaxPage = 0;
  83. pd.nCopies = 1;
  84. pd.hInstance = NULL;
  85. pd.lCustData = 0L;
  86. pd.lpfnPrintHook = NULL;
  87. pd.lpfnSetupHook = NULL;
  88. pd.lpPrintTemplateName = NULL;
  89. pd.lpSetupTemplateName = NULL;
  90. pd.hPrintTemplate = NULL;
  91. pd.hSetupTemplate = NULL;
  92. if (!PrintDlg(&pd))
  93. return TRUE;
  94. iTotalLines = SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0);
  95. if (iTotalLines == 0)
  96. return TRUE;
  97. // Calculate necessary metrics for file
  98. GetTextMetrics(pd.hDC, &tm);
  99. yChar = tm.tmHeight + tm.tmExternalLeading;
  100. iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES) / tm.tmAveCharWidth;
  101. iLinesPerPage = GetDeviceCaps(pd.hDC, VERTRES) / yChar;
  102. iTotalPages = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage;
  103. // Allocate a buffer for each line of text
  104. pstrBuffer = cast(typeof(pstrBuffer))GC.malloc(TCHAR.sizeof * (iCharsPerLine + 1));
  105. // Display the printing dialog box
  106. EnableWindow(hwnd, FALSE);
  107. bSuccess = TRUE;
  108. bUserAbort = FALSE;
  109. hDlgPrint = CreateDialog(hInst, "PrintDlgBox", hwnd, &PrintDlgProc);
  110. SetDlgItemText(hDlgPrint, IDC_FILENAME, szTitleName);
  111. // @BUG@ WindowsAPI callbacks are not defined properly:
  112. // alias BOOL function(HDC, int) ABORTPROC;
  113. //
  114. // should be:
  115. // alias extern(Windows) BOOL function(HDC, int) ABORTPROC;
  116. SetAbortProc(pd.hDC, cast(BOOL function(HDC, int))&AbortProc);
  117. // Start the document
  118. GetWindowText(hwnd, szJobName.ptr, szJobName.sizeof);
  119. di.lpszDocName = szJobName.ptr;
  120. if (StartDoc(pd.hDC, &di) > 0)
  121. {
  122. // Collation requires this loop and iNoiColCopy
  123. for (iColCopy = 0;
  124. iColCopy < (cast(WORD)pd.Flags & PD_COLLATE ? pd.nCopies : 1);
  125. iColCopy++)
  126. {
  127. for (iPage = 0; iPage < iTotalPages; iPage++)
  128. {
  129. for (iNoiColCopy = 0;
  130. iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies);
  131. iNoiColCopy++)
  132. {
  133. // Start the page
  134. if (StartPage(pd.hDC) < 0)
  135. {
  136. bSuccess = FALSE;
  137. break;
  138. }
  139. // For each page, print the lines
  140. for (iLine = 0; iLine < iLinesPerPage; iLine++)
  141. {
  142. iLineNum = iLinesPerPage * iPage + iLine;
  143. if (iLineNum > iTotalLines)
  144. break;
  145. *cast(int*)pstrBuffer = iCharsPerLine;
  146. TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer,
  147. cast(int)SendMessage(hwndEdit, EM_GETLINE,
  148. cast(WPARAM)iLineNum, cast(LPARAM)pstrBuffer));
  149. }
  150. if (EndPage(pd.hDC) < 0)
  151. {
  152. bSuccess = FALSE;
  153. break;
  154. }
  155. if (bUserAbort)
  156. break;
  157. }
  158. if (!bSuccess || bUserAbort)
  159. break;
  160. }
  161. if (!bSuccess || bUserAbort)
  162. break;
  163. }
  164. }
  165. else
  166. bSuccess = FALSE;
  167. if (bSuccess)
  168. EndDoc(pd.hDC);
  169. if (!bUserAbort)
  170. {
  171. EnableWindow(hwnd, TRUE);
  172. DestroyWindow(hDlgPrint);
  173. }
  174. GC.free(pstrBuffer);
  175. DeleteDC(pd.hDC);
  176. return bSuccess && !bUserAbort;
  177. }