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