+
+/* Create and return the cursor to be used for popup menus and
+ scroll bars on display DPY. */
+
+GdkCursor *
+xg_create_default_cursor (dpy)
+ Display *dpy;
+{
+ GdkDisplay *gdpy = gdk_x11_lookup_xdisplay (dpy);
+ return gdk_cursor_new_for_display (gdpy, GDK_LEFT_PTR);
+}
+
+/* Apply GMASK to GPIX and return a GdkPixbuf with an alpha channel. */
+
+GdkPixbuf *
+xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap)
+ GdkPixmap *gpix;
+ GdkPixmap *gmask;
+ GdkColormap *cmap;
+{
+ int x, y, width, height, rowstride, mask_rowstride;
+ GdkPixbuf *icon_buf, *tmp_buf;
+ guchar *pixels;
+ guchar *mask_pixels;
+
+ gdk_drawable_get_size (gpix, &width, &height);
+ tmp_buf = gdk_pixbuf_get_from_drawable (NULL, gpix, cmap,
+ 0, 0, 0, 0, width, height);
+ icon_buf = gdk_pixbuf_add_alpha (tmp_buf, FALSE, 0, 0, 0);
+ g_object_unref (G_OBJECT (tmp_buf));
+
+ if (gmask)
+ {
+ GdkPixbuf *mask_buf = gdk_pixbuf_get_from_drawable (NULL,
+ gmask,
+ NULL,
+ 0, 0, 0, 0,
+ width, height);
+ guchar *pixels = gdk_pixbuf_get_pixels (icon_buf);
+ guchar *mask_pixels = gdk_pixbuf_get_pixels (mask_buf);
+ int rowstride = gdk_pixbuf_get_rowstride (icon_buf);
+ int mask_rowstride = gdk_pixbuf_get_rowstride (mask_buf);
+ int y;
+
+ for (y = 0; y < height; ++y)
+ {
+ guchar *iconptr, *maskptr;
+ int x;
+
+ iconptr = pixels + y * rowstride;
+ maskptr = mask_pixels + y * mask_rowstride;
+
+ for (x = 0; x < width; ++x)
+ {
+ /* In a bitmap, RGB is either 255/255/255 or 0/0/0. Checking
+ just R is sufficient. */
+ if (maskptr[0] == 0)
+ iconptr[3] = 0; /* 0, 1, 2 is R, G, B. 3 is alpha. */
+
+ iconptr += rowstride/width;
+ maskptr += mask_rowstride/width;
+ }
+ }
+
+ g_object_unref (G_OBJECT (mask_buf));
+ }
+
+ return icon_buf;
+}
+
+/* For the image defined in IMG, make and return a GtkImage. For displays with
+ 8 planes or less we must make a GdkPixbuf and apply the mask manually.
+ Otherwise the highlightning and dimming the tool bar code in GTK does
+ will look bad. For display with more than 8 planes we just use the
+ pixmap and mask directly. For monochrome displays, GTK doesn't seem
+ able to use external pixmaps, it looks bad whatever we do.
+ The image is defined on the display where frame F is.
+ WIDGET is used to find the GdkColormap to use for the GdkPixbuf.
+ If OLD_WIDGET is NULL, a new widget is constructed and returned.
+ If OLD_WIDGET is not NULL, that widget is modified. */
+
+static GtkWidget *
+xg_get_image_for_pixmap (f, img, widget, old_widget)
+ FRAME_PTR f;
+ struct image *img;
+ GtkWidget *widget;
+ GtkImage *old_widget;
+{
+ GdkPixmap *gpix;
+ GdkPixmap *gmask;
+ GdkDisplay *gdpy;
+
+ /* If we are on a one bit display, let GTK do all the image handling.
+ This seems to be the only way to make insensitive and activated icons
+ look good. */
+ if (x_screen_planes (f) == 1)
+ {
+ Lisp_Object specified_file = Qnil;
+ Lisp_Object tail;
+ extern Lisp_Object QCfile;
+
+ for (tail = XCDR (img->spec);
+ NILP (specified_file) && CONSP (tail) && CONSP (XCDR (tail));
+ tail = XCDR (XCDR (tail)))
+ if (EQ (XCAR (tail), QCfile))
+ specified_file = XCAR (XCDR (tail));
+
+ if (STRINGP (specified_file))
+ {
+
+ Lisp_Object file = Qnil;
+ struct gcpro gcpro1;
+ GCPRO1 (file);
+
+ file = x_find_image_file (specified_file);
+ /* We already loaded the image once before calling this
+ function, so this should not fail. */
+ xassert (STRINGP (file) != 0);
+
+ if (! old_widget)
+ old_widget = GTK_IMAGE (gtk_image_new_from_file (SDATA (file)));
+ else
+ gtk_image_set_from_file (old_widget, SDATA (file));
+
+ UNGCPRO;
+ return GTK_WIDGET (old_widget);
+ }
+ }
+
+ gdpy = gdk_x11_lookup_xdisplay (FRAME_X_DISPLAY (f));
+ gpix = gdk_pixmap_foreign_new_for_display (gdpy, img->pixmap);
+ gmask = img->mask ? gdk_pixmap_foreign_new_for_display (gdpy, img->mask) : 0;
+
+ if (x_screen_planes (f) > 8 || x_screen_planes (f) == 1)
+ {
+ if (! old_widget)
+ old_widget = GTK_IMAGE (gtk_image_new_from_pixmap (gpix, gmask));
+ else
+ gtk_image_set_from_pixmap (old_widget, gpix, gmask);
+ }
+ else
+ {
+
+ /* This is a workaround to make icons look good on pseudo color
+ displays. Apparently GTK expects the images to have an alpha
+ channel. If they don't, insensitive and activated icons will
+ look bad. This workaround does not work on monochrome displays,
+ and is not needed on true color/static color displays (i.e.
+ 16 bits and higher). */
+ GdkColormap *cmap = gtk_widget_get_colormap (widget);
+ GdkPixbuf *icon_buf = xg_get_pixbuf_from_pix_and_mask (gpix, gmask, cmap);
+
+ if (! old_widget)
+ old_widget = GTK_IMAGE (gtk_image_new_from_pixbuf (icon_buf));
+ else
+ gtk_image_set_from_pixbuf (old_widget, icon_buf);
+
+ g_object_unref (G_OBJECT (icon_buf));
+ }
+
+ g_object_unref (G_OBJECT (gpix));
+ if (gmask) g_object_unref (G_OBJECT (gmask));
+
+ return GTK_WIDGET (old_widget);
+}
+
+
+/* Set CURSOR on W and all widgets W contain. We must do like this