PsppSheetView (draw_empty_focus): Port to Gtk3
[pspp] / src / ui / gui / pspp-sheet-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2012, 2013 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 /* gtktreeview.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 <gtk/gtk.h>
41 #include <gdk/gdk.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <gdk/gdkkeysyms-compat.h>
44 #include <string.h>
45
46 #include "ui/gui/psppire-marshal.h"
47 #include "ui/gui/pspp-sheet-selection.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 /* Many keyboard shortcuts for Mac are the same as for X
54  * except they use Command key instead of Control (e.g. Cut,
55  * Copy, Paste). This symbol is for those simple cases. */
56 #ifndef GDK_WINDOWING_QUARTZ
57 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_CONTROL_MASK
58 #else
59 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_META_MASK
60 #endif
61
62 #define PSPP_SHEET_VIEW_PRIORITY_VALIDATE (GDK_PRIORITY_REDRAW + 5)
63 #define PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC (PSPP_SHEET_VIEW_PRIORITY_VALIDATE + 2)
64 #define PSPP_SHEET_VIEW_TIME_MS_PER_IDLE 30
65 #define SCROLL_EDGE_SIZE 15
66 #define EXPANDER_EXTRA_PADDING 4
67 #define PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT 5000
68
69 /* The "background" areas of all rows/cells add up to cover the entire tree.
70  * The background includes all inter-row and inter-cell spacing.
71  * The "cell" areas are the cell_area passed in to gtk_cell_renderer_render(),
72  * i.e. just the cells, no spacing.
73  */
74
75 #define BACKGROUND_HEIGHT(tree_view) (tree_view->priv->fixed_height)
76 #define CELL_HEIGHT(tree_view, separator) ((BACKGROUND_HEIGHT (tree_view)) - (separator))
77
78 /* Translate from bin_window coordinates to rbtree (tree coordinates) and
79  * vice versa.
80  */
81 #define TREE_WINDOW_Y_TO_RBTREE_Y(tree_view,y) ((y) + tree_view->priv->dy)
82 #define RBTREE_Y_TO_TREE_WINDOW_Y(tree_view,y) ((y) - tree_view->priv->dy)
83
84 /* This is in bin_window coordinates */
85 #define BACKGROUND_FIRST_PIXEL(tree_view,node) (RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, pspp_sheet_view_node_find_offset (tree_view, (node))))
86 #define CELL_FIRST_PIXEL(tree_view,node,separator) (BACKGROUND_FIRST_PIXEL (tree_view,node) + separator/2)
87
88 #define ROW_HEIGHT(tree_view) \
89   ((tree_view->priv->fixed_height > 0) ? (tree_view->priv->fixed_height) : (tree_view)->priv->expander_size)
90
91
92 typedef struct _PsppSheetViewChild PsppSheetViewChild;
93 struct _PsppSheetViewChild
94 {
95   GtkWidget *widget;
96   gint x;
97   gint y;
98   gint width;
99   gint height;
100 };
101
102
103 typedef struct _TreeViewDragInfo TreeViewDragInfo;
104 struct _TreeViewDragInfo
105 {
106   GdkModifierType start_button_mask;
107   GtkTargetList *_unused_source_target_list;
108   GdkDragAction source_actions;
109
110   GtkTargetList *_unused_dest_target_list;
111
112   guint source_set : 1;
113   guint dest_set : 1;
114 };
115
116
117 /* Signals */
118 enum
119 {
120   ROW_ACTIVATED,
121   COLUMNS_CHANGED,
122   CURSOR_CHANGED,
123   MOVE_CURSOR,
124   SELECT_ALL,
125   UNSELECT_ALL,
126   SELECT_CURSOR_ROW,
127   TOGGLE_CURSOR_ROW,
128   START_INTERACTIVE_SEARCH,
129   LAST_SIGNAL
130 };
131
132 /* Properties */
133 enum {
134   PROP_0,
135   PROP_MODEL,
136   PROP_HADJUSTMENT,
137   PROP_VADJUSTMENT,
138   PROP_HEADERS_VISIBLE,
139   PROP_HEADERS_CLICKABLE,
140   PROP_REORDERABLE,
141   PROP_RULES_HINT,
142   PROP_ENABLE_SEARCH,
143   PROP_SEARCH_COLUMN,
144   PROP_HOVER_SELECTION,
145   PROP_RUBBER_BANDING,
146   PROP_ENABLE_GRID_LINES,
147   PROP_TOOLTIP_COLUMN,
148   PROP_SPECIAL_CELLS,
149   PROP_FIXED_HEIGHT,
150   PROP_FIXED_HEIGHT_SET
151 };
152
153 /* object signals */
154 static void     pspp_sheet_view_finalize             (GObject          *object);
155 static void     pspp_sheet_view_set_property         (GObject         *object,
156                                                     guint            prop_id,
157                                                     const GValue    *value,
158                                                     GParamSpec      *pspec);
159 static void     pspp_sheet_view_get_property         (GObject         *object,
160                                                     guint            prop_id,
161                                                     GValue          *value,
162                                                     GParamSpec      *pspec);
163
164 static void     pspp_sheet_view_dispose              (GObject        *object);
165
166 /* gtkwidget signals */
167 static void     pspp_sheet_view_realize              (GtkWidget        *widget);
168 static void     pspp_sheet_view_unrealize            (GtkWidget        *widget);
169 static void     pspp_sheet_view_map                  (GtkWidget        *widget);
170 static void     pspp_sheet_view_size_request         (GtkWidget        *widget,
171                                                     GtkRequisition   *requisition);
172 static void     pspp_sheet_view_size_allocate        (GtkWidget        *widget,
173                                                     GtkAllocation    *allocation);
174 static gboolean pspp_sheet_view_expose               (GtkWidget        *widget,
175                                                     GdkEventExpose   *event);
176 static gboolean pspp_sheet_view_key_press            (GtkWidget        *widget,
177                                                     GdkEventKey      *event);
178 static gboolean pspp_sheet_view_key_release          (GtkWidget        *widget,
179                                                     GdkEventKey      *event);
180 static gboolean pspp_sheet_view_motion               (GtkWidget        *widget,
181                                                     GdkEventMotion   *event);
182 static gboolean pspp_sheet_view_enter_notify         (GtkWidget        *widget,
183                                                     GdkEventCrossing *event);
184 static gboolean pspp_sheet_view_leave_notify         (GtkWidget        *widget,
185                                                     GdkEventCrossing *event);
186 static gboolean pspp_sheet_view_button_press         (GtkWidget        *widget,
187                                                     GdkEventButton   *event);
188 static gboolean pspp_sheet_view_button_release       (GtkWidget        *widget,
189                                                     GdkEventButton   *event);
190 static gboolean pspp_sheet_view_grab_broken          (GtkWidget          *widget,
191                                                     GdkEventGrabBroken *event);
192
193 static void     pspp_sheet_view_set_focus_child      (GtkContainer     *container,
194                                                     GtkWidget        *child);
195 static gint     pspp_sheet_view_focus_out            (GtkWidget        *widget,
196                                                     GdkEventFocus    *event);
197 static gint     pspp_sheet_view_focus                (GtkWidget        *widget,
198                                                     GtkDirectionType  direction);
199 static void     pspp_sheet_view_grab_focus           (GtkWidget        *widget);
200 static void     pspp_sheet_view_style_set            (GtkWidget        *widget,
201                                                     GtkStyle         *previous_style);
202 static void     pspp_sheet_view_grab_notify          (GtkWidget        *widget,
203                                                     gboolean          was_grabbed);
204 static void     pspp_sheet_view_state_changed        (GtkWidget        *widget,
205                                                     GtkStateType      previous_state);
206
207 /* container signals */
208 static void     pspp_sheet_view_remove               (GtkContainer     *container,
209                                                     GtkWidget        *widget);
210 static void     pspp_sheet_view_forall               (GtkContainer     *container,
211                                                     gboolean          include_internals,
212                                                     GtkCallback       callback,
213                                                     gpointer          callback_data);
214
215 /* Source side drag signals */
216 static void pspp_sheet_view_drag_begin       (GtkWidget        *widget,
217                                             GdkDragContext   *context);
218 static void pspp_sheet_view_drag_end         (GtkWidget        *widget,
219                                             GdkDragContext   *context);
220 static void pspp_sheet_view_drag_data_get    (GtkWidget        *widget,
221                                             GdkDragContext   *context,
222                                             GtkSelectionData *selection_data,
223                                             guint             info,
224                                             guint             time);
225 static void pspp_sheet_view_drag_data_delete (GtkWidget        *widget,
226                                             GdkDragContext   *context);
227
228 /* Target side drag signals */
229 static void     pspp_sheet_view_drag_leave         (GtkWidget        *widget,
230                                                   GdkDragContext   *context,
231                                                   guint             time);
232 static gboolean pspp_sheet_view_drag_motion        (GtkWidget        *widget,
233                                                   GdkDragContext   *context,
234                                                   gint              x,
235                                                   gint              y,
236                                                   guint             time);
237 static gboolean pspp_sheet_view_drag_drop          (GtkWidget        *widget,
238                                                   GdkDragContext   *context,
239                                                   gint              x,
240                                                   gint              y,
241                                                   guint             time);
242 static void     pspp_sheet_view_drag_data_received (GtkWidget        *widget,
243                                                   GdkDragContext   *context,
244                                                   gint              x,
245                                                   gint              y,
246                                                   GtkSelectionData *selection_data,
247                                                   guint             info,
248                                                   guint             time);
249
250 /* tree_model signals */
251 static void pspp_sheet_view_set_adjustments                 (PsppSheetView     *tree_view,
252                                                            GtkAdjustment   *hadj,
253                                                            GtkAdjustment   *vadj);
254 static gboolean pspp_sheet_view_real_move_cursor            (PsppSheetView     *tree_view,
255                                                            GtkMovementStep  step,
256                                                            gint             count);
257 static gboolean pspp_sheet_view_real_select_all             (PsppSheetView     *tree_view);
258 static gboolean pspp_sheet_view_real_unselect_all           (PsppSheetView     *tree_view);
259 static gboolean pspp_sheet_view_real_select_cursor_row      (PsppSheetView     *tree_view,
260                                                              gboolean         start_editing,
261                                                              PsppSheetSelectMode mode);
262 static gboolean pspp_sheet_view_real_toggle_cursor_row      (PsppSheetView     *tree_view);
263 static void pspp_sheet_view_row_changed                     (GtkTreeModel    *model,
264                                                            GtkTreePath     *path,
265                                                            GtkTreeIter     *iter,
266                                                            gpointer         data);
267 static void pspp_sheet_view_row_inserted                    (GtkTreeModel    *model,
268                                                            GtkTreePath     *path,
269                                                            GtkTreeIter     *iter,
270                                                            gpointer         data);
271 static void pspp_sheet_view_row_deleted                     (GtkTreeModel    *model,
272                                                            GtkTreePath     *path,
273                                                            gpointer         data);
274 static void pspp_sheet_view_rows_reordered                  (GtkTreeModel    *model,
275                                                            GtkTreePath     *parent,
276                                                            GtkTreeIter     *iter,
277                                                            gint            *new_order,
278                                                            gpointer         data);
279
280 /* Incremental reflow */
281 static gint validate_row             (PsppSheetView *tree_view,
282                                           int node,
283                                           GtkTreeIter *iter,
284                                           GtkTreePath *path);
285 static void     validate_visible_area    (PsppSheetView *tree_view);
286 static gboolean validate_rows_handler    (PsppSheetView *tree_view);
287 static gboolean presize_handler_callback (gpointer     data);
288 static void     install_presize_handler  (PsppSheetView *tree_view);
289 static void     install_scroll_sync_handler (PsppSheetView *tree_view);
290 static void     pspp_sheet_view_set_top_row   (PsppSheetView *tree_view,
291                                              GtkTreePath *path,
292                                              gint         offset);
293 static void     pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view);
294 static void     pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view);
295 static void     invalidate_empty_focus      (PsppSheetView *tree_view);
296
297 /* Internal functions */
298 static void     pspp_sheet_view_add_move_binding               (GtkBindingSet      *binding_set,
299                                                               guint               keyval,
300                                                               guint               modmask,
301                                                               gboolean            add_shifted_binding,
302                                                               GtkMovementStep     step,
303                                                               gint                count);
304 static void     pspp_sheet_view_queue_draw_path                (PsppSheetView        *tree_view,
305                                                               GtkTreePath        *path,
306                                                               const GdkRectangle *clip_rect);
307 static gint     pspp_sheet_view_new_column_width               (PsppSheetView        *tree_view,
308                                                               gint                i,
309                                                               gint               *x);
310 static void     pspp_sheet_view_adjustment_changed             (GtkAdjustment      *adjustment,
311                                                               PsppSheetView        *tree_view);
312 static void     pspp_sheet_view_clamp_node_visible             (PsppSheetView        *tree_view,
313                                                               int node);
314 static void     pspp_sheet_view_clamp_column_visible           (PsppSheetView        *tree_view,
315                                                               PsppSheetViewColumn  *column,
316                                                               gboolean            focus_to_cell);
317 static gboolean pspp_sheet_view_maybe_begin_dragging_row       (PsppSheetView        *tree_view,
318                                                               GdkEventMotion     *event);
319 static void     pspp_sheet_view_focus_to_cursor                (PsppSheetView        *tree_view);
320 static gboolean pspp_sheet_view_move_cursor_up_down            (PsppSheetView        *tree_view,
321                                                               gint                count,
322                                                                 PsppSheetSelectMode mode);
323 static void     pspp_sheet_view_move_cursor_page_up_down       (PsppSheetView        *tree_view,
324                                                               gint                count,
325                                                                 PsppSheetSelectMode mode);
326 static void     pspp_sheet_view_move_cursor_left_right         (PsppSheetView        *tree_view,
327                                                                 gint                count,
328                                                                 PsppSheetSelectMode mode);
329 static void     pspp_sheet_view_move_cursor_line_start_end     (PsppSheetView        *tree_view,
330                                                                 gint                count,
331                                                                 PsppSheetSelectMode mode);
332 static void     pspp_sheet_view_move_cursor_tab                (PsppSheetView        *tree_view,
333                                                               gint                count);
334 static void     pspp_sheet_view_move_cursor_start_end          (PsppSheetView        *tree_view,
335                                                               gint                count,
336                                                                 PsppSheetSelectMode mode);
337 static void     pspp_sheet_view_real_set_cursor                (PsppSheetView        *tree_view,
338                                                               GtkTreePath        *path,
339                                                               gboolean            clear_and_select,
340                                                               gboolean            clamp_node,
341                                                               PsppSheetSelectMode mode);
342 static gboolean pspp_sheet_view_has_special_cell               (PsppSheetView        *tree_view);
343 static void     pspp_sheet_view_stop_rubber_band               (PsppSheetView        *tree_view);
344 static void     update_prelight                              (PsppSheetView        *tree_view,
345                                                               int                 x,
346                                                               int                 y);
347 static void initialize_fixed_height_mode (PsppSheetView *tree_view);
348
349 /* interactive search */
350 static void     pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view);
351 static void     pspp_sheet_view_search_dialog_hide     (GtkWidget        *search_dialog,
352                                                          PsppSheetView      *tree_view);
353 static void     pspp_sheet_view_search_position_func      (PsppSheetView      *tree_view,
354                                                          GtkWidget        *search_dialog,
355                                                          gpointer          user_data);
356 static void     pspp_sheet_view_search_disable_popdown    (GtkEntry         *entry,
357                                                          GtkMenu          *menu,
358                                                          gpointer          data);
359 #if GTK3_TRANSITION
360 static void     pspp_sheet_view_search_preedit_changed    (GtkIMContext     *im_context,
361                                                          PsppSheetView      *tree_view);
362 #endif
363 static void     pspp_sheet_view_search_activate           (GtkEntry         *entry,
364                                                          PsppSheetView      *tree_view);
365 static gboolean pspp_sheet_view_real_search_enable_popdown(gpointer          data);
366 static void     pspp_sheet_view_search_enable_popdown     (GtkWidget        *widget,
367                                                          gpointer          data);
368 static gboolean pspp_sheet_view_search_delete_event       (GtkWidget        *widget,
369                                                          GdkEventAny      *event,
370                                                          PsppSheetView      *tree_view);
371 static gboolean pspp_sheet_view_search_button_press_event (GtkWidget        *widget,
372                                                          GdkEventButton   *event,
373                                                          PsppSheetView      *tree_view);
374 static gboolean pspp_sheet_view_search_scroll_event       (GtkWidget        *entry,
375                                                          GdkEventScroll   *event,
376                                                          PsppSheetView      *tree_view);
377 static gboolean pspp_sheet_view_search_key_press_event    (GtkWidget        *entry,
378                                                          GdkEventKey      *event,
379                                                          PsppSheetView      *tree_view);
380 static gboolean pspp_sheet_view_search_move               (GtkWidget        *window,
381                                                          PsppSheetView      *tree_view,
382                                                          gboolean          up);
383 static gboolean pspp_sheet_view_search_equal_func         (GtkTreeModel     *model,
384                                                          gint              column,
385                                                          const gchar      *key,
386                                                          GtkTreeIter      *iter,
387                                                          gpointer          search_data);
388 static gboolean pspp_sheet_view_search_iter               (GtkTreeModel     *model,
389                                                          PsppSheetSelection *selection,
390                                                          GtkTreeIter      *iter,
391                                                          const gchar      *text,
392                                                          gint             *count,
393                                                          gint              n);
394 static void     pspp_sheet_view_search_init               (GtkWidget        *entry,
395                                                          PsppSheetView      *tree_view);
396 static void     pspp_sheet_view_put                       (PsppSheetView      *tree_view,
397                                                          GtkWidget        *child_widget,
398                                                          gint              x,
399                                                          gint              y,
400                                                          gint              width,
401                                                          gint              height);
402 static gboolean pspp_sheet_view_start_editing             (PsppSheetView      *tree_view,
403                                                          GtkTreePath      *cursor_path);
404 static gboolean pspp_sheet_view_editable_button_press_event (GtkWidget *,
405                                                              GdkEventButton *,
406                                                              PsppSheetView *);
407 static void pspp_sheet_view_editable_clicked (GtkButton *, PsppSheetView *);
408 static void pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
409                                               PsppSheetViewColumn *column,
410                                               GtkTreePath       *path,
411                                               GtkCellEditable   *cell_editable,
412                                               GdkRectangle      *cell_area,
413                                               GdkEvent          *event,
414                                               guint              flags);
415 static gboolean pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
416                                                              gboolean     keybinding);
417 static gboolean pspp_sheet_view_start_interactive_search      (PsppSheetView *tree_view);
418 static PsppSheetViewColumn *pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
419                                                          PsppSheetViewColumn *column,
420                                                          gint               drop_position);
421 static void
422 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
423                                   PsppSheetViewColumn  *column,
424                                   const GdkRectangle   *background_area,
425                                   gboolean              subtract_focus_rect,
426                                   GdkRectangle         *cell_area);
427 static gint pspp_sheet_view_find_offset (PsppSheetView *tree_view,
428                                          gint height,
429                                          int *new_node);
430
431 /* GtkBuildable */
432 static void pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
433                                                GtkBuilder  *builder,
434                                                GObject     *child,
435                                                const gchar *type);
436 static void pspp_sheet_view_buildable_init      (GtkBuildableIface *iface);
437
438
439 static gboolean scroll_row_timeout                   (gpointer     data);
440 static void     add_scroll_timeout                   (PsppSheetView *tree_view);
441 static void     remove_scroll_timeout                (PsppSheetView *tree_view);
442
443 static guint tree_view_signals [LAST_SIGNAL] = { 0 };
444
445 static GtkBindingSet *edit_bindings;
446
447 \f
448
449 /* GType Methods
450  */
451
452 G_DEFINE_TYPE_WITH_CODE (PsppSheetView, pspp_sheet_view, GTK_TYPE_CONTAINER,
453                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
454                                                 pspp_sheet_view_buildable_init))
455
456 static void
457 pspp_sheet_view_class_init (PsppSheetViewClass *class)
458 {
459   GObjectClass *o_class;
460   GtkWidgetClass *widget_class;
461   GtkContainerClass *container_class;
462   GtkBindingSet *binding_set[2];
463   int i;
464
465   binding_set[0] = gtk_binding_set_by_class (class);
466
467   binding_set[1] = gtk_binding_set_new ("PsppSheetViewEditing");
468   edit_bindings = binding_set[1];
469
470   o_class = (GObjectClass *) class;
471   widget_class = (GtkWidgetClass *) class;
472   container_class = (GtkContainerClass *) class;
473
474   /* GObject signals */
475   o_class->set_property = pspp_sheet_view_set_property;
476   o_class->get_property = pspp_sheet_view_get_property;
477   o_class->finalize = pspp_sheet_view_finalize;
478   o_class->dispose = pspp_sheet_view_dispose;
479
480   /* GtkWidget signals */
481   widget_class->map = pspp_sheet_view_map;
482   widget_class->realize = pspp_sheet_view_realize;
483   widget_class->unrealize = pspp_sheet_view_unrealize;
484   widget_class->size_request = pspp_sheet_view_size_request;
485   widget_class->size_allocate = pspp_sheet_view_size_allocate;
486   widget_class->button_press_event = pspp_sheet_view_button_press;
487   widget_class->button_release_event = pspp_sheet_view_button_release;
488   widget_class->grab_broken_event = pspp_sheet_view_grab_broken;
489   /*widget_class->configure_event = pspp_sheet_view_configure;*/
490   widget_class->motion_notify_event = pspp_sheet_view_motion;
491   widget_class->expose_event = pspp_sheet_view_expose;
492   widget_class->key_press_event = pspp_sheet_view_key_press;
493   widget_class->key_release_event = pspp_sheet_view_key_release;
494   widget_class->enter_notify_event = pspp_sheet_view_enter_notify;
495   widget_class->leave_notify_event = pspp_sheet_view_leave_notify;
496   widget_class->focus_out_event = pspp_sheet_view_focus_out;
497   widget_class->drag_begin = pspp_sheet_view_drag_begin;
498   widget_class->drag_end = pspp_sheet_view_drag_end;
499   widget_class->drag_data_get = pspp_sheet_view_drag_data_get;
500   widget_class->drag_data_delete = pspp_sheet_view_drag_data_delete;
501   widget_class->drag_leave = pspp_sheet_view_drag_leave;
502   widget_class->drag_motion = pspp_sheet_view_drag_motion;
503   widget_class->drag_drop = pspp_sheet_view_drag_drop;
504   widget_class->drag_data_received = pspp_sheet_view_drag_data_received;
505   widget_class->focus = pspp_sheet_view_focus;
506   widget_class->grab_focus = pspp_sheet_view_grab_focus;
507   widget_class->style_set = pspp_sheet_view_style_set;
508   widget_class->grab_notify = pspp_sheet_view_grab_notify;
509   widget_class->state_changed = pspp_sheet_view_state_changed;
510
511   /* GtkContainer signals */
512   container_class->remove = pspp_sheet_view_remove;
513   container_class->forall = pspp_sheet_view_forall;
514   container_class->set_focus_child = pspp_sheet_view_set_focus_child;
515
516   class->set_scroll_adjustments = pspp_sheet_view_set_adjustments;
517   class->move_cursor = pspp_sheet_view_real_move_cursor;
518   class->select_all = pspp_sheet_view_real_select_all;
519   class->unselect_all = pspp_sheet_view_real_unselect_all;
520   class->select_cursor_row = pspp_sheet_view_real_select_cursor_row;
521   class->toggle_cursor_row = pspp_sheet_view_real_toggle_cursor_row;
522   class->start_interactive_search = pspp_sheet_view_start_interactive_search;
523
524   /* Properties */
525
526   g_object_class_install_property (o_class,
527                                    PROP_MODEL,
528                                    g_param_spec_object ("model",
529                                                         P_("TreeView Model"),
530                                                         P_("The model for the tree view"),
531                                                         GTK_TYPE_TREE_MODEL,
532                                                         GTK_PARAM_READWRITE));
533
534   g_object_class_install_property (o_class,
535                                    PROP_HADJUSTMENT,
536                                    g_param_spec_object ("hadjustment",
537                                                         P_("Horizontal Adjustment"),
538                                                         P_("Horizontal Adjustment for the widget"),
539                                                         GTK_TYPE_ADJUSTMENT,
540                                                         GTK_PARAM_READWRITE));
541
542   g_object_class_install_property (o_class,
543                                    PROP_VADJUSTMENT,
544                                    g_param_spec_object ("vadjustment",
545                                                         P_("Vertical Adjustment"),
546                                                         P_("Vertical Adjustment for the widget"),
547                                                         GTK_TYPE_ADJUSTMENT,
548                                                         GTK_PARAM_READWRITE));
549
550   g_object_class_install_property (o_class,
551                                    PROP_HEADERS_VISIBLE,
552                                    g_param_spec_boolean ("headers-visible",
553                                                          P_("Headers Visible"),
554                                                          P_("Show the column header buttons"),
555                                                          TRUE,
556                                                          GTK_PARAM_READWRITE));
557
558   g_object_class_install_property (o_class,
559                                    PROP_HEADERS_CLICKABLE,
560                                    g_param_spec_boolean ("headers-clickable",
561                                                          P_("Headers Clickable"),
562                                                          P_("Column headers respond to click events"),
563                                                          TRUE,
564                                                          GTK_PARAM_READWRITE));
565
566   g_object_class_install_property (o_class,
567                                    PROP_REORDERABLE,
568                                    g_param_spec_boolean ("reorderable",
569                                                          P_("Reorderable"),
570                                                          P_("View is reorderable"),
571                                                          FALSE,
572                                                          GTK_PARAM_READWRITE));
573
574   g_object_class_install_property (o_class,
575                                    PROP_RULES_HINT,
576                                    g_param_spec_boolean ("rules-hint",
577                                                          P_("Rules Hint"),
578                                                          P_("Set a hint to the theme engine to draw rows in alternating colors"),
579                                                          FALSE,
580                                                          GTK_PARAM_READWRITE));
581
582     g_object_class_install_property (o_class,
583                                      PROP_ENABLE_SEARCH,
584                                      g_param_spec_boolean ("enable-search",
585                                                            P_("Enable Search"),
586                                                            P_("View allows user to search through columns interactively"),
587                                                            TRUE,
588                                                            GTK_PARAM_READWRITE));
589
590     g_object_class_install_property (o_class,
591                                      PROP_SEARCH_COLUMN,
592                                      g_param_spec_int ("search-column",
593                                                        P_("Search Column"),
594                                                        P_("Model column to search through during interactive search"),
595                                                        -1,
596                                                        G_MAXINT,
597                                                        -1,
598                                                        GTK_PARAM_READWRITE));
599
600     /**
601      * PsppSheetView:hover-selection:
602      * 
603      * Enables of disables the hover selection mode of @tree_view.
604      * Hover selection makes the selected row follow the pointer.
605      * Currently, this works only for the selection modes 
606      * %PSPP_SHEET_SELECTION_SINGLE and %PSPP_SHEET_SELECTION_BROWSE.
607      *
608      * This mode is primarily intended for treeviews in popups, e.g.
609      * in #GtkComboBox or #GtkEntryCompletion.
610      *
611      * Since: 2.6
612      */
613     g_object_class_install_property (o_class,
614                                      PROP_HOVER_SELECTION,
615                                      g_param_spec_boolean ("hover-selection",
616                                                            P_("Hover Selection"),
617                                                            P_("Whether the selection should follow the pointer"),
618                                                            FALSE,
619                                                            GTK_PARAM_READWRITE));
620
621     g_object_class_install_property (o_class,
622                                      PROP_RUBBER_BANDING,
623                                      g_param_spec_boolean ("rubber-banding",
624                                                            P_("Rubber Banding"),
625                                                            P_("Whether to enable selection of multiple items by dragging the mouse pointer"),
626                                                            FALSE,
627                                                            GTK_PARAM_READWRITE));
628
629     g_object_class_install_property (o_class,
630                                      PROP_ENABLE_GRID_LINES,
631                                      g_param_spec_enum ("enable-grid-lines",
632                                                         P_("Enable Grid Lines"),
633                                                         P_("Whether grid lines should be drawn in the tree view"),
634                                                         PSPP_TYPE_SHEET_VIEW_GRID_LINES,
635                                                         PSPP_SHEET_VIEW_GRID_LINES_NONE,
636                                                         GTK_PARAM_READWRITE));
637
638     g_object_class_install_property (o_class,
639                                      PROP_TOOLTIP_COLUMN,
640                                      g_param_spec_int ("tooltip-column",
641                                                        P_("Tooltip Column"),
642                                                        P_("The column in the model containing the tooltip texts for the rows"),
643                                                        -1,
644                                                        G_MAXINT,
645                                                        -1,
646                                                        GTK_PARAM_READWRITE));
647
648     g_object_class_install_property (o_class,
649                                      PROP_SPECIAL_CELLS,
650                                      g_param_spec_enum ("special-cells",
651                                                         P_("Special Cells"),
652                                                         P_("Whether rows have special cells."),
653                                                         PSPP_TYPE_SHEET_VIEW_SPECIAL_CELLS,
654                                                         PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT,
655                                                         GTK_PARAM_READWRITE));
656
657     g_object_class_install_property (o_class,
658                                      PROP_FIXED_HEIGHT,
659                                      g_param_spec_int ("fixed-height",
660                                                        P_("Fixed Height"),
661                                                        P_("Height of a single row.  Normally the height of a row is determined automatically.  Writing this property sets fixed-height-set to true, preventing this property's value from changing."),
662                                                        -1,
663                                                        G_MAXINT,
664                                                        -1,
665                                                        GTK_PARAM_READWRITE));
666
667     g_object_class_install_property (o_class,
668                                      PROP_FIXED_HEIGHT_SET,
669                                      g_param_spec_boolean ("fixed-height-set",
670                                                            P_("Fixed Height Set"),
671                                                            P_("Whether fixed-height was set externally."),
672                                                            FALSE,
673                                                            GTK_PARAM_READWRITE));
674
675   /* Style properties */
676 #define _TREE_VIEW_EXPANDER_SIZE 12
677 #define _TREE_VIEW_VERTICAL_SEPARATOR 2
678 #define _TREE_VIEW_HORIZONTAL_SEPARATOR 2
679
680   gtk_widget_class_install_style_property (widget_class,
681                                            g_param_spec_int ("expander-size",
682                                                              P_("Expander Size"),
683                                                              P_("Size of the expander arrow"),
684                                                              0,
685                                                              G_MAXINT,
686                                                              _TREE_VIEW_EXPANDER_SIZE,
687                                                              GTK_PARAM_READABLE));
688
689   gtk_widget_class_install_style_property (widget_class,
690                                            g_param_spec_int ("vertical-separator",
691                                                              P_("Vertical Separator Width"),
692                                                              P_("Vertical space between cells.  Must be an even number"),
693                                                              0,
694                                                              G_MAXINT,
695                                                              _TREE_VIEW_VERTICAL_SEPARATOR,
696                                                              GTK_PARAM_READABLE));
697
698   gtk_widget_class_install_style_property (widget_class,
699                                            g_param_spec_int ("horizontal-separator",
700                                                              P_("Horizontal Separator Width"),
701                                                              P_("Horizontal space between cells.  Must be an even number"),
702                                                              0,
703                                                              G_MAXINT,
704                                                              _TREE_VIEW_HORIZONTAL_SEPARATOR,
705                                                              GTK_PARAM_READABLE));
706
707   gtk_widget_class_install_style_property (widget_class,
708                                            g_param_spec_boolean ("allow-rules",
709                                                                  P_("Allow Rules"),
710                                                                  P_("Allow drawing of alternating color rows"),
711                                                                  TRUE,
712                                                                  GTK_PARAM_READABLE));
713
714   gtk_widget_class_install_style_property (widget_class,
715                                            g_param_spec_boxed ("even-row-color",
716                                                                P_("Even Row Color"),
717                                                                P_("Color to use for even rows"),
718                                                                GDK_TYPE_COLOR,
719                                                                GTK_PARAM_READABLE));
720
721   gtk_widget_class_install_style_property (widget_class,
722                                            g_param_spec_boxed ("odd-row-color",
723                                                                P_("Odd Row Color"),
724                                                                P_("Color to use for odd rows"),
725                                                                GDK_TYPE_COLOR,
726                                                                GTK_PARAM_READABLE));
727
728   gtk_widget_class_install_style_property (widget_class,
729                                            g_param_spec_boolean ("row-ending-details",
730                                                                  P_("Row Ending details"),
731                                                                  P_("Enable extended row background theming"),
732                                                                  FALSE,
733                                                                  GTK_PARAM_READABLE));
734
735   gtk_widget_class_install_style_property (widget_class,
736                                            g_param_spec_int ("grid-line-width",
737                                                              P_("Grid line width"),
738                                                              P_("Width, in pixels, of the tree view grid lines"),
739                                                              0, G_MAXINT, 1,
740                                                              GTK_PARAM_READABLE));
741
742   gtk_widget_class_install_style_property (widget_class,
743                                            g_param_spec_int ("tree-line-width",
744                                                              P_("Tree line width"),
745                                                              P_("Width, in pixels, of the tree view lines"),
746                                                              0, G_MAXINT, 1,
747                                                              GTK_PARAM_READABLE));
748
749   gtk_widget_class_install_style_property (widget_class,
750                                            g_param_spec_string ("tree-line-pattern",
751                                                                 P_("Tree line pattern"),
752                                                                 P_("Dash pattern used to draw the tree view lines"),
753                                                                 "\1\1",
754                                                                 GTK_PARAM_READABLE));
755
756   /* Signals */
757   /**
758    * PsppSheetView::set-scroll-adjustments
759    * @horizontal: the horizontal #GtkAdjustment
760    * @vertical: the vertical #GtkAdjustment
761    *
762    * Set the scroll adjustments for the tree view. Usually scrolled containers
763    * like #GtkScrolledWindow will emit this signal to connect two instances
764    * of #GtkScrollbar to the scroll directions of the #PsppSheetView.
765    */
766   widget_class->set_scroll_adjustments_signal =
767     g_signal_new ("set-scroll-adjustments",
768                   G_TYPE_FROM_CLASS (o_class),
769                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
770                   G_STRUCT_OFFSET (PsppSheetViewClass, set_scroll_adjustments),
771                   NULL, NULL,
772                   psppire_marshal_VOID__OBJECT_OBJECT,
773                   G_TYPE_NONE, 2,
774                   GTK_TYPE_ADJUSTMENT,
775                   GTK_TYPE_ADJUSTMENT);
776
777   /**
778    * PsppSheetView::row-activated:
779    * @tree_view: the object on which the signal is emitted
780    * @path: the #GtkTreePath for the activated row
781    * @column: the #PsppSheetViewColumn in which the activation occurred
782    *
783    * The "row-activated" signal is emitted when the method
784    * pspp_sheet_view_row_activated() is called or the user double clicks 
785    * a treeview row. It is also emitted when a non-editable row is 
786    * selected and one of the keys: Space, Shift+Space, Return or 
787    * Enter is pressed.
788    * 
789    * For selection handling refer to the <link linkend="TreeWidget">tree 
790    * widget conceptual overview</link> as well as #PsppSheetSelection.
791    */
792   tree_view_signals[ROW_ACTIVATED] =
793     g_signal_new ("row-activated",
794                   G_TYPE_FROM_CLASS (o_class),
795                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
796                   G_STRUCT_OFFSET (PsppSheetViewClass, row_activated),
797                   NULL, NULL,
798                   psppire_marshal_VOID__BOXED_OBJECT,
799                   G_TYPE_NONE, 2,
800                   GTK_TYPE_TREE_PATH,
801                   PSPP_TYPE_SHEET_VIEW_COLUMN);
802
803   /**
804    * PsppSheetView::columns-changed:
805    * @tree_view: the object on which the signal is emitted 
806    * 
807    * The number of columns of the treeview has changed.
808    */
809   tree_view_signals[COLUMNS_CHANGED] =
810     g_signal_new ("columns-changed",
811                   G_TYPE_FROM_CLASS (o_class),
812                   G_SIGNAL_RUN_LAST,
813                   G_STRUCT_OFFSET (PsppSheetViewClass, columns_changed),
814                   NULL, NULL,
815                   g_cclosure_marshal_VOID__VOID,
816                   G_TYPE_NONE, 0);
817
818   /**
819    * PsppSheetView::cursor-changed:
820    * @tree_view: the object on which the signal is emitted
821    * 
822    * The position of the cursor (focused cell) has changed.
823    */
824   tree_view_signals[CURSOR_CHANGED] =
825     g_signal_new ("cursor-changed",
826                   G_TYPE_FROM_CLASS (o_class),
827                   G_SIGNAL_RUN_LAST,
828                   G_STRUCT_OFFSET (PsppSheetViewClass, cursor_changed),
829                   NULL, NULL,
830                   g_cclosure_marshal_VOID__VOID,
831                   G_TYPE_NONE, 0);
832
833   tree_view_signals[MOVE_CURSOR] =
834     g_signal_new ("move-cursor",
835                   G_TYPE_FROM_CLASS (o_class),
836                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
837                   G_STRUCT_OFFSET (PsppSheetViewClass, move_cursor),
838                   NULL, NULL,
839                   psppire_marshal_BOOLEAN__ENUM_INT,
840                   G_TYPE_BOOLEAN, 2,
841                   GTK_TYPE_MOVEMENT_STEP,
842                   G_TYPE_INT);
843
844   tree_view_signals[SELECT_ALL] =
845     g_signal_new ("select-all",
846                   G_TYPE_FROM_CLASS (o_class),
847                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
848                   G_STRUCT_OFFSET (PsppSheetViewClass, select_all),
849                   NULL, NULL,
850                   psppire_marshal_BOOLEAN__VOID,
851                   G_TYPE_BOOLEAN, 0);
852
853   tree_view_signals[UNSELECT_ALL] =
854     g_signal_new ("unselect-all",
855                   G_TYPE_FROM_CLASS (o_class),
856                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
857                   G_STRUCT_OFFSET (PsppSheetViewClass, unselect_all),
858                   NULL, NULL,
859                   psppire_marshal_BOOLEAN__VOID,
860                   G_TYPE_BOOLEAN, 0);
861
862   tree_view_signals[SELECT_CURSOR_ROW] =
863     g_signal_new ("select-cursor-row",
864                   G_TYPE_FROM_CLASS (o_class),
865                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
866                   G_STRUCT_OFFSET (PsppSheetViewClass, select_cursor_row),
867                   NULL, NULL,
868                   psppire_marshal_BOOLEAN__BOOLEAN,
869                   G_TYPE_BOOLEAN, 2,
870                   G_TYPE_BOOLEAN, G_TYPE_INT);
871
872   tree_view_signals[TOGGLE_CURSOR_ROW] =
873     g_signal_new ("toggle-cursor-row",
874                   G_TYPE_FROM_CLASS (o_class),
875                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
876                   G_STRUCT_OFFSET (PsppSheetViewClass, toggle_cursor_row),
877                   NULL, NULL,
878                   psppire_marshal_BOOLEAN__VOID,
879                   G_TYPE_BOOLEAN, 0);
880
881   tree_view_signals[START_INTERACTIVE_SEARCH] =
882     g_signal_new ("start-interactive-search",
883                   G_TYPE_FROM_CLASS (o_class),
884                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
885                   G_STRUCT_OFFSET (PsppSheetViewClass, start_interactive_search),
886                   NULL, NULL,
887                   psppire_marshal_BOOLEAN__VOID,
888                   G_TYPE_BOOLEAN, 0);
889
890   /* Key bindings */
891   for (i = 0; i < 2; i++)
892     {
893       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Up, 0, TRUE,
894                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
895       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Up, 0, TRUE,
896                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
897
898       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Down, 0, TRUE,
899                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
900       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Down, 0, TRUE,
901                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
902
903       pspp_sheet_view_add_move_binding (binding_set[i], GDK_p, GDK_CONTROL_MASK, FALSE,
904                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
905
906       pspp_sheet_view_add_move_binding (binding_set[i], GDK_n, GDK_CONTROL_MASK, FALSE,
907                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
908
909       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Home, 0, TRUE,
910                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
911       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Home, 0, TRUE,
912                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
913
914       pspp_sheet_view_add_move_binding (binding_set[i], GDK_End, 0, TRUE,
915                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
916       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_End, 0, TRUE,
917                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
918
919       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Page_Up, 0, TRUE,
920                                         GTK_MOVEMENT_PAGES, -1);
921       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Page_Up, 0, TRUE,
922                                         GTK_MOVEMENT_PAGES, -1);
923
924       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Page_Down, 0, TRUE,
925                                         GTK_MOVEMENT_PAGES, 1);
926       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Page_Down, 0, TRUE,
927                                         GTK_MOVEMENT_PAGES, 1);
928
929
930       gtk_binding_entry_add_signal (binding_set[i], GDK_Up, GDK_CONTROL_MASK, "move-cursor", 2,
931                                     G_TYPE_ENUM, GTK_MOVEMENT_BUFFER_ENDS,
932                                     G_TYPE_INT, -1);
933
934       gtk_binding_entry_add_signal (binding_set[i], GDK_Down, GDK_CONTROL_MASK, "move-cursor", 2,
935                                     G_TYPE_ENUM, GTK_MOVEMENT_BUFFER_ENDS,
936                                     G_TYPE_INT, 1);
937
938       gtk_binding_entry_add_signal (binding_set[i], GDK_Right, 0, "move-cursor", 2,
939                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
940                                     G_TYPE_INT, 1);
941
942       gtk_binding_entry_add_signal (binding_set[i], GDK_Left, 0, "move-cursor", 2,
943                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
944                                     G_TYPE_INT, -1);
945
946       gtk_binding_entry_add_signal (binding_set[i], GDK_Tab, 0, "move-cursor", 2,
947                                     G_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
948                                     G_TYPE_INT, 1);
949
950       gtk_binding_entry_add_signal (binding_set[i], GDK_Tab, GDK_SHIFT_MASK, "move-cursor", 2,
951                                     G_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
952                                     G_TYPE_INT, -1);
953
954       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Right, 0, "move-cursor", 2,
955                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
956                                     G_TYPE_INT, 1);
957
958       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Left, 0, "move-cursor", 2,
959                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
960                                     G_TYPE_INT, -1);
961
962       gtk_binding_entry_add_signal (binding_set[i], GDK_Right, GDK_CONTROL_MASK,
963                                     "move-cursor", 2,
964                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
965                                     G_TYPE_INT, 1);
966
967       gtk_binding_entry_add_signal (binding_set[i], GDK_Left, GDK_CONTROL_MASK,
968                                     "move-cursor", 2,
969                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
970                                     G_TYPE_INT, -1);
971
972       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Right, GDK_CONTROL_MASK,
973                                     "move-cursor", 2,
974                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
975                                     G_TYPE_INT, 1);
976
977       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Left, GDK_CONTROL_MASK,
978                                     "move-cursor", 2,
979                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
980                                     G_TYPE_INT, -1);
981
982       gtk_binding_entry_add_signal (binding_set[i], GDK_f, GDK_CONTROL_MASK, "start-interactive-search", 0);
983
984       gtk_binding_entry_add_signal (binding_set[i], GDK_F, GDK_CONTROL_MASK, "start-interactive-search", 0);
985     }
986
987   gtk_binding_entry_add_signal (binding_set[0], GDK_space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
988   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
989
990   gtk_binding_entry_add_signal (binding_set[0], GDK_a, GDK_CONTROL_MASK, "select-all", 0);
991   gtk_binding_entry_add_signal (binding_set[0], GDK_slash, GDK_CONTROL_MASK, "select-all", 0);
992
993   gtk_binding_entry_add_signal (binding_set[0], GDK_A, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "unselect-all", 0);
994   gtk_binding_entry_add_signal (binding_set[0], GDK_backslash, GDK_CONTROL_MASK, "unselect-all", 0);
995
996   gtk_binding_entry_add_signal (binding_set[0], GDK_space, GDK_SHIFT_MASK, "select-cursor-row", 1,
997                                 G_TYPE_BOOLEAN, TRUE,
998                                 G_TYPE_INT, PSPP_SHEET_SELECT_MODE_EXTEND);
999   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, GDK_SHIFT_MASK, "select-cursor-row", 1,
1000                                 G_TYPE_BOOLEAN, TRUE,
1001                                 G_TYPE_INT, PSPP_SHEET_SELECT_MODE_EXTEND);
1002
1003   gtk_binding_entry_add_signal (binding_set[0], GDK_space, 0, "select-cursor-row", 1,
1004                                 G_TYPE_BOOLEAN, TRUE,
1005                                 G_TYPE_INT, 0);
1006   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, 0, "select-cursor-row", 1,
1007                                 G_TYPE_BOOLEAN, TRUE,
1008                                 G_TYPE_INT, 0);
1009   gtk_binding_entry_add_signal (binding_set[0], GDK_Return, 0, "select-cursor-row", 1,
1010                                 G_TYPE_BOOLEAN, TRUE,
1011                                 G_TYPE_INT, 0);
1012   gtk_binding_entry_add_signal (binding_set[0], GDK_ISO_Enter, 0, "select-cursor-row", 1,
1013                                 G_TYPE_BOOLEAN, TRUE,
1014                                 G_TYPE_INT, 0);
1015   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Enter, 0, "select-cursor-row", 1,
1016                                 G_TYPE_BOOLEAN, TRUE,
1017                                 G_TYPE_INT, 0);
1018
1019   gtk_binding_entry_add_signal (binding_set[0], GDK_BackSpace, 0, "select-cursor-parent", 0);
1020   gtk_binding_entry_add_signal (binding_set[0], GDK_BackSpace, GDK_CONTROL_MASK, "select-cursor-parent", 0);
1021
1022   g_type_class_add_private (o_class, sizeof (PsppSheetViewPrivate));
1023 }
1024
1025 static void
1026 pspp_sheet_view_buildable_init (GtkBuildableIface *iface)
1027 {
1028   iface->add_child = pspp_sheet_view_buildable_add_child;
1029 }
1030
1031 static void
1032 pspp_sheet_view_init (PsppSheetView *tree_view)
1033 {
1034   tree_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (tree_view, PSPP_TYPE_SHEET_VIEW, PsppSheetViewPrivate);
1035
1036   gtk_widget_set_can_focus (GTK_WIDGET (tree_view), TRUE);
1037   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (tree_view), FALSE);
1038
1039   tree_view->priv->flags =  PSPP_SHEET_VIEW_DRAW_KEYFOCUS
1040                             | PSPP_SHEET_VIEW_HEADERS_VISIBLE;
1041
1042   /* We need some padding */
1043   tree_view->priv->selected = range_tower_create ();
1044   tree_view->priv->dy = 0;
1045   tree_view->priv->cursor_offset = 0;
1046   tree_view->priv->n_columns = 0;
1047   tree_view->priv->header_height = 1;
1048   tree_view->priv->x_drag = 0;
1049   tree_view->priv->drag_pos = -1;
1050   tree_view->priv->header_has_focus = FALSE;
1051   tree_view->priv->pressed_button = -1;
1052   tree_view->priv->press_start_x = -1;
1053   tree_view->priv->press_start_y = -1;
1054   tree_view->priv->reorderable = FALSE;
1055   tree_view->priv->presize_handler_timer = 0;
1056   tree_view->priv->scroll_sync_timer = 0;
1057   tree_view->priv->fixed_height = -1;
1058   tree_view->priv->fixed_height_set = FALSE;
1059   pspp_sheet_view_set_adjustments (tree_view, NULL, NULL);
1060   tree_view->priv->selection = _pspp_sheet_selection_new_with_tree_view (tree_view);
1061   tree_view->priv->enable_search = TRUE;
1062   tree_view->priv->search_column = -1;
1063   tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
1064   tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
1065   tree_view->priv->search_custom_entry_set = FALSE;
1066   tree_view->priv->typeselect_flush_timeout = 0;
1067   tree_view->priv->init_hadjust_value = TRUE;    
1068   tree_view->priv->width = 0;
1069           
1070   tree_view->priv->hover_selection = FALSE;
1071
1072   tree_view->priv->rubber_banding_enable = FALSE;
1073
1074   tree_view->priv->grid_lines = PSPP_SHEET_VIEW_GRID_LINES_NONE;
1075
1076   tree_view->priv->tooltip_column = -1;
1077
1078   tree_view->priv->special_cells = PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT;
1079
1080   tree_view->priv->post_validation_flag = FALSE;
1081
1082   tree_view->priv->last_button_x = -1;
1083   tree_view->priv->last_button_y = -1;
1084
1085   tree_view->priv->event_last_x = -10000;
1086   tree_view->priv->event_last_y = -10000;
1087
1088   tree_view->priv->prelight_node = -1;
1089   tree_view->priv->rubber_band_start_node = -1;
1090   tree_view->priv->rubber_band_end_node = -1;
1091
1092   tree_view->priv->anchor_column = NULL;
1093
1094   tree_view->priv->button_style = NULL;
1095
1096   tree_view->dispose_has_run = FALSE;
1097 }
1098
1099 \f
1100
1101 /* GObject Methods
1102  */
1103
1104 static void
1105 pspp_sheet_view_set_property (GObject         *object,
1106                             guint            prop_id,
1107                             const GValue    *value,
1108                             GParamSpec      *pspec)
1109 {
1110   PsppSheetView *tree_view;
1111
1112   tree_view = PSPP_SHEET_VIEW (object);
1113
1114   switch (prop_id)
1115     {
1116     case PROP_MODEL:
1117       pspp_sheet_view_set_model (tree_view, g_value_get_object (value));
1118       break;
1119     case PROP_HADJUSTMENT:
1120       pspp_sheet_view_set_hadjustment (tree_view, g_value_get_object (value));
1121       break;
1122     case PROP_VADJUSTMENT:
1123       pspp_sheet_view_set_vadjustment (tree_view, g_value_get_object (value));
1124       break;
1125     case PROP_HEADERS_VISIBLE:
1126       pspp_sheet_view_set_headers_visible (tree_view, g_value_get_boolean (value));
1127       break;
1128     case PROP_HEADERS_CLICKABLE:
1129       pspp_sheet_view_set_headers_clickable (tree_view, g_value_get_boolean (value));
1130       break;
1131     case PROP_REORDERABLE:
1132       pspp_sheet_view_set_reorderable (tree_view, g_value_get_boolean (value));
1133       break;
1134     case PROP_RULES_HINT:
1135       pspp_sheet_view_set_rules_hint (tree_view, g_value_get_boolean (value));
1136       break;
1137     case PROP_ENABLE_SEARCH:
1138       pspp_sheet_view_set_enable_search (tree_view, g_value_get_boolean (value));
1139       break;
1140     case PROP_SEARCH_COLUMN:
1141       pspp_sheet_view_set_search_column (tree_view, g_value_get_int (value));
1142       break;
1143     case PROP_HOVER_SELECTION:
1144       tree_view->priv->hover_selection = g_value_get_boolean (value);
1145       break;
1146     case PROP_RUBBER_BANDING:
1147       tree_view->priv->rubber_banding_enable = g_value_get_boolean (value);
1148       break;
1149     case PROP_ENABLE_GRID_LINES:
1150       pspp_sheet_view_set_grid_lines (tree_view, g_value_get_enum (value));
1151       break;
1152     case PROP_TOOLTIP_COLUMN:
1153       pspp_sheet_view_set_tooltip_column (tree_view, g_value_get_int (value));
1154       break;
1155     case PROP_SPECIAL_CELLS:
1156       pspp_sheet_view_set_special_cells (tree_view, g_value_get_enum (value));
1157       break;
1158     case PROP_FIXED_HEIGHT:
1159       pspp_sheet_view_set_fixed_height (tree_view, g_value_get_int (value));
1160       break;
1161     case PROP_FIXED_HEIGHT_SET:
1162       if (g_value_get_boolean (value))
1163         {
1164           if (!tree_view->priv->fixed_height_set
1165               && tree_view->priv->fixed_height >= 0)
1166             {
1167               tree_view->priv->fixed_height_set = true;
1168               g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
1169             }
1170         }
1171       else
1172         {
1173           if (tree_view->priv->fixed_height_set)
1174             {
1175               tree_view->priv->fixed_height_set = false;
1176               g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
1177               install_presize_handler (tree_view);
1178             }
1179         }
1180       break;
1181     default:
1182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1183       break;
1184     }
1185 }
1186
1187 static void
1188 pspp_sheet_view_get_property (GObject    *object,
1189                             guint       prop_id,
1190                             GValue     *value,
1191                             GParamSpec *pspec)
1192 {
1193   PsppSheetView *tree_view;
1194
1195   tree_view = PSPP_SHEET_VIEW (object);
1196
1197   switch (prop_id)
1198     {
1199     case PROP_MODEL:
1200       g_value_set_object (value, tree_view->priv->model);
1201       break;
1202     case PROP_HADJUSTMENT:
1203       g_value_set_object (value, tree_view->priv->hadjustment);
1204       break;
1205     case PROP_VADJUSTMENT:
1206       g_value_set_object (value, tree_view->priv->vadjustment);
1207       break;
1208     case PROP_HEADERS_VISIBLE:
1209       g_value_set_boolean (value, pspp_sheet_view_get_headers_visible (tree_view));
1210       break;
1211     case PROP_HEADERS_CLICKABLE:
1212       g_value_set_boolean (value, pspp_sheet_view_get_headers_clickable (tree_view));
1213       break;
1214     case PROP_REORDERABLE:
1215       g_value_set_boolean (value, tree_view->priv->reorderable);
1216       break;
1217     case PROP_RULES_HINT:
1218       g_value_set_boolean (value, tree_view->priv->has_rules);
1219       break;
1220     case PROP_ENABLE_SEARCH:
1221       g_value_set_boolean (value, tree_view->priv->enable_search);
1222       break;
1223     case PROP_SEARCH_COLUMN:
1224       g_value_set_int (value, tree_view->priv->search_column);
1225       break;
1226     case PROP_HOVER_SELECTION:
1227       g_value_set_boolean (value, tree_view->priv->hover_selection);
1228       break;
1229     case PROP_RUBBER_BANDING:
1230       g_value_set_boolean (value, tree_view->priv->rubber_banding_enable);
1231       break;
1232     case PROP_ENABLE_GRID_LINES:
1233       g_value_set_enum (value, tree_view->priv->grid_lines);
1234       break;
1235     case PROP_TOOLTIP_COLUMN:
1236       g_value_set_int (value, tree_view->priv->tooltip_column);
1237       break;
1238     case PROP_SPECIAL_CELLS:
1239       g_value_set_enum (value, tree_view->priv->special_cells);
1240       break;
1241     case PROP_FIXED_HEIGHT:
1242       g_value_set_int (value, pspp_sheet_view_get_fixed_height (tree_view));
1243       break;
1244     case PROP_FIXED_HEIGHT_SET:
1245       g_value_set_boolean (value, tree_view->priv->fixed_height_set);
1246       break;
1247     default:
1248       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1249       break;
1250     }
1251 }
1252
1253 static void
1254 pspp_sheet_view_dispose (GObject *object)
1255 {
1256   PsppSheetView *tree_view = PSPP_SHEET_VIEW (object);
1257
1258   if (tree_view->dispose_has_run)
1259     return;
1260
1261   tree_view->dispose_has_run = TRUE;
1262
1263   if (tree_view->priv->selection != NULL)
1264     {
1265       _pspp_sheet_selection_set_tree_view (tree_view->priv->selection, NULL);
1266       g_object_unref (tree_view->priv->selection);
1267       tree_view->priv->selection = NULL;
1268     }
1269
1270   if (tree_view->priv->hadjustment)
1271     {
1272       g_object_unref (tree_view->priv->hadjustment);
1273       tree_view->priv->hadjustment = NULL;
1274     }
1275   if (tree_view->priv->vadjustment)
1276     {
1277       g_object_unref (tree_view->priv->vadjustment);
1278       tree_view->priv->vadjustment = NULL;
1279     }
1280
1281   if (tree_view->priv->button_style)
1282     {
1283       g_object_unref (tree_view->priv->button_style);
1284       tree_view->priv->button_style = NULL;
1285     }
1286
1287
1288   G_OBJECT_CLASS (pspp_sheet_view_parent_class)->dispose (object);
1289 }
1290
1291 \f
1292
1293 static void
1294 pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
1295                                    GtkBuilder  *builder,
1296                                    GObject     *child,
1297                                    const gchar *type)
1298 {
1299   pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), PSPP_SHEET_VIEW_COLUMN (child));
1300 }
1301
1302 static void
1303 pspp_sheet_view_finalize (GObject *object)
1304 {
1305   PsppSheetView *tree_view = PSPP_SHEET_VIEW (object);
1306
1307   pspp_sheet_view_stop_editing (tree_view, TRUE);
1308
1309   if (tree_view->priv->selected != NULL)
1310     {
1311       range_tower_destroy (tree_view->priv->selected);
1312       tree_view->priv->selected = NULL;
1313     }
1314
1315
1316   tree_view->priv->prelight_node = -1;
1317
1318
1319   if (tree_view->priv->scroll_to_path != NULL)
1320     {
1321       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
1322       tree_view->priv->scroll_to_path = NULL;
1323     }
1324
1325   if (tree_view->priv->drag_dest_row != NULL)
1326     {
1327       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
1328       tree_view->priv->drag_dest_row = NULL;
1329     }
1330
1331   if (tree_view->priv->top_row != NULL)
1332     {
1333       gtk_tree_row_reference_free (tree_view->priv->top_row);
1334       tree_view->priv->top_row = NULL;
1335     }
1336
1337   if (tree_view->priv->column_drop_func_data &&
1338       tree_view->priv->column_drop_func_data_destroy)
1339     {
1340       tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
1341       tree_view->priv->column_drop_func_data = NULL;
1342     }
1343
1344   if (tree_view->priv->destroy_count_destroy &&
1345       tree_view->priv->destroy_count_data)
1346     {
1347       tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
1348       tree_view->priv->destroy_count_data = NULL;
1349     }
1350
1351   gtk_tree_row_reference_free (tree_view->priv->cursor);
1352   tree_view->priv->cursor = NULL;
1353
1354   gtk_tree_row_reference_free (tree_view->priv->anchor);
1355   tree_view->priv->anchor = NULL;
1356
1357   /* destroy interactive search dialog */
1358   if (tree_view->priv->search_window)
1359     {
1360       gtk_widget_destroy (tree_view->priv->search_window);
1361       tree_view->priv->search_window = NULL;
1362       tree_view->priv->search_entry = NULL;
1363       if (tree_view->priv->typeselect_flush_timeout)
1364         {
1365           g_source_remove (tree_view->priv->typeselect_flush_timeout);
1366           tree_view->priv->typeselect_flush_timeout = 0;
1367         }
1368     }
1369
1370   if (tree_view->priv->search_destroy && tree_view->priv->search_user_data)
1371     {
1372       tree_view->priv->search_destroy (tree_view->priv->search_user_data);
1373       tree_view->priv->search_user_data = NULL;
1374     }
1375
1376   if (tree_view->priv->search_position_destroy && tree_view->priv->search_position_user_data)
1377     {
1378       tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
1379       tree_view->priv->search_position_user_data = NULL;
1380     }
1381
1382   pspp_sheet_view_set_model (tree_view, NULL);
1383
1384
1385   G_OBJECT_CLASS (pspp_sheet_view_parent_class)->finalize (object);
1386 }
1387
1388 \f
1389
1390 /* GtkWidget Methods
1391  */
1392
1393 /* GtkWidget::map helper */
1394 static void
1395 pspp_sheet_view_map_buttons (PsppSheetView *tree_view)
1396 {
1397   GList *list;
1398
1399   g_return_if_fail (gtk_widget_get_mapped (GTK_WIDGET (tree_view)));
1400
1401   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
1402     {
1403       PsppSheetViewColumn *column;
1404
1405       for (list = tree_view->priv->columns; list; list = list->next)
1406         {
1407           column = list->data;
1408           if (column->button != NULL &&
1409               gtk_widget_get_visible (column->button) &&
1410               !gtk_widget_get_mapped (column->button))
1411             gtk_widget_map (column->button);
1412         }
1413       for (list = tree_view->priv->columns; list; list = list->next)
1414         {
1415           column = list->data;
1416           if (column->visible == FALSE || column->window == NULL)
1417             continue;
1418           if (column->resizable)
1419             {
1420               gdk_window_raise (column->window);
1421               gdk_window_show (column->window);
1422             }
1423           else
1424             gdk_window_hide (column->window);
1425         }
1426       gdk_window_show (tree_view->priv->header_window);
1427     }
1428 }
1429
1430 static void
1431 pspp_sheet_view_map (GtkWidget *widget)
1432 {
1433   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1434   GList *tmp_list;
1435
1436   gtk_widget_set_mapped (widget, TRUE);
1437
1438   tmp_list = tree_view->priv->children;
1439   while (tmp_list)
1440     {
1441       PsppSheetViewChild *child = tmp_list->data;
1442       tmp_list = tmp_list->next;
1443
1444       if (gtk_widget_get_visible (child->widget))
1445         {
1446           if (!gtk_widget_get_mapped (child->widget))
1447             gtk_widget_map (child->widget);
1448         }
1449     }
1450   gdk_window_show (tree_view->priv->bin_window);
1451
1452   pspp_sheet_view_map_buttons (tree_view);
1453
1454   gdk_window_show (gtk_widget_get_window (widget));
1455 }
1456
1457 static void
1458 pspp_sheet_view_realize (GtkWidget *widget)
1459 {
1460   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1461   GList *tmp_list;
1462   GdkWindowAttr attributes;
1463   gint attributes_mask;
1464   GtkAllocation allocation;
1465   GtkAllocation old_allocation;
1466
1467   gtk_widget_set_realized (widget, TRUE);
1468
1469   gtk_widget_get_allocation (widget, &allocation);
1470   gtk_widget_get_allocation (widget, &old_allocation);
1471
1472   /* Make the main, clipping window */
1473   attributes.window_type = GDK_WINDOW_CHILD;
1474   attributes.x =      allocation.x;
1475   attributes.y =      allocation.y;
1476   attributes.width =  allocation.width;
1477   attributes.height = allocation.height;
1478   attributes.wclass = GDK_INPUT_OUTPUT;
1479   attributes.visual = gtk_widget_get_visual (widget);
1480   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1481
1482   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1483
1484   gtk_widget_set_window (widget,
1485                          gdk_window_new (gtk_widget_get_parent_window (widget),
1486                                          &attributes, attributes_mask));
1487   gdk_window_set_user_data (gtk_widget_get_window (widget), widget);
1488
1489   /* Make the window for the tree */
1490   attributes.x = 0;
1491   attributes.y = TREE_VIEW_HEADER_HEIGHT (tree_view);
1492   attributes.width = MAX (tree_view->priv->width, old_allocation.width);
1493   attributes.height = old_allocation.height;
1494   attributes.event_mask = (GDK_EXPOSURE_MASK |
1495                            GDK_SCROLL_MASK |
1496                            GDK_POINTER_MOTION_MASK |
1497                            GDK_ENTER_NOTIFY_MASK |
1498                            GDK_LEAVE_NOTIFY_MASK |
1499                            GDK_BUTTON_PRESS_MASK |
1500                            GDK_BUTTON_RELEASE_MASK |
1501                            gtk_widget_get_events (widget));
1502
1503   tree_view->priv->bin_window = gdk_window_new (gtk_widget_get_window (widget),
1504                                                 &attributes, attributes_mask);
1505   gdk_window_set_user_data (tree_view->priv->bin_window, widget);
1506
1507   /* Make the column header window */
1508   attributes.x = 0;
1509   attributes.y = 0;
1510   attributes.width = MAX (tree_view->priv->width, old_allocation.width);
1511   attributes.height = tree_view->priv->header_height;
1512   attributes.event_mask = (GDK_EXPOSURE_MASK |
1513                            GDK_SCROLL_MASK |
1514                            GDK_BUTTON_PRESS_MASK |
1515                            GDK_BUTTON_RELEASE_MASK |
1516                            GDK_KEY_PRESS_MASK |
1517                            GDK_KEY_RELEASE_MASK |
1518                            gtk_widget_get_events (widget));
1519
1520   tree_view->priv->header_window = gdk_window_new (gtk_widget_get_window (widget),
1521                                                    &attributes, attributes_mask);
1522   gdk_window_set_user_data (tree_view->priv->header_window, widget);
1523
1524   /* Add them all up. */
1525   gtk_widget_set_style (widget,
1526                        gtk_style_attach (gtk_widget_get_style (widget), gtk_widget_get_window (widget)));
1527   gdk_window_set_background (tree_view->priv->bin_window, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1528   gtk_style_set_background (gtk_widget_get_style (widget), tree_view->priv->header_window, GTK_STATE_NORMAL);
1529
1530   tmp_list = tree_view->priv->children;
1531   while (tmp_list)
1532     {
1533       PsppSheetViewChild *child = tmp_list->data;
1534       tmp_list = tmp_list->next;
1535
1536       gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
1537     }
1538
1539   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
1540     _pspp_sheet_view_column_realize_button (PSPP_SHEET_VIEW_COLUMN (tmp_list->data));
1541
1542   /* Need to call those here, since they create GCs */
1543   pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
1544
1545   install_presize_handler (tree_view); 
1546 }
1547
1548 static void
1549 pspp_sheet_view_unrealize (GtkWidget *widget)
1550 {
1551   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1552   PsppSheetViewPrivate *priv = tree_view->priv;
1553   GList *list;
1554
1555   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->unrealize (widget);
1556
1557   if (priv->scroll_timeout != 0)
1558     {
1559       g_source_remove (priv->scroll_timeout);
1560       priv->scroll_timeout = 0;
1561     }
1562
1563   if (priv->open_dest_timeout != 0)
1564     {
1565       g_source_remove (priv->open_dest_timeout);
1566       priv->open_dest_timeout = 0;
1567     }
1568
1569   if (priv->presize_handler_timer != 0)
1570     {
1571       g_source_remove (priv->presize_handler_timer);
1572       priv->presize_handler_timer = 0;
1573     }
1574
1575   if (priv->validate_rows_timer != 0)
1576     {
1577       g_source_remove (priv->validate_rows_timer);
1578       priv->validate_rows_timer = 0;
1579     }
1580
1581   if (priv->scroll_sync_timer != 0)
1582     {
1583       g_source_remove (priv->scroll_sync_timer);
1584       priv->scroll_sync_timer = 0;
1585     }
1586
1587   if (priv->typeselect_flush_timeout)
1588     {
1589       g_source_remove (priv->typeselect_flush_timeout);
1590       priv->typeselect_flush_timeout = 0;
1591     }
1592   
1593   for (list = priv->columns; list; list = list->next)
1594     _pspp_sheet_view_column_unrealize_button (PSPP_SHEET_VIEW_COLUMN (list->data));
1595
1596   gdk_window_set_user_data (priv->bin_window, NULL);
1597   gdk_window_destroy (priv->bin_window);
1598   priv->bin_window = NULL;
1599
1600   gdk_window_set_user_data (priv->header_window, NULL);
1601   gdk_window_destroy (priv->header_window);
1602   priv->header_window = NULL;
1603
1604   if (priv->drag_window)
1605     {
1606       gdk_window_set_user_data (priv->drag_window, NULL);
1607       gdk_window_destroy (priv->drag_window);
1608       priv->drag_window = NULL;
1609     }
1610
1611   if (priv->drag_highlight_window)
1612     {
1613       gdk_window_set_user_data (priv->drag_highlight_window, NULL);
1614       gdk_window_destroy (priv->drag_highlight_window);
1615       priv->drag_highlight_window = NULL;
1616     }
1617
1618   if (tree_view->priv->columns != NULL)
1619     {
1620       list = tree_view->priv->columns;
1621       while (list)
1622         {
1623           PsppSheetViewColumn *column;
1624           column = PSPP_SHEET_VIEW_COLUMN (list->data);
1625           list = list->next;
1626           pspp_sheet_view_remove_column (tree_view, column);
1627         }
1628       tree_view->priv->columns = NULL;
1629     }
1630 }
1631
1632 /* GtkWidget::size_request helper */
1633 static void
1634 pspp_sheet_view_size_request_columns (PsppSheetView *tree_view)
1635 {
1636   GList *list;
1637
1638   tree_view->priv->header_height = 0;
1639
1640   if (tree_view->priv->model)
1641     {
1642       for (list = tree_view->priv->columns; list; list = list->next)
1643         {
1644           GtkRequisition requisition;
1645           PsppSheetViewColumn *column = list->data;
1646
1647           pspp_sheet_view_column_size_request (column, &requisition);
1648           column->button_request = requisition.width;
1649           tree_view->priv->header_height = MAX (tree_view->priv->header_height, requisition.height);
1650         }
1651     }
1652 }
1653
1654
1655 /* Called only by ::size_request */
1656 static void
1657 pspp_sheet_view_update_size (PsppSheetView *tree_view)
1658 {
1659   GList *list;
1660   PsppSheetViewColumn *column;
1661   gint i;
1662
1663   if (tree_view->priv->model == NULL)
1664     {
1665       tree_view->priv->width = 0;
1666       tree_view->priv->prev_width = 0;                   
1667       tree_view->priv->height = 0;
1668       return;
1669     }
1670
1671   tree_view->priv->prev_width = tree_view->priv->width;  
1672   tree_view->priv->width = 0;
1673
1674   /* keep this in sync with size_allocate below */
1675   for (list = tree_view->priv->columns, i = 0; list; list = list->next, i++)
1676     {
1677       gint real_requested_width = 0;
1678       column = list->data;
1679       if (!column->visible)
1680         continue;
1681
1682       if (column->use_resized_width)
1683         {
1684           real_requested_width = column->resized_width;
1685         }
1686       else
1687         {
1688           real_requested_width = column->fixed_width;
1689         }
1690
1691       if (column->min_width != -1)
1692         real_requested_width = MAX (real_requested_width, column->min_width);
1693       if (column->max_width != -1)
1694         real_requested_width = MIN (real_requested_width, column->max_width);
1695
1696       tree_view->priv->width += real_requested_width;
1697     }
1698
1699   tree_view->priv->height = tree_view->priv->fixed_height * tree_view->priv->row_count;
1700 }
1701
1702 static void
1703 pspp_sheet_view_size_request (GtkWidget      *widget,
1704                             GtkRequisition *requisition)
1705 {
1706   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1707   GList *tmp_list;
1708
1709   /* we validate some rows initially just to make sure we have some size. 
1710    * In practice, with a lot of static lists, this should get a good width.
1711    */
1712   initialize_fixed_height_mode (tree_view);
1713   pspp_sheet_view_size_request_columns (tree_view);
1714   pspp_sheet_view_update_size (PSPP_SHEET_VIEW (widget));
1715
1716   requisition->width = tree_view->priv->width;
1717   requisition->height = tree_view->priv->height + TREE_VIEW_HEADER_HEIGHT (tree_view);
1718
1719   tmp_list = tree_view->priv->children;
1720
1721   while (tmp_list)
1722     {
1723       PsppSheetViewChild *child = tmp_list->data;
1724       GtkRequisition child_requisition;
1725
1726       tmp_list = tmp_list->next;
1727
1728       if (gtk_widget_get_visible (child->widget))
1729         gtk_widget_size_request (child->widget, &child_requisition);
1730     }
1731 }
1732
1733 static void
1734 invalidate_column (PsppSheetView       *tree_view,
1735                    PsppSheetViewColumn *column)
1736 {
1737   gint column_offset = 0;
1738   GList *list;
1739   GtkWidget *widget = GTK_WIDGET (tree_view);
1740   gboolean rtl;
1741
1742   if (!gtk_widget_get_realized (widget))
1743     return;
1744
1745   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1746   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
1747        list;
1748        list = (rtl ? list->prev : list->next))
1749     {
1750       PsppSheetViewColumn *tmpcolumn = list->data;
1751       if (tmpcolumn == column)
1752         {
1753           GdkRectangle invalid_rect;
1754           GtkAllocation allocation;
1755
1756           gtk_widget_get_allocation (widget, &allocation);
1757           invalid_rect.x = column_offset;
1758           invalid_rect.y = 0;
1759           invalid_rect.width = column->width;
1760           invalid_rect.height = allocation.height;
1761           
1762           gdk_window_invalidate_rect (gtk_widget_get_window (widget), &invalid_rect, TRUE);
1763           break;
1764         }
1765       
1766       column_offset += tmpcolumn->width;
1767     }
1768 }
1769
1770 static void
1771 invalidate_last_column (PsppSheetView *tree_view)
1772 {
1773   GList *last_column;
1774   gboolean rtl;
1775
1776   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1777
1778   for (last_column = (rtl ? g_list_first (tree_view->priv->columns) : g_list_last (tree_view->priv->columns));
1779        last_column;
1780        last_column = (rtl ? last_column->next : last_column->prev))
1781     {
1782       if (PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible)
1783         {
1784           invalidate_column (tree_view, last_column->data);
1785           return;
1786         }
1787     }
1788 }
1789
1790 static gint
1791 pspp_sheet_view_get_real_requested_width_from_column (PsppSheetView       *tree_view,
1792                                                     PsppSheetViewColumn *column)
1793 {
1794   gint real_requested_width;
1795
1796   if (column->use_resized_width)
1797     {
1798       real_requested_width = column->resized_width;
1799     }
1800   else
1801     {
1802       real_requested_width = column->fixed_width;
1803     }
1804
1805   if (column->min_width != -1)
1806     real_requested_width = MAX (real_requested_width, column->min_width);
1807   if (column->max_width != -1)
1808     real_requested_width = MIN (real_requested_width, column->max_width);
1809
1810   return real_requested_width;
1811 }
1812
1813 static gboolean
1814 span_intersects (int a0, int a_width,
1815                  int b0, int b_width)
1816 {
1817   int a1 = a0 + a_width;
1818   int b1 = b0 + b_width;
1819   return (a0 >= b0 && a0 < b1) || (b0 >= a0 && b0 < a1);
1820 }
1821
1822 /* GtkWidget::size_allocate helper */
1823 static void
1824 pspp_sheet_view_size_allocate_columns (GtkWidget *widget,
1825                                      gboolean  *width_changed)
1826 {
1827   PsppSheetView *tree_view;
1828   GList *list, *first_column, *last_column;
1829   PsppSheetViewColumn *column;
1830   GtkAllocation col_allocation;
1831   GtkAllocation allocation;
1832   gint width = 0;
1833   gint extra, extra_per_column;
1834   gint full_requested_width = 0;
1835   gint number_of_expand_columns = 0;
1836   gboolean column_changed = FALSE;
1837   gboolean rtl;
1838
1839   tree_view = PSPP_SHEET_VIEW (widget);
1840
1841   for (last_column = g_list_last (tree_view->priv->columns);
1842        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
1843        last_column = last_column->prev)
1844     ;
1845
1846   if (last_column == NULL)
1847     return;
1848
1849   for (first_column = g_list_first (tree_view->priv->columns);
1850        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
1851        first_column = first_column->next)
1852     ;
1853
1854   col_allocation.y = 0;
1855   col_allocation.height = tree_view->priv->header_height;
1856
1857   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1858
1859   /* find out how many extra space and expandable columns we have */
1860   for (list = tree_view->priv->columns; list != last_column->next; list = list->next)
1861     {
1862       column = (PsppSheetViewColumn *)list->data;
1863
1864       if (!column->visible)
1865         continue;
1866
1867       full_requested_width += pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
1868
1869       if (column->expand)
1870         number_of_expand_columns++;
1871     }
1872
1873   gtk_widget_get_allocation (widget, &allocation);
1874   extra = MAX (allocation.width - full_requested_width, 0);
1875   if (number_of_expand_columns > 0)
1876     extra_per_column = extra/number_of_expand_columns;
1877   else
1878     extra_per_column = 0;
1879
1880   for (list = (rtl ? last_column : first_column); 
1881        list != (rtl ? first_column->prev : last_column->next);
1882        list = (rtl ? list->prev : list->next)) 
1883     {
1884       gint real_requested_width = 0;
1885       gint old_width;
1886
1887       column = list->data;
1888       old_width = column->width;
1889
1890       if (!column->visible)
1891         continue;
1892
1893       /* We need to handle the dragged button specially.
1894        */
1895       if (column == tree_view->priv->drag_column)
1896         {
1897           GtkAllocation drag_allocation;
1898           drag_allocation.width =  gdk_window_get_width (tree_view->priv->drag_window);
1899           drag_allocation.height = gdk_window_get_height (tree_view->priv->drag_window);
1900           drag_allocation.x = 0;
1901           drag_allocation.y = 0;
1902           pspp_sheet_view_column_size_allocate (tree_view->priv->drag_column,
1903                                                 &drag_allocation);
1904           width += drag_allocation.width;
1905           continue;
1906         }
1907
1908       real_requested_width = pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
1909
1910       col_allocation.x = width;
1911       column->width = real_requested_width;
1912
1913       if (column->expand)
1914         {
1915           if (number_of_expand_columns == 1)
1916             {
1917               /* We add the remander to the last column as
1918                * */
1919               column->width += extra;
1920             }
1921           else
1922             {
1923               column->width += extra_per_column;
1924               extra -= extra_per_column;
1925               number_of_expand_columns --;
1926             }
1927         }
1928
1929       if (column->width != old_width)
1930         g_object_notify (G_OBJECT (column), "width");
1931
1932       col_allocation.width = column->width;
1933       width += column->width;
1934
1935       if (column->width > old_width)
1936         column_changed = TRUE;
1937
1938       pspp_sheet_view_column_size_allocate (column, &col_allocation);
1939
1940       if (span_intersects (col_allocation.x, col_allocation.width,
1941                            gtk_adjustment_get_value (tree_view->priv->hadjustment),
1942                            allocation.width)
1943           && gtk_widget_get_realized (widget))
1944         pspp_sheet_view_column_set_need_button (column, TRUE);
1945
1946       if (column->window)
1947         gdk_window_move_resize (column->window,
1948                                 col_allocation.x + (rtl ? 0 : col_allocation.width) - TREE_VIEW_DRAG_WIDTH/2,
1949                                 col_allocation.y,
1950                                 TREE_VIEW_DRAG_WIDTH, col_allocation.height);
1951     }
1952
1953   /* We change the width here.  The user might have been resizing columns,
1954    * so the total width of the tree view changes.
1955    */
1956   tree_view->priv->width = width;
1957   if (width_changed)
1958     *width_changed = TRUE;
1959
1960   if (column_changed)
1961     gtk_widget_queue_draw (GTK_WIDGET (tree_view));
1962 }
1963
1964 static void
1965 pspp_sheet_view_size_allocate (GtkWidget     *widget,
1966                              GtkAllocation *allocation)
1967 {
1968   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1969   GList *tmp_list;
1970   gboolean width_changed = FALSE;
1971   GtkAllocation old_allocation;
1972   gtk_widget_get_allocation (widget, &old_allocation);
1973
1974   if (allocation->width != old_allocation.width)
1975     width_changed = TRUE;
1976
1977
1978   gtk_widget_set_allocation (widget, allocation);
1979
1980   tmp_list = tree_view->priv->children;
1981
1982   while (tmp_list)
1983     {
1984       GtkAllocation allocation;
1985
1986       PsppSheetViewChild *child = tmp_list->data;
1987       tmp_list = tmp_list->next;
1988
1989       /* totally ignore our child's requisition */
1990       allocation.x = child->x;
1991       allocation.y = child->y;
1992       allocation.width = child->width;
1993       allocation.height = child->height;
1994       gtk_widget_size_allocate (child->widget, &allocation);
1995     }
1996
1997   /* We size-allocate the columns first because the width of the
1998    * tree view (used in updating the adjustments below) might change.
1999    */
2000   pspp_sheet_view_size_allocate_columns (widget, &width_changed);
2001
2002   gtk_adjustment_set_page_size (tree_view->priv->hadjustment, allocation->width);
2003   gtk_adjustment_set_page_increment (tree_view->priv->hadjustment, allocation->width * 0.9);
2004   gtk_adjustment_set_step_increment (tree_view->priv->hadjustment, allocation->width * 0.1);
2005   gtk_adjustment_set_lower (tree_view->priv->hadjustment, 0);
2006   gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_page_size (tree_view->priv->hadjustment), tree_view->priv->width));
2007
2008   if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)   
2009     {
2010       if (allocation->width < tree_view->priv->width)
2011         {
2012           if (tree_view->priv->init_hadjust_value)
2013             {
2014               gtk_adjustment_set_value (tree_view->priv->hadjustment, MAX (tree_view->priv->width - allocation->width, 0));
2015               tree_view->priv->init_hadjust_value = FALSE;
2016             }
2017           else if (allocation->width != old_allocation.width)
2018             {
2019               gtk_adjustment_set_value (tree_view->priv->hadjustment, CLAMP (gtk_adjustment_get_value (tree_view->priv->hadjustment) - allocation->width + old_allocation.width, 0, tree_view->priv->width - allocation->width));
2020             }
2021           else
2022             gtk_adjustment_set_value (tree_view->priv->hadjustment, CLAMP (tree_view->priv->width - (tree_view->priv->prev_width - gtk_adjustment_get_value (tree_view->priv->hadjustment)), 0, tree_view->priv->width - allocation->width));
2023         }
2024       else
2025         {
2026           gtk_adjustment_set_value (tree_view->priv->hadjustment, 0);
2027           tree_view->priv->init_hadjust_value = TRUE;
2028         }
2029     }
2030   else
2031     if (gtk_adjustment_get_value (tree_view->priv->hadjustment) + allocation->width > tree_view->priv->width)
2032       gtk_adjustment_set_value (tree_view->priv->hadjustment, MAX (tree_view->priv->width - allocation->width, 0));
2033
2034   gtk_adjustment_changed (tree_view->priv->hadjustment);
2035
2036   gtk_adjustment_set_page_size (tree_view->priv->vadjustment, allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view));
2037   gtk_adjustment_set_step_increment (tree_view->priv->vadjustment, gtk_adjustment_get_page_size (tree_view->priv->vadjustment) * 0.1);
2038   gtk_adjustment_set_page_increment (tree_view->priv->vadjustment, gtk_adjustment_get_page_size (tree_view->priv->vadjustment) * 0.9);
2039   gtk_adjustment_set_lower (tree_view->priv->vadjustment, 0);
2040   gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_page_size (tree_view->priv->vadjustment), tree_view->priv->height));
2041
2042   gtk_adjustment_changed (tree_view->priv->vadjustment);
2043
2044   /* now the adjustments and window sizes are in sync, we can sync toprow/dy again */
2045   if (tree_view->priv->height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
2046     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
2047   else if (gtk_adjustment_get_value (tree_view->priv->vadjustment) + gtk_adjustment_get_page_size (tree_view->priv->vadjustment) > tree_view->priv->height)
2048     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment),
2049                               tree_view->priv->height - gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
2050   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
2051     pspp_sheet_view_top_row_to_dy (tree_view);
2052   else
2053     pspp_sheet_view_dy_to_top_row (tree_view);
2054   
2055   if (gtk_widget_get_realized (widget))
2056     {
2057       gdk_window_move_resize (gtk_widget_get_window (widget),
2058                               allocation->x, allocation->y,
2059                               allocation->width, allocation->height);
2060       gdk_window_move_resize (tree_view->priv->header_window,
2061                               - (gint) gtk_adjustment_get_value (tree_view->priv->hadjustment),
2062                               0,
2063                               MAX (tree_view->priv->width, allocation->width),
2064                               tree_view->priv->header_height);
2065       gdk_window_move_resize (tree_view->priv->bin_window,
2066                               - (gint) gtk_adjustment_get_value (tree_view->priv->hadjustment),
2067                               TREE_VIEW_HEADER_HEIGHT (tree_view),
2068                               MAX (tree_view->priv->width, allocation->width),
2069                               allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view));
2070     }
2071
2072   if (tree_view->priv->row_count == 0)
2073     invalidate_empty_focus (tree_view);
2074
2075   if (gtk_widget_get_realized (widget))
2076     {
2077       gboolean has_expand_column = FALSE;
2078       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
2079         {
2080           if (pspp_sheet_view_column_get_expand (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)))
2081             {
2082               has_expand_column = TRUE;
2083               break;
2084             }
2085         }
2086
2087       /* This little hack only works if we have an LTR locale, and no column has the  */
2088       if (width_changed)
2089         {
2090           if (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_LTR &&
2091               ! has_expand_column)
2092             invalidate_last_column (tree_view);
2093           else
2094             gtk_widget_queue_draw (widget);
2095         }
2096     }
2097 }
2098
2099 /* Grabs the focus and unsets the PSPP_SHEET_VIEW_DRAW_KEYFOCUS flag */
2100 static void
2101 grab_focus_and_unset_draw_keyfocus (PsppSheetView *tree_view)
2102 {
2103   GtkWidget *widget = GTK_WIDGET (tree_view);
2104
2105   if (gtk_widget_get_can_focus (widget) && !gtk_widget_has_focus (widget))
2106     gtk_widget_grab_focus (widget);
2107   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
2108 }
2109
2110 gboolean
2111 pspp_sheet_view_node_is_selected (PsppSheetView *tree_view,
2112                                   int node)
2113 {
2114   return node >= 0 && range_tower_contains (tree_view->priv->selected, node);
2115 }
2116
2117 void
2118 pspp_sheet_view_node_select (PsppSheetView *tree_view,
2119                              int node)
2120 {
2121   range_tower_set1 (tree_view->priv->selected, node, 1);
2122 }
2123
2124 void
2125 pspp_sheet_view_node_unselect (PsppSheetView *tree_view,
2126                                int node)
2127 {
2128   range_tower_set0 (tree_view->priv->selected, node, 1);
2129 }
2130
2131 gint
2132 pspp_sheet_view_node_next (PsppSheetView *tree_view,
2133                            gint node)
2134 {
2135   return node + 1 < tree_view->priv->row_count ? node + 1 : -1;
2136 }
2137
2138 gint
2139 pspp_sheet_view_node_prev (PsppSheetView *tree_view,
2140                            gint node)
2141 {
2142   return node > 0 ? node - 1 : -1;
2143 }
2144
2145 static gboolean
2146 all_columns_selected (PsppSheetView *tree_view)
2147 {
2148   GList *list;
2149
2150   for (list = tree_view->priv->columns; list; list = list->next)
2151     {
2152       PsppSheetViewColumn *column = list->data;
2153       if (column->selectable && !column->selected)
2154         return FALSE;
2155     }
2156
2157   return TRUE;
2158 }
2159
2160 static gboolean
2161 pspp_sheet_view_row_head_clicked (PsppSheetView *tree_view,
2162                                   gint node,
2163                                   PsppSheetViewColumn *column,
2164                                   GdkEventButton *event)
2165 {
2166   PsppSheetSelection *selection;
2167   PsppSheetSelectionMode mode;
2168   GtkTreePath *path;
2169   gboolean update_anchor;
2170   gboolean handled;
2171   guint modifiers;
2172
2173   g_return_val_if_fail (tree_view != NULL, FALSE);
2174   g_return_val_if_fail (column != NULL, FALSE);
2175
2176   selection = tree_view->priv->selection;
2177   mode = pspp_sheet_selection_get_mode (selection);
2178   if (mode != PSPP_SHEET_SELECTION_RECTANGLE)
2179     return FALSE;
2180
2181   if (!column->row_head)
2182     return FALSE;
2183
2184   if (event)
2185     {
2186       modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
2187       if (event->type != GDK_BUTTON_PRESS
2188           || (modifiers != GDK_CONTROL_MASK && modifiers != GDK_SHIFT_MASK))
2189         return FALSE;
2190     }
2191   else
2192     modifiers = 0;
2193
2194   path = gtk_tree_path_new_from_indices (node, -1);
2195   if (event == NULL)
2196     {
2197       pspp_sheet_selection_unselect_all (selection);
2198       pspp_sheet_selection_select_path (selection, path);
2199       pspp_sheet_selection_select_all_columns (selection);
2200       update_anchor = TRUE;
2201       handled = TRUE;
2202     }
2203   else if (event->type == GDK_BUTTON_PRESS && event->button == 3)
2204     {
2205       if (pspp_sheet_selection_count_selected_rows (selection) <= 1
2206           || !all_columns_selected (tree_view))
2207         {
2208           pspp_sheet_selection_unselect_all (selection);
2209           pspp_sheet_selection_select_path (selection, path);
2210           pspp_sheet_selection_select_all_columns (selection);
2211           update_anchor = TRUE;
2212           handled = FALSE;
2213         }
2214       else
2215         update_anchor = handled = FALSE;
2216     }
2217   else if (event->type == GDK_BUTTON_PRESS && event->button == 1
2218            && modifiers == GDK_CONTROL_MASK)
2219     {
2220       if (!all_columns_selected (tree_view))
2221         {
2222           pspp_sheet_selection_unselect_all (selection);
2223           pspp_sheet_selection_select_all_columns (selection);
2224         }
2225
2226       if (pspp_sheet_selection_path_is_selected (selection, path))
2227         pspp_sheet_selection_unselect_path (selection, path);
2228       else
2229         pspp_sheet_selection_select_path (selection, path);
2230       update_anchor = TRUE;
2231       handled = TRUE;
2232     }
2233   else if (event->type == GDK_BUTTON_PRESS && event->button == 1
2234            && modifiers == GDK_SHIFT_MASK)
2235     {
2236       GtkTreeRowReference *anchor = tree_view->priv->anchor;
2237       GtkTreePath *anchor_path;
2238
2239       if (all_columns_selected (tree_view)
2240           && gtk_tree_row_reference_valid (anchor))
2241         {
2242           update_anchor = FALSE;
2243           anchor_path = gtk_tree_row_reference_get_path (anchor);
2244         }
2245       else
2246         {
2247           update_anchor = TRUE;
2248           anchor_path = gtk_tree_path_copy (path);
2249         }
2250
2251       pspp_sheet_selection_unselect_all (selection);
2252       pspp_sheet_selection_select_range (selection, anchor_path, path);
2253       pspp_sheet_selection_select_all_columns (selection);
2254
2255       gtk_tree_path_free (anchor_path);
2256
2257       handled = TRUE;
2258     }
2259   else
2260     update_anchor = handled = FALSE;
2261
2262   if (update_anchor)
2263     {
2264       if (tree_view->priv->anchor)
2265         gtk_tree_row_reference_free (tree_view->priv->anchor);
2266       tree_view->priv->anchor =
2267         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
2268                                           tree_view->priv->model,
2269                                           path);
2270     }
2271
2272   gtk_tree_path_free (path);
2273   return handled;
2274 }
2275
2276 static gboolean
2277 find_click (PsppSheetView *tree_view,
2278             gint x, gint y,
2279             gint *node,
2280             PsppSheetViewColumn **column,
2281             GdkRectangle *background_area,
2282             GdkRectangle *cell_area)
2283 {
2284   gint y_offset;
2285   gboolean rtl;
2286   GList *list;
2287   gint new_y;
2288
2289   /* find the node that was clicked */
2290   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, y);
2291   if (new_y < 0)
2292     new_y = 0;
2293   y_offset = -pspp_sheet_view_find_offset (tree_view, new_y, node);
2294
2295   if (*node < 0)
2296     return FALSE;
2297
2298   background_area->y = y_offset + y;
2299   background_area->height = ROW_HEIGHT (tree_view);
2300   background_area->x = 0;
2301
2302   /* Let the column have a chance at selecting it. */
2303   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2304   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
2305        list; list = (rtl ? list->prev : list->next))
2306     {
2307       PsppSheetViewColumn *candidate = list->data;
2308
2309       if (!candidate->visible)
2310         continue;
2311
2312       background_area->width = candidate->width;
2313       if ((background_area->x > x) ||
2314           (background_area->x + background_area->width <= x))
2315         {
2316           background_area->x += background_area->width;
2317           continue;
2318         }
2319
2320       /* we found the focus column */
2321
2322       pspp_sheet_view_adjust_cell_area (tree_view, candidate, background_area,
2323                                         TRUE, cell_area);
2324       *column = candidate;
2325       return TRUE;
2326     }
2327
2328   return FALSE;
2329 }
2330
2331 static gboolean
2332 pspp_sheet_view_button_press (GtkWidget      *widget,
2333                             GdkEventButton *event)
2334 {
2335   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2336   GList *list;
2337   PsppSheetViewColumn *column = NULL;
2338   gint i;
2339   GdkRectangle background_area;
2340   GdkRectangle cell_area;
2341   gboolean rtl;
2342
2343   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2344   pspp_sheet_view_stop_editing (tree_view, FALSE);
2345
2346
2347   /* Because grab_focus can cause reentrancy, we delay grab_focus until after
2348    * we're done handling the button press.
2349    */
2350
2351   if (event->window == tree_view->priv->bin_window)
2352     {
2353       int node;
2354       GtkTreePath *path;
2355       gint dval;
2356       gint pre_val, aft_val;
2357       PsppSheetViewColumn *column = NULL;
2358       GtkCellRenderer *focus_cell = NULL;
2359       gboolean row_double_click = FALSE;
2360
2361       /* Empty tree? */
2362       if (tree_view->priv->row_count == 0)
2363         {
2364           grab_focus_and_unset_draw_keyfocus (tree_view);
2365           return TRUE;
2366         }
2367
2368       if (!find_click (tree_view, event->x, event->y, &node, &column,
2369                        &background_area, &cell_area))
2370         {
2371           grab_focus_and_unset_draw_keyfocus (tree_view);
2372           return FALSE;
2373         }
2374
2375       tree_view->priv->focus_column = column;
2376
2377       if (pspp_sheet_view_row_head_clicked (tree_view, node, column, event))
2378         return TRUE;
2379
2380       /* select */
2381       pre_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
2382
2383       path = _pspp_sheet_view_find_path (tree_view, node);
2384
2385       /* we only handle selection modifications on the first button press
2386        */
2387       if (event->type == GDK_BUTTON_PRESS)
2388         {
2389           PsppSheetSelectionMode mode = 0;
2390
2391           if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2392             mode |= PSPP_SHEET_SELECT_MODE_TOGGLE;
2393           if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2394             mode |= PSPP_SHEET_SELECT_MODE_EXTEND;
2395
2396           focus_cell = _pspp_sheet_view_column_get_cell_at_pos (column, event->x - background_area.x);
2397           if (focus_cell)
2398             pspp_sheet_view_column_focus_cell (column, focus_cell);
2399
2400           if (event->state & GDK_CONTROL_MASK)
2401             {
2402               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, mode);
2403               pspp_sheet_view_real_toggle_cursor_row (tree_view);
2404             }
2405           else if (event->state & GDK_SHIFT_MASK)
2406             {
2407               pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, mode);
2408               pspp_sheet_view_real_select_cursor_row (tree_view, FALSE, mode);
2409             }
2410           else
2411             {
2412               pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, 0);
2413             }
2414
2415           if (tree_view->priv->anchor_column == NULL ||
2416               !(event->state & GDK_SHIFT_MASK))
2417             tree_view->priv->anchor_column = column;
2418           pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
2419           pspp_sheet_selection_select_column_range (tree_view->priv->selection,
2420                                                     tree_view->priv->anchor_column,
2421                                                     column);
2422         }
2423
2424       /* the treeview may have been scrolled because of _set_cursor,
2425        * correct here
2426        */
2427
2428       aft_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
2429       dval = pre_val - aft_val;
2430
2431       cell_area.y += dval;
2432       background_area.y += dval;
2433
2434       /* Save press to possibly begin a drag
2435        */
2436       if (!tree_view->priv->in_grab &&
2437           tree_view->priv->pressed_button < 0)
2438         {
2439           tree_view->priv->pressed_button = event->button;
2440           tree_view->priv->press_start_x = event->x;
2441           tree_view->priv->press_start_y = event->y;
2442           tree_view->priv->press_start_node = node;
2443
2444           if (tree_view->priv->rubber_banding_enable
2445               && (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
2446                   tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE))
2447             {
2448               tree_view->priv->press_start_y += tree_view->priv->dy;
2449               tree_view->priv->rubber_band_x = event->x;
2450               tree_view->priv->rubber_band_y = event->y + tree_view->priv->dy;
2451               tree_view->priv->rubber_band_status = RUBBER_BAND_MAYBE_START;
2452
2453               if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2454                 tree_view->priv->rubber_band_ctrl = TRUE;
2455               if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2456                 tree_view->priv->rubber_band_shift = TRUE;
2457
2458             }
2459         }
2460
2461       /* Test if a double click happened on the same row. */
2462       if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
2463         {
2464           int double_click_time, double_click_distance;
2465
2466           g_object_get (gtk_settings_get_for_screen (
2467                           gtk_widget_get_screen (widget)),
2468                         "gtk-double-click-time", &double_click_time,
2469                         "gtk-double-click-distance", &double_click_distance,
2470                         NULL);
2471
2472           /* Same conditions as _gdk_event_button_generate */
2473           if (tree_view->priv->last_button_x != -1 &&
2474               (event->time < tree_view->priv->last_button_time + double_click_time) &&
2475               (ABS (event->x - tree_view->priv->last_button_x) <= double_click_distance) &&
2476               (ABS (event->y - tree_view->priv->last_button_y) <= double_click_distance))
2477             {
2478               /* We do no longer compare paths of this row and the
2479                * row clicked previously.  We use the double click
2480                * distance to decide whether this is a valid click,
2481                * allowing the mouse to slightly move over another row.
2482                */
2483               row_double_click = TRUE;
2484
2485               tree_view->priv->last_button_time = 0;
2486               tree_view->priv->last_button_x = -1;
2487               tree_view->priv->last_button_y = -1;
2488             }
2489           else
2490             {
2491               tree_view->priv->last_button_time = event->time;
2492               tree_view->priv->last_button_x = event->x;
2493               tree_view->priv->last_button_y = event->y;
2494             }
2495         }
2496
2497       if (row_double_click)
2498         {
2499           gtk_grab_remove (widget);
2500           pspp_sheet_view_row_activated (tree_view, path, column);
2501
2502           if (tree_view->priv->pressed_button == event->button)
2503             tree_view->priv->pressed_button = -1;
2504         }
2505
2506       gtk_tree_path_free (path);
2507
2508       /* If we activated the row through a double click we don't want to grab
2509        * focus back, as moving focus to another widget is pretty common.
2510        */
2511       if (!row_double_click)
2512         grab_focus_and_unset_draw_keyfocus (tree_view);
2513
2514       return TRUE;
2515     }
2516
2517   /* We didn't click in the window.  Let's check to see if we clicked on a column resize window.
2518    */
2519   for (i = 0, list = tree_view->priv->columns; list; list = list->next, i++)
2520     {
2521       column = list->data;
2522       if (event->window == column->window &&
2523           column->resizable &&
2524           column->window)
2525         {
2526           gpointer drag_data;
2527
2528           if (gdk_pointer_grab (column->window, FALSE,
2529                                 GDK_POINTER_MOTION_HINT_MASK |
2530                                 GDK_BUTTON1_MOTION_MASK |
2531                                 GDK_BUTTON_RELEASE_MASK,
2532                                 NULL, NULL, event->time))
2533             return FALSE;
2534
2535           gtk_grab_add (widget);
2536           PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
2537           column->resized_width = column->width;
2538
2539           /* block attached dnd signal handler */
2540           drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2541           if (drag_data)
2542             g_signal_handlers_block_matched (widget,
2543                                              G_SIGNAL_MATCH_DATA,
2544                                              0, 0, NULL, NULL,
2545                                              drag_data);
2546
2547           tree_view->priv->drag_pos = i;
2548           tree_view->priv->x_drag = column->allocation.x + (rtl ? 0 : column->allocation.width);
2549
2550           if (!gtk_widget_has_focus (widget))
2551             gtk_widget_grab_focus (widget);
2552
2553           return TRUE;
2554         }
2555     }
2556   return FALSE;
2557 }
2558
2559 /* GtkWidget::button_release_event helper */
2560 static gboolean
2561 pspp_sheet_view_button_release_drag_column (GtkWidget      *widget,
2562                                           GdkEventButton *event)
2563 {
2564   PsppSheetView *tree_view;
2565   GList *l;
2566   gboolean rtl;
2567
2568   tree_view = PSPP_SHEET_VIEW (widget);
2569
2570   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2571   gdk_display_pointer_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2572   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2573
2574   /* Move the button back */
2575   g_return_val_if_fail (tree_view->priv->drag_column->button, FALSE);
2576
2577   g_object_ref (tree_view->priv->drag_column->button);
2578   gtk_container_remove (GTK_CONTAINER (tree_view), tree_view->priv->drag_column->button);
2579   gtk_widget_set_parent_window (tree_view->priv->drag_column->button, tree_view->priv->header_window);
2580   gtk_widget_set_parent (tree_view->priv->drag_column->button, GTK_WIDGET (tree_view));
2581   g_object_unref (tree_view->priv->drag_column->button);
2582   gtk_widget_queue_resize (widget);
2583   if (tree_view->priv->drag_column->resizable)
2584     {
2585       gdk_window_raise (tree_view->priv->drag_column->window);
2586       gdk_window_show (tree_view->priv->drag_column->window);
2587     }
2588   else
2589     gdk_window_hide (tree_view->priv->drag_column->window);
2590
2591   gtk_widget_grab_focus (tree_view->priv->drag_column->button);
2592
2593   if (rtl)
2594     {
2595       if (tree_view->priv->cur_reorder &&
2596           tree_view->priv->cur_reorder->right_column != tree_view->priv->drag_column)
2597         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2598                                          tree_view->priv->cur_reorder->right_column);
2599     }
2600   else
2601     {
2602       if (tree_view->priv->cur_reorder &&
2603           tree_view->priv->cur_reorder->left_column != tree_view->priv->drag_column)
2604         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2605                                          tree_view->priv->cur_reorder->left_column);
2606     }
2607   tree_view->priv->drag_column = NULL;
2608   gdk_window_hide (tree_view->priv->drag_window);
2609
2610   for (l = tree_view->priv->column_drag_info; l != NULL; l = l->next)
2611     g_slice_free (PsppSheetViewColumnReorder, l->data);
2612   g_list_free (tree_view->priv->column_drag_info);
2613   tree_view->priv->column_drag_info = NULL;
2614   tree_view->priv->cur_reorder = NULL;
2615
2616   if (tree_view->priv->drag_highlight_window)
2617     gdk_window_hide (tree_view->priv->drag_highlight_window);
2618
2619   /* Reset our flags */
2620   tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_UNSET;
2621   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
2622
2623   return TRUE;
2624 }
2625
2626 /* GtkWidget::button_release_event helper */
2627 static gboolean
2628 pspp_sheet_view_button_release_column_resize (GtkWidget      *widget,
2629                                             GdkEventButton *event)
2630 {
2631   PsppSheetView *tree_view;
2632   gpointer drag_data;
2633
2634   tree_view = PSPP_SHEET_VIEW (widget);
2635
2636   tree_view->priv->drag_pos = -1;
2637
2638   /* unblock attached dnd signal handler */
2639   drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2640   if (drag_data)
2641     g_signal_handlers_unblock_matched (widget,
2642                                        G_SIGNAL_MATCH_DATA,
2643                                        0, 0, NULL, NULL,
2644                                        drag_data);
2645
2646   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
2647   gtk_grab_remove (widget);
2648   gdk_display_pointer_ungrab (gdk_window_get_display (event->window),
2649                               event->time);
2650   return TRUE;
2651 }
2652
2653 static gboolean
2654 pspp_sheet_view_button_release_edit (PsppSheetView *tree_view,
2655                                      GdkEventButton *event)
2656 {
2657   GtkCellEditable *cell_editable;
2658   gchar *path_string;
2659   GtkTreePath *path;
2660   gint left, right;
2661   GtkTreeIter iter;
2662   PsppSheetViewColumn *column;
2663   GdkRectangle background_area;
2664   GdkRectangle cell_area;
2665   GdkRectangle area;
2666   guint modifiers;
2667   guint flags;
2668   int node;
2669
2670   if (event->window != tree_view->priv->bin_window)
2671     return FALSE;
2672
2673   /* Ignore a released button, if that button wasn't depressed */
2674   if (tree_view->priv->pressed_button != event->button)
2675     return FALSE;
2676
2677   if (!find_click (tree_view, event->x, event->y, &node, &column, &background_area,
2678                    &cell_area))
2679     return FALSE;
2680
2681   /* decide if we edit */
2682   path = _pspp_sheet_view_find_path (tree_view, node);
2683   modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
2684   if (event->button != 1 || modifiers)
2685     return FALSE;
2686
2687   gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
2688   pspp_sheet_view_column_cell_set_cell_data (column,
2689                                              tree_view->priv->model,
2690                                              &iter);
2691
2692   if (!pspp_sheet_view_column_get_quick_edit (column)
2693       && _pspp_sheet_view_column_has_editable_cell (column))
2694     return FALSE;
2695
2696   flags = 0;                    /* FIXME: get the right flags */
2697   path_string = gtk_tree_path_to_string (path);
2698
2699   if (!_pspp_sheet_view_column_cell_event (column,
2700                                            &cell_editable,
2701                                            (GdkEvent *)event,
2702                                            path_string,
2703                                            &background_area,
2704                                            &cell_area, flags))
2705     return FALSE;
2706
2707   if (cell_editable == NULL)
2708     return FALSE;
2709
2710   pspp_sheet_view_real_set_cursor (tree_view, path,
2711                                    TRUE, TRUE, 0); /* XXX mode? */
2712   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
2713
2714   area = cell_area;
2715   _pspp_sheet_view_column_get_neighbor_sizes (
2716     column, _pspp_sheet_view_column_get_edited_cell (column), &left, &right);
2717
2718   area.x += left;
2719   area.width -= right + left;
2720
2721   pspp_sheet_view_real_start_editing (tree_view,
2722                                       column,
2723                                       path,
2724                                       cell_editable,
2725                                       &area,
2726                                       (GdkEvent *)event,
2727                                       flags);
2728   g_free (path_string);
2729   gtk_tree_path_free (path);
2730   return TRUE;
2731 }
2732
2733 static gboolean
2734 pspp_sheet_view_button_release (GtkWidget      *widget,
2735                               GdkEventButton *event)
2736 {
2737   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2738
2739   pspp_sheet_view_stop_editing (tree_view, FALSE);
2740   if (tree_view->priv->rubber_band_status != RUBBER_BAND_ACTIVE
2741       && pspp_sheet_view_button_release_edit (tree_view, event))
2742     {
2743       if (tree_view->priv->pressed_button == event->button)
2744         tree_view->priv->pressed_button = -1;
2745
2746       tree_view->priv->rubber_band_status = RUBBER_BAND_OFF;
2747       return TRUE;
2748     }
2749
2750   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
2751     return pspp_sheet_view_button_release_drag_column (widget, event);
2752
2753   if (tree_view->priv->rubber_band_status)
2754     pspp_sheet_view_stop_rubber_band (tree_view);
2755
2756   if (tree_view->priv->pressed_button == event->button)
2757     tree_view->priv->pressed_button = -1;
2758
2759   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
2760     return pspp_sheet_view_button_release_column_resize (widget, event);
2761
2762   return FALSE;
2763 }
2764
2765 static gboolean
2766 pspp_sheet_view_grab_broken (GtkWidget          *widget,
2767                            GdkEventGrabBroken *event)
2768 {
2769   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2770
2771   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
2772     pspp_sheet_view_button_release_drag_column (widget, (GdkEventButton *)event);
2773
2774   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
2775     pspp_sheet_view_button_release_column_resize (widget, (GdkEventButton *)event);
2776
2777   return TRUE;
2778 }
2779
2780 /* GtkWidget::motion_event function set.
2781  */
2782
2783 static void
2784 do_prelight (PsppSheetView *tree_view,
2785              int node,
2786              /* these are in bin_window coords */
2787              gint         x,
2788              gint         y)
2789 {
2790   int prev_node = tree_view->priv->prelight_node;
2791
2792   if (prev_node != node)
2793     {
2794       tree_view->priv->prelight_node = node;
2795
2796       if (prev_node >= 0)
2797         _pspp_sheet_view_queue_draw_node (tree_view, prev_node, NULL);
2798
2799       if (node >= 0)
2800         _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
2801     }
2802 }
2803
2804
2805 static void
2806 prelight_or_select (PsppSheetView *tree_view,
2807                     int node,
2808                     /* these are in bin_window coords */
2809                     gint         x,
2810                     gint         y)
2811 {
2812   PsppSheetSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
2813   
2814   if (tree_view->priv->hover_selection &&
2815       (mode == PSPP_SHEET_SELECTION_SINGLE || mode == PSPP_SHEET_SELECTION_BROWSE) &&
2816       !(tree_view->priv->edited_column &&
2817         tree_view->priv->edited_column->editable_widget))
2818     {
2819       if (node >= 0)
2820         {
2821           if (!pspp_sheet_view_node_is_selected (tree_view, node))
2822             {
2823               GtkTreePath *path;
2824               
2825               path = _pspp_sheet_view_find_path (tree_view, node);
2826               pspp_sheet_selection_select_path (tree_view->priv->selection, path);
2827               if (pspp_sheet_view_node_is_selected (tree_view, node))
2828                 {
2829                   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
2830                   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, FALSE, 0); /* XXX mode? */
2831                 }
2832               gtk_tree_path_free (path);
2833             }
2834         }
2835
2836       else if (mode == PSPP_SHEET_SELECTION_SINGLE)
2837         pspp_sheet_selection_unselect_all (tree_view->priv->selection);
2838     }
2839
2840     do_prelight (tree_view, node, x, y);
2841 }
2842
2843 static void
2844 ensure_unprelighted (PsppSheetView *tree_view)
2845 {
2846   do_prelight (tree_view,
2847                -1,
2848                -1000, -1000); /* coords not possibly over an arrow */
2849
2850   g_assert (tree_view->priv->prelight_node < 0);
2851 }
2852
2853 static void
2854 update_prelight (PsppSheetView *tree_view,
2855                  gint         x,
2856                  gint         y)
2857 {
2858   int new_y;
2859   int node;
2860
2861   if (tree_view->priv->row_count == 0)
2862     return;
2863
2864   if (x == -10000)
2865     {
2866       ensure_unprelighted (tree_view);
2867       return;
2868     }
2869
2870   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y);
2871   if (new_y < 0)
2872     new_y = 0;
2873
2874   pspp_sheet_view_find_offset (tree_view, new_y, &node);
2875
2876   if (node >= 0)
2877     prelight_or_select (tree_view, node, x, y);
2878 }
2879
2880
2881
2882
2883 /* Our motion arrow is either a box (in the case of the original spot)
2884  * or an arrow.  It is expander_size wide.
2885  */
2886 /*
2887  * 11111111111111
2888  * 01111111111110
2889  * 00111111111100
2890  * 00011111111000
2891  * 00001111110000
2892  * 00000111100000
2893  * 00000111100000
2894  * 00000111100000
2895  * ~ ~ ~ ~ ~ ~ ~
2896  * 00000111100000
2897  * 00000111100000
2898  * 00000111100000
2899  * 00001111110000
2900  * 00011111111000
2901  * 00111111111100
2902  * 01111111111110
2903  * 11111111111111
2904  */
2905
2906 static void
2907 pspp_sheet_view_motion_draw_column_motion_arrow (PsppSheetView *tree_view)
2908 {
2909 #if GTK3_TRANSITION
2910   PsppSheetViewColumnReorder *reorder = tree_view->priv->cur_reorder;
2911   GtkWidget *widget = GTK_WIDGET (tree_view);
2912   GdkBitmap *mask = NULL;
2913   gint x;
2914   gint y;
2915   gint width;
2916   gint height;
2917   gint arrow_type = DRAG_COLUMN_WINDOW_STATE_UNSET;
2918   GdkWindowAttr attributes;
2919   guint attributes_mask;
2920
2921   if (!reorder ||
2922       reorder->left_column == tree_view->priv->drag_column ||
2923       reorder->right_column == tree_view->priv->drag_column)
2924     arrow_type = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
2925   else if (reorder->left_column || reorder->right_column)
2926     {
2927       GdkRectangle visible_rect;
2928       pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
2929       if (reorder->left_column)
2930         x = reorder->left_column->allocation.x + reorder->left_column->allocation.width;
2931       else
2932         x = reorder->right_column->allocation.x;
2933
2934       if (x < visible_rect.x)
2935         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT;
2936       else if (x > visible_rect.x + visible_rect.width)
2937         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT;
2938       else
2939         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW;
2940     }
2941
2942   /* We want to draw the rectangle over the initial location. */
2943   if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
2944     {
2945       GdkGC *gc;
2946       GdkColor col;
2947
2948       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
2949         {
2950           if (tree_view->priv->drag_highlight_window)
2951             {
2952               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
2953                                         NULL);
2954               gdk_window_destroy (tree_view->priv->drag_highlight_window);
2955             }
2956
2957           attributes.window_type = GDK_WINDOW_CHILD;
2958           attributes.wclass = GDK_INPUT_OUTPUT;
2959           attributes.x = tree_view->priv->drag_column_x;
2960           attributes.y = 0;
2961           width = attributes.width = tree_view->priv->drag_column->allocation.width;
2962           height = attributes.height = tree_view->priv->drag_column->allocation.height;
2963           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
2964           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
2965           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
2966           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2967           tree_view->priv->drag_highlight_window = gdk_window_new (tree_view->priv->header_window, &attributes, attributes_mask);
2968           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
2969
2970           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
2971           gc = gdk_gc_new (mask);
2972           col.pixel = 1;
2973           gdk_gc_set_foreground (gc, &col);
2974           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
2975           col.pixel = 0;
2976           gdk_gc_set_foreground(gc, &col);
2977           gdk_draw_rectangle (mask, gc, TRUE, 2, 2, width - 4, height - 4);
2978           g_object_unref (gc);
2979
2980           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
2981                                          mask, 0, 0);
2982           if (mask) g_object_unref (mask);
2983           tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
2984         }
2985     }
2986   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW)
2987     {
2988       gint i, j = 1;
2989       GdkGC *gc;
2990       GdkColor col;
2991
2992       width = tree_view->priv->expander_size;
2993
2994       /* Get x, y, width, height of arrow */
2995       gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
2996       if (reorder->left_column)
2997         {
2998           x += reorder->left_column->allocation.x + reorder->left_column->allocation.width - width/2;
2999           height = reorder->left_column->allocation.height;
3000         }
3001       else
3002         {
3003           x += reorder->right_column->allocation.x - width/2;
3004           height = reorder->right_column->allocation.height;
3005         }
3006       y -= tree_view->priv->expander_size/2; /* The arrow takes up only half the space */
3007       height += tree_view->priv->expander_size;
3008
3009       /* Create the new window */
3010       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW)
3011         {
3012           if (tree_view->priv->drag_highlight_window)
3013             {
3014               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3015                                         NULL);
3016               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3017             }
3018
3019           attributes.window_type = GDK_WINDOW_TEMP;
3020           attributes.wclass = GDK_INPUT_OUTPUT;
3021           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3022           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3023           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3024           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3025           attributes.x = x;
3026           attributes.y = y;
3027           attributes.width = width;
3028           attributes.height = height;
3029           tree_view->priv->drag_highlight_window = gdk_window_new (gtk_widget_get_root_window (widget),
3030                                                                    &attributes, attributes_mask);
3031           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3032
3033           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3034           gc = gdk_gc_new (mask);
3035           col.pixel = 1;
3036           gdk_gc_set_foreground (gc, &col);
3037           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3038
3039           /* Draw the 2 arrows as per above */
3040           col.pixel = 0;
3041           gdk_gc_set_foreground (gc, &col);
3042           for (i = 0; i < width; i ++)
3043             {
3044               if (i == (width/2 - 1))
3045                 continue;
3046               gdk_draw_line (mask, gc, i, j, i, height - j);
3047               if (i < (width/2 - 1))
3048                 j++;
3049               else
3050                 j--;
3051             }
3052           g_object_unref (gc);
3053           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3054                                          mask, 0, 0);
3055           if (mask) g_object_unref (mask);
3056         }
3057
3058       tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ARROW;
3059       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3060     }
3061   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT ||
3062            arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3063     {
3064       gint i, j = 1;
3065       GdkGC *gc;
3066       GdkColor col;
3067
3068       width = tree_view->priv->expander_size;
3069
3070       /* Get x, y, width, height of arrow */
3071       width = width/2; /* remember, the arrow only takes half the available width */
3072       gdk_window_get_origin (gtk_widget_get_window (widget), &x, &y);
3073       if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3074         x += widget->allocation.width - width;
3075
3076       if (reorder->left_column)
3077         height = reorder->left_column->allocation.height;
3078       else
3079         height = reorder->right_column->allocation.height;
3080
3081       y -= tree_view->priv->expander_size;
3082       height += 2*tree_view->priv->expander_size;
3083
3084       /* Create the new window */
3085       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT &&
3086           tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3087         {
3088           if (tree_view->priv->drag_highlight_window)
3089             {
3090               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3091                                         NULL);
3092               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3093             }
3094
3095           attributes.window_type = GDK_WINDOW_TEMP;
3096           attributes.wclass = GDK_INPUT_OUTPUT;
3097           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3098           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3099           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3100           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3101           attributes.x = x;
3102           attributes.y = y;
3103           attributes.width = width;
3104           attributes.height = height;
3105           tree_view->priv->drag_highlight_window = gdk_window_new (NULL, &attributes, attributes_mask);
3106           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3107
3108           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3109           gc = gdk_gc_new (mask);
3110           col.pixel = 1;
3111           gdk_gc_set_foreground (gc, &col);
3112           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3113
3114           /* Draw the 2 arrows as per above */
3115           col.pixel = 0;
3116           gdk_gc_set_foreground (gc, &col);
3117           j = tree_view->priv->expander_size;
3118           for (i = 0; i < width; i ++)
3119             {
3120               gint k;
3121               if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT)
3122                 k = width - i - 1;
3123               else
3124                 k = i;
3125               gdk_draw_line (mask, gc, k, j, k, height - j);
3126               gdk_draw_line (mask, gc, k, 0, k, tree_view->priv->expander_size - j);
3127               gdk_draw_line (mask, gc, k, height, k, height - tree_view->priv->expander_size + j);
3128               j--;
3129             }
3130           g_object_unref (gc);
3131           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3132                                          mask, 0, 0);
3133           if (mask) g_object_unref (mask);
3134         }
3135
3136       tree_view->priv->drag_column_window_state = arrow_type;
3137       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3138    }
3139   else
3140     {
3141       g_warning (G_STRLOC"Invalid PsppSheetViewColumnReorder struct");
3142       gdk_window_hide (tree_view->priv->drag_highlight_window);
3143       return;
3144     }
3145
3146   gdk_window_show (tree_view->priv->drag_highlight_window);
3147   gdk_window_raise (tree_view->priv->drag_highlight_window);
3148 #endif
3149 }
3150
3151 static gboolean
3152 pspp_sheet_view_motion_resize_column (GtkWidget      *widget,
3153                                     GdkEventMotion *event)
3154 {
3155   gint x;
3156   gint new_width;
3157   PsppSheetViewColumn *column;
3158   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3159
3160   column = pspp_sheet_view_get_column (tree_view, tree_view->priv->drag_pos);
3161
3162   if (event->is_hint || event->window != gtk_widget_get_window (widget))
3163     gtk_widget_get_pointer (widget, &x, NULL);
3164   else
3165     x = event->x;
3166
3167   if (tree_view->priv->hadjustment)
3168     x += gtk_adjustment_get_value (tree_view->priv->hadjustment);
3169
3170   new_width = pspp_sheet_view_new_column_width (tree_view,
3171                                               tree_view->priv->drag_pos, &x);
3172   if (x != tree_view->priv->x_drag &&
3173       (new_width != column->fixed_width))
3174     {
3175       column->use_resized_width = TRUE;
3176       column->resized_width = new_width;
3177 #if 0
3178       if (column->expand)
3179         column->resized_width -= tree_view->priv->last_extra_space_per_column;
3180 #endif
3181       gtk_widget_queue_resize (widget);
3182     }
3183
3184   return FALSE;
3185 }
3186
3187
3188 static void
3189 pspp_sheet_view_update_current_reorder (PsppSheetView *tree_view)
3190 {
3191   PsppSheetViewColumnReorder *reorder = NULL;
3192   GList *list;
3193   gint mouse_x;
3194
3195   gdk_window_get_pointer (tree_view->priv->header_window, &mouse_x, NULL, NULL);
3196   for (list = tree_view->priv->column_drag_info; list; list = list->next)
3197     {
3198       reorder = (PsppSheetViewColumnReorder *) list->data;
3199       if (mouse_x >= reorder->left_align && mouse_x < reorder->right_align)
3200         break;
3201       reorder = NULL;
3202     }
3203
3204   /*  if (reorder && reorder == tree_view->priv->cur_reorder)
3205       return;*/
3206
3207   tree_view->priv->cur_reorder = reorder;
3208   pspp_sheet_view_motion_draw_column_motion_arrow (tree_view);
3209 }
3210
3211 static void
3212 pspp_sheet_view_vertical_autoscroll (PsppSheetView *tree_view)
3213 {
3214   GdkRectangle visible_rect;
3215   gint y;
3216   gint offset;
3217   gfloat value;
3218
3219   gdk_window_get_pointer (tree_view->priv->bin_window, NULL, &y, NULL);
3220   y += tree_view->priv->dy;
3221
3222   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3223
3224   /* see if we are near the edge. */
3225   offset = y - (visible_rect.y + 2 * SCROLL_EDGE_SIZE);
3226   if (offset > 0)
3227     {
3228       offset = y - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE);
3229       if (offset < 0)
3230         return;
3231     }
3232
3233   value = CLAMP (gtk_adjustment_get_value (tree_view->priv->vadjustment) + offset, 0.0,
3234                  gtk_adjustment_get_upper (tree_view->priv->vadjustment) - gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
3235   gtk_adjustment_set_value (tree_view->priv->vadjustment, value);
3236 }
3237
3238 static gboolean
3239 pspp_sheet_view_horizontal_autoscroll (PsppSheetView *tree_view)
3240 {
3241   GdkRectangle visible_rect;
3242   gint x;
3243   gint offset;
3244   gfloat value;
3245
3246   gdk_window_get_pointer (tree_view->priv->bin_window, &x, NULL, NULL);
3247
3248   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3249
3250   /* See if we are near the edge. */
3251   offset = x - (visible_rect.x + SCROLL_EDGE_SIZE);
3252   if (offset > 0)
3253     {
3254       offset = x - (visible_rect.x + visible_rect.width - SCROLL_EDGE_SIZE);
3255       if (offset < 0)
3256         return TRUE;
3257     }
3258   offset = offset/3;
3259
3260   value = CLAMP (gtk_adjustment_get_value (tree_view->priv->hadjustment) + offset,
3261                  0.0, gtk_adjustment_get_upper (tree_view->priv->hadjustment) - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
3262   gtk_adjustment_set_value (tree_view->priv->hadjustment, value);
3263
3264   return TRUE;
3265
3266 }
3267
3268 static gboolean
3269 pspp_sheet_view_motion_drag_column (GtkWidget      *widget,
3270                                   GdkEventMotion *event)
3271 {
3272   PsppSheetView *tree_view = (PsppSheetView *) widget;
3273   PsppSheetViewColumn *column = tree_view->priv->drag_column;
3274   gint x, y;
3275   GtkAllocation allocation;
3276
3277   /* Sanity Check */
3278   if ((column == NULL) ||
3279       (event->window != tree_view->priv->drag_window))
3280     return FALSE;
3281
3282   /* Handle moving the header */
3283   gdk_window_get_position (tree_view->priv->drag_window, &x, &y);
3284   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
3285   x = CLAMP (x + (gint)event->x - column->drag_x, 0,
3286              MAX (tree_view->priv->width, allocation.width) - column->allocation.width);
3287   gdk_window_move (tree_view->priv->drag_window, x, y);
3288   
3289   /* autoscroll, if needed */
3290   pspp_sheet_view_horizontal_autoscroll (tree_view);
3291   /* Update the current reorder position and arrow; */
3292   pspp_sheet_view_update_current_reorder (tree_view);
3293
3294   return TRUE;
3295 }
3296
3297 static void
3298 pspp_sheet_view_stop_rubber_band (PsppSheetView *tree_view)
3299 {
3300   remove_scroll_timeout (tree_view);
3301   gtk_grab_remove (GTK_WIDGET (tree_view));
3302
3303   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
3304     {
3305       GtkTreePath *tmp_path;
3306
3307       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
3308
3309       /* The anchor path should be set to the start path */
3310       tmp_path = _pspp_sheet_view_find_path (tree_view,
3311                                            tree_view->priv->rubber_band_start_node);
3312
3313       if (tree_view->priv->anchor)
3314         gtk_tree_row_reference_free (tree_view->priv->anchor);
3315
3316       tree_view->priv->anchor =
3317         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
3318                                           tree_view->priv->model,
3319                                           tmp_path);
3320
3321       gtk_tree_path_free (tmp_path);
3322
3323       /* ... and the cursor to the end path */
3324       tmp_path = _pspp_sheet_view_find_path (tree_view,
3325                                            tree_view->priv->rubber_band_end_node);
3326       pspp_sheet_view_real_set_cursor (PSPP_SHEET_VIEW (tree_view), tmp_path, FALSE, FALSE, 0); /* XXX mode? */
3327       gtk_tree_path_free (tmp_path);
3328
3329       _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
3330     }
3331
3332   /* Clear status variables */
3333   tree_view->priv->rubber_band_status = RUBBER_BAND_OFF;
3334   tree_view->priv->rubber_band_shift = 0;
3335   tree_view->priv->rubber_band_ctrl = 0;
3336
3337   tree_view->priv->rubber_band_start_node = -1;
3338   tree_view->priv->rubber_band_end_node = -1;
3339 }
3340
3341 static void
3342 pspp_sheet_view_update_rubber_band_selection_range (PsppSheetView *tree_view,
3343                                                  int start_node,
3344                                                  int end_node,
3345                                                  gboolean     select,
3346                                                  gboolean     skip_start,
3347                                                  gboolean     skip_end)
3348 {
3349   if (start_node == end_node)
3350     return;
3351
3352   /* We skip the first node and jump inside the loop */
3353   if (skip_start)
3354     goto skip_first;
3355
3356   do
3357     {
3358       /* Small optimization by assuming insensitive nodes are never
3359        * selected.
3360        */
3361       if (select)
3362         {
3363           if (tree_view->priv->rubber_band_shift)
3364             pspp_sheet_view_node_select (tree_view, start_node);
3365           else if (tree_view->priv->rubber_band_ctrl)
3366             {
3367               /* Toggle the selection state */
3368               if (pspp_sheet_view_node_is_selected (tree_view, start_node))
3369                 pspp_sheet_view_node_unselect (tree_view, start_node);
3370               else
3371                 pspp_sheet_view_node_select (tree_view, start_node);
3372             }
3373           else
3374             pspp_sheet_view_node_select (tree_view, start_node);
3375         }
3376       else
3377         {
3378           /* Mirror the above */
3379           if (tree_view->priv->rubber_band_shift)
3380                 pspp_sheet_view_node_unselect (tree_view, start_node);
3381           else if (tree_view->priv->rubber_band_ctrl)
3382             {
3383               /* Toggle the selection state */
3384               if (pspp_sheet_view_node_is_selected (tree_view, start_node))
3385                 pspp_sheet_view_node_unselect (tree_view, start_node);
3386               else
3387                 pspp_sheet_view_node_select (tree_view, start_node);
3388             }
3389           else
3390             pspp_sheet_view_node_unselect (tree_view, start_node);
3391         }
3392
3393       _pspp_sheet_view_queue_draw_node (tree_view, start_node, NULL);
3394
3395       if (start_node == end_node)
3396         break;
3397
3398 skip_first:
3399
3400       start_node = pspp_sheet_view_node_next (tree_view, start_node);
3401
3402       if (start_node < 0)
3403         /* Ran out of tree */
3404         break;
3405
3406       if (skip_end && start_node == end_node)
3407         break;
3408     }
3409   while (TRUE);
3410 }
3411
3412 static gint
3413 pspp_sheet_view_node_find_offset (PsppSheetView *tree_view,
3414                                   int node)
3415 {
3416   return node * tree_view->priv->fixed_height;
3417 }
3418
3419 static gint
3420 pspp_sheet_view_find_offset (PsppSheetView *tree_view,
3421                              gint height,
3422                              int *new_node)
3423 {
3424   int fixed_height = tree_view->priv->fixed_height;
3425   if (fixed_height <= 0
3426       || height < 0
3427       || height >= tree_view->priv->row_count * fixed_height)
3428     {
3429       *new_node = -1;
3430       return 0;
3431     }
3432   else
3433     {
3434       *new_node = height / fixed_height;
3435       return height % fixed_height;
3436     }
3437 }
3438
3439 static void
3440 pspp_sheet_view_update_rubber_band_selection (PsppSheetView *tree_view)
3441 {
3442   int start_node;
3443   int end_node;
3444
3445   pspp_sheet_view_find_offset (tree_view, MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &start_node);
3446   pspp_sheet_view_find_offset (tree_view, MAX (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &end_node);
3447
3448   /* Handle the start area first */
3449   if (tree_view->priv->rubber_band_start_node < 0)
3450     {
3451       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3452                                                        start_node,
3453                                                        end_node,
3454                                                        TRUE,
3455                                                        FALSE,
3456                                                        FALSE);
3457     }
3458   else if (start_node < tree_view->priv->rubber_band_start_node)
3459     {
3460       /* New node is above the old one; selection became bigger */
3461       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3462                                                        start_node,
3463                                                        tree_view->priv->rubber_band_start_node,
3464                                                        TRUE,
3465                                                        FALSE,
3466                                                        TRUE);
3467     }
3468   else if (start_node > tree_view->priv->rubber_band_start_node)
3469     {
3470       /* New node is below the old one; selection became smaller */
3471       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3472                                                        tree_view->priv->rubber_band_start_node,
3473                                                        start_node,
3474                                                        FALSE,
3475                                                        FALSE,
3476                                                        TRUE);
3477     }
3478
3479   tree_view->priv->rubber_band_start_node = start_node;
3480
3481   /* Next, handle the end area */
3482   if (tree_view->priv->rubber_band_end_node < 0)
3483     {
3484       /* In the event this happens, start_node was also -1; this case is
3485        * handled above.
3486        */
3487     }
3488   else if (end_node < 0)
3489     {
3490       /* Find the last node in the tree */
3491       pspp_sheet_view_find_offset (tree_view, tree_view->priv->height - 1,
3492                                &end_node);
3493
3494       /* Selection reached end of the tree */
3495       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3496                                                        tree_view->priv->rubber_band_end_node,
3497                                                        end_node,
3498                                                        TRUE,
3499                                                        TRUE,
3500                                                        FALSE);
3501     }
3502   else if (end_node > tree_view->priv->rubber_band_end_node)
3503     {
3504       /* New node is below the old one; selection became bigger */
3505       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3506                                                        tree_view->priv->rubber_band_end_node,
3507                                                        end_node,
3508                                                        TRUE,
3509                                                        TRUE,
3510                                                        FALSE);
3511     }
3512   else if (end_node < tree_view->priv->rubber_band_end_node)
3513     {
3514       /* New node is above the old one; selection became smaller */
3515       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3516                                                        end_node,
3517                                                        tree_view->priv->rubber_band_end_node,
3518                                                        FALSE,
3519                                                        TRUE,
3520                                                        FALSE);
3521     }
3522
3523   tree_view->priv->rubber_band_end_node = end_node;
3524 }
3525
3526 #define GDK_RECTANGLE_PTR(X) ((GdkRectangle *)(X))
3527
3528 static void
3529 pspp_sheet_view_update_rubber_band (PsppSheetView *tree_view)
3530 {
3531   gint x, y;
3532   cairo_rectangle_int_t old_area;
3533   cairo_rectangle_int_t new_area;
3534   cairo_rectangle_int_t common;
3535   cairo_region_t *invalid_region;
3536   PsppSheetViewColumn *column;
3537
3538   old_area.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
3539   old_area.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
3540   old_area.width = ABS (tree_view->priv->rubber_band_x - tree_view->priv->press_start_x) + 1;
3541   old_area.height = ABS (tree_view->priv->rubber_band_y - tree_view->priv->press_start_y) + 1;
3542
3543   gdk_window_get_pointer (tree_view->priv->bin_window, &x, &y, NULL);
3544
3545   x = MAX (x, 0);
3546   y = MAX (y, 0) + tree_view->priv->dy;
3547
3548   new_area.x = MIN (tree_view->priv->press_start_x, x);
3549   new_area.y = MIN (tree_view->priv->press_start_y, y) - tree_view->priv->dy;
3550   new_area.width = ABS (x - tree_view->priv->press_start_x) + 1;
3551   new_area.height = ABS (y - tree_view->priv->press_start_y) + 1;
3552
3553   invalid_region = cairo_region_create_rectangle (&old_area);
3554   cairo_region_union_rectangle (invalid_region, &new_area);
3555
3556   gdk_rectangle_intersect (GDK_RECTANGLE_PTR (&old_area), 
3557                            GDK_RECTANGLE_PTR (&new_area), GDK_RECTANGLE_PTR (&common));
3558   if (common.width > 2 && common.height > 2)
3559     {
3560       cairo_region_t *common_region;
3561
3562       /* make sure the border is invalidated */
3563       common.x += 1;
3564       common.y += 1;
3565       common.width -= 2;
3566       common.height -= 2;
3567
3568       common_region = cairo_region_create_rectangle (&common);
3569
3570       cairo_region_subtract (invalid_region, common_region);
3571       cairo_region_destroy (common_region);
3572     }
3573
3574 #if GTK_MAJOR_VERSION == 3
3575   gdk_window_invalidate_region (tree_view->priv->bin_window, invalid_region, TRUE);  
3576 #else
3577   {
3578     cairo_rectangle_int_t extents;
3579     GdkRegion *ereg;
3580     cairo_region_get_extents (invalid_region, &extents);
3581     ereg = gdk_region_rectangle (GDK_RECTANGLE_PTR (&extents));
3582     gdk_window_invalidate_region (tree_view->priv->bin_window, ereg, TRUE);
3583     gdk_region_destroy (ereg);
3584   }
3585 #endif
3586
3587   cairo_region_destroy (invalid_region);
3588
3589   tree_view->priv->rubber_band_x = x;
3590   tree_view->priv->rubber_band_y = y;
3591   pspp_sheet_view_get_path_at_pos (tree_view, x, y, NULL, &column, NULL, NULL);
3592
3593   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
3594   pspp_sheet_selection_select_column_range (tree_view->priv->selection,
3595                                             tree_view->priv->anchor_column,
3596                                             column);
3597
3598   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
3599
3600   pspp_sheet_view_update_rubber_band_selection (tree_view);
3601 }
3602
3603 #if GTK3_TRANSITION
3604 static void
3605 pspp_sheet_view_paint_rubber_band (PsppSheetView  *tree_view,
3606                                 GdkRectangle *area)
3607 {
3608   cairo_t *cr;
3609   GdkRectangle rect;
3610   GdkRectangle rubber_rect;
3611   GtkStyle *style;
3612
3613   return;
3614   rubber_rect.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
3615   rubber_rect.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
3616   rubber_rect.width = ABS (tree_view->priv->press_start_x - tree_view->priv->rubber_band_x) + 1;
3617   rubber_rect.height = ABS (tree_view->priv->press_start_y - tree_view->priv->rubber_band_y) + 1;
3618
3619   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
3620     return;
3621
3622   cr = gdk_cairo_create (tree_view->priv->bin_window);
3623   cairo_set_line_width (cr, 1.0);
3624
3625   style = gtk_widget_get_style (GTK_WIDGET (tree_view));
3626   cairo_set_source_rgba (cr,
3627                          style->fg[GTK_STATE_NORMAL].red / 65535.,
3628                          style->fg[GTK_STATE_NORMAL].green / 65535.,
3629                          style->fg[GTK_STATE_NORMAL].blue / 65535.,
3630                          .25);
3631
3632   gdk_cairo_rectangle (cr, &rect);
3633   cairo_clip (cr);
3634   cairo_paint (cr);
3635
3636   cairo_set_source_rgb (cr,
3637                         style->fg[GTK_STATE_NORMAL].red / 65535.,
3638                         style->fg[GTK_STATE_NORMAL].green / 65535.,
3639                         style->fg[GTK_STATE_NORMAL].blue / 65535.);
3640
3641   cairo_rectangle (cr,
3642                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
3643                    rubber_rect.width - 1, rubber_rect.height - 1);
3644   cairo_stroke (cr);
3645
3646   cairo_destroy (cr);
3647 }
3648 #endif
3649
3650
3651 static gboolean
3652 pspp_sheet_view_motion_bin_window (GtkWidget      *widget,
3653                                  GdkEventMotion *event)
3654 {
3655   PsppSheetView *tree_view;
3656   int node;
3657   gint new_y;
3658
3659   tree_view = (PsppSheetView *) widget;
3660
3661   if (tree_view->priv->row_count == 0)
3662     return FALSE;
3663
3664   if (tree_view->priv->rubber_band_status == RUBBER_BAND_MAYBE_START)
3665     {
3666       GdkRectangle background_area, cell_area;
3667       PsppSheetViewColumn *column;
3668
3669       if (find_click (tree_view, event->x, event->y, &node, &column,
3670                       &background_area, &cell_area)
3671           && tree_view->priv->focus_column == column
3672           && tree_view->priv->press_start_node == node)
3673         return FALSE;
3674
3675       gtk_grab_add (GTK_WIDGET (tree_view));
3676       pspp_sheet_view_update_rubber_band (tree_view);
3677
3678       tree_view->priv->rubber_band_status = RUBBER_BAND_ACTIVE;
3679     }
3680   else if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
3681     {
3682       pspp_sheet_view_update_rubber_band (tree_view);
3683
3684       add_scroll_timeout (tree_view);
3685     }
3686
3687   /* only check for an initiated drag when a button is pressed */
3688   if (tree_view->priv->pressed_button >= 0
3689       && !tree_view->priv->rubber_band_status)
3690     pspp_sheet_view_maybe_begin_dragging_row (tree_view, event);
3691
3692   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
3693   if (new_y < 0)
3694     new_y = 0;
3695
3696   pspp_sheet_view_find_offset (tree_view, new_y, &node);
3697
3698   tree_view->priv->event_last_x = event->x;
3699   tree_view->priv->event_last_y = event->y;
3700
3701   prelight_or_select (tree_view, node, event->x, event->y);
3702
3703   return TRUE;
3704 }
3705
3706 static gboolean
3707 pspp_sheet_view_motion (GtkWidget      *widget,
3708                       GdkEventMotion *event)
3709 {
3710   PsppSheetView *tree_view;
3711
3712   tree_view = (PsppSheetView *) widget;
3713
3714   /* Resizing a column */
3715   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
3716     return pspp_sheet_view_motion_resize_column (widget, event);
3717
3718   /* Drag column */
3719   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
3720     return pspp_sheet_view_motion_drag_column (widget, event);
3721
3722   /* Sanity check it */
3723   if (event->window == tree_view->priv->bin_window)
3724     return pspp_sheet_view_motion_bin_window (widget, event);
3725
3726   return FALSE;
3727 }
3728
3729 /* Invalidate the focus rectangle near the edge of the bin_window; used when
3730  * the tree is empty.
3731  */
3732 static void
3733 invalidate_empty_focus (PsppSheetView *tree_view)
3734 {
3735   GdkRectangle area;
3736
3737   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
3738     return;
3739
3740   area.x = 0;
3741   area.y = 0;
3742   area.width = gdk_window_get_width (tree_view->priv->bin_window);
3743   area.height = gdk_window_get_height (tree_view->priv->bin_window);
3744   gdk_window_invalidate_rect (tree_view->priv->bin_window, &area, FALSE);
3745 }
3746
3747 /* Draws a focus rectangle near the edge of the bin_window; used when the tree
3748  * is empty.
3749  */
3750 static void
3751 draw_empty_focus (PsppSheetView *tree_view)
3752 {
3753   GtkWidget *widget = GTK_WIDGET (tree_view);
3754   gint w, h;
3755   cairo_t *cr = gdk_cairo_create (tree_view->priv->bin_window);
3756
3757   if (!gtk_widget_has_focus (widget))
3758     return;
3759
3760   w = gdk_window_get_width (tree_view->priv->bin_window);
3761   h = gdk_window_get_height (tree_view->priv->bin_window);
3762
3763   w -= 2;
3764   h -= 2;
3765
3766   if (w > 0 && h > 0)
3767     gtk_paint_focus (gtk_widget_get_style (widget),
3768                      cr,
3769                      gtk_widget_get_state (widget),
3770                      widget,
3771                      NULL,
3772                      1, 1, w, h);
3773   cairo_destroy (cr);
3774 }
3775
3776 static void
3777 pspp_sheet_view_draw_vertical_grid_lines (PsppSheetView    *tree_view,
3778                                           cairo_t *cr,
3779                                           gint n_visible_columns,
3780                                           gint min_y,
3781                                           gint max_y)
3782 {
3783   GList *list = tree_view->priv->columns;
3784   gint i = 0;
3785   gint current_x = 0;
3786
3787   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
3788       && tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_BOTH)
3789     return;
3790
3791   /* Only draw the lines for visible rows and columns */
3792   for (list = tree_view->priv->columns; list; list = list->next, i++)
3793     {
3794       PsppSheetViewColumn *column = list->data;
3795
3796       /* We don't want a line for the last column */
3797       if (i == n_visible_columns - 1)
3798         break;
3799
3800       if (! column->visible)
3801         continue;
3802
3803       current_x += column->width;
3804
3805       cairo_set_line_width (cr, 1.0);
3806       cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
3807       cairo_move_to (cr, current_x - 0.5, min_y);
3808       cairo_line_to (cr, current_x - 0.5 , max_y - min_y);
3809       
3810       cairo_stroke (cr);
3811     }
3812 }
3813
3814 /* Warning: Very scary function.
3815  * Modify at your own risk
3816  *
3817  * KEEP IN SYNC WITH pspp_sheet_view_create_row_drag_icon()!
3818  * FIXME: It's not...
3819  */
3820 static gboolean
3821 pspp_sheet_view_bin_expose (GtkWidget      *widget,
3822                             GdkEventExpose *event)
3823 {
3824   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3825   GtkTreePath *path;
3826   GList *list;
3827   int node;
3828   int cursor = -1;
3829   int drag_highlight = -1;
3830   GtkTreeIter iter;
3831   gint new_y;
3832   gint y_offset, cell_offset;
3833   gint max_height;
3834   GdkRectangle background_area;
3835   GdkRectangle cell_area;
3836   guint flags;
3837   gint bin_window_width;
3838   gint bin_window_height;
3839   GtkTreePath *cursor_path;
3840   GtkTreePath *drag_dest_path;
3841   GList *first_column, *last_column;
3842   gint vertical_separator;
3843   gint horizontal_separator;
3844   gint focus_line_width;
3845   gboolean allow_rules;
3846   gboolean has_special_cell;
3847   gboolean rtl;
3848   gint n_visible_columns;
3849   gint grid_line_width;
3850   gboolean row_ending_details;
3851   gboolean draw_vgrid_lines, draw_hgrid_lines;
3852   gint min_y, max_y;
3853   cairo_t *cr = gdk_cairo_create (event->window);
3854   GdkRectangle Zarea;
3855   GtkAllocation allocation;
3856   gtk_widget_get_allocation (widget, &allocation);
3857
3858   Zarea.x =      0;
3859   Zarea.y =      0;
3860   Zarea.width =  gdk_window_get_width (event->window);
3861   Zarea.height = allocation.height;
3862
3863   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3864
3865   gtk_widget_style_get (widget,
3866                         "horizontal-separator", &horizontal_separator,
3867                         "vertical-separator", &vertical_separator,
3868                         "allow-rules", &allow_rules,
3869                         "focus-line-width", &focus_line_width,
3870                         "row-ending-details", &row_ending_details,
3871                         NULL);
3872
3873   if (tree_view->priv->row_count == 0)
3874     {
3875       draw_empty_focus (tree_view);
3876       return TRUE;
3877     }
3878
3879 #if GTK3_TRANSITION
3880   /* clip event->area to the visible area */
3881   if (Zarea.height < 0.5)
3882     return TRUE;
3883 #endif
3884
3885   validate_visible_area (tree_view);
3886
3887   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, Zarea.y);
3888
3889   if (new_y < 0)
3890     new_y = 0;
3891   y_offset = -pspp_sheet_view_find_offset (tree_view, new_y, &node);
3892   bin_window_width = 
3893     gdk_window_get_width (tree_view->priv->bin_window);
3894
3895   bin_window_height = 
3896     gdk_window_get_height (tree_view->priv->bin_window);
3897
3898
3899   if (tree_view->priv->height < bin_window_height)
3900     {
3901       gtk_paint_flat_box (gtk_widget_get_style (widget),
3902                           event->window,
3903                           gtk_widget_get_state (widget),
3904                           GTK_SHADOW_NONE,
3905                           &Zarea,
3906                           widget,
3907                           "cell_even",
3908                           0, tree_view->priv->height,
3909                           bin_window_width,
3910                           bin_window_height - tree_view->priv->height);
3911     }
3912
3913   if (node < 0)
3914     return TRUE;
3915
3916   /* find the path for the node */
3917   path = _pspp_sheet_view_find_path ((PsppSheetView *)widget, node);
3918   gtk_tree_model_get_iter (tree_view->priv->model,
3919                            &iter,
3920                            path);
3921   gtk_tree_path_free (path);
3922   
3923   cursor_path = NULL;
3924   drag_dest_path = NULL;
3925
3926   if (tree_view->priv->cursor)
3927     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
3928
3929   if (cursor_path)
3930     _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor);
3931
3932   if (tree_view->priv->drag_dest_row)
3933     drag_dest_path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
3934
3935   if (drag_dest_path)
3936     _pspp_sheet_view_find_node (tree_view, drag_dest_path,
3937                                 &drag_highlight);
3938
3939   draw_vgrid_lines =
3940     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
3941     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3942   draw_hgrid_lines =
3943     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
3944     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3945
3946   if (draw_vgrid_lines || draw_hgrid_lines)
3947     gtk_widget_style_get (widget, "grid-line-width", &grid_line_width, NULL);
3948   
3949   n_visible_columns = 0;
3950   for (list = tree_view->priv->columns; list; list = list->next)
3951     {
3952       if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
3953         continue;
3954       n_visible_columns ++;
3955     }
3956
3957   /* Find the last column */
3958   for (last_column = g_list_last (tree_view->priv->columns);
3959        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
3960        last_column = last_column->prev)
3961     ;
3962
3963   /* and the first */
3964   for (first_column = g_list_first (tree_view->priv->columns);
3965        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
3966        first_column = first_column->next)
3967     ;
3968
3969   /* Actually process the expose event.  To do this, we want to
3970    * start at the first node of the event, and walk the tree in
3971    * order, drawing each successive node.
3972    */
3973
3974   min_y = y_offset;
3975   do
3976     {
3977       gboolean parity;
3978       gboolean is_first = FALSE;
3979       gboolean is_last = FALSE;
3980       gboolean done = FALSE;
3981       gboolean selected;
3982
3983       max_height = ROW_HEIGHT (tree_view);
3984
3985       cell_offset = 0;
3986
3987       background_area.y = y_offset + Zarea.y;
3988       background_area.height = max_height;
3989       max_y = background_area.y + max_height;
3990
3991       flags = 0;
3992
3993       if (node == tree_view->priv->prelight_node)
3994         flags |= GTK_CELL_RENDERER_PRELIT;
3995
3996       selected = pspp_sheet_view_node_is_selected (tree_view, node);
3997
3998       parity = node % 2;
3999
4000       if (tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT)
4001         {
4002           /* we *need* to set cell data on all cells before the call
4003            * to _has_special_cell, else _has_special_cell() does not
4004            * return a correct value.
4005            */
4006           for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4007                list;
4008                list = (rtl ? list->prev : list->next))
4009             {
4010               PsppSheetViewColumn *column = list->data;
4011               pspp_sheet_view_column_cell_set_cell_data (column,
4012                                                          tree_view->priv->model,
4013                                                          &iter);
4014             }
4015
4016           has_special_cell = pspp_sheet_view_has_special_cell (tree_view);
4017         }
4018       else
4019         has_special_cell = tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_YES;
4020
4021       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4022            list;
4023            list = (rtl ? list->prev : list->next))
4024         {
4025           PsppSheetViewColumn *column = list->data;
4026           const gchar *detail = NULL;
4027           gboolean selected_column;
4028           GtkStateType state;
4029
4030           if (!column->visible)
4031             continue;
4032
4033           if (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE)
4034             selected_column = column->selected && column->selectable;
4035           else
4036             selected_column = TRUE;
4037
4038 #if GTK3_TRANSITION
4039           if (cell_offset > Zarea.x + Zarea.width ||
4040               cell_offset + column->width < Zarea.x)
4041             {
4042               cell_offset += column->width;
4043               continue;
4044             }
4045 #endif
4046
4047           if (selected && selected_column)
4048             flags |= GTK_CELL_RENDERER_SELECTED;
4049           else
4050             flags &= ~GTK_CELL_RENDERER_SELECTED;
4051
4052           if (column->show_sort_indicator)
4053             flags |= GTK_CELL_RENDERER_SORTED;
4054           else
4055             flags &= ~GTK_CELL_RENDERER_SORTED;
4056
4057           if (cursor == node)
4058             flags |= GTK_CELL_RENDERER_FOCUSED;
4059           else
4060             flags &= ~GTK_CELL_RENDERER_FOCUSED;
4061
4062           background_area.x = cell_offset;
4063           background_area.width = column->width;
4064
4065           cell_area = background_area;
4066           cell_area.y += vertical_separator / 2;
4067           cell_area.x += horizontal_separator / 2;
4068           cell_area.height -= vertical_separator;
4069           cell_area.width -= horizontal_separator;
4070
4071           if (draw_vgrid_lines)
4072             {
4073               if (list == first_column)
4074                 {
4075                   cell_area.width -= grid_line_width / 2;
4076                 }
4077               else if (list == last_column)
4078                 {
4079                   cell_area.x += grid_line_width / 2;
4080                   cell_area.width -= grid_line_width / 2;
4081                 }
4082               else
4083                 {
4084                   cell_area.x += grid_line_width / 2;
4085                   cell_area.width -= grid_line_width;
4086                 }
4087             }
4088
4089           if (draw_hgrid_lines)
4090             {
4091               cell_area.y += grid_line_width / 2;
4092               cell_area.height -= grid_line_width;
4093             }
4094
4095 #if GTK3_TRANSITION
4096           if (gdk_region_rect_in (event->region, &background_area) == GDK_OVERLAP_RECTANGLE_OUT)
4097             {
4098               cell_offset += column->width;
4099               continue;
4100             }
4101 #endif
4102
4103           pspp_sheet_view_column_cell_set_cell_data (column,
4104                                                      tree_view->priv->model,
4105                                                      &iter);
4106
4107           /* Select the detail for drawing the cell.  relevant
4108            * factors are parity, sortedness, and whether to
4109            * display rules.
4110            */
4111           if (allow_rules && tree_view->priv->has_rules)
4112             {
4113               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4114                   n_visible_columns >= 3)
4115                 {
4116                   if (parity)
4117                     detail = "cell_odd_ruled_sorted";
4118                   else
4119                     detail = "cell_even_ruled_sorted";
4120                 }
4121               else
4122                 {
4123                   if (parity)
4124                     detail = "cell_odd_ruled";
4125                   else
4126                     detail = "cell_even_ruled";
4127                 }
4128             }
4129           else
4130             {
4131               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4132                   n_visible_columns >= 3)
4133                 {
4134                   if (parity)
4135                     detail = "cell_odd_sorted";
4136                   else
4137                     detail = "cell_even_sorted";
4138                 }
4139               else
4140                 {
4141                   if (parity)
4142                     detail = "cell_odd";
4143                   else
4144                     detail = "cell_even";
4145                 }
4146             }
4147
4148           g_assert (detail);
4149
4150           if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
4151             state = GTK_STATE_INSENSITIVE;          
4152           else if (flags & GTK_CELL_RENDERER_SELECTED)
4153             state = GTK_STATE_SELECTED;
4154           else
4155             state = GTK_STATE_NORMAL;
4156
4157           /* Draw background */
4158           if (row_ending_details)
4159             {
4160               char new_detail[128];
4161
4162               is_first = (rtl ? !list->next : !list->prev);
4163               is_last = (rtl ? !list->prev : !list->next);
4164
4165               /* (I don't like the snprintfs either, but couldn't find a
4166                * less messy way).
4167                */
4168               if (is_first && is_last)
4169                 g_snprintf (new_detail, 127, "%s", detail);
4170               else if (is_first)
4171                 g_snprintf (new_detail, 127, "%s_start", detail);
4172               else if (is_last)
4173                 g_snprintf (new_detail, 127, "%s_end", detail);
4174               else
4175                 g_snprintf (new_detail, 128, "%s_middle", detail);
4176
4177               gtk_paint_flat_box (gtk_widget_get_style (widget),
4178                                   event->window,
4179                                   state,
4180                                   GTK_SHADOW_NONE,
4181                                   &Zarea,
4182                                   widget,
4183                                   new_detail,
4184                                   background_area.x,
4185                                   background_area.y,
4186                                   background_area.width,
4187                                   background_area.height);
4188             }
4189           else
4190             {
4191               gtk_paint_flat_box (gtk_widget_get_style (widget),
4192                                   event->window,
4193                                   state,
4194                                   GTK_SHADOW_NONE,
4195                                   &Zarea,
4196                                   widget,
4197                                   detail,
4198                                   background_area.x,
4199                                   background_area.y,
4200                                   background_area.width,
4201                                   background_area.height);
4202             }
4203
4204           if (draw_hgrid_lines)
4205             {
4206               cairo_set_line_width (cr, 1.0);
4207               cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
4208
4209               if (background_area.y >= 0)
4210                 {
4211 #if GTK3_TRANSITION
4212                   gdk_draw_line (event->window,
4213                                  tree_view->priv->grid_line_gc[widget->state],
4214                                  background_area.x, background_area.y,
4215                                  background_area.x + background_area.width,
4216                                  background_area.y);
4217 #else
4218                   cairo_move_to (cr, background_area.x, background_area.y - 0.5);
4219                   cairo_line_to (cr, background_area.x + background_area.width,
4220                                  background_area.y - 0.5);
4221 #endif
4222                 }
4223
4224               if (y_offset + max_height >= Zarea.height - 0.5)
4225                 {
4226 #if GTK3_TRANSITION
4227                   gdk_draw_line (event->window,
4228                                  tree_view->priv->grid_line_gc[widget->state],
4229                                  background_area.x, background_area.y + max_height,
4230                                  background_area.x + background_area.width,
4231                                  background_area.y + max_height);
4232 #else
4233
4234                   cairo_move_to (cr, background_area.x, background_area.y + max_height - 0.5);
4235                   cairo_line_to (cr, background_area.x + background_area.width,
4236                                  background_area.y + max_height - 0.5);
4237 #endif
4238                 }
4239               cairo_stroke (cr);
4240             }
4241
4242           _pspp_sheet_view_column_cell_render (column,
4243                                                event->window,
4244                                                &background_area,
4245                                                &cell_area,
4246                                                &Zarea,
4247                                                flags);
4248
4249           if (node == cursor && has_special_cell &&
4250               ((column == tree_view->priv->focus_column &&
4251                 PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4252                 gtk_widget_has_focus (widget)) ||
4253                (column == tree_view->priv->edited_column)))
4254             {
4255               _pspp_sheet_view_column_cell_draw_focus (column,
4256                                                      event->window,
4257                                                      &background_area,
4258                                                      &cell_area,
4259                                                      &Zarea,
4260                                                      flags);
4261             }
4262
4263           cell_offset += column->width;
4264         }
4265
4266       if (cell_offset < Zarea.x)
4267         {
4268           gtk_paint_flat_box (gtk_widget_get_style (widget),
4269                               event->window,
4270                               GTK_STATE_NORMAL,
4271                               GTK_SHADOW_NONE,
4272                               &Zarea,
4273                               widget,
4274                               "base",
4275                               cell_offset,
4276                               background_area.y,
4277                               Zarea.x - cell_offset,
4278                               background_area.height);
4279         }
4280
4281       if (node == drag_highlight)
4282         {
4283           /* Draw indicator for the drop
4284            */
4285           gint highlight_y = -1;
4286           int node = -1;
4287           gint width;
4288
4289           switch (tree_view->priv->drag_dest_pos)
4290             {
4291             case PSPP_SHEET_VIEW_DROP_BEFORE:
4292               highlight_y = background_area.y - 1;
4293               if (highlight_y < 0)
4294                       highlight_y = 0;
4295               break;
4296
4297             case PSPP_SHEET_VIEW_DROP_AFTER:
4298               highlight_y = background_area.y + background_area.height - 1;
4299               break;
4300
4301             case PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE:
4302             case PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER:
4303               _pspp_sheet_view_find_node (tree_view, drag_dest_path, &node);
4304
4305               if (node < 0)
4306                 break;
4307               width = gdk_window_get_width (tree_view->priv->bin_window);
4308
4309               if (row_ending_details)
4310                 gtk_paint_focus (gtk_widget_get_style (widget),
4311                                  tree_view->priv->bin_window,
4312                                  gtk_widget_get_state (widget),
4313                                  &Zarea,
4314                                  widget,
4315                                  (is_first
4316                                   ? (is_last ? "treeview-drop-indicator" : "treeview-drop-indicator-left" )
4317                                   : (is_last ? "treeview-drop-indicator-right" : "tree-view-drop-indicator-middle" )),
4318                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4319                                  - focus_line_width / 2,
4320                                  width, ROW_HEIGHT (tree_view)
4321                                - focus_line_width + 1);
4322               else
4323                 gtk_paint_focus (gtk_widget_get_style (widget),
4324                                  tree_view->priv->bin_window,
4325                                  gtk_widget_get_state (widget),
4326                                  &Zarea,
4327                                  widget,
4328                                  "treeview-drop-indicator",
4329                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4330                                  - focus_line_width / 2,
4331                                  width, ROW_HEIGHT (tree_view)
4332                                  - focus_line_width + 1);
4333               break;
4334             }
4335
4336 #if GTK3_TRANSITION
4337           if (highlight_y >= 0)
4338             {
4339               gdk_draw_line (event->window,
4340                              widget->style->fg_gc[gtk_widget_get_state (widget)],
4341                              0,
4342                              highlight_y,
4343                              rtl ? 0 : bin_window_width,
4344                              highlight_y);
4345             }
4346 #endif
4347         }
4348
4349       /* draw the big row-spanning focus rectangle, if needed */
4350       if (!has_special_cell && node == cursor &&
4351           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4352           gtk_widget_has_focus (widget))
4353         {
4354           gint tmp_y, tmp_height;
4355           gint width;
4356           GtkStateType focus_rect_state;
4357
4358           focus_rect_state =
4359             flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
4360             (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
4361              (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE :
4362               GTK_STATE_NORMAL));
4363
4364           width = gdk_window_get_width (tree_view->priv->bin_window);
4365           
4366           if (draw_hgrid_lines)
4367             {
4368               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node) + grid_line_width / 2;
4369               tmp_height = ROW_HEIGHT (tree_view) - grid_line_width;
4370             }
4371           else
4372             {
4373               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node);
4374               tmp_height = ROW_HEIGHT (tree_view);
4375             }
4376
4377           if (row_ending_details)
4378             gtk_paint_focus (gtk_widget_get_style (widget),
4379                              tree_view->priv->bin_window,
4380                              focus_rect_state,
4381                              &Zarea,
4382                              widget,
4383                              (is_first
4384                               ? (is_last ? "treeview" : "treeview-left" )
4385                               : (is_last ? "treeview-right" : "treeview-middle" )),
4386                              0, tmp_y,
4387                              width, tmp_height);
4388           else
4389             gtk_paint_focus (gtk_widget_get_style (widget),
4390                              tree_view->priv->bin_window,
4391                              focus_rect_state,
4392                              &Zarea,
4393                              widget,
4394                              "treeview",
4395                              0, tmp_y,
4396                              width, tmp_height);
4397         }
4398
4399       y_offset += max_height;
4400
4401       do
4402         {
4403           node = pspp_sheet_view_node_next (tree_view, node);
4404           if (node >= 0)
4405             {
4406               gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
4407               done = TRUE;
4408
4409               /* Sanity Check! */
4410               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
4411             }
4412           else
4413             goto done;
4414         }
4415       while (!done);
4416     }
4417   while (y_offset < Zarea.height);
4418
4419 done:
4420   pspp_sheet_view_draw_vertical_grid_lines (tree_view, cr, n_visible_columns,
4421                                    min_y, max_y);
4422
4423 #if GTK3_TRANSITION
4424  if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
4425    {
4426      GdkRectangle *rectangles;
4427      gint n_rectangles;
4428
4429      gdk_region_get_rectangles (event->region,
4430                                 &rectangles,
4431                                 &n_rectangles);
4432
4433      while (n_rectangles--)
4434        pspp_sheet_view_paint_rubber_band (tree_view, &rectangles[n_rectangles]);
4435
4436      g_free (rectangles);
4437    }
4438 #endif
4439
4440   if (cursor_path)
4441     gtk_tree_path_free (cursor_path);
4442
4443   if (drag_dest_path)
4444     gtk_tree_path_free (drag_dest_path);
4445
4446   return FALSE;
4447 }
4448
4449 static gboolean
4450 pspp_sheet_view_expose (GtkWidget      *widget,
4451                         GdkEventExpose *event)
4452 {
4453   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
4454   cairo_t *cr = gdk_cairo_create (event->window);
4455
4456   if (event->window == tree_view->priv->bin_window)
4457     {
4458       gboolean retval;
4459       GList *tmp_list;
4460
4461       retval = pspp_sheet_view_bin_expose (widget, event);
4462
4463       /* We can't just chain up to Container::expose as it will try to send the
4464        * event to the headers, so we handle propagating it to our children
4465        * (eg. widgets being edited) ourselves.
4466        */
4467       tmp_list = tree_view->priv->children;
4468       while (tmp_list)
4469         {
4470           PsppSheetViewChild *child = tmp_list->data;
4471           tmp_list = tmp_list->next;
4472
4473           gtk_container_propagate_expose (GTK_CONTAINER (tree_view), child->widget, event);
4474         }
4475
4476       return retval;
4477     }
4478   else if (event->window == tree_view->priv->header_window)
4479     {
4480       gint n_visible_columns;
4481       GList *list;
4482
4483       gtk_paint_flat_box (gtk_widget_get_style (widget),
4484                           event->window,
4485                           GTK_STATE_NORMAL,
4486                           GTK_SHADOW_NONE,
4487                           &event->area,
4488                           widget,
4489                           "cell_odd",
4490                           event->area.x,
4491                           event->area.y,
4492                           event->area.width,
4493                           event->area.height);
4494
4495       for (list = tree_view->priv->columns; list != NULL; list = list->next)
4496         {
4497           PsppSheetViewColumn *column = list->data;
4498
4499           if (column == tree_view->priv->drag_column || !column->visible)
4500             continue;
4501
4502           if (span_intersects (column->allocation.x, column->allocation.width,
4503                                event->area.x, event->area.width)
4504               && column->button != NULL)
4505             gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
4506                                             column->button, event);
4507         }
4508
4509       n_visible_columns = 0;
4510       for (list = tree_view->priv->columns; list; list = list->next)
4511         {
4512           if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
4513             continue;
4514           n_visible_columns ++;
4515         }
4516       pspp_sheet_view_draw_vertical_grid_lines (tree_view,
4517                                                 cr,
4518                                                 n_visible_columns,
4519                                                 event->area.y,
4520                                                 event->area.height);
4521     }
4522   else if (event->window == tree_view->priv->drag_window)
4523     {
4524       gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
4525                                       tree_view->priv->drag_column->button,
4526                                       event);
4527     }
4528   return TRUE;
4529 }
4530
4531 enum
4532 {
4533   DROP_HOME,
4534   DROP_RIGHT,
4535   DROP_LEFT,
4536   DROP_END
4537 };
4538
4539 /* returns 0x1 when no column has been found -- yes it's hackish */
4540 static PsppSheetViewColumn *
4541 pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
4542                                PsppSheetViewColumn *column,
4543                                gint               drop_position)
4544 {
4545   PsppSheetViewColumn *left_column = NULL;
4546   PsppSheetViewColumn *cur_column = NULL;
4547   GList *tmp_list;
4548
4549   if (!column->reorderable)
4550     return (PsppSheetViewColumn *)0x1;
4551
4552   switch (drop_position)
4553     {
4554       case DROP_HOME:
4555         /* find first column where we can drop */
4556         tmp_list = tree_view->priv->columns;
4557         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
4558           return (PsppSheetViewColumn *)0x1;
4559
4560         while (tmp_list)
4561           {
4562             g_assert (tmp_list);
4563
4564             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4565             tmp_list = tmp_list->next;
4566
4567             if (left_column && left_column->visible == FALSE)
4568               continue;
4569
4570             if (!tree_view->priv->column_drop_func)
4571               return left_column;
4572
4573             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4574               {
4575                 left_column = cur_column;
4576                 continue;
4577               }
4578
4579             return left_column;
4580           }
4581
4582         if (!tree_view->priv->column_drop_func)
4583           return left_column;
4584
4585         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
4586           return left_column;
4587         else
4588           return (PsppSheetViewColumn *)0x1;
4589         break;
4590
4591       case DROP_RIGHT:
4592         /* find first column after column where we can drop */
4593         tmp_list = tree_view->priv->columns;
4594
4595         for (; tmp_list; tmp_list = tmp_list->next)
4596           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
4597             break;
4598
4599         if (!tmp_list || !tmp_list->next)
4600           return (PsppSheetViewColumn *)0x1;
4601
4602         tmp_list = tmp_list->next;
4603         left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4604         tmp_list = tmp_list->next;
4605
4606         while (tmp_list)
4607           {
4608             g_assert (tmp_list);
4609
4610             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4611             tmp_list = tmp_list->next;
4612
4613             if (left_column && left_column->visible == FALSE)
4614               {
4615                 left_column = cur_column;
4616                 if (tmp_list)
4617                   tmp_list = tmp_list->next;
4618                 continue;
4619               }
4620
4621             if (!tree_view->priv->column_drop_func)
4622               return left_column;
4623
4624             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4625               {
4626                 left_column = cur_column;
4627                 continue;
4628               }
4629
4630             return left_column;
4631           }
4632
4633         if (!tree_view->priv->column_drop_func)
4634           return left_column;
4635
4636         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
4637           return left_column;
4638         else
4639           return (PsppSheetViewColumn *)0x1;
4640         break;
4641
4642       case DROP_LEFT:
4643         /* find first column before column where we can drop */
4644         tmp_list = tree_view->priv->columns;
4645
4646         for (; tmp_list; tmp_list = tmp_list->next)
4647           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
4648             break;
4649
4650         if (!tmp_list || !tmp_list->prev)
4651           return (PsppSheetViewColumn *)0x1;
4652
4653         tmp_list = tmp_list->prev;
4654         cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4655         tmp_list = tmp_list->prev;
4656
4657         while (tmp_list)
4658           {
4659             g_assert (tmp_list);
4660
4661             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4662
4663             if (left_column && !left_column->visible)
4664               {
4665                 /*if (!tmp_list->prev)
4666                   return (PsppSheetViewColumn *)0x1;
4667                   */
4668 /*
4669                 cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->prev->data);
4670                 tmp_list = tmp_list->prev->prev;
4671                 continue;*/
4672
4673                 cur_column = left_column;
4674                 if (tmp_list)
4675                   tmp_list = tmp_list->prev;
4676                 continue;
4677               }
4678
4679             if (!tree_view->priv->column_drop_func)
4680               return left_column;
4681
4682             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4683               return left_column;
4684
4685             cur_column = left_column;
4686             tmp_list = tmp_list->prev;
4687           }
4688
4689         if (!tree_view->priv->column_drop_func)
4690           return NULL;
4691
4692         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
4693           return NULL;
4694         else
4695           return (PsppSheetViewColumn *)0x1;
4696         break;
4697
4698       case DROP_END:
4699         /* same as DROP_HOME case, but doing it backwards */
4700         tmp_list = g_list_last (tree_view->priv->columns);
4701         cur_column = NULL;
4702
4703         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
4704           return (PsppSheetViewColumn *)0x1;
4705
4706         while (tmp_list)
4707           {
4708             g_assert (tmp_list);
4709
4710             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4711
4712             if (left_column && !left_column->visible)
4713               {
4714                 cur_column = left_column;
4715                 tmp_list = tmp_list->prev;
4716               }
4717
4718             if (!tree_view->priv->column_drop_func)
4719               return left_column;
4720
4721             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4722               return left_column;
4723
4724             cur_column = left_column;
4725             tmp_list = tmp_list->prev;
4726           }
4727
4728         if (!tree_view->priv->column_drop_func)
4729           return NULL;
4730
4731         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
4732           return NULL;
4733         else
4734           return (PsppSheetViewColumn *)0x1;
4735         break;
4736     }
4737
4738   return (PsppSheetViewColumn *)0x1;
4739 }
4740
4741 static gboolean
4742 pspp_sheet_view_key_press (GtkWidget   *widget,
4743                          GdkEventKey *event)
4744 {
4745   PsppSheetView *tree_view = (PsppSheetView *) widget;
4746
4747   if (tree_view->priv->rubber_band_status)
4748     {
4749       if (event->keyval == GDK_Escape)
4750         pspp_sheet_view_stop_rubber_band (tree_view);
4751
4752       return TRUE;
4753     }
4754
4755   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
4756     {
4757       if (event->keyval == GDK_Escape)
4758         {
4759           tree_view->priv->cur_reorder = NULL;
4760           pspp_sheet_view_button_release_drag_column (widget, NULL);
4761         }
4762       return TRUE;
4763     }
4764
4765   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
4766     {
4767       GList *focus_column;
4768       gboolean rtl;
4769
4770       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
4771
4772       for (focus_column = tree_view->priv->columns;
4773            focus_column;
4774            focus_column = focus_column->next)
4775         {
4776           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4777
4778           if (column->button && gtk_widget_has_focus (column->button))
4779             break;
4780         }
4781
4782       if (focus_column &&
4783           (event->state & GDK_SHIFT_MASK) && (event->state & GDK_MOD1_MASK) &&
4784           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
4785            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right))
4786         {
4787           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4788
4789           if (!column->resizable)
4790             {
4791               gtk_widget_error_bell (widget);
4792               return TRUE;
4793             }
4794
4795           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
4796               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
4797             {
4798               gint old_width = column->resized_width;
4799
4800               column->resized_width = MAX (column->resized_width,
4801                                            column->width);
4802               column->resized_width -= 2;
4803               if (column->resized_width < 0)
4804                 column->resized_width = 0;
4805
4806               if (column->min_width == -1)
4807                 column->resized_width = MAX (column->button_request,
4808                                              column->resized_width);
4809               else
4810                 column->resized_width = MAX (column->min_width,
4811                                              column->resized_width);
4812
4813               if (column->max_width != -1)
4814                 column->resized_width = MIN (column->resized_width,
4815                                              column->max_width);
4816
4817               column->use_resized_width = TRUE;
4818
4819               if (column->resized_width != old_width)
4820                 gtk_widget_queue_resize (widget);
4821               else
4822                 gtk_widget_error_bell (widget);
4823             }
4824           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
4825                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
4826             {
4827               gint old_width = column->resized_width;
4828
4829               column->resized_width = MAX (column->resized_width,
4830                                            column->width);
4831               column->resized_width += 2;
4832
4833               if (column->max_width != -1)
4834                 column->resized_width = MIN (column->resized_width,
4835                                              column->max_width);
4836
4837               column->use_resized_width = TRUE;
4838
4839               if (column->resized_width != old_width)
4840                 gtk_widget_queue_resize (widget);
4841               else
4842                 gtk_widget_error_bell (widget);
4843             }
4844
4845           return TRUE;
4846         }
4847
4848       if (focus_column &&
4849           (event->state & GDK_MOD1_MASK) &&
4850           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
4851            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
4852            || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
4853            || event->keyval == GDK_End || event->keyval == GDK_KP_End))
4854         {
4855           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4856
4857           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
4858               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
4859             {
4860               PsppSheetViewColumn *col;
4861               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_LEFT);
4862               if (col != (PsppSheetViewColumn *)0x1)
4863                 pspp_sheet_view_move_column_after (tree_view, column, col);
4864               else
4865                 gtk_widget_error_bell (widget);
4866             }
4867           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
4868                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
4869             {
4870               PsppSheetViewColumn *col;
4871               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_RIGHT);
4872               if (col != (PsppSheetViewColumn *)0x1)
4873                 pspp_sheet_view_move_column_after (tree_view, column, col);
4874               else
4875                 gtk_widget_error_bell (widget);
4876             }
4877           else if (event->keyval == GDK_Home || event->keyval == GDK_KP_Home)
4878             {
4879               PsppSheetViewColumn *col;
4880               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_HOME);
4881               if (col != (PsppSheetViewColumn *)0x1)
4882                 pspp_sheet_view_move_column_after (tree_view, column, col);
4883               else
4884                 gtk_widget_error_bell (widget);
4885             }
4886           else if (event->keyval == GDK_End || event->keyval == GDK_KP_End)
4887             {
4888               PsppSheetViewColumn *col;
4889               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_END);
4890               if (col != (PsppSheetViewColumn *)0x1)
4891                 pspp_sheet_view_move_column_after (tree_view, column, col);
4892               else
4893                 gtk_widget_error_bell (widget);
4894             }
4895
4896           return TRUE;
4897         }
4898     }
4899
4900   /* Chain up to the parent class.  It handles the keybindings. */
4901   if (GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_press_event (widget, event))
4902     return TRUE;
4903
4904   if (tree_view->priv->search_entry_avoid_unhandled_binding)
4905     {
4906       tree_view->priv->search_entry_avoid_unhandled_binding = FALSE;
4907       return FALSE;
4908     }
4909
4910   /* We pass the event to the search_entry.  If its text changes, then we start
4911    * the typeahead find capabilities. */
4912   if (gtk_widget_has_focus (GTK_WIDGET (tree_view))
4913       && tree_view->priv->enable_search
4914       && !tree_view->priv->search_custom_entry_set)
4915     {
4916       GdkEvent *new_event;
4917       char *old_text;
4918       const char *new_text;
4919       gboolean retval;
4920       GdkScreen *screen;
4921       gboolean text_modified;
4922       gulong popup_menu_id;
4923
4924       pspp_sheet_view_ensure_interactive_directory (tree_view);
4925
4926       /* Make a copy of the current text */
4927       old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry)));
4928       new_event = gdk_event_copy ((GdkEvent *) event);
4929       g_object_unref (((GdkEventKey *) new_event)->window);
4930       ((GdkEventKey *) new_event)->window = g_object_ref (gtk_widget_get_window (tree_view->priv->search_window));
4931       gtk_widget_realize (tree_view->priv->search_window);
4932
4933       popup_menu_id = g_signal_connect (tree_view->priv->search_entry, 
4934                                         "popup-menu", G_CALLBACK (gtk_true),
4935                                         NULL);
4936
4937       /* Move the entry off screen */
4938       screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
4939       gtk_window_move (GTK_WINDOW (tree_view->priv->search_window),
4940                        gdk_screen_get_width (screen) + 1,
4941                        gdk_screen_get_height (screen) + 1);
4942       gtk_widget_show (tree_view->priv->search_window);
4943
4944       /* Send the event to the window.  If the preedit_changed signal is emitted
4945        * during this event, we will set priv->imcontext_changed  */
4946       tree_view->priv->imcontext_changed = FALSE;
4947       retval = gtk_widget_event (tree_view->priv->search_window, new_event);
4948       gdk_event_free (new_event);
4949       gtk_widget_hide (tree_view->priv->search_window);
4950
4951       g_signal_handler_disconnect (tree_view->priv->search_entry, 
4952                                    popup_menu_id);
4953
4954       /* We check to make sure that the entry tried to handle the text, and that
4955        * the text has changed.
4956        */
4957       new_text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
4958       text_modified = strcmp (old_text, new_text) != 0;
4959       g_free (old_text);
4960       if (tree_view->priv->imcontext_changed ||    /* we're in a preedit */
4961           (retval && text_modified))               /* ...or the text was modified */
4962         {
4963           if (pspp_sheet_view_real_start_interactive_search (tree_view, FALSE))
4964             {
4965               gtk_widget_grab_focus (GTK_WIDGET (tree_view));
4966               return TRUE;
4967             }
4968           else
4969             {
4970               gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
4971               return FALSE;
4972             }
4973         }
4974     }
4975
4976   return FALSE;
4977 }
4978
4979 static gboolean
4980 pspp_sheet_view_key_release (GtkWidget   *widget,
4981                            GdkEventKey *event)
4982 {
4983   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
4984
4985   if (tree_view->priv->rubber_band_status)
4986     return TRUE;
4987
4988   return GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_release_event (widget, event);
4989 }
4990
4991 /* FIXME Is this function necessary? Can I get an enter_notify event
4992  * w/o either an expose event or a mouse motion event?
4993  */
4994 static gboolean
4995 pspp_sheet_view_enter_notify (GtkWidget        *widget,
4996                             GdkEventCrossing *event)
4997 {
4998   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
4999   int node;
5000   gint new_y;
5001
5002   /* Sanity check it */
5003   if (event->window != tree_view->priv->bin_window)
5004     return FALSE;
5005
5006   if (tree_view->priv->row_count == 0)
5007     return FALSE;
5008
5009   if (event->mode == GDK_CROSSING_GRAB ||
5010       event->mode == GDK_CROSSING_GTK_GRAB ||
5011       event->mode == GDK_CROSSING_GTK_UNGRAB ||
5012       event->mode == GDK_CROSSING_STATE_CHANGED)
5013     return TRUE;
5014
5015   /* find the node internally */
5016   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
5017   if (new_y < 0)
5018     new_y = 0;
5019   pspp_sheet_view_find_offset (tree_view, new_y, &node);
5020
5021   tree_view->priv->event_last_x = event->x;
5022   tree_view->priv->event_last_y = event->y;
5023
5024   prelight_or_select (tree_view, node, event->x, event->y);
5025
5026   return TRUE;
5027 }
5028
5029 static gboolean
5030 pspp_sheet_view_leave_notify (GtkWidget        *widget,
5031                             GdkEventCrossing *event)
5032 {
5033   PsppSheetView *tree_view;
5034
5035   if (event->mode == GDK_CROSSING_GRAB)
5036     return TRUE;
5037
5038   tree_view = PSPP_SHEET_VIEW (widget);
5039
5040   if (tree_view->priv->prelight_node >= 0)
5041     _pspp_sheet_view_queue_draw_node (tree_view,
5042                                    tree_view->priv->prelight_node,
5043                                    NULL);
5044
5045   tree_view->priv->event_last_x = -10000;
5046   tree_view->priv->event_last_y = -10000;
5047
5048   prelight_or_select (tree_view,
5049                       -1,
5050                       -1000, -1000); /* coords not possibly over an arrow */
5051
5052   return TRUE;
5053 }
5054
5055
5056 static gint
5057 pspp_sheet_view_focus_out (GtkWidget     *widget,
5058                          GdkEventFocus *event)
5059 {
5060   PsppSheetView *tree_view;
5061
5062   tree_view = PSPP_SHEET_VIEW (widget);
5063
5064   gtk_widget_queue_draw (widget);
5065
5066   /* destroy interactive search dialog */
5067   if (tree_view->priv->search_window)
5068     pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
5069
5070   return FALSE;
5071 }
5072
5073
5074 /* Incremental Reflow
5075  */
5076
5077 static void
5078 pspp_sheet_view_node_queue_redraw (PsppSheetView *tree_view,
5079                                  int node)
5080 {
5081   GtkAllocation allocation;
5082   gint y = pspp_sheet_view_node_find_offset (tree_view, node)
5083     - gtk_adjustment_get_value (tree_view->priv->vadjustment)
5084     + TREE_VIEW_HEADER_HEIGHT (tree_view);
5085
5086   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
5087
5088   gtk_widget_queue_draw_area (GTK_WIDGET (tree_view),
5089                               0, y,
5090                               allocation.width,
5091                               tree_view->priv->fixed_height);
5092 }
5093
5094 static gboolean
5095 node_is_visible (PsppSheetView *tree_view,
5096                  int node)
5097 {
5098   int y;
5099   int height;
5100
5101   y = pspp_sheet_view_node_find_offset (tree_view, node);
5102   height = ROW_HEIGHT (tree_view);
5103
5104   if (y >= gtk_adjustment_get_value (tree_view->priv->vadjustment) &&
5105       y + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
5106                      + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5107     return TRUE;
5108
5109   return FALSE;
5110 }
5111
5112 /* Returns the row height. */
5113 static gint
5114 validate_row (PsppSheetView *tree_view,
5115               int node,
5116               GtkTreeIter *iter,
5117               GtkTreePath *path)
5118 {
5119   PsppSheetViewColumn *column;
5120   GList *list, *first_column, *last_column;
5121   gint height = 0;
5122   gint horizontal_separator;
5123   gint vertical_separator;
5124   gint focus_line_width;
5125   gboolean draw_vgrid_lines, draw_hgrid_lines;
5126   gint focus_pad;
5127   gint grid_line_width;
5128   gboolean wide_separators;
5129   gint separator_height;
5130
5131   gtk_widget_style_get (GTK_WIDGET (tree_view),
5132                         "focus-padding", &focus_pad,
5133                         "focus-line-width", &focus_line_width,
5134                         "horizontal-separator", &horizontal_separator,
5135                         "vertical-separator", &vertical_separator,
5136                         "grid-line-width", &grid_line_width,
5137                         "wide-separators",  &wide_separators,
5138                         "separator-height", &separator_height,
5139                         NULL);
5140   
5141   draw_vgrid_lines =
5142     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
5143     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5144   draw_hgrid_lines =
5145     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
5146     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5147
5148   for (last_column = g_list_last (tree_view->priv->columns);
5149        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
5150        last_column = last_column->prev)
5151     ;
5152
5153   for (first_column = g_list_first (tree_view->priv->columns);
5154        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
5155        first_column = first_column->next)
5156     ;
5157
5158   for (list = tree_view->priv->columns; list; list = list->next)
5159     {
5160       gint tmp_width;
5161       gint tmp_height;
5162
5163       column = list->data;
5164
5165       if (! column->visible)
5166         continue;
5167
5168       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, iter);
5169       pspp_sheet_view_column_cell_get_size (column,
5170                                           NULL, NULL, NULL,
5171                                           &tmp_width, &tmp_height);
5172
5173       tmp_height += vertical_separator;
5174       height = MAX (height, tmp_height);
5175
5176       tmp_width = tmp_width + horizontal_separator;
5177
5178       if (draw_vgrid_lines)
5179         {
5180           if (list->data == first_column || list->data == last_column)
5181             tmp_width += grid_line_width / 2.0;
5182           else
5183             tmp_width += grid_line_width;
5184         }
5185
5186       if (tmp_width > column->requested_width)
5187         column->requested_width = tmp_width;
5188     }
5189
5190   if (draw_hgrid_lines)
5191     height += grid_line_width;
5192
5193   tree_view->priv->post_validation_flag = TRUE;
5194   return height;
5195 }
5196
5197
5198 static void
5199 validate_visible_area (PsppSheetView *tree_view)
5200 {
5201   GtkTreePath *path = NULL;
5202   GtkTreePath *above_path = NULL;
5203   GtkTreeIter iter;
5204   int node = -1;
5205   gboolean size_changed = FALSE;
5206   gint total_height;
5207   gint area_above = 0;
5208   gint area_below = 0;
5209   GtkAllocation allocation;
5210
5211   if (tree_view->priv->row_count == 0)
5212     return;
5213
5214   if (tree_view->priv->scroll_to_path == NULL)
5215     return;
5216
5217   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
5218
5219   total_height = allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
5220
5221   if (total_height == 0)
5222     return;
5223
5224   path = gtk_tree_row_reference_get_path (tree_view->priv->scroll_to_path);
5225   if (path)
5226     {
5227       /* we are going to scroll, and will update dy */
5228       _pspp_sheet_view_find_node (tree_view, path, &node);
5229       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5230
5231       if (tree_view->priv->scroll_to_use_align)
5232         {
5233           gint height = ROW_HEIGHT (tree_view);
5234           area_above = (total_height - height) *
5235             tree_view->priv->scroll_to_row_align;
5236           area_below = total_height - area_above - height;
5237           area_above = MAX (area_above, 0);
5238           area_below = MAX (area_below, 0);
5239         }
5240       else
5241         {
5242           /* two cases:
5243            * 1) row not visible
5244            * 2) row visible
5245            */
5246           gint dy;
5247           gint height = ROW_HEIGHT (tree_view);
5248
5249           dy = pspp_sheet_view_node_find_offset (tree_view, node);
5250
5251           if (dy >= gtk_adjustment_get_value (tree_view->priv->vadjustment) &&
5252               dy + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
5253                               + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5254             {
5255               /* row visible: keep the row at the same position */
5256               area_above = dy - gtk_adjustment_get_value (tree_view->priv->vadjustment);
5257               area_below = (gtk_adjustment_get_value (tree_view->priv->vadjustment) +
5258                             gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5259                 - dy - height;
5260             }
5261           else
5262             {
5263               /* row not visible */
5264               if (dy >= 0
5265                   && dy + height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5266                 {
5267                   /* row at the beginning -- fixed */
5268                   area_above = dy;
5269                   area_below = gtk_adjustment_get_page_size (tree_view->priv->vadjustment)
5270                     - area_above - height;
5271                 }
5272               else if (dy >= (gtk_adjustment_get_upper (tree_view->priv->vadjustment) -
5273                               gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5274                 {
5275                   /* row at the end -- fixed */
5276                   area_above = dy - (gtk_adjustment_get_upper (tree_view->priv->vadjustment) -
5277                                      gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
5278                   area_below = gtk_adjustment_get_page_size (tree_view->priv->vadjustment) -
5279                     area_above - height;
5280
5281                   if (area_below < 0)
5282                     {
5283                       area_above = gtk_adjustment_get_page_size (tree_view->priv->vadjustment) - height;
5284                       area_below = 0;
5285                     }
5286                 }
5287               else
5288                 {
5289                   /* row somewhere in the middle, bring it to the top
5290                    * of the view
5291                    */
5292                   area_above = 0;
5293                   area_below = total_height - height;
5294                 }
5295             }
5296         }
5297     }
5298   else
5299     /* the scroll to isn't valid; ignore it.
5300      */
5301     {
5302       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
5303       tree_view->priv->scroll_to_path = NULL;
5304       return;
5305     }
5306
5307   above_path = gtk_tree_path_copy (path);
5308
5309   /* Now, we walk forwards and backwards, measuring rows. Unfortunately,
5310    * backwards is much slower then forward, as there is no iter_prev function.
5311    * We go forwards first in case we run out of tree.  Then we go backwards to
5312    * fill out the top.
5313    */
5314   while (node >= 0 && area_below > 0)
5315     {
5316       gboolean done = FALSE;
5317       do
5318         {
5319           node = pspp_sheet_view_node_next (tree_view, node);
5320           if (node >= 0)
5321             {
5322               gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
5323               done = TRUE;
5324               gtk_tree_path_next (path);
5325
5326               /* Sanity Check! */
5327               TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
5328             }
5329           else
5330             break;
5331         }
5332       while (!done);
5333
5334       if (node < 0)
5335         break;
5336
5337       area_below -= ROW_HEIGHT (tree_view);
5338     }
5339   gtk_tree_path_free (path);
5340
5341   /* If we ran out of tree, and have extra area_below left, we need to add it
5342    * to area_above */
5343   if (area_below > 0)
5344     area_above += area_below;
5345
5346   _pspp_sheet_view_find_node (tree_view, above_path, &node);
5347
5348   /* We walk backwards */
5349   while (area_above > 0)
5350     {
5351       node = pspp_sheet_view_node_prev (tree_view, node);
5352
5353       /* Always find the new path in the tree.  We cannot just assume
5354        * a gtk_tree_path_prev() is enough here, as there might be children
5355        * in between this node and the previous sibling node.  If this
5356        * appears to be a performance hotspot in profiles, we can look into
5357        * intrigate logic for keeping path, node and iter in sync like
5358        * we do for forward walks.  (Which will be hard because of the lacking
5359        * iter_prev).
5360        */
5361
5362       if (node < 0)
5363         break;
5364
5365       gtk_tree_path_free (above_path);
5366       above_path = _pspp_sheet_view_find_path (tree_view, node);
5367
5368       gtk_tree_model_get_iter (tree_view->priv->model, &iter, above_path);
5369
5370       area_above -= ROW_HEIGHT (tree_view);
5371     }
5372
5373   /* set the dy here to scroll to the path,
5374    * and sync the top row accordingly
5375    */
5376   pspp_sheet_view_set_top_row (tree_view, above_path, -area_above);
5377   pspp_sheet_view_top_row_to_dy (tree_view);
5378
5379   /* update width/height and queue a resize */
5380   if (size_changed)
5381     {
5382       GtkRequisition requisition;
5383
5384       /* We temporarily guess a size, under the assumption that it will be the
5385        * same when we get our next size_allocate.  If we don't do this, we'll be
5386        * in an inconsistent state if we call top_row_to_dy. */
5387
5388       gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
5389       gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->hadjustment), (gfloat)requisition.width));
5390       gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->vadjustment), (gfloat)requisition.height));
5391       gtk_adjustment_changed (tree_view->priv->hadjustment);
5392       gtk_adjustment_changed (tree_view->priv->vadjustment);
5393       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
5394     }
5395
5396   gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
5397   tree_view->priv->scroll_to_path = NULL;
5398
5399   if (above_path)
5400     gtk_tree_path_free (above_path);
5401
5402   if (tree_view->priv->scroll_to_column)
5403     {
5404       tree_view->priv->scroll_to_column = NULL;
5405     }
5406   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
5407 }
5408
5409 static void
5410 initialize_fixed_height_mode (PsppSheetView *tree_view)
5411 {
5412   if (!tree_view->priv->row_count)
5413     return;
5414
5415   if (tree_view->priv->fixed_height_set)
5416     return;
5417
5418   if (tree_view->priv->fixed_height < 0)
5419     {
5420       GtkTreeIter iter;
5421       GtkTreePath *path;
5422
5423       int node = 0;
5424
5425       path = _pspp_sheet_view_find_path (tree_view, node);
5426       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5427
5428       tree_view->priv->fixed_height = validate_row (tree_view, node, &iter, path);
5429
5430       gtk_tree_path_free (path);
5431
5432       g_object_notify (G_OBJECT (tree_view), "fixed-height");
5433     }
5434 }
5435
5436 /* Our strategy for finding nodes to validate is a little convoluted.  We find
5437  * the left-most uninvalidated node.  We then try walking right, validating
5438  * nodes.  Once we find a valid node, we repeat the previous process of finding
5439  * the first invalid node.
5440  */
5441
5442 static gboolean
5443 validate_rows_handler (PsppSheetView *tree_view)
5444 {
5445   initialize_fixed_height_mode (tree_view);
5446   if (tree_view->priv->validate_rows_timer)
5447     {
5448       g_source_remove (tree_view->priv->validate_rows_timer);
5449       tree_view->priv->validate_rows_timer = 0;
5450     }
5451
5452   return FALSE;
5453 }
5454
5455 static gboolean
5456 do_presize_handler (PsppSheetView *tree_view)
5457 {
5458   GtkRequisition requisition;
5459
5460   validate_visible_area (tree_view);
5461   tree_view->priv->presize_handler_timer = 0;
5462
5463   if (! gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5464     return FALSE;
5465
5466   gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
5467
5468   gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->hadjustment), (gfloat)requisition.width));
5469   gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->vadjustment), (gfloat)requisition.height));
5470   gtk_adjustment_changed (tree_view->priv->hadjustment);
5471   gtk_adjustment_changed (tree_view->priv->vadjustment);
5472   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
5473                    
5474   return FALSE;
5475 }
5476
5477 static gboolean
5478 presize_handler_callback (gpointer data)
5479 {
5480   do_presize_handler (PSPP_SHEET_VIEW (data));
5481                    
5482   return FALSE;
5483 }
5484
5485 static void
5486 install_presize_handler (PsppSheetView *tree_view)
5487 {
5488   if (! gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5489     return;
5490
5491   if (! tree_view->priv->presize_handler_timer)
5492     {
5493       tree_view->priv->presize_handler_timer =
5494         gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, presize_handler_callback, tree_view, NULL);
5495     }
5496   if (! tree_view->priv->validate_rows_timer)
5497     {
5498       tree_view->priv->validate_rows_timer =
5499         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_VALIDATE, (GSourceFunc) validate_rows_handler, tree_view, NULL);
5500     }
5501 }
5502
5503 static gboolean
5504 scroll_sync_handler (PsppSheetView *tree_view)
5505 {
5506   if (tree_view->priv->height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5507     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
5508   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
5509     pspp_sheet_view_top_row_to_dy (tree_view);
5510   else
5511     pspp_sheet_view_dy_to_top_row (tree_view);
5512
5513   tree_view->priv->scroll_sync_timer = 0;
5514
5515   return FALSE;
5516 }
5517
5518 static void
5519 install_scroll_sync_handler (PsppSheetView *tree_view)
5520 {
5521   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5522     return;
5523
5524   if (!tree_view->priv->scroll_sync_timer)
5525     {
5526       tree_view->priv->scroll_sync_timer =
5527         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC, (GSourceFunc) scroll_sync_handler, tree_view, NULL);
5528     }
5529 }
5530
5531 static void
5532 pspp_sheet_view_set_top_row (PsppSheetView *tree_view,
5533                            GtkTreePath *path,
5534                            gint         offset)
5535 {
5536   gtk_tree_row_reference_free (tree_view->priv->top_row);
5537
5538   if (!path)
5539     {
5540       tree_view->priv->top_row = NULL;
5541       tree_view->priv->top_row_dy = 0;
5542     }
5543   else
5544     {
5545       tree_view->priv->top_row = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
5546       tree_view->priv->top_row_dy = offset;
5547     }
5548 }
5549
5550 /* Always call this iff dy is in the visible range.  If the tree is empty, then
5551  * it's set to be NULL, and top_row_dy is 0;
5552  */
5553 static void
5554 pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view)
5555 {
5556   gint offset;
5557   GtkTreePath *path;
5558   int node;
5559
5560   if (tree_view->priv->row_count == 0)
5561     {
5562       pspp_sheet_view_set_top_row (tree_view, NULL, 0);
5563     }
5564   else
5565     {
5566       offset = pspp_sheet_view_find_offset (tree_view,
5567                                             tree_view->priv->dy,
5568                                             &node);
5569
5570       if (node < 0)
5571         {
5572           pspp_sheet_view_set_top_row (tree_view, NULL, 0);
5573         }
5574       else
5575         {
5576           path = _pspp_sheet_view_find_path (tree_view, node);
5577           pspp_sheet_view_set_top_row (tree_view, path, offset);
5578           gtk_tree_path_free (path);
5579         }
5580     }
5581 }
5582
5583 static void
5584 pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view)
5585 {
5586   GtkTreePath *path;
5587   int node;
5588   int new_dy;
5589
5590   /* Avoid recursive calls */
5591   if (tree_view->priv->in_top_row_to_dy)
5592     return;
5593
5594   if (tree_view->priv->top_row)
5595     path = gtk_tree_row_reference_get_path (tree_view->priv->top_row);
5596   else
5597     path = NULL;
5598
5599   if (!path)
5600     node = -1;
5601   else
5602     _pspp_sheet_view_find_node (tree_view, path, &node);
5603
5604   if (path)
5605     gtk_tree_path_free (path);
5606
5607   if (node < 0)
5608     {
5609       /* keep dy and set new toprow */
5610       gtk_tree_row_reference_free (tree_view->priv->top_row);
5611       tree_view->priv->top_row = NULL;
5612       tree_view->priv->top_row_dy = 0;
5613       /* DO NOT install the idle handler */
5614       pspp_sheet_view_dy_to_top_row (tree_view);
5615       return;
5616     }
5617
5618   if (ROW_HEIGHT (tree_view) < tree_view->priv->top_row_dy)
5619     {
5620       /* new top row -- do NOT install the idle handler */
5621       pspp_sheet_view_dy_to_top_row (tree_view);
5622       return;
5623     }
5624
5625   new_dy = pspp_sheet_view_node_find_offset (tree_view, node);
5626   new_dy += tree_view->priv->top_row_dy;
5627
5628   if (new_dy + gtk_adjustment_get_page_size (tree_view->priv->vadjustment) > tree_view->priv->height)
5629     new_dy = tree_view->priv->height - gtk_adjustment_get_page_size (tree_view->priv->vadjustment);
5630
5631   new_dy = MAX (0, new_dy);
5632
5633   tree_view->priv->in_top_row_to_dy = TRUE;
5634   gtk_adjustment_set_value (tree_view->priv->vadjustment, (gdouble)new_dy);
5635   tree_view->priv->in_top_row_to_dy = FALSE;
5636 }
5637
5638
5639 void
5640 _pspp_sheet_view_install_mark_rows_col_dirty (PsppSheetView *tree_view)
5641 {
5642   install_presize_handler (tree_view);
5643 }
5644
5645 /* Drag-and-drop */
5646
5647 static void
5648 set_source_row (GdkDragContext *context,
5649                 GtkTreeModel   *model,
5650                 GtkTreePath    *source_row)
5651 {
5652   g_object_set_data_full (G_OBJECT (context),
5653                           "gtk-tree-view-source-row",
5654                           source_row ? gtk_tree_row_reference_new (model, source_row) : NULL,
5655                           (GDestroyNotify) (source_row ? gtk_tree_row_reference_free : NULL));
5656 }
5657
5658 static GtkTreePath*
5659 get_source_row (GdkDragContext *context)
5660 {
5661   GtkTreeRowReference *ref =
5662     g_object_get_data (G_OBJECT (context), "gtk-tree-view-source-row");
5663
5664   if (ref)
5665     return gtk_tree_row_reference_get_path (ref);
5666   else
5667     return NULL;
5668 }
5669
5670 typedef struct
5671 {
5672   GtkTreeRowReference *dest_row;
5673   guint                path_down_mode   : 1;
5674   guint                empty_view_drop  : 1;
5675   guint                drop_append_mode : 1;
5676 }
5677 DestRow;
5678
5679 static void
5680 dest_row_free (gpointer data)
5681 {
5682   DestRow *dr = (DestRow *)data;
5683
5684   gtk_tree_row_reference_free (dr->dest_row);
5685   g_slice_free (DestRow, dr);
5686 }
5687
5688 static void
5689 set_dest_row (GdkDragContext *context,
5690               GtkTreeModel   *model,
5691               GtkTreePath    *dest_row,
5692               gboolean        path_down_mode,
5693               gboolean        empty_view_drop,
5694               gboolean        drop_append_mode)
5695 {
5696   DestRow *dr;
5697
5698   if (!dest_row)
5699     {
5700       g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
5701                               NULL, NULL);
5702       return;
5703     }
5704
5705   dr = g_slice_new (DestRow);
5706
5707   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5708   dr->path_down_mode = path_down_mode != FALSE;
5709   dr->empty_view_drop = empty_view_drop != FALSE;
5710   dr->drop_append_mode = drop_append_mode != FALSE;
5711
5712   g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
5713                           dr, (GDestroyNotify) dest_row_free);
5714 }
5715
5716 static GtkTreePath*
5717 get_dest_row (GdkDragContext *context,
5718               gboolean       *path_down_mode)
5719 {
5720   DestRow *dr =
5721     g_object_get_data (G_OBJECT (context), "gtk-tree-view-dest-row");
5722
5723   if (dr)
5724     {
5725       GtkTreePath *path = NULL;
5726
5727       if (path_down_mode)
5728         *path_down_mode = dr->path_down_mode;
5729
5730       if (dr->dest_row)
5731         path = gtk_tree_row_reference_get_path (dr->dest_row);
5732       else if (dr->empty_view_drop)
5733         path = gtk_tree_path_new_from_indices (0, -1);
5734       else
5735         path = NULL;
5736
5737       if (path && dr->drop_append_mode)
5738         gtk_tree_path_next (path);
5739
5740       return path;
5741     }
5742   else
5743     return NULL;
5744 }
5745
5746 /* Get/set whether drag_motion requested the drag data and
5747  * drag_data_received should thus not actually insert the data,
5748  * since the data doesn't result from a drop.
5749  */
5750 static void
5751 set_status_pending (GdkDragContext *context,
5752                     GdkDragAction   suggested_action)
5753 {
5754   g_object_set_data (G_OBJECT (context),
5755                      "gtk-tree-view-status-pending",
5756                      GINT_TO_POINTER (suggested_action));
5757 }
5758
5759 static GdkDragAction
5760 get_status_pending (GdkDragContext *context)
5761 {
5762   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5763                                              "gtk-tree-view-status-pending"));
5764 }
5765
5766 static TreeViewDragInfo*
5767 get_info (PsppSheetView *tree_view)
5768 {
5769   return g_object_get_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info");
5770 }
5771
5772 static void
5773 destroy_info (TreeViewDragInfo *di)
5774 {
5775   g_slice_free (TreeViewDragInfo, di);
5776 }
5777
5778 static TreeViewDragInfo*
5779 ensure_info (PsppSheetView *tree_view)
5780 {
5781   TreeViewDragInfo *di;
5782
5783   di = get_info (tree_view);
5784
5785   if (di == NULL)
5786     {
5787       di = g_slice_new0 (TreeViewDragInfo);
5788
5789       g_object_set_data_full (G_OBJECT (tree_view),
5790                               "gtk-tree-view-drag-info",
5791                               di,
5792                               (GDestroyNotify) destroy_info);
5793     }
5794
5795   return di;
5796 }
5797
5798 static void
5799 remove_info (PsppSheetView *tree_view)
5800 {
5801   g_object_set_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info", NULL);
5802 }
5803
5804 #if 0
5805 static gint
5806 drag_scan_timeout (gpointer data)
5807 {
5808   PsppSheetView *tree_view;
5809   gint x, y;
5810   GdkModifierType state;
5811   GtkTreePath *path = NULL;
5812   PsppSheetViewColumn *column = NULL;
5813   GdkRectangle visible_rect;
5814
5815   GDK_THREADS_ENTER ();
5816
5817   tree_view = PSPP_SHEET_VIEW (data);
5818
5819   gdk_window_get_pointer (tree_view->priv->bin_window,
5820                           &x, &y, &state);
5821
5822   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
5823
5824   /* See if we are near the edge. */
5825   if ((x - visible_rect.x) < SCROLL_EDGE_SIZE ||
5826       (visible_rect.x + visible_rect.width - x) < SCROLL_EDGE_SIZE ||
5827       (y - visible_rect.y) < SCROLL_EDGE_SIZE ||
5828       (visible_rect.y + visible_rect.height - y) < SCROLL_EDGE_SIZE)
5829     {
5830       pspp_sheet_view_get_path_at_pos (tree_view,
5831                                      tree_view->priv->bin_window,
5832                                      x, y,
5833                                      &path,
5834                                      &column,
5835                                      NULL,
5836                                      NULL);
5837
5838       if (path != NULL)
5839         {
5840           pspp_sheet_view_scroll_to_cell (tree_view,
5841                                         path,
5842                                         column,
5843                                         TRUE,
5844                                         0.5, 0.5);
5845
5846           gtk_tree_path_free (path);
5847         }
5848     }
5849
5850   GDK_THREADS_LEAVE ();
5851
5852   return TRUE;
5853 }
5854 #endif /* 0 */
5855
5856 static void
5857 add_scroll_timeout (PsppSheetView *tree_view)
5858 {
5859   if (tree_view->priv->scroll_timeout == 0)
5860     {
5861       tree_view->priv->scroll_timeout =
5862         gdk_threads_add_timeout (150, scroll_row_timeout, tree_view);
5863     }
5864 }
5865
5866 static void
5867 remove_scroll_timeout (PsppSheetView *tree_view)
5868 {
5869   if (tree_view->priv->scroll_timeout != 0)
5870     {
5871       g_source_remove (tree_view->priv->scroll_timeout);
5872       tree_view->priv->scroll_timeout = 0;
5873     }
5874 }
5875
5876 static gboolean
5877 check_model_dnd (GtkTreeModel *model,
5878                  GType         required_iface,
5879                  const gchar  *signal)
5880 {
5881   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5882     {
5883       g_warning ("You must override the default '%s' handler "
5884                  "on PsppSheetView when using models that don't support "
5885                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5886                  "is to connect to '%s' and call "
5887                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5888                  "the default handler from running. Look at the source code "
5889                  "for the default handler in gtktreeview.c to get an idea what "
5890                  "your handler should do. (gtktreeview.c is in the GTK source "
5891                  "code.) If you're using GTK from a language other than C, "
5892                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5893                  signal, g_type_name (required_iface), signal);
5894       return FALSE;
5895     }
5896   else
5897     return TRUE;
5898 }
5899
5900 static gboolean
5901 scroll_row_timeout (gpointer data)
5902 {
5903   PsppSheetView *tree_view = data;
5904
5905   pspp_sheet_view_horizontal_autoscroll (tree_view);
5906   pspp_sheet_view_vertical_autoscroll (tree_view);
5907
5908   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
5909     pspp_sheet_view_update_rubber_band (tree_view);
5910
5911   return TRUE;
5912 }
5913
5914 /* Returns TRUE if event should not be propagated to parent widgets */
5915 static gboolean
5916 set_destination_row (PsppSheetView    *tree_view,
5917                      GdkDragContext *context,
5918                      /* coordinates relative to the widget */
5919                      gint            x,
5920                      gint            y,
5921                      GdkDragAction  *suggested_action,
5922                      GdkAtom        *target)
5923 {
5924   GtkTreePath *path = NULL;
5925   PsppSheetViewDropPosition pos;
5926   PsppSheetViewDropPosition old_pos;
5927   TreeViewDragInfo *di;
5928   GtkWidget *widget;
5929   GtkTreePath *old_dest_path = NULL;
5930   gboolean can_drop = FALSE;
5931
5932   *suggested_action = 0;
5933   *target = GDK_NONE;
5934
5935   widget = GTK_WIDGET (tree_view);
5936
5937   di = get_info (tree_view);
5938
5939   if (di == NULL || y - TREE_VIEW_HEADER_HEIGHT (tree_view) < 0)
5940     {
5941       /* someone unset us as a drag dest, note that if
5942        * we return FALSE drag_leave isn't called
5943        */
5944
5945       pspp_sheet_view_set_drag_dest_row (tree_view,
5946                                        NULL,
5947                                        PSPP_SHEET_VIEW_DROP_BEFORE);
5948
5949       remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
5950
5951       return FALSE; /* no longer a drop site */
5952     }
5953
5954   *target = gtk_drag_dest_find_target (widget, context,
5955                                        gtk_drag_dest_get_target_list (widget));
5956   if (*target == GDK_NONE)
5957     {
5958       return FALSE;
5959     }
5960
5961   if (!pspp_sheet_view_get_dest_row_at_pos (tree_view,
5962                                           x, y,
5963                                           &path,
5964                                           &pos))
5965     {
5966       gint n_children;
5967       GtkTreeModel *model;
5968
5969       /* the row got dropped on empty space, let's setup a special case
5970        */
5971
5972       if (path)
5973         gtk_tree_path_free (path);
5974
5975       model = pspp_sheet_view_get_model (tree_view);
5976
5977       n_children = gtk_tree_model_iter_n_children (model, NULL);
5978       if (n_children)
5979         {
5980           pos = PSPP_SHEET_VIEW_DROP_AFTER;
5981           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
5982         }
5983       else
5984         {
5985           pos = PSPP_SHEET_VIEW_DROP_BEFORE;
5986           path = gtk_tree_path_new_from_indices (0, -1);
5987         }
5988
5989       can_drop = TRUE;
5990
5991       goto out;
5992     }
5993
5994   g_assert (path);
5995
5996   /* If we left the current row's "open" zone, unset the timeout for
5997    * opening the row
5998    */
5999   pspp_sheet_view_get_drag_dest_row (tree_view,
6000                                    &old_dest_path,
6001                                    &old_pos);
6002
6003   if (old_dest_path)
6004     gtk_tree_path_free (old_dest_path);
6005
6006   if (TRUE /* FIXME if the location droppable predicate */)
6007     {
6008       can_drop = TRUE;
6009     }
6010
6011 out:
6012   if (can_drop)
6013     {
6014       GtkWidget *source_widget;
6015
6016       *suggested_action = gdk_drag_context_get_suggested_action (context);
6017       source_widget = gtk_drag_get_source_widget (context);
6018
6019       if (source_widget == widget)
6020         {
6021           /* Default to MOVE, unless the user has
6022            * pressed ctrl or shift to affect available actions
6023            */
6024           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6025             *suggested_action = GDK_ACTION_MOVE;
6026         }
6027
6028       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6029                                        path, pos);
6030     }
6031   else
6032     {
6033       /* can't drop here */
6034       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6035                                        NULL,
6036                                        PSPP_SHEET_VIEW_DROP_BEFORE);
6037     }
6038
6039   if (path)
6040     gtk_tree_path_free (path);
6041
6042   return TRUE;
6043 }
6044
6045 static GtkTreePath*
6046 get_logical_dest_row (PsppSheetView *tree_view,
6047                       gboolean    *path_down_mode,
6048                       gboolean    *drop_append_mode)
6049 {
6050   /* adjust path to point to the row the drop goes in front of */
6051   GtkTreePath *path = NULL;
6052   PsppSheetViewDropPosition pos;
6053
6054   g_return_val_if_fail (path_down_mode != NULL, NULL);
6055   g_return_val_if_fail (drop_append_mode != NULL, NULL);
6056
6057   *path_down_mode = FALSE;
6058   *drop_append_mode = 0;
6059
6060   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
6061
6062   if (path == NULL)
6063     return NULL;
6064
6065   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE)
6066     ; /* do nothing */
6067   else if (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE ||
6068            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER)
6069     *path_down_mode = TRUE;
6070   else
6071     {
6072       GtkTreeIter iter;
6073       GtkTreeModel *model = pspp_sheet_view_get_model (tree_view);
6074
6075       g_assert (pos == PSPP_SHEET_VIEW_DROP_AFTER);
6076
6077       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6078           !gtk_tree_model_iter_next (model, &iter))
6079         *drop_append_mode = 1;
6080       else
6081         {
6082           *drop_append_mode = 0;
6083           gtk_tree_path_next (path);
6084         }
6085     }
6086
6087   return path;
6088 }
6089
6090 static gboolean
6091 pspp_sheet_view_maybe_begin_dragging_row (PsppSheetView      *tree_view,
6092                                         GdkEventMotion   *event)
6093 {
6094   GtkWidget *widget = GTK_WIDGET (tree_view);
6095   GdkDragContext *context;
6096   TreeViewDragInfo *di;
6097   GtkTreePath *path = NULL;
6098   gint button;
6099   gint cell_x, cell_y;
6100   GtkTreeModel *model;
6101   gboolean retval = FALSE;
6102
6103   di = get_info (tree_view);
6104
6105   if (di == NULL || !di->source_set)
6106     goto out;
6107
6108   if (tree_view->priv->pressed_button < 0)
6109     goto out;
6110
6111   if (!gtk_drag_check_threshold (widget,
6112                                  tree_view->priv->press_start_x,
6113                                  tree_view->priv->press_start_y,
6114                                  event->x, event->y))
6115     goto out;
6116
6117   model = pspp_sheet_view_get_model (tree_view);
6118
6119   if (model == NULL)
6120     goto out;
6121
6122   button = tree_view->priv->pressed_button;
6123   tree_view->priv->pressed_button = -1;
6124
6125   pspp_sheet_view_get_path_at_pos (tree_view,
6126                                  tree_view->priv->press_start_x,
6127                                  tree_view->priv->press_start_y,
6128                                  &path,
6129                                  NULL,
6130                                  &cell_x,
6131                                  &cell_y);
6132
6133   if (path == NULL)
6134     goto out;
6135
6136   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6137       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6138                                            path))
6139     goto out;
6140
6141   if (!(GDK_BUTTON1_MASK << (button - 1) & di->start_button_mask))
6142     goto out;
6143
6144   /* Now we can begin the drag */
6145
6146   retval = TRUE;
6147
6148   context = gtk_drag_begin (widget,
6149                             gtk_drag_source_get_target_list (widget),
6150                             di->source_actions,
6151                             button,
6152                             (GdkEvent*)event);
6153
6154   set_source_row (context, model, path);
6155
6156  out:
6157   if (path)
6158     gtk_tree_path_free (path);
6159
6160   return retval;
6161 }
6162
6163
6164
6165 static void
6166 pspp_sheet_view_drag_begin (GtkWidget      *widget,
6167                           GdkDragContext *context)
6168 {
6169 #if GTK3_TRANSITION
6170   PsppSheetView *tree_view;
6171   GtkTreePath *path = NULL;
6172   gint cell_x, cell_y;
6173   GdkPixmap *row_pix;
6174   TreeViewDragInfo *di;
6175
6176   tree_view = PSPP_SHEET_VIEW (widget);
6177
6178   /* if the user uses a custom DND source impl, we don't set the icon here */
6179   di = get_info (tree_view);
6180
6181   if (di == NULL || !di->source_set)
6182     return;
6183
6184   pspp_sheet_view_get_path_at_pos (tree_view,
6185                                  tree_view->priv->press_start_x,
6186                                  tree_view->priv->press_start_y,
6187                                  &path,
6188                                  NULL,
6189                                  &cell_x,
6190                                  &cell_y);
6191
6192   g_return_if_fail (path != NULL);
6193
6194   row_pix = pspp_sheet_view_create_row_drag_icon (tree_view,
6195                                                 path);
6196
6197   gtk_drag_set_icon_pixmap (context,
6198                             gdk_drawable_get_colormap (row_pix),
6199                             row_pix,
6200                             NULL,
6201                             /* the + 1 is for the black border in the icon */
6202                             tree_view->priv->press_start_x + 1,
6203                             cell_y + 1);
6204
6205   g_object_unref (row_pix);
6206   gtk_tree_path_free (path);
6207 #endif
6208 }
6209
6210
6211 static void
6212 pspp_sheet_view_drag_end (GtkWidget      *widget,
6213                         GdkDragContext *context)
6214 {
6215   /* do nothing */
6216 }
6217
6218 /* Default signal implementations for the drag signals */
6219 static void
6220 pspp_sheet_view_drag_data_get (GtkWidget        *widget,
6221                              GdkDragContext   *context,
6222                              GtkSelectionData *selection_data,
6223                              guint             info,
6224                              guint             time)
6225 {
6226   PsppSheetView *tree_view;
6227   GtkTreeModel *model;
6228   TreeViewDragInfo *di;
6229   GtkTreePath *source_row;
6230
6231   tree_view = PSPP_SHEET_VIEW (widget);
6232
6233   model = pspp_sheet_view_get_model (tree_view);
6234
6235   if (model == NULL)
6236     return;
6237
6238   di = get_info (PSPP_SHEET_VIEW (widget));
6239
6240   if (di == NULL)
6241     return;
6242
6243   source_row = get_source_row (context);
6244
6245   if (source_row == NULL)
6246     return;
6247
6248   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6249    * any model; for DragSource models there are some other targets
6250    * we also support.
6251    */
6252
6253   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6254       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6255                                           source_row,
6256                                           selection_data))
6257     goto done;
6258
6259   /* If drag_data_get does nothing, try providing row data. */
6260   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6261     {
6262       gtk_tree_set_row_drag_data (selection_data,
6263                                   model,
6264                                   source_row);
6265     }
6266
6267  done:
6268   gtk_tree_path_free (source_row);
6269 }
6270
6271
6272 static void
6273 pspp_sheet_view_drag_data_delete (GtkWidget      *widget,
6274                                 GdkDragContext *context)
6275 {
6276   TreeViewDragInfo *di;
6277   GtkTreeModel *model;
6278   PsppSheetView *tree_view;
6279   GtkTreePath *source_row;
6280
6281   tree_view = PSPP_SHEET_VIEW (widget);
6282   model = pspp_sheet_view_get_model (tree_view);
6283
6284   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
6285     return;
6286
6287   di = get_info (tree_view);
6288
6289   if (di == NULL)
6290     return;
6291
6292   source_row = get_source_row (context);
6293
6294   if (source_row == NULL)
6295     return;
6296
6297   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6298                                          source_row);
6299
6300   gtk_tree_path_free (source_row);
6301
6302   set_source_row (context, NULL, NULL);
6303 }
6304
6305 static void
6306 pspp_sheet_view_drag_leave (GtkWidget      *widget,
6307                           GdkDragContext *context,
6308                           guint             time)
6309 {
6310   /* unset any highlight row */
6311   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6312                                    NULL,
6313                                    PSPP_SHEET_VIEW_DROP_BEFORE);
6314
6315   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
6316 }
6317
6318
6319 static gboolean
6320 pspp_sheet_view_drag_motion (GtkWidget        *widget,
6321                            GdkDragContext   *context,
6322                            /* coordinates relative to the widget */
6323                            gint              x,
6324                            gint              y,
6325                            guint             time)
6326 {
6327   gboolean empty;
6328   GtkTreePath *path = NULL;
6329   PsppSheetViewDropPosition pos;
6330   PsppSheetView *tree_view;
6331   GdkDragAction suggested_action = 0;
6332   GdkAtom target;
6333
6334   tree_view = PSPP_SHEET_VIEW (widget);
6335
6336   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
6337     return FALSE;
6338
6339   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
6340
6341   /* we only know this *after* set_desination_row */
6342   empty = tree_view->priv->empty_view_drop;
6343
6344   if (path == NULL && !empty)
6345     {
6346       /* Can't drop here. */
6347       gdk_drag_status (context, 0, time);
6348     }
6349   else
6350     {
6351       if (tree_view->priv->open_dest_timeout == 0 &&
6352           (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER ||
6353            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE))
6354         {
6355           /* Nothing. */
6356         }
6357       else
6358         {
6359           add_scroll_timeout (tree_view);
6360         }
6361
6362       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6363         {
6364           /* Request data so we can use the source row when
6365            * determining whether to accept the drop
6366            */
6367           set_status_pending (context, suggested_action);
6368           gtk_drag_get_data (widget, context, target, time);
6369         }
6370       else
6371         {
6372           set_status_pending (context, 0);
6373           gdk_drag_status (context, suggested_action, time);
6374         }
6375     }
6376
6377   if (path)
6378     gtk_tree_path_free (path);
6379
6380   return TRUE;
6381 }
6382
6383
6384 static gboolean
6385 pspp_sheet_view_drag_drop (GtkWidget        *widget,
6386                          GdkDragContext   *context,
6387                          /* coordinates relative to the widget */
6388                          gint              x,
6389                          gint              y,
6390                          guint             time)
6391 {
6392   PsppSheetView *tree_view;
6393   GtkTreePath *path;
6394   GdkDragAction suggested_action = 0;
6395   GdkAtom target = GDK_NONE;
6396   TreeViewDragInfo *di;
6397   GtkTreeModel *model;
6398   gboolean path_down_mode;
6399   gboolean drop_append_mode;
6400
6401   tree_view = PSPP_SHEET_VIEW (widget);
6402
6403   model = pspp_sheet_view_get_model (tree_view);
6404
6405   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
6406
6407   di = get_info (tree_view);
6408
6409   if (di == NULL)
6410     return FALSE;
6411
6412   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
6413     return FALSE;
6414
6415   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
6416     return FALSE;
6417
6418   path = get_logical_dest_row (tree_view, &path_down_mode, &drop_append_mode);
6419
6420   if (target != GDK_NONE && path != NULL)
6421     {
6422       /* in case a motion had requested drag data, change things so we
6423        * treat drag data receives as a drop.
6424        */
6425       set_status_pending (context, 0);
6426       set_dest_row (context, model, path,
6427                     path_down_mode, tree_view->priv->empty_view_drop,
6428                     drop_append_mode);
6429     }
6430
6431   if (path)
6432     gtk_tree_path_free (path);
6433
6434   /* Unset this thing */
6435   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6436                                    NULL,
6437                                    PSPP_SHEET_VIEW_DROP_BEFORE);
6438
6439   if (target != GDK_NONE)
6440     {
6441       gtk_drag_get_data (widget, context, target, time);
6442       return TRUE;
6443     }
6444   else
6445     return FALSE;
6446 }
6447
6448 static void
6449 pspp_sheet_view_drag_data_received (GtkWidget        *widget,
6450                                   GdkDragContext   *context,
6451                                   /* coordinates relative to the widget */
6452                                   gint              x,
6453                                   gint              y,
6454                                   GtkSelectionData *selection_data,
6455                                   guint             info,
6456                                   guint             time)
6457 {
6458   GtkTreePath *path;
6459   TreeViewDragInfo *di;
6460   gboolean accepted = FALSE;
6461   GtkTreeModel *model;
6462   PsppSheetView *tree_view;
6463   GtkTreePath *dest_row;
6464   GdkDragAction suggested_action;
6465   gboolean path_down_mode;
6466   gboolean drop_append_mode;
6467
6468   tree_view = PSPP_SHEET_VIEW (widget);
6469
6470   model = pspp_sheet_view_get_model (tree_view);
6471
6472   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
6473     return;
6474
6475   di = get_info (tree_view);
6476
6477   if (di == NULL)
6478     return;
6479
6480   suggested_action = get_status_pending (context);
6481
6482   if (suggested_action)
6483     {
6484       /* We are getting this data due to a request in drag_motion,
6485        * rather than due to a request in drag_drop, so we are just
6486        * supposed to call drag_status, not actually paste in the
6487        * data.
6488        */
6489       path = get_logical_dest_row (tree_view, &path_down_mode,
6490                                    &drop_append_mode);
6491
6492       if (path == NULL)
6493         suggested_action = 0;
6494       else if (path_down_mode)
6495         gtk_tree_path_down (path);
6496
6497       if (suggested_action)
6498         {
6499           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6500                                                      path,
6501                                                      selection_data))
6502             {
6503               if (path_down_mode)
6504                 {
6505                   path_down_mode = FALSE;
6506                   gtk_tree_path_up (path);
6507
6508                   if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6509                                                              path,
6510                                                              selection_data))
6511                     suggested_action = 0;
6512                 }
6513               else
6514                 suggested_action = 0;
6515             }
6516         }
6517
6518       gdk_drag_status (context, suggested_action, time);
6519
6520       if (path)
6521         gtk_tree_path_free (path);
6522
6523       /* If you can't drop, remove user drop indicator until the next motion */
6524       if (suggested_action == 0)
6525         pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6526                                          NULL,
6527                                          PSPP_SHEET_VIEW_DROP_BEFORE);
6528
6529       return;
6530     }
6531
6532   dest_row = get_dest_row (context, &path_down_mode);
6533
6534   if (dest_row == NULL)
6535     return;
6536
6537   if (gtk_selection_data_get_length (selection_data) >= 0)
6538     {
6539       if (path_down_mode)
6540         {
6541           gtk_tree_path_down (dest_row);
6542           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6543                                                      dest_row, selection_data))
6544             gtk_tree_path_up (dest_row);
6545         }
6546     }
6547
6548   if (gtk_selection_data_get_length (selection_data) >= 0)
6549     {
6550       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6551                                                  dest_row,
6552                                                  selection_data))
6553         accepted = TRUE;
6554     }
6555
6556   gtk_drag_finish (context,
6557                    accepted,
6558                    (gdk_drag_context_get_actions (context) == GDK_ACTION_MOVE),
6559                    time);
6560
6561   if (gtk_tree_path_get_depth (dest_row) == 1
6562       && gtk_tree_path_get_indices (dest_row)[0] == 0)
6563     {
6564       /* special special case drag to "0", scroll to first item */
6565       if (!tree_view->priv->scroll_to_path)
6566         pspp_sheet_view_scroll_to_cell (tree_view, dest_row, NULL, FALSE, 0.0, 0.0);
6567     }
6568
6569   gtk_tree_path_free (dest_row);
6570
6571   /* drop dest_row */
6572   set_dest_row (context, NULL, NULL, FALSE, FALSE, FALSE);
6573 }
6574
6575
6576
6577 /* GtkContainer Methods
6578  */
6579
6580
6581 static void
6582 pspp_sheet_view_remove (GtkContainer *container,
6583                       GtkWidget    *widget)
6584 {
6585   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6586   PsppSheetViewChild *child = NULL;
6587   GList *tmp_list;
6588
6589   tmp_list = tree_view->priv->children;
6590   while (tmp_list)
6591     {
6592       child = tmp_list->data;
6593       if (child->widget == widget)
6594         {
6595           gtk_widget_unparent (widget);
6596
6597           tree_view->priv->children = g_list_remove_link (tree_view->priv->children, tmp_list);
6598           g_list_free_1 (tmp_list);
6599           g_slice_free (PsppSheetViewChild, child);
6600           return;
6601         }
6602
6603       tmp_list = tmp_list->next;
6604     }
6605
6606   tmp_list = tree_view->priv->columns;
6607
6608   while (tmp_list)
6609     {
6610       PsppSheetViewColumn *column;
6611       column = tmp_list->data;
6612       if (column->button == widget)
6613         {
6614           gtk_widget_unparent (widget);
6615           return;
6616         }
6617       tmp_list = tmp_list->next;
6618     }
6619 }
6620
6621 static void
6622 pspp_sheet_view_forall (GtkContainer *container,
6623                       gboolean      include_internals,
6624                       GtkCallback   callback,
6625                       gpointer      callback_data)
6626 {
6627   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6628   PsppSheetViewChild *child = NULL;
6629   PsppSheetViewColumn *column;
6630   GList *tmp_list;
6631
6632   tmp_list = tree_view->priv->children;
6633   while (tmp_list)
6634     {
6635       child = tmp_list->data;
6636       tmp_list = tmp_list->next;
6637
6638       (* callback) (child->widget, callback_data);
6639     }
6640   if (include_internals == FALSE)
6641     return;
6642
6643   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
6644     {
6645       column = tmp_list->data;
6646
6647       if (column->button)
6648         (* callback) (column->button, callback_data);
6649     }
6650 }
6651
6652 /* Returns TRUE if the treeview contains no "special" (editable or activatable)
6653  * cells. If so we draw one big row-spanning focus rectangle.
6654  */
6655 static gboolean
6656 pspp_sheet_view_has_special_cell (PsppSheetView *tree_view)
6657 {
6658   GList *list;
6659
6660   if (tree_view->priv->special_cells != PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT)
6661     return tree_view->priv->special_cells = PSPP_SHEET_VIEW_SPECIAL_CELLS_YES;
6662
6663   for (list = tree_view->priv->columns; list; list = list->next)
6664     {
6665       if (!((PsppSheetViewColumn *)list->data)->visible)
6666         continue;
6667       if (_pspp_sheet_view_column_count_special_cells (list->data))
6668         return TRUE;
6669     }
6670
6671   return FALSE;
6672 }
6673
6674 static void
6675 pspp_sheet_view_focus_column (PsppSheetView *tree_view,
6676                               PsppSheetViewColumn *focus_column,
6677                               gboolean clamp_column_visible)
6678 {
6679   g_return_if_fail (focus_column != NULL);
6680
6681   tree_view->priv->focus_column = focus_column;
6682   if (!focus_column->button)
6683     {
6684       pspp_sheet_view_column_set_need_button (focus_column, TRUE);
6685       //      g_return_if_fail (focus_column->button != NULL);
6686       if (focus_column->button == NULL)
6687         return;
6688     }
6689
6690   if (gtk_container_get_focus_child (GTK_CONTAINER (tree_view)) != focus_column->button)
6691     gtk_widget_grab_focus (focus_column->button);
6692
6693   if (clamp_column_visible)
6694     pspp_sheet_view_clamp_column_visible (tree_view, focus_column, FALSE);
6695 }
6696
6697 /* Returns TRUE if the focus is within the headers, after the focus operation is
6698  * done
6699  */
6700 static gboolean
6701 pspp_sheet_view_header_focus (PsppSheetView      *tree_view,
6702                             GtkDirectionType  dir,
6703                             gboolean          clamp_column_visible)
6704 {
6705   GtkWidget *focus_child;
6706   PsppSheetViewColumn *focus_column;
6707   GList *last_column, *first_column;
6708   GList *tmp_list;
6709   gboolean rtl;
6710
6711   if (! PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
6712     return FALSE;
6713
6714   focus_child = gtk_container_get_focus_child (GTK_CONTAINER (tree_view));
6715
6716   first_column = tree_view->priv->columns;
6717   while (first_column)
6718     {
6719       PsppSheetViewColumn *c = PSPP_SHEET_VIEW_COLUMN (first_column->data);
6720
6721       if (pspp_sheet_view_column_can_focus (c) && c->visible)
6722         break;
6723       first_column = first_column->next;
6724     }
6725
6726   /* No headers are visible, or are focusable.  We can't focus in or out.
6727    */
6728   if (first_column == NULL)
6729     return FALSE;
6730
6731   last_column = g_list_last (tree_view->priv->columns);
6732   while (last_column)
6733     {
6734       PsppSheetViewColumn *c = PSPP_SHEET_VIEW_COLUMN (last_column->data);
6735
6736       if (pspp_sheet_view_column_can_focus (c) && c->visible)
6737         break;
6738       last_column = last_column->prev;
6739     }
6740
6741
6742   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
6743
6744   switch (dir)
6745     {
6746     case GTK_DIR_TAB_BACKWARD:
6747     case GTK_DIR_TAB_FORWARD:
6748     case GTK_DIR_UP:
6749     case GTK_DIR_DOWN:
6750       if (focus_child == NULL)
6751         {
6752           if (tree_view->priv->focus_column != NULL &&
6753               pspp_sheet_view_column_can_focus (tree_view->priv->focus_column))
6754             focus_column = tree_view->priv->focus_column;
6755           else
6756             focus_column = first_column->data;
6757           pspp_sheet_view_focus_column (tree_view, focus_column,
6758                                         clamp_column_visible);
6759           return TRUE;
6760         }
6761       return FALSE;
6762
6763     case GTK_DIR_LEFT:
6764     case GTK_DIR_RIGHT:
6765       if (focus_child == NULL)
6766         {
6767           if (tree_view->priv->focus_column != NULL)
6768             focus_column = tree_view->priv->focus_column;
6769           else if (dir == GTK_DIR_LEFT)
6770             focus_column = last_column->data;
6771           else
6772             focus_column = first_column->data;
6773           pspp_sheet_view_focus_column (tree_view, focus_column,
6774                                         clamp_column_visible);
6775           return TRUE;
6776         }
6777
6778       if (gtk_widget_child_focus (focus_child, dir))
6779         {
6780           /* The focus moves inside the button. */
6781           /* This is probably a great example of bad UI */
6782           if (clamp_column_visible)
6783             pspp_sheet_view_clamp_column_visible (tree_view,
6784                                                   tree_view->priv->focus_column,
6785                                                   FALSE);
6786           return TRUE;
6787         }
6788
6789       /* We need to move the focus among the row of buttons. */
6790       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
6791         if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)->button == focus_child)
6792           break;
6793
6794       if ((tmp_list == first_column && dir == (rtl ? GTK_DIR_RIGHT : GTK_DIR_LEFT))
6795           || (tmp_list == last_column && dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT)))
6796         {
6797           gtk_widget_error_bell (GTK_WIDGET (tree_view));
6798           return TRUE;
6799         }
6800
6801       while (tmp_list)
6802         {
6803           PsppSheetViewColumn *column;
6804
6805           if (dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT))
6806             tmp_list = tmp_list->next;
6807           else
6808             tmp_list = tmp_list->prev;
6809
6810           if (tmp_list == NULL)
6811             {
6812               g_warning ("Internal button not found");
6813               break;
6814             }
6815           column = tmp_list->data;
6816           if (column->visible &&
6817               pspp_sheet_view_column_can_focus (column))
6818             {
6819               pspp_sheet_view_column_set_need_button (column, TRUE);
6820               if (column->button)
6821                 {
6822                   pspp_sheet_view_focus_column (tree_view, column,
6823                                                 clamp_column_visible);
6824                   return TRUE;
6825                 }
6826             }
6827         }
6828       return FALSE;
6829
6830     default:
6831       g_assert_not_reached ();
6832       break;
6833     }
6834
6835   return FALSE;
6836 }
6837
6838 /* This function returns in 'path' the first focusable path, if the given path
6839  * is already focusable, it's the returned one.
6840  *
6841  */
6842 static gboolean
6843 search_first_focusable_path (PsppSheetView  *tree_view,
6844                              GtkTreePath **path,
6845                              gboolean      search_forward,
6846                              int *new_node)
6847 {
6848   /* XXX this function is trivial given that the sheetview doesn't support
6849      separator rows */
6850   int node = -1;
6851
6852   if (!path || !*path)
6853     return FALSE;
6854
6855   _pspp_sheet_view_find_node (tree_view, *path, &node);
6856
6857   if (node < 0)
6858     return FALSE;
6859
6860   if (new_node)
6861     *new_node = node;
6862
6863   return (*path != NULL);
6864 }
6865
6866 static gint
6867 pspp_sheet_view_focus (GtkWidget        *widget,
6868                      GtkDirectionType  direction)
6869 {
6870   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
6871   GtkContainer *container = GTK_CONTAINER (widget);
6872   GtkWidget *focus_child;
6873
6874   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget))
6875     return FALSE;
6876
6877   focus_child = gtk_container_get_focus_child (container);
6878
6879   pspp_sheet_view_stop_editing (PSPP_SHEET_VIEW (widget), FALSE);
6880   /* Case 1.  Headers currently have focus. */
6881   if (focus_child)
6882     {
6883       switch (direction)
6884         {
6885         case GTK_DIR_LEFT:
6886         case GTK_DIR_RIGHT:
6887           pspp_sheet_view_header_focus (tree_view, direction, TRUE);
6888           return TRUE;
6889         case GTK_DIR_TAB_BACKWARD:
6890         case GTK_DIR_UP:
6891           return FALSE;
6892         case GTK_DIR_TAB_FORWARD:
6893         case GTK_DIR_DOWN:
6894           gtk_widget_grab_focus (widget);
6895           return TRUE;
6896         default:
6897           g_assert_not_reached ();
6898           return FALSE;
6899         }
6900     }
6901
6902   /* Case 2. We don't have focus at all. */
6903   if (!gtk_widget_has_focus (widget))
6904     {
6905       if (!pspp_sheet_view_header_focus (tree_view, direction, FALSE))
6906         gtk_widget_grab_focus (widget);
6907       return TRUE;
6908     }
6909
6910   /* Case 3. We have focus already. */
6911   if (direction == GTK_DIR_TAB_BACKWARD)
6912     return (pspp_sheet_view_header_focus (tree_view, direction, FALSE));
6913   else if (direction == GTK_DIR_TAB_FORWARD)
6914     return FALSE;
6915
6916   /* Other directions caught by the keybindings */
6917   gtk_widget_grab_focus (widget);
6918   return TRUE;
6919 }
6920
6921 static void
6922 pspp_sheet_view_grab_focus (GtkWidget *widget)
6923 {
6924   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->grab_focus (widget);
6925
6926   pspp_sheet_view_focus_to_cursor (PSPP_SHEET_VIEW (widget));
6927 }
6928
6929 static void
6930 pspp_sheet_view_style_set (GtkWidget *widget,
6931                          GtkStyle *previous_style)
6932 {
6933   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
6934   GList *list;
6935   PsppSheetViewColumn *column;
6936
6937   if (gtk_widget_get_realized (widget))
6938     {
6939       gdk_window_set_background (tree_view->priv->bin_window, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
6940       gtk_style_set_background (gtk_widget_get_style (widget), tree_view->priv->header_window, GTK_STATE_NORMAL);
6941       pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
6942     }
6943
6944   gtk_widget_style_get (widget,
6945                         "expander-size", &tree_view->priv->expander_size,
6946                         NULL);
6947   tree_view->priv->expander_size += EXPANDER_EXTRA_PADDING;
6948
6949   for (list = tree_view->priv->columns; list; list = list->next)
6950     {
6951       column = list->data;
6952       _pspp_sheet_view_column_cell_set_dirty (column);
6953     }
6954
6955   tree_view->priv->fixed_height = -1;
6956
6957   /* Invalidate cached button style. */
6958   if (tree_view->priv->button_style)
6959     {
6960       g_object_unref (tree_view->priv->button_style);
6961       tree_view->priv->button_style = NULL;
6962     }
6963
6964   gtk_widget_queue_resize (widget);
6965 }
6966
6967
6968 static void
6969 pspp_sheet_view_set_focus_child (GtkContainer *container,
6970                                GtkWidget    *child)
6971 {
6972   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6973   GList *list;
6974
6975   for (list = tree_view->priv->columns; list; list = list->next)
6976     {
6977       if (PSPP_SHEET_VIEW_COLUMN (list->data)->button == child)
6978         {
6979           tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
6980           break;
6981         }
6982     }
6983
6984   GTK_CONTAINER_CLASS (pspp_sheet_view_parent_class)->set_focus_child (container, child);
6985 }
6986
6987 static void
6988 pspp_sheet_view_set_adjustments (PsppSheetView   *tree_view,
6989                                GtkAdjustment *hadj,
6990                                GtkAdjustment *vadj)
6991 {
6992   gboolean need_adjust = FALSE;
6993
6994   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
6995
6996   if (hadj)
6997     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6998   else
6999     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7000   if (vadj)
7001     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
7002   else
7003     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7004
7005   if (tree_view->priv->hadjustment && (tree_view->priv->hadjustment != hadj))
7006     {
7007       g_signal_handlers_disconnect_by_func (tree_view->priv->hadjustment,
7008                                             pspp_sheet_view_adjustment_changed,
7009                                             tree_view);
7010       g_object_unref (tree_view->priv->hadjustment);
7011     }
7012
7013   if (tree_view->priv->vadjustment && (tree_view->priv->vadjustment != vadj))
7014     {
7015       g_signal_handlers_disconnect_by_func (tree_view->priv->vadjustment,
7016                                             pspp_sheet_view_adjustment_changed,
7017                                             tree_view);
7018       g_object_unref (tree_view->priv->vadjustment);
7019     }
7020
7021   if (tree_view->priv->hadjustment != hadj)
7022     {
7023       tree_view->priv->hadjustment = hadj;
7024       g_object_ref_sink (tree_view->priv->hadjustment);
7025
7026       g_signal_connect (tree_view->priv->hadjustment, "value-changed",
7027                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
7028                         tree_view);
7029       need_adjust = TRUE;
7030     }
7031
7032   if (tree_view->priv->vadjustment != vadj)
7033     {
7034       tree_view->priv->vadjustment = vadj;
7035       g_object_ref_sink (tree_view->priv->vadjustment);
7036
7037       g_signal_connect (tree_view->priv->vadjustment, "value-changed",
7038                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
7039                         tree_view);
7040       need_adjust = TRUE;
7041     }
7042
7043   if (need_adjust)
7044     pspp_sheet_view_adjustment_changed (NULL, tree_view);
7045 }
7046
7047
7048 static gboolean
7049 pspp_sheet_view_real_move_cursor (PsppSheetView       *tree_view,
7050                                 GtkMovementStep    step,
7051                                 gint               count)
7052 {
7053   PsppSheetSelectMode mode;
7054   GdkModifierType state;
7055
7056   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
7057   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
7058                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
7059                         step == GTK_MOVEMENT_DISPLAY_LINES ||
7060                         step == GTK_MOVEMENT_PAGES ||
7061                         step == GTK_MOVEMENT_BUFFER_ENDS ||
7062                         step == GTK_MOVEMENT_DISPLAY_LINE_ENDS, FALSE);
7063
7064   if (tree_view->priv->row_count == 0)
7065     return FALSE;
7066   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
7067     return FALSE;
7068
7069   pspp_sheet_view_stop_editing (tree_view, FALSE);
7070   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
7071   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
7072
7073   mode = 0;
7074   if (gtk_get_current_event_state (&state))
7075     {
7076       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
7077         mode |= PSPP_SHEET_SELECT_MODE_TOGGLE;
7078       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
7079         mode |= PSPP_SHEET_SELECT_MODE_EXTEND;
7080     }
7081   /* else we assume not pressed */
7082
7083   switch (step)
7084     {
7085     case GTK_MOVEMENT_LOGICAL_POSITIONS:
7086       pspp_sheet_view_move_cursor_tab (tree_view, count);
7087       break;
7088     case GTK_MOVEMENT_VISUAL_POSITIONS:
7089       pspp_sheet_view_move_cursor_left_right (tree_view, count, mode);
7090       break;
7091     case GTK_MOVEMENT_DISPLAY_LINES:
7092       pspp_sheet_view_move_cursor_up_down (tree_view, count, mode);
7093       break;
7094     case GTK_MOVEMENT_PAGES:
7095       pspp_sheet_view_move_cursor_page_up_down (tree_view, count, mode);
7096       break;
7097     case GTK_MOVEMENT_BUFFER_ENDS:
7098       pspp_sheet_view_move_cursor_start_end (tree_view, count, mode);
7099       break;
7100     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
7101       pspp_sheet_view_move_cursor_line_start_end (tree_view, count, mode);
7102       break;
7103     default:
7104       g_assert_not_reached ();
7105     }
7106
7107   return TRUE;
7108 }
7109
7110 static void
7111 pspp_sheet_view_put (PsppSheetView *tree_view,
7112                    GtkWidget   *child_widget,
7113                    /* in bin_window coordinates */
7114                    gint         x,
7115                    gint         y,
7116                    gint         width,
7117                    gint         height)
7118 {
7119   PsppSheetViewChild *child;
7120   
7121   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
7122   g_return_if_fail (GTK_IS_WIDGET (child_widget));
7123
7124   child = g_slice_new (PsppSheetViewChild);
7125
7126   child->widget = child_widget;
7127   child->x = x;
7128   child->y = y;
7129   child->width = width;
7130   child->height = height;
7131
7132   tree_view->priv->children = g_list_append (tree_view->priv->children, child);
7133
7134   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7135     gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
7136   
7137   gtk_widget_set_parent (child_widget, GTK_WIDGET (tree_view));
7138 }
7139
7140 void
7141 _pspp_sheet_view_child_move_resize (PsppSheetView *tree_view,
7142                                   GtkWidget   *widget,
7143                                   /* in tree coordinates */
7144                                   gint         x,
7145                                   gint         y,
7146                                   gint         width,
7147                                   gint         height)
7148 {
7149   PsppSheetViewChild *child = NULL;
7150   GList *list;
7151   GdkRectangle allocation;
7152
7153   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
7154   g_return_if_fail (GTK_IS_WIDGET (widget));
7155
7156   for (list = tree_view->priv->children; list; list = list->next)
7157     {
7158       if (((PsppSheetViewChild *)list->data)->widget == widget)
7159         {
7160           child = list->data;
7161           break;
7162         }
7163     }
7164   if (child == NULL)
7165     return;
7166
7167   allocation.x = child->x = x;
7168   allocation.y = child->y = y;
7169   allocation.width = child->width = width;
7170   allocation.height = child->height = height;
7171
7172   if (gtk_widget_get_realized (widget))
7173     gtk_widget_size_allocate (widget, &allocation);
7174 }
7175
7176
7177 /* TreeModel Callbacks
7178  */
7179
7180 static void
7181 pspp_sheet_view_row_changed (GtkTreeModel *model,
7182                            GtkTreePath  *path,
7183                            GtkTreeIter  *iter,
7184                            gpointer      data)
7185 {
7186   PsppSheetView *tree_view = (PsppSheetView *)data;
7187   int node;
7188   gboolean free_path = FALSE;
7189   GtkTreePath *cursor_path;
7190
7191   g_return_if_fail (path != NULL || iter != NULL);
7192
7193   if (tree_view->priv->cursor != NULL)
7194     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7195   else
7196     cursor_path = NULL;
7197
7198   if (tree_view->priv->edited_column &&
7199       (cursor_path == NULL || gtk_tree_path_compare (cursor_path, path) == 0))
7200     pspp_sheet_view_stop_editing (tree_view, TRUE);
7201
7202   if (cursor_path != NULL)
7203     gtk_tree_path_free (cursor_path);
7204
7205   if (path == NULL)
7206     {
7207       path = gtk_tree_model_get_path (model, iter);
7208       free_path = TRUE;
7209     }
7210   else if (iter == NULL)
7211     gtk_tree_model_get_iter (model, iter, path);
7212
7213   _pspp_sheet_view_find_node (tree_view,
7214                               path,
7215                               &node);
7216
7217   if (node >= 0)
7218     {
7219       if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7220         pspp_sheet_view_node_queue_redraw (tree_view, node);
7221     }
7222   
7223   if (free_path)
7224     gtk_tree_path_free (path);
7225 }
7226
7227 static void
7228 pspp_sheet_view_row_inserted (GtkTreeModel *model,
7229                             GtkTreePath  *path,
7230                             GtkTreeIter  *iter,
7231                             gpointer      data)
7232 {
7233   PsppSheetView *tree_view = (PsppSheetView *) data;
7234   gint *indices;
7235   int tmpnode = -1;
7236   gint height = tree_view->priv->fixed_height;
7237   gboolean free_path = FALSE;
7238   gboolean node_visible = TRUE;
7239
7240   g_return_if_fail (path != NULL || iter != NULL);
7241
7242   if (path == NULL)
7243     {
7244       path = gtk_tree_model_get_path (model, iter);
7245       free_path = TRUE;
7246     }
7247   else if (iter == NULL)
7248     gtk_tree_model_get_iter (model, iter, path);
7249
7250   tree_view->priv->row_count = gtk_tree_model_iter_n_children (model, NULL);
7251
7252   /* Update all row-references */
7253   gtk_tree_row_reference_inserted (G_OBJECT (data), path);
7254   indices = gtk_tree_path_get_indices (path);
7255   tmpnode = indices[0];
7256
7257   range_tower_insert0 (tree_view->priv->selected, tmpnode, 1);
7258
7259   if (height > 0)
7260     {
7261       if (node_visible && node_is_visible (tree_view, tmpnode))
7262         gtk_widget_queue_resize (GTK_WIDGET (tree_view));
7263       else
7264         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (tree_view));
7265     }
7266   else
7267     install_presize_handler (tree_view);
7268   if (free_path)
7269     gtk_tree_path_free (path);
7270 }
7271
7272 static void
7273 pspp_sheet_view_row_deleted (GtkTreeModel *model,
7274                            GtkTreePath  *path,
7275                            gpointer      data)
7276 {
7277   PsppSheetView *tree_view = (PsppSheetView *)data;
7278   int node;
7279
7280   g_return_if_fail (path != NULL);
7281
7282   gtk_tree_row_reference_deleted (G_OBJECT (data), path);
7283
7284   _pspp_sheet_view_find_node (tree_view, path, &node);
7285
7286   if (node < 0)
7287     return;
7288
7289   range_tower_delete (tree_view->priv->selected, node, 1);
7290
7291   /* Ensure we don't have a dangling pointer to a dead node */
7292   ensure_unprelighted (tree_view);
7293
7294   /* Cancel editting if we've started */
7295   pspp_sheet_view_stop_editing (tree_view, TRUE);
7296
7297   if (tree_view->priv->destroy_count_func)
7298     {
7299       gint child_count = 0;
7300       tree_view->priv->destroy_count_func (tree_view, path, child_count, tree_view->priv->destroy_count_data);
7301     }
7302
7303   tree_view->priv->row_count = gtk_tree_model_iter_n_children (model, NULL);
7304
7305   if (! gtk_tree_row_reference_valid (tree_view->priv->top_row))
7306     {
7307       gtk_tree_row_reference_free (tree_view->priv->top_row);
7308       tree_view->priv->top_row = NULL;
7309     }
7310
7311   install_scroll_sync_handler (tree_view);
7312
7313   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
7314
7315 #if 0
7316   if (helper_data.changed)
7317     g_signal_emit_by_name (tree_view->priv->selection, "changed");
7318 #endif
7319 }
7320
7321 static void
7322 pspp_sheet_view_rows_reordered (GtkTreeModel *model,
7323                               GtkTreePath  *parent,
7324                               GtkTreeIter  *iter,
7325                               gint         *new_order,
7326                               gpointer      data)
7327 {
7328   PsppSheetView *tree_view = PSPP_SHEET_VIEW (data);
7329   gint len;
7330
7331   /* XXX need to adjust selection */
7332   len = gtk_tree_model_iter_n_children (model, iter);
7333
7334   if (len < 2)
7335     return;
7336
7337   gtk_tree_row_reference_reordered (G_OBJECT (data),
7338                                     parent,
7339                                     iter,
7340                                     new_order);
7341
7342   if (gtk_tree_path_get_depth (parent) != 0)
7343     return;
7344
7345   if (tree_view->priv->edited_column)
7346     pspp_sheet_view_stop_editing (tree_view, TRUE);
7347
7348   /* we need to be unprelighted */
7349   ensure_unprelighted (tree_view);
7350
7351   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
7352
7353   pspp_sheet_view_dy_to_top_row (tree_view);
7354 }
7355
7356
7357 /* Internal tree functions
7358  */
7359
7360
7361 static void
7362 pspp_sheet_view_get_background_xrange (PsppSheetView       *tree_view,
7363                                      PsppSheetViewColumn *column,
7364                                      gint              *x1,
7365                                      gint              *x2)
7366 {
7367   PsppSheetViewColumn *tmp_column = NULL;
7368   gint total_width;
7369   GList *list;
7370   gboolean rtl;
7371
7372   if (x1)
7373     *x1 = 0;
7374
7375   if (x2)
7376     *x2 = 0;
7377
7378   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
7379
7380   total_width = 0;
7381   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
7382        list;
7383        list = (rtl ? list->prev : list->next))
7384     {
7385       tmp_column = list->data;
7386
7387       if (tmp_column == column)
7388         break;
7389
7390       if (tmp_column->visible)
7391         total_width += tmp_column->width;
7392     }
7393
7394   if (tmp_column != column)
7395     {
7396       g_warning (G_STRLOC": passed-in column isn't in the tree");
7397       return;
7398     }
7399
7400   if (x1)
7401     *x1 = total_width;
7402
7403   if (x2)
7404     {
7405       if (column->visible)
7406         *x2 = total_width + column->width;
7407       else
7408         *x2 = total_width; /* width of 0 */
7409     }
7410 }
7411
7412 /* Make sure the node is visible vertically */
7413 static void
7414 pspp_sheet_view_clamp_node_visible (PsppSheetView *tree_view,
7415                                     int node)
7416 {
7417   gint node_dy, height;
7418   GtkTreePath *path = NULL;
7419
7420   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7421     return;
7422
7423   /* just return if the node is visible, avoiding a costly expose */
7424   node_dy = pspp_sheet_view_node_find_offset (tree_view, node);
7425   height = ROW_HEIGHT (tree_view);
7426   if (node_dy >= gtk_adjustment_get_value (tree_view->priv->vadjustment)
7427       && node_dy + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
7428                               + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
7429     return;
7430
7431   path = _pspp_sheet_view_find_path (tree_view, node);
7432   if (path)
7433     {
7434       /* We process updates because we want to clear old selected items when we scroll.
7435        * if this is removed, we get a "selection streak" at the bottom. */
7436       gdk_window_process_updates (tree_view->priv->bin_window, TRUE);
7437       pspp_sheet_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
7438       gtk_tree_path_free (path);
7439     }
7440 }
7441
7442 static void
7443 pspp_sheet_view_clamp_column_visible (PsppSheetView       *tree_view,
7444                                     PsppSheetViewColumn *column,
7445                                     gboolean           focus_to_cell)
7446 {
7447   gint x, width;
7448
7449   if (column == NULL)
7450     return;
7451
7452   x = column->allocation.x;
7453   width = column->allocation.width;
7454
7455   if (width > gtk_adjustment_get_page_size (tree_view->priv->hadjustment))
7456     {
7457       /* The column is larger than the horizontal page size.  If the
7458        * column has cells which can be focussed individually, then we make
7459        * sure the cell which gets focus is fully visible (if even the
7460        * focus cell is bigger than the page size, we make sure the
7461        * left-hand side of the cell is visible).
7462        *
7463        * If the column does not have those so-called special cells, we
7464        * make sure the left-hand side of the column is visible.
7465        */
7466
7467       if (focus_to_cell && pspp_sheet_view_has_special_cell (tree_view))
7468         {
7469           GtkTreePath *cursor_path;
7470           GdkRectangle background_area, cell_area, focus_area;
7471
7472           cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7473
7474           pspp_sheet_view_get_cell_area (tree_view,
7475                                        cursor_path, column, &cell_area);
7476           pspp_sheet_view_get_background_area (tree_view,
7477                                              cursor_path, column,
7478                                              &background_area);
7479
7480           gtk_tree_path_free (cursor_path);
7481
7482           _pspp_sheet_view_column_get_focus_area (column,
7483                                                 &background_area,
7484                                                 &cell_area,
7485                                                 &focus_area);
7486
7487           x = focus_area.x;
7488           width = focus_area.width;
7489
7490           if (width < gtk_adjustment_get_page_size (tree_view->priv->hadjustment))
7491             {
7492               if ((gtk_adjustment_get_value (tree_view->priv->hadjustment) + gtk_adjustment_get_page_size (tree_view->priv->hadjustment)) < (x + width))
7493                 gtk_adjustment_set_value (tree_view->priv->hadjustment,
7494                                           x + width - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
7495               else if (gtk_adjustment_get_value (tree_view->priv->hadjustment) > x)
7496                 gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
7497             }
7498         }
7499
7500       gtk_adjustment_set_value (tree_view->priv->hadjustment,
7501                                 CLAMP (x,
7502                                        gtk_adjustment_get_lower (tree_view->priv->hadjustment),
7503                                        gtk_adjustment_get_upper (tree_view->priv->hadjustment)
7504                                        - gtk_adjustment_get_page_size (tree_view->priv->hadjustment)));
7505     }
7506   else
7507     {
7508       if ((gtk_adjustment_get_value (tree_view->priv->hadjustment) + gtk_adjustment_get_page_size (tree_view->priv->hadjustment)) < (x + width))
7509           gtk_adjustment_set_value (tree_view->priv->hadjustment,
7510                                     x + width - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
7511       else if (gtk_adjustment_get_value (tree_view->priv->hadjustment) > x)
7512         gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
7513   }
7514 }
7515
7516 GtkTreePath *
7517 _pspp_sheet_view_find_path (PsppSheetView *tree_view,
7518                             int node)
7519 {
7520   GtkTreePath *path;
7521
7522   path = gtk_tree_path_new ();
7523   if (node >= 0)
7524     gtk_tree_path_append_index (path, node);
7525   return path;
7526 }
7527
7528 void
7529 _pspp_sheet_view_find_node (PsppSheetView  *tree_view,
7530                           GtkTreePath  *path,
7531                           int *node)
7532 {
7533   gint *indices = gtk_tree_path_get_indices (path);
7534   gint depth = gtk_tree_path_get_depth (path);
7535
7536   *node = -1;
7537   if (depth == 0 || indices[0] < 0 || indices[0] >= tree_view->priv->row_count)
7538     return;
7539   *node = indices[0];
7540 }
7541
7542 static void
7543 pspp_sheet_view_add_move_binding (GtkBindingSet  *binding_set,
7544                                 guint           keyval,
7545                                 guint           modmask,
7546                                 gboolean        add_shifted_binding,
7547                                 GtkMovementStep step,
7548                                 gint            count)
7549 {
7550   
7551   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
7552                                 "move-cursor", 2,
7553                                 G_TYPE_ENUM, step,
7554                                 G_TYPE_INT, count);
7555
7556   if (add_shifted_binding)
7557     gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
7558                                   "move-cursor", 2,
7559                                   G_TYPE_ENUM, step,
7560                                   G_TYPE_INT, count);
7561
7562   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
7563    return;
7564
7565   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
7566                                 "move-cursor", 2,
7567                                 G_TYPE_ENUM, step,
7568                                 G_TYPE_INT, count);
7569
7570   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
7571                                 "move-cursor", 2,
7572                                 G_TYPE_ENUM, step,
7573                                 G_TYPE_INT, count);
7574 }
7575
7576 static void
7577 pspp_sheet_view_set_column_drag_info (PsppSheetView       *tree_view,
7578                                     PsppSheetViewColumn *column)
7579 {
7580   PsppSheetViewColumn *left_column;
7581   PsppSheetViewColumn *cur_column = NULL;
7582   PsppSheetViewColumnReorder *reorder;
7583   gboolean rtl;
7584   GList *tmp_list;
7585   gint left;
7586
7587   /* We want to precalculate the motion list such that we know what column slots
7588    * are available.
7589    */
7590   left_column = NULL;
7591   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
7592
7593   /* First, identify all possible drop spots */
7594   if (rtl)
7595     tmp_list = g_list_last (tree_view->priv->columns);
7596   else
7597     tmp_list = g_list_first (tree_view->priv->columns);
7598
7599   while (tmp_list)
7600     {
7601       cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
7602       tmp_list = rtl?g_list_previous (tmp_list):g_list_next (tmp_list);
7603
7604       if (cur_column->visible == FALSE)
7605         continue;
7606
7607       /* If it's not the column moving and func tells us to skip over the column, we continue. */
7608       if (left_column != column && cur_column != column &&
7609           tree_view->priv->column_drop_func &&
7610           ! tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
7611         {
7612           left_column = cur_column;
7613           continue;
7614         }
7615       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
7616       reorder->left_column = left_column;
7617       left_column = reorder->right_column = cur_column;
7618
7619       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
7620     }
7621
7622   /* Add the last one */
7623   if (tree_view->priv->column_drop_func == NULL ||
7624       ((left_column != column) &&
7625        tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data)))
7626     {
7627       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
7628       reorder->left_column = left_column;
7629       reorder->right_column = NULL;
7630       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
7631     }
7632
7633   /* We quickly check to see if it even makes sense to reorder columns. */
7634   /* If there is nothing that can be moved, then we return */
7635
7636   if (tree_view->priv->column_drag_info == NULL)
7637     return;
7638
7639   /* We know there are always 2 slots possbile, as you can always return column. */
7640   /* If that's all there is, return */
7641   if (tree_view->priv->column_drag_info->next == NULL || 
7642       (tree_view->priv->column_drag_info->next->next == NULL &&
7643        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->data)->right_column == column &&
7644        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->next->data)->left_column == column))
7645     {
7646       for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
7647         g_slice_free (PsppSheetViewColumnReorder, tmp_list->data);
7648       g_list_free (tree_view->priv->column_drag_info);
7649       tree_view->priv->column_drag_info = NULL;
7650       return;
7651     }
7652   /* We fill in the ranges for the columns, now that we've isolated them */
7653   left = - TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
7654
7655   for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
7656     {
7657       reorder = (PsppSheetViewColumnReorder *) tmp_list->data;
7658
7659       reorder->left_align = left;
7660       if (tmp_list->next != NULL)
7661         {
7662           g_assert (tmp_list->next->data);
7663           left = reorder->right_align = (reorder->right_column->allocation.x +
7664                                          reorder->right_column->allocation.width +
7665                                          ((PsppSheetViewColumnReorder *)tmp_list->next->data)->left_column->allocation.x)/2;
7666         }
7667       else
7668         {
7669           gint width = gdk_window_get_width (tree_view->priv->header_window);
7670           reorder->right_align = width + TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
7671         }
7672     }
7673 }
7674
7675 void
7676 _pspp_sheet_view_column_start_drag (PsppSheetView       *tree_view,
7677                                   PsppSheetViewColumn *column)
7678 {
7679   GdkEvent *send_event;
7680   GtkAllocation allocation;
7681   gint x, y;
7682   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
7683   GdkDisplay *display = gdk_screen_get_display (screen);
7684
7685   g_return_if_fail (tree_view->priv->column_drag_info == NULL);
7686   g_return_if_fail (tree_view->priv->cur_reorder == NULL);
7687   g_return_if_fail (column->button);
7688
7689   pspp_sheet_view_set_column_drag_info (tree_view, column);
7690
7691   if (tree_view->priv->column_drag_info == NULL)
7692     return;
7693
7694   if (tree_view->priv->drag_window == NULL)
7695     {
7696       GdkWindowAttr attributes;
7697       guint attributes_mask;
7698
7699       attributes.window_type = GDK_WINDOW_CHILD;
7700       attributes.wclass = GDK_INPUT_OUTPUT;
7701       attributes.x = column->allocation.x;
7702       attributes.y = 0;
7703       attributes.width = column->allocation.width;
7704       attributes.height = column->allocation.height;
7705       attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
7706       attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
7707       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL ;
7708
7709       tree_view->priv->drag_window = gdk_window_new (tree_view->priv->bin_window,
7710                                                      &attributes,
7711                                                      attributes_mask);
7712       gdk_window_set_user_data (tree_view->priv->drag_window, GTK_WIDGET (tree_view));
7713     }
7714
7715   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
7716   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
7717
7718   gtk_grab_remove (column->button);
7719
7720   send_event = gdk_event_new (GDK_LEAVE_NOTIFY);
7721   send_event->crossing.send_event = TRUE;
7722   send_event->crossing.window = g_object_ref (gtk_button_get_event_window (GTK_BUTTON (column->button)));
7723   send_event->crossing.subwindow = NULL;
7724   send_event->crossing.detail = GDK_NOTIFY_ANCESTOR;
7725   send_event->crossing.time = GDK_CURRENT_TIME;
7726
7727   gtk_propagate_event (column->button, send_event);
7728   gdk_event_free (send_event);
7729
7730   send_event = gdk_event_new (GDK_BUTTON_RELEASE);
7731   send_event->button.window = g_object_ref (gdk_screen_get_root_window (screen));
7732   send_event->button.send_event = TRUE;
7733   send_event->button.time = GDK_CURRENT_TIME;
7734   send_event->button.x = -1;
7735   send_event->button.y = -1;
7736   send_event->button.axes = NULL;
7737   send_event->button.state = 0;
7738   send_event->button.button = 1;
7739   send_event->button.device = gdk_display_get_core_pointer (display);
7740   send_event->button.x_root = 0;
7741   send_event->button.y_root = 0;
7742
7743   gtk_propagate_event (column->button, send_event);
7744   gdk_event_free (send_event);
7745
7746   /* Kids, don't try this at home */
7747   g_object_ref (column->button);
7748   gtk_container_remove (GTK_CONTAINER (tree_view), column->button);
7749   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
7750   gtk_widget_set_parent (column->button, GTK_WIDGET (tree_view));
7751   g_object_unref (column->button);
7752
7753   tree_view->priv->drag_column_x = column->allocation.x;
7754   allocation = column->allocation;
7755   allocation.x = 0;
7756   gtk_widget_size_allocate (column->button, &allocation);
7757   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
7758
7759   tree_view->priv->drag_column = column;
7760   gdk_window_show (tree_view->priv->drag_window);
7761
7762   gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
7763
7764   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
7765   while (gtk_events_pending ())
7766     gtk_main_iteration ();
7767
7768   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
7769   gdk_pointer_grab (tree_view->priv->drag_window,
7770                     FALSE,
7771                     GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK,
7772                     NULL, NULL, GDK_CURRENT_TIME);
7773   gdk_keyboard_grab (tree_view->priv->drag_window,
7774                      FALSE,
7775                      GDK_CURRENT_TIME);
7776 }
7777
7778 void
7779 _pspp_sheet_view_queue_draw_node (PsppSheetView        *tree_view,
7780                                 int node,
7781                                 const GdkRectangle *clip_rect)
7782 {
7783   GdkRectangle rect;
7784   GtkAllocation allocation;
7785
7786   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7787     return;
7788
7789   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
7790   rect.x = 0;
7791   rect.width = MAX (tree_view->priv->width, allocation.width);
7792
7793   rect.y = BACKGROUND_FIRST_PIXEL (tree_view, node);
7794   rect.height = ROW_HEIGHT (tree_view);
7795
7796   if (clip_rect)
7797     {
7798       GdkRectangle new_rect;
7799
7800       gdk_rectangle_intersect (clip_rect, &rect, &new_rect);
7801
7802       gdk_window_invalidate_rect (tree_view->priv->bin_window, &new_rect, TRUE);
7803     }
7804   else
7805     {
7806       gdk_window_invalidate_rect (tree_view->priv->bin_window, &rect, TRUE);
7807     }
7808 }
7809
7810 static void
7811 pspp_sheet_view_queue_draw_path (PsppSheetView        *tree_view,
7812                                GtkTreePath        *path,
7813                                const GdkRectangle *clip_rect)
7814 {
7815   int node = -1;
7816
7817   _pspp_sheet_view_find_node (tree_view, path, &node);
7818
7819   if (node)
7820     _pspp_sheet_view_queue_draw_node (tree_view, node, clip_rect);
7821 }
7822
7823 static void
7824 pspp_sheet_view_focus_to_cursor (PsppSheetView *tree_view)
7825
7826 {
7827   GtkTreePath *cursor_path;
7828
7829   if ((tree_view->priv->row_count == 0) ||
7830       (! gtk_widget_get_realized (GTK_WIDGET (tree_view))))
7831     return;
7832
7833   cursor_path = NULL;
7834   if (tree_view->priv->cursor)
7835     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7836
7837   if (cursor_path == NULL)
7838     {
7839       /* There's no cursor.  Move the cursor to the first selected row, if any
7840        * are selected, otherwise to the first row in the sheetview.
7841        */
7842       GList *selected_rows;
7843       GtkTreeModel *model;
7844       PsppSheetSelection *selection;
7845
7846       selection = pspp_sheet_view_get_selection (tree_view);
7847       selected_rows = pspp_sheet_selection_get_selected_rows (selection, &model);
7848
7849       if (selected_rows)
7850         {
7851           /* XXX we could avoid doing O(n) work to get this result */
7852           cursor_path = gtk_tree_path_copy((const GtkTreePath *)(selected_rows->data));
7853           g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
7854           g_list_free (selected_rows);
7855         }
7856       else
7857         {
7858           cursor_path = gtk_tree_path_new_first ();
7859           search_first_focusable_path (tree_view, &cursor_path,
7860                                        TRUE, NULL);
7861         }
7862
7863       gtk_tree_row_reference_free (tree_view->priv->cursor);
7864       tree_view->priv->cursor = NULL;
7865
7866       if (cursor_path)
7867         {
7868           if (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
7869               tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE)
7870             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, FALSE, FALSE, 0);
7871           else
7872             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE, 0);
7873         }
7874     }
7875
7876   if (cursor_path)
7877     {
7878       /* Now find a column for the cursor. */
7879       PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
7880
7881       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
7882       gtk_tree_path_free (cursor_path);
7883
7884       if (tree_view->priv->focus_column == NULL)
7885         {
7886           GList *list;
7887           for (list = tree_view->priv->columns; list; list = list->next)
7888             {
7889               if (PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
7890                 {
7891                   tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
7892                   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
7893                   pspp_sheet_selection_select_column (tree_view->priv->selection, tree_view->priv->focus_column);
7894                   break;
7895                 }
7896             }
7897
7898         }
7899     }
7900 }
7901
7902 static gboolean
7903 pspp_sheet_view_move_cursor_up_down (PsppSheetView *tree_view,
7904                                    gint         count,
7905                                    PsppSheetSelectMode mode)
7906 {
7907   gint selection_count;
7908   int cursor_node = -1;
7909   int new_cursor_node = -1;
7910   GtkTreePath *cursor_path = NULL;
7911   gboolean grab_focus = TRUE;
7912
7913   if (! gtk_widget_has_focus (GTK_WIDGET (tree_view)))
7914     return FALSE;
7915
7916   cursor_path = NULL;
7917   if (!gtk_tree_row_reference_valid (tree_view->priv->cursor))
7918     /* FIXME: we lost the cursor; should we get the first? */
7919     return FALSE;
7920
7921   cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7922   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
7923
7924   if (cursor_node < 0)
7925     /* FIXME: we lost the cursor; should we get the first? */
7926     return FALSE;
7927
7928   selection_count = pspp_sheet_selection_count_selected_rows (tree_view->priv->selection);
7929
7930   if (selection_count == 0
7931       && tree_view->priv->selection->type != PSPP_SHEET_SELECTION_NONE
7932       && !(mode & PSPP_SHEET_SELECT_MODE_TOGGLE))
7933     {
7934       /* Don't move the cursor, but just select the current node */
7935       new_cursor_node = cursor_node;
7936     }
7937   else
7938     {
7939       if (count == -1)
7940         new_cursor_node = pspp_sheet_view_node_prev (tree_view, cursor_node);
7941       else
7942         new_cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
7943     }
7944
7945   gtk_tree_path_free (cursor_path);
7946
7947   if (new_cursor_node)
7948     {
7949       cursor_path = _pspp_sheet_view_find_path (tree_view, new_cursor_node);
7950
7951       search_first_focusable_path (tree_view, &cursor_path,
7952                                    (count != -1),
7953                                    &new_cursor_node);
7954
7955       if (cursor_path)
7956         gtk_tree_path_free (cursor_path);
7957     }
7958
7959   /*
7960    * If the list has only one item and multi-selection is set then select
7961    * the row (if not yet selected).
7962    */
7963   if ((tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
7964        tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE) &&
7965       new_cursor_node < 0)
7966     {
7967       if (count == -1)
7968         new_cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
7969       else
7970         new_cursor_node = pspp_sheet_view_node_prev (tree_view, cursor_node);
7971
7972       if (new_cursor_node < 0
7973           && !pspp_sheet_view_node_is_selected (tree_view, cursor_node))
7974         {
7975           new_cursor_node = cursor_node;
7976         }
7977       else
7978         {
7979           new_cursor_node = -1;
7980         }
7981     }
7982
7983   if (new_cursor_node >= 0)
7984     {
7985       cursor_path = _pspp_sheet_view_find_path (tree_view, new_cursor_node);
7986       pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, TRUE, mode);
7987       gtk_tree_path_free (cursor_path);
7988     }
7989   else
7990     {
7991       pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
7992
7993       if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND))
7994         {
7995           if (! gtk_widget_keynav_failed (GTK_WIDGET (tree_view),
7996                                           count < 0 ?
7997                                           GTK_DIR_UP : GTK_DIR_DOWN))
7998             {
7999               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
8000
8001               if (toplevel)
8002                 gtk_widget_child_focus (toplevel,
8003                                         count < 0 ?
8004                                         GTK_DIR_TAB_BACKWARD :
8005                                         GTK_DIR_TAB_FORWARD);
8006
8007               grab_focus = FALSE;
8008             }
8009         }
8010       else
8011         {
8012           gtk_widget_error_bell (GTK_WIDGET (tree_view));
8013         }
8014     }
8015
8016   if (grab_focus)
8017     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8018
8019   return new_cursor_node >= 0;
8020 }
8021
8022 static void
8023 pspp_sheet_view_move_cursor_page_up_down (PsppSheetView *tree_view,
8024                                           gint         count,
8025                                           PsppSheetSelectMode mode)
8026 {
8027   int cursor_node = -1;
8028   GtkTreePath *old_cursor_path = NULL;
8029   GtkTreePath *cursor_path = NULL;
8030   int start_cursor_node = -1;
8031   gint y;
8032   gint window_y;
8033   gint vertical_separator;
8034
8035   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8036     return;
8037
8038   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8039     old_cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8040   else
8041     /* This is sorta weird.  Focus in should give us a cursor */
8042     return;
8043
8044   gtk_widget_style_get (GTK_WIDGET (tree_view), "vertical-separator", &vertical_separator, NULL);
8045   _pspp_sheet_view_find_node (tree_view, old_cursor_path, &cursor_node);
8046
8047   if (cursor_node < 0)
8048     {
8049       /* FIXME: we lost the cursor.  Should we try to get one? */
8050       gtk_tree_path_free (old_cursor_path);
8051       return;
8052     }
8053
8054   y = pspp_sheet_view_node_find_offset (tree_view, cursor_node);
8055   window_y = RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, y);
8056   y += tree_view->priv->cursor_offset;
8057   y += count * (int)gtk_adjustment_get_page_increment (tree_view->priv->vadjustment);
8058   y = CLAMP (y, (gint)gtk_adjustment_get_lower (tree_view->priv->vadjustment),  (gint)gtk_adjustment_get_upper (tree_view->priv->vadjustment) - vertical_separator);
8059
8060   if (y >= tree_view->priv->height)
8061     y = tree_view->priv->height - 1;
8062
8063   tree_view->priv->cursor_offset =
8064     pspp_sheet_view_find_offset (tree_view, y, &cursor_node);
8065
8066   if (tree_view->priv->cursor_offset > BACKGROUND_HEIGHT (tree_view))
8067     {
8068       cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
8069       tree_view->priv->cursor_offset -= BACKGROUND_HEIGHT (tree_view);
8070     }
8071
8072   y -= tree_view->priv->cursor_offset;
8073   cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_node);
8074
8075   start_cursor_node = cursor_node;
8076
8077   if (! search_first_focusable_path (tree_view, &cursor_path,
8078                                      (count != -1),
8079                                      &cursor_node))
8080     {
8081       /* It looks like we reached the end of the view without finding
8082        * a focusable row.  We will step backwards to find the last
8083        * focusable row.
8084        */
8085       cursor_node = start_cursor_node;
8086       cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_node);
8087
8088       search_first_focusable_path (tree_view, &cursor_path,
8089                                    (count == -1),
8090                                    &cursor_node);
8091     }
8092
8093   if (!cursor_path)
8094     goto cleanup;
8095
8096   /* update y */
8097   y = pspp_sheet_view_node_find_offset (tree_view, cursor_node);
8098
8099   pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE, mode);
8100
8101   y -= window_y;
8102   pspp_sheet_view_scroll_to_point (tree_view, -1, y);
8103   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8104   _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8105
8106   if (!gtk_tree_path_compare (old_cursor_path, cursor_path))
8107     gtk_widget_error_bell (GTK_WIDGET (tree_view));
8108
8109   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8110
8111 cleanup:
8112   gtk_tree_path_free (old_cursor_path);
8113   gtk_tree_path_free (cursor_path);
8114 }
8115
8116 static void
8117 pspp_sheet_view_move_cursor_left_right (PsppSheetView *tree_view,
8118                                         gint         count,
8119                                         PsppSheetSelectMode mode)
8120 {
8121   int cursor_node = -1;
8122   GtkTreePath *cursor_path = NULL;
8123   PsppSheetViewColumn *column;
8124   GtkTreeIter iter;
8125   GList *list;
8126   gboolean found_column = FALSE;
8127   gboolean rtl;
8128
8129   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8130
8131   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8132     return;
8133
8134   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8135     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8136   else
8137     return;
8138
8139   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8140   if (cursor_node < 0)
8141     return;
8142   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8143     {
8144       gtk_tree_path_free (cursor_path);
8145       return;
8146     }
8147   gtk_tree_path_free (cursor_path);
8148
8149   list = rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns);
8150   if (tree_view->priv->focus_column)
8151     {
8152       for (; list; list = (rtl ? list->prev : list->next))
8153         {
8154           if (list->data == tree_view->priv->focus_column)
8155             break;
8156         }
8157     }
8158
8159   while (list)
8160     {
8161       gboolean left, right;
8162
8163       column = list->data;
8164       if (column->visible == FALSE || column->row_head)
8165         goto loop_end;
8166
8167       pspp_sheet_view_column_cell_set_cell_data (column,
8168                                                tree_view->priv->model,
8169                                                &iter);
8170
8171       if (rtl)
8172         {
8173           right = list->prev ? TRUE : FALSE;
8174           left = list->next ? TRUE : FALSE;
8175         }
8176       else
8177         {
8178           left = list->prev ? TRUE : FALSE;
8179           right = list->next ? TRUE : FALSE;
8180         }
8181
8182       if (_pspp_sheet_view_column_cell_focus (column, count, left, right))
8183         {
8184           tree_view->priv->focus_column = column;
8185           found_column = TRUE;
8186           break;
8187         }
8188     loop_end:
8189       if (count == 1)
8190         list = rtl ? list->prev : list->next;
8191       else
8192         list = rtl ? list->next : list->prev;
8193     }
8194
8195   if (found_column)
8196     {
8197       _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8198       g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8199       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8200     }
8201   else
8202     {
8203       gtk_widget_error_bell (GTK_WIDGET (tree_view));
8204     }
8205
8206   pspp_sheet_view_clamp_column_visible (tree_view,
8207                                       tree_view->priv->focus_column, TRUE);
8208 }
8209
8210 static void
8211 pspp_sheet_view_move_cursor_line_start_end (PsppSheetView *tree_view,
8212                                             gint         count,
8213                                             PsppSheetSelectMode mode)
8214 {
8215   int cursor_node = -1;
8216   GtkTreePath *cursor_path = NULL;
8217   PsppSheetViewColumn *column;
8218   PsppSheetViewColumn *found_column;
8219   GtkTreeIter iter;
8220   GList *list;
8221   gboolean rtl;
8222
8223   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8224
8225   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8226     return;
8227
8228   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8229     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8230   else
8231     return;
8232
8233   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8234   if (cursor_node < 0)
8235     return;
8236   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8237     {
8238       gtk_tree_path_free (cursor_path);
8239       return;
8240     }
8241   gtk_tree_path_free (cursor_path);
8242
8243   list = rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns);
8244   if (tree_view->priv->focus_column)
8245     {
8246       for (; list; list = (rtl ? list->prev : list->next))
8247         {
8248           if (list->data == tree_view->priv->focus_column)
8249             break;
8250         }
8251     }
8252
8253   found_column = NULL;
8254   while (list)
8255     {
8256       gboolean left, right;
8257
8258       column = list->data;
8259       if (column->visible == FALSE || column->row_head)
8260         goto loop_end;
8261
8262       pspp_sheet_view_column_cell_set_cell_data (column,
8263                                                tree_view->priv->model,
8264                                                &iter);
8265
8266       if (rtl)
8267         {
8268           right = list->prev ? TRUE : FALSE;
8269           left = list->next ? TRUE : FALSE;
8270         }
8271       else
8272         {
8273           left = list->prev ? TRUE : FALSE;
8274           right = list->next ? TRUE : FALSE;
8275         }
8276
8277       if (column->tabbable
8278           && _pspp_sheet_view_column_cell_focus (column, count, left, right))
8279         found_column = column;
8280
8281     loop_end:
8282       if (count == 1)
8283         list = rtl ? list->prev : list->next;
8284       else
8285         list = rtl ? list->next : list->prev;
8286     }
8287
8288   if (found_column)
8289     {
8290       tree_view->priv->focus_column = found_column;
8291       _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8292       g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8293       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8294     }
8295
8296   pspp_sheet_view_clamp_column_visible (tree_view,
8297                                       tree_view->priv->focus_column, TRUE);
8298 }
8299
8300 static gboolean
8301 try_move_cursor_tab (PsppSheetView *tree_view,
8302                      gboolean start_at_focus_column,
8303                      gint count)
8304 {
8305   PsppSheetViewColumn *column;
8306   GtkTreeIter iter;
8307   int cursor_node = -1;
8308   GtkTreePath *cursor_path = NULL;
8309   gboolean rtl;
8310   GList *list;
8311
8312   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8313     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8314   else
8315     return TRUE;
8316
8317   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8318   if (cursor_node < 0)
8319     return TRUE;
8320   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8321     {
8322       gtk_tree_path_free (cursor_path);
8323       return TRUE;
8324     }
8325   gtk_tree_path_free (cursor_path);
8326
8327   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
8328   if (start_at_focus_column)
8329     {
8330       list = (rtl
8331               ? g_list_last (tree_view->priv->columns)
8332               : g_list_first (tree_view->priv->columns));
8333       if (tree_view->priv->focus_column)
8334         {
8335           for (; list; list = (rtl ? list->prev : list->next))
8336             {
8337               if (list->data == tree_view->priv->focus_column)
8338                 break;
8339             }
8340         }
8341     }
8342   else
8343     {
8344       list = (rtl ^ (count == 1)
8345               ? g_list_first (tree_view->priv->columns)
8346               : g_list_last (tree_view->priv->columns));
8347     }
8348
8349   while (list)
8350     {
8351       gboolean left, right;
8352
8353       column = list->data;
8354       if (column->visible == FALSE || !column->tabbable)
8355         goto loop_end;
8356
8357       pspp_sheet_view_column_cell_set_cell_data (column,
8358                                                  tree_view->priv->model,
8359                                                  &iter);
8360
8361       if (rtl)
8362         {
8363           right = list->prev ? TRUE : FALSE;
8364           left = list->next ? TRUE : FALSE;
8365         }
8366       else
8367         {
8368           left = list->prev ? TRUE : FALSE;
8369           right = list->next ? TRUE : FALSE;
8370         }
8371
8372       if (column->tabbable
8373           && _pspp_sheet_view_column_cell_focus (column, count, left, right))
8374         {
8375           tree_view->priv->focus_column = column;
8376           _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8377           g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8378           gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8379           return TRUE;
8380         }
8381     loop_end:
8382       if (count == 1)
8383         list = rtl ? list->prev : list->next;
8384       else
8385         list = rtl ? list->next : list->prev;
8386     }
8387
8388   return FALSE;
8389 }
8390
8391 static void
8392 pspp_sheet_view_move_cursor_tab (PsppSheetView *tree_view,
8393                                  gint         count)
8394 {
8395   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8396     return;
8397
8398   if (!try_move_cursor_tab (tree_view, TRUE, count))
8399     {
8400       if (pspp_sheet_view_move_cursor_up_down (tree_view, count, 0)
8401           && !try_move_cursor_tab (tree_view, FALSE, count))
8402         gtk_widget_error_bell (GTK_WIDGET (tree_view));
8403     }
8404
8405   pspp_sheet_view_clamp_column_visible (tree_view,
8406                                         tree_view->priv->focus_column, TRUE);
8407 }
8408
8409 static void
8410 pspp_sheet_view_move_cursor_start_end (PsppSheetView *tree_view,
8411                                        gint         count,
8412                                        PsppSheetSelectMode mode)
8413 {
8414   int cursor_node;
8415   GtkTreePath *path;
8416   GtkTreePath *old_path;
8417
8418   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8419     return;
8420
8421   g_return_if_fail (tree_view->priv->row_count > 0);
8422
8423   pspp_sheet_view_get_cursor (tree_view, &old_path, NULL);
8424
8425   if (count == -1)
8426     {
8427       /* Now go forward to find the first focusable row. */
8428       path = _pspp_sheet_view_find_path (tree_view, 0);
8429       search_first_focusable_path (tree_view, &path,
8430                                    TRUE, &cursor_node);
8431     }
8432   else
8433     {
8434       /* Now go backwards to find last focusable row. */
8435       path = _pspp_sheet_view_find_path (tree_view, tree_view->priv->row_count - 1);
8436       search_first_focusable_path (tree_view, &path,
8437                                    FALSE, &cursor_node);
8438     }
8439
8440   if (!path)
8441     goto cleanup;
8442
8443   if (gtk_tree_path_compare (old_path, path))
8444     {
8445       pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, mode);
8446       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8447     }
8448   else
8449     {
8450       gtk_widget_error_bell (GTK_WIDGET (tree_view));
8451     }
8452
8453 cleanup:
8454   gtk_tree_path_free (old_path);
8455   gtk_tree_path_free (path);
8456 }
8457
8458 static gboolean
8459 pspp_sheet_view_real_select_all (PsppSheetView *tree_view)
8460 {
8461   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8462     return FALSE;
8463
8464   if (tree_view->priv->selection->type != PSPP_SHEET_SELECTION_MULTIPLE &&
8465       tree_view->priv->selection->type != PSPP_SHEET_SELECTION_RECTANGLE)
8466     return FALSE;
8467
8468   pspp_sheet_selection_select_all (tree_view->priv->selection);
8469
8470   return TRUE;
8471 }
8472
8473 static gboolean
8474 pspp_sheet_view_real_unselect_all (PsppSheetView *tree_view)
8475 {
8476   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8477     return FALSE;
8478
8479   if (tree_view->priv->selection->type != PSPP_SHEET_SELECTION_MULTIPLE &&
8480       tree_view->priv->selection->type != PSPP_SHEET_SELECTION_RECTANGLE)
8481     return FALSE;
8482
8483   pspp_sheet_selection_unselect_all (tree_view->priv->selection);
8484
8485   return TRUE;
8486 }
8487
8488 static gboolean
8489 pspp_sheet_view_real_select_cursor_row (PsppSheetView *tree_view,
8490                                         gboolean     start_editing,
8491                                         PsppSheetSelectMode mode)
8492 {
8493   int new_node = -1;
8494   int cursor_node = -1;
8495   GtkTreePath *cursor_path = NULL;
8496
8497   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8498     return FALSE;
8499
8500   if (tree_view->priv->cursor)
8501     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8502
8503   if (cursor_path == NULL)
8504     return FALSE;
8505
8506   _pspp_sheet_view_find_node (tree_view, cursor_path,
8507                               &cursor_node);
8508
8509   if (cursor_node < 0)
8510     {
8511       gtk_tree_path_free (cursor_path);
8512       return FALSE;
8513     }
8514
8515   if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND) && start_editing &&
8516       tree_view->priv->focus_column)
8517     {
8518       if (pspp_sheet_view_start_editing (tree_view, cursor_path))
8519         {
8520           gtk_tree_path_free (cursor_path);
8521           return TRUE;
8522         }
8523     }
8524
8525   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
8526                                             cursor_node,
8527                                             cursor_path,
8528                                             mode,
8529                                             FALSE);
8530
8531   /* We bail out if the original (tree, node) don't exist anymore after
8532    * handling the selection-changed callback.  We do return TRUE because
8533    * the key press has been handled at this point.
8534    */
8535   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_node);
8536
8537   if (cursor_node != new_node)
8538     return FALSE;
8539
8540   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8541
8542   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8543   _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8544
8545   if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND))
8546     pspp_sheet_view_row_activated (tree_view, cursor_path,
8547                                  tree_view->priv->focus_column);
8548     
8549   gtk_tree_path_free (cursor_path);
8550
8551   return TRUE;
8552 }
8553
8554 static gboolean
8555 pspp_sheet_view_real_toggle_cursor_row (PsppSheetView *tree_view)
8556 {
8557   int new_node = -1;
8558   int cursor_node = -1;
8559   GtkTreePath *cursor_path = NULL;
8560
8561   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8562     return FALSE;
8563
8564   cursor_path = NULL;
8565   if (tree_view->priv->cursor)
8566     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8567
8568   if (cursor_path == NULL)
8569     return FALSE;
8570
8571   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8572   if (cursor_node < 0)
8573     {
8574       gtk_tree_path_free (cursor_path);
8575       return FALSE;
8576     }
8577
8578   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
8579                                             cursor_node,
8580                                             cursor_path,
8581                                             PSPP_SHEET_SELECT_MODE_TOGGLE,
8582                                             FALSE);
8583
8584   /* We bail out if the original (tree, node) don't exist anymore after
8585    * handling the selection-changed callback.  We do return TRUE because
8586    * the key press has been handled at this point.
8587    */
8588   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_node);
8589
8590   if (cursor_node != new_node)
8591     return FALSE;
8592
8593   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8594
8595   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8596   pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
8597   gtk_tree_path_free (cursor_path);
8598
8599   return TRUE;
8600 }
8601
8602 static gboolean
8603 pspp_sheet_view_search_entry_flush_timeout (PsppSheetView *tree_view)
8604 {
8605   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
8606   tree_view->priv->typeselect_flush_timeout = 0;
8607
8608   return FALSE;
8609 }
8610
8611 /* Cut and paste from gtkwindow.c */
8612 static void
8613 send_focus_change (GtkWidget *widget,
8614                    gboolean   in)
8615 {
8616   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
8617
8618   fevent->focus_change.type = GDK_FOCUS_CHANGE;
8619   fevent->focus_change.window = g_object_ref (gtk_widget_get_window (widget));
8620   fevent->focus_change.in = in;
8621   
8622   gtk_widget_send_focus_change (widget, fevent);
8623   gdk_event_free (fevent);
8624 }
8625
8626 static void
8627 pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view)
8628 {
8629   GtkWidget *frame, *vbox, *toplevel;
8630   GdkScreen *screen;
8631
8632   if (tree_view->priv->search_custom_entry_set)
8633     return;
8634
8635   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
8636   screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
8637
8638    if (tree_view->priv->search_window != NULL)
8639      {
8640        if (gtk_window_get_group (GTK_WINDOW (toplevel)))
8641          gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
8642                                       GTK_WINDOW (tree_view->priv->search_window));
8643        else if (gtk_window_get_group (GTK_WINDOW (tree_view->priv->search_window)))
8644          gtk_window_group_remove_window (gtk_window_get_group (GTK_WINDOW (tree_view->priv->search_window)),
8645                                          GTK_WINDOW (tree_view->priv->search_window));
8646        gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
8647        return;
8648      }
8649    
8650   tree_view->priv->search_window = gtk_window_new (GTK_WINDOW_POPUP);
8651   gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
8652
8653   if (gtk_window_get_group (GTK_WINDOW (toplevel)))
8654     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
8655                                  GTK_WINDOW (tree_view->priv->search_window));
8656
8657   gtk_window_set_type_hint (GTK_WINDOW (tree_view->priv->search_window),
8658                             GDK_WINDOW_TYPE_HINT_UTILITY);
8659   gtk_window_set_modal (GTK_WINDOW (tree_view->priv->search_window), TRUE);
8660   g_signal_connect (tree_view->priv->search_window, "delete-event",
8661                     G_CALLBACK (pspp_sheet_view_search_delete_event),
8662                     tree_view);
8663   g_signal_connect (tree_view->priv->search_window, "key-press-event",
8664                     G_CALLBACK (pspp_sheet_view_search_key_press_event),
8665                     tree_view);
8666   g_signal_connect (tree_view->priv->search_window, "button-press-event",
8667                     G_CALLBACK (pspp_sheet_view_search_button_press_event),
8668                     tree_view);
8669   g_signal_connect (tree_view->priv->search_window, "scroll-event",
8670                     G_CALLBACK (pspp_sheet_view_search_scroll_event),
8671                     tree_view);
8672
8673   frame = gtk_frame_new (NULL);
8674   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
8675   gtk_widget_show (frame);
8676   gtk_container_add (GTK_CONTAINER (tree_view->priv->search_window), frame);
8677
8678   vbox = gtk_vbox_new (FALSE, 0);
8679   gtk_widget_show (vbox);
8680   gtk_container_add (GTK_CONTAINER (frame), vbox);
8681   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
8682
8683   /* add entry */
8684   tree_view->priv->search_entry = gtk_entry_new ();
8685   gtk_widget_show (tree_view->priv->search_entry);
8686   g_signal_connect (tree_view->priv->search_entry, "populate-popup",
8687                     G_CALLBACK (pspp_sheet_view_search_disable_popdown),
8688                     tree_view);
8689   g_signal_connect (tree_view->priv->search_entry,
8690                     "activate", G_CALLBACK (pspp_sheet_view_search_activate),
8691                     tree_view);
8692
8693 #if GTK3_TRANSITION
8694   g_signal_connect (GTK_ENTRY (tree_view->priv->search_entry)->im_context,
8695                     "preedit-changed",
8696                     G_CALLBACK (pspp_sheet_view_search_preedit_changed),
8697                     tree_view);
8698 #endif
8699
8700   gtk_container_add (GTK_CONTAINER (vbox),
8701                      tree_view->priv->search_entry);
8702
8703   gtk_widget_realize (tree_view->priv->search_entry);
8704 }
8705
8706 /* Pops up the interactive search entry.  If keybinding is TRUE then the user
8707  * started this by typing the start_interactive_search keybinding.  Otherwise, it came from 
8708  */
8709 static gboolean
8710 pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
8711                                              gboolean     keybinding)
8712 {
8713   /* We only start interactive search if we have focus or the columns
8714    * have focus.  If one of our children have focus, we don't want to
8715    * start the search.
8716    */
8717   GList *list;
8718   gboolean found_focus = FALSE;
8719   GtkWidgetClass *entry_parent_class;
8720   
8721   if (!tree_view->priv->enable_search && !keybinding)
8722     return FALSE;
8723
8724   if (tree_view->priv->search_custom_entry_set)
8725     return FALSE;
8726
8727   if (tree_view->priv->search_window != NULL &&
8728       gtk_widget_get_visible (tree_view->priv->search_window))
8729     return TRUE;
8730
8731   for (list = tree_view->priv->columns; list; list = list->next)
8732     {
8733       PsppSheetViewColumn *column;
8734
8735       column = list->data;
8736       if (! column->visible)
8737         continue;
8738
8739       if (column->button && gtk_widget_has_focus (column->button))
8740         {
8741           found_focus = TRUE;
8742           break;
8743         }
8744     }
8745   
8746   if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8747     found_focus = TRUE;
8748
8749   if (!found_focus)
8750     return FALSE;
8751
8752   if (tree_view->priv->search_column < 0)
8753     return FALSE;
8754
8755   pspp_sheet_view_ensure_interactive_directory (tree_view);
8756
8757   if (keybinding)
8758     gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
8759
8760   /* done, show it */
8761   tree_view->priv->search_position_func (tree_view, tree_view->priv->search_window, tree_view->priv->search_position_user_data);
8762   gtk_widget_show (tree_view->priv->search_window);
8763   if (tree_view->priv->search_entry_changed_id == 0)
8764     {
8765       tree_view->priv->search_entry_changed_id =
8766         g_signal_connect (tree_view->priv->search_entry, "changed",
8767                           G_CALLBACK (pspp_sheet_view_search_init),
8768                           tree_view);
8769     }
8770
8771   tree_view->priv->typeselect_flush_timeout =
8772     gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
8773                    (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
8774                    tree_view);
8775
8776   /* Grab focus will select all the text.  We don't want that to happen, so we
8777    * call the parent instance and bypass the selection change.  This is probably
8778    * really non-kosher. */
8779   entry_parent_class = g_type_class_peek_parent (GTK_ENTRY_GET_CLASS (tree_view->priv->search_entry));
8780   (entry_parent_class->grab_focus) (tree_view->priv->search_entry);
8781
8782   /* send focus-in event */
8783   send_focus_change (tree_view->priv->search_entry, TRUE);
8784
8785   /* search first matching iter */
8786   pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
8787
8788   return TRUE;
8789 }
8790
8791 static gboolean
8792 pspp_sheet_view_start_interactive_search (PsppSheetView *tree_view)
8793 {
8794   return pspp_sheet_view_real_start_interactive_search (tree_view, TRUE);
8795 }
8796
8797 /* this function returns the new width of the column being resized given
8798  * the column and x position of the cursor; the x cursor position is passed
8799  * in as a pointer and automagicly corrected if it's beyond min/max limits
8800  */
8801 static gint
8802 pspp_sheet_view_new_column_width (PsppSheetView *tree_view,
8803                                 gint       i,
8804                                 gint      *x)
8805 {
8806   PsppSheetViewColumn *column;
8807   gint width;
8808   gboolean rtl;
8809
8810   /* first translate the x position from gtk_widget_get_window (widget)
8811    * to clist->clist_window
8812    */
8813   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8814   column = g_list_nth (tree_view->priv->columns, i)->data;
8815   width = rtl ? (column->allocation.x + column->allocation.width - *x) : (*x - column->allocation.x);
8816  
8817   /* Clamp down the value */
8818   if (column->min_width == -1)
8819     width = MAX (column->button_request, width);
8820   else
8821     width = MAX (column->min_width, width);
8822   if (column->max_width != -1)
8823     width = MIN (width, column->max_width);
8824
8825   *x = rtl ? (column->allocation.x + column->allocation.width - width) : (column->allocation.x + width);
8826  
8827   return width;
8828 }
8829
8830
8831 /* FIXME this adjust_allocation is a big cut-and-paste from
8832  * GtkCList, needs to be some "official" way to do this
8833  * factored out.
8834  */
8835 typedef struct
8836 {
8837   GdkWindow *window;
8838   int dx;
8839   int dy;
8840 } ScrollData;
8841
8842 /* The window to which gtk_widget_get_window (widget) is relative */
8843 #define ALLOCATION_WINDOW(widget)               \
8844    (!gtk_widget_get_has_window (widget) ?               \
8845     gtk_widget_get_window (widget) :                          \
8846     gdk_window_get_parent (gtk_widget_get_window (widget)))
8847
8848 static void
8849 adjust_allocation_recurse (GtkWidget *widget,
8850                            gpointer   data)
8851 {
8852   ScrollData *scroll_data = data;
8853   GtkAllocation allocation;
8854   gtk_widget_get_allocation (widget, &allocation);
8855   /* Need to really size allocate instead of just poking
8856    * into widget->allocation if the widget is not realized.
8857    * FIXME someone figure out why this was.
8858    */
8859   if (!gtk_widget_get_realized (widget))
8860     {
8861       if (gtk_widget_get_visible (widget))
8862         {
8863           GdkRectangle tmp_rectangle = allocation;
8864           tmp_rectangle.x += scroll_data->dx;
8865           tmp_rectangle.y += scroll_data->dy;
8866           
8867           gtk_widget_size_allocate (widget, &tmp_rectangle);
8868         }
8869     }
8870   else
8871     {
8872       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
8873         {
8874           allocation.x += scroll_data->dx;
8875           allocation.y += scroll_data->dy;
8876           
8877           if (GTK_IS_CONTAINER (widget))
8878             gtk_container_forall (GTK_CONTAINER (widget),
8879                                   adjust_allocation_recurse,
8880                                   data);
8881         }
8882     }
8883 }
8884
8885 static void
8886 adjust_allocation (GtkWidget *widget,
8887                    int        dx,
8888                    int        dy)
8889 {
8890   ScrollData scroll_data;
8891
8892   if (gtk_widget_get_realized (widget))
8893     scroll_data.window = ALLOCATION_WINDOW (widget);
8894   else
8895     scroll_data.window = NULL;
8896     
8897   scroll_data.dx = dx;
8898   scroll_data.dy = dy;
8899   
8900   adjust_allocation_recurse (widget, &scroll_data);
8901 }
8902
8903 void 
8904 pspp_sheet_view_column_update_button (PsppSheetViewColumn *tree_column);
8905
8906 /* Callbacks */
8907 static void
8908 pspp_sheet_view_adjustment_changed (GtkAdjustment *adjustment,
8909                                   PsppSheetView   *tree_view)
8910 {
8911   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8912     {
8913       GList *list;
8914       gint dy;
8915         
8916       gdk_window_move (tree_view->priv->bin_window,
8917                        - gtk_adjustment_get_value (tree_view->priv->hadjustment),
8918                        TREE_VIEW_HEADER_HEIGHT (tree_view));
8919       gdk_window_move (tree_view->priv->header_window,
8920                        - gtk_adjustment_get_value (tree_view->priv->hadjustment),
8921                        0);
8922       dy = tree_view->priv->dy - (int) gtk_adjustment_get_value (tree_view->priv->vadjustment);
8923       if (dy)
8924         {
8925           update_prelight (tree_view,
8926                            tree_view->priv->event_last_x,
8927                            tree_view->priv->event_last_y - dy);
8928
8929           if (tree_view->priv->edited_column &&
8930               GTK_IS_WIDGET (tree_view->priv->edited_column->editable_widget))
8931             {
8932               GList *list;
8933               GtkWidget *widget;
8934               PsppSheetViewChild *child = NULL;
8935
8936               widget = GTK_WIDGET (tree_view->priv->edited_column->editable_widget);
8937               adjust_allocation (widget, 0, dy); 
8938               
8939               for (list = tree_view->priv->children; list; list = list->next)
8940                 {
8941                   child = (PsppSheetViewChild *)list->data;
8942                   if (child->widget == widget)
8943                     {
8944                       child->y += dy;
8945                       break;
8946                     }
8947                 }
8948             }
8949         }
8950       gdk_window_scroll (tree_view->priv->bin_window, 0, dy);
8951
8952       if (tree_view->priv->dy != (int) gtk_adjustment_get_value (tree_view->priv->vadjustment))
8953         {
8954           /* update our dy and top_row */
8955           tree_view->priv->dy = (int) gtk_adjustment_get_value (tree_view->priv->vadjustment);
8956
8957           if (!tree_view->priv->in_top_row_to_dy)
8958             pspp_sheet_view_dy_to_top_row (tree_view);
8959         }
8960
8961       for (list = tree_view->priv->columns; list; list = list->next)
8962         {
8963           PsppSheetViewColumn *column = list->data;
8964           GtkAllocation *col_allocation = &column->allocation;
8965           GtkAllocation widget_allocation;
8966           gtk_widget_get_allocation (GTK_WIDGET (tree_view), &widget_allocation);
8967
8968           if (span_intersects (col_allocation->x, col_allocation->width,
8969                                gtk_adjustment_get_value (tree_view->priv->hadjustment),
8970                                widget_allocation.width))
8971             {
8972               pspp_sheet_view_column_set_need_button (column, TRUE);
8973               if (!column->button)
8974                 pspp_sheet_view_column_update_button (column);
8975             }
8976         }
8977     }
8978 }
8979
8980 \f
8981
8982 /* Public methods
8983  */
8984
8985 /**
8986  * pspp_sheet_view_new:
8987  *
8988  * Creates a new #PsppSheetView widget.
8989  *
8990  * Return value: A newly created #PsppSheetView widget.
8991  **/
8992 GtkWidget *
8993 pspp_sheet_view_new (void)
8994 {
8995   return g_object_new (PSPP_TYPE_SHEET_VIEW, NULL);
8996 }
8997
8998 /**
8999  * pspp_sheet_view_new_with_model:
9000  * @model: the model.
9001  *
9002  * Creates a new #PsppSheetView widget with the model initialized to @model.
9003  *
9004  * Return value: A newly created #PsppSheetView widget.
9005  **/
9006 GtkWidget *
9007 pspp_sheet_view_new_with_model (GtkTreeModel *model)
9008 {
9009   return g_object_new (PSPP_TYPE_SHEET_VIEW, "model", model, NULL);
9010 }
9011
9012 /* Public Accessors
9013  */
9014
9015 /**
9016  * pspp_sheet_view_get_model:
9017  * @tree_view: a #PsppSheetView
9018  *
9019  * Returns the model the #PsppSheetView is based on.  Returns %NULL if the
9020  * model is unset.
9021  *
9022  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
9023  **/
9024 GtkTreeModel *
9025 pspp_sheet_view_get_model (PsppSheetView *tree_view)
9026 {
9027   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9028
9029   return tree_view->priv->model;
9030 }
9031
9032 /**
9033  * pspp_sheet_view_set_model:
9034  * @tree_view: A #GtkTreeNode.
9035  * @model: (allow-none): The model.
9036  *
9037  * Sets the model for a #PsppSheetView.  If the @tree_view already has a model
9038  * set, it will remove it before setting the new model.  If @model is %NULL,
9039  * then it will unset the old model.
9040  **/
9041 void
9042 pspp_sheet_view_set_model (PsppSheetView  *tree_view,
9043                          GtkTreeModel *model)
9044 {
9045   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9046   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
9047
9048   if (model == tree_view->priv->model)
9049     return;
9050
9051   if (tree_view->priv->scroll_to_path)
9052     {
9053       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9054       tree_view->priv->scroll_to_path = NULL;
9055     }
9056
9057   if (tree_view->priv->model)
9058     {
9059       GList *tmplist = tree_view->priv->columns;
9060
9061       if (tree_view->priv->selected)
9062         range_tower_set0 (tree_view->priv->selected, 0, ULONG_MAX);
9063       pspp_sheet_view_stop_editing (tree_view, TRUE);
9064
9065       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9066                                             pspp_sheet_view_row_changed,
9067                                             tree_view);
9068       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9069                                             pspp_sheet_view_row_inserted,
9070                                             tree_view);
9071       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9072                                             pspp_sheet_view_row_deleted,
9073                                             tree_view);
9074       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9075                                             pspp_sheet_view_rows_reordered,
9076                                             tree_view);
9077
9078       for (; tmplist; tmplist = tmplist->next)
9079         _pspp_sheet_view_column_unset_model (tmplist->data,
9080                                            tree_view->priv->model);
9081
9082       tree_view->priv->prelight_node = -1;
9083
9084       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
9085       tree_view->priv->drag_dest_row = NULL;
9086       gtk_tree_row_reference_free (tree_view->priv->cursor);
9087       tree_view->priv->cursor = NULL;
9088       gtk_tree_row_reference_free (tree_view->priv->anchor);
9089       tree_view->priv->anchor = NULL;
9090       gtk_tree_row_reference_free (tree_view->priv->top_row);
9091       tree_view->priv->top_row = NULL;
9092       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9093       tree_view->priv->scroll_to_path = NULL;
9094
9095       tree_view->priv->scroll_to_column = NULL;
9096
9097       g_object_unref (tree_view->priv->model);
9098
9099       tree_view->priv->search_column = -1;
9100       tree_view->priv->fixed_height = -1;
9101       tree_view->priv->dy = tree_view->priv->top_row_dy = 0;
9102       tree_view->priv->last_button_x = -1;
9103       tree_view->priv->last_button_y = -1;
9104     }
9105
9106   tree_view->priv->model = model;
9107
9108   if (tree_view->priv->model)
9109     {
9110       gint i;
9111
9112       if (tree_view->priv->search_column == -1)
9113         {
9114           for (i = 0; i < gtk_tree_model_get_n_columns (model); i++)
9115             {
9116               GType type = gtk_tree_model_get_column_type (model, i);
9117
9118               if (g_value_type_transformable (type, G_TYPE_STRING))
9119                 {
9120                   tree_view->priv->search_column = i;
9121                   break;
9122                 }
9123             }
9124         }
9125
9126       g_object_ref (tree_view->priv->model);
9127       g_signal_connect (tree_view->priv->model,
9128                         "row-changed",
9129                         G_CALLBACK (pspp_sheet_view_row_changed),
9130                         tree_view);
9131       g_signal_connect (tree_view->priv->model,
9132                         "row-inserted",
9133                         G_CALLBACK (pspp_sheet_view_row_inserted),
9134                         tree_view);
9135       g_signal_connect (tree_view->priv->model,
9136                         "row-deleted",
9137                         G_CALLBACK (pspp_sheet_view_row_deleted),
9138                         tree_view);
9139       g_signal_connect (tree_view->priv->model,
9140                         "rows-reordered",
9141                         G_CALLBACK (pspp_sheet_view_rows_reordered),
9142                         tree_view);
9143
9144       tree_view->priv->row_count = gtk_tree_model_iter_n_children (tree_view->priv->model, NULL);
9145
9146       /*  FIXME: do I need to do this? pspp_sheet_view_create_buttons (tree_view); */
9147       install_presize_handler (tree_view);
9148     }
9149
9150   g_object_notify (G_OBJECT (tree_view), "model");
9151
9152   if (tree_view->priv->selection)
9153     _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
9154
9155   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9156     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9157 }
9158
9159 /**
9160  * pspp_sheet_view_get_selection:
9161  * @tree_view: A #PsppSheetView.
9162  *
9163  * Gets the #PsppSheetSelection associated with @tree_view.
9164  *
9165  * Return value: A #PsppSheetSelection object.
9166  **/
9167 PsppSheetSelection *
9168 pspp_sheet_view_get_selection (PsppSheetView *tree_view)
9169 {
9170   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9171
9172   return tree_view->priv->selection;
9173 }
9174
9175 /**
9176  * pspp_sheet_view_get_hadjustment:
9177  * @tree_view: A #PsppSheetView
9178  *
9179  * Gets the #GtkAdjustment currently being used for the horizontal aspect.
9180  *
9181  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
9182  * used.
9183  **/
9184 GtkAdjustment *
9185 pspp_sheet_view_get_hadjustment (PsppSheetView *tree_view)
9186 {
9187   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9188
9189   if (tree_view->priv->hadjustment == NULL)
9190     pspp_sheet_view_set_hadjustment (tree_view, NULL);
9191
9192   return tree_view->priv->hadjustment;
9193 }
9194
9195 /**
9196  * pspp_sheet_view_set_hadjustment:
9197  * @tree_view: A #PsppSheetView
9198  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9199  *
9200  * Sets the #GtkAdjustment for the current horizontal aspect.
9201  **/
9202 void
9203 pspp_sheet_view_set_hadjustment (PsppSheetView   *tree_view,
9204                                GtkAdjustment *adjustment)
9205 {
9206   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9207
9208   pspp_sheet_view_set_adjustments (tree_view,
9209                                  adjustment,
9210                                  tree_view->priv->vadjustment);
9211
9212   g_object_notify (G_OBJECT (tree_view), "hadjustment");
9213 }
9214
9215 /**
9216  * pspp_sheet_view_get_vadjustment:
9217  * @tree_view: A #PsppSheetView
9218  *
9219  * Gets the #GtkAdjustment currently being used for the vertical aspect.
9220  *
9221  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
9222  * used.
9223  **/
9224 GtkAdjustment *
9225 pspp_sheet_view_get_vadjustment (PsppSheetView *tree_view)
9226 {
9227   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9228
9229   if (tree_view->priv->vadjustment == NULL)
9230     pspp_sheet_view_set_vadjustment (tree_view, NULL);
9231
9232   return tree_view->priv->vadjustment;
9233 }
9234
9235 /**
9236  * pspp_sheet_view_set_vadjustment:
9237  * @tree_view: A #PsppSheetView
9238  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9239  *
9240  * Sets the #GtkAdjustment for the current vertical aspect.
9241  **/
9242 void
9243 pspp_sheet_view_set_vadjustment (PsppSheetView   *tree_view,
9244                                GtkAdjustment *adjustment)
9245 {
9246   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9247
9248   pspp_sheet_view_set_adjustments (tree_view,
9249                                  tree_view->priv->hadjustment,
9250                                  adjustment);
9251
9252   g_object_notify (G_OBJECT (tree_view), "vadjustment");
9253 }
9254
9255 /* Column and header operations */
9256
9257 /**
9258  * pspp_sheet_view_get_headers_visible:
9259  * @tree_view: A #PsppSheetView.
9260  *
9261  * Returns %TRUE if the headers on the @tree_view are visible.
9262  *
9263  * Return value: Whether the headers are visible or not.
9264  **/
9265 gboolean
9266 pspp_sheet_view_get_headers_visible (PsppSheetView *tree_view)
9267 {
9268   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9269
9270   return PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9271 }
9272
9273 /**
9274  * pspp_sheet_view_set_headers_visible:
9275  * @tree_view: A #PsppSheetView.
9276  * @headers_visible: %TRUE if the headers are visible
9277  *
9278  * Sets the visibility state of the headers.
9279  **/
9280 void
9281 pspp_sheet_view_set_headers_visible (PsppSheetView *tree_view,
9282                                    gboolean     headers_visible)
9283 {
9284   gint x, y;
9285   GList *list;
9286   PsppSheetViewColumn *column;
9287   GtkAllocation allocation;
9288
9289   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9290
9291   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
9292
9293   headers_visible = !! headers_visible;
9294
9295   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE) == headers_visible)
9296     return;
9297
9298   if (headers_visible)
9299     PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9300   else
9301     PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9302
9303   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9304     {
9305       gdk_window_get_position (tree_view->priv->bin_window, &x, &y);
9306       if (headers_visible)
9307         {
9308           gdk_window_move_resize (tree_view->priv->bin_window, x, y  + TREE_VIEW_HEADER_HEIGHT (tree_view), 
9309                                   tree_view->priv->width, allocation.height -  + TREE_VIEW_HEADER_HEIGHT (tree_view));
9310
9311           if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
9312             pspp_sheet_view_map_buttons (tree_view);
9313         }
9314       else
9315         {
9316           gdk_window_move_resize (tree_view->priv->bin_window, x, y, tree_view->priv->width, tree_view->priv->height);
9317
9318           for (list = tree_view->priv->columns; list; list = list->next)
9319             {
9320               column = list->data;
9321               if (column->button)
9322                 gtk_widget_unmap (column->button);
9323             }
9324           gdk_window_hide (tree_view->priv->header_window);
9325         }
9326     }
9327
9328   gtk_adjustment_set_page_size (tree_view->priv->vadjustment, allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view));
9329   gtk_adjustment_set_page_increment (tree_view->priv->vadjustment, (allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view)) / 2);
9330   gtk_adjustment_set_lower (tree_view->priv->vadjustment, 0);
9331   gtk_adjustment_set_upper (tree_view->priv->vadjustment, tree_view->priv->height);
9332   gtk_adjustment_changed (tree_view->priv->vadjustment);
9333
9334   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9335
9336   g_object_notify (G_OBJECT (tree_view), "headers-visible");
9337 }
9338
9339 /**
9340  * pspp_sheet_view_columns_autosize:
9341  * @tree_view: A #PsppSheetView.
9342  *
9343  * Resizes all columns to their optimal width. Only works after the
9344  * treeview has been realized.
9345  **/
9346 void
9347 pspp_sheet_view_columns_autosize (PsppSheetView *tree_view)
9348 {
9349   gboolean dirty = FALSE;
9350   GList *list;
9351   PsppSheetViewColumn *column;
9352
9353   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9354
9355   for (list = tree_view->priv->columns; list; list = list->next)
9356     {
9357       column = list->data;
9358       _pspp_sheet_view_column_cell_set_dirty (column);
9359       dirty = TRUE;
9360     }
9361
9362   if (dirty)
9363     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9364 }
9365
9366 /**
9367  * pspp_sheet_view_set_headers_clickable:
9368  * @tree_view: A #PsppSheetView.
9369  * @setting: %TRUE if the columns are clickable.
9370  *
9371  * Allow the column title buttons to be clicked.
9372  **/
9373 void
9374 pspp_sheet_view_set_headers_clickable (PsppSheetView *tree_view,
9375                                      gboolean   setting)
9376 {
9377   GList *list;
9378
9379   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9380
9381   for (list = tree_view->priv->columns; list; list = list->next)
9382     pspp_sheet_view_column_set_clickable (PSPP_SHEET_VIEW_COLUMN (list->data), setting);
9383
9384   g_object_notify (G_OBJECT (tree_view), "headers-clickable");
9385 }
9386
9387
9388 /**
9389  * pspp_sheet_view_get_headers_clickable:
9390  * @tree_view: A #PsppSheetView.
9391  *
9392  * Returns whether all header columns are clickable.
9393  *
9394  * Return value: %TRUE if all header columns are clickable, otherwise %FALSE
9395  *
9396  * Since: 2.10
9397  **/
9398 gboolean 
9399 pspp_sheet_view_get_headers_clickable (PsppSheetView *tree_view)
9400 {
9401   GList *list;
9402   
9403   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9404
9405   for (list = tree_view->priv->columns; list; list = list->next)
9406     if (!PSPP_SHEET_VIEW_COLUMN (list->data)->clickable)
9407       return FALSE;
9408
9409   return TRUE;
9410 }
9411
9412 /**
9413  * pspp_sheet_view_set_rules_hint
9414  * @tree_view: a #PsppSheetView
9415  * @setting: %TRUE if the tree requires reading across rows
9416  *
9417  * This function tells GTK+ that the user interface for your
9418  * application requires users to read across tree rows and associate
9419  * cells with one another. By default, GTK+ will then render the tree
9420  * with alternating row colors. Do <emphasis>not</emphasis> use it
9421  * just because you prefer the appearance of the ruled tree; that's a
9422  * question for the theme. Some themes will draw tree rows in
9423  * alternating colors even when rules are turned off, and users who
9424  * prefer that appearance all the time can choose those themes. You
9425  * should call this function only as a <emphasis>semantic</emphasis>
9426  * hint to the theme engine that your tree makes alternating colors
9427  * useful from a functional standpoint (since it has lots of columns,
9428  * generally).
9429  *
9430  **/
9431 void
9432 pspp_sheet_view_set_rules_hint (PsppSheetView  *tree_view,
9433                               gboolean      setting)
9434 {
9435   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9436
9437   setting = setting != FALSE;
9438
9439   if (tree_view->priv->has_rules != setting)
9440     {
9441       tree_view->priv->has_rules = setting;
9442       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
9443     }
9444
9445   g_object_notify (G_OBJECT (tree_view), "rules-hint");
9446 }
9447
9448 /**
9449  * pspp_sheet_view_get_rules_hint
9450  * @tree_view: a #PsppSheetView
9451  *
9452  * Gets the setting set by pspp_sheet_view_set_rules_hint().
9453  *
9454  * Return value: %TRUE if rules are useful for the user of this tree
9455  **/
9456 gboolean
9457 pspp_sheet_view_get_rules_hint (PsppSheetView  *tree_view)
9458 {
9459   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9460
9461   return tree_view->priv->has_rules;
9462 }
9463
9464 /* Public Column functions
9465  */
9466
9467 /**
9468  * pspp_sheet_view_append_column:
9469  * @tree_view: A #PsppSheetView.
9470  * @column: The #PsppSheetViewColumn to add.
9471  *
9472  * Appends @column to the list of columns.
9473  *
9474  * Return value: The number of columns in @tree_view after appending.
9475  **/
9476 gint
9477 pspp_sheet_view_append_column (PsppSheetView       *tree_view,
9478                              PsppSheetViewColumn *column)
9479 {
9480   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9481   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9482   g_return_val_if_fail (column->tree_view == NULL, -1);
9483
9484   return pspp_sheet_view_insert_column (tree_view, column, -1);
9485 }
9486
9487
9488 /**
9489  * pspp_sheet_view_remove_column:
9490  * @tree_view: A #PsppSheetView.
9491  * @column: The #PsppSheetViewColumn to remove.
9492  *
9493  * Removes @column from @tree_view.
9494  *
9495  * Return value: The number of columns in @tree_view after removing.
9496  **/
9497 gint
9498 pspp_sheet_view_remove_column (PsppSheetView       *tree_view,
9499                              PsppSheetViewColumn *column)
9500 {
9501   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9502   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9503   g_return_val_if_fail (column->tree_view == GTK_WIDGET (tree_view), -1);
9504
9505   if (tree_view->priv->focus_column == column)
9506     tree_view->priv->focus_column = NULL;
9507
9508   if (tree_view->priv->edited_column == column)
9509     {
9510       pspp_sheet_view_stop_editing (tree_view, TRUE);
9511
9512       /* no need to, but just to be sure ... */
9513       tree_view->priv->edited_column = NULL;
9514     }
9515
9516   _pspp_sheet_view_column_unset_tree_view (column);
9517
9518   tree_view->priv->columns = g_list_remove (tree_view->priv->columns, column);
9519   tree_view->priv->n_columns--;
9520
9521   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9522     {
9523       GList *list;
9524
9525       _pspp_sheet_view_column_unrealize_button (column);
9526       for (list = tree_view->priv->columns; list; list = list->next)
9527         {
9528           PsppSheetViewColumn *tmp_column;
9529
9530           tmp_column = PSPP_SHEET_VIEW_COLUMN (list->data);
9531           if (tmp_column->visible)
9532             _pspp_sheet_view_column_cell_set_dirty (tmp_column);
9533         }
9534
9535       if (tree_view->priv->n_columns == 0 &&
9536           pspp_sheet_view_get_headers_visible (tree_view) && 
9537           tree_view->priv->header_window)
9538         gdk_window_hide (tree_view->priv->header_window);
9539
9540       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9541     }
9542
9543   g_object_unref (column);
9544   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9545
9546   return tree_view->priv->n_columns;
9547 }
9548
9549 /**
9550  * pspp_sheet_view_insert_column:
9551  * @tree_view: A #PsppSheetView.
9552  * @column: The #PsppSheetViewColumn to be inserted.
9553  * @position: The position to insert @column in.
9554  *
9555  * This inserts the @column into the @tree_view at @position.  If @position is
9556  * -1, then the column is inserted at the end.
9557  *
9558  * Return value: The number of columns in @tree_view after insertion.
9559  **/
9560 gint
9561 pspp_sheet_view_insert_column (PsppSheetView       *tree_view,
9562                              PsppSheetViewColumn *column,
9563                              gint               position)
9564 {
9565   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9566   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9567   g_return_val_if_fail (column->tree_view == NULL, -1);
9568
9569   g_object_ref_sink (column);
9570
9571   if (tree_view->priv->n_columns == 0 &&
9572       gtk_widget_get_realized (GTK_WIDGET (tree_view)) &&
9573       pspp_sheet_view_get_headers_visible (tree_view))
9574     {
9575       gdk_window_show (tree_view->priv->header_window);
9576     }
9577
9578   tree_view->priv->columns = g_list_insert (tree_view->priv->columns,
9579                                             column, position);
9580   tree_view->priv->n_columns++;
9581
9582   _pspp_sheet_view_column_set_tree_view (column, tree_view);
9583
9584   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9585     {
9586       GList *list;
9587
9588       _pspp_sheet_view_column_realize_button (column);
9589
9590       for (list = tree_view->priv->columns; list; list = list->next)
9591         {
9592           column = PSPP_SHEET_VIEW_COLUMN (list->data);
9593           if (column->visible)
9594             _pspp_sheet_view_column_cell_set_dirty (column);
9595         }
9596       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9597     }
9598
9599   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9600
9601   return tree_view->priv->n_columns;
9602 }
9603
9604 /**
9605  * pspp_sheet_view_insert_column_with_attributes:
9606  * @tree_view: A #PsppSheetView
9607  * @position: The position to insert the new column in.
9608  * @title: The title to set the header to.
9609  * @cell: The #GtkCellRenderer.
9610  * @Varargs: A %NULL-terminated list of attributes.
9611  *
9612  * Creates a new #PsppSheetViewColumn and inserts it into the @tree_view at
9613  * @position.  If @position is -1, then the newly created column is inserted at
9614  * the end.  The column is initialized with the attributes given.
9615  *
9616  * Return value: The number of columns in @tree_view after insertion.
9617  **/
9618 gint
9619 pspp_sheet_view_insert_column_with_attributes (PsppSheetView     *tree_view,
9620                                              gint             position,
9621                                              const gchar     *title,
9622                                              GtkCellRenderer *cell,
9623                                              ...)
9624 {
9625   PsppSheetViewColumn *column;
9626   gchar *attribute;
9627   va_list args;
9628   gint column_id;
9629
9630   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9631
9632   column = pspp_sheet_view_column_new ();
9633   pspp_sheet_view_column_set_title (column, title);
9634   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9635
9636   va_start (args, cell);
9637
9638   attribute = va_arg (args, gchar *);
9639
9640   while (attribute != NULL)
9641     {
9642       column_id = va_arg (args, gint);
9643       pspp_sheet_view_column_add_attribute (column, cell, attribute, column_id);
9644       attribute = va_arg (args, gchar *);
9645     }
9646
9647   va_end (args);
9648
9649   pspp_sheet_view_insert_column (tree_view, column, position);
9650
9651   return tree_view->priv->n_columns;
9652 }
9653
9654 /**
9655  * pspp_sheet_view_insert_column_with_data_func:
9656  * @tree_view: a #PsppSheetView
9657  * @position: Position to insert, -1 for append
9658  * @title: column title
9659  * @cell: cell renderer for column
9660  * @func: function to set attributes of cell renderer
9661  * @data: data for @func
9662  * @dnotify: destroy notifier for @data
9663  *
9664  * Convenience function that inserts a new column into the #PsppSheetView
9665  * with the given cell renderer and a #GtkCellDataFunc to set cell renderer
9666  * attributes (normally using data from the model). See also
9667  * pspp_sheet_view_column_set_cell_data_func(), pspp_sheet_view_column_pack_start().
9668  *
9669  * Return value: number of columns in the tree view post-insert
9670  **/
9671 gint
9672 pspp_sheet_view_insert_column_with_data_func  (PsppSheetView               *tree_view,
9673                                              gint                       position,
9674                                              const gchar               *title,
9675                                              GtkCellRenderer           *cell,
9676                                              PsppSheetCellDataFunc        func,
9677                                              gpointer                   data,
9678                                              GDestroyNotify             dnotify)
9679 {
9680   PsppSheetViewColumn *column;
9681
9682   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9683
9684   column = pspp_sheet_view_column_new ();
9685   pspp_sheet_view_column_set_title (column, title);
9686   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9687   pspp_sheet_view_column_set_cell_data_func (column, cell, func, data, dnotify);
9688
9689   pspp_sheet_view_insert_column (tree_view, column, position);
9690
9691   return tree_view->priv->n_columns;
9692 }
9693
9694 /**
9695  * pspp_sheet_view_get_column:
9696  * @tree_view: A #PsppSheetView.
9697  * @n: The position of the column, counting from 0.
9698  *
9699  * Gets the #PsppSheetViewColumn at the given position in the #tree_view.
9700  *
9701  * Return value: The #PsppSheetViewColumn, or %NULL if the position is outside the
9702  * range of columns.
9703  **/
9704 PsppSheetViewColumn *
9705 pspp_sheet_view_get_column (PsppSheetView *tree_view,
9706                           gint         n)
9707 {
9708   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9709
9710   if (n < 0 || n >= tree_view->priv->n_columns)
9711     return NULL;
9712
9713   if (tree_view->priv->columns == NULL)
9714     return NULL;
9715
9716   return PSPP_SHEET_VIEW_COLUMN (g_list_nth (tree_view->priv->columns, n)->data);
9717 }
9718
9719 /**
9720  * pspp_sheet_view_get_columns:
9721  * @tree_view: A #PsppSheetView
9722  *
9723  * Returns a #GList of all the #PsppSheetViewColumn s currently in @tree_view.
9724  * The returned list must be freed with g_list_free ().
9725  *
9726  * Return value: (element-type PsppSheetViewColumn) (transfer container): A list of #PsppSheetViewColumn s
9727  **/
9728 GList *
9729 pspp_sheet_view_get_columns (PsppSheetView *tree_view)
9730 {
9731   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9732
9733   return g_list_copy (tree_view->priv->columns);
9734 }
9735
9736 /**
9737  * pspp_sheet_view_move_column_after:
9738  * @tree_view: A #PsppSheetView
9739  * @column: The #PsppSheetViewColumn to be moved.
9740  * @base_column: (allow-none): The #PsppSheetViewColumn to be moved relative to, or %NULL.
9741  *
9742  * Moves @column to be after to @base_column.  If @base_column is %NULL, then
9743  * @column is placed in the first position.
9744  **/
9745 void
9746 pspp_sheet_view_move_column_after (PsppSheetView       *tree_view,
9747                                  PsppSheetViewColumn *column,
9748                                  PsppSheetViewColumn *base_column)
9749 {
9750   GList *column_list_el, *base_el = NULL;
9751
9752   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9753
9754   column_list_el = g_list_find (tree_view->priv->columns, column);
9755   g_return_if_fail (column_list_el != NULL);
9756
9757   if (base_column)
9758     {
9759       base_el = g_list_find (tree_view->priv->columns, base_column);
9760       g_return_if_fail (base_el != NULL);
9761     }
9762
9763   if (column_list_el->prev == base_el)
9764     return;
9765
9766   tree_view->priv->columns = g_list_remove_link (tree_view->priv->columns, column_list_el);
9767   if (base_el == NULL)
9768     {
9769       column_list_el->prev = NULL;
9770       column_list_el->next = tree_view->priv->columns;
9771       if (column_list_el->next)
9772         column_list_el->next->prev = column_list_el;
9773       tree_view->priv->columns = column_list_el;
9774     }
9775   else
9776     {
9777       column_list_el->prev = base_el;
9778       column_list_el->next = base_el->next;
9779       if (column_list_el->next)
9780         column_list_el->next->prev = column_list_el;
9781       base_el->next = column_list_el;
9782     }
9783
9784   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9785     {
9786       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9787       pspp_sheet_view_size_allocate_columns (GTK_WIDGET (tree_view), NULL);
9788     }
9789
9790   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9791 }
9792
9793 /**
9794  * pspp_sheet_view_set_column_drag_function:
9795  * @tree_view: A #PsppSheetView.
9796  * @func: (allow-none): A function to determine which columns are reorderable, or %NULL.
9797  * @user_data: (allow-none): User data to be passed to @func, or %NULL
9798  * @destroy: (allow-none): Destroy notifier for @user_data, or %NULL
9799  *
9800  * Sets a user function for determining where a column may be dropped when
9801  * dragged.  This function is called on every column pair in turn at the
9802  * beginning of a column drag to determine where a drop can take place.  The
9803  * arguments passed to @func are: the @tree_view, the #PsppSheetViewColumn being
9804  * dragged, the two #PsppSheetViewColumn s determining the drop spot, and
9805  * @user_data.  If either of the #PsppSheetViewColumn arguments for the drop spot
9806  * are %NULL, then they indicate an edge.  If @func is set to be %NULL, then
9807  * @tree_view reverts to the default behavior of allowing all columns to be
9808  * dropped everywhere.
9809  **/
9810 void
9811 pspp_sheet_view_set_column_drag_function (PsppSheetView               *tree_view,
9812                                         PsppSheetViewColumnDropFunc  func,
9813                                         gpointer                   user_data,
9814                                         GDestroyNotify             destroy)
9815 {
9816   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9817
9818   if (tree_view->priv->column_drop_func_data_destroy)
9819     tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
9820
9821   tree_view->priv->column_drop_func = func;
9822   tree_view->priv->column_drop_func_data = user_data;
9823   tree_view->priv->column_drop_func_data_destroy = destroy;
9824 }
9825
9826 /**
9827  * pspp_sheet_view_scroll_to_point:
9828  * @tree_view: a #PsppSheetView
9829  * @tree_x: X coordinate of new top-left pixel of visible area, or -1
9830  * @tree_y: Y coordinate of new top-left pixel of visible area, or -1
9831  *
9832  * Scrolls the tree view such that the top-left corner of the visible
9833  * area is @tree_x, @tree_y, where @tree_x and @tree_y are specified
9834  * in tree coordinates.  The @tree_view must be realized before
9835  * this function is called.  If it isn't, you probably want to be
9836  * using pspp_sheet_view_scroll_to_cell().
9837  *
9838  * If either @tree_x or @tree_y are -1, then that direction isn't scrolled.
9839  **/
9840 void
9841 pspp_sheet_view_scroll_to_point (PsppSheetView *tree_view,
9842                                gint         tree_x,
9843                                gint         tree_y)
9844 {
9845   GtkAdjustment *hadj;
9846   GtkAdjustment *vadj;
9847
9848   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9849   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
9850
9851   hadj = tree_view->priv->hadjustment;
9852   vadj = tree_view->priv->vadjustment;
9853
9854   if (tree_x != -1)
9855     gtk_adjustment_set_value (hadj, CLAMP (tree_x, gtk_adjustment_get_lower (hadj), gtk_adjustment_get_upper (hadj) - gtk_adjustment_get_page_size (hadj)));
9856   if (tree_y != -1)
9857     gtk_adjustment_set_value (vadj, CLAMP (tree_y, gtk_adjustment_get_lower (vadj), gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj)));
9858 }
9859
9860 /**
9861  * pspp_sheet_view_scroll_to_cell:
9862  * @tree_view: A #PsppSheetView.
9863  * @path: (allow-none): The path of the row to move to, or %NULL.
9864  * @column: (allow-none): The #PsppSheetViewColumn to move horizontally to, or %NULL.
9865  * @use_align: whether to use alignment arguments, or %FALSE.
9866  * @row_align: The vertical alignment of the row specified by @path.
9867  * @col_align: The horizontal alignment of the column specified by @column.
9868  *
9869  * Moves the alignments of @tree_view to the position specified by @column and
9870  * @path.  If @column is %NULL, then no horizontal scrolling occurs.  Likewise,
9871  * if @path is %NULL no vertical scrolling occurs.  At a minimum, one of @column
9872  * or @path need to be non-%NULL.  @row_align determines where the row is
9873  * placed, and @col_align determines where @column is placed.  Both are expected
9874  * to be between 0.0 and 1.0. 0.0 means left/top alignment, 1.0 means
9875  * right/bottom alignment, 0.5 means center.
9876  *
9877  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
9878  * tree does the minimum amount of work to scroll the cell onto the screen.
9879  * This means that the cell will be scrolled to the edge closest to its current
9880  * position.  If the cell is currently visible on the screen, nothing is done.
9881  *
9882  * This function only works if the model is set, and @path is a valid row on the
9883  * model.  If the model changes before the @tree_view is realized, the centered
9884  * path will be modified to reflect this change.
9885  **/
9886 void
9887 pspp_sheet_view_scroll_to_cell (PsppSheetView       *tree_view,
9888                               GtkTreePath       *path,
9889                               PsppSheetViewColumn *column,
9890                               gboolean           use_align,
9891                               gfloat             row_align,
9892                               gfloat             col_align)
9893 {
9894   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9895   g_return_if_fail (tree_view->priv->model != NULL);
9896   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
9897   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
9898   g_return_if_fail (path != NULL || column != NULL);
9899
9900 #if 0
9901   g_print ("pspp_sheet_view_scroll_to_cell:\npath: %s\ncolumn: %s\nuse_align: %d\nrow_align: %f\ncol_align: %f\n",
9902            gtk_tree_path_to_string (path), column?"non-null":"null", use_align, row_align, col_align);
9903 #endif
9904   row_align = CLAMP (row_align, 0.0, 1.0);
9905   col_align = CLAMP (col_align, 0.0, 1.0);
9906
9907
9908   /* Note: Despite the benefits that come from having one code path for the
9909    * scrolling code, we short-circuit validate_visible_area's immplementation as
9910    * it is much slower than just going to the point.
9911    */
9912   if (!gtk_widget_get_visible (GTK_WIDGET (tree_view)) ||
9913       !gtk_widget_get_realized (GTK_WIDGET (tree_view))
9914       /* XXX || GTK_WIDGET_ALLOC_NEEDED (tree_view) */)
9915     {
9916       if (tree_view->priv->scroll_to_path)
9917         gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9918
9919       tree_view->priv->scroll_to_path = NULL;
9920       tree_view->priv->scroll_to_column = NULL;
9921
9922       if (path)
9923         tree_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
9924       if (column)
9925         tree_view->priv->scroll_to_column = column;
9926       tree_view->priv->scroll_to_use_align = use_align;
9927       tree_view->priv->scroll_to_row_align = row_align;
9928       tree_view->priv->scroll_to_col_align = col_align;
9929
9930       install_presize_handler (tree_view);
9931     }
9932   else
9933     {
9934       GdkRectangle cell_rect;
9935       GdkRectangle vis_rect;
9936       gint dest_x, dest_y;
9937
9938       pspp_sheet_view_get_background_area (tree_view, path, column, &cell_rect);
9939       pspp_sheet_view_get_visible_rect (tree_view, &vis_rect);
9940
9941       cell_rect.y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, cell_rect.y);
9942
9943       dest_x = vis_rect.x;
9944       dest_y = vis_rect.y;
9945
9946       if (column)
9947         {
9948           if (use_align)
9949             {
9950               dest_x = cell_rect.x - ((vis_rect.width - cell_rect.width) * col_align);
9951             }
9952           else
9953             {
9954               if (cell_rect.x < vis_rect.x)
9955                 dest_x = cell_rect.x;
9956               if (cell_rect.x + cell_rect.width > vis_rect.x + vis_rect.width)
9957                 dest_x = cell_rect.x + cell_rect.width - vis_rect.width;
9958             }
9959         }
9960
9961       if (path)
9962         {
9963           if (use_align)
9964             {
9965               dest_y = cell_rect.y - ((vis_rect.height - cell_rect.height) * row_align);
9966               dest_y = MAX (dest_y, 0);
9967             }
9968           else
9969             {
9970               if (cell_rect.y < vis_rect.y)
9971                 dest_y = cell_rect.y;
9972               if (cell_rect.y + cell_rect.height > vis_rect.y + vis_rect.height)
9973                 dest_y = cell_rect.y + cell_rect.height - vis_rect.height;
9974             }
9975         }
9976
9977       pspp_sheet_view_scroll_to_point (tree_view, dest_x, dest_y);
9978     }
9979 }
9980
9981 /**
9982  * pspp_sheet_view_row_activated:
9983  * @tree_view: A #PsppSheetView
9984  * @path: The #GtkTreePath to be activated.
9985  * @column: The #PsppSheetViewColumn to be activated.
9986  *
9987  * Activates the cell determined by @path and @column.
9988  **/
9989 void
9990 pspp_sheet_view_row_activated (PsppSheetView       *tree_view,
9991                              GtkTreePath       *path,
9992                              PsppSheetViewColumn *column)
9993 {
9994   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9995
9996   g_signal_emit (tree_view, tree_view_signals[ROW_ACTIVATED], 0, path, column);
9997 }
9998
9999
10000 /**
10001  * pspp_sheet_view_get_reorderable:
10002  * @tree_view: a #PsppSheetView
10003  *
10004  * Retrieves whether the user can reorder the tree via drag-and-drop. See
10005  * pspp_sheet_view_set_reorderable().
10006  *
10007  * Return value: %TRUE if the tree can be reordered.
10008  **/
10009 gboolean
10010 pspp_sheet_view_get_reorderable (PsppSheetView *tree_view)
10011 {
10012   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10013
10014   return tree_view->priv->reorderable;
10015 }
10016
10017 /**
10018  * pspp_sheet_view_set_reorderable:
10019  * @tree_view: A #PsppSheetView.
10020  * @reorderable: %TRUE, if the tree can be reordered.
10021  *
10022  * This function is a convenience function to allow you to reorder
10023  * models that support the #GtkDragSourceIface and the
10024  * #GtkDragDestIface.  Both #GtkTreeStore and #GtkListStore support
10025  * these.  If @reorderable is %TRUE, then the user can reorder the
10026  * model by dragging and dropping rows. The developer can listen to
10027  * these changes by connecting to the model's row_inserted and
10028  * row_deleted signals. The reordering is implemented by setting up
10029  * the tree view as a drag source and destination. Therefore, drag and
10030  * drop can not be used in a reorderable view for any other purpose.
10031  *
10032  * This function does not give you any degree of control over the order -- any
10033  * reordering is allowed.  If more control is needed, you should probably
10034  * handle drag and drop manually.
10035  **/
10036 void
10037 pspp_sheet_view_set_reorderable (PsppSheetView *tree_view,
10038                                gboolean     reorderable)
10039 {
10040   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10041
10042   reorderable = reorderable != FALSE;
10043
10044   if (tree_view->priv->reorderable == reorderable)
10045     return;
10046
10047   if (reorderable)
10048     {
10049       const GtkTargetEntry row_targets[] = {
10050         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
10051       };
10052
10053       pspp_sheet_view_enable_model_drag_source (tree_view,
10054                                               GDK_BUTTON1_MASK,
10055                                               row_targets,
10056                                               G_N_ELEMENTS (row_targets),
10057                                               GDK_ACTION_MOVE);
10058       pspp_sheet_view_enable_model_drag_dest (tree_view,
10059                                             row_targets,
10060                                             G_N_ELEMENTS (row_targets),
10061                                             GDK_ACTION_MOVE);
10062     }
10063   else
10064     {
10065       pspp_sheet_view_unset_rows_drag_source (tree_view);
10066       pspp_sheet_view_unset_rows_drag_dest (tree_view);
10067     }
10068
10069   tree_view->priv->reorderable = reorderable;
10070
10071   g_object_notify (G_OBJECT (tree_view), "reorderable");
10072 }
10073
10074 /* If CLEAR_AND_SELECT is true, then the row will be selected and, unless Shift
10075    is pressed, other rows will be unselected.
10076
10077    If CLAMP_NODE is true, then the sheetview will scroll to make the row
10078    visible. */
10079 static void
10080 pspp_sheet_view_real_set_cursor (PsppSheetView     *tree_view,
10081                                GtkTreePath     *path,
10082                                gboolean         clear_and_select,
10083                                gboolean         clamp_node,
10084                                PsppSheetSelectMode mode)
10085 {
10086   int node = -1;
10087
10088   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10089     {
10090       GtkTreePath *cursor_path;
10091       cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10092       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10093       gtk_tree_path_free (cursor_path);
10094     }
10095
10096   gtk_tree_row_reference_free (tree_view->priv->cursor);
10097   tree_view->priv->cursor = NULL;
10098
10099   _pspp_sheet_view_find_node (tree_view, path, &node);
10100   tree_view->priv->cursor =
10101     gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
10102                                       tree_view->priv->model,
10103                                       path);
10104
10105   if (tree_view->priv->row_count > 0)
10106     {
10107       int new_node = -1;
10108
10109       if (clear_and_select && !(mode & PSPP_SHEET_SELECT_MODE_TOGGLE))
10110         _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
10111                                                     node, path, mode,
10112                                                     FALSE);
10113
10114       /* We have to re-find tree and node here again, somebody might have
10115        * cleared the node or the whole tree in the PsppSheetSelection::changed
10116        * callback. If the nodes differ we bail out here.
10117        */
10118       _pspp_sheet_view_find_node (tree_view, path, &new_node);
10119
10120       if (node != new_node)
10121         return;
10122
10123       if (clamp_node)
10124         {
10125           pspp_sheet_view_clamp_node_visible (tree_view, node);
10126           _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
10127         }
10128     }
10129
10130   g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
10131 }
10132
10133 /**
10134  * pspp_sheet_view_get_cursor:
10135  * @tree_view: A #PsppSheetView
10136  * @path: (allow-none): A pointer to be filled with the current cursor path, or %NULL
10137  * @focus_column: (allow-none): A pointer to be filled with the current focus column, or %NULL
10138  *
10139  * Fills in @path and @focus_column with the current path and focus column.  If
10140  * the cursor isn't currently set, then *@path will be %NULL.  If no column
10141  * currently has focus, then *@focus_column will be %NULL.
10142  *
10143  * The returned #GtkTreePath must be freed with gtk_tree_path_free() when
10144  * you are done with it.
10145  **/
10146 void
10147 pspp_sheet_view_get_cursor (PsppSheetView        *tree_view,
10148                           GtkTreePath       **path,
10149                           PsppSheetViewColumn **focus_column)
10150 {
10151   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10152
10153   if (path)
10154     {
10155       if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10156         *path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10157       else
10158         *path = NULL;
10159     }
10160
10161   if (focus_column)
10162     {
10163       *focus_column = tree_view->priv->focus_column;
10164     }
10165 }
10166
10167 /**
10168  * pspp_sheet_view_set_cursor:
10169  * @tree_view: A #PsppSheetView
10170  * @path: A #GtkTreePath
10171  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10172  * @start_editing: %TRUE if the specified cell should start being edited.
10173  *
10174  * Sets the current keyboard focus to be at @path, and selects it.  This is
10175  * useful when you want to focus the user's attention on a particular row.  If
10176  * @focus_column is not %NULL, then focus is given to the column specified by 
10177  * it. Additionally, if @focus_column is specified, and @start_editing is 
10178  * %TRUE, then editing should be started in the specified cell.  
10179  * This function is often followed by @gtk_widget_grab_focus (@tree_view) 
10180  * in order to give keyboard focus to the widget.  Please note that editing 
10181  * can only happen when the widget is realized.
10182  *
10183  * If @path is invalid for @model, the current cursor (if any) will be unset
10184  * and the function will return without failing.
10185  **/
10186 void
10187 pspp_sheet_view_set_cursor (PsppSheetView       *tree_view,
10188                           GtkTreePath       *path,
10189                           PsppSheetViewColumn *focus_column,
10190                           gboolean           start_editing)
10191 {
10192   pspp_sheet_view_set_cursor_on_cell (tree_view, path, focus_column,
10193                                     NULL, start_editing);
10194 }
10195
10196 /**
10197  * pspp_sheet_view_set_cursor_on_cell:
10198  * @tree_view: A #PsppSheetView
10199  * @path: A #GtkTreePath
10200  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10201  * @focus_cell: (allow-none): A #GtkCellRenderer, or %NULL
10202  * @start_editing: %TRUE if the specified cell should start being edited.
10203  *
10204  * Sets the current keyboard focus to be at @path, and selects it.  This is
10205  * useful when you want to focus the user's attention on a particular row.  If
10206  * @focus_column is not %NULL, then focus is given to the column specified by
10207  * it. If @focus_column and @focus_cell are not %NULL, and @focus_column
10208  * contains 2 or more editable or activatable cells, then focus is given to
10209  * the cell specified by @focus_cell. Additionally, if @focus_column is
10210  * specified, and @start_editing is %TRUE, then editing should be started in
10211  * the specified cell.  This function is often followed by
10212  * @gtk_widget_grab_focus (@tree_view) in order to give keyboard focus to the
10213  * widget.  Please note that editing can only happen when the widget is
10214  * realized.
10215  *
10216  * If @path is invalid for @model, the current cursor (if any) will be unset
10217  * and the function will return without failing.
10218  *
10219  * Since: 2.2
10220  **/
10221 void
10222 pspp_sheet_view_set_cursor_on_cell (PsppSheetView       *tree_view,
10223                                   GtkTreePath       *path,
10224                                   PsppSheetViewColumn *focus_column,
10225                                   GtkCellRenderer   *focus_cell,
10226                                   gboolean           start_editing)
10227 {
10228   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10229   g_return_if_fail (path != NULL);
10230   g_return_if_fail (focus_column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (focus_column));
10231
10232   if (!tree_view->priv->model)
10233     return;
10234
10235   if (focus_cell)
10236     {
10237       g_return_if_fail (focus_column);
10238       g_return_if_fail (GTK_IS_CELL_RENDERER (focus_cell));
10239     }
10240
10241   /* cancel the current editing, if it exists */
10242   if (tree_view->priv->edited_column &&
10243       tree_view->priv->edited_column->editable_widget)
10244     pspp_sheet_view_stop_editing (tree_view, TRUE);
10245
10246   pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, 0);
10247
10248   if (focus_column && focus_column->visible)
10249     {
10250       GList *list;
10251       gboolean column_in_tree = FALSE;
10252
10253       for (list = tree_view->priv->columns; list; list = list->next)
10254         if (list->data == focus_column)
10255           {
10256             column_in_tree = TRUE;
10257             break;
10258           }
10259       g_return_if_fail (column_in_tree);
10260       tree_view->priv->focus_column = focus_column;
10261       if (focus_cell)
10262         pspp_sheet_view_column_focus_cell (focus_column, focus_cell);
10263       if (start_editing)
10264         pspp_sheet_view_start_editing (tree_view, path);
10265
10266       pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
10267       pspp_sheet_selection_select_column (tree_view->priv->selection, focus_column);
10268
10269     }
10270 }
10271
10272 /**
10273  * pspp_sheet_view_get_bin_window:
10274  * @tree_view: A #PsppSheetView
10275  * 
10276  * Returns the window that @tree_view renders to.  This is used primarily to
10277  * compare to <literal>event->window</literal> to confirm that the event on
10278  * @tree_view is on the right window.
10279  * 
10280  * Return value: A #GdkWindow, or %NULL when @tree_view hasn't been realized yet
10281  **/
10282 GdkWindow *
10283 pspp_sheet_view_get_bin_window (PsppSheetView *tree_view)
10284 {
10285   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10286
10287   return tree_view->priv->bin_window;
10288 }
10289
10290 /**
10291  * pspp_sheet_view_get_path_at_pos:
10292  * @tree_view: A #PsppSheetView.
10293  * @x: The x position to be identified (relative to bin_window).
10294  * @y: The y position to be identified (relative to bin_window).
10295  * @path: (out) (allow-none): A pointer to a #GtkTreePath pointer to be filled in, or %NULL
10296  * @column: (out) (allow-none): A pointer to a #PsppSheetViewColumn pointer to be filled in, or %NULL
10297  * @cell_x: (out) (allow-none): A pointer where the X coordinate relative to the cell can be placed, or %NULL
10298  * @cell_y: (out) (allow-none): A pointer where the Y coordinate relative to the cell can be placed, or %NULL
10299  *
10300  * Finds the path at the point (@x, @y), relative to bin_window coordinates
10301  * (please see pspp_sheet_view_get_bin_window()).
10302  * That is, @x and @y are relative to an events coordinates. @x and @y must
10303  * come from an event on the @tree_view only where <literal>event->window ==
10304  * pspp_sheet_view_get_bin_window (<!-- -->)</literal>. It is primarily for
10305  * things like popup menus. If @path is non-%NULL, then it will be filled
10306  * with the #GtkTreePath at that point.  This path should be freed with
10307  * gtk_tree_path_free().  If @column is non-%NULL, then it will be filled
10308  * with the column at that point.  @cell_x and @cell_y return the coordinates
10309  * relative to the cell background (i.e. the @background_area passed to
10310  * gtk_cell_renderer_render()).  This function is only meaningful if
10311  * @tree_view is realized.  Therefore this function will always return %FALSE
10312  * if @tree_view is not realized or does not have a model.
10313  *
10314  * For converting widget coordinates (eg. the ones you get from
10315  * GtkWidget::query-tooltip), please see
10316  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10317  *
10318  * Return value: %TRUE if a row exists at that coordinate.
10319  **/
10320 gboolean
10321 pspp_sheet_view_get_path_at_pos (PsppSheetView        *tree_view,
10322                                gint                x,
10323                                gint                y,
10324                                GtkTreePath       **path,
10325                                PsppSheetViewColumn **column,
10326                                gint               *cell_x,
10327                                gint               *cell_y)
10328 {
10329   int node;
10330   gint y_offset;
10331
10332   g_return_val_if_fail (tree_view != NULL, FALSE);
10333
10334   if (path)
10335     *path = NULL;
10336   if (column)
10337     *column = NULL;
10338
10339   if (tree_view->priv->bin_window == NULL)
10340     return FALSE;
10341
10342   if (tree_view->priv->row_count == 0)
10343     return FALSE;
10344
10345   if (x > gtk_adjustment_get_upper (tree_view->priv->hadjustment))
10346     return FALSE;
10347
10348   if (x < 0 || y < 0)
10349     return FALSE;
10350
10351   if (column || cell_x)
10352     {
10353       PsppSheetViewColumn *tmp_column;
10354       PsppSheetViewColumn *last_column = NULL;
10355       GList *list;
10356       gint remaining_x = x;
10357       gboolean found = FALSE;
10358       gboolean rtl;
10359
10360       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
10361       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
10362            list;
10363            list = (rtl ? list->prev : list->next))
10364         {
10365           tmp_column = list->data;
10366
10367           if (tmp_column->visible == FALSE)
10368             continue;
10369
10370           last_column = tmp_column;
10371           if (remaining_x <= tmp_column->width)
10372             {
10373               found = TRUE;
10374
10375               if (column)
10376                 *column = tmp_column;
10377
10378               if (cell_x)
10379                 *cell_x = remaining_x;
10380
10381               break;
10382             }
10383           remaining_x -= tmp_column->width;
10384         }
10385
10386       /* If found is FALSE and there is a last_column, then it the remainder
10387        * space is in that area
10388        */
10389       if (!found)
10390         {
10391           if (last_column)
10392             {
10393               if (column)
10394                 *column = last_column;
10395               
10396               if (cell_x)
10397                 *cell_x = last_column->width + remaining_x;
10398             }
10399           else
10400             {
10401               return FALSE;
10402             }
10403         }
10404     }
10405
10406   y_offset = pspp_sheet_view_find_offset (tree_view,
10407                                           TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y),
10408                                           &node);
10409
10410   if (node < 0)
10411     return FALSE;
10412
10413   if (cell_y)
10414     *cell_y = y_offset;
10415
10416   if (path)
10417     *path = _pspp_sheet_view_find_path (tree_view, node);
10418
10419   return TRUE;
10420 }
10421
10422 /* Computes 'cell_area' from 'background_area', which must be the background
10423    area for a cell.  Set 'subtract_focus_rect' to TRUE to compute the cell area
10424    as passed to a GtkCellRenderer's "render" function, or to FALSE to compute
10425    the cell area as passed to _pspp_sheet_view_column_cell_render().
10426
10427    'column' is required to properly adjust 'cell_area->x' and
10428    'cell_area->width'.  It may be set to NULL if these values are not of
10429    interest.  In this case 'cell_area->x' and 'cell_area->width' will be
10430    returned as 0. */
10431 static void
10432 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
10433                                   PsppSheetViewColumn  *column,
10434                                   const GdkRectangle   *background_area,
10435                                   gboolean              subtract_focus_rect,
10436                                   GdkRectangle         *cell_area)
10437 {
10438   gint vertical_separator;
10439   gint horizontal_separator;
10440
10441   *cell_area = *background_area;
10442
10443   gtk_widget_style_get (GTK_WIDGET (tree_view),
10444                         "vertical-separator", &vertical_separator,
10445                         "horizontal-separator", &horizontal_separator,
10446                         NULL);
10447   cell_area->x += horizontal_separator / 2;
10448   cell_area->y += vertical_separator / 2;
10449   cell_area->width -= horizontal_separator;
10450   cell_area->height -= vertical_separator;
10451
10452   if (subtract_focus_rect)
10453     {
10454       int focus_line_width;
10455
10456       gtk_widget_style_get (GTK_WIDGET (tree_view),
10457                             "focus-line-width", &focus_line_width,
10458                             NULL);
10459       cell_area->x += focus_line_width;
10460       cell_area->y += focus_line_width;
10461       cell_area->width -= 2 * focus_line_width;
10462       cell_area->height -= 2 * focus_line_width;
10463     }
10464
10465   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_NONE)
10466     {
10467       gint grid_line_width;
10468       gtk_widget_style_get (GTK_WIDGET (tree_view),
10469                             "grid-line-width", &grid_line_width,
10470                             NULL);
10471
10472       if ((tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
10473            || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10474           && column != NULL)
10475         {
10476           PsppSheetViewColumn *first_column, *last_column;
10477           GList *list;
10478
10479           /* Find the last visible column. */
10480           last_column = NULL;
10481           for (list = g_list_last (tree_view->priv->columns);
10482                list;
10483                list = list->prev)
10484             {
10485               PsppSheetViewColumn *c = list->data;
10486               if (c->visible)
10487                 {
10488                   last_column = c;
10489                   break;
10490                 }
10491             }
10492
10493           /* Find the first visible column. */
10494           first_column = NULL;
10495           for (list = g_list_first (tree_view->priv->columns);
10496                list;
10497                list = list->next)
10498             {
10499               PsppSheetViewColumn *c = list->data;
10500               if (c->visible)
10501                 {
10502                   first_column = c;
10503                   break;
10504                 }
10505             }
10506
10507           if (column == first_column)
10508             {
10509               cell_area->width -= grid_line_width / 2;
10510             }
10511           else if (column == last_column)
10512             {
10513               cell_area->x += grid_line_width / 2;
10514               cell_area->width -= grid_line_width / 2;
10515             }
10516           else
10517             {
10518               cell_area->x += grid_line_width / 2;
10519               cell_area->width -= grid_line_width;
10520             }
10521         }
10522
10523       if (tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
10524           || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10525         {
10526           cell_area->y += grid_line_width / 2;
10527           cell_area->height -= grid_line_width;
10528         }
10529     }
10530
10531   if (column == NULL)
10532     {
10533       cell_area->x = 0;
10534       cell_area->width = 0;
10535     }
10536 }
10537
10538 /**
10539  * pspp_sheet_view_get_cell_area:
10540  * @tree_view: a #PsppSheetView
10541  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10542  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordinates
10543  * @rect: rectangle to fill with cell rect
10544  *
10545  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10546  * row specified by @path and the column specified by @column.  If @path is
10547  * %NULL, or points to a path not currently displayed, the @y and @height fields
10548  * of the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10549  * fields will be filled with 0.  The sum of all cell rects does not cover the
10550  * entire tree; there are extra pixels in between rows, for example. The
10551  * returned rectangle is equivalent to the @cell_area passed to
10552  * gtk_cell_renderer_render().  This function is only valid if @tree_view is
10553  * realized.
10554  **/
10555 void
10556 pspp_sheet_view_get_cell_area (PsppSheetView        *tree_view,
10557                              GtkTreePath        *path,
10558                              PsppSheetViewColumn  *column,
10559                              GdkRectangle       *rect)
10560 {
10561   GdkRectangle background_area;
10562
10563   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10564   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10565   g_return_if_fail (rect != NULL);
10566   g_return_if_fail (!column || column->tree_view == (GtkWidget *) tree_view);
10567   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
10568
10569   pspp_sheet_view_get_background_area (tree_view, path, column,
10570                                        &background_area);
10571   pspp_sheet_view_adjust_cell_area (tree_view, column, &background_area,
10572                                     FALSE, rect);
10573 }
10574
10575 /**
10576  * pspp_sheet_view_get_background_area:
10577  * @tree_view: a #PsppSheetView
10578  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10579  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordiantes
10580  * @rect: rectangle to fill with cell background rect
10581  *
10582  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10583  * row specified by @path and the column specified by @column.  If @path is
10584  * %NULL, or points to a node not found in the tree, the @y and @height fields of
10585  * the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10586  * fields will be filled with 0.  The returned rectangle is equivalent to the
10587  * @background_area passed to gtk_cell_renderer_render().  These background
10588  * areas tile to cover the entire bin window.  Contrast with the @cell_area,
10589  * returned by pspp_sheet_view_get_cell_area(), which returns only the cell
10590  * itself, excluding surrounding borders.
10591  *
10592  **/
10593 void
10594 pspp_sheet_view_get_background_area (PsppSheetView        *tree_view,
10595                                    GtkTreePath        *path,
10596                                    PsppSheetViewColumn  *column,
10597                                    GdkRectangle       *rect)
10598 {
10599   int node = -1;
10600
10601   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10602   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10603   g_return_if_fail (rect != NULL);
10604
10605   rect->x = 0;
10606   rect->y = 0;
10607   rect->width = 0;
10608   rect->height = 0;
10609
10610   if (path)
10611     {
10612       /* Get vertical coords */
10613
10614       _pspp_sheet_view_find_node (tree_view, path, &node);
10615       if (node < 0)
10616         return;
10617
10618       rect->y = BACKGROUND_FIRST_PIXEL (tree_view, node);
10619
10620       rect->height = ROW_HEIGHT (tree_view);
10621     }
10622
10623   if (column)
10624     {
10625       gint x2 = 0;
10626
10627       pspp_sheet_view_get_background_xrange (tree_view, column, &rect->x, &x2);
10628       rect->width = x2 - rect->x;
10629     }
10630 }
10631
10632 /**
10633  * pspp_sheet_view_get_visible_rect:
10634  * @tree_view: a #PsppSheetView
10635  * @visible_rect: rectangle to fill
10636  *
10637  * Fills @visible_rect with the currently-visible region of the
10638  * buffer, in tree coordinates. Convert to bin_window coordinates with
10639  * pspp_sheet_view_convert_tree_to_bin_window_coords().
10640  * Tree coordinates start at 0,0 for row 0 of the tree, and cover the entire
10641  * scrollable area of the tree.
10642  **/
10643 void
10644 pspp_sheet_view_get_visible_rect (PsppSheetView  *tree_view,
10645                                 GdkRectangle *visible_rect)
10646 {
10647   GtkWidget *widget;
10648
10649   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10650
10651   widget = GTK_WIDGET (tree_view);
10652
10653   if (visible_rect)
10654     {
10655       GtkAllocation allocation;
10656       gtk_widget_get_allocation (widget, &allocation);
10657       visible_rect->x = gtk_adjustment_get_value (tree_view->priv->hadjustment);
10658       visible_rect->y = gtk_adjustment_get_value (tree_view->priv->vadjustment);
10659       visible_rect->width  = allocation.width;
10660       visible_rect->height = allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
10661     }
10662 }
10663
10664 /**
10665  * pspp_sheet_view_widget_to_tree_coords:
10666  * @tree_view: a #PsppSheetView
10667  * @wx: X coordinate relative to bin_window
10668  * @wy: Y coordinate relative to bin_window
10669  * @tx: return location for tree X coordinate
10670  * @ty: return location for tree Y coordinate
10671  *
10672  * Converts bin_window coordinates to coordinates for the
10673  * tree (the full scrollable area of the tree).
10674  *
10675  * Deprecated: 2.12: Due to historial reasons the name of this function is
10676  * incorrect.  For converting coordinates relative to the widget to
10677  * bin_window coordinates, please see
10678  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10679  *
10680  **/
10681 void
10682 pspp_sheet_view_widget_to_tree_coords (PsppSheetView *tree_view,
10683                                       gint         wx,
10684                                       gint         wy,
10685                                       gint        *tx,
10686                                       gint        *ty)
10687 {
10688   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10689
10690   if (tx)
10691     *tx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10692   if (ty)
10693     *ty = wy + tree_view->priv->dy;
10694 }
10695
10696 /**
10697  * pspp_sheet_view_tree_to_widget_coords:
10698  * @tree_view: a #PsppSheetView
10699  * @tx: tree X coordinate
10700  * @ty: tree Y coordinate
10701  * @wx: return location for X coordinate relative to bin_window
10702  * @wy: return location for Y coordinate relative to bin_window
10703  *
10704  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10705  * to bin_window coordinates.
10706  *
10707  * Deprecated: 2.12: Due to historial reasons the name of this function is
10708  * incorrect.  For converting bin_window coordinates to coordinates relative
10709  * to bin_window, please see
10710  * pspp_sheet_view_convert_bin_window_to_widget_coords().
10711  *
10712  **/
10713 void
10714 pspp_sheet_view_tree_to_widget_coords (PsppSheetView *tree_view,
10715                                      gint         tx,
10716                                      gint         ty,
10717                                      gint        *wx,
10718                                      gint        *wy)
10719 {
10720   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10721
10722   if (wx)
10723     *wx = tx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10724   if (wy)
10725     *wy = ty - tree_view->priv->dy;
10726 }
10727
10728
10729 /**
10730  * pspp_sheet_view_convert_widget_to_tree_coords:
10731  * @tree_view: a #PsppSheetView
10732  * @wx: X coordinate relative to the widget
10733  * @wy: Y coordinate relative to the widget
10734  * @tx: return location for tree X coordinate
10735  * @ty: return location for tree Y coordinate
10736  *
10737  * Converts widget coordinates to coordinates for the
10738  * tree (the full scrollable area of the tree).
10739  *
10740  * Since: 2.12
10741  **/
10742 void
10743 pspp_sheet_view_convert_widget_to_tree_coords (PsppSheetView *tree_view,
10744                                              gint         wx,
10745                                              gint         wy,
10746                                              gint        *tx,
10747                                              gint        *ty)
10748 {
10749   gint x, y;
10750
10751   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10752
10753   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
10754                                                      wx, wy,
10755                                                      &x, &y);
10756   pspp_sheet_view_convert_bin_window_to_tree_coords (tree_view,
10757                                                    x, y,
10758                                                    tx, ty);
10759 }
10760
10761 /**
10762  * pspp_sheet_view_convert_tree_to_widget_coords:
10763  * @tree_view: a #PsppSheetView
10764  * @tx: X coordinate relative to the tree
10765  * @ty: Y coordinate relative to the tree
10766  * @wx: return location for widget X coordinate
10767  * @wy: return location for widget Y coordinate
10768  *
10769  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10770  * to widget coordinates.
10771  *
10772  * Since: 2.12
10773  **/
10774 void
10775 pspp_sheet_view_convert_tree_to_widget_coords (PsppSheetView *tree_view,
10776                                              gint         tx,
10777                                              gint         ty,
10778                                              gint        *wx,
10779                                              gint        *wy)
10780 {
10781   gint x, y;
10782
10783   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10784
10785   pspp_sheet_view_convert_tree_to_bin_window_coords (tree_view,
10786                                                    tx, ty,
10787                                                    &x, &y);
10788   pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
10789                                                      x, y,
10790                                                      wx, wy);
10791 }
10792
10793 /**
10794  * pspp_sheet_view_convert_widget_to_bin_window_coords:
10795  * @tree_view: a #PsppSheetView
10796  * @wx: X coordinate relative to the widget
10797  * @wy: Y coordinate relative to the widget
10798  * @bx: return location for bin_window X coordinate
10799  * @by: return location for bin_window Y coordinate
10800  *
10801  * Converts widget coordinates to coordinates for the bin_window
10802  * (see pspp_sheet_view_get_bin_window()).
10803  *
10804  * Since: 2.12
10805  **/
10806 void
10807 pspp_sheet_view_convert_widget_to_bin_window_coords (PsppSheetView *tree_view,
10808                                                    gint         wx,
10809                                                    gint         wy,
10810                                                    gint        *bx,
10811                                                    gint        *by)
10812 {
10813   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10814
10815   if (bx)
10816     *bx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10817   if (by)
10818     *by = wy - TREE_VIEW_HEADER_HEIGHT (tree_view);
10819 }
10820
10821 /**
10822  * pspp_sheet_view_convert_bin_window_to_widget_coords:
10823  * @tree_view: a #PsppSheetView
10824  * @bx: bin_window X coordinate
10825  * @by: bin_window Y coordinate
10826  * @wx: return location for widget X coordinate
10827  * @wy: return location for widget Y coordinate
10828  *
10829  * Converts bin_window coordinates (see pspp_sheet_view_get_bin_window())
10830  * to widget relative coordinates.
10831  *
10832  * Since: 2.12
10833  **/
10834 void
10835 pspp_sheet_view_convert_bin_window_to_widget_coords (PsppSheetView *tree_view,
10836                                                    gint         bx,
10837                                                    gint         by,
10838                                                    gint        *wx,
10839                                                    gint        *wy)
10840 {
10841   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10842
10843   if (wx)
10844     *wx = bx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10845   if (wy)
10846     *wy = by + TREE_VIEW_HEADER_HEIGHT (tree_view);
10847 }
10848
10849 /**
10850  * pspp_sheet_view_convert_tree_to_bin_window_coords:
10851  * @tree_view: a #PsppSheetView
10852  * @tx: tree X coordinate
10853  * @ty: tree Y coordinate
10854  * @bx: return location for X coordinate relative to bin_window
10855  * @by: return location for Y coordinate relative to bin_window
10856  *
10857  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10858  * to bin_window coordinates.
10859  *
10860  * Since: 2.12
10861  **/
10862 void
10863 pspp_sheet_view_convert_tree_to_bin_window_coords (PsppSheetView *tree_view,
10864                                                  gint         tx,
10865                                                  gint         ty,
10866                                                  gint        *bx,
10867                                                  gint        *by)
10868 {
10869   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10870
10871   if (bx)
10872     *bx = tx;
10873   if (by)
10874     *by = ty - tree_view->priv->dy;
10875 }
10876
10877 /**
10878  * pspp_sheet_view_convert_bin_window_to_tree_coords:
10879  * @tree_view: a #PsppSheetView
10880  * @bx: X coordinate relative to bin_window
10881  * @by: Y coordinate relative to bin_window
10882  * @tx: return location for tree X coordinate
10883  * @ty: return location for tree Y coordinate
10884  *
10885  * Converts bin_window coordinates to coordinates for the
10886  * tree (the full scrollable area of the tree).
10887  *
10888  * Since: 2.12
10889  **/
10890 void
10891 pspp_sheet_view_convert_bin_window_to_tree_coords (PsppSheetView *tree_view,
10892                                                  gint         bx,
10893                                                  gint         by,
10894                                                  gint        *tx,
10895                                                  gint        *ty)
10896 {
10897   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10898
10899   if (tx)
10900     *tx = bx;
10901   if (ty)
10902     *ty = by + tree_view->priv->dy;
10903 }
10904
10905
10906
10907 /**
10908  * pspp_sheet_view_get_visible_range:
10909  * @tree_view: A #PsppSheetView
10910  * @start_path: (allow-none): Return location for start of region, or %NULL.
10911  * @end_path: (allow-none): Return location for end of region, or %NULL.
10912  *
10913  * Sets @start_path and @end_path to be the first and last visible path.
10914  * Note that there may be invisible paths in between.
10915  *
10916  * The paths should be freed with gtk_tree_path_free() after use.
10917  *
10918  * Returns: %TRUE, if valid paths were placed in @start_path and @end_path.
10919  *
10920  * Since: 2.8
10921  **/
10922 gboolean
10923 pspp_sheet_view_get_visible_range (PsppSheetView  *tree_view,
10924                                  GtkTreePath **start_path,
10925                                  GtkTreePath **end_path)
10926 {
10927   int node;
10928   gboolean retval;
10929   
10930   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10931
10932   if (!tree_view->priv->row_count)
10933     return FALSE;
10934
10935   retval = TRUE;
10936
10937   if (start_path)
10938     {
10939       pspp_sheet_view_find_offset (tree_view,
10940                                    TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, 0),
10941                                    &node);
10942       if (node >= 0)
10943         *start_path = _pspp_sheet_view_find_path (tree_view, node);
10944       else
10945         retval = FALSE;
10946     }
10947
10948   if (end_path)
10949     {
10950       gint y;
10951
10952       if (tree_view->priv->height < gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
10953         y = tree_view->priv->height - 1;
10954       else
10955         y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, gtk_adjustment_get_page_size (tree_view->priv->vadjustment)) - 1;
10956
10957       pspp_sheet_view_find_offset (tree_view, y, &node);
10958       if (node >= 0)
10959         *end_path = _pspp_sheet_view_find_path (tree_view, node);
10960       else
10961         retval = FALSE;
10962     }
10963
10964   return retval;
10965 }
10966
10967 static void
10968 unset_reorderable (PsppSheetView *tree_view)
10969 {
10970   if (tree_view->priv->reorderable)
10971     {
10972       tree_view->priv->reorderable = FALSE;
10973       g_object_notify (G_OBJECT (tree_view), "reorderable");
10974     }
10975 }
10976
10977 /**
10978  * pspp_sheet_view_enable_model_drag_source:
10979  * @tree_view: a #PsppSheetView
10980  * @start_button_mask: Mask of allowed buttons to start drag
10981  * @targets: the table of targets that the drag will support
10982  * @n_targets: the number of items in @targets
10983  * @actions: the bitmask of possible actions for a drag from this
10984  *    widget
10985  *
10986  * Turns @tree_view into a drag source for automatic DND. Calling this
10987  * method sets #PsppSheetView:reorderable to %FALSE.
10988  **/
10989 void
10990 pspp_sheet_view_enable_model_drag_source (PsppSheetView              *tree_view,
10991                                         GdkModifierType           start_button_mask,
10992                                         const GtkTargetEntry     *targets,
10993                                         gint                      n_targets,
10994                                         GdkDragAction             actions)
10995 {
10996   TreeViewDragInfo *di;
10997
10998   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10999
11000   gtk_drag_source_set (GTK_WIDGET (tree_view),
11001                        0,
11002                        targets,
11003                        n_targets,
11004                        actions);
11005
11006   di = ensure_info (tree_view);
11007
11008   di->start_button_mask = start_button_mask;
11009   di->source_actions = actions;
11010   di->source_set = TRUE;
11011
11012   unset_reorderable (tree_view);
11013 }
11014
11015 /**
11016  * pspp_sheet_view_enable_model_drag_dest:
11017  * @tree_view: a #PsppSheetView
11018  * @targets: the table of targets that the drag will support
11019  * @n_targets: the number of items in @targets
11020  * @actions: the bitmask of possible actions for a drag from this
11021  *    widget
11022  * 
11023  * Turns @tree_view into a drop destination for automatic DND. Calling
11024  * this method sets #PsppSheetView:reorderable to %FALSE.
11025  **/
11026 void
11027 pspp_sheet_view_enable_model_drag_dest (PsppSheetView              *tree_view,
11028                                       const GtkTargetEntry     *targets,
11029                                       gint                      n_targets,
11030                                       GdkDragAction             actions)
11031 {
11032   TreeViewDragInfo *di;
11033
11034   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11035
11036   gtk_drag_dest_set (GTK_WIDGET (tree_view),
11037                      0,
11038                      targets,
11039                      n_targets,
11040                      actions);
11041
11042   di = ensure_info (tree_view);
11043   di->dest_set = TRUE;
11044
11045   unset_reorderable (tree_view);
11046 }
11047
11048 /**
11049  * pspp_sheet_view_unset_rows_drag_source:
11050  * @tree_view: a #PsppSheetView
11051  *
11052  * Undoes the effect of
11053  * pspp_sheet_view_enable_model_drag_source(). Calling this method sets
11054  * #PsppSheetView:reorderable to %FALSE.
11055  **/
11056 void
11057 pspp_sheet_view_unset_rows_drag_source (PsppSheetView *tree_view)
11058 {
11059   TreeViewDragInfo *di;
11060
11061   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11062
11063   di = get_info (tree_view);
11064
11065   if (di)
11066     {
11067       if (di->source_set)
11068         {
11069           gtk_drag_source_unset (GTK_WIDGET (tree_view));
11070           di->source_set = FALSE;
11071         }
11072
11073       if (!di->dest_set && !di->source_set)
11074         remove_info (tree_view);
11075     }
11076   
11077   unset_reorderable (tree_view);
11078 }
11079
11080 /**
11081  * pspp_sheet_view_unset_rows_drag_dest:
11082  * @tree_view: a #PsppSheetView
11083  *
11084  * Undoes the effect of
11085  * pspp_sheet_view_enable_model_drag_dest(). Calling this method sets
11086  * #PsppSheetView:reorderable to %FALSE.
11087  **/
11088 void
11089 pspp_sheet_view_unset_rows_drag_dest (PsppSheetView *tree_view)
11090 {
11091   TreeViewDragInfo *di;
11092
11093   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11094
11095   di = get_info (tree_view);
11096
11097   if (di)
11098     {
11099       if (di->dest_set)
11100         {
11101           gtk_drag_dest_unset (GTK_WIDGET (tree_view));
11102           di->dest_set = FALSE;
11103         }
11104
11105       if (!di->dest_set && !di->source_set)
11106         remove_info (tree_view);
11107     }
11108
11109   unset_reorderable (tree_view);
11110 }
11111
11112 /**
11113  * pspp_sheet_view_set_drag_dest_row:
11114  * @tree_view: a #PsppSheetView
11115  * @path: (allow-none): The path of the row to highlight, or %NULL.
11116  * @pos: Specifies whether to drop before, after or into the row
11117  * 
11118  * Sets the row that is highlighted for feedback.
11119  **/
11120 void
11121 pspp_sheet_view_set_drag_dest_row (PsppSheetView            *tree_view,
11122                                  GtkTreePath            *path,
11123                                  PsppSheetViewDropPosition pos)
11124 {
11125   GtkTreePath *current_dest;
11126
11127   /* Note; this function is exported to allow a custom DND
11128    * implementation, so it can't touch TreeViewDragInfo
11129    */
11130
11131   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11132
11133   current_dest = NULL;
11134
11135   if (tree_view->priv->drag_dest_row)
11136     {
11137       current_dest = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11138       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
11139     }
11140
11141   /* special case a drop on an empty model */
11142   tree_view->priv->empty_view_drop = 0;
11143
11144   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE && path
11145       && gtk_tree_path_get_depth (path) == 1
11146       && gtk_tree_path_get_indices (path)[0] == 0)
11147     {
11148       gint n_children;
11149
11150       n_children = gtk_tree_model_iter_n_children (tree_view->priv->model,
11151                                                    NULL);
11152
11153       if (!n_children)
11154         tree_view->priv->empty_view_drop = 1;
11155     }
11156
11157   tree_view->priv->drag_dest_pos = pos;
11158
11159   if (path)
11160     {
11161       tree_view->priv->drag_dest_row =
11162         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
11163       pspp_sheet_view_queue_draw_path (tree_view, path, NULL);
11164     }
11165   else
11166     tree_view->priv->drag_dest_row = NULL;
11167
11168   if (current_dest)
11169     {
11170       int node, new_node;
11171
11172       _pspp_sheet_view_find_node (tree_view, current_dest, &node);
11173       _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
11174
11175       if (node >= 0)
11176         {
11177           new_node = pspp_sheet_view_node_next (tree_view, node);
11178           if (new_node >= 0)
11179             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11180
11181           new_node = pspp_sheet_view_node_prev (tree_view, node);
11182           if (new_node >= 0)
11183             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11184         }
11185       gtk_tree_path_free (current_dest);
11186     }
11187 }
11188
11189 /**
11190  * pspp_sheet_view_get_drag_dest_row:
11191  * @tree_view: a #PsppSheetView
11192  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11193  * @pos: (allow-none): Return location for the drop position, or %NULL
11194  * 
11195  * Gets information about the row that is highlighted for feedback.
11196  **/
11197 void
11198 pspp_sheet_view_get_drag_dest_row (PsppSheetView              *tree_view,
11199                                  GtkTreePath             **path,
11200                                  PsppSheetViewDropPosition  *pos)
11201 {
11202   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11203
11204   if (path)
11205     {
11206       if (tree_view->priv->drag_dest_row)
11207         *path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11208       else
11209         {
11210           if (tree_view->priv->empty_view_drop)
11211             *path = gtk_tree_path_new_from_indices (0, -1);
11212           else
11213             *path = NULL;
11214         }
11215     }
11216
11217   if (pos)
11218     *pos = tree_view->priv->drag_dest_pos;
11219 }
11220
11221 /**
11222  * pspp_sheet_view_get_dest_row_at_pos:
11223  * @tree_view: a #PsppSheetView
11224  * @drag_x: the position to determine the destination row for
11225  * @drag_y: the position to determine the destination row for
11226  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11227  * @pos: (allow-none): Return location for the drop position, or %NULL
11228  * 
11229  * Determines the destination row for a given position.  @drag_x and
11230  * @drag_y are expected to be in widget coordinates.  This function is only
11231  * meaningful if @tree_view is realized.  Therefore this function will always
11232  * return %FALSE if @tree_view is not realized or does not have a model.
11233  * 
11234  * Return value: whether there is a row at the given position, %TRUE if this
11235  * is indeed the case.
11236  **/
11237 gboolean
11238 pspp_sheet_view_get_dest_row_at_pos (PsppSheetView             *tree_view,
11239                                    gint                     drag_x,
11240                                    gint                     drag_y,
11241                                    GtkTreePath            **path,
11242                                    PsppSheetViewDropPosition *pos)
11243 {
11244   gint cell_y;
11245   gint bin_x, bin_y;
11246   gdouble offset_into_row;
11247   gdouble third;
11248   GdkRectangle cell;
11249   PsppSheetViewColumn *column = NULL;
11250   GtkTreePath *tmp_path = NULL;
11251
11252   /* Note; this function is exported to allow a custom DND
11253    * implementation, so it can't touch TreeViewDragInfo
11254    */
11255
11256   g_return_val_if_fail (tree_view != NULL, FALSE);
11257   g_return_val_if_fail (drag_x >= 0, FALSE);
11258   g_return_val_if_fail (drag_y >= 0, FALSE);
11259
11260   if (path)
11261     *path = NULL;
11262
11263   if (tree_view->priv->bin_window == NULL)
11264     return FALSE;
11265
11266   if (tree_view->priv->row_count == 0)
11267     return FALSE;
11268
11269   /* If in the top third of a row, we drop before that row; if
11270    * in the bottom third, drop after that row; if in the middle,
11271    * and the row has children, drop into the row.
11272    */
11273   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, drag_x, drag_y,
11274                                                      &bin_x, &bin_y);
11275
11276   if (!pspp_sheet_view_get_path_at_pos (tree_view,
11277                                       bin_x,
11278                                       bin_y,
11279                                       &tmp_path,
11280                                       &column,
11281                                       NULL,
11282                                       &cell_y))
11283     return FALSE;
11284
11285   pspp_sheet_view_get_background_area (tree_view, tmp_path, column,
11286                                      &cell);
11287
11288   offset_into_row = cell_y;
11289
11290   if (path)
11291     *path = tmp_path;
11292   else
11293     gtk_tree_path_free (tmp_path);
11294
11295   tmp_path = NULL;
11296
11297   third = cell.height / 3.0;
11298
11299   if (pos)
11300     {
11301       if (offset_into_row < third)
11302         {
11303           *pos = PSPP_SHEET_VIEW_DROP_BEFORE;
11304         }
11305       else if (offset_into_row < (cell.height / 2.0))
11306         {
11307           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE;
11308         }
11309       else if (offset_into_row < third * 2.0)
11310         {
11311           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER;
11312         }
11313       else
11314         {
11315           *pos = PSPP_SHEET_VIEW_DROP_AFTER;
11316         }
11317     }
11318
11319   return TRUE;
11320 }
11321
11322
11323 #if GTK3_TRANSITION
11324 /* KEEP IN SYNC WITH PSPP_SHEET_VIEW_BIN_EXPOSE */
11325 /**
11326  * pspp_sheet_view_create_row_drag_icon:
11327  * @tree_view: a #PsppSheetView
11328  * @path: a #GtkTreePath in @tree_view
11329  *
11330  * Creates a #GdkPixmap representation of the row at @path.  
11331  * This image is used for a drag icon.
11332  *
11333  * Return value: a newly-allocated pixmap of the drag icon.
11334  **/
11335 GdkPixmap *
11336 pspp_sheet_view_create_row_drag_icon (PsppSheetView  *tree_view,
11337                                     GtkTreePath  *path)
11338 {
11339   GtkTreeIter   iter;
11340   int node;
11341   gint cell_offset;
11342   GList *list;
11343   GdkRectangle background_area;
11344   GdkRectangle expose_area;
11345   GtkWidget *widget;
11346   /* start drawing inside the black outline */
11347   gint x = 1, y = 1;
11348   GdkDrawable *drawable;
11349   gint bin_window_width;
11350   gboolean rtl;
11351
11352   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11353   g_return_val_if_fail (path != NULL, NULL);
11354
11355   widget = GTK_WIDGET (tree_view);
11356
11357   if (!gtk_widget_get_realized (widget))
11358     return NULL;
11359
11360   _pspp_sheet_view_find_node (tree_view,
11361                             path,
11362                             &node);
11363
11364   if (node < 0)
11365     return NULL;
11366
11367   if (!gtk_tree_model_get_iter (tree_view->priv->model,
11368                                 &iter,
11369                                 path))
11370     return NULL;
11371   
11372   cell_offset = x;
11373
11374   background_area.y = y;
11375   background_area.height = ROW_HEIGHT (tree_view);
11376
11377   bin_window_width = gdk_window_get_width (tree_view->priv->bin_window);
11378
11379   drawable = gdk_pixmap_new (tree_view->priv->bin_window,
11380                              bin_window_width + 2,
11381                              background_area.height + 2,
11382                              -1);
11383
11384   expose_area.x = 0;
11385   expose_area.y = 0;
11386   expose_area.width = bin_window_width + 2;
11387   expose_area.height = background_area.height + 2;
11388
11389 #if GTK3_TRANSITION
11390   gdk_draw_rectangle (drawable,
11391                       widget->style->base_gc [gtk_widget_get_state (widget)],
11392                       TRUE,
11393                       0, 0,
11394                       bin_window_width + 2,
11395                       background_area.height + 2);
11396 #endif
11397
11398   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
11399
11400   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
11401       list;
11402       list = (rtl ? list->prev : list->next))
11403     {
11404       PsppSheetViewColumn *column = list->data;
11405       GdkRectangle cell_area;
11406       gint vertical_separator;
11407
11408       if (!column->visible)
11409         continue;
11410
11411       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, &iter);
11412
11413       background_area.x = cell_offset;
11414       background_area.width = column->width;
11415
11416       gtk_widget_style_get (widget,
11417                             "vertical-separator", &vertical_separator,
11418                             NULL);
11419
11420       cell_area = background_area;
11421
11422       cell_area.y += vertical_separator / 2;
11423       cell_area.height -= vertical_separator;
11424
11425       if (pspp_sheet_view_column_cell_is_visible (column))
11426         _pspp_sheet_view_column_cell_render (column,
11427                                              drawable,
11428                                              &background_area,
11429                                              &cell_area,
11430                                              &expose_area,
11431                                              0);
11432       cell_offset += column->width;
11433     }
11434
11435 #if GTK3_TRANSITION
11436   gdk_draw_rectangle (drawable,
11437                       widget->style->black_gc,
11438                       FALSE,
11439                       0, 0,
11440                       bin_window_width + 1,
11441                       background_area.height + 1);
11442 #endif
11443
11444   return drawable;
11445 }
11446 #endif
11447
11448 /**
11449  * pspp_sheet_view_set_destroy_count_func:
11450  * @tree_view: A #PsppSheetView
11451  * @func: (allow-none): Function to be called when a view row is destroyed, or %NULL
11452  * @data: (allow-none): User data to be passed to @func, or %NULL
11453  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11454  *
11455  * This function should almost never be used.  It is meant for private use by
11456  * ATK for determining the number of visible children that are removed when a row is deleted.
11457  **/
11458 void
11459 pspp_sheet_view_set_destroy_count_func (PsppSheetView             *tree_view,
11460                                       PsppSheetDestroyCountFunc  func,
11461                                       gpointer                 data,
11462                                       GDestroyNotify           destroy)
11463 {
11464   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11465
11466   if (tree_view->priv->destroy_count_destroy)
11467     tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
11468
11469   tree_view->priv->destroy_count_func = func;
11470   tree_view->priv->destroy_count_data = data;
11471   tree_view->priv->destroy_count_destroy = destroy;
11472 }
11473
11474
11475 /*
11476  * Interactive search
11477  */
11478
11479 /**
11480  * pspp_sheet_view_set_enable_search:
11481  * @tree_view: A #PsppSheetView
11482  * @enable_search: %TRUE, if the user can search interactively
11483  *
11484  * If @enable_search is set, then the user can type in text to search through
11485  * the tree interactively (this is sometimes called "typeahead find").
11486  * 
11487  * Note that even if this is %FALSE, the user can still initiate a search 
11488  * using the "start-interactive-search" key binding.
11489  */
11490 void
11491 pspp_sheet_view_set_enable_search (PsppSheetView *tree_view,
11492                                  gboolean     enable_search)
11493 {
11494   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11495
11496   enable_search = !!enable_search;
11497   
11498   if (tree_view->priv->enable_search != enable_search)
11499     {
11500        tree_view->priv->enable_search = enable_search;
11501        g_object_notify (G_OBJECT (tree_view), "enable-search");
11502     }
11503 }
11504
11505 /**
11506  * pspp_sheet_view_get_enable_search:
11507  * @tree_view: A #PsppSheetView
11508  *
11509  * Returns whether or not the tree allows to start interactive searching 
11510  * by typing in text.
11511  *
11512  * Return value: whether or not to let the user search interactively
11513  */
11514 gboolean
11515 pspp_sheet_view_get_enable_search (PsppSheetView *tree_view)
11516 {
11517   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11518
11519   return tree_view->priv->enable_search;
11520 }
11521
11522
11523 /**
11524  * pspp_sheet_view_get_search_column:
11525  * @tree_view: A #PsppSheetView
11526  *
11527  * Gets the column searched on by the interactive search code.
11528  *
11529  * Return value: the column the interactive search code searches in.
11530  */
11531 gint
11532 pspp_sheet_view_get_search_column (PsppSheetView *tree_view)
11533 {
11534   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11535
11536   return (tree_view->priv->search_column);
11537 }
11538
11539 /**
11540  * pspp_sheet_view_set_search_column:
11541  * @tree_view: A #PsppSheetView
11542  * @column: the column of the model to search in, or -1 to disable searching
11543  *
11544  * Sets @column as the column where the interactive search code should
11545  * search in for the current model. 
11546  * 
11547  * If the search column is set, users can use the "start-interactive-search"
11548  * key binding to bring up search popup. The enable-search property controls
11549  * whether simply typing text will also start an interactive search.
11550  *
11551  * Note that @column refers to a column of the current model. The search 
11552  * column is reset to -1 when the model is changed.
11553  */
11554 void
11555 pspp_sheet_view_set_search_column (PsppSheetView *tree_view,
11556                                  gint         column)
11557 {
11558   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11559   g_return_if_fail (column >= -1);
11560
11561   if (tree_view->priv->search_column == column)
11562     return;
11563
11564   tree_view->priv->search_column = column;
11565   g_object_notify (G_OBJECT (tree_view), "search-column");
11566 }
11567
11568 /**
11569  * pspp_sheet_view_get_search_equal_func:
11570  * @tree_view: A #PsppSheetView
11571  *
11572  * Returns the compare function currently in use.
11573  *
11574  * Return value: the currently used compare function for the search code.
11575  */
11576
11577 PsppSheetViewSearchEqualFunc
11578 pspp_sheet_view_get_search_equal_func (PsppSheetView *tree_view)
11579 {
11580   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11581
11582   return tree_view->priv->search_equal_func;
11583 }
11584
11585 /**
11586  * pspp_sheet_view_set_search_equal_func:
11587  * @tree_view: A #PsppSheetView
11588  * @search_equal_func: the compare function to use during the search
11589  * @search_user_data: (allow-none): user data to pass to @search_equal_func, or %NULL
11590  * @search_destroy: (allow-none): Destroy notifier for @search_user_data, or %NULL
11591  *
11592  * Sets the compare function for the interactive search capabilities; note
11593  * that somewhat like strcmp() returning 0 for equality
11594  * #PsppSheetViewSearchEqualFunc returns %FALSE on matches.
11595  **/
11596 void
11597 pspp_sheet_view_set_search_equal_func (PsppSheetView                *tree_view,
11598                                      PsppSheetViewSearchEqualFunc  search_equal_func,
11599                                      gpointer                    search_user_data,
11600                                      GDestroyNotify              search_destroy)
11601 {
11602   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11603   g_return_if_fail (search_equal_func != NULL);
11604
11605   if (tree_view->priv->search_destroy)
11606     tree_view->priv->search_destroy (tree_view->priv->search_user_data);
11607
11608   tree_view->priv->search_equal_func = search_equal_func;
11609   tree_view->priv->search_user_data = search_user_data;
11610   tree_view->priv->search_destroy = search_destroy;
11611   if (tree_view->priv->search_equal_func == NULL)
11612     tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
11613 }
11614
11615 /**
11616  * pspp_sheet_view_get_search_entry:
11617  * @tree_view: A #PsppSheetView
11618  *
11619  * Returns the #GtkEntry which is currently in use as interactive search
11620  * entry for @tree_view.  In case the built-in entry is being used, %NULL
11621  * will be returned.
11622  *
11623  * Return value: the entry currently in use as search entry.
11624  *
11625  * Since: 2.10
11626  */
11627 GtkEntry *
11628 pspp_sheet_view_get_search_entry (PsppSheetView *tree_view)
11629 {
11630   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11631
11632   if (tree_view->priv->search_custom_entry_set)
11633     return GTK_ENTRY (tree_view->priv->search_entry);
11634
11635   return NULL;
11636 }
11637
11638 /**
11639  * pspp_sheet_view_set_search_entry:
11640  * @tree_view: A #PsppSheetView
11641  * @entry: (allow-none): the entry the interactive search code of @tree_view should use or %NULL
11642  *
11643  * Sets the entry which the interactive search code will use for this
11644  * @tree_view.  This is useful when you want to provide a search entry
11645  * in our interface at all time at a fixed position.  Passing %NULL for
11646  * @entry will make the interactive search code use the built-in popup
11647  * entry again.
11648  *
11649  * Since: 2.10
11650  */
11651 void
11652 pspp_sheet_view_set_search_entry (PsppSheetView *tree_view,
11653                                 GtkEntry    *entry)
11654 {
11655   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11656   g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
11657
11658   if (tree_view->priv->search_custom_entry_set)
11659     {
11660       if (tree_view->priv->search_entry_changed_id)
11661         {
11662           g_signal_handler_disconnect (tree_view->priv->search_entry,
11663                                        tree_view->priv->search_entry_changed_id);
11664           tree_view->priv->search_entry_changed_id = 0;
11665         }
11666       g_signal_handlers_disconnect_by_func (tree_view->priv->search_entry,
11667                                             G_CALLBACK (pspp_sheet_view_search_key_press_event),
11668                                             tree_view);
11669
11670       g_object_unref (tree_view->priv->search_entry);
11671     }
11672   else if (tree_view->priv->search_window)
11673     {
11674       gtk_widget_destroy (tree_view->priv->search_window);
11675
11676       tree_view->priv->search_window = NULL;
11677     }
11678
11679   if (entry)
11680     {
11681       tree_view->priv->search_entry = g_object_ref (entry);
11682       tree_view->priv->search_custom_entry_set = TRUE;
11683
11684       if (tree_view->priv->search_entry_changed_id == 0)
11685         {
11686           tree_view->priv->search_entry_changed_id =
11687             g_signal_connect (tree_view->priv->search_entry, "changed",
11688                               G_CALLBACK (pspp_sheet_view_search_init),
11689                               tree_view);
11690         }
11691       
11692         g_signal_connect (tree_view->priv->search_entry, "key-press-event",
11693                           G_CALLBACK (pspp_sheet_view_search_key_press_event),
11694                           tree_view);
11695
11696         pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
11697     }
11698   else
11699     {
11700       tree_view->priv->search_entry = NULL;
11701       tree_view->priv->search_custom_entry_set = FALSE;
11702     }
11703 }
11704
11705 /**
11706  * pspp_sheet_view_set_search_position_func:
11707  * @tree_view: A #PsppSheetView
11708  * @func: (allow-none): the function to use to position the search dialog, or %NULL
11709  *    to use the default search position function
11710  * @data: (allow-none): user data to pass to @func, or %NULL
11711  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11712  *
11713  * Sets the function to use when positioning the search dialog.
11714  *
11715  * Since: 2.10
11716  **/
11717 void
11718 pspp_sheet_view_set_search_position_func (PsppSheetView                   *tree_view,
11719                                         PsppSheetViewSearchPositionFunc  func,
11720                                         gpointer                       user_data,
11721                                         GDestroyNotify                 destroy)
11722 {
11723   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11724
11725   if (tree_view->priv->search_position_destroy)
11726     tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
11727
11728   tree_view->priv->search_position_func = func;
11729   tree_view->priv->search_position_user_data = user_data;
11730   tree_view->priv->search_position_destroy = destroy;
11731   if (tree_view->priv->search_position_func == NULL)
11732     tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
11733 }
11734
11735 /**
11736  * pspp_sheet_view_get_search_position_func:
11737  * @tree_view: A #PsppSheetView
11738  *
11739  * Returns the positioning function currently in use.
11740  *
11741  * Return value: the currently used function for positioning the search dialog.
11742  *
11743  * Since: 2.10
11744  */
11745 PsppSheetViewSearchPositionFunc
11746 pspp_sheet_view_get_search_position_func (PsppSheetView *tree_view)
11747 {
11748   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11749
11750   return tree_view->priv->search_position_func;
11751 }
11752
11753
11754 static void
11755 pspp_sheet_view_search_dialog_hide (GtkWidget   *search_dialog,
11756                                   PsppSheetView *tree_view)
11757 {
11758   if (tree_view->priv->disable_popdown)
11759     return;
11760
11761   if (tree_view->priv->search_entry_changed_id)
11762     {
11763       g_signal_handler_disconnect (tree_view->priv->search_entry,
11764                                    tree_view->priv->search_entry_changed_id);
11765       tree_view->priv->search_entry_changed_id = 0;
11766     }
11767   if (tree_view->priv->typeselect_flush_timeout)
11768     {
11769       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11770       tree_view->priv->typeselect_flush_timeout = 0;
11771     }
11772         
11773   if (gtk_widget_get_visible (search_dialog))
11774     {
11775       /* send focus-in event */
11776       send_focus_change (GTK_WIDGET (tree_view->priv->search_entry), FALSE);
11777       gtk_widget_hide (search_dialog);
11778       gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
11779       send_focus_change (GTK_WIDGET (tree_view), TRUE);
11780     }
11781 }
11782
11783 static void
11784 pspp_sheet_view_search_position_func (PsppSheetView *tree_view,
11785                                     GtkWidget   *search_dialog,
11786                                     gpointer     user_data)
11787 {
11788   gint x, y;
11789   gint tree_x, tree_y;
11790   gint tree_width, tree_height;
11791   GdkWindow *tree_window = gtk_widget_get_window (GTK_WIDGET (tree_view));
11792   GdkScreen *screen = gdk_window_get_screen (tree_window);
11793   GtkRequisition requisition;
11794   gint monitor_num;
11795   GdkRectangle monitor;
11796
11797   monitor_num = gdk_screen_get_monitor_at_window (screen, tree_window);
11798   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
11799
11800   gtk_widget_realize (search_dialog);
11801
11802   gdk_window_get_origin (tree_window, &tree_x, &tree_y);
11803   tree_width = gdk_window_get_width (tree_window);
11804   tree_height = gdk_window_get_height (tree_window);
11805
11806   gtk_widget_size_request (search_dialog, &requisition);
11807
11808   if (tree_x + tree_width > gdk_screen_get_width (screen))
11809     x = gdk_screen_get_width (screen) - requisition.width;
11810   else if (tree_x + tree_width - requisition.width < 0)
11811     x = 0;
11812   else
11813     x = tree_x + tree_width - requisition.width;
11814
11815   if (tree_y + tree_height + requisition.height > gdk_screen_get_height (screen))
11816     y = gdk_screen_get_height (screen) - requisition.height;
11817   else if (tree_y + tree_height < 0) /* isn't really possible ... */
11818     y = 0;
11819   else
11820     y = tree_y + tree_height;
11821
11822   gtk_window_move (GTK_WINDOW (search_dialog), x, y);
11823 }
11824
11825 static void
11826 pspp_sheet_view_search_disable_popdown (GtkEntry *entry,
11827                                       GtkMenu  *menu,
11828                                       gpointer  data)
11829 {
11830   PsppSheetView *tree_view = (PsppSheetView *)data;
11831
11832   tree_view->priv->disable_popdown = 1;
11833   g_signal_connect (menu, "hide",
11834                     G_CALLBACK (pspp_sheet_view_search_enable_popdown), data);
11835 }
11836
11837 #if GTK3_TRANSITION
11838 /* Because we're visible but offscreen, we just set a flag in the preedit
11839  * callback.
11840  */
11841 static void
11842 pspp_sheet_view_search_preedit_changed (GtkIMContext *im_context,
11843                                       PsppSheetView  *tree_view)
11844 {
11845   tree_view->priv->imcontext_changed = 1;
11846   if (tree_view->priv->typeselect_flush_timeout)
11847     {
11848       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11849       tree_view->priv->typeselect_flush_timeout =
11850         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11851                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11852                        tree_view);
11853     }
11854
11855 }
11856 #endif
11857
11858 static void
11859 pspp_sheet_view_search_activate (GtkEntry    *entry,
11860                                PsppSheetView *tree_view)
11861 {
11862   GtkTreePath *path;
11863   int node;
11864
11865   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window,
11866                                     tree_view);
11867
11868   /* If we have a row selected and it's the cursor row, we activate
11869    * the row XXX */
11870   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
11871     {
11872       path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
11873       
11874       _pspp_sheet_view_find_node (tree_view, path, &node);
11875       
11876       if (node >= 0 && pspp_sheet_view_node_is_selected (tree_view, node))
11877         pspp_sheet_view_row_activated (tree_view, path, tree_view->priv->focus_column);
11878       
11879       gtk_tree_path_free (path);
11880     }
11881 }
11882
11883 static gboolean
11884 pspp_sheet_view_real_search_enable_popdown (gpointer data)
11885 {
11886   PsppSheetView *tree_view = (PsppSheetView *)data;
11887
11888   tree_view->priv->disable_popdown = 0;
11889
11890   return FALSE;
11891 }
11892
11893 static void
11894 pspp_sheet_view_search_enable_popdown (GtkWidget *widget,
11895                                      gpointer   data)
11896 {
11897   gdk_threads_add_timeout_full (G_PRIORITY_HIGH, 200, pspp_sheet_view_real_search_enable_popdown, g_object_ref (data), g_object_unref);
11898 }
11899
11900 static gboolean
11901 pspp_sheet_view_search_delete_event (GtkWidget *widget,
11902                                    GdkEventAny *event,
11903                                    PsppSheetView *tree_view)
11904 {
11905   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11906
11907   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11908
11909   return TRUE;
11910 }
11911
11912 static gboolean
11913 pspp_sheet_view_search_button_press_event (GtkWidget *widget,
11914                                          GdkEventButton *event,
11915                                          PsppSheetView *tree_view)
11916 {
11917   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11918
11919   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11920
11921   if (event->window == tree_view->priv->bin_window)
11922     pspp_sheet_view_button_press (GTK_WIDGET (tree_view), event);
11923
11924   return TRUE;
11925 }
11926
11927 static gboolean
11928 pspp_sheet_view_search_scroll_event (GtkWidget *widget,
11929                                    GdkEventScroll *event,
11930                                    PsppSheetView *tree_view)
11931 {
11932   gboolean retval = FALSE;
11933
11934   if (event->direction == GDK_SCROLL_UP)
11935     {
11936       pspp_sheet_view_search_move (widget, tree_view, TRUE);
11937       retval = TRUE;
11938     }
11939   else if (event->direction == GDK_SCROLL_DOWN)
11940     {
11941       pspp_sheet_view_search_move (widget, tree_view, FALSE);
11942       retval = TRUE;
11943     }
11944
11945   /* renew the flush timeout */
11946   if (retval && tree_view->priv->typeselect_flush_timeout
11947       && !tree_view->priv->search_custom_entry_set)
11948     {
11949       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11950       tree_view->priv->typeselect_flush_timeout =
11951         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11952                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11953                        tree_view);
11954     }
11955
11956   return retval;
11957 }
11958
11959 static gboolean
11960 pspp_sheet_view_search_key_press_event (GtkWidget *widget,
11961                                       GdkEventKey *event,
11962                                       PsppSheetView *tree_view)
11963 {
11964   gboolean retval = FALSE;
11965
11966   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11967   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11968
11969   /* close window and cancel the search */
11970   if (!tree_view->priv->search_custom_entry_set
11971       && (event->keyval == GDK_Escape ||
11972           event->keyval == GDK_Tab ||
11973             event->keyval == GDK_KP_Tab ||
11974             event->keyval == GDK_ISO_Left_Tab))
11975     {
11976       pspp_sheet_view_search_dialog_hide (widget, tree_view);
11977       return TRUE;
11978     }
11979
11980   /* select previous matching iter */
11981   if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
11982     {
11983       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
11984         gtk_widget_error_bell (widget);
11985
11986       retval = TRUE;
11987     }
11988
11989   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK))
11990       && (event->keyval == GDK_g || event->keyval == GDK_G))
11991     {
11992       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
11993         gtk_widget_error_bell (widget);
11994
11995       retval = TRUE;
11996     }
11997
11998   /* select next matching iter */
11999   if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
12000     {
12001       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12002         gtk_widget_error_bell (widget);
12003
12004       retval = TRUE;
12005     }
12006
12007   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == GTK_DEFAULT_ACCEL_MOD_MASK)
12008       && (event->keyval == GDK_g || event->keyval == GDK_G))
12009     {
12010       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12011         gtk_widget_error_bell (widget);
12012
12013       retval = TRUE;
12014     }
12015
12016   /* renew the flush timeout */
12017   if (retval && tree_view->priv->typeselect_flush_timeout
12018       && !tree_view->priv->search_custom_entry_set)
12019     {
12020       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12021       tree_view->priv->typeselect_flush_timeout =
12022         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12023                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12024                        tree_view);
12025     }
12026
12027   return retval;
12028 }
12029
12030 /*  this function returns FALSE if there is a search string but
12031  *  nothing was found, and TRUE otherwise.
12032  */
12033 static gboolean
12034 pspp_sheet_view_search_move (GtkWidget   *window,
12035                            PsppSheetView *tree_view,
12036                            gboolean     up)
12037 {
12038   gboolean ret;
12039   gint len;
12040   gint count = 0;
12041   const gchar *text;
12042   GtkTreeIter iter;
12043   GtkTreeModel *model;
12044   PsppSheetSelection *selection;
12045
12046   text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
12047
12048   g_return_val_if_fail (text != NULL, FALSE);
12049
12050   len = strlen (text);
12051
12052   if (up && tree_view->priv->selected_iter == 1)
12053     return strlen (text) < 1;
12054
12055   len = strlen (text);
12056
12057   if (len < 1)
12058     return TRUE;
12059
12060   model = pspp_sheet_view_get_model (tree_view);
12061   selection = pspp_sheet_view_get_selection (tree_view);
12062
12063   /* search */
12064   pspp_sheet_selection_unselect_all (selection);
12065   if (!gtk_tree_model_get_iter_first (model, &iter))
12066     return TRUE;
12067
12068   ret = pspp_sheet_view_search_iter (model, selection, &iter, text,
12069                                    &count, up?((tree_view->priv->selected_iter) - 1):((tree_view->priv->selected_iter + 1)));
12070
12071   if (ret)
12072     {
12073       /* found */
12074       tree_view->priv->selected_iter += up?(-1):(1);
12075       return TRUE;
12076     }
12077   else
12078     {
12079       /* return to old iter */
12080       count = 0;
12081       gtk_tree_model_get_iter_first (model, &iter);
12082       pspp_sheet_view_search_iter (model, selection,
12083                                  &iter, text,
12084                                  &count, tree_view->priv->selected_iter);
12085       return FALSE;
12086     }
12087 }
12088
12089 static gboolean
12090 pspp_sheet_view_search_equal_func (GtkTreeModel *model,
12091                                  gint          column,
12092                                  const gchar  *key,
12093                                  GtkTreeIter  *iter,
12094                                  gpointer      search_data)
12095 {
12096   gboolean retval = TRUE;
12097   const gchar *str;
12098   gchar *normalized_string;
12099   gchar *normalized_key;
12100   gchar *case_normalized_string = NULL;
12101   gchar *case_normalized_key = NULL;
12102   GValue value = {0,};
12103   GValue transformed = {0,};
12104
12105   gtk_tree_model_get_value (model, iter, column, &value);
12106
12107   g_value_init (&transformed, G_TYPE_STRING);
12108
12109   if (!g_value_transform (&value, &transformed))
12110     {
12111       g_value_unset (&value);
12112       return TRUE;
12113     }
12114
12115   g_value_unset (&value);
12116
12117   str = g_value_get_string (&transformed);
12118   if (!str)
12119     {
12120       g_value_unset (&transformed);
12121       return TRUE;
12122     }
12123
12124   normalized_string = g_utf8_normalize (str, -1, G_NORMALIZE_ALL);
12125   normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
12126
12127   if (normalized_string && normalized_key)
12128     {
12129       case_normalized_string = g_utf8_casefold (normalized_string, -1);
12130       case_normalized_key = g_utf8_casefold (normalized_key, -1);
12131
12132       if (strncmp (case_normalized_key, case_normalized_string, strlen (case_normalized_key)) == 0)
12133         retval = FALSE;
12134     }
12135
12136   g_value_unset (&transformed);
12137   g_free (normalized_key);
12138   g_free (normalized_string);
12139   g_free (case_normalized_key);
12140   g_free (case_normalized_string);
12141
12142   return retval;
12143 }
12144
12145 static gboolean
12146 pspp_sheet_view_search_iter (GtkTreeModel     *model,
12147                              PsppSheetSelection *selection,
12148                              GtkTreeIter      *iter,
12149                              const gchar      *text,
12150                              gint             *count,
12151                              gint              n)
12152 {
12153   int node = -1;
12154   GtkTreePath *path;
12155
12156   PsppSheetView *tree_view = pspp_sheet_selection_get_tree_view (selection);
12157
12158   path = gtk_tree_model_get_path (model, iter);
12159   _pspp_sheet_view_find_node (tree_view, path, &node);
12160
12161   do
12162     {
12163       gboolean done = FALSE;
12164
12165       if (! tree_view->priv->search_equal_func (model, tree_view->priv->search_column, text, iter, tree_view->priv->search_user_data))
12166         {
12167           (*count)++;
12168           if (*count == n)
12169             {
12170               pspp_sheet_view_scroll_to_cell (tree_view, path, NULL,
12171                                               TRUE, 0.5, 0.0);
12172               pspp_sheet_selection_select_iter (selection, iter);
12173               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12174
12175               if (path)
12176                 gtk_tree_path_free (path);
12177
12178               return TRUE;
12179             }
12180         }
12181
12182
12183       do
12184         {
12185           node = pspp_sheet_view_node_next (tree_view, node);
12186
12187           if (node >= 0)
12188             {
12189               gboolean has_next;
12190
12191               has_next = gtk_tree_model_iter_next (model, iter);
12192
12193               done = TRUE;
12194               gtk_tree_path_next (path);
12195
12196               /* sanity check */
12197               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
12198             }
12199           else
12200             {
12201               if (path)
12202                 gtk_tree_path_free (path);
12203
12204               /* we've run out of tree, done with this func */
12205               return FALSE;
12206             }
12207         }
12208       while (!done);
12209     }
12210   while (1);
12211
12212   return FALSE;
12213 }
12214
12215 static void
12216 pspp_sheet_view_search_init (GtkWidget   *entry,
12217                            PsppSheetView *tree_view)
12218 {
12219   gint ret;
12220   gint count = 0;
12221   const gchar *text;
12222   GtkTreeIter iter;
12223   GtkTreeModel *model;
12224   PsppSheetSelection *selection;
12225
12226   g_return_if_fail (GTK_IS_ENTRY (entry));
12227   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12228
12229   text = gtk_entry_get_text (GTK_ENTRY (entry));
12230
12231   model = pspp_sheet_view_get_model (tree_view);
12232   selection = pspp_sheet_view_get_selection (tree_view);
12233
12234   /* search */
12235   pspp_sheet_selection_unselect_all (selection);
12236   if (tree_view->priv->typeselect_flush_timeout
12237       && !tree_view->priv->search_custom_entry_set)
12238     {
12239       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12240       tree_view->priv->typeselect_flush_timeout =
12241         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12242                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12243                        tree_view);
12244     }
12245
12246   if (*text == '\0')
12247     return;
12248
12249   if (!gtk_tree_model_get_iter_first (model, &iter))
12250     return;
12251
12252   ret = pspp_sheet_view_search_iter (model, selection,
12253                                    &iter, text,
12254                                    &count, 1);
12255
12256   if (ret)
12257     tree_view->priv->selected_iter = 1;
12258 }
12259
12260 static void
12261 pspp_sheet_view_remove_widget (GtkCellEditable *cell_editable,
12262                              PsppSheetView     *tree_view)
12263 {
12264   if (tree_view->priv->edited_column == NULL)
12265     return;
12266
12267   _pspp_sheet_view_column_stop_editing (tree_view->priv->edited_column);
12268   tree_view->priv->edited_column = NULL;
12269
12270   if (gtk_widget_has_focus (GTK_WIDGET (cell_editable)))
12271     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12272
12273   g_signal_handlers_disconnect_by_func (cell_editable,
12274                                         pspp_sheet_view_remove_widget,
12275                                         tree_view);
12276   g_signal_handlers_disconnect_by_func (cell_editable,
12277                                         pspp_sheet_view_editable_button_press_event,
12278                                         tree_view);
12279   g_signal_handlers_disconnect_by_func (cell_editable,
12280                                         pspp_sheet_view_editable_clicked,
12281                                         tree_view);
12282
12283   gtk_container_remove (GTK_CONTAINER (tree_view),
12284                         GTK_WIDGET (cell_editable));  
12285
12286   /* FIXME should only redraw a single node */
12287   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12288 }
12289
12290 static gboolean
12291 pspp_sheet_view_start_editing (PsppSheetView *tree_view,
12292                              GtkTreePath *cursor_path)
12293 {
12294   GtkTreeIter iter;
12295   GdkRectangle background_area;
12296   GdkRectangle cell_area;
12297   GtkCellEditable *editable_widget = NULL;
12298   gchar *path_string;
12299   guint flags = 0; /* can be 0, as the flags are primarily for rendering */
12300   gint retval = FALSE;
12301   int cursor_node;
12302
12303   g_assert (tree_view->priv->focus_column);
12304
12305   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
12306     return FALSE;
12307
12308   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
12309   if (cursor_node < 0)
12310     return FALSE;
12311
12312   path_string = gtk_tree_path_to_string (cursor_path);
12313   gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path);
12314
12315   pspp_sheet_view_column_cell_set_cell_data (tree_view->priv->focus_column,
12316                                            tree_view->priv->model,
12317                                            &iter);
12318   pspp_sheet_view_get_background_area (tree_view,
12319                                      cursor_path,
12320                                      tree_view->priv->focus_column,
12321                                      &background_area);
12322   pspp_sheet_view_get_cell_area (tree_view,
12323                                cursor_path,
12324                                tree_view->priv->focus_column,
12325                                &cell_area);
12326
12327   if (_pspp_sheet_view_column_cell_event (tree_view->priv->focus_column,
12328                                         &editable_widget,
12329                                         NULL,
12330                                         path_string,
12331                                         &background_area,
12332                                         &cell_area,
12333                                         flags))
12334     {
12335       retval = TRUE;
12336       if (editable_widget != NULL)
12337         {
12338           gint left, right;
12339           GdkRectangle area;
12340           GtkCellRenderer *cell;
12341
12342           area = cell_area;
12343           cell = _pspp_sheet_view_column_get_edited_cell (tree_view->priv->focus_column);
12344
12345           _pspp_sheet_view_column_get_neighbor_sizes (tree_view->priv->focus_column, cell, &left, &right);
12346
12347           area.x += left;
12348           area.width -= right + left;
12349
12350           pspp_sheet_view_real_start_editing (tree_view,
12351                                             tree_view->priv->focus_column,
12352                                             cursor_path,
12353                                             editable_widget,
12354                                             &area,
12355                                             NULL,
12356                                             flags);
12357         }
12358
12359     }
12360   g_free (path_string);
12361   return retval;
12362 }
12363
12364 static gboolean
12365 pspp_sheet_view_editable_button_press_event (GtkWidget *widget,
12366                                              GdkEventButton *event,
12367                                              PsppSheetView *sheet_view)
12368 {
12369   gint node;
12370
12371   node = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
12372                                              "pspp-sheet-view-node"));
12373   return pspp_sheet_view_row_head_clicked (sheet_view,
12374                                            node,
12375                                            sheet_view->priv->edited_column,
12376                                            event);
12377 }
12378
12379 static void
12380 pspp_sheet_view_editable_clicked (GtkButton *button,
12381                                   PsppSheetView *sheet_view)
12382 {
12383   pspp_sheet_view_editable_button_press_event (GTK_WIDGET (button), NULL,
12384                                                sheet_view);
12385 }
12386
12387 static gboolean
12388 is_all_selected (GtkWidget *widget)
12389 {
12390   GtkEntryBuffer *buffer;
12391   gint start_pos, end_pos;
12392
12393   if (!GTK_IS_ENTRY (widget))
12394     return FALSE;
12395
12396   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12397   return (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget),
12398                                              &start_pos, &end_pos)
12399           && start_pos == 0
12400           && end_pos == gtk_entry_buffer_get_length (buffer));
12401 }
12402
12403 static gboolean
12404 is_at_left (GtkWidget *widget)
12405 {
12406   return (GTK_IS_ENTRY (widget)
12407           && gtk_editable_get_position (GTK_EDITABLE (widget)) == 0);
12408 }
12409
12410 static gboolean
12411 is_at_right (GtkWidget *widget)
12412 {
12413   GtkEntryBuffer *buffer;
12414   gint length;
12415
12416   if (!GTK_IS_ENTRY (widget))
12417     return FALSE;
12418
12419   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12420   length = gtk_entry_buffer_get_length (buffer);
12421   return gtk_editable_get_position (GTK_EDITABLE (widget)) == length;
12422 }
12423
12424 static gboolean
12425 pspp_sheet_view_event (GtkWidget *widget,
12426                        GdkEventKey *event,
12427                        PsppSheetView *tree_view)
12428 {
12429   PsppSheetViewColumn *column;
12430   GtkTreePath *path;
12431   gboolean handled;
12432   gboolean cancel;
12433   guint keyval;
12434   gint row;
12435
12436   /* Intercept only key press events.
12437      It would make sense to use "key-press-event" instead of "event", but
12438      GtkEntry attaches its own signal handler to "key-press-event" that runs
12439      before ours and overrides our desired behavior for GDK_Up and GDK_Down.
12440   */
12441   if (event->type != GDK_KEY_PRESS)
12442     return FALSE;
12443
12444   keyval = event->keyval;
12445   cancel = FALSE;
12446   switch (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK))
12447     {
12448     case 0:
12449       switch (event->keyval)
12450         {
12451         case GDK_Left:      case GDK_KP_Left:
12452         case GDK_Home:      case GDK_KP_Home:
12453           if (!is_all_selected (widget) && !is_at_left (widget))
12454             return FALSE;
12455           break;
12456
12457         case GDK_Right:     case GDK_KP_Right:
12458         case GDK_End:       case GDK_KP_End:
12459           if (!is_all_selected (widget) && !is_at_right (widget))
12460             return FALSE;
12461           break;
12462
12463         case GDK_Up:        case GDK_KP_Up:
12464         case GDK_Down:      case GDK_KP_Down:
12465           break;
12466
12467         case GDK_Page_Up:   case GDK_KP_Page_Up:
12468         case GDK_Page_Down: case GDK_KP_Page_Down:
12469           break;
12470
12471         case GDK_Escape:
12472           cancel = TRUE;
12473           break;
12474
12475         case GDK_Return:
12476           keyval = GDK_Down;
12477           break;
12478
12479         case GDK_Tab:       case GDK_KP_Tab:
12480         case GDK_ISO_Left_Tab:
12481           keyval = GDK_Tab;
12482           break;
12483
12484         default:
12485           return FALSE;
12486         }
12487       break;
12488
12489     case GDK_SHIFT_MASK:
12490       switch (event->keyval)
12491         {
12492         case GDK_Tab:
12493         case GDK_ISO_Left_Tab:
12494           keyval = GDK_Tab;
12495           break;
12496
12497         default:
12498           return FALSE;
12499         }
12500       break;
12501
12502     case GDK_CONTROL_MASK:
12503       switch (event->keyval)
12504         {
12505         case GDK_Left:      case GDK_KP_Left:
12506           if (!is_all_selected (widget) && !is_at_left (widget))
12507             return FALSE;
12508           break;
12509
12510         case GDK_Right:     case GDK_KP_Right:
12511           if (!is_all_selected (widget) && !is_at_right (widget))
12512             return FALSE;
12513           break;
12514
12515         case GDK_Up:        case GDK_KP_Up:
12516         case GDK_Down:      case GDK_KP_Down:
12517           break;
12518
12519         default:
12520           return FALSE;
12521         }
12522       break;
12523
12524     default:
12525       return FALSE;
12526     }
12527
12528   row = tree_view->priv->edited_row;
12529   column = tree_view->priv->edited_column;
12530   path = gtk_tree_path_new_from_indices (row, -1);
12531
12532   pspp_sheet_view_stop_editing (tree_view, cancel);
12533   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12534
12535   pspp_sheet_view_set_cursor (tree_view, path, column, FALSE);
12536   gtk_tree_path_free (path);
12537
12538   handled = gtk_binding_set_activate (edit_bindings, keyval, event->state,
12539                                       GTK_OBJECT (tree_view));
12540   if (handled)
12541     g_signal_stop_emission_by_name (widget, "event");
12542
12543   pspp_sheet_view_get_cursor (tree_view, &path, NULL);
12544   pspp_sheet_view_start_editing (tree_view, path);
12545   gtk_tree_path_free (path);
12546
12547   return handled;
12548 }
12549
12550 static void
12551 pspp_sheet_view_override_cell_keypresses (GtkWidget *widget,
12552                                           gpointer data)
12553 {
12554   PsppSheetView *sheet_view = data;
12555
12556   g_signal_connect (widget, "event",
12557                     G_CALLBACK (pspp_sheet_view_event),
12558                     sheet_view);
12559
12560   if (GTK_IS_CONTAINER (widget))
12561     gtk_container_foreach (GTK_CONTAINER (widget),
12562                            pspp_sheet_view_override_cell_keypresses,
12563                            data);
12564 }
12565
12566 static void
12567 pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
12568                                   PsppSheetViewColumn *column,
12569                                   GtkTreePath       *path,
12570                                   GtkCellEditable   *cell_editable,
12571                                   GdkRectangle      *cell_area,
12572                                   GdkEvent          *event,
12573                                   guint              flags)
12574 {
12575   PsppSheetSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
12576   gint pre_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
12577   GtkRequisition requisition;
12578   gint row;
12579
12580   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
12581
12582   tree_view->priv->edited_column = column;
12583   _pspp_sheet_view_column_start_editing (column, GTK_CELL_EDITABLE (cell_editable));
12584
12585   row = gtk_tree_path_get_indices (path)[0];
12586   tree_view->priv->edited_row = row;
12587   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12588   cell_area->y += pre_val - (int)gtk_adjustment_get_value (tree_view->priv->vadjustment);
12589
12590   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
12591   pspp_sheet_selection_select_column (tree_view->priv->selection, column);
12592   tree_view->priv->anchor_column = column;
12593
12594   gtk_widget_size_request (GTK_WIDGET (cell_editable), &requisition);
12595
12596   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
12597
12598   if (requisition.height < cell_area->height)
12599     {
12600       gint diff = cell_area->height - requisition.height;
12601       pspp_sheet_view_put (tree_view,
12602                          GTK_WIDGET (cell_editable),
12603                          cell_area->x, cell_area->y + diff/2,
12604                          cell_area->width, requisition.height);
12605     }
12606   else
12607     {
12608       pspp_sheet_view_put (tree_view,
12609                          GTK_WIDGET (cell_editable),
12610                          cell_area->x, cell_area->y,
12611                          cell_area->width, cell_area->height);
12612     }
12613
12614   gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (cell_editable),
12615                                    (GdkEvent *)event);
12616
12617   gtk_widget_grab_focus (GTK_WIDGET (cell_editable));
12618   g_signal_connect (cell_editable, "remove-widget",
12619                     G_CALLBACK (pspp_sheet_view_remove_widget), tree_view);
12620   if (mode == PSPP_SHEET_SELECTION_RECTANGLE && column->row_head &&
12621       GTK_IS_BUTTON (cell_editable))
12622     {
12623       g_signal_connect (cell_editable, "button-press-event",
12624                         G_CALLBACK (pspp_sheet_view_editable_button_press_event),
12625                         tree_view);
12626       g_object_set_data (G_OBJECT (cell_editable), "pspp-sheet-view-node",
12627                          GINT_TO_POINTER (row));
12628       g_signal_connect (cell_editable, "clicked",
12629                         G_CALLBACK (pspp_sheet_view_editable_clicked),
12630                         tree_view);
12631     }
12632
12633   pspp_sheet_view_override_cell_keypresses (GTK_WIDGET (cell_editable),
12634                                             tree_view);
12635 }
12636
12637 void
12638 pspp_sheet_view_stop_editing (PsppSheetView *tree_view,
12639                               gboolean     cancel_editing)
12640 {
12641   PsppSheetViewColumn *column;
12642   GtkCellRenderer *cell;
12643
12644   if (tree_view->priv->edited_column == NULL)
12645     return;
12646
12647   /*
12648    * This is very evil. We need to do this, because
12649    * gtk_cell_editable_editing_done may trigger pspp_sheet_view_row_changed
12650    * later on. If pspp_sheet_view_row_changed notices
12651    * tree_view->priv->edited_column != NULL, it'll call
12652    * pspp_sheet_view_stop_editing again. Bad things will happen then.
12653    *
12654    * Please read that again if you intend to modify anything here.
12655    */
12656
12657   column = tree_view->priv->edited_column;
12658   tree_view->priv->edited_column = NULL;
12659
12660   cell = _pspp_sheet_view_column_get_edited_cell (column);
12661   gtk_cell_renderer_stop_editing (cell, cancel_editing);
12662
12663   if (!cancel_editing)
12664     gtk_cell_editable_editing_done (column->editable_widget);
12665
12666   tree_view->priv->edited_column = column;
12667
12668   gtk_cell_editable_remove_widget (column->editable_widget);
12669 }
12670
12671
12672 /**
12673  * pspp_sheet_view_set_hover_selection:
12674  * @tree_view: a #PsppSheetView
12675  * @hover: %TRUE to enable hover selection mode
12676  *
12677  * Enables of disables the hover selection mode of @tree_view.
12678  * Hover selection makes the selected row follow the pointer.
12679  * Currently, this works only for the selection modes 
12680  * %PSPP_SHEET_SELECTION_SINGLE and %PSPP_SHEET_SELECTION_BROWSE.
12681  * 
12682  * Since: 2.6
12683  **/
12684 void     
12685 pspp_sheet_view_set_hover_selection (PsppSheetView *tree_view,
12686                                    gboolean     hover)
12687 {
12688   hover = hover != FALSE;
12689
12690   if (hover != tree_view->priv->hover_selection)
12691     {
12692       tree_view->priv->hover_selection = hover;
12693
12694       g_object_notify (G_OBJECT (tree_view), "hover-selection");
12695     }
12696 }
12697
12698 /**
12699  * pspp_sheet_view_get_hover_selection:
12700  * @tree_view: a #PsppSheetView
12701  * 
12702  * Returns whether hover selection mode is turned on for @tree_view.
12703  * 
12704  * Return value: %TRUE if @tree_view is in hover selection mode
12705  *
12706  * Since: 2.6 
12707  **/
12708 gboolean 
12709 pspp_sheet_view_get_hover_selection (PsppSheetView *tree_view)
12710 {
12711   return tree_view->priv->hover_selection;
12712 }
12713
12714 /**
12715  * pspp_sheet_view_set_rubber_banding:
12716  * @tree_view: a #PsppSheetView
12717  * @enable: %TRUE to enable rubber banding
12718  *
12719  * Enables or disables rubber banding in @tree_view.  If the selection mode is
12720  * #PSPP_SHEET_SELECTION_MULTIPLE or #PSPP_SHEET_SELECTION_RECTANGLE, rubber
12721  * banding will allow the user to select multiple rows by dragging the mouse.
12722  * 
12723  * Since: 2.10
12724  **/
12725 void
12726 pspp_sheet_view_set_rubber_banding (PsppSheetView *tree_view,
12727                                   gboolean     enable)
12728 {
12729   enable = enable != FALSE;
12730
12731   if (enable != tree_view->priv->rubber_banding_enable)
12732     {
12733       tree_view->priv->rubber_banding_enable = enable;
12734
12735       g_object_notify (G_OBJECT (tree_view), "rubber-banding");
12736     }
12737 }
12738
12739 /**
12740  * pspp_sheet_view_get_rubber_banding:
12741  * @tree_view: a #PsppSheetView
12742  * 
12743  * Returns whether rubber banding is turned on for @tree_view.  If the
12744  * selection mode is #PSPP_SHEET_SELECTION_MULTIPLE or
12745  * #PSPP_SHEET_SELECTION_RECTANGLE, rubber banding will allow the user to
12746  * select multiple rows by dragging the mouse.
12747  * 
12748  * Return value: %TRUE if rubber banding in @tree_view is enabled.
12749  *
12750  * Since: 2.10
12751  **/
12752 gboolean
12753 pspp_sheet_view_get_rubber_banding (PsppSheetView *tree_view)
12754 {
12755   return tree_view->priv->rubber_banding_enable;
12756 }
12757
12758 /**
12759  * pspp_sheet_view_is_rubber_banding_active:
12760  * @tree_view: a #PsppSheetView
12761  * 
12762  * Returns whether a rubber banding operation is currently being done
12763  * in @tree_view.
12764  *
12765  * Return value: %TRUE if a rubber banding operation is currently being
12766  * done in @tree_view.
12767  *
12768  * Since: 2.12
12769  **/
12770 gboolean
12771 pspp_sheet_view_is_rubber_banding_active (PsppSheetView *tree_view)
12772 {
12773   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12774
12775   if (tree_view->priv->rubber_banding_enable
12776       && tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
12777     return TRUE;
12778
12779   return FALSE;
12780 }
12781
12782 static void
12783 pspp_sheet_view_grab_notify (GtkWidget *widget,
12784                            gboolean   was_grabbed)
12785 {
12786   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12787
12788   tree_view->priv->in_grab = !was_grabbed;
12789
12790   if (!was_grabbed)
12791     {
12792       tree_view->priv->pressed_button = -1;
12793
12794       if (tree_view->priv->rubber_band_status)
12795         pspp_sheet_view_stop_rubber_band (tree_view);
12796     }
12797 }
12798
12799 static void
12800 pspp_sheet_view_state_changed (GtkWidget      *widget,
12801                              GtkStateType    previous_state)
12802 {
12803   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12804
12805   if (gtk_widget_get_realized (widget))
12806     {
12807       GtkStyle *style = gtk_widget_get_style (widget);
12808       gdk_window_set_background (tree_view->priv->bin_window, &style->base[gtk_widget_get_state (widget)]);
12809     }
12810
12811   gtk_widget_queue_draw (widget);
12812 }
12813
12814 /**
12815  * pspp_sheet_view_get_grid_lines:
12816  * @tree_view: a #PsppSheetView
12817  *
12818  * Returns which grid lines are enabled in @tree_view.
12819  *
12820  * Return value: a #PsppSheetViewGridLines value indicating which grid lines
12821  * are enabled.
12822  *
12823  * Since: 2.10
12824  */
12825 PsppSheetViewGridLines
12826 pspp_sheet_view_get_grid_lines (PsppSheetView *tree_view)
12827 {
12828   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12829
12830   return tree_view->priv->grid_lines;
12831 }
12832
12833 /**
12834  * pspp_sheet_view_set_grid_lines:
12835  * @tree_view: a #PsppSheetView
12836  * @grid_lines: a #PsppSheetViewGridLines value indicating which grid lines to
12837  * enable.
12838  *
12839  * Sets which grid lines to draw in @tree_view.
12840  *
12841  * Since: 2.10
12842  */
12843 void
12844 pspp_sheet_view_set_grid_lines (PsppSheetView           *tree_view,
12845                               PsppSheetViewGridLines   grid_lines)
12846 {
12847   PsppSheetViewPrivate *priv;
12848   PsppSheetViewGridLines old_grid_lines;
12849
12850   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12851
12852   priv = tree_view->priv;
12853
12854   old_grid_lines = priv->grid_lines;
12855   priv->grid_lines = grid_lines;
12856   
12857   if (old_grid_lines != grid_lines)
12858     {
12859       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12860       
12861       g_object_notify (G_OBJECT (tree_view), "enable-grid-lines");
12862     }
12863 }
12864
12865 /**
12866  * pspp_sheet_view_get_special_cells:
12867  * @tree_view: a #PsppSheetView
12868  *
12869  * Returns which grid lines are enabled in @tree_view.
12870  *
12871  * Return value: a #PsppSheetViewSpecialCells value indicating whether rows in
12872  * the sheet view contain special cells.
12873  */
12874 PsppSheetViewSpecialCells
12875 pspp_sheet_view_get_special_cells (PsppSheetView *tree_view)
12876 {
12877   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12878
12879   return tree_view->priv->special_cells;
12880 }
12881
12882 /**
12883  * pspp_sheet_view_set_special_cells:
12884  * @tree_view: a #PsppSheetView
12885  * @special_cells: a #PsppSheetViewSpecialCells value indicating whether rows in
12886  * the sheet view contain special cells.
12887  *
12888  * Sets whether rows in the sheet view contain special cells, controlling the
12889  * rendering of row selections.
12890  */
12891 void
12892 pspp_sheet_view_set_special_cells (PsppSheetView           *tree_view,
12893                               PsppSheetViewSpecialCells   special_cells)
12894 {
12895   PsppSheetViewPrivate *priv;
12896
12897   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12898
12899   priv = tree_view->priv;
12900
12901   if (priv->special_cells != special_cells)
12902     {
12903       priv->special_cells = special_cells;
12904       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12905       g_object_notify (G_OBJECT (tree_view), "special-cells");
12906     }
12907 }
12908
12909 int
12910 pspp_sheet_view_get_fixed_height (const PsppSheetView *tree_view)
12911 {
12912   /* XXX (re)calculate fixed_height if necessary */
12913   return tree_view->priv->fixed_height;
12914 }
12915
12916 void
12917 pspp_sheet_view_set_fixed_height (PsppSheetView *tree_view,
12918                                   int fixed_height)
12919 {
12920   g_return_if_fail (fixed_height > 0);
12921
12922   if (tree_view->priv->fixed_height != fixed_height)
12923     {
12924       tree_view->priv->fixed_height = fixed_height;
12925       g_object_notify (G_OBJECT (tree_view), "fixed-height");
12926     }
12927   if (!tree_view->priv->fixed_height_set)
12928     {
12929       tree_view->priv->fixed_height_set = TRUE;
12930       g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
12931     }
12932 }
12933
12934 /**
12935  * pspp_sheet_view_set_tooltip_row:
12936  * @tree_view: a #PsppSheetView
12937  * @tooltip: a #GtkTooltip
12938  * @path: a #GtkTreePath
12939  *
12940  * Sets the tip area of @tooltip to be the area covered by the row at @path.
12941  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
12942  * See also gtk_tooltip_set_tip_area().
12943  *
12944  * Since: 2.12
12945  */
12946 void
12947 pspp_sheet_view_set_tooltip_row (PsppSheetView *tree_view,
12948                                GtkTooltip  *tooltip,
12949                                GtkTreePath *path)
12950 {
12951   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12952   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
12953
12954   pspp_sheet_view_set_tooltip_cell (tree_view, tooltip, path, NULL, NULL);
12955 }
12956
12957 /**
12958  * pspp_sheet_view_set_tooltip_cell:
12959  * @tree_view: a #PsppSheetView
12960  * @tooltip: a #GtkTooltip
12961  * @path: (allow-none): a #GtkTreePath or %NULL
12962  * @column: (allow-none): a #PsppSheetViewColumn or %NULL
12963  * @cell: (allow-none): a #GtkCellRenderer or %NULL
12964  *
12965  * Sets the tip area of @tooltip to the area @path, @column and @cell have
12966  * in common.  For example if @path is %NULL and @column is set, the tip
12967  * area will be set to the full area covered by @column.  See also
12968  * gtk_tooltip_set_tip_area().
12969  *
12970  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
12971  *
12972  * Since: 2.12
12973  */
12974 void
12975 pspp_sheet_view_set_tooltip_cell (PsppSheetView       *tree_view,
12976                                 GtkTooltip        *tooltip,
12977                                 GtkTreePath       *path,
12978                                 PsppSheetViewColumn *column,
12979                                 GtkCellRenderer   *cell)
12980 {
12981   GdkRectangle rect;
12982
12983   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12984   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
12985   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
12986   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
12987
12988   /* Determine x values. */
12989   if (column && cell)
12990     {
12991       GdkRectangle tmp;
12992       gint start, width;
12993
12994       pspp_sheet_view_get_cell_area (tree_view, path, column, &tmp);
12995       pspp_sheet_view_column_cell_get_position (column, cell, &start, &width);
12996
12997       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
12998                                                          tmp.x + start, 0,
12999                                                          &rect.x, NULL);
13000       rect.width = width;
13001     }
13002   else if (column)
13003     {
13004       GdkRectangle tmp;
13005
13006       pspp_sheet_view_get_background_area (tree_view, NULL, column, &tmp);
13007       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13008                                                          tmp.x, 0,
13009                                                          &rect.x, NULL);
13010       rect.width = tmp.width;
13011     }
13012   else
13013     {
13014       GtkAllocation allocation;
13015       gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
13016       rect.x = 0;
13017       rect.width = allocation.width;
13018     }
13019
13020   /* Determine y values. */
13021   if (path)
13022     {
13023       GdkRectangle tmp;
13024
13025       pspp_sheet_view_get_background_area (tree_view, path, NULL, &tmp);
13026       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13027                                                          0, tmp.y,
13028                                                          NULL, &rect.y);
13029       rect.height = tmp.height;
13030     }
13031   else
13032     {
13033       rect.y = 0;
13034       rect.height = gtk_adjustment_get_page_size (tree_view->priv->vadjustment);
13035     }
13036
13037   gtk_tooltip_set_tip_area (tooltip, &rect);
13038 }
13039
13040 /**
13041  * pspp_sheet_view_get_tooltip_context:
13042  * @tree_view: a #PsppSheetView
13043  * @x: the x coordinate (relative to widget coordinates)
13044  * @y: the y coordinate (relative to widget coordinates)
13045  * @keyboard_tip: whether this is a keyboard tooltip or not
13046  * @model: (allow-none): a pointer to receive a #GtkTreeModel or %NULL
13047  * @path: (allow-none): a pointer to receive a #GtkTreePath or %NULL
13048  * @iter: (allow-none): a pointer to receive a #GtkTreeIter or %NULL
13049  *
13050  * This function is supposed to be used in a #GtkWidget::query-tooltip
13051  * signal handler for #PsppSheetView.  The @x, @y and @keyboard_tip values
13052  * which are received in the signal handler, should be passed to this
13053  * function without modification.
13054  *
13055  * The return value indicates whether there is a tree view row at the given
13056  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips.  For keyboard
13057  * tooltips the row returned will be the cursor row.  When %TRUE, then any of
13058  * @model, @path and @iter which have been provided will be set to point to
13059  * that row and the corresponding model.  @x and @y will always be converted
13060  * to be relative to @tree_view's bin_window if @keyboard_tooltip is %FALSE.
13061  *
13062  * Return value: whether or not the given tooltip context points to a row.
13063  *
13064  * Since: 2.12
13065  */
13066 gboolean
13067 pspp_sheet_view_get_tooltip_context (PsppSheetView   *tree_view,
13068                                    gint          *x,
13069                                    gint          *y,
13070                                    gboolean       keyboard_tip,
13071                                    GtkTreeModel **model,
13072                                    GtkTreePath  **path,
13073                                    GtkTreeIter   *iter)
13074 {
13075   GtkTreePath *tmppath = NULL;
13076
13077   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
13078   g_return_val_if_fail (x != NULL, FALSE);
13079   g_return_val_if_fail (y != NULL, FALSE);
13080
13081   if (keyboard_tip)
13082     {
13083       pspp_sheet_view_get_cursor (tree_view, &tmppath, NULL);
13084
13085       if (!tmppath)
13086         return FALSE;
13087     }
13088   else
13089     {
13090       pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, *x, *y,
13091                                                          x, y);
13092
13093       if (!pspp_sheet_view_get_path_at_pos (tree_view, *x, *y,
13094                                           &tmppath, NULL, NULL, NULL))
13095         return FALSE;
13096     }
13097
13098   if (model)
13099     *model = pspp_sheet_view_get_model (tree_view);
13100
13101   if (iter)
13102     gtk_tree_model_get_iter (pspp_sheet_view_get_model (tree_view),
13103                              iter, tmppath);
13104
13105   if (path)
13106     *path = tmppath;
13107   else
13108     gtk_tree_path_free (tmppath);
13109
13110   return TRUE;
13111 }
13112
13113 static gboolean
13114 pspp_sheet_view_set_tooltip_query_cb (GtkWidget  *widget,
13115                                     gint        x,
13116                                     gint        y,
13117                                     gboolean    keyboard_tip,
13118                                     GtkTooltip *tooltip,
13119                                     gpointer    data)
13120 {
13121   GValue value = { 0, };
13122   GValue transformed = { 0, };
13123   GtkTreeIter iter;
13124   GtkTreePath *path;
13125   GtkTreeModel *model;
13126   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
13127
13128   if (!pspp_sheet_view_get_tooltip_context (PSPP_SHEET_VIEW (widget),
13129                                           &x, &y,
13130                                           keyboard_tip,
13131                                           &model, &path, &iter))
13132     return FALSE;
13133
13134   gtk_tree_model_get_value (model, &iter,
13135                             tree_view->priv->tooltip_column, &value);
13136
13137   g_value_init (&transformed, G_TYPE_STRING);
13138
13139   if (!g_value_transform (&value, &transformed))
13140     {
13141       g_value_unset (&value);
13142       gtk_tree_path_free (path);
13143
13144       return FALSE;
13145     }
13146
13147   g_value_unset (&value);
13148
13149   if (!g_value_get_string (&transformed))
13150     {
13151       g_value_unset (&transformed);
13152       gtk_tree_path_free (path);
13153
13154       return FALSE;
13155     }
13156
13157   gtk_tooltip_set_markup (tooltip, g_value_get_string (&transformed));
13158   pspp_sheet_view_set_tooltip_row (tree_view, tooltip, path);
13159
13160   gtk_tree_path_free (path);
13161   g_value_unset (&transformed);
13162
13163   return TRUE;
13164 }
13165
13166 /**
13167  * pspp_sheet_view_set_tooltip_column:
13168  * @tree_view: a #PsppSheetView
13169  * @column: an integer, which is a valid column number for @tree_view's model
13170  *
13171  * If you only plan to have simple (text-only) tooltips on full rows, you
13172  * can use this function to have #PsppSheetView handle these automatically
13173  * for you. @column should be set to the column in @tree_view's model
13174  * containing the tooltip texts, or -1 to disable this feature.
13175  *
13176  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
13177  * @tree_view will connect a #GtkWidget::query-tooltip signal handler.
13178  *
13179  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
13180  * so &amp;, &lt;, etc have to be escaped in the text.
13181  *
13182  * Since: 2.12
13183  */
13184 void
13185 pspp_sheet_view_set_tooltip_column (PsppSheetView *tree_view,
13186                                   gint         column)
13187 {
13188   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13189
13190   if (column == tree_view->priv->tooltip_column)
13191     return;
13192
13193   if (column == -1)
13194     {
13195       g_signal_handlers_disconnect_by_func (tree_view,
13196                                             pspp_sheet_view_set_tooltip_query_cb,
13197                                             NULL);
13198       gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), FALSE);
13199     }
13200   else
13201     {
13202       if (tree_view->priv->tooltip_column == -1)
13203         {
13204           g_signal_connect (tree_view, "query-tooltip",
13205                             G_CALLBACK (pspp_sheet_view_set_tooltip_query_cb), NULL);
13206           gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), TRUE);
13207         }
13208     }
13209
13210   tree_view->priv->tooltip_column = column;
13211   g_object_notify (G_OBJECT (tree_view), "tooltip-column");
13212 }
13213
13214 /**
13215  * pspp_sheet_view_get_tooltip_column:
13216  * @tree_view: a #PsppSheetView
13217  *
13218  * Returns the column of @tree_view's model which is being used for
13219  * displaying tooltips on @tree_view's rows.
13220  *
13221  * Return value: the index of the tooltip column that is currently being
13222  * used, or -1 if this is disabled.
13223  *
13224  * Since: 2.12
13225  */
13226 gint
13227 pspp_sheet_view_get_tooltip_column (PsppSheetView *tree_view)
13228 {
13229   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
13230
13231   return tree_view->priv->tooltip_column;
13232 }
13233
13234 gboolean
13235 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
13236                                   GValue                *return_accu,
13237                                   const GValue          *handler_return,
13238                                   gpointer               dummy)
13239 {
13240   gboolean continue_emission;
13241   gboolean signal_handled;
13242   
13243   signal_handled = g_value_get_boolean (handler_return);
13244   g_value_set_boolean (return_accu, signal_handled);
13245   continue_emission = !signal_handled;
13246   
13247   return continue_emission;
13248 }
13249
13250
13251 GType
13252 pspp_sheet_view_grid_lines_get_type (void)
13253 {
13254     static GType etype = 0;
13255     if (G_UNLIKELY(etype == 0)) {
13256         static const GEnumValue values[] = {
13257             { PSPP_SHEET_VIEW_GRID_LINES_NONE, "PSPP_SHEET_VIEW_GRID_LINES_NONE", "none" },
13258             { PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL, "PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL", "horizontal" },
13259             { PSPP_SHEET_VIEW_GRID_LINES_VERTICAL, "PSPP_SHEET_VIEW_GRID_LINES_VERTICAL", "vertical" },
13260             { PSPP_SHEET_VIEW_GRID_LINES_BOTH, "PSPP_SHEET_VIEW_GRID_LINES_BOTH", "both" },
13261             { 0, NULL, NULL }
13262         };
13263         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewGridLines"), values);
13264     }
13265     return etype;
13266 }
13267
13268 GType
13269 pspp_sheet_view_special_cells_get_type (void)
13270 {
13271     static GType etype = 0;
13272     if (G_UNLIKELY(etype == 0)) {
13273         static const GEnumValue values[] = {
13274             { PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT, "PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT", "detect" },
13275             { PSPP_SHEET_VIEW_SPECIAL_CELLS_YES, "PSPP_SHEET_VIEW_SPECIAL_CELLS_YES", "yes" },
13276             { PSPP_SHEET_VIEW_SPECIAL_CELLS_NO, "PSPP_SHEET_VIEW_SPECIAL_CELLS_NO", "no" },
13277             { 0, NULL, NULL }
13278         };
13279         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewSpecialCells"), values);
13280     }
13281     return etype;
13282 }