1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gdkpixmap.h"
28 #include "gdkinternals.h"
29 #include "gdkpixbuf.h"
31 static GdkGC *gdk_pixmap_create_gc (GdkDrawable *drawable,
33 GdkGCValuesMask mask);
34 static void gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
41 static void gdk_pixmap_draw_arc (GdkDrawable *drawable,
50 static void gdk_pixmap_draw_polygon (GdkDrawable *drawable,
55 static void gdk_pixmap_draw_text (GdkDrawable *drawable,
62 static void gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
69 static void gdk_pixmap_draw_drawable (GdkDrawable *drawable,
78 static void gdk_pixmap_draw_points (GdkDrawable *drawable,
82 static void gdk_pixmap_draw_segments (GdkDrawable *drawable,
86 static void gdk_pixmap_draw_lines (GdkDrawable *drawable,
90 static void gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
95 PangoGlyphString *glyphs);
96 static void gdk_pixmap_draw_image (GdkDrawable *drawable,
106 static void gdk_pixmap_real_get_size (GdkDrawable *drawable,
110 static GdkVisual* gdk_pixmap_real_get_visual (GdkDrawable *drawable);
111 static gint gdk_pixmap_real_get_depth (GdkDrawable *drawable);
112 static void gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
114 static GdkColormap* gdk_pixmap_real_get_colormap (GdkDrawable *drawable);
116 static void gdk_pixmap_init (GdkPixmapObject *pixmap);
117 static void gdk_pixmap_class_init (GdkPixmapObjectClass *klass);
118 static void gdk_pixmap_finalize (GObject *object);
120 static gpointer parent_class = NULL;
123 gdk_pixmap_get_type (void)
125 static GType object_type = 0;
129 static const GTypeInfo object_info =
131 sizeof (GdkPixmapObjectClass),
132 (GBaseInitFunc) NULL,
133 (GBaseFinalizeFunc) NULL,
134 (GClassInitFunc) gdk_pixmap_class_init,
135 NULL, /* class_finalize */
136 NULL, /* class_data */
137 sizeof (GdkPixmapObject),
139 (GInstanceInitFunc) gdk_pixmap_init,
142 object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
151 gdk_pixmap_init (GdkPixmapObject *pixmap)
153 /* 0-initialization is good for all other fields. */
154 pixmap->impl = g_object_new (_gdk_pixmap_impl_get_type (), NULL);
158 gdk_pixmap_class_init (GdkPixmapObjectClass *klass)
160 GObjectClass *object_class = G_OBJECT_CLASS (klass);
161 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
163 parent_class = g_type_class_peek_parent (klass);
165 object_class->finalize = gdk_pixmap_finalize;
167 drawable_class->create_gc = gdk_pixmap_create_gc;
168 drawable_class->draw_rectangle = gdk_pixmap_draw_rectangle;
169 drawable_class->draw_arc = gdk_pixmap_draw_arc;
170 drawable_class->draw_polygon = gdk_pixmap_draw_polygon;
171 drawable_class->draw_text = gdk_pixmap_draw_text;
172 drawable_class->draw_text_wc = gdk_pixmap_draw_text_wc;
173 drawable_class->draw_drawable = gdk_pixmap_draw_drawable;
174 drawable_class->draw_points = gdk_pixmap_draw_points;
175 drawable_class->draw_segments = gdk_pixmap_draw_segments;
176 drawable_class->draw_lines = gdk_pixmap_draw_lines;
177 drawable_class->draw_glyphs = gdk_pixmap_draw_glyphs;
178 drawable_class->draw_image = gdk_pixmap_draw_image;
179 drawable_class->get_depth = gdk_pixmap_real_get_depth;
180 drawable_class->get_size = gdk_pixmap_real_get_size;
181 drawable_class->set_colormap = gdk_pixmap_real_set_colormap;
182 drawable_class->get_colormap = gdk_pixmap_real_get_colormap;
183 drawable_class->get_visual = gdk_pixmap_real_get_visual;
187 gdk_pixmap_finalize (GObject *object)
189 GdkPixmapObject *obj = (GdkPixmapObject *) object;
191 g_object_unref (G_OBJECT (obj->impl));
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 gdk_pixmap_create_gc (GdkDrawable *drawable,
200 GdkGCValuesMask mask)
202 return gdk_gc_new_with_values (((GdkPixmapObject *) drawable)->impl,
207 gdk_pixmap_draw_rectangle (GdkDrawable *drawable,
215 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
217 gdk_draw_rectangle (private->impl, gc, filled,
218 x, y, width, height);
222 gdk_pixmap_draw_arc (GdkDrawable *drawable,
232 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
234 gdk_draw_arc (private->impl, gc, filled,
236 width, height, angle1, angle2);
240 gdk_pixmap_draw_polygon (GdkDrawable *drawable,
246 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
248 gdk_draw_polygon (private->impl, gc, filled, points, npoints);
252 gdk_pixmap_draw_text (GdkDrawable *drawable,
260 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
262 gdk_draw_text (private->impl, font, gc,
263 x, y, text, text_length);
267 gdk_pixmap_draw_text_wc (GdkDrawable *drawable,
272 const GdkWChar *text,
275 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
277 gdk_draw_text_wc (private->impl, font, gc,
278 x, y, text, text_length);
282 gdk_pixmap_draw_drawable (GdkDrawable *drawable,
292 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
294 gdk_draw_drawable (private->impl, gc, src, xsrc, ysrc,
300 gdk_pixmap_draw_points (GdkDrawable *drawable,
305 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
307 gdk_draw_points (private->impl, gc, points, npoints);
311 gdk_pixmap_draw_segments (GdkDrawable *drawable,
316 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
318 gdk_draw_segments (private->impl, gc, segs, nsegs);
322 gdk_pixmap_draw_lines (GdkDrawable *drawable,
327 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
329 gdk_draw_lines (private->impl, gc, points, npoints);
333 gdk_pixmap_draw_glyphs (GdkDrawable *drawable,
338 PangoGlyphString *glyphs)
340 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
342 gdk_draw_glyphs (private->impl, gc, font, x, y, glyphs);
346 gdk_pixmap_draw_image (GdkDrawable *drawable,
356 GdkPixmapObject *private = (GdkPixmapObject *)drawable;
358 gdk_draw_image (private->impl, gc, image, xsrc, ysrc, xdest, ydest,
363 gdk_pixmap_real_get_size (GdkDrawable *drawable,
367 g_return_if_fail (GDK_IS_PIXMAP (drawable));
369 gdk_drawable_get_size (GDK_DRAWABLE (((GdkPixmapObject*)drawable)->impl),
374 gdk_pixmap_real_get_visual (GdkDrawable *drawable)
376 GdkColormap *colormap;
378 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
380 colormap = gdk_drawable_get_colormap (drawable);
381 return colormap ? gdk_colormap_get_visual (colormap) : NULL;
385 gdk_pixmap_real_get_depth (GdkDrawable *drawable)
389 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), 0);
391 depth = GDK_PIXMAP_OBJECT (drawable)->depth;
397 gdk_pixmap_real_set_colormap (GdkDrawable *drawable,
400 g_return_if_fail (GDK_IS_PIXMAP (drawable));
402 gdk_drawable_set_colormap (((GdkPixmapObject*)drawable)->impl, cmap);
406 gdk_pixmap_real_get_colormap (GdkDrawable *drawable)
408 g_return_val_if_fail (GDK_IS_PIXMAP (drawable), NULL);
410 return gdk_drawable_get_colormap (((GdkPixmapObject*)drawable)->impl);
413 #define PACKED_COLOR(c) ((((c)->red & 0xff) << 8) | ((c)->green & 0xff) | ((c)->blue >> 8))
416 gdk_pixmap_colormap_new_from_pixbuf (GdkColormap *colormap,
418 GdkColor *transparent_color,
422 GdkPixbuf *render_pixbuf;
425 pixmap = gdk_pixmap_new (NULL,
426 gdk_pixbuf_get_width (pixbuf),
427 gdk_pixbuf_get_height (pixbuf),
428 gdk_colormap_get_visual (colormap)->depth);
429 gdk_drawable_set_colormap (pixmap, colormap);
431 if (transparent_color)
433 guint32 packed_color = PACKED_COLOR (transparent_color);
434 render_pixbuf = gdk_pixbuf_composite_color_simple (pixbuf,
435 gdk_pixbuf_get_width (pixbuf),
436 gdk_pixbuf_get_height (pixbuf),
438 255, 16, packed_color, packed_color);
441 render_pixbuf = pixbuf;
443 tmp_gc = gdk_gc_new (pixmap);
444 gdk_pixbuf_render_to_drawable (render_pixbuf, pixmap, tmp_gc, 0, 0, 0, 0,
445 gdk_pixbuf_get_width (render_pixbuf),
446 gdk_pixbuf_get_height (render_pixbuf),
447 GDK_RGB_DITHER_NORMAL, 0, 0);
448 gdk_gc_unref (tmp_gc);
450 if (render_pixbuf != pixbuf)
451 gdk_pixbuf_unref (render_pixbuf);
454 gdk_pixbuf_render_pixmap_and_mask (pixbuf, NULL, mask, 128);
460 gdk_pixmap_colormap_create_from_xpm (GdkWindow *window,
461 GdkColormap *colormap,
463 GdkColor *transparent_color,
464 const gchar *filename)
469 g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
470 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
471 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
473 if (colormap == NULL)
474 colormap = gdk_drawable_get_colormap (window);
476 pixbuf = gdk_pixbuf_new_from_file (filename);
480 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
481 gdk_pixbuf_unref (pixbuf);
487 gdk_pixmap_create_from_xpm (GdkWindow *window,
489 GdkColor *transparent_color,
490 const gchar *filename)
492 return gdk_pixmap_colormap_create_from_xpm (window, NULL, mask,
493 transparent_color, filename);
497 gdk_pixmap_colormap_create_from_xpm_d (GdkWindow *window,
498 GdkColormap *colormap,
500 GdkColor *transparent_color,
506 g_return_val_if_fail (window != NULL || colormap != NULL, NULL);
507 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
508 g_return_val_if_fail (colormap == NULL || GDK_IS_COLORMAP (colormap), NULL);
510 if (colormap == NULL)
511 colormap = gdk_drawable_get_colormap (window);
513 pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)data);
517 pixmap = gdk_pixmap_colormap_new_from_pixbuf (colormap, mask, transparent_color, pixbuf);
518 gdk_pixbuf_unref (pixbuf);
524 gdk_pixmap_create_from_xpm_d (GdkWindow *window,
526 GdkColor *transparent_color,
529 return gdk_pixmap_colormap_create_from_xpm_d (window, NULL, mask,
530 transparent_color, data);