home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvwdisp.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  7KB  |  232 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvwdisp.c */
  19. /* Display GSview routines for Windows */
  20. #include "gvwin.h"
  21.  
  22. /* handle messages while we are waiting */
  23. void
  24. do_message(void)
  25. {
  26.     MSG msg;
  27.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  28.     if ((hDlgModeless == 0) || !IsDialogMessage(hDlgModeless, &msg)) {
  29.             TranslateMessage(&msg);
  30.             DispatchMessage(&msg);
  31.         }
  32.     }
  33. }
  34.  
  35. /* run Ghostscript for previewing document */
  36. /* return TRUE if ok, FALSE if error */
  37. BOOL
  38. gs_open(void)
  39. {
  40. #ifdef WIN32
  41. char command[256];
  42. #else
  43. char command[512];
  44. #endif
  45.     /* return if already open */
  46.     if ((gsprog.valid) && IsWindow(hwndimgchild))
  47.         return TRUE;
  48.  
  49.     display.do_endfile = FALSE;
  50.     display.do_resize = FALSE;
  51.     zoom = FALSE;
  52.     pipeinit();        /* so we wait for first request */
  53.     gs_size();
  54.     sprintf(command,"%s -dBitsPerPixel=%d %s -r%gx%g -g%ux%u -sGSVIEW=%u -",
  55.         option.gscommand, (option.depth ? option.depth : display.bitcount*display.planes),
  56.         option.safer ? "-dSAFER" : "", 
  57.         option.xdpi, option.ydpi, 
  58.                 display.width, display.height, (unsigned int)hwndimg);
  59.     if ( ((strlen(command) > 126) && !(is_winnt || is_win95))
  60.       ||  (strlen(command) > 256) ) {
  61.         display.do_display = FALSE;
  62.         gserror(IDS_TOOLONG, command, MB_ICONSTOP, SOUND_ERROR);
  63.         return FALSE;
  64.     }
  65.     load_string(IDS_WAITGSOPEN, szWait, sizeof(szWait));
  66.     info_wait(TRUE);
  67.     gsprog.hinst = (HINSTANCE)WinExec(command, SW_SHOWMINNOACTIVE);
  68.  
  69. #ifdef __WIN32__
  70.     if ( (is_win95 && ((int)gsprog.hinst < HINSTANCE_ERROR))
  71.       || (is_winnt && ((int)gsprog.hinst < HINSTANCE_ERROR))
  72.           || (gsprog.hinst == NULL) ) {    /* Win32s WinExec returned buggy value */
  73. #else
  74.     if (gsprog.hinst < HINSTANCE_ERROR) {
  75. #endif
  76.         info_wait(FALSE);
  77.         display.do_display = FALSE;
  78.         gserror(IDS_CANNOTRUN, command, MB_ICONSTOP, SOUND_ERROR);
  79.         load_string(IDS_TOPICINSTALL, szHelpTopic, sizeof(szHelpTopic));
  80.         get_help();
  81.         return FALSE;
  82.     }
  83.     {   /* TEMPORARY KLUDGE */
  84.     int i;
  85.     for (i=0; i<10; i++)
  86.         do_message();    /* wait for gswin to establish with debugger running */
  87.     }
  88. #ifdef NOTUSED
  89. #endif
  90.     if (hwndtext == (HWND)NULL) {
  91.         /* we are running an incompatible version of Ghostscript */
  92.         hwndtext = FindWindow("BCEasyWin","Ghostscript");
  93.         if (hwndtext) {
  94.             SendMessage(hwndtext, WM_CHAR, 'q', 1L);
  95.             SendMessage(hwndtext, WM_CHAR, 'u', 1L);
  96.             SendMessage(hwndtext, WM_CHAR, 'i', 1L);
  97.             SendMessage(hwndtext, WM_CHAR, 't', 1L);
  98.             SendMessage(hwndtext, WM_CHAR, '\r', 1L);
  99.         }
  100.         hwndtext = (HWND)NULL;
  101.         hwndimgchild = (HWND)NULL;
  102.         gsprog.hinst = (HINSTANCE)NULL;
  103.         clear_timer();
  104.         info_wait(FALSE);
  105.         gserror(IDS_WRONGGS, NULL, MB_ICONSTOP, SOUND_ERROR);
  106.         return FALSE;
  107.     }
  108.  
  109.     /* wait for gswin to initialise */
  110.     if (set_timer(CLOSE_TIMEOUT))
  111.         EnableWindow(hwndimg, FALSE);
  112.     while (!is_pipe_done()&&  !bTimeout)
  113.         do_message();    /* wait for pipe data request from gswin */
  114.     clear_timer();
  115.     EnableWindow(hwndimg, TRUE);
  116.  
  117.     display.saved = FALSE;
  118.     display.page = FALSE;
  119.     display.sync = FALSE;
  120.     gsprog.input = pipeopen();    /* open pipe to gswin */
  121.     gsprog.valid = TRUE;
  122.     BringWindowToTop(hwndimg);
  123.     SetFocus(hwndimg);    /* kludge: without this desktop gets focus */
  124.     return TRUE;
  125. }
  126.  
  127. /* close Ghostscript */
  128. BOOL
  129. gs_close(void)
  130. {
  131. BOOL force = FALSE;
  132.     if (!gsprog.valid)
  133.         return TRUE;
  134.  
  135.     if (doc == (PSDOC *)NULL) {
  136.         /* we don't know how many pages remain so we must force an exit */
  137.         if (!is_pipe_done())
  138.         force = TRUE;
  139.     }
  140.     else {
  141.         if (display.page)
  142.         next_page();
  143.     }
  144.  
  145.     if (!force) {
  146.         /* try to close Ghostscript cleanly */
  147.         pipeclose();
  148.         if (set_timer(CLOSE_TIMEOUT))
  149.         EnableWindow(hwndimg, FALSE);
  150. #ifdef __WIN32__
  151.         {   /* TEMPORARY KLUDGE */
  152.         int i;
  153.         for (i=0; i<10; i++) {
  154.             do_message();    /* wait for gswin to close */
  155.             Sleep(50);
  156.         }
  157.         }
  158. #else
  159.         while (GetModuleUsage(gsprog.hinst) &&  !bTimeout)
  160.         do_message();    /* wait for gswin to close */
  161. #endif
  162.         clear_timer();
  163.         EnableWindow(hwndimg, TRUE);
  164.     }
  165.     do_message();
  166.  
  167.     /* if still there try killing it a using a brute force method */
  168.     if (IsWindow(hwndtext)) {
  169.         if (is_win31) {
  170.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  171.             if (IsWindow(hwndtext))
  172.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  173.         }
  174.         else {
  175.             /* Windows 3.0 hangs if we use SendMessage */
  176.             PostMessage(hwndtext, WM_CLOSE, 0, 0L);
  177.             do_message();
  178.         }
  179.     }
  180.  
  181.     do_message();
  182.     gsprog.hinst = (HINSTANCE)NULL;
  183.     hwndimgchild = (HWND)NULL;
  184.     hwndtext = (HWND)NULL;
  185.     bitmap_scrollx = bitmap_scrolly = 0;
  186.     display.saved = FALSE;
  187.     display.epsf_clipped = FALSE;
  188.     display.page = FALSE;
  189.     display.sync = FALSE;
  190.     pipeclose();
  191.     return TRUE;
  192. }
  193.  
  194. /* send a NEXT_PAGE message to Ghostscript */
  195. void
  196. next_page(void)
  197. {
  198. int i;
  199.     if (hwndimgchild && IsWindow(hwndimgchild)) {
  200.         SendMessage(hwndimgchild, WM_GSVIEW, NEXT_PAGE, 0L);
  201.         display.page = FALSE;
  202.     }
  203.     do_message();    /* wait for Ghostscript to process message */
  204.     for (i=0; i<32; i++) {
  205.        /* Wait a bit for pipe contents after showpage to be read */
  206.        do_message();
  207.        if (is_pipe_done())
  208.         break;
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. /* return TRUE if file length or modification time changed */
  215. BOOL
  216. psfile_changed(void)
  217. {
  218. struct ftime thisftime;
  219. long thisflength;
  220.     getftime(fileno(psfile.file), &thisftime);
  221.     thisflength = filelength(fileno(psfile.file));
  222.     return ( (thisflength != psfile.length) ||
  223.         memcmp(&thisftime, &psfile.datetime, sizeof(thisftime)) );
  224. }
  225.  
  226. void
  227. psfile_savestat(void)
  228. {
  229.     getftime(fileno(psfile.file), &psfile.datetime);
  230.     psfile.length = filelength(fileno(psfile.file));
  231. }
  232.