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