home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 May / cica_0595_4.zip / cica_0595_4 / UTIL / GPT34SRC / WIN / WPRINTER.C < prev    next >
C/C++ Source or Header  |  1993-05-11  |  9KB  |  323 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: wprinter.c%v 3.38.2.74 1993/02/19 01:16:40 woo Exp woo $";
  3. #endif
  4.  
  5. /* GNUPLOT - win/wprinter.c */
  6. /*
  7.  * Copyright (C) 1992   Maurice Castro, Russell Lang
  8.  *
  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.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  *
  22.  * AUTHORS
  23.  * 
  24.  *   Maurice Castro
  25.  *   Russell Lang
  26.  * 
  27.  * Send your comments or suggestions to 
  28.  *  info-gnuplot@dartmouth.edu.
  29.  * This is a mailing list; to join it send a note to 
  30.  *  info-gnuplot-request@dartmouth.edu.  
  31.  * Send bug reports to
  32.  *  bug-gnuplot@dartmouth.edu.
  33.  */
  34.  
  35. /* Dump a file to the printer */
  36.  
  37. #define STRICT
  38. #include <windows.h>
  39. #include <windowsx.h>
  40. #if WINVER >= 0x030a
  41. #include <commdlg.h>
  42. #endif
  43. #ifdef __MSC__
  44. #include <memory.h>
  45. #else
  46. #include <mem.h>
  47. #endif
  48. #include "wgnuplib.h"
  49. #include "wresourc.h"
  50. #include "wcommon.h"
  51.  
  52. LPPRINT prlist = NULL;
  53.  
  54. BOOL CALLBACK _export PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam);
  55.  
  56. BOOL CALLBACK _export
  57. PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam)
  58. {
  59.     char buf[8];
  60.     LPPRINT lpr;
  61.     lpr = (LPPRINT)GetWindowLong(GetParent(hdlg), 4);
  62.  
  63.     switch (wmsg) {
  64.         case WM_INITDIALOG:
  65.             wsprintf(buf,"%d",lpr->pdef.x);
  66.             SetDlgItemText(hdlg, PSIZE_DEFX, buf);
  67.             wsprintf(buf,"%d",lpr->pdef.y);
  68.             SetDlgItemText(hdlg, PSIZE_DEFY, buf);
  69.             wsprintf(buf,"%d",lpr->poff.x);
  70.             SetDlgItemText(hdlg, PSIZE_OFFX, buf);
  71.             wsprintf(buf,"%d",lpr->poff.y);
  72.             SetDlgItemText(hdlg, PSIZE_OFFY, buf);
  73.             wsprintf(buf,"%d",lpr->psize.x);
  74.             SetDlgItemText(hdlg, PSIZE_X, buf);
  75.             wsprintf(buf,"%d",lpr->psize.y);
  76.             SetDlgItemText(hdlg, PSIZE_Y, buf);
  77.             CheckDlgButton(hdlg, PSIZE_DEF, TRUE);
  78.             EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  79.             EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  80.             return TRUE;
  81.         case WM_COMMAND:
  82.             switch (wparam) {
  83.                 case PSIZE_DEF:
  84.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  85.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  86.                     return FALSE;
  87.                 case PSIZE_OTHER:
  88.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), TRUE);
  89.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), TRUE);
  90.                     return FALSE;
  91.                 case IDOK:
  92.                     if (SendDlgItemMessage(hdlg, PSIZE_OTHER, BM_GETCHECK, 0, 0L)) {
  93.                         SendDlgItemMessage(hdlg, PSIZE_X, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  94.                         GetInt(buf, &lpr->psize.x);
  95.                         SendDlgItemMessage(hdlg, PSIZE_Y, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  96.                         GetInt(buf, &lpr->psize.y);
  97.                     }
  98.                     else {
  99.                         lpr->psize.x = lpr->pdef.x;
  100.                         lpr->psize.y = lpr->pdef.y;
  101.                     }
  102.                     SendDlgItemMessage(hdlg, PSIZE_OFFX, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  103.                     GetInt(buf, &lpr->poff.x);
  104.                     SendDlgItemMessage(hdlg, PSIZE_OFFY, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  105.                     GetInt(buf, &lpr->poff.y);
  106.  
  107.                     if (lpr->psize.x <= 0)
  108.                         lpr->psize.x = lpr->pdef.x;
  109.                     if (lpr->psize.y <= 0)
  110.                         lpr->psize.y = lpr->pdef.y;
  111.  
  112.                     EndDialog(hdlg, IDOK);
  113.                     return TRUE;
  114.                 case IDCANCEL:
  115.                     EndDialog(hdlg, IDCANCEL);
  116.                     return TRUE;
  117.             }
  118.             break;
  119.     }
  120.     return FALSE;
  121. }
  122.  
  123.  
  124.  
  125. /* GetWindowLong(hwnd, 4) must be available for use */
  126. BOOL
  127. PrintSize(HDC printer, HWND hwnd, LPRECT lprect)
  128. {
  129. HDC hdc;
  130. DLGPROC lpfnPrintSizeDlgProc ;
  131. BOOL status = FALSE;
  132. PRINT pr;
  133.  
  134.     SetWindowLong(hwnd, 4, (LONG)((LPPRINT)&pr));
  135.     pr.poff.x = 0;
  136.     pr.poff.y = 0;
  137.     pr.psize.x = GetDeviceCaps(printer, HORZSIZE);
  138.     pr.psize.y = GetDeviceCaps(printer, VERTSIZE);
  139.     hdc = GetDC(hwnd);
  140.     GetClientRect(hwnd,lprect);
  141.     pr.pdef.x = MulDiv(lprect->right-lprect->left, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  142.     pr.pdef.y = MulDiv(lprect->bottom-lprect->top, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  143.     ReleaseDC(hwnd,hdc);
  144. #ifdef __DLL__
  145.     lpfnPrintSizeDlgProc = (DLGPROC)GetProcAddress(hdllInstance, "PrintSizeDlgProc");
  146. #else
  147.     lpfnPrintSizeDlgProc = (DLGPROC)MakeProcInstance((FARPROC)PrintSizeDlgProc, hdllInstance);
  148. #endif
  149.     if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, lpfnPrintSizeDlgProc)
  150.         == IDOK) {
  151.         lprect->left = MulDiv(pr.poff.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  152.         lprect->top = MulDiv(pr.poff.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  153.         lprect->right = lprect->left + MulDiv(pr.psize.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  154.         lprect->bottom = lprect->top + MulDiv(pr.psize.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  155.         status = TRUE;
  156.     }
  157. #ifndef __DLL__
  158.     FreeProcInstance((FARPROC)lpfnPrintSizeDlgProc);
  159. #endif
  160.     SetWindowLong(hwnd, 4, (LONG)(0L));
  161.     return status;
  162. }
  163.  
  164.  
  165. #if WINVER >= 0x030a
  166. void 
  167. PrintRegister(LPPRINT lpr)
  168. {
  169.     LPPRINT next;
  170.     next = prlist;
  171.     prlist = lpr;
  172.     lpr->next = next;
  173. }
  174.  
  175. LPPRINT
  176. PrintFind(HDC hdc)
  177. {
  178.     LPPRINT this;
  179.     this = prlist;
  180.     while (this && (this->hdcPrn!=hdc)) {
  181.         this = this->next;
  182.     }
  183.     return this;
  184. }
  185.  
  186. void
  187. PrintUnregister(LPPRINT lpr)
  188. {
  189.     LPPRINT this, prev;
  190.     prev = (LPPRINT)NULL;
  191.     this = prlist;
  192.     while (this && (this!=lpr)) {
  193.         prev = this;
  194.         this = this->next;
  195.     }
  196.     if (this && (this == lpr)) {
  197.         /* unhook it */
  198.         if (prev)
  199.             prev->next = this->next;
  200.         else
  201.             prlist = this->next;
  202.     }
  203. }
  204.  
  205.  
  206. /* GetWindowLong(GetParent(hDlg), 4) must be available for use */
  207. BOOL CALLBACK _export
  208. PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  209. {
  210.     LPPRINT lpr;
  211.     lpr = (LPPRINT)GetWindowLong(GetParent(hDlg), 4);
  212.  
  213.     switch(message) {
  214.         case WM_INITDIALOG:
  215.             lpr->hDlgPrint = hDlg;
  216.             SetWindowText(hDlg,(LPSTR)lParam);
  217.             EnableMenuItem(GetSystemMenu(hDlg,FALSE),SC_CLOSE,MF_GRAYED);
  218.             return TRUE;
  219.         case WM_COMMAND:
  220.             lpr->bUserAbort = TRUE;
  221.             lpr->hDlgPrint = 0;
  222.             EnableWindow(GetParent(hDlg),TRUE);
  223.             EndDialog(hDlg, FALSE);
  224.             return TRUE;
  225.     }
  226.     return FALSE;
  227. }
  228.  
  229.     
  230. BOOL CALLBACK _export
  231. PrintAbortProc(HDC hdcPrn, int code)
  232. {
  233.     MSG msg;
  234.     LPPRINT lpr;
  235.     lpr = PrintFind(hdcPrn);
  236.  
  237.     while (!lpr->bUserAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  238.         if (!lpr->hDlgPrint || !IsDialogMessage(lpr->hDlgPrint,&msg)) {
  239.             TranslateMessage(&msg);
  240.             DispatchMessage(&msg);
  241.         }
  242.     }
  243.     return(!lpr->bUserAbort);
  244. }
  245.  
  246.  
  247. /* GetWindowLong(hwnd, 4) must be available for use */
  248. void WDPROC
  249. DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
  250. {
  251. HDC printer;
  252. char *buf;
  253. int *bufcount, count;
  254. HFILE hfile;
  255. DLGPROC lpfnAbortProc;
  256. DLGPROC lpfnPrintDlgProc;
  257. PRINTDLG pd;
  258. PRINT pr;
  259.  
  260.     _fmemset(&pd, 0, sizeof(PRINTDLG));
  261.     pd.lStructSize = sizeof(PRINTDLG);
  262.     pd.hwndOwner = hwnd;
  263.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  264.  
  265.     if (PrintDlg(&pd)) {
  266.     printer = pd.hDC;
  267.     if (printer != (HDC)NULL) {
  268.       pr.hdcPrn = printer;
  269.       SetWindowLong(hwnd, 4, (LONG)((LPPRINT)&pr));
  270.       PrintRegister((LPPRINT)&pr);
  271.        if ( (hfile = _lopen(szFileName, READ)) != HFILE_ERROR) {
  272.           if ( (buf = LocalAllocPtr(LHND, 4096+2)) != (char *)NULL ) {
  273.             bufcount = (int *)buf;
  274.             EnableWindow(hwnd,FALSE);
  275.             pr.bUserAbort = FALSE;
  276. #ifdef __DLL__
  277.             lpfnPrintDlgProc = (DLGPROC)GetProcAddress(hdllInstance, "PrintDlgProc");
  278.             lpfnAbortProc = (DLGPROC)GetProcAddress(hdllInstance, "PrintAbortProc");
  279. #else
  280.             lpfnPrintDlgProc = (DLGPROC)MakeProcInstance((FARPROC)PrintDlgProc, hdllInstance);
  281.             lpfnAbortProc = (DLGPROC)MakeProcInstance((FARPROC)PrintAbortProc, hdllInstance);
  282. #endif
  283.             pr.hDlgPrint = CreateDialogParam(hdllInstance,"PrintDlgBox",hwnd,lpfnPrintDlgProc,(LPARAM)szAppName);
  284.             Escape(printer,SETABORTPROC,0,(LPSTR)lpfnAbortProc,NULL);  
  285.             if (Escape(printer, STARTDOC, lstrlen(szAppName),szAppName, NULL) > 0)
  286.             {
  287.                 while ( (count = _lread(hfile, buf+2, 4096)) != 0 ) {
  288.                     *bufcount = count;
  289.                     Escape(printer, PASSTHROUGH, count+2, (LPSTR)buf, NULL);
  290.                 }
  291.                 if (pr.bUserAbort) 
  292.                     Escape(printer,ABORTDOC,0,NULL,NULL);
  293.                 else
  294.                     Escape(printer,ENDDOC,0,NULL,NULL);
  295.             }
  296.             if (!pr.bUserAbort) {
  297.                 EnableWindow(hwnd,TRUE);
  298.                 DestroyWindow(pr.hDlgPrint);
  299.             }
  300.             LocalFreePtr(buf);
  301. #ifndef __DLL__
  302.             FreeProcInstance((FARPROC)lpfnPrintDlgProc);
  303.             FreeProcInstance((FARPROC)lpfnAbortProc);
  304. #endif
  305.         }
  306.         _lclose(hfile);
  307.       }
  308.       DeleteDC(printer);
  309.       SetWindowLong(hwnd, 4, (LONG)(0L));
  310.       PrintUnregister((LPPRINT)&pr);
  311.     }
  312.     }
  313. }
  314.  
  315. #else
  316. void WDPROC
  317. DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName);
  318. {
  319.     MessageBox(GetDesktopWindow(), "wgnuplib.dll needs to be compiled with WINVER >= 0x030a",
  320.         "wgnuplib.dll", MB_OK | MB_ICONEXCLAMATION
  321. }
  322. #endif
  323.