home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / GrayPixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-01  |  3.5 KB  |  112 lines

  1. /* static char Xrcsid[] = "$XConsortium: GrayPixmap.c,v 1.6 90/12/01 12:58:06 rws Exp $"; */
  2.  
  3.  
  4. /***********************************************************
  5. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  6. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  7.  
  8.                         All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the names of Digital or MIT not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25.  
  26. ******************************************************************/
  27.  
  28. #include <stdio.h>
  29. #include <X11/IntrinsicP.h>
  30.  
  31. typedef struct _PixmapCache {
  32.     Screen *screen;
  33.     Pixmap pixmap;
  34.     Pixel foreground, background;
  35.     unsigned int depth;
  36.     int ref_count;
  37.     struct _PixmapCache *next;
  38.   } CacheEntry;
  39.  
  40. static CacheEntry *pixmapCache = NULL;
  41.  
  42.  
  43.  
  44. Pixmap XmuCreateStippledPixmap(screen, fore, back, depth)
  45.     Screen *screen;
  46.     Pixel fore, back;
  47.     unsigned int depth;
  48. /*
  49.  *    Creates a stippled pixmap of specified depth
  50.  *    caches these so that multiple requests share the pixmap
  51.  */
  52. {
  53.     register Display *display = DisplayOfScreen(screen);
  54.     CacheEntry *cachePtr;
  55.     Pixmap stippled_pixmap;
  56.     static unsigned char pixmap_bits[] = {
  57.     0x02, 0x01,
  58.     };
  59.  
  60. /*
  61.  *    Creates a stippled pixmap of depth DefaultDepth(screen)
  62.  *    caches these so that multiple requests share the pixmap
  63.  */
  64.  
  65. #define pixmap_width 2
  66. #define pixmap_height 2
  67.  
  68.     /* see if we already have a pixmap suitable for this screen */
  69.     for (cachePtr = pixmapCache; cachePtr; cachePtr = cachePtr->next) {
  70.     if (cachePtr->screen == screen && cachePtr->foreground == fore &&
  71.         cachePtr->background == back && cachePtr->depth == depth)
  72.         return( cachePtr->ref_count++, cachePtr->pixmap );
  73.     }
  74.  
  75.     stippled_pixmap = XCreatePixmapFromBitmapData (display,
  76.             RootWindowOfScreen(screen), (char *)pixmap_bits, 
  77.             pixmap_width, pixmap_height, fore, back, depth);
  78.  
  79.     /* and insert it at the head of the cache */
  80.     cachePtr = XtNew(CacheEntry);
  81.     cachePtr->screen = screen;
  82.     cachePtr->foreground = fore;
  83.     cachePtr->background = back;
  84.     cachePtr->depth = depth;
  85.     cachePtr->pixmap = stippled_pixmap;
  86.     cachePtr->ref_count = 1;
  87.     cachePtr->next = pixmapCache;
  88.     pixmapCache = cachePtr;
  89.  
  90.     return( stippled_pixmap );
  91. }
  92.  
  93. void XmuReleaseStippledPixmap(screen, pixmap)
  94.     Screen *screen;
  95.     Pixmap pixmap;
  96. {
  97.     register Display *display = DisplayOfScreen(screen);
  98.     CacheEntry *cachePtr, **prevP;
  99.     for (prevP = &pixmapCache, cachePtr = pixmapCache; cachePtr;) {
  100.     if (cachePtr->screen == screen && cachePtr->pixmap == pixmap) {
  101.         if (--cachePtr->ref_count == 0) {
  102.         XFreePixmap( display, pixmap );
  103.         *prevP = cachePtr->next;
  104.         XtFree( (char*)cachePtr );
  105.         break;
  106.         }
  107.     }
  108.     prevP = &cachePtr->next;
  109.     cachePtr = *prevP;
  110.     }
  111. }
  112.