]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellarea.c
Fixed gtk_cell_layout_set_cell_data_func() to pass the correct layout object
[~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_RENDERER,
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
909   G_OBJECT_CLASS (gtk_cell_area_parent_class)->finalize (object);
910 }
911
912
913 static void
914 gtk_cell_area_dispose (GObject *object)
915 {
916   /* This removes every cell renderer that may be added to the GtkCellArea,
917    * subclasses should be breaking references to the GtkCellRenderers
918    * at this point.
919    */
920   gtk_cell_layout_clear (GTK_CELL_LAYOUT (object));
921
922   /* Remove any ref to a focused/edited cell */
923   gtk_cell_area_set_focus_cell (GTK_CELL_AREA (object), NULL);
924   gtk_cell_area_set_edited_cell (GTK_CELL_AREA (object), NULL);
925   gtk_cell_area_set_edit_widget (GTK_CELL_AREA (object), NULL);
926
927   G_OBJECT_CLASS (gtk_cell_area_parent_class)->dispose (object);
928 }
929
930 static void
931 gtk_cell_area_set_property (GObject       *object,
932                             guint          prop_id,
933                             const GValue  *value,
934                             GParamSpec    *pspec)
935 {
936   GtkCellArea *area = GTK_CELL_AREA (object);
937
938   switch (prop_id)
939     {
940     case PROP_FOCUS_CELL:
941       gtk_cell_area_set_focus_cell (area, (GtkCellRenderer *)g_value_get_object (value));
942       break;
943     default:
944       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
945       break;
946     }
947 }
948
949 static void
950 gtk_cell_area_get_property (GObject     *object,
951                             guint        prop_id,
952                             GValue      *value,
953                             GParamSpec  *pspec)
954 {
955   GtkCellArea        *area = GTK_CELL_AREA (object);
956   GtkCellAreaPrivate *priv = area->priv;
957
958   switch (prop_id)
959     {
960     case PROP_FOCUS_CELL:
961       g_value_set_object (value, priv->focus_cell);
962       break;
963     case PROP_EDITED_CELL:
964       g_value_set_object (value, priv->edited_cell);
965       break;
966     case PROP_EDIT_WIDGET:
967       g_value_set_object (value, priv->edit_widget);
968       break;
969     default:
970       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
971       break;
972     }
973 }
974
975 /*************************************************************
976  *                    GtkCellAreaClass                       *
977  *************************************************************/
978 static gint
979 gtk_cell_area_real_event (GtkCellArea          *area,
980                           GtkCellAreaContext   *context,
981                           GtkWidget            *widget,
982                           GdkEvent             *event,
983                           const GdkRectangle   *cell_area,
984                           GtkCellRendererState  flags)
985 {
986   GtkCellAreaPrivate *priv = area->priv;
987   gboolean            retval = FALSE;
988
989   if (event->type == GDK_KEY_PRESS && (flags & GTK_CELL_RENDERER_FOCUSED) != 0)
990     {
991       GdkEventKey *key_event = (GdkEventKey *)event;
992
993       /* Cancel any edits in progress */
994       if (priv->edited_cell && (key_event->keyval == GDK_KEY_Escape))
995         {
996           gtk_cell_area_stop_editing (area, TRUE);
997           retval = TRUE;
998         }
999     }
1000   else if (event->type == GDK_BUTTON_PRESS)
1001     {
1002       GdkEventButton *button_event = (GdkEventButton *)event;
1003
1004       if (button_event->button == 1)
1005         {
1006           GtkCellRenderer *renderer = NULL;
1007           GtkCellRenderer *focus_renderer;
1008           GdkRectangle     alloc_area;
1009           gint             event_x, event_y;
1010
1011           /* We may need some semantics to tell us the offset of the event
1012            * window we are handling events for (i.e. GtkTreeView has a bin_window) */
1013           event_x = button_event->x;
1014           event_y = button_event->y;
1015
1016           /* Dont try to search for an event coordinate that is not in the area, that will
1017            * trigger a runtime warning.
1018            */
1019           if (event_x >= cell_area->x && event_x <= cell_area->x + cell_area->width &&
1020               event_y >= cell_area->y && event_y <= cell_area->y + cell_area->height)
1021             renderer =
1022               gtk_cell_area_get_cell_at_position (area, context, widget,
1023                                                   cell_area, event_x, event_y,
1024                                                   &alloc_area);
1025
1026           if (renderer)
1027             {
1028               focus_renderer = gtk_cell_area_get_focus_from_sibling (area, renderer);
1029               if (!focus_renderer)
1030                 focus_renderer = renderer;
1031
1032               /* If we're already editing, cancel it and set focus */
1033               if (gtk_cell_area_get_edited_cell (area))
1034                 {
1035                   /* XXX Was it really canceled in this case ? */
1036                   gtk_cell_area_stop_editing (area, TRUE);
1037                   gtk_cell_area_set_focus_cell (area, focus_renderer);
1038                   retval = TRUE;
1039                 }
1040               else
1041                 {
1042                   /* If we are activating via a focus sibling,
1043                    * we need to fetch the right cell area for the real event renderer */
1044                   if (focus_renderer != renderer)
1045                     gtk_cell_area_get_cell_allocation (area, context, widget, focus_renderer,
1046                                                        cell_area, &alloc_area);
1047
1048                   gtk_cell_area_set_focus_cell (area, focus_renderer);
1049                   retval = gtk_cell_area_activate_cell (area, widget, focus_renderer,
1050                                                         event, &alloc_area, flags);
1051                 }
1052             }
1053         }
1054     }
1055
1056   return retval;
1057 }
1058
1059 static gboolean
1060 render_cell (GtkCellRenderer        *renderer,
1061              const GdkRectangle     *cell_area,
1062              const GdkRectangle     *cell_background,
1063              CellRenderData         *data)
1064 {
1065   GtkCellRenderer      *focus_cell;
1066   GtkCellRendererState  flags;
1067   GdkRectangle          inner_area;
1068
1069   focus_cell = gtk_cell_area_get_focus_cell (data->area);
1070   flags      = data->render_flags;
1071
1072   gtk_cell_area_inner_cell_area (data->area, data->widget, cell_area, &inner_area);
1073
1074   if ((flags & GTK_CELL_RENDERER_FOCUSED) &&
1075       (data->focus_all ||
1076        (focus_cell &&
1077         (renderer == focus_cell ||
1078          gtk_cell_area_is_focus_sibling (data->area, focus_cell, renderer)))))
1079     {
1080       gint focus_line_width;
1081       GdkRectangle cell_focus;
1082
1083       gtk_cell_renderer_get_aligned_area (renderer, data->widget, flags, &inner_area, &cell_focus);
1084
1085       gtk_widget_style_get (data->widget,
1086                             "focus-line-width", &focus_line_width,
1087                             NULL);
1088
1089       /* The focus rectangle is located around the aligned area of the cell */
1090       cell_focus.x -= focus_line_width;
1091       cell_focus.y -= focus_line_width;
1092       cell_focus.width += 2 * focus_line_width;
1093       cell_focus.height += 2 * focus_line_width;
1094
1095       if (data->first_focus)
1096         {
1097           data->first_focus = FALSE;
1098           data->focus_rect  = cell_focus;
1099         }
1100       else
1101         {
1102           gdk_rectangle_union (&data->focus_rect, &cell_focus, &data->focus_rect);
1103         }
1104     }
1105   else
1106     flags &= ~GTK_CELL_RENDERER_FOCUSED;
1107
1108   gtk_cell_renderer_render (renderer, data->cr, data->widget,
1109                             cell_background, &inner_area, flags);
1110
1111   return FALSE;
1112 }
1113
1114 static void
1115 gtk_cell_area_real_render (GtkCellArea          *area,
1116                            GtkCellAreaContext   *context,
1117                            GtkWidget            *widget,
1118                            cairo_t              *cr,
1119                            const GdkRectangle   *background_area,
1120                            const GdkRectangle   *cell_area,
1121                            GtkCellRendererState  flags,
1122                            gboolean              paint_focus)
1123 {
1124   CellRenderData render_data =
1125     {
1126       area,
1127       widget,
1128       cr,
1129       { 0, },
1130       flags,
1131       paint_focus,
1132       FALSE, TRUE
1133     };
1134
1135   /* Make sure we dont paint a focus rectangle while there
1136    * is an editable widget in play
1137    */
1138   if (gtk_cell_area_get_edited_cell (area))
1139     render_data.paint_focus = FALSE;
1140
1141   /* If no cell can activate but the caller wants focus painted,
1142    * then we paint focus around all cells */
1143   if ((flags & GTK_CELL_RENDERER_FOCUSED) != 0 && paint_focus &&
1144       !gtk_cell_area_is_activatable (area))
1145     render_data.focus_all = TRUE;
1146
1147   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, background_area,
1148                                (GtkCellAllocCallback)render_cell, &render_data);
1149
1150   if (render_data.paint_focus &&
1151       render_data.focus_rect.width != 0 &&
1152       render_data.focus_rect.height != 0)
1153     {
1154       GtkStateType renderer_state =
1155         flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
1156         (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
1157          (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL));
1158
1159       cairo_save (cr);
1160
1161       gdk_cairo_rectangle (cr, background_area);
1162       cairo_clip (cr);
1163
1164       gtk_paint_focus (gtk_widget_get_style (widget), cr,
1165                        renderer_state, widget,
1166                        gtk_cell_area_get_style_detail (area),
1167                        render_data.focus_rect.x,     render_data.focus_rect.y,
1168                        render_data.focus_rect.width, render_data.focus_rect.height);
1169
1170       cairo_restore (cr);
1171     }
1172 }
1173
1174 static void
1175 apply_cell_attributes (GtkCellRenderer *renderer,
1176                        CellInfo        *info,
1177                        AttributeData   *data)
1178 {
1179   CellAttribute *attribute;
1180   GSList        *list;
1181   GValue         value = { 0, };
1182   gboolean       is_expander;
1183   gboolean       is_expanded;
1184
1185   g_object_freeze_notify (G_OBJECT (renderer));
1186
1187   /* Whether a row expands or is presently expanded can only be
1188    * provided by the view (as these states can vary across views
1189    * accessing the same model).
1190    */
1191   g_object_get (renderer, "is-expander", &is_expander, NULL);
1192   if (is_expander != data->is_expander)
1193     g_object_set (renderer, "is-expander", data->is_expander, NULL);
1194
1195   g_object_get (renderer, "is-expanded", &is_expanded, NULL);
1196   if (is_expanded != data->is_expanded)
1197     g_object_set (renderer, "is-expanded", data->is_expanded, NULL);
1198
1199   /* Apply the attributes directly to the renderer */
1200   for (list = info->attributes; list; list = list->next)
1201     {
1202       attribute = list->data;
1203
1204       gtk_tree_model_get_value (data->model, data->iter, attribute->column, &value);
1205       g_object_set_property (G_OBJECT (renderer), attribute->attribute, &value);
1206       g_value_unset (&value);
1207     }
1208
1209   /* Call any GtkCellLayoutDataFunc that may have been set by the user
1210    */
1211   if (info->func)
1212     info->func (info->proxy ? info->proxy : GTK_CELL_LAYOUT (data->area), renderer,
1213                 data->model, data->iter, info->data);
1214
1215   g_object_thaw_notify (G_OBJECT (renderer));
1216 }
1217
1218 static void
1219 gtk_cell_area_real_apply_attributes (GtkCellArea           *area,
1220                                      GtkTreeModel          *tree_model,
1221                                      GtkTreeIter           *iter,
1222                                      gboolean               is_expander,
1223                                      gboolean               is_expanded)
1224 {
1225
1226   GtkCellAreaPrivate *priv;
1227   AttributeData       data;
1228   GtkTreePath        *path;
1229
1230   priv = area->priv;
1231
1232   /* Feed in data needed to apply to every renderer */
1233   data.area        = area;
1234   data.model       = tree_model;
1235   data.iter        = iter;
1236   data.is_expander = is_expander;
1237   data.is_expanded = is_expanded;
1238
1239   /* Go over any cells that have attributes or custom GtkCellLayoutDataFuncs and
1240    * apply the data from the treemodel */
1241   g_hash_table_foreach (priv->cell_info, (GHFunc)apply_cell_attributes, &data);
1242
1243   /* Update the currently applied path */
1244   g_free (priv->current_path);
1245   path               = gtk_tree_model_get_path (tree_model, iter);
1246   priv->current_path = gtk_tree_path_to_string (path);
1247   gtk_tree_path_free (path);
1248 }
1249
1250 static void
1251 gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea        *area,
1252                                                    GtkCellAreaContext *context,
1253                                                    GtkWidget          *widget,
1254                                                    gint                width,
1255                                                    gint               *minimum_height,
1256                                                    gint               *natural_height)
1257 {
1258   /* If the area doesnt do height-for-width, fallback on base preferred height */
1259   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_width (area, context, widget, minimum_height, natural_height);
1260 }
1261
1262 static void
1263 gtk_cell_area_real_get_preferred_width_for_height (GtkCellArea        *area,
1264                                                    GtkCellAreaContext *context,
1265                                                    GtkWidget          *widget,
1266                                                    gint                height,
1267                                                    gint               *minimum_width,
1268                                                    gint               *natural_width)
1269 {
1270   /* If the area doesnt do width-for-height, fallback on base preferred width */
1271   GTK_CELL_AREA_GET_CLASS (area)->get_preferred_width (area, context, widget, minimum_width, natural_width);
1272 }
1273
1274 static gboolean
1275 get_is_activatable (GtkCellRenderer *renderer,
1276                     gboolean        *activatable)
1277 {
1278
1279   if (gtk_cell_renderer_is_activatable (renderer))
1280     *activatable = TRUE;
1281
1282   return *activatable;
1283 }
1284
1285 static gboolean
1286 gtk_cell_area_real_is_activatable (GtkCellArea *area)
1287 {
1288   gboolean activatable = FALSE;
1289
1290   /* Checks if any renderer can focus for the currently applied
1291    * attributes.
1292    *
1293    * Subclasses can override this in the case that they are also
1294    * rendering widgets as well as renderers.
1295    */
1296   gtk_cell_area_foreach (area, (GtkCellCallback)get_is_activatable, &activatable);
1297
1298   return activatable;
1299 }
1300
1301 static gboolean
1302 gtk_cell_area_real_activate (GtkCellArea         *area,
1303                              GtkCellAreaContext  *context,
1304                              GtkWidget           *widget,
1305                              const GdkRectangle  *cell_area,
1306                              GtkCellRendererState flags,
1307                              gboolean             edit_only)
1308 {
1309   GtkCellAreaPrivate *priv = area->priv;
1310   GdkRectangle        renderer_area;
1311   GtkCellRenderer    *activate_cell = NULL;
1312   GtkCellRendererMode mode;
1313
1314   if (priv->focus_cell)
1315     {
1316       g_object_get (priv->focus_cell, "mode", &mode, NULL);
1317
1318       if (gtk_cell_renderer_get_visible (priv->focus_cell) &&
1319           (edit_only ? mode == GTK_CELL_RENDERER_MODE_EDITABLE :
1320            mode != GTK_CELL_RENDERER_MODE_INERT))
1321         activate_cell = priv->focus_cell;
1322     }
1323   else
1324     {
1325       GList *cells, *l;
1326
1327       /* GtkTreeView sometimes wants to activate a cell when no
1328        * cells are in focus.
1329        */
1330       cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
1331       for (l = cells; l && !activate_cell; l = l->next)
1332         {
1333           GtkCellRenderer *renderer = l->data;
1334
1335           g_object_get (renderer, "mode", &mode, NULL);
1336
1337           if (gtk_cell_renderer_get_visible (renderer) &&
1338               (edit_only ? mode == GTK_CELL_RENDERER_MODE_EDITABLE :
1339                mode != GTK_CELL_RENDERER_MODE_INERT))
1340             activate_cell = renderer;
1341         }
1342       g_list_free (cells);
1343     }
1344
1345   if (activate_cell)
1346     {
1347       /* Get the allocation of the focused cell.
1348        */
1349       gtk_cell_area_get_cell_allocation (area, context, widget, activate_cell,
1350                                          cell_area, &renderer_area);
1351
1352       /* Activate or Edit the cell
1353        *
1354        * Currently just not sending an event, renderers afaics dont use
1355        * the event argument anyway, worst case is we can synthesize one.
1356        */
1357       if (gtk_cell_area_activate_cell (area, widget, activate_cell, NULL,
1358                                        &renderer_area, flags))
1359         return TRUE;
1360     }
1361
1362   return FALSE;
1363 }
1364
1365 /*************************************************************
1366  *                   GtkCellLayoutIface                      *
1367  *************************************************************/
1368 static void
1369 gtk_cell_area_cell_layout_init (GtkCellLayoutIface *iface)
1370 {
1371   iface->pack_start         = gtk_cell_area_pack_default;
1372   iface->pack_end           = gtk_cell_area_pack_default;
1373   iface->clear              = gtk_cell_area_clear;
1374   iface->add_attribute      = gtk_cell_area_add_attribute;
1375   iface->set_cell_data_func = gtk_cell_area_set_cell_data_func;
1376   iface->clear_attributes   = gtk_cell_area_clear_attributes;
1377   iface->reorder            = gtk_cell_area_reorder;
1378   iface->get_cells          = gtk_cell_area_get_cells;
1379   iface->get_area           = gtk_cell_area_get_area;
1380 }
1381
1382 static void
1383 gtk_cell_area_pack_default (GtkCellLayout         *cell_layout,
1384                             GtkCellRenderer       *renderer,
1385                             gboolean               expand)
1386 {
1387   gtk_cell_area_add (GTK_CELL_AREA (cell_layout), renderer);
1388 }
1389
1390 static void
1391 gtk_cell_area_clear (GtkCellLayout *cell_layout)
1392 {
1393   GtkCellArea *area = GTK_CELL_AREA (cell_layout);
1394   GList *l, *cells  =
1395     gtk_cell_layout_get_cells (cell_layout);
1396
1397   for (l = cells; l; l = l->next)
1398     {
1399       GtkCellRenderer *renderer = l->data;
1400       gtk_cell_area_remove (area, renderer);
1401     }
1402
1403   g_list_free (cells);
1404 }
1405
1406 static void
1407 gtk_cell_area_add_attribute (GtkCellLayout         *cell_layout,
1408                              GtkCellRenderer       *renderer,
1409                              const gchar           *attribute,
1410                              gint                   column)
1411 {
1412   gtk_cell_area_attribute_connect (GTK_CELL_AREA (cell_layout),
1413                                    renderer, attribute, column);
1414 }
1415
1416 static void
1417 gtk_cell_area_set_cell_data_func (GtkCellLayout         *cell_layout,
1418                                   GtkCellRenderer       *renderer,
1419                                   GtkCellLayoutDataFunc  func,
1420                                   gpointer               func_data,
1421                                   GDestroyNotify         destroy)
1422 {
1423   GtkCellArea *area   = GTK_CELL_AREA (cell_layout);
1424
1425   _gtk_cell_area_set_cell_data_func_with_proxy (area, renderer, (GFunc)func, func_data, destroy, NULL);
1426 }
1427
1428 static void
1429 gtk_cell_area_clear_attributes (GtkCellLayout         *cell_layout,
1430                                 GtkCellRenderer       *renderer)
1431 {
1432   GtkCellArea        *area = GTK_CELL_AREA (cell_layout);
1433   GtkCellAreaPrivate *priv = area->priv;
1434   CellInfo           *info;
1435
1436   info = g_hash_table_lookup (priv->cell_info, renderer);
1437
1438   if (info)
1439     {
1440       g_slist_foreach (info->attributes, (GFunc)cell_attribute_free, NULL);
1441       g_slist_free (info->attributes);
1442
1443       info->attributes = NULL;
1444     }
1445 }
1446
1447 static void
1448 gtk_cell_area_reorder (GtkCellLayout   *cell_layout,
1449                        GtkCellRenderer *cell,
1450                        gint             position)
1451 {
1452   g_warning ("GtkCellLayout::reorder not implemented for `%s'",
1453              g_type_name (G_TYPE_FROM_INSTANCE (cell_layout)));
1454 }
1455
1456 static gboolean
1457 accum_cells (GtkCellRenderer *renderer,
1458              GList          **accum)
1459 {
1460   *accum = g_list_prepend (*accum, renderer);
1461
1462   return FALSE;
1463 }
1464
1465 static GList *
1466 gtk_cell_area_get_cells (GtkCellLayout *cell_layout)
1467 {
1468   GList *cells = NULL;
1469
1470   gtk_cell_area_foreach (GTK_CELL_AREA (cell_layout),
1471                          (GtkCellCallback)accum_cells,
1472                          &cells);
1473
1474   return g_list_reverse (cells);
1475 }
1476
1477 static GtkCellArea *
1478 gtk_cell_area_get_area (GtkCellLayout *cell_layout)
1479 {
1480   return GTK_CELL_AREA (cell_layout);
1481 }
1482
1483 /*************************************************************
1484  *                   GtkBuildableIface                       *
1485  *************************************************************/
1486 static void
1487 gtk_cell_area_buildable_init (GtkBuildableIface *iface)
1488 {
1489   iface->add_child = _gtk_cell_layout_buildable_add_child;
1490   iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
1491   iface->custom_tag_end = gtk_cell_area_buildable_custom_tag_end;
1492 }
1493
1494 static void
1495 gtk_cell_area_buildable_custom_tag_end (GtkBuildable *buildable,
1496                                         GtkBuilder   *builder,
1497                                         GObject      *child,
1498                                         const gchar  *tagname,
1499                                         gpointer     *data)
1500 {
1501   /* Just ignore the boolean return from here */
1502   _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname, data);
1503 }
1504
1505 /*************************************************************
1506  *                            API                            *
1507  *************************************************************/
1508
1509 /**
1510  * gtk_cell_area_add:
1511  * @area: a #GtkCellArea
1512  * @renderer: the #GtkCellRenderer to add to @area
1513  *
1514  * Adds @renderer to @area with the default child cell properties.
1515  *
1516  * Since: 3.0
1517  */
1518 void
1519 gtk_cell_area_add (GtkCellArea        *area,
1520                    GtkCellRenderer    *renderer)
1521 {
1522   GtkCellAreaClass *class;
1523
1524   g_return_if_fail (GTK_IS_CELL_AREA (area));
1525   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1526
1527   class = GTK_CELL_AREA_GET_CLASS (area);
1528
1529   if (class->add)
1530     class->add (area, renderer);
1531   else
1532     g_warning ("GtkCellAreaClass::add not implemented for `%s'",
1533                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1534 }
1535
1536 /**
1537  * gtk_cell_area_remove:
1538  * @area: a #GtkCellArea
1539  * @renderer: the #GtkCellRenderer to remove from @area
1540  *
1541  * Removes @renderer from @area.
1542  *
1543  * Since: 3.0
1544  */
1545 void
1546 gtk_cell_area_remove (GtkCellArea        *area,
1547                       GtkCellRenderer    *renderer)
1548 {
1549   GtkCellAreaClass   *class;
1550   GtkCellAreaPrivate *priv;
1551   GList              *renderers, *l;
1552
1553   g_return_if_fail (GTK_IS_CELL_AREA (area));
1554   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1555
1556   class = GTK_CELL_AREA_GET_CLASS (area);
1557   priv  = area->priv;
1558
1559   /* Remove any custom attributes and custom cell data func here first */
1560   g_hash_table_remove (priv->cell_info, renderer);
1561
1562   /* Remove focus siblings of this renderer */
1563   g_hash_table_remove (priv->focus_siblings, renderer);
1564
1565   /* Remove this renderer from any focus renderer's sibling list */
1566   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
1567
1568   for (l = renderers; l; l = l->next)
1569     {
1570       GtkCellRenderer *focus_renderer = l->data;
1571
1572       if (gtk_cell_area_is_focus_sibling (area, focus_renderer, renderer))
1573         {
1574           gtk_cell_area_remove_focus_sibling (area, focus_renderer, renderer);
1575           break;
1576         }
1577     }
1578
1579   g_list_free (renderers);
1580
1581   if (class->remove)
1582     class->remove (area, renderer);
1583   else
1584     g_warning ("GtkCellAreaClass::remove not implemented for `%s'",
1585                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1586 }
1587
1588 static gboolean
1589 get_has_renderer (GtkCellRenderer  *renderer,
1590                   HasRendererCheck *check)
1591 {
1592   if (renderer == check->renderer)
1593     check->has_renderer = TRUE;
1594
1595   return check->has_renderer;
1596 }
1597
1598 /**
1599  * gtk_cell_area_has_renderer:
1600  * @area: a #GtkCellArea
1601  * @renderer: the #GtkCellRenderer to check
1602  *
1603  * Checks if @area contains @renderer.
1604  *
1605  * Return value: %TRUE if @renderer is in the @area.
1606  *
1607  * Since: 3.0
1608  */
1609 gboolean
1610 gtk_cell_area_has_renderer (GtkCellArea     *area,
1611                             GtkCellRenderer *renderer)
1612 {
1613   HasRendererCheck check = { renderer, FALSE };
1614
1615   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
1616   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
1617
1618   gtk_cell_area_foreach (area, (GtkCellCallback)get_has_renderer, &check);
1619
1620   return check.has_renderer;
1621 }
1622
1623 /**
1624  * gtk_cell_area_foreach:
1625  * @area: a #GtkCellArea
1626  * @callback: the #GtkCellCallback to call
1627  * @callback_data: user provided data pointer
1628  *
1629  * Calls @callback for every #GtkCellRenderer in @area.
1630  *
1631  * Since: 3.0
1632  */
1633 void
1634 gtk_cell_area_foreach (GtkCellArea        *area,
1635                        GtkCellCallback     callback,
1636                        gpointer            callback_data)
1637 {
1638   GtkCellAreaClass *class;
1639
1640   g_return_if_fail (GTK_IS_CELL_AREA (area));
1641   g_return_if_fail (callback != NULL);
1642
1643   class = GTK_CELL_AREA_GET_CLASS (area);
1644
1645   if (class->foreach)
1646     class->foreach (area, callback, callback_data);
1647   else
1648     g_warning ("GtkCellAreaClass::foreach not implemented for `%s'",
1649                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1650 }
1651
1652 /**
1653  * gtk_cell_area_foreach_alloc:
1654  * @area: a #GtkCellArea
1655  * @context: the #GtkCellAreaContext for this row of data.
1656  * @widget: the #GtkWidget that @area is rendering to
1657  * @cell_area: the @widget relative coordinates and size for @area
1658  * @background_area: the @widget relative coordinates of the background area
1659  * @callback: the #GtkCellAllocCallback to call
1660  * @callback_data: user provided data pointer
1661  *
1662  * Calls @callback for every #GtkCellRenderer in @area with the
1663  * allocated rectangle inside @cell_area.
1664  *
1665  * Since: 3.0
1666  */
1667 void
1668 gtk_cell_area_foreach_alloc (GtkCellArea          *area,
1669                              GtkCellAreaContext   *context,
1670                              GtkWidget            *widget,
1671                              const GdkRectangle   *cell_area,
1672                              const GdkRectangle   *background_area,
1673                              GtkCellAllocCallback  callback,
1674                              gpointer              callback_data)
1675 {
1676   GtkCellAreaClass *class;
1677
1678   g_return_if_fail (GTK_IS_CELL_AREA (area));
1679   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1680   g_return_if_fail (GTK_IS_WIDGET (widget));
1681   g_return_if_fail (cell_area != NULL);
1682   g_return_if_fail (callback != NULL);
1683
1684   class = GTK_CELL_AREA_GET_CLASS (area);
1685
1686   if (class->foreach_alloc)
1687     class->foreach_alloc (area, context, widget, cell_area, background_area, callback, callback_data);
1688   else
1689     g_warning ("GtkCellAreaClass::foreach_alloc not implemented for `%s'",
1690                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1691 }
1692
1693 /**
1694  * gtk_cell_area_event:
1695  * @area: a #GtkCellArea
1696  * @context: the #GtkCellAreaContext for this row of data.
1697  * @widget: the #GtkWidget that @area is rendering to
1698  * @event: the #GdkEvent to handle
1699  * @cell_area: the @widget relative coordinates for @area
1700  * @flags: the #GtkCellRendererState for @area in this row.
1701  *
1702  * Delegates event handling to a #GtkCellArea.
1703  *
1704  * Return value: %TRUE if the event was handled by @area.
1705  *
1706  * Since: 3.0
1707  */
1708 gint
1709 gtk_cell_area_event (GtkCellArea          *area,
1710                      GtkCellAreaContext   *context,
1711                      GtkWidget            *widget,
1712                      GdkEvent             *event,
1713                      const GdkRectangle   *cell_area,
1714                      GtkCellRendererState  flags)
1715 {
1716   GtkCellAreaClass *class;
1717
1718   g_return_val_if_fail (GTK_IS_CELL_AREA (area), 0);
1719   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), 0);
1720   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
1721   g_return_val_if_fail (event != NULL, 0);
1722   g_return_val_if_fail (cell_area != NULL, 0);
1723
1724   class = GTK_CELL_AREA_GET_CLASS (area);
1725
1726   if (class->event)
1727     return class->event (area, context, widget, event, cell_area, flags);
1728
1729   g_warning ("GtkCellAreaClass::event not implemented for `%s'",
1730              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1731   return 0;
1732 }
1733
1734 /**
1735  * gtk_cell_area_render:
1736  * @area: a #GtkCellArea
1737  * @context: the #GtkCellAreaContext for this row of data.
1738  * @widget: the #GtkWidget that @area is rendering to
1739  * @cr: the #cairo_t to render with
1740  * @background_area: the @widget relative coordinates for @area's background
1741  * @cell_area: the @widget relative coordinates for @area
1742  * @flags: the #GtkCellRendererState for @area in this row.
1743  * @paint_focus: whether @area should paint focus on focused cells for focused rows or not.
1744  *
1745  * Renders @area's cells according to @area's layout onto @widget at
1746  * the given coordinates.
1747  *
1748  * Since: 3.0
1749  */
1750 void
1751 gtk_cell_area_render (GtkCellArea          *area,
1752                       GtkCellAreaContext   *context,
1753                       GtkWidget            *widget,
1754                       cairo_t              *cr,
1755                       const GdkRectangle   *background_area,
1756                       const GdkRectangle   *cell_area,
1757                       GtkCellRendererState  flags,
1758                       gboolean              paint_focus)
1759 {
1760   GtkCellAreaClass *class;
1761
1762   g_return_if_fail (GTK_IS_CELL_AREA (area));
1763   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1764   g_return_if_fail (GTK_IS_WIDGET (widget));
1765   g_return_if_fail (cr != NULL);
1766   g_return_if_fail (background_area != NULL);
1767   g_return_if_fail (cell_area != NULL);
1768
1769   class = GTK_CELL_AREA_GET_CLASS (area);
1770
1771   if (class->render)
1772     class->render (area, context, widget, cr, background_area, cell_area, flags, paint_focus);
1773   else
1774     g_warning ("GtkCellAreaClass::render not implemented for `%s'",
1775                g_type_name (G_TYPE_FROM_INSTANCE (area)));
1776 }
1777
1778 /**
1779  * gtk_cell_area_set_style_detail:
1780  * @area: a #GtkCellArea
1781  * @detail: the #GtkStyle detail string to set
1782  *
1783  * Sets the detail string used in any gtk_paint_*() functions
1784  * used by @area.
1785  *
1786  * Since: 3.0
1787  */
1788 void
1789 gtk_cell_area_set_style_detail (GtkCellArea *area,
1790                                 const gchar *detail)
1791 {
1792   GtkCellAreaPrivate *priv;
1793
1794   g_return_if_fail (GTK_IS_CELL_AREA (area));
1795
1796   priv = area->priv;
1797
1798   if (g_strcmp0 (priv->style_detail, detail) != 0)
1799     {
1800       g_free (priv->style_detail);
1801       priv->style_detail = g_strdup (detail);
1802     }
1803 }
1804
1805 /**
1806  * gtk_cell_area_get_style_detail:
1807  * @area: a #GtkCellArea
1808  *
1809  * Gets the detail string used in any gtk_paint_*() functions
1810  * used by @area.
1811  *
1812  * Return value: the detail string, the string belongs to the area and should not be freed.
1813  *
1814  * Since: 3.0
1815  */
1816 G_CONST_RETURN gchar *
1817 gtk_cell_area_get_style_detail (GtkCellArea *area)
1818 {
1819   GtkCellAreaPrivate *priv;
1820
1821   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
1822
1823   priv = area->priv;
1824
1825   return priv->style_detail;
1826 }
1827
1828 static gboolean
1829 get_cell_allocation (GtkCellRenderer        *renderer,
1830                      const GdkRectangle     *cell_area,
1831                      const GdkRectangle     *cell_background,
1832                      RendererAllocationData *data)
1833 {
1834   if (data->renderer == renderer)
1835     data->allocation = *cell_area;
1836
1837   return (data->renderer == renderer);
1838 }
1839
1840 /**
1841  * gtk_cell_area_get_cell_allocation:
1842  * @area: a #GtkCellArea
1843  * @context: the #GtkCellAreaContext used to hold sizes for @area.
1844  * @widget: the #GtkWidget that @area is rendering on
1845  * @renderer: the #GtkCellRenderer to get the allocation for
1846  * @cell_area: the whole allocated area for @area in @widget
1847  *             for this row
1848  * @allocation: (out): where to store the allocation for @renderer
1849  *
1850  * Derives the allocation of @renderer inside @area if @area
1851  * were to be renderered in @cell_area.
1852  *
1853  * Since: 3.0
1854  */
1855 void
1856 gtk_cell_area_get_cell_allocation (GtkCellArea          *area,
1857                                    GtkCellAreaContext   *context,
1858                                    GtkWidget            *widget,
1859                                    GtkCellRenderer      *renderer,
1860                                    const GdkRectangle   *cell_area,
1861                                    GdkRectangle         *allocation)
1862 {
1863   RendererAllocationData data = { renderer, { 0, } };
1864
1865   g_return_if_fail (GTK_IS_CELL_AREA (area));
1866   g_return_if_fail (GTK_IS_CELL_AREA_CONTEXT (context));
1867   g_return_if_fail (GTK_IS_WIDGET (widget));
1868   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
1869   g_return_if_fail (cell_area != NULL);
1870   g_return_if_fail (allocation != NULL);
1871
1872   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, cell_area,
1873                                (GtkCellAllocCallback)get_cell_allocation, &data);
1874
1875   *allocation = data.allocation;
1876 }
1877
1878 static gboolean
1879 get_cell_by_position (GtkCellRenderer     *renderer,
1880                       const GdkRectangle  *cell_area,
1881                       const GdkRectangle  *cell_background,
1882                       CellByPositionData  *data)
1883 {
1884   if (data->x >= cell_area->x && data->x < cell_area->x + cell_area->width &&
1885       data->y >= cell_area->y && data->y < cell_area->y + cell_area->height)
1886     {
1887       data->renderer  = renderer;
1888       data->cell_area = *cell_area;
1889     }
1890
1891   return (data->renderer != NULL);
1892 }
1893
1894 /**
1895  * gtk_cell_area_get_cell_at_position:
1896  * @area: a #GtkCellArea
1897  * @context: the #GtkCellAreaContext used to hold sizes for @area.
1898  * @widget: the #GtkWidget that @area is rendering on
1899  * @cell_area: the whole allocated area for @area in @widget
1900  *             for this row
1901  * @x: the x position
1902  * @y: the y position
1903  * @alloc_area: (out) (allow-none): where to store the inner allocated area of the
1904  *                                  returned cell renderer, or %NULL.
1905  *
1906  * Gets the #GtkCellRenderer at @x and @y coordinates inside @area and optionally
1907  * returns the full cell allocation for it inside @cell_area.
1908  *
1909  * Return value: the #GtkCellRenderer at @x and @y.
1910  *
1911  * Since: 3.0
1912  */
1913 GtkCellRenderer *
1914 gtk_cell_area_get_cell_at_position (GtkCellArea          *area,
1915                                     GtkCellAreaContext   *context,
1916                                     GtkWidget            *widget,
1917                                     const GdkRectangle   *cell_area,
1918                                     gint                  x,
1919                                     gint                  y,
1920                                     GdkRectangle         *alloc_area)
1921 {
1922   CellByPositionData data = { x, y, NULL, { 0, } };
1923
1924   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
1925   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), NULL);
1926   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1927   g_return_val_if_fail (cell_area != NULL, NULL);
1928   g_return_val_if_fail (x >= cell_area->x && x <= cell_area->x + cell_area->width, NULL);
1929   g_return_val_if_fail (y >= cell_area->y && y <= cell_area->y + cell_area->height, NULL);
1930
1931   gtk_cell_area_foreach_alloc (area, context, widget, cell_area, cell_area,
1932                                (GtkCellAllocCallback)get_cell_by_position, &data);
1933
1934   if (alloc_area)
1935     *alloc_area = data.cell_area;
1936
1937   return data.renderer;
1938 }
1939
1940 /*************************************************************
1941  *                      API: Geometry                        *
1942  *************************************************************/
1943 /**
1944  * gtk_cell_area_create_context:
1945  * @area: a #GtkCellArea
1946  *
1947  * Creates a #GtkCellAreaContext to be used with @area for
1948  * all purposes. #GtkCellAreaContext stores geometry information
1949  * for rows for which it was operated on, it is important to use
1950  * the same context for the same row of data at all times (i.e.
1951  * one should render and handle events with the same #GtkCellAreaContext
1952  * which was used to request the size of those rows of data).
1953  *
1954  * Return value: (transfer full): a newly created #GtkCellAreaContext which can be used with @area.
1955  *
1956  * Since: 3.0
1957  */
1958 GtkCellAreaContext *
1959 gtk_cell_area_create_context (GtkCellArea *area)
1960 {
1961   GtkCellAreaClass *class;
1962
1963   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
1964
1965   class = GTK_CELL_AREA_GET_CLASS (area);
1966
1967   if (class->create_context)
1968     return class->create_context (area);
1969
1970   g_warning ("GtkCellAreaClass::create_context not implemented for `%s'",
1971              g_type_name (G_TYPE_FROM_INSTANCE (area)));
1972
1973   return NULL;
1974 }
1975
1976 /**
1977  * gtk_cell_area_copy_context:
1978  * @area: a #GtkCellArea
1979  * @context: the #GtkCellAreaContext to copy
1980  *
1981  * This is sometimes needed for cases where rows need to share
1982  * alignments in one orientation but may be separately grouped
1983  * in the opposing orientation.
1984  *
1985  * For instance, #GtkIconView creates all icons (rows) to have
1986  * the same width and the cells theirin to have the same
1987  * horizontal alignments. However each row of icons may have
1988  * a separate collective height. #GtkIconView uses this to
1989  * request the heights of each row based on a context which
1990  * was already used to request all the row widths that are
1991  * to be displayed.
1992  *
1993  * Return value: (transfer full): a newly created #GtkCellAreaContext copy of @context.
1994  *
1995  * Since: 3.0
1996  */
1997 GtkCellAreaContext *
1998 gtk_cell_area_copy_context (GtkCellArea        *area,
1999                             GtkCellAreaContext *context)
2000 {
2001   GtkCellAreaClass *class;
2002
2003   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
2004   g_return_val_if_fail (GTK_IS_CELL_AREA_CONTEXT (context), NULL);
2005
2006   class = GTK_CELL_AREA_GET_CLASS (area);
2007
2008   if (class->copy_context)
2009     return class->copy_context (area, context);
2010
2011   g_warning ("GtkCellAreaClass::copy_context not implemented for `%s'",
2012              g_type_name (G_TYPE_FROM_INSTANCE (area)));
2013
2014   return NULL;
2015 }
2016
2017 /**
2018  * gtk_cell_area_get_request_mode:
2019  * @area: a #GtkCellArea
2020  *
2021  * Gets whether the area prefers a height-for-width layout
2022  * or a width-for-height layout.
2023  *
2024  * Return value: The #GtkSizeRequestMode preferred by @area.
2025  *
2026  * Since: 3.0
2027  */
2028 GtkSizeRequestMode
2029 gtk_cell_area_get_request_mode (GtkCellArea *area)
2030 {
2031   GtkCellAreaClass *class;
2032
2033   g_return_val_if_fail (GTK_IS_CELL_AREA (area),
2034                         GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH);
2035
2036   class = GTK_CELL_AREA_GET_CLASS (area);
2037
2038   if (class->get_request_mode)
2039     return class->get_request_mode (area);
2040
2041   g_warning ("GtkCellAreaClass::get_request_mode not implemented for `%s'",
2042              g_type_name (G_TYPE_FROM_INSTANCE (area)));
2043
2044   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
2045 }
2046
2047 /**
2048  * gtk_cell_area_get_preferred_width:
2049  * @area: a #GtkCellArea
2050  * @context: the #GtkCellAreaContext to perform this request with
2051  * @widget: the #GtkWidget where @area will be rendering
2052  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
2053  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
2054  *
2055  * Retrieves a cell area's initial minimum and natural width.
2056  *
2057  * @area will store some geometrical information in @context along the way,
2058  * when requesting sizes over an arbitrary number of rows, its not important
2059  * to check the @minimum_width and @natural_width of this call but rather to
2060  * consult gtk_cell_area_context_get_preferred_width() after a series of
2061  * requests.
2062  *
2063  * Since: 3.0
2064  */
2065 void
2066 gtk_cell_area_get_preferred_width (GtkCellArea        *area,
2067                                    GtkCellAreaContext *context,
2068                                    GtkWidget          *widget,
2069                                    gint               *minimum_width,
2070                                    gint               *natural_width)
2071 {
2072   GtkCellAreaClass *class;
2073
2074   g_return_if_fail (GTK_IS_CELL_AREA (area));
2075   g_return_if_fail (GTK_IS_WIDGET (widget));
2076
2077   class = GTK_CELL_AREA_GET_CLASS (area);
2078
2079   if (class->get_preferred_width)
2080     class->get_preferred_width (area, context, widget, minimum_width, natural_width);
2081   else
2082     g_warning ("GtkCellAreaClass::get_preferred_width not implemented for `%s'",
2083                g_type_name (G_TYPE_FROM_INSTANCE (area)));
2084 }
2085
2086 /**
2087  * gtk_cell_area_get_preferred_height_for_width:
2088  * @area: a #GtkCellArea
2089  * @context: the #GtkCellAreaContext which has already been requested for widths.
2090  * @widget: the #GtkWidget where @area will be rendering
2091  * @width: the width for which to check the height of this area
2092  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
2093  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
2094  *
2095  * Retrieves a cell area's minimum and natural height if it would be given
2096  * the specified @width.
2097  *
2098  * @area stores some geometrical information in @context along the way
2099  * while calling gtk_cell_area_get_preferred_width(), it's important to
2100  * perform a series of gtk_cell_area_get_preferred_width() requests with
2101  * @context first and then call gtk_cell_area_get_preferred_height_for_width()
2102  * on each cell area individually to get the height for width of each
2103  * fully requested row.
2104  *
2105  * If at some point, the width of a single row changes, it should be
2106  * requested with gtk_cell_area_get_preferred_width() again and then
2107  * the full width of the requested rows checked again with
2108  * gtk_cell_area_context_get_preferred_width().
2109  *
2110  * Since: 3.0
2111  */
2112 void
2113 gtk_cell_area_get_preferred_height_for_width (GtkCellArea        *area,
2114                                               GtkCellAreaContext *context,
2115                                               GtkWidget          *widget,
2116                                               gint                width,
2117                                               gint               *minimum_height,
2118                                               gint               *natural_height)
2119 {
2120   GtkCellAreaClass *class;
2121
2122   g_return_if_fail (GTK_IS_CELL_AREA (area));
2123   g_return_if_fail (GTK_IS_WIDGET (widget));
2124
2125   class = GTK_CELL_AREA_GET_CLASS (area);
2126   class->get_preferred_height_for_width (area, context, widget, width, minimum_height, natural_height);
2127 }
2128
2129
2130 /**
2131  * gtk_cell_area_get_preferred_height:
2132  * @area: a #GtkCellArea
2133  * @context: the #GtkCellAreaContext to perform this request with
2134  * @widget: the #GtkWidget where @area will be rendering
2135  * @minimum_height: (out) (allow-none): location to store the minimum height, or %NULL
2136  * @natural_height: (out) (allow-none): location to store the natural height, or %NULL
2137  *
2138  * Retrieves a cell area's initial minimum and natural height.
2139  *
2140  * @area will store some geometrical information in @context along the way,
2141  * when requesting sizes over an arbitrary number of rows, its not important
2142  * to check the @minimum_height and @natural_height of this call but rather to
2143  * consult gtk_cell_area_context_get_preferred_height() after a series of
2144  * requests.
2145  *
2146  * Since: 3.0
2147  */
2148 void
2149 gtk_cell_area_get_preferred_height (GtkCellArea        *area,
2150                                     GtkCellAreaContext *context,
2151                                     GtkWidget          *widget,
2152                                     gint               *minimum_height,
2153                                     gint               *natural_height)
2154 {
2155   GtkCellAreaClass *class;
2156
2157   g_return_if_fail (GTK_IS_CELL_AREA (area));
2158   g_return_if_fail (GTK_IS_WIDGET (widget));
2159
2160   class = GTK_CELL_AREA_GET_CLASS (area);
2161
2162   if (class->get_preferred_height)
2163     class->get_preferred_height (area, context, widget, minimum_height, natural_height);
2164   else
2165     g_warning ("GtkCellAreaClass::get_preferred_height not implemented for `%s'",
2166                g_type_name (G_TYPE_FROM_INSTANCE (area)));
2167 }
2168
2169 /**
2170  * gtk_cell_area_get_preferred_width_for_height:
2171  * @area: a #GtkCellArea
2172  * @context: the #GtkCellAreaContext which has already been requested for widths.
2173  * @widget: the #GtkWidget where @area will be rendering
2174  * @height: the height for which to check the width of this area
2175  * @minimum_width: (out) (allow-none): location to store the minimum width, or %NULL
2176  * @natural_width: (out) (allow-none): location to store the natural width, or %NULL
2177  *
2178  * Retrieves a cell area's minimum and natural width if it would be given
2179  * the specified @height.
2180  *
2181  * @area stores some geometrical information in @context along the way
2182  * while calling gtk_cell_area_get_preferred_height(), it's important to
2183  * perform a series of gtk_cell_area_get_preferred_height() requests with
2184  * @context first and then call gtk_cell_area_get_preferred_width_for_height()
2185  * on each cell area individually to get the height for width of each
2186  * fully requested row.
2187  *
2188  * If at some point, the height of a single row changes, it should be
2189  * requested with gtk_cell_area_get_preferred_height() again and then
2190  * the full height of the requested rows checked again with
2191  * gtk_cell_area_context_get_preferred_height().
2192  *
2193  * Since: 3.0
2194  */
2195 void
2196 gtk_cell_area_get_preferred_width_for_height (GtkCellArea        *area,
2197                                               GtkCellAreaContext *context,
2198                                               GtkWidget          *widget,
2199                                               gint                height,
2200                                               gint               *minimum_width,
2201                                               gint               *natural_width)
2202 {
2203   GtkCellAreaClass *class;
2204
2205   g_return_if_fail (GTK_IS_CELL_AREA (area));
2206   g_return_if_fail (GTK_IS_WIDGET (widget));
2207
2208   class = GTK_CELL_AREA_GET_CLASS (area);
2209   class->get_preferred_width_for_height (area, context, widget, height, minimum_width, natural_width);
2210 }
2211
2212 /*************************************************************
2213  *                      API: Attributes                      *
2214  *************************************************************/
2215
2216 /**
2217  * gtk_cell_area_attribute_connect:
2218  * @area: a #GtkCellArea
2219  * @renderer: the #GtkCellRenderer to connect an attribute for
2220  * @attribute: the attribute name
2221  * @column: the #GtkTreeModel column to fetch attribute values from
2222  *
2223  * Connects an @attribute to apply values from @column for the
2224  * #GtkTreeModel in use.
2225  *
2226  * Since: 3.0
2227  */
2228 void
2229 gtk_cell_area_attribute_connect (GtkCellArea        *area,
2230                                  GtkCellRenderer    *renderer,
2231                                  const gchar        *attribute,
2232                                  gint                column)
2233 {
2234   GtkCellAreaPrivate *priv;
2235   CellInfo           *info;
2236   CellAttribute      *cell_attribute;
2237
2238   g_return_if_fail (GTK_IS_CELL_AREA (area));
2239   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2240   g_return_if_fail (attribute != NULL);
2241   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
2242
2243   priv = area->priv;
2244   info = g_hash_table_lookup (priv->cell_info, renderer);
2245
2246   if (!info)
2247     {
2248       info = cell_info_new (NULL, NULL, NULL);
2249
2250       g_hash_table_insert (priv->cell_info, renderer, info);
2251     }
2252   else
2253     {
2254       GSList *node;
2255
2256       /* Check we are not adding the same attribute twice */
2257       if ((node = g_slist_find_custom (info->attributes, attribute,
2258                                        (GCompareFunc)cell_attribute_find)) != NULL)
2259         {
2260           cell_attribute = node->data;
2261
2262           g_warning ("Cannot connect attribute `%s' for cell renderer class `%s' "
2263                      "since `%s' is already attributed to column %d",
2264                      attribute,
2265                      g_type_name (G_TYPE_FROM_INSTANCE (area)),
2266                      attribute, cell_attribute->column);
2267           return;
2268         }
2269     }
2270
2271   cell_attribute = cell_attribute_new (renderer, attribute, column);
2272
2273   if (!cell_attribute)
2274     {
2275       g_warning ("Cannot connect attribute `%s' for cell renderer class `%s' "
2276                  "since attribute does not exist",
2277                  attribute,
2278                  g_type_name (G_TYPE_FROM_INSTANCE (area)));
2279       return;
2280     }
2281
2282   info->attributes = g_slist_prepend (info->attributes, cell_attribute);
2283 }
2284
2285 /**
2286  * gtk_cell_area_attribute_disconnect:
2287  * @area: a #GtkCellArea
2288  * @renderer: the #GtkCellRenderer to disconnect an attribute for
2289  * @attribute: the attribute name
2290  *
2291  * Disconnects @attribute for the @renderer in @area so that
2292  * attribute will no longer be updated with values from the
2293  * model.
2294  *
2295  * Since: 3.0
2296  */
2297 void
2298 gtk_cell_area_attribute_disconnect (GtkCellArea        *area,
2299                                     GtkCellRenderer    *renderer,
2300                                     const gchar        *attribute)
2301 {
2302   GtkCellAreaPrivate *priv;
2303   CellInfo           *info;
2304   CellAttribute      *cell_attribute;
2305   GSList             *node;
2306
2307   g_return_if_fail (GTK_IS_CELL_AREA (area));
2308   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2309   g_return_if_fail (attribute != NULL);
2310   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
2311
2312   priv = area->priv;
2313   info = g_hash_table_lookup (priv->cell_info, renderer);
2314
2315   if (info)
2316     {
2317       node = g_slist_find_custom (info->attributes, attribute,
2318                                   (GCompareFunc)cell_attribute_find);
2319       if (node)
2320         {
2321           cell_attribute = node->data;
2322
2323           cell_attribute_free (cell_attribute);
2324
2325           info->attributes = g_slist_delete_link (info->attributes, node);
2326         }
2327     }
2328 }
2329
2330 /**
2331  * gtk_cell_area_apply_attributes
2332  * @area: a #GtkCellArea
2333  * @tree_model: the #GtkTreeModel to pull values from
2334  * @iter: the #GtkTreeIter in @tree_model to apply values for
2335  * @is_expander: whether @iter has children
2336  * @is_expanded: whether @iter is expanded in the view and
2337  *               children are visible
2338  *
2339  * Applies any connected attributes to the renderers in
2340  * @area by pulling the values from @tree_model.
2341  *
2342  * Since: 3.0
2343  */
2344 void
2345 gtk_cell_area_apply_attributes (GtkCellArea  *area,
2346                                 GtkTreeModel *tree_model,
2347                                 GtkTreeIter  *iter,
2348                                 gboolean      is_expander,
2349                                 gboolean      is_expanded)
2350 {
2351   g_return_if_fail (GTK_IS_CELL_AREA (area));
2352   g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
2353   g_return_if_fail (iter != NULL);
2354
2355   g_signal_emit (area, cell_area_signals[SIGNAL_APPLY_ATTRIBUTES], 0,
2356                  tree_model, iter, is_expander, is_expanded);
2357 }
2358
2359 /**
2360  * gtk_cell_area_get_current_path_string:
2361  * @area: a #GtkCellArea
2362  *
2363  * Gets the current #GtkTreePath string for the currently
2364  * applied #GtkTreeIter, this is implicitly updated when
2365  * gtk_cell_area_apply_attributes() is called and can be
2366  * used to interact with renderers from #GtkCellArea
2367  * subclasses.
2368  *
2369  * Return value: The current #GtkTreePath string for the current
2370  * attributes applied to @area. This string belongs to the area and
2371  * should not be freed.
2372  *
2373  * Since: 3.0
2374  */
2375 G_CONST_RETURN gchar *
2376 gtk_cell_area_get_current_path_string (GtkCellArea *area)
2377 {
2378   GtkCellAreaPrivate *priv;
2379
2380   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
2381
2382   priv = area->priv;
2383
2384   return priv->current_path;
2385 }
2386
2387
2388 /*************************************************************
2389  *                    API: Cell Properties                   *
2390  *************************************************************/
2391 /**
2392  * gtk_cell_area_class_install_cell_property:
2393  * @aclass: a #GtkCellAreaClass
2394  * @property_id: the id for the property
2395  * @pspec: the #GParamSpec for the property
2396  *
2397  * Installs a cell property on a cell area class.
2398  *
2399  * Since: 3.0
2400  */
2401 void
2402 gtk_cell_area_class_install_cell_property (GtkCellAreaClass   *aclass,
2403                                            guint               property_id,
2404                                            GParamSpec         *pspec)
2405 {
2406   g_return_if_fail (GTK_IS_CELL_AREA_CLASS (aclass));
2407   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
2408   if (pspec->flags & G_PARAM_WRITABLE)
2409     g_return_if_fail (aclass->set_cell_property != NULL);
2410   if (pspec->flags & G_PARAM_READABLE)
2411     g_return_if_fail (aclass->get_cell_property != NULL);
2412   g_return_if_fail (property_id > 0);
2413   g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0);  /* paranoid */
2414   g_return_if_fail ((pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) == 0);
2415
2416   if (g_param_spec_pool_lookup (cell_property_pool, pspec->name, G_OBJECT_CLASS_TYPE (aclass), TRUE))
2417     {
2418       g_warning (G_STRLOC ": class `%s' already contains a cell property named `%s'",
2419                  G_OBJECT_CLASS_NAME (aclass), pspec->name);
2420       return;
2421     }
2422   g_param_spec_ref (pspec);
2423   g_param_spec_sink (pspec);
2424   PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
2425   g_param_spec_pool_insert (cell_property_pool, pspec, G_OBJECT_CLASS_TYPE (aclass));
2426 }
2427
2428 /**
2429  * gtk_cell_area_class_find_cell_property:
2430  * @aclass: a #GtkCellAreaClass
2431  * @property_name: the name of the child property to find
2432  *
2433  * Finds a cell property of a cell area class by name.
2434  *
2435  * Return value: (allow-none): the #GParamSpec of the child property or %NULL if @aclass has no
2436  *   child property with that name.
2437  *
2438  * Since: 3.0
2439  */
2440 GParamSpec*
2441 gtk_cell_area_class_find_cell_property (GtkCellAreaClass   *aclass,
2442                                         const gchar        *property_name)
2443 {
2444   g_return_val_if_fail (GTK_IS_CELL_AREA_CLASS (aclass), NULL);
2445   g_return_val_if_fail (property_name != NULL, NULL);
2446
2447   return g_param_spec_pool_lookup (cell_property_pool,
2448                                    property_name,
2449                                    G_OBJECT_CLASS_TYPE (aclass),
2450                                    TRUE);
2451 }
2452
2453 /**
2454  * gtk_cell_area_class_list_cell_properties:
2455  * @aclass: a #GtkCellAreaClass
2456  * @n_properties: location to return the number of cell properties found
2457  *
2458  * Returns all cell properties of a cell area class.
2459  *
2460  * Return value: a newly allocated %NULL-terminated array of #GParamSpec*.
2461  *           The array must be freed with g_free().
2462  *
2463  * Since: 3.0
2464  */
2465 GParamSpec**
2466 gtk_cell_area_class_list_cell_properties (GtkCellAreaClass  *aclass,
2467                                           guint             *n_properties)
2468 {
2469   GParamSpec **pspecs;
2470   guint n;
2471
2472   g_return_val_if_fail (GTK_IS_CELL_AREA_CLASS (aclass), NULL);
2473
2474   pspecs = g_param_spec_pool_list (cell_property_pool,
2475                                    G_OBJECT_CLASS_TYPE (aclass),
2476                                    &n);
2477   if (n_properties)
2478     *n_properties = n;
2479
2480   return pspecs;
2481 }
2482
2483 /**
2484  * gtk_cell_area_add_with_properties:
2485  * @area: a #GtkCellArea
2486  * @renderer: a #GtkCellRenderer to be placed inside @area
2487  * @first_prop_name: the name of the first cell property to set
2488  * @Varargs: a %NULL-terminated list of property names and values, starting
2489  *           with @first_prop_name
2490  *
2491  * Adds @renderer to @area, setting cell properties at the same time.
2492  * See gtk_cell_area_add() and gtk_cell_area_child_set() for more details.
2493  *
2494  * Since: 3.0
2495  */
2496 void
2497 gtk_cell_area_add_with_properties (GtkCellArea        *area,
2498                                    GtkCellRenderer    *renderer,
2499                                    const gchar        *first_prop_name,
2500                                    ...)
2501 {
2502   GtkCellAreaClass *class;
2503
2504   g_return_if_fail (GTK_IS_CELL_AREA (area));
2505   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2506
2507   class = GTK_CELL_AREA_GET_CLASS (area);
2508
2509   if (class->add)
2510     {
2511       va_list var_args;
2512
2513       class->add (area, renderer);
2514
2515       va_start (var_args, first_prop_name);
2516       gtk_cell_area_cell_set_valist (area, renderer, first_prop_name, var_args);
2517       va_end (var_args);
2518     }
2519   else
2520     g_warning ("GtkCellAreaClass::add not implemented for `%s'",
2521                g_type_name (G_TYPE_FROM_INSTANCE (area)));
2522 }
2523
2524 /**
2525  * gtk_cell_area_cell_set:
2526  * @area: a #GtkCellArea
2527  * @renderer: a #GtkCellRenderer which is a cell inside @area
2528  * @first_prop_name: the name of the first cell property to set
2529  * @Varargs: a %NULL-terminated list of property names and values, starting
2530  *           with @first_prop_name
2531  *
2532  * Sets one or more cell properties for @cell in @area.
2533  *
2534  * Since: 3.0
2535  */
2536 void
2537 gtk_cell_area_cell_set (GtkCellArea        *area,
2538                         GtkCellRenderer    *renderer,
2539                         const gchar        *first_prop_name,
2540                         ...)
2541 {
2542   va_list var_args;
2543
2544   g_return_if_fail (GTK_IS_CELL_AREA (area));
2545   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2546
2547   va_start (var_args, first_prop_name);
2548   gtk_cell_area_cell_set_valist (area, renderer, first_prop_name, var_args);
2549   va_end (var_args);
2550 }
2551
2552 /**
2553  * gtk_cell_area_cell_get:
2554  * @area: a #GtkCellArea
2555  * @renderer: a #GtkCellRenderer which is inside @area
2556  * @first_prop_name: the name of the first cell property to get
2557  * @Varargs: return location for the first cell property, followed
2558  *     optionally by more name/return location pairs, followed by %NULL
2559  *
2560  * Gets the values of one or more cell properties for @renderer in @area.
2561  *
2562  * Since: 3.0
2563  */
2564 void
2565 gtk_cell_area_cell_get (GtkCellArea        *area,
2566                         GtkCellRenderer    *renderer,
2567                         const gchar        *first_prop_name,
2568                         ...)
2569 {
2570   va_list var_args;
2571
2572   g_return_if_fail (GTK_IS_CELL_AREA (area));
2573   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2574
2575   va_start (var_args, first_prop_name);
2576   gtk_cell_area_cell_get_valist (area, renderer, first_prop_name, var_args);
2577   va_end (var_args);
2578 }
2579
2580 static inline void
2581 area_get_cell_property (GtkCellArea     *area,
2582                         GtkCellRenderer *renderer,
2583                         GParamSpec      *pspec,
2584                         GValue          *value)
2585 {
2586   GtkCellAreaClass *class = g_type_class_peek (pspec->owner_type);
2587
2588   class->get_cell_property (area, renderer, PARAM_SPEC_PARAM_ID (pspec), value, pspec);
2589 }
2590
2591 static inline void
2592 area_set_cell_property (GtkCellArea     *area,
2593                         GtkCellRenderer *renderer,
2594                         GParamSpec      *pspec,
2595                         const GValue    *value)
2596 {
2597   GValue tmp_value = { 0, };
2598   GtkCellAreaClass *class = g_type_class_peek (pspec->owner_type);
2599
2600   /* provide a copy to work from, convert (if necessary) and validate */
2601   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2602   if (!g_value_transform (value, &tmp_value))
2603     g_warning ("unable to set cell property `%s' of type `%s' from value of type `%s'",
2604                pspec->name,
2605                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
2606                G_VALUE_TYPE_NAME (value));
2607   else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
2608     {
2609       gchar *contents = g_strdup_value_contents (value);
2610
2611       g_warning ("value \"%s\" of type `%s' is invalid for property `%s' of type `%s'",
2612                  contents,
2613                  G_VALUE_TYPE_NAME (value),
2614                  pspec->name,
2615                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
2616       g_free (contents);
2617     }
2618   else
2619     {
2620       class->set_cell_property (area, renderer, PARAM_SPEC_PARAM_ID (pspec), &tmp_value, pspec);
2621     }
2622   g_value_unset (&tmp_value);
2623 }
2624
2625 /**
2626  * gtk_cell_area_cell_set_valist:
2627  * @area: a #GtkCellArea
2628  * @renderer: a #GtkCellRenderer which inside @area
2629  * @first_property_name: the name of the first cell property to set
2630  * @var_args: a %NULL-terminated list of property names and values, starting
2631  *           with @first_prop_name
2632  *
2633  * Sets one or more cell properties for @renderer in @area.
2634  *
2635  * Since: 3.0
2636  */
2637 void
2638 gtk_cell_area_cell_set_valist (GtkCellArea        *area,
2639                                GtkCellRenderer    *renderer,
2640                                const gchar        *first_property_name,
2641                                va_list             var_args)
2642 {
2643   const gchar *name;
2644
2645   g_return_if_fail (GTK_IS_CELL_AREA (area));
2646   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2647
2648   name = first_property_name;
2649   while (name)
2650     {
2651       GValue value = { 0, };
2652       gchar *error = NULL;
2653       GParamSpec *pspec =
2654         g_param_spec_pool_lookup (cell_property_pool, name,
2655                                   G_OBJECT_TYPE (area), TRUE);
2656       if (!pspec)
2657         {
2658           g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2659                      G_STRLOC, G_OBJECT_TYPE_NAME (area), name);
2660           break;
2661         }
2662       if (!(pspec->flags & G_PARAM_WRITABLE))
2663         {
2664           g_warning ("%s: cell property `%s' of cell area class `%s' is not writable",
2665                      G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2666           break;
2667         }
2668
2669       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2670       G_VALUE_COLLECT (&value, var_args, 0, &error);
2671       if (error)
2672         {
2673           g_warning ("%s: %s", G_STRLOC, error);
2674           g_free (error);
2675
2676           /* we purposely leak the value here, it might not be
2677            * in a sane state if an error condition occoured
2678            */
2679           break;
2680         }
2681       area_set_cell_property (area, renderer, pspec, &value);
2682       g_value_unset (&value);
2683       name = va_arg (var_args, gchar*);
2684     }
2685 }
2686
2687 /**
2688  * gtk_cell_area_cell_get_valist:
2689  * @area: a #GtkCellArea
2690  * @renderer: a #GtkCellRenderer inside @area
2691  * @first_property_name: the name of the first property to get
2692  * @var_args: return location for the first property, followed
2693  *     optionally by more name/return location pairs, followed by %NULL
2694  *
2695  * Gets the values of one or more cell properties for @renderer in @area.
2696  *
2697  * Since: 3.0
2698  */
2699 void
2700 gtk_cell_area_cell_get_valist (GtkCellArea        *area,
2701                                GtkCellRenderer    *renderer,
2702                                const gchar        *first_property_name,
2703                                va_list             var_args)
2704 {
2705   const gchar *name;
2706
2707   g_return_if_fail (GTK_IS_CELL_AREA (area));
2708   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2709
2710   name = first_property_name;
2711   while (name)
2712     {
2713       GValue value = { 0, };
2714       GParamSpec *pspec;
2715       gchar *error;
2716
2717       pspec = g_param_spec_pool_lookup (cell_property_pool, name,
2718                                         G_OBJECT_TYPE (area), TRUE);
2719       if (!pspec)
2720         {
2721           g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2722                      G_STRLOC, G_OBJECT_TYPE_NAME (area), name);
2723           break;
2724         }
2725       if (!(pspec->flags & G_PARAM_READABLE))
2726         {
2727           g_warning ("%s: cell property `%s' of cell area class `%s' is not readable",
2728                      G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2729           break;
2730         }
2731
2732       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2733       area_get_cell_property (area, renderer, pspec, &value);
2734       G_VALUE_LCOPY (&value, var_args, 0, &error);
2735       if (error)
2736         {
2737           g_warning ("%s: %s", G_STRLOC, error);
2738           g_free (error);
2739           g_value_unset (&value);
2740           break;
2741         }
2742       g_value_unset (&value);
2743       name = va_arg (var_args, gchar*);
2744     }
2745 }
2746
2747 /**
2748  * gtk_cell_area_cell_set_property:
2749  * @area: a #GtkCellArea
2750  * @renderer: a #GtkCellRenderer inside @area
2751  * @property_name: the name of the cell property to set
2752  * @value: the value to set the cell property to
2753  *
2754  * Sets a cell property for @renderer in @area.
2755  *
2756  * Since: 3.0
2757  */
2758 void
2759 gtk_cell_area_cell_set_property (GtkCellArea        *area,
2760                                  GtkCellRenderer    *renderer,
2761                                  const gchar        *property_name,
2762                                  const GValue       *value)
2763 {
2764   GParamSpec *pspec;
2765
2766   g_return_if_fail (GTK_IS_CELL_AREA (area));
2767   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2768   g_return_if_fail (property_name != NULL);
2769   g_return_if_fail (G_IS_VALUE (value));
2770
2771   pspec = g_param_spec_pool_lookup (cell_property_pool, property_name,
2772                                     G_OBJECT_TYPE (area), TRUE);
2773   if (!pspec)
2774     g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2775                G_STRLOC, G_OBJECT_TYPE_NAME (area), property_name);
2776   else if (!(pspec->flags & G_PARAM_WRITABLE))
2777     g_warning ("%s: cell property `%s' of cell area class `%s' is not writable",
2778                G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2779   else
2780     {
2781       area_set_cell_property (area, renderer, pspec, value);
2782     }
2783 }
2784
2785 /**
2786  * gtk_cell_area_cell_get_property:
2787  * @area: a #GtkCellArea
2788  * @renderer: a #GtkCellRenderer inside @area
2789  * @property_name: the name of the property to get
2790  * @value: a location to return the value
2791  *
2792  * Gets the value of a cell property for @renderer in @area.
2793  *
2794  * Since: 3.0
2795  */
2796 void
2797 gtk_cell_area_cell_get_property (GtkCellArea        *area,
2798                                  GtkCellRenderer    *renderer,
2799                                  const gchar        *property_name,
2800                                  GValue             *value)
2801 {
2802   GParamSpec *pspec;
2803
2804   g_return_if_fail (GTK_IS_CELL_AREA (area));
2805   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
2806   g_return_if_fail (property_name != NULL);
2807   g_return_if_fail (G_IS_VALUE (value));
2808
2809   pspec = g_param_spec_pool_lookup (cell_property_pool, property_name,
2810                                     G_OBJECT_TYPE (area), TRUE);
2811   if (!pspec)
2812     g_warning ("%s: cell area class `%s' has no cell property named `%s'",
2813                G_STRLOC, G_OBJECT_TYPE_NAME (area), property_name);
2814   else if (!(pspec->flags & G_PARAM_READABLE))
2815     g_warning ("%s: cell property `%s' of cell area class `%s' is not readable",
2816                G_STRLOC, pspec->name, G_OBJECT_TYPE_NAME (area));
2817   else
2818     {
2819       GValue *prop_value, tmp_value = { 0, };
2820
2821       /* auto-conversion of the callers value type
2822        */
2823       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
2824         {
2825           g_value_reset (value);
2826           prop_value = value;
2827         }
2828       else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
2829         {
2830           g_warning ("can't retrieve cell property `%s' of type `%s' as value of type `%s'",
2831                      pspec->name,
2832                      g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
2833                      G_VALUE_TYPE_NAME (value));
2834           return;
2835         }
2836       else
2837         {
2838           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2839           prop_value = &tmp_value;
2840         }
2841
2842       area_get_cell_property (area, renderer, pspec, prop_value);
2843
2844       if (prop_value != value)
2845         {
2846           g_value_transform (prop_value, value);
2847           g_value_unset (&tmp_value);
2848         }
2849     }
2850 }
2851
2852 /*************************************************************
2853  *                         API: Focus                        *
2854  *************************************************************/
2855
2856 /**
2857  * gtk_cell_area_is_activatable:
2858  * @area: a #GtkCellArea
2859  *
2860  * Returns whether the area can do anything when activated,
2861  * after applying new attributes to @area.
2862  *
2863  * Return value: whether @area can do anything when activated.
2864  *
2865  * Since: 3.0
2866  */
2867 gboolean
2868 gtk_cell_area_is_activatable (GtkCellArea *area)
2869 {
2870   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2871
2872   return GTK_CELL_AREA_GET_CLASS (area)->is_activatable (area);
2873 }
2874
2875 /**
2876  * gtk_cell_area_focus:
2877  * @area: a #GtkCellArea
2878  * @direction: the #GtkDirectionType
2879  *
2880  * This should be called by the @area's owning layout widget
2881  * when focus is to be passed to @area, or moved within @area
2882  * for a given @direction and row data.
2883  *
2884  * Implementing #GtkCellArea classes should implement this
2885  * method to receive and navigate focus in it's own way particular
2886  * to how it lays out cells.
2887  *
2888  * Return value: %TRUE if focus remains inside @area as a result of this call.
2889  *
2890  * Since: 3.0
2891  */
2892 gboolean
2893 gtk_cell_area_focus (GtkCellArea      *area,
2894                      GtkDirectionType  direction)
2895 {
2896   GtkCellAreaClass *class;
2897
2898   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2899
2900   class = GTK_CELL_AREA_GET_CLASS (area);
2901
2902   if (class->focus)
2903     return class->focus (area, direction);
2904
2905   g_warning ("GtkCellAreaClass::focus not implemented for `%s'",
2906              g_type_name (G_TYPE_FROM_INSTANCE (area)));
2907
2908   return FALSE;
2909 }
2910
2911 /**
2912  * gtk_cell_area_activate:
2913  * @area: a #GtkCellArea
2914  * @context: the #GtkCellAreaContext in context with the current row data
2915  * @widget: the #GtkWidget that @area is rendering on
2916  * @cell_area: the size and location of @area relative to @widget's allocation
2917  * @flags: the #GtkCellRendererState flags for @area for this row of data.
2918  * @edit_only: if %TRUE then only cell renderers that are %GTK_CELL_RENDERER_MODE_EDITABLE
2919  *             will be activated.
2920  *
2921  * Activates @area, usually by activating the currently focused
2922  * cell, however some subclasses which embed widgets in the area
2923  * can also activate a widget if it currently has the focus.
2924  *
2925  * Return value: Whether @area was successfully activated.
2926  *
2927  * Since: 3.0
2928  */
2929 gboolean
2930 gtk_cell_area_activate (GtkCellArea         *area,
2931                         GtkCellAreaContext  *context,
2932                         GtkWidget           *widget,
2933                         const GdkRectangle  *cell_area,
2934                         GtkCellRendererState flags,
2935                         gboolean             edit_only)
2936 {
2937   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
2938
2939   return GTK_CELL_AREA_GET_CLASS (area)->activate (area, context, widget, cell_area, flags, edit_only);
2940 }
2941
2942
2943 /**
2944  * gtk_cell_area_set_focus_cell:
2945  * @area: a #GtkCellArea
2946  * @focus_cell: the #GtkCellRenderer to give focus to
2947  *
2948  * This is generally called from #GtkCellArea implementations
2949  * either gtk_cell_area_grab_focus() or gtk_cell_area_update_focus()
2950  * is called. It's also up to the #GtkCellArea implementation
2951  * to update the focused cell when receiving events from
2952  * gtk_cell_area_event() appropriately.
2953  *
2954  * Since: 3.0
2955  */
2956 void
2957 gtk_cell_area_set_focus_cell (GtkCellArea     *area,
2958                               GtkCellRenderer *renderer)
2959 {
2960   GtkCellAreaPrivate *priv;
2961
2962   g_return_if_fail (GTK_IS_CELL_AREA (area));
2963   g_return_if_fail (renderer == NULL || GTK_IS_CELL_RENDERER (renderer));
2964
2965   priv = area->priv;
2966
2967   if (priv->focus_cell != renderer)
2968     {
2969       if (priv->focus_cell)
2970         g_object_unref (priv->focus_cell);
2971
2972       priv->focus_cell = renderer;
2973
2974       if (priv->focus_cell)
2975         g_object_ref (priv->focus_cell);
2976
2977       g_object_notify (G_OBJECT (area), "focus-cell");
2978     }
2979
2980   /* Signal that the current focus renderer for this path changed
2981    * (it may be that the focus cell did not change, but the row
2982    * may have changed so we need to signal it) */
2983   g_signal_emit (area, cell_area_signals[SIGNAL_FOCUS_CHANGED], 0,
2984                  priv->focus_cell, priv->current_path);
2985
2986 }
2987
2988 /**
2989  * gtk_cell_area_get_focus_cell:
2990  * @area: a #GtkCellArea
2991  *
2992  * Retrieves the currently focused cell for @area
2993  *
2994  * Return value: the currently focused cell in @area.
2995  *
2996  * Since: 3.0
2997  */
2998 GtkCellRenderer *
2999 gtk_cell_area_get_focus_cell (GtkCellArea *area)
3000 {
3001   GtkCellAreaPrivate *priv;
3002
3003   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3004
3005   priv = area->priv;
3006
3007   return priv->focus_cell;
3008 }
3009
3010
3011 /*************************************************************
3012  *                    API: Focus Siblings                    *
3013  *************************************************************/
3014
3015 /**
3016  * gtk_cell_area_add_focus_sibling:
3017  * @area: a #GtkCellArea
3018  * @renderer: the #GtkCellRenderer expected to have focus
3019  * @sibling: the #GtkCellRenderer to add to @renderer's focus area
3020  *
3021  * Adds @sibling to @renderer's focusable area, focus will be drawn
3022  * around @renderer and all of it's siblings if @renderer can
3023  * focus for a given row.
3024  *
3025  * Events handled by focus siblings can also activate the given
3026  * focusable @renderer.
3027  *
3028  * Since: 3.0
3029  */
3030 void
3031 gtk_cell_area_add_focus_sibling (GtkCellArea     *area,
3032                                  GtkCellRenderer *renderer,
3033                                  GtkCellRenderer *sibling)
3034 {
3035   GtkCellAreaPrivate *priv;
3036   GList              *siblings;
3037
3038   g_return_if_fail (GTK_IS_CELL_AREA (area));
3039   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3040   g_return_if_fail (GTK_IS_CELL_RENDERER (sibling));
3041   g_return_if_fail (renderer != sibling);
3042   g_return_if_fail (gtk_cell_area_has_renderer (area, renderer));
3043   g_return_if_fail (gtk_cell_area_has_renderer (area, sibling));
3044   g_return_if_fail (!gtk_cell_area_is_focus_sibling (area, renderer, sibling));
3045
3046   /* XXX We should also check that sibling is not in any other renderer's sibling
3047    * list already, a renderer can be sibling of only one focusable renderer
3048    * at a time.
3049    */
3050
3051   priv = area->priv;
3052
3053   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3054
3055   if (siblings)
3056     siblings = g_list_append (siblings, sibling);
3057   else
3058     {
3059       siblings = g_list_append (siblings, sibling);
3060       g_hash_table_insert (priv->focus_siblings, renderer, siblings);
3061     }
3062 }
3063
3064 /**
3065  * gtk_cell_area_remove_focus_sibling:
3066  * @area: a #GtkCellArea
3067  * @renderer: the #GtkCellRenderer expected to have focus
3068  * @sibling: the #GtkCellRenderer to remove from @renderer's focus area
3069  *
3070  * Removes @sibling from @renderer's focus sibling list
3071  * (see gtk_cell_area_add_focus_sibling()).
3072  *
3073  * Since: 3.0
3074  */
3075 void
3076 gtk_cell_area_remove_focus_sibling (GtkCellArea     *area,
3077                                     GtkCellRenderer *renderer,
3078                                     GtkCellRenderer *sibling)
3079 {
3080   GtkCellAreaPrivate *priv;
3081   GList              *siblings;
3082
3083   g_return_if_fail (GTK_IS_CELL_AREA (area));
3084   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3085   g_return_if_fail (GTK_IS_CELL_RENDERER (sibling));
3086   g_return_if_fail (gtk_cell_area_is_focus_sibling (area, renderer, sibling));
3087
3088   priv = area->priv;
3089
3090   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3091
3092   siblings = g_list_copy (siblings);
3093   siblings = g_list_remove (siblings, sibling);
3094
3095   if (!siblings)
3096     g_hash_table_remove (priv->focus_siblings, renderer);
3097   else
3098     g_hash_table_insert (priv->focus_siblings, renderer, siblings);
3099 }
3100
3101 /**
3102  * gtk_cell_area_is_focus_sibling:
3103  * @area: a #GtkCellArea
3104  * @renderer: the #GtkCellRenderer expected to have focus
3105  * @sibling: the #GtkCellRenderer to check against @renderer's sibling list
3106  *
3107  * Returns %TRUE if @sibling is one of @renderer's focus siblings
3108  * (see gtk_cell_area_add_focus_sibling()).
3109  *
3110  * Since: 3.0
3111  */
3112 gboolean
3113 gtk_cell_area_is_focus_sibling (GtkCellArea     *area,
3114                                 GtkCellRenderer *renderer,
3115                                 GtkCellRenderer *sibling)
3116 {
3117   GtkCellAreaPrivate *priv;
3118   GList              *siblings, *l;
3119
3120   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
3121   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
3122   g_return_val_if_fail (GTK_IS_CELL_RENDERER (sibling), FALSE);
3123
3124   priv = area->priv;
3125
3126   siblings = g_hash_table_lookup (priv->focus_siblings, renderer);
3127
3128   for (l = siblings; l; l = l->next)
3129     {
3130       GtkCellRenderer *a_sibling = l->data;
3131
3132       if (a_sibling == sibling)
3133         return TRUE;
3134     }
3135
3136   return FALSE;
3137 }
3138
3139 /**
3140  * gtk_cell_area_get_focus_siblings:
3141  * @area: a #GtkCellArea
3142  * @renderer: the #GtkCellRenderer expected to have focus
3143  *
3144  * Gets the focus sibling cell renderers for @renderer.
3145  *
3146  * Return value: (element-type GtkCellRenderer) (transfer none): A #GList of #GtkCellRenderers.
3147  *       The returned list is internal and should not be freed.
3148  *
3149  * Since: 3.0
3150  */
3151 const GList *
3152 gtk_cell_area_get_focus_siblings (GtkCellArea     *area,
3153                                   GtkCellRenderer *renderer)
3154 {
3155   GtkCellAreaPrivate *priv;
3156
3157   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3158   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), NULL);
3159
3160   priv = area->priv;
3161
3162   return g_hash_table_lookup (priv->focus_siblings, renderer);
3163 }
3164
3165 /**
3166  * gtk_cell_area_get_focus_from_sibling:
3167  * @area: a #GtkCellArea
3168  * @renderer: the #GtkCellRenderer
3169  *
3170  * Gets the #GtkCellRenderer which is expected to be focusable
3171  * for which @renderer is, or may be a sibling.
3172  *
3173  * This is handy for #GtkCellArea subclasses when handling events,
3174  * after determining the renderer at the event location it can
3175  * then chose to activate the focus cell for which the event
3176  * cell may have been a sibling.
3177  *
3178  * Return value: the #GtkCellRenderer for which @renderer is a sibling, or %NULL.
3179  *
3180  * Since: 3.0
3181  */
3182 GtkCellRenderer *
3183 gtk_cell_area_get_focus_from_sibling (GtkCellArea          *area,
3184                                       GtkCellRenderer      *renderer)
3185 {
3186   GtkCellRenderer *ret_renderer = NULL;
3187   GList           *renderers, *l;
3188
3189   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3190   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), NULL);
3191
3192   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (area));
3193
3194   for (l = renderers; l; l = l->next)
3195     {
3196       GtkCellRenderer *a_renderer = l->data;
3197       const GList     *list;
3198
3199       for (list = gtk_cell_area_get_focus_siblings (area, a_renderer);
3200            list; list = list->next)
3201         {
3202           GtkCellRenderer *sibling_renderer = list->data;
3203
3204           if (sibling_renderer == renderer)
3205             {
3206               ret_renderer = a_renderer;
3207               break;
3208             }
3209         }
3210     }
3211   g_list_free (renderers);
3212
3213   return ret_renderer;
3214 }
3215
3216 /*************************************************************
3217  *              API: Cell Activation/Editing                 *
3218  *************************************************************/
3219 static void
3220 gtk_cell_area_add_editable (GtkCellArea        *area,
3221                             GtkCellRenderer    *renderer,
3222                             GtkCellEditable    *editable,
3223                             const GdkRectangle *cell_area)
3224 {
3225   g_signal_emit (area, cell_area_signals[SIGNAL_ADD_EDITABLE], 0,
3226                  renderer, editable, cell_area, area->priv->current_path);
3227 }
3228
3229 static void
3230 gtk_cell_area_remove_editable  (GtkCellArea        *area,
3231                                 GtkCellRenderer    *renderer,
3232                                 GtkCellEditable    *editable)
3233 {
3234   g_signal_emit (area, cell_area_signals[SIGNAL_REMOVE_EDITABLE], 0, renderer, editable);
3235 }
3236
3237 static void
3238 cell_area_remove_widget_cb (GtkCellEditable *editable,
3239                             GtkCellArea     *area)
3240 {
3241   GtkCellAreaPrivate *priv = area->priv;
3242
3243   g_assert (priv->edit_widget == editable);
3244   g_assert (priv->edited_cell != NULL);
3245
3246   gtk_cell_area_remove_editable (area, priv->edited_cell, priv->edit_widget);
3247
3248   /* Now that we're done with editing the widget and it can be removed,
3249    * remove our references to the widget and disconnect handlers */
3250   gtk_cell_area_set_edited_cell (area, NULL);
3251   gtk_cell_area_set_edit_widget (area, NULL);
3252 }
3253
3254 static void
3255 gtk_cell_area_set_edited_cell (GtkCellArea     *area,
3256                                GtkCellRenderer *renderer)
3257 {
3258   GtkCellAreaPrivate *priv;
3259
3260   g_return_if_fail (GTK_IS_CELL_AREA (area));
3261   g_return_if_fail (renderer == NULL || GTK_IS_CELL_RENDERER (renderer));
3262
3263   priv = area->priv;
3264
3265   if (priv->edited_cell != renderer)
3266     {
3267       if (priv->edited_cell)
3268         g_object_unref (priv->edited_cell);
3269
3270       priv->edited_cell = renderer;
3271
3272       if (priv->edited_cell)
3273         g_object_ref (priv->edited_cell);
3274
3275       g_object_notify (G_OBJECT (area), "edited-cell");
3276     }
3277 }
3278
3279 static void
3280 gtk_cell_area_set_edit_widget (GtkCellArea     *area,
3281                                GtkCellEditable *editable)
3282 {
3283   GtkCellAreaPrivate *priv;
3284
3285   g_return_if_fail (GTK_IS_CELL_AREA (area));
3286   g_return_if_fail (editable == NULL || GTK_IS_CELL_EDITABLE (editable));
3287
3288   priv = area->priv;
3289
3290   if (priv->edit_widget != editable)
3291     {
3292       if (priv->edit_widget)
3293         {
3294           g_signal_handler_disconnect (priv->edit_widget, priv->remove_widget_id);
3295
3296           g_object_unref (priv->edit_widget);
3297         }
3298
3299       priv->edit_widget = editable;
3300
3301       if (priv->edit_widget)
3302         {
3303           priv->remove_widget_id =
3304             g_signal_connect (priv->edit_widget, "remove-widget",
3305                               G_CALLBACK (cell_area_remove_widget_cb), area);
3306
3307           g_object_ref (priv->edit_widget);
3308         }
3309
3310       g_object_notify (G_OBJECT (area), "edit-widget");
3311     }
3312 }
3313
3314 /**
3315  * gtk_cell_area_get_edited_cell:
3316  * @area: a #GtkCellArea
3317  *
3318  * Gets the #GtkCellRenderer in @area that is currently
3319  * being edited.
3320  *
3321  * Return value: The currently edited #GtkCellRenderer
3322  *
3323  * Since: 3.0
3324  */
3325 GtkCellRenderer   *
3326 gtk_cell_area_get_edited_cell (GtkCellArea *area)
3327 {
3328   GtkCellAreaPrivate *priv;
3329
3330   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3331
3332   priv = area->priv;
3333
3334   return priv->edited_cell;
3335 }
3336
3337 /**
3338  * gtk_cell_area_get_edit_widget:
3339  * @area: a #GtkCellArea
3340  *
3341  * Gets the #GtkCellEditable widget currently used
3342  * to edit the currently edited cell.
3343  *
3344  * Return value: The currently active #GtkCellEditable widget
3345  *
3346  * Since: 3.0
3347  */
3348 GtkCellEditable *
3349 gtk_cell_area_get_edit_widget (GtkCellArea *area)
3350 {
3351   GtkCellAreaPrivate *priv;
3352
3353   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
3354
3355   priv = area->priv;
3356
3357   return priv->edit_widget;
3358 }
3359
3360 /**
3361  * gtk_cell_area_activate_cell:
3362  * @area: a #GtkCellArea
3363  * @widget: the #GtkWidget that @area is rendering onto
3364  * @renderer: the #GtkCellRenderer in @area to activate
3365  * @event: the #GdkEvent for which cell activation should occur
3366  * @cell_area: the #GdkRectangle in @widget relative coordinates
3367  *             of @renderer for the current row.
3368  * @flags: the #GtkCellRendererState for @renderer
3369  *
3370  * This is used by #GtkCellArea subclasses when handling events
3371  * to activate cells, the base #GtkCellArea class activates cells
3372  * for keyboard events for free in it's own GtkCellArea->activate()
3373  * implementation.
3374  *
3375  * Return value: whether cell activation was successful
3376  *
3377  * Since: 3.0
3378  */
3379 gboolean
3380 gtk_cell_area_activate_cell (GtkCellArea          *area,
3381                              GtkWidget            *widget,
3382                              GtkCellRenderer      *renderer,
3383                              GdkEvent             *event,
3384                              const GdkRectangle   *cell_area,
3385                              GtkCellRendererState  flags)
3386 {
3387   GtkCellRendererMode mode;
3388   GtkCellAreaPrivate *priv;
3389
3390   g_return_val_if_fail (GTK_IS_CELL_AREA (area), FALSE);
3391   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3392   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), FALSE);
3393   g_return_val_if_fail (cell_area != NULL, FALSE);
3394
3395   priv = area->priv;
3396
3397   g_object_get (renderer, "mode", &mode, NULL);
3398
3399   if (mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
3400     {
3401       if (gtk_cell_renderer_activate (renderer,
3402                                       event, widget,
3403                                       priv->current_path,
3404                                       cell_area,
3405                                       cell_area,
3406                                       flags))
3407         return TRUE;
3408     }
3409   else if (mode == GTK_CELL_RENDERER_MODE_EDITABLE)
3410     {
3411       GtkCellEditable *editable_widget;
3412       GdkRectangle inner_area;
3413
3414       gtk_cell_area_inner_cell_area (area, widget, cell_area, &inner_area);
3415
3416       editable_widget =
3417         gtk_cell_renderer_start_editing (renderer,
3418                                          event, widget,
3419                                          priv->current_path,
3420                                          &inner_area,
3421                                          &inner_area,
3422                                          flags);
3423
3424       if (editable_widget != NULL)
3425         {
3426           g_return_val_if_fail (GTK_IS_CELL_EDITABLE (editable_widget), FALSE);
3427
3428           gtk_cell_area_set_edited_cell (area, renderer);
3429           gtk_cell_area_set_edit_widget (area, editable_widget);
3430
3431           /* Signal that editing started so that callers can get
3432            * a handle on the editable_widget */
3433           gtk_cell_area_add_editable (area, priv->focus_cell, editable_widget, cell_area);
3434
3435           /* If the signal was successfully handled start the editing */
3436           if (gtk_widget_get_parent (GTK_WIDGET (editable_widget)))
3437             {
3438               gtk_cell_editable_start_editing (editable_widget, NULL);
3439               gtk_widget_grab_focus (GTK_WIDGET (editable_widget));
3440             }
3441           else
3442             {
3443               /* Otherwise clear the editing state and fire a warning */
3444               gtk_cell_area_set_edited_cell (area, NULL);
3445               gtk_cell_area_set_edit_widget (area, NULL);
3446
3447               g_warning ("GtkCellArea::add-editable fired in the dark, no cell editing was started.");
3448             }
3449
3450           return TRUE;
3451         }
3452     }
3453
3454   return FALSE;
3455 }
3456
3457 /**
3458  * gtk_cell_area_stop_editing:
3459  * @area: a #GtkCellArea
3460  * @canceled: whether editing was canceled.
3461  *
3462  * Explicitly stops the editing of the currently
3463  * edited cell (see gtk_cell_area_get_edited_cell()).
3464  *
3465  * If @canceled is %TRUE, the cell renderer will emit
3466  * the ::editing-canceled signal.
3467  *
3468  * Since: 3.0
3469  */
3470 void
3471 gtk_cell_area_stop_editing (GtkCellArea *area,
3472                             gboolean     canceled)
3473 {
3474   GtkCellAreaPrivate *priv;
3475
3476   g_return_if_fail (GTK_IS_CELL_AREA (area));
3477
3478   priv = area->priv;
3479
3480   if (priv->edited_cell)
3481     {
3482       GtkCellEditable *edit_widget = g_object_ref (priv->edit_widget);
3483       GtkCellRenderer *edit_cell   = g_object_ref (priv->edited_cell);
3484
3485       /* Stop editing of the cell renderer */
3486       gtk_cell_renderer_stop_editing (priv->edited_cell, canceled);
3487
3488       /* Remove any references to the editable widget */
3489       gtk_cell_area_set_edited_cell (area, NULL);
3490       gtk_cell_area_set_edit_widget (area, NULL);
3491
3492       /* Send the remove-widget signal explicitly (this is done after setting
3493        * the edit cell/widget NULL to avoid feedback)
3494        */
3495       gtk_cell_area_remove_editable (area, edit_cell, edit_widget);
3496       g_object_unref (edit_cell);
3497       g_object_unref (edit_widget);
3498     }
3499 }
3500
3501 /*************************************************************
3502  *         API: Convenience for area implementations         *
3503  *************************************************************/
3504
3505 /**
3506  * gtk_cell_area_inner_cell_area:
3507  * @area: a #GtkCellArea
3508  * @widget: the #GtkWidget that @area is rendering onto
3509  * @cell_area: the @widget relative coordinates where one of @area's cells
3510  *             is to be placed
3511  * @inner_area: (out): the return location for the inner cell area
3512  *
3513  * This is a convenience function for #GtkCellArea implementations
3514  * to get the inner area where a given #GtkCellRenderer will be
3515  * rendered. It removes any padding previously added by gtk_cell_area_request_renderer().
3516  *
3517  * Since: 3.0
3518  */
3519 void
3520 gtk_cell_area_inner_cell_area (GtkCellArea        *area,
3521                                GtkWidget          *widget,
3522                                const GdkRectangle *cell_area,
3523                                GdkRectangle       *inner_area)
3524 {
3525   gint focus_line_width;
3526
3527   g_return_if_fail (GTK_IS_CELL_AREA (area));
3528   g_return_if_fail (GTK_IS_WIDGET (widget));
3529   g_return_if_fail (cell_area != NULL);
3530   g_return_if_fail (inner_area != NULL);
3531
3532   gtk_widget_style_get (widget, "focus-line-width", &focus_line_width, NULL);
3533
3534   *inner_area = *cell_area;
3535
3536   inner_area->x      += focus_line_width;
3537   inner_area->width  -= focus_line_width * 2;
3538   inner_area->y      += focus_line_width;
3539   inner_area->height -= focus_line_width * 2;
3540 }
3541
3542 /**
3543  * gtk_cell_area_request_renderer:
3544  * @area: a #GtkCellArea
3545  * @renderer: the #GtkCellRenderer to request size for
3546  * @orientation: the #GtkOrientation in which to request size
3547  * @widget: the #GtkWidget that @area is rendering onto
3548  * @for_size: the allocation contextual size to request for, or -1 if
3549  * the base request for the orientation is to be returned.
3550  * @minimum_size: (out) (allow-none): location to store the minimum size, or %NULL
3551  * @natural_size: (out) (allow-none): location to store the natural size, or %NULL
3552  *
3553  * This is a convenience function for #GtkCellArea implementations
3554  * to request size for cell renderers. It's important to use this
3555  * function to request size and then use gtk_cell_area_inner_cell_area()
3556  * at render and event time since this function will add padding
3557  * around the cell for focus painting.
3558  *
3559  * Since: 3.0
3560  */
3561 void
3562 gtk_cell_area_request_renderer (GtkCellArea        *area,
3563                                 GtkCellRenderer    *renderer,
3564                                 GtkOrientation      orientation,
3565                                 GtkWidget          *widget,
3566                                 gint                for_size,
3567                                 gint               *minimum_size,
3568                                 gint               *natural_size)
3569 {
3570   GtkCellAreaPrivate *priv;
3571   gint                focus_line_width;
3572
3573   g_return_if_fail (GTK_IS_CELL_AREA (area));
3574   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
3575   g_return_if_fail (GTK_IS_WIDGET (widget));
3576   g_return_if_fail (minimum_size != NULL);
3577   g_return_if_fail (natural_size != NULL);
3578
3579   priv = area->priv;
3580
3581   gtk_widget_style_get (widget, "focus-line-width", &focus_line_width, NULL);
3582
3583   focus_line_width *= 2;
3584
3585   if (orientation == GTK_ORIENTATION_HORIZONTAL)
3586     {
3587       if (for_size < 0)
3588           gtk_cell_renderer_get_preferred_width (renderer, widget, minimum_size, natural_size);
3589       else
3590         {
3591           for_size = MAX (0, for_size - focus_line_width);
3592
3593           gtk_cell_renderer_get_preferred_width_for_height (renderer, widget, for_size,
3594                                                             minimum_size, natural_size);
3595         }
3596     }
3597   else /* GTK_ORIENTATION_VERTICAL */
3598     {
3599       if (for_size < 0)
3600         gtk_cell_renderer_get_preferred_height (renderer, widget, minimum_size, natural_size);
3601       else
3602         {
3603           for_size = MAX (0, for_size - focus_line_width);
3604
3605           gtk_cell_renderer_get_preferred_height_for_width (renderer, widget, for_size,
3606                                                             minimum_size, natural_size);
3607         }
3608     }
3609
3610   *minimum_size += focus_line_width;
3611   *natural_size += focus_line_width;
3612 }
3613
3614 void
3615 _gtk_cell_area_set_cell_data_func_with_proxy (GtkCellArea           *area,
3616                                               GtkCellRenderer       *cell,
3617                                               GFunc                  func,
3618                                               gpointer               func_data,
3619                                               GDestroyNotify         destroy,
3620                                               gpointer               proxy)
3621 {
3622   GtkCellAreaPrivate *priv;
3623   CellInfo           *info;
3624
3625   g_return_if_fail (GTK_IS_CELL_AREA (area));
3626   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3627
3628   priv = area->priv;
3629
3630   info = g_hash_table_lookup (priv->cell_info, cell);
3631
3632   /* Note we do not take a reference to the proxy, the proxy is a GtkCellLayout
3633    * that is forwarding it's implementation to a delegate GtkCellArea therefore
3634    * it's life-cycle is longer than the area's life cycle. 
3635    */
3636   if (info)
3637     {
3638       if (info->destroy && info->data)
3639         info->destroy (info->data);
3640
3641       if (func)
3642         {
3643           info->func    = (GtkCellLayoutDataFunc)func;
3644           info->data    = func_data;
3645           info->destroy = destroy;
3646           info->proxy   = proxy;
3647         }
3648       else
3649         {
3650           info->func    = NULL;
3651           info->data    = NULL;
3652           info->destroy = NULL;
3653           info->proxy   = NULL;
3654         }
3655     }
3656   else
3657     {
3658       info = cell_info_new ((GtkCellLayoutDataFunc)func, func_data, destroy);
3659
3660       g_hash_table_insert (priv->cell_info, cell, info);
3661     }
3662 }