gtk2/gdk/gdkpixmap.c
Benjamin Otte 6a48a042c7 API: remove GdkGC
XXX: Some traces in the win32 code are left. I suppose they'd best be
replaced using Cairo.
2010-08-10 21:02:29 +02:00

444 lines
14 KiB
C

/* GDK - The GIMP Drawing Kit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "config.h"
#include "gdkpixmap.h"
#include "gdkinternals.h"
#include "gdkpixbuf.h"
#include "gdkscreen.h"
static void gdk_pixmap_real_get_size (GdkDrawable *drawable,
gint *width,
gint *height);
static cairo_surface_t *gdk_pixmap_ref_cairo_surface (GdkDrawable *drawable);
static cairo_surface_t *gdk_pixmap_create_cairo_surface (GdkDrawable *drawable,
int width,
int height);
static GdkVisual* gdk_pixmap_real_get_visual (GdkDrawable *drawable);
static gint gdk_pixmap_real_get_depth (GdkDrawable *drawable);
static void gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
GdkColormap *cmap);
static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
static GdkScreen* gdk_pixmap_real_get_screen (GdkDrawable *drawable);
static void gdk_pixmap_init (GdkPixmapObject *pixmap);
static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
static void gdk_pixmap_finalize (GObject *object);
static gpointer parent_class = NULL;
GType
gdk_pixmap_get_type (void)
{
static GType object_type = 0;
if (!object_type)
object_type = g_type_register_static_simple (GDK_TYPE_DRAWABLE,
"GdkPixmap",
sizeof (GdkPixmapObjectClass),
(GClassInitFunc) gdk_pixmap_class_init,
sizeof (GdkPixmapObject),
(GInstanceInitFunc) gdk_pixmap_init,
0);
return object_type;
}
static void
gdk_pixmap_init (GdkPixmapObject *pixmap)
{
/* 0-initialization is good for all other fields. */
pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
}
static void
gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gdk_pixmap_finalize;
drawable_class->get_depth = gdk_pixmap_real_get_depth;
drawable_class->get_screen = gdk_pixmap_real_get_screen;
drawable_class->get_size = gdk_pixmap_real_get_size;
drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
drawable_class->get_visual = gdk_pixmap_real_get_visual;
drawable_class->ref_cairo_surface = gdk_pixmap_ref_cairo_surface;
drawable_class->create_cairo_surface = gdk_pixmap_create_cairo_surface;
}
static void
gdk_pixmap_finalize (GObject *object)
{
GdkPixmapObject *obj = (GdkPixmapObject *) object;
g_object_unref (obj->impl);
obj->impl = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
GdkPixmap *
gdk_pixmap_new (GdkDrawable *drawable,
gint width,
gint height,
gint depth)
{
GdkDrawable *source_drawable;
if (drawable)
source_drawable = _gdk_drawable_get_source_drawable (drawable);
else
source_drawable = NULL;
return _gdk_pixmap_new (source_drawable, width, height, depth);
}
GdkPixmap *
gdk_bitmap_create_from_data (GdkDrawable *drawable,
const gchar *data,
gint width,
gint height)
{
GdkDrawable *source_drawable;
if (drawable)
source_drawable = _gdk_drawable_get_source_drawable (drawable);
else
source_drawable = NULL;
return _gdk_bitmap_create_from_data (source_drawable, data, width, height);
}
GdkPixmap*
gdk_pixmap_create_from_data (GdkDrawable *drawable,
const gchar *data,
gint width,
gint height,
gint depth,
const GdkColor *fg,
const GdkColor *bg)
{
GdkDrawable *source_drawable;
source_drawable = _gdk_drawable_get_source_drawable (drawable);
return _gdk_pixmap_create_from_data (source_drawable,
data, width, height,
depth, fg,bg);
}
static void
gdk_pixmap_real_get_size (GdkDrawable *drawable,
gint *width,
gint *height)
{
g_return_if_fail (GDK_IS_PIXMAP (drawable));
gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
width, height);
}
static GdkVisual*
gdk_pixmap_real_get_visual (GdkDrawable *drawable)
{
GdkColormap *colormap;
g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
colormap = gdk_drawable_get_colormap (drawable);
return colormap ? gdk_colormap_get_visual (colormap) : NULL;
}
static gint
gdk_pixmap_real_get_depth (GdkDrawable *drawable)
{
gint depth;
g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
depth = GDK_PIXMAP_OBJECT (drawable)->depth;
return depth;
}
static void
gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
GdkColormap *cmap)
{
g_return_if_fail (GDK_IS_PIXMAP (drawable));
gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
}
static GdkColormap*
gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
{
g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
}
static cairo_surface_t *
gdk_pixmap_ref_cairo_surface (GdkDrawable *drawable)
{
return _gdk_drawable_ref_cairo_surface (((GdkPixmapObject*)drawable)->impl);
}
static cairo_surface_t *
gdk_pixmap_create_cairo_surface (GdkDrawable *drawable,
int width,
int height)
{
return _gdk_windowing_create_cairo_surface (GDK_PIXMAP_OBJECT(drawable)->impl,
width, height);
}
static GdkBitmap *
make_solid_mask (GdkScreen *screen, gint width, gint height)
{
GdkBitmap *bitmap;
cairo_t *cr;
bitmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
width, height, 1);
cr = gdk_cairo_create (bitmap);
cairo_paint (cr);
cairo_destroy (cr);
return bitmap;
}
#define PACKED_COLOR(c) ((((c)->red & 0xff00) << 8) | \
((c)->green & 0xff00) | \
((c)->blue >> 8))
static GdkPixmap *
gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
GdkBitmap **mask,
const GdkColor *transparent_color,
GdkPixbuf *pixbuf)
{
GdkPixmap *pixmap;
GdkPixbuf *render_pixbuf;
cairo_t *cr;
GdkScreen *screen = gdk_colormap_get_screen (colormap);
pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
gdk_colormap_get_visual (colormap)->depth);
gdk_drawable_set_colormap (pixmap, colormap);
if (transparent_color)
{
guint32 packed_color = PACKED_COLOR (transparent_color);
render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
GDK_INTERP_NEAREST,
255, 16, packed_color, packed_color);
}
else
render_pixbuf = pixbuf;
cr = gdk_cairo_create (pixmap);
gdk_cairo_set_source_pixbuf (cr, render_pixbuf, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
if (render_pixbuf != pixbuf)
g_object_unref (render_pixbuf);
if (mask)
gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf, colormap, NULL, mask, 128);
if (mask && !*mask)
*mask = make_solid_mask (screen,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf));
return pixmap;
}
/**
* gdk_pixmap_colormap_create_from_xpm:
* @drawable: a #GdkDrawable, used to determine default values
* for the new pixmap. Can be %NULL if @colormap is given.
* @colormap: the #GdkColormap that the new pixmap will be use.
* If omitted, the colormap for @window will be used.
* @mask: a pointer to a place to store a bitmap representing
* the transparency mask of the XPM file. Can be %NULL,
* in which case transparency will be ignored.
* @transparent_color: the color to be used for the pixels
* that are transparent in the input file. Can be %NULL,
* in which case a default color will be used.
* @filename: the filename of a file containing XPM data.
*
* Create a pixmap from a XPM file using a particular colormap.
*
* Returns: (transfer none): the #GdkPixmap.
*/
GdkPixmap*
gdk_pixmap_colormap_create_from_xpm (GdkDrawable *drawable,
GdkColormap *colormap,
GdkBitmap **mask,
const GdkColor *transparent_color,
const gchar *filename)
{
GdkPixbuf *pixbuf;
GdkPixmap *pixmap;
g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
if (colormap == NULL)
colormap = gdk_drawable_get_colormap (drawable);
pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
if (!pixbuf)
return NULL;
pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
g_object_unref (pixbuf);
return pixmap;
}
/**
* gdk_pixmap_create_from_xpm:
* @drawable: a #GdkDrawable, used to determine default values
* for the new pixmap.
* @mask: (out) a pointer to a place to store a bitmap representing
* the transparency mask of the XPM file. Can be %NULL,
* in which case transparency will be ignored.
* @transparent_color: the color to be used for the pixels
* that are transparent in the input file. Can be %NULL,
* in which case a default color will be used.
* @filename: the filename of a file containing XPM data.
*
* Create a pixmap from a XPM file.
*
* Returns: (transfer none): the #GdkPixmap
*/
GdkPixmap*
gdk_pixmap_create_from_xpm (GdkDrawable *drawable,
GdkBitmap **mask,
const GdkColor *transparent_color,
const gchar *filename)
{
return gdk_pixmap_colormap_create_from_xpm (drawable, NULL, mask,
transparent_color, filename);
}
/**
* gdk_pixmap_colormap_create_from_xpm_d:
* @drawable: a #GdkDrawable, used to determine default values
* for the new pixmap. Can be %NULL if @colormap is given.
* @colormap: the #GdkColormap that the new pixmap will be use.
* If omitted, the colormap for @window will be used.
* @mask: a pointer to a place to store a bitmap representing
* the transparency mask of the XPM file. Can be %NULL,
* in which case transparency will be ignored.
* @transparent_color: the color to be used for the pixels
* that are transparent in the input file. Can be %NULL,
* in which case a default color will be used.
* @data: Pointer to a string containing the XPM data.
*
* Create a pixmap from data in XPM format using a particular
* colormap.
*
* Returns: (transfer none): the #GdkPixmap.
*/
GdkPixmap*
gdk_pixmap_colormap_create_from_xpm_d (GdkDrawable *drawable,
GdkColormap *colormap,
GdkBitmap **mask,
const GdkColor *transparent_color,
gchar **data)
{
GdkPixbuf *pixbuf;
GdkPixmap *pixmap;
g_return_val_if_fail (drawable != NULL || colormap != NULL, NULL);
g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
if (colormap == NULL)
colormap = gdk_drawable_get_colormap (drawable);
pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
if (!pixbuf)
return NULL;
pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
g_object_unref (pixbuf);
return pixmap;
}
/**
* gdk_pixmap_create_from_xpm_d:
* @drawable: a #GdkDrawable, used to determine default values
* for the new pixmap.
* @mask: (out): Pointer to a place to store a bitmap representing
* the transparency mask of the XPM file. Can be %NULL,
* in which case transparency will be ignored.
* @transparent_color: This color will be used for the pixels
* that are transparent in the input file. Can be %NULL
* in which case a default color will be used.
* @data: Pointer to a string containing the XPM data.
*
* Create a pixmap from data in XPM format.
*
* Returns: (transfer none): the #GdkPixmap.
*/
GdkPixmap*
gdk_pixmap_create_from_xpm_d (GdkDrawable *drawable,
GdkBitmap **mask,
const GdkColor *transparent_color,
gchar **data)
{
return gdk_pixmap_colormap_create_from_xpm_d (drawable, NULL, mask,
transparent_color, data);
}
static GdkScreen*
gdk_pixmap_real_get_screen (GdkDrawable *drawable)
{
return gdk_drawable_get_screen (GDK_PIXMAP_OBJECT (drawable)->impl);
}