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

  1. /* -*- Mode: C; tab-width: 8; 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. /* images.c --- X front-end stuff dealing with images
  20.    Created: Jamie Zawinski <jwz@netscape.com>.
  21.  */
  22.  
  23.  
  24. #define JMC_INIT_IMGCB_ID
  25. #include "mozilla.h"
  26. #include "xfe.h"
  27.  
  28. #include "libimg.h"             /* Image Library public API. */
  29. #include "il_util.h"            /* Colormap/Colorspace API. */
  30. #include "prtypes.h"
  31.  
  32. #ifdef EDITOR
  33. #include "xeditor.h"
  34. #endif
  35.  
  36. #define GRAYSCALE_WORKS
  37.  
  38. #define PARTIAL_IMAGE_BORDER_WIDTH 2
  39.  
  40. /* We set this flag when we're running on a visual of a depth that we
  41.    know the image library won't support at all. */
  42. Boolean fe_ImagesCantWork = False;
  43.  
  44. typedef struct fe_PixmapClientData {
  45.     Pixmap pixmap;
  46.     Display *dpy;
  47. } fe_PixmapClientData;
  48.  
  49. /*
  50.  * Layout has moved this image to a new location, and it may get
  51.  * moved again before it is next displayed, so set valid to
  52.  * false so its final new coords will be saved when next it is displayed.
  53.  */
  54. void
  55. FE_ShiftImage (MWContext *context, LO_ImageStruct *lo_image)
  56. {
  57.                                 /* XXXM12N Obsolete. */
  58. }
  59.  
  60. /* Delaying images. */
  61. void
  62. fe_LoadDelayedImage (MWContext *context, const char *url)
  63. {
  64.   /* If autoload images is on and there are no delayed images, then there is
  65.      nothing to be done. */
  66.   if (fe_globalPrefs.autoload_images_p &&
  67.       !CONTEXT_DATA (context)->delayed_images_p)
  68.       return;
  69.  
  70.   if (url == FORCE_LOAD_ALL_IMAGES)
  71.       LO_SetForceLoadImage(NULL, TRUE);
  72.   else
  73.       LO_SetForceLoadImage((char *)url, FALSE);
  74.   CONTEXT_DATA (context)->delayed_images_p = False;
  75.  
  76.   fe_ReLayout (context, NET_DONT_RELOAD);
  77. }
  78.  
  79. void
  80. fe_LoadDelayedImages (MWContext *context)
  81. {
  82.   fe_LoadDelayedImage (context, FORCE_LOAD_ALL_IMAGES);
  83. }
  84.  
  85. #ifdef EDITOR
  86. void
  87. FE_ClearBackgroundImage(MWContext* context)
  88. {
  89.                                 /* XXXM12N Obsolete. */
  90. }
  91. #endif /*EDITOR*/
  92.  
  93.  
  94. /* #define DEBUG_GROUP_OBSERVER */
  95.  
  96. /* Image group observer callback. */
  97. void
  98. fe_ImageGroupObserver(XP_Observable observable, XP_ObservableMsg message,
  99.                  void *message_data, void *closure)
  100. {
  101.     IL_GroupMessageData *data = (IL_GroupMessageData*)message_data;
  102.     MWContext *context = (MWContext *)closure;
  103.  
  104.     XP_ASSERT(context == data->display_context);
  105.     XP_ASSERT(context->img_cx == data->image_context);
  106.  
  107.     switch (message) {
  108.     case IL_STARTED_LOADING:
  109.         CONTEXT_DATA(context)->loading_images_p = True;
  110.         break;
  111.  
  112.     case IL_ABORTED_LOADING:
  113.         if (!CONTEXT_DATA (context)->delayed_images_p &&
  114.             !fe_ImagesCantWork) {
  115.             CONTEXT_DATA (context)->delayed_images_p = True;
  116.         }
  117.     break;
  118.  
  119.     case IL_FINISHED_LOADING:
  120.         /* Note: This means that all images currently in the context have
  121.            finished loading.  If network activity has not ceased, then
  122.            new images could start to load in the image context. */
  123.         CONTEXT_DATA(context)->loading_images_p = False;
  124.         break;
  125.  
  126.     case IL_STARTED_LOOPING:
  127.         CONTEXT_DATA(context)->looping_images_p = True;
  128.         break;
  129.  
  130.     case IL_FINISHED_LOOPING:
  131.         /* Note: This means that all loaded images currently in the context
  132.            have finished looping.  If network activity has not ceased, then
  133.            new images could start to loop in the image context. */
  134.         CONTEXT_DATA(context)->looping_images_p = False;
  135.         break;
  136.         
  137.     default:
  138.         break;
  139.     }
  140.     FE_UpdateStopState(context);
  141. }
  142.  
  143. /*****************************************************************************/
  144. /*                       Image Library callbacks                             */
  145. /*****************************************************************************/
  146.  
  147. #define HOWMANY(x, r)     (((x) + ((r) - 1)) / (r))
  148. #define ROUNDUP(x, r)     (HOWMANY(x, r) * (r))
  149.  
  150. JMC_PUBLIC_API(void)
  151. _IMGCB_init(struct IMGCB* self, JMCException* *exception)
  152. {
  153.     /* Nothing to be done here. */
  154. }
  155.  
  156. extern JMC_PUBLIC_API(void*)
  157. _IMGCB_getBackwardCompatibleInterface(struct IMGCB* self,
  158.                                       const JMCInterfaceID* iid,
  159.                                       JMCException* *exception)
  160. {
  161.     return NULL;                /* Nothing to be done here. */
  162. }
  163.  
  164.  
  165. /**************************** Pixmap creation ********************************/
  166. JMC_PUBLIC_API(void)
  167. _IMGCB_NewPixmap(IMGCB* img_cb, jint op, void *dpy_cx, jint width, jint height,
  168.           IL_Pixmap *image, IL_Pixmap *mask)
  169. {
  170.     uint8 img_depth;
  171.     NI_PixmapHeader *img_header = &image->header;
  172.     NI_PixmapHeader *mask_header = mask ? &mask->header : NULL;
  173.     MWContext *context = (MWContext *)dpy_cx; /* XXXM12N This should be the
  174.                                                  FE's display context. */
  175.     Widget widget = CONTEXT_WIDGET(context);
  176.     Display *dpy = XtDisplay(widget);
  177.     Visual *visual = 0;
  178.     Window window;
  179.     unsigned int visual_depth;
  180.     unsigned int pixmap_depth;
  181.     Pixmap img_x_pixmap, mask_x_pixmap = NULL;
  182.     fe_PixmapClientData *img_client_data, *mask_client_data = NULL;
  183.  
  184.     /* Allocate the client data structures for the IL_Pixmaps. */
  185.     img_client_data = XP_NEW_ZAP(fe_PixmapClientData);
  186.     if (!img_client_data) {
  187.         image->bits = NULL;
  188.         mask->bits = NULL;
  189.         return;
  190.     }
  191.     if (mask) {
  192.         mask_client_data = XP_NEW_ZAP(fe_PixmapClientData);
  193.         if (!mask_client_data) {
  194.             image->bits = NULL;
  195.             mask->bits = NULL;
  196.             return;
  197.         }
  198.     }
  199.  
  200.     /* Get the required display parameters */
  201.     XtVaGetValues (widget, XmNvisual, &visual, 0);
  202.     if (!visual) visual = fe_globalData.default_visual;
  203.     window = XtWindow (CONTEXT_DATA (context)->drawing_area);
  204.     visual_depth = fe_VisualDepth (dpy, visual);
  205.     pixmap_depth = fe_VisualPixmapDepth (dpy, visual);
  206.  
  207.     /* Override the image and mask dimensions with the requested target
  208.        dimensions.  This instructs the image library to do any necessary
  209.        scaling. */
  210.     img_header->width = width;
  211.     img_header->height = height;
  212.     if (mask) {
  213.         mask_header->width = width;
  214.         mask_header->height = height;
  215.     }
  216.  
  217.     /* Override the image colorspace with the display colorspace.  This
  218.        instructs the image library to decode to the display colorspace
  219.        instead of decoding to the image's source colorspace. */
  220.     IL_ReleaseColorSpace(img_header->color_space);
  221.     img_header->color_space = context->color_space;
  222.     IL_AddRefToColorSpace(img_header->color_space);
  223.  
  224.     /* Compute the number of bytes per scan line for the image and mask,
  225.        and make sure it is quadlet aligned. */
  226.     img_depth = img_header->color_space->pixmap_depth;
  227.     XP_ASSERT(img_depth == pixmap_depth);
  228.     img_header->widthBytes = (img_header->width * img_depth + 7) / 8;
  229.     img_header->widthBytes = ROUNDUP(img_header->widthBytes, 4);
  230.     if (mask) {
  231.         mask_header->widthBytes = (mask_header->width + 7) / 8;
  232.         mask_header->widthBytes = ROUNDUP(mask_header->widthBytes, 4);
  233.     }
  234.  
  235.     /* Allocate memory for the image bits, and for the mask bits (if
  236.        required.) */
  237.     image->bits = calloc(img_header->widthBytes * img_header->height, 1);
  238.     if (!image->bits)
  239.         return;
  240.     if (mask) {
  241.         mask->bits = calloc(mask_header->widthBytes * mask_header->height, 1);
  242.         if (!mask->bits) {
  243.             free(image->bits);
  244.             image->bits = NULL;
  245.             return;
  246.         }
  247.     }
  248.  
  249.     /* Create an X pixmap for the image, and for the mask (if required.) */
  250.     img_x_pixmap = XCreatePixmap(dpy, window, img_header->width,
  251.                                  img_header->height, visual_depth);
  252.     if (mask)
  253.         mask_x_pixmap = XCreatePixmap(dpy, window, mask_header->width,
  254.                                       mask_header->height, 1);
  255.  
  256.     /* Fill in the pixmap client_data.  We store the Display pointer for use
  257.        in DestroyPixmap, which can be called after the FE's display context
  258.        (MWContext) has been destroyed. */
  259.     img_client_data->pixmap = img_x_pixmap;
  260.     img_client_data->dpy = dpy;
  261.     image->client_data = (void *)img_client_data;
  262.     if (mask) {
  263.         mask_client_data->pixmap = mask_x_pixmap;
  264.         mask_client_data->dpy = dpy;
  265.         mask->client_data = (void *)mask_client_data;
  266.     }
  267. }
  268.  
  269.  
  270. /**************************** Pixmap update **********************************/
  271. JMC_PUBLIC_API(void)
  272. _IMGCB_UpdatePixmap(IMGCB* img_cb, jint op, void* dpy_cx, IL_Pixmap* pixmap,
  273.              jint x_offset, jint y_offset, jint width, jint height)
  274. {
  275.     uint32 widthBytes;
  276.     MWContext *context = (MWContext *)dpy_cx; /* XXX This should be the FE's
  277.                                                  display context. */
  278.     Widget widget;
  279.     Display *dpy;
  280.     Visual *visual = 0;
  281.     Window window;
  282.     unsigned int visual_depth;
  283.     unsigned int visual_pixmap_depth; /* Pixmap depth supported by visual. */
  284.     unsigned int pixmap_depth;  /* Depth of the IL_Pixmap. */
  285.     Pixmap x_pixmap;
  286.     fe_PixmapClientData *pixmap_client_data;
  287.     XImage *x_image;
  288.     IL_ColorSpace *color_space = pixmap->header.color_space;
  289.     char *bits;
  290.     GC gc;
  291.     XGCValues gcv;
  292.     XP_Bool cached_gc;
  293.     
  294.     if (!context)
  295.         return;
  296.     widget = CONTEXT_WIDGET(context);
  297.     dpy = XtDisplay(widget);
  298.  
  299.     /* Check for zero dimensions. */
  300.     if (width == 0 || height == 0)
  301.         return;
  302.  
  303.     /* Get the required display parameters */
  304.     XtVaGetValues (widget, XmNvisual, &visual, 0);
  305.     if (!visual) visual = fe_globalData.default_visual;
  306.     window = XtWindow (CONTEXT_DATA (context)->drawing_area);
  307.     visual_depth = fe_VisualDepth (dpy, visual);
  308.     visual_pixmap_depth = fe_VisualPixmapDepth (dpy, visual);
  309.  
  310.     /* Get the depth of the IL_Pixmap.  This should be the same as the
  311.        visual_pixmap_depth if the IL_Pixmap is an image, or 1 if the
  312.        IL_Pixmap is a mask. */
  313.     pixmap_depth = color_space->pixmap_depth;
  314.  
  315.     widthBytes = pixmap->header.widthBytes;
  316.     bits = (unsigned char *)pixmap->bits + widthBytes * y_offset;
  317.  
  318.     x_image = XCreateImage(dpy, visual,
  319.                            (pixmap_depth == 1 ? 1 : visual_depth),
  320.                            (pixmap_depth == 1 ? XYPixmap : ZPixmap),
  321.                            x_offset,                   /* offset */
  322.                            bits,
  323.                            width,
  324.                            height,
  325.                            8,                          /* bitmap_pad */
  326.                            widthBytes);                /* bytes_per_line */
  327.  
  328.     if (pixmap_depth == 1) {
  329.         /* Image library always places pixels left-to-right MSB to LSB */
  330.         x_image->bitmap_bit_order = MSBFirst;
  331.  
  332.         /* This definition doesn't depend on client byte ordering
  333.            because the image library ensures that the bytes in
  334.            bitmask data are arranged left to right on the screen,
  335.            low to high address in memory. */
  336.         x_image->byte_order = MSBFirst;
  337.     }
  338.     else {
  339. #if defined(IS_LITTLE_ENDIAN)
  340.         x_image->byte_order = LSBFirst;
  341. #elif defined (IS_BIG_ENDIAN)
  342.         x_image->byte_order = MSBFirst;
  343. #else
  344.         ERROR! Endianness is unknown;
  345. #endif
  346.     }
  347.  
  348.     pixmap_client_data = (fe_PixmapClientData *)pixmap->client_data;
  349.     x_pixmap = pixmap_client_data->pixmap;
  350.  
  351.     memset(&gcv, ~0, sizeof(XGCValues));
  352.     gcv.function = GXcopy;
  353.  
  354.     if (pixmap_depth == visual_pixmap_depth) {
  355.         /* Get the GC out of the GC cache. */
  356.         gc = fe_GetGC(widget, GCFunction, &gcv);
  357.         cached_gc = TRUE;
  358.     }
  359.     else {
  360.         /* The pixmap_depth does not correspond to that of the GCs in the
  361.            cache, so create a new GC. */
  362.         gc = XCreateGC(dpy, x_pixmap, GCFunction, &gcv);
  363.         cached_gc = FALSE;
  364.     }
  365.  
  366.     XPutImage(dpy, x_pixmap, gc, x_image, x_offset, 0, x_offset,
  367.               y_offset, width, height);
  368.  
  369.     if (!cached_gc)
  370.         XFreeGC(dpy, gc);
  371.     x_image->data = 0;          /* Don't free the IL_Pixmap's bits. */
  372.     XDestroyImage(x_image);
  373. }
  374.  
  375.  
  376. /************************** Pixmap memory management *************************/
  377. JMC_PUBLIC_API(void)
  378. _IMGCB_ControlPixmapBits(IMGCB* img_cb, jint op, void* dpy_cx,
  379.                          IL_Pixmap* pixmap, IL_PixmapControl message)
  380. {
  381. }
  382.  
  383.  
  384. /**************************** Pixmap destruction *****************************/
  385. /* XXXM12N The dpy_cx argument is not used in DestroyPixmap and should be
  386.    removed. */
  387. JMC_PUBLIC_API(void)
  388. _IMGCB_DestroyPixmap(IMGCB* img_cb, jint op, void* dpy_cx, IL_Pixmap* pixmap)
  389. {
  390.     fe_PixmapClientData *pixmap_client_data = pixmap->client_data;
  391.     Display *dpy;
  392.     Pixmap x_pixmap;
  393.  
  394.     if (!pixmap_client_data)
  395.         return;
  396.  
  397.     dpy = pixmap_client_data->dpy;
  398.     x_pixmap = pixmap_client_data->pixmap;
  399.  
  400.     if (x_pixmap) {
  401.         XFreePixmap(dpy, x_pixmap);
  402.         pixmap->client_data = NULL;
  403.     }
  404.  
  405.     if (pixmap->bits) {
  406.         free(pixmap->bits);
  407.         pixmap->bits = NULL;
  408.     }
  409. }
  410.  
  411.  
  412. /**************************** Pixmap display *********************************/
  413. typedef struct 
  414. {
  415.     Pixmap x_pixmap;
  416.     GC gc;
  417.     Display *dpy;
  418.     int32 x_clip_offset, y_clip_offset;
  419.     XP_Rect *fill_rect;
  420. } fe_FillTiledPixmapClosure;
  421.  
  422. static void
  423. fe_FillTiledPixmapRectFunc(void *closure, XP_Rect *clip_rect)
  424. {
  425.     fe_FillTiledPixmapClosure *fill_closure =
  426.         (fe_FillTiledPixmapClosure*)closure;
  427.     Display *dpy = fill_closure->dpy; 
  428.     Pixmap x_pixmap = fill_closure->x_pixmap;
  429.     GC gc = fill_closure->gc;
  430.     int32 x_clip_offset = fill_closure->x_clip_offset;
  431.     int32 y_clip_offset = fill_closure->y_clip_offset;
  432.     XP_Rect *fill_rect = fill_closure->fill_rect;
  433.     XP_Rect sub_rect;
  434.     uint32 sub_rect_width, sub_rect_height;
  435.  
  436.     /* Convert the clip rect's coordinates to be relative to the pixmap's
  437.        origin. */
  438.     XP_OffsetRect(clip_rect, x_clip_offset, y_clip_offset);
  439.     XP_IntersectRect(clip_rect, fill_rect, &sub_rect);
  440.  
  441.     /* Compute the width and height of the actual area to be drawn.  If either
  442.        dimension is zero, we have nothing to do. */
  443.     sub_rect_width = sub_rect.right - sub_rect.left;
  444.     sub_rect_height = sub_rect.bottom - sub_rect.top;
  445.     if (!sub_rect_width || !sub_rect_height)
  446.         return;
  447.  
  448.     /* Draw a sub_rect of the temporary mask. */
  449.     XFillRectangle(dpy, x_pixmap, gc, sub_rect.left, sub_rect.top,
  450.                    sub_rect_width, sub_rect_height);
  451. }
  452.  
  453. /* Generate a tiled mask for the case where the fe_Drawable has a clip
  454.    region.  Offset arguments are wrt the origin of the tiled mask. */
  455. static Pixmap
  456. fe_TiledMaskWithClipRegion(Display *dpy, Drawable drawable,
  457.                            Pixmap mask_x_pixmap,
  458.                            unsigned int width, unsigned int height,
  459.                            int x_tile_offset, int y_tile_offset,
  460.                            int x_clip_offset, int y_clip_offset,
  461.                            Region clip_region)
  462. {
  463.     GC gc;
  464.     XGCValues gcv;
  465.     unsigned long flags;
  466.     XP_Rect fill_rect;
  467.     Pixmap ret_x_pixmap;
  468.     fe_FillTiledPixmapClosure fill_closure;
  469.  
  470.     memset (&gcv, ~0, sizeof (XGCValues));
  471.  
  472.     ret_x_pixmap = XCreatePixmap(dpy, drawable, width, height, 1);
  473.     
  474.     /* The pixmap must be cleared since it may cover areas which do not
  475.        belong to the clip region. */
  476.     gcv.function = GXclear;
  477.     gc = XCreateGC (dpy, ret_x_pixmap, GCFunction, &gcv);
  478.     XFillRectangle(dpy, ret_x_pixmap, gc, 0, 0, width, height);
  479.  
  480.     /* Set up the GC. */
  481.     gcv.function = GXcopy;
  482.     gcv.fill_style = FillTiled;
  483.     gcv.tile = mask_x_pixmap;
  484.     gcv.ts_x_origin = x_tile_offset;
  485.     gcv.ts_y_origin = y_tile_offset;
  486.     flags = GCFunction | GCFillStyle | GCTile |
  487.         GCTileStipXOrigin | GCTileStipYOrigin;
  488.     XChangeGC(dpy, gc, flags, &gcv);
  489.  
  490.     /* Tile the mask_pixmap into the tiled_mask_pixmap, but only do so for
  491.        areas in the clip region's rectangle list. */
  492.     fill_rect.left = 0;
  493.     fill_rect.top = 0;
  494.     fill_rect.right = width;
  495.     fill_rect.bottom = height;
  496.     fill_closure.fill_rect = &fill_rect;
  497.     fill_closure.x_pixmap = ret_x_pixmap;
  498.     fill_closure.gc = gc;
  499.     fill_closure.dpy = dpy;
  500.     fill_closure.x_clip_offset = x_clip_offset;
  501.     fill_closure.y_clip_offset = y_clip_offset;
  502.     FE_ForEachRectInRegion(clip_region, fe_FillTiledPixmapRectFunc,
  503.                            &fill_closure);
  504.  
  505.     /* Clean up. */
  506.     XFreeGC(dpy, gc);
  507.  
  508.     return ret_x_pixmap;
  509. }
  510.  
  511.  
  512. typedef struct 
  513. {
  514.     Pixmap img_x_pixmap, mask_x_pixmap;
  515.     Drawable drawable;
  516.     GC gc, mask_gc;
  517.     Display *dpy;
  518.     int32 img_x_offset, img_y_offset;
  519.     XP_Rect *fill_rect;
  520. } fe_FillPixmapClosure;
  521.  
  522. static void
  523. fe_FillPixmapRectFunc(void *closure, XP_Rect *clip_rect)
  524. {
  525.     fe_FillPixmapClosure *fill_closure = (fe_FillPixmapClosure*)closure;
  526.     Display *dpy = fill_closure->dpy; 
  527.     Pixmap img_x_pixmap = fill_closure->img_x_pixmap;
  528.     Pixmap mask_x_pixmap = fill_closure->mask_x_pixmap;
  529.     Drawable drawable = fill_closure->drawable;
  530.     GC gc = fill_closure->gc;
  531.     GC mask_gc = fill_closure->mask_gc;
  532.     int32 img_x_offset = fill_closure->img_x_offset;
  533.     int32 img_y_offset = fill_closure->img_y_offset;
  534.     XP_Rect *fill_rect = fill_closure->fill_rect;
  535.     XP_Rect sub_rect;
  536.     Pixmap tmp_pixmap;
  537.     XGCValues gcv;
  538.     unsigned long flags;
  539.     int32 sub_rect_x_offset, sub_rect_y_offset; /* Offsets wrt image origin. */
  540.     uint32 sub_rect_width, sub_rect_height;
  541.  
  542.     /* Rectangle coordinates are all relative to the drawable origin. */
  543.     XP_IntersectRect(clip_rect, fill_rect, &sub_rect);
  544.  
  545.     /* Compute the width and height of the actual area to be drawn.  If either
  546.        dimension is zero, we have nothing to do. */
  547.     sub_rect_width = sub_rect.right - sub_rect.left;
  548.     sub_rect_height = sub_rect.bottom - sub_rect.top;
  549.     if (!sub_rect_width || !sub_rect_height)
  550.         return;
  551.  
  552.     /* Compute the offsets, wrt the image origin, of the area to be drawn. */
  553.     sub_rect_x_offset = sub_rect.left - img_x_offset;
  554.     sub_rect_y_offset = sub_rect.top - img_y_offset;
  555.  
  556.     /* Create a temporary mask pixmap which is the size of the area to be
  557.        drawn. */
  558.     tmp_pixmap = XCreatePixmap(dpy, drawable, sub_rect_width, sub_rect_height,
  559.                                1);
  560.     XCopyArea(dpy, mask_x_pixmap, tmp_pixmap, mask_gc, sub_rect_x_offset,
  561.               sub_rect_y_offset, sub_rect_width, sub_rect_height, 0, 0);
  562.  
  563.     /* Use the temporary mask as the GC's clip mask, instead of using the
  564.        entire image mask.  For complicated masks, this has a significant
  565.        performance benefit on X servers which convert clip_masks to
  566.        rectangle lists. */
  567.     gcv.clip_mask = tmp_pixmap;
  568.     gcv.clip_x_origin = sub_rect.left;
  569.     gcv.clip_y_origin = sub_rect.top;
  570.     flags = GCClipMask | GCClipXOrigin | GCClipYOrigin;
  571.     XChangeGC(dpy, gc, flags, &gcv);
  572.  
  573.     /* Draw a sub_rect of the image. */
  574.     XCopyArea(dpy, img_x_pixmap, drawable, gc, sub_rect_x_offset,
  575.               sub_rect_y_offset, sub_rect_width, sub_rect_height,
  576.               sub_rect.left, sub_rect.top);
  577.  
  578.     /* Clean up. */
  579.     XFreePixmap(dpy, tmp_pixmap);
  580. }
  581.  
  582. /* Draw the masked image for each rectangle in the compositor's clip region.
  583.    x_offset and y_offset are wrt the image origin, while rect_x_offset and
  584.    rect_y_offset are wrt the drawable origin. */
  585. static void
  586. fe_DrawMaskedImageWithClipRegion(Display *dpy, Drawable drawable,
  587.                                  Pixmap img_x_pixmap, Pixmap mask_x_pixmap,
  588.                                  unsigned int width, unsigned int height,
  589.                                  int img_x_offset, int img_y_offset,
  590.                                  int x_offset, int y_offset,
  591.                                  Region clip_region)
  592. {
  593.     GC gc, mask_gc;
  594.     XGCValues gcv;
  595.     XP_Rect fill_rect;
  596.     fe_FillPixmapClosure fill_closure;
  597.  
  598.     memset (&gcv, ~0, sizeof (XGCValues));
  599.  
  600.     /* Set up the GCs. */
  601.     gcv.function = GXcopy;
  602.     gc = XCreateGC(dpy, drawable, GCFunction, &gcv);
  603.     mask_gc = XCreateGC(dpy, mask_x_pixmap, GCFunction, &gcv);
  604.  
  605.     /* Draw the masked image into the drawable, but only do so for areas in
  606.        the clip region's rectangle list. */
  607.     fill_rect.left = img_x_offset + x_offset;
  608.     fill_rect.top = img_y_offset + y_offset;
  609.     fill_rect.right = fill_rect.left + width;
  610.     fill_rect.bottom = fill_rect.top + height;
  611.     fill_closure.fill_rect = &fill_rect;
  612.     fill_closure.img_x_pixmap = img_x_pixmap;
  613.     fill_closure.mask_x_pixmap = mask_x_pixmap;
  614.     fill_closure.drawable = drawable;
  615.     fill_closure.gc = gc;
  616.     fill_closure.mask_gc = mask_gc;
  617.     fill_closure.dpy = dpy;
  618.     fill_closure.img_x_offset = img_x_offset;
  619.     fill_closure.img_y_offset = img_y_offset;
  620.     FE_ForEachRectInRegion(clip_region, fe_FillPixmapRectFunc, &fill_closure);
  621.  
  622.     /* Clean up. */
  623.     XFreeGC(dpy, gc);
  624.     XFreeGC(dpy, mask_gc);
  625. }
  626.  
  627.  
  628. JMC_PUBLIC_API(void)
  629. _IMGCB_DisplayPixmap(IMGCB* img_cb, jint op, void* dpy_cx, IL_Pixmap* image,
  630.                      IL_Pixmap* mask, jint x, jint y, jint x_offset,
  631.                      jint y_offset, jint width, jint height)
  632. {
  633.     int32 img_x_offset, img_y_offset;   /* Offset of image in drawable. */
  634.     int32 rect_x_offset, rect_y_offset; /* Offset of update rect in
  635.                                            drawable. */
  636.     NI_PixmapHeader *img_header = &image->header;
  637.     uint32 img_width = img_header->width;     /* Image width. */
  638.     uint32 img_height = img_header->height;   /* Image height. */
  639.     MWContext *context = (MWContext *)dpy_cx; /* XXX This should be the FE's
  640.                                                  display context. */
  641.     Widget widget = CONTEXT_WIDGET(context);
  642.     fe_Drawable *fe_drawable = CONTEXT_DATA(context)->drawable;
  643.     Drawable drawable = fe_drawable->xdrawable;
  644.     Display *dpy = XtDisplay(widget);
  645.     Pixmap img_x_pixmap, mask_x_pixmap;
  646.     fe_PixmapClientData *img_client_data, *mask_client_data;
  647.     GC gc;
  648.     XGCValues gcv;
  649.     unsigned long flags;
  650.     XP_Bool tiling_required = FALSE;
  651.     
  652.     /* Check for zero display area. */
  653.     if (width == 0 || height == 0)
  654.         return;
  655.  
  656.     /* Retrieve the server pixmaps. */
  657.     img_client_data = (fe_PixmapClientData *)image->client_data;
  658.     if (!img_client_data)
  659.         return;
  660.     img_x_pixmap = img_client_data->pixmap;
  661.     if (!img_x_pixmap)
  662.         return;
  663.     if (mask) {
  664.         mask_client_data = (fe_PixmapClientData *)mask->client_data;
  665.         mask_x_pixmap = mask_client_data->pixmap;
  666.     }
  667.  
  668.     /* Determine whether tiling is required. */
  669.     if ((x_offset + width > img_width) || (y_offset + height > img_height))
  670.         tiling_required = TRUE;
  671.  
  672.     /* Compute the offset into the drawable of the image origin. */
  673.     img_x_offset = x - CONTEXT_DATA(context)->document_x +
  674.         fe_drawable->x_origin;
  675.     img_y_offset = y - CONTEXT_DATA(context)->document_y +
  676.         fe_drawable->y_origin;
  677.  
  678.     /* Compute the offset into the drawable for the area to be drawn. */
  679.     rect_x_offset = img_x_offset + x_offset;
  680.     rect_y_offset = img_y_offset + y_offset;
  681.  
  682.     /* Do the actual drawing.  There are several cases to be dealt with:
  683.        transparent vs non-transparent, tiled vs non-tiled and clipped by
  684.        compositor's clip region vs not clipped. */
  685.     memset(&gcv, ~0, sizeof (XGCValues));
  686.     if (mask) {                 /* Image is transparent. */
  687.         if (tiling_required) {
  688.             /* Offsets are measured wrt the origin of the tiled mask to
  689.                be generated. */
  690.             int x_tile_offset = img_x_offset - rect_x_offset;
  691.             int y_tile_offset = img_y_offset - rect_y_offset;
  692.             Pixmap tmp_pixmap = 0;
  693.  
  694.             /* Create the mask by tiling the mask_x_pixmap and computing
  695.                the intersection with the compositor's clip region. */
  696.             tmp_pixmap =
  697.                 fe_TiledMaskWithClipRegion(dpy, drawable, mask_x_pixmap,
  698.                                            width, height, x_tile_offset,
  699.                                            y_tile_offset, -rect_x_offset,
  700.                                            -rect_y_offset,
  701.                                            fe_drawable->clip_region);
  702.             
  703.             /* Create the GC.  Don't attempt to get a GC from the GC cache
  704.                because we are using a temporary mask pixmap. */
  705.             gcv.fill_style = FillTiled;
  706.             gcv.tile = img_x_pixmap;
  707.             gcv.ts_x_origin = img_x_offset;
  708.             gcv.ts_y_origin = img_y_offset;
  709.             gcv.clip_mask = tmp_pixmap;
  710.             gcv.clip_x_origin = rect_x_offset;
  711.             gcv.clip_y_origin = rect_y_offset;
  712.             flags = GCFillStyle | GCTile | GCTileStipXOrigin |
  713.                 GCTileStipYOrigin | GCClipMask | GCClipXOrigin | GCClipYOrigin;
  714.             gc = XCreateGC(dpy, drawable, flags, &gcv);
  715.  
  716.             /* Draw the image (transparent and tiled.) */
  717.             XFillRectangle (dpy, drawable, gc, rect_x_offset, rect_y_offset,
  718.                             width, height);
  719.  
  720.             /* Clean up. */
  721.             XFreeGC(dpy, gc);
  722.             XFreePixmap(dpy, tmp_pixmap);
  723.         }
  724.         else {                  /* Tiling not required. */
  725.             if (fe_drawable->clip_region) {
  726.                 
  727.                 /* Draw the image (transparent, non-tiled and with
  728.                    clip_region.)  x_offset and y_offset are wrt the image
  729.                    origin, while rect_x_offset and rect_y_offset are wrt the
  730.                    drawable origin. */
  731.                 fe_DrawMaskedImageWithClipRegion(dpy, drawable, img_x_pixmap,
  732.                                                  mask_x_pixmap, width, height,
  733.                                                  img_x_offset, img_y_offset,
  734.                                                  x_offset, y_offset,
  735.                                                  fe_drawable->clip_region);
  736.             }
  737.             else {              /* No clip region. */
  738.                 /* XXX transparent, non-tiled and no clip_region. */
  739.             }
  740.         }
  741.     }
  742.     else {                      /* Image is not transparent. */
  743.         if (tiling_required) {
  744.             /* Get the GC from the GC cache.  If the compositor has given us
  745.                a clip region, then the GC must have a matching clip mask. */
  746.             gcv.fill_style = FillTiled;
  747.             gcv.tile = img_x_pixmap;
  748.             gcv.ts_x_origin = img_x_offset;
  749.             gcv.ts_y_origin = img_y_offset;
  750.             flags = GCFillStyle | GCTile | GCTileStipXOrigin |
  751.                 GCTileStipYOrigin;
  752.             if (fe_drawable->clip_region)
  753.                 gc = fe_GetGCfromDW(dpy, drawable, flags, &gcv,
  754.                                     fe_drawable->clip_region);
  755.             else
  756.                 gc = fe_GetGCfromDW(dpy, drawable, flags, &gcv, NULL);
  757.  
  758.             /* Draw the image (opaque and tiled.) */
  759.             XFillRectangle (dpy, drawable, gc, rect_x_offset, rect_y_offset,
  760.                             width, height);  
  761.         }
  762.         else {                  /* Tiling not required. */
  763.             /* Get the GC from the GC cache.  If the compositor has given us
  764.                a clip region, then the GC must have a matching clip mask. */
  765.             gcv.function = GXcopy;
  766.             if (fe_drawable->clip_region)
  767.                 gc = fe_GetGCfromDW(dpy, drawable, GCFunction,
  768.                                     &gcv, fe_drawable->clip_region);
  769.             else
  770.                 gc = fe_GetGCfromDW(dpy, drawable, GCFunction, &gcv, NULL);
  771.  
  772.             /* Draw the image (opaque and non-tiled.) */
  773.             XCopyArea (dpy, img_x_pixmap, drawable, gc, x_offset,
  774.                        y_offset, width, height, rect_x_offset, rect_y_offset);
  775.         }
  776.     }
  777. }
  778.  
  779. /* Mocha image group observer callback. */
  780. void
  781. FE_MochaImageGroupObserver(XP_Observable observable, XP_ObservableMsg message,
  782.                            void *message_data, void *closure)
  783. {
  784. }
  785.  
  786. /*****************************************************************************/
  787. /*                       End of Image Library callbacks                      */
  788. /*****************************************************************************/
  789.