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