ReactOS 0.4.16-dev-109-gf4cb10f
printing.c File Reference
#include "notepad.h"
#include <strsafe.h>
#include <assert.h>
Include dependency graph for printing.c:

Go to the source code of this file.

Classes

struct  PRINT_DATA
 

Macros

#define CONVERT_X(x)   MulDiv((x), iLogPixelsX, 2540) /* 100th millimeters to pixels */
 
#define CONVERT_Y(y)   MulDiv((y), iLogPixelsY, 2540) /* 100th millimeters to pixels */
 
#define X_POINTS_TO_PIXELS(hDC, points)   MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSX), 72)
 
#define Y_POINTS_TO_PIXELS(hDC, points)   MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSY), 72)
 
#define TAB_STOP   8
 
#define DO_FLUSH()
 
#define BODY_FONT_SIZE   10 /* 10pt */
 
#define HEADER_FONT_SIZE   9 /* 9pt */
 
#define SPACING_HEIGHT   4 /* 4pt */
 
#define PRINTING_MESSAGE   (WM_USER + 100)
 

Typedefs

typedef struct PRINT_DATAPPRINT_DATA
 

Functions

static VOID AlertPrintError (VOID)
 
static RECT GetPrintingRect (IN HDC hdc, IN LPCRECT pMargins)
 
static INT GetSelectionTextLength (HWND hWnd)
 
static INT GetSelectionText (HWND hWnd, LPTSTR lpString, INT nMaxCount)
 
static VOID DrawHeaderOrFooter (HDC hDC, LPRECT pRect, LPCTSTR pszFormat, INT nPageNo, const SYSTEMTIME *pstNow)
 
static BOOL DoPrintBody (PPRINT_DATA pData, DWORD PageCount, BOOL bSkipPage)
 
static BOOL DoPrintPage (PPRINT_DATA pData, DWORD PageCount)
 
static BOOL DoCreatePrintFonts (LPPRINTDLG pPrinter, PPRINT_DATA pPrintData)
 
static BOOL DoPrintDocument (PPRINT_DATA printData)
 
static DWORD WINAPI PrintThreadFunc (LPVOID arg)
 
static INT_PTR CALLBACK DIALOG_Printing_DialogProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
VOID DIALOG_FilePrint (VOID)
 
static UINT_PTR CALLBACK DIALOG_PAGESETUP_Hook (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
VOID DIALOG_FilePageSetup (VOID)
 

Macro Definition Documentation

◆ BODY_FONT_SIZE

#define BODY_FONT_SIZE   10 /* 10pt */

Definition at line 361 of file printing.c.

◆ CONVERT_X

#define CONVERT_X (   x)    MulDiv((x), iLogPixelsX, 2540) /* 100th millimeters to pixels */

◆ CONVERT_Y

#define CONVERT_Y (   y)    MulDiv((y), iLogPixelsY, 2540) /* 100th millimeters to pixels */

◆ DO_FLUSH

#define DO_FLUSH ( )
Value:
do { \
if (ichStart < pData->ich && !bSkipPage) { \
TextOut(pPrinter->hDC, xStart, yTop, &pData->pszText[ichStart], pData->ich - ichStart); \
} \
ichStart = pData->ich; \
xStart = xLeft; \
if (pData->status == STRING_PRINTCANCELING) return FALSE; \
} while (0)
#define FALSE
Definition: types.h:117
#define STRING_PRINTCANCELING
Definition: notepad_res.h:94
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830

◆ HEADER_FONT_SIZE

#define HEADER_FONT_SIZE   9 /* 9pt */

Definition at line 362 of file printing.c.

◆ PRINTING_MESSAGE

#define PRINTING_MESSAGE   (WM_USER + 100)

Definition at line 364 of file printing.c.

◆ SPACING_HEIGHT

#define SPACING_HEIGHT   4 /* 4pt */

Definition at line 363 of file printing.c.

◆ TAB_STOP

#define TAB_STOP   8

◆ X_POINTS_TO_PIXELS

#define X_POINTS_TO_PIXELS (   hDC,
  points 
)    MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSX), 72)

Definition at line 116 of file printing.c.

◆ Y_POINTS_TO_PIXELS

#define Y_POINTS_TO_PIXELS (   hDC,
  points 
)    MulDiv((points), GetDeviceCaps((hDC), LOGPIXELSY), 72)

Definition at line 117 of file printing.c.

Typedef Documentation

◆ PPRINT_DATA

Function Documentation

◆ AlertPrintError()

static VOID AlertPrintError ( VOID  )
static

Definition at line 16 of file printing.c.

17{
18 TCHAR szUntitled[MAX_STRING_LEN];
19
20 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, _countof(szUntitled));
21
23 Globals.szFileName[0] ? Globals.szFileName : szUntitled,
25}
#define MAX_STRING_LEN
Definition: precomp.h:36
int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCTSTR szString, DWORD dwFlags)
Definition: dialog.c:193
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
#define STRING_PRINTERROR
Definition: notepad_res.h:90
#define STRING_UNTITLED
Definition: notepad_res.h:67
#define _countof(array)
Definition: sndvol32.h:70
HINSTANCE hInstance
Definition: precomp.h:43
#define MB_ICONEXCLAMATION
Definition: winuser.h:788
#define MB_OK
Definition: winuser.h:793
#define LoadString
Definition: winuser.h:5831
char TCHAR
Definition: xmlstorage.h:189

Referenced by DIALOG_FilePrint().

◆ DIALOG_FilePageSetup()

VOID DIALOG_FilePageSetup ( VOID  )

Definition at line 670 of file printing.c.

671{
673
674 ZeroMemory(&page, sizeof(page));
675 page.lStructSize = sizeof(page);
676 page.hwndOwner = Globals.hMainWnd;
678 page.hInstance = Globals.hInstance;
679 page.rtMargin = Globals.lMargins;
680 page.hDevMode = Globals.hDevMode;
681 page.hDevNames = Globals.hDevNames;
682 page.lpPageSetupTemplateName = MAKEINTRESOURCE(DIALOG_PAGESETUP);
683 page.lpfnPageSetupHook = DIALOG_PAGESETUP_Hook;
684
686
687 /* NOTE: Even if PageSetupDlg returns FALSE, the following members may have changed */
688 Globals.hDevMode = page.hDevMode;
689 Globals.hDevNames = page.hDevNames;
690 Globals.lMargins = page.rtMargin;
691}
#define PSD_ENABLEPAGESETUPHOOK
Definition: commdlg.h:183
#define PSD_ENABLEPAGESETUPTEMPLATE
Definition: commdlg.h:184
#define PSD_MARGINS
Definition: commdlg.h:173
#define PageSetupDlg
Definition: commdlg.h:667
#define DIALOG_PAGESETUP
Definition: notepad_res.h:12
static UINT_PTR CALLBACK DIALOG_PAGESETUP_Hook(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: printing.c:621
Definition: module.h:576
#define ZeroMemory
Definition: winbase.h:1712
#define MAKEINTRESOURCE
Definition: winuser.h:591

Referenced by NOTEPAD_MenuCommand().

◆ DIALOG_FilePrint()

VOID DIALOG_FilePrint ( VOID  )

Definition at line 565 of file printing.c.

566{
567 BOOL ret;
568 LPPRINTDLG printer;
569 PPRINT_DATA printData = LocalAlloc(LPTR, sizeof(PRINT_DATA));
570 if (!printData)
571 {
573 return;
574 }
575
576 printer = &printData->printer;
577 printer->lStructSize = sizeof(PRINTDLG);
578 printer->hwndOwner = Globals.hMainWnd;
579 printer->Flags = PD_RETURNDC | PD_SELECTION;
580
581 /* Disable the selection radio button if there is no text selected */
583 printer->Flags |= PD_NOSELECTION;
584
585 printer->nFromPage = 1;
586 printer->nToPage = MAXWORD;
587 printer->nMinPage = 1;
588 printer->nMaxPage = MAXWORD;
589
590 printer->hDevMode = Globals.hDevMode;
591 printer->hDevNames = Globals.hDevNames;
592
593 ret = PrintDlg(printer);
594 /* NOTE: Even if PrintDlg returns FALSE, hDevMode and hDevNames may have changed. */
595 Globals.hDevMode = printer->hDevMode;
596 Globals.hDevNames = printer->hDevNames;
597
598 if (!ret)
599 {
600 LocalFree(printData);
601 return; /* The user canceled printing */
602 }
603 assert(printer->hDC != NULL);
604
605 /* Ensure that each logical unit maps to one pixel */
606 SetMapMode(printer->hDC, MM_TEXT);
607
612 (LPARAM)printer) == IDABORT)
613 {
615 }
616}
VOID ShowLastError(VOID)
Definition: dialog.c:45
#define PD_NOSELECTION
Definition: commdlg.h:149
#define PD_SELECTION
Definition: commdlg.h:147
#define PrintDlg
Definition: commdlg.h:668
#define PD_RETURNDC
Definition: commdlg.h:155
PRINTDLGA PRINTDLG
Definition: commdlg.h:660
#define NULL
Definition: types.h:112
#define assert(x)
Definition: debug.h:53
unsigned int BOOL
Definition: ntddk_ex.h:94
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define DIALOG_PRINTING
Definition: notepad_res.h:21
#define MAXWORD
static INT_PTR CALLBACK DIALOG_Printing_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: printing.c:493
static INT GetSelectionTextLength(HWND hWnd)
Definition: printing.c:57
static VOID AlertPrintError(VOID)
Definition: printing.c:16
PRINTDLG printer
Definition: printing.c:99
WORD nMaxPage
Definition: commdlg.h:474
HDC hDC
Definition: commdlg.h:469
DWORD Flags
Definition: commdlg.h:470
WORD nFromPage
Definition: commdlg.h:471
HGLOBAL hDevMode
Definition: commdlg.h:467
WORD nToPage
Definition: commdlg.h:472
WORD nMinPage
Definition: commdlg.h:473
HGLOBAL hDevNames
Definition: commdlg.h:468
HWND hwndOwner
Definition: commdlg.h:466
DWORD lStructSize
Definition: commdlg.h:465
int ret
#define LPTR
Definition: winbase.h:381
LONG_PTR LPARAM
Definition: windef.h:208
int WINAPI SetMapMode(_In_ HDC, _In_ int)
#define MM_TEXT
Definition: wingdi.h:873
#define DialogBoxParam
Definition: winuser.h:5776
#define IDABORT
Definition: winuser.h:835

Referenced by HandleCommandLine(), and NOTEPAD_MenuCommand().

◆ DIALOG_PAGESETUP_Hook()

static UINT_PTR CALLBACK DIALOG_PAGESETUP_Hook ( HWND  hDlg,
UINT  uMsg,
WPARAM  wParam,
LPARAM  lParam 
)
static

Definition at line 621 of file printing.c.

622{
623 switch (uMsg)
624 {
625 case WM_INITDIALOG:
626 /* fetch last user input prior to display dialog */
627 SetDlgItemText(hDlg, 0x141, Globals.szHeader);
628 SetDlgItemText(hDlg, 0x143, Globals.szFooter);
629 break;
630
631 case WM_COMMAND:
632 {
633 if (HIWORD(wParam) == BN_CLICKED)
634 {
635 switch (LOWORD(wParam))
636 {
637 case IDOK:
638 /* save user input and close dialog */
639 GetDlgItemText(hDlg, 0x141, Globals.szHeader, _countof(Globals.szHeader));
640 GetDlgItemText(hDlg, 0x143, Globals.szFooter, _countof(Globals.szFooter));
641 return FALSE;
642
643 case IDCANCEL:
644 /* discard user input and close dialog */
645 return FALSE;
646
647 case IDHELP:
648 {
649 /* FIXME: Bring this to work */
650 static const TCHAR sorry[] = _T("Sorry, no help available");
651 static const TCHAR help[] = _T("Help");
653 return TRUE;
654 }
655
656 default:
657 break;
658 }
659 }
660 break;
661 }
662 }
663
664 return FALSE;
665}
int help
Definition: sort.c:20
WPARAM wParam
Definition: combotst.c:138
#define TRUE
Definition: types.h:120
#define LOWORD(l)
Definition: pedump.c:82
#define IDHELP
Definition: resource_2.h:8
#define HIWORD(l)
Definition: typedefs.h:247
#define _T(x)
Definition: vfdio.h:22
#define IDCANCEL
Definition: winuser.h:834
#define GetDlgItemText
Definition: winuser.h:5797
#define WM_COMMAND
Definition: winuser.h:1743
#define WM_INITDIALOG
Definition: winuser.h:1742
#define IDOK
Definition: winuser.h:833
#define MessageBox
Definition: winuser.h:5834
#define BN_CLICKED
Definition: winuser.h:1928
#define SetDlgItemText
Definition: winuser.h:5861

Referenced by DIALOG_FilePageSetup().

◆ DIALOG_Printing_DialogProc()

static INT_PTR CALLBACK DIALOG_Printing_DialogProc ( HWND  hwnd,
UINT  uMsg,
WPARAM  wParam,
LPARAM  lParam 
)
static

Definition at line 493 of file printing.c.

494{
495 TCHAR szText[MAX_STRING_LEN];
496 static TCHAR s_szPage[64];
497 static PPRINT_DATA s_pData = NULL;
498 static HANDLE s_hThread = NULL;
499
500 switch (uMsg)
501 {
502 case WM_INITDIALOG:
503 s_pData = (PPRINT_DATA)lParam;
504 s_pData->hwndDlg = hwnd;
506 GetDlgItemText(hwnd, IDC_PRINTING_PAGE, s_szPage, _countof(s_szPage));
508
509 s_hThread = CreateThread(NULL, 0, PrintThreadFunc, s_pData, 0, NULL);
510 if (!s_hThread)
511 {
512 s_pData->status = STRING_PRINTFAILED;
514 }
515 return TRUE;
516
517 case PRINTING_MESSAGE:
518 switch (s_pData->status)
519 {
522 StringCchPrintf(szText, _countof(szText), s_szPage, s_pData->currentPage);
524
525 LoadString(Globals.hInstance, s_pData->status, szText, _countof(szText));
527 break;
528
532 LoadString(Globals.hInstance, s_pData->status, szText, _countof(szText));
534
535 if (s_pData->status == STRING_PRINTCOMPLETE)
537 else if (s_pData->status == STRING_PRINTFAILED)
539 else
541 break;
542 }
543 break;
544
545 case WM_COMMAND:
546 if (LOWORD(wParam) == IDCANCEL && s_pData->status == STRING_NOWPRINTING)
547 {
549 s_pData->status = STRING_PRINTCANCELING;
551 }
552 break;
553
554 case WM_DESTROY:
555 if (s_hThread)
557 DeleteDC(s_pData->printer.hDC);
558 s_pData = LocalFree(s_pData);
559 break;
560 }
561
562 return 0;
563}
static HANDLE s_hThread
LPARAM lParam
Definition: combotst.c:139
#define CloseHandle
Definition: compat.h:739
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
#define STRING_PRINTFAILED
Definition: notepad_res.h:97
#define STRING_PRINTCANCELED
Definition: notepad_res.h:96
#define IDC_PRINTING_PAGE
Definition: notepad_res.h:24
#define IDC_PRINTING_FILENAME
Definition: notepad_res.h:23
#define IDC_PRINTING_STATUS
Definition: notepad_res.h:22
#define STRING_NOWPRINTING
Definition: notepad_res.h:93
#define STRING_PRINTCOMPLETE
Definition: notepad_res.h:95
struct PRINT_DATA * PPRINT_DATA
#define PRINTING_MESSAGE
Definition: printing.c:364
static DWORD WINAPI PrintThreadFunc(LPVOID arg)
Definition: printing.c:483
#define StringCchPrintf
Definition: strsafe.h:517
HWND hwndDlg
Definition: printing.c:100
INT currentPage
Definition: printing.c:102
INT status
Definition: printing.c:101
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
BOOL WINAPI DeleteDC(_In_ HDC)
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
#define PostMessage
Definition: winuser.h:5844
#define WM_DESTROY
Definition: winuser.h:1612
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)

Referenced by DIALOG_FilePrint().

◆ DoCreatePrintFonts()

static BOOL DoCreatePrintFonts ( LPPRINTDLG  pPrinter,
PPRINT_DATA  pPrintData 
)
static

Definition at line 366 of file printing.c.

367{
368 LOGFONT lfBody, lfHeader;
369
370 /* Create the main text font for printing */
371 lfBody = Globals.lfFont;
372 lfBody.lfHeight = -Y_POINTS_TO_PIXELS(pPrinter->hDC, BODY_FONT_SIZE);
373 pPrintData->hBodyFont = CreateFontIndirect(&lfBody);
374 if (pPrintData->hBodyFont == NULL)
375 return FALSE;
376
377 /* Create the header/footer font */
378 lfHeader = Globals.lfFont;
379 lfHeader.lfHeight = -Y_POINTS_TO_PIXELS(pPrinter->hDC, HEADER_FONT_SIZE);
380 lfHeader.lfWeight = FW_BOLD;
381 pPrintData->hHeaderFont = CreateFontIndirect(&lfHeader);
382 if (pPrintData->hHeaderFont == NULL)
383 return FALSE;
384
385 return TRUE;
386}
#define Y_POINTS_TO_PIXELS(hDC, points)
Definition: printing.c:117
#define HEADER_FONT_SIZE
Definition: printing.c:362
#define BODY_FONT_SIZE
Definition: printing.c:361
LONG lfHeight
Definition: dimm.idl:42
LONG lfWeight
Definition: dimm.idl:46
HFONT hBodyFont
Definition: printing.c:106
HFONT hHeaderFont
Definition: printing.c:105
#define FW_BOLD
Definition: wingdi.h:378
#define CreateFontIndirect
Definition: wingdi.h:4444

Referenced by DoPrintDocument().

◆ DoPrintBody()

static BOOL DoPrintBody ( PPRINT_DATA  pData,
DWORD  PageCount,
BOOL  bSkipPage 
)
static

Definition at line 211 of file printing.c.

212{
213 LPPRINTDLG pPrinter = &pData->printer;
214 RECT printRect = pData->printRect;
215 INT xLeft = printRect.left, yTop = printRect.top + pData->cyHeader + pData->cySpacing;
216 INT xStart, tabWidth;
217 DWORD ichStart;
218 SIZE charMetrics;
219 TEXTMETRIC tmText;
220
221 /* Calculate a tab width */
222#define TAB_STOP 8
223 GetTextMetrics(pPrinter->hDC, &tmText);
224 tabWidth = TAB_STOP * tmText.tmAveCharWidth;
225
226#define DO_FLUSH() do { \
227 if (ichStart < pData->ich && !bSkipPage) { \
228 TextOut(pPrinter->hDC, xStart, yTop, &pData->pszText[ichStart], pData->ich - ichStart); \
229 } \
230 ichStart = pData->ich; \
231 xStart = xLeft; \
232 if (pData->status == STRING_PRINTCANCELING) return FALSE; \
233} while (0)
234
235 /* The drawing-body loop */
236 for (ichStart = pData->ich, xStart = xLeft; pData->ich < pData->cchText; )
237 {
238 TCHAR ch = pData->pszText[pData->ich];
239
240 if (ch == _T('\r'))
241 {
242 DO_FLUSH();
243
244 pData->ich++; /* Next char */
245 ichStart = pData->ich;
246 continue;
247 }
248
249 if (ch == _T('\n'))
250 {
251 DO_FLUSH();
252
253 /* Next line */
254 yTop += tmText.tmHeight;
255 xLeft = xStart = printRect.left;
256 }
257 else
258 {
259 if (ch == _T('\t'))
260 {
261 INT nStepWidth = tabWidth - ((xLeft - printRect.left) % tabWidth);
262
263 DO_FLUSH();
264
265 /* Go to the next tab stop */
266 xLeft += nStepWidth;
267 xStart = xLeft;
268 }
269 else /* Normal char */
270 {
271 GetTextExtentPoint32(pPrinter->hDC, &ch, 1, &charMetrics);
272 xLeft += charMetrics.cx;
273 }
274
275 /* Insert a line break if the next position reached the right edge */
276 if (xLeft + charMetrics.cx >= printRect.right)
277 {
278 if (ch != _T('\t'))
279 DO_FLUSH();
280
281 /* Next line */
282 yTop += tmText.tmHeight;
283 xLeft = xStart = printRect.left;
284 }
285 }
286
287 pData->ich++; /* Next char */
288 if (ch == _T('\t') || ch == _T('\n'))
289 ichStart = pData->ich;
290
291 if (yTop + tmText.tmHeight >= printRect.bottom - pData->cyFooter)
292 break; /* The next line reached the body bottom */
293 }
294
295 DO_FLUSH();
296 return TRUE;
297}
unsigned long DWORD
Definition: ntddk_ex.h:95
#define DO_FLUSH()
#define TAB_STOP
LONG cx
Definition: kdterminal.h:27
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
LONG tmAveCharWidth
Definition: wingdi.h:2366
LONG tmHeight
Definition: wingdi.h:2361
int32_t INT
Definition: typedefs.h:58
#define GetTextExtentPoint32
Definition: wingdi.h:4472
#define GetTextMetrics
Definition: wingdi.h:4474

Referenced by DoPrintPage().

◆ DoPrintDocument()

static BOOL DoPrintDocument ( PPRINT_DATA  printData)
static

Definition at line 388 of file printing.c.

389{
390 DOCINFO docInfo;
391 LPPRINTDLG pPrinter = &printData->printer;
392 DWORD CopyCount, PageCount;
393 TEXTMETRIC tmHeader;
394 BOOL ret = FALSE;
395 HFONT hOldFont;
396
397 GetLocalTime(&printData->stNow);
398
399 printData->printRect = GetPrintingRect(pPrinter->hDC, &Globals.lMargins);
400
401 if (!DoCreatePrintFonts(pPrinter, printData))
402 {
403 printData->status = STRING_PRINTFAILED;
404 goto Quit;
405 }
406
407 if (pPrinter->Flags & PD_SELECTION)
408 printData->cchText = GetSelectionTextLength(Globals.hEdit);
409 else
410 printData->cchText = GetWindowTextLength(Globals.hEdit);
411
412 /* Allocate a buffer for the text */
413 printData->pszText = HeapAlloc(GetProcessHeap(), 0, (printData->cchText + 1) * sizeof(TCHAR));
414 if (!printData->pszText)
415 {
416 printData->status = STRING_PRINTFAILED;
417 goto Quit;
418 }
419
420 if (pPrinter->Flags & PD_SELECTION)
421 GetSelectionText(Globals.hEdit, printData->pszText, printData->cchText + 1);
422 else
423 GetWindowText(Globals.hEdit, printData->pszText, printData->cchText + 1);
424
425 /* Start a document */
426 ZeroMemory(&docInfo, sizeof(docInfo));
427 docInfo.cbSize = sizeof(DOCINFO);
428 docInfo.lpszDocName = Globals.szFileTitle;
429 if (StartDoc(pPrinter->hDC, &docInfo) <= 0)
430 {
431 printData->status = STRING_PRINTFAILED;
432 goto Quit;
433 }
434
435 /* Calculate the header and footer heights */
436 hOldFont = SelectObject(pPrinter->hDC, printData->hHeaderFont);
437 GetTextMetrics(pPrinter->hDC, &tmHeader);
438 printData->cyHeader = printData->cyFooter = 2 * tmHeader.tmHeight;
439 printData->cySpacing = Y_POINTS_TO_PIXELS(pPrinter->hDC, SPACING_HEIGHT);
440 SelectObject(pPrinter->hDC, hOldFont); /* De-select the font */
441 if (!Globals.szHeader[0])
442 printData->cyHeader = printData->cySpacing = 0;
443 if (!Globals.szFooter[0])
444 printData->cyFooter = 0;
445
446 /* The printing-copies loop */
447 for (CopyCount = 1; CopyCount <= pPrinter->nCopies; ++CopyCount)
448 {
449 /* The printing-pages loop */
450 for (PageCount = 1, printData->ich = 0; printData->ich < printData->cchText; ++PageCount)
451 {
452 printData->currentPage = PageCount;
453 PostMessage(printData->hwndDlg, PRINTING_MESSAGE, 0, 0);
454
455 if (!DoPrintPage(printData, PageCount))
456 {
457 AbortDoc(pPrinter->hDC); /* Cancel printing */
458 goto Quit;
459 }
460 }
461 }
462
463 if (EndDoc(pPrinter->hDC) <= 0)
464 {
465 printData->status = STRING_PRINTFAILED;
466 goto Quit;
467 }
468
469 ret = TRUE;
470 printData->status = STRING_PRINTCOMPLETE;
471
472Quit:
473 DeleteObject(printData->hHeaderFont);
474 DeleteObject(printData->hBodyFont);
475 if (printData->pszText)
476 HeapFree(GetProcessHeap(), 0, printData->pszText);
477 if (printData->status == STRING_PRINTCANCELING)
478 printData->status = STRING_PRINTCANCELED;
479 PostMessage(printData->hwndDlg, PRINTING_MESSAGE, 0, 0);
480 return ret;
481}
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:286
pKey DeleteObject()
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
static RECT GetPrintingRect(IN HDC hdc, IN LPCRECT pMargins)
Definition: printing.c:28
#define SPACING_HEIGHT
Definition: printing.c:363
static INT GetSelectionText(HWND hWnd, LPTSTR lpString, INT nMaxCount)
Definition: printing.c:64
static BOOL DoPrintPage(PPRINT_DATA pData, DWORD PageCount)
Definition: printing.c:299
static BOOL DoCreatePrintFonts(LPPRINTDLG pPrinter, PPRINT_DATA pPrintData)
Definition: printing.c:366
SYSTEMTIME stNow
Definition: printing.c:104
INT cyFooter
Definition: printing.c:112
DWORD cchText
Definition: printing.c:109
RECT printRect
Definition: printing.c:103
LPTSTR pszText
Definition: printing.c:107
INT cySpacing
Definition: printing.c:111
INT cyHeader
Definition: printing.c:110
DWORD ich
Definition: printing.c:108
int cbSize
Definition: wingdi.h:1676
LPCSTR lpszDocName
Definition: wingdi.h:1677
WORD nCopies
Definition: commdlg.h:475
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1546
int WINAPI EndDoc(_In_ HDC)
DOCINFOA DOCINFO
Definition: wingdi.h:4414
INT WINAPI AbortDoc(_In_ HDC hdc)
#define StartDoc
Definition: wingdi.h:4482
#define GetWindowTextLength
Definition: winuser.h:5811
#define GetWindowText
Definition: winuser.h:5810

Referenced by PrintThreadFunc().

◆ DoPrintPage()

static BOOL DoPrintPage ( PPRINT_DATA  pData,
DWORD  PageCount 
)
static

Definition at line 299 of file printing.c.

300{
301 LPPRINTDLG pPrinter = &pData->printer;
302 BOOL bSkipPage, ret;
303 HFONT hOldFont;
304
305 /* Should we skip this page? */
306 bSkipPage = !(pPrinter->Flags & PD_SELECTION) &&
307 (pPrinter->Flags & PD_PAGENUMS) &&
308 !(pPrinter->nFromPage <= PageCount && PageCount <= pPrinter->nToPage);
309
310 /* The prologue of a page */
311 if (!bSkipPage)
312 {
313 if (StartPage(pPrinter->hDC) <= 0)
314 {
315 pData->status = STRING_PRINTFAILED;
316 return FALSE;
317 }
318
319 if (pData->cyHeader > 0)
320 {
321 /* Draw the page header */
322 RECT rc = pData->printRect;
323 rc.bottom = rc.top + pData->cyHeader;
324
325 hOldFont = SelectObject(pPrinter->hDC, pData->hHeaderFont);
326 DrawHeaderOrFooter(pPrinter->hDC, &rc, Globals.szHeader, PageCount, &pData->stNow);
327 SelectObject(pPrinter->hDC, hOldFont); /* De-select the font */
328 }
329 }
330
331 hOldFont = SelectObject(pPrinter->hDC, pData->hBodyFont);
332 ret = DoPrintBody(pData, PageCount, bSkipPage);
333 SelectObject(pPrinter->hDC, hOldFont);
334 if (!ret)
335 return FALSE; /* Canceled */
336
337 /* The epilogue of a page */
338 if (!bSkipPage)
339 {
340 if (pData->cyFooter > 0)
341 {
342 /* Draw the page footer */
343 RECT rc = pData->printRect;
344 rc.top = rc.bottom - pData->cyFooter;
345
346 hOldFont = SelectObject(pPrinter->hDC, pData->hHeaderFont);
347 DrawHeaderOrFooter(pPrinter->hDC, &rc, Globals.szFooter, PageCount, &pData->stNow);
348 SelectObject(pPrinter->hDC, hOldFont);
349 }
350
351 if (EndPage(pPrinter->hDC) <= 0)
352 {
353 pData->status = STRING_PRINTFAILED;
354 return FALSE;
355 }
356 }
357
358 return TRUE;
359}
#define PD_PAGENUMS
Definition: commdlg.h:148
static BOOL DoPrintBody(PPRINT_DATA pData, DWORD PageCount, BOOL bSkipPage)
Definition: printing.c:211
static VOID DrawHeaderOrFooter(HDC hDC, LPRECT pRect, LPCTSTR pszFormat, INT nPageNo, const SYSTEMTIME *pstNow)
Definition: printing.c:124
int WINAPI EndPage(_In_ HDC)
int WINAPI StartPage(_In_ HDC)

Referenced by DoPrintDocument().

◆ DrawHeaderOrFooter()

static VOID DrawHeaderOrFooter ( HDC  hDC,
LPRECT  pRect,
LPCTSTR  pszFormat,
INT  nPageNo,
const SYSTEMTIME pstNow 
)
static

Definition at line 124 of file printing.c.

125{
126 TCHAR szText[256], szField[128];
127 const TCHAR *pchFormat;
129 HGDIOBJ hOldPen, hOldBrush;
130
131 /* Draw a rectangle */
134 Rectangle(hDC, pRect->left, pRect->top, pRect->right, pRect->bottom);
135 SelectObject(hDC, hOldBrush);
136 SelectObject(hDC, hOldPen);
137
138 InflateRect(pRect, -X_POINTS_TO_PIXELS(hDC, 3), 0); /* Shrink 3pt */
139
140 szText[0] = 0;
141
142 for (pchFormat = pszFormat; *pchFormat; ++pchFormat)
143 {
144 if (*pchFormat != _T('&'))
145 {
146 StringCchCatN(szText, _countof(szText), pchFormat, 1);
147 continue;
148 }
149
150 ++pchFormat;
151 if (*pchFormat == 0)
152 break;
153
154 switch (_totupper(*pchFormat)) /* Make it uppercase */
155 {
156 case _T('&'): /* Found double ampersand */
157 StringCchCat(szText, _countof(szText), TEXT("&"));
158 break;
159
160 case _T('L'): /* Left */
161 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
162 szText[0] = 0;
163 uAlign = DT_LEFT;
164 break;
165
166 case _T('C'): /* Center */
167 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
168 szText[0] = 0;
169 uAlign = DT_CENTER;
170 break;
171
172 case _T('R'): /* Right */
173 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
174 szText[0] = 0;
175 uAlign = DT_RIGHT;
176 break;
177
178 case _T('D'): /* Date */
180 szField, (INT)_countof(szField));
181 StringCchCat(szText, _countof(szText), szField);
182 break;
183
184 case _T('T'): /* Time */
186 szField, (INT)_countof(szField));
187 StringCchCat(szText, _countof(szText), szField);
188 break;
189
190 case _T('F'): /* Filename */
191 StringCchCat(szText, _countof(szText), Globals.szFileTitle);
192 break;
193
194 case _T('P'): /* Page number */
195 StringCchPrintf(szField, _countof(szField), TEXT("%u"), nPageNo);
196 StringCchCat(szText, _countof(szText), szField);
197 break;
198
199 default: /* Otherwise */
200 szField[0] = _T('&');
201 szField[1] = *pchFormat;
202 szField[2] = 0;
203 StringCchCat(szText, _countof(szText), szField);
204 break;
205 }
206 }
207
208 DrawText(hDC, szText, -1, pRect, uAlign | uFlags);
209}
static HDC hDC
Definition: 3dtext.c:33
UINT uFlags
Definition: api.c:59
#define _totupper
Definition: tchar.h:1509
#define TEXT(s)
Definition: k32.h:26
unsigned int UINT
Definition: ndis.h:50
#define LOCALE_USER_DEFAULT
#define X_POINTS_TO_PIXELS(hDC, points)
Definition: printing.c:116
#define StringCchCatN
Definition: strsafe.h:399
#define StringCchCat
Definition: strsafe.h:317
HGDIOBJ WINAPI GetStockObject(_In_ int)
#define NULL_BRUSH
Definition: wingdi.h:901
#define BLACK_PEN
Definition: wingdi.h:903
BOOL WINAPI Rectangle(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
#define GetTimeFormat
Definition: winnls.h:1189
#define GetDateFormat
Definition: winnls.h:1184
#define DT_NOPREFIX
Definition: winuser.h:537
#define DT_CENTER
Definition: winuser.h:527
#define DT_SINGLELINE
Definition: winuser.h:540
#define DrawText
Definition: winuser.h:5783
#define DT_LEFT
Definition: winuser.h:534
#define DT_VCENTER
Definition: winuser.h:543
#define DT_RIGHT
Definition: winuser.h:538
BOOL WINAPI InflateRect(_Inout_ LPRECT, _In_ int, _In_ int)

Referenced by DoPrintPage().

◆ GetPrintingRect()

static RECT GetPrintingRect ( IN HDC  hdc,
IN LPCRECT  pMargins 
)
static

Definition at line 28 of file printing.c.

29{
30 INT iLogPixelsX = GetDeviceCaps(hdc, LOGPIXELSX);
31 INT iLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
32 INT iHorzRes = GetDeviceCaps(hdc, HORZRES); /* in pixels */
33 INT iVertRes = GetDeviceCaps(hdc, VERTRES); /* in pixels */
34 RECT rcPrintRect, rcPhysical;
35
36#define CONVERT_X(x) MulDiv((x), iLogPixelsX, 2540) /* 100th millimeters to pixels */
37#define CONVERT_Y(y) MulDiv((y), iLogPixelsY, 2540) /* 100th millimeters to pixels */
38 SetRect(&rcPrintRect,
39 CONVERT_X(pMargins->left), CONVERT_Y(pMargins->top),
40 iHorzRes - CONVERT_X(pMargins->right),
41 iVertRes - CONVERT_Y(pMargins->bottom));
42
44 rcPhysical.right = rcPhysical.left + GetDeviceCaps(hdc, PHYSICALWIDTH);
45 rcPhysical.top = GetDeviceCaps(hdc, PHYSICALOFFSETY);
46 rcPhysical.bottom = rcPhysical.top + GetDeviceCaps(hdc, PHYSICALHEIGHT);
47
48 /* Adjust the margin */
49 rcPrintRect.left = max(rcPrintRect.left, rcPhysical.left);
50 rcPrintRect.top = max(rcPrintRect.top, rcPhysical.top);
51 rcPrintRect.right = min(rcPrintRect.right, rcPhysical.right);
52 rcPrintRect.bottom = min(rcPrintRect.bottom, rcPhysical.bottom);
53
54 return rcPrintRect;
55}
HDC hdc
Definition: main.c:9
#define min(a, b)
Definition: monoChain.cc:55
#define CONVERT_Y(y)
#define CONVERT_X(x)
#define max(a, b)
Definition: svc.c:63
#define PHYSICALOFFSETY
Definition: wingdi.h:738
#define HORZRES
Definition: wingdi.h:716
int WINAPI GetDeviceCaps(_In_opt_ HDC, _In_ int)
#define PHYSICALHEIGHT
Definition: wingdi.h:736
#define LOGPIXELSY
Definition: wingdi.h:719
#define PHYSICALOFFSETX
Definition: wingdi.h:737
#define VERTRES
Definition: wingdi.h:717
#define PHYSICALWIDTH
Definition: wingdi.h:735
#define LOGPIXELSX
Definition: wingdi.h:718
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)

Referenced by DoPrintDocument().

◆ GetSelectionText()

static INT GetSelectionText ( HWND  hWnd,
LPTSTR  lpString,
INT  nMaxCount 
)
static

Definition at line 64 of file printing.c.

65{
66 DWORD dwStart = 0, dwEnd = 0;
68 LPTSTR pszText;
69 HLOCAL hLocal;
70 HRESULT hr;
71
72 SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
73 if (!lpString || dwStart == dwEnd || cchText == 0)
74 return 0;
75
76 hLocal = (HLOCAL)SendMessage(hWnd, EM_GETHANDLE, 0, 0);
77 pszText = (LPTSTR)LocalLock(hLocal);
78 if (!pszText)
79 return 0;
80
81 hr = StringCchCopyN(lpString, nMaxCount, pszText + dwStart, dwEnd - dwStart);
82 LocalUnlock(hLocal);
83
84 switch (hr)
85 {
86 case S_OK:
87 return dwEnd - dwStart;
88
90 return nMaxCount - 1;
91
92 default:
93 return 0;
94 }
95}
HWND hWnd
Definition: settings.c:17
LPVOID NTAPI LocalLock(HLOCAL hMem)
Definition: heapmem.c:1616
BOOL NTAPI LocalUnlock(HLOCAL hMem)
Definition: heapmem.c:1805
#define S_OK
Definition: intsafe.h:52
HRESULT hr
Definition: shlfolder.c:183
#define StringCchCopyN
Definition: strsafe.h:227
#define STRSAFE_E_INSUFFICIENT_BUFFER
Definition: strsafe.h:103
UINT_PTR WPARAM
Definition: windef.h:207
HANDLE HLOCAL
Definition: windef.h:244
#define EM_GETSEL
Definition: winuser.h:2000
#define EM_GETHANDLE
Definition: winuser.h:1992
_In_ int nMaxCount
Definition: winuser.h:4880
_In_ int cchText
Definition: winuser.h:4468
#define SendMessage
Definition: winuser.h:5855
CHAR * LPTSTR
Definition: xmlstorage.h:192

Referenced by DoPrintDocument().

◆ GetSelectionTextLength()

static INT GetSelectionTextLength ( HWND  hWnd)
static

Definition at line 57 of file printing.c.

58{
59 DWORD dwStart = 0, dwEnd = 0;
60 SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
61 return dwEnd - dwStart;
62}

Referenced by DIALOG_FilePrint(), and DoPrintDocument().

◆ PrintThreadFunc()

static DWORD WINAPI PrintThreadFunc ( LPVOID  arg)
static

Definition at line 483 of file printing.c.

484{
486 pData->currentPage = 1;
487 pData->status = STRING_NOWPRINTING;
488 PostMessage(pData->hwndDlg, PRINTING_MESSAGE, 0, 0);
489 return DoPrintDocument(pData);
490}
static BOOL DoPrintDocument(PPRINT_DATA printData)
Definition: printing.c:388
void * arg
Definition: msvc.h:10

Referenced by DIALOG_Printing_DialogProc().