home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GP_MSWIN.C < prev    next >
C/C++ Source or Header  |  1992-07-17  |  14KB  |  580 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gp_mswin.c */
  21. /*
  22.  * Microsoft Windows 3.n platform support for Ghostscript.
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  26.  */
  27.  
  28. #include <windows.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. #include "gp_mswin.h"
  33.  
  34. #include "memory_.h"
  35. #include "gx.h"
  36. #include "gp.h"
  37. #include "gpcheck.h"
  38. #include "gserrors.h"
  39. #include "gxdevice.h"
  40.  
  41. #include "dos_.h"
  42. #include <fcntl.h>
  43. #include <io.h>
  44. #include <signal.h>
  45. #include "string_.h"
  46.  
  47. /* Library routines not declared in a standard header */
  48. extern char *getenv(P1(const char *));
  49.  
  50. /* ------ from gnuplot winmain.c plus new stuff ------ */
  51.  
  52.  
  53. /* limits */
  54. #define MAXSTR 255
  55. #define BIGBLK 4096
  56.  
  57. /* Linked list of currently open window devices */
  58. gx_device *gdev_win_open_list;
  59.  
  60. /* public handles */
  61. HWND FAR hwndeasy;
  62. HANDLE FAR phInstance;
  63.  
  64. const char FAR szAppName[] = "Ghostscript";
  65. char win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  66.  
  67. /* printer structure */
  68. struct PrStr{
  69.     char far *dev;
  70.     char far *out;
  71.     char far *drv;
  72.     struct PrStr * far next;
  73.     };
  74.  
  75. struct PrStr * far prlst = NULL;
  76. struct PrStr * far defprlst = NULL;
  77. static HANDLE FAR lib = 0;
  78.  
  79. int FAR PASCAL printselproc();
  80. BOOL FAR PASCAL AbortProc();
  81.  
  82. /* EasyWin */
  83. extern POINT _ScreenSize;
  84. extern BOOL _KeyPressed();
  85.  
  86. int main(int argc, char *argv[], char *env[]);
  87.  
  88. int win_init = 0;    /* flag to know if gp_exit has been called */
  89.  
  90. /* our exit handler */
  91. void win_exit(void)
  92. {
  93.     /* if we didn't exit through gs_exit() then do so now */
  94.     if (win_init)
  95.         gs_exit(0);
  96.  
  97.     fcloseall();
  98.     DestroyWindow(hwndeasy);
  99. }
  100.  
  101. extern long far PASCAL WndProc(HWND, WORD, WORD, LONG);
  102.  
  103. int PASCAL 
  104. WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  105. {
  106.     WNDCLASS wndclass;
  107.  
  108.     char modulename[MAXSTR];
  109.  
  110.     /* Initialize the list of open Windows devices */
  111.     gdev_win_open_list = 0;
  112.  
  113.     /* copy the hInstance into a variable so it can be used */
  114.     phInstance = hInstance;
  115.  
  116.         /* start up the text window */
  117.     _ScreenSize.y = 50;
  118.     _InitEasyWin();
  119.  
  120.     /* fix up the EasyWindows window provided by Borland */
  121.     GetModuleFileName(hInstance, (LPSTR) modulename, MAXSTR);
  122.     hwndeasy = FindWindow("BCEasyWin", modulename);
  123.     SetWindowText(hwndeasy, szAppName);            /* change title */
  124.     SetClassWord(hwndeasy, GCW_HICON, LoadIcon(hInstance, "texticon"));
  125.  
  126.     /* if this is the first Ghostscript then register the window class */
  127.     /* for graphics */
  128.     if (!hPrevInstance) {
  129.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  130.         wndclass.lpfnWndProc = WndProc;
  131.         wndclass.cbClsExtra = 0;
  132.         wndclass.cbWndExtra = 0;
  133.         wndclass.hInstance = hInstance;
  134.         wndclass.hIcon = LoadIcon(hInstance,"grpicon");
  135.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  136.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  137.         wndclass.lpszMenuName = NULL;
  138.         wndclass.lpszClassName = szAppName;
  139.         RegisterClass(&wndclass);
  140.     }
  141.  
  142.     (void) atexit(win_exit); /* setup exit handler */
  143.  
  144.     main(_argc, _argv, environ);
  145.  
  146.     /* never reached */
  147.     win_exit(); 
  148.     return 0;
  149. }
  150.  
  151. void destroyproflist()
  152. {
  153.     struct PrStr far *cur;
  154.     struct PrStr far *next;
  155.  
  156.     cur = prlst;
  157.     prlst = NULL;
  158.     while (cur != NULL)
  159.     {
  160.         free(cur->dev);
  161.         free(cur->out);
  162.         free(cur->drv);
  163.         next = cur->next;
  164.         free(cur);
  165.         cur = next;
  166.         }
  167.     if (defprlst != NULL)
  168.     {
  169.         free(defprlst->dev);
  170.         free(defprlst->out);
  171.         free(defprlst->drv);
  172.         free(defprlst);
  173.         defprlst = NULL;
  174.         }
  175.     }
  176.  
  177. HDC FAR getprinter()
  178. {
  179.     /* See Petzold, "Programming Windows", ed 2, p721 */
  180.     /* Use Default device and allow others */
  181.     char name[MAXSTR];
  182.     LPSTR dev;
  183.     LPSTR drv;
  184.     LPSTR out;
  185.     FARPROC prslinst;
  186.     int dlgret;
  187.     char alldev[BIGBLK];
  188.     struct PrStr far *cur = NULL; 
  189.     struct PrStr far *last = NULL; 
  190.     char *strptr;
  191.     HDC retdc;
  192.  
  193.     /* get the useful info on the default printer */
  194.     GetProfileString("windows", "device", ",,,", name, MAXSTR);
  195.     dev = strtok(name, ",");
  196.     drv = strtok(NULL, ", ");
  197.     out = strtok(NULL, ", ");
  198.     /* if OK store it as default */
  199.     if (dev && drv && out)
  200.     {
  201.         defprlst = (struct PrStr far *) malloc(sizeof(struct PrStr));
  202.         defprlst->next = NULL;
  203.         defprlst->dev = (char far *) malloc(strlen(dev)+1);
  204.         defprlst->out = (char far *) malloc(strlen(out)+1);
  205.         defprlst->drv = (char far *) malloc(strlen(drv)+1);
  206.         _fstrcpy(defprlst->dev, (LPSTR) dev);
  207.         _fstrcpy(defprlst->out, (LPSTR) out);
  208.         _fstrcpy(defprlst->drv, (LPSTR) drv);
  209.         }
  210.     
  211.     /* find out about the names of other devices */
  212.     GetProfileString("devices", NULL, "", alldev, BIGBLK);
  213.     strptr = alldev;
  214.     while (*strptr)
  215.     {
  216.         if (cur == NULL)
  217.         {
  218.             if (prlst == NULL)
  219.                 cur = last = prlst = (struct PrStr far *) malloc(sizeof(struct PrStr));
  220.             else
  221.                 cur = last = prlst->next = (struct PrStr far *) malloc(sizeof(struct PrStr));
  222.             }
  223.         else
  224.         {
  225.             last = last->next = (struct PrStr far *) malloc(sizeof(struct PrStr));
  226.             }
  227.         last->dev = (char far *) malloc(strlen(strptr)+1);
  228.         _fstrcpy(last->dev, (LPSTR) strptr);
  229.         last->next = NULL;
  230.         last->drv = NULL;
  231.         last->out = NULL;
  232.         strptr += strlen(strptr) + 1;
  233.         }
  234.  
  235.     /* find out the details */
  236.     while (cur)
  237.     {
  238.         GetProfileString("devices", cur->dev, "", name, MAXSTR);
  239.         drv = strtok(name, ",");
  240.         out = strtok(NULL, ", ");
  241.         cur->out = (char far *) malloc(strlen(out)+1);
  242.         cur->drv = (char far *) malloc(strlen(drv)+1);
  243.         _fstrcpy(cur->out, (LPSTR) out);
  244.         _fstrcpy(cur->drv, (LPSTR) drv);
  245.         cur = cur->next;
  246.         }
  247.  
  248.     /* open dialog box */
  249.     prslinst = MakeProcInstance(printselproc, phInstance);
  250.     dlgret = DialogBox(phInstance, "PRSEL", hwndeasy, prslinst);
  251.     if (dlgret == IDCANCEL)
  252.     {
  253.         destroyproflist();
  254.         return NULL;
  255.         }
  256.     dlgret = dlgret - IDOK - IDCANCEL;
  257.  
  258.     if (dlgret == 0)
  259.     {
  260.         dev = defprlst->dev;
  261.         drv = defprlst->drv;
  262.         out = defprlst->out;
  263.         }
  264.     else
  265.     {
  266.         dlgret--;
  267.         cur = prlst;
  268.         while (dlgret > 0)
  269.         {
  270.             cur = cur->next;
  271.             dlgret--;
  272.             }
  273.  
  274.         dev = cur->dev;
  275.         drv = cur->drv;
  276.         out = cur->out;
  277.         }
  278.  
  279.     /* create device context */
  280.     retdc = CreateDC(drv, dev, out, (void *) NULL);
  281.     destroyproflist();
  282.  
  283.     return retdc;
  284.     }
  285.  
  286. int FAR PASCAL printselproc(hdlg, wmsg, wparam, lparam)
  287. HWND hdlg;
  288. WORD wmsg;
  289. WORD wparam;
  290. DWORD lparam;
  291. {
  292.     struct PrStr far *cur; 
  293.     LPSTR proutstr;
  294.     HWND menu;
  295.     int val;
  296.     LPSTR dev;
  297.     LPSTR drv;
  298.     LPSTR out;
  299.     char *drvnam;
  300.     void (FAR PASCAL *dm)(HWND, HANDLE, LPSTR, LPSTR);
  301.  
  302.     switch (wmsg)
  303.     {
  304.         case WM_INITDIALOG:
  305.             /* default message */
  306.             if (defprlst != NULL)
  307.             {
  308.                 menu = GetDlgItem(hdlg, DEFPRNSTR);
  309.                 proutstr = (LPSTR) malloc(strlen(defprlst->dev) + strlen(defprlst->out) + 17);
  310.                 _fstrcpy(proutstr, (LPSTR) "(Currently ");
  311.                 _fstrcat(proutstr, defprlst->dev);
  312.                 _fstrcat(proutstr, (LPSTR) " on ");
  313.                 _fstrcat(proutstr, defprlst->out);
  314.                 _fstrcat(proutstr, (LPSTR) ")");
  315.                 SetWindowText(menu, (LPSTR) proutstr);
  316.                 EnableWindow(GetDlgItem(hdlg, DEFPRN), TRUE);
  317.                 SendMessage(GetDlgItem(hdlg, DEFPRN), BM_SETCHECK, 1, 0L);
  318.                 }
  319.             else
  320.                 EnableWindow(GetDlgItem(hdlg, DEFPRN), FALSE);
  321.                 
  322.  
  323.             /* list of all other printers */
  324.             menu = GetDlgItem(hdlg, PRMNU);
  325.             if (prlst == NULL)
  326.                 EnableWindow(GetDlgItem(hdlg, SPECPRN), FALSE);
  327.             else
  328.             {
  329.                 EnableWindow(GetDlgItem(hdlg, SPECPRN), TRUE);
  330.                 if (defprlst == NULL)
  331.                     SendMessage(GetDlgItem(hdlg, SPECPRN), BM_SETCHECK, 1, 0L);
  332.                 }
  333.             cur = prlst;
  334.             while (cur != NULL)
  335.             {
  336.                 proutstr = (LPSTR) malloc(strlen(cur->dev) + strlen(cur->out) + 5);
  337.                 _fstrcpy(proutstr, cur->dev);
  338.                 _fstrcat(proutstr, (LPSTR) " on ");
  339.                 _fstrcat(proutstr, cur->out);
  340.                 SendMessage(menu,CB_INSERTSTRING, -1, (LONG) (LPSTR) proutstr);
  341.                 free(proutstr);
  342.                 cur = cur->next;
  343.                 }
  344.             SendMessage(menu,CB_SETCURSEL, 0, 0);
  345.             return TRUE;
  346.         case WM_COMMAND:
  347.             switch (wparam)
  348.             {
  349.                 case IDOK:
  350.                     if (SendMessage(GetDlgItem(hdlg, DEFPRN), BM_GETCHECK, 0, 0L))
  351.                         val = 0;
  352.                     else if (SendMessage(GetDlgItem(hdlg, SPECPRN), BM_GETCHECK, 0, 0L))
  353.                         {
  354.                             menu = GetDlgItem(hdlg, PRMNU);
  355.                             val = SendMessage(menu,CB_GETCURSEL, 0, 0L)+1;
  356.                             }
  357.                     else
  358.                         EndDialog(hdlg, IDCANCEL);
  359.                     if (val == CB_ERR)
  360.                         EndDialog(hdlg, IDCANCEL);        /* catch all */
  361.                     else
  362.                         EndDialog(hdlg, val + IDOK + IDCANCEL);
  363.                     return TRUE;
  364.                 case IDCANCEL:
  365.                     EndDialog(hdlg, IDCANCEL);
  366.                     return TRUE;
  367.                 case PRSET:
  368.                     dev = NULL;        /* clear the options */
  369.                     drv = NULL;
  370.                     out = NULL;
  371.                     /* if button DEFAULT get options */
  372.                     if (SendMessage(GetDlgItem(hdlg, DEFPRN), BM_GETCHECK, 0, 0L))
  373.                     {
  374.                         dev = defprlst->dev;
  375.                         drv = defprlst->drv;
  376.                         out = defprlst->out;
  377.                         }
  378.                     /* if button SPECIFIC get options */
  379.                     if (SendMessage(GetDlgItem(hdlg, SPECPRN), BM_GETCHECK, 0, 0L))
  380.                     {
  381.                         menu = GetDlgItem(hdlg, PRMNU);
  382.                         val = SendMessage(menu,CB_GETCURSEL, 0, 0);
  383.                         cur = prlst;
  384.                         while (val > 0)
  385.                         {
  386.                             cur = cur->next;
  387.                             val--;
  388.                             }
  389.                         dev = cur->dev;
  390.                         drv = cur->drv;
  391.                         out = cur->out;
  392.                         }
  393.             
  394.                     if (dev != NULL)
  395.                     {    
  396.                         /* allow configuration of printer */
  397.                         drvnam = (char *) malloc(strlen(drv) + 5);
  398.                         strcpy(drvnam, drv);
  399.                         strcat(drvnam, ".DRV");
  400.                         lib = LoadLibrary(drvnam);
  401.                         free(drvnam);
  402.                         if (lib >= 32)
  403.                         {
  404.                             dm = (void (FAR PASCAL *)()) GetProcAddress(lib, (LPSTR) "DEVICEMODE");
  405.                             (*dm)((HWND) hdlg, (HANDLE)lib, (LPSTR) dev, (LPSTR) out);
  406.                             FreeLibrary(lib);
  407.                             }
  408.                         }
  409.                     return TRUE;
  410.                 }
  411.             break;
  412.         }
  413.     return FALSE;
  414.     }
  415.  
  416. BOOL FAR PASCAL AbortProc(HDC hdcPrn, int code)
  417. {
  418.     MSG msg;
  419.  
  420.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  421.         {
  422.         TranslateMessage(&msg);
  423.         DispatchMessage(&msg);
  424.         }
  425.     return(TRUE);
  426. }
  427.   
  428. /* ------ Process message loop ------ */
  429. /*
  430.  * Check messages and interrupts; return true if interrupted.
  431.  * This is called frequently - it must be quick!
  432.  */
  433. int
  434. gp_check_interrupts()
  435. {
  436.     _KeyPressed();    /* Borland EasyWin message loop */
  437.     return 0;
  438. }
  439.  
  440. /* ------ from gp_dosfb.c ------ */
  441.  
  442. /* ------ Screen management ------ */
  443.  
  444. /* Write a string to the console. */
  445. void
  446. gp_console_puts(const char *str, uint size)
  447. {    fwrite(str, 1, size, stdout);
  448. }
  449.  
  450. /* Make the console current on the screen. */
  451. int
  452. gp_make_console_current(struct gx_device_s *dev)
  453. {    return 0;
  454. }
  455.  
  456. /* Make the graphics current on the screen. */
  457. int
  458. gp_make_graphics_current(struct gx_device_s *dev)
  459. {    return 0;
  460. }
  461.  
  462. /* ------ from gp_itbc.c ------ */
  463.  
  464. /* Do platform-dependent initialization. */
  465. void
  466. gp_init()
  467. {
  468.     win_init = 1;
  469. }
  470.  
  471. /* Do platform-dependent cleanup. */
  472. void
  473. gp_exit()
  474. {
  475.     win_init = 0;
  476. }
  477.  
  478. /* ------ Printer accessing ------ */
  479.  
  480. /* Open a connection to a printer.  A null file name means use the */
  481. /* standard printer connected to the machine, if any. */
  482. /* Return NULL if the connection could not be opened. */
  483. extern void gp_set_printer_binary(P1(int));
  484. FILE *
  485. gp_open_printer(char *fname)
  486. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  487.     {    FILE *pfile;
  488.         pfile = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  489.             win_prntmp, "wb");
  490.         return pfile;
  491.     }
  492.     else
  493.         return fopen(fname, "wb");
  494. }
  495.  
  496. /* Close the connection to the printer. */
  497. void
  498. gp_close_printer(FILE *pfile, const char *fname)
  499. {
  500. HDC printer;
  501. char *buf;
  502. int *bufcount, count;
  503. FARPROC lpfnAbortProc;
  504.     fclose(pfile);
  505.     if (strlen(fname) && strcmp(fname,"PRN"))
  506.         return;        /* a file, not a printer */
  507.     printer = getprinter();
  508.     if (printer != (HDC)NULL) {
  509.       if ( (pfile = fopen(win_prntmp,"rb")) != (FILE *)NULL) {
  510.         if ( (buf = malloc(4096+2)) != (char *)NULL ) {
  511.             bufcount = (int *)buf;
  512.         EnableWindow(hwndeasy,FALSE);
  513.         lpfnAbortProc = MakeProcInstance(AbortProc,phInstance); 
  514.             if (Escape(printer, STARTDOC, strlen(szAppName),szAppName, NULL) > 0) {
  515.             while (!feof(pfile)) {
  516.                 count = fread(buf+2,1,4096,pfile);
  517.                 *bufcount = count;
  518.                 Escape(printer, PASSTHROUGH, count+2, (LPSTR)buf, NULL);
  519.             }
  520.             Escape(printer,ENDDOC,0,NULL,NULL);
  521.         }
  522.         FreeProcInstance(lpfnAbortProc);
  523.         EnableWindow(hwndeasy,TRUE);
  524.         free(buf);
  525.         }
  526.         fclose(pfile);
  527.       }
  528.       DeleteDC(printer);
  529.     }
  530.     unlink(win_prntmp);
  531. }
  532.  
  533. /* ------ File names ------ */
  534.  
  535. /* Create and open a scratch file with a given name prefix. */
  536. /* Write the actual file name at fname. */
  537. FILE *
  538. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  539. {    char *temp;
  540.     if ( (temp = getenv("TEMP")) == NULL )
  541.         *fname = 0;
  542.     else
  543.     {    strcpy(fname, temp);
  544.         /* Prevent X's in path from being converted by mktemp. */
  545.         for ( temp = fname; *temp; temp++ )
  546.             *temp = tolower(*temp);
  547.         strcat(fname, "\\");
  548.     }
  549.     strcat(fname, prefix);
  550.     strcat(fname, "XXXXXX");
  551.     mktemp(fname);
  552.     return fopen(fname, mode);
  553. }
  554.  
  555. /* ------ File operations ------ */
  556.  
  557. /* If the file given by fname exists, fill in its status and return 1; */
  558. /* otherwise return 0. */
  559. int
  560. gp_file_status(const char *fname, file_status *pstatus)
  561. {    FILE *f = fopen(fname, "r");
  562.     long flen;
  563.     struct ftime ft;
  564.     if ( f == NULL ) return 0;
  565.     if ( getftime(fileno(f), &ft) < 0 )
  566.        {    fclose(f);
  567.         return 0;
  568.        }
  569.     fseek(f, 0, SEEK_END);
  570.     flen = ftell(f);
  571.     pstatus->size_pages = (flen + 1023) >> 10;
  572.     pstatus->size_bytes = flen;
  573.     /* Make a single long value from the ftime structure. */
  574.     pstatus->time_referenced = pstatus->time_created =
  575.       ((long)((ft.ft_year << 9) + (ft.ft_month << 5) + ft.ft_day) << 16) +
  576.       ((ft.ft_hour << 11) + (ft.ft_min << 5) + ft.ft_tsec);
  577.     fclose(f);
  578.     return 1;
  579. }
  580.