Assamese translation updated
[platform/upstream/evolution-data-server.git] / libedataserverui / e-cell-renderer-color.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* e-cell-renderer-color.c
3  *
4  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
5  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "e-cell-renderer-color.h"
26
27 #include <string.h>
28 #include <glib/gi18n-lib.h>
29
30 #define E_CELL_RENDERER_COLOR_GET_PRIVATE(obj) \
31         (G_TYPE_INSTANCE_GET_PRIVATE \
32         ((obj), E_TYPE_CELL_RENDERER_COLOR, ECellRendererColorPrivate))
33
34 enum {
35         PROP_0,
36         PROP_COLOR
37 };
38
39 struct _ECellRendererColorPrivate {
40         GdkColor *color;
41 };
42
43 G_DEFINE_TYPE (
44         ECellRendererColor,
45         e_cell_renderer_color,
46         GTK_TYPE_CELL_RENDERER)
47
48 static void
49 cell_renderer_color_get_size (GtkCellRenderer *cell,
50                               GtkWidget *widget,
51                               const GdkRectangle *cell_area,
52                               gint *x_offset,
53                               gint *y_offset,
54                               gint *width,
55                               gint *height)
56 {
57         gint color_width  = 16;
58         gint color_height = 16;
59         gint calc_width;
60         gint calc_height;
61         gfloat xalign;
62         gfloat yalign;
63         guint xpad;
64         guint ypad;
65
66         g_object_get (
67                 cell, "xalign", &xalign, "yalign", &yalign,
68                 "xpad", &xpad, "ypad", &ypad, NULL);
69
70         calc_width  = (gint) xpad * 2 + color_width;
71         calc_height = (gint) ypad * 2 + color_height;
72
73         if (cell_area && color_width > 0 && color_height > 0) {
74                 if (x_offset) {
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);
79                 }
80
81                 if (y_offset) {
82                         *y_offset =(yalign *
83                                 (cell_area->height - calc_height));
84                         *y_offset = MAX (*y_offset, 0);
85                 }
86         } else {
87                 if (x_offset) *x_offset = 0;
88                 if (y_offset) *y_offset = 0;
89         }
90
91         if (width)
92                 *width = calc_width;
93
94         if (height)
95                 *height = calc_height;
96 }
97
98 static void
99 cell_renderer_color_render (GtkCellRenderer *cell,
100                             cairo_t *cr,
101                             GtkWidget *widget,
102                             const GdkRectangle *background_area,
103                             const GdkRectangle *cell_area,
104                             GtkCellRendererState flags)
105 {
106         ECellRendererColorPrivate *priv;
107         GdkRectangle pix_rect;
108         GdkRectangle draw_rect;
109         GdkRGBA rgba;
110         guint xpad;
111         guint ypad;
112
113         priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cell);
114
115         if (priv->color == NULL)
116                 return;
117
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);
122
123         g_object_get (cell, "xpad", &xpad, "ypad", &ypad, NULL);
124
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;
129
130         if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
131                 return;
132
133         rgba.red = priv->color->red / 65535.0;
134         rgba.green = priv->color->green / 65535.0;
135         rgba.blue = priv->color->blue / 65535.0;
136         rgba.alpha = 1.0;
137
138         gdk_cairo_set_source_rgba (cr, &rgba);
139         cairo_rectangle (cr, pix_rect.x, pix_rect.y, draw_rect.width, draw_rect.height);
140
141         cairo_fill (cr);
142 }
143
144 static void
145 cell_renderer_color_set_property (GObject *object,
146                                   guint property_id,
147                                   const GValue *value,
148                                   GParamSpec *pspec)
149 {
150         ECellRendererColorPrivate *priv;
151
152         priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
153
154         switch (property_id) {
155                 case PROP_COLOR:
156                         if (priv->color != NULL)
157                                 gdk_color_free (priv->color);
158                         priv->color = g_value_dup_boxed (value);
159                         return;
160         }
161
162         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 }
164
165 static void
166 cell_renderer_color_get_property (GObject *object,
167                                   guint property_id,
168                                   GValue *value,
169                                   GParamSpec *pspec)
170 {
171         ECellRendererColorPrivate *priv;
172
173         priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
174
175         switch (property_id) {
176                 case PROP_COLOR:
177                         g_value_set_boxed (value, priv->color);
178                         return;
179         }
180
181         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
182 }
183
184 static void
185 cell_renderer_color_finalize (GObject *object)
186 {
187         ECellRendererColorPrivate *priv;
188
189         priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (object);
190
191         if (priv->color != NULL)
192                 gdk_color_free (priv->color);
193
194         /* Chain up to parent's finalize() method. */
195         G_OBJECT_CLASS (e_cell_renderer_color_parent_class)->finalize (object);
196 }
197
198 static void
199 e_cell_renderer_color_class_init (ECellRendererColorClass *class)
200 {
201         GObjectClass *object_class;
202         GtkCellRendererClass *cell_class;
203
204         g_type_class_add_private (class, sizeof (ECellRendererColorPrivate));
205
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;
210
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;
214
215         g_object_class_install_property (
216                 object_class,
217                 PROP_COLOR,
218                 g_param_spec_boxed (
219                         "color",
220                         "Color Info",
221                         "The color to render",
222                         GDK_TYPE_COLOR,
223                         G_PARAM_READWRITE));
224 }
225
226 static void
227 e_cell_renderer_color_init (ECellRendererColor *cellcolor)
228 {
229         cellcolor->priv = E_CELL_RENDERER_COLOR_GET_PRIVATE (cellcolor);
230
231         g_object_set (cellcolor, "xpad", 4, NULL);
232 }
233
234 /**
235  * e_cell_renderer_color_new:
236  *
237  * Since: 2.22
238  **/
239 GtkCellRenderer *
240 e_cell_renderer_color_new (void)
241 {
242         return g_object_new (E_TYPE_CELL_RENDERER_COLOR, NULL);
243 }