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