3 * Copyright (C) 2010 Openismus GmbH
6 * Tristan Van Berkom <tristanvb@openismus.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "gtkorientable.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellareabox.h"
29 static void gtk_cell_area_box_finalize (GObject *object);
30 static void gtk_cell_area_box_dispose (GObject *object);
31 static void gtk_cell_area_box_set_property (GObject *object,
35 static void gtk_cell_area_box_get_property (GObject *object,
40 /* GtkCellAreaClass */
41 static void gtk_cell_area_box_add (GtkCellArea *area,
42 GtkCellRenderer *renderer);
43 static void gtk_cell_area_box_remove (GtkCellArea *area,
44 GtkCellRenderer *renderer);
45 static void gtk_cell_area_box_forall (GtkCellArea *area,
46 GtkCellCallback callback,
47 gpointer callback_data);
48 static gint gtk_cell_area_box_event (GtkCellArea *area,
51 const GdkRectangle *cell_area);
52 static void gtk_cell_area_box_render (GtkCellArea *area,
55 const GdkRectangle *cell_area);
57 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
58 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
62 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
65 gint *natural_height);
66 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
70 gint *natural_height);
71 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
77 /* GtkCellLayoutIface */
78 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
79 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
80 GtkCellRenderer *renderer,
82 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
83 GtkCellRenderer *renderer,
87 /* CellInfo metadata handling */
89 GtkCellRenderer *renderer;
95 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
98 static void cell_info_free (CellInfo *info);
99 static gint cell_info_find (CellInfo *info,
100 GtkCellRenderer *renderer);
103 struct _GtkCellAreaBoxPrivate
105 GtkOrientation orientation;
115 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
116 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
117 gtk_cell_area_box_cell_layout_init)
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
121 gtk_cell_area_box_init (GtkCellAreaBox *box)
123 GtkCellAreaBoxPrivate *priv;
125 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
126 GTK_TYPE_CELL_AREA_BOX,
127 GtkCellAreaBoxPrivate);
130 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
135 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
137 GObjectClass *object_class = G_OBJECT_CLASS (class);
138 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
141 object_class->finalize = gtk_cell_area_box_finalize;
142 object_class->dispose = gtk_cell_area_box_dispose;
143 object_class->set_property = gtk_cell_area_box_set_property;
144 object_class->get_property = gtk_cell_area_box_get_property;
146 /* GtkCellAreaClass */
147 area_class->add = gtk_cell_area_box_add;
148 area_class->remove = gtk_cell_area_box_remove;
149 area_class->forall = gtk_cell_area_box_forall;
150 area_class->event = gtk_cell_area_box_event;
151 area_class->render = gtk_cell_area_box_render;
153 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
154 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
155 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
156 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
157 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
159 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
162 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
166 /*************************************************************
168 *************************************************************/
170 cell_info_new (GtkCellRenderer *renderer,
174 CellInfo *info = g_slice_new (CellInfo);
176 info->renderer = g_object_ref_sink (renderer);
177 info->expand = expand;
184 cell_info_free (CellInfo *info)
186 g_object_unref (info->renderer);
188 g_slice_free (CellInfo, info);
192 cell_info_find (CellInfo *info,
193 GtkCellRenderer *renderer)
195 return (info->renderer == renderer) ? 0 : -1;
198 /*************************************************************
200 *************************************************************/
202 gtk_cell_area_box_finalize (GObject *object)
204 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
208 gtk_cell_area_box_dispose (GObject *object)
210 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
214 gtk_cell_area_box_set_property (GObject *object,
223 gtk_cell_area_box_get_property (GObject *object,
231 /*************************************************************
233 *************************************************************/
235 gtk_cell_area_box_add (GtkCellArea *area,
236 GtkCellRenderer *renderer)
238 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
243 gtk_cell_area_box_remove (GtkCellArea *area,
244 GtkCellRenderer *renderer)
246 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
247 GtkCellAreaBoxPrivate *priv = box->priv;
250 node = g_list_find_custom (priv->cells, renderer,
251 (GCompareFunc)cell_info_find);
255 CellInfo *info = node->data;
257 cell_info_free (info);
259 priv->cells = g_list_delete_link (priv->cells, node);
262 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
266 gtk_cell_area_box_forall (GtkCellArea *area,
267 GtkCellCallback callback,
268 gpointer callback_data)
270 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
271 GtkCellAreaBoxPrivate *priv = box->priv;
274 for (list = priv->cells; list; list = list->next)
276 CellInfo *info = list->data;
278 callback (info->renderer, callback_data);
283 gtk_cell_area_box_event (GtkCellArea *area,
286 const GdkRectangle *cell_area)
294 gtk_cell_area_box_render (GtkCellArea *area,
297 const GdkRectangle *cell_area)
302 static GtkSizeRequestMode
303 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
305 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
306 GtkCellAreaBoxPrivate *priv = box->priv;
308 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
309 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
310 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
314 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
323 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
325 gint *minimum_height,
326 gint *natural_height)
333 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
336 gint *minimum_height,
337 gint *natural_height)
343 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
354 /*************************************************************
355 * GtkCellLayoutIface *
356 *************************************************************/
358 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
360 iface->pack_start = gtk_cell_area_box_layout_pack_start;
361 iface->pack_end = gtk_cell_area_box_layout_pack_end;
365 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
366 GtkCellRenderer *renderer,
369 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
373 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
374 GtkCellRenderer *renderer,
377 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
380 /*************************************************************
382 *************************************************************/
384 gtk_cell_area_box_new (void)
386 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
390 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
391 GtkCellRenderer *renderer,
394 GtkCellAreaBoxPrivate *priv;
397 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
398 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
402 if (g_list_find_custom (priv->cells, renderer,
403 (GCompareFunc)cell_info_find))
405 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
409 info = cell_info_new (renderer, expand, GTK_PACK_START);
411 priv->cells = g_list_append (priv->cells, info);
415 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
416 GtkCellRenderer *renderer,
419 GtkCellAreaBoxPrivate *priv;
422 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
423 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
427 if (g_list_find_custom (priv->cells, renderer,
428 (GCompareFunc)cell_info_find))
430 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
434 info = cell_info_new (renderer, expand, GTK_PACK_END);
436 priv->cells = g_list_append (priv->cells, info);