forked from Discriminator/PDW
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprinter.cpp
More file actions
187 lines (164 loc) · 4.74 KB
/
Copy pathprinter.cpp
File metadata and controls
187 lines (164 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// Printer.cpp
//
#include <windows.h>
#include <commctrl.h>
#include <mmsystem.h>
#include <stdio.h>
#include <commdlg.h>
#include <string.h>
#include <time.h>
#include "headers\resource.h"
#include "headers\PDW.h"
#include "headers\slicer.h"
#include "headers\toolbar.h"
#include "headers\gfx.h"
#include "headers\initapp.h"
#include "headers\sigind.h"
#include "headers\decode.h"
#include "headers\sound_in.h"
#include "headers\printer.h"
PRINTDLG printdlg;
DOCINFO docinfo;
int printOK = 1;
HWND PrtCancel_hDlg = NULL;
// Print out buffer,
// Seperate each line if '\n' is detected.
//
void PrintBuffer(LPSTR lpBuffer)
{
TEXTMETRIC tm;
LPSTR s;
int i, copies;
int prtX = 0; // current X output location
int prtY = 0; // current Y output location
/* initialize PRINTDLG struct */
PrintInit(&printdlg, ghWnd);
if (!PrintDlg(&printdlg)) return;
docinfo.cbSize = sizeof(DOCINFO);
docinfo.lpszDocName = "Print...";
docinfo.lpszOutput = NULL;
docinfo.lpszDatatype = NULL;
docinfo.fwType = 0;
// FIX [PrinterJobGuards]: StartDoc was unchecked - on failure the code still
// "printed" into a dead job. Bail out and release the DC and the PrintDlg-
// allocated hDevMode/hDevNames (which used to leak on every print job).
if (StartDoc(printdlg.hDC, &docinfo) <= 0)
{
DeleteDC(printdlg.hDC);
if (printdlg.hDevMode) { GlobalFree(printdlg.hDevMode); printdlg.hDevMode = NULL; }
if (printdlg.hDevNames) { GlobalFree(printdlg.hDevNames); printdlg.hDevNames = NULL; }
return;
}
/* get text metrics for printer */
GetTextMetrics(printdlg.hDC, &tm);
printOK = 1;
SetAbortProc(printdlg.hDC, (ABORTPROC) AbortFunc);
PrtCancel_hDlg = CreateDialog(ghInstance, "PrCancel", ghWnd, (DLGPROC) KillPrint);
// FIX [PrinterJobGuards]: stop looping once the user cancelled - the old loops
// kept TextOut-ing up to 15000 chars x remaining copies into an aborted job.
for (copies=0; copies<printdlg.nCopies && printOK; copies++)
{
StartPage(printdlg.hDC);
prtX = 0;
prtY = 0;
i=0;
s = lpBuffer;
while ((*s) && (i < 15000) && printOK) // FIX [PrinterJobGuards]
{
if (*s == '\n')
{
prtX = 0;
prtY += tm.tmHeight + tm.tmExternalLeading;
// FIX [PrinterPaging]: break to a new physical page once we pass the printable
// height. Without this the loop kept advancing prtY with no EndPage/StartPage, so
// everything beyond the first page was drawn off-page and silently clipped.
if (prtY + (tm.tmHeight + tm.tmExternalLeading) > GetDeviceCaps(printdlg.hDC, VERTRES))
{
EndPage(printdlg.hDC);
StartPage(printdlg.hDC);
prtY = 0;
}
s++;
continue;
}
if (((*s != ' ') && ((*s < 0x21) || (*s > 0x7E))))
{
s++;
continue;
}
TextOut(printdlg.hDC, prtX, prtY,(LPSTR)s, 1);
prtX += tm.tmAveCharWidth + tm.tmExternalLeading;
s++;
i++;
}
if (printOK) EndPage(printdlg.hDC);
}
if (PrtCancel_hDlg) { DestroyWindow(PrtCancel_hDlg); PrtCancel_hDlg = NULL; }
// FIX [PrinterJobGuards]: a cancelled job never called EndDoc or AbortDoc, and
// the PrintDlg-allocated hDevMode/hDevNames handles leaked on every print job.
if (printOK) EndDoc(printdlg.hDC);
else AbortDoc(printdlg.hDC);
DeleteDC(printdlg.hDC);
if (printdlg.hDevMode) { GlobalFree(printdlg.hDevMode); printdlg.hDevMode = NULL; }
if (printdlg.hDevNames) { GlobalFree(printdlg.hDevNames); printdlg.hDevNames = NULL; }
}
/* Initialize PRINTDLG structure. */
void PrintInit(PRINTDLG *printdlg, HWND hwnd)
{
printdlg->lStructSize = sizeof(PRINTDLG);
printdlg->hwndOwner = hwnd;
printdlg->hDevMode = NULL;
printdlg->hDevNames = NULL;
printdlg->hDC = NULL;
printdlg->Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE;
printdlg->nFromPage = 0;
printdlg->nToPage = 0;
printdlg->nMinPage = 0;
printdlg->nMaxPage = 0;
printdlg->nCopies = 1;
printdlg->hInstance = NULL;
printdlg->lCustData = 0;
printdlg->lpfnPrintHook = NULL;
printdlg->lpfnSetupHook = NULL;
printdlg->lpPrintTemplateName = NULL;
printdlg->lpSetupTemplateName = NULL;
printdlg->hPrintTemplate = NULL;
printdlg->hSetupTemplate = NULL;
}
/* Printer abort function. */
BOOL CALLBACK AbortFunc(HDC hdc, int err)
{
MSG message;
while (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
if (!IsDialogMessage(PrtCancel_hDlg, &message))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
return printOK;
}
/* Let user kill print process. */
LRESULT CALLBACK KillPrint(HWND hdwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
CenterWindow(hdwnd);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
printOK = 0;
DestroyWindow(PrtCancel_hDlg);
PrtCancel_hDlg = NULL;
return 1;
}
break;
}
return 0;
}