home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tk8.0.5 / os2 / tkOS2Pixmap.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  9KB  |  304 lines

  1. /* 
  2.  * tkOS2Pixmap.c --
  3.  *
  4.  *    This file contains the Xlib emulation functions pertaining to
  5.  *    creating and destroying pixmaps.
  6.  *
  7.  * Copyright (c) 1996-2000 Illya Vaes
  8.  * Copyright (c) 1995 Sun Microsystems, Inc.
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  */
  13.  
  14.  
  15. #include "tkOS2Int.h"
  16.  
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * Tk_GetPixmap --
  22.  *
  23.  *    Creates an in memory drawing surface.
  24.  *
  25.  * Results:
  26.  *    Returns a handle to a new pixmap.
  27.  *
  28.  * Side effects:
  29.  *    Allocates a new OS/2 bitmap, hps, DC.
  30.  *
  31.  *----------------------------------------------------------------------
  32.  */
  33.  
  34. Pixmap
  35. Tk_GetPixmap(display, d, width, height, depth)
  36.     Display* display;
  37.     Drawable d;
  38.     int width;
  39.     int height;
  40.     int depth;
  41. {
  42.     TkOS2Drawable *newTodPtr, *todPtr;
  43.     int planes;
  44.     Screen *screen;
  45.     BITMAPINFOHEADER2 bmpInfo;
  46.     LONG rc;
  47.     DEVOPENSTRUC dop = {0L, (PSZ)"DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
  48.     SIZEL sizl = {0,0}; /* use same page size as device */
  49.     sizl.cx = width; sizl.cy = height;
  50.     
  51.     display->request++;
  52.  
  53.     newTodPtr = (TkOS2Drawable*) ckalloc(sizeof(TkOS2Drawable));
  54.     if (newTodPtr == NULL) {
  55.     return (Pixmap)None;
  56.     }
  57. #ifdef VERBOSE
  58.     printf("    new todPtr (drawable) %x\n", todPtr);
  59. #endif
  60.  
  61.     newTodPtr->type = TOD_BITMAP;
  62.     newTodPtr->bitmap.depth = depth;
  63.     todPtr = (TkOS2Drawable *)d;
  64.     if (todPtr->type != TOD_BITMAP) {
  65. #ifdef VERBOSE
  66.         printf("XCreatePixmap %x, depth %d, parent %x, %dx%d\n", newTodPtr,
  67.                depth, todPtr->window.handle, sizl.cx, sizl.cy);
  68. #endif
  69.         newTodPtr->bitmap.parent = todPtr->window.handle;
  70.         if (todPtr->window.winPtr == NULL) {
  71.             newTodPtr->bitmap.colormap = DefaultColormap(display,
  72.                     DefaultScreen(display));
  73.         } else {
  74.             newTodPtr->bitmap.colormap = todPtr->window.winPtr->atts.colormap;
  75.         }
  76.     } else {
  77. #ifdef VERBOSE
  78.         printf("XCreatePixmap %x, depth %d, parent (bitmap) %x, %dx%d\n",
  79.                newTodPtr, depth, todPtr->bitmap.parent, sizl.cx, sizl.cy);
  80. #endif
  81.         newTodPtr->bitmap.colormap = todPtr->bitmap.colormap;
  82.         newTodPtr->bitmap.parent = todPtr->bitmap.parent;
  83.     }
  84.  
  85.     screen = &(display->screens[0]);
  86. #ifdef VERBOSE
  87.     printf("XCreatePixmap: colormap %x, parent %x, screen %x\n",
  88.            newTodPtr->bitmap.colormap, newTodPtr->bitmap.parent, screen);
  89. #endif
  90.     planes = 1;
  91.     if (depth == screen->root_depth) {
  92.         planes = (int) screen->ext_data;
  93.         depth /= planes;
  94.     }
  95.  
  96.     bmpInfo.cbFix = 16L;
  97.     bmpInfo.cx = width;
  98.     bmpInfo.cy = height;
  99.     bmpInfo.cPlanes = planes;
  100.     bmpInfo.cBitCount = depth;
  101.     newTodPtr->bitmap.dc = DevOpenDC(TclOS2GetHAB(), OD_MEMORY, (PSZ)"*", 5L,
  102.                                      (PDEVOPENDATA)&dop, NULLHANDLE);
  103.     if (newTodPtr->bitmap.dc == DEV_ERROR) {
  104. #ifdef VERBOSE
  105.         printf("DevOpenDC failed in XCreatePixmap\n");
  106. #endif
  107.         ckfree((char *) newTodPtr);
  108.         return (Pixmap)None;
  109.     }
  110. #ifdef VERBOSE
  111.     printf("DevOpenDC in XCreatePixmap returns %x\n", newTodPtr->bitmap.dc);
  112. #endif
  113.     newTodPtr->bitmap.hps = GpiCreatePS(TclOS2GetHAB(), newTodPtr->bitmap.dc,
  114.                                         &sizl,
  115.                                         PU_PELS | GPIT_MICRO | GPIA_ASSOC);
  116.     if (newTodPtr->bitmap.hps == GPI_ERROR) {
  117.         DevCloseDC(newTodPtr->bitmap.dc);
  118. #ifdef VERBOSE
  119.         printf("GpiCreatePS failed in XCreatePixmap\n");
  120. #endif
  121.         ckfree((char *) newTodPtr);
  122.         return (Pixmap)None;
  123.     }
  124. #ifdef VERBOSE
  125.     printf("GpiCreatePS in XCreatePixmap returns %x\n", newTodPtr->bitmap.hps);
  126. #endif
  127.     newTodPtr->bitmap.handle = GpiCreateBitmap(newTodPtr->bitmap.hps,
  128.                                                &bmpInfo, 0L, NULL, NULL);
  129.  
  130.     if (newTodPtr->bitmap.handle == NULLHANDLE) {
  131. #ifdef VERBOSE
  132.         printf("GpiCreateBitmap ERROR %x in XCreatePixmap\n",
  133.                WinGetLastError(TclOS2GetHAB()));
  134. #endif
  135.     ckfree((char *) newTodPtr);
  136.     return (Pixmap)None;
  137.     }
  138. #ifdef VERBOSE
  139.     printf("GpiCreateBitmap in XCreatePixmap returns %x\n",
  140.            newTodPtr->bitmap.handle);
  141. #endif
  142. /*
  143.     sizl.cx = width;
  144.     sizl.cy = height;
  145.     rc = GpiSetBitmapDimension(newTodPtr->bitmap.handle, &sizl);
  146. #ifdef VERBOSE
  147.     if (rc == FALSE) {
  148.         printf("    GpiSetBitmapDimension ERROR %x\n",
  149.                WinGetLastError(TclOS2GetHAB()));
  150.     } else {
  151.         printf("    GpiSetBitmapDimension: %dx%d\n", sizl.cx, sizl.cy);
  152.     }
  153.     rc = GpiQueryBitmapDimension(newTodPtr->bitmap.handle, &sizl);
  154.     if (rc == FALSE) {
  155.         printf("    GpiQueryBitmapDimension ERROR %x\n",
  156.                WinGetLastError(TclOS2GetHAB()));
  157.     } else {
  158.         printf("    GpiQueryBitmapDimension: %dx%d\n", sizl.cx, sizl.cy);
  159.     }
  160. #endif
  161. */
  162.     rc = GpiSetBitmap(newTodPtr->bitmap.hps, newTodPtr->bitmap.handle);
  163.     if (rc == HBM_ERROR) {
  164. #ifdef VERBOSE
  165.         printf("GpiSetBitmap returned HBM_ERROR %x\n",
  166.                WinGetLastError(TclOS2GetHAB()));
  167. #endif
  168.         GpiDestroyPS(newTodPtr->bitmap.hps);
  169.         DevCloseDC(newTodPtr->bitmap.dc);
  170.         ckfree((char *) newTodPtr);
  171.         return (Pixmap)None;
  172.     }
  173. #ifdef VERBOSE
  174.     else printf("GpiSetBitmap %x into hps %x returns %x\n",
  175.                 newTodPtr->bitmap.handle, newTodPtr->bitmap.hps, rc);
  176. #endif
  177.  
  178.     return (Pixmap)newTodPtr;
  179. }
  180.  
  181. /*
  182.  *----------------------------------------------------------------------
  183.  *
  184.  * Tk_FreePixmap --
  185.  *
  186.  *    Release the resources associated with a pixmap.
  187.  *
  188.  * Results:
  189.  *    None.
  190.  *
  191.  * Side effects:
  192.  *    Deletes the bitmap created by Tk_FreePixmap.
  193.  *
  194.  *----------------------------------------------------------------------
  195.  */
  196.  
  197. void
  198. Tk_FreePixmap(display, pixmap)
  199.     Display* display;
  200.     Pixmap pixmap;
  201. {
  202.     TkOS2Drawable *todPtr = (TkOS2Drawable *) pixmap;
  203.     HBITMAP hbm;
  204.  
  205. #ifdef VERBOSE
  206.     printf("XFreePixmap %x\n", todPtr);
  207. #endif
  208.  
  209.     display->request++;
  210.     if (todPtr != NULL) {
  211.         hbm = GpiSetBitmap(todPtr->bitmap.hps, NULLHANDLE);
  212. #ifdef VERBOSE
  213.         printf("    XFreePixmap GpiSetBitmap hps %x NULLHANDLE returned %x\n",
  214.                todPtr->bitmap.hps, hbm);
  215. #endif
  216.     GpiDeleteBitmap(todPtr->bitmap.handle);
  217.         GpiDestroyPS(todPtr->bitmap.hps);
  218.         DevCloseDC(todPtr->bitmap.dc);
  219.     ckfree((char *)todPtr);
  220.     }
  221. }
  222.  
  223. /*
  224.  *----------------------------------------------------------------------
  225.  *
  226.  * TkSetPixmapColormap --
  227.  *
  228.  *      The following function is a hack used by the photo widget to
  229.  *      explicitly set the colormap slot of a Pixmap.
  230.  *
  231.  * Results:
  232.  *      None.
  233.  *
  234.  * Side effects:
  235.  *      None.
  236.  *
  237.  *----------------------------------------------------------------------
  238.  */
  239.  
  240. void
  241. TkSetPixmapColormap(pixmap, colormap)
  242.     Pixmap pixmap;
  243.     Colormap colormap;
  244. {
  245.     TkOS2Drawable *todPtr = (TkOS2Drawable *)pixmap;
  246.  
  247. #ifdef VERBOSE
  248.     printf("TkSetPixmapColormap, bitmap %x, colormap %x\n",
  249.            TkOS2GetHBITMAP(todPtr), colormap);
  250. #endif
  251.     todPtr->bitmap.colormap = colormap;
  252.     /*
  253.     rc = (HPAL) TkOS2SelectPalette(todPtr->bitmap.hps, todPtr->bitmap.parent,
  254.                                    colormap);
  255.     */
  256. }
  257.  
  258. /*
  259.  *----------------------------------------------------------------------
  260.  *
  261.  * XGetGeometry --
  262.  *
  263.  *    Retrieve the geometry of the given drawable.  Note that
  264.  *    this is a degenerate implementation that only returns the
  265.  *    size of a pixmap.
  266.  *
  267.  * Results:
  268.  *    Returns 0.
  269.  *
  270.  * Side effects:
  271.  *    None.
  272.  *
  273.  *----------------------------------------------------------------------
  274.  */
  275.  
  276. int
  277. XGetGeometry(display, d, root_return, x_return, y_return, width_return,
  278.         height_return, border_width_return, depth_return)
  279.     Display* display;
  280.     Drawable d;
  281.     Window* root_return;
  282.     int* x_return;
  283.     int* y_return;
  284.     unsigned int* width_return;
  285.     unsigned int* height_return;
  286.     unsigned int* border_width_return;
  287.     unsigned int* depth_return;
  288. {
  289.     TkOS2Drawable *todPtr = (TkOS2Drawable *)d;
  290.     BITMAPINFOHEADER2 info;
  291.  
  292.     if ((todPtr->type != TOD_BITMAP) || (todPtr->bitmap.handle == NULLHANDLE)) {
  293.         panic("XGetGeometry: invalid pixmap");
  294.     }
  295.     info.cbFix = sizeof(BITMAPINFOHEADER2);
  296.     if (!GpiQueryBitmapInfoHeader(todPtr->bitmap.handle, &info)) {
  297.         panic("XGetGeometry: unable to get bitmap size");
  298.     }
  299.  
  300.     *width_return = info.cx;
  301.     *height_return = info.cy;
  302.     return 1;
  303. }
  304.