1 /* Copyright (c) 2007, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <glib/gi18n-lib.h>
34 #include <gtk/gtkwidget.h>
36 #include <modest-text-utils.h>
37 #include <modest-vbox-cell-renderer.h>
39 #define RENDERER_EXPAND_ATTRIBUTE "box-expand"
41 static GObjectClass *parent_class = NULL;
48 typedef struct _ModestVBoxCellRendererPrivate ModestVBoxCellRendererPrivate;
50 struct _ModestVBoxCellRendererPrivate
52 GList *renderers_list;
55 #define MODEST_VBOX_CELL_RENDERER_GET_PRIVATE(o) \
56 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_VBOX_CELL_RENDERER, ModestVBoxCellRendererPrivate))
58 /* static guint signals[LAST_SIGNAL] = {0}; */
60 /* static functions: GObject */
61 static void modest_vbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class);
62 static void modest_vbox_cell_renderer_finalize (GObject *object);
63 static void modest_vbox_cell_renderer_class_init (ModestVBoxCellRendererClass *klass);
65 /* static functions: GtkCellRenderer */
66 static void modest_vbox_cell_renderer_get_size (GtkCellRenderer *cell,
68 GdkRectangle *rectangle,
73 static void modest_vbox_cell_renderer_render (GtkCellRenderer *cell,
76 GdkRectangle *background_area,
77 GdkRectangle *cell_area,
78 GdkRectangle *expose_area,
79 GtkCellRendererState flags);
83 * modest_vbox_cell_renderer_new:
85 * Return value: a new #ModestVBoxCellRenderer instance implemented for Gtk+
88 modest_vbox_cell_renderer_new (void)
90 ModestVBoxCellRenderer *self = g_object_new (MODEST_TYPE_VBOX_CELL_RENDERER, NULL);
92 return GTK_CELL_RENDERER (self);
96 modest_vbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class)
98 ModestVBoxCellRendererPrivate *priv = MODEST_VBOX_CELL_RENDERER_GET_PRIVATE (instance);
100 priv->renderers_list = NULL;
106 modest_vbox_cell_renderer_finalize (GObject *object)
108 ModestVBoxCellRendererPrivate *priv = MODEST_VBOX_CELL_RENDERER_GET_PRIVATE (object);
110 if (priv->renderers_list != NULL) {
111 g_list_foreach (priv->renderers_list, (GFunc) g_object_unref, NULL);
112 g_list_free (priv->renderers_list);
113 priv->renderers_list = NULL;
116 (*parent_class->finalize) (object);
122 modest_vbox_cell_renderer_class_init (ModestVBoxCellRendererClass *klass)
124 GObjectClass *object_class;
125 GtkCellRendererClass *renderer_class;
127 parent_class = g_type_class_peek_parent (klass);
128 object_class = (GObjectClass*) klass;
129 renderer_class = (GtkCellRendererClass*) klass;
131 object_class->finalize = modest_vbox_cell_renderer_finalize;
132 renderer_class->get_size = modest_vbox_cell_renderer_get_size;
133 renderer_class->render = modest_vbox_cell_renderer_render;
135 g_type_class_add_private (object_class, sizeof (ModestVBoxCellRendererPrivate));
141 modest_vbox_cell_renderer_get_type (void)
143 static GType type = 0;
145 if (G_UNLIKELY(type == 0))
147 static const GTypeInfo info =
149 sizeof (ModestVBoxCellRendererClass),
150 NULL, /* base_init */
151 NULL, /* base_finalize */
152 (GClassInitFunc) modest_vbox_cell_renderer_class_init, /* class_init */
153 NULL, /* class_finalize */
154 NULL, /* class_data */
155 sizeof (ModestVBoxCellRenderer),
157 modest_vbox_cell_renderer_instance_init /* instance_init */
160 type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
161 "ModestVBoxCellRenderer",
171 * modest_vbox_cell_renderer_append:
172 * @vbox_renderer: a #ModestVBoxCellRenderer
173 * @cell: a #GtkCellRenderer
175 * Appends @cell to the end of the list of renderers shown in @vbox_renderer
178 modest_vbox_cell_renderer_append (ModestVBoxCellRenderer *vbox_renderer,
179 GtkCellRenderer *cell,
182 ModestVBoxCellRendererPrivate *priv = MODEST_VBOX_CELL_RENDERER_GET_PRIVATE (vbox_renderer);
184 priv->renderers_list = g_list_append (priv->renderers_list, cell);
185 g_object_set_data (G_OBJECT (cell), RENDERER_EXPAND_ATTRIBUTE, GINT_TO_POINTER (expand));
187 #if GLIB_CHECK_VERSION(2, 10, 0) /* g_object_ref_sink() was added in glib 2.10: */
188 g_object_ref_sink (G_OBJECT (cell));
190 g_object_ref (G_OBJECT (cell));
191 gtk_object_sink (GTK_OBJECT (cell));
196 modest_vbox_cell_renderer_get_size (GtkCellRenderer *cell,
198 GdkRectangle *rectangle,
204 gint calc_width, calc_height;
205 gint full_width, full_height;
207 ModestVBoxCellRendererPrivate *priv = MODEST_VBOX_CELL_RENDERER_GET_PRIVATE (cell);
212 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
213 gint renderer_width, renderer_height;
214 GtkCellRenderer *renderer = (GtkCellRenderer *) node->data;
216 gtk_cell_renderer_get_size (renderer, widget, NULL, NULL, NULL,
217 &renderer_width, &renderer_height);
218 if ((renderer_width > 0)&&(renderer_height > 0)) {
219 calc_width = MAX (calc_width, renderer_width);
220 calc_height += renderer_height;
224 full_width = (gint) cell->xpad * 2 + calc_width;
225 full_height = (gint) cell->ypad * 2 + calc_height;
227 if (rectangle && calc_width > 0 && calc_height > 0) {
229 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
230 (1.0 - cell->xalign) : cell->xalign) *
231 (rectangle->width - full_width));
232 *x_offset = MAX (*x_offset, 0);
235 *y_offset = ((cell->yalign) *
236 (rectangle->height - full_height));
237 *y_offset = MAX (*y_offset, 0);
249 *height = full_height;
253 modest_vbox_cell_renderer_render (GtkCellRenderer *cell,
256 GdkRectangle *background_area,
257 GdkRectangle *cell_area,
258 GdkRectangle *expose_area,
259 GtkCellRendererState flags)
261 ModestVBoxCellRendererPrivate *priv = MODEST_VBOX_CELL_RENDERER_GET_PRIVATE (cell);
262 gint nvis_children = 0;
263 gint nexpand_children = 0;
264 GtkTextDirection direction;
266 GtkCellRenderer *child;
269 direction = gtk_widget_get_direction (widget);
271 nexpand_children = 0;
274 /* Counts visible and expandable children cell renderers */
275 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
276 gboolean visible, expand;
277 child = (GtkCellRenderer *) node->data;
278 g_object_get (G_OBJECT (child), "visible", &visible, NULL);
279 expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
284 nexpand_children += 1;
288 if (nvis_children > 0) {
291 GdkRectangle child_alloc;
293 if (nexpand_children > 0) {
296 modest_vbox_cell_renderer_get_size (cell, widget, NULL, NULL, NULL, &(req.width), &(req.height));
297 height = cell_area->height - req.height;
298 extra = height / nexpand_children;
304 g_object_get (cell, "xpad", &x_pad, "ypad", &y_pad, NULL);
305 y = cell_area->y + y_pad;
306 child_alloc.x = cell_area->x + x_pad;
307 child_alloc.width = MAX (1, cell_area->width - x_pad * 2);
309 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
310 gboolean visible, expand;
312 child = (GtkCellRenderer *) node->data;
313 g_object_get (G_OBJECT (child), "visible", &visible, NULL);
314 expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
317 GtkRequisition child_req;
318 gint child_xpad, child_ypad;
319 GdkRectangle child_expose_area;
321 gtk_cell_renderer_get_size (child, widget, NULL, NULL, NULL, &(child_req.width), &(child_req.height));
322 g_object_get (child, "xpad", &child_xpad, "ypad", &child_ypad, NULL);
325 if (nexpand_children == 1)
326 child_req.height += height;
328 child_req.height += extra;
329 nexpand_children -= 1;
333 child_alloc.height = MAX (1, child_req.height);
336 if (gdk_rectangle_intersect (&child_alloc, expose_area, &child_expose_area))
337 gtk_cell_renderer_render (child, window, widget, background_area, &child_alloc, &child_expose_area, flags);
338 y += child_req.height;