1 /* gtkcellrendererpixbuf.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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 #include "gtkcellrendererpixbuf.h"
28 static void gtk_cell_renderer_pixbuf_get_property (GObject *object,
32 const gchar *trailer);
33 static void gtk_cell_renderer_pixbuf_set_property (GObject *object,
37 const gchar *trailer);
38 static void gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *celltext);
39 static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class);
40 static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
44 static void gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
47 GdkRectangle *background_area,
48 GdkRectangle *cell_area,
49 GdkRectangle *expose_area,
60 gtk_cell_renderer_pixbuf_get_type (void)
62 static GtkType cell_pixbuf_type = 0;
64 if (!cell_pixbuf_type)
66 static const GTypeInfo cell_pixbuf_info =
68 sizeof (GtkCellRendererPixbufClass),
70 NULL, /* base_finalize */
71 (GClassInitFunc) gtk_cell_renderer_pixbuf_class_init,
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof (GtkCellRendererPixbuf),
76 (GInstanceInitFunc) gtk_cell_renderer_pixbuf_init,
79 cell_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererPixbuf", &cell_pixbuf_info, 0);
82 return cell_pixbuf_type;
86 gtk_cell_renderer_pixbuf_init (GtkCellRendererPixbuf *cellpixbuf)
91 gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
93 GObjectClass *object_class = G_OBJECT_CLASS (class);
94 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
96 object_class->get_property = gtk_cell_renderer_pixbuf_get_property;
97 object_class->set_property = gtk_cell_renderer_pixbuf_set_property;
99 cell_class->get_size = gtk_cell_renderer_pixbuf_get_size;
100 cell_class->render = gtk_cell_renderer_pixbuf_render;
102 g_object_class_install_property (object_class,
104 g_param_spec_object ("pixbuf",
106 _("The pixbuf to render."),
113 gtk_cell_renderer_pixbuf_get_property (GObject *object,
117 const gchar *trailer)
119 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
124 g_value_init (value, G_TYPE_OBJECT);
125 g_value_set_object (value, G_OBJECT (cellpixbuf->pixbuf));
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
135 gtk_cell_renderer_pixbuf_set_property (GObject *object,
139 const gchar *trailer)
142 GtkCellRendererPixbuf *cellpixbuf = GTK_CELL_RENDERER_PIXBUF (object);
147 pixbuf = GDK_PIXBUF (g_value_get_object (value));
148 g_object_ref (G_OBJECT (pixbuf));
149 if (cellpixbuf->pixbuf)
150 g_object_unref (G_OBJECT (cellpixbuf->pixbuf));
151 cellpixbuf->pixbuf = pixbuf;
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
160 gtk_cell_renderer_pixbuf_new (void)
162 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_pixbuf_get_type ()));
166 gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
171 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
174 *width = (gint) GTK_CELL_RENDERER (cellpixbuf)->xpad * 2 +
175 (cellpixbuf->pixbuf ? gdk_pixbuf_get_width (cellpixbuf->pixbuf) : 0);
178 *height = (gint) GTK_CELL_RENDERER (cellpixbuf)->ypad * 2 +
179 (cellpixbuf->pixbuf ? gdk_pixbuf_get_height (cellpixbuf->pixbuf) : 0);
183 gtk_cell_renderer_pixbuf_render (GtkCellRenderer *cell,
186 GdkRectangle *background_area,
187 GdkRectangle *cell_area,
188 GdkRectangle *expose_area,
192 GtkCellRendererPixbuf *cellpixbuf = (GtkCellRendererPixbuf *) cell;
200 pixbuf = cellpixbuf->pixbuf;
205 if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
206 bg_gc = widget->style->bg_gc [GTK_STATE_SELECTED];
208 bg_gc = widget->style->base_gc [GTK_STATE_NORMAL];
210 gdk_gc_set_clip_rectangle (bg_gc, cell_area);
212 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
213 pixels = gdk_pixbuf_get_pixels (pixbuf);
215 real_xoffset = GTK_CELL_RENDERER (cellpixbuf)->xalign * (cell_area->width - gdk_pixbuf_get_width (pixbuf) - (2 * GTK_CELL_RENDERER (cellpixbuf)->xpad));
216 real_xoffset = MAX (real_xoffset, 0) + GTK_CELL_RENDERER (cellpixbuf)->xpad;
217 real_yoffset = GTK_CELL_RENDERER (cellpixbuf)->yalign * (cell_area->height - gdk_pixbuf_get_height (pixbuf) - (2 * GTK_CELL_RENDERER (cellpixbuf)->ypad));
218 real_yoffset = MAX (real_yoffset, 0) + GTK_CELL_RENDERER (cellpixbuf)->ypad;
220 if (gdk_pixbuf_get_has_alpha (pixbuf))
221 gdk_draw_rgb_32_image (window,
223 cell_area->x + real_xoffset,
224 cell_area->y + real_yoffset,
225 gdk_pixbuf_get_width (pixbuf),
226 gdk_pixbuf_get_height (pixbuf),
227 GDK_RGB_DITHER_NORMAL,
231 gdk_draw_rgb_image (window,
233 cell_area->x + real_xoffset,
234 cell_area->y + real_yoffset,
235 gdk_pixbuf_get_width (pixbuf),
236 gdk_pixbuf_get_height (pixbuf),
237 GDK_RGB_DITHER_NORMAL,
241 gdk_gc_set_clip_rectangle (bg_gc, NULL);