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