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,
85 static void gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
86 GtkCellRenderer *renderer,
90 /* CellInfo metadata handling */
92 GtkCellRenderer *renderer;
98 static CellInfo *cell_info_new (GtkCellRenderer *renderer,
101 static void cell_info_free (CellInfo *info);
102 static gint cell_info_find (CellInfo *info,
103 GtkCellRenderer *renderer);
106 struct _GtkCellAreaBoxPrivate
108 GtkOrientation orientation;
118 G_DEFINE_TYPE_WITH_CODE (GtkCellAreaBox, gtk_cell_area_box, GTK_TYPE_CELL_AREA,
119 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
120 gtk_cell_area_box_cell_layout_init)
121 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
124 gtk_cell_area_box_init (GtkCellAreaBox *box)
126 GtkCellAreaBoxPrivate *priv;
128 box->priv = G_TYPE_INSTANCE_GET_PRIVATE (box,
129 GTK_TYPE_CELL_AREA_BOX,
130 GtkCellAreaBoxPrivate);
133 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
138 gtk_cell_area_box_class_init (GtkCellAreaBoxClass *class)
140 GObjectClass *object_class = G_OBJECT_CLASS (class);
141 GtkCellAreaClass *area_class = GTK_CELL_AREA_CLASS (class);
144 object_class->finalize = gtk_cell_area_box_finalize;
145 object_class->dispose = gtk_cell_area_box_dispose;
146 object_class->set_property = gtk_cell_area_box_set_property;
147 object_class->get_property = gtk_cell_area_box_get_property;
149 /* GtkCellAreaClass */
150 area_class->add = gtk_cell_area_box_add;
151 area_class->remove = gtk_cell_area_box_remove;
152 area_class->forall = gtk_cell_area_box_forall;
153 area_class->event = gtk_cell_area_box_event;
154 area_class->render = gtk_cell_area_box_render;
156 area_class->get_request_mode = gtk_cell_area_box_get_request_mode;
157 area_class->get_preferred_width = gtk_cell_area_box_get_preferred_width;
158 area_class->get_preferred_height = gtk_cell_area_box_get_preferred_height;
159 area_class->get_preferred_height_for_width = gtk_cell_area_box_get_preferred_height_for_width;
160 area_class->get_preferred_width_for_height = gtk_cell_area_box_get_preferred_width_for_height;
162 g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
165 g_type_class_add_private (object_class, sizeof (GtkCellAreaBoxPrivate));
169 /*************************************************************
171 *************************************************************/
173 cell_info_new (GtkCellRenderer *renderer,
177 CellInfo *info = g_slice_new (CellInfo);
179 info->renderer = g_object_ref_sink (renderer);
180 info->expand = expand;
187 cell_info_free (CellInfo *info)
189 g_object_unref (info->renderer);
191 g_slice_free (CellInfo, info);
195 cell_info_find (CellInfo *info,
196 GtkCellRenderer *renderer)
198 return (info->renderer == renderer) ? 0 : -1;
201 /*************************************************************
203 *************************************************************/
205 gtk_cell_area_box_finalize (GObject *object)
207 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->finalize (object);
211 gtk_cell_area_box_dispose (GObject *object)
213 G_OBJECT_CLASS (gtk_cell_area_box_parent_class)->dispose (object);
217 gtk_cell_area_box_set_property (GObject *object,
226 gtk_cell_area_box_get_property (GObject *object,
234 /*************************************************************
236 *************************************************************/
238 gtk_cell_area_box_add (GtkCellArea *area,
239 GtkCellRenderer *renderer)
241 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area),
246 gtk_cell_area_box_remove (GtkCellArea *area,
247 GtkCellRenderer *renderer)
249 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
250 GtkCellAreaBoxPrivate *priv = box->priv;
253 node = g_list_find_custom (priv->cells, renderer,
254 (GCompareFunc)cell_info_find);
258 CellInfo *info = node->data;
260 cell_info_free (info);
262 priv->cells = g_list_delete_link (priv->cells, node);
265 g_warning ("Trying to remove a cell renderer that is not present GtkCellAreaBox");
269 gtk_cell_area_box_forall (GtkCellArea *area,
270 GtkCellCallback callback,
271 gpointer callback_data)
273 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
274 GtkCellAreaBoxPrivate *priv = box->priv;
277 for (list = priv->cells; list; list = list->next)
279 CellInfo *info = list->data;
281 callback (info->renderer, callback_data);
286 gtk_cell_area_box_event (GtkCellArea *area,
289 const GdkRectangle *cell_area)
297 gtk_cell_area_box_render (GtkCellArea *area,
300 const GdkRectangle *cell_area)
305 static GtkSizeRequestMode
306 gtk_cell_area_box_get_request_mode (GtkCellArea *area)
308 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (area);
309 GtkCellAreaBoxPrivate *priv = box->priv;
311 return (priv->orientation) == GTK_ORIENTATION_HORIZONTAL ?
312 GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH :
313 GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
317 gtk_cell_area_box_get_preferred_width (GtkCellArea *area,
326 gtk_cell_area_box_get_preferred_height (GtkCellArea *area,
328 gint *minimum_height,
329 gint *natural_height)
336 gtk_cell_area_box_get_preferred_height_for_width (GtkCellArea *area,
339 gint *minimum_height,
340 gint *natural_height)
346 gtk_cell_area_box_get_preferred_width_for_height (GtkCellArea *area,
357 /*************************************************************
358 * GtkCellLayoutIface *
359 *************************************************************/
361 gtk_cell_area_box_cell_layout_init (GtkCellLayoutIface *iface)
363 iface->pack_start = gtk_cell_area_box_layout_pack_start;
364 iface->pack_end = gtk_cell_area_box_layout_pack_end;
365 iface->reorder = gtk_cell_area_box_layout_reorder;
369 gtk_cell_area_box_layout_pack_start (GtkCellLayout *cell_layout,
370 GtkCellRenderer *renderer,
373 gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
377 gtk_cell_area_box_layout_pack_end (GtkCellLayout *cell_layout,
378 GtkCellRenderer *renderer,
381 gtk_cell_area_box_pack_end (GTK_CELL_AREA_BOX (cell_layout), renderer, expand);
385 gtk_cell_area_box_layout_reorder (GtkCellLayout *cell_layout,
386 GtkCellRenderer *renderer,
389 GtkCellAreaBox *box = GTK_CELL_AREA_BOX (cell_layout);
390 GtkCellAreaBoxPrivate *priv = box->priv;
394 node = g_list_find_custom (priv->cells, renderer,
395 (GCompareFunc)cell_info_find);
401 priv->cells = g_list_delete_link (priv->cells, node);
402 priv->cells = g_list_insert (priv->cells, info, position);
406 /*************************************************************
408 *************************************************************/
410 gtk_cell_area_box_new (void)
412 return (GtkCellArea *)g_object_new (GTK_TYPE_CELL_AREA_BOX, NULL);
416 gtk_cell_area_box_pack_start (GtkCellAreaBox *box,
417 GtkCellRenderer *renderer,
420 GtkCellAreaBoxPrivate *priv;
423 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
424 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
428 if (g_list_find_custom (priv->cells, renderer,
429 (GCompareFunc)cell_info_find))
431 g_warning ("Refusing to add the same cell renderer to a GtkCellAreaBox twice");
435 info = cell_info_new (renderer, expand, GTK_PACK_START);
437 priv->cells = g_list_append (priv->cells, info);
441 gtk_cell_area_box_pack_end (GtkCellAreaBox *box,
442 GtkCellRenderer *renderer,
445 GtkCellAreaBoxPrivate *priv;
448 g_return_if_fail (GTK_IS_CELL_AREA_BOX (box));
449 g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
453 if (g_list_find_custom (priv->cells, renderer,
454 (GCompareFunc)cell_info_find))
456 g_warning ("Refusing to add the same cell renderer to a GtkCellArea twice");
460 info = cell_info_new (renderer, expand, GTK_PACK_END);
462 priv->cells = g_list_append (priv->cells, info);