1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* e-cell-renderer-color.c
4 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "e-cell-renderer-color.h"
28 #include <glib/gi18n-lib.h>
30 #define E_CELL_RENDERER_COLOR_GET_PRIVATE(obj) \
31 (G_TYPE_INSTANCE_GET_PRIVATE \
32 ((obj), E_TYPE_CELL_RENDERER_COLOR, ECellRendererColorPrivate))
39 struct _ECellRendererColorPrivate {
45 e_cell_renderer_color,
46 GTK_TYPE_CELL_RENDERER)
49 cell_renderer_color_get_size (GtkCellRenderer *cell,
51 const GdkRectangle *cell_area,
57 gint color_width = 16;
58 gint color_height = 16;
67 cell, "xalign", &xalign, "yalign", &yalign,
68 "xpad", &xpad, "ypad", &ypad, NULL);
70 calc_width = (gint) xpad * 2 + color_width;
71 calc_height = (gint) ypad * 2 + color_height;
73 if (cell_area && color_width > 0 && color_height > 0) {
75 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
76 (1.0 - xalign) : xalign) *
77 (cell_area->width - calc_width));
78 *x_offset = MAX (*x_offset, 0);
83 (cell_area->height - calc_height));
84 *y_offset = MAX (*y_offset, 0);
87 if (x_offset) *x_offset = 0;
88 if (y_offset) *y_offset = 0;
95 *height = calc_height;
99 cell_renderer_color_render (GtkCellRenderer *cell,
102 const GdkRectangle *background_area,
103 const GdkRectangle *cell_area,
104 GtkCellRendererState flags)
106 ECellRendererColorPrivate *priv;
107 GdkRectangle pix_rect;
108 GdkRectangle draw_rect;
113 priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cell);
115 if (priv->color == NULL)
118 cell_renderer_color_get_size (
119 cell, widget, cell_area,
120 &pix_rect.x, &pix_rect.y,
121 &pix_rect.width, &pix_rect.height);
123 g_object_get (cell, "xpad", &xpad, "ypad", &ypad, NULL);
125 pix_rect.x += cell_area->x + xpad;
126 pix_rect.y += cell_area->y + ypad;
127 pix_rect.width -= xpad * 2;
128 pix_rect.height -= ypad * 2;
130 if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
133 rgba.red = priv->color->red / 65535.0;
134 rgba.green = priv->color->green / 65535.0;
135 rgba.blue = priv->color->blue / 65535.0;
138 gdk_cairo_set_source_rgba (cr, &rgba);
139 cairo_rectangle (cr, pix_rect.x, pix_rect.y, draw_rect.width, draw_rect.height);
145 cell_renderer_color_set_property (GObject *object,
150 ECellRendererColorPrivate *priv;
152 priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
154 switch (property_id) {
156 if (priv->color != NULL)
157 gdk_color_free (priv->color);
158 priv->color = g_value_dup_boxed (value);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
166 cell_renderer_color_get_property (GObject *object,
171 ECellRendererColorPrivate *priv;
173 priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
175 switch (property_id) {
177 g_value_set_boxed (value, priv->color);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185 cell_renderer_color_finalize (GObject *object)
187 ECellRendererColorPrivate *priv;
189 priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
191 if (priv->color != NULL)
192 gdk_color_free (priv->color);
194 /* Chain up to parent's finalize() method. */
195 G_OBJECT_CLASS (e_cell_renderer_color_parent_class)->finalize (object);
199 e_cell_renderer_color_class_init (ECellRendererColorClass *class)
201 GObjectClass *object_class;
202 GtkCellRendererClass *cell_class;
204 g_type_class_add_private (class, sizeof (ECellRendererColorPrivate));
206 object_class = G_OBJECT_CLASS (class);
207 object_class->set_property = cell_renderer_color_set_property;
208 object_class->get_property = cell_renderer_color_get_property;
209 object_class->finalize = cell_renderer_color_finalize;
211 cell_class = GTK_CELL_RENDERER_CLASS (class);
212 cell_class->get_size = cell_renderer_color_get_size;
213 cell_class->render = cell_renderer_color_render;
215 g_object_class_install_property (
221 "The color to render",
227 e_cell_renderer_color_init (ECellRendererColor *cellcolor)
229 cellcolor->priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cellcolor);
231 g_object_set (cellcolor, "xpad", 4, NULL);
235 * e_cell_renderer_color_new:
240 e_cell_renderer_color_new (void)
242 return g_object_new (E_TYPE_CELL_RENDERER_COLOR, NULL);