1 /* gtkcellrenderertextpixbuf.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 "gtkcellrenderertextpixbuf.h"
35 static void gtk_cell_renderer_text_pixbuf_get_property (GObject *object,
39 static void gtk_cell_renderer_text_pixbuf_set_property (GObject *object,
43 static void gtk_cell_renderer_text_pixbuf_init (GtkCellRendererTextPixbuf *celltextpixbuf);
44 static void gtk_cell_renderer_text_pixbuf_class_init (GtkCellRendererTextPixbufClass *class);
45 static void gtk_cell_renderer_text_pixbuf_get_size (GtkCellRenderer *cell,
47 GdkRectangle *cell_area,
52 static void gtk_cell_renderer_text_pixbuf_render (GtkCellRenderer *cell,
55 GdkRectangle *background_area,
56 GdkRectangle *cell_area,
57 GdkRectangle *expose_area,
61 static GtkCellRendererTextClass *parent_class = NULL;
65 gtk_cell_renderer_text_pixbuf_get_type (void)
67 static GtkType cell_text_pixbuf_type = 0;
69 if (!cell_text_pixbuf_type)
71 static const GTypeInfo cell_text_pixbuf_info =
73 sizeof (GtkCellRendererTextPixbufClass),
75 NULL, /* base_finalize */
76 (GClassInitFunc) gtk_cell_renderer_text_pixbuf_class_init,
77 NULL, /* class_finalize */
78 NULL, /* class_data */
79 sizeof (GtkCellRendererTextPixbuf),
81 (GInstanceInitFunc) gtk_cell_renderer_text_pixbuf_init,
84 cell_text_pixbuf_type = g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT, "GtkCellRendererTextPixbuf", &cell_text_pixbuf_info, 0);
87 return cell_text_pixbuf_type;
91 gtk_cell_renderer_text_pixbuf_init (GtkCellRendererTextPixbuf *celltextpixbuf)
93 celltextpixbuf->pixbuf = GTK_CELL_RENDERER_PIXBUF (gtk_cell_renderer_pixbuf_new ());
94 celltextpixbuf->pixbuf_pos = GTK_POS_LEFT;
98 gtk_cell_renderer_text_pixbuf_class_init (GtkCellRendererTextPixbufClass *class)
100 GObjectClass *object_class = G_OBJECT_CLASS (class);
101 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
103 parent_class = g_type_class_peek_parent (class);
105 object_class->get_property = gtk_cell_renderer_text_pixbuf_get_property;
106 object_class->set_property = gtk_cell_renderer_text_pixbuf_set_property;
108 cell_class->get_size = gtk_cell_renderer_text_pixbuf_get_size;
109 cell_class->render = gtk_cell_renderer_text_pixbuf_render;
111 g_object_class_install_property (object_class,
113 g_param_spec_int ("pixbuf_pos",
114 _("Pixbuf location"),
115 _("The relative location of the pixbuf to the text."),
122 g_object_class_install_property (object_class,
124 g_param_spec_object ("pixbuf",
126 _("The pixbuf to render."),
131 g_object_class_install_property (object_class,
133 g_param_spec_float ("pixbuf_xalign",
135 _("The x-align of the pixbuf."),
142 g_object_class_install_property (object_class,
144 g_param_spec_float ("pixbuf_yalign",
146 _("The y-align of the pixbuf."),
153 g_object_class_install_property (object_class,
155 g_param_spec_uint ("pixbuf_xpad",
157 _("The xpad of the pixbuf."),
164 g_object_class_install_property (object_class,
166 g_param_spec_uint ("pixbuf_ypad",
168 _("The ypad of the pixbuf."),
177 gtk_cell_renderer_text_pixbuf_get_property (GObject *object,
182 GtkCellRendererTextPixbuf *celltextpixbuf = GTK_CELL_RENDERER_TEXT_PIXBUF (object);
186 case PROP_PIXBUF_POS:
187 g_value_set_int (value, celltextpixbuf->pixbuf_pos);
190 g_object_get_property (G_OBJECT (celltextpixbuf->pixbuf),
194 case PROP_PIXBUF_XALIGN:
195 g_object_get_property (G_OBJECT (celltextpixbuf->pixbuf),
199 case PROP_PIXBUF_YALIGN:
200 g_object_get_property (G_OBJECT (celltextpixbuf->pixbuf),
204 case PROP_PIXBUF_XPAD:
205 g_object_get_property (G_OBJECT (celltextpixbuf->pixbuf),
209 case PROP_PIXBUF_YPAD:
210 g_object_get_property (G_OBJECT (celltextpixbuf->pixbuf),
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
221 gtk_cell_renderer_text_pixbuf_set_property (GObject *object,
226 GtkCellRendererTextPixbuf *celltextpixbuf = GTK_CELL_RENDERER_TEXT_PIXBUF (object);
231 g_object_set_property (G_OBJECT (celltextpixbuf->pixbuf),
234 g_object_notify (G_OBJECT(object), "pixbuf");
236 case PROP_PIXBUF_POS:
237 celltextpixbuf->pixbuf_pos = g_value_get_int (value);
238 g_object_notify (G_OBJECT(object), "pixbuf_pos");
240 case PROP_PIXBUF_XALIGN:
241 g_object_set_property (G_OBJECT (celltextpixbuf->pixbuf),
244 g_object_notify (G_OBJECT(object), "pixbuf_xalign");
246 case PROP_PIXBUF_YALIGN:
247 g_object_set_property (G_OBJECT (celltextpixbuf->pixbuf),
250 g_object_notify (G_OBJECT(object), "pixbuf_yalign");
252 case PROP_PIXBUF_XPAD:
253 g_object_set_property (G_OBJECT (celltextpixbuf->pixbuf),
256 g_object_notify (G_OBJECT(object), "pixbuf_xpad");
258 case PROP_PIXBUF_YPAD:
259 g_object_set_property (G_OBJECT (celltextpixbuf->pixbuf),
262 g_object_notify (G_OBJECT(object), "pixbuf_ypad");
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
271 * gtk_cell_renderer_text_pixbuf_new:
273 * Creates a new #GtkCellRendererTextPixbuf. Adjust rendering
274 * parameters using object properties. Object properties can be set
275 * globally (with g_object_set()). Also, with #GtkTreeViewColumn, you
276 * can bind a property to a value in a #GtkTreeModel. For example, you
277 * can bind the "text" property on the cell renderer to a string value
278 * in the model, thus rendering a different string in each row of the
281 * Return value: the new cell renderer
284 gtk_cell_renderer_text_pixbuf_new (void)
286 return GTK_CELL_RENDERER (gtk_type_new (gtk_cell_renderer_text_pixbuf_get_type ()));
289 typedef void (* CellSizeFunc) (GtkCellRenderer *cell,
291 GdkRectangle *rectangle,
296 typedef void (* CellRenderFunc) (GtkCellRenderer *cell,
299 GdkRectangle *background_area,
300 GdkRectangle *cell_area,
301 GdkRectangle *expose_area,
305 gtk_cell_renderer_text_pixbuf_get_size (GtkCellRenderer *cell,
307 GdkRectangle *cell_area,
313 GtkCellRendererTextPixbuf *celltextpixbuf = (GtkCellRendererTextPixbuf *)cell;
321 (* GTK_CELL_RENDERER_CLASS (parent_class)->get_size) (cell, widget, NULL, NULL, NULL, &text_width, &text_height);
322 (* GTK_CELL_RENDERER_CLASS (G_OBJECT_GET_CLASS (celltextpixbuf->pixbuf))->get_size) (GTK_CELL_RENDERER (celltextpixbuf->pixbuf),
327 if (celltextpixbuf->pixbuf_pos == GTK_POS_LEFT ||
328 celltextpixbuf->pixbuf_pos == GTK_POS_RIGHT)
330 calc_width = pixbuf_width + text_width;
331 calc_height = MAX (pixbuf_height, text_height);
335 calc_width = MAX (pixbuf_width, text_width);
336 calc_height = pixbuf_height + text_height;
342 *height = calc_height;
348 *x_offset = cell->xalign * (cell_area->width - calc_width - (2 * cell->xpad));
349 *x_offset = MAX (*x_offset, 0) + cell->xpad;
353 *y_offset = cell->yalign * (cell_area->height - calc_height - (2 * cell->ypad));
354 *y_offset = MAX (*y_offset, 0) + cell->ypad;
360 gtk_cell_renderer_text_pixbuf_render (GtkCellRenderer *cell,
363 GdkRectangle *background_area,
364 GdkRectangle *cell_area,
365 GdkRectangle *expose_area,
369 GtkCellRendererTextPixbuf *celltextpixbuf = (GtkCellRendererTextPixbuf *) cell;
370 CellSizeFunc size_func1, size_func2;
371 CellRenderFunc render_func1, render_func2;
372 GtkCellRenderer *cell1, *cell2;
375 GdkRectangle real_cell_area;
377 if (celltextpixbuf->pixbuf_pos == GTK_POS_LEFT ||
378 celltextpixbuf->pixbuf_pos == GTK_POS_TOP)
380 size_func1 = GTK_CELL_RENDERER_CLASS (G_OBJECT_GET_CLASS (celltextpixbuf->pixbuf))->get_size;
381 render_func1 = GTK_CELL_RENDERER_CLASS (G_OBJECT_GET_CLASS (celltextpixbuf->pixbuf))->render;
382 cell1 = GTK_CELL_RENDERER (celltextpixbuf->pixbuf);
384 size_func2 = GTK_CELL_RENDERER_CLASS (parent_class)->get_size;
385 render_func2 = GTK_CELL_RENDERER_CLASS (parent_class)->render;
390 size_func1 = GTK_CELL_RENDERER_CLASS (parent_class)->get_size;
391 render_func1 = GTK_CELL_RENDERER_CLASS (parent_class)->render;
394 size_func2 = GTK_CELL_RENDERER_CLASS (G_OBJECT_GET_CLASS (celltextpixbuf->pixbuf))->get_size;
395 render_func2 = GTK_CELL_RENDERER_CLASS (G_OBJECT_GET_CLASS (celltextpixbuf->pixbuf))->render;
396 cell2 = GTK_CELL_RENDERER (celltextpixbuf->pixbuf);
399 (size_func1) (cell1, widget, NULL, NULL, NULL, &tmp_width, &tmp_height);
401 real_cell_area.x = cell_area->x;
402 real_cell_area.y = cell_area->y;
404 if (celltextpixbuf->pixbuf_pos == GTK_POS_LEFT ||
405 celltextpixbuf->pixbuf_pos == GTK_POS_RIGHT)
407 real_cell_area.width = MIN (tmp_width, cell_area->width);
408 real_cell_area.height = cell_area->height;
412 real_cell_area.height = MIN (tmp_height, cell_area->height);
413 real_cell_area.width = cell_area->width;
416 (render_func1) (cell1,
424 if (celltextpixbuf->pixbuf_pos == GTK_POS_LEFT ||
425 celltextpixbuf->pixbuf_pos == GTK_POS_RIGHT)
427 real_cell_area.x = real_cell_area.x + real_cell_area.width;
428 real_cell_area.width = cell_area->width - real_cell_area.width;
432 real_cell_area.y = real_cell_area.y + real_cell_area.height;
433 real_cell_area.height = cell_area->height - real_cell_area.height;
436 (render_func2 ) (cell2,