]> Pileus Git - ~andy/gtk/blob - gtk/gtktreeviewcolumn.c
gtk: Don't set colormap anymore when creating GDK windows
[~andy/gtk] / gtk / gtktreeviewcolumn.c
1 /* gtktreeviewcolumn.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtktreeviewcolumn.h"
23
24 #include <string.h>
25
26 #include "gtkcellsizerequest.h"
27 #include "gtktreeview.h"
28 #include "gtktreeprivate.h"
29 #include "gtkcelllayout.h"
30 #include "gtkbutton.h"
31 #include "gtkalignment.h"
32 #include "gtklabel.h"
33 #include "gtkhbox.h"
34 #include "gtkmarshalers.h"
35 #include "gtkarrow.h"
36 #include "gtkprivate.h"
37 #include "gtkintl.h"
38
39
40 enum
41 {
42   PROP_0,
43   PROP_VISIBLE,
44   PROP_RESIZABLE,
45   PROP_WIDTH,
46   PROP_SPACING,
47   PROP_SIZING,
48   PROP_FIXED_WIDTH,
49   PROP_MIN_WIDTH,
50   PROP_MAX_WIDTH,
51   PROP_TITLE,
52   PROP_EXPAND,
53   PROP_CLICKABLE,
54   PROP_WIDGET,
55   PROP_ALIGNMENT,
56   PROP_REORDERABLE,
57   PROP_SORT_INDICATOR,
58   PROP_SORT_ORDER,
59   PROP_SORT_COLUMN_ID
60 };
61
62 enum
63 {
64   CLICKED,
65   LAST_SIGNAL
66 };
67
68 typedef struct _GtkTreeViewColumnCellInfo GtkTreeViewColumnCellInfo;
69 struct _GtkTreeViewColumnCellInfo
70 {
71   GtkCellRenderer *cell;
72   GSList *attributes;
73   GtkTreeCellDataFunc func;
74   gpointer func_data;
75   GDestroyNotify destroy;
76   gint requested_width;
77   gint real_width;
78   guint expand : 1;
79   guint pack : 1;
80   guint has_focus : 1;
81   guint in_editing_mode : 1;
82 };
83
84 /* Type methods */
85 static void gtk_tree_view_column_cell_layout_init              (GtkCellLayoutIface      *iface);
86
87 /* GObject methods */
88 static void gtk_tree_view_column_set_property                  (GObject                 *object,
89                                                                 guint                    prop_id,
90                                                                 const GValue            *value,
91                                                                 GParamSpec              *pspec);
92 static void gtk_tree_view_column_get_property                  (GObject                 *object,
93                                                                 guint                    prop_id,
94                                                                 GValue                  *value,
95                                                                 GParamSpec              *pspec);
96 static void gtk_tree_view_column_finalize                      (GObject                 *object);
97
98 /* GtkCellLayout implementation */
99 static void gtk_tree_view_column_cell_layout_pack_start         (GtkCellLayout         *cell_layout,
100                                                                  GtkCellRenderer       *cell,
101                                                                  gboolean               expand);
102 static void gtk_tree_view_column_cell_layout_pack_end           (GtkCellLayout         *cell_layout,
103                                                                  GtkCellRenderer       *cell,
104                                                                  gboolean               expand);
105 static void gtk_tree_view_column_cell_layout_clear              (GtkCellLayout         *cell_layout);
106 static void gtk_tree_view_column_cell_layout_add_attribute      (GtkCellLayout         *cell_layout,
107                                                                  GtkCellRenderer       *cell,
108                                                                  const gchar           *attribute,
109                                                                  gint                   column);
110 static void gtk_tree_view_column_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
111                                                                  GtkCellRenderer       *cell,
112                                                                  GtkCellLayoutDataFunc  func,
113                                                                  gpointer               func_data,
114                                                                  GDestroyNotify         destroy);
115 static void gtk_tree_view_column_cell_layout_clear_attributes   (GtkCellLayout         *cell_layout,
116                                                                  GtkCellRenderer       *cell);
117 static void gtk_tree_view_column_cell_layout_reorder            (GtkCellLayout         *cell_layout,
118                                                                  GtkCellRenderer       *cell,
119                                                                  gint                   position);
120 static GList *gtk_tree_view_column_cell_layout_get_cells        (GtkCellLayout         *cell_layout);
121
122 /* Button handling code */
123 static void gtk_tree_view_column_create_button                 (GtkTreeViewColumn       *tree_column);
124 static void gtk_tree_view_column_update_button                 (GtkTreeViewColumn       *tree_column);
125
126 /* Button signal handlers */
127 static gint gtk_tree_view_column_button_event                  (GtkWidget               *widget,
128                                                                 GdkEvent                *event,
129                                                                 gpointer                 data);
130 static void gtk_tree_view_column_button_clicked                (GtkWidget               *widget,
131                                                                 gpointer                 data);
132 static gboolean gtk_tree_view_column_mnemonic_activate         (GtkWidget *widget,
133                                                                 gboolean   group_cycling,
134                                                                 gpointer   data);
135
136 /* Property handlers */
137 static void gtk_tree_view_model_sort_column_changed            (GtkTreeSortable         *sortable,
138                                                                 GtkTreeViewColumn       *tree_column);
139
140 /* Internal functions */
141 static void gtk_tree_view_column_sort                          (GtkTreeViewColumn       *tree_column,
142                                                                 gpointer                 data);
143 static void gtk_tree_view_column_setup_sort_column_id_callback (GtkTreeViewColumn       *tree_column);
144 static void gtk_tree_view_column_set_attributesv               (GtkTreeViewColumn       *tree_column,
145                                                                 GtkCellRenderer         *cell_renderer,
146                                                                 va_list                  args);
147 static GtkTreeViewColumnCellInfo *gtk_tree_view_column_get_cell_info (GtkTreeViewColumn *tree_column,
148                                                                       GtkCellRenderer   *cell_renderer);
149
150 /* cell list manipulation */
151 static GList *gtk_tree_view_column_cell_first                  (GtkTreeViewColumn      *tree_column);
152 static GList *gtk_tree_view_column_cell_last                   (GtkTreeViewColumn      *tree_column);
153 static GList *gtk_tree_view_column_cell_next                   (GtkTreeViewColumn      *tree_column,
154                                                                 GList                  *current);
155 static GList *gtk_tree_view_column_cell_prev                   (GtkTreeViewColumn      *tree_column,
156                                                                 GList                  *current);
157 static void gtk_tree_view_column_clear_attributes_by_info      (GtkTreeViewColumn      *tree_column,
158                                                                 GtkTreeViewColumnCellInfo *info);
159 /* GtkBuildable implementation */
160 static void gtk_tree_view_column_buildable_init                 (GtkBuildableIface     *iface);
161
162 static guint tree_column_signals[LAST_SIGNAL] = { 0 };
163
164 G_DEFINE_TYPE_WITH_CODE (GtkTreeViewColumn, gtk_tree_view_column, GTK_TYPE_OBJECT,
165                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
166                                                 gtk_tree_view_column_cell_layout_init)
167                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
168                                                 gtk_tree_view_column_buildable_init))
169
170
171 static void
172 gtk_tree_view_column_class_init (GtkTreeViewColumnClass *class)
173 {
174   GObjectClass *object_class;
175
176   object_class = (GObjectClass*) class;
177
178   class->clicked = NULL;
179
180   object_class->finalize = gtk_tree_view_column_finalize;
181   object_class->set_property = gtk_tree_view_column_set_property;
182   object_class->get_property = gtk_tree_view_column_get_property;
183   
184   tree_column_signals[CLICKED] =
185     g_signal_new (I_("clicked"),
186                   G_OBJECT_CLASS_TYPE (object_class),
187                   G_SIGNAL_RUN_LAST,
188                   G_STRUCT_OFFSET (GtkTreeViewColumnClass, clicked),
189                   NULL, NULL,
190                   _gtk_marshal_VOID__VOID,
191                   G_TYPE_NONE, 0);
192
193   g_object_class_install_property (object_class,
194                                    PROP_VISIBLE,
195                                    g_param_spec_boolean ("visible",
196                                                         P_("Visible"),
197                                                         P_("Whether to display the column"),
198                                                          TRUE,
199                                                          GTK_PARAM_READWRITE));
200   
201   g_object_class_install_property (object_class,
202                                    PROP_RESIZABLE,
203                                    g_param_spec_boolean ("resizable",
204                                                          P_("Resizable"),
205                                                          P_("Column is user-resizable"),
206                                                          FALSE,
207                                                          GTK_PARAM_READWRITE));
208   
209   g_object_class_install_property (object_class,
210                                    PROP_WIDTH,
211                                    g_param_spec_int ("width",
212                                                      P_("Width"),
213                                                      P_("Current width of the column"),
214                                                      0,
215                                                      G_MAXINT,
216                                                      0,
217                                                      GTK_PARAM_READABLE));
218   g_object_class_install_property (object_class,
219                                    PROP_SPACING,
220                                    g_param_spec_int ("spacing",
221                                                      P_("Spacing"),
222                                                      P_("Space which is inserted between cells"),
223                                                      0,
224                                                      G_MAXINT,
225                                                      0,
226                                                      GTK_PARAM_READWRITE));
227   g_object_class_install_property (object_class,
228                                    PROP_SIZING,
229                                    g_param_spec_enum ("sizing",
230                                                       P_("Sizing"),
231                                                       P_("Resize mode of the column"),
232                                                       GTK_TYPE_TREE_VIEW_COLUMN_SIZING,
233                                                       GTK_TREE_VIEW_COLUMN_GROW_ONLY,
234                                                       GTK_PARAM_READWRITE));
235   
236   g_object_class_install_property (object_class,
237                                    PROP_FIXED_WIDTH,
238                                    g_param_spec_int ("fixed-width",
239                                                      P_("Fixed Width"),
240                                                      P_("Current fixed width of the column"),
241                                                      1,
242                                                      G_MAXINT,
243                                                      1, /* not useful */
244                                                      GTK_PARAM_READWRITE));
245
246   g_object_class_install_property (object_class,
247                                    PROP_MIN_WIDTH,
248                                    g_param_spec_int ("min-width",
249                                                      P_("Minimum Width"),
250                                                      P_("Minimum allowed width of the column"),
251                                                      -1,
252                                                      G_MAXINT,
253                                                      -1,
254                                                      GTK_PARAM_READWRITE));
255
256   g_object_class_install_property (object_class,
257                                    PROP_MAX_WIDTH,
258                                    g_param_spec_int ("max-width",
259                                                      P_("Maximum Width"),
260                                                      P_("Maximum allowed width of the column"),
261                                                      -1,
262                                                      G_MAXINT,
263                                                      -1,
264                                                      GTK_PARAM_READWRITE));
265
266   g_object_class_install_property (object_class,
267                                    PROP_TITLE,
268                                    g_param_spec_string ("title",
269                                                         P_("Title"),
270                                                         P_("Title to appear in column header"),
271                                                         "",
272                                                         GTK_PARAM_READWRITE));
273   
274   g_object_class_install_property (object_class,
275                                    PROP_EXPAND,
276                                    g_param_spec_boolean ("expand",
277                                                          P_("Expand"),
278                                                          P_("Column gets share of extra width allocated to the widget"),
279                                                          FALSE,
280                                                          GTK_PARAM_READWRITE));
281   
282   g_object_class_install_property (object_class,
283                                    PROP_CLICKABLE,
284                                    g_param_spec_boolean ("clickable",
285                                                         P_("Clickable"),
286                                                         P_("Whether the header can be clicked"),
287                                                          FALSE,
288                                                          GTK_PARAM_READWRITE));
289   
290
291   g_object_class_install_property (object_class,
292                                    PROP_WIDGET,
293                                    g_param_spec_object ("widget",
294                                                         P_("Widget"),
295                                                         P_("Widget to put in column header button instead of column title"),
296                                                         GTK_TYPE_WIDGET,
297                                                         GTK_PARAM_READWRITE));
298
299   g_object_class_install_property (object_class,
300                                    PROP_ALIGNMENT,
301                                    g_param_spec_float ("alignment",
302                                                        P_("Alignment"),
303                                                        P_("X Alignment of the column header text or widget"),
304                                                        0.0,
305                                                        1.0,
306                                                        0.0,
307                                                        GTK_PARAM_READWRITE));
308
309   g_object_class_install_property (object_class,
310                                    PROP_REORDERABLE,
311                                    g_param_spec_boolean ("reorderable",
312                                                          P_("Reorderable"),
313                                                          P_("Whether the column can be reordered around the headers"),
314                                                          FALSE,
315                                                          GTK_PARAM_READWRITE));
316
317   g_object_class_install_property (object_class,
318                                    PROP_SORT_INDICATOR,
319                                    g_param_spec_boolean ("sort-indicator",
320                                                         P_("Sort indicator"),
321                                                         P_("Whether to show a sort indicator"),
322                                                          FALSE,
323                                                          GTK_PARAM_READWRITE));
324
325   g_object_class_install_property (object_class,
326                                    PROP_SORT_ORDER,
327                                    g_param_spec_enum ("sort-order",
328                                                       P_("Sort order"),
329                                                       P_("Sort direction the sort indicator should indicate"),
330                                                       GTK_TYPE_SORT_TYPE,
331                                                       GTK_SORT_ASCENDING,
332                                                       GTK_PARAM_READWRITE));
333
334   /**
335    * GtkTreeViewColumn:sort-column-id:
336    *
337    * Logical sort column ID this column sorts on when selected for sorting. Setting the sort column ID makes the column header
338    * clickable. Set to %-1 to make the column unsortable.
339    *
340    * Since: 2.18
341    **/
342   g_object_class_install_property (object_class,
343                                    PROP_SORT_COLUMN_ID,
344                                    g_param_spec_int ("sort-column-id",
345                                                      P_("Sort column ID"),
346                                                      P_("Logical sort column ID this column sorts on when selected for sorting"),
347                                                      -1,
348                                                      G_MAXINT,
349                                                      -1,
350                                                      GTK_PARAM_READWRITE));
351 }
352
353 static void
354 gtk_tree_view_column_buildable_init (GtkBuildableIface *iface)
355 {
356   iface->add_child = _gtk_cell_layout_buildable_add_child;
357   iface->custom_tag_start = _gtk_cell_layout_buildable_custom_tag_start;
358   iface->custom_tag_end = _gtk_cell_layout_buildable_custom_tag_end;
359 }
360
361 static void
362 gtk_tree_view_column_cell_layout_init (GtkCellLayoutIface *iface)
363 {
364   iface->pack_start = gtk_tree_view_column_cell_layout_pack_start;
365   iface->pack_end = gtk_tree_view_column_cell_layout_pack_end;
366   iface->clear = gtk_tree_view_column_cell_layout_clear;
367   iface->add_attribute = gtk_tree_view_column_cell_layout_add_attribute;
368   iface->set_cell_data_func = gtk_tree_view_column_cell_layout_set_cell_data_func;
369   iface->clear_attributes = gtk_tree_view_column_cell_layout_clear_attributes;
370   iface->reorder = gtk_tree_view_column_cell_layout_reorder;
371   iface->get_cells = gtk_tree_view_column_cell_layout_get_cells;
372 }
373
374 static void
375 gtk_tree_view_column_init (GtkTreeViewColumn *tree_column)
376 {
377   tree_column->button = NULL;
378   tree_column->xalign = 0.0;
379   tree_column->width = 0;
380   tree_column->spacing = 0;
381   tree_column->requested_width = -1;
382   tree_column->min_width = -1;
383   tree_column->max_width = -1;
384   tree_column->resized_width = 0;
385   tree_column->column_type = GTK_TREE_VIEW_COLUMN_GROW_ONLY;
386   tree_column->visible = TRUE;
387   tree_column->resizable = FALSE;
388   tree_column->expand = FALSE;
389   tree_column->clickable = FALSE;
390   tree_column->dirty = TRUE;
391   tree_column->sort_order = GTK_SORT_ASCENDING;
392   tree_column->show_sort_indicator = FALSE;
393   tree_column->property_changed_signal = 0;
394   tree_column->sort_clicked_signal = 0;
395   tree_column->sort_column_changed_signal = 0;
396   tree_column->sort_column_id = -1;
397   tree_column->reorderable = FALSE;
398   tree_column->maybe_reordered = FALSE;
399   tree_column->fixed_width = 1;
400   tree_column->use_resized_width = FALSE;
401   tree_column->title = g_strdup ("");
402 }
403
404 static void
405 gtk_tree_view_column_finalize (GObject *object)
406 {
407   GtkTreeViewColumn *tree_column = (GtkTreeViewColumn *) object;
408   GList *list;
409
410   for (list = tree_column->cell_list; list; list = list->next)
411     {
412       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
413
414       if (info->destroy)
415         {
416           GDestroyNotify d = info->destroy;
417
418           info->destroy = NULL;
419           d (info->func_data);
420         }
421       gtk_tree_view_column_clear_attributes_by_info (tree_column, info);
422       g_object_unref (info->cell);
423       g_free (info);
424     }
425
426   g_free (tree_column->title);
427   g_list_free (tree_column->cell_list);
428
429   if (tree_column->child)
430     g_object_unref (tree_column->child);
431
432   G_OBJECT_CLASS (gtk_tree_view_column_parent_class)->finalize (object);
433 }
434
435 static void
436 gtk_tree_view_column_set_property (GObject         *object,
437                                    guint            prop_id,
438                                    const GValue    *value,
439                                    GParamSpec      *pspec)
440 {
441   GtkTreeViewColumn *tree_column;
442
443   tree_column = GTK_TREE_VIEW_COLUMN (object);
444
445   switch (prop_id)
446     {
447     case PROP_VISIBLE:
448       gtk_tree_view_column_set_visible (tree_column,
449                                         g_value_get_boolean (value));
450       break;
451
452     case PROP_RESIZABLE:
453       gtk_tree_view_column_set_resizable (tree_column,
454                                           g_value_get_boolean (value));
455       break;
456
457     case PROP_SIZING:
458       gtk_tree_view_column_set_sizing (tree_column,
459                                        g_value_get_enum (value));
460       break;
461
462     case PROP_FIXED_WIDTH:
463       gtk_tree_view_column_set_fixed_width (tree_column,
464                                             g_value_get_int (value));
465       break;
466
467     case PROP_MIN_WIDTH:
468       gtk_tree_view_column_set_min_width (tree_column,
469                                           g_value_get_int (value));
470       break;
471
472     case PROP_MAX_WIDTH:
473       gtk_tree_view_column_set_max_width (tree_column,
474                                           g_value_get_int (value));
475       break;
476
477     case PROP_SPACING:
478       gtk_tree_view_column_set_spacing (tree_column,
479                                         g_value_get_int (value));
480       break;
481
482     case PROP_TITLE:
483       gtk_tree_view_column_set_title (tree_column,
484                                       g_value_get_string (value));
485       break;
486
487     case PROP_EXPAND:
488       gtk_tree_view_column_set_expand (tree_column,
489                                        g_value_get_boolean (value));
490       break;
491
492     case PROP_CLICKABLE:
493       gtk_tree_view_column_set_clickable (tree_column,
494                                           g_value_get_boolean (value));
495       break;
496
497     case PROP_WIDGET:
498       gtk_tree_view_column_set_widget (tree_column,
499                                        (GtkWidget*) g_value_get_object (value));
500       break;
501
502     case PROP_ALIGNMENT:
503       gtk_tree_view_column_set_alignment (tree_column,
504                                           g_value_get_float (value));
505       break;
506
507     case PROP_REORDERABLE:
508       gtk_tree_view_column_set_reorderable (tree_column,
509                                             g_value_get_boolean (value));
510       break;
511
512     case PROP_SORT_INDICATOR:
513       gtk_tree_view_column_set_sort_indicator (tree_column,
514                                                g_value_get_boolean (value));
515       break;
516
517     case PROP_SORT_ORDER:
518       gtk_tree_view_column_set_sort_order (tree_column,
519                                            g_value_get_enum (value));
520       break;
521       
522     case PROP_SORT_COLUMN_ID:
523       gtk_tree_view_column_set_sort_column_id (tree_column,
524                                                g_value_get_int (value));
525       break;
526       
527     default:
528       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529       break;
530     }
531 }
532
533 static void
534 gtk_tree_view_column_get_property (GObject         *object,
535                                    guint            prop_id,
536                                    GValue          *value,
537                                    GParamSpec      *pspec)
538 {
539   GtkTreeViewColumn *tree_column;
540
541   tree_column = GTK_TREE_VIEW_COLUMN (object);
542
543   switch (prop_id)
544     {
545     case PROP_VISIBLE:
546       g_value_set_boolean (value,
547                            gtk_tree_view_column_get_visible (tree_column));
548       break;
549
550     case PROP_RESIZABLE:
551       g_value_set_boolean (value,
552                            gtk_tree_view_column_get_resizable (tree_column));
553       break;
554
555     case PROP_WIDTH:
556       g_value_set_int (value,
557                        gtk_tree_view_column_get_width (tree_column));
558       break;
559
560     case PROP_SPACING:
561       g_value_set_int (value,
562                        gtk_tree_view_column_get_spacing (tree_column));
563       break;
564
565     case PROP_SIZING:
566       g_value_set_enum (value,
567                         gtk_tree_view_column_get_sizing (tree_column));
568       break;
569
570     case PROP_FIXED_WIDTH:
571       g_value_set_int (value,
572                        gtk_tree_view_column_get_fixed_width (tree_column));
573       break;
574
575     case PROP_MIN_WIDTH:
576       g_value_set_int (value,
577                        gtk_tree_view_column_get_min_width (tree_column));
578       break;
579
580     case PROP_MAX_WIDTH:
581       g_value_set_int (value,
582                        gtk_tree_view_column_get_max_width (tree_column));
583       break;
584
585     case PROP_TITLE:
586       g_value_set_string (value,
587                           gtk_tree_view_column_get_title (tree_column));
588       break;
589
590     case PROP_EXPAND:
591       g_value_set_boolean (value,
592                           gtk_tree_view_column_get_expand (tree_column));
593       break;
594
595     case PROP_CLICKABLE:
596       g_value_set_boolean (value,
597                            gtk_tree_view_column_get_clickable (tree_column));
598       break;
599
600     case PROP_WIDGET:
601       g_value_set_object (value,
602                           (GObject*) gtk_tree_view_column_get_widget (tree_column));
603       break;
604
605     case PROP_ALIGNMENT:
606       g_value_set_float (value,
607                          gtk_tree_view_column_get_alignment (tree_column));
608       break;
609
610     case PROP_REORDERABLE:
611       g_value_set_boolean (value,
612                            gtk_tree_view_column_get_reorderable (tree_column));
613       break;
614
615     case PROP_SORT_INDICATOR:
616       g_value_set_boolean (value,
617                            gtk_tree_view_column_get_sort_indicator (tree_column));
618       break;
619
620     case PROP_SORT_ORDER:
621       g_value_set_enum (value,
622                         gtk_tree_view_column_get_sort_order (tree_column));
623       break;
624       
625     case PROP_SORT_COLUMN_ID:
626       g_value_set_int (value,
627                        gtk_tree_view_column_get_sort_column_id (tree_column));
628       break;
629       
630     default:
631       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632       break;
633     }
634 }
635
636 /* Implementation of GtkCellLayout interface
637  */
638
639 static void
640 gtk_tree_view_column_cell_layout_pack_start (GtkCellLayout   *cell_layout,
641                                              GtkCellRenderer *cell,
642                                              gboolean         expand)
643 {
644   GtkTreeViewColumn *column;
645   GtkTreeViewColumnCellInfo *cell_info;
646
647   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
648   column = GTK_TREE_VIEW_COLUMN (cell_layout);
649   g_return_if_fail (! gtk_tree_view_column_get_cell_info (column, cell));
650
651   g_object_ref_sink (cell);
652
653   cell_info = g_new0 (GtkTreeViewColumnCellInfo, 1);
654   cell_info->cell = cell;
655   cell_info->expand = expand ? TRUE : FALSE;
656   cell_info->pack = GTK_PACK_START;
657   cell_info->has_focus = 0;
658   cell_info->attributes = NULL;
659
660   column->cell_list = g_list_append (column->cell_list, cell_info);
661 }
662
663 static void
664 gtk_tree_view_column_cell_layout_pack_end (GtkCellLayout   *cell_layout,
665                                            GtkCellRenderer *cell,
666                                            gboolean         expand)
667 {
668   GtkTreeViewColumn *column;
669   GtkTreeViewColumnCellInfo *cell_info;
670
671   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
672   column = GTK_TREE_VIEW_COLUMN (cell_layout);
673   g_return_if_fail (! gtk_tree_view_column_get_cell_info (column, cell));
674
675   g_object_ref_sink (cell);
676
677   cell_info = g_new0 (GtkTreeViewColumnCellInfo, 1);
678   cell_info->cell = cell;
679   cell_info->expand = expand ? TRUE : FALSE;
680   cell_info->pack = GTK_PACK_END;
681   cell_info->has_focus = 0;
682   cell_info->attributes = NULL;
683
684   column->cell_list = g_list_append (column->cell_list, cell_info);
685 }
686
687 static void
688 gtk_tree_view_column_cell_layout_clear (GtkCellLayout *cell_layout)
689 {
690   GtkTreeViewColumn *column;
691
692   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
693   column = GTK_TREE_VIEW_COLUMN (cell_layout);
694
695   while (column->cell_list)
696     {
697       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *)column->cell_list->data;
698
699       gtk_tree_view_column_cell_layout_clear_attributes (cell_layout, info->cell);
700       g_object_unref (info->cell);
701       g_free (info);
702       column->cell_list = g_list_delete_link (column->cell_list, 
703                                               column->cell_list);
704     }
705 }
706
707 static void
708 gtk_tree_view_column_cell_layout_add_attribute (GtkCellLayout   *cell_layout,
709                                                 GtkCellRenderer *cell,
710                                                 const gchar     *attribute,
711                                                 gint             column)
712 {
713   GtkTreeViewColumn *tree_column;
714   GtkTreeViewColumnCellInfo *info;
715
716   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
717   tree_column = GTK_TREE_VIEW_COLUMN (cell_layout);
718
719   info = gtk_tree_view_column_get_cell_info (tree_column, cell);
720   g_return_if_fail (info != NULL);
721
722   info->attributes = g_slist_prepend (info->attributes, GINT_TO_POINTER (column));
723   info->attributes = g_slist_prepend (info->attributes, g_strdup (attribute));
724
725   if (tree_column->tree_view)
726     _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
727 }
728
729 static void
730 gtk_tree_view_column_cell_layout_set_cell_data_func (GtkCellLayout         *cell_layout,
731                                                      GtkCellRenderer       *cell,
732                                                      GtkCellLayoutDataFunc  func,
733                                                      gpointer               func_data,
734                                                      GDestroyNotify         destroy)
735 {
736   GtkTreeViewColumn *column;
737   GtkTreeViewColumnCellInfo *info;
738
739   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
740   column = GTK_TREE_VIEW_COLUMN (cell_layout);
741
742   info = gtk_tree_view_column_get_cell_info (column, cell);
743   g_return_if_fail (info != NULL);
744
745   if (info->destroy)
746     {
747       GDestroyNotify d = info->destroy;
748
749       info->destroy = NULL;
750       d (info->func_data);
751     }
752
753   info->func = (GtkTreeCellDataFunc)func;
754   info->func_data = func_data;
755   info->destroy = destroy;
756
757   if (column->tree_view)
758     _gtk_tree_view_column_cell_set_dirty (column, TRUE);
759 }
760
761 static void
762 gtk_tree_view_column_cell_layout_clear_attributes (GtkCellLayout    *cell_layout,
763                                                    GtkCellRenderer  *cell_renderer)
764 {
765   GtkTreeViewColumn *column;
766   GtkTreeViewColumnCellInfo *info;
767
768   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
769   column = GTK_TREE_VIEW_COLUMN (cell_layout);
770
771   info = gtk_tree_view_column_get_cell_info (column, cell_renderer);
772   if (info)
773     gtk_tree_view_column_clear_attributes_by_info (column, info);
774 }
775
776 static void
777 gtk_tree_view_column_cell_layout_reorder (GtkCellLayout   *cell_layout,
778                                           GtkCellRenderer *cell,
779                                           gint             position)
780 {
781   GList *link;
782   GtkTreeViewColumn *column;
783   GtkTreeViewColumnCellInfo *info;
784
785   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (cell_layout));
786   column = GTK_TREE_VIEW_COLUMN (cell_layout);
787
788   info = gtk_tree_view_column_get_cell_info (column, cell);
789
790   g_return_if_fail (info != NULL);
791   g_return_if_fail (position >= 0);
792
793   link = g_list_find (column->cell_list, info);
794
795   g_return_if_fail (link != NULL);
796
797   column->cell_list = g_list_delete_link (column->cell_list, link);
798   column->cell_list = g_list_insert (column->cell_list, info, position);
799
800   if (column->tree_view)
801     gtk_widget_queue_draw (column->tree_view);
802 }
803
804 static void
805 gtk_tree_view_column_clear_attributes_by_info (GtkTreeViewColumn *tree_column,
806                                                GtkTreeViewColumnCellInfo *info)
807 {
808   GSList *list;
809
810   list = info->attributes;
811
812   while (list && list->next)
813     {
814       g_free (list->data);
815       list = list->next->next;
816     }
817   g_slist_free (info->attributes);
818   info->attributes = NULL;
819
820   if (tree_column->tree_view)
821     _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
822 }
823
824 /* Helper functions
825  */
826
827 /* Button handling code
828  */
829 static void
830 gtk_tree_view_column_create_button (GtkTreeViewColumn *tree_column)
831 {
832   GtkTreeView *tree_view;
833   GtkWidget *child;
834   GtkWidget *hbox;
835
836   tree_view = (GtkTreeView *) tree_column->tree_view;
837
838   g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
839   g_return_if_fail (tree_column->button == NULL);
840
841   gtk_widget_push_composite_child ();
842   tree_column->button = gtk_button_new ();
843   gtk_widget_add_events (tree_column->button, GDK_POINTER_MOTION_MASK);
844   gtk_widget_pop_composite_child ();
845
846   /* make sure we own a reference to it as well. */
847   if (tree_view->priv->header_window)
848     gtk_widget_set_parent_window (tree_column->button, tree_view->priv->header_window);
849   gtk_widget_set_parent (tree_column->button, GTK_WIDGET (tree_view));
850
851   g_signal_connect (tree_column->button, "event",
852                     G_CALLBACK (gtk_tree_view_column_button_event),
853                     tree_column);
854   g_signal_connect (tree_column->button, "clicked",
855                     G_CALLBACK (gtk_tree_view_column_button_clicked),
856                     tree_column);
857
858   tree_column->alignment = gtk_alignment_new (tree_column->xalign, 0.5, 0.0, 0.0);
859
860   hbox = gtk_hbox_new (FALSE, 2);
861   tree_column->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_IN);
862
863   if (tree_column->child)
864     child = tree_column->child;
865   else
866     {
867       child = gtk_label_new (tree_column->title);
868       gtk_widget_show (child);
869     }
870
871   g_signal_connect (child, "mnemonic-activate",
872                     G_CALLBACK (gtk_tree_view_column_mnemonic_activate),
873                     tree_column);
874
875   if (tree_column->xalign <= 0.5)
876     gtk_box_pack_end (GTK_BOX (hbox), tree_column->arrow, FALSE, FALSE, 0);
877   else
878     gtk_box_pack_start (GTK_BOX (hbox), tree_column->arrow, FALSE, FALSE, 0);
879
880   gtk_box_pack_start (GTK_BOX (hbox), tree_column->alignment, TRUE, TRUE, 0);
881         
882   gtk_container_add (GTK_CONTAINER (tree_column->alignment), child);
883   gtk_container_add (GTK_CONTAINER (tree_column->button), hbox);
884
885   gtk_widget_show (hbox);
886   gtk_widget_show (tree_column->alignment);
887   gtk_tree_view_column_update_button (tree_column);
888 }
889
890 static void 
891 gtk_tree_view_column_update_button (GtkTreeViewColumn *tree_column)
892 {
893   gint sort_column_id = -1;
894   GtkWidget *hbox;
895   GtkWidget *alignment;
896   GtkWidget *arrow;
897   GtkWidget *current_child;
898   GtkArrowType arrow_type = GTK_ARROW_NONE;
899   GtkTreeModel *model;
900
901   if (tree_column->tree_view)
902     model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_column->tree_view));
903   else
904     model = NULL;
905
906   /* Create a button if necessary */
907   if (tree_column->visible &&
908       tree_column->button == NULL &&
909       tree_column->tree_view &&
910       gtk_widget_get_realized (tree_column->tree_view))
911     gtk_tree_view_column_create_button (tree_column);
912   
913   if (! tree_column->button)
914     return;
915
916   hbox = gtk_bin_get_child (GTK_BIN (tree_column->button));
917   alignment = tree_column->alignment;
918   arrow = tree_column->arrow;
919   current_child = gtk_bin_get_child (GTK_BIN (alignment));
920
921   /* Set up the actual button */
922   gtk_alignment_set (GTK_ALIGNMENT (alignment), tree_column->xalign,
923                      0.5, 0.0, 0.0);
924       
925   if (tree_column->child)
926     {
927       if (current_child != tree_column->child)
928         {
929           gtk_container_remove (GTK_CONTAINER (alignment),
930                                 current_child);
931           gtk_container_add (GTK_CONTAINER (alignment),
932                              tree_column->child);
933         }
934     }
935   else 
936     {
937       if (current_child == NULL)
938         {
939           current_child = gtk_label_new (NULL);
940           gtk_widget_show (current_child);
941           gtk_container_add (GTK_CONTAINER (alignment),
942                              current_child);
943         }
944
945       g_return_if_fail (GTK_IS_LABEL (current_child));
946
947       if (tree_column->title)
948         gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
949                                           tree_column->title);
950       else
951         gtk_label_set_text_with_mnemonic (GTK_LABEL (current_child),
952                                           "");
953     }
954
955   if (GTK_IS_TREE_SORTABLE (model))
956     gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (model),
957                                           &sort_column_id,
958                                           NULL);
959
960   if (tree_column->show_sort_indicator)
961     {
962       gboolean alternative;
963
964       g_object_get (gtk_widget_get_settings (tree_column->tree_view),
965                     "gtk-alternative-sort-arrows", &alternative,
966                     NULL);
967
968       switch (tree_column->sort_order)
969         {
970           case GTK_SORT_ASCENDING:
971             arrow_type = alternative ? GTK_ARROW_UP : GTK_ARROW_DOWN;
972             break;
973
974           case GTK_SORT_DESCENDING:
975             arrow_type = alternative ? GTK_ARROW_DOWN : GTK_ARROW_UP;
976             break;
977
978           default:
979             g_warning (G_STRLOC": bad sort order");
980             break;
981         }
982     }
983
984   gtk_arrow_set (GTK_ARROW (arrow),
985                  arrow_type,
986                  GTK_SHADOW_IN);
987
988   /* Put arrow on the right if the text is left-or-center justified, and on the
989    * left otherwise; do this by packing boxes, so flipping text direction will
990    * reverse things
991    */
992   g_object_ref (arrow);
993   gtk_container_remove (GTK_CONTAINER (hbox), arrow);
994
995   if (tree_column->xalign <= 0.5)
996     {
997       gtk_box_pack_end (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
998     }
999   else
1000     {
1001       gtk_box_pack_start (GTK_BOX (hbox), arrow, FALSE, FALSE, 0);
1002       /* move it to the front */
1003       gtk_box_reorder_child (GTK_BOX (hbox), arrow, 0);
1004     }
1005   g_object_unref (arrow);
1006
1007   if (tree_column->show_sort_indicator
1008       || (GTK_IS_TREE_SORTABLE (model) && tree_column->sort_column_id >= 0))
1009     gtk_widget_show (arrow);
1010   else
1011     gtk_widget_hide (arrow);
1012
1013   /* It's always safe to hide the button.  It isn't always safe to show it, as
1014    * if you show it before it's realized, it'll get the wrong window. */
1015   if (tree_column->button &&
1016       tree_column->tree_view != NULL &&
1017       gtk_widget_get_realized (tree_column->tree_view))
1018     {
1019       if (tree_column->visible)
1020         {
1021           gtk_widget_show_now (tree_column->button);
1022           if (tree_column->window)
1023             {
1024               if (tree_column->resizable)
1025                 {
1026                   gdk_window_show (tree_column->window);
1027                   gdk_window_raise (tree_column->window);
1028                 }
1029               else
1030                 {
1031                   gdk_window_hide (tree_column->window);
1032                 }
1033             }
1034         }
1035       else
1036         {
1037           gtk_widget_hide (tree_column->button);
1038           if (tree_column->window)
1039             gdk_window_hide (tree_column->window);
1040         }
1041     }
1042   
1043   if (tree_column->reorderable || tree_column->clickable)
1044     {
1045       gtk_widget_set_can_focus (tree_column->button, TRUE);
1046     }
1047   else
1048     {
1049       gtk_widget_set_can_focus (tree_column->button, FALSE);
1050       if (gtk_widget_has_focus (tree_column->button))
1051         {
1052           GtkWidget *toplevel = gtk_widget_get_toplevel (tree_column->tree_view);
1053           if (gtk_widget_is_toplevel (toplevel))
1054             {
1055               gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1056             }
1057         }
1058     }
1059   /* Queue a resize on the assumption that we always want to catch all changes
1060    * and columns don't change all that often.
1061    */
1062   if (gtk_widget_get_realized (tree_column->tree_view))
1063      gtk_widget_queue_resize (tree_column->tree_view);
1064
1065 }
1066
1067 /* Button signal handlers
1068  */
1069
1070 static gint
1071 gtk_tree_view_column_button_event (GtkWidget *widget,
1072                                    GdkEvent  *event,
1073                                    gpointer   data)
1074 {
1075   GtkTreeViewColumn *column = (GtkTreeViewColumn *) data;
1076
1077   g_return_val_if_fail (event != NULL, FALSE);
1078
1079   if (event->type == GDK_BUTTON_PRESS &&
1080       column->reorderable &&
1081       ((GdkEventButton *)event)->button == 1)
1082     {
1083       column->maybe_reordered = TRUE;
1084       gdk_window_get_pointer (GTK_BUTTON (widget)->event_window,
1085                               &column->drag_x,
1086                               &column->drag_y,
1087                               NULL);
1088       gtk_widget_grab_focus (widget);
1089     }
1090
1091   if (event->type == GDK_BUTTON_RELEASE ||
1092       event->type == GDK_LEAVE_NOTIFY)
1093     column->maybe_reordered = FALSE;
1094   
1095   if (event->type == GDK_MOTION_NOTIFY &&
1096       column->maybe_reordered &&
1097       (gtk_drag_check_threshold (widget,
1098                                  column->drag_x,
1099                                  column->drag_y,
1100                                  (gint) ((GdkEventMotion *)event)->x,
1101                                  (gint) ((GdkEventMotion *)event)->y)))
1102     {
1103       column->maybe_reordered = FALSE;
1104       _gtk_tree_view_column_start_drag (GTK_TREE_VIEW (column->tree_view), column,
1105                                         event->motion.device);
1106       return TRUE;
1107     }
1108   if (column->clickable == FALSE)
1109     {
1110       switch (event->type)
1111         {
1112         case GDK_BUTTON_PRESS:
1113         case GDK_2BUTTON_PRESS:
1114         case GDK_3BUTTON_PRESS:
1115         case GDK_MOTION_NOTIFY:
1116         case GDK_BUTTON_RELEASE:
1117         case GDK_ENTER_NOTIFY:
1118         case GDK_LEAVE_NOTIFY:
1119           return TRUE;
1120         default:
1121           return FALSE;
1122         }
1123     }
1124   return FALSE;
1125 }
1126
1127
1128 static void
1129 gtk_tree_view_column_button_clicked (GtkWidget *widget, gpointer data)
1130 {
1131   g_signal_emit_by_name (data, "clicked");
1132 }
1133
1134 static gboolean
1135 gtk_tree_view_column_mnemonic_activate (GtkWidget *widget,
1136                                         gboolean   group_cycling,
1137                                         gpointer   data)
1138 {
1139   GtkTreeViewColumn *column = (GtkTreeViewColumn *)data;
1140
1141   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (column), FALSE);
1142
1143   GTK_TREE_VIEW (column->tree_view)->priv->focus_column = column;
1144   if (column->clickable)
1145     gtk_button_clicked (GTK_BUTTON (column->button));
1146   else if (gtk_widget_get_can_focus (column->button))
1147     gtk_widget_grab_focus (column->button);
1148   else
1149     gtk_widget_grab_focus (column->tree_view);
1150
1151   return TRUE;
1152 }
1153
1154 static void
1155 gtk_tree_view_model_sort_column_changed (GtkTreeSortable   *sortable,
1156                                          GtkTreeViewColumn *column)
1157 {
1158   gint sort_column_id;
1159   GtkSortType order;
1160
1161   if (gtk_tree_sortable_get_sort_column_id (sortable,
1162                                             &sort_column_id,
1163                                             &order))
1164     {
1165       if (sort_column_id == column->sort_column_id)
1166         {
1167           gtk_tree_view_column_set_sort_indicator (column, TRUE);
1168           gtk_tree_view_column_set_sort_order (column, order);
1169         }
1170       else
1171         {
1172           gtk_tree_view_column_set_sort_indicator (column, FALSE);
1173         }
1174     }
1175   else
1176     {
1177       gtk_tree_view_column_set_sort_indicator (column, FALSE);
1178     }
1179 }
1180
1181 static void
1182 gtk_tree_view_column_sort (GtkTreeViewColumn *tree_column,
1183                            gpointer           data)
1184 {
1185   gint sort_column_id;
1186   GtkSortType order;
1187   gboolean has_sort_column;
1188   gboolean has_default_sort_func;
1189
1190   g_return_if_fail (tree_column->tree_view != NULL);
1191
1192   has_sort_column =
1193     gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
1194                                           &sort_column_id,
1195                                           &order);
1196   has_default_sort_func =
1197     gtk_tree_sortable_has_default_sort_func (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model));
1198
1199   if (has_sort_column &&
1200       sort_column_id == tree_column->sort_column_id)
1201     {
1202       if (order == GTK_SORT_ASCENDING)
1203         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
1204                                               tree_column->sort_column_id,
1205                                               GTK_SORT_DESCENDING);
1206       else if (order == GTK_SORT_DESCENDING && has_default_sort_func)
1207         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
1208                                               GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
1209                                               GTK_SORT_ASCENDING);
1210       else
1211         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
1212                                               tree_column->sort_column_id,
1213                                               GTK_SORT_ASCENDING);
1214     }
1215   else
1216     {
1217       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GTK_TREE_VIEW (tree_column->tree_view)->priv->model),
1218                                             tree_column->sort_column_id,
1219                                             GTK_SORT_ASCENDING);
1220     }
1221 }
1222
1223
1224 static void
1225 gtk_tree_view_column_setup_sort_column_id_callback (GtkTreeViewColumn *tree_column)
1226 {
1227   GtkTreeModel *model;
1228
1229   if (tree_column->tree_view == NULL)
1230     return;
1231
1232   model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_column->tree_view));
1233
1234   if (model == NULL)
1235     return;
1236
1237   if (GTK_IS_TREE_SORTABLE (model) &&
1238       tree_column->sort_column_id != -1)
1239     {
1240       gint real_sort_column_id;
1241       GtkSortType real_order;
1242
1243       if (tree_column->sort_column_changed_signal == 0)
1244         tree_column->sort_column_changed_signal =
1245           g_signal_connect (model, "sort-column-changed",
1246                             G_CALLBACK (gtk_tree_view_model_sort_column_changed),
1247                             tree_column);
1248       
1249       if (gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (model),
1250                                                 &real_sort_column_id,
1251                                                 &real_order) &&
1252           (real_sort_column_id == tree_column->sort_column_id))
1253         {
1254           gtk_tree_view_column_set_sort_indicator (tree_column, TRUE);
1255           gtk_tree_view_column_set_sort_order (tree_column, real_order);
1256         }
1257       else 
1258         {
1259           gtk_tree_view_column_set_sort_indicator (tree_column, FALSE);
1260         }
1261    }
1262 }
1263
1264
1265 /* Exported Private Functions.
1266  * These should only be called by gtktreeview.c or gtktreeviewcolumn.c
1267  */
1268
1269 void
1270 _gtk_tree_view_column_realize_button (GtkTreeViewColumn *column)
1271 {
1272   GtkAllocation allocation;
1273   GtkTreeView *tree_view;
1274   GdkWindowAttr attr;
1275   guint attributes_mask;
1276   gboolean rtl;
1277
1278   tree_view = (GtkTreeView *)column->tree_view;
1279   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1280
1281   g_return_if_fail (GTK_IS_TREE_VIEW (tree_view));
1282   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
1283   g_return_if_fail (tree_view->priv->header_window != NULL);
1284   g_return_if_fail (column->button != NULL);
1285
1286   gtk_widget_set_parent_window (column->button, tree_view->priv->header_window);
1287
1288   if (column->visible)
1289     gtk_widget_show (column->button);
1290
1291   attr.window_type = GDK_WINDOW_CHILD;
1292   attr.wclass = GDK_INPUT_ONLY;
1293   attr.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
1294   attr.event_mask = gtk_widget_get_events (GTK_WIDGET (tree_view)) |
1295                     (GDK_BUTTON_PRESS_MASK |
1296                      GDK_BUTTON_RELEASE_MASK |
1297                      GDK_POINTER_MOTION_MASK |
1298                      GDK_POINTER_MOTION_HINT_MASK |
1299                      GDK_KEY_PRESS_MASK);
1300   attributes_mask = GDK_WA_CURSOR | GDK_WA_X | GDK_WA_Y;
1301   attr.cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (tree_view->priv->header_window),
1302                                             GDK_SB_H_DOUBLE_ARROW);
1303   attr.y = 0;
1304   attr.width = TREE_VIEW_DRAG_WIDTH;
1305   attr.height = tree_view->priv->header_height;
1306
1307   gtk_widget_get_allocation (column->button, &allocation);
1308   attr.x = (allocation.x + (rtl ? 0 : allocation.width)) - TREE_VIEW_DRAG_WIDTH / 2;
1309   column->window = gdk_window_new (tree_view->priv->header_window,
1310                                    &attr, attributes_mask);
1311   gdk_window_set_user_data (column->window, tree_view);
1312
1313   gtk_tree_view_column_update_button (column);
1314
1315   gdk_cursor_unref (attr.cursor);
1316 }
1317
1318 void
1319 _gtk_tree_view_column_unrealize_button (GtkTreeViewColumn *column)
1320 {
1321   g_return_if_fail (column != NULL);
1322   g_return_if_fail (column->window != NULL);
1323
1324   gdk_window_set_user_data (column->window, NULL);
1325   gdk_window_destroy (column->window);
1326   column->window = NULL;
1327 }
1328
1329 void
1330 _gtk_tree_view_column_unset_model (GtkTreeViewColumn *column,
1331                                    GtkTreeModel      *old_model)
1332 {
1333   if (column->sort_column_changed_signal)
1334     {
1335       g_signal_handler_disconnect (old_model,
1336                                    column->sort_column_changed_signal);
1337       column->sort_column_changed_signal = 0;
1338     }
1339   gtk_tree_view_column_set_sort_indicator (column, FALSE);
1340 }
1341
1342 void
1343 _gtk_tree_view_column_set_tree_view (GtkTreeViewColumn *column,
1344                                      GtkTreeView       *tree_view)
1345 {
1346   g_assert (column->tree_view == NULL);
1347
1348   column->tree_view = GTK_WIDGET (tree_view);
1349   gtk_tree_view_column_create_button (column);
1350
1351   column->property_changed_signal =
1352           g_signal_connect_swapped (tree_view,
1353                                     "notify::model",
1354                                     G_CALLBACK (gtk_tree_view_column_setup_sort_column_id_callback),
1355                                     column);
1356
1357   gtk_tree_view_column_setup_sort_column_id_callback (column);
1358 }
1359
1360 void
1361 _gtk_tree_view_column_unset_tree_view (GtkTreeViewColumn *column)
1362 {
1363   if (column->tree_view && column->button)
1364     {
1365       gtk_container_remove (GTK_CONTAINER (column->tree_view), column->button);
1366     }
1367   if (column->property_changed_signal)
1368     {
1369       g_signal_handler_disconnect (column->tree_view, column->property_changed_signal);
1370       column->property_changed_signal = 0;
1371     }
1372
1373   if (column->sort_column_changed_signal)
1374     {
1375       g_signal_handler_disconnect (gtk_tree_view_get_model (GTK_TREE_VIEW (column->tree_view)),
1376                                    column->sort_column_changed_signal);
1377       column->sort_column_changed_signal = 0;
1378     }
1379
1380   column->tree_view = NULL;
1381   column->button = NULL;
1382 }
1383
1384 gboolean
1385 _gtk_tree_view_column_has_editable_cell (GtkTreeViewColumn *column)
1386 {
1387   GtkCellRenderer *cell;
1388   GtkCellRendererMode mode;
1389   GList *list;
1390
1391   for (list = column->cell_list; list; list = list->next)
1392     {
1393       cell = ((GtkTreeViewColumnCellInfo *)list->data)->cell;
1394       g_object_get (cell, "mode", &mode, NULL);
1395       if (mode == GTK_CELL_RENDERER_MODE_EDITABLE)
1396         return TRUE;
1397     }
1398
1399   return FALSE;
1400 }
1401
1402 /* gets cell being edited */
1403 GtkCellRenderer *
1404 _gtk_tree_view_column_get_edited_cell (GtkTreeViewColumn *column)
1405 {
1406   GList *list;
1407
1408   for (list = column->cell_list; list; list = list->next)
1409     if (((GtkTreeViewColumnCellInfo *)list->data)->in_editing_mode)
1410       return ((GtkTreeViewColumnCellInfo *)list->data)->cell;
1411
1412   return NULL;
1413 }
1414
1415 gint
1416 _gtk_tree_view_column_count_special_cells (GtkTreeViewColumn *column)
1417 {
1418   gint i = 0;
1419   GList *list;
1420
1421   for (list = column->cell_list; list; list = list->next)
1422     {
1423       GtkCellRendererMode mode;
1424       GtkTreeViewColumnCellInfo *cellinfo = list->data;
1425
1426       g_object_get (cellinfo->cell, "mode", &mode, NULL);
1427       if ((mode == GTK_CELL_RENDERER_MODE_EDITABLE ||
1428            mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE) &&
1429           gtk_cell_renderer_get_visible (cellinfo->cell))
1430         i++;
1431     }
1432
1433   return i;
1434 }
1435
1436 GtkCellRenderer *
1437 _gtk_tree_view_column_get_cell_at_pos (GtkTreeViewColumn *column,
1438                                        gint               x)
1439 {
1440   GList *list;
1441   gint current_x = 0;
1442
1443   list = gtk_tree_view_column_cell_first (column);
1444   for (; list; list = gtk_tree_view_column_cell_next (column, list))
1445    {
1446      GtkTreeViewColumnCellInfo *cellinfo = list->data;
1447      if (current_x <= x && x <= current_x + cellinfo->real_width)
1448        return cellinfo->cell;
1449      current_x += cellinfo->real_width;
1450    }
1451
1452   return NULL;
1453 }
1454
1455 /* Public Functions */
1456
1457
1458 /**
1459  * gtk_tree_view_column_new:
1460  * 
1461  * Creates a new #GtkTreeViewColumn.
1462  * 
1463  * Return value: A newly created #GtkTreeViewColumn.
1464  **/
1465 GtkTreeViewColumn *
1466 gtk_tree_view_column_new (void)
1467 {
1468   GtkTreeViewColumn *tree_column;
1469
1470   tree_column = g_object_new (GTK_TYPE_TREE_VIEW_COLUMN, NULL);
1471
1472   return tree_column;
1473 }
1474
1475 /**
1476  * gtk_tree_view_column_new_with_attributes:
1477  * @title: The title to set the header to.
1478  * @cell: The #GtkCellRenderer.
1479  * @Varargs: A %NULL-terminated list of attributes.
1480  * 
1481  * Creates a new #GtkTreeViewColumn with a number of default values.  This is
1482  * equivalent to calling gtk_tree_view_column_set_title(),
1483  * gtk_tree_view_column_pack_start(), and
1484  * gtk_tree_view_column_set_attributes() on the newly created #GtkTreeViewColumn.
1485  *
1486  * Here's a simple example:
1487  * |[
1488  *  enum { TEXT_COLUMN, COLOR_COLUMN, N_COLUMNS };
1489  *  ...
1490  *  {
1491  *    GtkTreeViewColumn *column;
1492  *    GtkCellRenderer   *renderer = gtk_cell_renderer_text_new ();
1493  *  
1494  *    column = gtk_tree_view_column_new_with_attributes ("Title",
1495  *                                                       renderer,
1496  *                                                       "text", TEXT_COLUMN,
1497  *                                                       "foreground", COLOR_COLUMN,
1498  *                                                       NULL);
1499  *  }
1500  * ]|
1501  * 
1502  * Return value: A newly created #GtkTreeViewColumn.
1503  **/
1504 GtkTreeViewColumn *
1505 gtk_tree_view_column_new_with_attributes (const gchar     *title,
1506                                           GtkCellRenderer *cell,
1507                                           ...)
1508 {
1509   GtkTreeViewColumn *retval;
1510   va_list args;
1511
1512   retval = gtk_tree_view_column_new ();
1513
1514   gtk_tree_view_column_set_title (retval, title);
1515   gtk_tree_view_column_pack_start (retval, cell, TRUE);
1516
1517   va_start (args, cell);
1518   gtk_tree_view_column_set_attributesv (retval, cell, args);
1519   va_end (args);
1520
1521   return retval;
1522 }
1523
1524 static GtkTreeViewColumnCellInfo *
1525 gtk_tree_view_column_get_cell_info (GtkTreeViewColumn *tree_column,
1526                                     GtkCellRenderer   *cell_renderer)
1527 {
1528   GList *list;
1529   for (list = tree_column->cell_list; list; list = list->next)
1530     if (((GtkTreeViewColumnCellInfo *)list->data)->cell == cell_renderer)
1531       return (GtkTreeViewColumnCellInfo *) list->data;
1532   return NULL;
1533 }
1534
1535
1536 /**
1537  * gtk_tree_view_column_pack_start:
1538  * @tree_column: A #GtkTreeViewColumn.
1539  * @cell: The #GtkCellRenderer. 
1540  * @expand: %TRUE if @cell is to be given extra space allocated to @tree_column.
1541  *
1542  * Packs the @cell into the beginning of the column. If @expand is %FALSE, then
1543  * the @cell is allocated no more space than it needs. Any unused space is divided
1544  * evenly between cells for which @expand is %TRUE.
1545  **/
1546 void
1547 gtk_tree_view_column_pack_start (GtkTreeViewColumn *tree_column,
1548                                  GtkCellRenderer   *cell,
1549                                  gboolean           expand)
1550 {
1551   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (tree_column), cell, expand);
1552 }
1553
1554 /**
1555  * gtk_tree_view_column_pack_end:
1556  * @tree_column: A #GtkTreeViewColumn.
1557  * @cell: The #GtkCellRenderer. 
1558  * @expand: %TRUE if @cell is to be given extra space allocated to @tree_column.
1559  *
1560  * Adds the @cell to end of the column. If @expand is %FALSE, then the @cell
1561  * is allocated no more space than it needs. Any unused space is divided
1562  * evenly between cells for which @expand is %TRUE.
1563  **/
1564 void
1565 gtk_tree_view_column_pack_end (GtkTreeViewColumn  *tree_column,
1566                                GtkCellRenderer    *cell,
1567                                gboolean            expand)
1568 {
1569   gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (tree_column), cell, expand);
1570 }
1571
1572 /**
1573  * gtk_tree_view_column_clear:
1574  * @tree_column: A #GtkTreeViewColumn
1575  * 
1576  * Unsets all the mappings on all renderers on the @tree_column.
1577  **/
1578 void
1579 gtk_tree_view_column_clear (GtkTreeViewColumn *tree_column)
1580 {
1581   gtk_cell_layout_clear (GTK_CELL_LAYOUT (tree_column));
1582 }
1583
1584 static GList *
1585 gtk_tree_view_column_cell_layout_get_cells (GtkCellLayout *layout)
1586 {
1587   GtkTreeViewColumn *tree_column = GTK_TREE_VIEW_COLUMN (layout);
1588   GList *retval = NULL, *list;
1589
1590   g_return_val_if_fail (tree_column != NULL, NULL);
1591
1592   for (list = tree_column->cell_list; list; list = list->next)
1593     {
1594       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *)list->data;
1595
1596       retval = g_list_append (retval, info->cell);
1597     }
1598
1599   return retval;
1600 }
1601
1602 /**
1603  * gtk_tree_view_column_add_attribute:
1604  * @tree_column: A #GtkTreeViewColumn.
1605  * @cell_renderer: the #GtkCellRenderer to set attributes on
1606  * @attribute: An attribute on the renderer
1607  * @column: The column position on the model to get the attribute from.
1608  * 
1609  * Adds an attribute mapping to the list in @tree_column.  The @column is the
1610  * column of the model to get a value from, and the @attribute is the
1611  * parameter on @cell_renderer to be set from the value. So for example
1612  * if column 2 of the model contains strings, you could have the
1613  * "text" attribute of a #GtkCellRendererText get its values from
1614  * column 2.
1615  **/
1616 void
1617 gtk_tree_view_column_add_attribute (GtkTreeViewColumn *tree_column,
1618                                     GtkCellRenderer   *cell_renderer,
1619                                     const gchar       *attribute,
1620                                     gint               column)
1621 {
1622   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (tree_column),
1623                                  cell_renderer, attribute, column);
1624 }
1625
1626 static void
1627 gtk_tree_view_column_set_attributesv (GtkTreeViewColumn *tree_column,
1628                                       GtkCellRenderer   *cell_renderer,
1629                                       va_list            args)
1630 {
1631   gchar *attribute;
1632   gint column;
1633
1634   attribute = va_arg (args, gchar *);
1635
1636   gtk_tree_view_column_clear_attributes (tree_column, cell_renderer);
1637   
1638   while (attribute != NULL)
1639     {
1640       column = va_arg (args, gint);
1641       gtk_tree_view_column_add_attribute (tree_column, cell_renderer, attribute, column);
1642       attribute = va_arg (args, gchar *);
1643     }
1644 }
1645
1646 /**
1647  * gtk_tree_view_column_set_attributes:
1648  * @tree_column: A #GtkTreeViewColumn.
1649  * @cell_renderer: the #GtkCellRenderer we're setting the attributes of
1650  * @Varargs: A %NULL-terminated list of attributes.
1651  * 
1652  * Sets the attributes in the list as the attributes of @tree_column.
1653  * The attributes should be in attribute/column order, as in
1654  * gtk_tree_view_column_add_attribute(). All existing attributes
1655  * are removed, and replaced with the new attributes.
1656  **/
1657 void
1658 gtk_tree_view_column_set_attributes (GtkTreeViewColumn *tree_column,
1659                                      GtkCellRenderer   *cell_renderer,
1660                                      ...)
1661 {
1662   va_list args;
1663
1664   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1665   g_return_if_fail (GTK_IS_CELL_RENDERER (cell_renderer));
1666   g_return_if_fail (gtk_tree_view_column_get_cell_info (tree_column, cell_renderer));
1667
1668   va_start (args, cell_renderer);
1669   gtk_tree_view_column_set_attributesv (tree_column, cell_renderer, args);
1670   va_end (args);
1671 }
1672
1673
1674 /**
1675  * gtk_tree_view_column_set_cell_data_func:
1676  * @tree_column: A #GtkTreeViewColumn
1677  * @cell_renderer: A #GtkCellRenderer
1678  * @func: The #GtkTreeViewColumnFunc to use. 
1679  * @func_data: The user data for @func.
1680  * @destroy: The destroy notification for @func_data
1681  * 
1682  * Sets the #GtkTreeViewColumnFunc to use for the column.  This
1683  * function is used instead of the standard attributes mapping for
1684  * setting the column value, and should set the value of @tree_column's
1685  * cell renderer as appropriate.  @func may be %NULL to remove an
1686  * older one.
1687  **/
1688 void
1689 gtk_tree_view_column_set_cell_data_func (GtkTreeViewColumn   *tree_column,
1690                                          GtkCellRenderer     *cell_renderer,
1691                                          GtkTreeCellDataFunc  func,
1692                                          gpointer             func_data,
1693                                          GDestroyNotify       destroy)
1694 {
1695   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (tree_column),
1696                                       cell_renderer,
1697                                       (GtkCellLayoutDataFunc)func,
1698                                       func_data, destroy);
1699 }
1700
1701
1702 /**
1703  * gtk_tree_view_column_clear_attributes:
1704  * @tree_column: a #GtkTreeViewColumn
1705  * @cell_renderer: a #GtkCellRenderer to clear the attribute mapping on.
1706  * 
1707  * Clears all existing attributes previously set with
1708  * gtk_tree_view_column_set_attributes().
1709  **/
1710 void
1711 gtk_tree_view_column_clear_attributes (GtkTreeViewColumn *tree_column,
1712                                        GtkCellRenderer   *cell_renderer)
1713 {
1714   gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (tree_column),
1715                                     cell_renderer);
1716 }
1717
1718 /**
1719  * gtk_tree_view_column_set_spacing:
1720  * @tree_column: A #GtkTreeViewColumn.
1721  * @spacing: distance between cell renderers in pixels.
1722  * 
1723  * Sets the spacing field of @tree_column, which is the number of pixels to
1724  * place between cell renderers packed into it.
1725  **/
1726 void
1727 gtk_tree_view_column_set_spacing (GtkTreeViewColumn *tree_column,
1728                                   gint               spacing)
1729 {
1730   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1731   g_return_if_fail (spacing >= 0);
1732
1733   if (tree_column->spacing == spacing)
1734     return;
1735
1736   tree_column->spacing = spacing;
1737   if (tree_column->tree_view)
1738     _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
1739 }
1740
1741 /**
1742  * gtk_tree_view_column_get_spacing:
1743  * @tree_column: A #GtkTreeViewColumn.
1744  * 
1745  * Returns the spacing of @tree_column.
1746  * 
1747  * Return value: the spacing of @tree_column.
1748  **/
1749 gint
1750 gtk_tree_view_column_get_spacing (GtkTreeViewColumn *tree_column)
1751 {
1752   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
1753
1754   return tree_column->spacing;
1755 }
1756
1757 /* Options for manipulating the columns */
1758
1759 /**
1760  * gtk_tree_view_column_set_visible:
1761  * @tree_column: A #GtkTreeViewColumn.
1762  * @visible: %TRUE if the @tree_column is visible.
1763  * 
1764  * Sets the visibility of @tree_column.
1765  **/
1766 void
1767 gtk_tree_view_column_set_visible (GtkTreeViewColumn *tree_column,
1768                                   gboolean           visible)
1769 {
1770   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1771
1772   visible = !! visible;
1773   
1774   if (tree_column->visible == visible)
1775     return;
1776
1777   tree_column->visible = visible;
1778
1779   if (tree_column->visible)
1780     _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
1781
1782   gtk_tree_view_column_update_button (tree_column);
1783   g_object_notify (G_OBJECT (tree_column), "visible");
1784 }
1785
1786 /**
1787  * gtk_tree_view_column_get_visible:
1788  * @tree_column: A #GtkTreeViewColumn.
1789  * 
1790  * Returns %TRUE if @tree_column is visible.
1791  * 
1792  * Return value: whether the column is visible or not.  If it is visible, then
1793  * the tree will show the column.
1794  **/
1795 gboolean
1796 gtk_tree_view_column_get_visible (GtkTreeViewColumn *tree_column)
1797 {
1798   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
1799
1800   return tree_column->visible;
1801 }
1802
1803 /**
1804  * gtk_tree_view_column_set_resizable:
1805  * @tree_column: A #GtkTreeViewColumn
1806  * @resizable: %TRUE, if the column can be resized
1807  * 
1808  * If @resizable is %TRUE, then the user can explicitly resize the column by
1809  * grabbing the outer edge of the column button.  If resizable is %TRUE and
1810  * sizing mode of the column is #GTK_TREE_VIEW_COLUMN_AUTOSIZE, then the sizing
1811  * mode is changed to #GTK_TREE_VIEW_COLUMN_GROW_ONLY.
1812  **/
1813 void
1814 gtk_tree_view_column_set_resizable (GtkTreeViewColumn *tree_column,
1815                                     gboolean           resizable)
1816 {
1817   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1818
1819   resizable = !! resizable;
1820
1821   if (tree_column->resizable == resizable)
1822     return;
1823
1824   tree_column->resizable = resizable;
1825
1826   if (resizable && tree_column->column_type == GTK_TREE_VIEW_COLUMN_AUTOSIZE)
1827     gtk_tree_view_column_set_sizing (tree_column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
1828
1829   gtk_tree_view_column_update_button (tree_column);
1830
1831   g_object_notify (G_OBJECT (tree_column), "resizable");
1832 }
1833
1834 /**
1835  * gtk_tree_view_column_get_resizable:
1836  * @tree_column: A #GtkTreeViewColumn
1837  * 
1838  * Returns %TRUE if the @tree_column can be resized by the end user.
1839  * 
1840  * Return value: %TRUE, if the @tree_column can be resized.
1841  **/
1842 gboolean
1843 gtk_tree_view_column_get_resizable (GtkTreeViewColumn *tree_column)
1844 {
1845   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
1846
1847   return tree_column->resizable;
1848 }
1849
1850
1851 /**
1852  * gtk_tree_view_column_set_sizing:
1853  * @tree_column: A #GtkTreeViewColumn.
1854  * @type: The #GtkTreeViewColumnSizing.
1855  * 
1856  * Sets the growth behavior of @tree_column to @type.
1857  **/
1858 void
1859 gtk_tree_view_column_set_sizing (GtkTreeViewColumn       *tree_column,
1860                                  GtkTreeViewColumnSizing  type)
1861 {
1862   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1863
1864   if (type == tree_column->column_type)
1865     return;
1866
1867   if (type == GTK_TREE_VIEW_COLUMN_AUTOSIZE)
1868     gtk_tree_view_column_set_resizable (tree_column, FALSE);
1869
1870 #if 0
1871   /* I was clearly on crack when I wrote this.  I'm not sure what's supposed to
1872    * be below so I'll leave it until I figure it out.
1873    */
1874   if (tree_column->column_type == GTK_TREE_VIEW_COLUMN_AUTOSIZE &&
1875       tree_column->requested_width != -1)
1876     {
1877       gtk_tree_view_column_set_sizing (tree_column, tree_column->requested_width);
1878     }
1879 #endif
1880   tree_column->column_type = type;
1881
1882   gtk_tree_view_column_update_button (tree_column);
1883
1884   g_object_notify (G_OBJECT (tree_column), "sizing");
1885 }
1886
1887 /**
1888  * gtk_tree_view_column_get_sizing:
1889  * @tree_column: A #GtkTreeViewColumn.
1890  * 
1891  * Returns the current type of @tree_column.
1892  * 
1893  * Return value: The type of @tree_column.
1894  **/
1895 GtkTreeViewColumnSizing
1896 gtk_tree_view_column_get_sizing (GtkTreeViewColumn *tree_column)
1897 {
1898   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
1899
1900   return tree_column->column_type;
1901 }
1902
1903 /**
1904  * gtk_tree_view_column_get_width:
1905  * @tree_column: A #GtkTreeViewColumn.
1906  * 
1907  * Returns the current size of @tree_column in pixels.
1908  * 
1909  * Return value: The current width of @tree_column.
1910  **/
1911 gint
1912 gtk_tree_view_column_get_width (GtkTreeViewColumn *tree_column)
1913 {
1914   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
1915
1916   return tree_column->width;
1917 }
1918
1919 /**
1920  * gtk_tree_view_column_set_fixed_width:
1921  * @tree_column: A #GtkTreeViewColumn.
1922  * @fixed_width: The size to set @tree_column to. Must be greater than 0.
1923  * 
1924  * Sets the size of the column in pixels.  This is meaningful only if the sizing
1925  * type is #GTK_TREE_VIEW_COLUMN_FIXED.  The size of the column is clamped to
1926  * the min/max width for the column.  Please note that the min/max width of the
1927  * column doesn't actually affect the "fixed_width" property of the widget, just
1928  * the actual size when displayed.
1929  **/
1930 void
1931 gtk_tree_view_column_set_fixed_width (GtkTreeViewColumn *tree_column,
1932                                       gint               fixed_width)
1933 {
1934   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1935   g_return_if_fail (fixed_width > 0);
1936
1937   tree_column->fixed_width = fixed_width;
1938   tree_column->use_resized_width = FALSE;
1939
1940   if (tree_column->tree_view &&
1941       gtk_widget_get_realized (tree_column->tree_view) &&
1942       tree_column->column_type == GTK_TREE_VIEW_COLUMN_FIXED)
1943     {
1944       gtk_widget_queue_resize (tree_column->tree_view);
1945     }
1946
1947   g_object_notify (G_OBJECT (tree_column), "fixed-width");
1948 }
1949
1950 /**
1951  * gtk_tree_view_column_get_fixed_width:
1952  * @tree_column: a #GtkTreeViewColumn
1953  * 
1954  * Gets the fixed width of the column.  This value is only meaning may not be
1955  * the actual width of the column on the screen, just what is requested.
1956  * 
1957  * Return value: the fixed width of the column
1958  **/
1959 gint
1960 gtk_tree_view_column_get_fixed_width (GtkTreeViewColumn *tree_column)
1961 {
1962   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
1963
1964   return tree_column->fixed_width;
1965 }
1966
1967 /**
1968  * gtk_tree_view_column_set_min_width:
1969  * @tree_column: A #GtkTreeViewColumn.
1970  * @min_width: The minimum width of the column in pixels, or -1.
1971  * 
1972  * Sets the minimum width of the @tree_column.  If @min_width is -1, then the
1973  * minimum width is unset.
1974  **/
1975 void
1976 gtk_tree_view_column_set_min_width (GtkTreeViewColumn *tree_column,
1977                                     gint               min_width)
1978 {
1979   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
1980   g_return_if_fail (min_width >= -1);
1981
1982   if (min_width == tree_column->min_width)
1983     return;
1984
1985   if (tree_column->visible &&
1986       tree_column->tree_view != NULL &&
1987       gtk_widget_get_realized (tree_column->tree_view))
1988     {
1989       if (min_width > tree_column->width)
1990         gtk_widget_queue_resize (tree_column->tree_view);
1991     }
1992
1993   tree_column->min_width = min_width;
1994   g_object_freeze_notify (G_OBJECT (tree_column));
1995   if (tree_column->max_width != -1 && tree_column->max_width < min_width)
1996     {
1997       tree_column->max_width = min_width;
1998       g_object_notify (G_OBJECT (tree_column), "max-width");
1999     }
2000   g_object_notify (G_OBJECT (tree_column), "min-width");
2001   g_object_thaw_notify (G_OBJECT (tree_column));
2002
2003   if (tree_column->column_type == GTK_TREE_VIEW_COLUMN_AUTOSIZE)
2004     _gtk_tree_view_column_autosize (GTK_TREE_VIEW (tree_column->tree_view),
2005                                     tree_column);
2006 }
2007
2008 /**
2009  * gtk_tree_view_column_get_min_width:
2010  * @tree_column: A #GtkTreeViewColumn.
2011  * 
2012  * Returns the minimum width in pixels of the @tree_column, or -1 if no minimum
2013  * width is set.
2014  * 
2015  * Return value: The minimum width of the @tree_column.
2016  **/
2017 gint
2018 gtk_tree_view_column_get_min_width (GtkTreeViewColumn *tree_column)
2019 {
2020   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);
2021
2022   return tree_column->min_width;
2023 }
2024
2025 /**
2026  * gtk_tree_view_column_set_max_width:
2027  * @tree_column: A #GtkTreeViewColumn.
2028  * @max_width: The maximum width of the column in pixels, or -1.
2029  * 
2030  * Sets the maximum width of the @tree_column.  If @max_width is -1, then the
2031  * maximum width is unset.  Note, the column can actually be wider than max
2032  * width if it's the last column in a view.  In this case, the column expands to
2033  * fill any extra space.
2034  **/
2035 void
2036 gtk_tree_view_column_set_max_width (GtkTreeViewColumn *tree_column,
2037                                     gint               max_width)
2038 {
2039   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2040   g_return_if_fail (max_width >= -1);
2041
2042   if (max_width == tree_column->max_width)
2043     return;
2044
2045   if (tree_column->visible &&
2046       tree_column->tree_view != NULL &&
2047       gtk_widget_get_realized (tree_column->tree_view))
2048     {
2049       if (max_width != -1 && max_width < tree_column->width)
2050         gtk_widget_queue_resize (tree_column->tree_view);
2051     }
2052
2053   tree_column->max_width = max_width;
2054   g_object_freeze_notify (G_OBJECT (tree_column));
2055   if (max_width != -1 && max_width < tree_column->min_width)
2056     {
2057       tree_column->min_width = max_width;
2058       g_object_notify (G_OBJECT (tree_column), "min-width");
2059     }
2060   g_object_notify (G_OBJECT (tree_column), "max-width");
2061   g_object_thaw_notify (G_OBJECT (tree_column));
2062
2063   if (tree_column->column_type == GTK_TREE_VIEW_COLUMN_AUTOSIZE)
2064     _gtk_tree_view_column_autosize (GTK_TREE_VIEW (tree_column->tree_view),
2065                                     tree_column);
2066 }
2067
2068 /**
2069  * gtk_tree_view_column_get_max_width:
2070  * @tree_column: A #GtkTreeViewColumn.
2071  * 
2072  * Returns the maximum width in pixels of the @tree_column, or -1 if no maximum
2073  * width is set.
2074  * 
2075  * Return value: The maximum width of the @tree_column.
2076  **/
2077 gint
2078 gtk_tree_view_column_get_max_width (GtkTreeViewColumn *tree_column)
2079 {
2080   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), -1);
2081
2082   return tree_column->max_width;
2083 }
2084
2085 /**
2086  * gtk_tree_view_column_clicked:
2087  * @tree_column: a #GtkTreeViewColumn
2088  * 
2089  * Emits the "clicked" signal on the column.  This function will only work if
2090  * @tree_column is clickable.
2091  **/
2092 void
2093 gtk_tree_view_column_clicked (GtkTreeViewColumn *tree_column)
2094 {
2095   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2096
2097   if (tree_column->visible &&
2098       tree_column->button &&
2099       tree_column->clickable)
2100     gtk_button_clicked (GTK_BUTTON (tree_column->button));
2101 }
2102
2103 /**
2104  * gtk_tree_view_column_set_title:
2105  * @tree_column: A #GtkTreeViewColumn.
2106  * @title: The title of the @tree_column.
2107  * 
2108  * Sets the title of the @tree_column.  If a custom widget has been set, then
2109  * this value is ignored.
2110  **/
2111 void
2112 gtk_tree_view_column_set_title (GtkTreeViewColumn *tree_column,
2113                                 const gchar       *title)
2114 {
2115   gchar *new_title;
2116   
2117   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2118
2119   new_title = g_strdup (title);
2120   g_free (tree_column->title);
2121   tree_column->title = new_title;
2122
2123   gtk_tree_view_column_update_button (tree_column);
2124   g_object_notify (G_OBJECT (tree_column), "title");
2125 }
2126
2127 /**
2128  * gtk_tree_view_column_get_title:
2129  * @tree_column: A #GtkTreeViewColumn.
2130  * 
2131  * Returns the title of the widget.
2132  * 
2133  * Return value: the title of the column. This string should not be
2134  * modified or freed.
2135  **/
2136 G_CONST_RETURN gchar *
2137 gtk_tree_view_column_get_title (GtkTreeViewColumn *tree_column)
2138 {
2139   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
2140
2141   return tree_column->title;
2142 }
2143
2144 /**
2145  * gtk_tree_view_column_set_expand:
2146  * @tree_column: A #GtkTreeViewColumn
2147  * @expand: %TRUE if the column should take available extra space, %FALSE if not
2148  * 
2149  * Sets the column to take available extra space.  This space is shared equally
2150  * amongst all columns that have the expand set to %TRUE.  If no column has this
2151  * option set, then the last column gets all extra space.  By default, every
2152  * column is created with this %FALSE.
2153  *
2154  * Since: 2.4
2155  **/
2156 void
2157 gtk_tree_view_column_set_expand (GtkTreeViewColumn *tree_column,
2158                                  gboolean           expand)
2159 {
2160   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2161
2162   expand = expand?TRUE:FALSE;
2163   if (tree_column->expand == expand)
2164     return;
2165   tree_column->expand = expand;
2166
2167   if (tree_column->visible &&
2168       tree_column->tree_view != NULL &&
2169       gtk_widget_get_realized (tree_column->tree_view))
2170     {
2171       /* We want to continue using the original width of the
2172        * column that includes additional space added by the user
2173        * resizing the columns and possibly extra (expanded) space, which
2174        * are not included in the resized width.
2175        */
2176       tree_column->use_resized_width = FALSE;
2177
2178       gtk_widget_queue_resize (tree_column->tree_view);
2179     }
2180
2181   g_object_notify (G_OBJECT (tree_column), "expand");
2182 }
2183
2184 /**
2185  * gtk_tree_view_column_get_expand:
2186  * @tree_column: a #GtkTreeViewColumn
2187  * 
2188  * Return %TRUE if the column expands to take any available space.
2189  * 
2190  * Return value: %TRUE, if the column expands
2191  *
2192  * Since: 2.4
2193  **/
2194 gboolean
2195 gtk_tree_view_column_get_expand (GtkTreeViewColumn *tree_column)
2196 {
2197   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
2198
2199   return tree_column->expand;
2200 }
2201
2202 /**
2203  * gtk_tree_view_column_set_clickable:
2204  * @tree_column: A #GtkTreeViewColumn.
2205  * @clickable: %TRUE if the header is active.
2206  * 
2207  * Sets the header to be active if @active is %TRUE.  When the header is active,
2208  * then it can take keyboard focus, and can be clicked.
2209  **/
2210 void
2211 gtk_tree_view_column_set_clickable (GtkTreeViewColumn *tree_column,
2212                                     gboolean           clickable)
2213 {
2214   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2215
2216   clickable = !! clickable;
2217   if (tree_column->clickable == clickable)
2218     return;
2219
2220   tree_column->clickable = clickable;
2221   gtk_tree_view_column_update_button (tree_column);
2222   g_object_notify (G_OBJECT (tree_column), "clickable");
2223 }
2224
2225 /**
2226  * gtk_tree_view_column_get_clickable:
2227  * @tree_column: a #GtkTreeViewColumn
2228  * 
2229  * Returns %TRUE if the user can click on the header for the column.
2230  * 
2231  * Return value: %TRUE if user can click the column header.
2232  **/
2233 gboolean
2234 gtk_tree_view_column_get_clickable (GtkTreeViewColumn *tree_column)
2235 {
2236   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
2237
2238   return tree_column->clickable;
2239 }
2240
2241 /**
2242  * gtk_tree_view_column_set_widget:
2243  * @tree_column: A #GtkTreeViewColumn.
2244  * @widget: (allow-none): A child #GtkWidget, or %NULL.
2245  *
2246  * Sets the widget in the header to be @widget.  If widget is %NULL, then the
2247  * header button is set with a #GtkLabel set to the title of @tree_column.
2248  **/
2249 void
2250 gtk_tree_view_column_set_widget (GtkTreeViewColumn *tree_column,
2251                                  GtkWidget         *widget)
2252 {
2253   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2254   g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
2255
2256   if (widget)
2257     g_object_ref_sink (widget);
2258
2259   if (tree_column->child)      
2260     g_object_unref (tree_column->child);
2261
2262   tree_column->child = widget;
2263   gtk_tree_view_column_update_button (tree_column);
2264   g_object_notify (G_OBJECT (tree_column), "widget");
2265 }
2266
2267 /**
2268  * gtk_tree_view_column_get_widget:
2269  * @tree_column: A #GtkTreeViewColumn.
2270  *
2271  * Returns the #GtkWidget in the button on the column header.
2272  * If a custom widget has not been set then %NULL is returned.
2273  *
2274  * Return value: (transfer none): The #GtkWidget in the column
2275  *     header, or %NULL
2276  **/
2277 GtkWidget *
2278 gtk_tree_view_column_get_widget (GtkTreeViewColumn *tree_column)
2279 {
2280   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
2281
2282   return tree_column->child;
2283 }
2284
2285 /**
2286  * gtk_tree_view_column_set_alignment:
2287  * @tree_column: A #GtkTreeViewColumn.
2288  * @xalign: The alignment, which is between [0.0 and 1.0] inclusive.
2289  * 
2290  * Sets the alignment of the title or custom widget inside the column header.
2291  * The alignment determines its location inside the button -- 0.0 for left, 0.5
2292  * for center, 1.0 for right.
2293  **/
2294 void
2295 gtk_tree_view_column_set_alignment (GtkTreeViewColumn *tree_column,
2296                                     gfloat             xalign)
2297 {
2298   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2299
2300   xalign = CLAMP (xalign, 0.0, 1.0);
2301
2302   if (tree_column->xalign == xalign)
2303     return;
2304
2305   tree_column->xalign = xalign;
2306   gtk_tree_view_column_update_button (tree_column);
2307   g_object_notify (G_OBJECT (tree_column), "alignment");
2308 }
2309
2310 /**
2311  * gtk_tree_view_column_get_alignment:
2312  * @tree_column: A #GtkTreeViewColumn.
2313  * 
2314  * Returns the current x alignment of @tree_column.  This value can range
2315  * between 0.0 and 1.0.
2316  * 
2317  * Return value: The current alignent of @tree_column.
2318  **/
2319 gfloat
2320 gtk_tree_view_column_get_alignment (GtkTreeViewColumn *tree_column)
2321 {
2322   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0.5);
2323
2324   return tree_column->xalign;
2325 }
2326
2327 /**
2328  * gtk_tree_view_column_set_reorderable:
2329  * @tree_column: A #GtkTreeViewColumn
2330  * @reorderable: %TRUE, if the column can be reordered.
2331  * 
2332  * If @reorderable is %TRUE, then the column can be reordered by the end user
2333  * dragging the header.
2334  **/
2335 void
2336 gtk_tree_view_column_set_reorderable (GtkTreeViewColumn *tree_column,
2337                                       gboolean           reorderable)
2338 {
2339   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2340
2341   /*  if (reorderable)
2342       gtk_tree_view_column_set_clickable (tree_column, TRUE);*/
2343
2344   if (tree_column->reorderable == (reorderable?TRUE:FALSE))
2345     return;
2346
2347   tree_column->reorderable = (reorderable?TRUE:FALSE);
2348   gtk_tree_view_column_update_button (tree_column);
2349   g_object_notify (G_OBJECT (tree_column), "reorderable");
2350 }
2351
2352 /**
2353  * gtk_tree_view_column_get_reorderable:
2354  * @tree_column: A #GtkTreeViewColumn
2355  * 
2356  * Returns %TRUE if the @tree_column can be reordered by the user.
2357  * 
2358  * Return value: %TRUE if the @tree_column can be reordered by the user.
2359  **/
2360 gboolean
2361 gtk_tree_view_column_get_reorderable (GtkTreeViewColumn *tree_column)
2362 {
2363   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
2364
2365   return tree_column->reorderable;
2366 }
2367
2368
2369 /**
2370  * gtk_tree_view_column_set_sort_column_id:
2371  * @tree_column: a #GtkTreeViewColumn
2372  * @sort_column_id: The @sort_column_id of the model to sort on.
2373  *
2374  * Sets the logical @sort_column_id that this column sorts on when this column 
2375  * is selected for sorting.  Doing so makes the column header clickable.
2376  **/
2377 void
2378 gtk_tree_view_column_set_sort_column_id (GtkTreeViewColumn *tree_column,
2379                                          gint               sort_column_id)
2380 {
2381   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2382   g_return_if_fail (sort_column_id >= -1);
2383
2384   if (tree_column->sort_column_id == sort_column_id)
2385     return;
2386
2387   tree_column->sort_column_id = sort_column_id;
2388
2389   /* Handle unsetting the id */
2390   if (sort_column_id == -1)
2391     {
2392       GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_column->tree_view));
2393
2394       if (tree_column->sort_clicked_signal)
2395         {
2396           g_signal_handler_disconnect (tree_column, tree_column->sort_clicked_signal);
2397           tree_column->sort_clicked_signal = 0;
2398         }
2399
2400       if (tree_column->sort_column_changed_signal)
2401         {
2402           g_signal_handler_disconnect (model, tree_column->sort_column_changed_signal);
2403           tree_column->sort_column_changed_signal = 0;
2404         }
2405
2406       gtk_tree_view_column_set_sort_order (tree_column, GTK_SORT_ASCENDING);
2407       gtk_tree_view_column_set_sort_indicator (tree_column, FALSE);
2408       gtk_tree_view_column_set_clickable (tree_column, FALSE);
2409       g_object_notify (G_OBJECT (tree_column), "sort-column-id");
2410       return;
2411     }
2412
2413   gtk_tree_view_column_set_clickable (tree_column, TRUE);
2414
2415   if (! tree_column->sort_clicked_signal)
2416     tree_column->sort_clicked_signal = g_signal_connect (tree_column,
2417                                                          "clicked",
2418                                                          G_CALLBACK (gtk_tree_view_column_sort),
2419                                                          NULL);
2420
2421   gtk_tree_view_column_setup_sort_column_id_callback (tree_column);
2422   g_object_notify (G_OBJECT (tree_column), "sort-column-id");
2423 }
2424
2425 /**
2426  * gtk_tree_view_column_get_sort_column_id:
2427  * @tree_column: a #GtkTreeViewColumn
2428  *
2429  * Gets the logical @sort_column_id that the model sorts on when this
2430  * column is selected for sorting.
2431  * See gtk_tree_view_column_set_sort_column_id().
2432  *
2433  * Return value: the current @sort_column_id for this column, or -1 if
2434  *               this column can't be used for sorting.
2435  **/
2436 gint
2437 gtk_tree_view_column_get_sort_column_id (GtkTreeViewColumn *tree_column)
2438 {
2439   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
2440
2441   return tree_column->sort_column_id;
2442 }
2443
2444 /**
2445  * gtk_tree_view_column_set_sort_indicator:
2446  * @tree_column: a #GtkTreeViewColumn
2447  * @setting: %TRUE to display an indicator that the column is sorted
2448  *
2449  * Call this function with a @setting of %TRUE to display an arrow in
2450  * the header button indicating the column is sorted. Call
2451  * gtk_tree_view_column_set_sort_order() to change the direction of
2452  * the arrow.
2453  * 
2454  **/
2455 void
2456 gtk_tree_view_column_set_sort_indicator (GtkTreeViewColumn     *tree_column,
2457                                          gboolean               setting)
2458 {
2459   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2460
2461   setting = setting != FALSE;
2462
2463   if (setting == tree_column->show_sort_indicator)
2464     return;
2465
2466   tree_column->show_sort_indicator = setting;
2467   gtk_tree_view_column_update_button (tree_column);
2468   g_object_notify (G_OBJECT (tree_column), "sort-indicator");
2469 }
2470
2471 /**
2472  * gtk_tree_view_column_get_sort_indicator:
2473  * @tree_column: a #GtkTreeViewColumn
2474  * 
2475  * Gets the value set by gtk_tree_view_column_set_sort_indicator().
2476  * 
2477  * Return value: whether the sort indicator arrow is displayed
2478  **/
2479 gboolean
2480 gtk_tree_view_column_get_sort_indicator  (GtkTreeViewColumn     *tree_column)
2481 {
2482   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
2483
2484   return tree_column->show_sort_indicator;
2485 }
2486
2487 /**
2488  * gtk_tree_view_column_set_sort_order:
2489  * @tree_column: a #GtkTreeViewColumn
2490  * @order: sort order that the sort indicator should indicate
2491  *
2492  * Changes the appearance of the sort indicator. 
2493  * 
2494  * This <emphasis>does not</emphasis> actually sort the model.  Use
2495  * gtk_tree_view_column_set_sort_column_id() if you want automatic sorting
2496  * support.  This function is primarily for custom sorting behavior, and should
2497  * be used in conjunction with gtk_tree_sortable_set_sort_column() to do
2498  * that. For custom models, the mechanism will vary. 
2499  * 
2500  * The sort indicator changes direction to indicate normal sort or reverse sort.
2501  * Note that you must have the sort indicator enabled to see anything when 
2502  * calling this function; see gtk_tree_view_column_set_sort_indicator().
2503  **/
2504 void
2505 gtk_tree_view_column_set_sort_order      (GtkTreeViewColumn     *tree_column,
2506                                           GtkSortType            order)
2507 {
2508   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2509
2510   if (order == tree_column->sort_order)
2511     return;
2512
2513   tree_column->sort_order = order;
2514   gtk_tree_view_column_update_button (tree_column);
2515   g_object_notify (G_OBJECT (tree_column), "sort-order");
2516 }
2517
2518 /**
2519  * gtk_tree_view_column_get_sort_order:
2520  * @tree_column: a #GtkTreeViewColumn
2521  * 
2522  * Gets the value set by gtk_tree_view_column_set_sort_order().
2523  * 
2524  * Return value: the sort order the sort indicator is indicating
2525  **/
2526 GtkSortType
2527 gtk_tree_view_column_get_sort_order      (GtkTreeViewColumn     *tree_column)
2528 {
2529   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), 0);
2530
2531   return tree_column->sort_order;
2532 }
2533
2534 /**
2535  * gtk_tree_view_column_cell_set_cell_data:
2536  * @tree_column: A #GtkTreeViewColumn.
2537  * @tree_model: The #GtkTreeModel to to get the cell renderers attributes from.
2538  * @iter: The #GtkTreeIter to to get the cell renderer's attributes from.
2539  * @is_expander: %TRUE, if the row has children
2540  * @is_expanded: %TRUE, if the row has visible children
2541  * 
2542  * Sets the cell renderer based on the @tree_model and @iter.  That is, for
2543  * every attribute mapping in @tree_column, it will get a value from the set
2544  * column on the @iter, and use that value to set the attribute on the cell
2545  * renderer.  This is used primarily by the #GtkTreeView.
2546  **/
2547 void
2548 gtk_tree_view_column_cell_set_cell_data (GtkTreeViewColumn *tree_column,
2549                                          GtkTreeModel      *tree_model,
2550                                          GtkTreeIter       *iter,
2551                                          gboolean           is_expander,
2552                                          gboolean           is_expanded)
2553 {
2554   GSList *list;
2555   GValue value = { 0, };
2556   GList *cell_list;
2557   gboolean cell_is_expander, cell_is_expanded;
2558
2559   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2560
2561   if (tree_model == NULL)
2562     return;
2563
2564   for (cell_list = tree_column->cell_list; cell_list; cell_list = cell_list->next)
2565     {
2566       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) cell_list->data;
2567       GObject *cell = (GObject *) info->cell;
2568
2569       list = info->attributes;
2570
2571       g_object_freeze_notify (cell);
2572
2573       g_object_get (cell, "is-expander", &cell_is_expander, NULL);
2574       if (cell_is_expander != is_expander)
2575         g_object_set (cell, "is-expander", is_expander, NULL);
2576
2577       g_object_get (cell, "is-expanded", &cell_is_expanded, NULL);
2578       if (cell_is_expanded != is_expanded)
2579         g_object_set (cell, "is-expanded", is_expanded, NULL);
2580
2581       while (list && list->next)
2582         {
2583           gtk_tree_model_get_value (tree_model, iter,
2584                                     GPOINTER_TO_INT (list->next->data),
2585                                     &value);
2586           g_object_set_property (cell, (gchar *) list->data, &value);
2587           g_value_unset (&value);
2588           list = list->next->next;
2589         }
2590
2591       if (info->func)
2592         (* info->func) (tree_column, info->cell, tree_model, iter, info->func_data);
2593       g_object_thaw_notify (G_OBJECT (info->cell));
2594     }
2595
2596 }
2597
2598 /**
2599  * gtk_tree_view_column_cell_get_size:
2600  * @tree_column: A #GtkTreeViewColumn.
2601  * @cell_area: (allow-none): The area a cell in the column will be allocated, or %NULL
2602  * @x_offset: (allow-none): location to return x offset of a cell relative to @cell_area, or %NULL
2603  * @y_offset: (allow-none): location to return y offset of a cell relative to @cell_area, or %NULL
2604  * @width: (allow-none): location to return width needed to render a cell, or %NULL
2605  * @height: (allow-none): location to return height needed to render a cell, or %NULL
2606  * 
2607  * Obtains the width and height needed to render the column.  This is used
2608  * primarily by the #GtkTreeView.
2609  **/
2610 void
2611 gtk_tree_view_column_cell_get_size (GtkTreeViewColumn  *tree_column,
2612                                     const GdkRectangle *cell_area,
2613                                     gint               *x_offset,
2614                                     gint               *y_offset,
2615                                     gint               *width,
2616                                     gint               *height)
2617 {
2618   GtkRequisition min_size;
2619   GList *list;
2620   gboolean first_cell = TRUE;
2621   gint focus_line_width;
2622
2623   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
2624
2625   if (height)
2626     * height = 0;
2627   if (width)
2628     * width = 0;
2629
2630   gtk_widget_style_get (tree_column->tree_view, "focus-line-width", &focus_line_width, NULL);
2631   
2632   for (list = tree_column->cell_list; list; list = list->next)
2633     {
2634       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
2635       gboolean visible;
2636       g_object_get (info->cell, "visible", &visible, NULL);
2637
2638       if (visible == FALSE)
2639         continue;
2640
2641       if (first_cell == FALSE && width)
2642         *width += tree_column->spacing;
2643
2644       gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (info->cell),
2645                                       GTK_WIDGET (tree_column->tree_view),
2646                                       &min_size, NULL);
2647
2648       if (height)
2649         * height = MAX (*height, min_size.height + focus_line_width * 2);
2650       info->requested_width = MAX (info->requested_width, min_size.width + focus_line_width * 2);
2651       if (width)
2652         * width += info->requested_width;
2653       first_cell = FALSE;
2654     }
2655 }
2656
2657 /* rendering, event handling and rendering focus are somewhat complicated, and
2658  * quite a bit of code.  Rather than duplicate them, we put them together to
2659  * keep the code in one place.
2660  *
2661  * To better understand what's going on, check out
2662  * docs/tree-column-sizing.png
2663  */
2664 enum {
2665   CELL_ACTION_RENDER,
2666   CELL_ACTION_FOCUS,
2667   CELL_ACTION_EVENT
2668 };
2669
2670 static gboolean
2671 gtk_tree_view_column_cell_process_action (GtkTreeViewColumn  *tree_column,
2672                                           cairo_t            *cr,
2673                                           const GdkRectangle *background_area,
2674                                           const GdkRectangle *cell_area,
2675                                           guint               flags,
2676                                           gint                action,
2677                                           GdkRectangle       *focus_rectangle, /* FOCUS  */
2678                                           GtkCellEditable   **editable_widget, /* EVENT  */
2679                                           GdkEvent           *event,           /* EVENT  */
2680                                           gchar              *path_string)     /* EVENT  */
2681 {
2682   GList *list;
2683   GdkRectangle real_cell_area;
2684   GdkRectangle real_background_area;
2685   gint depth = 0;
2686   gint expand_cell_count = 0;
2687   gint full_requested_width = 0;
2688   gint extra_space;
2689   gint min_x, min_y, max_x, max_y;
2690   gint focus_line_width;
2691   gint special_cells;
2692   gint horizontal_separator;
2693   gboolean cursor_row = FALSE;
2694   gboolean first_cell = TRUE;
2695   gboolean rtl;
2696   /* If we have rtl text, we need to transform our areas */
2697   GdkRectangle rtl_cell_area;
2698   GdkRectangle rtl_background_area;
2699
2700   min_x = G_MAXINT;
2701   min_y = G_MAXINT;
2702   max_x = 0;
2703   max_y = 0;
2704
2705   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_column->tree_view)) == GTK_TEXT_DIR_RTL);
2706   special_cells = _gtk_tree_view_column_count_special_cells (tree_column);
2707
2708   if (special_cells > 1 && action == CELL_ACTION_FOCUS)
2709     {
2710       GtkTreeViewColumnCellInfo *info = NULL;
2711       gboolean found_has_focus = FALSE;
2712
2713       /* one should have focus */
2714       for (list = tree_column->cell_list; list; list = list->next)
2715         {
2716           info = list->data;
2717           if (info && info->has_focus)
2718             {
2719               found_has_focus = TRUE;
2720               break;
2721             }
2722         }
2723
2724       if (!found_has_focus)
2725         {
2726           /* give the first one focus */
2727           info = gtk_tree_view_column_cell_first (tree_column)->data;
2728           info->has_focus = TRUE;
2729         }
2730     }
2731
2732   cursor_row = flags & GTK_CELL_RENDERER_FOCUSED;
2733
2734   gtk_widget_style_get (GTK_WIDGET (tree_column->tree_view),
2735                         "focus-line-width", &focus_line_width,
2736                         "horizontal-separator", &horizontal_separator,
2737                         NULL);
2738
2739   real_cell_area = *cell_area;
2740   real_background_area = *background_area;
2741
2742
2743   real_cell_area.x += focus_line_width;
2744   real_cell_area.y += focus_line_width;
2745   real_cell_area.height -= 2 * focus_line_width;
2746
2747   if (rtl)
2748     depth = real_background_area.width - real_cell_area.width;
2749   else
2750     depth = real_cell_area.x - real_background_area.x;
2751
2752   /* Find out how much extra space we have to allocate */
2753   for (list = tree_column->cell_list; list; list = list->next)
2754     {
2755       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *)list->data;
2756
2757       if (!gtk_cell_renderer_get_visible (info->cell))
2758         continue;
2759
2760       if (info->expand == TRUE)
2761         expand_cell_count ++;
2762       full_requested_width += info->requested_width;
2763
2764       if (!first_cell)
2765         full_requested_width += tree_column->spacing;
2766
2767       first_cell = FALSE;
2768     }
2769
2770   extra_space = cell_area->width - full_requested_width;
2771   if (extra_space < 0)
2772     extra_space = 0;
2773   else if (extra_space > 0 && expand_cell_count > 0)
2774     extra_space /= expand_cell_count;
2775
2776   /* iterate list for GTK_PACK_START cells */
2777   for (list = tree_column->cell_list; list; list = list->next)
2778     {
2779       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
2780
2781       if (info->pack == GTK_PACK_END)
2782         continue;
2783
2784       if (!gtk_cell_renderer_get_visible (info->cell))
2785         continue;
2786
2787       if ((info->has_focus || special_cells == 1) && cursor_row)
2788         flags |= GTK_CELL_RENDERER_FOCUSED;
2789       else
2790         flags &= ~GTK_CELL_RENDERER_FOCUSED;
2791
2792       info->real_width = info->requested_width + (info->expand?extra_space:0);
2793
2794       /* We constrain ourselves to only the width available */
2795       if (real_cell_area.x - focus_line_width + info->real_width > cell_area->x + cell_area->width)
2796         {
2797           info->real_width = cell_area->x + cell_area->width - real_cell_area.x;
2798         }   
2799
2800       if (real_cell_area.x > cell_area->x + cell_area->width)
2801         break;
2802
2803       real_cell_area.width = info->real_width;
2804       real_cell_area.width -= 2 * focus_line_width;
2805
2806       if (list->next)
2807         {
2808           real_background_area.width = info->real_width + depth;
2809         }
2810       else
2811         {
2812           /* fill the rest of background for the last cell */
2813           real_background_area.width = background_area->x + background_area->width - real_background_area.x;
2814         }
2815
2816       rtl_cell_area = real_cell_area;
2817       rtl_background_area = real_background_area;
2818       
2819       if (rtl)
2820         {
2821           rtl_cell_area.x = cell_area->x + cell_area->width - (real_cell_area.x - cell_area->x) - real_cell_area.width;
2822           rtl_background_area.x = background_area->x + background_area->width - (real_background_area.x - background_area->x) - real_background_area.width;
2823         }
2824
2825       /* RENDER */
2826       if (action == CELL_ACTION_RENDER)
2827         {
2828           gtk_cell_renderer_render_cairo (info->cell,
2829                                           cr,
2830                                           tree_column->tree_view,
2831                                           &rtl_background_area,
2832                                           &rtl_cell_area,
2833                                           flags);
2834         }
2835       /* FOCUS */
2836       else if (action == CELL_ACTION_FOCUS)
2837         {
2838           gint x_offset, y_offset;
2839           GtkRequisition min_size;
2840
2841           gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (info->cell),
2842                                           tree_column->tree_view,
2843                                           &min_size, NULL);
2844
2845           _gtk_cell_renderer_calc_offset (info->cell, &rtl_cell_area,
2846                                           gtk_widget_get_direction (tree_column->tree_view),
2847                                           min_size.width, min_size.height,
2848                                           &x_offset, &y_offset);
2849
2850           if (special_cells > 1)
2851             {
2852               if (info->has_focus)
2853                 {
2854                   min_x = rtl_cell_area.x + x_offset;
2855                   max_x = min_x + min_size.width;
2856                   min_y = rtl_cell_area.y + y_offset;
2857                   max_y = min_y + min_size.height;
2858                 }
2859             }
2860           else
2861             {
2862               if (min_x > (rtl_cell_area.x + x_offset))
2863                 min_x = rtl_cell_area.x + x_offset;
2864               if (max_x < rtl_cell_area.x + x_offset + min_size.width)
2865                 max_x = rtl_cell_area.x + x_offset + min_size.width;
2866               if (min_y > (rtl_cell_area.y + y_offset))
2867                 min_y = rtl_cell_area.y + y_offset;
2868               if (max_y < rtl_cell_area.y + y_offset + min_size.height)
2869                 max_y = rtl_cell_area.y + y_offset + min_size.height;
2870             }
2871         }
2872       /* EVENT */
2873       else if (action == CELL_ACTION_EVENT)
2874         {
2875           gboolean try_event = FALSE;
2876
2877           if (event)
2878             {
2879               if (special_cells == 1)
2880                 {
2881                   /* only 1 activatable cell -> whole column can activate */
2882                   if (cell_area->x <= ((GdkEventButton *)event)->x &&
2883                       cell_area->x + cell_area->width > ((GdkEventButton *)event)->x)
2884                     try_event = TRUE;
2885                 }
2886               else if (rtl_cell_area.x <= ((GdkEventButton *)event)->x &&
2887                   rtl_cell_area.x + rtl_cell_area.width > ((GdkEventButton *)event)->x)
2888                   /* only activate cell if the user clicked on an individual
2889                    * cell
2890                    */
2891                 try_event = TRUE;
2892             }
2893           else if (special_cells > 1 && info->has_focus)
2894             try_event = TRUE;
2895           else if (special_cells == 1)
2896             try_event = TRUE;
2897
2898           if (try_event)
2899             {
2900               gboolean visible, mode;
2901
2902               g_object_get (info->cell,
2903                             "visible", &visible,
2904                             "mode", &mode,
2905                             NULL);
2906               if (visible && mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
2907                 {
2908                   if (gtk_cell_renderer_activate (info->cell,
2909                                                   event,
2910                                                   tree_column->tree_view,
2911                                                   path_string,
2912                                                   &rtl_background_area,
2913                                                   &rtl_cell_area,
2914                                                   flags))
2915                     {
2916                       flags &= ~GTK_CELL_RENDERER_FOCUSED;
2917                       return TRUE;
2918                     }
2919                 }
2920               else if (visible && mode == GTK_CELL_RENDERER_MODE_EDITABLE)
2921                 {
2922                   *editable_widget =
2923                     gtk_cell_renderer_start_editing (info->cell,
2924                                                      event,
2925                                                      tree_column->tree_view,
2926                                                      path_string,
2927                                                      &rtl_background_area,
2928                                                      &rtl_cell_area,
2929                                                      flags);
2930
2931                   if (*editable_widget != NULL)
2932                     {
2933                       g_return_val_if_fail (GTK_IS_CELL_EDITABLE (*editable_widget), FALSE);
2934                       info->in_editing_mode = TRUE;
2935                       gtk_tree_view_column_focus_cell (tree_column, info->cell);
2936                       
2937                       flags &= ~GTK_CELL_RENDERER_FOCUSED;
2938
2939                       return TRUE;
2940                     }
2941                 }
2942             }
2943         }
2944
2945       flags &= ~GTK_CELL_RENDERER_FOCUSED;
2946
2947       real_cell_area.x += (real_cell_area.width + 2 * focus_line_width + tree_column->spacing);
2948       real_background_area.x += real_background_area.width + tree_column->spacing;
2949
2950       /* Only needed for first cell */
2951       depth = 0;
2952     }
2953
2954   /* iterate list for PACK_END cells */
2955   for (list = g_list_last (tree_column->cell_list); list; list = list->prev)
2956     {
2957       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
2958
2959       if (info->pack == GTK_PACK_START)
2960         continue;
2961
2962       if (!gtk_cell_renderer_get_visible(info->cell))
2963         continue;
2964
2965       if ((info->has_focus || special_cells == 1) && cursor_row)
2966         flags |= GTK_CELL_RENDERER_FOCUSED;
2967       else
2968         flags &= ~GTK_CELL_RENDERER_FOCUSED;
2969
2970       info->real_width = info->requested_width + (info->expand?extra_space:0);
2971
2972       /* We constrain ourselves to only the width available */
2973       if (real_cell_area.x - focus_line_width + info->real_width > cell_area->x + cell_area->width)
2974         {
2975           info->real_width = cell_area->x + cell_area->width - real_cell_area.x;
2976         }   
2977
2978       if (real_cell_area.x > cell_area->x + cell_area->width)
2979         break;
2980
2981       real_cell_area.width = info->real_width;
2982       real_cell_area.width -= 2 * focus_line_width;
2983       real_background_area.width = info->real_width + depth;
2984
2985       rtl_cell_area = real_cell_area;
2986       rtl_background_area = real_background_area;
2987       if (rtl)
2988         {
2989           rtl_cell_area.x = cell_area->x + cell_area->width - (real_cell_area.x - cell_area->x) - real_cell_area.width;
2990           rtl_background_area.x = background_area->x + background_area->width - (real_background_area.x - background_area->x) - real_background_area.width;
2991         }
2992
2993       /* RENDER */
2994       if (action == CELL_ACTION_RENDER)
2995         {
2996           gtk_cell_renderer_render_cairo (info->cell,
2997                                           cr,
2998                                           tree_column->tree_view,
2999                                           &rtl_background_area,
3000                                           &rtl_cell_area,
3001                                           flags);
3002         }
3003       /* FOCUS */
3004       else if (action == CELL_ACTION_FOCUS)
3005         {
3006           gint x_offset, y_offset;
3007           GtkRequisition min_size;
3008
3009           gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (info->cell),
3010                                           tree_column->tree_view,
3011                                           &min_size, NULL);
3012
3013           _gtk_cell_renderer_calc_offset (info->cell, &rtl_cell_area,
3014                                           gtk_widget_get_direction (tree_column->tree_view),
3015                                           min_size.width, min_size.height,
3016                                           &x_offset, &y_offset);
3017
3018           if (special_cells > 1)
3019             {
3020               if (info->has_focus)
3021                 {
3022                   min_x = rtl_cell_area.x + x_offset;
3023                   max_x = min_x + min_size.width;
3024                   min_y = rtl_cell_area.y + y_offset;
3025                   max_y = min_y + min_size.height;
3026                 }
3027             }
3028           else
3029             {
3030               if (min_x > (rtl_cell_area.x + x_offset))
3031                 min_x = rtl_cell_area.x + x_offset;
3032               if (max_x < rtl_cell_area.x + x_offset + min_size.width)
3033                 max_x = rtl_cell_area.x + x_offset + min_size.width;
3034               if (min_y > (rtl_cell_area.y + y_offset))
3035                 min_y = rtl_cell_area.y + y_offset;
3036               if (max_y < rtl_cell_area.y + y_offset + min_size.height)
3037                 max_y = rtl_cell_area.y + y_offset + min_size.height;
3038             }
3039         }
3040       /* EVENT */
3041       else if (action == CELL_ACTION_EVENT)
3042         {
3043           gboolean try_event = FALSE;
3044
3045           if (event)
3046             {
3047               if (special_cells == 1)
3048                 {
3049                   /* only 1 activatable cell -> whole column can activate */
3050                   if (cell_area->x <= ((GdkEventButton *)event)->x &&
3051                       cell_area->x + cell_area->width > ((GdkEventButton *)event)->x)
3052                     try_event = TRUE;
3053                 }
3054               else if (rtl_cell_area.x <= ((GdkEventButton *)event)->x &&
3055                   rtl_cell_area.x + rtl_cell_area.width > ((GdkEventButton *)event)->x)
3056                 /* only activate cell if the user clicked on an individual
3057                  * cell
3058                  */
3059                 try_event = TRUE;
3060             }
3061           else if (special_cells > 1 && info->has_focus)
3062             try_event = TRUE;
3063           else if (special_cells == 1)
3064             try_event = TRUE;
3065
3066           if (try_event)
3067             {
3068               gboolean visible, mode;
3069
3070               g_object_get (info->cell,
3071                             "visible", &visible,
3072                             "mode", &mode,
3073                             NULL);
3074               if (visible && mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
3075                 {
3076                   if (gtk_cell_renderer_activate (info->cell,
3077                                                   event,
3078                                                   tree_column->tree_view,
3079                                                   path_string,
3080                                                   &rtl_background_area,
3081                                                   &rtl_cell_area,
3082                                                   flags))
3083                     {
3084                       flags &= ~GTK_CELL_RENDERER_FOCUSED;
3085                       return TRUE;
3086                     }
3087                 }
3088               else if (visible && mode == GTK_CELL_RENDERER_MODE_EDITABLE)
3089                 {
3090                   *editable_widget =
3091                     gtk_cell_renderer_start_editing (info->cell,
3092                                                      event,
3093                                                      tree_column->tree_view,
3094                                                      path_string,
3095                                                      &rtl_background_area,
3096                                                      &rtl_cell_area,
3097                                                      flags);
3098
3099                   if (*editable_widget != NULL)
3100                     {
3101                       g_return_val_if_fail (GTK_IS_CELL_EDITABLE (*editable_widget), FALSE);
3102                       info->in_editing_mode = TRUE;
3103                       gtk_tree_view_column_focus_cell (tree_column, info->cell);
3104
3105                       flags &= ~GTK_CELL_RENDERER_FOCUSED;
3106                       return TRUE;
3107                     }
3108                 }
3109             }
3110         }
3111
3112       flags &= ~GTK_CELL_RENDERER_FOCUSED;
3113
3114       real_cell_area.x += (real_cell_area.width + 2 * focus_line_width + tree_column->spacing);
3115       real_background_area.x += (real_background_area.width + tree_column->spacing);
3116
3117       /* Only needed for first cell */
3118       depth = 0;
3119     }
3120
3121   /* fill focus_rectangle when required */
3122   if (action == CELL_ACTION_FOCUS)
3123     {
3124       if (min_x >= max_x || min_y >= max_y)
3125         {
3126           *focus_rectangle = *cell_area;
3127           /* don't change the focus_rectangle, just draw it nicely inside
3128            * the cell area */
3129         }
3130       else
3131         {
3132           focus_rectangle->x = min_x - focus_line_width;
3133           focus_rectangle->y = min_y - focus_line_width;
3134           focus_rectangle->width = (max_x - min_x) + 2 * focus_line_width;
3135           focus_rectangle->height = (max_y - min_y) + 2 * focus_line_width;
3136         }
3137     }
3138
3139   return FALSE;
3140 }
3141
3142 /**
3143  * gtk_tree_view_column_cell_render:
3144  * @tree_column: A #GtkTreeViewColumn.
3145  * @cr: cairo context to draw to
3146  * @background_area: entire cell area (including tree expanders and maybe padding on the sides)
3147  * @cell_area: area normally rendered by a cell renderer
3148  * @flags: flags that affect rendering
3149  * 
3150  * Renders the cell contained by #tree_column. This is used primarily by the
3151  * #GtkTreeView.
3152  **/
3153 void
3154 _gtk_tree_view_column_cell_render (GtkTreeViewColumn  *tree_column,
3155                                    cairo_t            *cr,
3156                                    const GdkRectangle *background_area,
3157                                    const GdkRectangle *cell_area,
3158                                    guint               flags)
3159 {
3160   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
3161   g_return_if_fail (cr != NULL);
3162   g_return_if_fail (background_area != NULL);
3163   g_return_if_fail (cell_area != NULL);
3164
3165   cairo_save (cr);
3166
3167   gtk_tree_view_column_cell_process_action (tree_column,
3168                                             cr,
3169                                             background_area,
3170                                             cell_area,
3171                                             flags,
3172                                             CELL_ACTION_RENDER,
3173                                             NULL, NULL, NULL, NULL);
3174
3175   cairo_restore (cr);
3176 }
3177
3178 gboolean
3179 _gtk_tree_view_column_cell_event (GtkTreeViewColumn  *tree_column,
3180                                   GtkCellEditable   **editable_widget,
3181                                   GdkEvent           *event,
3182                                   gchar              *path_string,
3183                                   const GdkRectangle *background_area,
3184                                   const GdkRectangle *cell_area,
3185                                   guint               flags)
3186 {
3187   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
3188
3189   return gtk_tree_view_column_cell_process_action (tree_column,
3190                                                    NULL,
3191                                                    background_area,
3192                                                    cell_area,
3193                                                    flags,
3194                                                    CELL_ACTION_EVENT,
3195                                                    NULL,
3196                                                    editable_widget,
3197                                                    event,
3198                                                    path_string);
3199 }
3200
3201 void
3202 _gtk_tree_view_column_get_focus_area (GtkTreeViewColumn  *tree_column,
3203                                       const GdkRectangle *background_area,
3204                                       const GdkRectangle *cell_area,
3205                                       GdkRectangle       *focus_area)
3206 {
3207   gtk_tree_view_column_cell_process_action (tree_column,
3208                                             NULL,
3209                                             background_area,
3210                                             cell_area,
3211                                             0,
3212                                             CELL_ACTION_FOCUS,
3213                                             focus_area,
3214                                             NULL, NULL, NULL);
3215 }
3216
3217
3218 /* cell list manipulation */
3219 static GList *
3220 gtk_tree_view_column_cell_first (GtkTreeViewColumn *tree_column)
3221 {
3222   GList *list = tree_column->cell_list;
3223
3224   /* first GTK_PACK_START cell we find */
3225   for ( ; list; list = list->next)
3226     {
3227       GtkTreeViewColumnCellInfo *info = list->data;
3228       if (info->pack == GTK_PACK_START)
3229         return list;
3230     }
3231
3232   /* hmm, else the *last* GTK_PACK_END cell */
3233   list = g_list_last (tree_column->cell_list);
3234
3235   for ( ; list; list = list->prev)
3236     {
3237       GtkTreeViewColumnCellInfo *info = list->data;
3238       if (info->pack == GTK_PACK_END)
3239         return list;
3240     }
3241
3242   return NULL;
3243 }
3244
3245 static GList *
3246 gtk_tree_view_column_cell_last (GtkTreeViewColumn *tree_column)
3247 {
3248   GList *list = tree_column->cell_list;
3249
3250   /* *first* GTK_PACK_END cell we find */
3251   for ( ; list ; list = list->next)
3252     {
3253       GtkTreeViewColumnCellInfo *info = list->data;
3254       if (info->pack == GTK_PACK_END)
3255         return list;
3256     }
3257
3258   /* hmm, else the last GTK_PACK_START cell */
3259   list = g_list_last (tree_column->cell_list);
3260
3261   for ( ; list; list = list->prev)
3262     {
3263       GtkTreeViewColumnCellInfo *info = list->data;
3264       if (info->pack == GTK_PACK_START)
3265         return list;
3266     }
3267
3268   return NULL;
3269 }
3270
3271 static GList *
3272 gtk_tree_view_column_cell_next (GtkTreeViewColumn *tree_column,
3273                                 GList             *current)
3274 {
3275   GList *list;
3276   GtkTreeViewColumnCellInfo *info = current->data;
3277
3278   if (info->pack == GTK_PACK_START)
3279     {
3280       for (list = current->next; list; list = list->next)
3281         {
3282           GtkTreeViewColumnCellInfo *inf = list->data;
3283           if (inf->pack == GTK_PACK_START)
3284             return list;
3285         }
3286
3287       /* out of GTK_PACK_START cells, get *last* GTK_PACK_END one */
3288       list = g_list_last (tree_column->cell_list);
3289       for (; list; list = list->prev)
3290         {
3291           GtkTreeViewColumnCellInfo *inf = list->data;
3292           if (inf->pack == GTK_PACK_END)
3293             return list;
3294         }
3295     }
3296
3297   for (list = current->prev; list; list = list->prev)
3298     {
3299       GtkTreeViewColumnCellInfo *inf = list->data;
3300       if (inf->pack == GTK_PACK_END)
3301         return list;
3302     }
3303
3304   return NULL;
3305 }
3306
3307 static GList *
3308 gtk_tree_view_column_cell_prev (GtkTreeViewColumn *tree_column,
3309                                 GList             *current)
3310 {
3311   GList *list;
3312   GtkTreeViewColumnCellInfo *info = current->data;
3313
3314   if (info->pack == GTK_PACK_END)
3315     {
3316       for (list = current->next; list; list = list->next)
3317         {
3318           GtkTreeViewColumnCellInfo *inf = list->data;
3319           if (inf->pack == GTK_PACK_END)
3320             return list;
3321         }
3322
3323       /* out of GTK_PACK_END, get last GTK_PACK_START one */
3324       list = g_list_last (tree_column->cell_list);
3325       for ( ; list; list = list->prev)
3326         {
3327           GtkTreeViewColumnCellInfo *inf = list->data;
3328           if (inf->pack == GTK_PACK_START)
3329             return list;
3330         }
3331     }
3332
3333   for (list = current->prev; list; list = list->prev)
3334     {
3335       GtkTreeViewColumnCellInfo *inf = list->data;
3336       if (inf->pack == GTK_PACK_START)
3337         return list;
3338     }
3339
3340   return NULL;
3341 }
3342
3343 gboolean
3344 _gtk_tree_view_column_cell_focus (GtkTreeViewColumn *tree_column,
3345                                   gint               direction,
3346                                   gboolean           left,
3347                                   gboolean           right)
3348 {
3349   gint count;
3350   gboolean rtl;
3351
3352   count = _gtk_tree_view_column_count_special_cells (tree_column);
3353   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_column->tree_view)) == GTK_TEXT_DIR_RTL;
3354
3355   /* if we are the current focus column and have multiple editable cells,
3356    * try to select the next one, else move the focus to the next column
3357    */
3358   if (GTK_TREE_VIEW (tree_column->tree_view)->priv->focus_column == tree_column)
3359     {
3360       if (count > 1)
3361         {
3362           GList *next, *prev;
3363           GList *list = tree_column->cell_list;
3364           GtkTreeViewColumnCellInfo *info = NULL;
3365
3366           /* find current focussed cell */
3367           for ( ; list; list = list->next)
3368             {
3369               info = list->data;
3370               if (info->has_focus)
3371                 break;
3372             }
3373
3374           /* not a focussed cell in the focus column? */
3375           if (!list || !info || !info->has_focus)
3376             return FALSE;
3377
3378           if (rtl)
3379             {
3380               prev = gtk_tree_view_column_cell_next (tree_column, list);
3381               next = gtk_tree_view_column_cell_prev (tree_column, list);
3382             }
3383           else
3384             {
3385               next = gtk_tree_view_column_cell_next (tree_column, list);
3386               prev = gtk_tree_view_column_cell_prev (tree_column, list);
3387             }
3388
3389           info->has_focus = FALSE;
3390           if (direction > 0 && next)
3391             {
3392               info = next->data;
3393               info->has_focus = TRUE;
3394               return TRUE;
3395             }
3396           else if (direction > 0 && !next && !right)
3397             {
3398               /* keep focus on last cell */
3399               if (rtl)
3400                 info = gtk_tree_view_column_cell_first (tree_column)->data;
3401               else
3402                 info = gtk_tree_view_column_cell_last (tree_column)->data;
3403
3404               info->has_focus = TRUE;
3405               return TRUE;
3406             }
3407           else if (direction < 0 && prev)
3408             {
3409               info = prev->data;
3410               info->has_focus = TRUE;
3411               return TRUE;
3412             }
3413           else if (direction < 0 && !prev && !left)
3414             {
3415               /* keep focus on first cell */
3416               if (rtl)
3417                 info = gtk_tree_view_column_cell_last (tree_column)->data;
3418               else
3419                 info = gtk_tree_view_column_cell_first (tree_column)->data;
3420
3421               info->has_focus = TRUE;
3422               return TRUE;
3423             }
3424         }
3425       return FALSE;
3426     }
3427
3428   /* we get focus, if we have multiple editable cells, give the correct one
3429    * focus
3430    */
3431   if (count > 1)
3432     {
3433       GList *list = tree_column->cell_list;
3434
3435       /* clear focus first */
3436       for ( ; list ; list = list->next)
3437         {
3438           GtkTreeViewColumnCellInfo *info = list->data;
3439           if (info->has_focus)
3440             info->has_focus = FALSE;
3441         }
3442
3443       list = NULL;
3444       if (rtl)
3445         {
3446           if (direction > 0)
3447             list = gtk_tree_view_column_cell_last (tree_column);
3448           else if (direction < 0)
3449             list = gtk_tree_view_column_cell_first (tree_column);
3450         }
3451       else
3452         {
3453           if (direction > 0)
3454             list = gtk_tree_view_column_cell_first (tree_column);
3455           else if (direction < 0)
3456             list = gtk_tree_view_column_cell_last (tree_column);
3457         }
3458
3459       if (list)
3460         ((GtkTreeViewColumnCellInfo *) list->data)->has_focus = TRUE;
3461     }
3462
3463   return TRUE;
3464 }
3465
3466 void
3467 _gtk_tree_view_column_cell_draw_focus (GtkTreeViewColumn  *tree_column,
3468                                        cairo_t            *cr,
3469                                        const GdkRectangle *background_area,
3470                                        const GdkRectangle *cell_area,
3471                                        guint               flags)
3472 {
3473   gint focus_line_width;
3474   GtkStateType cell_state;
3475   
3476   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
3477   g_return_if_fail (cr != NULL);
3478
3479   gtk_widget_style_get (GTK_WIDGET (tree_column->tree_view),
3480                         "focus-line-width", &focus_line_width, NULL);
3481   if (tree_column->editable_widget)
3482     {
3483       /* This function is only called on the editable row when editing.
3484        */
3485 #if 0
3486       gtk_paint_focus (tree_column->tree_view->style,
3487                        window,
3488                        gtk_widget_get_state (tree_column->tree_view),
3489                        NULL,
3490                        tree_column->tree_view,
3491                        "treeview",
3492                        cell_area->x - focus_line_width,
3493                        cell_area->y - focus_line_width,
3494                        cell_area->width + 2 * focus_line_width,
3495                        cell_area->height + 2 * focus_line_width);
3496 #endif      
3497     }
3498   else
3499     {
3500       GdkRectangle focus_rectangle;
3501       gtk_tree_view_column_cell_process_action (tree_column,
3502                                                 cr,
3503                                                 background_area,
3504                                                 cell_area,
3505                                                 flags,
3506                                                 CELL_ACTION_FOCUS,
3507                                                 &focus_rectangle,
3508                                                 NULL, NULL, NULL);
3509
3510       cell_state = flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
3511               (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
3512               (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL));
3513       gtk_cairo_paint_focus (gtk_widget_get_style (tree_column->tree_view),
3514                              cr,
3515                              cell_state,
3516                              tree_column->tree_view,
3517                              "treeview",
3518                              focus_rectangle.x,
3519                              focus_rectangle.y,
3520                              focus_rectangle.width,
3521                              focus_rectangle.height);
3522     }
3523 }
3524
3525 /**
3526  * gtk_tree_view_column_cell_is_visible:
3527  * @tree_column: A #GtkTreeViewColumn
3528  * 
3529  * Returns %TRUE if any of the cells packed into the @tree_column are visible.
3530  * For this to be meaningful, you must first initialize the cells with
3531  * gtk_tree_view_column_cell_set_cell_data()
3532  * 
3533  * Return value: %TRUE, if any of the cells packed into the @tree_column are currently visible
3534  **/
3535 gboolean
3536 gtk_tree_view_column_cell_is_visible (GtkTreeViewColumn *tree_column)
3537 {
3538   GList *list;
3539
3540   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), FALSE);
3541
3542   for (list = tree_column->cell_list; list; list = list->next)
3543     {
3544       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
3545
3546       if (gtk_cell_renderer_get_visible (info->cell))
3547         return TRUE;
3548     }
3549
3550   return FALSE;
3551 }
3552
3553 /**
3554  * gtk_tree_view_column_focus_cell:
3555  * @tree_column: A #GtkTreeViewColumn
3556  * @cell: A #GtkCellRenderer
3557  *
3558  * Sets the current keyboard focus to be at @cell, if the column contains
3559  * 2 or more editable and activatable cells.
3560  *
3561  * Since: 2.2
3562  **/
3563 void
3564 gtk_tree_view_column_focus_cell (GtkTreeViewColumn *tree_column,
3565                                  GtkCellRenderer   *cell)
3566 {
3567   GList *list;
3568   gboolean found_cell = FALSE;
3569
3570   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
3571   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
3572
3573   if (_gtk_tree_view_column_count_special_cells (tree_column) < 2)
3574     return;
3575
3576   for (list = tree_column->cell_list; list; list = list->next)
3577     {
3578       GtkTreeViewColumnCellInfo *info = list->data;
3579
3580       if (info->cell == cell)
3581         {
3582           info->has_focus = TRUE;
3583           found_cell = TRUE;
3584           break;
3585         }
3586     }
3587
3588   if (found_cell)
3589     {
3590       for (list = tree_column->cell_list; list; list = list->next)
3591         {
3592           GtkTreeViewColumnCellInfo *info = list->data;
3593
3594           if (info->cell != cell)
3595             info->has_focus = FALSE;
3596         }
3597
3598       /* FIXME: redraw? */
3599     }
3600 }
3601
3602 void
3603 _gtk_tree_view_column_cell_set_dirty (GtkTreeViewColumn *tree_column,
3604                                       gboolean           install_handler)
3605 {
3606   GList *list;
3607
3608   for (list = tree_column->cell_list; list; list = list->next)
3609     {
3610       GtkTreeViewColumnCellInfo *info = (GtkTreeViewColumnCellInfo *) list->data;
3611
3612       info->requested_width = 0;
3613     }
3614   tree_column->dirty = TRUE;
3615   tree_column->requested_width = -1;
3616   tree_column->width = 0;
3617
3618   if (tree_column->tree_view &&
3619       gtk_widget_get_realized (tree_column->tree_view))
3620     {
3621       if (install_handler)
3622         _gtk_tree_view_install_mark_rows_col_dirty (GTK_TREE_VIEW (tree_column->tree_view));
3623       else
3624         GTK_TREE_VIEW (tree_column->tree_view)->priv->mark_rows_col_dirty = TRUE;
3625       gtk_widget_queue_resize (tree_column->tree_view);
3626     }
3627 }
3628
3629 void
3630 _gtk_tree_view_column_start_editing (GtkTreeViewColumn *tree_column,
3631                                      GtkCellEditable   *cell_editable)
3632 {
3633   g_return_if_fail (tree_column->editable_widget == NULL);
3634
3635   tree_column->editable_widget = cell_editable;
3636 }
3637
3638 void
3639 _gtk_tree_view_column_stop_editing (GtkTreeViewColumn *tree_column)
3640 {
3641   GList *list;
3642
3643   g_return_if_fail (tree_column->editable_widget != NULL);
3644
3645   tree_column->editable_widget = NULL;
3646   for (list = tree_column->cell_list; list; list = list->next)
3647     ((GtkTreeViewColumnCellInfo *)list->data)->in_editing_mode = FALSE;
3648 }
3649
3650 void
3651 _gtk_tree_view_column_get_neighbor_sizes (GtkTreeViewColumn *column,
3652                                           GtkCellRenderer   *cell,
3653                                           gint              *left,
3654                                           gint              *right)
3655 {
3656   GList *list;
3657   GtkTreeViewColumnCellInfo *info;
3658   gint l, r;
3659   gboolean rtl;
3660
3661   l = r = 0;
3662
3663   list = gtk_tree_view_column_cell_first (column);  
3664
3665   while (list)
3666     {
3667       info = (GtkTreeViewColumnCellInfo *)list->data;
3668       
3669       list = gtk_tree_view_column_cell_next (column, list);
3670
3671       if (info->cell == cell)
3672         break;
3673       
3674       if (gtk_cell_renderer_get_visible (info->cell))
3675         l += info->real_width + column->spacing;
3676     }
3677
3678   while (list)
3679     {
3680       info = (GtkTreeViewColumnCellInfo *)list->data;
3681       
3682       list = gtk_tree_view_column_cell_next (column, list);
3683
3684       if (gtk_cell_renderer_get_visible (info->cell))
3685         r += info->real_width + column->spacing;
3686     }
3687
3688   rtl = (gtk_widget_get_direction (GTK_WIDGET (column->tree_view)) == GTK_TEXT_DIR_RTL);
3689   if (left)
3690     *left = rtl ? r : l;
3691
3692   if (right)
3693     *right = rtl ? l : r;
3694 }
3695
3696 /**
3697  * gtk_tree_view_column_cell_get_position:
3698  * @tree_column: a #GtkTreeViewColumn
3699  * @cell_renderer: a #GtkCellRenderer
3700  * @start_pos: return location for the horizontal position of @cell within
3701  *            @tree_column, may be %NULL
3702  * @width: return location for the width of @cell, may be %NULL
3703  *
3704  * Obtains the horizontal position and size of a cell in a column. If the
3705  * cell is not found in the column, @start_pos and @width are not changed and
3706  * %FALSE is returned.
3707  * 
3708  * Return value: %TRUE if @cell belongs to @tree_column.
3709  */
3710 gboolean
3711 gtk_tree_view_column_cell_get_position (GtkTreeViewColumn *tree_column,
3712                                         GtkCellRenderer   *cell_renderer,
3713                                         gint              *start_pos,
3714                                         gint              *width)
3715 {
3716   GList *list;
3717   gint current_x = 0;
3718   gboolean found_cell = FALSE;
3719   GtkTreeViewColumnCellInfo *cellinfo = NULL;
3720
3721   list = gtk_tree_view_column_cell_first (tree_column);
3722   for (; list; list = gtk_tree_view_column_cell_next (tree_column, list))
3723     {
3724       cellinfo = list->data;
3725       if (cellinfo->cell == cell_renderer)
3726         {
3727           found_cell = TRUE;
3728           break;
3729         }
3730
3731       if (gtk_cell_renderer_get_visible (cellinfo->cell))
3732         current_x += cellinfo->real_width;
3733     }
3734
3735   if (found_cell)
3736     {
3737       if (start_pos)
3738         *start_pos = current_x;
3739       if (width)
3740         *width = cellinfo->real_width;
3741     }
3742
3743   return found_cell;
3744 }
3745
3746 /**
3747  * gtk_tree_view_column_queue_resize:
3748  * @tree_column: A #GtkTreeViewColumn
3749  *
3750  * Flags the column, and the cell renderers added to this column, to have
3751  * their sizes renegotiated.
3752  *
3753  * Since: 2.8
3754  **/
3755 void
3756 gtk_tree_view_column_queue_resize (GtkTreeViewColumn *tree_column)
3757 {
3758   g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column));
3759
3760   if (tree_column->tree_view)
3761     _gtk_tree_view_column_cell_set_dirty (tree_column, TRUE);
3762 }
3763
3764 /**
3765  * gtk_tree_view_column_get_tree_view:
3766  * @tree_column: A #GtkTreeViewColumn
3767  *
3768  * Returns the #GtkTreeView wherein @tree_column has been inserted.
3769  * If @column is currently not inserted in any tree view, %NULL is
3770  * returned.
3771  *
3772  * Return value: (transfer none): The tree view wherein @column has
3773  *     been inserted if any, %NULL otherwise.
3774  *
3775  * Since: 2.12
3776  */
3777 GtkWidget *
3778 gtk_tree_view_column_get_tree_view (GtkTreeViewColumn *tree_column)
3779 {
3780   g_return_val_if_fail (GTK_IS_TREE_VIEW_COLUMN (tree_column), NULL);
3781
3782   return tree_column->tree_view;
3783 }