Files
mfc/tests/addprinter.cpp
Claude 2e13672985 Print on Linux and macOS: printer DCs, PDF and CUPS, and the print dialogs
The portable layer now prints. Its printers are the CUPS queues that
lpstat lists and "Save as PDF". A printer DC takes its paper, orientation
and resolution from the DEVMODE (300 dpi by default, Letter or A4 by the
locale), with a quarter inch the printer cannot print on, and draws each
page between StartPage and EndPage as a picture. EndPage adds it to a
PDF file (compressed with zlib when the system has it, else run-length
encoded), and EndDoc hands the file to lp, or leaves it where DOCINFO
says, or in ~/Documents. SetAbortProc's procedure runs at each EndPage;
AbortDoc and ResetDC work.

PrintDlg and PageSetupDlg return the default printer's DEVNAMES and
DEVMODE (A and W forms) or show their dialogs: Print, Print Setup and
Page Setup, with the control IDs of a new dlgs.h, hooks, page ranges,
copies, paper, orientation, margins and a sample page.
CommDlgExtendedError reports their errors.

Print preview in sditest and mditest now passes natively; dlgtest,
sditest and mditest join the native test list. wintest prints a job to
a file, checks printer DCs and drives both dialogs through their hooks.
Under Wine 9 the test printer uses the wineps print processor, without
which StartDoc fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRw4kkFHrrHRhkcuLw
Signed-off-by: Claude <noreply@anthropic.com>
2026-11-17 11:29:21 +00:00

124 lines
4.2 KiB
C++

// addprinter.cpp - gives the Wine prefix the tests run in a printer, so
// printing and print preview have a printer DC. Wine's PostScript driver
// needs a PPD file; this one describes a plain US Letter printer.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
static const char szPPD[] =
"*PPD-Adobe: \"4.3\"\n"
"*FormatVersion: \"4.3\"\n"
"*FileVersion: \"1.0\"\n"
"*LanguageVersion: English\n"
"*LanguageEncoding: ISOLatin1\n"
"*PCFileName: \"MFCTEST.PPD\"\n"
"*Manufacturer: \"MFC Test\"\n"
"*Product: \"(MFC Test Printer)\"\n"
"*PSVersion: \"(3010.000) 0\"\n"
"*ModelName: \"MFC Test Printer\"\n"
"*ShortNickName: \"MFC Test Printer\"\n"
"*NickName: \"MFC Test Printer\"\n"
"*ColorDevice: True\n"
"*DefaultColorSpace: RGB\n"
"*LanguageLevel: \"3\"\n"
"*DefaultResolution: 300dpi\n"
"*OpenUI *PageSize: PickOne\n"
"*DefaultPageSize: Letter\n"
"*PageSize Letter/US Letter: \"<</PageSize[612 792]>>setpagedevice\"\n"
"*CloseUI: *PageSize\n"
"*DefaultPageRegion: Letter\n"
"*DefaultImageableArea: Letter\n"
"*ImageableArea Letter/US Letter: \"18 36 594 756\"\n"
"*DefaultPaperDimension: Letter\n"
"*PaperDimension Letter/US Letter: \"612 792\"\n";
static BOOL HasPrintProcessor(const char* pszName) {
DWORD cb = 0, n = 0;
EnumPrintProcessorsA(NULL, NULL, 1, NULL, 0, &cb, &n);
if (cb == 0) return FALSE;
PRINTPROCESSOR_INFO_1A* p = (PRINTPROCESSOR_INFO_1A*)malloc(cb);
BOOL bFound = FALSE;
if (p != NULL && EnumPrintProcessorsA(NULL, NULL, 1, (BYTE*)p, cb, &cb, &n))
for (DWORD i = 0; i < n; i++)
if (lstrcmpiA(p[i].pName, pszName) == 0) bFound = TRUE;
free(p);
return bFound;
}
int main() {
const char* pszName = "MFC Test Printer";
char szDir[MAX_PATH], szPath[MAX_PATH];
DWORD cb = sizeof(szDir);
if (!GetPrinterDriverDirectoryA(NULL, NULL, 1, (BYTE*)szDir, cb, &cb)) {
printf("no printer driver directory\n");
return 1;
}
CreateDirectoryA(szDir, NULL);
snprintf(szPath, sizeof(szPath), "%s\\mfctest.ppd", szDir);
FILE* f = fopen(szPath, "wb");
if (f == NULL) {
printf("cannot write %s\n", szPath);
return 1;
}
fputs(szPPD, f);
fclose(f);
DRIVER_INFO_3A di;
ZeroMemory(&di, sizeof(di));
di.cVersion = 3;
di.pName = (LPSTR) "MFC Test PostScript";
di.pDriverPath = (LPSTR) "wineps.drv";
di.pDataFile = (LPSTR) "mfctest.ppd";
di.pConfigFile = (LPSTR) "wineps.drv";
if (!AddPrinterDriverA(NULL, 3, (BYTE*)&di) && GetLastError() != ERROR_PRINTER_DRIVER_ALREADY_INSTALLED) {
printf("AddPrinterDriver failed: %lu\n", GetLastError());
return 1;
}
PRINTER_INFO_2A pi;
ZeroMemory(&pi, sizeof(pi));
pi.pPrinterName = (LPSTR)pszName;
pi.pPortName = (LPSTR) "FILE:";
pi.pDriverName = di.pName;
// Wine 9 spools jobs as EMF, which its wineps print processor plays;
// WinPrint takes only RAW, and StartDoc would fail.
const char* pszProcessor = HasPrintProcessor("wineps") ? "wineps" : "WinPrint";
pi.pPrintProcessor = (LPSTR)pszProcessor;
pi.pDatatype = (LPSTR) "RAW";
HANDLE h = AddPrinterA(NULL, 2, (BYTE*)&pi);
if (h != NULL)
ClosePrinter(h);
else if (GetLastError() != ERROR_PRINTER_ALREADY_EXISTS) {
printf("AddPrinter failed: %lu\n", GetLastError());
return 1;
}
// A prefix from before keeps its printer; give it the processor too.
HANDLE hPrinter;
PRINTER_DEFAULTSA pd = {NULL, NULL, PRINTER_ALL_ACCESS};
if (OpenPrinterA((LPSTR)pszName, &hPrinter, &pd)) {
DWORD cbInfo = 0;
GetPrinterA(hPrinter, 2, NULL, 0, &cbInfo);
PRINTER_INFO_2A* ppi = cbInfo ? (PRINTER_INFO_2A*)malloc(cbInfo) : NULL;
if (ppi != NULL && GetPrinterA(hPrinter, 2, (BYTE*)ppi, cbInfo, &cbInfo) &&
lstrcmpiA(ppi->pPrintProcessor, pszProcessor) != 0) {
ppi->pPrintProcessor = (LPSTR)pszProcessor;
ppi->pSecurityDescriptor = NULL;
if (!SetPrinterA(hPrinter, 2, (BYTE*)ppi, 0))
printf("SetPrinter failed: %lu\n", GetLastError());
}
free(ppi);
ClosePrinter(hPrinter);
}
if (!SetDefaultPrinterA(pszName)) {
printf("SetDefaultPrinter failed: %lu\n", GetLastError());
return 1;
}
HDC hDC = CreateDCA("WINSPOOL", pszName, NULL, NULL);
if (hDC == NULL) {
printf("CreateDC failed\n");
return 1;
}
DeleteDC(hDC);
return 0;
}