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