]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellarea.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcellarea.c
1 /* gtkcellarea.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.com>
7  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /**
23  * SECTION:gtkcellarea
24  * @Short_Description: An abstract class for laying out GtkCellRenderers
25  * @Title: GtkCellArea
26  *
27  * The #GtkCellArea is an abstract class for #GtkCellLayout widgets
28  * (also referred to as "layouting widgets") to interface with an
29  * arbitrary number of #GtkCellRenderers and interact with the user
30  * for a given #GtkTreeModel row.
31  *
32  * The cell area handles events, focus navigation, drawing and
33  * size requests and allocations for a given row of data.
34  *
35  * Usually users dont have to interact with the #GtkCellArea directly
36  * unless they are implementing a cell-layouting widget themselves.
37  *
38  * <refsect2 id="cell-area-geometry-management">
39  * <title>Requesting area sizes</title>
40  * <para>
41  * As outlined in <link linkend="geometry-management">GtkWidget's
42  * geometry management section</link>, GTK+ uses a height-for-width
43  * geometry management system to compute the sizes of widgets and user
44  * interfaces. #GtkCellArea uses the same semantics to calculate the
45  * size of an area for an arbitrary number of #GtkTreeModel rows.
46  *
47  * When requesting the size of a cell area one needs to calculate
48  * the size for a handful of rows, and this will be done differently by
49  * different layouting widgets. For instance a #GtkTreeViewColumn
50  * always lines up the areas from top to bottom while a #GtkIconView
51  * on the other hand might enforce that all areas received the same
52  * width and wrap the areas around, requesting height for more cell
53  * areas when allocated less width.
54  *
55  * It's also important for areas to maintain some cell
56  * alignments with areas rendered for adjacent rows (cells can
57  * appear "columnized" inside an area even when the size of
58  * cells are different in each row). For this reason the #GtkCellArea
59  * uses a #GtkCellAreaContext object to store the alignments
60  * and sizes along the way (as well as the overall largest minimum
61  * and natural size for all the rows which have been calculated
62  * with the said context).
63  *
64  * The #GtkCellAreaContext is an opaque object specific to the
65  * #GtkCellArea which created it (see gtk_cell_area_create_context()).
66  * The owning cell-layouting widget can create as many contexts as
67  * it wishes to calculate sizes of rows which should receive the
68  * same size in at least one orientation (horizontally or vertically),
69  * However, it's important that the same #GtkCellAreaContext which
70  * was used to request the sizes for a given #GtkTreeModel row be
71  * used when rendering or processing events for that row.
72  *
73  * In order to request the width of all the rows at the root level
74  * of a #GtkTreeModel one would do the following:
75  * <example>
76  *   <title>Requesting the width of a handful of GtkTreeModel rows</title>
77  *   <programlisting>
78  * GtkTreeIter iter;
79  * gint        minimum_width;
80  * gint        natural_width;
81  *
82  * valid = gtk_tree_model_get_iter_first (model, &iter);
83  * while (valid)
84  *   {
85  *     gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
86  *     gtk_cell_area_get_preferred_width (area, context, widget, NULL, NULL);
87  *
88  *     valid = gtk_tree_model_iter_next (model, &iter);
89  *   }
90  * gtk_cell_area_context_get_preferred_width (context, &minimum_width, &natural_width);
91  *   </programlisting>
92  * </example>
93  * Note that in this example it's not important to observe the
94  * returned minimum and natural width of the area for each row
95  * unless the cell-layouting object is actually interested in the
96  * widths of individual rows. The overall width is however stored
97  * in the accompanying #GtkCellAreaContext object and can be consulted
98  * at any time.
99  *
100  * This can be useful since #GtkCellLayout widgets usually have to
101  * support requesting and rendering rows in treemodels with an
102  * exceedingly large amount of rows. The #GtkCellLayout widget in
103  * that case would calculate the required width of the rows in an
104  * idle or timeout source (see g_timeout_add()) and when the widget
105  * is requested its actual width in #GtkWidgetClass.get_preferred_width()
106  * it can simply consult the width accumulated so far in the
107  * #GtkCellAreaContext object.
108  *
109  * A simple example where rows are rendered from top to bottom and
110  * take up the full width of the layouting widget would look like:
111  * <example>
112  *   <title>A typical get_preferred_width() implementation</title>
113  *   <programlisting>
114  * static void
115  * foo_get_preferred_width (GtkWidget       *widget,
116  *                          gint            *minimum_size,
117  *                          gint            *natural_size)
118  * {
119  *   Foo        *foo  = FOO (widget);
120  *   FooPrivate *priv = foo->priv;
121  *
122  *   foo_ensure_at_least_one_handfull_of_rows_have_been_requested (foo);
123  *
124  *   gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
125  * }
126  *   </programlisting>
127  * </example>
128  * In the above example the Foo widget has to make sure that some
129  * row sizes have been calculated (the amount of rows that Foo judged
130  * was appropriate to request space for in a single timeout iteration)
131  * before simply returning the amount of space required by the area via
132  * the #GtkCellAreaContext.
133  *
134  * Requesting the height for width (or width for height) of an area is
135  * a similar task except in this case the #GtkCellAreaContext does not
136  * store the data (actually, it does not know how much space the layouting
137  * widget plans to allocate it for every row. It's up to the layouting
138  * widget to render each row of data with the appropriate height and
139  * width which was requested by the #GtkCellArea).
140  *
141  * In order to request the height for width of all the rows at the
142  * root level of a #GtkTreeModel one would do the following:
143  * <example>
144  *   <title>Requesting the height for width of a handful of GtkTreeModel rows</title>
145  *   <programlisting>
146  * GtkTreeIter iter;
147  * gint        minimum_height;
148  * gint        natural_height;
149  * gint        full_minimum_height = 0;
150  * gint        full_natural_height = 0;
151  *
152  * valid = gtk_tree_model_get_iter_first (model, &iter);
153  * while (valid)
154  *   {
155  *     gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
156  *     gtk_cell_area_get_preferred_height_for_width (area, context, widget,
157  *                                                   width, &minimum_height, &natural_height);
158  *
159  *     if (width_is_for_allocation)
160  *        cache_row_height (&iter, minimum_height, natural_height);
161  *
162  *     full_minimum_height += minimum_height;
163  *     full_natural_height += natural_height;
164  *
165  *     valid = gtk_tree_model_iter_next (model, &iter);
166  *   }
167  *   </programlisting>
168  * </example>
169  * Note that in the above example we would need to cache the heights
170  * returned for each row so that we would know what sizes to render the
171  * areas for each row. However we would only want to really cache the
172  * heights if the request is intended for the layouting widgets real
173  * allocation.
174  *
175  * In some cases the layouting widget is requested the height for an
176  * arbitrary for_width, this is a special case for layouting widgets
177  * who need to request size for tens of thousands  of rows. For this
178  * case it's only important that the layouting widget calculate
179  * one reasonably sized chunk of rows and return that height
180  * synchronously. The reasoning here is that any layouting widget is
181  * at least capable of synchronously calculating enough height to fill
182  * the screen height (or scrolled window height) in response to a single
183  * call to #GtkWidgetClass.get_preferred_height_for_width(). Returning
184  * a perfect height for width that is larger than the screen area is
185  * inconsequential since after the layouting receives an allocation
186  * from a scrolled window it simply continues to drive the scrollbar
187  * values while more and more height is required for the row heights
188  * that are calculated in the background.
189  * </para>
190  * </refsect2>
191  * <refsect2 id="cell-area-rendering">
192  * <title>Rendering Areas</title>
193  * <para>
194  * Once area sizes have been aquired at least for the rows in the
195  * visible area of the layouting widget they can be rendered at
196  * #GtkWidgetClass.draw() time.
197  *
198  * A crude example of how to render all the rows at the root level
199  * runs as follows:
200  * <example>
201  *   <title>Requesting the width of a handful of GtkTreeModel rows</title>
202  *   <programlisting>
203  * GtkAllocation allocation;
204  * GdkRectangle  cell_area = { 0, };
205  * GtkTreeIter   iter;
206  * gint          minimum_width;
207  * gint          natural_width;
208  *
209  * gtk_widget_get_allocation (widget, &allocation);
210  * cell_area.width = allocation.width;
211  *
212  * valid = gtk_tree_model_get_iter_first (model, &iter);
213  * while (valid)
214  *   {
215  *     cell_area.height = get_cached_height_for_row (&iter);
216  *
217  *     gtk_cell_area_apply_attributes (area, model, &iter, FALSE, FALSE);
218  *     gtk_cell_area_render (area, context, widget, cr,
219  *                           &cell_area, &cell_area, state_flags, FALSE);
220  *
221  *     cell_area.y += cell_area.height;
222  *
223  *     valid = gtk_tree_model_iter_next (model, &iter);
224  *   }
225  *   </programlisting>
226  * </example>
227  * Note that the cached height in this example really depends on how
228  * the layouting widget works. The layouting widget might decide to
229  * give every row its minimum or natural height or, if the model content
230  * is expected to fit inside the layouting widget without scrolling, it
231  * would make sense to calculate the allocation for each row at
232  * #GtkWidget::size-allocate time using gtk_distribute_natural_allocation().
233  * </para>
234  * </refsect2>
235  * <refsect2 id="cell-area-events-and-focus">
236  * <title>Handling Events and Driving Keyboard Focus</title>
237  * <para>
238  * Passing events to the area is as simple as handling events on any
239  * normal widget and then passing them to the gtk_cell_area_event()
240  * API as they come in. Usually #GtkCellArea is only interested in
241  * button events, however some customized derived areas can be implemented
242  * who are interested in handling other events. Handling an event can
243  * trigger the #GtkCellArea::focus-changed signal to fire; as well as
244  * #GtkCellArea::add-editable in the case that an editable cell was
245  * clicked and needs to start editing. You can call
246  * gtk_cell_area_stop_editing() at any time to cancel any cell editing
247  * that is currently in progress.
248  *
249  * The #GtkCellArea drives keyboard focus from cell to cell in a way
250  * similar to #GtkWidget. For layouting widgets that support giving
251  * focus to cells it's important to remember to pass %GTK_CELL_RENDERER_FOCUSED
252  * to the area functions for the row that has focus and to tell the
253  * area to paint the focus at render time.
254  *
255  * Layouting widgets that accept focus on cells should implement the
256  * #GtkWidgetClass.focus() virtual method. The layouting widget is always
257  * responsible for knowing where #GtkTreeModel rows are rendered inside
258  * the widget, so at #GtkWidgetClass.focus() time the layouting widget
259  * should use the #GtkCellArea methods to navigate focus inside the area
260  * and then observe the GtkDirectionType to pass the focus to adjacent
261  * rows and areas.
262  *
263  * A basic example of how the #GtkWidgetClass.focus() virtual method
264  * should be implemented:
265  * <example>
266  *   <title>Implementing keyboard focus navigation</title>
267  *   <programlisting>
268  * static gboolean
269  * foo_focus (GtkWidget       *widget,
270  *            GtkDirectionType direction)
271  * {
272  *   Foo        *foo  = FOO (widget);
273  *   FooPrivate *priv = foo->priv;
274  *   gint        focus_row;
275  *   gboolean    have_focus = FALSE;
276  *
277  *   focus_row = priv->focus_row;
278  *
279  *   if (!gtk_widget_has_focus (widget))
280  *     gtk_widget_grab_focus (widget);
281  *
282  *   valid = gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, priv->focus_row);
283  *   while (valid)
284  *     {
285  *       gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
286  *
287  *       if (gtk_cell_area_focus (priv->area, direction))
288  *         {
289  *            priv->focus_row = focus_row;
290  *            have_focus = TRUE;
291  *            break;
292  *         }
293  *       else
294  *         {
295  *           if (direction == GTK_DIR_RIGHT ||
296  *               direction == GTK_DIR_LEFT)
297  *             break;
298  *           else if (direction == GTK_DIR_UP ||
299  *                    direction == GTK_DIR_TAB_BACKWARD)
300  *            {
301  *               if (focus_row == 0)
302  *                 break;
303  *               else
304  *                {
305  *                   focus_row--;
306  *                   valid = gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, focus_row);
307  *                }
308  *             }
309  *           else
310  *             {
311  *               if (focus_row == last_row)
312  *                 break;
313  *               else
314  *                 {
315  *                   focus_row++;
316  *                   valid = gtk_tree_model_iter_next (priv->model, &iter);
317  *                 }
318  *             }
319  *         }
320  *     }
321  *     return have_focus;
322  * }
323  *   </programlisting>
324  * </example>
325  * Note that the layouting widget is responsible for matching the
326  * GtkDirectionType values to the way it lays out its cells.
327  * </para>
328  * </refsect2>
329  * <refsect2 id="cell-properties">
330  * <title>Cell Properties</title>
331  * <para>
332  * The #GtkCellArea introduces <emphasis>cell properties</emphasis>
333  * for #GtkCellRenderers in very much the same way that #GtkContainer
334  * introduces <link linkend="child-properties">child properties</link>
335  * for #GtkWidgets. This provides some general interfaces for defining
336  * the relationship cell areas have with their cells. For instance in a
337  * #GtkCellAreaBox a cell might "expand" and receive extra space when
338  * the area is allocated more than its full natural request, or a cell
339  * might be configured to "align" with adjacent rows which were requested
340  * and rendered with the same #GtkCellAreaContext.
341  *
342  * Use gtk_cell_area_class_install_cell_property() to install cell
343  * properties for a cell area class and gtk_cell_area_class_find_cell_property()
344  * or gtk_cell_area_class_list_cell_properties() to get information about
345  * existing cell properties.
346  *
347  * To set the value of a cell property, use gtk_cell_area_cell_set_property(),
348  * gtk_cell_area_cell_set() or gtk_cell_area_cell_set_valist(). To obtain
349  * the value of a cell property, use gtk_cell_area_cell_get_property(),
350  * gtk_cell_area_cell_get() or gtk_cell_area_cell_get_valist().
351  * </para>
352  * </refsect2>
353  */
354
355 #include "config.h"
356
357 #include <stdarg.h>
358 #include <string.h>
359 #include <stdlib.h>
360
361 #include "gtkintl.h"
362 #include "gtkcelllayout.h"
363 #include "gtkcellarea.h"
364 #include "gtkcellareacontext.h"
365 #include "gtkmarshalers.h"
366 #include "gtkprivate.h"
367
368 #include <gobject/gvaluecollector.h>
369
370
371 /* GObjectClass */
372 static void      gtk_cell_area_dispose                             (GObject            *object);
373 static void      gtk_cell_area_finalize                            (GObject            *object);
374 static void      gtk_cell_area_set_property                        (GObject            *object,
375                                                                     guint               prop_id,
376                                                                     const GValue       *value,
377                                                                     GParamSpec         *pspec);
378 static void      gtk_cell_area_get_property                        (GObject            *object,
379                                                                     guint               prop_id,
380                                                                     GValue             *value,
381                                                                     GParamSpec         *pspec);
382
383 /* GtkCellAreaClass */
384 static void      gtk_cell_area_real_add                            (GtkCellArea         *area,
385                                                                     GtkCellRenderer     *renderer);
386 static void      gtk_cell_area_real_remove                         (GtkCellArea         *area,
387                                                                     GtkCellRenderer     *renderer);
388 static void      gtk_cell_area_real_foreach                        (GtkCellArea         *area,
389                                                                     GtkCellCallback      callback,
390                                                                     gpointer             callback_data);
391 static void      gtk_cell_area_real_foreach_alloc                  (GtkCellArea         *area,
392                                                                     GtkCellAreaContext  *context,
393                                                                     GtkWidget           *widget,
394                                                                     const GdkRectangle  *cell_area,
395                                                                     const GdkRectangle  *background_area,
396                                                                     GtkCellAllocCallback callback,
397                                                                     gpointer             callback_data);
398 static gint      gtk_cell_area_real_event                          (GtkCellArea          *area,
399                                                                     GtkCellAreaContext   *context,
400                                                                     GtkWidget            *widget,
401                                                                     GdkEvent             *event,
402                                                                     const GdkRectangle   *cell_area,
403                                                                     GtkCellRendererState  flags);
404 static void      gtk_cell_area_real_render                         (GtkCellArea          *area,
405                                                                     GtkCellAreaContext   *context,
406                                                                     GtkWidget            *widget,
407                                                                     cairo_t              *cr,
408                                                                     const GdkRectangle   *background_area,
409                                                                     const GdkRectangle   *cell_area,
410                                                                     GtkCellRendererState  flags,
411                                                                     gboolean              paint_focus);
412 static void      gtk_cell_area_real_apply_attributes               (GtkCellArea           *area,
413                                                                     GtkTreeModel          *tree_model,
414                                                                     GtkTreeIter           *iter,
415                                                                     gboolean               is_expander,
416                                                                     gboolean               is_expanded);
417
418 static GtkCellAreaContext *gtk_cell_area_real_create_context       (GtkCellArea           *area);
419 static GtkCellAreaContext *gtk_cell_area_real_copy_context         (GtkCellArea           *area,
420                                                                     GtkCellAreaContext    *context);
421 static GtkSizeRequestMode  gtk_cell_area_real_get_request_mode     (GtkCellArea           *area);
422 static void      gtk_cell_area_real_get_preferred_width            (GtkCellArea           *area,
423                                                                     GtkCellAreaContext    *context,
424                                                                     GtkWidget             *widget,
425                                                                     gint                  *minimum_width,
426                                                                     gint                  *natural_width);
427 static void      gtk_cell_area_real_get_preferred_height           (GtkCellArea           *area,
428                                                                     GtkCellAreaContext    *context,
429                                                                     GtkWidget             *widget,
430                                                                     gint                  *minimum_height,
431                                                                     gint                  *natural_height);
432 static void      gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea           *area,
433                                                                     GtkCellAreaContext    *context,
434                                                                     GtkWidget             *widget,
435                                                                     gint                   width,
436                                                                     gint                  *minimum_height,
437                                                                     gint                  *natural_height);
438 static void      gtk_cell_area_real_get_preferred_width_for_height (GtkCellArea           *area,
439                                                                     GtkCellAreaContext    *context,
440                                                                     GtkWidget             *widget,
441                                                                     gint                   height,
442                                                                     gint                  *minimum_width,
443                                                                     gint                  *natural_width);
444 static gboolean  gtk_cell_area_real_is_activatable                 (GtkCellArea           *area);
445 static gboolean  gtk_cell_area_real_activate                       (GtkCellArea           *area,
446                                                                     GtkCellAreaContext    *context,
447                                                                     GtkWidget             *widget,
448                                                                     const GdkRectangle    *cell_area,
449                                                                     GtkCellRendererState   flags,
450                                                                     gboolean               edit_only);
451 static gboolean  gtk_cell_area_real_focus                          (GtkCellArea           *area,
452                                                                     GtkDirectionType       direction);
453
454 /* GtkCellLayoutIface */
455 static void      gtk_cell_area_cell_layout_init              (GtkCellLayoutIface    *iface);
456 static void      gtk_cell_area_pack_default                  (GtkCellLayout         *cell_layout,
457                                                               GtkCellRenderer       *renderer,
458                                                               gboolean               expand);
459 static void      gtk_cell_area_clear                         (GtkCellLayout         *cell_layout);
460 static void      gtk_cell_area_add_attribute                 (GtkCellLayout         *cell_layout,
461                                                               GtkCellRenderer       *renderer,
462                                                               const gchar           *attribute,
463                                                               gint                   column);
464 static void      gtk_cell_area_set_cell_data_func            (GtkCellLayout         *cell_layout,
465                                                               GtkCellRenderer       *cell,
466                                                               GtkCellLayoutDataFunc  func,
467                                                               gpointer               func_data,
468                                                               GDestroyNotify         destroy);
469 static void      gtk_cell_area_clear_attributes              (GtkCellLayout         *cell_layout,
470                                                               GtkCellRenderer       *renderer);
471 static void      gtk_cell_area_reorder                       (GtkCellLayout         *cell_layout,
472                                                               GtkCellRenderer       *cell,
473                                                               gint                   position);
474 static GList    *gtk_cell_area_get_cells                     (GtkCellLayout         *cell_layout);
475 static GtkCellArea *gtk_cell_area_get_area                   (GtkCellLayout         *cell_layout);
476
477 /* GtkBuildableIface */
478 static void      gtk_cell_area_buildable_init                (GtkBuildableIface     *iface);
479 static void      gtk_cell_area_buildable_custom_tag_end      (GtkBuildable          *buildable,
480                                                               GtkBuilder            *builder,
481                                                               GObject               *child,
482                                                               const gchar           *tagname,
483                                                               gpointer              *data);
484
485 /* Used in foreach loop to check if a child renderer is present */
486 typedef struct {
487   GtkCellRenderer *renderer;
488   gboolean         has_renderer;
489 } HasRendererCheck;
490
491 /* Used in foreach loop to get a cell's allocation */
492 typedef struct {
493   GtkCellRenderer *renderer;
494   GdkRectangle     allocation;
495 } RendererAllocationData;
496
497 /* Used in foreach loop to render cells */
498 typedef struct {
499   GtkCellArea         *area;
500   GtkWidget           *widget;
501   cairo_t             *cr;
502   GdkRectangle         focus_rect;
503   GtkCellRendererState render_flags;
504   guint                paint_focus : 1;
505   guint                focus_all   : 1;
506   guint                first_focus : 1;
507 } CellRenderData;
508
509 /* Used in foreach loop to get a cell by position */
510 typedef struct {
511   gint             x;
512   gint             y;
513   GtkCellRenderer *renderer;
514   GdkRectangle     cell_area;
515 } CellByPositionData;
516
517 /* Attribute/Cell metadata */
518 typedef struct {
519   const gchar *attribute;
520   gint         column;
521 } CellAttribute;
522
523 typedef struct {
524   GSList          *attributes;
525
526   GtkCellLayoutDataFunc  func;
527   gpointer               data;
528   GDestroyNotify         destroy;
529   GtkCellLayout         *proxy;
530 } CellInfo;
531
532 static CellInfo       *cell_info_new       (GtkCellLayoutDataFunc  func,
533                                             gpointer               data,
534                                             GDestroyNotify         destroy);
535 static void            cell_info_free      (CellInfo              *info);
536 static CellAttribute  *cell_attribute_new  (GtkCellRenderer       *renderer,
537                                             const gchar           *attribute,
538                                             gint                   column);
539 static void            cell_attribute_free (CellAttribute         *attribute);
540 static gint            cell_attribute_find (CellAttribute         *cell_attribute,
541                                             const gchar           *attribute);
542
543 /* Internal functions/signal emissions */
544 static void            gtk_cell_area_add_editable     (GtkCellArea        *area,
545                                                        GtkCellRenderer    *renderer,
546                                                        GtkCellEditable    *editable,
547                                                        const GdkRectangle *cell_area);
548 static void            gtk_cell_area_remove_editable  (GtkCellArea        *area,
549                                                        GtkCellRenderer    *renderer,
550                                                        GtkCellEditable    *editable);
551 static void            gtk_cell_area_set_edit_widget  (GtkCellArea        *area,
552                                                        GtkCellEditable    *editable);
553 static void            gtk_cell_area_set_edited_cell  (GtkCellArea        *area,
554                                                        GtkCellRenderer    *renderer);
555
556
557 /* Struct to pass data along while looping over
558  * cell renderers to apply attributes
559  */
560 typedef struct {
561   GtkCellArea  *area;
562   GtkTreeModel *model;
563   GtkTreeIter  *iter;
564   gboolean      is_expander;
565   gboolean      is_expanded;
566 } AttributeData;
567
568 struct _GtkCellAreaPrivate
569 {
570   /* The GtkCellArea bookkeeps any connected
571    * attributes in this hash table.
572    */
573   GHashTable      *cell_info;
574
575   /* Current path is saved as a side-effect
576    * of gtk_cell_area_apply_attributes()
577    */
578   gchar           *current_path;
579
580   /* Current cell being edited and editable widget used */
581   GtkCellEditable *edit_widget;
582   GtkCellRenderer *edited_cell;
583
584   /* Signal connections to the editable widget */
585   gulong           remove_widget_id;
586
587   /* Currently focused cell */
588   GtkCellRenderer *focus_cell;
589
590   /* Tracking which cells are focus siblings of focusable cells */
591   GHashTable      *focus_siblings;
592 };
593
594 enum {
595   PROP_0,
596   PROP_FOCUS_CELL,
597   PROP_EDITED_CELL,
598   PROP_EDIT_WIDGET
599 };
600
601 enum {
602   SIGNAL_APPLY_ATTRIBUTES,
603   SIGNAL_ADD_EDITABLE,
604   SIGNAL_REMOVE_EDITABLE,
605   SIGNAL_FOCUS_CHANGED,
606   LAST_SIGNAL
607 };
608
609 /* Keep the paramspec pool internal, no need to deliver notifications
610  * on cells. at least no perceived need for now
611  */
612 static GParamSpecPool *cell_property_pool = NULL;
613 static guint           cell_area_signals[LAST_SIGNAL] = { 0 };
614
615 #define PARAM_SPEC_PARAM_ID(pspec)              ((pspec)->param_id)
616 #define PARAM_SPEC_SET_PARAM_ID(pspec, id)      ((pspec)->param_id = (id))
617
618 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkCellArea, gtk_cell_area, G_TYPE_INITIALLY_UNOWNED,
619                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
620                                                          gtk_cell_area_cell_layout_init)
621                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
622                                                          gtk_cell_area_buildable_init))
623
624 static void
625 gtk_cell_area_init (GtkCellArea *area)
626 {
627   GtkCellAreaPrivate *priv;
628
629   area->priv = G_TYPE_INSTANCE_GET_PRIVATE (area,
630                                             GTK_TYPE_CELL_AREA,
631                                             GtkCellAreaPrivate);
632   priv = area->priv;
633
634   priv->cell_info = g_hash_table_new_full (g_direct_hash,
635                                            g_direct_equal,
636                                            NULL,
637                                            (GDestroyNotify)cell_info_free);
638
639   priv->focus_siblings = g_hash_table_new_full (g_direct_hash,
640                                                 g_direct_equal,
641                                                 NULL,
642                                                 (GDestroyNotify)g_list_free);
643
644   priv->focus_cell         = NULL;
645   priv->edited_cell        = NULL;
646   priv->edit_widget        = NULL;
647
648   priv->remove_widget_id   = 0;
649 }
650
651 static void
652 gtk_cell_area_class_init (GtkCellAreaClass *class)
653 {
654   GObjectClass *object_class = G_OBJECT_CLASS (class);
655
656   /* GObjectClass */
657   object_class->dispose      = gtk_cell_area_dispose;
658   object_class->finalize     = gtk_cell_area_finalize;
659   object_class->get_property = gtk_cell_area_get_property;
660   object_class->set_property = gtk_cell_area_set_property;
661
662   /* general */
663   class->add              = gtk_cell_area_real_add;
664   class->remove           = gtk_cell_area_real_remove;
665   class->foreach          = gtk_cell_area_real_foreach;
666   class->foreach_alloc    = gtk_cell_area_real_foreach_alloc;
667   class->event            = gtk_cell_area_real_event;
668   class->render           = gtk_cell_area_real_render;
669   class->apply_attributes = gtk_cell_area_real_apply_attributes;
670
671   /* geometry */
672   class->create_context                 = gtk_cell_area_real_create_context;
673   class->copy_context                   = gtk_cell_area_real_copy_context;
674   class->get_request_mode               = gtk_cell_area_real_get_request_mode;
675   class->get_preferred_width            = gtk_cell_area_real_get_preferred_width;
676   class->get_preferred_height           = gtk_cell_area_real_get_preferred_height;
677   class->get_preferred_height_for_width = gtk_cell_area_real_get_preferred_height_for_width;
678   class->get_preferred_width_for_height = gtk_cell_area_real_get_preferred_width_for_height;
679
680   /* focus */
681   class->is_activatable = gtk_cell_area_real_is_activatable;
682   class->activate       = gtk_cell_area_real_activate;
683   class->focus          = gtk_cell_area_real_focus;
684
685   /* Signals */
686   /**
687    * GtkCellArea::apply-attributes:
688    * @area: the #GtkCellArea to apply the attributes to
689    * @model: the #GtkTreeModel to apply the attributes from
690    * @iter: the #GtkTreeIter indicating which row to apply the attributes of
691    * @is_expander: whether the view shows children for this row
692    * @is_expanded: whether the view is currently showing the children of this row
693    *
694    * This signal is emitted whenever applying attributes to @area from @model
695    *
696    * Since: 3.0
697    */
698   cell_area_signals[SIGNAL_APPLY_ATTRIBUTES] =
699     g_signal_new (I_("apply-attributes"),
700                   G_OBJECT_CLASS_TYPE (object_class),
701                   G_SIGNAL_RUN_FIRST,
702                   G_STRUCT_OFFSET (GtkCellAreaClass, apply_attributes),
703                   NULL, NULL,
704                   _gtk_marshal_VOID__OBJECT_BOXED_BOOLEAN_BOOLEAN,
705                   G_TYPE_NONE, 4,
706                   GTK_TYPE_TREE_MODEL,
707                   GTK_TYPE_TREE_ITER,
708                   G_TYPE_BOOLEAN,
709                   G_TYPE_BOOLEAN);
710   g_signal_set_va_marshaller (cell_area_signals[SIGNAL_APPLY_ATTRIBUTES], G_TYPE_FROM_CLASS (class),
711                               _gtk_marshal_VOID__OBJECT_BOXED_BOOLEAN_BOOLEANv);
712
713   /**
714    * GtkCellArea::add-editable:
715    * @area: the #GtkCellArea where editing started
716    * @renderer: the #GtkCellRenderer that started the edited
717    * @editable: the #GtkCellEditable widget to add
718    * @cell_area: the #GtkWidget relative #GdkRectangle coordinates
719    *             where @editable should be added
720    * @path: the #GtkTreePath string this edit was initiated for
721    *
722    * Indicates that editing has started on @renderer and that @editable
723    * should be added to the owning cell-layouting widget at @cell_area.
724    *
725    * Since: 3.0
726    */
727   cell_area_signals[SIGNAL_ADD_EDITABLE] =
728     g_signal_new (I_("add-editable"),
729                   G_OBJECT_CLASS_TYPE (object_class),
730                   G_SIGNAL_RUN_FIRST,
731                   0, /* No class closure here */
732                   NULL, NULL,
733                   _gtk_marshal_VOID__OBJECT_OBJECT_BOXED_STRING,
734                   G_TYPE_NONE, 4,
735                   GTK_TYPE_CELL_RENDERER,
736                   GTK_TYPE_CELL_EDITABLE,
737                   GDK_TYPE_RECTANGLE,
738                   G_TYPE_STRING);
739
740
741   /**
742    * GtkCellArea::remove-editable:
743    * @area: the #GtkCellArea where editing finished
744    * @renderer: the #GtkCellRenderer that finished editeding
745    * @editable: the #GtkCellEditable widget to remove
746    *
747    * Indicates that editing finished on @renderer and that @editable
748    * should be removed from the owning cell-layouting widget.
749    *
750    * Since: 3.0
751    */
752   cell_area_signals[SIGNAL_REMOVE_EDITABLE] =
753     g_signal_new (I_("remove-editable"),
754                   G_OBJECT_CLASS_TYPE (object_class),
755                   G_SIGNAL_RUN_FIRST,
756                   0, /* No class closure here */
757                   NULL, NULL,
758                   _gtk_marshal_VOID__OBJECT_OBJECT,
759                   G_TYPE_NONE, 2,
760                   GTK_TYPE_CELL_RENDERER,
761                   GTK_TYPE_CELL_EDITABLE);
762
763   /**
764    * GtkCellArea::focus-changed:
765    * @area: the #GtkCellArea where focus changed
766    * @renderer: the #GtkCellRenderer that has focus
767    * @path: the current #GtkTreePath string set for @area
768    *
769    * Indicates that focus changed on this @area. This signal
770    * is emitted either as a result of focus handling or event
771    * handling.
772    *
773    * It's possible that the signal is emitted even if the
774    * currently focused renderer did not change, this is
775    * because focus may change to the same renderer in the
776    * same cell area for a different row of data.
777    *
778    * Since: 3.0
779    */
780   cell_area_signals[SIGNAL_FOCUS_CHANGED] =
781     g_signal_new (I_("focus-changed"),
782                   G_OBJECT_CLASS_TYPE (object_class),
783                   G_SIGNAL_RUN_FIRST,
784                   0, /* No class closure here */
785                   NULL, NULL,
786                   _gtk_marshal_VOID__OBJECT_STRING,
787                   G_TYPE_NONE, 2,
788                   GTK_TYPE_CELL_RENDERER,
789                   G_TYPE_STRING);
790
791   /* Properties */
792   /**
793    * GtkCellArea:focus-cell:
794    *
795    * The cell in the area that currently has focus
796    *
797    * Since: 3.0
798    */
799   g_object_class_install_property (object_class,
800                                    PROP_FOCUS_CELL,
801                                    g_param_spec_object
802                                    ("focus-cell",
803                                     P_("Focus Cell"),
804                                     P_("The cell which currently has focus"),
805                                     GTK_TYPE_CELL_RENDERER,
806                                     GTK_PARAM_READWRITE));
807
808   /**
809    * GtkCellArea:edited-cell:
810    *
811    * The cell in the area that is currently edited
812    *
813    * This property is read-only and only changes as
814    * a result of a call gtk_cell_area_activate_cell().
815    *
816    * Since: 3.0
817    */
818   g_object_class_install_property (object_class,
819                                    PROP_EDITED_CELL,
820                                    g_param_spec_object
821                                    ("edited-cell",
822                                     P_("Edited Cell"),
823                                     P_("The cell which is currently being edited"),
824                                     GTK_TYPE_CELL_RENDERER,
825                                     G_PARAM_READABLE));
826
827   /**
828    * GtkCellArea:edit-widget:
829    *
830    * The widget currently editing the edited cell
831    *
832    * This property is read-only and only changes as
833    * a result of a call gtk_cell_area_activate_cell().
834    *
835    * Since: 3.0
836    */
837   g_object_class_install_property (object_class,
838                                    PROP_EDIT_WIDGET,
839                                    g_param_spec_object
840                                    ("edit-widget",
841                                     P_("Edit Widget"),
842                                     P_("The widget currently editing the edited cell"),
843                                     GTK_TYPE_CELL_EDITABLE,
844                                     G_PARAM_READABLE));
845
846   /* Pool for Cell Properties */
847   if (!cell_property_pool)
848     cell_property_pool = g_param_spec_pool_new (FALSE);
849
850   g_type_class_add_private (object_class, sizeof (GtkCellAreaPrivate));
851 }
852
853 /*************************************************************
854  *                    CellInfo Basics                        *
855  *************************************************************/
856 static CellInfo *
857 cell_info_new (GtkCellLayoutDataFunc  func,
858                gpointer               data,
859                GDestroyNotify         destroy)
860 {
861   CellInfo *info = g_slice_new0 (CellInfo);
862
863   info->func     = func;
864   info->data     = data;
865   info->destroy  = destroy;
866
867   return info;
868 }
869
870 static void
871 cell_info_free (CellInfo *info)
872 {
873   if (info->destroy)
874     info->destroy (info->data);
875
876   g_slist_foreach (info->attributes, (GFunc)cell_attribute_free, NULL);
877   g_slist_free (info->attributes);
878
879   g_slice_free (CellInfo, info);
880 }
881
882 static CellAttribute  *
883 cell_attribute_new  (GtkCellRenderer       *renderer,
884                      const gchar           *attribute,
885                      gint                   column)
886 {
887   GParamSpec *pspec;
888
889   /* Check if the attribute really exists and point to
890    * the property string installed on the cell renderer
891    * class (dont dup the string)
892    */
893   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (renderer), attribute);
894
895   if (pspec)
896     {
897       CellAttribute *cell_attribute = g_slice_new (CellAttribute);
898
899       cell_attribute->attribute = pspec->name;
900       cell_attribute->column    = column;
901
902       return cell_attribute;
903     }
904
905   return NULL;
906 }
907
908 static void
909 cell_attribute_free (CellAttribute *attribute)
910 {
911   g_slice_free (CellAttribute, attribute);
912 }
913
914 /* GCompareFunc for g_slist_find_custom() */
915 static gint
916 cell_attribute_find (CellAttribute *cell_attribute,
917                      const gchar   *attribute)
918 {
919   return g_strcmp0 (cell_attribute->attribute, attribute);
920 }
921
922 /*************************************************************
923  *                      GObjectClass                         *
924  *************************************************************/
925 static void
926 gtk_cell_area_finalize (GObject *object)
927 {
928   GtkCellArea        *area   = GTK_CELL_AREA (object);
929   GtkCellAreaPrivate *priv   = area->priv;
930
931   /* All cell renderers should already be removed at this point,
932    * just kill our (empty) hash tables here.
933    */
934   g_hash_table_destroy (priv->cell_info);
935   g_hash_table_destroy (priv->focus_siblings);
936
937   g_free (priv->current_path);
938
939   G_OBJECT_CLASS (gtk_cell_area_parent_class)->finalize (object);
940 }
941
942
943 static void
944 gtk_cell_area_dispose (GObject *object)
945 {
946   /* This removes every cell renderer that may be added to the GtkCellArea,
947    * subclasses should be breaking references to the GtkCellRenderers
948    * at this point.
949    */
950   gtk_cell_layout_clear (GTK_CELL_LAYOUT (object));
951
952   /* Remove any ref to a focused/edited cell */
953   gtk_cell_area_set_focus_cell (GTK_CELL_AREA (object), NULL);
954   gtk_cell_area_set_edited_cell (GTK_CELL_AREA (object), NULL);
955   gtk_cell_area_set_edit_widget (GTK_CELL_AREA (object), NULL);
956
957   G_OBJECT_CLASS (gtk_cell_area_parent_class)->dispose (object);
958 }
959
960 static void
961 gtk_cell_area_set_property (GObject       *object,
962                             guint          prop_id,
963                             const GValue  *value,
964                             GParamSpec    *pspec)
965 {
966   GtkCellArea *area = GTK_CELL_AREA (object);
967
968   switch (prop_id)
969     {
970     case PROP_FOCUS_CELL:
971       gtk_cell_area_set_focus_cell (area, (GtkCellRenderer *)g_value_get_object (value));
972       break;
973     default:
974       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
975       break;
976     }
977 }
978
979 static void
980 gtk_cell_area_get_property (GObject     *object,
981                             guint        prop_id,
982                             GValue      *value,
983                             GParamSpec  *pspec)
984 {
985   GtkCellArea        *area = GTK_CELL_AREA (object);
986   GtkCellAreaPrivate *priv = area->priv;
987
988   switch (prop_id)
989     {
990     case PROP_FOCUS_CELL:
991       g_value_set_object (value, priv->focus_cell);
992       break;
993     case PROP_EDITED_CELL:
994       g_value_set_object (value, priv->edited_cell);
995       break;
996     case PROP_EDIT_WIDGET:
997       g_value_set_object (value, priv->edit_widget);
998       break;
999     default:
1000       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1001       break;
1002     }
1003 }
1004
1005 /*************************************************************
1006  *                    GtkCellAreaClass                       *
1007  *************************************************************/
1008 static void
1009 gtk_cell_area_real_add (GtkCellArea         *area,
1010                         GtkCellRenderer     *renderer)
1011 {
1012     g_warning ("GtkCellAreaClass::add not implemented for `%s'",
1013                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1014 }
1015
1016 static void      
1017 gtk_cell_area_real_remove (GtkCellArea         *area,
1018                            GtkCellRenderer     *renderer)
1019 {
1020     g_warning ("GtkCellAreaClass::remove not implemented for `%s'",
1021                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1022 }
1023
1024 static void
1025 gtk_cell_area_real_foreach (GtkCellArea         *area,
1026                             GtkCellCallback      callback,
1027                             gpointer             callback_data)
1028 {
1029     g_warning ("GtkCellAreaClass::foreach not implemented for `%s'",
1030                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1031 }
1032
1033 static void
1034 gtk_cell_area_real_foreach_alloc (GtkCellArea         *area,
1035                                   GtkCellAreaContext  *context,
1036                                   GtkWidget           *widget,
1037                                   const GdkRectangle  *cell_area,
1038                                   const GdkRectangle  *background_area,
1039                                   GtkCellAllocCallback callback,
1040                                   gpointer             callback_data)
1041 {
1042     g_warning ("GtkCellAreaClass::foreach_alloc not implemented for `%s'",
1043                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1044 }
1045
1046 static gint
1047 gtk_cell_area_real_event (GtkCellArea          *area,
1048                           GtkCellAreaContext   *context,
1049                           GtkWidget            *widget,
1050                           GdkEvent             *event,
1051                           const GdkRectangle   *cell_area,
1052                           GtkCellRendererState  flags)
1053 {
1054   GtkCellAreaPrivate *priv = area->priv;
1055   gboolean            retval = FALSE;
1056
1057   if (event->type == GDK_KEY_PRESS && (flags & GTK_CELL_RENDERER_FOCUSED) != 0)
1058     {
1059       GdkEventKey *key_event = (GdkEventKey *)event;
1060
1061       /* Cancel any edits in progress */
1062       if (priv->edited_cell && (key_event->keyval == GDK_KEY_Escape))
1063         {
1064           gtk_cell_area_stop_editing (area, TRUE);
1065           retval = TRUE;
1066         }
1067     }
1068   else if (event->type == GDK_BUTTON_PRESS)
1069     {
1070       GdkEventButton *button_event = (GdkEventButton *)event;
1071
1072       if (button_event->button == GDK_BUTTON_PRIMARY)
1073         {
1074           GtkCellRenderer *renderer = NULL;
1075           GtkCellRenderer *focus_renderer;
1076           GdkRectangle     alloc_area;
1077           gint             event_x, event_y;
1078
1079           /* We may need some semantics to tell us the offset of the event
1080            * window we are handling events for (i.e. GtkTreeView has a bin_window) */
1081           event_x = button_event->x;
1082           event_y = button_event->y;
1083
1084           /* Dont try to search for an event coordinate that is not in the area, that will
1085            * trigger a runtime warning.
1086            */
1087           if (event_x >= cell_area->x && event_x <= cell_area->x + cell_area->width &&
1088               event_y >= cell_area->y && event_y <= cell_area->y + cell_area->height)
1089             renderer =
1090               gtk_cell_area_get_cell_at_position (area, context, widget,
1091                                                   cell_area, event_x, event_y,
1092                                                   &alloc_area);
1093
1094           if (renderer)
1095             {
1096               focus_renderer = gtk_cell_area_get_focus_from_sibling (area, renderer);
1097               if (!focus_renderer)
1098                 focus_renderer = renderer;
1099
1100               /* If we're already editing, cancel it and set focus */
1101               if (gtk_cell_area_get_edited_cell (area))
1102                 {
1103                   /* XXX Was it really canceled in this case ? */
1104                   gtk_cell_area_stop_editing (area, TRUE);
1105                   gtk_cell_area_set_focus_cell (area, focus_renderer);
1106                   retval = TRUE;
1107                 }
1108               else
1109                 {
1110                   /* If we are activating via a focus sibling,
1111                    * we need to fetch the right cell area for the real event renderer */
1112                   if (focus_renderer != renderer)
1113                     gtk_cell_area_get_cell_allocation (area, context, widget, focus_renderer,
1114                                                        cell_area, &alloc_area);
1115
1116                   gtk_cell_area_set_focus_cell (area, focus_renderer);
1117                   retval = gtk_cell_area_activate_cell (area, widget, focus_renderer,
1118                                                         event, &alloc_area, flags);
1119                 }
1120             }
1121         }
1122     }
1123
1124   return retval;
1125 }
1126
1127 static gboolean
1128 render_cell (GtkCellRenderer        *renderer,
1129              const GdkRectangle     *cell_area,
1130              const GdkRectangle     *cell_background,
1131              CellRenderData         *data)
1132 {
1133   GtkCellRenderer      *focus_cell;
1134   GtkCellRendererState  flags;
1135   GdkRectangle          inner_area;
1136
1137   focus_cell = gtk_cell_area_get_focus_cell (data->area);
1138   flags      = data->render_flags;
1139
1140   gtk_cell_area_inner_cell_area (data->area, data->widget, cell_area, &inner_area);
1141
1142   if ((flags & GTK_CELL_RENDERER_FOCUSED) &&
1143       (data->focus_all ||
1144        (focus_cell &&
1145         (renderer == focus_cell ||
1146          gtk_cell_area_is_focus_sibling (data->area, focus_cell, renderer)))))
1147     {
1148       gint focus_line_width;
1149       GdkRectangle cell_focus;
1150
1151       gtk_cell_renderer_get_aligned_area (renderer, data->widget, flags, &inner_area, &cell_focus);
1152
1153       gtk_widget_style_get (data->widget,
1154                             "focus-line-width", &focus_line_width,
1155                             NULL);
1156
1157       /* The focus rectangle is located around the aligned area of the cell */
1158       cell_focus.x -= focus_line_width;
1159       cell_focus.y -= focus_line_width;
1160       cell_focus.width += 2 * focus_line_width;
1161       cell_focus.height += 2 * focus_line_width;
1162
1163       if (data->first_focus)
1164         {
1165           data->first_focus = FALSE;
1166           data->focus_rect  = cell_focus;
1167         }
1168       else
1169         {
1170           gdk_rectangle_union (&data->focus_rect, &cell_focus, &data->focus_rect);
1171         }
1172     }
1173
1174   gtk_cell_renderer_render (renderer, data->cr, data->widget,
1175                             cell_background, &inner_area, flags);
1176
1177   return FALSE;
1178 }
1179
1180 static void
1181 gtk_cell_area_real_render (GtkCellArea          *area,
1182                            GtkCellAreaContext   *context,
1183                            GtkWidget            *widget,
1184                            cairo_t              *cr,
1185                            const GdkRectangle   *background_area,
1186                            const GdkRectangle   *cell_area,
1187                            GtkCellRendererState  flags,
1188                            gboolean              paint_focus)
1189 {
1190   CellRenderData render_data =
1191     {
1192       area,
1193       widget,
1194       cr,
1195       { 0, },
1196       flags,
1197       paint_focus,
1198       FALSE, TRUE
1199     };
1200
1201   /* Make sure we dont paint a focus rectangle while there
1202    * is an editable widget in play
1203    */
1204   if (gtk_cell_area_get_edited_cell (area))
1205     render_data.paint_focus = FALSE;
1206
1207   if (!gtk_widget_has_visible_focus (widget))
1208     render_data.paint_focus = FALSE;
1209
1210   /* If no cell can activate but the caller wants focus painted,
1211    * then we paint focus around all cells */
1212   if ((flags & GTK_CELL_RENDERER_FOCUSED) != 0 && paint_focus &&
1213       !gtk_cell_area_is_activatable (area))
1214     render_data.focus_all = TRUE;
1215
1216   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, background_area,
1217                                (GtkCellAllocCallback)render_cell, &render_data);
1218
1219   if (render_data.paint_focus &&
1220       render_data.focus_rect.width != 0 &&
1221       render_data.focus_rect.height != 0)
1222     {
1223       GtkStyleContext *style_context;
1224       GtkStateFlags renderer_state = 0;
1225
1226       style_context = gtk_widget_get_style_context (widget);
1227       gtk_style_context_save (style_context);
1228
1229       renderer_state = gtk_cell_renderer_get_state (NULL, widget, flags);
1230       gtk_style_context_set_state (style_context, renderer_state);
1231
1232       cairo_save (cr);
1233
1234       gdk_cairo_rectangle (cr, background_area);
1235       cairo_clip (cr);
1236
1237       gtk_render_focus (style_context, cr,
1238                         render_data.focus_rect.x,     render_data.focus_rect.y,
1239                         render_data.focus_rect.width, render_data.focus_rect.height);
1240
1241       gtk_style_context_restore (style_context);
1242       cairo_restore (cr);
1243     }
1244 }
1245
1246 static void
1247 apply_cell_attributes (GtkCellRenderer *renderer,
1248                        CellInfo        *info,
1249                        AttributeData   *data)
1250 {
1251   CellAttribute *attribute;
1252   GSList        *list;
1253   GValue         value = G_VALUE_INIT;
1254   gboolean       is_expander;
1255   gboolean       is_expanded;
1256
1257   g_object_freeze_notify (G_OBJECT (renderer));
1258
1259   /* Whether a row expands or is presently expanded can only be
1260    * provided by the view (as these states can vary across views
1261    * accessing the same model).
1262    */
1263   g_object_get (renderer, "is-expander", &is_expander, NULL);
1264   if (is_expander != data->is_expander)
1265     g_object_set (renderer, "is-expander", data->is_expander, NULL);
1266
1267   g_object_get (renderer, "is-expanded", &is_expanded, NULL);
1268   if (is_expanded != data->is_expanded)
1269     g_object_set (renderer, "is-expanded", data->is_expanded, NULL);
1270
1271   /* Apply the attributes directly to the renderer */
1272   for (list = info->attributes; list; list = list->next)
1273     {
1274       attribute = list->data;
1275
1276       gtk_tree_model_get_value (data->model, data->iter, attribute->column, &value);
1277       g_object_set_property (G_OBJECT (renderer), attribute->attribute, &value);
1278       g_value_unset (&value);
1279     }
1280
1281   /* Call any GtkCellLayoutDataFunc that may have been set by the user
1282    */
1283   if (info->func)
1284     info->func (info->proxy ? info->proxy : GTK_CELL_LAYOUT (data->area), renderer,
1285                 data->model, data->iter, info->data);
1286
1287   g_object_thaw_notify (G_OBJECT (renderer));
1288 }
1289
1290 static void
1291 gtk_cell_area_real_apply_attributes (GtkCellArea           *area,
1292                                      GtkTreeModel          *tree_model,
1293                                      GtkTreeIter           *iter,
1294                                      gboolean               is_expander,
1295                                      gboolean               is_expanded)
1296 {
1297
1298   GtkCellAreaPrivate *priv;
1299   AttributeData       data;
1300   GtkTreePath        *path;
1301
1302   priv = area->priv;
1303
1304   /* Feed in data needed to apply to every renderer */
1305   data.area        = area;
1306   data.model       = tree_model;
1307   data.iter        = iter;
1308   data.is_expander = is_expander;
1309   data.is_expanded = is_expanded;
1310
1311   /* Go over any cells that have attributes or custom GtkCellLayoutDataFuncs and
1312    * apply the data from the treemodel */
1313   g_hash_table_foreach (priv->cell_info, (GHFunc)apply_cell_attributes, &data);
1314
1315   /* Update the currently applied path */
1316   g_free (priv->current_path);
1317   path               = gtk_tree_model_get_path (tree_model, iter);
1318   priv->current_path = gtk_tree_path_to_string (path);
1319   gtk_tree_path_free (path);
1320 }
1321
1322 static GtkCellAreaContext *
1323 gtk_cell_area_real_create_context (GtkCellArea *area)
1324 {
1325   g_warning ("GtkCellAreaClass::create_context not implemented for `%s'",
1326              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1327
1328   return NULL;
1329 }
1330
1331 static GtkCellAreaContext *
1332 gtk_cell_area_real_copy_context (GtkCellArea        *area,
1333                                  GtkCellAreaContext *context)
1334 {
1335   g_warning ("GtkCellAreaClass::copy_context not implemented for `%s'",
1336              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1337
1338   return NULL;
1339 }
1340
1341 static GtkSizeRequestMode
1342 gtk_cell_area_real_get_request_mode (GtkCellArea *area)
1343 {
1344   /* By default cell areas are height-for-width. */
1345   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1346 }
1347
1348 static void
1349 gtk_cell_area_real_get_preferred_width (GtkCellArea        *area,
1350                                         GtkCellAreaContext *context,
1351                                         GtkWidget          *widget,
1352                                         gint               *minimum_width,
1353                                         gint               *natural_width)
1354 {
1355   g_warning ("GtkCellAreaClass::get_preferred_width not implemented for `%s'",
1356              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1357 }
1358
1359 static void
1360 gtk_cell_area_real_get_preferred_height (GtkCellArea        *area,
1361                                          GtkCellAreaContext *context,
1362                                          GtkWidget          *widget,
1363                                          gint               *minimum_height,
1364                                          gint               *natural_height)
1365 {
1366   g_warning ("GtkCellAreaClass::get_preferred_height not implemented for `%s'",
1367              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1368 }
1369
1370 static void
1371 gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea        *area,
1372                                                    GtkCellAreaContext *context,
1373                                                    GtkWidget          *widget,
1374                                                    gint                width,
1375                                                    gint               *minimum_height,
1376                                                    gint               *natural_height)
1377 {
1378   /* If the area doesnt do height-for-width, fallback on base preferred height */
1379   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_height (area, context, widget, minimum_height, natural_height);
1380 }
1381
1382 static void
1383 gtk_cell_area_real_get_preferred_width_for_height (GtkCellArea        *area,
1384                                                    GtkCellAreaContext *context,
1385                                                    GtkWidget          *widget,
1386                                                    gint                height,
1387                                                    gint               *minimum_width,
1388                                                    gint               *natural_width)
1389 {
1390   /* If the area doesnt do width-for-height, fallback on base preferred width */
1391   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_width (area, context, widget, minimum_width, natural_width);
1392 }
1393
1394 static gboolean
1395 get_is_activatable (GtkCellRenderer *renderer,
1396                     gboolean        *activatable)
1397 {
1398
1399   if (gtk_cell_renderer_is_activatable (renderer))
1400     *activatable = TRUE;
1401
1402   return *activatable;
1403 }
1404
1405 static gboolean
1406 gtk_cell_area_real_is_activatable (GtkCellArea *area)
1407 {
1408   gboolean activatable = FALSE;
1409
1410   /* Checks if any renderer can focus for the currently applied
1411    * attributes.
1412    *
1413    * Subclasses can override this in the case that they are also
1414    * rendering widgets as well as renderers.
1415    */
1416   gtk_cell_area_foreach (area, (GtkCellCallback)get_is_activatable, &activatable);
1417
1418   return activatable;
1419 }
1420
1421 static gboolean
1422 gtk_cell_area_real_activate (GtkCellArea         *area,
1423                              GtkCellAreaContext  *context,
1424                              GtkWidget           *widget,
1425                              const GdkRectangle  *cell_area,
1426                              GtkCellRendererState flags,
1427                              gboolean             edit_only)
1428 {
1429   GtkCellAreaPrivate *priv = area->priv;
1430   GdkRectangle        renderer_area;
1431   GtkCellRenderer    *activate_cell = NULL;
1432   GtkCellRendererMode mode;
1433
1434   if (priv->focus_cell)
1435     {
1436       g_object_get (priv->focus_cell, "mode", &mode, NULL);
1437
1438       if (gtk_cell_renderer_get_visible (priv->focus_cell) &&
1439           (edit_only ? mode == GTK_CELL_RENDERER_MODE_EDITABLE :
1440            mode != GTK_CELL_RENDERER_MODE_INERT))
1441         activate_cell = priv->focus_cell;
1442     }
1443   else
1444     {
1445       GList *cells, *l;
1446
1447       /* GtkTreeView sometimes wants to activate a cell when no
1448        * cells are in focus.
1449        */
1450       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
1451       for (l = cells; l && !activate_cell; l = l->next)
1452         {
1453           GtkCellRenderer *renderer = l->data;
1454
1455           g_object_get (renderer, "mode", &mode, NULL);
1456
1457           if (gtk_cell_renderer_get_visible (renderer) &&
1458               (edit_only ? mode == GTK_CELL_RENDERER_MODE_EDITABLE :
1459                mode != GTK_CELL_RENDERER_MODE_INERT))
1460             activate_cell = renderer;
1461         }
1462       g_list_free (cells);
1463     }
1464
1465   if (activate_cell)
1466     {
1467       /* Get the allocation of the focused cell.
1468        */
1469       gtk_cell_area_get_cell_allocation (area, context, widget, activate_cell,
1470                                          cell_area, &renderer_area);
1471
1472       /* Activate or Edit the cell
1473        *
1474        * Currently just not sending an event, renderers afaics dont use
1475        * the event argument anyway, worst case is we can synthesize one.
1476        */
1477       if (gtk_cell_area_activate_cell (area, widget, activate_cell, NULL,
1478                                        &renderer_area, flags))
1479         return TRUE;
1480     }
1481
1482   return FALSE;
1483 }
1484
1485 static gboolean
1486 gtk_cell_area_real_focus (GtkCellArea           *area,
1487                           GtkDirectionType       direction)
1488 {
1489   g_warning ("GtkCellAreaClass::focus not implemented for `%s'",
1490              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1491   return FALSE;
1492 }
1493
1494 /*************************************************************
1495  *                   GtkCellLayoutIface                      *
1496  *************************************************************/
1497 static void
1498 gtk_cell_area_cell_layout_init (GtkCellLayoutIface *iface)
1499 {
1500   iface->pack_start         = gtk_cell_area_pack_default;
1501   iface->pack_end           = gtk_cell_area_pack_default;
1502   iface->clear              = gtk_cell_area_clear;
1503   iface->add_attribute      = gtk_cell_area_add_attribute;
1504   iface->set_cell_data_func = gtk_cell_area_set_cell_data_func;
1505   iface->clear_attributes   = gtk_cell_area_clear_attributes;
1506   iface->reorder            = gtk_cell_area_reorder;
1507   iface->get_cells          = gtk_cell_area_get_cells;
1508   iface->get_area           = gtk_cell_area_get_area;
1509 }
1510
1511 static void
1512 gtk_cell_area_pack_default (GtkCellLayout         *cell_layout,
1513                             GtkCellRenderer       *renderer,
1514                             gboolean               expand)
1515 {
1516   gtk_cell_area_add (GTK_CELL_AREA (cell_layout), renderer);
1517 }
1518
1519 static void
1520 gtk_cell_area_clear (GtkCellLayout *cell_layout)
1521 {
1522   GtkCellArea *area = GTK_CELL_AREA (cell_layout);
1523   GList *l, *cells  =
1524     gtk_cell_layout_get_cells (cell_layout);
1525
1526   for (l = cells; l; l = l->next)
1527     {
1528       GtkCellRenderer *renderer = l->data;
1529       gtk_cell_area_remove (area, renderer);
1530     }
1531
1532   g_list_free (cells);
1533 }
1534
1535 static void
1536 gtk_cell_area_add_attribute (GtkCellLayout         *cell_layout,
1537                              GtkCellRenderer       *renderer,
1538                              const gchar           *attribute,
1539                              gint                   column)
1540 {
1541   gtk_cell_area_attribute_connect (GTK_CELL_AREA (cell_layout),
1542                                    renderer, attribute, column);
1543 }
1544
1545 static void
1546 gtk_cell_area_set_cell_data_func (GtkCellLayout         *cell_layout,
1547                                   GtkCellRenderer       *renderer,
1548                                   GtkCellLayoutDataFunc  func,
1549                                   gpointer               func_data,
1550                                   GDestroyNotify         destroy)
1551 {
1552   GtkCellArea *area   = GTK_CELL_AREA (cell_layout);
1553
1554   _gtk_cell_area_set_cell_data_func_with_proxy (area, renderer, (GFunc)func, func_data, destroy, NULL);
1555 }
1556
1557 static void
1558 gtk_cell_area_clear_attributes (GtkCellLayout         *cell_layout,
1559                                 GtkCellRenderer       *renderer)
1560 {
1561   GtkCellArea        *area = GTK_CELL_AREA (cell_layout);
1562   GtkCellAreaPrivate *priv = area->priv;
1563   CellInfo           *info;
1564
1565   info = g_hash_table_lookup (priv->cell_info, renderer);
1566
1567   if (info)
1568     {
1569       g_slist_foreach (info->attributes, (GFunc)cell_attribute_free, NULL);
1570       g_slist_free (info->attributes);
1571
1572       info->attributes = NULL;
1573     }
1574 }
1575
1576 static void
1577 gtk_cell_area_reorder (GtkCellLayout   *cell_layout,
1578                        GtkCellRenderer *cell,
1579                        gint             position)
1580 {
1581   g_warning ("GtkCellLayout::reorder not implemented for `%s'",
1582              g_type_name (G_TYPE_FROM_INSTANCE (cell_layout)));
1583 }
1584
1585 static gboolean
1586 accum_cells (GtkCellRenderer *renderer,
1587              GList          **accum)
1588 {
1589   *accum = g_list_prepend (*accum, renderer);
1590
1591   return FALSE;
1592 }
1593
1594 static GList *
1595 gtk_cell_area_get_cells (GtkCellLayout *cell_layout)
1596 {
1597   GList *cells = NULL;
1598
1599   gtk_cell_area_foreach (GTK_CELL_AREA (cell_layout),
1600                          (GtkCellCallback)accum_cells,
1601                          &cells);
1602
1603   return g_list_reverse (cells);
1604 }
1605
1606 static GtkCellArea *
1607 gtk_cell_area_get_area (GtkCellLayout *cell_layout)
1608 {
1609   return GTK_CELL_AREA (cell_layout);
1610 }
1611
1612 /*************************************************************
1613  *                   GtkBuildableIface                       *
1614  *************************************************************/
1615 static void
1616 gtk_cell_area_buildable_init (GtkBuildableIface *iface)
1617 {
1618   iface->add_child = _gtk_cell_layout_buildable_add_child;
1619   iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
1620   iface->custom_tag_end = gtk_cell_area_buildable_custom_tag_end;
1621 }
1622
1623 static void
1624 gtk_cell_area_buildable_custom_tag_end (GtkBuildable *buildable,
1625                                         GtkBuilder   *builder,
1626                                         GObject      *child,
1627                                         const gchar  *tagname,
1628                                         gpointer     *data)
1629 {
1630   /* Just ignore the boolean return from here */
1631   _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
1632 }
1633
1634 /*************************************************************
1635  *                            API                            *
1636  *************************************************************/
1637
1638 /**
1639  * gtk_cell_area_add:
1640  * @area: a #GtkCellArea
1641  * @renderer: the #GtkCellRenderer to add to @area
1642  *
1643  * Adds @renderer to @area with the default child cell properties.
1644  *
1645  * Since: 3.0
1646  */
1647 void
1648 gtk_cell_area_add (GtkCellArea        *area,
1649                    GtkCellRenderer    *renderer)
1650 {
1651   g_return_if_fail (GTK_IS_CELL_AREA (area));
1652   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1653
1654   GTK_CELL_AREA_GET_CLASS (area)->add (area, renderer);
1655 }
1656
1657 /**
1658  * gtk_cell_area_remove:
1659  * @area: a #GtkCellArea
1660  * @renderer: the #GtkCellRenderer to remove from @area
1661  *
1662  * Removes @renderer from @area.
1663  *
1664  * Since: 3.0
1665  */
1666 void
1667 gtk_cell_area_remove (GtkCellArea        *area,
1668                       GtkCellRenderer    *renderer)
1669 {
1670   GtkCellAreaPrivate *priv;
1671   GList              *renderers, *l;
1672
1673   g_return_if_fail (GTK_IS_CELL_AREA (area));
1674   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1675
1676   priv  = area->priv;
1677
1678   /* Remove any custom attributes and custom cell data func here first */
1679   g_hash_table_remove (priv->cell_info, renderer);
1680
1681   /* Remove focus siblings of this renderer */
1682   g_hash_table_remove (priv->focus_siblings, renderer);
1683
1684   /* Remove this renderer from any focus renderer's sibling list */
1685   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
1686
1687   for (l = renderers; l; l = l->next)
1688     {
1689       GtkCellRenderer *focus_renderer = l->data;
1690
1691       if (gtk_cell_area_is_focus_sibling (area, focus_renderer, renderer))
1692         {
1693           gtk_cell_area_remove_focus_sibling (area, focus_renderer, renderer);
1694           break;
1695         }
1696     }
1697
1698   g_list_free (renderers);
1699
1700   GTK_CELL_AREA_GET_CLASS (area)->remove (area, renderer);
1701 }
1702
1703 static gboolean
1704 get_has_renderer (GtkCellRenderer  *renderer,
1705                   HasRendererCheck *check)
1706 {
1707   if (renderer == check->renderer)
1708     check->has_renderer = TRUE;
1709
1710   return check->has_renderer;
1711 }
1712
1713 /**
1714  * gtk_cell_area_has_renderer:
1715  * @area: a #GtkCellArea
1716  * @renderer: the #GtkCellRenderer to check
1717  *
1718  * Checks if @area contains @renderer.
1719  *
1720  * Return value: %TRUE if @renderer is in the @area.
1721  *
1722  * Since: 3.0
1723  */
1724 gboolean
1725 gtk_cell_area_has_renderer (GtkCellArea     *area,
1726                             GtkCellRenderer *renderer)
1727 {
1728   HasRendererCheck check = { renderer, FALSE };
1729
1730   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
1731   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
1732
1733   gtk_cell_area_foreach (area, (GtkCellCallback)get_has_renderer, &check);
1734
1735   return check.has_renderer;
1736 }
1737
1738 /**
1739  * gtk_cell_area_foreach:
1740  * @area: a #GtkCellArea
1741  * @callback: (scope call): the #GtkCellCallback to call
1742  * @callback_data: user provided data pointer
1743  *
1744  * Calls @callback for every #GtkCellRenderer in @area.
1745  *
1746  * Since: 3.0
1747  */
1748 void
1749 gtk_cell_area_foreach (GtkCellArea        *area,
1750                        GtkCellCallback     callback,
1751                        gpointer            callback_data)
1752 {
1753   g_return_if_fail (GTK_IS_CELL_AREA (area));
1754   g_return_if_fail (callback != NULL);
1755
1756   GTK_CELL_AREA_GET_CLASS (area)->foreach (area, callback, callback_data);
1757 }
1758
1759 /**
1760  * gtk_cell_area_foreach_alloc:
1761  * @area: a #GtkCellArea
1762  * @context: the #GtkCellAreaContext for this row of data.
1763  * @widget: the #GtkWidget that @area is rendering to
1764  * @cell_area: the @widget relative coordinates and size for @area
1765  * @background_area: the @widget relative coordinates of the background area
1766  * @callback: (scope call): the #GtkCellAllocCallback to call
1767  * @callback_data: user provided data pointer
1768  *
1769  * Calls @callback for every #GtkCellRenderer in @area with the
1770  * allocated rectangle inside @cell_area.
1771  *
1772  * Since: 3.0
1773  */
1774 void
1775 gtk_cell_area_foreach_alloc (GtkCellArea          *area,
1776                              GtkCellAreaContext   *context,
1777                              GtkWidget            *widget,
1778                              const GdkRectangle   *cell_area,
1779                              const GdkRectangle   *background_area,
1780                              GtkCellAllocCallback  callback,
1781                              gpointer              callback_data)
1782 {
1783   g_return_if_fail (GTK_IS_CELL_AREA (area));
1784   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1785   g_return_if_fail (GTK_IS_WIDGET (widget));
1786   g_return_if_fail (cell_area != NULL);
1787   g_return_if_fail (callback != NULL);
1788
1789   GTK_CELL_AREA_GET_CLASS (area)->foreach_alloc (area, context, widget, 
1790                                                  cell_area, background_area, 
1791                                                  callback, callback_data);
1792 }
1793
1794 /**
1795  * gtk_cell_area_event:
1796  * @area: a #GtkCellArea
1797  * @context: the #GtkCellAreaContext for this row of data.
1798  * @widget: the #GtkWidget that @area is rendering to
1799  * @event: the #GdkEvent to handle
1800  * @cell_area: the @widget relative coordinates for @area
1801  * @flags: the #GtkCellRendererState for @area in this row.
1802  *
1803  * Delegates event handling to a #GtkCellArea.
1804  *
1805  * Return value: %TRUE if the event was handled by @area.
1806  *
1807  * Since: 3.0
1808  */
1809 gint
1810 gtk_cell_area_event (GtkCellArea          *area,
1811                      GtkCellAreaContext   *context,
1812                      GtkWidget            *widget,
1813                      GdkEvent             *event,
1814                      const GdkRectangle   *cell_area,
1815                      GtkCellRendererState  flags)
1816 {
1817   GtkCellAreaClass *class;
1818
1819   g_return_val_if_fail (GTK_IS_CELL_AREA (area), 0);
1820   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), 0);
1821   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
1822   g_return_val_if_fail (event != NULL, 0);
1823   g_return_val_if_fail (cell_area != NULL, 0);
1824
1825   class = GTK_CELL_AREA_GET_CLASS (area);
1826
1827   if (class->event)
1828     return class->event (area, context, widget, event, cell_area, flags);
1829
1830   g_warning ("GtkCellAreaClass::event not implemented for `%s'",
1831              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1832   return 0;
1833 }
1834
1835 /**
1836  * gtk_cell_area_render:
1837  * @area: a #GtkCellArea
1838  * @context: the #GtkCellAreaContext for this row of data.
1839  * @widget: the #GtkWidget that @area is rendering to
1840  * @cr: the #cairo_t to render with
1841  * @background_area: the @widget relative coordinates for @area's background
1842  * @cell_area: the @widget relative coordinates for @area
1843  * @flags: the #GtkCellRendererState for @area in this row.
1844  * @paint_focus: whether @area should paint focus on focused cells for focused rows or not.
1845  *
1846  * Renders @area's cells according to @area's layout onto @widget at
1847  * the given coordinates.
1848  *
1849  * Since: 3.0
1850  */
1851 void
1852 gtk_cell_area_render (GtkCellArea          *area,
1853                       GtkCellAreaContext   *context,
1854                       GtkWidget            *widget,
1855                       cairo_t              *cr,
1856                       const GdkRectangle   *background_area,
1857                       const GdkRectangle   *cell_area,
1858                       GtkCellRendererState  flags,
1859                       gboolean              paint_focus)
1860 {
1861   GtkCellAreaClass *class;
1862
1863   g_return_if_fail (GTK_IS_CELL_AREA (area));
1864   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1865   g_return_if_fail (GTK_IS_WIDGET (widget));
1866   g_return_if_fail (cr != NULL);
1867   g_return_if_fail (background_area != NULL);
1868   g_return_if_fail (cell_area != NULL);
1869
1870   class = GTK_CELL_AREA_GET_CLASS (area);
1871
1872   if (class->render)
1873     class->render (area, context, widget, cr, background_area, cell_area, flags, paint_focus);
1874   else
1875     g_warning ("GtkCellAreaClass::render not implemented for `%s'",
1876                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1877 }
1878
1879 static gboolean
1880 get_cell_allocation (GtkCellRenderer        *renderer,
1881                      const GdkRectangle     *cell_area,
1882                      const GdkRectangle     *cell_background,
1883                      RendererAllocationData *data)
1884 {
1885   if (data->renderer == renderer)
1886     data->allocation = *cell_area;
1887
1888   return (data->renderer == renderer);
1889 }
1890
1891 /**
1892  * gtk_cell_area_get_cell_allocation:
1893  * @area: a #GtkCellArea
1894  * @context: the #GtkCellAreaContext used to hold sizes for @area.
1895  * @widget: the #GtkWidget that @area is rendering on
1896  * @renderer: the #GtkCellRenderer to get the allocation for
1897  * @cell_area: the whole allocated area for @area in @widget
1898  *             for this row
1899  * @allocation: (out): where to store the allocation for @renderer
1900  *
1901  * Derives the allocation of @renderer inside @area if @area
1902  * were to be renderered in @cell_area.
1903  *
1904  * Since: 3.0
1905  */
1906 void
1907 gtk_cell_area_get_cell_allocation (GtkCellArea          *area,
1908                                    GtkCellAreaContext   *context,
1909                                    GtkWidget            *widget,
1910                                    GtkCellRenderer      *renderer,
1911                                    const GdkRectangle   *cell_area,
1912                                    GdkRectangle         *allocation)
1913 {
1914   RendererAllocationData data = { renderer, { 0, } };
1915
1916   g_return_if_fail (GTK_IS_CELL_AREA (area));
1917   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1918   g_return_if_fail (GTK_IS_WIDGET (widget));
1919   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1920   g_return_if_fail (cell_area != NULL);
1921   g_return_if_fail (allocation != NULL);
1922
1923   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, cell_area,
1924                                (GtkCellAllocCallback)get_cell_allocation, &data);
1925
1926   *allocation = data.allocation;
1927 }
1928
1929 static gboolean
1930 get_cell_by_position (GtkCellRenderer     *renderer,
1931                       const GdkRectangle  *cell_area,
1932                       const GdkRectangle  *cell_background,
1933                       CellByPositionData  *data)
1934 {
1935   if (data->x >= cell_area->x && data->x < cell_area->x + cell_area->width &&
1936       data->y >= cell_area->y && data->y < cell_area->y + cell_area->height)
1937     {
1938       data->renderer  = renderer;
1939       data->cell_area = *cell_area;
1940     }
1941
1942   return (data->renderer != NULL);
1943 }
1944
1945 /**
1946  * gtk_cell_area_get_cell_at_position:
1947  * @area: a #GtkCellArea
1948  * @context: the #GtkCellAreaContext used to hold sizes for @area.
1949  * @widget: the #GtkWidget that @area is rendering on
1950  * @cell_area: the whole allocated area for @area in @widget
1951  *             for this row
1952  * @x: the x position
1953  * @y: the y position
1954  * @alloc_area: (out) (allow-none): where to store the inner allocated area of the
1955  *                                  returned cell renderer, or %NULL.
1956  *
1957  * Gets the #GtkCellRenderer at @x and @y coordinates inside @area and optionally
1958  * returns the full cell allocation for it inside @cell_area.
1959  *
1960  * Return value: (transfer none): the #GtkCellRenderer at @x and @y.
1961  *
1962  * Since: 3.0
1963  */
1964 GtkCellRenderer *
1965 gtk_cell_area_get_cell_at_position (GtkCellArea          *area,
1966                                     GtkCellAreaContext   *context,
1967                                     GtkWidget            *widget,
1968                                     const GdkRectangle   *cell_area,
1969                                     gint                  x,
1970                                     gint                  y,
1971                                     GdkRectangle         *alloc_area)
1972 {
1973   CellByPositionData data = { x, y, NULL, { 0, } };
1974
1975   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
1976   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), NULL);
1977   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1978   g_return_val_if_fail (cell_area != NULL, NULL);
1979   g_return_val_if_fail (x >= cell_area->x && x <= cell_area->x + cell_area->width, NULL);
1980   g_return_val_if_fail (y >= cell_area->y && y <= cell_area->y + cell_area->height, NULL);
1981
1982   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, cell_area,
1983                                (GtkCellAllocCallback)get_cell_by_position, &data);
1984
1985   if (alloc_area)
1986     *alloc_area = data.cell_area;
1987
1988   return data.renderer;
1989 }
1990
1991 /*************************************************************
1992  *                      API: Geometry                        *
1993  *************************************************************/
1994 /**
1995  * gtk_cell_area_create_context:
1996  * @area: a #GtkCellArea
1997  *
1998  * Creates a #GtkCellAreaContext to be used with @area for
1999  * all purposes. #GtkCellAreaContext stores geometry information
2000  * for rows for which it was operated on, it is important to use
2001  * the same context for the same row of data at all times (i.e.
2002  * one should render and handle events with the same #GtkCellAreaContext
2003  * which was used to request the size of those rows of data).
2004  *
2005  * Return value: (transfer full): a newly created #GtkCellAreaContext which can be used with @area.
2006  *
2007  * Since: 3.0
2008  */
2009 GtkCellAreaContext *
2010 gtk_cell_area_create_context (GtkCellArea *area)
2011 {
2012   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
2013
2014   return GTK_CELL_AREA_GET_CLASS (area)->create_context (area);
2015 }
2016
2017 /**
2018  * gtk_cell_area_copy_context:
2019  * @area: a #GtkCellArea
2020  * @context: the #GtkCellAreaContext to copy
2021  *
2022  * This is sometimes needed for cases where rows need to share
2023  * alignments in one orientation but may be separately grouped
2024  * in the opposing orientation.
2025  *
2026  * For instance, #GtkIconView creates all icons (rows) to have
2027  * the same width and the cells theirin to have the same
2028  * horizontal alignments. However each row of icons may have
2029  * a separate collective height. #GtkIconView uses this to
2030  * request the heights of each row based on a context which
2031  * was already used to request all the row widths that are
2032  * to be displayed.
2033  *
2034  * Return value: (transfer full): a newly created #GtkCellAreaContext copy of @context.
2035  *
2036  * Since: 3.0
2037  */
2038 GtkCellAreaContext *
2039 gtk_cell_area_copy_context (GtkCellArea        *area,
2040                             GtkCellAreaContext *context)
2041 {
2042   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
2043   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), NULL);
2044
2045   return GTK_CELL_AREA_GET_CLASS (area)->copy_context (area, context);
2046 }
2047
2048 /**
2049  * gtk_cell_area_get_request_mode:
2050  * @area: a #GtkCellArea
2051  *
2052  * Gets whether the area prefers a height-for-width layout
2053  * or a width-for-height layout.
2054  *
2055  * Return value: The #GtkSizeRequestMode preferred by @area.
2056  *
2057  * Since: 3.0
2058  */
2059 GtkSizeRequestMode
2060 gtk_cell_area_get_request_mode (GtkCellArea *area)
2061 {
2062   g_return_val_if_fail (GTK_IS_CELL_AREA (area),
2063                         GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
2064
2065   return GTK_CELL_AREA_GET_CLASS (area)->get_request_mode (area);
2066 }
2067
2068 /**
2069  * gtk_cell_area_get_preferred_width:
2070  * @area: a #GtkCellArea
2071  * @context: the #GtkCellAreaContext to perform this request with
2072  * @widget: the #GtkWidget where @area will be rendering
2073  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
2074  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
2075  *
2076  * Retrieves a cell area's initial minimum and natural width.
2077  *
2078  * @area will store some geometrical information in @context along the way;
2079  * when requesting sizes over an arbitrary number of rows, it's not important
2080  * to check the @minimum_width and @natural_width of this call but rather to
2081  * consult gtk_cell_area_context_get_preferred_width() after a series of
2082  * requests.
2083  *
2084  * Since: 3.0
2085  */
2086 void
2087 gtk_cell_area_get_preferred_width (GtkCellArea        *area,
2088                                    GtkCellAreaContext *context,
2089                                    GtkWidget          *widget,
2090                                    gint               *minimum_width,
2091                                    gint               *natural_width)
2092 {
2093   g_return_if_fail (GTK_IS_CELL_AREA (area));
2094   g_return_if_fail (GTK_IS_WIDGET (widget));
2095
2096   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_width (area, context, widget, 
2097                                                        minimum_width, natural_width);
2098 }
2099
2100 /**
2101  * gtk_cell_area_get_preferred_height_for_width:
2102  * @area: a #GtkCellArea
2103  * @context: the #GtkCellAreaContext which has already been requested for widths.
2104  * @widget: the #GtkWidget where @area will be rendering
2105  * @width: the width for which to check the height of this area
2106  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
2107  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
2108  *
2109  * Retrieves a cell area's minimum and natural height if it would be given
2110  * the specified @width.
2111  *
2112  * @area stores some geometrical information in @context along the way
2113  * while calling gtk_cell_area_get_preferred_width(). It's important to
2114  * perform a series of gtk_cell_area_get_preferred_width() requests with
2115  * @context first and then call gtk_cell_area_get_preferred_height_for_width()
2116  * on each cell area individually to get the height for width of each
2117  * fully requested row.
2118  *
2119  * If at some point, the width of a single row changes, it should be
2120  * requested with gtk_cell_area_get_preferred_width() again and then
2121  * the full width of the requested rows checked again with
2122  * gtk_cell_area_context_get_preferred_width().
2123  *
2124  * Since: 3.0
2125  */
2126 void
2127 gtk_cell_area_get_preferred_height_for_width (GtkCellArea        *area,
2128                                               GtkCellAreaContext *context,
2129                                               GtkWidget          *widget,
2130                                               gint                width,
2131                                               gint               *minimum_height,
2132                                               gint               *natural_height)
2133 {
2134   GtkCellAreaClass *class;
2135
2136   g_return_if_fail (GTK_IS_CELL_AREA (area));
2137   g_return_if_fail (GTK_IS_WIDGET (widget));
2138
2139   class = GTK_CELL_AREA_GET_CLASS (area);
2140   class->get_preferred_height_for_width (area, context, widget, width, minimum_height, natural_height);
2141 }
2142
2143
2144 /**
2145  * gtk_cell_area_get_preferred_height:
2146  * @area: a #GtkCellArea
2147  * @context: the #GtkCellAreaContext to perform this request with
2148  * @widget: the #GtkWidget where @area will be rendering
2149  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
2150  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
2151  *
2152  * Retrieves a cell area's initial minimum and natural height.
2153  *
2154  * @area will store some geometrical information in @context along the way;
2155  * when requesting sizes over an arbitrary number of rows, it's not important
2156  * to check the @minimum_height and @natural_height of this call but rather to
2157  * consult gtk_cell_area_context_get_preferred_height() after a series of
2158  * requests.
2159  *
2160  * Since: 3.0
2161  */
2162 void
2163 gtk_cell_area_get_preferred_height (GtkCellArea        *area,
2164                                     GtkCellAreaContext *context,
2165                                     GtkWidget          *widget,
2166                                     gint               *minimum_height,
2167                                     gint               *natural_height)
2168 {
2169   g_return_if_fail (GTK_IS_CELL_AREA (area));
2170   g_return_if_fail (GTK_IS_WIDGET (widget));
2171
2172   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_height (area, context, widget, 
2173                                                         minimum_height, natural_height);
2174 }
2175
2176 /**
2177  * gtk_cell_area_get_preferred_width_for_height:
2178  * @area: a #GtkCellArea
2179  * @context: the #GtkCellAreaContext which has already been requested for widths.
2180  * @widget: the #GtkWidget where @area will be rendering
2181  * @height: the height for which to check the width of this area
2182  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
2183  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
2184  *
2185  * Retrieves a cell area's minimum and natural width if it would be given
2186  * the specified @height.
2187  *
2188  * @area stores some geometrical information in @context along the way
2189  * while calling gtk_cell_area_get_preferred_height(). It's important to
2190  * perform a series of gtk_cell_area_get_preferred_height() requests with
2191  * @context first and then call gtk_cell_area_get_preferred_width_for_height()
2192  * on each cell area individually to get the height for width of each
2193  * fully requested row.
2194  *
2195  * If at some point, the height of a single row changes, it should be
2196  * requested with gtk_cell_area_get_preferred_height() again and then
2197  * the full height of the requested rows checked again with
2198  * gtk_cell_area_context_get_preferred_height().
2199  *
2200  * Since: 3.0
2201  */
2202 void
2203 gtk_cell_area_get_preferred_width_for_height (GtkCellArea        *area,
2204                                               GtkCellAreaContext *context,
2205                                               GtkWidget          *widget,
2206                                               gint                height,
2207                                               gint               *minimum_width,
2208                                               gint               *natural_width)
2209 {
2210   GtkCellAreaClass *class;
2211
2212   g_return_if_fail (GTK_IS_CELL_AREA (area));
2213   g_return_if_fail (GTK_IS_WIDGET (widget));
2214
2215   class = GTK_CELL_AREA_GET_CLASS (area);
2216   class->get_preferred_width_for_height (area, context, widget, height, minimum_width, natural_width);
2217 }
2218
2219 /*************************************************************
2220  *                      API: Attributes                      *
2221  *************************************************************/
2222
2223 /**
2224  * gtk_cell_area_attribute_connect:
2225  * @area: a #GtkCellArea
2226  * @renderer: the #GtkCellRenderer to connect an attribute for
2227  * @attribute: the attribute name
2228  * @column: the #GtkTreeModel column to fetch attribute values from
2229  *
2230  * Connects an @attribute to apply values from @column for the
2231  * #GtkTreeModel in use.
2232  *
2233  * Since: 3.0
2234  */
2235 void
2236 gtk_cell_area_attribute_connect (GtkCellArea        *area,
2237                                  GtkCellRenderer    *renderer,
2238                                  const gchar        *attribute,
2239                                  gint                column)
2240 {
2241   GtkCellAreaPrivate *priv;
2242   CellInfo           *info;
2243   CellAttribute      *cell_attribute;
2244
2245   g_return_if_fail (GTK_IS_CELL_AREA (area));
2246   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2247   g_return_if_fail (attribute != NULL);
2248   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
2249
2250   priv = area->priv;
2251   info = g_hash_table_lookup (priv->cell_info, renderer);
2252
2253   if (!info)
2254     {
2255       info = cell_info_new (NULL, NULL, NULL);
2256
2257       g_hash_table_insert (priv->cell_info, renderer, info);
2258     }
2259   else
2260     {
2261       GSList *node;
2262
2263       /* Check we are not adding the same attribute twice */
2264       if ((node = g_slist_find_custom (info->attributes, attribute,
2265                                        (GCompareFunc)cell_attribute_find)) != NULL)
2266         {
2267           cell_attribute = node->data;
2268
2269           g_warning ("Cannot connect attribute `%s' for cell renderer class `%s' "
2270                      "since `%s' is already attributed to column %d",
2271                      attribute,
2272                      G_OBJECT_TYPE_NAME (renderer),
2273                      attribute, cell_attribute->column);
2274           return;
2275         }
2276     }
2277
2278   cell_attribute = cell_attribute_new (renderer, attribute, column);
2279
2280   if (!cell_attribute)
2281     {
2282       g_warning ("Cannot connect attribute `%s' for cell renderer class `%s' "
2283                  "since attribute does not exist",
2284                  attribute,
2285                  G_OBJECT_TYPE_NAME (renderer));
2286       return;
2287     }
2288
2289   info->attributes = g_slist_prepend (info->attributes, cell_attribute);
2290 }
2291
2292 /**
2293  * gtk_cell_area_attribute_disconnect:
2294  * @area: a #GtkCellArea
2295  * @renderer: the #GtkCellRenderer to disconnect an attribute for
2296  * @attribute: the attribute name
2297  *
2298  * Disconnects @attribute for the @renderer in @area so that
2299  * attribute will no longer be updated with values from the
2300  * model.
2301  *
2302  * Since: 3.0
2303  */
2304 void
2305 gtk_cell_area_attribute_disconnect (GtkCellArea        *area,
2306                                     GtkCellRenderer    *renderer,
2307                                     const gchar        *attribute)
2308 {
2309   GtkCellAreaPrivate *priv;
2310   CellInfo           *info;
2311   CellAttribute      *cell_attribute;
2312   GSList             *node;
2313
2314   g_return_if_fail (GTK_IS_CELL_AREA (area));
2315   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2316   g_return_if_fail (attribute != NULL);
2317   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
2318
2319   priv = area->priv;
2320   info = g_hash_table_lookup (priv->cell_info, renderer);
2321
2322   if (info)
2323     {
2324       node = g_slist_find_custom (info->attributes, attribute,
2325                                   (GCompareFunc)cell_attribute_find);
2326       if (node)
2327         {
2328           cell_attribute = node->data;
2329
2330           cell_attribute_free (cell_attribute);
2331
2332           info->attributes = g_slist_delete_link (info->attributes, node);
2333         }
2334     }
2335 }
2336
2337 /**
2338  * gtk_cell_area_apply_attributes:
2339  * @area: a #GtkCellArea
2340  * @tree_model: the #GtkTreeModel to pull values from
2341  * @iter: the #GtkTreeIter in @tree_model to apply values for
2342  * @is_expander: whether @iter has children
2343  * @is_expanded: whether @iter is expanded in the view and
2344  *               children are visible
2345  *
2346  * Applies any connected attributes to the renderers in
2347  * @area by pulling the values from @tree_model.
2348  *
2349  * Since: 3.0
2350  */
2351 void
2352 gtk_cell_area_apply_attributes (GtkCellArea  *area,
2353                                 GtkTreeModel *tree_model,
2354                                 GtkTreeIter  *iter,
2355                                 gboolean      is_expander,
2356                                 gboolean      is_expanded)
2357 {
2358   g_return_if_fail (GTK_IS_CELL_AREA (area));
2359   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
2360   g_return_if_fail (iter != NULL);
2361
2362   g_signal_emit (area, cell_area_signals[SIGNAL_APPLY_ATTRIBUTES], 0,
2363                  tree_model, iter, is_expander, is_expanded);
2364 }
2365
2366 /**
2367  * gtk_cell_area_get_current_path_string:
2368  * @area: a #GtkCellArea
2369  *
2370  * Gets the current #GtkTreePath string for the currently
2371  * applied #GtkTreeIter, this is implicitly updated when
2372  * gtk_cell_area_apply_attributes() is called and can be
2373  * used to interact with renderers from #GtkCellArea
2374  * subclasses.
2375  *
2376  * Return value: The current #GtkTreePath string for the current
2377  * attributes applied to @area. This string belongs to the area and
2378  * should not be freed.
2379  *
2380  * Since: 3.0
2381  */
2382 const gchar *
2383 gtk_cell_area_get_current_path_string (GtkCellArea *area)
2384 {
2385   GtkCellAreaPrivate *priv;
2386
2387   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
2388
2389   priv = area->priv;
2390
2391   return priv->current_path;
2392 }
2393
2394
2395 /*************************************************************
2396  *                    API: Cell Properties                   *
2397  *************************************************************/
2398 /**
2399  * gtk_cell_area_class_install_cell_property:
2400  * @aclass: a #GtkCellAreaClass
2401  * @property_id: the id for the property
2402  * @pspec: the #GParamSpec for the property
2403  *
2404  * Installs a cell property on a cell area class.
2405  *
2406  * Since: 3.0
2407  */
2408 void
2409 gtk_cell_area_class_install_cell_property (GtkCellAreaClass   *aclass,
2410                                            guint               property_id,
2411                                            GParamSpec         *pspec)
2412 {
2413   g_return_if_fail (GTK_IS_CELL_AREA_CLASS (aclass));
2414   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
2415   if (pspec->flags & G_PARAM_WRITABLE)
2416     g_return_if_fail (aclass->set_cell_property != NULL);
2417   if (pspec->flags & G_PARAM_READABLE)
2418     g_return_if_fail (aclass->get_cell_property != NULL);
2419   g_return_if_fail (property_id > 0);
2420   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
2421   g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
2422
2423   if (g_param_spec_pool_lookup (cell_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (aclass), TRUE))
2424     {
2425       g_warning (G_STRLOC ": class `%s' already contains a cell property named `%s'",
2426                  G_OBJECT_CLASS_NAME (aclass), pspec->name);
2427       return;
2428     }
2429   g_param_spec_ref (pspec);
2430   g_param_spec_sink (pspec);
2431   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
2432   g_param_spec_pool_insert (cell_property_pool, pspec, G_OBJECT_CLASS_TYPE (aclass));
2433 }
2434
2435 /**
2436  * gtk_cell_area_class_find_cell_property:
2437  * @aclass: a #GtkCellAreaClass
2438  * @property_name: the name of the child property to find
2439  *
2440  * Finds a cell property of a cell area class by name.
2441  *
2442  * Return value: (transfer none): the #GParamSpec of the child property
2443  *   or %NULL if @aclass has no child property with that name.
2444  *
2445  * Since: 3.0
2446  */
2447 GParamSpec*
2448 gtk_cell_area_class_find_cell_property (GtkCellAreaClass   *aclass,
2449                                         const gchar        *property_name)
2450 {
2451   g_return_val_if_fail (GTK_IS_CELL_AREA_CLASS (aclass), NULL);
2452   g_return_val_if_fail (property_name != NULL, NULL);
2453
2454   return g_param_spec_pool_lookup (cell_property_pool,
2455                                    property_name,
2456                                    G_OBJECT_CLASS_TYPE (aclass),
2457                                    TRUE);
2458 }
2459
2460 /**
2461  * gtk_cell_area_class_list_cell_properties:
2462  * @aclass: a #GtkCellAreaClass
2463  * @n_properties: location to return the number of cell properties found
2464  *
2465  * Returns all cell properties of a cell area class.
2466  *
2467  * Return value: (array length=n_properties) (transfer container): a newly
2468  *     allocated %NULL-terminated array of #GParamSpec*.  The array
2469  *     must be freed with g_free().
2470  *
2471  * Since: 3.0
2472  */
2473 GParamSpec**
2474 gtk_cell_area_class_list_cell_properties (GtkCellAreaClass  *aclass,
2475                                           guint             *n_properties)
2476 {
2477   GParamSpec **pspecs;
2478   guint n;
2479
2480   g_return_val_if_fail (GTK_IS_CELL_AREA_CLASS (aclass), NULL);
2481
2482   pspecs = g_param_spec_pool_list (cell_property_pool,
2483                                    G_OBJECT_CLASS_TYPE (aclass),
2484                                    &n);
2485   if (n_properties)
2486     *n_properties = n;
2487
2488   return pspecs;
2489 }
2490
2491 /**
2492  * gtk_cell_area_add_with_properties:
2493  * @area: a #GtkCellArea
2494  * @renderer: a #GtkCellRenderer to be placed inside @area
2495  * @first_prop_name: the name of the first cell property to set
2496  * @...: a %NULL-terminated list of property names and values, starting
2497  *     with @first_prop_name
2498  *
2499  * Adds @renderer to @area, setting cell properties at the same time.
2500  * See gtk_cell_area_add() and gtk_cell_area_cell_set() for more details.
2501  *
2502  * Since: 3.0
2503  */
2504 void
2505 gtk_cell_area_add_with_properties (GtkCellArea        *area,
2506                                    GtkCellRenderer    *renderer,
2507                                    const gchar        *first_prop_name,
2508                                    ...)
2509 {
2510   GtkCellAreaClass *class;
2511
2512   g_return_if_fail (GTK_IS_CELL_AREA (area));
2513   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2514
2515   class = GTK_CELL_AREA_GET_CLASS (area);
2516
2517   if (class->add)
2518     {
2519       va_list var_args;
2520
2521       class->add (area, renderer);
2522
2523       va_start (var_args, first_prop_name);
2524       gtk_cell_area_cell_set_valist (area, renderer, first_prop_name, var_args);
2525       va_end (var_args);
2526     }
2527   else
2528     g_warning ("GtkCellAreaClass::add not implemented for `%s'",
2529                g_type_name (G_TYPE_FROM_INSTANCE (area)));
2530 }
2531
2532 /**
2533  * gtk_cell_area_cell_set:
2534  * @area: a #GtkCellArea
2535  * @renderer: a #GtkCellRenderer which is a cell inside @area
2536  * @first_prop_name: the name of the first cell property to set
2537  * @...: a %NULL-terminated list of property names and values, starting
2538  *           with @first_prop_name
2539  *
2540  * Sets one or more cell properties for @cell in @area.
2541  *
2542  * Since: 3.0
2543  */
2544 void
2545 gtk_cell_area_cell_set (GtkCellArea        *area,
2546                         GtkCellRenderer    *renderer,
2547                         const gchar        *first_prop_name,
2548                         ...)
2549 {
2550   va_list var_args;
2551
2552   g_return_if_fail (GTK_IS_CELL_AREA (area));
2553   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2554
2555   va_start (var_args, first_prop_name);
2556   gtk_cell_area_cell_set_valist (area, renderer, first_prop_name, var_args);
2557   va_end (var_args);
2558 }
2559
2560 /**
2561  * gtk_cell_area_cell_get:
2562  * @area: a #GtkCellArea
2563  * @renderer: a #GtkCellRenderer which is inside @area
2564  * @first_prop_name: the name of the first cell property to get
2565  * @...: return location for the first cell property, followed
2566  *     optionally by more name/return location pairs, followed by %NULL
2567  *
2568  * Gets the values of one or more cell properties for @renderer in @area.
2569  *
2570  * Since: 3.0
2571  */
2572 void
2573 gtk_cell_area_cell_get (GtkCellArea        *area,
2574                         GtkCellRenderer    *renderer,
2575                         const gchar        *first_prop_name,
2576                         ...)
2577 {
2578   va_list var_args;
2579
2580   g_return_if_fail (GTK_IS_CELL_AREA (area));
2581   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2582
2583   va_start (var_args, first_prop_name);
2584   gtk_cell_area_cell_get_valist (area, renderer, first_prop_name, var_args);
2585   va_end (var_args);
2586 }
2587
2588 static inline void
2589 area_get_cell_property (GtkCellArea     *area,
2590                         GtkCellRenderer *renderer,
2591                         GParamSpec      *pspec,
2592                         GValue          *value)
2593 {
2594   GtkCellAreaClass *class = g_type_class_peek (pspec->owner_type);
2595
2596   class->get_cell_property (area, renderer, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
2597 }
2598
2599 static inline void
2600 area_set_cell_property (GtkCellArea     *area,
2601                         GtkCellRenderer *renderer,
2602                         GParamSpec      *pspec,
2603                         const GValue    *value)
2604 {
2605   GValue tmp_value = G_VALUE_INIT;
2606   GtkCellAreaClass *class = g_type_class_peek (pspec->owner_type);
2607
2608   /* provide a copy to work from, convert (if necessary) and validate */
2609   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2610   if (!g_value_transform (value, &tmp_value))
2611     g_warning ("unable to set cell property `%s' of type `%s' from value of type `%s'",
2612                pspec->name,
2613                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
2614                G_VALUE_TYPE_NAME (value));
2615   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
2616     {
2617       gchar *contents = g_strdup_value_contents (value);
2618
2619       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
2620                  contents,
2621                  G_VALUE_TYPE_NAME (value),
2622                  pspec->name,
2623                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
2624       g_free (contents);
2625     }
2626   else
2627     {
2628       class->set_cell_property (area, renderer, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
2629     }
2630   g_value_unset (&tmp_value);
2631 }
2632
2633 /**
2634  * gtk_cell_area_cell_set_valist:
2635  * @area: a #GtkCellArea
2636  * @renderer: a #GtkCellRenderer which inside @area
2637  * @first_property_name: the name of the first cell property to set
2638  * @var_args: a %NULL-terminated list of property names and values, starting
2639  *           with @first_prop_name
2640  *
2641  * Sets one or more cell properties for @renderer in @area.
2642  *
2643  * Since: 3.0
2644  */
2645 void
2646 gtk_cell_area_cell_set_valist (GtkCellArea        *area,
2647                                GtkCellRenderer    *renderer,
2648                                const gchar        *first_property_name,
2649                                va_list             var_args)
2650 {
2651   const gchar *name;
2652
2653   g_return_if_fail (GTK_IS_CELL_AREA (area));
2654   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2655
2656   name = first_property_name;
2657   while (name)
2658     {
2659       GValue value = G_VALUE_INIT;
2660       gchar *error = NULL;
2661       GParamSpec *pspec =
2662         g_param_spec_pool_lookup (cell_property_pool, name,
2663                                   G_OBJECT_TYPE (area), TRUE);
2664       if (!pspec)
2665         {
2666           g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2667                      G_STRLOC, G_OBJECT_TYPE_NAME (area), name);
2668           break;
2669         }
2670       if (!(pspec->flags & G_PARAM_WRITABLE))
2671         {
2672           g_warning ("%s: cell property `%s' of cell area class `%s' is not writable",
2673                      G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2674           break;
2675         }
2676
2677       G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
2678                             var_args, 0, &error);
2679       if (error)
2680         {
2681           g_warning ("%s: %s", G_STRLOC, error);
2682           g_free (error);
2683
2684           /* we purposely leak the value here, it might not be
2685            * in a sane state if an error condition occoured
2686            */
2687           break;
2688         }
2689       area_set_cell_property (area, renderer, pspec, &value);
2690       g_value_unset (&value);
2691       name = va_arg (var_args, gchar*);
2692     }
2693 }
2694
2695 /**
2696  * gtk_cell_area_cell_get_valist:
2697  * @area: a #GtkCellArea
2698  * @renderer: a #GtkCellRenderer inside @area
2699  * @first_property_name: the name of the first property to get
2700  * @var_args: return location for the first property, followed
2701  *     optionally by more name/return location pairs, followed by %NULL
2702  *
2703  * Gets the values of one or more cell properties for @renderer in @area.
2704  *
2705  * Since: 3.0
2706  */
2707 void
2708 gtk_cell_area_cell_get_valist (GtkCellArea        *area,
2709                                GtkCellRenderer    *renderer,
2710                                const gchar        *first_property_name,
2711                                va_list             var_args)
2712 {
2713   const gchar *name;
2714
2715   g_return_if_fail (GTK_IS_CELL_AREA (area));
2716   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2717
2718   name = first_property_name;
2719   while (name)
2720     {
2721       GValue value = G_VALUE_INIT;
2722       GParamSpec *pspec;
2723       gchar *error;
2724
2725       pspec = g_param_spec_pool_lookup (cell_property_pool, name,
2726                                         G_OBJECT_TYPE (area), TRUE);
2727       if (!pspec)
2728         {
2729           g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2730                      G_STRLOC, G_OBJECT_TYPE_NAME (area), name);
2731           break;
2732         }
2733       if (!(pspec->flags & G_PARAM_READABLE))
2734         {
2735           g_warning ("%s: cell property `%s' of cell area class `%s' is not readable",
2736                      G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2737           break;
2738         }
2739
2740       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2741       area_get_cell_property (area, renderer, pspec, &value);
2742       G_VALUE_LCOPY (&value, var_args, 0, &error);
2743       if (error)
2744         {
2745           g_warning ("%s: %s", G_STRLOC, error);
2746           g_free (error);
2747           g_value_unset (&value);
2748           break;
2749         }
2750       g_value_unset (&value);
2751       name = va_arg (var_args, gchar*);
2752     }
2753 }
2754
2755 /**
2756  * gtk_cell_area_cell_set_property:
2757  * @area: a #GtkCellArea
2758  * @renderer: a #GtkCellRenderer inside @area
2759  * @property_name: the name of the cell property to set
2760  * @value: the value to set the cell property to
2761  *
2762  * Sets a cell property for @renderer in @area.
2763  *
2764  * Since: 3.0
2765  */
2766 void
2767 gtk_cell_area_cell_set_property (GtkCellArea        *area,
2768                                  GtkCellRenderer    *renderer,
2769                                  const gchar        *property_name,
2770                                  const GValue       *value)
2771 {
2772   GParamSpec *pspec;
2773
2774   g_return_if_fail (GTK_IS_CELL_AREA (area));
2775   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2776   g_return_if_fail (property_name != NULL);
2777   g_return_if_fail (G_IS_VALUE (value));
2778
2779   pspec = g_param_spec_pool_lookup (cell_property_pool, property_name,
2780                                     G_OBJECT_TYPE (area), TRUE);
2781   if (!pspec)
2782     g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2783                G_STRLOC, G_OBJECT_TYPE_NAME (area), property_name);
2784   else if (!(pspec->flags & G_PARAM_WRITABLE))
2785     g_warning ("%s: cell property `%s' of cell area class `%s' is not writable",
2786                G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2787   else
2788     {
2789       area_set_cell_property (area, renderer, pspec, value);
2790     }
2791 }
2792
2793 /**
2794  * gtk_cell_area_cell_get_property:
2795  * @area: a #GtkCellArea
2796  * @renderer: a #GtkCellRenderer inside @area
2797  * @property_name: the name of the property to get
2798  * @value: a location to return the value
2799  *
2800  * Gets the value of a cell property for @renderer in @area.
2801  *
2802  * Since: 3.0
2803  */
2804 void
2805 gtk_cell_area_cell_get_property (GtkCellArea        *area,
2806                                  GtkCellRenderer    *renderer,
2807                                  const gchar        *property_name,
2808                                  GValue             *value)
2809 {
2810   GParamSpec *pspec;
2811
2812   g_return_if_fail (GTK_IS_CELL_AREA (area));
2813   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2814   g_return_if_fail (property_name != NULL);
2815   g_return_if_fail (G_IS_VALUE (value));
2816
2817   pspec = g_param_spec_pool_lookup (cell_property_pool, property_name,
2818                                     G_OBJECT_TYPE (area), TRUE);
2819   if (!pspec)
2820     g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2821                G_STRLOC, G_OBJECT_TYPE_NAME (area), property_name);
2822   else if (!(pspec->flags & G_PARAM_READABLE))
2823     g_warning ("%s: cell property `%s' of cell area class `%s' is not readable",
2824                G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2825   else
2826     {
2827       GValue *prop_value, tmp_value = G_VALUE_INIT;
2828
2829       /* auto-conversion of the callers value type
2830        */
2831       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
2832         {
2833           g_value_reset (value);
2834           prop_value = value;
2835         }
2836       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
2837         {
2838           g_warning ("can't retrieve cell property `%s' of type `%s' as value of type `%s'",
2839                      pspec->name,
2840                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
2841                      G_VALUE_TYPE_NAME (value));
2842           return;
2843         }
2844       else
2845         {
2846           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2847           prop_value = &tmp_value;
2848         }
2849
2850       area_get_cell_property (area, renderer, pspec, prop_value);
2851
2852       if (prop_value != value)
2853         {
2854           g_value_transform (prop_value, value);
2855           g_value_unset (&tmp_value);
2856         }
2857     }
2858 }
2859
2860 /*************************************************************
2861  *                         API: Focus                        *
2862  *************************************************************/
2863
2864 /**
2865  * gtk_cell_area_is_activatable:
2866  * @area: a #GtkCellArea
2867  *
2868  * Returns whether the area can do anything when activated,
2869  * after applying new attributes to @area.
2870  *
2871  * Return value: whether @area can do anything when activated.
2872  *
2873  * Since: 3.0
2874  */
2875 gboolean
2876 gtk_cell_area_is_activatable (GtkCellArea *area)
2877 {
2878   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2879
2880   return GTK_CELL_AREA_GET_CLASS (area)->is_activatable (area);
2881 }
2882
2883 /**
2884  * gtk_cell_area_focus:
2885  * @area: a #GtkCellArea
2886  * @direction: the #GtkDirectionType
2887  *
2888  * This should be called by the @area's owning layout widget
2889  * when focus is to be passed to @area, or moved within @area
2890  * for a given @direction and row data.
2891  *
2892  * Implementing #GtkCellArea classes should implement this
2893  * method to receive and navigate focus in its own way particular
2894  * to how it lays out cells.
2895  *
2896  * Return value: %TRUE if focus remains inside @area as a result of this call.
2897  *
2898  * Since: 3.0
2899  */
2900 gboolean
2901 gtk_cell_area_focus (GtkCellArea      *area,
2902                      GtkDirectionType  direction)
2903 {
2904   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2905
2906   return GTK_CELL_AREA_GET_CLASS (area)->focus (area, direction);
2907 }
2908
2909 /**
2910  * gtk_cell_area_activate:
2911  * @area: a #GtkCellArea
2912  * @context: the #GtkCellAreaContext in context with the current row data
2913  * @widget: the #GtkWidget that @area is rendering on
2914  * @cell_area: the size and location of @area relative to @widget's allocation
2915  * @flags: the #GtkCellRendererState flags for @area for this row of data.
2916  * @edit_only: if %TRUE then only cell renderers that are %GTK_CELL_RENDERER_MODE_EDITABLE
2917  *             will be activated.
2918  *
2919  * Activates @area, usually by activating the currently focused
2920  * cell, however some subclasses which embed widgets in the area
2921  * can also activate a widget if it currently has the focus.
2922  *
2923  * Return value: Whether @area was successfully activated.
2924  *
2925  * Since: 3.0
2926  */
2927 gboolean
2928 gtk_cell_area_activate (GtkCellArea         *area,
2929                         GtkCellAreaContext  *context,
2930                         GtkWidget           *widget,
2931                         const GdkRectangle  *cell_area,
2932                         GtkCellRendererState flags,
2933                         gboolean             edit_only)
2934 {
2935   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2936
2937   return GTK_CELL_AREA_GET_CLASS (area)->activate (area, context, widget, cell_area, flags, edit_only);
2938 }
2939
2940
2941 /**
2942  * gtk_cell_area_set_focus_cell:
2943  * @area: a #GtkCellArea
2944  * @renderer: the #GtkCellRenderer to give focus to
2945  *
2946  * Explicitly sets the currently focused cell to @renderer.
2947  *
2948  * This is generally called by implementations of
2949  * #GtkCellAreaClass.focus() or #GtkCellAreaClass.event(),
2950  * however it can also be used to implement functions such
2951  * as gtk_tree_view_set_cursor_on_cell().
2952  *
2953  * Since: 3.0
2954  */
2955 void
2956 gtk_cell_area_set_focus_cell (GtkCellArea     *area,
2957                               GtkCellRenderer *renderer)
2958 {
2959   GtkCellAreaPrivate *priv;
2960
2961   g_return_if_fail (GTK_IS_CELL_AREA (area));
2962   g_return_if_fail (renderer == NULL || GTK_IS_CELL_RENDERER (renderer));
2963
2964   priv = area->priv;
2965
2966   if (priv->focus_cell != renderer)
2967     {
2968       if (priv->focus_cell)
2969         g_object_unref (priv->focus_cell);
2970
2971       priv->focus_cell = renderer;
2972
2973       if (priv->focus_cell)
2974         g_object_ref (priv->focus_cell);
2975
2976       g_object_notify (G_OBJECT (area), "focus-cell");
2977     }
2978
2979   /* Signal that the current focus renderer for this path changed
2980    * (it may be that the focus cell did not change, but the row
2981    * may have changed so we need to signal it) */
2982   g_signal_emit (area, cell_area_signals[SIGNAL_FOCUS_CHANGED], 0,
2983                  priv->focus_cell, priv->current_path);
2984
2985 }
2986
2987 /**
2988  * gtk_cell_area_get_focus_cell:
2989  * @area: a #GtkCellArea
2990  *
2991  * Retrieves the currently focused cell for @area
2992  *
2993  * Return value: (transfer none): the currently focused cell in @area.
2994  *
2995  * Since: 3.0
2996  */
2997 GtkCellRenderer *
2998 gtk_cell_area_get_focus_cell (GtkCellArea *area)
2999 {
3000   GtkCellAreaPrivate *priv;
3001
3002   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3003
3004   priv = area->priv;
3005
3006   return priv->focus_cell;
3007 }
3008
3009
3010 /*************************************************************
3011  *                    API: Focus Siblings                    *
3012  *************************************************************/
3013
3014 /**
3015  * gtk_cell_area_add_focus_sibling:
3016  * @area: a #GtkCellArea
3017  * @renderer: the #GtkCellRenderer expected to have focus
3018  * @sibling: the #GtkCellRenderer to add to @renderer's focus area
3019  *
3020  * Adds @sibling to @renderer's focusable area, focus will be drawn
3021  * around @renderer and all of its siblings if @renderer can
3022  * focus for a given row.
3023  *
3024  * Events handled by focus siblings can also activate the given
3025  * focusable @renderer.
3026  *
3027  * Since: 3.0
3028  */
3029 void
3030 gtk_cell_area_add_focus_sibling (GtkCellArea     *area,
3031                                  GtkCellRenderer *renderer,
3032                                  GtkCellRenderer *sibling)
3033 {
3034   GtkCellAreaPrivate *priv;
3035   GList              *siblings;
3036
3037   g_return_if_fail (GTK_IS_CELL_AREA (area));
3038   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3039   g_return_if_fail (GTK_IS_CELL_RENDERER (sibling));
3040   g_return_if_fail (renderer != sibling);
3041   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
3042   g_return_if_fail (gtk_cell_area_has_renderer (area, sibling));
3043   g_return_if_fail (!gtk_cell_area_is_focus_sibling (area, renderer, sibling));
3044
3045   /* XXX We should also check that sibling is not in any other renderer's sibling
3046    * list already, a renderer can be sibling of only one focusable renderer
3047    * at a time.
3048    */
3049
3050   priv = area->priv;
3051
3052   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3053
3054   if (siblings)
3055     siblings = g_list_append (siblings, sibling);
3056   else
3057     {
3058       siblings = g_list_append (siblings, sibling);
3059       g_hash_table_insert (priv->focus_siblings, renderer, siblings);
3060     }
3061 }
3062
3063 /**
3064  * gtk_cell_area_remove_focus_sibling:
3065  * @area: a #GtkCellArea
3066  * @renderer: the #GtkCellRenderer expected to have focus
3067  * @sibling: the #GtkCellRenderer to remove from @renderer's focus area
3068  *
3069  * Removes @sibling from @renderer's focus sibling list
3070  * (see gtk_cell_area_add_focus_sibling()).
3071  *
3072  * Since: 3.0
3073  */
3074 void
3075 gtk_cell_area_remove_focus_sibling (GtkCellArea     *area,
3076                                     GtkCellRenderer *renderer,
3077                                     GtkCellRenderer *sibling)
3078 {
3079   GtkCellAreaPrivate *priv;
3080   GList              *siblings;
3081
3082   g_return_if_fail (GTK_IS_CELL_AREA (area));
3083   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3084   g_return_if_fail (GTK_IS_CELL_RENDERER (sibling));
3085   g_return_if_fail (gtk_cell_area_is_focus_sibling (area, renderer, sibling));
3086
3087   priv = area->priv;
3088
3089   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3090
3091   siblings = g_list_copy (siblings);
3092   siblings = g_list_remove (siblings, sibling);
3093
3094   if (!siblings)
3095     g_hash_table_remove (priv->focus_siblings, renderer);
3096   else
3097     g_hash_table_insert (priv->focus_siblings, renderer, siblings);
3098 }
3099
3100 /**
3101  * gtk_cell_area_is_focus_sibling:
3102  * @area: a #GtkCellArea
3103  * @renderer: the #GtkCellRenderer expected to have focus
3104  * @sibling: the #GtkCellRenderer to check against @renderer's sibling list
3105  *
3106  * Returns whether @sibling is one of @renderer's focus siblings
3107  * (see gtk_cell_area_add_focus_sibling()).
3108  *
3109  * Return value: %TRUE if @sibling is a focus sibling of @renderer
3110  *
3111  * Since: 3.0
3112  */
3113 gboolean
3114 gtk_cell_area_is_focus_sibling (GtkCellArea     *area,
3115                                 GtkCellRenderer *renderer,
3116                                 GtkCellRenderer *sibling)
3117 {
3118   GtkCellAreaPrivate *priv;
3119   GList              *siblings, *l;
3120
3121   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
3122   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
3123   g_return_val_if_fail (GTK_IS_CELL_RENDERER (sibling), FALSE);
3124
3125   priv = area->priv;
3126
3127   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3128
3129   for (l = siblings; l; l = l->next)
3130     {
3131       GtkCellRenderer *a_sibling = l->data;
3132
3133       if (a_sibling == sibling)
3134         return TRUE;
3135     }
3136
3137   return FALSE;
3138 }
3139
3140 /**
3141  * gtk_cell_area_get_focus_siblings:
3142  * @area: a #GtkCellArea
3143  * @renderer: the #GtkCellRenderer expected to have focus
3144  *
3145  * Gets the focus sibling cell renderers for @renderer.
3146  *
3147  * Return value: (element-type GtkCellRenderer) (transfer none): A #GList of #GtkCellRenderers.
3148  *       The returned list is internal and should not be freed.
3149  *
3150  * Since: 3.0
3151  */
3152 const GList *
3153 gtk_cell_area_get_focus_siblings (GtkCellArea     *area,
3154                                   GtkCellRenderer *renderer)
3155 {
3156   GtkCellAreaPrivate *priv;
3157
3158   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3159   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), NULL);
3160
3161   priv = area->priv;
3162
3163   return g_hash_table_lookup (priv->focus_siblings, renderer);
3164 }
3165
3166 /**
3167  * gtk_cell_area_get_focus_from_sibling:
3168  * @area: a #GtkCellArea
3169  * @renderer: the #GtkCellRenderer
3170  *
3171  * Gets the #GtkCellRenderer which is expected to be focusable
3172  * for which @renderer is, or may be a sibling.
3173  *
3174  * This is handy for #GtkCellArea subclasses when handling events,
3175  * after determining the renderer at the event location it can
3176  * then chose to activate the focus cell for which the event
3177  * cell may have been a sibling.
3178  *
3179  * Return value: (transfer none): the #GtkCellRenderer for which @renderer
3180  *    is a sibling, or %NULL.
3181  *
3182  * Since: 3.0
3183  */
3184 GtkCellRenderer *
3185 gtk_cell_area_get_focus_from_sibling (GtkCellArea          *area,
3186                                       GtkCellRenderer      *renderer)
3187 {
3188   GtkCellRenderer *ret_renderer = NULL;
3189   GList           *renderers, *l;
3190
3191   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3192   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), NULL);
3193
3194   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
3195
3196   for (l = renderers; l; l = l->next)
3197     {
3198       GtkCellRenderer *a_renderer = l->data;
3199       const GList     *list;
3200
3201       for (list = gtk_cell_area_get_focus_siblings (area, a_renderer);
3202            list; list = list->next)
3203         {
3204           GtkCellRenderer *sibling_renderer = list->data;
3205
3206           if (sibling_renderer == renderer)
3207             {
3208               ret_renderer = a_renderer;
3209               break;
3210             }
3211         }
3212     }
3213   g_list_free (renderers);
3214
3215   return ret_renderer;
3216 }
3217
3218 /*************************************************************
3219  *              API: Cell Activation/Editing                 *
3220  *************************************************************/
3221 static void
3222 gtk_cell_area_add_editable (GtkCellArea        *area,
3223                             GtkCellRenderer    *renderer,
3224                             GtkCellEditable    *editable,
3225                             const GdkRectangle *cell_area)
3226 {
3227   g_signal_emit (area, cell_area_signals[SIGNAL_ADD_EDITABLE], 0,
3228                  renderer, editable, cell_area, area->priv->current_path);
3229 }
3230
3231 static void
3232 gtk_cell_area_remove_editable  (GtkCellArea        *area,
3233                                 GtkCellRenderer    *renderer,
3234                                 GtkCellEditable    *editable)
3235 {
3236   g_signal_emit (area, cell_area_signals[SIGNAL_REMOVE_EDITABLE], 0, renderer, editable);
3237 }
3238
3239 static void
3240 cell_area_remove_widget_cb (GtkCellEditable *editable,
3241                             GtkCellArea     *area)
3242 {
3243   GtkCellAreaPrivate *priv = area->priv;
3244
3245   g_assert (priv->edit_widget == editable);
3246   g_assert (priv->edited_cell != NULL);
3247
3248   gtk_cell_area_remove_editable (area, priv->edited_cell, priv->edit_widget);
3249
3250   /* Now that we're done with editing the widget and it can be removed,
3251    * remove our references to the widget and disconnect handlers */
3252   gtk_cell_area_set_edited_cell (area, NULL);
3253   gtk_cell_area_set_edit_widget (area, NULL);
3254 }
3255
3256 static void
3257 gtk_cell_area_set_edited_cell (GtkCellArea     *area,
3258                                GtkCellRenderer *renderer)
3259 {
3260   GtkCellAreaPrivate *priv;
3261
3262   g_return_if_fail (GTK_IS_CELL_AREA (area));
3263   g_return_if_fail (renderer == NULL || GTK_IS_CELL_RENDERER (renderer));
3264
3265   priv = area->priv;
3266
3267   if (priv->edited_cell != renderer)
3268     {
3269       if (priv->edited_cell)
3270         g_object_unref (priv->edited_cell);
3271
3272       priv->edited_cell = renderer;
3273
3274       if (priv->edited_cell)
3275         g_object_ref (priv->edited_cell);
3276
3277       g_object_notify (G_OBJECT (area), "edited-cell");
3278     }
3279 }
3280
3281 static void
3282 gtk_cell_area_set_edit_widget (GtkCellArea     *area,
3283                                GtkCellEditable *editable)
3284 {
3285   GtkCellAreaPrivate *priv;
3286
3287   g_return_if_fail (GTK_IS_CELL_AREA (area));
3288   g_return_if_fail (editable == NULL || GTK_IS_CELL_EDITABLE (editable));
3289
3290   priv = area->priv;
3291
3292   if (priv->edit_widget != editable)
3293     {
3294       if (priv->edit_widget)
3295         {
3296           g_signal_handler_disconnect (priv->edit_widget, priv->remove_widget_id);
3297
3298           g_object_unref (priv->edit_widget);
3299         }
3300
3301       priv->edit_widget = editable;
3302
3303       if (priv->edit_widget)
3304         {
3305           priv->remove_widget_id =
3306             g_signal_connect (priv->edit_widget, "remove-widget",
3307                               G_CALLBACK (cell_area_remove_widget_cb), area);
3308
3309           g_object_ref (priv->edit_widget);
3310         }
3311
3312       g_object_notify (G_OBJECT (area), "edit-widget");
3313     }
3314 }
3315
3316 /**
3317  * gtk_cell_area_get_edited_cell:
3318  * @area: a #GtkCellArea
3319  *
3320  * Gets the #GtkCellRenderer in @area that is currently
3321  * being edited.
3322  *
3323  * Return value: (transfer none): The currently edited #GtkCellRenderer
3324  *
3325  * Since: 3.0
3326  */
3327 GtkCellRenderer   *
3328 gtk_cell_area_get_edited_cell (GtkCellArea *area)
3329 {
3330   GtkCellAreaPrivate *priv;
3331
3332   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3333
3334   priv = area->priv;
3335
3336   return priv->edited_cell;
3337 }
3338
3339 /**
3340  * gtk_cell_area_get_edit_widget:
3341  * @area: a #GtkCellArea
3342  *
3343  * Gets the #GtkCellEditable widget currently used
3344  * to edit the currently edited cell.
3345  *
3346  * Return value: (transfer none): The currently active #GtkCellEditable widget
3347  *
3348  * Since: 3.0
3349  */
3350 GtkCellEditable *
3351 gtk_cell_area_get_edit_widget (GtkCellArea *area)
3352 {
3353   GtkCellAreaPrivate *priv;
3354
3355   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3356
3357   priv = area->priv;
3358
3359   return priv->edit_widget;
3360 }
3361
3362 /**
3363  * gtk_cell_area_activate_cell:
3364  * @area: a #GtkCellArea
3365  * @widget: the #GtkWidget that @area is rendering onto
3366  * @renderer: the #GtkCellRenderer in @area to activate
3367  * @event: the #GdkEvent for which cell activation should occur
3368  * @cell_area: the #GdkRectangle in @widget relative coordinates
3369  *             of @renderer for the current row.
3370  * @flags: the #GtkCellRendererState for @renderer
3371  *
3372  * This is used by #GtkCellArea subclasses when handling events
3373  * to activate cells, the base #GtkCellArea class activates cells
3374  * for keyboard events for free in its own GtkCellArea->activate()
3375  * implementation.
3376  *
3377  * Return value: whether cell activation was successful
3378  *
3379  * Since: 3.0
3380  */
3381 gboolean
3382 gtk_cell_area_activate_cell (GtkCellArea          *area,
3383                              GtkWidget            *widget,
3384                              GtkCellRenderer      *renderer,
3385                              GdkEvent             *event,
3386                              const GdkRectangle   *cell_area,
3387                              GtkCellRendererState  flags)
3388 {
3389   GtkCellRendererMode mode;
3390   GtkCellAreaPrivate *priv;
3391
3392   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
3393   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3394   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
3395   g_return_val_if_fail (cell_area != NULL, FALSE);
3396
3397   priv = area->priv;
3398
3399   if (!gtk_cell_renderer_get_sensitive (renderer))
3400     return FALSE;
3401
3402   g_object_get (renderer, "mode", &mode, NULL);
3403
3404   if (mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
3405     {
3406       if (gtk_cell_renderer_activate (renderer,
3407                                       event, widget,
3408                                       priv->current_path,
3409                                       cell_area,
3410                                       cell_area,
3411                                       flags))
3412         return TRUE;
3413     }
3414   else if (mode == GTK_CELL_RENDERER_MODE_EDITABLE)
3415     {
3416       GtkCellEditable *editable_widget;
3417       GdkRectangle inner_area;
3418
3419       gtk_cell_area_inner_cell_area (area, widget, cell_area, &inner_area);
3420
3421       editable_widget =
3422         gtk_cell_renderer_start_editing (renderer,
3423                                          event, widget,
3424                                          priv->current_path,
3425                                          &inner_area,
3426                                          &inner_area,
3427                                          flags);
3428
3429       if (editable_widget != NULL)
3430         {
3431           g_return_val_if_fail (GTK_IS_CELL_EDITABLE (editable_widget), FALSE);
3432
3433           gtk_cell_area_set_edited_cell (area, renderer);
3434           gtk_cell_area_set_edit_widget (area, editable_widget);
3435
3436           /* Signal that editing started so that callers can get
3437            * a handle on the editable_widget */
3438           gtk_cell_area_add_editable (area, priv->focus_cell, editable_widget, cell_area);
3439
3440           /* If the signal was successfully handled start the editing */
3441           if (gtk_widget_get_parent (GTK_WIDGET (editable_widget)))
3442             {
3443               gtk_cell_editable_start_editing (editable_widget, NULL);
3444               gtk_widget_grab_focus (GTK_WIDGET (editable_widget));
3445             }
3446           else
3447             {
3448               /* Otherwise clear the editing state and fire a warning */
3449               gtk_cell_area_set_edited_cell (area, NULL);
3450               gtk_cell_area_set_edit_widget (area, NULL);
3451
3452               g_warning ("GtkCellArea::add-editable fired in the dark, no cell editing was started.");
3453             }
3454
3455           return TRUE;
3456         }
3457     }
3458
3459   return FALSE;
3460 }
3461
3462 /**
3463  * gtk_cell_area_stop_editing:
3464  * @area: a #GtkCellArea
3465  * @canceled: whether editing was canceled.
3466  *
3467  * Explicitly stops the editing of the currently edited cell.
3468  *
3469  * If @canceled is %TRUE, the currently edited cell renderer
3470  * will emit the ::editing-canceled signal, otherwise the
3471  * the ::editing-done signal will be emitted on the current
3472  * edit widget.
3473  *
3474  * See gtk_cell_area_get_edited_cell() and gtk_cell_area_get_edit_widget().
3475  *
3476  * Since: 3.0
3477  */
3478 void
3479 gtk_cell_area_stop_editing (GtkCellArea *area,
3480                             gboolean     canceled)
3481 {
3482   GtkCellAreaPrivate *priv;
3483
3484   g_return_if_fail (GTK_IS_CELL_AREA (area));
3485
3486   priv = area->priv;
3487
3488   if (priv->edited_cell)
3489     {
3490       GtkCellEditable *edit_widget = g_object_ref (priv->edit_widget);
3491       GtkCellRenderer *edit_cell   = g_object_ref (priv->edited_cell);
3492
3493       /* Stop editing of the cell renderer */
3494       gtk_cell_renderer_stop_editing (priv->edited_cell, canceled);
3495
3496       /* When editing is explicitly halted either
3497        * the "editing-canceled" signal is emitted on the cell 
3498        * renderer or the "editing-done" signal on the GtkCellEditable widget
3499        */
3500       if (!canceled)
3501         gtk_cell_editable_editing_done (edit_widget);
3502
3503       /* Remove any references to the editable widget */
3504       gtk_cell_area_set_edited_cell (area, NULL);
3505       gtk_cell_area_set_edit_widget (area, NULL);
3506
3507       /* Send the remove-widget signal explicitly (this is done after setting
3508        * the edit cell/widget NULL to avoid feedback)
3509        */
3510       gtk_cell_area_remove_editable (area, edit_cell, edit_widget);
3511       g_object_unref (edit_cell);
3512       g_object_unref (edit_widget);
3513     }
3514 }
3515
3516 /*************************************************************
3517  *         API: Convenience for area implementations         *
3518  *************************************************************/
3519
3520 /**
3521  * gtk_cell_area_inner_cell_area:
3522  * @area: a #GtkCellArea
3523  * @widget: the #GtkWidget that @area is rendering onto
3524  * @cell_area: the @widget relative coordinates where one of @area's cells
3525  *             is to be placed
3526  * @inner_area: (out): the return location for the inner cell area
3527  *
3528  * This is a convenience function for #GtkCellArea implementations
3529  * to get the inner area where a given #GtkCellRenderer will be
3530  * rendered. It removes any padding previously added by gtk_cell_area_request_renderer().
3531  *
3532  * Since: 3.0
3533  */
3534 void
3535 gtk_cell_area_inner_cell_area (GtkCellArea        *area,
3536                                GtkWidget          *widget,
3537                                const GdkRectangle *cell_area,
3538                                GdkRectangle       *inner_area)
3539 {
3540   gint focus_line_width;
3541
3542   g_return_if_fail (GTK_IS_CELL_AREA (area));
3543   g_return_if_fail (GTK_IS_WIDGET (widget));
3544   g_return_if_fail (cell_area != NULL);
3545   g_return_if_fail (inner_area != NULL);
3546
3547   gtk_widget_style_get (widget, "focus-line-width", &focus_line_width, NULL);
3548
3549   *inner_area = *cell_area;
3550
3551   inner_area->x      += focus_line_width;
3552   inner_area->width  -= focus_line_width * 2;
3553   inner_area->y      += focus_line_width;
3554   inner_area->height -= focus_line_width * 2;
3555 }
3556
3557 /**
3558  * gtk_cell_area_request_renderer:
3559  * @area: a #GtkCellArea
3560  * @renderer: the #GtkCellRenderer to request size for
3561  * @orientation: the #GtkOrientation in which to request size
3562  * @widget: the #GtkWidget that @area is rendering onto
3563  * @for_size: the allocation contextual size to request for, or -1 if
3564  * the base request for the orientation is to be returned.
3565  * @minimum_size: (out) (allow-none): location to store the minimum size, or %NULL
3566  * @natural_size: (out) (allow-none): location to store the natural size, or %NULL
3567  *
3568  * This is a convenience function for #GtkCellArea implementations
3569  * to request size for cell renderers. It's important to use this
3570  * function to request size and then use gtk_cell_area_inner_cell_area()
3571  * at render and event time since this function will add padding
3572  * around the cell for focus painting.
3573  *
3574  * Since: 3.0
3575  */
3576 void
3577 gtk_cell_area_request_renderer (GtkCellArea        *area,
3578                                 GtkCellRenderer    *renderer,
3579                                 GtkOrientation      orientation,
3580                                 GtkWidget          *widget,
3581                                 gint                for_size,
3582                                 gint               *minimum_size,
3583                                 gint               *natural_size)
3584 {
3585   gint focus_line_width;
3586
3587   g_return_if_fail (GTK_IS_CELL_AREA (area));
3588   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3589   g_return_if_fail (GTK_IS_WIDGET (widget));
3590   g_return_if_fail (minimum_size != NULL);
3591   g_return_if_fail (natural_size != NULL);
3592
3593   gtk_widget_style_get (widget, "focus-line-width", &focus_line_width, NULL);
3594
3595   focus_line_width *= 2;
3596
3597   if (orientation == GTK_ORIENTATION_HORIZONTAL)
3598     {
3599       if (for_size < 0)
3600           gtk_cell_renderer_get_preferred_width (renderer, widget, minimum_size, natural_size);
3601       else
3602         {
3603           for_size = MAX (0, for_size - focus_line_width);
3604
3605           gtk_cell_renderer_get_preferred_width_for_height (renderer, widget, for_size,
3606                                                             minimum_size, natural_size);
3607         }
3608     }
3609   else /* GTK_ORIENTATION_VERTICAL */
3610     {
3611       if (for_size < 0)
3612         gtk_cell_renderer_get_preferred_height (renderer, widget, minimum_size, natural_size);
3613       else
3614         {
3615           for_size = MAX (0, for_size - focus_line_width);
3616
3617           gtk_cell_renderer_get_preferred_height_for_width (renderer, widget, for_size,
3618                                                             minimum_size, natural_size);
3619         }
3620     }
3621
3622   *minimum_size += focus_line_width;
3623   *natural_size += focus_line_width;
3624 }
3625
3626 void
3627 _gtk_cell_area_set_cell_data_func_with_proxy (GtkCellArea           *area,
3628                                               GtkCellRenderer       *cell,
3629                                               GFunc                  func,
3630                                               gpointer               func_data,
3631                                               GDestroyNotify         destroy,
3632                                               gpointer               proxy)
3633 {
3634   GtkCellAreaPrivate *priv;
3635   CellInfo           *info;
3636
3637   g_return_if_fail (GTK_IS_CELL_AREA (area));
3638   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3639
3640   priv = area->priv;
3641
3642   info = g_hash_table_lookup (priv->cell_info, cell);
3643
3644   /* Note we do not take a reference to the proxy, the proxy is a GtkCellLayout
3645    * that is forwarding its implementation to a delegate GtkCellArea therefore
3646    * its life-cycle is longer than the area's life cycle. 
3647    */
3648   if (info)
3649     {
3650       if (info->destroy && info->data)
3651         info->destroy (info->data);
3652
3653       if (func)
3654         {
3655           info->func    = (GtkCellLayoutDataFunc)func;
3656           info->data    = func_data;
3657           info->destroy = destroy;
3658           info->proxy   = proxy;
3659         }
3660       else
3661         {
3662           info->func    = NULL;
3663           info->data    = NULL;
3664           info->destroy = NULL;
3665           info->proxy   = NULL;
3666         }
3667     }
3668   else
3669     {
3670       info = cell_info_new ((GtkCellLayoutDataFunc)func, func_data, destroy);
3671       info->proxy = proxy;
3672
3673       g_hash_table_insert (priv->cell_info, cell, info);
3674     }
3675 }