home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / PixmapUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.6 KB  |  383 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*-----------------------------------------*/
  19. /*                                                                        */
  20. /* Name:        <Xfe/PixmapUtil.c>                                        */
  21. /* Description:    Xfe widgets pixmap utilities.                            */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <Xfe/XfeP.h>
  28.  
  29. #define MESSAGE1  "The pixmap needs to have the same depth as the widget."
  30. #define MESSAGE2  "Cannot obtain geometry for pixmap."
  31.  
  32. /*----------------------------------------------------------------------*/
  33. /*                                                                        */
  34. /* Double buffer global variables                                        */
  35. /*                                                                        */
  36. /*----------------------------------------------------------------------*/
  37. static Pixmap        _pixmap_buffer = XmUNSPECIFIED_PIXMAP;
  38. static Cardinal        _pixmap_buffer_depth = 0;
  39. static Dimension    _pixmap_buffer_width = 0;
  40. static Dimension    _pixmap_buffer_height = 0;
  41. /*----------------------------------------------------------------------*/
  42.  
  43. static Pixmap    BufferCreate        (Widget,Dimension,Dimension);
  44. static void        BufferUpdate        (Widget,Pixmap,
  45.                                      Dimension,Dimension);
  46. static Boolean    BufferCheck            (Widget);
  47. static void        BufferFree            (Widget);
  48.  
  49. /*----------------------------------------------------------------------*/
  50. /*                                                                        */
  51. /* Double buffer utilities                                                */
  52. /*                                                                        */
  53. /*----------------------------------------------------------------------*/
  54. Pixmap
  55. _XfePixmapBufferAllocate(Widget w)
  56. {
  57.     Pixmap buffer = XmUNSPECIFIED_PIXMAP;
  58.  
  59.     assert( w != NULL );
  60.  
  61.     /* Check whether a buffer already exists */
  62.     if (_XfePixmapGood(_pixmap_buffer))
  63.     {
  64.         assert( _pixmap_buffer_depth == _XfeDepth(w));
  65.  
  66.         /* Check whether this widget can use the buffer */
  67.         if (BufferCheck(w))
  68.         {
  69.             buffer = _pixmap_buffer;
  70.         }
  71.         else
  72.         {
  73.             Dimension new_width = XfeMax(_XfeWidth(w),_pixmap_buffer_width);
  74.             Dimension new_height = XfeMax(_XfeHeight(w),_pixmap_buffer_height);
  75.  
  76.             BufferFree(w);
  77.  
  78.             buffer = BufferCreate(w,new_width,new_height);
  79.         
  80.             assert( _XfePixmapGood(buffer) );
  81.         
  82.             BufferUpdate(w,buffer,new_width,new_height);
  83.         }
  84.     }
  85.     /* Create a buffer pixmap for the first time */
  86.     else
  87.     {
  88.         buffer = BufferCreate(w,_XfeWidth(w),_XfeHeight(w));
  89.  
  90.         assert( _XfePixmapGood(buffer) );
  91.  
  92.         BufferUpdate(w,buffer,_XfeWidth(w),_XfeHeight(w));
  93.     }
  94.  
  95.     return buffer;
  96. }
  97. /*----------------------------------------------------------------------*/
  98. void
  99. _XfePixmapBufferRelease(Widget w,Pixmap buffer)
  100. {
  101. }
  102. /*----------------------------------------------------------------------*/
  103. Pixmap
  104. _XfePixmapBufferAccess()
  105. {
  106.     return _pixmap_buffer;
  107. }
  108. /*----------------------------------------------------------------------*/
  109. static void
  110. BufferFree(Widget w)
  111. {
  112.     assert( _XfePixmapGood(_pixmap_buffer) );
  113.  
  114.     /* Free the buffer pixmap */
  115.     XFreePixmap(XtDisplay(w),_pixmap_buffer);
  116. }
  117. /*----------------------------------------------------------------------*/
  118. static Pixmap
  119. BufferCreate(Widget w,Dimension width,Dimension height)
  120. {
  121.     assert( w != NULL );
  122.  
  123.     return XCreatePixmap(XtDisplay(w),
  124.                          DefaultRootWindow(XtDisplay(w)),
  125.                          width,
  126.                          height,
  127.                          _XfeDepth(w));
  128. }
  129. /*----------------------------------------------------------------------*/
  130. static void
  131. BufferUpdate(Widget w,Pixmap buffer,Dimension width,Dimension height)
  132. {
  133.     assert( _XfePixmapGood(buffer) );
  134.  
  135.     _pixmap_buffer      = buffer;
  136.     _pixmap_buffer_depth  = _XfeDepth(w);
  137.     _pixmap_buffer_width  = width;
  138.     _pixmap_buffer_height = height;
  139. }
  140. /*----------------------------------------------------------------------*/
  141. static Boolean
  142. BufferCheck(Widget w)
  143. {
  144.     assert( w != NULL );
  145.  
  146.     return ((_XfeWidth(w) <= _pixmap_buffer_width) &&
  147.             (_XfeHeight(w) <= _pixmap_buffer_height) &&
  148.             (_XfeDepth(w) == _pixmap_buffer_depth));
  149. }
  150. /*----------------------------------------------------------------------*/
  151.  
  152.  
  153.  
  154. /*----------------------------------------------------------------------*/
  155. /*                                                                        */
  156. /* Pixmap                                                                */
  157. /*                                                                        */
  158. /*----------------------------------------------------------------------*/
  159. Boolean
  160. XfePixmapExtent(Display *    dpy,
  161.                 Pixmap        pixmap,
  162.                 Dimension *    width,
  163.                 Dimension *    height,
  164.                 Cardinal *    depth)
  165. {
  166.     Boolean result = False;
  167.     
  168.     assert( dpy != NULL );
  169.     
  170.     if (_XfePixmapGood(pixmap))
  171.     {
  172.         unsigned int    borderWidth;
  173.         Window        rootWindow;
  174.         int         x;
  175.         int         y;
  176.         unsigned int    pixmap_width;
  177.         unsigned int    pixmap_height;
  178.         unsigned int    pixmap_depth;
  179.  
  180.         /* Query Server for Pixmap info */
  181.         result = XGetGeometry(dpy,pixmap,&rootWindow,&x,&y,
  182.                               &pixmap_width,&pixmap_height,
  183.                               &borderWidth,&pixmap_depth);
  184.     
  185.  
  186.         /* Verify that the pixmap extent was obtained */
  187.         if(result)
  188.         {
  189.             /* Assign the required components */
  190.             if (width)
  191.             {
  192.                 *width = pixmap_width;
  193.             }
  194.         
  195.             if (height)
  196.             {
  197.                 *height = pixmap_height;
  198.             }
  199.         
  200.             if (depth)
  201.             {
  202.                 *depth = pixmap_depth;
  203.             }
  204.         }
  205.         else
  206.         {
  207.             /* Make them NULL as an extra warning */
  208.             if (width)
  209.             {
  210.                 *width = 0;
  211.             }
  212.         
  213.             if (height)
  214.             {
  215.                 *height = 0;
  216.             }
  217.         
  218.             if (depth)
  219.             {
  220.                 *depth = 0;
  221.             }
  222.         }
  223.     }
  224.     
  225.     return result;
  226. }
  227. /*----------------------------------------------------------------------*/
  228. Boolean
  229. XfePixmapGood(Pixmap pixmap)
  230. {
  231.    return _XfePixmapGood(pixmap);
  232. }
  233. /*----------------------------------------------------------------------*/
  234.  
  235.  
  236. /*----------------------------------------------------------------------*/
  237. /*                                                                        */
  238. /* Stipple / Tile functions                        */
  239. /*                                                                        */
  240. /*----------------------------------------------------------------------*/
  241. Pixmap
  242. XfeInsensitiveTile(Screen * screen,int depth,Pixel fg,Pixel bg)
  243. {
  244.    return XmGetPixmapByDepth(screen,"50_foreground",fg,bg,depth);
  245. }
  246. /*----------------------------------------------------------------------*/
  247. Pixmap
  248. XfeInsensitiveStipple(Screen * screen,int depth,Pixel fg,Pixel bg)
  249. {
  250.    return XmGetPixmapByDepth(screen,"50_foreground",fg,bg,depth);
  251. }
  252. /*----------------------------------------------------------------------*/
  253. void
  254. XfePixmapClear(Display *    dpy,
  255.                Pixmap        pixmap,
  256.                GC            gc,
  257.                Dimension    width,
  258.                Dimension    height)
  259. {
  260.     assert( dpy != NULL );
  261.     assert( _XfePixmapGood(pixmap) );
  262.     assert( gc != None );
  263.     assert( width > 0 );
  264.     assert( height > 0 );
  265.  
  266.     XFillRectangle(dpy,pixmap,gc,0,0,width,height);
  267. }
  268. /*----------------------------------------------------------------------*/
  269. Pixmap
  270. XfePixmapCheck(Widget        w,
  271.                Pixmap        pixmap,
  272.                Dimension *    width_out,
  273.                Dimension *    height_out)
  274. {
  275.     Pixmap        result = XmUNSPECIFIED_PIXMAP;
  276.     Dimension    width = 0;
  277.     Dimension    height = 0;
  278.  
  279.     /* Check the pixmap only if it is good */
  280.     if (_XfePixmapGood(pixmap))
  281.     {
  282.         Cardinal depth;
  283.  
  284.         if (XfePixmapExtent(XtDisplay(w),
  285.                             pixmap,
  286.                             &width,
  287.                             &height,
  288.                             &depth))
  289.         {
  290.             /* Make sure the pixmap's depth matches the widget */
  291.             if (_XfeDepth(w) == depth)
  292.             {
  293.                 result = pixmap;
  294.             }
  295.             else
  296.             {
  297.                 _XfeWarning(w,MESSAGE1);
  298.             }
  299.         }
  300.         else
  301.         {
  302.             _XfeWarning(w,MESSAGE2);
  303.         }
  304.     }
  305.  
  306.     if (width_out)
  307.     {
  308.         *width_out = width;
  309.     }
  310.  
  311.     if (height_out)
  312.     {
  313.         *height_out = height;
  314.     }
  315.  
  316.     return pixmap;
  317. }
  318. /*----------------------------------------------------------------------*/
  319.  
  320. /*----------------------------------------------------------------------*/
  321. /*                                                                        */
  322. /* Preparation utlities                                                    */
  323. /*                                                                        */
  324. /*----------------------------------------------------------------------*/
  325. /* extern */ void
  326. _XfePixmapPrepare(Widget        w,
  327.                   Pixmap *        pixmap_in_out,
  328.                   Dimension *    width_out,
  329.                   Dimension *    height_out,
  330.                   String        name)
  331. {
  332.     Dimension width = 0;
  333.     Dimension height = 0;
  334.  
  335. /*     assert( width_out != NULL ); */
  336. /*     assert( height_out != NULL ); */
  337.     assert( pixmap_in_out != NULL );
  338.     assert( _XfeIsAlive(w) );
  339.             
  340.     /* Obtain geometry info for the pixmap if its good */
  341.     if (_XfePixmapGood(*pixmap_in_out))
  342.     {
  343.         Cardinal depth;
  344.         
  345.         if (XfePixmapExtent(XtDisplay(w),
  346.                             *pixmap_in_out,
  347.                             &width,
  348.                             &height,
  349.                             &depth))
  350.         {
  351.             /* Make sure the pixmap's depth matches the widget */
  352.             if (_XfeDepth(w) != depth)
  353.             {
  354.                 width = 0;
  355.                 height = 0;
  356.  
  357.                 *pixmap_in_out = XmUNSPECIFIED_PIXMAP;
  358.  
  359.                 _XfeArgWarning(w,MESSAGE1,name);
  360.             }
  361.         }
  362.         /* Could not get geometry for the pixmap */
  363.         else
  364.         {
  365.             *pixmap_in_out = XmUNSPECIFIED_PIXMAP;
  366.  
  367.             _XfeArgWarning(w,MESSAGE2,name);
  368.         }
  369.     }
  370.  
  371.     /* Assign the dimensions if needed */
  372.     if (width_out)
  373.     {
  374.         *width_out = width;
  375.     }
  376.  
  377.     if (height_out)
  378.     {
  379.         *height_out = height;
  380.     }
  381. }
  382. /*----------------------------------------------------------------------*/
  383.