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