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