2 static char *RCSid() { return RCSid("$Id: wprinter.c,v 1.5 2005/08/03 16:55:40 mikulik Exp $"); }
5 /* GNUPLOT - win/wprinter.c */
7 * Copyright 1992, 1993, 1998, 2004 Maurice Castro, Russell Lang
9 * Permission to use, copy, and distribute this software and its
10 * documentation for any purpose with or without fee is hereby granted,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
15 * Permission to modify the software is granted, but not the right to
16 * distribute the complete modified source code. Modifications are to
17 * be distributed as patches to the released version. Permission to
18 * distribute binaries produced by compiling modified sources is granted,
20 * 1. distribute the corresponding source modifications from the
21 * released version in the form of a patch file along with the binaries,
22 * 2. add special version identification to distinguish your version
23 * in addition to the base release version number,
24 * 3. provide your name and address as the primary contact for the
25 * support of your modified version, and
26 * 4. retain our contact information in regard to use of the base
28 * Permission to distribute the released version of the source code along
29 * with corresponding source modifications in the form of a patch file is
30 * granted with same provisions 2 through 4 for binary distributions.
32 * This software is provided "as is" without express or implied warranty
33 * to the extent permitted by applicable law.
43 /* Dump a file to the printer */
64 GP_LPPRINT prlist = NULL;
66 BOOL CALLBACK WINEXPORT PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam);
68 BOOL CALLBACK WINEXPORT
69 PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam)
72 GP_LPPRINT lpr = (GP_LPPRINT)GetWindowLong(GetParent(hdlg), 4);
76 wsprintf(buf,"%d",lpr->pdef.x);
77 SetDlgItemText(hdlg, PSIZE_DEFX, buf);
78 wsprintf(buf,"%d",lpr->pdef.y);
79 SetDlgItemText(hdlg, PSIZE_DEFY, buf);
80 wsprintf(buf,"%d",lpr->poff.x);
81 SetDlgItemText(hdlg, PSIZE_OFFX, buf);
82 wsprintf(buf,"%d",lpr->poff.y);
83 SetDlgItemText(hdlg, PSIZE_OFFY, buf);
84 wsprintf(buf,"%d",lpr->psize.x);
85 SetDlgItemText(hdlg, PSIZE_X, buf);
86 wsprintf(buf,"%d",lpr->psize.y);
87 SetDlgItemText(hdlg, PSIZE_Y, buf);
88 CheckDlgButton(hdlg, PSIZE_DEF, TRUE);
89 EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
90 EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
95 EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
96 EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
99 EnableWindow(GetDlgItem(hdlg, PSIZE_X), TRUE);
100 EnableWindow(GetDlgItem(hdlg, PSIZE_Y), TRUE);
103 if (SendDlgItemMessage(hdlg, PSIZE_OTHER, BM_GETCHECK, 0, 0L)) {
104 SendDlgItemMessage(hdlg, PSIZE_X, WM_GETTEXT, 7,
105 (LPARAM) (LPSTR) buf);
106 GetInt(buf, (LPINT)&lpr->psize.x);
107 SendDlgItemMessage(hdlg, PSIZE_Y, WM_GETTEXT, 7,
108 (LPARAM) (LPSTR) buf);
109 GetInt(buf, (LPINT)&lpr->psize.y);
111 lpr->psize.x = lpr->pdef.x;
112 lpr->psize.y = lpr->pdef.y;
114 SendDlgItemMessage(hdlg, PSIZE_OFFX, WM_GETTEXT, 7,
115 (LPARAM) (LPSTR) buf);
116 GetInt(buf, (LPINT)&lpr->poff.x);
117 SendDlgItemMessage(hdlg, PSIZE_OFFY, WM_GETTEXT, 7,
118 (LPARAM) (LPSTR) buf);
119 GetInt(buf, (LPINT)&lpr->poff.y);
121 if (lpr->psize.x <= 0)
122 lpr->psize.x = lpr->pdef.x;
123 if (lpr->psize.y <= 0)
124 lpr->psize.y = lpr->pdef.y;
126 EndDialog(hdlg, IDOK);
129 EndDialog(hdlg, IDCANCEL);
131 } /* switch(wparam) */
139 /* GetWindowLong(hwnd, 4) must be available for use */
141 PrintSize(HDC printer, HWND hwnd, LPRECT lprect)
145 DLGPROC lpfnPrintSizeDlgProc;
150 SetWindowLong(hwnd, 4, (LONG)((GP_LPPRINT)&pr));
153 pr.psize.x = GetDeviceCaps(printer, HORZSIZE);
154 pr.psize.y = GetDeviceCaps(printer, VERTSIZE);
156 GetClientRect(hwnd,lprect);
157 pr.pdef.x = MulDiv(lprect->right-lprect->left, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
158 pr.pdef.y = MulDiv(lprect->bottom-lprect->top, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
162 if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, PrintSizeDlgProc)
166 lpfnPrintSizeDlgProc = (DLGPROC) GetProcAddress(hdllInstance,
169 lpfnPrintSizeDlgProc = (DLGPROC) MakeProcInstance(
170 (FARPROC) PrintSizeDlgProc, hdllInstance);
172 if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, lpfnPrintSizeDlgProc)
176 lprect->left = MulDiv(pr.poff.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
177 lprect->top = MulDiv(pr.poff.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
178 lprect->right = lprect->left + MulDiv(pr.psize.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
179 lprect->bottom = lprect->top + MulDiv(pr.psize.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
184 FreeProcInstance((FARPROC)lpfnPrintSizeDlgProc);
187 SetWindowLong(hwnd, 4, (LONG)(0L));
193 /* Win32 doesn't support OpenJob() etc. so we must use some old code
194 * which attempts to sneak the output through a Windows printer driver */
196 PrintRegister(GP_LPPRINT lpr)
209 while (this && (this->hdcPrn!=hdc)) {
216 PrintUnregister(GP_LPPRINT lpr)
218 GP_LPPRINT this, prev;
219 prev = (GP_LPPRINT)NULL;
221 while (this && (this!=lpr)) {
225 if (this && (this == lpr)) {
228 prev->next = this->next;
235 /* GetWindowLong(GetParent(hDlg), 4) must be available for use */
236 BOOL CALLBACK WINEXPORT
237 PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
240 lpr = (GP_LPPRINT)GetWindowLong(GetParent(hDlg), 4);
244 lpr->hDlgPrint = hDlg;
245 SetWindowText(hDlg,(LPSTR)lParam);
246 EnableMenuItem(GetSystemMenu(hDlg,FALSE),SC_CLOSE,MF_GRAYED);
249 lpr->bUserAbort = TRUE;
251 EnableWindow(GetParent(hDlg),TRUE);
252 EndDialog(hDlg, FALSE);
259 BOOL CALLBACK WINEXPORT
260 PrintAbortProc(HDC hdcPrn, int code)
264 lpr = PrintFind(hdcPrn);
266 while (!lpr->bUserAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
267 if (!lpr->hDlgPrint || !IsDialogMessage(lpr->hDlgPrint,&msg)) {
268 TranslateMessage(&msg);
269 DispatchMessage(&msg);
272 return(!lpr->bUserAbort);
276 /* GetWindowLong(hwnd, 4) must be available for use */
278 DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
292 memset(&pd, 0, sizeof(PRINTDLG));
293 pd.lStructSize = sizeof(PRINTDLG);
295 pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
297 if ((f = fopen(szFileName, "rb")) == (FILE *)NULL)
299 fseek(f, 0L, SEEK_END);
303 fseek(f, 0L, SEEK_SET);
308 if (printer != (HDC)NULL) {
310 SetWindowLong(hwnd, 4, (LONG)((GP_LPPRINT)&pr));
311 PrintRegister((GP_LPPRINT)&pr);
312 if ( (buf = malloc(4096+2)) != (char *)NULL ) {
313 bufcount = (WORD *)buf;
314 EnableWindow(hwnd,FALSE);
315 pr.bUserAbort = FALSE;
316 /* is parent set correctly */
317 pr.hDlgPrint = CreateDialogParam(hdllInstance,
321 SetAbortProc(printer, PrintAbortProc);
322 di.cbSize = sizeof(DOCINFO);
323 di.lpszDocName = szAppName;
324 di.lpszOutput = NULL;
325 if (StartDoc(printer, &di) > 0) {
326 while ( pr.hDlgPrint && !pr.bUserAbort
327 && (count = fread(buf+2, 1, 4096, f)) != 0 ) {
329 Escape(printer, PASSTHROUGH, count+2, (LPSTR)buf, NULL);
331 sprintf(pcdone, "%d%% done",
332 (int) (ldone * 100 / lsize));
333 SetWindowText(GetDlgItem(pr.hDlgPrint, CANCEL_PCDONE),
340 if (!pr.bUserAbort) {
341 EnableWindow(hwnd,TRUE);
342 DestroyWindow(pr.hDlgPrint);
348 SetWindowLong(hwnd, 4, (LONG)(0L));
349 PrintUnregister((GP_LPPRINT)&pr);
357 /* documented in Device Driver Adaptation Guide */
358 /* Prototypes taken from print.h */
359 DECLARE_HANDLE(HPJOB);
361 HPJOB WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
362 int WINAPI StartSpoolPage(HPJOB);
363 int WINAPI EndSpoolPage(HPJOB);
364 int WINAPI WriteSpool(HPJOB, LPSTR, int);
365 int WINAPI CloseJob(HPJOB);
366 int WINAPI DeleteJob(HPJOB, int);
367 int WINAPI WriteDialog(HPJOB, LPSTR, int);
368 int WINAPI DeleteSpoolPage(HPJOB);
370 /* Modeless dialog box - Cancel printing */
371 BOOL CALLBACK WINEXPORT
372 CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
376 SetWindowText(hDlg,(LPSTR)lParam);
379 switch(LOWORD(wParam)) {
390 /* Dialog box to select printer port */
391 BOOL CALLBACK WINEXPORT
392 SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
398 entry = (LPSTR)lParam;
400 SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
401 entry += lstrlen(entry)+1;
403 SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
406 switch(LOWORD(wParam)) {
408 if (HIWORD(lParam) == LBN_DBLCLK)
409 PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
412 EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
422 /* Print File to port */
424 DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
426 #define PRINT_BUF_SIZE 4096
430 DLGPROC lpfnSpoolProc;
435 DLGPROC lpfnCancelProc;
442 if ((buffer = LocalAllocPtr(LHND, PRINT_BUF_SIZE)) == (char *)NULL)
444 /* get list of ports */
445 GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
447 lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, hdllInstance);
448 iport = DialogBoxParam(hdllInstance, "SpoolDlgBox", hwnd, lpfnSpoolProc, (LPARAM)buffer);
449 FreeProcInstance((FARPROC)lpfnSpoolProc);
451 LocalFreePtr((void NEAR *)buffer);
455 for (i=1; i<iport && lstrlen(portname)!=0; i++)
456 portname += lstrlen(portname)+1;
458 /* open file and get length */
459 hf = _lopen(szFileName, OF_READ);
460 if (hf == HFILE_ERROR) {
461 LocalFreePtr((void NEAR *)buffer);
464 lsize = _llseek(hf, 0L, 2);
465 (void) _llseek(hf, 0L, 0);
469 hJob = OpenJob(portname, szFileName, (HDC)NULL);
477 LocalFreePtr((void NEAR *)buffer);
480 if (StartSpoolPage(hJob) < 0)
484 lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, hdllInstance);
485 hDlgModeless = CreateDialogParam(hdllInstance, "CancelDlgBox", hwnd, lpfnCancelProc, (LPARAM)szAppName);
487 while (!error && hDlgModeless && IsWindow(hDlgModeless)
488 && ((count = _lread(hf, buffer, PRINT_BUF_SIZE))!= 0) ) {
489 wsprintf(pcdone, "%d%% done", (int)(ldone * 100 / lsize));
490 SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
491 if (WriteSpool(hJob, buffer, count) < 0)
494 while (IsWindow(hDlgModeless) && PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
495 if (!IsDialogMessage(hDlgModeless, &msg)) {
496 TranslateMessage(&msg);
497 DispatchMessage(&msg);
501 LocalFreePtr((void NEAR *)buffer);
504 if (!hDlgModeless || !IsWindow(hDlgModeless))
506 if (IsWindow(hDlgModeless))
507 DestroyWindow(hDlgModeless);
509 FreeProcInstance((FARPROC)lpfnCancelProc);