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 / GDEVMSWN.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  29KB  |  1,009 lines

  1. /* Copyright (C) 1989, 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. /* gdevmswn.c */
  21. /*
  22.  * Microsoft Windows 3.n driver for Ghostscript.
  23.  * Original version by Russell Lang and Maurice Castro with help from
  24.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  25.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  26.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  27.  */
  28. #include <windows.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "gx.h"
  32. #include "gserrors.h"
  33. #include "gxdevice.h"
  34. #include "gp.h"
  35. #include "gpcheck.h"
  36.  
  37. /* system menu constants for image window */
  38. #define M_COPY_CLIP 1
  39.  
  40. /* externals from gp_win.c */
  41. extern HWND FAR hwndeasy;
  42. extern HANDLE FAR phInstance;
  43. extern const char FAR szAppName[];
  44. extern gx_device *gdev_win_open_list;
  45.  
  46. typedef struct gx_device_win_s gx_device_win;
  47.  
  48. /* Forward references */
  49. private LPLOGPALETTE near win_makepalette(P1(gx_device_win *));
  50. private void near win_makeimg(P1(gx_device_win *));
  51. private void near win_addtool(P2(gx_device_win *, int));
  52. private void near win_maketools(P2(gx_device_win *, HDC));
  53. private void near win_destroytools(P1(gx_device_win *));
  54. private void near win_nomemory();
  55. private void near win_update(P2(gx_device_win *, int));
  56. private const char FAR imgname[] = "Ghostscript Image";
  57.  
  58. /* See gxdevice.h for the definitions of the procedures. */
  59.  
  60. private dev_proc_open_device(win_open);
  61. private dev_proc_sync_output(win_sync_output);
  62. private dev_proc_output_page(win_output_page);
  63. private dev_proc_close_device(win_close);
  64. private dev_proc_map_rgb_color(win_map_rgb_color);
  65. private dev_proc_map_color_rgb(win_map_color_rgb);
  66. private dev_proc_fill_rectangle(win_fill_rectangle);
  67. private dev_proc_tile_rectangle(win_tile_rectangle);
  68. private dev_proc_copy_mono(win_copy_mono);
  69. private dev_proc_copy_color(win_copy_color);
  70. private dev_proc_draw_line(win_draw_line);
  71.  
  72. /* The device descriptor */
  73. struct gx_device_win_s {
  74.     gx_device_common;
  75.     int nColors;
  76.  
  77.     /* Handles */
  78.  
  79.     HWND FAR hwndimg;
  80.     HBITMAP FAR hbitmap;
  81.     HDC FAR hdcbit;
  82.     HPEN hpen, *hpens;
  83.     HBRUSH hbrush, *hbrushs;
  84.     HPALETTE hpalette, himgpalette;
  85.     LPLOGPALETTE lpalette, limgpalette;
  86.  
  87.     /* A staging bitmap for copy_mono. */
  88.     /* We want one big enough to handle the standard 16x16 halftone; */
  89.     /* this is also big enough for ordinary-size characters. */
  90.  
  91. #define bmWidthBytes 4        /* must be even */
  92. #define bmWidthBits (bmWidthBytes * 8)
  93. #define bmHeight 16
  94.     HBITMAP FAR hbmmono;
  95.     HDC FAR hdcmono;
  96. #define rop_write_at_1s 0xE20746L    /* write brush at 1's */
  97. #define rop_write_at_0s 0xB8074AL    /* write brush at 0's */
  98.     gx_bitmap_id bm_id;
  99.  
  100.     /* Window scrolling stuff */
  101.  
  102.     int cxClient, cyClient;
  103.     int cxAdjust, cyAdjust;
  104.     int nVscrollPos, nVscrollMax;
  105.     int nHscrollPos, nHscrollMax;
  106.  
  107.     /* Link for the list of open Windows devices */
  108.  
  109.     gx_device_win *next_wdev;
  110. };
  111. #define wdev ((gx_device_win *)dev)
  112. private gx_device_procs win_procs = {
  113.     win_open,
  114.     gx_default_get_initial_matrix,
  115.     win_sync_output,
  116.     win_output_page,
  117.     win_close,
  118.     win_map_rgb_color,
  119.     win_map_color_rgb,
  120.     win_fill_rectangle,
  121.     win_tile_rectangle,
  122.     win_copy_mono,
  123.     win_copy_color,
  124.     win_draw_line,
  125.     gx_default_get_bits,
  126.     gx_default_get_props,
  127.     gx_default_put_props
  128. };
  129. gx_device_win gs_mswin_device = {
  130.     sizeof(gx_device_win),
  131.     &win_procs,
  132.     "win",
  133. #define INITIAL_RESOLUTION 96
  134. #define INITIAL_WIDTH (INITIAL_RESOLUTION * 85 / 10 + 1)
  135. #define INITIAL_HEIGHT (INITIAL_RESOLUTION * 11 + 1)
  136.     INITIAL_WIDTH, INITIAL_HEIGHT,     /* win_open() fills these in later */
  137.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,    /* win_open() fills these in later */
  138.     no_margins,
  139.     dci_black_and_white,
  140.     0,        /* not open yet */
  141.     2        /* nColors = 2 */
  142. };
  143.  
  144. /* Open the win driver */
  145. private int
  146. win_open(gx_device *dev)
  147. {    HDC hdc;
  148.     int i;
  149.     int depth;
  150.  
  151.     win_makeimg(wdev);
  152.     ShowWindow(wdev->hwndimg, SW_SHOWMINIMIZED);
  153.     BringWindowToTop(hwndeasy);
  154.  
  155.     hdc = GetDC(wdev->hwndimg);
  156.  
  157.     /* Set parameters that were unknown before opening device */
  158.     if (dev->width == INITIAL_WIDTH)
  159.       dev->width  = (int)(8.5  * dev->x_pixels_per_inch);
  160.     if (dev->height == INITIAL_HEIGHT)
  161.       dev->height = (int)(11.0 * dev->y_pixels_per_inch);
  162.  
  163.     /* Find out if the device supports color */
  164.     /* We recognize 2, 16 or 256 color devices */
  165.     depth = GetDeviceCaps(hdc,PLANES) * GetDeviceCaps(hdc,BITSPIXEL);
  166.     if ( depth >= 8 ) { /* use 64 static colors and 166 dynamic colors from 8 planes */
  167.         static const gx_device_color_info win_256color = dci_color(8,31,4);
  168.         dev->color_info = win_256color;
  169.         wdev->nColors = 64;
  170.     }
  171.     else if ( depth >= 4 ) {
  172.         static const gx_device_color_info win_16color = dci_color(4, 2, 3);
  173.         dev->color_info = win_16color;
  174.         wdev->nColors = 16;
  175.     } 
  176.     else {   /* default is black_and_white */
  177.         wdev->nColors = 2;
  178.     }
  179.     
  180.     wdev->hbitmap = CreateCompatibleBitmap(hdc,dev->width,dev->height);
  181.     for (i=0; (wdev->hbitmap==(HBITMAP)NULL) && (i<4); i++) {
  182.         fprintf(stderr,"\nNot enough memory for bitmap.  Halving resolution... ");
  183.         dev->x_pixels_per_inch = dev->x_pixels_per_inch/2;
  184.         dev->y_pixels_per_inch = dev->y_pixels_per_inch/2;
  185.         dev->width  = dev->width/2;
  186.         dev->height = dev->height/2;
  187.         wdev->hbitmap = CreateCompatibleBitmap(hdc,dev->width,dev->height);
  188.     }
  189.     if (wdev->hbitmap==(HBITMAP)NULL) {
  190.         ReleaseDC(wdev->hwndimg, hdc);
  191.         win_nomemory();
  192.     }
  193.  
  194.     /* Create the bitmap and DC for copy_mono. */
  195.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  196.     wdev->hdcmono = CreateCompatibleDC(hdc);
  197.     if ( wdev->hbmmono == NULL || wdev->hdcmono == NULL )
  198.     {    DeleteObject(wdev->hbitmap);
  199.         ReleaseDC(wdev->hwndimg, hdc);
  200.         win_nomemory();
  201.     }
  202.     SetMapMode(wdev->hdcmono, GetMapMode(hdc));
  203.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  204.     wdev->bm_id = gx_no_bitmap_id;
  205.        
  206.     wdev->hdcbit = CreateCompatibleDC(hdc);  /* create Device Context for drawing */
  207.     SelectObject(wdev->hdcbit,wdev->hbitmap);
  208.     ReleaseDC(wdev->hwndimg, hdc);
  209.  
  210.     /* create palette and tools for bitmap */
  211.     if ((wdev->lpalette = win_makepalette(wdev))
  212.         == (LPLOGPALETTE)NULL)
  213.         win_nomemory();
  214.     wdev->hpalette = CreatePalette(wdev->lpalette);
  215.     (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  216.     RealizePalette(wdev->hdcbit);
  217.     win_maketools(wdev,wdev->hdcbit);
  218.  
  219.     /* create palette for display */
  220.     if ((wdev->limgpalette = win_makepalette(wdev))
  221.         == (LPLOGPALETTE)NULL)
  222.         win_nomemory();
  223.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  224.  
  225.     /* Initialize the scrolling information. */
  226.     
  227.     wdev->cxClient = wdev->cyClient = 0;
  228.     wdev->nVscrollPos = wdev->nVscrollMax = 0;
  229.     wdev->nHscrollPos = wdev->nHscrollMax = 0;
  230.  
  231.     /* Link this device onto the list */
  232.     wdev->next_wdev = (gx_device_win *)gdev_win_open_list;
  233.     gdev_win_open_list = dev;    
  234.  
  235.     return 0;
  236. }
  237.  
  238. /* Make the output appear on the screen. */
  239. private int
  240. win_sync_output(gx_device *dev)
  241. {
  242.     RECT rect;
  243.     if ( !IsWindow(wdev->hwndimg) ) {  /* some clod closed the window */
  244.         win_makeimg(wdev);
  245.         ShowWindow(wdev->hwndimg, SW_SHOWMINIMIZED);
  246.     }
  247.     if ( !IsIconic(wdev->hwndimg) ) {  /* redraw window */
  248.         GetClientRect(wdev->hwndimg, &rect);
  249.         InvalidateRect(wdev->hwndimg, (LPRECT) &rect, 1);
  250.         UpdateWindow(wdev->hwndimg);
  251.     }
  252.     return(0);
  253. }
  254.  
  255. /* Make the window visible, and display the output. */
  256. private int
  257. win_output_page(gx_device *dev, int copies, int flush)
  258. {
  259.     if (IsIconic(wdev->hwndimg))    /* useless as an Icon so fix it */
  260.         ShowWindow(wdev->hwndimg, SW_SHOWNORMAL);
  261.  
  262.     BringWindowToTop(wdev->hwndimg);
  263.     return( win_sync_output(dev) );
  264. }
  265.  
  266. /* Close the win driver */
  267. private int
  268. win_close(gx_device *dev)
  269. {
  270.     /* Free resources */
  271.  
  272.     win_destroytools(wdev);
  273.     DeleteDC(wdev->hdcbit);
  274.     DeleteDC(wdev->hdcmono);
  275.     DeleteObject(wdev->hbitmap);
  276.     DeleteObject(wdev->hpalette);
  277.     DeleteObject(wdev->himgpalette);
  278.     DeleteObject(wdev->hbmmono);
  279.     free(wdev->lpalette);
  280.     free(wdev->limgpalette);
  281.     DestroyWindow(wdev->hwndimg);
  282.     gp_check_interrupts();    /* process WIN_DESTROY message */
  283.  
  284.     /* Unlink from list. We do this last so that the WndProc */
  285.     /* can process the destroy message. */
  286.  
  287.     {    gx_device_win **pwd = (gx_device_win **)&gdev_win_open_list;
  288.         while ( *pwd != 0 && *pwd != wdev )
  289.             pwd = &(*pwd)->next_wdev;
  290.         if ( pwd != 0 )        /* == 0 should never happen! */
  291.             *pwd = (*pwd)->next_wdev;
  292.     }
  293.  
  294.     return(0);
  295. }
  296.  
  297. /* Map a r-g-b color to the colors available under Windows */
  298. #define win_map32(z)\
  299.   ((((z) >> (gx_color_value_bits - 5)) << 3) +\
  300.    ((z) >> (gx_color_value_bits - 3)))
  301. private gx_color_index
  302. win_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  303.   gx_color_value b)
  304. {
  305.     switch(dev->color_info.depth) {
  306.       case 8: {
  307.         int i;
  308.         LPLOGPALETTE lpal = wdev->lpalette;
  309.         PALETTEENTRY *pep;
  310.         byte cr, cg, cb;
  311.  
  312.         /* map colors to 0->255 in 32 steps */
  313.         cr = win_map32(r);
  314.         cg = win_map32(g);
  315.         cb = win_map32(b);
  316.  
  317.         /* search in palette */
  318.         for ( i = 0, pep = &lpal->palPalEntry[i];
  319.               i < wdev->nColors; i++, pep++
  320.             )
  321.         {    if ((cr == pep->peRed) &&
  322.                 (cg == pep->peGreen) &&
  323.                 (cb == pep->peBlue))
  324.                 return((gx_color_index)i);    /* found it */
  325.         }
  326.         
  327.         /* next try adding it to palette */
  328.         if (i < 220) { /* allow 36 for windows and other apps */
  329.             LPLOGPALETTE lipal = wdev->limgpalette;
  330.             wdev->nColors = i+1;
  331.             DeleteObject(wdev->hpalette);
  332.               lpal->palPalEntry[i].peFlags = NULL;
  333.             lpal->palPalEntry[i].peRed   =  cr;
  334.             lpal->palPalEntry[i].peGreen =  cg;
  335.             lpal->palPalEntry[i].peBlue  =  cb;
  336.             lpal->palNumEntries = wdev->nColors;
  337.             wdev->hpalette = CreatePalette(lpal);
  338.             (void) SelectPalette(wdev->hdcbit,wdev->hpalette,NULL);
  339.             RealizePalette(wdev->hdcbit);
  340.             win_addtool(wdev, i);
  341.  
  342.             DeleteObject(wdev->himgpalette);
  343.              lipal->palPalEntry[i].peFlags = NULL;
  344.             lipal->palPalEntry[i].peRed   =  cr;
  345.             lipal->palPalEntry[i].peGreen =  cg;
  346.             lipal->palPalEntry[i].peBlue  =  cb;
  347.             lipal->palNumEntries = wdev->nColors;
  348.             wdev->himgpalette = CreatePalette(lipal);
  349.  
  350.             return((gx_color_index)i);    /* return new palette index */
  351.         }
  352.  
  353.         return(-1);    /* not found - dither instead */
  354.         }
  355.       case 4: {
  356.         int color =  ((r > gx_max_color_value / 4 ? 4 : 0) +
  357.              (g > gx_max_color_value / 4 ? 2 : 0) +
  358.              (b > gx_max_color_value / 4 ? 1 : 0) +
  359.               (r > gx_max_color_value / 4 * 3 ||
  360.               g > gx_max_color_value / 4 * 3 ? 8 : 0));
  361.         return((gx_color_index)color);
  362.         }
  363.     }
  364.     return (gx_default_map_rgb_color(dev,r,g,b));
  365. }
  366.  
  367. /* Map a color code to r-g-b. */
  368. private int
  369. win_map_color_rgb(gx_device *dev, gx_color_index color,
  370.   gx_color_value prgb[3])
  371. {
  372. gx_color_value one;
  373.     switch(dev->color_info.depth) {
  374.       case 8:
  375.         one = (gx_color_value) (gx_max_color_value / 255);
  376.         prgb[0] = wdev->lpalette->palPalEntry[(int)color].peRed * one;
  377.         prgb[1] = wdev->lpalette->palPalEntry[(int)color].peGreen * one;
  378.         prgb[2] = wdev->lpalette->palPalEntry[(int)color].peBlue * one;
  379.         break;
  380.       case 4:
  381.         one = ((int)color & 8 ? gx_max_color_value : gx_max_color_value / 2);
  382.         prgb[0] = ((int)color & 4 ? one : 0);
  383.         prgb[1] = ((int)color & 2 ? one : 0);
  384.         prgb[2] = ((int)color & 1 ? one : 0);
  385.         break;
  386.       default:
  387.         prgb[0] = prgb[1] = prgb[2] = 
  388.             (int)color ? gx_max_color_value : 0;
  389.     }
  390.     return 0;
  391. }
  392.  
  393.  
  394. /* Macro for filling a rectangle with a color. */
  395. /* Note that it starts with a declaration. */
  396. #define fill_rect(x, y, w, h, color)\
  397. RECT rect;\
  398. rect.left = x, rect.top = y;\
  399. rect.right = x + w, rect.bottom = y + h;\
  400. FillRect(wdev->hdcbit, &rect, wdev->hbrushs[(int)color])
  401.  
  402.  
  403. /* Fill a rectangle. */
  404. private int
  405. win_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  406.   gx_color_index color)
  407. {
  408.     fit_fill(dev, x, y, w, h);
  409.     {    fill_rect(x, y, w, h, color);
  410.     }
  411.     win_update(wdev, h);
  412.     
  413.     return 0;
  414. }
  415.  
  416. /* Tile a rectangle.  If neither color is transparent, */
  417. /* pre-clear the rectangle to color0 and just tile with color1. */
  418. /* This is faster because of how win_copy_mono is implemented. */
  419. /* Note that this also does the right thing for colored tiles. */
  420. private int
  421. win_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  422.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  423.   int px, int py)
  424. {    fit_fill(dev, x, y, w, h);
  425.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  426.        {    fill_rect(x, y, w, h, czero);
  427.         czero = gx_no_color_index;
  428.        }
  429.     if ( tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  430.          (px | py) == 0 && cone != gx_no_color_index
  431.        )
  432.     {    /* We can do this much more efficiently */
  433.         /* by using the internal algorithms of copy_mono */
  434.         /* and gx_default_tile_rectangle. */
  435.         int width = tile->size.x;
  436.         int height = tile->size.y;
  437.         int rwidth = tile->rep_width;
  438.         int irx = ((rwidth & (rwidth - 1)) == 0 ? /* power of 2 */
  439.             x & (rwidth - 1) :
  440.             x % rwidth);
  441.         int ry = y % tile->rep_height;
  442.         int icw = width - irx;
  443.         int ch = height - ry;
  444.         int ex = x + w, ey = y + h;
  445.         int fex = ex - width, fey = ey - height;
  446.         int cx, cy;
  447.  
  448.         if (wdev->hbrush != wdev->hbrushs[(int)cone])
  449.         {    wdev->hbrush = wdev->hbrushs[(int)cone];
  450.             SelectObject(wdev->hdcbit,wdev->hbrush);
  451.         }
  452.  
  453.         if ( tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id )
  454.         {    wdev->bm_id = tile->id;
  455.             SetBitmapBits(wdev->hbmmono,
  456.                       (DWORD)(bmWidthBytes * tile->size.y),
  457.                       (BYTE *)tile->data);
  458.         }
  459.  
  460. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  461.   BitBlt(wdev->hdcbit, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  462.  
  463.         if ( ch > h ) ch = h;
  464.         for ( cy = y; ; )
  465.            {    if ( w <= icw )
  466.                 copy_tile(irx, ry, x, cy, w, ch);
  467.             else
  468.             {    copy_tile(irx, ry, x, cy, icw, ch);
  469.                 cx = x + icw;
  470.                 while ( cx <= fex )
  471.                 {    copy_tile(0, ry, cx, cy, width, ch);
  472.                     cx += width;
  473.                 }
  474.                 if ( cx < ex )
  475.                 {    copy_tile(0, ry, cx, cy, ex - cx, ch);
  476.                 }
  477.             }
  478.             if ( (cy += ch) >= ey ) break;
  479.             ch = (cy > fey ? ey - cy : height);
  480.             ry = 0;
  481.            }
  482.  
  483.         win_update(wdev, h);
  484.         return 0;
  485.     }
  486.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  487. }
  488.  
  489.  
  490. /* Draw a line */
  491. private int
  492. win_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  493.   gx_color_index color)
  494. {
  495.     if (wdev->hpen != wdev->hpens[(int)color]) {
  496.         wdev->hpen = wdev->hpens[(int)color];
  497.         SelectObject(wdev->hdcbit,wdev->hpen);
  498.     }
  499.     MoveTo(wdev->hdcbit, x0, y0);
  500.     LineTo(wdev->hdcbit, x1, y1);
  501.     return 0;
  502. }
  503.  
  504. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  505. /* Color = gx_no_color_index means transparent (no effect on the image). */
  506. private int
  507. win_copy_mono(gx_device *dev,
  508.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  509.   int x, int y, int w, int h,
  510.   gx_color_index zero, gx_color_index one)
  511. {    int endx;
  512.     const byte *ptr_line;
  513.     int width_bytes, height;
  514.     DWORD rop = rop_write_at_1s;
  515.     int color;
  516.     BYTE aBit[bmWidthBytes * bmHeight];
  517.     BYTE *aptr = aBit;
  518.  
  519.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  520.  
  521.     if ( sourcex & ~7 )
  522.     {    base += sourcex >> 3;
  523.         sourcex &= 7;
  524.     }
  525.  
  526.     /* Break up large transfers into smaller ones. */
  527.     while ( (endx = sourcex + w) > bmWidthBits )
  528.     {    int lastx = (endx - 1) & -bmWidthBits;
  529.         int subw = endx - lastx;
  530.         int code = win_copy_mono(dev, base, lastx,
  531.                      raster, gx_no_bitmap_id,
  532.                      x + lastx - sourcex, y,
  533.                      subw, h, zero, one);
  534.         if ( code < 0 ) return code;
  535.         w -= subw;
  536.     }
  537.     while ( h > bmHeight )
  538.     {    int code;
  539.         h -= bmHeight;
  540.         code = win_copy_mono(dev, base + h * raster, sourcex, raster,
  541.                      gx_no_bitmap_id, x, y + h, w, bmHeight,
  542.                      zero, one);
  543.         if ( code < 0 ) return code;
  544.     }
  545.  
  546.     if ( w <= 0 || h <= 0 ) return 0;
  547.     width_bytes = (sourcex + w + 7) >> 3;
  548.     ptr_line = base;
  549.  
  550.     if ( zero == gx_no_color_index )
  551.        {    if ( one == gx_no_color_index ) return 0;
  552.         color = (int)one;
  553.        }
  554.     else
  555.        {    if ( one == gx_no_color_index )
  556.            {    color = (int)zero;
  557.             rop = rop_write_at_0s;
  558.            }
  559.         else
  560.            {    /* Pre-clear the rectangle to zero */
  561.             fill_rect(x, y, w, h, zero);
  562.             color = (int)one;
  563.            }
  564.        }
  565.  
  566.     if (wdev->hbrush != wdev->hbrushs[(int)color])
  567.     {    wdev->hbrush = wdev->hbrushs[(int)color];
  568.         SelectObject(wdev->hdcbit,wdev->hbrush);
  569.     }
  570.  
  571.     if ( id != wdev->bm_id || id == gx_no_bitmap_id )
  572.     {    wdev->bm_id = id;
  573.         if ( raster == bmWidthBytes )
  574.         {    /* We can do the whole thing in a single transfer! */
  575.             SetBitmapBits(wdev->hbmmono,
  576.                       (DWORD)(bmWidthBytes * h),
  577.                       (BYTE *)base);
  578.         }
  579.         else
  580.         {    for ( height = h; height--;
  581.                   ptr_line += raster, aptr += bmWidthBytes
  582.                 )
  583.             {    /* Pack the bits into the bitmap. */
  584.                 switch ( width_bytes )
  585.                 {
  586.                     default: memcpy(aptr, ptr_line, width_bytes); break;
  587.                     case 4: aptr[3] = ptr_line[3];
  588.                     case 3: aptr[2] = ptr_line[2];
  589.                     case 2: aptr[1] = ptr_line[1];
  590.                     case 1: aptr[0] = ptr_line[0];
  591.                 }
  592.             }
  593.             SetBitmapBits(wdev->hbmmono,
  594.                       (DWORD)(bmWidthBytes * h),
  595.                       &aBit[0]);
  596.         }
  597.     }
  598.  
  599.     BitBlt(wdev->hdcbit, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  600.     win_update(wdev, h);    /* message handler */
  601.     return 0;
  602. }
  603.  
  604.  
  605. /* Copy a color pixel map.  This is just like a bitmap, except that */
  606. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  607. private int
  608. win_copy_color(gx_device *dev,
  609.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  610.   int x, int y, int w, int h)
  611. {
  612.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  613.  
  614.     if ( gx_device_has_color(dev) )
  615.     {
  616.     switch(dev->color_info.depth) {
  617.       case 8:
  618.         {    int xi, yi;
  619.         int skip = raster - w;
  620.         const byte *sptr = base + sourcex;
  621.           if ( w <= 0 ) return 0;
  622.           if ( x < 0 || x + w > dev->width )
  623.             return_error(gs_error_rangecheck);
  624.         for ( yi = y; yi - y < h; yi++ )
  625.            {
  626.             for ( xi = x; xi - x < w; xi++ )
  627.                {    int color =  *sptr++;
  628.                 SetPixel(wdev->hdcbit,xi,yi,PALETTEINDEX(color));
  629.                }
  630.             sptr += skip;
  631.            }
  632.         }
  633.         break;
  634.       case 4:
  635.        {    /* color device, four bits per pixel */
  636.         const byte *line = base + (sourcex >> 1);
  637.         int dest_y = y, end_x = x + w;
  638.  
  639.         if ( w <= 0 ) return 0;
  640.         while ( h-- )              /* for each line */
  641.            {    const byte *source = line;
  642.             register int dest_x = x;
  643.             if ( sourcex & 1 )    /* odd nibble first */
  644.                {    int color =  *source++ & 0xf;
  645.                 SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  646.                 dest_x++;
  647.                }
  648.             /* Now do full bytes */
  649.             while ( dest_x < end_x )
  650.                {    int color = *source >> 4;
  651.                 SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  652.                 dest_x++;
  653.                 if ( dest_x < end_x )
  654.                    {    color =  *source++ & 0xf;
  655.                     SetPixel(wdev->hdcbit,dest_x,dest_y,PALETTEINDEX(color));
  656.                     dest_x++;
  657.                    }
  658.                }
  659.             dest_y++;
  660.             line += raster;
  661.            }
  662.        }
  663.        break;
  664.     default:
  665.         return(-1); /* panic */
  666.     }
  667.     }
  668.     else 
  669.     /* monochrome device: one bit per pixel */
  670.        {    /* bitmap is the same as win_copy_mono: one bit per pixel */
  671.         win_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  672.             (gx_color_index)0, 
  673.             (gx_color_index)(dev->color_info.depth==8 ? 63 : dev->color_info.max_gray));
  674.        }
  675.     win_update(wdev, h);    /* message handler */
  676.     return 0;
  677. }
  678.  
  679. /* ------ Internal routines ------ */
  680.  
  681. #undef wdev
  682.  
  683. /* make image window */
  684. private void near 
  685. win_makeimg(gx_device_win *wdev)
  686. {    HMENU sysmenu;
  687.     wdev->hwndimg = CreateWindow(szAppName, (LPSTR)imgname,
  688.           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  689.           CW_USEDEFAULT, CW_USEDEFAULT, 
  690.           CW_USEDEFAULT, CW_USEDEFAULT, 
  691.           NULL, NULL, phInstance, (LPSTR)NULL);
  692.     /* modify the menu to have the new items we want */
  693.     sysmenu = GetSystemMenu(wdev->hwndimg,0);    /* get the sysmenu */
  694.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  695.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  696. }
  697.  
  698.  
  699. /* out of memory error exit with message box */
  700. private void near
  701. win_nomemory()
  702. {
  703.            MessageBox(hwndeasy,(LPSTR)"Not enough memory",(LPSTR) szAppName, MB_ICONSTOP);
  704.     gs_exit(1);
  705. }
  706.  
  707.  
  708. private LPLOGPALETTE near
  709. win_makepalette(gx_device_win *wdev)
  710. {    int i, val;
  711.     LPLOGPALETTE logpalette;
  712.     logpalette = (LPLOGPALETTE) malloc( sizeof(LOGPALETTE) + 
  713.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY) );
  714.     if (logpalette == (LPLOGPALETTE)NULL)
  715.         return(0);
  716.     logpalette->palVersion = 0x300;
  717.     logpalette->palNumEntries = wdev->nColors;
  718.     for (i=0; i<wdev->nColors; i++) {
  719.       logpalette->palPalEntry[i].peFlags = NULL;
  720.       switch (wdev->nColors) {
  721.         case 64:
  722.           /* colors are rrggbb */
  723.           logpalette->palPalEntry[i].peRed   =
  724.               win_map32(((i & 0x30)>>4)*(gx_max_color_value/3));
  725.           logpalette->palPalEntry[i].peGreen =
  726.               win_map32(((i & 0xC)>>2)*(gx_max_color_value/3));
  727.           logpalette->palPalEntry[i].peBlue  =
  728.               win_map32((i & 3)*(gx_max_color_value/3));
  729.           break;
  730.         case 16:
  731.           /* colors are irgb */
  732.           val = (i & 8 ? 255 : 255 / 2);
  733.           logpalette->palPalEntry[i].peRed   = i & 4 ? val : 0;
  734.           logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
  735.           logpalette->palPalEntry[i].peBlue  = i & 1 ? val : 0;
  736.           break;
  737.         case 2:
  738.           logpalette->palPalEntry[i].peRed =
  739.             logpalette->palPalEntry[i].peGreen =
  740.             logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
  741.           break;
  742.       }
  743.     }
  744.     return(logpalette);
  745. }
  746.  
  747.  
  748. private void near
  749. win_addtool(gx_device_win *wdev, int i)
  750. {
  751.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  752.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  753. }
  754.  
  755.  
  756. private void near
  757. win_maketools(gx_device_win *wdev, HDC hdc)
  758. {    int i;
  759.     wdev->hpens = malloc( (1<<(wdev->color_info.depth)) * sizeof(HPEN) );
  760.     wdev->hbrushs = malloc( (1<<(wdev->color_info.depth)) * sizeof(HBRUSH) );
  761.     if (wdev->hpens && wdev->hbrushs) {
  762.         for (i=0; i<wdev->nColors; i++)
  763.             win_addtool(wdev, i);
  764.  
  765.         wdev->hpen = wdev->hpens[0];
  766.         SelectObject(hdc,wdev->hpen);
  767.  
  768.         wdev->hbrush = wdev->hbrushs[0];
  769.         SelectObject(hdc,wdev->hbrush);
  770.     }
  771. }
  772.  
  773.  
  774. private void near
  775. win_destroytools(gx_device_win *wdev)
  776. {    int i;
  777.     for (i=0; i<wdev->nColors; i++) {
  778.         DeleteObject(wdev->hpens[i]);
  779.         DeleteObject(wdev->hbrushs[i]);
  780.     }
  781.     free(wdev->hpens);
  782.     free(wdev->hbrushs);
  783. }
  784.  
  785.  
  786. private void near
  787. win_update(gx_device_win *wdev, int amount)
  788. {    /* Update the image screen every 5 seconds, */
  789.     /* checking the time every N scan lines. */
  790.     static int fcount = 0;
  791.     static long time;
  792.     long date_time[2];
  793.     
  794.     if (fcount == 0)
  795.     {    gp_get_clock(date_time);
  796.         time = date_time[1];
  797.     }
  798.     if ((fcount += amount) > 200 || fcount < 0)
  799.     {    long deltatime;
  800.         gp_get_clock(date_time);
  801.         deltatime = date_time[1] - time;
  802.         if ( (deltatime > 5000L) || (deltatime < 0L) )
  803.         {    win_sync_output((gx_device *)wdev);    /* time to redraw */
  804.             time = date_time[1];
  805.         }
  806.         fcount = 1;
  807.     }
  808. }
  809.  
  810.  
  811. long far PASCAL 
  812. WndProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
  813. {    gx_device_win *wdev;
  814.     HDC hdc, mem;
  815.     PAINTSTRUCT ps;
  816.     RECT rect;
  817.     HBITMAP bitmap;
  818.     HPALETTE oldpalette;
  819.     int nVscrollInc, nHscrollInc;
  820.  
  821.     /* Search the list of active Windows devices */
  822.     /* to find the one owning this window. */
  823.     for ( wdev = (gx_device_win *)gdev_win_open_list;
  824.           wdev != 0; wdev = wdev->next_wdev
  825.         )
  826.       if ( wdev->hwndimg == hwnd ) break;
  827.     if ( wdev == 0 ) goto not_ours;        /* can this happen??? */
  828.  
  829.     switch(message) {
  830.         case WM_SYSCOMMAND:
  831.             switch(wParam) {
  832.                 case M_COPY_CLIP:
  833.                     /* make some where to put it and copy */
  834.                     bitmap = CreateCompatibleBitmap(wdev->hdcbit, wdev->width,
  835.                         wdev->height);
  836.                     if (bitmap) {
  837.                         /* there is enough memory and the bitmaps OK */
  838.                         mem = CreateCompatibleDC(wdev->hdcbit);
  839.                         SelectObject(mem, bitmap);
  840.                         BitBlt(mem,0,0,wdev->width,
  841.                             wdev->height, wdev->hdcbit, 0, 0, SRCCOPY);
  842.                         DeleteDC(mem);
  843.                     }
  844.                     else {
  845.                         puts("\nInsufficient Memory to Copy Clipboard");
  846.                         MessageBeep(0);
  847.                     }
  848.  
  849.                     OpenClipboard(hwnd);
  850.                     EmptyClipboard();
  851.                     SetClipboardData(CF_BITMAP, bitmap);
  852.                     CloseClipboard();
  853.  
  854.                     return 0;
  855.             }
  856.             break;
  857.         case WM_SIZE:
  858.             wdev->cyClient = HIWORD(lParam);
  859.             wdev->cxClient = LOWORD(lParam);
  860.  
  861.             wdev->cyAdjust = max(32,min(wdev->height, wdev->cyClient)) - wdev->cyClient;
  862.             wdev->cyClient += wdev->cyAdjust;
  863.  
  864.             wdev->nVscrollMax = max(0, wdev->height - wdev->cyClient);
  865.             wdev->nVscrollPos = min(wdev->nVscrollPos, wdev->nVscrollMax);
  866.  
  867.             SetScrollRange(hwnd, SB_VERT, 0, wdev->nVscrollMax, FALSE);
  868.             SetScrollPos(hwnd, SB_VERT, wdev->nVscrollPos, TRUE);
  869.  
  870.             wdev->cxAdjust = max(32,min(wdev->width,  wdev->cxClient)) - wdev->cxClient;
  871.             wdev->cxClient += wdev->cxAdjust;
  872.  
  873.             wdev->nHscrollMax = max(0, wdev->width - wdev->cxClient);
  874.             wdev->nHscrollPos = min(wdev->nHscrollPos, wdev->nHscrollMax);
  875.  
  876.             SetScrollRange(hwnd, SB_HORZ, 0, wdev->nHscrollMax, FALSE);
  877.             SetScrollPos(hwnd, SB_HORZ, wdev->nHscrollPos, TRUE);
  878.  
  879.             if ((wParam==SIZENORMAL) && (wdev->cxAdjust!=0 || wdev->cyAdjust!=0)) {
  880.                 GetWindowRect(hwnd,&rect);
  881.                 MoveWindow(hwnd,rect.left,rect.top,
  882.                 rect.right-rect.left+wdev->cxAdjust,
  883.                 rect.bottom-rect.top+wdev->cyAdjust, TRUE);
  884.                 wdev->cxAdjust = wdev->cyAdjust = 0;
  885.             }
  886.             return(0);
  887.         case WM_VSCROLL:
  888.             switch(wParam) {
  889.                 case SB_TOP:
  890.                     nVscrollInc = -wdev->nVscrollPos;
  891.                     break;
  892.                 case SB_BOTTOM:
  893.                     nVscrollInc = wdev->nVscrollMax - wdev->nVscrollPos;
  894.                     break;
  895.                 case SB_LINEUP:
  896.                     nVscrollInc = -wdev->cyClient/16;
  897.                     break;
  898.                 case SB_LINEDOWN:
  899.                     nVscrollInc = wdev->cyClient/16;
  900.                     break;
  901.                 case SB_PAGEUP:
  902.                     nVscrollInc = min(-1,-wdev->cyClient);
  903.                     break;
  904.                 case SB_PAGEDOWN:
  905.                     nVscrollInc = max(1,wdev->cyClient);
  906.                     break;
  907.                 case SB_THUMBPOSITION:
  908.                     nVscrollInc = LOWORD(lParam) - wdev->nVscrollPos;
  909.                     break;
  910.                 default:
  911.                     nVscrollInc = 0;
  912.                 }
  913.             if (nVscrollInc = max(-wdev->nVscrollPos, 
  914.                 min(nVscrollInc, wdev->nVscrollMax - wdev->nVscrollPos))) {
  915.                 wdev->nVscrollPos += nVscrollInc;
  916.                 ScrollWindow(hwnd,0,-nVscrollInc,NULL,NULL);
  917.                 SetScrollPos(hwnd,SB_VERT,wdev->nVscrollPos,TRUE);
  918.                 UpdateWindow(hwnd);
  919.             }
  920.             return(0);
  921.         case WM_HSCROLL:
  922.             switch(wParam) {
  923.                 case SB_LINEUP:
  924.                     nHscrollInc = -wdev->cxClient/16;
  925.                     break;
  926.                 case SB_LINEDOWN:
  927.                     nHscrollInc = wdev->cyClient/16;
  928.                     break;
  929.                 case SB_PAGEUP:
  930.                     nHscrollInc = min(-1,-wdev->cxClient);
  931.                     break;
  932.                 case SB_PAGEDOWN:
  933.                     nHscrollInc = max(1,wdev->cxClient);
  934.                     break;
  935.                 case SB_THUMBPOSITION:
  936.                     nHscrollInc = LOWORD(lParam) - wdev->nHscrollPos;
  937.                     break;
  938.                 default:
  939.                     nHscrollInc = 0;
  940.                 }
  941.             if (nHscrollInc = max(-wdev->nHscrollPos, 
  942.                 min(nHscrollInc, wdev->nHscrollMax - wdev->nHscrollPos))) {
  943.                 wdev->nHscrollPos += nHscrollInc;
  944.                 ScrollWindow(hwnd,-nHscrollInc,0,NULL,NULL);
  945.                 SetScrollPos(hwnd,SB_HORZ,wdev->nHscrollPos,TRUE);
  946.                 UpdateWindow(hwnd);
  947.             }
  948.             return(0);
  949.         case WM_KEYDOWN:
  950.             switch(wParam) {
  951.                 case VK_HOME:
  952.                     SendMessage(hwnd,WM_VSCROLL,SB_TOP,0L);
  953.                     break;
  954.                 case VK_END:
  955.                     SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0L);
  956.                     break;
  957.                 case VK_PRIOR:
  958.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEUP,0L);
  959.                     break;
  960.                 case VK_NEXT:
  961.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEDOWN,0L);
  962.                     break;
  963.                 case VK_UP:
  964.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEUP,0L);
  965.                     break;
  966.                 case VK_DOWN:
  967.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0L);
  968.                     break;
  969.                 case VK_LEFT:
  970.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEUP,0L);
  971.                     break;
  972.                 case VK_RIGHT:
  973.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEDOWN,0L);
  974.                     break;
  975.             }
  976.             return(0);
  977.         case WM_PAINT:
  978.             {
  979.             int sx,sy,wx,wy,dx,dy;
  980.             hdc = BeginPaint(hwnd, &ps);
  981.             oldpalette = SelectPalette(hdc,wdev->himgpalette,NULL);
  982.             RealizePalette(hdc);
  983.             SetMapMode(hdc, MM_TEXT);
  984.             SetBkMode(hdc,OPAQUE);
  985.             GetClientRect(hwnd, &rect);
  986.             SetViewportExt(hdc, rect.right, rect.bottom);
  987.             dx = rect.left;    /* destination */
  988.             dy = rect.top;
  989.             wx = rect.right-rect.left; /* width */
  990.             wy = rect.bottom-rect.top;
  991.             sx = rect.left;    /* source */
  992.             sy = rect.top;
  993.             sx += wdev->nHscrollPos; /* scrollbars */
  994.             sy += wdev->nVscrollPos;    
  995.             if (sx+wx > wdev->width)
  996.                 wx = wdev->width - sx;
  997.             if (sy+wy > wdev->height)
  998.                 wy = wdev->height - sy;
  999.             BitBlt(hdc,dx,dy,wx,wy,wdev->hdcbit,sx,sy,SRCCOPY);
  1000.             SelectPalette(hdc,oldpalette,NULL);
  1001.             EndPaint(hwnd, &ps);
  1002.             return 0;
  1003.             }
  1004.     }
  1005.  
  1006. not_ours:
  1007.     return DefWindowProc((HWND)hwnd, (WORD)message, (WORD)wParam, (DWORD)lParam);
  1008. }
  1009.