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>
35 #include <modest-text-utils.h>
36 #include <modest-hbox-cell-renderer.h>
38 #define RENDERER_EXPAND_ATTRIBUTE "box-expand"
40 static GObjectClass *parent_class = NULL;
47 typedef struct _ModestHBoxCellRendererPrivate ModestHBoxCellRendererPrivate;
49 struct _ModestHBoxCellRendererPrivate
51 GList *renderers_list;
54 #define MODEST_HBOX_CELL_RENDERER_GET_PRIVATE(o) \
55 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_HBOX_CELL_RENDERER, ModestHBoxCellRendererPrivate))
57 /* static guint signals[LAST_SIGNAL] = {0}; */
59 /* static functions: GObject */
60 static void modest_hbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class);
61 static void modest_hbox_cell_renderer_finalize (GObject *object);
62 static void modest_hbox_cell_renderer_class_init (ModestHBoxCellRendererClass *klass);
64 /* static functions: GtkCellRenderer */
65 static void modest_hbox_cell_renderer_get_size (GtkCellRenderer *cell,
67 GdkRectangle *rectangle,
72 static void modest_hbox_cell_renderer_render (GtkCellRenderer *cell,
75 GdkRectangle *background_area,
76 GdkRectangle *cell_area,
77 GdkRectangle *expose_area,
78 GtkCellRendererState flags);
82 * modest_hbox_cell_renderer_new:
84 * Return value: a new #ModestHBoxCellRenderer instance implemented for Gtk+
87 modest_hbox_cell_renderer_new (void)
89 ModestHBoxCellRenderer *self = g_object_new (MODEST_TYPE_HBOX_CELL_RENDERER, NULL);
91 return GTK_CELL_RENDERER (self);
95 modest_hbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class)
97 ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (instance);
99 priv->renderers_list = NULL;
105 modest_hbox_cell_renderer_finalize (GObject *object)
107 ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (object);
109 if (priv->renderers_list != NULL) {
110 g_list_foreach (priv->renderers_list, (GFunc) g_object_unref, NULL);
111 g_list_free (priv->renderers_list);
112 priv->renderers_list = NULL;
115 (*parent_class->finalize) (object);
121 modest_hbox_cell_renderer_class_init (ModestHBoxCellRendererClass *klass)
123 GObjectClass *object_class;
124 GtkCellRendererClass *renderer_class;
126 parent_class = g_type_class_peek_parent (klass);
127 object_class = (GObjectClass*) klass;
128 renderer_class = (GtkCellRendererClass*) klass;
130 object_class->finalize = modest_hbox_cell_renderer_finalize;
131 renderer_class->get_size = modest_hbox_cell_renderer_get_size;
132 renderer_class->render = modest_hbox_cell_renderer_render;
134 g_type_class_add_private (object_class, sizeof (ModestHBoxCellRendererPrivate));
140 modest_hbox_cell_renderer_get_type (void)
142 static GType type = 0;
144 if (G_UNLIKELY(type == 0))
146 static const GTypeInfo info =
148 sizeof (ModestHBoxCellRendererClass),
149 NULL, /* base_init */
150 NULL, /* base_finalize */
151 (GClassInitFunc) modest_hbox_cell_renderer_class_init, /* class_init */
152 NULL, /* class_finalize */
153 NULL, /* class_data */
154 sizeof (ModestHBoxCellRenderer),
156 modest_hbox_cell_renderer_instance_init /* instance_init */
159 type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
160 "ModestHBoxCellRenderer",
170 * modest_hbox_cell_renderer_append:
171 * @hbox_renderer: a #ModestHBoxCellRenderer
172 * @cell: a #GtkCellRenderer
174 * Appends @cell to the end of the list of renderers shown in @hbox_renderer
177 modest_hbox_cell_renderer_append (ModestHBoxCellRenderer *hbox_renderer,
178 GtkCellRenderer *cell,
181 ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (hbox_renderer);
183 priv->renderers_list = g_list_append (priv->renderers_list, cell);
184 g_object_set_data (G_OBJECT (cell), RENDERER_EXPAND_ATTRIBUTE, GINT_TO_POINTER (expand));
186 #if GLIB_CHECK_VERSION(2, 10, 0) /* g_object_ref_sink() was added in glib 2.10: */
187 g_object_ref_sink (G_OBJECT (cell));
189 g_object_ref (G_OBJECT (cell));
190 gtk_object_sink (GTK_OBJECT (cell));
195 modest_hbox_cell_renderer_get_size (GtkCellRenderer *cell,
197 GdkRectangle *rectangle,
203 gint calc_width, calc_height;
204 gint full_width, full_height;
206 gfloat xalign, yalign;
208 ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (cell);
213 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
214 gint renderer_width, renderer_height;
215 GtkCellRenderer *renderer = (GtkCellRenderer *) node->data;
217 gtk_cell_renderer_get_size (renderer, widget, NULL, NULL, NULL,
218 &renderer_width, &renderer_height);
219 if ((renderer_width > 0)&&(renderer_height > 0)) {
220 calc_height = MAX (calc_height, renderer_height);
221 calc_width += renderer_width;
232 full_width = (gint) xpad * 2 + calc_width;
233 full_height = (gint) ypad * 2 + calc_height;
235 if (rectangle && calc_width > 0 && calc_height > 0) {
237 *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
238 (1.0 - xalign) : xalign) *
239 (rectangle->width - full_width));
240 *x_offset = MAX (*x_offset, 0);
243 *y_offset = ((yalign) *
244 (rectangle->height - full_height));
245 *y_offset = MAX (*y_offset, 0);
257 *height = full_height;
261 modest_hbox_cell_renderer_render (GtkCellRenderer *cell,
264 GdkRectangle *background_area,
265 GdkRectangle *cell_area,
266 GdkRectangle *expose_area,
267 GtkCellRendererState flags)
269 ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (cell);
270 gint nvis_children = 0;
271 gint nexpand_children = 0;
272 GtkTextDirection direction;
274 GtkCellRenderer *child;
277 direction = gtk_widget_get_direction (widget);
279 nexpand_children = 0;
281 /* Counts visible and expandable children cell renderers */
282 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
283 gboolean visible, expand;
284 child = (GtkCellRenderer *) node->data;
285 g_object_get (G_OBJECT (child), "visible", &visible, NULL);
286 expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
291 nexpand_children += 1;
295 if (nvis_children > 0) {
298 GdkRectangle child_alloc;
300 if (nexpand_children > 0) {
303 /* retrieve the requisition of the children cell renderers */
304 modest_hbox_cell_renderer_get_size (cell, widget, NULL, NULL, NULL, &(req.width), &(req.height));
305 width = cell_area->width - req.width;
306 extra = width / nexpand_children;
312 g_object_get (cell, "xpad", &x_pad, "ypad", &y_pad, NULL);
313 x = cell_area->x + x_pad;
314 child_alloc.y = cell_area->y + y_pad;
315 child_alloc.height = MAX (1, cell_area->height - y_pad * 2);
317 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
318 gboolean visible, expand;
320 child = (GtkCellRenderer *) node->data;
321 g_object_get (G_OBJECT (child), "visible", &visible, NULL);
322 expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
325 GtkRequisition child_req;
326 gint child_xpad, child_ypad;
327 GdkRectangle child_expose_area;
328 gtk_cell_renderer_get_size (child, widget, NULL, NULL, NULL, &(child_req.width), &(child_req.height));
329 g_object_get (child, "xpad", &child_xpad, "ypad", &child_ypad, NULL);
332 if (nexpand_children == 1)
333 child_req.width += width;
335 child_req.width += extra;
336 nexpand_children -= 1;
340 child_alloc.width = MAX (1, child_req.width);
343 if (direction == GTK_TEXT_DIR_RTL)
344 child_alloc.x = cell_area->x + cell_area->width - (child_alloc.x - cell_area->x) - child_alloc.width;
346 if (gdk_rectangle_intersect (&child_alloc, expose_area, &child_expose_area))
347 gtk_cell_renderer_render (child, window, widget, background_area, &child_alloc, &child_expose_area, flags);
348 x += child_req.width;