home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVMSWN.C < prev    next >
C/C++ Source or Header  |  1994-07-31  |  26KB  |  896 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript 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 Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevmswn.c */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  25.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  26.  */
  27. #include "gdevmswn.h"
  28. #include "gp.h"
  29. #include "gpcheck.h"
  30. #include "gsparam.h"
  31. #include "gdevpccm.h"
  32. #ifdef __DLL__
  33. #include "gsdll.h"
  34. #endif
  35.  
  36. /* Forward references */
  37. private void near win_makeimg(P1(gx_device_win *));
  38. private int win_set_bits_per_pixel(P2(gx_device_win *, int));
  39. LRESULT CALLBACK _export WndImgProc(HWND, UINT, WPARAM, LPARAM);
  40. LRESULT CALLBACK _export WndImgChildProc(HWND, UINT, WPARAM, LPARAM);
  41.  
  42. #define TIMER_ID 1
  43.  
  44. /* Open the win driver */
  45. int
  46. win_open(gx_device *dev)
  47. {    HDC hdc;
  48.     WNDCLASS wndclass;
  49.     static BOOL registered = FALSE;
  50.  
  51.     /* Initialize the scrolling information. */
  52.     
  53.     wdev->cxClient = wdev->cyClient = 0;
  54.     wdev->nVscrollPos = wdev->nVscrollMax = 0;
  55.     wdev->nHscrollPos = wdev->nHscrollMax = 0;
  56.  
  57.     wdev->update = wdev->timer = FALSE;
  58.  
  59.     if (dev->width == INITIAL_WIDTH)
  60.       dev->width  = (int)(8.5  * dev->x_pixels_per_inch);
  61.     if (dev->height == INITIAL_HEIGHT)
  62.       dev->height = (int)(11.0 * dev->y_pixels_per_inch);
  63.  
  64.     if (!wdev->dll) {
  65.       /* If this is the first instance, register the window classes. */
  66.       if (!registered) {
  67.         /* register the window class for graphics */
  68.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  69.         wndclass.lpfnWndProc = WndImgChildProc;
  70.         wndclass.cbClsExtra = 0;
  71.         wndclass.cbWndExtra = sizeof(LONG);
  72.         wndclass.hInstance = phInstance;
  73.         wndclass.hIcon = LoadIcon(phInstance,(LPSTR)MAKEINTRESOURCE(IMAGE_ICON));
  74.         wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  75.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  76.         wndclass.lpszMenuName = NULL;
  77.         wndclass.lpszClassName = szAppName;
  78.         RegisterClass(&wndclass);
  79.  
  80.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  81.         wndclass.lpfnWndProc = WndImgProc;
  82.         wndclass.cbClsExtra = 0;
  83.         wndclass.cbWndExtra = sizeof(LONG);
  84.         wndclass.hInstance = phInstance;
  85.         wndclass.hIcon = LoadIcon(phInstance,(LPSTR)MAKEINTRESOURCE(IMAGE_ICON));
  86.         wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  87.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  88.         wndclass.lpszMenuName = NULL;
  89.         wndclass.lpszClassName = szImgName;
  90.         RegisterClass(&wndclass);
  91.     
  92.         registered = TRUE;
  93.       }
  94.  
  95.       win_makeimg(wdev);
  96.     } /* !wdev->dll */
  97.  
  98.     wdev->hdctext = NULL;
  99.  
  100.     if (wdev->BitsPerPixel == 0) {
  101.         /* Set parameters that were unknown before opening device */
  102.         /* Find out if the device supports color */
  103.         /* We recognize 1, 4, 8, 15, 16 and 24 bit/pixel devices */
  104.         hdc = GetDC(NULL);    /* get hdc for desktop */
  105.         wdev->BitsPerPixel = GetDeviceCaps(hdc,PLANES) * GetDeviceCaps(hdc,BITSPIXEL);
  106.         ReleaseDC(NULL,hdc);
  107.     }
  108.  
  109.     if (win_set_bits_per_pixel(wdev, wdev->BitsPerPixel) < 0)
  110.         return gs_error_limitcheck;
  111.  
  112.     if (wdev->nColors > 0) {
  113.         /* create palette for display */
  114.         if ((wdev->limgpalette = win_makepalette(wdev))
  115.         == (LPLOGPALETTE)NULL)
  116.         return win_nomemory();
  117.         wdev->himgpalette = CreatePalette(wdev->limgpalette);
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
  123. /* Make the output appear on the screen. */
  124. int
  125. win_sync_output(gx_device *dev)
  126. {
  127.     if (wdev->timer)
  128.     KillTimer(wdev->hwndimgchild, TIMER_ID);
  129.     wdev->timer = FALSE;
  130.     wdev->update = FALSE;
  131. #ifdef __DLL__
  132.     if (wdev->dll) {
  133.     (*pgsdll_callback)(GSDLL_SYNC, (unsigned char *)wdev, 0);
  134.     return 0;
  135.     }
  136. #endif
  137.  
  138.     if (gsview) {
  139.     SendMessage(gsview_hwnd, WM_GSVIEW, SYNC_OUTPUT, (LPARAM)NULL);
  140.     }
  141.     else {
  142.     if ( !IsWindow(wdev->hwndimg) ) {  /* some clod closed the window */
  143.         win_makeimg(wdev);
  144.     }
  145.     if ( !IsIconic(wdev->hwndimg) ) {  /* redraw window */
  146.         InvalidateRect(wdev->hwndimg, NULL, 1);
  147.         UpdateWindow(wdev->hwndimg);
  148.     }
  149.     }
  150.     return(0);
  151. }
  152.  
  153. /* Make the window visible, and display the output. */
  154. int
  155. win_output_page(gx_device *dev, int copies, int flush)
  156. {
  157.     if (wdev->timer)
  158.         KillTimer(wdev->hwndimgchild, TIMER_ID);
  159.     wdev->timer = FALSE;
  160.     wdev->update = FALSE;
  161.  
  162. #ifdef __DLL__
  163.         if (wdev->dll) {
  164.         (*pgsdll_callback)(GSDLL_PAGE, (unsigned char *)wdev, 0);
  165.         return 0;
  166.         }
  167. #endif
  168.  
  169.     if (gsview) {
  170.         if (copies == -2) {
  171.             SendMessage(gsview_hwnd, WM_GSVIEW, END, (LPARAM)NULL);
  172.         }
  173.         else if (copies == -1) {
  174.             SendMessage(gsview_hwnd, WM_GSVIEW, BEGIN, (LPARAM)NULL);
  175.         }
  176.         else {
  177.             SendMessage(gsview_hwnd, WM_GSVIEW, OUTPUT_PAGE, (LPARAM)NULL);
  178.             gsview_next = FALSE;
  179.             /* wait for NEXT_PAGE message */
  180.             while (!gsview_next)
  181.             (void)gp_check_interrupts();
  182.         }
  183.         return(0);
  184.     }
  185.     else {
  186.         if (IsIconic(wdev->hwndimg))    /* useless as an Icon so fix it */
  187.         ShowWindow(wdev->hwndimg, SW_SHOWNORMAL);
  188.         BringWindowToTop(wdev->hwndimg);
  189.     }
  190.     return( win_sync_output(dev) );
  191. }
  192.  
  193. /* Close the win driver */
  194. int
  195. win_close(gx_device *dev)
  196. {
  197.     /* Free resources */
  198.     if (wdev->timer)
  199.         KillTimer(wdev->hwndimgchild, TIMER_ID);
  200.     wdev->timer = FALSE;
  201.     wdev->update = FALSE;
  202.  
  203.     if (wdev->nColors > 0) {
  204.         DeleteObject(wdev->himgpalette);
  205.         gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  206.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  207.         "win_close");
  208.     }
  209.  
  210.     if (!wdev->dll) {
  211.         if (gsview)
  212.         DestroyWindow(wdev->hwndimgchild);
  213.         else
  214.         DestroyWindow(wdev->hwndimg);
  215.         (void)gp_check_interrupts();    /* process WIN_DESTROY message */
  216.     }
  217.  
  218.     return(0);
  219. }
  220.  
  221. /* Map a r-g-b color to the colors available under Windows */
  222. gx_color_index
  223. win_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  224.   gx_color_value b)
  225. {
  226.     if (!dev->is_open)
  227.         return 0;
  228.     switch(wdev->BitsPerPixel) {
  229.       case 24:
  230.         return (((unsigned long)b >> (gx_color_value_bits - 8)) << 16) +
  231.                (((unsigned long)g >> (gx_color_value_bits - 8)) << 8) +
  232.                (((unsigned long)r >> (gx_color_value_bits - 8)));
  233.       case 16:
  234.       case 15:
  235.         return ((unsigned long)win_color_value(b) << 16) +
  236.                ((unsigned long)win_color_value(g) << 8) +
  237.                ((unsigned long)win_color_value(r));
  238.       case 8: {
  239.         int i;
  240.         LPLOGPALETTE lpal = wdev->limgpalette;
  241.         PALETTEENTRY *pep;
  242.         byte cr, cg, cb;
  243.  
  244.         /* map colors to 0->255 in 32 steps */
  245.         cr = win_color_value(r);
  246.         cg = win_color_value(g);
  247.         cb = win_color_value(b);
  248.  
  249.         /* search in palette */
  250.         for ( i = 0, pep = &lpal->palPalEntry[i];
  251.               i < wdev->nColors; i++, pep++
  252.             )
  253.         {    if ( !((cr ^ pep->peRed) & 0xf8) &&
  254.                  !((cg ^ pep->peGreen) & 0xf8) &&
  255.                  !((cb ^ pep->peBlue) & 0xf8)
  256.                )
  257.                 return((gx_color_index)i);    /* found it */
  258.         }
  259.         
  260.         /* next try adding it to palette */
  261.         if (i < 220) { /* allow 36 for windows and other apps */
  262.             LPLOGPALETTE lipal = wdev->limgpalette;
  263.             wdev->nColors = i+1;
  264.  
  265.             DeleteObject(wdev->himgpalette);
  266.              lipal->palPalEntry[i].peFlags = 0;
  267.             lipal->palPalEntry[i].peRed   =  cr;
  268.             lipal->palPalEntry[i].peGreen =  cg;
  269.             lipal->palPalEntry[i].peBlue  =  cb;
  270.             lipal->palNumEntries = wdev->nColors;
  271.             wdev->himgpalette = CreatePalette(lipal);
  272.  
  273.             return((gx_color_index)i);    /* return new palette index */
  274.         }
  275.  
  276.         return(gx_no_color_index);  /* not found - dither instead */
  277.         }
  278.       case 4:
  279.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  280.            && (r < gx_max_color_value / 4 * 3))
  281.             return ((gx_color_index)8);    /* light gray */
  282.         return pc_4bit_map_rgb_color(dev, r, g, b);
  283.     }
  284.     return (gx_default_map_rgb_color(dev,r,g,b));
  285. }
  286.  
  287. /* Map a color code to r-g-b. */
  288. int
  289. win_map_color_rgb(gx_device *dev, gx_color_index color,
  290.   gx_color_value prgb[3])
  291. {    gx_color_value one;
  292.     if (!dev->is_open)
  293.         return 0;
  294.     switch(wdev->BitsPerPixel) {
  295.       case 24:
  296.       case 16:
  297.       case 15:
  298.         one = (gx_color_value) (gx_max_color_value / 255);
  299.         prgb[0] = ((color)     & 255) * one;
  300.         prgb[1] = ((color>>8)  & 255) * one;
  301.         prgb[2] = ((color>>16) & 255) * one;
  302.         break;
  303.       case 8:
  304.         one = (gx_color_value) (gx_max_color_value / 255);
  305.         prgb[0] = wdev->limgpalette->palPalEntry[(int)color].peRed * one;
  306.         prgb[1] = wdev->limgpalette->palPalEntry[(int)color].peGreen * one;
  307.         prgb[2] = wdev->limgpalette->palPalEntry[(int)color].peBlue * one;
  308.         break;
  309.       case 4:
  310.         if (color == 8)    /* VGA light gray */
  311.             prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  312.         else
  313.             pc_4bit_map_color_rgb(dev, color, prgb);
  314.         break;
  315.       default:
  316.         prgb[0] = prgb[1] = prgb[2] = 
  317.             (int)color ? gx_max_color_value : 0;
  318.     }
  319.     return 0;
  320. }
  321.  
  322. /* Get Win parameters */
  323. int
  324. win_get_params(gx_device *dev, gs_param_list *plist)
  325. {    int code = gx_default_get_params(dev, plist);
  326.     gs_param_string gvs;
  327.     gvs.data = wdev->GSVIEW_STR, gvs.size = strlen(gvs.data),
  328.       gvs.persistent = false;
  329.     code < 0 ||
  330.     (code = param_write_int(plist, "BitsPerPixel", &wdev->BitsPerPixel)) < 0 ||
  331.     (code = param_write_int(plist, "UpdateInterval", &wdev->UpdateInterval)) < 0 ||
  332.     (code = param_write_string(plist, "GSVIEW", &gvs)) < 0;
  333.     return code;
  334. }
  335.  
  336. /* Set window parameters -- size and resolution. */
  337. /* We implement this ourselves so that we can do it without */
  338. /* closing and opening the device. */
  339. int
  340. win_put_params(gx_device *dev, gs_param_list *plist)
  341. {    gx_device_win temp_dev;
  342.     int ecode, rcode = 0, code;
  343.     int v;
  344.     gs_param_string s;
  345.     temp_dev = *((gx_device_win *)dev);
  346.     ecode = gx_default_put_params_only((gx_device *)&temp_dev, plist);
  347.  
  348.     /* Handle extra parameters */
  349.     switch ( code = param_read_string(plist, "GSVIEW", &s) )
  350.     {
  351.     default:
  352.         ecode = code;
  353.     case 1:
  354.         break;
  355.     case 0:
  356.         if ( dev->is_open )
  357.             ecode = gs_error_rangecheck;
  358.         else if ( s.size >= win_gsview_sizeof )
  359.             ecode = gs_error_limitcheck;
  360.         else
  361.           {    memcpy(temp_dev.GSVIEW_STR, s.data, s.size);
  362.             temp_dev.GSVIEW_STR[s.size] = 0;
  363.             break;
  364.           }
  365.         param_signal_error(plist, "GSVIEW", ecode);
  366.     }
  367.  
  368.     switch ( code = param_read_int(plist, "UpdateInterval", &v) )
  369.     {
  370.     default:
  371.         ecode = code;
  372.     case 1:
  373.         break;
  374.     case 0:
  375.         if ( v < 0 )
  376.             param_signal_error(plist, "UpdateInterval",
  377.                        ecode = gs_error_rangecheck);
  378.         else
  379.             temp_dev.UpdateInterval = v;
  380.     }
  381.  
  382.     switch ( code = param_read_int(plist, "BitsPerPixel", &v) )
  383.     {
  384.     default:
  385.         ecode = code;
  386.     case 1:
  387.         break;
  388.     case 0:
  389.         if ( dev->is_open )
  390.             ecode = gs_error_rangecheck;
  391.         else if ((v!=1) && (v!=4) && (v!=8) && (v!=15) && (v!=16) && (v!=24))
  392.             ecode = gs_error_rangecheck;
  393.         else 
  394.           {    code = win_set_bits_per_pixel(&temp_dev, v);
  395.             if ( code < 0 )
  396.                 ecode = code;
  397.             else
  398.               {    rcode = 1;
  399.                 break;
  400.               }
  401.           }
  402.         param_signal_error(plist, "BitsPerPixel", ecode);
  403.     }
  404.  
  405.     if ( ecode < 0 )
  406.       return ecode;
  407.  
  408.     /* Hand off the change to the implementation. */
  409.     if ( dev->is_open && rcode )
  410.     {    int ccode;
  411.         (*wdev->free_bitmap)(wdev);
  412.         ccode = (*wdev->alloc_bitmap)(wdev, (gx_device *)&temp_dev);
  413.         if ( ccode < 0 )
  414.         {    (*wdev->alloc_bitmap)(wdev, dev);
  415.             return ccode;
  416.         }
  417.     }
  418.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  419.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  420.     dev->width = temp_dev.width;
  421.     dev->height = temp_dev.height;
  422.     dev->color_info = temp_dev.color_info;
  423.     wdev->BitsPerPixel = temp_dev.BitsPerPixel;
  424.     wdev->UpdateInterval = temp_dev.UpdateInterval;
  425.     strcpy(wdev->GSVIEW_STR, temp_dev.GSVIEW_STR);
  426.     if (IsWindow(wdev->hwndimg) && IsWindow(wdev->hwndimgchild)) {
  427.         RECT rect;
  428.         /* erase bitmap - before window gets redrawn */
  429.         (*dev_proc(dev, fill_rectangle))(dev, 0, 0,
  430.         dev->width, dev->height,
  431.         win_map_rgb_color(dev, gx_max_color_value, 
  432.             gx_max_color_value, gx_max_color_value));
  433.         /* cause scroll bars to be redrawn */
  434.         GetClientRect(wdev->hwndimgchild,&rect);
  435.         SendMessage(wdev->hwndimgchild, WM_SIZE, SIZE_RESTORED, 
  436.         MAKELPARAM(rect.right-rect.left, rect.bottom-rect.top));
  437.     }
  438. #ifdef __DLL__
  439.     if (wdev->dll)
  440.         (*pgsdll_callback)(GSDLL_SIZE, (unsigned char *)dev,
  441.             (dev->width & 0xffff) + ( (dev->height & 0xffff)<<16) );
  442. #endif
  443.     return 0;
  444. }
  445.  
  446.  
  447. /* ------ Internal routines ------ */
  448.  
  449. #undef wdev
  450.  
  451. /* make image window */
  452. private void near 
  453. win_makeimg(gx_device_win *wdev)
  454. {    HMENU sysmenu;
  455.     RECT rect;
  456.     /* parent window */
  457.     if (gsview) {
  458.         wdev->hwndimg = gsview_hwnd;
  459.         if (IsIconic(wdev->hwndimg))
  460.         ShowWindow(wdev->hwndimg, SW_SHOWNORMAL);
  461.     }
  462.     else {
  463.         wdev->hwndimg = CreateWindow(szImgName, (LPSTR)szImgName,
  464.           WS_OVERLAPPEDWINDOW,
  465.           CW_USEDEFAULT, CW_USEDEFAULT, 
  466.           CW_USEDEFAULT, CW_USEDEFAULT, 
  467.           NULL, NULL, phInstance, (void FAR *)wdev);
  468.         ShowWindow(wdev->hwndimg, SW_SHOWMINNOACTIVE);
  469.     }
  470.     /* child for graphics */
  471.     GetClientRect(wdev->hwndimg, &rect);
  472.     CreateWindow(szAppName, (LPSTR)szImgName,
  473.           WS_CHILD | WS_VSCROLL | WS_HSCROLL,
  474.           rect.left, rect.top,
  475.           rect.right-rect.left, rect.bottom-rect.top, 
  476.           wdev->hwndimg, NULL, phInstance, (void FAR *)wdev);
  477.     ShowWindow(wdev->hwndimgchild, SW_SHOWNORMAL);
  478.  
  479.     if (!gsview) {
  480.         /* modify the menu to have the new items we want */
  481.         sysmenu = GetSystemMenu(wdev->hwndimg,0);    /* get the sysmenu */
  482.         AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  483.         AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  484.     }
  485. }
  486.  
  487.  
  488. /* out of memory error message box */
  489. int
  490. win_nomemory(void)
  491. {
  492.            MessageBox(hwndtext,(LPSTR)"Not enough memory",(LPSTR) szAppName, MB_ICONSTOP);
  493.     return gs_error_limitcheck;
  494. }
  495.  
  496.  
  497. LPLOGPALETTE
  498. win_makepalette(gx_device_win *wdev)
  499. {    int i, val;
  500.     LPLOGPALETTE logpalette;
  501.     logpalette = (LPLOGPALETTE)gs_malloc(1, sizeof(LOGPALETTE) + 
  502.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  503.         "win_makepalette");
  504.     if (logpalette == (LPLOGPALETTE)NULL)
  505.         return(0);
  506.     logpalette->palVersion = 0x300;
  507.     logpalette->palNumEntries = wdev->nColors;
  508.     for (i=0; i<wdev->nColors; i++) {
  509.       logpalette->palPalEntry[i].peFlags = 0;
  510.       switch (wdev->nColors) {
  511.         case 64:
  512.           /* colors are rrggbb */
  513.           logpalette->palPalEntry[i].peRed   = ((i & 0x30)>>4)*85;
  514.           logpalette->palPalEntry[i].peGreen = ((i & 0xC)>>2)*85;
  515.           logpalette->palPalEntry[i].peBlue  = (i & 3)*85;
  516.           break;
  517.         case 16:
  518.           /* colors are irgb */
  519.           val = (i & 8 ? 255 : 128);
  520.           logpalette->palPalEntry[i].peRed   = i & 4 ? val : 0;
  521.           logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
  522.           logpalette->palPalEntry[i].peBlue  = i & 1 ? val : 0;
  523.           if (i == 8) {    /* light gray */
  524.               logpalette->palPalEntry[i].peRed   = 
  525.               logpalette->palPalEntry[i].peGreen = 
  526.               logpalette->palPalEntry[i].peBlue  = 192;
  527.           }
  528.           break;
  529.         case 2:
  530.           logpalette->palPalEntry[i].peRed =
  531.             logpalette->palPalEntry[i].peGreen =
  532.             logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
  533.           break;
  534.       }
  535.     }
  536.     return(logpalette);
  537. }
  538.  
  539.  
  540. void
  541. win_update(gx_device_win *wdev)
  542. {
  543. #ifdef __DLL__
  544.     if (wdev->dll)
  545.         return;    /* can't use timer since don't have window */
  546. #endif
  547.     if (!wdev->UpdateInterval)
  548.         return;
  549.     wdev->update = TRUE;
  550.     if (!wdev->timer) {
  551.         SetTimer(wdev->hwndimgchild, TIMER_ID, wdev->UpdateInterval, NULL);
  552.         wdev->timer = TRUE;
  553.       }
  554. }
  555.   
  556. private int
  557. win_set_bits_per_pixel(gx_device_win *wdev, int bpp)
  558. {
  559. static const gx_device_color_info win_24bit_color = dci_color(24,255,255);
  560. static const gx_device_color_info win_16bit_color = dci_color(24,31,31);
  561. static const gx_device_color_info win_8bit_color = dci_color(8,31,4);
  562. static const gx_device_color_info win_ega_color = dci_pc_4bit;
  563. static const gx_device_color_info win_vga_color = dci_pc_4bit;
  564. static const gx_device_color_info win_mono_color = dci_black_and_white;
  565. HDC hdc;
  566.     switch(bpp) {
  567.         case 24:
  568.         wdev->color_info = win_24bit_color;
  569.         wdev->nColors = -1;
  570.         break;
  571.         case 16:
  572.         case 15:
  573.         /* bitmap is stored as 24bits/pixel */
  574.         wdev->color_info = win_16bit_color;
  575.         wdev->nColors = -1;
  576.         break;
  577.         case 8:
  578.         /* use 64 static colors and 166 dynamic colors from 8 planes */
  579.         wdev->color_info = win_8bit_color;
  580.         wdev->nColors = 64;
  581.         break;
  582.         case 4:
  583.         hdc = GetDC(NULL);
  584.         if (GetDeviceCaps(hdc, VERTRES) <= 350)
  585.             wdev->color_info = win_ega_color;
  586.         else
  587.             wdev->color_info = win_vga_color;
  588.         ReleaseDC(NULL,hdc);
  589.         wdev->nColors = 16;
  590.         break;
  591.         case 1:
  592.         wdev->color_info = win_mono_color;
  593.         wdev->nColors = 2;
  594.         break;
  595.         default:
  596.         return (gs_error_rangecheck);
  597.     }
  598.     wdev->BitsPerPixel = bpp;
  599.     return 0;
  600. }
  601.  
  602.  
  603. /* child graphics window */
  604. LRESULT CALLBACK _export
  605. WndImgChildProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  606. {    gx_device_win *wdev;
  607.     HDC hdc;
  608.     PAINTSTRUCT ps;
  609.     RECT rect;
  610.     HPALETTE oldpalette;
  611.     int nVscrollInc, nHscrollInc;
  612.  
  613.     wdev = (gx_device_win *)GetWindowLong(hwnd, 0);
  614.  
  615.     switch(message) {
  616.         case WM_CREATE:
  617.             wdev =  (gx_device_win *)(((CREATESTRUCT *)lParam)->lpCreateParams);
  618.             SetWindowLong(hwnd, 0, (LONG)wdev);
  619.             wdev->hwndimgchild = hwnd;
  620.             if (gsview)
  621.                 SendMessage(gsview_hwnd, WM_GSVIEW, HWND_IMGCHILD, (LPARAM)wdev->hwndimgchild);
  622. #if WINVER >= 0x030a
  623.             /* Enable Drag Drop */
  624.             if (is_win31)
  625.                 DragAcceptFiles(hwnd, TRUE);
  626. #endif
  627.             break;
  628.         case WM_GSVIEW:
  629.             switch(wParam) {
  630.                 case NEXT_PAGE:
  631.                     gsview_next = TRUE;
  632.                     return 0;
  633.                 case COPY_CLIPBOARD:
  634.                     (*wdev->copy_to_clipboard)(wdev);
  635.                     return 0;
  636.             }
  637.             break;
  638.         case WM_TIMER:
  639.             if ((wParam == TIMER_ID) && wdev->update) {
  640.                 InvalidateRect(wdev->hwndimgchild, (LPRECT)NULL, FALSE);
  641.                 UpdateWindow(wdev->hwndimgchild);
  642.                 wdev->update = FALSE;
  643.             }
  644.             break;
  645.         case WM_SIZE:
  646.             if (wParam == SIZE_MINIMIZED)
  647.                 return(0);
  648.             wdev->cyClient = HIWORD(lParam);
  649.             wdev->cxClient = LOWORD(lParam);
  650.  
  651.             wdev->cyAdjust = min(wdev->height, wdev->cyClient) - wdev->cyClient;
  652.             wdev->cyClient += wdev->cyAdjust;
  653.  
  654.             wdev->nVscrollMax = max(0, wdev->height - wdev->cyClient);
  655.             wdev->nVscrollPos = min(wdev->nVscrollPos, wdev->nVscrollMax);
  656.  
  657.             SetScrollRange(hwnd, SB_VERT, 0, wdev->nVscrollMax, FALSE);
  658.             SetScrollPos(hwnd, SB_VERT, wdev->nVscrollPos, TRUE);
  659.  
  660.             wdev->cxAdjust = min(wdev->width,  wdev->cxClient) - wdev->cxClient;
  661.             wdev->cxClient += wdev->cxAdjust;
  662.  
  663.             wdev->nHscrollMax = max(0, wdev->width - wdev->cxClient);
  664.             wdev->nHscrollPos = min(wdev->nHscrollPos, wdev->nHscrollMax);
  665.  
  666.             SetScrollRange(hwnd, SB_HORZ, 0, wdev->nHscrollMax, FALSE);
  667.             SetScrollPos(hwnd, SB_HORZ, wdev->nHscrollPos, TRUE);
  668.  
  669.             if (gsview && wdev->cxAdjust > 0)    /* don't make gsview window grow */
  670.                 wdev->cxAdjust = 0;
  671.             if (gsview && wdev->cyAdjust > 0)
  672.                 wdev->cyAdjust = 0;
  673.  
  674.             if ((wParam==SIZENORMAL) && (wdev->cxAdjust!=0 || wdev->cyAdjust!=0)) {
  675.                 GetWindowRect(GetParent(hwnd),&rect);
  676.                 MoveWindow(GetParent(hwnd),rect.left,rect.top,
  677.                 rect.right-rect.left+wdev->cxAdjust,
  678.                 rect.bottom-rect.top+wdev->cyAdjust, TRUE);
  679.                 wdev->cxAdjust = wdev->cyAdjust = 0;
  680.             }
  681.             if (gsview)
  682.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  683.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  684.             return(0);
  685.         case WM_VSCROLL:
  686.             switch(LOWORD(wParam)) {
  687.                 case SB_TOP:
  688.                     nVscrollInc = -wdev->nVscrollPos;
  689.                     break;
  690.                 case SB_BOTTOM:
  691.                     nVscrollInc = wdev->nVscrollMax - wdev->nVscrollPos;
  692.                     break;
  693.                 case SB_LINEUP:
  694.                     nVscrollInc = -wdev->cyClient/16;
  695.                     break;
  696.                 case SB_LINEDOWN:
  697.                     nVscrollInc = wdev->cyClient/16;
  698.                     break;
  699.                 case SB_PAGEUP:
  700.                     nVscrollInc = min(-1,-wdev->cyClient);
  701.                     break;
  702.                 case SB_PAGEDOWN:
  703.                     nVscrollInc = max(1,wdev->cyClient);
  704.                     break;
  705.                 case SB_THUMBPOSITION:
  706. #ifdef __WIN32__
  707.                     nVscrollInc = HIWORD(wParam) - wdev->nVscrollPos;
  708. #else
  709.                     nVscrollInc = LOWORD(lParam) - wdev->nVscrollPos;
  710. #endif
  711.                     break;
  712.                 default:
  713.                     nVscrollInc = 0;
  714.                 }
  715.             if ((nVscrollInc = max(-wdev->nVscrollPos, 
  716.                 min(nVscrollInc, wdev->nVscrollMax - wdev->nVscrollPos)))!=0) {
  717.                 wdev->nVscrollPos += nVscrollInc;
  718.                 ScrollWindow(hwnd,0,-nVscrollInc,NULL,NULL);
  719.                 SetScrollPos(hwnd,SB_VERT,wdev->nVscrollPos,TRUE);
  720.                 UpdateWindow(hwnd);
  721.             }
  722.             if (gsview)
  723.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  724.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  725.             return(0);
  726.         case WM_HSCROLL:
  727.             switch(LOWORD(wParam)) {
  728.                 case SB_LINEUP:
  729.                     nHscrollInc = -wdev->cxClient/16;
  730.                     break;
  731.                 case SB_LINEDOWN:
  732.                     nHscrollInc = wdev->cyClient/16;
  733.                     break;
  734.                 case SB_PAGEUP:
  735.                     nHscrollInc = min(-1,-wdev->cxClient);
  736.                     break;
  737.                 case SB_PAGEDOWN:
  738.                     nHscrollInc = max(1,wdev->cxClient);
  739.                     break;
  740.                 case SB_THUMBPOSITION:
  741. #ifdef __WIN32__
  742.                     nHscrollInc = HIWORD(wParam) - wdev->nHscrollPos;
  743. #else
  744.                     nHscrollInc = LOWORD(lParam) - wdev->nHscrollPos;
  745. #endif
  746.                     break;
  747.                 default:
  748.                     nHscrollInc = 0;
  749.                 }
  750.             if ((nHscrollInc = max(-wdev->nHscrollPos, 
  751.                 min(nHscrollInc, wdev->nHscrollMax - wdev->nHscrollPos)))!=0) {
  752.                 wdev->nHscrollPos += nHscrollInc;
  753.                 ScrollWindow(hwnd,-nHscrollInc,0,NULL,NULL);
  754.                 SetScrollPos(hwnd,SB_HORZ,wdev->nHscrollPos,TRUE);
  755.                 UpdateWindow(hwnd);
  756.             }
  757.             if (gsview)
  758.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  759.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  760.             return(0);
  761.         case WM_KEYDOWN:
  762.             switch(LOWORD(wParam)) {
  763.                 case VK_HOME:
  764.                     SendMessage(hwnd,WM_VSCROLL,SB_TOP,0L);
  765.                     break;
  766.                 case VK_END:
  767.                     SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0L);
  768.                     break;
  769.                 case VK_PRIOR:
  770.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEUP,0L);
  771.                     break;
  772.                 case VK_NEXT:
  773.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEDOWN,0L);
  774.                     break;
  775.                 case VK_UP:
  776.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEUP,0L);
  777.                     break;
  778.                 case VK_DOWN:
  779.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0L);
  780.                     break;
  781.                 case VK_LEFT:
  782.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEUP,0L);
  783.                     break;
  784.                 case VK_RIGHT:
  785.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEDOWN,0L);
  786.                     break;
  787.                 case VK_RETURN:
  788.                     BringWindowToTop(hwndtext);
  789.                     break;
  790.             }
  791.             return(0);
  792.         case WM_PAINT:
  793.             {
  794.             int sx,sy,wx,wy,dx,dy;
  795.             hdc = BeginPaint(hwnd, &ps);
  796.             if (wdev->nColors > 0) {
  797.                 oldpalette = SelectPalette(hdc,wdev->himgpalette,NULL);
  798.                 RealizePalette(hdc);
  799.             }
  800.             SetMapMode(hdc, MM_TEXT);
  801.             SetBkMode(hdc,OPAQUE);
  802.             GetClientRect(hwnd, &rect);
  803. #ifdef __WIN32__
  804.             SetViewportExtEx(hdc, rect.right, rect.bottom, NULL);
  805. #else
  806.             SetViewportExt(hdc, rect.right, rect.bottom);
  807. #endif
  808.             dx = rect.left;    /* destination */
  809.             dy = rect.top;
  810.             wx = rect.right-rect.left; /* width */
  811.             wy = rect.bottom-rect.top;
  812.             sx = rect.left;    /* source */
  813.             sy = rect.top;
  814.             sx += wdev->nHscrollPos; /* scrollbars */
  815.             sy += wdev->nVscrollPos;    
  816.             if (sx+wx > wdev->width)
  817.                 wx = wdev->width - sx;
  818.             if (sy+wy > wdev->height)
  819.                 wy = wdev->height - sy;
  820.             (*wdev->repaint)(wdev,hdc,dx,dy,wx,wy,sx,sy);
  821.             if (wdev->nColors > 0) {
  822.                 SelectPalette(hdc,oldpalette,NULL);
  823.             }
  824.             EndPaint(hwnd, &ps);
  825.             return 0;
  826.             }
  827. #if WINVER >= 0x030a
  828.         case WM_DROPFILES:
  829.             if (is_win31)
  830.                 SendMessage(hwndtext, message, wParam, lParam);
  831.         case WM_DESTROY:
  832.             /* disable Drag Drop */
  833.             if (is_win31)
  834.                 DragAcceptFiles(hwnd, FALSE);
  835.             break;
  836. #endif
  837.     }
  838.  
  839. not_ours:
  840.     return DefWindowProc((HWND)hwnd, (WORD)message, (WORD)wParam, (DWORD)lParam);
  841. }
  842.  
  843. /* parent overlapped window */
  844. LRESULT CALLBACK _export
  845. WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  846. {
  847.     gx_device_win *wdev;
  848.  
  849.     wdev = (gx_device_win *)GetWindowLong(hwnd, 0);
  850.  
  851.     switch(message) {
  852.         case WM_CREATE:
  853.             wdev =  (gx_device_win *)(((CREATESTRUCT *)lParam)->lpCreateParams);
  854.             wdev->hwndimg = hwnd;
  855.             SetWindowLong(hwnd, 0, (LONG)wdev);
  856. #if WINVER >= 0x030a
  857.             /* Enable Drag Drop */
  858.             if (is_win31)
  859.                 DragAcceptFiles(hwnd, TRUE);
  860. #endif
  861.             break;
  862.         case WM_SYSCOMMAND:
  863.             switch(LOWORD(wParam)) {
  864.                 case M_COPY_CLIP:
  865.                     (*wdev->copy_to_clipboard)(wdev);
  866.                     return 0;
  867.             }
  868.             break;
  869.         case WM_KEYDOWN:
  870.         case WM_KEYUP:
  871.             if (wdev->hwndimgchild) {
  872.                 SendMessage(wdev->hwndimgchild, message, wParam, lParam);
  873.                 return 0;
  874.             }
  875.             break;
  876.         case WM_SIZE:
  877.             if (wParam != SIZE_MINIMIZED  && wdev->hwndimgchild!=(HWND)NULL)
  878.                 SetWindowPos(wdev->hwndimgchild, (HWND)NULL, 0, 0,
  879.                 LOWORD(lParam), HIWORD(lParam), 
  880.                 SWP_NOZORDER | SWP_NOACTIVATE);
  881.             return(0);
  882. #if WINVER >= 0x030a
  883.         case WM_DROPFILES:
  884.             if (is_win31)
  885.                 SendMessage(hwndtext, message, wParam, lParam);
  886.             break;
  887.         case WM_DESTROY:
  888.             if (is_win31)
  889.                 DragAcceptFiles(hwnd, FALSE);
  890. #endif
  891.             break;
  892.     }
  893.     return DefWindowProc(hwnd, message, wParam, lParam);
  894. }
  895.  
  896.