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 GtkCellAreaIter *gtk_cell_area_box_create_iter (GtkCellArea *area);
58 static GtkSizeRequestMode gtk_cell_area_box_get_request_mode (GtkCellArea *area);
59 static void gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
60 GtkCellAreaIter *iter,
64 static void gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
65 GtkCellAreaIter *iter,
68 gint *natural_height);
69 static void gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
70 GtkCellAreaIter *iter,
74 gint *natural_height);
75 static void gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
76 GtkCellAreaIter *iter,
82 /* GtkCellLayoutIface */
83 static void gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface);
84 static void gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
85 GtkCellRenderer *renderer,
87 static void gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
88 GtkCellRenderer *renderer,
90 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
91 GtkCellRenderer *renderer,
95 /* CellInfo metadata handling */
97 GtkCellRenderer *renderer;
103 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
106 static void cell_info_free (CellInfo *info);
107 static gint cell_info_find (CellInfo *info,
108 GtkCellRenderer *renderer);
111 struct _GtkCellAreaBoxPrivate
113 GtkOrientation orientation;
123 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
124 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
125 gtk_cell_area_box_cell_layout_init)
126 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
129 gtk_cell_area_box_init (GtkCellAreaBox *box)
131 GtkCellAreaBoxPrivate *priv;
133 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
134 GTK_TYPE_CELL_AREA_BOX,
135 GtkCellAreaBoxPrivate);
138 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
143 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
145 GObjectClass *object_class = G_OBJECT_CLASS (class);
146 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
149 object_class->finalize = gtk_cell_area_box_finalize;
150 object_class->dispose = gtk_cell_area_box_dispose;
151 object_class->set_property = gtk_cell_area_box_set_property;
152 object_class->get_property = gtk_cell_area_box_get_property;
154 /* GtkCellAreaClass */
155 area_class->add = gtk_cell_area_box_add;
156 area_class->remove = gtk_cell_area_box_remove;
157 area_class->forall = gtk_cell_area_box_forall;
158 area_class->event = gtk_cell_area_box_event;
159 area_class->render = gtk_cell_area_box_render;
161 area_class->create_iter = gtk_cell_area_box_create_iter;
162 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
163 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
164 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
165 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
166 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
168 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
171 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
175 /*************************************************************
177 *************************************************************/
179 cell_info_new (GtkCellRenderer *renderer,
183 CellInfo *info = g_slice_new (CellInfo);
185 info->renderer = g_object_ref_sink (renderer);
186 info->expand = expand;
193 cell_info_free (CellInfo *info)
195 g_object_unref (info->renderer);
197 g_slice_free (CellInfo, info);
201 cell_info_find (CellInfo *info,
202 GtkCellRenderer *renderer)
204 return (info->renderer == renderer) ? 0 : -1;
207 /*************************************************************
209 *************************************************************/
211 gtk_cell_area_box_finalize (GObject *object)
213 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
217 gtk_cell_area_box_dispose (GObject *object)
219 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
223 gtk_cell_area_box_set_property (GObject *object,
232 gtk_cell_area_box_get_property (GObject *object,
240 /*************************************************************
242 *************************************************************/
244 gtk_cell_area_box_add (GtkCellArea *area,
245 GtkCellRenderer *renderer)
247 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
252 gtk_cell_area_box_remove (GtkCellArea *area,
253 GtkCellRenderer *renderer)
255 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
256 GtkCellAreaBoxPrivate *priv = box->priv;
259 node = g_list_find_custom (priv->cells, renderer,
260 (GCompareFunc)cell_info_find);
264 CellInfo *info = node->data;
266 cell_info_free (info);
268 priv->cells = g_list_delete_link (priv->cells, node);
271 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
275 gtk_cell_area_box_forall (GtkCellArea *area,
276 GtkCellCallback callback,
277 gpointer callback_data)
279 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
280 GtkCellAreaBoxPrivate *priv = box->priv;
283 for (list = priv->cells; list; list = list->next)
285 CellInfo *info = list->data;
287 callback (info->renderer, callback_data);
292 gtk_cell_area_box_event (GtkCellArea *area,
295 const GdkRectangle *cell_area)
303 gtk_cell_area_box_render (GtkCellArea *area,
306 const GdkRectangle *cell_area)
311 static GtkCellAreaIter *
312 gtk_cell_area_box_create_iter (GtkCellArea *area)
317 static GtkSizeRequestMode
318 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
320 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
321 GtkCellAreaBoxPrivate *priv = box->priv;
323 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
324 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
325 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
329 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
330 GtkCellAreaIter *iter,
339 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
340 GtkCellAreaIter *iter,
342 gint *minimum_height,
343 gint *natural_height)
350 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
351 GtkCellAreaIter *iter,
354 gint *minimum_height,
355 gint *natural_height)
361 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
362 GtkCellAreaIter *iter,
372 /*************************************************************
373 * GtkCellLayoutIface *
374 *************************************************************/
376 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
378 iface->pack_start = gtk_cell_area_box_layout_pack_start;
379 iface->pack_end = gtk_cell_area_box_layout_pack_end;
380 iface->reorder = gtk_cell_area_box_layout_reorder;
384 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
385 GtkCellRenderer *renderer,
388 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
392 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
393 GtkCellRenderer *renderer,
396 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
400 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
401 GtkCellRenderer *renderer,
404 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
405 GtkCellAreaBoxPrivate *priv = box->priv;
409 node = g_list_find_custom (priv->cells, renderer,
410 (GCompareFunc)cell_info_find);
416 priv->cells = g_list_delete_link (priv->cells, node);
417 priv->cells = g_list_insert (priv->cells, info, position);
421 /*************************************************************
423 *************************************************************/
425 gtk_cell_area_box_new (void)
427 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
431 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
432 GtkCellRenderer *renderer,
435 GtkCellAreaBoxPrivate *priv;
438 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
439 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
443 if (g_list_find_custom (priv->cells, renderer,
444 (GCompareFunc)cell_info_find))
446 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
450 info = cell_info_new (renderer, expand, GTK_PACK_START);
452 priv->cells = g_list_append (priv->cells, info);
456 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
457 GtkCellRenderer *renderer,
460 GtkCellAreaBoxPrivate *priv;
463 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
464 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
468 if (g_list_find_custom (priv->cells, renderer,
469 (GCompareFunc)cell_info_find))
471 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
475 info = cell_info_new (renderer, expand, GTK_PACK_END);
477 priv->cells = g_list_append (priv->cells, info);