* src/widgets/modest-header-view-render.c:
[modest] / src / widgets / modest-hbox-cell-renderer.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <config.h>
31
32 #include <glib/gi18n-lib.h>
33
34 #include <gtk/gtkwidget.h>
35
36 #include <modest-text-utils.h>
37 #include <modest-hbox-cell-renderer.h>
38
39 #define RENDERER_EXPAND_ATTRIBUTE "box-expand"
40
41 static GObjectClass *parent_class = NULL;
42
43 /* /\* signals *\/ */
44 /* enum { */
45 /*      LAST_SIGNAL */
46 /* }; */
47
48 typedef struct _ModestHBoxCellRendererPrivate ModestHBoxCellRendererPrivate;
49
50 struct _ModestHBoxCellRendererPrivate
51 {
52         GList *renderers_list;
53 };
54
55 #define MODEST_HBOX_CELL_RENDERER_GET_PRIVATE(o)        \
56         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_HBOX_CELL_RENDERER, ModestHBoxCellRendererPrivate))
57
58 /* static guint signals[LAST_SIGNAL] = {0}; */
59
60 /* static functions: GObject */
61 static void modest_hbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class);
62 static void modest_hbox_cell_renderer_finalize (GObject *object);
63 static void modest_hbox_cell_renderer_class_init (ModestHBoxCellRendererClass *klass);
64
65 /* static functions: GtkCellRenderer */
66 static void modest_hbox_cell_renderer_get_size     (GtkCellRenderer       *cell,
67                                                     GtkWidget             *widget,
68                                                     GdkRectangle          *rectangle,
69                                                     gint                  *x_offset,
70                                                     gint                  *y_offset,
71                                                     gint                  *width,
72                                                     gint                  *height);
73 static void modest_hbox_cell_renderer_render       (GtkCellRenderer       *cell,
74                                                     GdkDrawable           *window,
75                                                     GtkWidget             *widget,
76                                                     GdkRectangle          *background_area,
77                                                     GdkRectangle          *cell_area,
78                                                     GdkRectangle          *expose_area,
79                                                     GtkCellRendererState  flags);
80                                                 
81
82 /**
83  * modest_hbox_cell_renderer_new:
84  *
85  * Return value: a new #ModestHBoxCellRenderer instance implemented for Gtk+
86  **/
87 GtkCellRenderer*
88 modest_hbox_cell_renderer_new (void)
89 {
90         ModestHBoxCellRenderer *self = g_object_new (MODEST_TYPE_HBOX_CELL_RENDERER, NULL);
91
92         return GTK_CELL_RENDERER (self);
93 }
94
95 static void
96 modest_hbox_cell_renderer_instance_init (GTypeInstance *instance, gpointer g_class)
97 {
98         ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (instance);
99
100         priv->renderers_list = NULL;
101         
102         return;
103 }
104
105 static void
106 modest_hbox_cell_renderer_finalize (GObject *object)
107 {
108         ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (object);
109
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;
114         }
115
116         (*parent_class->finalize) (object);
117
118         return;
119 }
120
121 static void 
122 modest_hbox_cell_renderer_class_init (ModestHBoxCellRendererClass *klass)
123 {
124         GObjectClass *object_class;
125         GtkCellRendererClass *renderer_class;
126
127         parent_class = g_type_class_peek_parent (klass);
128         object_class = (GObjectClass*) klass;
129         renderer_class = (GtkCellRendererClass*) klass;
130
131         object_class->finalize = modest_hbox_cell_renderer_finalize;
132         renderer_class->get_size = modest_hbox_cell_renderer_get_size;
133         renderer_class->render = modest_hbox_cell_renderer_render;
134
135         g_type_class_add_private (object_class, sizeof (ModestHBoxCellRendererPrivate));
136
137         return;
138 }
139
140 GType 
141 modest_hbox_cell_renderer_get_type (void)
142 {
143         static GType type = 0;
144
145         if (G_UNLIKELY(type == 0))
146         {
147                 static const GTypeInfo info = 
148                 {
149                   sizeof (ModestHBoxCellRendererClass),
150                   NULL,   /* base_init */
151                   NULL,   /* base_finalize */
152                   (GClassInitFunc) modest_hbox_cell_renderer_class_init,   /* class_init */
153                   NULL,   /* class_finalize */
154                   NULL,   /* class_data */
155                   sizeof (ModestHBoxCellRenderer),
156                   0,      /* n_preallocs */
157                   modest_hbox_cell_renderer_instance_init    /* instance_init */
158                 };
159
160                 type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
161                         "ModestHBoxCellRenderer",
162                         &info, 0);
163
164         }
165
166         return type;
167 }
168
169
170 /**
171  * modest_hbox_cell_renderer_append:
172  * @hbox_renderer: a #ModestHBoxCellRenderer
173  * @cell: a #GtkCellRenderer
174  *
175  * Appends @cell to the end of the list of renderers shown in @hbox_renderer
176  */
177 void 
178 modest_hbox_cell_renderer_append (ModestHBoxCellRenderer *hbox_renderer, 
179                                   GtkCellRenderer *cell,
180                                   gboolean expand)
181 {
182         ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (hbox_renderer);
183         
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));
186         g_object_ref_sink (G_OBJECT (cell));
187 }
188
189 static void 
190 modest_hbox_cell_renderer_get_size     (GtkCellRenderer       *cell,
191                                         GtkWidget             *widget,
192                                         GdkRectangle          *rectangle,
193                                         gint                  *x_offset,
194                                         gint                  *y_offset,
195                                         gint                  *width,
196                                         gint                  *height)
197 {
198         gint calc_width, calc_height;
199         gint full_width, full_height;
200         GList *node;
201         ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (cell);
202
203         calc_width = 0;
204         calc_height = 0;
205
206         for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
207                 gint renderer_width, renderer_height;
208                 GtkCellRenderer *renderer = (GtkCellRenderer *) node->data;
209
210                 gtk_cell_renderer_get_size (renderer, widget, NULL, NULL, NULL,
211                                             &renderer_width, &renderer_height);
212                 if ((renderer_width > 0)&&(renderer_height > 0)) {
213                         calc_height = MAX (calc_height, renderer_height);
214                         calc_width += renderer_width;
215                 }
216         }
217
218         full_width = (gint) cell->xpad * 2 + calc_width;
219         full_height = (gint) cell->ypad * 2 + calc_height;
220
221         if (rectangle && calc_width > 0 && calc_height > 0) {
222                 if (x_offset) {
223                         *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
224                                       (1.0 - cell->xalign) : cell->xalign) *
225                                      (rectangle->width - full_width));
226                         *x_offset = MAX (*x_offset, 0);
227                 }
228                 if (y_offset) {
229                         *y_offset = ((cell->yalign) *
230                                      (rectangle->height - full_height));
231                         *y_offset = MAX (*y_offset, 0);
232                 }
233         } else {
234                 if (x_offset)
235                         *x_offset = 0;
236                 if (y_offset)
237                         *y_offset = 0;
238         }
239
240         if (width)
241                 *width = full_width;
242         if (height)
243                 *height = full_height;
244 }
245
246 static void 
247 modest_hbox_cell_renderer_render       (GtkCellRenderer       *cell,
248                                         GdkDrawable           *window,
249                                         GtkWidget             *widget,
250                                         GdkRectangle          *background_area,
251                                         GdkRectangle          *cell_area,
252                                         GdkRectangle          *expose_area,
253                                         GtkCellRendererState  flags)
254 {
255         ModestHBoxCellRendererPrivate *priv = MODEST_HBOX_CELL_RENDERER_GET_PRIVATE (cell);
256         gint nvis_children = 0;
257         gint nexpand_children = 0;
258         GtkTextDirection direction;
259         GList *node = NULL;
260         GtkCellRenderer *child;
261         gint width, extra;
262         GtkRequisition req;
263         
264         direction = gtk_widget_get_direction (widget);
265         nvis_children = 0;
266         nexpand_children = 0;
267
268         /* first, retrieve the requisition of the children cell renderers */
269         modest_hbox_cell_renderer_get_size (cell, widget, NULL, NULL, NULL, &(req.width), &(req.height));
270
271         /* Counts visible and expandable children cell renderers */
272         for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
273                 gboolean visible, expand;
274                 child = (GtkCellRenderer *) node->data;
275                 g_object_get (G_OBJECT (child), "visible", &visible, NULL);
276                 expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
277                 
278                 if (visible) {
279                         nvis_children += 1;
280                         if (expand)
281                                 nexpand_children += 1;
282                 }
283         }
284
285         if (nvis_children > 0) {
286                 gint x_pad, y_pad;
287                 gint x;
288                 GdkRectangle child_alloc;
289
290                 if (nexpand_children > 0) {
291                         width = cell_area->width - req.width;
292                         extra = width / nexpand_children;
293                 } else {
294                         width = 0;
295                         extra = 0;
296                 }
297
298                 g_object_get (cell, "xpad", &x_pad, "ypad", &y_pad, NULL);
299                 x = cell_area->x + x_pad;
300                 child_alloc.y = cell_area->y + y_pad;
301                 child_alloc.height = MAX (1, cell_area->height - y_pad * 2);
302
303                 for (node = priv->renderers_list; node != NULL; node = g_list_next (node)) {
304                         gboolean visible, expand;
305
306                         child = (GtkCellRenderer *) node->data;
307                         g_object_get (G_OBJECT (child), "visible", &visible, NULL);
308                         expand = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (child), RENDERER_EXPAND_ATTRIBUTE));
309
310                         if (visible) {
311                                 GtkRequisition child_req;
312                                 gint child_xpad, child_ypad;
313                                 GdkRectangle child_expose_area;
314
315                                 gtk_cell_renderer_get_size (child, widget, NULL, NULL, NULL, &(child_req.width), &(child_req.height));
316                                 g_object_get (child, "xpad", &child_xpad, "ypad", &child_ypad, NULL);
317
318                                 if (expand) {
319                                         if (nexpand_children == 1)
320                                                 child_req.width += width;
321                                         else
322                                                 child_req.width += extra;
323                                         nexpand_children -= 1;
324                                         width -= extra;
325                                 }
326
327                                 child_alloc.width = MAX (1, child_req.width);
328                                 child_alloc.x = x;
329
330                                 if (direction == GTK_TEXT_DIR_RTL)
331                                         child_alloc.x = cell_area->x + cell_area->width - (child_alloc.x - cell_area->x) - child_alloc.width;
332
333                                 if (gdk_rectangle_intersect (&child_alloc, expose_area, &child_expose_area))
334                                         gtk_cell_renderer_render (child, window, widget, background_area, &child_alloc, &child_expose_area, flags);
335                                 x += child_req.width;
336                         }
337                 }
338         }
339         
340 }