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