Remove set_scroll_adjustment_signal
[pspp] / src / ui / gui / pspp-sheet-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 /* gtktreeview.c
18  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Library General Public
22  * License as published by the Free Software Foundation; either
23  * version 2 of the License, or (at your option) any later version.
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License along with this library; if not, write to the
32  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33  * Boston, MA 02111-1307, USA.
34  */
35
36 #include <config.h>
37
38 #include "ui/gui/pspp-sheet-private.h"
39
40 #include <gtk/gtk.h>
41 #include <gdk/gdk.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <gdk/gdkkeysyms-compat.h>
44 #include <string.h>
45
46 #include "ui/gui/psppire-marshal.h"
47 #include "ui/gui/pspp-sheet-selection.h"
48
49 #define P_(STRING) STRING
50 #define GTK_PARAM_READABLE G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
51 #define GTK_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
52
53 /* Many keyboard shortcuts for Mac are the same as for X
54  * except they use Command key instead of Control (e.g. Cut,
55  * Copy, Paste). This symbol is for those simple cases. */
56 #ifndef GDK_WINDOWING_QUARTZ
57 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_CONTROL_MASK
58 #else
59 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_META_MASK
60 #endif
61
62 #define PSPP_SHEET_VIEW_PRIORITY_VALIDATE (GDK_PRIORITY_REDRAW + 5)
63 #define PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC (PSPP_SHEET_VIEW_PRIORITY_VALIDATE + 2)
64 #define PSPP_SHEET_VIEW_TIME_MS_PER_IDLE 30
65 #define SCROLL_EDGE_SIZE 15
66 #define EXPANDER_EXTRA_PADDING 4
67 #define PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT 5000
68
69 /* The "background" areas of all rows/cells add up to cover the entire tree.
70  * The background includes all inter-row and inter-cell spacing.
71  * The "cell" areas are the cell_area passed in to gtk_cell_renderer_render(),
72  * i.e. just the cells, no spacing.
73  */
74
75 #define BACKGROUND_HEIGHT(tree_view) (tree_view->priv->fixed_height)
76 #define CELL_HEIGHT(tree_view, separator) ((BACKGROUND_HEIGHT (tree_view)) - (separator))
77
78 /* Translate from bin_window coordinates to rbtree (tree coordinates) and
79  * vice versa.
80  */
81 #define TREE_WINDOW_Y_TO_RBTREE_Y(tree_view,y) ((y) + tree_view->priv->dy)
82 #define RBTREE_Y_TO_TREE_WINDOW_Y(tree_view,y) ((y) - tree_view->priv->dy)
83
84 /* This is in bin_window coordinates */
85 #define BACKGROUND_FIRST_PIXEL(tree_view,node) (RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, pspp_sheet_view_node_find_offset (tree_view, (node))))
86 #define CELL_FIRST_PIXEL(tree_view,node,separator) (BACKGROUND_FIRST_PIXEL (tree_view,node) + separator/2)
87
88 #define ROW_HEIGHT(tree_view) \
89   ((tree_view->priv->fixed_height > 0) ? (tree_view->priv->fixed_height) : (tree_view)->priv->expander_size)
90
91
92 typedef struct _PsppSheetViewChild PsppSheetViewChild;
93 struct _PsppSheetViewChild
94 {
95   GtkWidget *widget;
96   gint x;
97   gint y;
98   gint width;
99   gint height;
100 };
101
102
103 typedef struct _TreeViewDragInfo TreeViewDragInfo;
104 struct _TreeViewDragInfo
105 {
106   GdkModifierType start_button_mask;
107   GtkTargetList *_unused_source_target_list;
108   GdkDragAction source_actions;
109
110   GtkTargetList *_unused_dest_target_list;
111
112   guint source_set : 1;
113   guint dest_set : 1;
114 };
115
116
117 /* Signals */
118 enum
119 {
120   ROW_ACTIVATED,
121   COLUMNS_CHANGED,
122   CURSOR_CHANGED,
123   MOVE_CURSOR,
124   SELECT_ALL,
125   UNSELECT_ALL,
126   SELECT_CURSOR_ROW,
127   TOGGLE_CURSOR_ROW,
128   START_INTERACTIVE_SEARCH,
129   LAST_SIGNAL
130 };
131
132 /* Properties */
133 enum {
134   PROP_0,
135   PROP_MODEL,
136   PROP_HADJUSTMENT,
137   PROP_VADJUSTMENT,
138   PROP_HEADERS_VISIBLE,
139   PROP_HEADERS_CLICKABLE,
140   PROP_REORDERABLE,
141   PROP_RULES_HINT,
142   PROP_ENABLE_SEARCH,
143   PROP_SEARCH_COLUMN,
144   PROP_HOVER_SELECTION,
145   PROP_RUBBER_BANDING,
146   PROP_ENABLE_GRID_LINES,
147   PROP_TOOLTIP_COLUMN,
148   PROP_SPECIAL_CELLS,
149   PROP_FIXED_HEIGHT,
150   PROP_FIXED_HEIGHT_SET
151 };
152
153 /* object signals */
154 static void     pspp_sheet_view_finalize             (GObject          *object);
155 static void     pspp_sheet_view_set_property         (GObject         *object,
156                                                     guint            prop_id,
157                                                     const GValue    *value,
158                                                     GParamSpec      *pspec);
159 static void     pspp_sheet_view_get_property         (GObject         *object,
160                                                     guint            prop_id,
161                                                     GValue          *value,
162                                                     GParamSpec      *pspec);
163
164 static void     pspp_sheet_view_dispose              (GObject        *object);
165
166 /* gtkwidget signals */
167 static void     pspp_sheet_view_realize              (GtkWidget        *widget);
168 static void     pspp_sheet_view_unrealize            (GtkWidget        *widget);
169 static void     pspp_sheet_view_map                  (GtkWidget        *widget);
170 static void     pspp_sheet_view_size_request         (GtkWidget        *widget,
171                                                     GtkRequisition   *requisition);
172 static void     pspp_sheet_view_size_allocate        (GtkWidget        *widget,
173                                                     GtkAllocation    *allocation);
174 static gboolean pspp_sheet_view_expose               (GtkWidget        *widget,
175                                                     GdkEventExpose   *event);
176 static gboolean pspp_sheet_view_key_press            (GtkWidget        *widget,
177                                                     GdkEventKey      *event);
178 static gboolean pspp_sheet_view_key_release          (GtkWidget        *widget,
179                                                     GdkEventKey      *event);
180 static gboolean pspp_sheet_view_motion               (GtkWidget        *widget,
181                                                     GdkEventMotion   *event);
182 static gboolean pspp_sheet_view_enter_notify         (GtkWidget        *widget,
183                                                     GdkEventCrossing *event);
184 static gboolean pspp_sheet_view_leave_notify         (GtkWidget        *widget,
185                                                     GdkEventCrossing *event);
186 static gboolean pspp_sheet_view_button_press         (GtkWidget        *widget,
187                                                     GdkEventButton   *event);
188 static gboolean pspp_sheet_view_button_release       (GtkWidget        *widget,
189                                                     GdkEventButton   *event);
190 static gboolean pspp_sheet_view_grab_broken          (GtkWidget          *widget,
191                                                     GdkEventGrabBroken *event);
192
193 static void     pspp_sheet_view_set_focus_child      (GtkContainer     *container,
194                                                     GtkWidget        *child);
195 static gint     pspp_sheet_view_focus_out            (GtkWidget        *widget,
196                                                     GdkEventFocus    *event);
197 static gint     pspp_sheet_view_focus                (GtkWidget        *widget,
198                                                     GtkDirectionType  direction);
199 static void     pspp_sheet_view_grab_focus           (GtkWidget        *widget);
200 static void     pspp_sheet_view_style_set            (GtkWidget        *widget,
201                                                     GtkStyle         *previous_style);
202 static void     pspp_sheet_view_grab_notify          (GtkWidget        *widget,
203                                                     gboolean          was_grabbed);
204 static void     pspp_sheet_view_state_changed        (GtkWidget        *widget,
205                                                     GtkStateType      previous_state);
206
207 /* container signals */
208 static void     pspp_sheet_view_remove               (GtkContainer     *container,
209                                                     GtkWidget        *widget);
210 static void     pspp_sheet_view_forall               (GtkContainer     *container,
211                                                     gboolean          include_internals,
212                                                     GtkCallback       callback,
213                                                     gpointer          callback_data);
214
215 /* Source side drag signals */
216 static void pspp_sheet_view_drag_begin       (GtkWidget        *widget,
217                                             GdkDragContext   *context);
218 static void pspp_sheet_view_drag_end         (GtkWidget        *widget,
219                                             GdkDragContext   *context);
220 static void pspp_sheet_view_drag_data_get    (GtkWidget        *widget,
221                                             GdkDragContext   *context,
222                                             GtkSelectionData *selection_data,
223                                             guint             info,
224                                             guint             time);
225 static void pspp_sheet_view_drag_data_delete (GtkWidget        *widget,
226                                             GdkDragContext   *context);
227
228 /* Target side drag signals */
229 static void     pspp_sheet_view_drag_leave         (GtkWidget        *widget,
230                                                   GdkDragContext   *context,
231                                                   guint             time);
232 static gboolean pspp_sheet_view_drag_motion        (GtkWidget        *widget,
233                                                   GdkDragContext   *context,
234                                                   gint              x,
235                                                   gint              y,
236                                                   guint             time);
237 static gboolean pspp_sheet_view_drag_drop          (GtkWidget        *widget,
238                                                   GdkDragContext   *context,
239                                                   gint              x,
240                                                   gint              y,
241                                                   guint             time);
242 static void     pspp_sheet_view_drag_data_received (GtkWidget        *widget,
243                                                   GdkDragContext   *context,
244                                                   gint              x,
245                                                   gint              y,
246                                                   GtkSelectionData *selection_data,
247                                                   guint             info,
248                                                   guint             time);
249
250 /* tree_model signals */
251 static void pspp_sheet_view_set_adjustments                 (PsppSheetView     *tree_view,
252                                                            GtkAdjustment   *hadj,
253                                                            GtkAdjustment   *vadj);
254 static gboolean pspp_sheet_view_real_move_cursor            (PsppSheetView     *tree_view,
255                                                            GtkMovementStep  step,
256                                                            gint             count);
257 static gboolean pspp_sheet_view_real_select_all             (PsppSheetView     *tree_view);
258 static gboolean pspp_sheet_view_real_unselect_all           (PsppSheetView     *tree_view);
259 static gboolean pspp_sheet_view_real_select_cursor_row      (PsppSheetView     *tree_view,
260                                                              gboolean         start_editing,
261                                                              PsppSheetSelectMode mode);
262 static gboolean pspp_sheet_view_real_toggle_cursor_row      (PsppSheetView     *tree_view);
263 static void pspp_sheet_view_row_changed                     (GtkTreeModel    *model,
264                                                            GtkTreePath     *path,
265                                                            GtkTreeIter     *iter,
266                                                            gpointer         data);
267 static void pspp_sheet_view_row_inserted                    (GtkTreeModel    *model,
268                                                            GtkTreePath     *path,
269                                                            GtkTreeIter     *iter,
270                                                            gpointer         data);
271 static void pspp_sheet_view_row_deleted                     (GtkTreeModel    *model,
272                                                            GtkTreePath     *path,
273                                                            gpointer         data);
274 static void pspp_sheet_view_rows_reordered                  (GtkTreeModel    *model,
275                                                            GtkTreePath     *parent,
276                                                            GtkTreeIter     *iter,
277                                                            gint            *new_order,
278                                                            gpointer         data);
279
280 /* Incremental reflow */
281 static gint validate_row             (PsppSheetView *tree_view,
282                                           int node,
283                                           GtkTreeIter *iter,
284                                           GtkTreePath *path);
285 static void     validate_visible_area    (PsppSheetView *tree_view);
286 static gboolean validate_rows_handler    (PsppSheetView *tree_view);
287 static gboolean presize_handler_callback (gpointer     data);
288 static void     install_presize_handler  (PsppSheetView *tree_view);
289 static void     install_scroll_sync_handler (PsppSheetView *tree_view);
290 static void     pspp_sheet_view_set_top_row   (PsppSheetView *tree_view,
291                                              GtkTreePath *path,
292                                              gint         offset);
293 static void     pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view);
294 static void     pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view);
295 static void     invalidate_empty_focus      (PsppSheetView *tree_view);
296
297 /* Internal functions */
298 static void     pspp_sheet_view_add_move_binding               (GtkBindingSet      *binding_set,
299                                                               guint               keyval,
300                                                               guint               modmask,
301                                                               gboolean            add_shifted_binding,
302                                                               GtkMovementStep     step,
303                                                               gint                count);
304 static void     pspp_sheet_view_queue_draw_path                (PsppSheetView        *tree_view,
305                                                               GtkTreePath        *path,
306                                                               const GdkRectangle *clip_rect);
307 static gint     pspp_sheet_view_new_column_width               (PsppSheetView        *tree_view,
308                                                               gint                i,
309                                                               gint               *x);
310 static void     pspp_sheet_view_adjustment_changed             (GtkAdjustment      *adjustment,
311                                                               PsppSheetView        *tree_view);
312 static void     pspp_sheet_view_clamp_node_visible             (PsppSheetView        *tree_view,
313                                                               int node);
314 static void     pspp_sheet_view_clamp_column_visible           (PsppSheetView        *tree_view,
315                                                               PsppSheetViewColumn  *column,
316                                                               gboolean            focus_to_cell);
317 static gboolean pspp_sheet_view_maybe_begin_dragging_row       (PsppSheetView        *tree_view,
318                                                               GdkEventMotion     *event);
319 static void     pspp_sheet_view_focus_to_cursor                (PsppSheetView        *tree_view);
320 static gboolean pspp_sheet_view_move_cursor_up_down            (PsppSheetView        *tree_view,
321                                                               gint                count,
322                                                                 PsppSheetSelectMode mode);
323 static void     pspp_sheet_view_move_cursor_page_up_down       (PsppSheetView        *tree_view,
324                                                               gint                count,
325                                                                 PsppSheetSelectMode mode);
326 static void     pspp_sheet_view_move_cursor_left_right         (PsppSheetView        *tree_view,
327                                                                 gint                count,
328                                                                 PsppSheetSelectMode mode);
329 static void     pspp_sheet_view_move_cursor_line_start_end     (PsppSheetView        *tree_view,
330                                                                 gint                count,
331                                                                 PsppSheetSelectMode mode);
332 static void     pspp_sheet_view_move_cursor_tab                (PsppSheetView        *tree_view,
333                                                               gint                count);
334 static void     pspp_sheet_view_move_cursor_start_end          (PsppSheetView        *tree_view,
335                                                               gint                count,
336                                                                 PsppSheetSelectMode mode);
337 static void     pspp_sheet_view_real_set_cursor                (PsppSheetView        *tree_view,
338                                                               GtkTreePath        *path,
339                                                               gboolean            clear_and_select,
340                                                               gboolean            clamp_node,
341                                                               PsppSheetSelectMode mode);
342 static gboolean pspp_sheet_view_has_special_cell               (PsppSheetView        *tree_view);
343 static void     pspp_sheet_view_stop_rubber_band               (PsppSheetView        *tree_view);
344 static void     update_prelight                              (PsppSheetView        *tree_view,
345                                                               int                 x,
346                                                               int                 y);
347 static void initialize_fixed_height_mode (PsppSheetView *tree_view);
348
349 /* interactive search */
350 static void     pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view);
351 static void     pspp_sheet_view_search_dialog_hide     (GtkWidget        *search_dialog,
352                                                          PsppSheetView      *tree_view);
353 static void     pspp_sheet_view_search_position_func      (PsppSheetView      *tree_view,
354                                                          GtkWidget        *search_dialog,
355                                                          gpointer          user_data);
356 static void     pspp_sheet_view_search_disable_popdown    (GtkEntry         *entry,
357                                                          GtkMenu          *menu,
358                                                          gpointer          data);
359 #if GTK3_TRANSITION
360 static void     pspp_sheet_view_search_preedit_changed    (GtkIMContext     *im_context,
361                                                          PsppSheetView      *tree_view);
362 #endif
363 static void     pspp_sheet_view_search_activate           (GtkEntry         *entry,
364                                                          PsppSheetView      *tree_view);
365 static gboolean pspp_sheet_view_real_search_enable_popdown(gpointer          data);
366 static void     pspp_sheet_view_search_enable_popdown     (GtkWidget        *widget,
367                                                          gpointer          data);
368 static gboolean pspp_sheet_view_search_delete_event       (GtkWidget        *widget,
369                                                          GdkEventAny      *event,
370                                                          PsppSheetView      *tree_view);
371 static gboolean pspp_sheet_view_search_button_press_event (GtkWidget        *widget,
372                                                          GdkEventButton   *event,
373                                                          PsppSheetView      *tree_view);
374 static gboolean pspp_sheet_view_search_scroll_event       (GtkWidget        *entry,
375                                                          GdkEventScroll   *event,
376                                                          PsppSheetView      *tree_view);
377 static gboolean pspp_sheet_view_search_key_press_event    (GtkWidget        *entry,
378                                                          GdkEventKey      *event,
379                                                          PsppSheetView      *tree_view);
380 static gboolean pspp_sheet_view_search_move               (GtkWidget        *window,
381                                                          PsppSheetView      *tree_view,
382                                                          gboolean          up);
383 static gboolean pspp_sheet_view_search_equal_func         (GtkTreeModel     *model,
384                                                          gint              column,
385                                                          const gchar      *key,
386                                                          GtkTreeIter      *iter,
387                                                          gpointer          search_data);
388 static gboolean pspp_sheet_view_search_iter               (GtkTreeModel     *model,
389                                                          PsppSheetSelection *selection,
390                                                          GtkTreeIter      *iter,
391                                                          const gchar      *text,
392                                                          gint             *count,
393                                                          gint              n);
394 static void     pspp_sheet_view_search_init               (GtkWidget        *entry,
395                                                          PsppSheetView      *tree_view);
396 static void     pspp_sheet_view_put                       (PsppSheetView      *tree_view,
397                                                          GtkWidget        *child_widget,
398                                                          gint              x,
399                                                          gint              y,
400                                                          gint              width,
401                                                          gint              height);
402 static gboolean pspp_sheet_view_start_editing             (PsppSheetView      *tree_view,
403                                                          GtkTreePath      *cursor_path);
404 static gboolean pspp_sheet_view_editable_button_press_event (GtkWidget *,
405                                                              GdkEventButton *,
406                                                              PsppSheetView *);
407 static void pspp_sheet_view_editable_clicked (GtkButton *, PsppSheetView *);
408 static void pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
409                                               PsppSheetViewColumn *column,
410                                               GtkTreePath       *path,
411                                               GtkCellEditable   *cell_editable,
412                                               GdkRectangle      *cell_area,
413                                               GdkEvent          *event,
414                                               guint              flags);
415 static gboolean pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
416                                                              gboolean     keybinding);
417 static gboolean pspp_sheet_view_start_interactive_search      (PsppSheetView *tree_view);
418 static PsppSheetViewColumn *pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
419                                                          PsppSheetViewColumn *column,
420                                                          gint               drop_position);
421 static void
422 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
423                                   PsppSheetViewColumn  *column,
424                                   const GdkRectangle   *background_area,
425                                   gboolean              subtract_focus_rect,
426                                   GdkRectangle         *cell_area);
427 static gint pspp_sheet_view_find_offset (PsppSheetView *tree_view,
428                                          gint height,
429                                          int *new_node);
430
431 /* GtkBuildable */
432 static void pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
433                                                GtkBuilder  *builder,
434                                                GObject     *child,
435                                                const gchar *type);
436 static void pspp_sheet_view_buildable_init      (GtkBuildableIface *iface);
437
438
439 static gboolean scroll_row_timeout                   (gpointer     data);
440 static void     add_scroll_timeout                   (PsppSheetView *tree_view);
441 static void     remove_scroll_timeout                (PsppSheetView *tree_view);
442
443 static guint tree_view_signals [LAST_SIGNAL] = { 0 };
444
445 static GtkBindingSet *edit_bindings;
446
447 \f
448
449 /* GType Methods
450  */
451
452 G_DEFINE_TYPE_WITH_CODE (PsppSheetView, pspp_sheet_view, GTK_TYPE_CONTAINER,
453                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
454                                                 pspp_sheet_view_buildable_init))
455
456 static void
457 pspp_sheet_view_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->expose_event = pspp_sheet_view_expose;
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   cairo_t *bwcr = gdk_cairo_create (tree_view->priv->bin_window);
3882   GdkRectangle Zarea;
3883   GtkAllocation allocation;
3884   gtk_widget_get_allocation (widget, &allocation);
3885
3886   Zarea.x =      0;
3887   Zarea.y =      0;
3888   Zarea.height = allocation.height;
3889
3890   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3891
3892   gtk_widget_style_get (widget,
3893                         "horizontal-separator", &horizontal_separator,
3894                         "vertical-separator", &vertical_separator,
3895                         "allow-rules", &allow_rules,
3896                         "focus-line-width", &focus_line_width,
3897                         "row-ending-details", &row_ending_details,
3898                         NULL);
3899
3900   if (tree_view->priv->row_count == 0)
3901     {
3902       draw_empty_focus (tree_view);
3903       return TRUE;
3904     }
3905
3906 #if GTK3_TRANSITION
3907   /* clip event->area to the visible area */
3908   if (Zarea.height < 0.5)
3909     return TRUE;
3910 #endif
3911
3912   validate_visible_area (tree_view);
3913
3914   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, Zarea.y);
3915
3916   if (new_y < 0)
3917     new_y = 0;
3918   y_offset = -pspp_sheet_view_find_offset (tree_view, new_y, &node);
3919   bin_window_width = 
3920     gdk_window_get_width (tree_view->priv->bin_window);
3921
3922   bin_window_height = 
3923     gdk_window_get_height (tree_view->priv->bin_window);
3924
3925
3926   if (tree_view->priv->height < bin_window_height)
3927     {
3928       gtk_paint_flat_box (gtk_widget_get_style (widget),
3929                           cr,
3930                           gtk_widget_get_state (widget),
3931                           GTK_SHADOW_NONE,
3932                           widget,
3933                           "cell_even",
3934                           0, tree_view->priv->height,
3935                           bin_window_width,
3936                           bin_window_height - tree_view->priv->height);
3937     }
3938
3939   if (node < 0)
3940     return TRUE;
3941
3942   /* find the path for the node */
3943   path = _pspp_sheet_view_find_path ((PsppSheetView *)widget, node);
3944   gtk_tree_model_get_iter (tree_view->priv->model,
3945                            &iter,
3946                            path);
3947   gtk_tree_path_free (path);
3948   
3949   cursor_path = NULL;
3950   drag_dest_path = NULL;
3951
3952   if (tree_view->priv->cursor)
3953     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
3954
3955   if (cursor_path)
3956     _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor);
3957
3958   if (tree_view->priv->drag_dest_row)
3959     drag_dest_path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
3960
3961   if (drag_dest_path)
3962     _pspp_sheet_view_find_node (tree_view, drag_dest_path,
3963                                 &drag_highlight);
3964
3965   draw_vgrid_lines =
3966     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
3967     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3968   draw_hgrid_lines =
3969     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
3970     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3971
3972   if (draw_vgrid_lines || draw_hgrid_lines)
3973     gtk_widget_style_get (widget, "grid-line-width", &grid_line_width, NULL);
3974   
3975   n_visible_columns = 0;
3976   for (list = tree_view->priv->columns; list; list = list->next)
3977     {
3978       if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
3979         continue;
3980       n_visible_columns ++;
3981     }
3982
3983   /* Find the last column */
3984   for (last_column = g_list_last (tree_view->priv->columns);
3985        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
3986        last_column = last_column->prev)
3987     ;
3988
3989   /* and the first */
3990   for (first_column = g_list_first (tree_view->priv->columns);
3991        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
3992        first_column = first_column->next)
3993     ;
3994
3995   /* Actually process the expose event.  To do this, we want to
3996    * start at the first node of the event, and walk the tree in
3997    * order, drawing each successive node.
3998    */
3999
4000   min_y = y_offset;
4001   do
4002     {
4003       gboolean parity;
4004       gboolean is_first = FALSE;
4005       gboolean is_last = FALSE;
4006       gboolean done = FALSE;
4007       gboolean selected;
4008
4009       max_height = ROW_HEIGHT (tree_view);
4010
4011       cell_offset = 0;
4012
4013       background_area.y = y_offset + Zarea.y;
4014       background_area.height = max_height;
4015       max_y = background_area.y + max_height;
4016
4017       flags = 0;
4018
4019       if (node == tree_view->priv->prelight_node)
4020         flags |= GTK_CELL_RENDERER_PRELIT;
4021
4022       selected = pspp_sheet_view_node_is_selected (tree_view, node);
4023
4024       parity = node % 2;
4025
4026       if (tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT)
4027         {
4028           /* we *need* to set cell data on all cells before the call
4029            * to _has_special_cell, else _has_special_cell() does not
4030            * return a correct value.
4031            */
4032           for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4033                list;
4034                list = (rtl ? list->prev : list->next))
4035             {
4036               PsppSheetViewColumn *column = list->data;
4037               pspp_sheet_view_column_cell_set_cell_data (column,
4038                                                          tree_view->priv->model,
4039                                                          &iter);
4040             }
4041
4042           has_special_cell = pspp_sheet_view_has_special_cell (tree_view);
4043         }
4044       else
4045         has_special_cell = tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_YES;
4046
4047       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4048            list;
4049            list = (rtl ? list->prev : list->next))
4050         {
4051           PsppSheetViewColumn *column = list->data;
4052           const gchar *detail = NULL;
4053           gboolean selected_column;
4054           GtkStateType state;
4055
4056           if (!column->visible)
4057             continue;
4058
4059           if (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE)
4060             selected_column = column->selected && column->selectable;
4061           else
4062             selected_column = TRUE;
4063
4064 #if GTK3_TRANSITION
4065           if (cell_offset > Zarea.x + Zarea.width ||
4066               cell_offset + column->width < Zarea.x)
4067             {
4068               cell_offset += column->width;
4069               continue;
4070             }
4071 #endif
4072
4073           if (selected && selected_column)
4074             flags |= GTK_CELL_RENDERER_SELECTED;
4075           else
4076             flags &= ~GTK_CELL_RENDERER_SELECTED;
4077
4078           if (column->show_sort_indicator)
4079             flags |= GTK_CELL_RENDERER_SORTED;
4080           else
4081             flags &= ~GTK_CELL_RENDERER_SORTED;
4082
4083           if (cursor == node)
4084             flags |= GTK_CELL_RENDERER_FOCUSED;
4085           else
4086             flags &= ~GTK_CELL_RENDERER_FOCUSED;
4087
4088           background_area.x = cell_offset;
4089           background_area.width = column->width;
4090
4091           cell_area = background_area;
4092           cell_area.y += vertical_separator / 2;
4093           cell_area.x += horizontal_separator / 2;
4094           cell_area.height -= vertical_separator;
4095           cell_area.width -= horizontal_separator;
4096
4097           if (draw_vgrid_lines)
4098             {
4099               if (list == first_column)
4100                 {
4101                   cell_area.width -= grid_line_width / 2;
4102                 }
4103               else if (list == last_column)
4104                 {
4105                   cell_area.x += grid_line_width / 2;
4106                   cell_area.width -= grid_line_width / 2;
4107                 }
4108               else
4109                 {
4110                   cell_area.x += grid_line_width / 2;
4111                   cell_area.width -= grid_line_width;
4112                 }
4113             }
4114
4115           if (draw_hgrid_lines)
4116             {
4117               cell_area.y += grid_line_width / 2;
4118               cell_area.height -= grid_line_width;
4119             }
4120
4121 #if GTK3_TRANSITION
4122           if (gdk_region_rect_in (event->region, &background_area) == GDK_OVERLAP_RECTANGLE_OUT)
4123             {
4124               cell_offset += column->width;
4125               continue;
4126             }
4127 #endif
4128
4129           pspp_sheet_view_column_cell_set_cell_data (column,
4130                                                      tree_view->priv->model,
4131                                                      &iter);
4132
4133           /* Select the detail for drawing the cell.  relevant
4134            * factors are parity, sortedness, and whether to
4135            * display rules.
4136            */
4137           if (allow_rules && tree_view->priv->has_rules)
4138             {
4139               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4140                   n_visible_columns >= 3)
4141                 {
4142                   if (parity)
4143                     detail = "cell_odd_ruled_sorted";
4144                   else
4145                     detail = "cell_even_ruled_sorted";
4146                 }
4147               else
4148                 {
4149                   if (parity)
4150                     detail = "cell_odd_ruled";
4151                   else
4152                     detail = "cell_even_ruled";
4153                 }
4154             }
4155           else
4156             {
4157               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4158                   n_visible_columns >= 3)
4159                 {
4160                   if (parity)
4161                     detail = "cell_odd_sorted";
4162                   else
4163                     detail = "cell_even_sorted";
4164                 }
4165               else
4166                 {
4167                   if (parity)
4168                     detail = "cell_odd";
4169                   else
4170                     detail = "cell_even";
4171                 }
4172             }
4173
4174           g_assert (detail);
4175
4176           if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
4177             state = GTK_STATE_INSENSITIVE;          
4178           else if (flags & GTK_CELL_RENDERER_SELECTED)
4179             state = GTK_STATE_SELECTED;
4180           else
4181             state = GTK_STATE_NORMAL;
4182
4183           /* Draw background */
4184           if (row_ending_details)
4185             {
4186               char new_detail[128];
4187
4188               is_first = (rtl ? !list->next : !list->prev);
4189               is_last = (rtl ? !list->prev : !list->next);
4190
4191               /* (I don't like the snprintfs either, but couldn't find a
4192                * less messy way).
4193                */
4194               if (is_first && is_last)
4195                 g_snprintf (new_detail, 127, "%s", detail);
4196               else if (is_first)
4197                 g_snprintf (new_detail, 127, "%s_start", detail);
4198               else if (is_last)
4199                 g_snprintf (new_detail, 127, "%s_end", detail);
4200               else
4201                 g_snprintf (new_detail, 128, "%s_middle", detail);
4202
4203               gtk_paint_flat_box (gtk_widget_get_style (widget),
4204                                   cr,
4205                                   state,
4206                                   GTK_SHADOW_NONE,
4207                                   widget,
4208                                   new_detail,
4209                                   background_area.x,
4210                                   background_area.y,
4211                                   background_area.width,
4212                                   background_area.height);
4213             }
4214           else
4215             {
4216               gtk_paint_flat_box (gtk_widget_get_style (widget),
4217                                   cr,
4218                                   state,
4219                                   GTK_SHADOW_NONE,
4220                                   widget,
4221                                   detail,
4222                                   background_area.x,
4223                                   background_area.y,
4224                                   background_area.width,
4225                                   background_area.height);
4226             }
4227
4228           if (draw_hgrid_lines)
4229             {
4230               cairo_set_line_width (cr, 1.0);
4231               cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
4232
4233               if (background_area.y >= 0)
4234                 {
4235 #if GTK3_TRANSITION
4236                   gdk_draw_line (event->window,
4237                                  tree_view->priv->grid_line_gc[widget->state],
4238                                  background_area.x, background_area.y,
4239                                  background_area.x + background_area.width,
4240                                  background_area.y);
4241 #else
4242                   cairo_move_to (cr, background_area.x, background_area.y - 0.5);
4243                   cairo_line_to (cr, background_area.x + background_area.width,
4244                                  background_area.y - 0.5);
4245 #endif
4246                 }
4247
4248               if (y_offset + max_height >= Zarea.height - 0.5)
4249                 {
4250 #if GTK3_TRANSITION
4251                   gdk_draw_line (event->window,
4252                                  tree_view->priv->grid_line_gc[widget->state],
4253                                  background_area.x, background_area.y + max_height,
4254                                  background_area.x + background_area.width,
4255                                  background_area.y + max_height);
4256 #else
4257
4258                   cairo_move_to (cr, background_area.x, background_area.y + max_height - 0.5);
4259                   cairo_line_to (cr, background_area.x + background_area.width,
4260                                  background_area.y + max_height - 0.5);
4261 #endif
4262                 }
4263               cairo_stroke (cr);
4264             }
4265
4266           _pspp_sheet_view_column_cell_render (column,
4267                                                cr,
4268                                                &background_area,
4269                                                &cell_area,
4270                                                flags);
4271
4272           if (node == cursor && has_special_cell &&
4273               ((column == tree_view->priv->focus_column &&
4274                 PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4275                 gtk_widget_has_focus (widget)) ||
4276                (column == tree_view->priv->edited_column)))
4277             {
4278               _pspp_sheet_view_column_cell_draw_focus (column,
4279                                                        cr,
4280                                                      &background_area,
4281                                                      &cell_area,
4282                                                      flags);
4283             }
4284
4285           cell_offset += column->width;
4286         }
4287
4288       if (cell_offset < Zarea.x)
4289         {
4290           gtk_paint_flat_box (gtk_widget_get_style (widget),
4291                               cr,
4292                               GTK_STATE_NORMAL,
4293                               GTK_SHADOW_NONE,
4294                               widget,
4295                               "base",
4296                               cell_offset,
4297                               background_area.y,
4298                               Zarea.x - cell_offset,
4299                               background_area.height);
4300         }
4301
4302       if (node == drag_highlight)
4303         {
4304           /* Draw indicator for the drop
4305            */
4306           gint highlight_y = -1;
4307           int node = -1;
4308           gint width;
4309
4310           switch (tree_view->priv->drag_dest_pos)
4311             {
4312             case PSPP_SHEET_VIEW_DROP_BEFORE:
4313               highlight_y = background_area.y - 1;
4314               if (highlight_y < 0)
4315                       highlight_y = 0;
4316               break;
4317
4318             case PSPP_SHEET_VIEW_DROP_AFTER:
4319               highlight_y = background_area.y + background_area.height - 1;
4320               break;
4321
4322             case PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE:
4323             case PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER:
4324               _pspp_sheet_view_find_node (tree_view, drag_dest_path, &node);
4325
4326               if (node < 0)
4327                 break;
4328               width = gdk_window_get_width (tree_view->priv->bin_window);
4329
4330               if (row_ending_details)
4331                 gtk_paint_focus (gtk_widget_get_style (widget),
4332                                  bwcr,
4333                                  gtk_widget_get_state (widget),
4334                                  widget,
4335                                  (is_first
4336                                   ? (is_last ? "treeview-drop-indicator" : "treeview-drop-indicator-left" )
4337                                   : (is_last ? "treeview-drop-indicator-right" : "tree-view-drop-indicator-middle" )),
4338                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4339                                  - focus_line_width / 2,
4340                                  width, ROW_HEIGHT (tree_view)
4341                                - focus_line_width + 1);
4342               else
4343                 gtk_paint_focus (gtk_widget_get_style (widget),
4344                                  bwcr,
4345                                  gtk_widget_get_state (widget),
4346                                  widget,
4347                                  "treeview-drop-indicator",
4348                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4349                                  - focus_line_width / 2,
4350                                  width, ROW_HEIGHT (tree_view)
4351                                  - focus_line_width + 1);
4352               break;
4353             }
4354
4355 #if GTK3_TRANSITION
4356           if (highlight_y >= 0)
4357             {
4358               gdk_draw_line (event->window,
4359                              widget->style->fg_gc[gtk_widget_get_state (widget)],
4360                              0,
4361                              highlight_y,
4362                              rtl ? 0 : bin_window_width,
4363                              highlight_y);
4364             }
4365 #endif
4366         }
4367
4368       /* draw the big row-spanning focus rectangle, if needed */
4369       if (!has_special_cell && node == cursor &&
4370           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4371           gtk_widget_has_focus (widget))
4372         {
4373           gint tmp_y, tmp_height;
4374           gint width;
4375           GtkStateType focus_rect_state;
4376
4377           focus_rect_state =
4378             flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
4379             (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
4380              (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE :
4381               GTK_STATE_NORMAL));
4382
4383           width = gdk_window_get_width (tree_view->priv->bin_window);
4384           
4385           if (draw_hgrid_lines)
4386             {
4387               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node) + grid_line_width / 2;
4388               tmp_height = ROW_HEIGHT (tree_view) - grid_line_width;
4389             }
4390           else
4391             {
4392               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node);
4393               tmp_height = ROW_HEIGHT (tree_view);
4394             }
4395
4396           if (row_ending_details)
4397             gtk_paint_focus (gtk_widget_get_style (widget),
4398                              bwcr,
4399                              focus_rect_state,
4400                              widget,
4401                              (is_first
4402                               ? (is_last ? "treeview" : "treeview-left" )
4403                               : (is_last ? "treeview-right" : "treeview-middle" )),
4404                              0, tmp_y,
4405                              width, tmp_height);
4406           else
4407             gtk_paint_focus (gtk_widget_get_style (widget),
4408                              bwcr,
4409                              focus_rect_state,
4410                              widget,
4411                              "treeview",
4412                              0, tmp_y,
4413                              width, tmp_height);
4414         }
4415
4416       y_offset += max_height;
4417
4418       do
4419         {
4420           node = pspp_sheet_view_node_next (tree_view, node);
4421           if (node >= 0)
4422             {
4423               gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
4424               done = TRUE;
4425
4426               /* Sanity Check! */
4427               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
4428             }
4429           else
4430             goto done;
4431         }
4432       while (!done);
4433     }
4434   while (y_offset < Zarea.height);
4435
4436 done:
4437   pspp_sheet_view_draw_vertical_grid_lines (tree_view, cr, n_visible_columns,
4438                                    min_y, max_y);
4439
4440 #if GTK3_TRANSITION
4441  if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
4442    {
4443      GdkRectangle *rectangles;
4444      gint n_rectangles;
4445
4446      gdk_region_get_rectangles (event->region,
4447                                 &rectangles,
4448                                 &n_rectangles);
4449
4450      while (n_rectangles--)
4451        pspp_sheet_view_paint_rubber_band (tree_view, &rectangles[n_rectangles]);
4452
4453      g_free (rectangles);
4454    }
4455 #endif
4456
4457   if (cursor_path)
4458     gtk_tree_path_free (cursor_path);
4459
4460   if (drag_dest_path)
4461     gtk_tree_path_free (drag_dest_path);
4462
4463   return FALSE;
4464 }
4465
4466 static gboolean
4467 pspp_sheet_view_expose (GtkWidget      *widget,
4468                         GdkEventExpose *event)
4469 {
4470   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
4471   cairo_t *cr = gdk_cairo_create (event->window);
4472
4473   if (event->window == tree_view->priv->bin_window)
4474     {
4475       gboolean retval;
4476       GList *tmp_list;
4477
4478       retval = pspp_sheet_view_bin_expose (widget, event);
4479
4480       /* We can't just chain up to Container::expose as it will try to send the
4481        * event to the headers, so we handle propagating it to our children
4482        * (eg. widgets being edited) ourselves.
4483        */
4484       tmp_list = tree_view->priv->children;
4485       while (tmp_list)
4486         {
4487           PsppSheetViewChild *child = tmp_list->data;
4488           tmp_list = tmp_list->next;
4489
4490           gtk_container_propagate_draw (GTK_CONTAINER (tree_view), child->widget, cr);
4491         }
4492
4493       return retval;
4494     }
4495   else if (event->window == tree_view->priv->header_window)
4496     {
4497       gint n_visible_columns;
4498       GList *list;
4499
4500       gtk_paint_flat_box (gtk_widget_get_style (widget),
4501                           cr,
4502                           GTK_STATE_NORMAL,
4503                           GTK_SHADOW_NONE,
4504                           widget,
4505                           "cell_odd",
4506                           event->area.x,
4507                           event->area.y,
4508                           event->area.width,
4509                           event->area.height);
4510
4511       for (list = tree_view->priv->columns; list != NULL; list = list->next)
4512         {
4513           PsppSheetViewColumn *column = list->data;
4514
4515           if (column == tree_view->priv->drag_column || !column->visible)
4516             continue;
4517
4518           if (span_intersects (column->allocation.x, column->allocation.width,
4519                                event->area.x, event->area.width)
4520               && column->button != NULL)
4521             gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
4522                                             column->button, event);
4523         }
4524
4525       n_visible_columns = 0;
4526       for (list = tree_view->priv->columns; list; list = list->next)
4527         {
4528           if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
4529             continue;
4530           n_visible_columns ++;
4531         }
4532       pspp_sheet_view_draw_vertical_grid_lines (tree_view,
4533                                                 cr,
4534                                                 n_visible_columns,
4535                                                 event->area.y,
4536                                                 event->area.height);
4537     }
4538   else if (event->window == tree_view->priv->drag_window)
4539     {
4540       gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
4541                                       tree_view->priv->drag_column->button,
4542                                       event);
4543     }
4544   return TRUE;
4545 }
4546
4547 enum
4548 {
4549   DROP_HOME,
4550   DROP_RIGHT,
4551   DROP_LEFT,
4552   DROP_END
4553 };
4554
4555 /* returns 0x1 when no column has been found -- yes it's hackish */
4556 static PsppSheetViewColumn *
4557 pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
4558                                PsppSheetViewColumn *column,
4559                                gint               drop_position)
4560 {
4561   PsppSheetViewColumn *left_column = NULL;
4562   PsppSheetViewColumn *cur_column = NULL;
4563   GList *tmp_list;
4564
4565   if (!column->reorderable)
4566     return (PsppSheetViewColumn *)0x1;
4567
4568   switch (drop_position)
4569     {
4570       case DROP_HOME:
4571         /* find first column where we can drop */
4572         tmp_list = tree_view->priv->columns;
4573         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
4574           return (PsppSheetViewColumn *)0x1;
4575
4576         while (tmp_list)
4577           {
4578             g_assert (tmp_list);
4579
4580             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4581             tmp_list = tmp_list->next;
4582
4583             if (left_column && left_column->visible == FALSE)
4584               continue;
4585
4586             if (!tree_view->priv->column_drop_func)
4587               return left_column;
4588
4589             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4590               {
4591                 left_column = cur_column;
4592                 continue;
4593               }
4594
4595             return left_column;
4596           }
4597
4598         if (!tree_view->priv->column_drop_func)
4599           return left_column;
4600
4601         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
4602           return left_column;
4603         else
4604           return (PsppSheetViewColumn *)0x1;
4605         break;
4606
4607       case DROP_RIGHT:
4608         /* find first column after column where we can drop */
4609         tmp_list = tree_view->priv->columns;
4610
4611         for (; tmp_list; tmp_list = tmp_list->next)
4612           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
4613             break;
4614
4615         if (!tmp_list || !tmp_list->next)
4616           return (PsppSheetViewColumn *)0x1;
4617
4618         tmp_list = tmp_list->next;
4619         left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4620         tmp_list = tmp_list->next;
4621
4622         while (tmp_list)
4623           {
4624             g_assert (tmp_list);
4625
4626             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4627             tmp_list = tmp_list->next;
4628
4629             if (left_column && left_column->visible == FALSE)
4630               {
4631                 left_column = cur_column;
4632                 if (tmp_list)
4633                   tmp_list = tmp_list->next;
4634                 continue;
4635               }
4636
4637             if (!tree_view->priv->column_drop_func)
4638               return left_column;
4639
4640             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4641               {
4642                 left_column = cur_column;
4643                 continue;
4644               }
4645
4646             return left_column;
4647           }
4648
4649         if (!tree_view->priv->column_drop_func)
4650           return left_column;
4651
4652         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
4653           return left_column;
4654         else
4655           return (PsppSheetViewColumn *)0x1;
4656         break;
4657
4658       case DROP_LEFT:
4659         /* find first column before column where we can drop */
4660         tmp_list = tree_view->priv->columns;
4661
4662         for (; tmp_list; tmp_list = tmp_list->next)
4663           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
4664             break;
4665
4666         if (!tmp_list || !tmp_list->prev)
4667           return (PsppSheetViewColumn *)0x1;
4668
4669         tmp_list = tmp_list->prev;
4670         cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4671         tmp_list = tmp_list->prev;
4672
4673         while (tmp_list)
4674           {
4675             g_assert (tmp_list);
4676
4677             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4678
4679             if (left_column && !left_column->visible)
4680               {
4681                 /*if (!tmp_list->prev)
4682                   return (PsppSheetViewColumn *)0x1;
4683                   */
4684 /*
4685                 cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->prev->data);
4686                 tmp_list = tmp_list->prev->prev;
4687                 continue;*/
4688
4689                 cur_column = left_column;
4690                 if (tmp_list)
4691                   tmp_list = tmp_list->prev;
4692                 continue;
4693               }
4694
4695             if (!tree_view->priv->column_drop_func)
4696               return left_column;
4697
4698             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4699               return left_column;
4700
4701             cur_column = left_column;
4702             tmp_list = tmp_list->prev;
4703           }
4704
4705         if (!tree_view->priv->column_drop_func)
4706           return NULL;
4707
4708         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
4709           return NULL;
4710         else
4711           return (PsppSheetViewColumn *)0x1;
4712         break;
4713
4714       case DROP_END:
4715         /* same as DROP_HOME case, but doing it backwards */
4716         tmp_list = g_list_last (tree_view->priv->columns);
4717         cur_column = NULL;
4718
4719         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
4720           return (PsppSheetViewColumn *)0x1;
4721
4722         while (tmp_list)
4723           {
4724             g_assert (tmp_list);
4725
4726             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
4727
4728             if (left_column && !left_column->visible)
4729               {
4730                 cur_column = left_column;
4731                 tmp_list = tmp_list->prev;
4732               }
4733
4734             if (!tree_view->priv->column_drop_func)
4735               return left_column;
4736
4737             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
4738               return left_column;
4739
4740             cur_column = left_column;
4741             tmp_list = tmp_list->prev;
4742           }
4743
4744         if (!tree_view->priv->column_drop_func)
4745           return NULL;
4746
4747         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
4748           return NULL;
4749         else
4750           return (PsppSheetViewColumn *)0x1;
4751         break;
4752     }
4753
4754   return (PsppSheetViewColumn *)0x1;
4755 }
4756
4757 static gboolean
4758 pspp_sheet_view_key_press (GtkWidget   *widget,
4759                          GdkEventKey *event)
4760 {
4761   PsppSheetView *tree_view = (PsppSheetView *) widget;
4762
4763   if (tree_view->priv->rubber_band_status)
4764     {
4765       if (event->keyval == GDK_Escape)
4766         pspp_sheet_view_stop_rubber_band (tree_view);
4767
4768       return TRUE;
4769     }
4770
4771   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
4772     {
4773       if (event->keyval == GDK_Escape)
4774         {
4775           tree_view->priv->cur_reorder = NULL;
4776           pspp_sheet_view_button_release_drag_column (widget, NULL);
4777         }
4778       return TRUE;
4779     }
4780
4781   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
4782     {
4783       GList *focus_column;
4784       gboolean rtl;
4785
4786       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
4787
4788       for (focus_column = tree_view->priv->columns;
4789            focus_column;
4790            focus_column = focus_column->next)
4791         {
4792           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4793
4794           if (column->button && gtk_widget_has_focus (column->button))
4795             break;
4796         }
4797
4798       if (focus_column &&
4799           (event->state & GDK_SHIFT_MASK) && (event->state & GDK_MOD1_MASK) &&
4800           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
4801            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right))
4802         {
4803           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4804
4805           if (!column->resizable)
4806             {
4807               gtk_widget_error_bell (widget);
4808               return TRUE;
4809             }
4810
4811           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
4812               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
4813             {
4814               gint old_width = column->resized_width;
4815
4816               column->resized_width = MAX (column->resized_width,
4817                                            column->width);
4818               column->resized_width -= 2;
4819               if (column->resized_width < 0)
4820                 column->resized_width = 0;
4821
4822               if (column->min_width == -1)
4823                 column->resized_width = MAX (column->button_request,
4824                                              column->resized_width);
4825               else
4826                 column->resized_width = MAX (column->min_width,
4827                                              column->resized_width);
4828
4829               if (column->max_width != -1)
4830                 column->resized_width = MIN (column->resized_width,
4831                                              column->max_width);
4832
4833               column->use_resized_width = TRUE;
4834
4835               if (column->resized_width != old_width)
4836                 gtk_widget_queue_resize (widget);
4837               else
4838                 gtk_widget_error_bell (widget);
4839             }
4840           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
4841                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
4842             {
4843               gint old_width = column->resized_width;
4844
4845               column->resized_width = MAX (column->resized_width,
4846                                            column->width);
4847               column->resized_width += 2;
4848
4849               if (column->max_width != -1)
4850                 column->resized_width = MIN (column->resized_width,
4851                                              column->max_width);
4852
4853               column->use_resized_width = TRUE;
4854
4855               if (column->resized_width != old_width)
4856                 gtk_widget_queue_resize (widget);
4857               else
4858                 gtk_widget_error_bell (widget);
4859             }
4860
4861           return TRUE;
4862         }
4863
4864       if (focus_column &&
4865           (event->state & GDK_MOD1_MASK) &&
4866           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
4867            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
4868            || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
4869            || event->keyval == GDK_End || event->keyval == GDK_KP_End))
4870         {
4871           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
4872
4873           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
4874               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
4875             {
4876               PsppSheetViewColumn *col;
4877               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_LEFT);
4878               if (col != (PsppSheetViewColumn *)0x1)
4879                 pspp_sheet_view_move_column_after (tree_view, column, col);
4880               else
4881                 gtk_widget_error_bell (widget);
4882             }
4883           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
4884                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
4885             {
4886               PsppSheetViewColumn *col;
4887               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_RIGHT);
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 == GDK_Home || event->keyval == GDK_KP_Home)
4894             {
4895               PsppSheetViewColumn *col;
4896               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_HOME);
4897               if (col != (PsppSheetViewColumn *)0x1)
4898                 pspp_sheet_view_move_column_after (tree_view, column, col);
4899               else
4900                 gtk_widget_error_bell (widget);
4901             }
4902           else if (event->keyval == GDK_End || event->keyval == GDK_KP_End)
4903             {
4904               PsppSheetViewColumn *col;
4905               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_END);
4906               if (col != (PsppSheetViewColumn *)0x1)
4907                 pspp_sheet_view_move_column_after (tree_view, column, col);
4908               else
4909                 gtk_widget_error_bell (widget);
4910             }
4911
4912           return TRUE;
4913         }
4914     }
4915
4916   /* Chain up to the parent class.  It handles the keybindings. */
4917   if (GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_press_event (widget, event))
4918     return TRUE;
4919
4920   if (tree_view->priv->search_entry_avoid_unhandled_binding)
4921     {
4922       tree_view->priv->search_entry_avoid_unhandled_binding = FALSE;
4923       return FALSE;
4924     }
4925
4926   /* We pass the event to the search_entry.  If its text changes, then we start
4927    * the typeahead find capabilities. */
4928   if (gtk_widget_has_focus (GTK_WIDGET (tree_view))
4929       && tree_view->priv->enable_search
4930       && !tree_view->priv->search_custom_entry_set)
4931     {
4932       GdkEvent *new_event;
4933       char *old_text;
4934       const char *new_text;
4935       gboolean retval;
4936       GdkScreen *screen;
4937       gboolean text_modified;
4938       gulong popup_menu_id;
4939
4940       pspp_sheet_view_ensure_interactive_directory (tree_view);
4941
4942       /* Make a copy of the current text */
4943       old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry)));
4944       new_event = gdk_event_copy ((GdkEvent *) event);
4945       g_object_unref (((GdkEventKey *) new_event)->window);
4946       ((GdkEventKey *) new_event)->window = g_object_ref (gtk_widget_get_window (tree_view->priv->search_window));
4947       gtk_widget_realize (tree_view->priv->search_window);
4948
4949       popup_menu_id = g_signal_connect (tree_view->priv->search_entry, 
4950                                         "popup-menu", G_CALLBACK (gtk_true),
4951                                         NULL);
4952
4953       /* Move the entry off screen */
4954       screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
4955       gtk_window_move (GTK_WINDOW (tree_view->priv->search_window),
4956                        gdk_screen_get_width (screen) + 1,
4957                        gdk_screen_get_height (screen) + 1);
4958       gtk_widget_show (tree_view->priv->search_window);
4959
4960       /* Send the event to the window.  If the preedit_changed signal is emitted
4961        * during this event, we will set priv->imcontext_changed  */
4962       tree_view->priv->imcontext_changed = FALSE;
4963       retval = gtk_widget_event (tree_view->priv->search_window, new_event);
4964       gdk_event_free (new_event);
4965       gtk_widget_hide (tree_view->priv->search_window);
4966
4967       g_signal_handler_disconnect (tree_view->priv->search_entry, 
4968                                    popup_menu_id);
4969
4970       /* We check to make sure that the entry tried to handle the text, and that
4971        * the text has changed.
4972        */
4973       new_text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
4974       text_modified = strcmp (old_text, new_text) != 0;
4975       g_free (old_text);
4976       if (tree_view->priv->imcontext_changed ||    /* we're in a preedit */
4977           (retval && text_modified))               /* ...or the text was modified */
4978         {
4979           if (pspp_sheet_view_real_start_interactive_search (tree_view, FALSE))
4980             {
4981               gtk_widget_grab_focus (GTK_WIDGET (tree_view));
4982               return TRUE;
4983             }
4984           else
4985             {
4986               gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
4987               return FALSE;
4988             }
4989         }
4990     }
4991
4992   return FALSE;
4993 }
4994
4995 static gboolean
4996 pspp_sheet_view_key_release (GtkWidget   *widget,
4997                            GdkEventKey *event)
4998 {
4999   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
5000
5001   if (tree_view->priv->rubber_band_status)
5002     return TRUE;
5003
5004   return GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_release_event (widget, event);
5005 }
5006
5007 /* FIXME Is this function necessary? Can I get an enter_notify event
5008  * w/o either an expose event or a mouse motion event?
5009  */
5010 static gboolean
5011 pspp_sheet_view_enter_notify (GtkWidget        *widget,
5012                             GdkEventCrossing *event)
5013 {
5014   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
5015   int node;
5016   gint new_y;
5017
5018   /* Sanity check it */
5019   if (event->window != tree_view->priv->bin_window)
5020     return FALSE;
5021
5022   if (tree_view->priv->row_count == 0)
5023     return FALSE;
5024
5025   if (event->mode == GDK_CROSSING_GRAB ||
5026       event->mode == GDK_CROSSING_GTK_GRAB ||
5027       event->mode == GDK_CROSSING_GTK_UNGRAB ||
5028       event->mode == GDK_CROSSING_STATE_CHANGED)
5029     return TRUE;
5030
5031   /* find the node internally */
5032   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
5033   if (new_y < 0)
5034     new_y = 0;
5035   pspp_sheet_view_find_offset (tree_view, new_y, &node);
5036
5037   tree_view->priv->event_last_x = event->x;
5038   tree_view->priv->event_last_y = event->y;
5039
5040   prelight_or_select (tree_view, node, event->x, event->y);
5041
5042   return TRUE;
5043 }
5044
5045 static gboolean
5046 pspp_sheet_view_leave_notify (GtkWidget        *widget,
5047                             GdkEventCrossing *event)
5048 {
5049   PsppSheetView *tree_view;
5050
5051   if (event->mode == GDK_CROSSING_GRAB)
5052     return TRUE;
5053
5054   tree_view = PSPP_SHEET_VIEW (widget);
5055
5056   if (tree_view->priv->prelight_node >= 0)
5057     _pspp_sheet_view_queue_draw_node (tree_view,
5058                                    tree_view->priv->prelight_node,
5059                                    NULL);
5060
5061   tree_view->priv->event_last_x = -10000;
5062   tree_view->priv->event_last_y = -10000;
5063
5064   prelight_or_select (tree_view,
5065                       -1,
5066                       -1000, -1000); /* coords not possibly over an arrow */
5067
5068   return TRUE;
5069 }
5070
5071
5072 static gint
5073 pspp_sheet_view_focus_out (GtkWidget     *widget,
5074                          GdkEventFocus *event)
5075 {
5076   PsppSheetView *tree_view;
5077
5078   tree_view = PSPP_SHEET_VIEW (widget);
5079
5080   gtk_widget_queue_draw (widget);
5081
5082   /* destroy interactive search dialog */
5083   if (tree_view->priv->search_window)
5084     pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
5085
5086   return FALSE;
5087 }
5088
5089
5090 /* Incremental Reflow
5091  */
5092
5093 static void
5094 pspp_sheet_view_node_queue_redraw (PsppSheetView *tree_view,
5095                                  int node)
5096 {
5097   GtkAllocation allocation;
5098   gint y = pspp_sheet_view_node_find_offset (tree_view, node)
5099     - gtk_adjustment_get_value (tree_view->priv->vadjustment)
5100     + TREE_VIEW_HEADER_HEIGHT (tree_view);
5101
5102   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
5103
5104   gtk_widget_queue_draw_area (GTK_WIDGET (tree_view),
5105                               0, y,
5106                               allocation.width,
5107                               tree_view->priv->fixed_height);
5108 }
5109
5110 static gboolean
5111 node_is_visible (PsppSheetView *tree_view,
5112                  int node)
5113 {
5114   int y;
5115   int height;
5116
5117   y = pspp_sheet_view_node_find_offset (tree_view, node);
5118   height = ROW_HEIGHT (tree_view);
5119
5120   if (y >= gtk_adjustment_get_value (tree_view->priv->vadjustment) &&
5121       y + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
5122                      + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5123     return TRUE;
5124
5125   return FALSE;
5126 }
5127
5128 /* Returns the row height. */
5129 static gint
5130 validate_row (PsppSheetView *tree_view,
5131               int node,
5132               GtkTreeIter *iter,
5133               GtkTreePath *path)
5134 {
5135   PsppSheetViewColumn *column;
5136   GList *list, *first_column, *last_column;
5137   gint height = 0;
5138   gint horizontal_separator;
5139   gint vertical_separator;
5140   gint focus_line_width;
5141   gboolean draw_vgrid_lines, draw_hgrid_lines;
5142   gint focus_pad;
5143   gint grid_line_width;
5144   gboolean wide_separators;
5145   gint separator_height;
5146
5147   gtk_widget_style_get (GTK_WIDGET (tree_view),
5148                         "focus-padding", &focus_pad,
5149                         "focus-line-width", &focus_line_width,
5150                         "horizontal-separator", &horizontal_separator,
5151                         "vertical-separator", &vertical_separator,
5152                         "grid-line-width", &grid_line_width,
5153                         "wide-separators",  &wide_separators,
5154                         "separator-height", &separator_height,
5155                         NULL);
5156   
5157   draw_vgrid_lines =
5158     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
5159     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5160   draw_hgrid_lines =
5161     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
5162     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5163
5164   for (last_column = g_list_last (tree_view->priv->columns);
5165        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
5166        last_column = last_column->prev)
5167     ;
5168
5169   for (first_column = g_list_first (tree_view->priv->columns);
5170        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
5171        first_column = first_column->next)
5172     ;
5173
5174   for (list = tree_view->priv->columns; list; list = list->next)
5175     {
5176       gint tmp_width;
5177       gint tmp_height;
5178
5179       column = list->data;
5180
5181       if (! column->visible)
5182         continue;
5183
5184       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, iter);
5185       pspp_sheet_view_column_cell_get_size (column,
5186                                           NULL, NULL, NULL,
5187                                           &tmp_width, &tmp_height);
5188
5189       tmp_height += vertical_separator;
5190       height = MAX (height, tmp_height);
5191
5192       tmp_width = tmp_width + horizontal_separator;
5193
5194       if (draw_vgrid_lines)
5195         {
5196           if (list->data == first_column || list->data == last_column)
5197             tmp_width += grid_line_width / 2.0;
5198           else
5199             tmp_width += grid_line_width;
5200         }
5201
5202       if (tmp_width > column->requested_width)
5203         column->requested_width = tmp_width;
5204     }
5205
5206   if (draw_hgrid_lines)
5207     height += grid_line_width;
5208
5209   tree_view->priv->post_validation_flag = TRUE;
5210   return height;
5211 }
5212
5213
5214 static void
5215 validate_visible_area (PsppSheetView *tree_view)
5216 {
5217   GtkTreePath *path = NULL;
5218   GtkTreePath *above_path = NULL;
5219   GtkTreeIter iter;
5220   int node = -1;
5221   gboolean size_changed = FALSE;
5222   gint total_height;
5223   gint area_above = 0;
5224   gint area_below = 0;
5225   GtkAllocation allocation;
5226
5227   if (tree_view->priv->row_count == 0)
5228     return;
5229
5230   if (tree_view->priv->scroll_to_path == NULL)
5231     return;
5232
5233   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
5234
5235   total_height = allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
5236
5237   if (total_height == 0)
5238     return;
5239
5240   path = gtk_tree_row_reference_get_path (tree_view->priv->scroll_to_path);
5241   if (path)
5242     {
5243       /* we are going to scroll, and will update dy */
5244       _pspp_sheet_view_find_node (tree_view, path, &node);
5245       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5246
5247       if (tree_view->priv->scroll_to_use_align)
5248         {
5249           gint height = ROW_HEIGHT (tree_view);
5250           area_above = (total_height - height) *
5251             tree_view->priv->scroll_to_row_align;
5252           area_below = total_height - area_above - height;
5253           area_above = MAX (area_above, 0);
5254           area_below = MAX (area_below, 0);
5255         }
5256       else
5257         {
5258           /* two cases:
5259            * 1) row not visible
5260            * 2) row visible
5261            */
5262           gint dy;
5263           gint height = ROW_HEIGHT (tree_view);
5264
5265           dy = pspp_sheet_view_node_find_offset (tree_view, node);
5266
5267           if (dy >= gtk_adjustment_get_value (tree_view->priv->vadjustment) &&
5268               dy + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
5269                               + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5270             {
5271               /* row visible: keep the row at the same position */
5272               area_above = dy - gtk_adjustment_get_value (tree_view->priv->vadjustment);
5273               area_below = (gtk_adjustment_get_value (tree_view->priv->vadjustment) +
5274                             gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5275                 - dy - height;
5276             }
5277           else
5278             {
5279               /* row not visible */
5280               if (dy >= 0
5281                   && dy + height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5282                 {
5283                   /* row at the beginning -- fixed */
5284                   area_above = dy;
5285                   area_below = gtk_adjustment_get_page_size (tree_view->priv->vadjustment)
5286                     - area_above - height;
5287                 }
5288               else if (dy >= (gtk_adjustment_get_upper (tree_view->priv->vadjustment) -
5289                               gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
5290                 {
5291                   /* row at the end -- fixed */
5292                   area_above = dy - (gtk_adjustment_get_upper (tree_view->priv->vadjustment) -
5293                                      gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
5294                   area_below = gtk_adjustment_get_page_size (tree_view->priv->vadjustment) -
5295                     area_above - height;
5296
5297                   if (area_below < 0)
5298                     {
5299                       area_above = gtk_adjustment_get_page_size (tree_view->priv->vadjustment) - height;
5300                       area_below = 0;
5301                     }
5302                 }
5303               else
5304                 {
5305                   /* row somewhere in the middle, bring it to the top
5306                    * of the view
5307                    */
5308                   area_above = 0;
5309                   area_below = total_height - height;
5310                 }
5311             }
5312         }
5313     }
5314   else
5315     /* the scroll to isn't valid; ignore it.
5316      */
5317     {
5318       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
5319       tree_view->priv->scroll_to_path = NULL;
5320       return;
5321     }
5322
5323   above_path = gtk_tree_path_copy (path);
5324
5325   /* Now, we walk forwards and backwards, measuring rows. Unfortunately,
5326    * backwards is much slower then forward, as there is no iter_prev function.
5327    * We go forwards first in case we run out of tree.  Then we go backwards to
5328    * fill out the top.
5329    */
5330   while (node >= 0 && area_below > 0)
5331     {
5332       gboolean done = FALSE;
5333       do
5334         {
5335           node = pspp_sheet_view_node_next (tree_view, node);
5336           if (node >= 0)
5337             {
5338               gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
5339               done = TRUE;
5340               gtk_tree_path_next (path);
5341
5342               /* Sanity Check! */
5343               TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
5344             }
5345           else
5346             break;
5347         }
5348       while (!done);
5349
5350       if (node < 0)
5351         break;
5352
5353       area_below -= ROW_HEIGHT (tree_view);
5354     }
5355   gtk_tree_path_free (path);
5356
5357   /* If we ran out of tree, and have extra area_below left, we need to add it
5358    * to area_above */
5359   if (area_below > 0)
5360     area_above += area_below;
5361
5362   _pspp_sheet_view_find_node (tree_view, above_path, &node);
5363
5364   /* We walk backwards */
5365   while (area_above > 0)
5366     {
5367       node = pspp_sheet_view_node_prev (tree_view, node);
5368
5369       /* Always find the new path in the tree.  We cannot just assume
5370        * a gtk_tree_path_prev() is enough here, as there might be children
5371        * in between this node and the previous sibling node.  If this
5372        * appears to be a performance hotspot in profiles, we can look into
5373        * intrigate logic for keeping path, node and iter in sync like
5374        * we do for forward walks.  (Which will be hard because of the lacking
5375        * iter_prev).
5376        */
5377
5378       if (node < 0)
5379         break;
5380
5381       gtk_tree_path_free (above_path);
5382       above_path = _pspp_sheet_view_find_path (tree_view, node);
5383
5384       gtk_tree_model_get_iter (tree_view->priv->model, &iter, above_path);
5385
5386       area_above -= ROW_HEIGHT (tree_view);
5387     }
5388
5389   /* set the dy here to scroll to the path,
5390    * and sync the top row accordingly
5391    */
5392   pspp_sheet_view_set_top_row (tree_view, above_path, -area_above);
5393   pspp_sheet_view_top_row_to_dy (tree_view);
5394
5395   /* update width/height and queue a resize */
5396   if (size_changed)
5397     {
5398       GtkRequisition requisition;
5399
5400       /* We temporarily guess a size, under the assumption that it will be the
5401        * same when we get our next size_allocate.  If we don't do this, we'll be
5402        * in an inconsistent state if we call top_row_to_dy. */
5403
5404       gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
5405       gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->hadjustment), (gfloat)requisition.width));
5406       gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->vadjustment), (gfloat)requisition.height));
5407       gtk_adjustment_changed (tree_view->priv->hadjustment);
5408       gtk_adjustment_changed (tree_view->priv->vadjustment);
5409       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
5410     }
5411
5412   gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
5413   tree_view->priv->scroll_to_path = NULL;
5414
5415   if (above_path)
5416     gtk_tree_path_free (above_path);
5417
5418   if (tree_view->priv->scroll_to_column)
5419     {
5420       tree_view->priv->scroll_to_column = NULL;
5421     }
5422   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
5423 }
5424
5425 static void
5426 initialize_fixed_height_mode (PsppSheetView *tree_view)
5427 {
5428   if (!tree_view->priv->row_count)
5429     return;
5430
5431   if (tree_view->priv->fixed_height_set)
5432     return;
5433
5434   if (tree_view->priv->fixed_height < 0)
5435     {
5436       GtkTreeIter iter;
5437       GtkTreePath *path;
5438
5439       int node = 0;
5440
5441       path = _pspp_sheet_view_find_path (tree_view, node);
5442       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5443
5444       tree_view->priv->fixed_height = validate_row (tree_view, node, &iter, path);
5445
5446       gtk_tree_path_free (path);
5447
5448       g_object_notify (G_OBJECT (tree_view), "fixed-height");
5449     }
5450 }
5451
5452 /* Our strategy for finding nodes to validate is a little convoluted.  We find
5453  * the left-most uninvalidated node.  We then try walking right, validating
5454  * nodes.  Once we find a valid node, we repeat the previous process of finding
5455  * the first invalid node.
5456  */
5457
5458 static gboolean
5459 validate_rows_handler (PsppSheetView *tree_view)
5460 {
5461   initialize_fixed_height_mode (tree_view);
5462   if (tree_view->priv->validate_rows_timer)
5463     {
5464       g_source_remove (tree_view->priv->validate_rows_timer);
5465       tree_view->priv->validate_rows_timer = 0;
5466     }
5467
5468   return FALSE;
5469 }
5470
5471 static gboolean
5472 do_presize_handler (PsppSheetView *tree_view)
5473 {
5474   GtkRequisition requisition;
5475
5476   validate_visible_area (tree_view);
5477   tree_view->priv->presize_handler_timer = 0;
5478
5479   if (! gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5480     return FALSE;
5481
5482   gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
5483
5484   gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->hadjustment), (gfloat)requisition.width));
5485   gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_upper (tree_view->priv->vadjustment), (gfloat)requisition.height));
5486   gtk_adjustment_changed (tree_view->priv->hadjustment);
5487   gtk_adjustment_changed (tree_view->priv->vadjustment);
5488   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
5489                    
5490   return FALSE;
5491 }
5492
5493 static gboolean
5494 presize_handler_callback (gpointer data)
5495 {
5496   do_presize_handler (PSPP_SHEET_VIEW (data));
5497                    
5498   return FALSE;
5499 }
5500
5501 static void
5502 install_presize_handler (PsppSheetView *tree_view)
5503 {
5504   if (! gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5505     return;
5506
5507   if (! tree_view->priv->presize_handler_timer)
5508     {
5509       tree_view->priv->presize_handler_timer =
5510         gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, presize_handler_callback, tree_view, NULL);
5511     }
5512   if (! tree_view->priv->validate_rows_timer)
5513     {
5514       tree_view->priv->validate_rows_timer =
5515         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_VALIDATE, (GSourceFunc) validate_rows_handler, tree_view, NULL);
5516     }
5517 }
5518
5519 static gboolean
5520 scroll_sync_handler (PsppSheetView *tree_view)
5521 {
5522   if (tree_view->priv->height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
5523     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
5524   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
5525     pspp_sheet_view_top_row_to_dy (tree_view);
5526   else
5527     pspp_sheet_view_dy_to_top_row (tree_view);
5528
5529   tree_view->priv->scroll_sync_timer = 0;
5530
5531   return FALSE;
5532 }
5533
5534 static void
5535 install_scroll_sync_handler (PsppSheetView *tree_view)
5536 {
5537   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
5538     return;
5539
5540   if (!tree_view->priv->scroll_sync_timer)
5541     {
5542       tree_view->priv->scroll_sync_timer =
5543         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC, (GSourceFunc) scroll_sync_handler, tree_view, NULL);
5544     }
5545 }
5546
5547 static void
5548 pspp_sheet_view_set_top_row (PsppSheetView *tree_view,
5549                            GtkTreePath *path,
5550                            gint         offset)
5551 {
5552   gtk_tree_row_reference_free (tree_view->priv->top_row);
5553
5554   if (!path)
5555     {
5556       tree_view->priv->top_row = NULL;
5557       tree_view->priv->top_row_dy = 0;
5558     }
5559   else
5560     {
5561       tree_view->priv->top_row = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
5562       tree_view->priv->top_row_dy = offset;
5563     }
5564 }
5565
5566 /* Always call this iff dy is in the visible range.  If the tree is empty, then
5567  * it's set to be NULL, and top_row_dy is 0;
5568  */
5569 static void
5570 pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view)
5571 {
5572   gint offset;
5573   GtkTreePath *path;
5574   int node;
5575
5576   if (tree_view->priv->row_count == 0)
5577     {
5578       pspp_sheet_view_set_top_row (tree_view, NULL, 0);
5579     }
5580   else
5581     {
5582       offset = pspp_sheet_view_find_offset (tree_view,
5583                                             tree_view->priv->dy,
5584                                             &node);
5585
5586       if (node < 0)
5587         {
5588           pspp_sheet_view_set_top_row (tree_view, NULL, 0);
5589         }
5590       else
5591         {
5592           path = _pspp_sheet_view_find_path (tree_view, node);
5593           pspp_sheet_view_set_top_row (tree_view, path, offset);
5594           gtk_tree_path_free (path);
5595         }
5596     }
5597 }
5598
5599 static void
5600 pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view)
5601 {
5602   GtkTreePath *path;
5603   int node;
5604   int new_dy;
5605
5606   /* Avoid recursive calls */
5607   if (tree_view->priv->in_top_row_to_dy)
5608     return;
5609
5610   if (tree_view->priv->top_row)
5611     path = gtk_tree_row_reference_get_path (tree_view->priv->top_row);
5612   else
5613     path = NULL;
5614
5615   if (!path)
5616     node = -1;
5617   else
5618     _pspp_sheet_view_find_node (tree_view, path, &node);
5619
5620   if (path)
5621     gtk_tree_path_free (path);
5622
5623   if (node < 0)
5624     {
5625       /* keep dy and set new toprow */
5626       gtk_tree_row_reference_free (tree_view->priv->top_row);
5627       tree_view->priv->top_row = NULL;
5628       tree_view->priv->top_row_dy = 0;
5629       /* DO NOT install the idle handler */
5630       pspp_sheet_view_dy_to_top_row (tree_view);
5631       return;
5632     }
5633
5634   if (ROW_HEIGHT (tree_view) < tree_view->priv->top_row_dy)
5635     {
5636       /* new top row -- do NOT install the idle handler */
5637       pspp_sheet_view_dy_to_top_row (tree_view);
5638       return;
5639     }
5640
5641   new_dy = pspp_sheet_view_node_find_offset (tree_view, node);
5642   new_dy += tree_view->priv->top_row_dy;
5643
5644   if (new_dy + gtk_adjustment_get_page_size (tree_view->priv->vadjustment) > tree_view->priv->height)
5645     new_dy = tree_view->priv->height - gtk_adjustment_get_page_size (tree_view->priv->vadjustment);
5646
5647   new_dy = MAX (0, new_dy);
5648
5649   tree_view->priv->in_top_row_to_dy = TRUE;
5650   gtk_adjustment_set_value (tree_view->priv->vadjustment, (gdouble)new_dy);
5651   tree_view->priv->in_top_row_to_dy = FALSE;
5652 }
5653
5654
5655 void
5656 _pspp_sheet_view_install_mark_rows_col_dirty (PsppSheetView *tree_view)
5657 {
5658   install_presize_handler (tree_view);
5659 }
5660
5661 /* Drag-and-drop */
5662
5663 static void
5664 set_source_row (GdkDragContext *context,
5665                 GtkTreeModel   *model,
5666                 GtkTreePath    *source_row)
5667 {
5668   g_object_set_data_full (G_OBJECT (context),
5669                           "gtk-tree-view-source-row",
5670                           source_row ? gtk_tree_row_reference_new (model, source_row) : NULL,
5671                           (GDestroyNotify) (source_row ? gtk_tree_row_reference_free : NULL));
5672 }
5673
5674 static GtkTreePath*
5675 get_source_row (GdkDragContext *context)
5676 {
5677   GtkTreeRowReference *ref =
5678     g_object_get_data (G_OBJECT (context), "gtk-tree-view-source-row");
5679
5680   if (ref)
5681     return gtk_tree_row_reference_get_path (ref);
5682   else
5683     return NULL;
5684 }
5685
5686 typedef struct
5687 {
5688   GtkTreeRowReference *dest_row;
5689   guint                path_down_mode   : 1;
5690   guint                empty_view_drop  : 1;
5691   guint                drop_append_mode : 1;
5692 }
5693 DestRow;
5694
5695 static void
5696 dest_row_free (gpointer data)
5697 {
5698   DestRow *dr = (DestRow *)data;
5699
5700   gtk_tree_row_reference_free (dr->dest_row);
5701   g_slice_free (DestRow, dr);
5702 }
5703
5704 static void
5705 set_dest_row (GdkDragContext *context,
5706               GtkTreeModel   *model,
5707               GtkTreePath    *dest_row,
5708               gboolean        path_down_mode,
5709               gboolean        empty_view_drop,
5710               gboolean        drop_append_mode)
5711 {
5712   DestRow *dr;
5713
5714   if (!dest_row)
5715     {
5716       g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
5717                               NULL, NULL);
5718       return;
5719     }
5720
5721   dr = g_slice_new (DestRow);
5722
5723   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5724   dr->path_down_mode = path_down_mode != FALSE;
5725   dr->empty_view_drop = empty_view_drop != FALSE;
5726   dr->drop_append_mode = drop_append_mode != FALSE;
5727
5728   g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
5729                           dr, (GDestroyNotify) dest_row_free);
5730 }
5731
5732 static GtkTreePath*
5733 get_dest_row (GdkDragContext *context,
5734               gboolean       *path_down_mode)
5735 {
5736   DestRow *dr =
5737     g_object_get_data (G_OBJECT (context), "gtk-tree-view-dest-row");
5738
5739   if (dr)
5740     {
5741       GtkTreePath *path = NULL;
5742
5743       if (path_down_mode)
5744         *path_down_mode = dr->path_down_mode;
5745
5746       if (dr->dest_row)
5747         path = gtk_tree_row_reference_get_path (dr->dest_row);
5748       else if (dr->empty_view_drop)
5749         path = gtk_tree_path_new_from_indices (0, -1);
5750       else
5751         path = NULL;
5752
5753       if (path && dr->drop_append_mode)
5754         gtk_tree_path_next (path);
5755
5756       return path;
5757     }
5758   else
5759     return NULL;
5760 }
5761
5762 /* Get/set whether drag_motion requested the drag data and
5763  * drag_data_received should thus not actually insert the data,
5764  * since the data doesn't result from a drop.
5765  */
5766 static void
5767 set_status_pending (GdkDragContext *context,
5768                     GdkDragAction   suggested_action)
5769 {
5770   g_object_set_data (G_OBJECT (context),
5771                      "gtk-tree-view-status-pending",
5772                      GINT_TO_POINTER (suggested_action));
5773 }
5774
5775 static GdkDragAction
5776 get_status_pending (GdkDragContext *context)
5777 {
5778   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5779                                              "gtk-tree-view-status-pending"));
5780 }
5781
5782 static TreeViewDragInfo*
5783 get_info (PsppSheetView *tree_view)
5784 {
5785   return g_object_get_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info");
5786 }
5787
5788 static void
5789 destroy_info (TreeViewDragInfo *di)
5790 {
5791   g_slice_free (TreeViewDragInfo, di);
5792 }
5793
5794 static TreeViewDragInfo*
5795 ensure_info (PsppSheetView *tree_view)
5796 {
5797   TreeViewDragInfo *di;
5798
5799   di = get_info (tree_view);
5800
5801   if (di == NULL)
5802     {
5803       di = g_slice_new0 (TreeViewDragInfo);
5804
5805       g_object_set_data_full (G_OBJECT (tree_view),
5806                               "gtk-tree-view-drag-info",
5807                               di,
5808                               (GDestroyNotify) destroy_info);
5809     }
5810
5811   return di;
5812 }
5813
5814 static void
5815 remove_info (PsppSheetView *tree_view)
5816 {
5817   g_object_set_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info", NULL);
5818 }
5819
5820 #if 0
5821 static gint
5822 drag_scan_timeout (gpointer data)
5823 {
5824   PsppSheetView *tree_view;
5825   gint x, y;
5826   GdkModifierType state;
5827   GtkTreePath *path = NULL;
5828   PsppSheetViewColumn *column = NULL;
5829   GdkRectangle visible_rect;
5830
5831   GDK_THREADS_ENTER ();
5832
5833   tree_view = PSPP_SHEET_VIEW (data);
5834
5835   gdk_window_get_pointer (tree_view->priv->bin_window,
5836                           &x, &y, &state);
5837
5838   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
5839
5840   /* See if we are near the edge. */
5841   if ((x - visible_rect.x) < SCROLL_EDGE_SIZE ||
5842       (visible_rect.x + visible_rect.width - x) < SCROLL_EDGE_SIZE ||
5843       (y - visible_rect.y) < SCROLL_EDGE_SIZE ||
5844       (visible_rect.y + visible_rect.height - y) < SCROLL_EDGE_SIZE)
5845     {
5846       pspp_sheet_view_get_path_at_pos (tree_view,
5847                                      tree_view->priv->bin_window,
5848                                      x, y,
5849                                      &path,
5850                                      &column,
5851                                      NULL,
5852                                      NULL);
5853
5854       if (path != NULL)
5855         {
5856           pspp_sheet_view_scroll_to_cell (tree_view,
5857                                         path,
5858                                         column,
5859                                         TRUE,
5860                                         0.5, 0.5);
5861
5862           gtk_tree_path_free (path);
5863         }
5864     }
5865
5866   GDK_THREADS_LEAVE ();
5867
5868   return TRUE;
5869 }
5870 #endif /* 0 */
5871
5872 static void
5873 add_scroll_timeout (PsppSheetView *tree_view)
5874 {
5875   if (tree_view->priv->scroll_timeout == 0)
5876     {
5877       tree_view->priv->scroll_timeout =
5878         gdk_threads_add_timeout (150, scroll_row_timeout, tree_view);
5879     }
5880 }
5881
5882 static void
5883 remove_scroll_timeout (PsppSheetView *tree_view)
5884 {
5885   if (tree_view->priv->scroll_timeout != 0)
5886     {
5887       g_source_remove (tree_view->priv->scroll_timeout);
5888       tree_view->priv->scroll_timeout = 0;
5889     }
5890 }
5891
5892 static gboolean
5893 check_model_dnd (GtkTreeModel *model,
5894                  GType         required_iface,
5895                  const gchar  *signal)
5896 {
5897   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5898     {
5899       g_warning ("You must override the default '%s' handler "
5900                  "on PsppSheetView when using models that don't support "
5901                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5902                  "is to connect to '%s' and call "
5903                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5904                  "the default handler from running. Look at the source code "
5905                  "for the default handler in gtktreeview.c to get an idea what "
5906                  "your handler should do. (gtktreeview.c is in the GTK source "
5907                  "code.) If you're using GTK from a language other than C, "
5908                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5909                  signal, g_type_name (required_iface), signal);
5910       return FALSE;
5911     }
5912   else
5913     return TRUE;
5914 }
5915
5916 static gboolean
5917 scroll_row_timeout (gpointer data)
5918 {
5919   PsppSheetView *tree_view = data;
5920
5921   pspp_sheet_view_horizontal_autoscroll (tree_view);
5922   pspp_sheet_view_vertical_autoscroll (tree_view);
5923
5924   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
5925     pspp_sheet_view_update_rubber_band (tree_view);
5926
5927   return TRUE;
5928 }
5929
5930 /* Returns TRUE if event should not be propagated to parent widgets */
5931 static gboolean
5932 set_destination_row (PsppSheetView    *tree_view,
5933                      GdkDragContext *context,
5934                      /* coordinates relative to the widget */
5935                      gint            x,
5936                      gint            y,
5937                      GdkDragAction  *suggested_action,
5938                      GdkAtom        *target)
5939 {
5940   GtkTreePath *path = NULL;
5941   PsppSheetViewDropPosition pos;
5942   PsppSheetViewDropPosition old_pos;
5943   TreeViewDragInfo *di;
5944   GtkWidget *widget;
5945   GtkTreePath *old_dest_path = NULL;
5946   gboolean can_drop = FALSE;
5947
5948   *suggested_action = 0;
5949   *target = GDK_NONE;
5950
5951   widget = GTK_WIDGET (tree_view);
5952
5953   di = get_info (tree_view);
5954
5955   if (di == NULL || y - TREE_VIEW_HEADER_HEIGHT (tree_view) < 0)
5956     {
5957       /* someone unset us as a drag dest, note that if
5958        * we return FALSE drag_leave isn't called
5959        */
5960
5961       pspp_sheet_view_set_drag_dest_row (tree_view,
5962                                        NULL,
5963                                        PSPP_SHEET_VIEW_DROP_BEFORE);
5964
5965       remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
5966
5967       return FALSE; /* no longer a drop site */
5968     }
5969
5970   *target = gtk_drag_dest_find_target (widget, context,
5971                                        gtk_drag_dest_get_target_list (widget));
5972   if (*target == GDK_NONE)
5973     {
5974       return FALSE;
5975     }
5976
5977   if (!pspp_sheet_view_get_dest_row_at_pos (tree_view,
5978                                           x, y,
5979                                           &path,
5980                                           &pos))
5981     {
5982       gint n_children;
5983       GtkTreeModel *model;
5984
5985       /* the row got dropped on empty space, let's setup a special case
5986        */
5987
5988       if (path)
5989         gtk_tree_path_free (path);
5990
5991       model = pspp_sheet_view_get_model (tree_view);
5992
5993       n_children = gtk_tree_model_iter_n_children (model, NULL);
5994       if (n_children)
5995         {
5996           pos = PSPP_SHEET_VIEW_DROP_AFTER;
5997           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
5998         }
5999       else
6000         {
6001           pos = PSPP_SHEET_VIEW_DROP_BEFORE;
6002           path = gtk_tree_path_new_from_indices (0, -1);
6003         }
6004
6005       can_drop = TRUE;
6006
6007       goto out;
6008     }
6009
6010   g_assert (path);
6011
6012   /* If we left the current row's "open" zone, unset the timeout for
6013    * opening the row
6014    */
6015   pspp_sheet_view_get_drag_dest_row (tree_view,
6016                                    &old_dest_path,
6017                                    &old_pos);
6018
6019   if (old_dest_path)
6020     gtk_tree_path_free (old_dest_path);
6021
6022   if (TRUE /* FIXME if the location droppable predicate */)
6023     {
6024       can_drop = TRUE;
6025     }
6026
6027 out:
6028   if (can_drop)
6029     {
6030       GtkWidget *source_widget;
6031
6032       *suggested_action = gdk_drag_context_get_suggested_action (context);
6033       source_widget = gtk_drag_get_source_widget (context);
6034
6035       if (source_widget == widget)
6036         {
6037           /* Default to MOVE, unless the user has
6038            * pressed ctrl or shift to affect available actions
6039            */
6040           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6041             *suggested_action = GDK_ACTION_MOVE;
6042         }
6043
6044       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6045                                        path, pos);
6046     }
6047   else
6048     {
6049       /* can't drop here */
6050       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6051                                        NULL,
6052                                        PSPP_SHEET_VIEW_DROP_BEFORE);
6053     }
6054
6055   if (path)
6056     gtk_tree_path_free (path);
6057
6058   return TRUE;
6059 }
6060
6061 static GtkTreePath*
6062 get_logical_dest_row (PsppSheetView *tree_view,
6063                       gboolean    *path_down_mode,
6064                       gboolean    *drop_append_mode)
6065 {
6066   /* adjust path to point to the row the drop goes in front of */
6067   GtkTreePath *path = NULL;
6068   PsppSheetViewDropPosition pos;
6069
6070   g_return_val_if_fail (path_down_mode != NULL, NULL);
6071   g_return_val_if_fail (drop_append_mode != NULL, NULL);
6072
6073   *path_down_mode = FALSE;
6074   *drop_append_mode = 0;
6075
6076   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
6077
6078   if (path == NULL)
6079     return NULL;
6080
6081   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE)
6082     ; /* do nothing */
6083   else if (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE ||
6084            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER)
6085     *path_down_mode = TRUE;
6086   else
6087     {
6088       GtkTreeIter iter;
6089       GtkTreeModel *model = pspp_sheet_view_get_model (tree_view);
6090
6091       g_assert (pos == PSPP_SHEET_VIEW_DROP_AFTER);
6092
6093       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6094           !gtk_tree_model_iter_next (model, &iter))
6095         *drop_append_mode = 1;
6096       else
6097         {
6098           *drop_append_mode = 0;
6099           gtk_tree_path_next (path);
6100         }
6101     }
6102
6103   return path;
6104 }
6105
6106 static gboolean
6107 pspp_sheet_view_maybe_begin_dragging_row (PsppSheetView      *tree_view,
6108                                         GdkEventMotion   *event)
6109 {
6110   GtkWidget *widget = GTK_WIDGET (tree_view);
6111   GdkDragContext *context;
6112   TreeViewDragInfo *di;
6113   GtkTreePath *path = NULL;
6114   gint button;
6115   gint cell_x, cell_y;
6116   GtkTreeModel *model;
6117   gboolean retval = FALSE;
6118
6119   di = get_info (tree_view);
6120
6121   if (di == NULL || !di->source_set)
6122     goto out;
6123
6124   if (tree_view->priv->pressed_button < 0)
6125     goto out;
6126
6127   if (!gtk_drag_check_threshold (widget,
6128                                  tree_view->priv->press_start_x,
6129                                  tree_view->priv->press_start_y,
6130                                  event->x, event->y))
6131     goto out;
6132
6133   model = pspp_sheet_view_get_model (tree_view);
6134
6135   if (model == NULL)
6136     goto out;
6137
6138   button = tree_view->priv->pressed_button;
6139   tree_view->priv->pressed_button = -1;
6140
6141   pspp_sheet_view_get_path_at_pos (tree_view,
6142                                  tree_view->priv->press_start_x,
6143                                  tree_view->priv->press_start_y,
6144                                  &path,
6145                                  NULL,
6146                                  &cell_x,
6147                                  &cell_y);
6148
6149   if (path == NULL)
6150     goto out;
6151
6152   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6153       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6154                                            path))
6155     goto out;
6156
6157   if (!(GDK_BUTTON1_MASK << (button - 1) & di->start_button_mask))
6158     goto out;
6159
6160   /* Now we can begin the drag */
6161
6162   retval = TRUE;
6163
6164   context = gtk_drag_begin (widget,
6165                             gtk_drag_source_get_target_list (widget),
6166                             di->source_actions,
6167                             button,
6168                             (GdkEvent*)event);
6169
6170   set_source_row (context, model, path);
6171
6172  out:
6173   if (path)
6174     gtk_tree_path_free (path);
6175
6176   return retval;
6177 }
6178
6179
6180
6181 static void
6182 pspp_sheet_view_drag_begin (GtkWidget      *widget,
6183                           GdkDragContext *context)
6184 {
6185 #if GTK3_TRANSITION
6186   PsppSheetView *tree_view;
6187   GtkTreePath *path = NULL;
6188   gint cell_x, cell_y;
6189   GdkPixmap *row_pix;
6190   TreeViewDragInfo *di;
6191
6192   tree_view = PSPP_SHEET_VIEW (widget);
6193
6194   /* if the user uses a custom DND source impl, we don't set the icon here */
6195   di = get_info (tree_view);
6196
6197   if (di == NULL || !di->source_set)
6198     return;
6199
6200   pspp_sheet_view_get_path_at_pos (tree_view,
6201                                  tree_view->priv->press_start_x,
6202                                  tree_view->priv->press_start_y,
6203                                  &path,
6204                                  NULL,
6205                                  &cell_x,
6206                                  &cell_y);
6207
6208   g_return_if_fail (path != NULL);
6209
6210   row_pix = pspp_sheet_view_create_row_drag_icon (tree_view,
6211                                                 path);
6212
6213   gtk_drag_set_icon_pixmap (context,
6214                             gdk_drawable_get_colormap (row_pix),
6215                             row_pix,
6216                             NULL,
6217                             /* the + 1 is for the black border in the icon */
6218                             tree_view->priv->press_start_x + 1,
6219                             cell_y + 1);
6220
6221   g_object_unref (row_pix);
6222   gtk_tree_path_free (path);
6223 #endif
6224 }
6225
6226
6227 static void
6228 pspp_sheet_view_drag_end (GtkWidget      *widget,
6229                         GdkDragContext *context)
6230 {
6231   /* do nothing */
6232 }
6233
6234 /* Default signal implementations for the drag signals */
6235 static void
6236 pspp_sheet_view_drag_data_get (GtkWidget        *widget,
6237                              GdkDragContext   *context,
6238                              GtkSelectionData *selection_data,
6239                              guint             info,
6240                              guint             time)
6241 {
6242   PsppSheetView *tree_view;
6243   GtkTreeModel *model;
6244   TreeViewDragInfo *di;
6245   GtkTreePath *source_row;
6246
6247   tree_view = PSPP_SHEET_VIEW (widget);
6248
6249   model = pspp_sheet_view_get_model (tree_view);
6250
6251   if (model == NULL)
6252     return;
6253
6254   di = get_info (PSPP_SHEET_VIEW (widget));
6255
6256   if (di == NULL)
6257     return;
6258
6259   source_row = get_source_row (context);
6260
6261   if (source_row == NULL)
6262     return;
6263
6264   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6265    * any model; for DragSource models there are some other targets
6266    * we also support.
6267    */
6268
6269   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6270       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6271                                           source_row,
6272                                           selection_data))
6273     goto done;
6274
6275   /* If drag_data_get does nothing, try providing row data. */
6276   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6277     {
6278       gtk_tree_set_row_drag_data (selection_data,
6279                                   model,
6280                                   source_row);
6281     }
6282
6283  done:
6284   gtk_tree_path_free (source_row);
6285 }
6286
6287
6288 static void
6289 pspp_sheet_view_drag_data_delete (GtkWidget      *widget,
6290                                 GdkDragContext *context)
6291 {
6292   TreeViewDragInfo *di;
6293   GtkTreeModel *model;
6294   PsppSheetView *tree_view;
6295   GtkTreePath *source_row;
6296
6297   tree_view = PSPP_SHEET_VIEW (widget);
6298   model = pspp_sheet_view_get_model (tree_view);
6299
6300   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
6301     return;
6302
6303   di = get_info (tree_view);
6304
6305   if (di == NULL)
6306     return;
6307
6308   source_row = get_source_row (context);
6309
6310   if (source_row == NULL)
6311     return;
6312
6313   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6314                                          source_row);
6315
6316   gtk_tree_path_free (source_row);
6317
6318   set_source_row (context, NULL, NULL);
6319 }
6320
6321 static void
6322 pspp_sheet_view_drag_leave (GtkWidget      *widget,
6323                           GdkDragContext *context,
6324                           guint             time)
6325 {
6326   /* unset any highlight row */
6327   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6328                                    NULL,
6329                                    PSPP_SHEET_VIEW_DROP_BEFORE);
6330
6331   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
6332 }
6333
6334
6335 static gboolean
6336 pspp_sheet_view_drag_motion (GtkWidget        *widget,
6337                            GdkDragContext   *context,
6338                            /* coordinates relative to the widget */
6339                            gint              x,
6340                            gint              y,
6341                            guint             time)
6342 {
6343   gboolean empty;
6344   GtkTreePath *path = NULL;
6345   PsppSheetViewDropPosition pos;
6346   PsppSheetView *tree_view;
6347   GdkDragAction suggested_action = 0;
6348   GdkAtom target;
6349
6350   tree_view = PSPP_SHEET_VIEW (widget);
6351
6352   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
6353     return FALSE;
6354
6355   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
6356
6357   /* we only know this *after* set_desination_row */
6358   empty = tree_view->priv->empty_view_drop;
6359
6360   if (path == NULL && !empty)
6361     {
6362       /* Can't drop here. */
6363       gdk_drag_status (context, 0, time);
6364     }
6365   else
6366     {
6367       if (tree_view->priv->open_dest_timeout == 0 &&
6368           (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER ||
6369            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE))
6370         {
6371           /* Nothing. */
6372         }
6373       else
6374         {
6375           add_scroll_timeout (tree_view);
6376         }
6377
6378       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6379         {
6380           /* Request data so we can use the source row when
6381            * determining whether to accept the drop
6382            */
6383           set_status_pending (context, suggested_action);
6384           gtk_drag_get_data (widget, context, target, time);
6385         }
6386       else
6387         {
6388           set_status_pending (context, 0);
6389           gdk_drag_status (context, suggested_action, time);
6390         }
6391     }
6392
6393   if (path)
6394     gtk_tree_path_free (path);
6395
6396   return TRUE;
6397 }
6398
6399
6400 static gboolean
6401 pspp_sheet_view_drag_drop (GtkWidget        *widget,
6402                          GdkDragContext   *context,
6403                          /* coordinates relative to the widget */
6404                          gint              x,
6405                          gint              y,
6406                          guint             time)
6407 {
6408   PsppSheetView *tree_view;
6409   GtkTreePath *path;
6410   GdkDragAction suggested_action = 0;
6411   GdkAtom target = GDK_NONE;
6412   TreeViewDragInfo *di;
6413   GtkTreeModel *model;
6414   gboolean path_down_mode;
6415   gboolean drop_append_mode;
6416
6417   tree_view = PSPP_SHEET_VIEW (widget);
6418
6419   model = pspp_sheet_view_get_model (tree_view);
6420
6421   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
6422
6423   di = get_info (tree_view);
6424
6425   if (di == NULL)
6426     return FALSE;
6427
6428   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
6429     return FALSE;
6430
6431   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
6432     return FALSE;
6433
6434   path = get_logical_dest_row (tree_view, &path_down_mode, &drop_append_mode);
6435
6436   if (target != GDK_NONE && path != NULL)
6437     {
6438       /* in case a motion had requested drag data, change things so we
6439        * treat drag data receives as a drop.
6440        */
6441       set_status_pending (context, 0);
6442       set_dest_row (context, model, path,
6443                     path_down_mode, tree_view->priv->empty_view_drop,
6444                     drop_append_mode);
6445     }
6446
6447   if (path)
6448     gtk_tree_path_free (path);
6449
6450   /* Unset this thing */
6451   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6452                                    NULL,
6453                                    PSPP_SHEET_VIEW_DROP_BEFORE);
6454
6455   if (target != GDK_NONE)
6456     {
6457       gtk_drag_get_data (widget, context, target, time);
6458       return TRUE;
6459     }
6460   else
6461     return FALSE;
6462 }
6463
6464 static void
6465 pspp_sheet_view_drag_data_received (GtkWidget        *widget,
6466                                   GdkDragContext   *context,
6467                                   /* coordinates relative to the widget */
6468                                   gint              x,
6469                                   gint              y,
6470                                   GtkSelectionData *selection_data,
6471                                   guint             info,
6472                                   guint             time)
6473 {
6474   GtkTreePath *path;
6475   TreeViewDragInfo *di;
6476   gboolean accepted = FALSE;
6477   GtkTreeModel *model;
6478   PsppSheetView *tree_view;
6479   GtkTreePath *dest_row;
6480   GdkDragAction suggested_action;
6481   gboolean path_down_mode;
6482   gboolean drop_append_mode;
6483
6484   tree_view = PSPP_SHEET_VIEW (widget);
6485
6486   model = pspp_sheet_view_get_model (tree_view);
6487
6488   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
6489     return;
6490
6491   di = get_info (tree_view);
6492
6493   if (di == NULL)
6494     return;
6495
6496   suggested_action = get_status_pending (context);
6497
6498   if (suggested_action)
6499     {
6500       /* We are getting this data due to a request in drag_motion,
6501        * rather than due to a request in drag_drop, so we are just
6502        * supposed to call drag_status, not actually paste in the
6503        * data.
6504        */
6505       path = get_logical_dest_row (tree_view, &path_down_mode,
6506                                    &drop_append_mode);
6507
6508       if (path == NULL)
6509         suggested_action = 0;
6510       else if (path_down_mode)
6511         gtk_tree_path_down (path);
6512
6513       if (suggested_action)
6514         {
6515           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6516                                                      path,
6517                                                      selection_data))
6518             {
6519               if (path_down_mode)
6520                 {
6521                   path_down_mode = FALSE;
6522                   gtk_tree_path_up (path);
6523
6524                   if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6525                                                              path,
6526                                                              selection_data))
6527                     suggested_action = 0;
6528                 }
6529               else
6530                 suggested_action = 0;
6531             }
6532         }
6533
6534       gdk_drag_status (context, suggested_action, time);
6535
6536       if (path)
6537         gtk_tree_path_free (path);
6538
6539       /* If you can't drop, remove user drop indicator until the next motion */
6540       if (suggested_action == 0)
6541         pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
6542                                          NULL,
6543                                          PSPP_SHEET_VIEW_DROP_BEFORE);
6544
6545       return;
6546     }
6547
6548   dest_row = get_dest_row (context, &path_down_mode);
6549
6550   if (dest_row == NULL)
6551     return;
6552
6553   if (gtk_selection_data_get_length (selection_data) >= 0)
6554     {
6555       if (path_down_mode)
6556         {
6557           gtk_tree_path_down (dest_row);
6558           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6559                                                      dest_row, selection_data))
6560             gtk_tree_path_up (dest_row);
6561         }
6562     }
6563
6564   if (gtk_selection_data_get_length (selection_data) >= 0)
6565     {
6566       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6567                                                  dest_row,
6568                                                  selection_data))
6569         accepted = TRUE;
6570     }
6571
6572   gtk_drag_finish (context,
6573                    accepted,
6574                    (gdk_drag_context_get_actions (context) == GDK_ACTION_MOVE),
6575                    time);
6576
6577   if (gtk_tree_path_get_depth (dest_row) == 1
6578       && gtk_tree_path_get_indices (dest_row)[0] == 0)
6579     {
6580       /* special special case drag to "0", scroll to first item */
6581       if (!tree_view->priv->scroll_to_path)
6582         pspp_sheet_view_scroll_to_cell (tree_view, dest_row, NULL, FALSE, 0.0, 0.0);
6583     }
6584
6585   gtk_tree_path_free (dest_row);
6586
6587   /* drop dest_row */
6588   set_dest_row (context, NULL, NULL, FALSE, FALSE, FALSE);
6589 }
6590
6591
6592
6593 /* GtkContainer Methods
6594  */
6595
6596
6597 static void
6598 pspp_sheet_view_remove (GtkContainer *container,
6599                       GtkWidget    *widget)
6600 {
6601   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6602   PsppSheetViewChild *child = NULL;
6603   GList *tmp_list;
6604
6605   tmp_list = tree_view->priv->children;
6606   while (tmp_list)
6607     {
6608       child = tmp_list->data;
6609       if (child->widget == widget)
6610         {
6611           gtk_widget_unparent (widget);
6612
6613           tree_view->priv->children = g_list_remove_link (tree_view->priv->children, tmp_list);
6614           g_list_free_1 (tmp_list);
6615           g_slice_free (PsppSheetViewChild, child);
6616           return;
6617         }
6618
6619       tmp_list = tmp_list->next;
6620     }
6621
6622   tmp_list = tree_view->priv->columns;
6623
6624   while (tmp_list)
6625     {
6626       PsppSheetViewColumn *column;
6627       column = tmp_list->data;
6628       if (column->button == widget)
6629         {
6630           gtk_widget_unparent (widget);
6631           return;
6632         }
6633       tmp_list = tmp_list->next;
6634     }
6635 }
6636
6637 static void
6638 pspp_sheet_view_forall (GtkContainer *container,
6639                       gboolean      include_internals,
6640                       GtkCallback   callback,
6641                       gpointer      callback_data)
6642 {
6643   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6644   PsppSheetViewChild *child = NULL;
6645   PsppSheetViewColumn *column;
6646   GList *tmp_list;
6647
6648   tmp_list = tree_view->priv->children;
6649   while (tmp_list)
6650     {
6651       child = tmp_list->data;
6652       tmp_list = tmp_list->next;
6653
6654       (* callback) (child->widget, callback_data);
6655     }
6656   if (include_internals == FALSE)
6657     return;
6658
6659   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
6660     {
6661       column = tmp_list->data;
6662
6663       if (column->button)
6664         (* callback) (column->button, callback_data);
6665     }
6666 }
6667
6668 /* Returns TRUE if the treeview contains no "special" (editable or activatable)
6669  * cells. If so we draw one big row-spanning focus rectangle.
6670  */
6671 static gboolean
6672 pspp_sheet_view_has_special_cell (PsppSheetView *tree_view)
6673 {
6674   GList *list;
6675
6676   if (tree_view->priv->special_cells != PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT)
6677     return tree_view->priv->special_cells = PSPP_SHEET_VIEW_SPECIAL_CELLS_YES;
6678
6679   for (list = tree_view->priv->columns; list; list = list->next)
6680     {
6681       if (!((PsppSheetViewColumn *)list->data)->visible)
6682         continue;
6683       if (_pspp_sheet_view_column_count_special_cells (list->data))
6684         return TRUE;
6685     }
6686
6687   return FALSE;
6688 }
6689
6690 static void
6691 pspp_sheet_view_focus_column (PsppSheetView *tree_view,
6692                               PsppSheetViewColumn *focus_column,
6693                               gboolean clamp_column_visible)
6694 {
6695   g_return_if_fail (focus_column != NULL);
6696
6697   tree_view->priv->focus_column = focus_column;
6698   if (!focus_column->button)
6699     {
6700       pspp_sheet_view_column_set_need_button (focus_column, TRUE);
6701       //      g_return_if_fail (focus_column->button != NULL);
6702       if (focus_column->button == NULL)
6703         return;
6704     }
6705
6706   if (gtk_container_get_focus_child (GTK_CONTAINER (tree_view)) != focus_column->button)
6707     gtk_widget_grab_focus (focus_column->button);
6708
6709   if (clamp_column_visible)
6710     pspp_sheet_view_clamp_column_visible (tree_view, focus_column, FALSE);
6711 }
6712
6713 /* Returns TRUE if the focus is within the headers, after the focus operation is
6714  * done
6715  */
6716 static gboolean
6717 pspp_sheet_view_header_focus (PsppSheetView      *tree_view,
6718                             GtkDirectionType  dir,
6719                             gboolean          clamp_column_visible)
6720 {
6721   GtkWidget *focus_child;
6722   PsppSheetViewColumn *focus_column;
6723   GList *last_column, *first_column;
6724   GList *tmp_list;
6725   gboolean rtl;
6726
6727   if (! PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
6728     return FALSE;
6729
6730   focus_child = gtk_container_get_focus_child (GTK_CONTAINER (tree_view));
6731
6732   first_column = tree_view->priv->columns;
6733   while (first_column)
6734     {
6735       PsppSheetViewColumn *c = PSPP_SHEET_VIEW_COLUMN (first_column->data);
6736
6737       if (pspp_sheet_view_column_can_focus (c) && c->visible)
6738         break;
6739       first_column = first_column->next;
6740     }
6741
6742   /* No headers are visible, or are focusable.  We can't focus in or out.
6743    */
6744   if (first_column == NULL)
6745     return FALSE;
6746
6747   last_column = g_list_last (tree_view->priv->columns);
6748   while (last_column)
6749     {
6750       PsppSheetViewColumn *c = PSPP_SHEET_VIEW_COLUMN (last_column->data);
6751
6752       if (pspp_sheet_view_column_can_focus (c) && c->visible)
6753         break;
6754       last_column = last_column->prev;
6755     }
6756
6757
6758   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
6759
6760   switch (dir)
6761     {
6762     case GTK_DIR_TAB_BACKWARD:
6763     case GTK_DIR_TAB_FORWARD:
6764     case GTK_DIR_UP:
6765     case GTK_DIR_DOWN:
6766       if (focus_child == NULL)
6767         {
6768           if (tree_view->priv->focus_column != NULL &&
6769               pspp_sheet_view_column_can_focus (tree_view->priv->focus_column))
6770             focus_column = tree_view->priv->focus_column;
6771           else
6772             focus_column = first_column->data;
6773           pspp_sheet_view_focus_column (tree_view, focus_column,
6774                                         clamp_column_visible);
6775           return TRUE;
6776         }
6777       return FALSE;
6778
6779     case GTK_DIR_LEFT:
6780     case GTK_DIR_RIGHT:
6781       if (focus_child == NULL)
6782         {
6783           if (tree_view->priv->focus_column != NULL)
6784             focus_column = tree_view->priv->focus_column;
6785           else if (dir == GTK_DIR_LEFT)
6786             focus_column = last_column->data;
6787           else
6788             focus_column = first_column->data;
6789           pspp_sheet_view_focus_column (tree_view, focus_column,
6790                                         clamp_column_visible);
6791           return TRUE;
6792         }
6793
6794       if (gtk_widget_child_focus (focus_child, dir))
6795         {
6796           /* The focus moves inside the button. */
6797           /* This is probably a great example of bad UI */
6798           if (clamp_column_visible)
6799             pspp_sheet_view_clamp_column_visible (tree_view,
6800                                                   tree_view->priv->focus_column,
6801                                                   FALSE);
6802           return TRUE;
6803         }
6804
6805       /* We need to move the focus among the row of buttons. */
6806       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
6807         if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)->button == focus_child)
6808           break;
6809
6810       if ((tmp_list == first_column && dir == (rtl ? GTK_DIR_RIGHT : GTK_DIR_LEFT))
6811           || (tmp_list == last_column && dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT)))
6812         {
6813           gtk_widget_error_bell (GTK_WIDGET (tree_view));
6814           return TRUE;
6815         }
6816
6817       while (tmp_list)
6818         {
6819           PsppSheetViewColumn *column;
6820
6821           if (dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT))
6822             tmp_list = tmp_list->next;
6823           else
6824             tmp_list = tmp_list->prev;
6825
6826           if (tmp_list == NULL)
6827             {
6828               g_warning ("Internal button not found");
6829               break;
6830             }
6831           column = tmp_list->data;
6832           if (column->visible &&
6833               pspp_sheet_view_column_can_focus (column))
6834             {
6835               pspp_sheet_view_column_set_need_button (column, TRUE);
6836               if (column->button)
6837                 {
6838                   pspp_sheet_view_focus_column (tree_view, column,
6839                                                 clamp_column_visible);
6840                   return TRUE;
6841                 }
6842             }
6843         }
6844       return FALSE;
6845
6846     default:
6847       g_assert_not_reached ();
6848       break;
6849     }
6850
6851   return FALSE;
6852 }
6853
6854 /* This function returns in 'path' the first focusable path, if the given path
6855  * is already focusable, it's the returned one.
6856  *
6857  */
6858 static gboolean
6859 search_first_focusable_path (PsppSheetView  *tree_view,
6860                              GtkTreePath **path,
6861                              gboolean      search_forward,
6862                              int *new_node)
6863 {
6864   /* XXX this function is trivial given that the sheetview doesn't support
6865      separator rows */
6866   int node = -1;
6867
6868   if (!path || !*path)
6869     return FALSE;
6870
6871   _pspp_sheet_view_find_node (tree_view, *path, &node);
6872
6873   if (node < 0)
6874     return FALSE;
6875
6876   if (new_node)
6877     *new_node = node;
6878
6879   return (*path != NULL);
6880 }
6881
6882 static gint
6883 pspp_sheet_view_focus (GtkWidget        *widget,
6884                      GtkDirectionType  direction)
6885 {
6886   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
6887   GtkContainer *container = GTK_CONTAINER (widget);
6888   GtkWidget *focus_child;
6889
6890   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget))
6891     return FALSE;
6892
6893   focus_child = gtk_container_get_focus_child (container);
6894
6895   pspp_sheet_view_stop_editing (PSPP_SHEET_VIEW (widget), FALSE);
6896   /* Case 1.  Headers currently have focus. */
6897   if (focus_child)
6898     {
6899       switch (direction)
6900         {
6901         case GTK_DIR_LEFT:
6902         case GTK_DIR_RIGHT:
6903           pspp_sheet_view_header_focus (tree_view, direction, TRUE);
6904           return TRUE;
6905         case GTK_DIR_TAB_BACKWARD:
6906         case GTK_DIR_UP:
6907           return FALSE;
6908         case GTK_DIR_TAB_FORWARD:
6909         case GTK_DIR_DOWN:
6910           gtk_widget_grab_focus (widget);
6911           return TRUE;
6912         default:
6913           g_assert_not_reached ();
6914           return FALSE;
6915         }
6916     }
6917
6918   /* Case 2. We don't have focus at all. */
6919   if (!gtk_widget_has_focus (widget))
6920     {
6921       if (!pspp_sheet_view_header_focus (tree_view, direction, FALSE))
6922         gtk_widget_grab_focus (widget);
6923       return TRUE;
6924     }
6925
6926   /* Case 3. We have focus already. */
6927   if (direction == GTK_DIR_TAB_BACKWARD)
6928     return (pspp_sheet_view_header_focus (tree_view, direction, FALSE));
6929   else if (direction == GTK_DIR_TAB_FORWARD)
6930     return FALSE;
6931
6932   /* Other directions caught by the keybindings */
6933   gtk_widget_grab_focus (widget);
6934   return TRUE;
6935 }
6936
6937 static void
6938 pspp_sheet_view_grab_focus (GtkWidget *widget)
6939 {
6940   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->grab_focus (widget);
6941
6942   pspp_sheet_view_focus_to_cursor (PSPP_SHEET_VIEW (widget));
6943 }
6944
6945 static void
6946 pspp_sheet_view_style_set (GtkWidget *widget,
6947                          GtkStyle *previous_style)
6948 {
6949   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
6950   GList *list;
6951   PsppSheetViewColumn *column;
6952
6953   if (gtk_widget_get_realized (widget))
6954     {
6955       gdk_window_set_background (tree_view->priv->bin_window, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
6956       gtk_style_set_background (gtk_widget_get_style (widget), tree_view->priv->header_window, GTK_STATE_NORMAL);
6957       pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
6958     }
6959
6960   gtk_widget_style_get (widget,
6961                         "expander-size", &tree_view->priv->expander_size,
6962                         NULL);
6963   tree_view->priv->expander_size += EXPANDER_EXTRA_PADDING;
6964
6965   for (list = tree_view->priv->columns; list; list = list->next)
6966     {
6967       column = list->data;
6968       _pspp_sheet_view_column_cell_set_dirty (column);
6969     }
6970
6971   tree_view->priv->fixed_height = -1;
6972
6973   /* Invalidate cached button style. */
6974   if (tree_view->priv->button_style)
6975     {
6976       g_object_unref (tree_view->priv->button_style);
6977       tree_view->priv->button_style = NULL;
6978     }
6979
6980   gtk_widget_queue_resize (widget);
6981 }
6982
6983
6984 static void
6985 pspp_sheet_view_set_focus_child (GtkContainer *container,
6986                                GtkWidget    *child)
6987 {
6988   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
6989   GList *list;
6990
6991   for (list = tree_view->priv->columns; list; list = list->next)
6992     {
6993       if (PSPP_SHEET_VIEW_COLUMN (list->data)->button == child)
6994         {
6995           tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
6996           break;
6997         }
6998     }
6999
7000   GTK_CONTAINER_CLASS (pspp_sheet_view_parent_class)->set_focus_child (container, child);
7001 }
7002
7003 static void
7004 pspp_sheet_view_set_adjustments (PsppSheetView   *tree_view,
7005                                GtkAdjustment *hadj,
7006                                GtkAdjustment *vadj)
7007 {
7008   gboolean need_adjust = FALSE;
7009
7010   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
7011
7012   if (hadj)
7013     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
7014   else
7015     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7016   if (vadj)
7017     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
7018   else
7019     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7020
7021   if (tree_view->priv->hadjustment && (tree_view->priv->hadjustment != hadj))
7022     {
7023       g_signal_handlers_disconnect_by_func (tree_view->priv->hadjustment,
7024                                             pspp_sheet_view_adjustment_changed,
7025                                             tree_view);
7026       g_object_unref (tree_view->priv->hadjustment);
7027     }
7028
7029   if (tree_view->priv->vadjustment && (tree_view->priv->vadjustment != vadj))
7030     {
7031       g_signal_handlers_disconnect_by_func (tree_view->priv->vadjustment,
7032                                             pspp_sheet_view_adjustment_changed,
7033                                             tree_view);
7034       g_object_unref (tree_view->priv->vadjustment);
7035     }
7036
7037   if (tree_view->priv->hadjustment != hadj)
7038     {
7039       tree_view->priv->hadjustment = hadj;
7040       g_object_ref_sink (tree_view->priv->hadjustment);
7041
7042       g_signal_connect (tree_view->priv->hadjustment, "value-changed",
7043                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
7044                         tree_view);
7045       need_adjust = TRUE;
7046     }
7047
7048   if (tree_view->priv->vadjustment != vadj)
7049     {
7050       tree_view->priv->vadjustment = vadj;
7051       g_object_ref_sink (tree_view->priv->vadjustment);
7052
7053       g_signal_connect (tree_view->priv->vadjustment, "value-changed",
7054                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
7055                         tree_view);
7056       need_adjust = TRUE;
7057     }
7058
7059   if (need_adjust)
7060     pspp_sheet_view_adjustment_changed (NULL, tree_view);
7061 }
7062
7063
7064 static gboolean
7065 pspp_sheet_view_real_move_cursor (PsppSheetView       *tree_view,
7066                                 GtkMovementStep    step,
7067                                 gint               count)
7068 {
7069   PsppSheetSelectMode mode;
7070   GdkModifierType state;
7071
7072   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
7073   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
7074                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
7075                         step == GTK_MOVEMENT_DISPLAY_LINES ||
7076                         step == GTK_MOVEMENT_PAGES ||
7077                         step == GTK_MOVEMENT_BUFFER_ENDS ||
7078                         step == GTK_MOVEMENT_DISPLAY_LINE_ENDS, FALSE);
7079
7080   if (tree_view->priv->row_count == 0)
7081     return FALSE;
7082   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
7083     return FALSE;
7084
7085   pspp_sheet_view_stop_editing (tree_view, FALSE);
7086   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
7087   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
7088
7089   mode = 0;
7090   if (gtk_get_current_event_state (&state))
7091     {
7092       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
7093         mode |= PSPP_SHEET_SELECT_MODE_TOGGLE;
7094       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
7095         mode |= PSPP_SHEET_SELECT_MODE_EXTEND;
7096     }
7097   /* else we assume not pressed */
7098
7099   switch (step)
7100     {
7101     case GTK_MOVEMENT_LOGICAL_POSITIONS:
7102       pspp_sheet_view_move_cursor_tab (tree_view, count);
7103       break;
7104     case GTK_MOVEMENT_VISUAL_POSITIONS:
7105       pspp_sheet_view_move_cursor_left_right (tree_view, count, mode);
7106       break;
7107     case GTK_MOVEMENT_DISPLAY_LINES:
7108       pspp_sheet_view_move_cursor_up_down (tree_view, count, mode);
7109       break;
7110     case GTK_MOVEMENT_PAGES:
7111       pspp_sheet_view_move_cursor_page_up_down (tree_view, count, mode);
7112       break;
7113     case GTK_MOVEMENT_BUFFER_ENDS:
7114       pspp_sheet_view_move_cursor_start_end (tree_view, count, mode);
7115       break;
7116     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
7117       pspp_sheet_view_move_cursor_line_start_end (tree_view, count, mode);
7118       break;
7119     default:
7120       g_assert_not_reached ();
7121     }
7122
7123   return TRUE;
7124 }
7125
7126 static void
7127 pspp_sheet_view_put (PsppSheetView *tree_view,
7128                    GtkWidget   *child_widget,
7129                    /* in bin_window coordinates */
7130                    gint         x,
7131                    gint         y,
7132                    gint         width,
7133                    gint         height)
7134 {
7135   PsppSheetViewChild *child;
7136   
7137   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
7138   g_return_if_fail (GTK_IS_WIDGET (child_widget));
7139
7140   child = g_slice_new (PsppSheetViewChild);
7141
7142   child->widget = child_widget;
7143   child->x = x;
7144   child->y = y;
7145   child->width = width;
7146   child->height = height;
7147
7148   tree_view->priv->children = g_list_append (tree_view->priv->children, child);
7149
7150   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7151     gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
7152   
7153   gtk_widget_set_parent (child_widget, GTK_WIDGET (tree_view));
7154 }
7155
7156 void
7157 _pspp_sheet_view_child_move_resize (PsppSheetView *tree_view,
7158                                   GtkWidget   *widget,
7159                                   /* in tree coordinates */
7160                                   gint         x,
7161                                   gint         y,
7162                                   gint         width,
7163                                   gint         height)
7164 {
7165   PsppSheetViewChild *child = NULL;
7166   GList *list;
7167   GdkRectangle allocation;
7168
7169   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
7170   g_return_if_fail (GTK_IS_WIDGET (widget));
7171
7172   for (list = tree_view->priv->children; list; list = list->next)
7173     {
7174       if (((PsppSheetViewChild *)list->data)->widget == widget)
7175         {
7176           child = list->data;
7177           break;
7178         }
7179     }
7180   if (child == NULL)
7181     return;
7182
7183   allocation.x = child->x = x;
7184   allocation.y = child->y = y;
7185   allocation.width = child->width = width;
7186   allocation.height = child->height = height;
7187
7188   if (gtk_widget_get_realized (widget))
7189     gtk_widget_size_allocate (widget, &allocation);
7190 }
7191
7192
7193 /* TreeModel Callbacks
7194  */
7195
7196 static void
7197 pspp_sheet_view_row_changed (GtkTreeModel *model,
7198                            GtkTreePath  *path,
7199                            GtkTreeIter  *iter,
7200                            gpointer      data)
7201 {
7202   PsppSheetView *tree_view = (PsppSheetView *)data;
7203   int node;
7204   gboolean free_path = FALSE;
7205   GtkTreePath *cursor_path;
7206
7207   g_return_if_fail (path != NULL || iter != NULL);
7208
7209   if (tree_view->priv->cursor != NULL)
7210     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7211   else
7212     cursor_path = NULL;
7213
7214   if (tree_view->priv->edited_column &&
7215       (cursor_path == NULL || gtk_tree_path_compare (cursor_path, path) == 0))
7216     pspp_sheet_view_stop_editing (tree_view, TRUE);
7217
7218   if (cursor_path != NULL)
7219     gtk_tree_path_free (cursor_path);
7220
7221   if (path == NULL)
7222     {
7223       path = gtk_tree_model_get_path (model, iter);
7224       free_path = TRUE;
7225     }
7226   else if (iter == NULL)
7227     gtk_tree_model_get_iter (model, iter, path);
7228
7229   _pspp_sheet_view_find_node (tree_view,
7230                               path,
7231                               &node);
7232
7233   if (node >= 0)
7234     {
7235       if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7236         pspp_sheet_view_node_queue_redraw (tree_view, node);
7237     }
7238   
7239   if (free_path)
7240     gtk_tree_path_free (path);
7241 }
7242
7243 static void
7244 pspp_sheet_view_row_inserted (GtkTreeModel *model,
7245                             GtkTreePath  *path,
7246                             GtkTreeIter  *iter,
7247                             gpointer      data)
7248 {
7249   PsppSheetView *tree_view = (PsppSheetView *) data;
7250   gint *indices;
7251   int tmpnode = -1;
7252   gint height = tree_view->priv->fixed_height;
7253   gboolean free_path = FALSE;
7254   gboolean node_visible = TRUE;
7255
7256   g_return_if_fail (path != NULL || iter != NULL);
7257
7258   if (path == NULL)
7259     {
7260       path = gtk_tree_model_get_path (model, iter);
7261       free_path = TRUE;
7262     }
7263   else if (iter == NULL)
7264     gtk_tree_model_get_iter (model, iter, path);
7265
7266   tree_view->priv->row_count = gtk_tree_model_iter_n_children (model, NULL);
7267
7268   /* Update all row-references */
7269   gtk_tree_row_reference_inserted (G_OBJECT (data), path);
7270   indices = gtk_tree_path_get_indices (path);
7271   tmpnode = indices[0];
7272
7273   range_tower_insert0 (tree_view->priv->selected, tmpnode, 1);
7274
7275   if (height > 0)
7276     {
7277       if (node_visible && node_is_visible (tree_view, tmpnode))
7278         gtk_widget_queue_resize (GTK_WIDGET (tree_view));
7279       else
7280         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (tree_view));
7281     }
7282   else
7283     install_presize_handler (tree_view);
7284   if (free_path)
7285     gtk_tree_path_free (path);
7286 }
7287
7288 static void
7289 pspp_sheet_view_row_deleted (GtkTreeModel *model,
7290                            GtkTreePath  *path,
7291                            gpointer      data)
7292 {
7293   PsppSheetView *tree_view = (PsppSheetView *)data;
7294   int node;
7295
7296   g_return_if_fail (path != NULL);
7297
7298   gtk_tree_row_reference_deleted (G_OBJECT (data), path);
7299
7300   _pspp_sheet_view_find_node (tree_view, path, &node);
7301
7302   if (node < 0)
7303     return;
7304
7305   range_tower_delete (tree_view->priv->selected, node, 1);
7306
7307   /* Ensure we don't have a dangling pointer to a dead node */
7308   ensure_unprelighted (tree_view);
7309
7310   /* Cancel editting if we've started */
7311   pspp_sheet_view_stop_editing (tree_view, TRUE);
7312
7313   if (tree_view->priv->destroy_count_func)
7314     {
7315       gint child_count = 0;
7316       tree_view->priv->destroy_count_func (tree_view, path, child_count, tree_view->priv->destroy_count_data);
7317     }
7318
7319   tree_view->priv->row_count = gtk_tree_model_iter_n_children (model, NULL);
7320
7321   if (! gtk_tree_row_reference_valid (tree_view->priv->top_row))
7322     {
7323       gtk_tree_row_reference_free (tree_view->priv->top_row);
7324       tree_view->priv->top_row = NULL;
7325     }
7326
7327   install_scroll_sync_handler (tree_view);
7328
7329   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
7330
7331 #if 0
7332   if (helper_data.changed)
7333     g_signal_emit_by_name (tree_view->priv->selection, "changed");
7334 #endif
7335 }
7336
7337 static void
7338 pspp_sheet_view_rows_reordered (GtkTreeModel *model,
7339                               GtkTreePath  *parent,
7340                               GtkTreeIter  *iter,
7341                               gint         *new_order,
7342                               gpointer      data)
7343 {
7344   PsppSheetView *tree_view = PSPP_SHEET_VIEW (data);
7345   gint len;
7346
7347   /* XXX need to adjust selection */
7348   len = gtk_tree_model_iter_n_children (model, iter);
7349
7350   if (len < 2)
7351     return;
7352
7353   gtk_tree_row_reference_reordered (G_OBJECT (data),
7354                                     parent,
7355                                     iter,
7356                                     new_order);
7357
7358   if (gtk_tree_path_get_depth (parent) != 0)
7359     return;
7360
7361   if (tree_view->priv->edited_column)
7362     pspp_sheet_view_stop_editing (tree_view, TRUE);
7363
7364   /* we need to be unprelighted */
7365   ensure_unprelighted (tree_view);
7366
7367   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
7368
7369   pspp_sheet_view_dy_to_top_row (tree_view);
7370 }
7371
7372
7373 /* Internal tree functions
7374  */
7375
7376
7377 static void
7378 pspp_sheet_view_get_background_xrange (PsppSheetView       *tree_view,
7379                                      PsppSheetViewColumn *column,
7380                                      gint              *x1,
7381                                      gint              *x2)
7382 {
7383   PsppSheetViewColumn *tmp_column = NULL;
7384   gint total_width;
7385   GList *list;
7386   gboolean rtl;
7387
7388   if (x1)
7389     *x1 = 0;
7390
7391   if (x2)
7392     *x2 = 0;
7393
7394   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
7395
7396   total_width = 0;
7397   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
7398        list;
7399        list = (rtl ? list->prev : list->next))
7400     {
7401       tmp_column = list->data;
7402
7403       if (tmp_column == column)
7404         break;
7405
7406       if (tmp_column->visible)
7407         total_width += tmp_column->width;
7408     }
7409
7410   if (tmp_column != column)
7411     {
7412       g_warning (G_STRLOC": passed-in column isn't in the tree");
7413       return;
7414     }
7415
7416   if (x1)
7417     *x1 = total_width;
7418
7419   if (x2)
7420     {
7421       if (column->visible)
7422         *x2 = total_width + column->width;
7423       else
7424         *x2 = total_width; /* width of 0 */
7425     }
7426 }
7427
7428 /* Make sure the node is visible vertically */
7429 static void
7430 pspp_sheet_view_clamp_node_visible (PsppSheetView *tree_view,
7431                                     int node)
7432 {
7433   gint node_dy, height;
7434   GtkTreePath *path = NULL;
7435
7436   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7437     return;
7438
7439   /* just return if the node is visible, avoiding a costly expose */
7440   node_dy = pspp_sheet_view_node_find_offset (tree_view, node);
7441   height = ROW_HEIGHT (tree_view);
7442   if (node_dy >= gtk_adjustment_get_value (tree_view->priv->vadjustment)
7443       && node_dy + height <= (gtk_adjustment_get_value (tree_view->priv->vadjustment)
7444                               + gtk_adjustment_get_page_size (tree_view->priv->vadjustment)))
7445     return;
7446
7447   path = _pspp_sheet_view_find_path (tree_view, node);
7448   if (path)
7449     {
7450       /* We process updates because we want to clear old selected items when we scroll.
7451        * if this is removed, we get a "selection streak" at the bottom. */
7452       gdk_window_process_updates (tree_view->priv->bin_window, TRUE);
7453       pspp_sheet_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
7454       gtk_tree_path_free (path);
7455     }
7456 }
7457
7458 static void
7459 pspp_sheet_view_clamp_column_visible (PsppSheetView       *tree_view,
7460                                     PsppSheetViewColumn *column,
7461                                     gboolean           focus_to_cell)
7462 {
7463   gint x, width;
7464
7465   if (column == NULL)
7466     return;
7467
7468   x = column->allocation.x;
7469   width = column->allocation.width;
7470
7471   if (width > gtk_adjustment_get_page_size (tree_view->priv->hadjustment))
7472     {
7473       /* The column is larger than the horizontal page size.  If the
7474        * column has cells which can be focussed individually, then we make
7475        * sure the cell which gets focus is fully visible (if even the
7476        * focus cell is bigger than the page size, we make sure the
7477        * left-hand side of the cell is visible).
7478        *
7479        * If the column does not have those so-called special cells, we
7480        * make sure the left-hand side of the column is visible.
7481        */
7482
7483       if (focus_to_cell && pspp_sheet_view_has_special_cell (tree_view))
7484         {
7485           GtkTreePath *cursor_path;
7486           GdkRectangle background_area, cell_area, focus_area;
7487
7488           cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7489
7490           pspp_sheet_view_get_cell_area (tree_view,
7491                                        cursor_path, column, &cell_area);
7492           pspp_sheet_view_get_background_area (tree_view,
7493                                              cursor_path, column,
7494                                              &background_area);
7495
7496           gtk_tree_path_free (cursor_path);
7497
7498           _pspp_sheet_view_column_get_focus_area (column,
7499                                                 &background_area,
7500                                                 &cell_area,
7501                                                 &focus_area);
7502
7503           x = focus_area.x;
7504           width = focus_area.width;
7505
7506           if (width < gtk_adjustment_get_page_size (tree_view->priv->hadjustment))
7507             {
7508               if ((gtk_adjustment_get_value (tree_view->priv->hadjustment) + gtk_adjustment_get_page_size (tree_view->priv->hadjustment)) < (x + width))
7509                 gtk_adjustment_set_value (tree_view->priv->hadjustment,
7510                                           x + width - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
7511               else if (gtk_adjustment_get_value (tree_view->priv->hadjustment) > x)
7512                 gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
7513             }
7514         }
7515
7516       gtk_adjustment_set_value (tree_view->priv->hadjustment,
7517                                 CLAMP (x,
7518                                        gtk_adjustment_get_lower (tree_view->priv->hadjustment),
7519                                        gtk_adjustment_get_upper (tree_view->priv->hadjustment)
7520                                        - gtk_adjustment_get_page_size (tree_view->priv->hadjustment)));
7521     }
7522   else
7523     {
7524       if ((gtk_adjustment_get_value (tree_view->priv->hadjustment) + gtk_adjustment_get_page_size (tree_view->priv->hadjustment)) < (x + width))
7525           gtk_adjustment_set_value (tree_view->priv->hadjustment,
7526                                     x + width - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
7527       else if (gtk_adjustment_get_value (tree_view->priv->hadjustment) > x)
7528         gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
7529   }
7530 }
7531
7532 GtkTreePath *
7533 _pspp_sheet_view_find_path (PsppSheetView *tree_view,
7534                             int node)
7535 {
7536   GtkTreePath *path;
7537
7538   path = gtk_tree_path_new ();
7539   if (node >= 0)
7540     gtk_tree_path_append_index (path, node);
7541   return path;
7542 }
7543
7544 void
7545 _pspp_sheet_view_find_node (PsppSheetView  *tree_view,
7546                           GtkTreePath  *path,
7547                           int *node)
7548 {
7549   gint *indices = gtk_tree_path_get_indices (path);
7550   gint depth = gtk_tree_path_get_depth (path);
7551
7552   *node = -1;
7553   if (depth == 0 || indices[0] < 0 || indices[0] >= tree_view->priv->row_count)
7554     return;
7555   *node = indices[0];
7556 }
7557
7558 static void
7559 pspp_sheet_view_add_move_binding (GtkBindingSet  *binding_set,
7560                                 guint           keyval,
7561                                 guint           modmask,
7562                                 gboolean        add_shifted_binding,
7563                                 GtkMovementStep step,
7564                                 gint            count)
7565 {
7566   
7567   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
7568                                 "move-cursor", 2,
7569                                 G_TYPE_ENUM, step,
7570                                 G_TYPE_INT, count);
7571
7572   if (add_shifted_binding)
7573     gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
7574                                   "move-cursor", 2,
7575                                   G_TYPE_ENUM, step,
7576                                   G_TYPE_INT, count);
7577
7578   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
7579    return;
7580
7581   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
7582                                 "move-cursor", 2,
7583                                 G_TYPE_ENUM, step,
7584                                 G_TYPE_INT, count);
7585
7586   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
7587                                 "move-cursor", 2,
7588                                 G_TYPE_ENUM, step,
7589                                 G_TYPE_INT, count);
7590 }
7591
7592 static void
7593 pspp_sheet_view_set_column_drag_info (PsppSheetView       *tree_view,
7594                                     PsppSheetViewColumn *column)
7595 {
7596   PsppSheetViewColumn *left_column;
7597   PsppSheetViewColumn *cur_column = NULL;
7598   PsppSheetViewColumnReorder *reorder;
7599   gboolean rtl;
7600   GList *tmp_list;
7601   gint left;
7602
7603   /* We want to precalculate the motion list such that we know what column slots
7604    * are available.
7605    */
7606   left_column = NULL;
7607   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
7608
7609   /* First, identify all possible drop spots */
7610   if (rtl)
7611     tmp_list = g_list_last (tree_view->priv->columns);
7612   else
7613     tmp_list = g_list_first (tree_view->priv->columns);
7614
7615   while (tmp_list)
7616     {
7617       cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
7618       tmp_list = rtl?g_list_previous (tmp_list):g_list_next (tmp_list);
7619
7620       if (cur_column->visible == FALSE)
7621         continue;
7622
7623       /* If it's not the column moving and func tells us to skip over the column, we continue. */
7624       if (left_column != column && cur_column != column &&
7625           tree_view->priv->column_drop_func &&
7626           ! tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
7627         {
7628           left_column = cur_column;
7629           continue;
7630         }
7631       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
7632       reorder->left_column = left_column;
7633       left_column = reorder->right_column = cur_column;
7634
7635       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
7636     }
7637
7638   /* Add the last one */
7639   if (tree_view->priv->column_drop_func == NULL ||
7640       ((left_column != column) &&
7641        tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data)))
7642     {
7643       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
7644       reorder->left_column = left_column;
7645       reorder->right_column = NULL;
7646       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
7647     }
7648
7649   /* We quickly check to see if it even makes sense to reorder columns. */
7650   /* If there is nothing that can be moved, then we return */
7651
7652   if (tree_view->priv->column_drag_info == NULL)
7653     return;
7654
7655   /* We know there are always 2 slots possbile, as you can always return column. */
7656   /* If that's all there is, return */
7657   if (tree_view->priv->column_drag_info->next == NULL || 
7658       (tree_view->priv->column_drag_info->next->next == NULL &&
7659        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->data)->right_column == column &&
7660        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->next->data)->left_column == column))
7661     {
7662       for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
7663         g_slice_free (PsppSheetViewColumnReorder, tmp_list->data);
7664       g_list_free (tree_view->priv->column_drag_info);
7665       tree_view->priv->column_drag_info = NULL;
7666       return;
7667     }
7668   /* We fill in the ranges for the columns, now that we've isolated them */
7669   left = - TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
7670
7671   for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
7672     {
7673       reorder = (PsppSheetViewColumnReorder *) tmp_list->data;
7674
7675       reorder->left_align = left;
7676       if (tmp_list->next != NULL)
7677         {
7678           g_assert (tmp_list->next->data);
7679           left = reorder->right_align = (reorder->right_column->allocation.x +
7680                                          reorder->right_column->allocation.width +
7681                                          ((PsppSheetViewColumnReorder *)tmp_list->next->data)->left_column->allocation.x)/2;
7682         }
7683       else
7684         {
7685           gint width = gdk_window_get_width (tree_view->priv->header_window);
7686           reorder->right_align = width + TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
7687         }
7688     }
7689 }
7690
7691 void
7692 _pspp_sheet_view_column_start_drag (PsppSheetView       *tree_view,
7693                                   PsppSheetViewColumn *column)
7694 {
7695   GdkEvent *send_event;
7696   GtkAllocation allocation;
7697   gint x, y;
7698   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
7699   GdkDisplay *display = gdk_screen_get_display (screen);
7700
7701   g_return_if_fail (tree_view->priv->column_drag_info == NULL);
7702   g_return_if_fail (tree_view->priv->cur_reorder == NULL);
7703   g_return_if_fail (column->button);
7704
7705   pspp_sheet_view_set_column_drag_info (tree_view, column);
7706
7707   if (tree_view->priv->column_drag_info == NULL)
7708     return;
7709
7710   if (tree_view->priv->drag_window == NULL)
7711     {
7712       GdkWindowAttr attributes;
7713       guint attributes_mask;
7714
7715       attributes.window_type = GDK_WINDOW_CHILD;
7716       attributes.wclass = GDK_INPUT_OUTPUT;
7717       attributes.x = column->allocation.x;
7718       attributes.y = 0;
7719       attributes.width = column->allocation.width;
7720       attributes.height = column->allocation.height;
7721       attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
7722       attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
7723       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL ;
7724
7725       tree_view->priv->drag_window = gdk_window_new (tree_view->priv->bin_window,
7726                                                      &attributes,
7727                                                      attributes_mask);
7728       gdk_window_set_user_data (tree_view->priv->drag_window, GTK_WIDGET (tree_view));
7729     }
7730
7731   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
7732   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
7733
7734   gtk_grab_remove (column->button);
7735
7736   send_event = gdk_event_new (GDK_LEAVE_NOTIFY);
7737   send_event->crossing.send_event = TRUE;
7738   send_event->crossing.window = g_object_ref (gtk_button_get_event_window (GTK_BUTTON (column->button)));
7739   send_event->crossing.subwindow = NULL;
7740   send_event->crossing.detail = GDK_NOTIFY_ANCESTOR;
7741   send_event->crossing.time = GDK_CURRENT_TIME;
7742
7743   gtk_propagate_event (column->button, send_event);
7744   gdk_event_free (send_event);
7745
7746   send_event = gdk_event_new (GDK_BUTTON_RELEASE);
7747   send_event->button.window = g_object_ref (gdk_screen_get_root_window (screen));
7748   send_event->button.send_event = TRUE;
7749   send_event->button.time = GDK_CURRENT_TIME;
7750   send_event->button.x = -1;
7751   send_event->button.y = -1;
7752   send_event->button.axes = NULL;
7753   send_event->button.state = 0;
7754   send_event->button.button = 1;
7755   send_event->button.device = 
7756     gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (display));
7757
7758   send_event->button.x_root = 0;
7759   send_event->button.y_root = 0;
7760
7761   gtk_propagate_event (column->button, send_event);
7762   gdk_event_free (send_event);
7763
7764   /* Kids, don't try this at home */
7765   g_object_ref (column->button);
7766   gtk_container_remove (GTK_CONTAINER (tree_view), column->button);
7767   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
7768   gtk_widget_set_parent (column->button, GTK_WIDGET (tree_view));
7769   g_object_unref (column->button);
7770
7771   tree_view->priv->drag_column_x = column->allocation.x;
7772   allocation = column->allocation;
7773   allocation.x = 0;
7774   gtk_widget_size_allocate (column->button, &allocation);
7775   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
7776
7777   tree_view->priv->drag_column = column;
7778   gdk_window_show (tree_view->priv->drag_window);
7779
7780   gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
7781
7782   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
7783   while (gtk_events_pending ())
7784     gtk_main_iteration ();
7785
7786   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
7787   gdk_pointer_grab (tree_view->priv->drag_window,
7788                     FALSE,
7789                     GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK,
7790                     NULL, NULL, GDK_CURRENT_TIME);
7791   gdk_keyboard_grab (tree_view->priv->drag_window,
7792                      FALSE,
7793                      GDK_CURRENT_TIME);
7794 }
7795
7796 void
7797 _pspp_sheet_view_queue_draw_node (PsppSheetView        *tree_view,
7798                                 int node,
7799                                 const GdkRectangle *clip_rect)
7800 {
7801   GdkRectangle rect;
7802   GtkAllocation allocation;
7803
7804   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
7805     return;
7806
7807   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
7808   rect.x = 0;
7809   rect.width = MAX (tree_view->priv->width, allocation.width);
7810
7811   rect.y = BACKGROUND_FIRST_PIXEL (tree_view, node);
7812   rect.height = ROW_HEIGHT (tree_view);
7813
7814   if (clip_rect)
7815     {
7816       GdkRectangle new_rect;
7817
7818       gdk_rectangle_intersect (clip_rect, &rect, &new_rect);
7819
7820       gdk_window_invalidate_rect (tree_view->priv->bin_window, &new_rect, TRUE);
7821     }
7822   else
7823     {
7824       gdk_window_invalidate_rect (tree_view->priv->bin_window, &rect, TRUE);
7825     }
7826 }
7827
7828 static void
7829 pspp_sheet_view_queue_draw_path (PsppSheetView        *tree_view,
7830                                GtkTreePath        *path,
7831                                const GdkRectangle *clip_rect)
7832 {
7833   int node = -1;
7834
7835   _pspp_sheet_view_find_node (tree_view, path, &node);
7836
7837   if (node)
7838     _pspp_sheet_view_queue_draw_node (tree_view, node, clip_rect);
7839 }
7840
7841 static void
7842 pspp_sheet_view_focus_to_cursor (PsppSheetView *tree_view)
7843
7844 {
7845   GtkTreePath *cursor_path;
7846
7847   if ((tree_view->priv->row_count == 0) ||
7848       (! gtk_widget_get_realized (GTK_WIDGET (tree_view))))
7849     return;
7850
7851   cursor_path = NULL;
7852   if (tree_view->priv->cursor)
7853     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7854
7855   if (cursor_path == NULL)
7856     {
7857       /* There's no cursor.  Move the cursor to the first selected row, if any
7858        * are selected, otherwise to the first row in the sheetview.
7859        */
7860       GList *selected_rows;
7861       GtkTreeModel *model;
7862       PsppSheetSelection *selection;
7863
7864       selection = pspp_sheet_view_get_selection (tree_view);
7865       selected_rows = pspp_sheet_selection_get_selected_rows (selection, &model);
7866
7867       if (selected_rows)
7868         {
7869           /* XXX we could avoid doing O(n) work to get this result */
7870           cursor_path = gtk_tree_path_copy((const GtkTreePath *)(selected_rows->data));
7871           g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
7872           g_list_free (selected_rows);
7873         }
7874       else
7875         {
7876           cursor_path = gtk_tree_path_new_first ();
7877           search_first_focusable_path (tree_view, &cursor_path,
7878                                        TRUE, NULL);
7879         }
7880
7881       gtk_tree_row_reference_free (tree_view->priv->cursor);
7882       tree_view->priv->cursor = NULL;
7883
7884       if (cursor_path)
7885         {
7886           if (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
7887               tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE)
7888             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, FALSE, FALSE, 0);
7889           else
7890             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE, 0);
7891         }
7892     }
7893
7894   if (cursor_path)
7895     {
7896       /* Now find a column for the cursor. */
7897       PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
7898
7899       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
7900       gtk_tree_path_free (cursor_path);
7901
7902       if (tree_view->priv->focus_column == NULL)
7903         {
7904           GList *list;
7905           for (list = tree_view->priv->columns; list; list = list->next)
7906             {
7907               if (PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
7908                 {
7909                   tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
7910                   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
7911                   pspp_sheet_selection_select_column (tree_view->priv->selection, tree_view->priv->focus_column);
7912                   break;
7913                 }
7914             }
7915
7916         }
7917     }
7918 }
7919
7920 static gboolean
7921 pspp_sheet_view_move_cursor_up_down (PsppSheetView *tree_view,
7922                                    gint         count,
7923                                    PsppSheetSelectMode mode)
7924 {
7925   gint selection_count;
7926   int cursor_node = -1;
7927   int new_cursor_node = -1;
7928   GtkTreePath *cursor_path = NULL;
7929   gboolean grab_focus = TRUE;
7930
7931   if (! gtk_widget_has_focus (GTK_WIDGET (tree_view)))
7932     return FALSE;
7933
7934   cursor_path = NULL;
7935   if (!gtk_tree_row_reference_valid (tree_view->priv->cursor))
7936     /* FIXME: we lost the cursor; should we get the first? */
7937     return FALSE;
7938
7939   cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
7940   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
7941
7942   if (cursor_node < 0)
7943     /* FIXME: we lost the cursor; should we get the first? */
7944     return FALSE;
7945
7946   selection_count = pspp_sheet_selection_count_selected_rows (tree_view->priv->selection);
7947
7948   if (selection_count == 0
7949       && tree_view->priv->selection->type != PSPP_SHEET_SELECTION_NONE
7950       && !(mode & PSPP_SHEET_SELECT_MODE_TOGGLE))
7951     {
7952       /* Don't move the cursor, but just select the current node */
7953       new_cursor_node = cursor_node;
7954     }
7955   else
7956     {
7957       if (count == -1)
7958         new_cursor_node = pspp_sheet_view_node_prev (tree_view, cursor_node);
7959       else
7960         new_cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
7961     }
7962
7963   gtk_tree_path_free (cursor_path);
7964
7965   if (new_cursor_node)
7966     {
7967       cursor_path = _pspp_sheet_view_find_path (tree_view, new_cursor_node);
7968
7969       search_first_focusable_path (tree_view, &cursor_path,
7970                                    (count != -1),
7971                                    &new_cursor_node);
7972
7973       if (cursor_path)
7974         gtk_tree_path_free (cursor_path);
7975     }
7976
7977   /*
7978    * If the list has only one item and multi-selection is set then select
7979    * the row (if not yet selected).
7980    */
7981   if ((tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
7982        tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE) &&
7983       new_cursor_node < 0)
7984     {
7985       if (count == -1)
7986         new_cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
7987       else
7988         new_cursor_node = pspp_sheet_view_node_prev (tree_view, cursor_node);
7989
7990       if (new_cursor_node < 0
7991           && !pspp_sheet_view_node_is_selected (tree_view, cursor_node))
7992         {
7993           new_cursor_node = cursor_node;
7994         }
7995       else
7996         {
7997           new_cursor_node = -1;
7998         }
7999     }
8000
8001   if (new_cursor_node >= 0)
8002     {
8003       cursor_path = _pspp_sheet_view_find_path (tree_view, new_cursor_node);
8004       pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, TRUE, mode);
8005       gtk_tree_path_free (cursor_path);
8006     }
8007   else
8008     {
8009       pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8010
8011       if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND))
8012         {
8013           if (! gtk_widget_keynav_failed (GTK_WIDGET (tree_view),
8014                                           count < 0 ?
8015                                           GTK_DIR_UP : GTK_DIR_DOWN))
8016             {
8017               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
8018
8019               if (toplevel)
8020                 gtk_widget_child_focus (toplevel,
8021                                         count < 0 ?
8022                                         GTK_DIR_TAB_BACKWARD :
8023                                         GTK_DIR_TAB_FORWARD);
8024
8025               grab_focus = FALSE;
8026             }
8027         }
8028       else
8029         {
8030           gtk_widget_error_bell (GTK_WIDGET (tree_view));
8031         }
8032     }
8033
8034   if (grab_focus)
8035     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8036
8037   return new_cursor_node >= 0;
8038 }
8039
8040 static void
8041 pspp_sheet_view_move_cursor_page_up_down (PsppSheetView *tree_view,
8042                                           gint         count,
8043                                           PsppSheetSelectMode mode)
8044 {
8045   int cursor_node = -1;
8046   GtkTreePath *old_cursor_path = NULL;
8047   GtkTreePath *cursor_path = NULL;
8048   int start_cursor_node = -1;
8049   gint y;
8050   gint window_y;
8051   gint vertical_separator;
8052
8053   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8054     return;
8055
8056   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8057     old_cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8058   else
8059     /* This is sorta weird.  Focus in should give us a cursor */
8060     return;
8061
8062   gtk_widget_style_get (GTK_WIDGET (tree_view), "vertical-separator", &vertical_separator, NULL);
8063   _pspp_sheet_view_find_node (tree_view, old_cursor_path, &cursor_node);
8064
8065   if (cursor_node < 0)
8066     {
8067       /* FIXME: we lost the cursor.  Should we try to get one? */
8068       gtk_tree_path_free (old_cursor_path);
8069       return;
8070     }
8071
8072   y = pspp_sheet_view_node_find_offset (tree_view, cursor_node);
8073   window_y = RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, y);
8074   y += tree_view->priv->cursor_offset;
8075   y += count * (int)gtk_adjustment_get_page_increment (tree_view->priv->vadjustment);
8076   y = CLAMP (y, (gint)gtk_adjustment_get_lower (tree_view->priv->vadjustment),  (gint)gtk_adjustment_get_upper (tree_view->priv->vadjustment) - vertical_separator);
8077
8078   if (y >= tree_view->priv->height)
8079     y = tree_view->priv->height - 1;
8080
8081   tree_view->priv->cursor_offset =
8082     pspp_sheet_view_find_offset (tree_view, y, &cursor_node);
8083
8084   if (tree_view->priv->cursor_offset > BACKGROUND_HEIGHT (tree_view))
8085     {
8086       cursor_node = pspp_sheet_view_node_next (tree_view, cursor_node);
8087       tree_view->priv->cursor_offset -= BACKGROUND_HEIGHT (tree_view);
8088     }
8089
8090   y -= tree_view->priv->cursor_offset;
8091   cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_node);
8092
8093   start_cursor_node = cursor_node;
8094
8095   if (! search_first_focusable_path (tree_view, &cursor_path,
8096                                      (count != -1),
8097                                      &cursor_node))
8098     {
8099       /* It looks like we reached the end of the view without finding
8100        * a focusable row.  We will step backwards to find the last
8101        * focusable row.
8102        */
8103       cursor_node = start_cursor_node;
8104       cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_node);
8105
8106       search_first_focusable_path (tree_view, &cursor_path,
8107                                    (count == -1),
8108                                    &cursor_node);
8109     }
8110
8111   if (!cursor_path)
8112     goto cleanup;
8113
8114   /* update y */
8115   y = pspp_sheet_view_node_find_offset (tree_view, cursor_node);
8116
8117   pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE, mode);
8118
8119   y -= window_y;
8120   pspp_sheet_view_scroll_to_point (tree_view, -1, y);
8121   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8122   _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8123
8124   if (!gtk_tree_path_compare (old_cursor_path, cursor_path))
8125     gtk_widget_error_bell (GTK_WIDGET (tree_view));
8126
8127   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8128
8129 cleanup:
8130   gtk_tree_path_free (old_cursor_path);
8131   gtk_tree_path_free (cursor_path);
8132 }
8133
8134 static void
8135 pspp_sheet_view_move_cursor_left_right (PsppSheetView *tree_view,
8136                                         gint         count,
8137                                         PsppSheetSelectMode mode)
8138 {
8139   int cursor_node = -1;
8140   GtkTreePath *cursor_path = NULL;
8141   PsppSheetViewColumn *column;
8142   GtkTreeIter iter;
8143   GList *list;
8144   gboolean found_column = FALSE;
8145   gboolean rtl;
8146
8147   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8148
8149   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8150     return;
8151
8152   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8153     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8154   else
8155     return;
8156
8157   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8158   if (cursor_node < 0)
8159     return;
8160   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8161     {
8162       gtk_tree_path_free (cursor_path);
8163       return;
8164     }
8165   gtk_tree_path_free (cursor_path);
8166
8167   list = rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns);
8168   if (tree_view->priv->focus_column)
8169     {
8170       for (; list; list = (rtl ? list->prev : list->next))
8171         {
8172           if (list->data == tree_view->priv->focus_column)
8173             break;
8174         }
8175     }
8176
8177   while (list)
8178     {
8179       gboolean left, right;
8180
8181       column = list->data;
8182       if (column->visible == FALSE || column->row_head)
8183         goto loop_end;
8184
8185       pspp_sheet_view_column_cell_set_cell_data (column,
8186                                                tree_view->priv->model,
8187                                                &iter);
8188
8189       if (rtl)
8190         {
8191           right = list->prev ? TRUE : FALSE;
8192           left = list->next ? TRUE : FALSE;
8193         }
8194       else
8195         {
8196           left = list->prev ? TRUE : FALSE;
8197           right = list->next ? TRUE : FALSE;
8198         }
8199
8200       if (_pspp_sheet_view_column_cell_focus (column, count, left, right))
8201         {
8202           tree_view->priv->focus_column = column;
8203           found_column = TRUE;
8204           break;
8205         }
8206     loop_end:
8207       if (count == 1)
8208         list = rtl ? list->prev : list->next;
8209       else
8210         list = rtl ? list->next : list->prev;
8211     }
8212
8213   if (found_column)
8214     {
8215       _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8216       g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8217       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8218     }
8219   else
8220     {
8221       gtk_widget_error_bell (GTK_WIDGET (tree_view));
8222     }
8223
8224   pspp_sheet_view_clamp_column_visible (tree_view,
8225                                       tree_view->priv->focus_column, TRUE);
8226 }
8227
8228 static void
8229 pspp_sheet_view_move_cursor_line_start_end (PsppSheetView *tree_view,
8230                                             gint         count,
8231                                             PsppSheetSelectMode mode)
8232 {
8233   int cursor_node = -1;
8234   GtkTreePath *cursor_path = NULL;
8235   PsppSheetViewColumn *column;
8236   PsppSheetViewColumn *found_column;
8237   GtkTreeIter iter;
8238   GList *list;
8239   gboolean rtl;
8240
8241   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8242
8243   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8244     return;
8245
8246   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8247     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8248   else
8249     return;
8250
8251   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8252   if (cursor_node < 0)
8253     return;
8254   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8255     {
8256       gtk_tree_path_free (cursor_path);
8257       return;
8258     }
8259   gtk_tree_path_free (cursor_path);
8260
8261   list = rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns);
8262   if (tree_view->priv->focus_column)
8263     {
8264       for (; list; list = (rtl ? list->prev : list->next))
8265         {
8266           if (list->data == tree_view->priv->focus_column)
8267             break;
8268         }
8269     }
8270
8271   found_column = NULL;
8272   while (list)
8273     {
8274       gboolean left, right;
8275
8276       column = list->data;
8277       if (column->visible == FALSE || column->row_head)
8278         goto loop_end;
8279
8280       pspp_sheet_view_column_cell_set_cell_data (column,
8281                                                tree_view->priv->model,
8282                                                &iter);
8283
8284       if (rtl)
8285         {
8286           right = list->prev ? TRUE : FALSE;
8287           left = list->next ? TRUE : FALSE;
8288         }
8289       else
8290         {
8291           left = list->prev ? TRUE : FALSE;
8292           right = list->next ? TRUE : FALSE;
8293         }
8294
8295       if (column->tabbable
8296           && _pspp_sheet_view_column_cell_focus (column, count, left, right))
8297         found_column = column;
8298
8299     loop_end:
8300       if (count == 1)
8301         list = rtl ? list->prev : list->next;
8302       else
8303         list = rtl ? list->next : list->prev;
8304     }
8305
8306   if (found_column)
8307     {
8308       tree_view->priv->focus_column = found_column;
8309       _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8310       g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8311       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8312     }
8313
8314   pspp_sheet_view_clamp_column_visible (tree_view,
8315                                       tree_view->priv->focus_column, TRUE);
8316 }
8317
8318 static gboolean
8319 try_move_cursor_tab (PsppSheetView *tree_view,
8320                      gboolean start_at_focus_column,
8321                      gint count)
8322 {
8323   PsppSheetViewColumn *column;
8324   GtkTreeIter iter;
8325   int cursor_node = -1;
8326   GtkTreePath *cursor_path = NULL;
8327   gboolean rtl;
8328   GList *list;
8329
8330   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
8331     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8332   else
8333     return TRUE;
8334
8335   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8336   if (cursor_node < 0)
8337     return TRUE;
8338   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
8339     {
8340       gtk_tree_path_free (cursor_path);
8341       return TRUE;
8342     }
8343   gtk_tree_path_free (cursor_path);
8344
8345   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
8346   if (start_at_focus_column)
8347     {
8348       list = (rtl
8349               ? g_list_last (tree_view->priv->columns)
8350               : g_list_first (tree_view->priv->columns));
8351       if (tree_view->priv->focus_column)
8352         {
8353           for (; list; list = (rtl ? list->prev : list->next))
8354             {
8355               if (list->data == tree_view->priv->focus_column)
8356                 break;
8357             }
8358         }
8359     }
8360   else
8361     {
8362       list = (rtl ^ (count == 1)
8363               ? g_list_first (tree_view->priv->columns)
8364               : g_list_last (tree_view->priv->columns));
8365     }
8366
8367   while (list)
8368     {
8369       gboolean left, right;
8370
8371       column = list->data;
8372       if (column->visible == FALSE || !column->tabbable)
8373         goto loop_end;
8374
8375       pspp_sheet_view_column_cell_set_cell_data (column,
8376                                                  tree_view->priv->model,
8377                                                  &iter);
8378
8379       if (rtl)
8380         {
8381           right = list->prev ? TRUE : FALSE;
8382           left = list->next ? TRUE : FALSE;
8383         }
8384       else
8385         {
8386           left = list->prev ? TRUE : FALSE;
8387           right = list->next ? TRUE : FALSE;
8388         }
8389
8390       if (column->tabbable
8391           && _pspp_sheet_view_column_cell_focus (column, count, left, right))
8392         {
8393           tree_view->priv->focus_column = column;
8394           _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8395           g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
8396           gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8397           return TRUE;
8398         }
8399     loop_end:
8400       if (count == 1)
8401         list = rtl ? list->prev : list->next;
8402       else
8403         list = rtl ? list->next : list->prev;
8404     }
8405
8406   return FALSE;
8407 }
8408
8409 static void
8410 pspp_sheet_view_move_cursor_tab (PsppSheetView *tree_view,
8411                                  gint         count)
8412 {
8413   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8414     return;
8415
8416   if (!try_move_cursor_tab (tree_view, TRUE, count))
8417     {
8418       if (pspp_sheet_view_move_cursor_up_down (tree_view, count, 0)
8419           && !try_move_cursor_tab (tree_view, FALSE, count))
8420         gtk_widget_error_bell (GTK_WIDGET (tree_view));
8421     }
8422
8423   pspp_sheet_view_clamp_column_visible (tree_view,
8424                                         tree_view->priv->focus_column, TRUE);
8425 }
8426
8427 static void
8428 pspp_sheet_view_move_cursor_start_end (PsppSheetView *tree_view,
8429                                        gint         count,
8430                                        PsppSheetSelectMode mode)
8431 {
8432   int cursor_node;
8433   GtkTreePath *path;
8434   GtkTreePath *old_path;
8435
8436   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8437     return;
8438
8439   g_return_if_fail (tree_view->priv->row_count > 0);
8440
8441   pspp_sheet_view_get_cursor (tree_view, &old_path, NULL);
8442
8443   if (count == -1)
8444     {
8445       /* Now go forward to find the first focusable row. */
8446       path = _pspp_sheet_view_find_path (tree_view, 0);
8447       search_first_focusable_path (tree_view, &path,
8448                                    TRUE, &cursor_node);
8449     }
8450   else
8451     {
8452       /* Now go backwards to find last focusable row. */
8453       path = _pspp_sheet_view_find_path (tree_view, tree_view->priv->row_count - 1);
8454       search_first_focusable_path (tree_view, &path,
8455                                    FALSE, &cursor_node);
8456     }
8457
8458   if (!path)
8459     goto cleanup;
8460
8461   if (gtk_tree_path_compare (old_path, path))
8462     {
8463       pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, mode);
8464       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8465     }
8466   else
8467     {
8468       gtk_widget_error_bell (GTK_WIDGET (tree_view));
8469     }
8470
8471 cleanup:
8472   gtk_tree_path_free (old_path);
8473   gtk_tree_path_free (path);
8474 }
8475
8476 static gboolean
8477 pspp_sheet_view_real_select_all (PsppSheetView *tree_view)
8478 {
8479   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8480     return FALSE;
8481
8482   if (tree_view->priv->selection->type != PSPP_SHEET_SELECTION_MULTIPLE &&
8483       tree_view->priv->selection->type != PSPP_SHEET_SELECTION_RECTANGLE)
8484     return FALSE;
8485
8486   pspp_sheet_selection_select_all (tree_view->priv->selection);
8487
8488   return TRUE;
8489 }
8490
8491 static gboolean
8492 pspp_sheet_view_real_unselect_all (PsppSheetView *tree_view)
8493 {
8494   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8495     return FALSE;
8496
8497   if (tree_view->priv->selection->type != PSPP_SHEET_SELECTION_MULTIPLE &&
8498       tree_view->priv->selection->type != PSPP_SHEET_SELECTION_RECTANGLE)
8499     return FALSE;
8500
8501   pspp_sheet_selection_unselect_all (tree_view->priv->selection);
8502
8503   return TRUE;
8504 }
8505
8506 static gboolean
8507 pspp_sheet_view_real_select_cursor_row (PsppSheetView *tree_view,
8508                                         gboolean     start_editing,
8509                                         PsppSheetSelectMode mode)
8510 {
8511   int new_node = -1;
8512   int cursor_node = -1;
8513   GtkTreePath *cursor_path = NULL;
8514
8515   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8516     return FALSE;
8517
8518   if (tree_view->priv->cursor)
8519     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8520
8521   if (cursor_path == NULL)
8522     return FALSE;
8523
8524   _pspp_sheet_view_find_node (tree_view, cursor_path,
8525                               &cursor_node);
8526
8527   if (cursor_node < 0)
8528     {
8529       gtk_tree_path_free (cursor_path);
8530       return FALSE;
8531     }
8532
8533   if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND) && start_editing &&
8534       tree_view->priv->focus_column)
8535     {
8536       if (pspp_sheet_view_start_editing (tree_view, cursor_path))
8537         {
8538           gtk_tree_path_free (cursor_path);
8539           return TRUE;
8540         }
8541     }
8542
8543   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
8544                                             cursor_node,
8545                                             cursor_path,
8546                                             mode,
8547                                             FALSE);
8548
8549   /* We bail out if the original (tree, node) don't exist anymore after
8550    * handling the selection-changed callback.  We do return TRUE because
8551    * the key press has been handled at this point.
8552    */
8553   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_node);
8554
8555   if (cursor_node != new_node)
8556     return FALSE;
8557
8558   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8559
8560   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8561   _pspp_sheet_view_queue_draw_node (tree_view, cursor_node, NULL);
8562
8563   if (!(mode & PSPP_SHEET_SELECT_MODE_EXTEND))
8564     pspp_sheet_view_row_activated (tree_view, cursor_path,
8565                                  tree_view->priv->focus_column);
8566     
8567   gtk_tree_path_free (cursor_path);
8568
8569   return TRUE;
8570 }
8571
8572 static gboolean
8573 pspp_sheet_view_real_toggle_cursor_row (PsppSheetView *tree_view)
8574 {
8575   int new_node = -1;
8576   int cursor_node = -1;
8577   GtkTreePath *cursor_path = NULL;
8578
8579   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8580     return FALSE;
8581
8582   cursor_path = NULL;
8583   if (tree_view->priv->cursor)
8584     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8585
8586   if (cursor_path == NULL)
8587     return FALSE;
8588
8589   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
8590   if (cursor_node < 0)
8591     {
8592       gtk_tree_path_free (cursor_path);
8593       return FALSE;
8594     }
8595
8596   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
8597                                             cursor_node,
8598                                             cursor_path,
8599                                             PSPP_SHEET_SELECT_MODE_TOGGLE,
8600                                             FALSE);
8601
8602   /* We bail out if the original (tree, node) don't exist anymore after
8603    * handling the selection-changed callback.  We do return TRUE because
8604    * the key press has been handled at this point.
8605    */
8606   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_node);
8607
8608   if (cursor_node != new_node)
8609     return FALSE;
8610
8611   pspp_sheet_view_clamp_node_visible (tree_view, cursor_node);
8612
8613   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8614   pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
8615   gtk_tree_path_free (cursor_path);
8616
8617   return TRUE;
8618 }
8619
8620 static gboolean
8621 pspp_sheet_view_search_entry_flush_timeout (PsppSheetView *tree_view)
8622 {
8623   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
8624   tree_view->priv->typeselect_flush_timeout = 0;
8625
8626   return FALSE;
8627 }
8628
8629 /* Cut and paste from gtkwindow.c */
8630 static void
8631 send_focus_change (GtkWidget *widget,
8632                    gboolean   in)
8633 {
8634   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
8635
8636   fevent->focus_change.type = GDK_FOCUS_CHANGE;
8637   fevent->focus_change.window = g_object_ref (gtk_widget_get_window (widget));
8638   fevent->focus_change.in = in;
8639   
8640   gtk_widget_send_focus_change (widget, fevent);
8641   gdk_event_free (fevent);
8642 }
8643
8644 static void
8645 pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view)
8646 {
8647   GtkWidget *frame, *vbox, *toplevel;
8648   GdkScreen *screen;
8649
8650   if (tree_view->priv->search_custom_entry_set)
8651     return;
8652
8653   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
8654   screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
8655
8656    if (tree_view->priv->search_window != NULL)
8657      {
8658        if (gtk_window_get_group (GTK_WINDOW (toplevel)))
8659          gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
8660                                       GTK_WINDOW (tree_view->priv->search_window));
8661        else if (gtk_window_get_group (GTK_WINDOW (tree_view->priv->search_window)))
8662          gtk_window_group_remove_window (gtk_window_get_group (GTK_WINDOW (tree_view->priv->search_window)),
8663                                          GTK_WINDOW (tree_view->priv->search_window));
8664        gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
8665        return;
8666      }
8667    
8668   tree_view->priv->search_window = gtk_window_new (GTK_WINDOW_POPUP);
8669   gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
8670
8671   if (gtk_window_get_group (GTK_WINDOW (toplevel)))
8672     gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
8673                                  GTK_WINDOW (tree_view->priv->search_window));
8674
8675   gtk_window_set_type_hint (GTK_WINDOW (tree_view->priv->search_window),
8676                             GDK_WINDOW_TYPE_HINT_UTILITY);
8677   gtk_window_set_modal (GTK_WINDOW (tree_view->priv->search_window), TRUE);
8678   g_signal_connect (tree_view->priv->search_window, "delete-event",
8679                     G_CALLBACK (pspp_sheet_view_search_delete_event),
8680                     tree_view);
8681   g_signal_connect (tree_view->priv->search_window, "key-press-event",
8682                     G_CALLBACK (pspp_sheet_view_search_key_press_event),
8683                     tree_view);
8684   g_signal_connect (tree_view->priv->search_window, "button-press-event",
8685                     G_CALLBACK (pspp_sheet_view_search_button_press_event),
8686                     tree_view);
8687   g_signal_connect (tree_view->priv->search_window, "scroll-event",
8688                     G_CALLBACK (pspp_sheet_view_search_scroll_event),
8689                     tree_view);
8690
8691   frame = gtk_frame_new (NULL);
8692   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
8693   gtk_widget_show (frame);
8694   gtk_container_add (GTK_CONTAINER (tree_view->priv->search_window), frame);
8695
8696   vbox = gtk_vbox_new (FALSE, 0);
8697   gtk_widget_show (vbox);
8698   gtk_container_add (GTK_CONTAINER (frame), vbox);
8699   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
8700
8701   /* add entry */
8702   tree_view->priv->search_entry = gtk_entry_new ();
8703   gtk_widget_show (tree_view->priv->search_entry);
8704   g_signal_connect (tree_view->priv->search_entry, "populate-popup",
8705                     G_CALLBACK (pspp_sheet_view_search_disable_popdown),
8706                     tree_view);
8707   g_signal_connect (tree_view->priv->search_entry,
8708                     "activate", G_CALLBACK (pspp_sheet_view_search_activate),
8709                     tree_view);
8710
8711 #if GTK3_TRANSITION
8712   g_signal_connect (GTK_ENTRY (tree_view->priv->search_entry)->im_context,
8713                     "preedit-changed",
8714                     G_CALLBACK (pspp_sheet_view_search_preedit_changed),
8715                     tree_view);
8716 #endif
8717
8718   gtk_container_add (GTK_CONTAINER (vbox),
8719                      tree_view->priv->search_entry);
8720
8721   gtk_widget_realize (tree_view->priv->search_entry);
8722 }
8723
8724 /* Pops up the interactive search entry.  If keybinding is TRUE then the user
8725  * started this by typing the start_interactive_search keybinding.  Otherwise, it came from 
8726  */
8727 static gboolean
8728 pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
8729                                              gboolean     keybinding)
8730 {
8731   /* We only start interactive search if we have focus or the columns
8732    * have focus.  If one of our children have focus, we don't want to
8733    * start the search.
8734    */
8735   GList *list;
8736   gboolean found_focus = FALSE;
8737   GtkWidgetClass *entry_parent_class;
8738   
8739   if (!tree_view->priv->enable_search && !keybinding)
8740     return FALSE;
8741
8742   if (tree_view->priv->search_custom_entry_set)
8743     return FALSE;
8744
8745   if (tree_view->priv->search_window != NULL &&
8746       gtk_widget_get_visible (tree_view->priv->search_window))
8747     return TRUE;
8748
8749   for (list = tree_view->priv->columns; list; list = list->next)
8750     {
8751       PsppSheetViewColumn *column;
8752
8753       column = list->data;
8754       if (! column->visible)
8755         continue;
8756
8757       if (column->button && gtk_widget_has_focus (column->button))
8758         {
8759           found_focus = TRUE;
8760           break;
8761         }
8762     }
8763   
8764   if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8765     found_focus = TRUE;
8766
8767   if (!found_focus)
8768     return FALSE;
8769
8770   if (tree_view->priv->search_column < 0)
8771     return FALSE;
8772
8773   pspp_sheet_view_ensure_interactive_directory (tree_view);
8774
8775   if (keybinding)
8776     gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
8777
8778   /* done, show it */
8779   tree_view->priv->search_position_func (tree_view, tree_view->priv->search_window, tree_view->priv->search_position_user_data);
8780   gtk_widget_show (tree_view->priv->search_window);
8781   if (tree_view->priv->search_entry_changed_id == 0)
8782     {
8783       tree_view->priv->search_entry_changed_id =
8784         g_signal_connect (tree_view->priv->search_entry, "changed",
8785                           G_CALLBACK (pspp_sheet_view_search_init),
8786                           tree_view);
8787     }
8788
8789   tree_view->priv->typeselect_flush_timeout =
8790     gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
8791                    (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
8792                    tree_view);
8793
8794   /* Grab focus will select all the text.  We don't want that to happen, so we
8795    * call the parent instance and bypass the selection change.  This is probably
8796    * really non-kosher. */
8797   entry_parent_class = g_type_class_peek_parent (GTK_ENTRY_GET_CLASS (tree_view->priv->search_entry));
8798   (entry_parent_class->grab_focus) (tree_view->priv->search_entry);
8799
8800   /* send focus-in event */
8801   send_focus_change (tree_view->priv->search_entry, TRUE);
8802
8803   /* search first matching iter */
8804   pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
8805
8806   return TRUE;
8807 }
8808
8809 static gboolean
8810 pspp_sheet_view_start_interactive_search (PsppSheetView *tree_view)
8811 {
8812   return pspp_sheet_view_real_start_interactive_search (tree_view, TRUE);
8813 }
8814
8815 /* this function returns the new width of the column being resized given
8816  * the column and x position of the cursor; the x cursor position is passed
8817  * in as a pointer and automagicly corrected if it's beyond min/max limits
8818  */
8819 static gint
8820 pspp_sheet_view_new_column_width (PsppSheetView *tree_view,
8821                                 gint       i,
8822                                 gint      *x)
8823 {
8824   PsppSheetViewColumn *column;
8825   gint width;
8826   gboolean rtl;
8827
8828   /* first translate the x position from gtk_widget_get_window (widget)
8829    * to clist->clist_window
8830    */
8831   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8832   column = g_list_nth (tree_view->priv->columns, i)->data;
8833   width = rtl ? (column->allocation.x + column->allocation.width - *x) : (*x - column->allocation.x);
8834  
8835   /* Clamp down the value */
8836   if (column->min_width == -1)
8837     width = MAX (column->button_request, width);
8838   else
8839     width = MAX (column->min_width, width);
8840   if (column->max_width != -1)
8841     width = MIN (width, column->max_width);
8842
8843   *x = rtl ? (column->allocation.x + column->allocation.width - width) : (column->allocation.x + width);
8844  
8845   return width;
8846 }
8847
8848
8849 /* FIXME this adjust_allocation is a big cut-and-paste from
8850  * GtkCList, needs to be some "official" way to do this
8851  * factored out.
8852  */
8853 typedef struct
8854 {
8855   GdkWindow *window;
8856   int dx;
8857   int dy;
8858 } ScrollData;
8859
8860 /* The window to which gtk_widget_get_window (widget) is relative */
8861 #define ALLOCATION_WINDOW(widget)               \
8862    (!gtk_widget_get_has_window (widget) ?               \
8863     gtk_widget_get_window (widget) :                          \
8864     gdk_window_get_parent (gtk_widget_get_window (widget)))
8865
8866 static void
8867 adjust_allocation_recurse (GtkWidget *widget,
8868                            gpointer   data)
8869 {
8870   ScrollData *scroll_data = data;
8871   GtkAllocation allocation;
8872   gtk_widget_get_allocation (widget, &allocation);
8873   /* Need to really size allocate instead of just poking
8874    * into widget->allocation if the widget is not realized.
8875    * FIXME someone figure out why this was.
8876    */
8877   if (!gtk_widget_get_realized (widget))
8878     {
8879       if (gtk_widget_get_visible (widget))
8880         {
8881           GdkRectangle tmp_rectangle = allocation;
8882           tmp_rectangle.x += scroll_data->dx;
8883           tmp_rectangle.y += scroll_data->dy;
8884           
8885           gtk_widget_size_allocate (widget, &tmp_rectangle);
8886         }
8887     }
8888   else
8889     {
8890       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
8891         {
8892           allocation.x += scroll_data->dx;
8893           allocation.y += scroll_data->dy;
8894           
8895           if (GTK_IS_CONTAINER (widget))
8896             gtk_container_forall (GTK_CONTAINER (widget),
8897                                   adjust_allocation_recurse,
8898                                   data);
8899         }
8900     }
8901 }
8902
8903 static void
8904 adjust_allocation (GtkWidget *widget,
8905                    int        dx,
8906                    int        dy)
8907 {
8908   ScrollData scroll_data;
8909
8910   if (gtk_widget_get_realized (widget))
8911     scroll_data.window = ALLOCATION_WINDOW (widget);
8912   else
8913     scroll_data.window = NULL;
8914     
8915   scroll_data.dx = dx;
8916   scroll_data.dy = dy;
8917   
8918   adjust_allocation_recurse (widget, &scroll_data);
8919 }
8920
8921 void 
8922 pspp_sheet_view_column_update_button (PsppSheetViewColumn *tree_column);
8923
8924 /* Callbacks */
8925 static void
8926 pspp_sheet_view_adjustment_changed (GtkAdjustment *adjustment,
8927                                   PsppSheetView   *tree_view)
8928 {
8929   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8930     {
8931       GList *list;
8932       gint dy;
8933         
8934       gdk_window_move (tree_view->priv->bin_window,
8935                        - gtk_adjustment_get_value (tree_view->priv->hadjustment),
8936                        TREE_VIEW_HEADER_HEIGHT (tree_view));
8937       gdk_window_move (tree_view->priv->header_window,
8938                        - gtk_adjustment_get_value (tree_view->priv->hadjustment),
8939                        0);
8940       dy = tree_view->priv->dy - (int) gtk_adjustment_get_value (tree_view->priv->vadjustment);
8941       if (dy)
8942         {
8943           update_prelight (tree_view,
8944                            tree_view->priv->event_last_x,
8945                            tree_view->priv->event_last_y - dy);
8946
8947           if (tree_view->priv->edited_column &&
8948               GTK_IS_WIDGET (tree_view->priv->edited_column->editable_widget))
8949             {
8950               GList *list;
8951               GtkWidget *widget;
8952               PsppSheetViewChild *child = NULL;
8953
8954               widget = GTK_WIDGET (tree_view->priv->edited_column->editable_widget);
8955               adjust_allocation (widget, 0, dy); 
8956               
8957               for (list = tree_view->priv->children; list; list = list->next)
8958                 {
8959                   child = (PsppSheetViewChild *)list->data;
8960                   if (child->widget == widget)
8961                     {
8962                       child->y += dy;
8963                       break;
8964                     }
8965                 }
8966             }
8967         }
8968       gdk_window_scroll (tree_view->priv->bin_window, 0, dy);
8969
8970       if (tree_view->priv->dy != (int) gtk_adjustment_get_value (tree_view->priv->vadjustment))
8971         {
8972           /* update our dy and top_row */
8973           tree_view->priv->dy = (int) gtk_adjustment_get_value (tree_view->priv->vadjustment);
8974
8975           if (!tree_view->priv->in_top_row_to_dy)
8976             pspp_sheet_view_dy_to_top_row (tree_view);
8977         }
8978
8979       for (list = tree_view->priv->columns; list; list = list->next)
8980         {
8981           PsppSheetViewColumn *column = list->data;
8982           GtkAllocation *col_allocation = &column->allocation;
8983           GtkAllocation widget_allocation;
8984           gtk_widget_get_allocation (GTK_WIDGET (tree_view), &widget_allocation);
8985
8986           if (span_intersects (col_allocation->x, col_allocation->width,
8987                                gtk_adjustment_get_value (tree_view->priv->hadjustment),
8988                                widget_allocation.width))
8989             {
8990               pspp_sheet_view_column_set_need_button (column, TRUE);
8991               if (!column->button)
8992                 pspp_sheet_view_column_update_button (column);
8993             }
8994         }
8995     }
8996 }
8997
8998 \f
8999
9000 /* Public methods
9001  */
9002
9003 /**
9004  * pspp_sheet_view_new:
9005  *
9006  * Creates a new #PsppSheetView widget.
9007  *
9008  * Return value: A newly created #PsppSheetView widget.
9009  **/
9010 GtkWidget *
9011 pspp_sheet_view_new (void)
9012 {
9013   return g_object_new (PSPP_TYPE_SHEET_VIEW, NULL);
9014 }
9015
9016 /**
9017  * pspp_sheet_view_new_with_model:
9018  * @model: the model.
9019  *
9020  * Creates a new #PsppSheetView widget with the model initialized to @model.
9021  *
9022  * Return value: A newly created #PsppSheetView widget.
9023  **/
9024 GtkWidget *
9025 pspp_sheet_view_new_with_model (GtkTreeModel *model)
9026 {
9027   return g_object_new (PSPP_TYPE_SHEET_VIEW, "model", model, NULL);
9028 }
9029
9030 /* Public Accessors
9031  */
9032
9033 /**
9034  * pspp_sheet_view_get_model:
9035  * @tree_view: a #PsppSheetView
9036  *
9037  * Returns the model the #PsppSheetView is based on.  Returns %NULL if the
9038  * model is unset.
9039  *
9040  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
9041  **/
9042 GtkTreeModel *
9043 pspp_sheet_view_get_model (PsppSheetView *tree_view)
9044 {
9045   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9046
9047   return tree_view->priv->model;
9048 }
9049
9050 /**
9051  * pspp_sheet_view_set_model:
9052  * @tree_view: A #GtkTreeNode.
9053  * @model: (allow-none): The model.
9054  *
9055  * Sets the model for a #PsppSheetView.  If the @tree_view already has a model
9056  * set, it will remove it before setting the new model.  If @model is %NULL,
9057  * then it will unset the old model.
9058  **/
9059 void
9060 pspp_sheet_view_set_model (PsppSheetView  *tree_view,
9061                          GtkTreeModel *model)
9062 {
9063   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9064   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
9065
9066   if (model == tree_view->priv->model)
9067     return;
9068
9069   if (tree_view->priv->scroll_to_path)
9070     {
9071       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9072       tree_view->priv->scroll_to_path = NULL;
9073     }
9074
9075   if (tree_view->priv->model)
9076     {
9077       GList *tmplist = tree_view->priv->columns;
9078
9079       if (tree_view->priv->selected)
9080         range_tower_set0 (tree_view->priv->selected, 0, ULONG_MAX);
9081       pspp_sheet_view_stop_editing (tree_view, TRUE);
9082
9083       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9084                                             pspp_sheet_view_row_changed,
9085                                             tree_view);
9086       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9087                                             pspp_sheet_view_row_inserted,
9088                                             tree_view);
9089       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9090                                             pspp_sheet_view_row_deleted,
9091                                             tree_view);
9092       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
9093                                             pspp_sheet_view_rows_reordered,
9094                                             tree_view);
9095
9096       for (; tmplist; tmplist = tmplist->next)
9097         _pspp_sheet_view_column_unset_model (tmplist->data,
9098                                            tree_view->priv->model);
9099
9100       tree_view->priv->prelight_node = -1;
9101
9102       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
9103       tree_view->priv->drag_dest_row = NULL;
9104       gtk_tree_row_reference_free (tree_view->priv->cursor);
9105       tree_view->priv->cursor = NULL;
9106       gtk_tree_row_reference_free (tree_view->priv->anchor);
9107       tree_view->priv->anchor = NULL;
9108       gtk_tree_row_reference_free (tree_view->priv->top_row);
9109       tree_view->priv->top_row = NULL;
9110       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9111       tree_view->priv->scroll_to_path = NULL;
9112
9113       tree_view->priv->scroll_to_column = NULL;
9114
9115       g_object_unref (tree_view->priv->model);
9116
9117       tree_view->priv->search_column = -1;
9118       tree_view->priv->fixed_height = -1;
9119       tree_view->priv->dy = tree_view->priv->top_row_dy = 0;
9120       tree_view->priv->last_button_x = -1;
9121       tree_view->priv->last_button_y = -1;
9122     }
9123
9124   tree_view->priv->model = model;
9125
9126   if (tree_view->priv->model)
9127     {
9128       gint i;
9129
9130       if (tree_view->priv->search_column == -1)
9131         {
9132           for (i = 0; i < gtk_tree_model_get_n_columns (model); i++)
9133             {
9134               GType type = gtk_tree_model_get_column_type (model, i);
9135
9136               if (g_value_type_transformable (type, G_TYPE_STRING))
9137                 {
9138                   tree_view->priv->search_column = i;
9139                   break;
9140                 }
9141             }
9142         }
9143
9144       g_object_ref (tree_view->priv->model);
9145       g_signal_connect (tree_view->priv->model,
9146                         "row-changed",
9147                         G_CALLBACK (pspp_sheet_view_row_changed),
9148                         tree_view);
9149       g_signal_connect (tree_view->priv->model,
9150                         "row-inserted",
9151                         G_CALLBACK (pspp_sheet_view_row_inserted),
9152                         tree_view);
9153       g_signal_connect (tree_view->priv->model,
9154                         "row-deleted",
9155                         G_CALLBACK (pspp_sheet_view_row_deleted),
9156                         tree_view);
9157       g_signal_connect (tree_view->priv->model,
9158                         "rows-reordered",
9159                         G_CALLBACK (pspp_sheet_view_rows_reordered),
9160                         tree_view);
9161
9162       tree_view->priv->row_count = gtk_tree_model_iter_n_children (tree_view->priv->model, NULL);
9163
9164       /*  FIXME: do I need to do this? pspp_sheet_view_create_buttons (tree_view); */
9165       install_presize_handler (tree_view);
9166     }
9167
9168   g_object_notify (G_OBJECT (tree_view), "model");
9169
9170   if (tree_view->priv->selection)
9171     _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
9172
9173   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9174     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9175 }
9176
9177 /**
9178  * pspp_sheet_view_get_selection:
9179  * @tree_view: A #PsppSheetView.
9180  *
9181  * Gets the #PsppSheetSelection associated with @tree_view.
9182  *
9183  * Return value: A #PsppSheetSelection object.
9184  **/
9185 PsppSheetSelection *
9186 pspp_sheet_view_get_selection (PsppSheetView *tree_view)
9187 {
9188   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9189
9190   return tree_view->priv->selection;
9191 }
9192
9193 /**
9194  * pspp_sheet_view_get_hadjustment:
9195  * @tree_view: A #PsppSheetView
9196  *
9197  * Gets the #GtkAdjustment currently being used for the horizontal aspect.
9198  *
9199  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
9200  * used.
9201  **/
9202 GtkAdjustment *
9203 pspp_sheet_view_get_hadjustment (PsppSheetView *tree_view)
9204 {
9205   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9206
9207   if (tree_view->priv->hadjustment == NULL)
9208     pspp_sheet_view_set_hadjustment (tree_view, NULL);
9209
9210   return tree_view->priv->hadjustment;
9211 }
9212
9213 /**
9214  * pspp_sheet_view_set_hadjustment:
9215  * @tree_view: A #PsppSheetView
9216  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9217  *
9218  * Sets the #GtkAdjustment for the current horizontal aspect.
9219  **/
9220 void
9221 pspp_sheet_view_set_hadjustment (PsppSheetView   *tree_view,
9222                                GtkAdjustment *adjustment)
9223 {
9224   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9225
9226   pspp_sheet_view_set_adjustments (tree_view,
9227                                  adjustment,
9228                                  tree_view->priv->vadjustment);
9229
9230   g_object_notify (G_OBJECT (tree_view), "hadjustment");
9231 }
9232
9233 /**
9234  * pspp_sheet_view_get_vadjustment:
9235  * @tree_view: A #PsppSheetView
9236  *
9237  * Gets the #GtkAdjustment currently being used for the vertical aspect.
9238  *
9239  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
9240  * used.
9241  **/
9242 GtkAdjustment *
9243 pspp_sheet_view_get_vadjustment (PsppSheetView *tree_view)
9244 {
9245   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9246
9247   if (tree_view->priv->vadjustment == NULL)
9248     pspp_sheet_view_set_vadjustment (tree_view, NULL);
9249
9250   return tree_view->priv->vadjustment;
9251 }
9252
9253 /**
9254  * pspp_sheet_view_set_vadjustment:
9255  * @tree_view: A #PsppSheetView
9256  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9257  *
9258  * Sets the #GtkAdjustment for the current vertical aspect.
9259  **/
9260 void
9261 pspp_sheet_view_set_vadjustment (PsppSheetView   *tree_view,
9262                                GtkAdjustment *adjustment)
9263 {
9264   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9265
9266   pspp_sheet_view_set_adjustments (tree_view,
9267                                  tree_view->priv->hadjustment,
9268                                  adjustment);
9269
9270   g_object_notify (G_OBJECT (tree_view), "vadjustment");
9271 }
9272
9273 /* Column and header operations */
9274
9275 /**
9276  * pspp_sheet_view_get_headers_visible:
9277  * @tree_view: A #PsppSheetView.
9278  *
9279  * Returns %TRUE if the headers on the @tree_view are visible.
9280  *
9281  * Return value: Whether the headers are visible or not.
9282  **/
9283 gboolean
9284 pspp_sheet_view_get_headers_visible (PsppSheetView *tree_view)
9285 {
9286   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9287
9288   return PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9289 }
9290
9291 /**
9292  * pspp_sheet_view_set_headers_visible:
9293  * @tree_view: A #PsppSheetView.
9294  * @headers_visible: %TRUE if the headers are visible
9295  *
9296  * Sets the visibility state of the headers.
9297  **/
9298 void
9299 pspp_sheet_view_set_headers_visible (PsppSheetView *tree_view,
9300                                    gboolean     headers_visible)
9301 {
9302   gint x, y;
9303   GList *list;
9304   PsppSheetViewColumn *column;
9305   GtkAllocation allocation;
9306
9307   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9308
9309   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
9310
9311   headers_visible = !! headers_visible;
9312
9313   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE) == headers_visible)
9314     return;
9315
9316   if (headers_visible)
9317     PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9318   else
9319     PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9320
9321   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9322     {
9323       gdk_window_get_position (tree_view->priv->bin_window, &x, &y);
9324       if (headers_visible)
9325         {
9326           gdk_window_move_resize (tree_view->priv->bin_window, x, y  + TREE_VIEW_HEADER_HEIGHT (tree_view), 
9327                                   tree_view->priv->width, allocation.height -  + TREE_VIEW_HEADER_HEIGHT (tree_view));
9328
9329           if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
9330             pspp_sheet_view_map_buttons (tree_view);
9331         }
9332       else
9333         {
9334           gdk_window_move_resize (tree_view->priv->bin_window, x, y, tree_view->priv->width, tree_view->priv->height);
9335
9336           for (list = tree_view->priv->columns; list; list = list->next)
9337             {
9338               column = list->data;
9339               if (column->button)
9340                 gtk_widget_unmap (column->button);
9341             }
9342           gdk_window_hide (tree_view->priv->header_window);
9343         }
9344     }
9345
9346   gtk_adjustment_set_page_size (tree_view->priv->vadjustment, allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view));
9347   gtk_adjustment_set_page_increment (tree_view->priv->vadjustment, (allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view)) / 2);
9348   gtk_adjustment_set_lower (tree_view->priv->vadjustment, 0);
9349   gtk_adjustment_set_upper (tree_view->priv->vadjustment, tree_view->priv->height);
9350   gtk_adjustment_changed (tree_view->priv->vadjustment);
9351
9352   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9353
9354   g_object_notify (G_OBJECT (tree_view), "headers-visible");
9355 }
9356
9357 /**
9358  * pspp_sheet_view_columns_autosize:
9359  * @tree_view: A #PsppSheetView.
9360  *
9361  * Resizes all columns to their optimal width. Only works after the
9362  * treeview has been realized.
9363  **/
9364 void
9365 pspp_sheet_view_columns_autosize (PsppSheetView *tree_view)
9366 {
9367   gboolean dirty = FALSE;
9368   GList *list;
9369   PsppSheetViewColumn *column;
9370
9371   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9372
9373   for (list = tree_view->priv->columns; list; list = list->next)
9374     {
9375       column = list->data;
9376       _pspp_sheet_view_column_cell_set_dirty (column);
9377       dirty = TRUE;
9378     }
9379
9380   if (dirty)
9381     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9382 }
9383
9384 /**
9385  * pspp_sheet_view_set_headers_clickable:
9386  * @tree_view: A #PsppSheetView.
9387  * @setting: %TRUE if the columns are clickable.
9388  *
9389  * Allow the column title buttons to be clicked.
9390  **/
9391 void
9392 pspp_sheet_view_set_headers_clickable (PsppSheetView *tree_view,
9393                                      gboolean   setting)
9394 {
9395   GList *list;
9396
9397   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9398
9399   for (list = tree_view->priv->columns; list; list = list->next)
9400     pspp_sheet_view_column_set_clickable (PSPP_SHEET_VIEW_COLUMN (list->data), setting);
9401
9402   g_object_notify (G_OBJECT (tree_view), "headers-clickable");
9403 }
9404
9405
9406 /**
9407  * pspp_sheet_view_get_headers_clickable:
9408  * @tree_view: A #PsppSheetView.
9409  *
9410  * Returns whether all header columns are clickable.
9411  *
9412  * Return value: %TRUE if all header columns are clickable, otherwise %FALSE
9413  *
9414  * Since: 2.10
9415  **/
9416 gboolean 
9417 pspp_sheet_view_get_headers_clickable (PsppSheetView *tree_view)
9418 {
9419   GList *list;
9420   
9421   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9422
9423   for (list = tree_view->priv->columns; list; list = list->next)
9424     if (!PSPP_SHEET_VIEW_COLUMN (list->data)->clickable)
9425       return FALSE;
9426
9427   return TRUE;
9428 }
9429
9430 /**
9431  * pspp_sheet_view_set_rules_hint
9432  * @tree_view: a #PsppSheetView
9433  * @setting: %TRUE if the tree requires reading across rows
9434  *
9435  * This function tells GTK+ that the user interface for your
9436  * application requires users to read across tree rows and associate
9437  * cells with one another. By default, GTK+ will then render the tree
9438  * with alternating row colors. Do <emphasis>not</emphasis> use it
9439  * just because you prefer the appearance of the ruled tree; that's a
9440  * question for the theme. Some themes will draw tree rows in
9441  * alternating colors even when rules are turned off, and users who
9442  * prefer that appearance all the time can choose those themes. You
9443  * should call this function only as a <emphasis>semantic</emphasis>
9444  * hint to the theme engine that your tree makes alternating colors
9445  * useful from a functional standpoint (since it has lots of columns,
9446  * generally).
9447  *
9448  **/
9449 void
9450 pspp_sheet_view_set_rules_hint (PsppSheetView  *tree_view,
9451                               gboolean      setting)
9452 {
9453   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9454
9455   setting = setting != FALSE;
9456
9457   if (tree_view->priv->has_rules != setting)
9458     {
9459       tree_view->priv->has_rules = setting;
9460       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
9461     }
9462
9463   g_object_notify (G_OBJECT (tree_view), "rules-hint");
9464 }
9465
9466 /**
9467  * pspp_sheet_view_get_rules_hint
9468  * @tree_view: a #PsppSheetView
9469  *
9470  * Gets the setting set by pspp_sheet_view_set_rules_hint().
9471  *
9472  * Return value: %TRUE if rules are useful for the user of this tree
9473  **/
9474 gboolean
9475 pspp_sheet_view_get_rules_hint (PsppSheetView  *tree_view)
9476 {
9477   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9478
9479   return tree_view->priv->has_rules;
9480 }
9481
9482 /* Public Column functions
9483  */
9484
9485 /**
9486  * pspp_sheet_view_append_column:
9487  * @tree_view: A #PsppSheetView.
9488  * @column: The #PsppSheetViewColumn to add.
9489  *
9490  * Appends @column to the list of columns.
9491  *
9492  * Return value: The number of columns in @tree_view after appending.
9493  **/
9494 gint
9495 pspp_sheet_view_append_column (PsppSheetView       *tree_view,
9496                              PsppSheetViewColumn *column)
9497 {
9498   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9499   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9500   g_return_val_if_fail (column->tree_view == NULL, -1);
9501
9502   return pspp_sheet_view_insert_column (tree_view, column, -1);
9503 }
9504
9505
9506 /**
9507  * pspp_sheet_view_remove_column:
9508  * @tree_view: A #PsppSheetView.
9509  * @column: The #PsppSheetViewColumn to remove.
9510  *
9511  * Removes @column from @tree_view.
9512  *
9513  * Return value: The number of columns in @tree_view after removing.
9514  **/
9515 gint
9516 pspp_sheet_view_remove_column (PsppSheetView       *tree_view,
9517                              PsppSheetViewColumn *column)
9518 {
9519   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9520   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9521   g_return_val_if_fail (column->tree_view == GTK_WIDGET (tree_view), -1);
9522
9523   if (tree_view->priv->focus_column == column)
9524     tree_view->priv->focus_column = NULL;
9525
9526   if (tree_view->priv->edited_column == column)
9527     {
9528       pspp_sheet_view_stop_editing (tree_view, TRUE);
9529
9530       /* no need to, but just to be sure ... */
9531       tree_view->priv->edited_column = NULL;
9532     }
9533
9534   _pspp_sheet_view_column_unset_tree_view (column);
9535
9536   tree_view->priv->columns = g_list_remove (tree_view->priv->columns, column);
9537   tree_view->priv->n_columns--;
9538
9539   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9540     {
9541       GList *list;
9542
9543       _pspp_sheet_view_column_unrealize_button (column);
9544       for (list = tree_view->priv->columns; list; list = list->next)
9545         {
9546           PsppSheetViewColumn *tmp_column;
9547
9548           tmp_column = PSPP_SHEET_VIEW_COLUMN (list->data);
9549           if (tmp_column->visible)
9550             _pspp_sheet_view_column_cell_set_dirty (tmp_column);
9551         }
9552
9553       if (tree_view->priv->n_columns == 0 &&
9554           pspp_sheet_view_get_headers_visible (tree_view) && 
9555           tree_view->priv->header_window)
9556         gdk_window_hide (tree_view->priv->header_window);
9557
9558       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9559     }
9560
9561   g_object_unref (column);
9562   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9563
9564   return tree_view->priv->n_columns;
9565 }
9566
9567 /**
9568  * pspp_sheet_view_insert_column:
9569  * @tree_view: A #PsppSheetView.
9570  * @column: The #PsppSheetViewColumn to be inserted.
9571  * @position: The position to insert @column in.
9572  *
9573  * This inserts the @column into the @tree_view at @position.  If @position is
9574  * -1, then the column is inserted at the end.
9575  *
9576  * Return value: The number of columns in @tree_view after insertion.
9577  **/
9578 gint
9579 pspp_sheet_view_insert_column (PsppSheetView       *tree_view,
9580                              PsppSheetViewColumn *column,
9581                              gint               position)
9582 {
9583   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9584   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9585   g_return_val_if_fail (column->tree_view == NULL, -1);
9586
9587   g_object_ref_sink (column);
9588
9589   if (tree_view->priv->n_columns == 0 &&
9590       gtk_widget_get_realized (GTK_WIDGET (tree_view)) &&
9591       pspp_sheet_view_get_headers_visible (tree_view))
9592     {
9593       gdk_window_show (tree_view->priv->header_window);
9594     }
9595
9596   tree_view->priv->columns = g_list_insert (tree_view->priv->columns,
9597                                             column, position);
9598   tree_view->priv->n_columns++;
9599
9600   _pspp_sheet_view_column_set_tree_view (column, tree_view);
9601
9602   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9603     {
9604       GList *list;
9605
9606       _pspp_sheet_view_column_realize_button (column);
9607
9608       for (list = tree_view->priv->columns; list; list = list->next)
9609         {
9610           column = PSPP_SHEET_VIEW_COLUMN (list->data);
9611           if (column->visible)
9612             _pspp_sheet_view_column_cell_set_dirty (column);
9613         }
9614       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9615     }
9616
9617   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9618
9619   return tree_view->priv->n_columns;
9620 }
9621
9622 /**
9623  * pspp_sheet_view_insert_column_with_attributes:
9624  * @tree_view: A #PsppSheetView
9625  * @position: The position to insert the new column in.
9626  * @title: The title to set the header to.
9627  * @cell: The #GtkCellRenderer.
9628  * @Varargs: A %NULL-terminated list of attributes.
9629  *
9630  * Creates a new #PsppSheetViewColumn and inserts it into the @tree_view at
9631  * @position.  If @position is -1, then the newly created column is inserted at
9632  * the end.  The column is initialized with the attributes given.
9633  *
9634  * Return value: The number of columns in @tree_view after insertion.
9635  **/
9636 gint
9637 pspp_sheet_view_insert_column_with_attributes (PsppSheetView     *tree_view,
9638                                              gint             position,
9639                                              const gchar     *title,
9640                                              GtkCellRenderer *cell,
9641                                              ...)
9642 {
9643   PsppSheetViewColumn *column;
9644   gchar *attribute;
9645   va_list args;
9646   gint column_id;
9647
9648   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9649
9650   column = pspp_sheet_view_column_new ();
9651   pspp_sheet_view_column_set_title (column, title);
9652   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9653
9654   va_start (args, cell);
9655
9656   attribute = va_arg (args, gchar *);
9657
9658   while (attribute != NULL)
9659     {
9660       column_id = va_arg (args, gint);
9661       pspp_sheet_view_column_add_attribute (column, cell, attribute, column_id);
9662       attribute = va_arg (args, gchar *);
9663     }
9664
9665   va_end (args);
9666
9667   pspp_sheet_view_insert_column (tree_view, column, position);
9668
9669   return tree_view->priv->n_columns;
9670 }
9671
9672 /**
9673  * pspp_sheet_view_insert_column_with_data_func:
9674  * @tree_view: a #PsppSheetView
9675  * @position: Position to insert, -1 for append
9676  * @title: column title
9677  * @cell: cell renderer for column
9678  * @func: function to set attributes of cell renderer
9679  * @data: data for @func
9680  * @dnotify: destroy notifier for @data
9681  *
9682  * Convenience function that inserts a new column into the #PsppSheetView
9683  * with the given cell renderer and a #GtkCellDataFunc to set cell renderer
9684  * attributes (normally using data from the model). See also
9685  * pspp_sheet_view_column_set_cell_data_func(), pspp_sheet_view_column_pack_start().
9686  *
9687  * Return value: number of columns in the tree view post-insert
9688  **/
9689 gint
9690 pspp_sheet_view_insert_column_with_data_func  (PsppSheetView               *tree_view,
9691                                              gint                       position,
9692                                              const gchar               *title,
9693                                              GtkCellRenderer           *cell,
9694                                              PsppSheetCellDataFunc        func,
9695                                              gpointer                   data,
9696                                              GDestroyNotify             dnotify)
9697 {
9698   PsppSheetViewColumn *column;
9699
9700   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9701
9702   column = pspp_sheet_view_column_new ();
9703   pspp_sheet_view_column_set_title (column, title);
9704   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9705   pspp_sheet_view_column_set_cell_data_func (column, cell, func, data, dnotify);
9706
9707   pspp_sheet_view_insert_column (tree_view, column, position);
9708
9709   return tree_view->priv->n_columns;
9710 }
9711
9712 /**
9713  * pspp_sheet_view_get_column:
9714  * @tree_view: A #PsppSheetView.
9715  * @n: The position of the column, counting from 0.
9716  *
9717  * Gets the #PsppSheetViewColumn at the given position in the #tree_view.
9718  *
9719  * Return value: The #PsppSheetViewColumn, or %NULL if the position is outside the
9720  * range of columns.
9721  **/
9722 PsppSheetViewColumn *
9723 pspp_sheet_view_get_column (PsppSheetView *tree_view,
9724                           gint         n)
9725 {
9726   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9727
9728   if (n < 0 || n >= tree_view->priv->n_columns)
9729     return NULL;
9730
9731   if (tree_view->priv->columns == NULL)
9732     return NULL;
9733
9734   return PSPP_SHEET_VIEW_COLUMN (g_list_nth (tree_view->priv->columns, n)->data);
9735 }
9736
9737 /**
9738  * pspp_sheet_view_get_columns:
9739  * @tree_view: A #PsppSheetView
9740  *
9741  * Returns a #GList of all the #PsppSheetViewColumn s currently in @tree_view.
9742  * The returned list must be freed with g_list_free ().
9743  *
9744  * Return value: (element-type PsppSheetViewColumn) (transfer container): A list of #PsppSheetViewColumn s
9745  **/
9746 GList *
9747 pspp_sheet_view_get_columns (PsppSheetView *tree_view)
9748 {
9749   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9750
9751   return g_list_copy (tree_view->priv->columns);
9752 }
9753
9754 /**
9755  * pspp_sheet_view_move_column_after:
9756  * @tree_view: A #PsppSheetView
9757  * @column: The #PsppSheetViewColumn to be moved.
9758  * @base_column: (allow-none): The #PsppSheetViewColumn to be moved relative to, or %NULL.
9759  *
9760  * Moves @column to be after to @base_column.  If @base_column is %NULL, then
9761  * @column is placed in the first position.
9762  **/
9763 void
9764 pspp_sheet_view_move_column_after (PsppSheetView       *tree_view,
9765                                  PsppSheetViewColumn *column,
9766                                  PsppSheetViewColumn *base_column)
9767 {
9768   GList *column_list_el, *base_el = NULL;
9769
9770   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9771
9772   column_list_el = g_list_find (tree_view->priv->columns, column);
9773   g_return_if_fail (column_list_el != NULL);
9774
9775   if (base_column)
9776     {
9777       base_el = g_list_find (tree_view->priv->columns, base_column);
9778       g_return_if_fail (base_el != NULL);
9779     }
9780
9781   if (column_list_el->prev == base_el)
9782     return;
9783
9784   tree_view->priv->columns = g_list_remove_link (tree_view->priv->columns, column_list_el);
9785   if (base_el == NULL)
9786     {
9787       column_list_el->prev = NULL;
9788       column_list_el->next = tree_view->priv->columns;
9789       if (column_list_el->next)
9790         column_list_el->next->prev = column_list_el;
9791       tree_view->priv->columns = column_list_el;
9792     }
9793   else
9794     {
9795       column_list_el->prev = base_el;
9796       column_list_el->next = base_el->next;
9797       if (column_list_el->next)
9798         column_list_el->next->prev = column_list_el;
9799       base_el->next = column_list_el;
9800     }
9801
9802   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9803     {
9804       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9805       pspp_sheet_view_size_allocate_columns (GTK_WIDGET (tree_view), NULL);
9806     }
9807
9808   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9809 }
9810
9811 /**
9812  * pspp_sheet_view_set_column_drag_function:
9813  * @tree_view: A #PsppSheetView.
9814  * @func: (allow-none): A function to determine which columns are reorderable, or %NULL.
9815  * @user_data: (allow-none): User data to be passed to @func, or %NULL
9816  * @destroy: (allow-none): Destroy notifier for @user_data, or %NULL
9817  *
9818  * Sets a user function for determining where a column may be dropped when
9819  * dragged.  This function is called on every column pair in turn at the
9820  * beginning of a column drag to determine where a drop can take place.  The
9821  * arguments passed to @func are: the @tree_view, the #PsppSheetViewColumn being
9822  * dragged, the two #PsppSheetViewColumn s determining the drop spot, and
9823  * @user_data.  If either of the #PsppSheetViewColumn arguments for the drop spot
9824  * are %NULL, then they indicate an edge.  If @func is set to be %NULL, then
9825  * @tree_view reverts to the default behavior of allowing all columns to be
9826  * dropped everywhere.
9827  **/
9828 void
9829 pspp_sheet_view_set_column_drag_function (PsppSheetView               *tree_view,
9830                                         PsppSheetViewColumnDropFunc  func,
9831                                         gpointer                   user_data,
9832                                         GDestroyNotify             destroy)
9833 {
9834   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9835
9836   if (tree_view->priv->column_drop_func_data_destroy)
9837     tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
9838
9839   tree_view->priv->column_drop_func = func;
9840   tree_view->priv->column_drop_func_data = user_data;
9841   tree_view->priv->column_drop_func_data_destroy = destroy;
9842 }
9843
9844 /**
9845  * pspp_sheet_view_scroll_to_point:
9846  * @tree_view: a #PsppSheetView
9847  * @tree_x: X coordinate of new top-left pixel of visible area, or -1
9848  * @tree_y: Y coordinate of new top-left pixel of visible area, or -1
9849  *
9850  * Scrolls the tree view such that the top-left corner of the visible
9851  * area is @tree_x, @tree_y, where @tree_x and @tree_y are specified
9852  * in tree coordinates.  The @tree_view must be realized before
9853  * this function is called.  If it isn't, you probably want to be
9854  * using pspp_sheet_view_scroll_to_cell().
9855  *
9856  * If either @tree_x or @tree_y are -1, then that direction isn't scrolled.
9857  **/
9858 void
9859 pspp_sheet_view_scroll_to_point (PsppSheetView *tree_view,
9860                                gint         tree_x,
9861                                gint         tree_y)
9862 {
9863   GtkAdjustment *hadj;
9864   GtkAdjustment *vadj;
9865
9866   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9867   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
9868
9869   hadj = tree_view->priv->hadjustment;
9870   vadj = tree_view->priv->vadjustment;
9871
9872   if (tree_x != -1)
9873     gtk_adjustment_set_value (hadj, CLAMP (tree_x, gtk_adjustment_get_lower (hadj), gtk_adjustment_get_upper (hadj) - gtk_adjustment_get_page_size (hadj)));
9874   if (tree_y != -1)
9875     gtk_adjustment_set_value (vadj, CLAMP (tree_y, gtk_adjustment_get_lower (vadj), gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj)));
9876 }
9877
9878 /**
9879  * pspp_sheet_view_scroll_to_cell:
9880  * @tree_view: A #PsppSheetView.
9881  * @path: (allow-none): The path of the row to move to, or %NULL.
9882  * @column: (allow-none): The #PsppSheetViewColumn to move horizontally to, or %NULL.
9883  * @use_align: whether to use alignment arguments, or %FALSE.
9884  * @row_align: The vertical alignment of the row specified by @path.
9885  * @col_align: The horizontal alignment of the column specified by @column.
9886  *
9887  * Moves the alignments of @tree_view to the position specified by @column and
9888  * @path.  If @column is %NULL, then no horizontal scrolling occurs.  Likewise,
9889  * if @path is %NULL no vertical scrolling occurs.  At a minimum, one of @column
9890  * or @path need to be non-%NULL.  @row_align determines where the row is
9891  * placed, and @col_align determines where @column is placed.  Both are expected
9892  * to be between 0.0 and 1.0. 0.0 means left/top alignment, 1.0 means
9893  * right/bottom alignment, 0.5 means center.
9894  *
9895  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
9896  * tree does the minimum amount of work to scroll the cell onto the screen.
9897  * This means that the cell will be scrolled to the edge closest to its current
9898  * position.  If the cell is currently visible on the screen, nothing is done.
9899  *
9900  * This function only works if the model is set, and @path is a valid row on the
9901  * model.  If the model changes before the @tree_view is realized, the centered
9902  * path will be modified to reflect this change.
9903  **/
9904 void
9905 pspp_sheet_view_scroll_to_cell (PsppSheetView       *tree_view,
9906                               GtkTreePath       *path,
9907                               PsppSheetViewColumn *column,
9908                               gboolean           use_align,
9909                               gfloat             row_align,
9910                               gfloat             col_align)
9911 {
9912   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9913   g_return_if_fail (tree_view->priv->model != NULL);
9914   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
9915   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
9916   g_return_if_fail (path != NULL || column != NULL);
9917
9918 #if 0
9919   g_print ("pspp_sheet_view_scroll_to_cell:\npath: %s\ncolumn: %s\nuse_align: %d\nrow_align: %f\ncol_align: %f\n",
9920            gtk_tree_path_to_string (path), column?"non-null":"null", use_align, row_align, col_align);
9921 #endif
9922   row_align = CLAMP (row_align, 0.0, 1.0);
9923   col_align = CLAMP (col_align, 0.0, 1.0);
9924
9925
9926   /* Note: Despite the benefits that come from having one code path for the
9927    * scrolling code, we short-circuit validate_visible_area's immplementation as
9928    * it is much slower than just going to the point.
9929    */
9930   if (!gtk_widget_get_visible (GTK_WIDGET (tree_view)) ||
9931       !gtk_widget_get_realized (GTK_WIDGET (tree_view))
9932       /* XXX || GTK_WIDGET_ALLOC_NEEDED (tree_view) */)
9933     {
9934       if (tree_view->priv->scroll_to_path)
9935         gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9936
9937       tree_view->priv->scroll_to_path = NULL;
9938       tree_view->priv->scroll_to_column = NULL;
9939
9940       if (path)
9941         tree_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
9942       if (column)
9943         tree_view->priv->scroll_to_column = column;
9944       tree_view->priv->scroll_to_use_align = use_align;
9945       tree_view->priv->scroll_to_row_align = row_align;
9946       tree_view->priv->scroll_to_col_align = col_align;
9947
9948       install_presize_handler (tree_view);
9949     }
9950   else
9951     {
9952       GdkRectangle cell_rect;
9953       GdkRectangle vis_rect;
9954       gint dest_x, dest_y;
9955
9956       pspp_sheet_view_get_background_area (tree_view, path, column, &cell_rect);
9957       pspp_sheet_view_get_visible_rect (tree_view, &vis_rect);
9958
9959       cell_rect.y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, cell_rect.y);
9960
9961       dest_x = vis_rect.x;
9962       dest_y = vis_rect.y;
9963
9964       if (column)
9965         {
9966           if (use_align)
9967             {
9968               dest_x = cell_rect.x - ((vis_rect.width - cell_rect.width) * col_align);
9969             }
9970           else
9971             {
9972               if (cell_rect.x < vis_rect.x)
9973                 dest_x = cell_rect.x;
9974               if (cell_rect.x + cell_rect.width > vis_rect.x + vis_rect.width)
9975                 dest_x = cell_rect.x + cell_rect.width - vis_rect.width;
9976             }
9977         }
9978
9979       if (path)
9980         {
9981           if (use_align)
9982             {
9983               dest_y = cell_rect.y - ((vis_rect.height - cell_rect.height) * row_align);
9984               dest_y = MAX (dest_y, 0);
9985             }
9986           else
9987             {
9988               if (cell_rect.y < vis_rect.y)
9989                 dest_y = cell_rect.y;
9990               if (cell_rect.y + cell_rect.height > vis_rect.y + vis_rect.height)
9991                 dest_y = cell_rect.y + cell_rect.height - vis_rect.height;
9992             }
9993         }
9994
9995       pspp_sheet_view_scroll_to_point (tree_view, dest_x, dest_y);
9996     }
9997 }
9998
9999 /**
10000  * pspp_sheet_view_row_activated:
10001  * @tree_view: A #PsppSheetView
10002  * @path: The #GtkTreePath to be activated.
10003  * @column: The #PsppSheetViewColumn to be activated.
10004  *
10005  * Activates the cell determined by @path and @column.
10006  **/
10007 void
10008 pspp_sheet_view_row_activated (PsppSheetView       *tree_view,
10009                              GtkTreePath       *path,
10010                              PsppSheetViewColumn *column)
10011 {
10012   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10013
10014   g_signal_emit (tree_view, tree_view_signals[ROW_ACTIVATED], 0, path, column);
10015 }
10016
10017
10018 /**
10019  * pspp_sheet_view_get_reorderable:
10020  * @tree_view: a #PsppSheetView
10021  *
10022  * Retrieves whether the user can reorder the tree via drag-and-drop. See
10023  * pspp_sheet_view_set_reorderable().
10024  *
10025  * Return value: %TRUE if the tree can be reordered.
10026  **/
10027 gboolean
10028 pspp_sheet_view_get_reorderable (PsppSheetView *tree_view)
10029 {
10030   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10031
10032   return tree_view->priv->reorderable;
10033 }
10034
10035 /**
10036  * pspp_sheet_view_set_reorderable:
10037  * @tree_view: A #PsppSheetView.
10038  * @reorderable: %TRUE, if the tree can be reordered.
10039  *
10040  * This function is a convenience function to allow you to reorder
10041  * models that support the #GtkDragSourceIface and the
10042  * #GtkDragDestIface.  Both #GtkTreeStore and #GtkListStore support
10043  * these.  If @reorderable is %TRUE, then the user can reorder the
10044  * model by dragging and dropping rows. The developer can listen to
10045  * these changes by connecting to the model's row_inserted and
10046  * row_deleted signals. The reordering is implemented by setting up
10047  * the tree view as a drag source and destination. Therefore, drag and
10048  * drop can not be used in a reorderable view for any other purpose.
10049  *
10050  * This function does not give you any degree of control over the order -- any
10051  * reordering is allowed.  If more control is needed, you should probably
10052  * handle drag and drop manually.
10053  **/
10054 void
10055 pspp_sheet_view_set_reorderable (PsppSheetView *tree_view,
10056                                gboolean     reorderable)
10057 {
10058   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10059
10060   reorderable = reorderable != FALSE;
10061
10062   if (tree_view->priv->reorderable == reorderable)
10063     return;
10064
10065   if (reorderable)
10066     {
10067       const GtkTargetEntry row_targets[] = {
10068         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
10069       };
10070
10071       pspp_sheet_view_enable_model_drag_source (tree_view,
10072                                               GDK_BUTTON1_MASK,
10073                                               row_targets,
10074                                               G_N_ELEMENTS (row_targets),
10075                                               GDK_ACTION_MOVE);
10076       pspp_sheet_view_enable_model_drag_dest (tree_view,
10077                                             row_targets,
10078                                             G_N_ELEMENTS (row_targets),
10079                                             GDK_ACTION_MOVE);
10080     }
10081   else
10082     {
10083       pspp_sheet_view_unset_rows_drag_source (tree_view);
10084       pspp_sheet_view_unset_rows_drag_dest (tree_view);
10085     }
10086
10087   tree_view->priv->reorderable = reorderable;
10088
10089   g_object_notify (G_OBJECT (tree_view), "reorderable");
10090 }
10091
10092 /* If CLEAR_AND_SELECT is true, then the row will be selected and, unless Shift
10093    is pressed, other rows will be unselected.
10094
10095    If CLAMP_NODE is true, then the sheetview will scroll to make the row
10096    visible. */
10097 static void
10098 pspp_sheet_view_real_set_cursor (PsppSheetView     *tree_view,
10099                                GtkTreePath     *path,
10100                                gboolean         clear_and_select,
10101                                gboolean         clamp_node,
10102                                PsppSheetSelectMode mode)
10103 {
10104   int node = -1;
10105
10106   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10107     {
10108       GtkTreePath *cursor_path;
10109       cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10110       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10111       gtk_tree_path_free (cursor_path);
10112     }
10113
10114   gtk_tree_row_reference_free (tree_view->priv->cursor);
10115   tree_view->priv->cursor = NULL;
10116
10117   _pspp_sheet_view_find_node (tree_view, path, &node);
10118   tree_view->priv->cursor =
10119     gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
10120                                       tree_view->priv->model,
10121                                       path);
10122
10123   if (tree_view->priv->row_count > 0)
10124     {
10125       int new_node = -1;
10126
10127       if (clear_and_select && !(mode & PSPP_SHEET_SELECT_MODE_TOGGLE))
10128         _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
10129                                                     node, path, mode,
10130                                                     FALSE);
10131
10132       /* We have to re-find tree and node here again, somebody might have
10133        * cleared the node or the whole tree in the PsppSheetSelection::changed
10134        * callback. If the nodes differ we bail out here.
10135        */
10136       _pspp_sheet_view_find_node (tree_view, path, &new_node);
10137
10138       if (node != new_node)
10139         return;
10140
10141       if (clamp_node)
10142         {
10143           pspp_sheet_view_clamp_node_visible (tree_view, node);
10144           _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
10145         }
10146     }
10147
10148   g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
10149 }
10150
10151 /**
10152  * pspp_sheet_view_get_cursor:
10153  * @tree_view: A #PsppSheetView
10154  * @path: (allow-none): A pointer to be filled with the current cursor path, or %NULL
10155  * @focus_column: (allow-none): A pointer to be filled with the current focus column, or %NULL
10156  *
10157  * Fills in @path and @focus_column with the current path and focus column.  If
10158  * the cursor isn't currently set, then *@path will be %NULL.  If no column
10159  * currently has focus, then *@focus_column will be %NULL.
10160  *
10161  * The returned #GtkTreePath must be freed with gtk_tree_path_free() when
10162  * you are done with it.
10163  **/
10164 void
10165 pspp_sheet_view_get_cursor (PsppSheetView        *tree_view,
10166                           GtkTreePath       **path,
10167                           PsppSheetViewColumn **focus_column)
10168 {
10169   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10170
10171   if (path)
10172     {
10173       if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10174         *path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10175       else
10176         *path = NULL;
10177     }
10178
10179   if (focus_column)
10180     {
10181       *focus_column = tree_view->priv->focus_column;
10182     }
10183 }
10184
10185 /**
10186  * pspp_sheet_view_set_cursor:
10187  * @tree_view: A #PsppSheetView
10188  * @path: A #GtkTreePath
10189  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10190  * @start_editing: %TRUE if the specified cell should start being edited.
10191  *
10192  * Sets the current keyboard focus to be at @path, and selects it.  This is
10193  * useful when you want to focus the user's attention on a particular row.  If
10194  * @focus_column is not %NULL, then focus is given to the column specified by 
10195  * it. Additionally, if @focus_column is specified, and @start_editing is 
10196  * %TRUE, then editing should be started in the specified cell.  
10197  * This function is often followed by @gtk_widget_grab_focus (@tree_view) 
10198  * in order to give keyboard focus to the widget.  Please note that editing 
10199  * can only happen when the widget is realized.
10200  *
10201  * If @path is invalid for @model, the current cursor (if any) will be unset
10202  * and the function will return without failing.
10203  **/
10204 void
10205 pspp_sheet_view_set_cursor (PsppSheetView       *tree_view,
10206                           GtkTreePath       *path,
10207                           PsppSheetViewColumn *focus_column,
10208                           gboolean           start_editing)
10209 {
10210   pspp_sheet_view_set_cursor_on_cell (tree_view, path, focus_column,
10211                                     NULL, start_editing);
10212 }
10213
10214 /**
10215  * pspp_sheet_view_set_cursor_on_cell:
10216  * @tree_view: A #PsppSheetView
10217  * @path: A #GtkTreePath
10218  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10219  * @focus_cell: (allow-none): A #GtkCellRenderer, or %NULL
10220  * @start_editing: %TRUE if the specified cell should start being edited.
10221  *
10222  * Sets the current keyboard focus to be at @path, and selects it.  This is
10223  * useful when you want to focus the user's attention on a particular row.  If
10224  * @focus_column is not %NULL, then focus is given to the column specified by
10225  * it. If @focus_column and @focus_cell are not %NULL, and @focus_column
10226  * contains 2 or more editable or activatable cells, then focus is given to
10227  * the cell specified by @focus_cell. Additionally, if @focus_column is
10228  * specified, and @start_editing is %TRUE, then editing should be started in
10229  * the specified cell.  This function is often followed by
10230  * @gtk_widget_grab_focus (@tree_view) in order to give keyboard focus to the
10231  * widget.  Please note that editing can only happen when the widget is
10232  * realized.
10233  *
10234  * If @path is invalid for @model, the current cursor (if any) will be unset
10235  * and the function will return without failing.
10236  *
10237  * Since: 2.2
10238  **/
10239 void
10240 pspp_sheet_view_set_cursor_on_cell (PsppSheetView       *tree_view,
10241                                   GtkTreePath       *path,
10242                                   PsppSheetViewColumn *focus_column,
10243                                   GtkCellRenderer   *focus_cell,
10244                                   gboolean           start_editing)
10245 {
10246   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10247   g_return_if_fail (path != NULL);
10248   g_return_if_fail (focus_column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (focus_column));
10249
10250   if (!tree_view->priv->model)
10251     return;
10252
10253   if (focus_cell)
10254     {
10255       g_return_if_fail (focus_column);
10256       g_return_if_fail (GTK_IS_CELL_RENDERER (focus_cell));
10257     }
10258
10259   /* cancel the current editing, if it exists */
10260   if (tree_view->priv->edited_column &&
10261       tree_view->priv->edited_column->editable_widget)
10262     pspp_sheet_view_stop_editing (tree_view, TRUE);
10263
10264   pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, 0);
10265
10266   if (focus_column && focus_column->visible)
10267     {
10268       GList *list;
10269       gboolean column_in_tree = FALSE;
10270
10271       for (list = tree_view->priv->columns; list; list = list->next)
10272         if (list->data == focus_column)
10273           {
10274             column_in_tree = TRUE;
10275             break;
10276           }
10277       g_return_if_fail (column_in_tree);
10278       tree_view->priv->focus_column = focus_column;
10279       if (focus_cell)
10280         pspp_sheet_view_column_focus_cell (focus_column, focus_cell);
10281       if (start_editing)
10282         pspp_sheet_view_start_editing (tree_view, path);
10283
10284       pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
10285       pspp_sheet_selection_select_column (tree_view->priv->selection, focus_column);
10286
10287     }
10288 }
10289
10290 /**
10291  * pspp_sheet_view_get_bin_window:
10292  * @tree_view: A #PsppSheetView
10293  * 
10294  * Returns the window that @tree_view renders to.  This is used primarily to
10295  * compare to <literal>event->window</literal> to confirm that the event on
10296  * @tree_view is on the right window.
10297  * 
10298  * Return value: A #GdkWindow, or %NULL when @tree_view hasn't been realized yet
10299  **/
10300 GdkWindow *
10301 pspp_sheet_view_get_bin_window (PsppSheetView *tree_view)
10302 {
10303   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10304
10305   return tree_view->priv->bin_window;
10306 }
10307
10308 /**
10309  * pspp_sheet_view_get_path_at_pos:
10310  * @tree_view: A #PsppSheetView.
10311  * @x: The x position to be identified (relative to bin_window).
10312  * @y: The y position to be identified (relative to bin_window).
10313  * @path: (out) (allow-none): A pointer to a #GtkTreePath pointer to be filled in, or %NULL
10314  * @column: (out) (allow-none): A pointer to a #PsppSheetViewColumn pointer to be filled in, or %NULL
10315  * @cell_x: (out) (allow-none): A pointer where the X coordinate relative to the cell can be placed, or %NULL
10316  * @cell_y: (out) (allow-none): A pointer where the Y coordinate relative to the cell can be placed, or %NULL
10317  *
10318  * Finds the path at the point (@x, @y), relative to bin_window coordinates
10319  * (please see pspp_sheet_view_get_bin_window()).
10320  * That is, @x and @y are relative to an events coordinates. @x and @y must
10321  * come from an event on the @tree_view only where <literal>event->window ==
10322  * pspp_sheet_view_get_bin_window (<!-- -->)</literal>. It is primarily for
10323  * things like popup menus. If @path is non-%NULL, then it will be filled
10324  * with the #GtkTreePath at that point.  This path should be freed with
10325  * gtk_tree_path_free().  If @column is non-%NULL, then it will be filled
10326  * with the column at that point.  @cell_x and @cell_y return the coordinates
10327  * relative to the cell background (i.e. the @background_area passed to
10328  * gtk_cell_renderer_render()).  This function is only meaningful if
10329  * @tree_view is realized.  Therefore this function will always return %FALSE
10330  * if @tree_view is not realized or does not have a model.
10331  *
10332  * For converting widget coordinates (eg. the ones you get from
10333  * GtkWidget::query-tooltip), please see
10334  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10335  *
10336  * Return value: %TRUE if a row exists at that coordinate.
10337  **/
10338 gboolean
10339 pspp_sheet_view_get_path_at_pos (PsppSheetView        *tree_view,
10340                                gint                x,
10341                                gint                y,
10342                                GtkTreePath       **path,
10343                                PsppSheetViewColumn **column,
10344                                gint               *cell_x,
10345                                gint               *cell_y)
10346 {
10347   int node;
10348   gint y_offset;
10349
10350   g_return_val_if_fail (tree_view != NULL, FALSE);
10351
10352   if (path)
10353     *path = NULL;
10354   if (column)
10355     *column = NULL;
10356
10357   if (tree_view->priv->bin_window == NULL)
10358     return FALSE;
10359
10360   if (tree_view->priv->row_count == 0)
10361     return FALSE;
10362
10363   if (x > gtk_adjustment_get_upper (tree_view->priv->hadjustment))
10364     return FALSE;
10365
10366   if (x < 0 || y < 0)
10367     return FALSE;
10368
10369   if (column || cell_x)
10370     {
10371       PsppSheetViewColumn *tmp_column;
10372       PsppSheetViewColumn *last_column = NULL;
10373       GList *list;
10374       gint remaining_x = x;
10375       gboolean found = FALSE;
10376       gboolean rtl;
10377
10378       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
10379       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
10380            list;
10381            list = (rtl ? list->prev : list->next))
10382         {
10383           tmp_column = list->data;
10384
10385           if (tmp_column->visible == FALSE)
10386             continue;
10387
10388           last_column = tmp_column;
10389           if (remaining_x <= tmp_column->width)
10390             {
10391               found = TRUE;
10392
10393               if (column)
10394                 *column = tmp_column;
10395
10396               if (cell_x)
10397                 *cell_x = remaining_x;
10398
10399               break;
10400             }
10401           remaining_x -= tmp_column->width;
10402         }
10403
10404       /* If found is FALSE and there is a last_column, then it the remainder
10405        * space is in that area
10406        */
10407       if (!found)
10408         {
10409           if (last_column)
10410             {
10411               if (column)
10412                 *column = last_column;
10413               
10414               if (cell_x)
10415                 *cell_x = last_column->width + remaining_x;
10416             }
10417           else
10418             {
10419               return FALSE;
10420             }
10421         }
10422     }
10423
10424   y_offset = pspp_sheet_view_find_offset (tree_view,
10425                                           TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y),
10426                                           &node);
10427
10428   if (node < 0)
10429     return FALSE;
10430
10431   if (cell_y)
10432     *cell_y = y_offset;
10433
10434   if (path)
10435     *path = _pspp_sheet_view_find_path (tree_view, node);
10436
10437   return TRUE;
10438 }
10439
10440 /* Computes 'cell_area' from 'background_area', which must be the background
10441    area for a cell.  Set 'subtract_focus_rect' to TRUE to compute the cell area
10442    as passed to a GtkCellRenderer's "render" function, or to FALSE to compute
10443    the cell area as passed to _pspp_sheet_view_column_cell_render().
10444
10445    'column' is required to properly adjust 'cell_area->x' and
10446    'cell_area->width'.  It may be set to NULL if these values are not of
10447    interest.  In this case 'cell_area->x' and 'cell_area->width' will be
10448    returned as 0. */
10449 static void
10450 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
10451                                   PsppSheetViewColumn  *column,
10452                                   const GdkRectangle   *background_area,
10453                                   gboolean              subtract_focus_rect,
10454                                   GdkRectangle         *cell_area)
10455 {
10456   gint vertical_separator;
10457   gint horizontal_separator;
10458
10459   *cell_area = *background_area;
10460
10461   gtk_widget_style_get (GTK_WIDGET (tree_view),
10462                         "vertical-separator", &vertical_separator,
10463                         "horizontal-separator", &horizontal_separator,
10464                         NULL);
10465   cell_area->x += horizontal_separator / 2;
10466   cell_area->y += vertical_separator / 2;
10467   cell_area->width -= horizontal_separator;
10468   cell_area->height -= vertical_separator;
10469
10470   if (subtract_focus_rect)
10471     {
10472       int focus_line_width;
10473
10474       gtk_widget_style_get (GTK_WIDGET (tree_view),
10475                             "focus-line-width", &focus_line_width,
10476                             NULL);
10477       cell_area->x += focus_line_width;
10478       cell_area->y += focus_line_width;
10479       cell_area->width -= 2 * focus_line_width;
10480       cell_area->height -= 2 * focus_line_width;
10481     }
10482
10483   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_NONE)
10484     {
10485       gint grid_line_width;
10486       gtk_widget_style_get (GTK_WIDGET (tree_view),
10487                             "grid-line-width", &grid_line_width,
10488                             NULL);
10489
10490       if ((tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
10491            || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10492           && column != NULL)
10493         {
10494           PsppSheetViewColumn *first_column, *last_column;
10495           GList *list;
10496
10497           /* Find the last visible column. */
10498           last_column = NULL;
10499           for (list = g_list_last (tree_view->priv->columns);
10500                list;
10501                list = list->prev)
10502             {
10503               PsppSheetViewColumn *c = list->data;
10504               if (c->visible)
10505                 {
10506                   last_column = c;
10507                   break;
10508                 }
10509             }
10510
10511           /* Find the first visible column. */
10512           first_column = NULL;
10513           for (list = g_list_first (tree_view->priv->columns);
10514                list;
10515                list = list->next)
10516             {
10517               PsppSheetViewColumn *c = list->data;
10518               if (c->visible)
10519                 {
10520                   first_column = c;
10521                   break;
10522                 }
10523             }
10524
10525           if (column == first_column)
10526             {
10527               cell_area->width -= grid_line_width / 2;
10528             }
10529           else if (column == last_column)
10530             {
10531               cell_area->x += grid_line_width / 2;
10532               cell_area->width -= grid_line_width / 2;
10533             }
10534           else
10535             {
10536               cell_area->x += grid_line_width / 2;
10537               cell_area->width -= grid_line_width;
10538             }
10539         }
10540
10541       if (tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
10542           || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10543         {
10544           cell_area->y += grid_line_width / 2;
10545           cell_area->height -= grid_line_width;
10546         }
10547     }
10548
10549   if (column == NULL)
10550     {
10551       cell_area->x = 0;
10552       cell_area->width = 0;
10553     }
10554 }
10555
10556 /**
10557  * pspp_sheet_view_get_cell_area:
10558  * @tree_view: a #PsppSheetView
10559  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10560  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordinates
10561  * @rect: rectangle to fill with cell rect
10562  *
10563  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10564  * row specified by @path and the column specified by @column.  If @path is
10565  * %NULL, or points to a path not currently displayed, the @y and @height fields
10566  * of the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10567  * fields will be filled with 0.  The sum of all cell rects does not cover the
10568  * entire tree; there are extra pixels in between rows, for example. The
10569  * returned rectangle is equivalent to the @cell_area passed to
10570  * gtk_cell_renderer_render().  This function is only valid if @tree_view is
10571  * realized.
10572  **/
10573 void
10574 pspp_sheet_view_get_cell_area (PsppSheetView        *tree_view,
10575                              GtkTreePath        *path,
10576                              PsppSheetViewColumn  *column,
10577                              GdkRectangle       *rect)
10578 {
10579   GdkRectangle background_area;
10580
10581   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10582   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10583   g_return_if_fail (rect != NULL);
10584   g_return_if_fail (!column || column->tree_view == (GtkWidget *) tree_view);
10585   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
10586
10587   pspp_sheet_view_get_background_area (tree_view, path, column,
10588                                        &background_area);
10589   pspp_sheet_view_adjust_cell_area (tree_view, column, &background_area,
10590                                     FALSE, rect);
10591 }
10592
10593 /**
10594  * pspp_sheet_view_get_background_area:
10595  * @tree_view: a #PsppSheetView
10596  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10597  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordiantes
10598  * @rect: rectangle to fill with cell background rect
10599  *
10600  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10601  * row specified by @path and the column specified by @column.  If @path is
10602  * %NULL, or points to a node not found in the tree, the @y and @height fields of
10603  * the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10604  * fields will be filled with 0.  The returned rectangle is equivalent to the
10605  * @background_area passed to gtk_cell_renderer_render().  These background
10606  * areas tile to cover the entire bin window.  Contrast with the @cell_area,
10607  * returned by pspp_sheet_view_get_cell_area(), which returns only the cell
10608  * itself, excluding surrounding borders.
10609  *
10610  **/
10611 void
10612 pspp_sheet_view_get_background_area (PsppSheetView        *tree_view,
10613                                    GtkTreePath        *path,
10614                                    PsppSheetViewColumn  *column,
10615                                    GdkRectangle       *rect)
10616 {
10617   int node = -1;
10618
10619   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10620   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10621   g_return_if_fail (rect != NULL);
10622
10623   rect->x = 0;
10624   rect->y = 0;
10625   rect->width = 0;
10626   rect->height = 0;
10627
10628   if (path)
10629     {
10630       /* Get vertical coords */
10631
10632       _pspp_sheet_view_find_node (tree_view, path, &node);
10633       if (node < 0)
10634         return;
10635
10636       rect->y = BACKGROUND_FIRST_PIXEL (tree_view, node);
10637
10638       rect->height = ROW_HEIGHT (tree_view);
10639     }
10640
10641   if (column)
10642     {
10643       gint x2 = 0;
10644
10645       pspp_sheet_view_get_background_xrange (tree_view, column, &rect->x, &x2);
10646       rect->width = x2 - rect->x;
10647     }
10648 }
10649
10650 /**
10651  * pspp_sheet_view_get_visible_rect:
10652  * @tree_view: a #PsppSheetView
10653  * @visible_rect: rectangle to fill
10654  *
10655  * Fills @visible_rect with the currently-visible region of the
10656  * buffer, in tree coordinates. Convert to bin_window coordinates with
10657  * pspp_sheet_view_convert_tree_to_bin_window_coords().
10658  * Tree coordinates start at 0,0 for row 0 of the tree, and cover the entire
10659  * scrollable area of the tree.
10660  **/
10661 void
10662 pspp_sheet_view_get_visible_rect (PsppSheetView  *tree_view,
10663                                 GdkRectangle *visible_rect)
10664 {
10665   GtkWidget *widget;
10666
10667   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10668
10669   widget = GTK_WIDGET (tree_view);
10670
10671   if (visible_rect)
10672     {
10673       GtkAllocation allocation;
10674       gtk_widget_get_allocation (widget, &allocation);
10675       visible_rect->x = gtk_adjustment_get_value (tree_view->priv->hadjustment);
10676       visible_rect->y = gtk_adjustment_get_value (tree_view->priv->vadjustment);
10677       visible_rect->width  = allocation.width;
10678       visible_rect->height = allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
10679     }
10680 }
10681
10682 /**
10683  * pspp_sheet_view_widget_to_tree_coords:
10684  * @tree_view: a #PsppSheetView
10685  * @wx: X coordinate relative to bin_window
10686  * @wy: Y coordinate relative to bin_window
10687  * @tx: return location for tree X coordinate
10688  * @ty: return location for tree Y coordinate
10689  *
10690  * Converts bin_window coordinates to coordinates for the
10691  * tree (the full scrollable area of the tree).
10692  *
10693  * Deprecated: 2.12: Due to historial reasons the name of this function is
10694  * incorrect.  For converting coordinates relative to the widget to
10695  * bin_window coordinates, please see
10696  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10697  *
10698  **/
10699 void
10700 pspp_sheet_view_widget_to_tree_coords (PsppSheetView *tree_view,
10701                                       gint         wx,
10702                                       gint         wy,
10703                                       gint        *tx,
10704                                       gint        *ty)
10705 {
10706   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10707
10708   if (tx)
10709     *tx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10710   if (ty)
10711     *ty = wy + tree_view->priv->dy;
10712 }
10713
10714 /**
10715  * pspp_sheet_view_tree_to_widget_coords:
10716  * @tree_view: a #PsppSheetView
10717  * @tx: tree X coordinate
10718  * @ty: tree Y coordinate
10719  * @wx: return location for X coordinate relative to bin_window
10720  * @wy: return location for Y coordinate relative to bin_window
10721  *
10722  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10723  * to bin_window coordinates.
10724  *
10725  * Deprecated: 2.12: Due to historial reasons the name of this function is
10726  * incorrect.  For converting bin_window coordinates to coordinates relative
10727  * to bin_window, please see
10728  * pspp_sheet_view_convert_bin_window_to_widget_coords().
10729  *
10730  **/
10731 void
10732 pspp_sheet_view_tree_to_widget_coords (PsppSheetView *tree_view,
10733                                      gint         tx,
10734                                      gint         ty,
10735                                      gint        *wx,
10736                                      gint        *wy)
10737 {
10738   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10739
10740   if (wx)
10741     *wx = tx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10742   if (wy)
10743     *wy = ty - tree_view->priv->dy;
10744 }
10745
10746
10747 /**
10748  * pspp_sheet_view_convert_widget_to_tree_coords:
10749  * @tree_view: a #PsppSheetView
10750  * @wx: X coordinate relative to the widget
10751  * @wy: Y coordinate relative to the widget
10752  * @tx: return location for tree X coordinate
10753  * @ty: return location for tree Y coordinate
10754  *
10755  * Converts widget coordinates to coordinates for the
10756  * tree (the full scrollable area of the tree).
10757  *
10758  * Since: 2.12
10759  **/
10760 void
10761 pspp_sheet_view_convert_widget_to_tree_coords (PsppSheetView *tree_view,
10762                                              gint         wx,
10763                                              gint         wy,
10764                                              gint        *tx,
10765                                              gint        *ty)
10766 {
10767   gint x, y;
10768
10769   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10770
10771   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
10772                                                      wx, wy,
10773                                                      &x, &y);
10774   pspp_sheet_view_convert_bin_window_to_tree_coords (tree_view,
10775                                                    x, y,
10776                                                    tx, ty);
10777 }
10778
10779 /**
10780  * pspp_sheet_view_convert_tree_to_widget_coords:
10781  * @tree_view: a #PsppSheetView
10782  * @tx: X coordinate relative to the tree
10783  * @ty: Y coordinate relative to the tree
10784  * @wx: return location for widget X coordinate
10785  * @wy: return location for widget Y coordinate
10786  *
10787  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10788  * to widget coordinates.
10789  *
10790  * Since: 2.12
10791  **/
10792 void
10793 pspp_sheet_view_convert_tree_to_widget_coords (PsppSheetView *tree_view,
10794                                              gint         tx,
10795                                              gint         ty,
10796                                              gint        *wx,
10797                                              gint        *wy)
10798 {
10799   gint x, y;
10800
10801   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10802
10803   pspp_sheet_view_convert_tree_to_bin_window_coords (tree_view,
10804                                                    tx, ty,
10805                                                    &x, &y);
10806   pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
10807                                                      x, y,
10808                                                      wx, wy);
10809 }
10810
10811 /**
10812  * pspp_sheet_view_convert_widget_to_bin_window_coords:
10813  * @tree_view: a #PsppSheetView
10814  * @wx: X coordinate relative to the widget
10815  * @wy: Y coordinate relative to the widget
10816  * @bx: return location for bin_window X coordinate
10817  * @by: return location for bin_window Y coordinate
10818  *
10819  * Converts widget coordinates to coordinates for the bin_window
10820  * (see pspp_sheet_view_get_bin_window()).
10821  *
10822  * Since: 2.12
10823  **/
10824 void
10825 pspp_sheet_view_convert_widget_to_bin_window_coords (PsppSheetView *tree_view,
10826                                                    gint         wx,
10827                                                    gint         wy,
10828                                                    gint        *bx,
10829                                                    gint        *by)
10830 {
10831   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10832
10833   if (bx)
10834     *bx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10835   if (by)
10836     *by = wy - TREE_VIEW_HEADER_HEIGHT (tree_view);
10837 }
10838
10839 /**
10840  * pspp_sheet_view_convert_bin_window_to_widget_coords:
10841  * @tree_view: a #PsppSheetView
10842  * @bx: bin_window X coordinate
10843  * @by: bin_window Y coordinate
10844  * @wx: return location for widget X coordinate
10845  * @wy: return location for widget Y coordinate
10846  *
10847  * Converts bin_window coordinates (see pspp_sheet_view_get_bin_window())
10848  * to widget relative coordinates.
10849  *
10850  * Since: 2.12
10851  **/
10852 void
10853 pspp_sheet_view_convert_bin_window_to_widget_coords (PsppSheetView *tree_view,
10854                                                    gint         bx,
10855                                                    gint         by,
10856                                                    gint        *wx,
10857                                                    gint        *wy)
10858 {
10859   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10860
10861   if (wx)
10862     *wx = bx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10863   if (wy)
10864     *wy = by + TREE_VIEW_HEADER_HEIGHT (tree_view);
10865 }
10866
10867 /**
10868  * pspp_sheet_view_convert_tree_to_bin_window_coords:
10869  * @tree_view: a #PsppSheetView
10870  * @tx: tree X coordinate
10871  * @ty: tree Y coordinate
10872  * @bx: return location for X coordinate relative to bin_window
10873  * @by: return location for Y coordinate relative to bin_window
10874  *
10875  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10876  * to bin_window coordinates.
10877  *
10878  * Since: 2.12
10879  **/
10880 void
10881 pspp_sheet_view_convert_tree_to_bin_window_coords (PsppSheetView *tree_view,
10882                                                  gint         tx,
10883                                                  gint         ty,
10884                                                  gint        *bx,
10885                                                  gint        *by)
10886 {
10887   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10888
10889   if (bx)
10890     *bx = tx;
10891   if (by)
10892     *by = ty - tree_view->priv->dy;
10893 }
10894
10895 /**
10896  * pspp_sheet_view_convert_bin_window_to_tree_coords:
10897  * @tree_view: a #PsppSheetView
10898  * @bx: X coordinate relative to bin_window
10899  * @by: Y coordinate relative to bin_window
10900  * @tx: return location for tree X coordinate
10901  * @ty: return location for tree Y coordinate
10902  *
10903  * Converts bin_window coordinates to coordinates for the
10904  * tree (the full scrollable area of the tree).
10905  *
10906  * Since: 2.12
10907  **/
10908 void
10909 pspp_sheet_view_convert_bin_window_to_tree_coords (PsppSheetView *tree_view,
10910                                                  gint         bx,
10911                                                  gint         by,
10912                                                  gint        *tx,
10913                                                  gint        *ty)
10914 {
10915   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10916
10917   if (tx)
10918     *tx = bx;
10919   if (ty)
10920     *ty = by + tree_view->priv->dy;
10921 }
10922
10923
10924
10925 /**
10926  * pspp_sheet_view_get_visible_range:
10927  * @tree_view: A #PsppSheetView
10928  * @start_path: (allow-none): Return location for start of region, or %NULL.
10929  * @end_path: (allow-none): Return location for end of region, or %NULL.
10930  *
10931  * Sets @start_path and @end_path to be the first and last visible path.
10932  * Note that there may be invisible paths in between.
10933  *
10934  * The paths should be freed with gtk_tree_path_free() after use.
10935  *
10936  * Returns: %TRUE, if valid paths were placed in @start_path and @end_path.
10937  *
10938  * Since: 2.8
10939  **/
10940 gboolean
10941 pspp_sheet_view_get_visible_range (PsppSheetView  *tree_view,
10942                                  GtkTreePath **start_path,
10943                                  GtkTreePath **end_path)
10944 {
10945   int node;
10946   gboolean retval;
10947   
10948   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10949
10950   if (!tree_view->priv->row_count)
10951     return FALSE;
10952
10953   retval = TRUE;
10954
10955   if (start_path)
10956     {
10957       pspp_sheet_view_find_offset (tree_view,
10958                                    TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, 0),
10959                                    &node);
10960       if (node >= 0)
10961         *start_path = _pspp_sheet_view_find_path (tree_view, node);
10962       else
10963         retval = FALSE;
10964     }
10965
10966   if (end_path)
10967     {
10968       gint y;
10969
10970       if (tree_view->priv->height < gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
10971         y = tree_view->priv->height - 1;
10972       else
10973         y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, gtk_adjustment_get_page_size (tree_view->priv->vadjustment)) - 1;
10974
10975       pspp_sheet_view_find_offset (tree_view, y, &node);
10976       if (node >= 0)
10977         *end_path = _pspp_sheet_view_find_path (tree_view, node);
10978       else
10979         retval = FALSE;
10980     }
10981
10982   return retval;
10983 }
10984
10985 static void
10986 unset_reorderable (PsppSheetView *tree_view)
10987 {
10988   if (tree_view->priv->reorderable)
10989     {
10990       tree_view->priv->reorderable = FALSE;
10991       g_object_notify (G_OBJECT (tree_view), "reorderable");
10992     }
10993 }
10994
10995 /**
10996  * pspp_sheet_view_enable_model_drag_source:
10997  * @tree_view: a #PsppSheetView
10998  * @start_button_mask: Mask of allowed buttons to start drag
10999  * @targets: the table of targets that the drag will support
11000  * @n_targets: the number of items in @targets
11001  * @actions: the bitmask of possible actions for a drag from this
11002  *    widget
11003  *
11004  * Turns @tree_view into a drag source for automatic DND. Calling this
11005  * method sets #PsppSheetView:reorderable to %FALSE.
11006  **/
11007 void
11008 pspp_sheet_view_enable_model_drag_source (PsppSheetView              *tree_view,
11009                                         GdkModifierType           start_button_mask,
11010                                         const GtkTargetEntry     *targets,
11011                                         gint                      n_targets,
11012                                         GdkDragAction             actions)
11013 {
11014   TreeViewDragInfo *di;
11015
11016   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11017
11018   gtk_drag_source_set (GTK_WIDGET (tree_view),
11019                        0,
11020                        targets,
11021                        n_targets,
11022                        actions);
11023
11024   di = ensure_info (tree_view);
11025
11026   di->start_button_mask = start_button_mask;
11027   di->source_actions = actions;
11028   di->source_set = TRUE;
11029
11030   unset_reorderable (tree_view);
11031 }
11032
11033 /**
11034  * pspp_sheet_view_enable_model_drag_dest:
11035  * @tree_view: a #PsppSheetView
11036  * @targets: the table of targets that the drag will support
11037  * @n_targets: the number of items in @targets
11038  * @actions: the bitmask of possible actions for a drag from this
11039  *    widget
11040  * 
11041  * Turns @tree_view into a drop destination for automatic DND. Calling
11042  * this method sets #PsppSheetView:reorderable to %FALSE.
11043  **/
11044 void
11045 pspp_sheet_view_enable_model_drag_dest (PsppSheetView              *tree_view,
11046                                       const GtkTargetEntry     *targets,
11047                                       gint                      n_targets,
11048                                       GdkDragAction             actions)
11049 {
11050   TreeViewDragInfo *di;
11051
11052   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11053
11054   gtk_drag_dest_set (GTK_WIDGET (tree_view),
11055                      0,
11056                      targets,
11057                      n_targets,
11058                      actions);
11059
11060   di = ensure_info (tree_view);
11061   di->dest_set = TRUE;
11062
11063   unset_reorderable (tree_view);
11064 }
11065
11066 /**
11067  * pspp_sheet_view_unset_rows_drag_source:
11068  * @tree_view: a #PsppSheetView
11069  *
11070  * Undoes the effect of
11071  * pspp_sheet_view_enable_model_drag_source(). Calling this method sets
11072  * #PsppSheetView:reorderable to %FALSE.
11073  **/
11074 void
11075 pspp_sheet_view_unset_rows_drag_source (PsppSheetView *tree_view)
11076 {
11077   TreeViewDragInfo *di;
11078
11079   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11080
11081   di = get_info (tree_view);
11082
11083   if (di)
11084     {
11085       if (di->source_set)
11086         {
11087           gtk_drag_source_unset (GTK_WIDGET (tree_view));
11088           di->source_set = FALSE;
11089         }
11090
11091       if (!di->dest_set && !di->source_set)
11092         remove_info (tree_view);
11093     }
11094   
11095   unset_reorderable (tree_view);
11096 }
11097
11098 /**
11099  * pspp_sheet_view_unset_rows_drag_dest:
11100  * @tree_view: a #PsppSheetView
11101  *
11102  * Undoes the effect of
11103  * pspp_sheet_view_enable_model_drag_dest(). Calling this method sets
11104  * #PsppSheetView:reorderable to %FALSE.
11105  **/
11106 void
11107 pspp_sheet_view_unset_rows_drag_dest (PsppSheetView *tree_view)
11108 {
11109   TreeViewDragInfo *di;
11110
11111   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11112
11113   di = get_info (tree_view);
11114
11115   if (di)
11116     {
11117       if (di->dest_set)
11118         {
11119           gtk_drag_dest_unset (GTK_WIDGET (tree_view));
11120           di->dest_set = FALSE;
11121         }
11122
11123       if (!di->dest_set && !di->source_set)
11124         remove_info (tree_view);
11125     }
11126
11127   unset_reorderable (tree_view);
11128 }
11129
11130 /**
11131  * pspp_sheet_view_set_drag_dest_row:
11132  * @tree_view: a #PsppSheetView
11133  * @path: (allow-none): The path of the row to highlight, or %NULL.
11134  * @pos: Specifies whether to drop before, after or into the row
11135  * 
11136  * Sets the row that is highlighted for feedback.
11137  **/
11138 void
11139 pspp_sheet_view_set_drag_dest_row (PsppSheetView            *tree_view,
11140                                  GtkTreePath            *path,
11141                                  PsppSheetViewDropPosition pos)
11142 {
11143   GtkTreePath *current_dest;
11144
11145   /* Note; this function is exported to allow a custom DND
11146    * implementation, so it can't touch TreeViewDragInfo
11147    */
11148
11149   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11150
11151   current_dest = NULL;
11152
11153   if (tree_view->priv->drag_dest_row)
11154     {
11155       current_dest = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11156       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
11157     }
11158
11159   /* special case a drop on an empty model */
11160   tree_view->priv->empty_view_drop = 0;
11161
11162   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE && path
11163       && gtk_tree_path_get_depth (path) == 1
11164       && gtk_tree_path_get_indices (path)[0] == 0)
11165     {
11166       gint n_children;
11167
11168       n_children = gtk_tree_model_iter_n_children (tree_view->priv->model,
11169                                                    NULL);
11170
11171       if (!n_children)
11172         tree_view->priv->empty_view_drop = 1;
11173     }
11174
11175   tree_view->priv->drag_dest_pos = pos;
11176
11177   if (path)
11178     {
11179       tree_view->priv->drag_dest_row =
11180         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
11181       pspp_sheet_view_queue_draw_path (tree_view, path, NULL);
11182     }
11183   else
11184     tree_view->priv->drag_dest_row = NULL;
11185
11186   if (current_dest)
11187     {
11188       int node, new_node;
11189
11190       _pspp_sheet_view_find_node (tree_view, current_dest, &node);
11191       _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
11192
11193       if (node >= 0)
11194         {
11195           new_node = pspp_sheet_view_node_next (tree_view, node);
11196           if (new_node >= 0)
11197             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11198
11199           new_node = pspp_sheet_view_node_prev (tree_view, node);
11200           if (new_node >= 0)
11201             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11202         }
11203       gtk_tree_path_free (current_dest);
11204     }
11205 }
11206
11207 /**
11208  * pspp_sheet_view_get_drag_dest_row:
11209  * @tree_view: a #PsppSheetView
11210  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11211  * @pos: (allow-none): Return location for the drop position, or %NULL
11212  * 
11213  * Gets information about the row that is highlighted for feedback.
11214  **/
11215 void
11216 pspp_sheet_view_get_drag_dest_row (PsppSheetView              *tree_view,
11217                                  GtkTreePath             **path,
11218                                  PsppSheetViewDropPosition  *pos)
11219 {
11220   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11221
11222   if (path)
11223     {
11224       if (tree_view->priv->drag_dest_row)
11225         *path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11226       else
11227         {
11228           if (tree_view->priv->empty_view_drop)
11229             *path = gtk_tree_path_new_from_indices (0, -1);
11230           else
11231             *path = NULL;
11232         }
11233     }
11234
11235   if (pos)
11236     *pos = tree_view->priv->drag_dest_pos;
11237 }
11238
11239 /**
11240  * pspp_sheet_view_get_dest_row_at_pos:
11241  * @tree_view: a #PsppSheetView
11242  * @drag_x: the position to determine the destination row for
11243  * @drag_y: the position to determine the destination row for
11244  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11245  * @pos: (allow-none): Return location for the drop position, or %NULL
11246  * 
11247  * Determines the destination row for a given position.  @drag_x and
11248  * @drag_y are expected to be in widget coordinates.  This function is only
11249  * meaningful if @tree_view is realized.  Therefore this function will always
11250  * return %FALSE if @tree_view is not realized or does not have a model.
11251  * 
11252  * Return value: whether there is a row at the given position, %TRUE if this
11253  * is indeed the case.
11254  **/
11255 gboolean
11256 pspp_sheet_view_get_dest_row_at_pos (PsppSheetView             *tree_view,
11257                                    gint                     drag_x,
11258                                    gint                     drag_y,
11259                                    GtkTreePath            **path,
11260                                    PsppSheetViewDropPosition *pos)
11261 {
11262   gint cell_y;
11263   gint bin_x, bin_y;
11264   gdouble offset_into_row;
11265   gdouble third;
11266   GdkRectangle cell;
11267   PsppSheetViewColumn *column = NULL;
11268   GtkTreePath *tmp_path = NULL;
11269
11270   /* Note; this function is exported to allow a custom DND
11271    * implementation, so it can't touch TreeViewDragInfo
11272    */
11273
11274   g_return_val_if_fail (tree_view != NULL, FALSE);
11275   g_return_val_if_fail (drag_x >= 0, FALSE);
11276   g_return_val_if_fail (drag_y >= 0, FALSE);
11277
11278   if (path)
11279     *path = NULL;
11280
11281   if (tree_view->priv->bin_window == NULL)
11282     return FALSE;
11283
11284   if (tree_view->priv->row_count == 0)
11285     return FALSE;
11286
11287   /* If in the top third of a row, we drop before that row; if
11288    * in the bottom third, drop after that row; if in the middle,
11289    * and the row has children, drop into the row.
11290    */
11291   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, drag_x, drag_y,
11292                                                      &bin_x, &bin_y);
11293
11294   if (!pspp_sheet_view_get_path_at_pos (tree_view,
11295                                       bin_x,
11296                                       bin_y,
11297                                       &tmp_path,
11298                                       &column,
11299                                       NULL,
11300                                       &cell_y))
11301     return FALSE;
11302
11303   pspp_sheet_view_get_background_area (tree_view, tmp_path, column,
11304                                      &cell);
11305
11306   offset_into_row = cell_y;
11307
11308   if (path)
11309     *path = tmp_path;
11310   else
11311     gtk_tree_path_free (tmp_path);
11312
11313   tmp_path = NULL;
11314
11315   third = cell.height / 3.0;
11316
11317   if (pos)
11318     {
11319       if (offset_into_row < third)
11320         {
11321           *pos = PSPP_SHEET_VIEW_DROP_BEFORE;
11322         }
11323       else if (offset_into_row < (cell.height / 2.0))
11324         {
11325           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE;
11326         }
11327       else if (offset_into_row < third * 2.0)
11328         {
11329           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER;
11330         }
11331       else
11332         {
11333           *pos = PSPP_SHEET_VIEW_DROP_AFTER;
11334         }
11335     }
11336
11337   return TRUE;
11338 }
11339
11340
11341 #if GTK3_TRANSITION
11342 /* KEEP IN SYNC WITH PSPP_SHEET_VIEW_BIN_EXPOSE */
11343 /**
11344  * pspp_sheet_view_create_row_drag_icon:
11345  * @tree_view: a #PsppSheetView
11346  * @path: a #GtkTreePath in @tree_view
11347  *
11348  * Creates a #GdkPixmap representation of the row at @path.  
11349  * This image is used for a drag icon.
11350  *
11351  * Return value: a newly-allocated pixmap of the drag icon.
11352  **/
11353 GdkPixmap *
11354 pspp_sheet_view_create_row_drag_icon (PsppSheetView  *tree_view,
11355                                     GtkTreePath  *path)
11356 {
11357   GtkTreeIter   iter;
11358   int node;
11359   gint cell_offset;
11360   GList *list;
11361   GdkRectangle background_area;
11362   GdkRectangle expose_area;
11363   GtkWidget *widget;
11364   /* start drawing inside the black outline */
11365   gint x = 1, y = 1;
11366   GdkDrawable *drawable;
11367   gint bin_window_width;
11368   gboolean rtl;
11369
11370   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11371   g_return_val_if_fail (path != NULL, NULL);
11372
11373   widget = GTK_WIDGET (tree_view);
11374
11375   if (!gtk_widget_get_realized (widget))
11376     return NULL;
11377
11378   _pspp_sheet_view_find_node (tree_view,
11379                             path,
11380                             &node);
11381
11382   if (node < 0)
11383     return NULL;
11384
11385   if (!gtk_tree_model_get_iter (tree_view->priv->model,
11386                                 &iter,
11387                                 path))
11388     return NULL;
11389   
11390   cell_offset = x;
11391
11392   background_area.y = y;
11393   background_area.height = ROW_HEIGHT (tree_view);
11394
11395   bin_window_width = gdk_window_get_width (tree_view->priv->bin_window);
11396
11397   drawable = gdk_pixmap_new (tree_view->priv->bin_window,
11398                              bin_window_width + 2,
11399                              background_area.height + 2,
11400                              -1);
11401
11402   expose_area.x = 0;
11403   expose_area.y = 0;
11404   expose_area.width = bin_window_width + 2;
11405   expose_area.height = background_area.height + 2;
11406
11407 #if GTK3_TRANSITION
11408   gdk_draw_rectangle (drawable,
11409                       widget->style->base_gc [gtk_widget_get_state (widget)],
11410                       TRUE,
11411                       0, 0,
11412                       bin_window_width + 2,
11413                       background_area.height + 2);
11414 #endif
11415
11416   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
11417
11418   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
11419       list;
11420       list = (rtl ? list->prev : list->next))
11421     {
11422       PsppSheetViewColumn *column = list->data;
11423       GdkRectangle cell_area;
11424       gint vertical_separator;
11425
11426       if (!column->visible)
11427         continue;
11428
11429       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, &iter);
11430
11431       background_area.x = cell_offset;
11432       background_area.width = column->width;
11433
11434       gtk_widget_style_get (widget,
11435                             "vertical-separator", &vertical_separator,
11436                             NULL);
11437
11438       cell_area = background_area;
11439
11440       cell_area.y += vertical_separator / 2;
11441       cell_area.height -= vertical_separator;
11442
11443       if (pspp_sheet_view_column_cell_is_visible (column))
11444         _pspp_sheet_view_column_cell_render (column,
11445                                              drawable,
11446                                              &background_area,
11447                                              &cell_area,
11448                                              &expose_area,
11449                                              0);
11450       cell_offset += column->width;
11451     }
11452
11453 #if GTK3_TRANSITION
11454   gdk_draw_rectangle (drawable,
11455                       widget->style->black_gc,
11456                       FALSE,
11457                       0, 0,
11458                       bin_window_width + 1,
11459                       background_area.height + 1);
11460 #endif
11461
11462   return drawable;
11463 }
11464 #endif
11465
11466 /**
11467  * pspp_sheet_view_set_destroy_count_func:
11468  * @tree_view: A #PsppSheetView
11469  * @func: (allow-none): Function to be called when a view row is destroyed, or %NULL
11470  * @data: (allow-none): User data to be passed to @func, or %NULL
11471  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11472  *
11473  * This function should almost never be used.  It is meant for private use by
11474  * ATK for determining the number of visible children that are removed when a row is deleted.
11475  **/
11476 void
11477 pspp_sheet_view_set_destroy_count_func (PsppSheetView             *tree_view,
11478                                       PsppSheetDestroyCountFunc  func,
11479                                       gpointer                 data,
11480                                       GDestroyNotify           destroy)
11481 {
11482   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11483
11484   if (tree_view->priv->destroy_count_destroy)
11485     tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
11486
11487   tree_view->priv->destroy_count_func = func;
11488   tree_view->priv->destroy_count_data = data;
11489   tree_view->priv->destroy_count_destroy = destroy;
11490 }
11491
11492
11493 /*
11494  * Interactive search
11495  */
11496
11497 /**
11498  * pspp_sheet_view_set_enable_search:
11499  * @tree_view: A #PsppSheetView
11500  * @enable_search: %TRUE, if the user can search interactively
11501  *
11502  * If @enable_search is set, then the user can type in text to search through
11503  * the tree interactively (this is sometimes called "typeahead find").
11504  * 
11505  * Note that even if this is %FALSE, the user can still initiate a search 
11506  * using the "start-interactive-search" key binding.
11507  */
11508 void
11509 pspp_sheet_view_set_enable_search (PsppSheetView *tree_view,
11510                                  gboolean     enable_search)
11511 {
11512   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11513
11514   enable_search = !!enable_search;
11515   
11516   if (tree_view->priv->enable_search != enable_search)
11517     {
11518        tree_view->priv->enable_search = enable_search;
11519        g_object_notify (G_OBJECT (tree_view), "enable-search");
11520     }
11521 }
11522
11523 /**
11524  * pspp_sheet_view_get_enable_search:
11525  * @tree_view: A #PsppSheetView
11526  *
11527  * Returns whether or not the tree allows to start interactive searching 
11528  * by typing in text.
11529  *
11530  * Return value: whether or not to let the user search interactively
11531  */
11532 gboolean
11533 pspp_sheet_view_get_enable_search (PsppSheetView *tree_view)
11534 {
11535   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11536
11537   return tree_view->priv->enable_search;
11538 }
11539
11540
11541 /**
11542  * pspp_sheet_view_get_search_column:
11543  * @tree_view: A #PsppSheetView
11544  *
11545  * Gets the column searched on by the interactive search code.
11546  *
11547  * Return value: the column the interactive search code searches in.
11548  */
11549 gint
11550 pspp_sheet_view_get_search_column (PsppSheetView *tree_view)
11551 {
11552   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11553
11554   return (tree_view->priv->search_column);
11555 }
11556
11557 /**
11558  * pspp_sheet_view_set_search_column:
11559  * @tree_view: A #PsppSheetView
11560  * @column: the column of the model to search in, or -1 to disable searching
11561  *
11562  * Sets @column as the column where the interactive search code should
11563  * search in for the current model. 
11564  * 
11565  * If the search column is set, users can use the "start-interactive-search"
11566  * key binding to bring up search popup. The enable-search property controls
11567  * whether simply typing text will also start an interactive search.
11568  *
11569  * Note that @column refers to a column of the current model. The search 
11570  * column is reset to -1 when the model is changed.
11571  */
11572 void
11573 pspp_sheet_view_set_search_column (PsppSheetView *tree_view,
11574                                  gint         column)
11575 {
11576   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11577   g_return_if_fail (column >= -1);
11578
11579   if (tree_view->priv->search_column == column)
11580     return;
11581
11582   tree_view->priv->search_column = column;
11583   g_object_notify (G_OBJECT (tree_view), "search-column");
11584 }
11585
11586 /**
11587  * pspp_sheet_view_get_search_equal_func:
11588  * @tree_view: A #PsppSheetView
11589  *
11590  * Returns the compare function currently in use.
11591  *
11592  * Return value: the currently used compare function for the search code.
11593  */
11594
11595 PsppSheetViewSearchEqualFunc
11596 pspp_sheet_view_get_search_equal_func (PsppSheetView *tree_view)
11597 {
11598   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11599
11600   return tree_view->priv->search_equal_func;
11601 }
11602
11603 /**
11604  * pspp_sheet_view_set_search_equal_func:
11605  * @tree_view: A #PsppSheetView
11606  * @search_equal_func: the compare function to use during the search
11607  * @search_user_data: (allow-none): user data to pass to @search_equal_func, or %NULL
11608  * @search_destroy: (allow-none): Destroy notifier for @search_user_data, or %NULL
11609  *
11610  * Sets the compare function for the interactive search capabilities; note
11611  * that somewhat like strcmp() returning 0 for equality
11612  * #PsppSheetViewSearchEqualFunc returns %FALSE on matches.
11613  **/
11614 void
11615 pspp_sheet_view_set_search_equal_func (PsppSheetView                *tree_view,
11616                                      PsppSheetViewSearchEqualFunc  search_equal_func,
11617                                      gpointer                    search_user_data,
11618                                      GDestroyNotify              search_destroy)
11619 {
11620   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11621   g_return_if_fail (search_equal_func != NULL);
11622
11623   if (tree_view->priv->search_destroy)
11624     tree_view->priv->search_destroy (tree_view->priv->search_user_data);
11625
11626   tree_view->priv->search_equal_func = search_equal_func;
11627   tree_view->priv->search_user_data = search_user_data;
11628   tree_view->priv->search_destroy = search_destroy;
11629   if (tree_view->priv->search_equal_func == NULL)
11630     tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
11631 }
11632
11633 /**
11634  * pspp_sheet_view_get_search_entry:
11635  * @tree_view: A #PsppSheetView
11636  *
11637  * Returns the #GtkEntry which is currently in use as interactive search
11638  * entry for @tree_view.  In case the built-in entry is being used, %NULL
11639  * will be returned.
11640  *
11641  * Return value: the entry currently in use as search entry.
11642  *
11643  * Since: 2.10
11644  */
11645 GtkEntry *
11646 pspp_sheet_view_get_search_entry (PsppSheetView *tree_view)
11647 {
11648   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11649
11650   if (tree_view->priv->search_custom_entry_set)
11651     return GTK_ENTRY (tree_view->priv->search_entry);
11652
11653   return NULL;
11654 }
11655
11656 /**
11657  * pspp_sheet_view_set_search_entry:
11658  * @tree_view: A #PsppSheetView
11659  * @entry: (allow-none): the entry the interactive search code of @tree_view should use or %NULL
11660  *
11661  * Sets the entry which the interactive search code will use for this
11662  * @tree_view.  This is useful when you want to provide a search entry
11663  * in our interface at all time at a fixed position.  Passing %NULL for
11664  * @entry will make the interactive search code use the built-in popup
11665  * entry again.
11666  *
11667  * Since: 2.10
11668  */
11669 void
11670 pspp_sheet_view_set_search_entry (PsppSheetView *tree_view,
11671                                 GtkEntry    *entry)
11672 {
11673   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11674   g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
11675
11676   if (tree_view->priv->search_custom_entry_set)
11677     {
11678       if (tree_view->priv->search_entry_changed_id)
11679         {
11680           g_signal_handler_disconnect (tree_view->priv->search_entry,
11681                                        tree_view->priv->search_entry_changed_id);
11682           tree_view->priv->search_entry_changed_id = 0;
11683         }
11684       g_signal_handlers_disconnect_by_func (tree_view->priv->search_entry,
11685                                             G_CALLBACK (pspp_sheet_view_search_key_press_event),
11686                                             tree_view);
11687
11688       g_object_unref (tree_view->priv->search_entry);
11689     }
11690   else if (tree_view->priv->search_window)
11691     {
11692       gtk_widget_destroy (tree_view->priv->search_window);
11693
11694       tree_view->priv->search_window = NULL;
11695     }
11696
11697   if (entry)
11698     {
11699       tree_view->priv->search_entry = g_object_ref (entry);
11700       tree_view->priv->search_custom_entry_set = TRUE;
11701
11702       if (tree_view->priv->search_entry_changed_id == 0)
11703         {
11704           tree_view->priv->search_entry_changed_id =
11705             g_signal_connect (tree_view->priv->search_entry, "changed",
11706                               G_CALLBACK (pspp_sheet_view_search_init),
11707                               tree_view);
11708         }
11709       
11710         g_signal_connect (tree_view->priv->search_entry, "key-press-event",
11711                           G_CALLBACK (pspp_sheet_view_search_key_press_event),
11712                           tree_view);
11713
11714         pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
11715     }
11716   else
11717     {
11718       tree_view->priv->search_entry = NULL;
11719       tree_view->priv->search_custom_entry_set = FALSE;
11720     }
11721 }
11722
11723 /**
11724  * pspp_sheet_view_set_search_position_func:
11725  * @tree_view: A #PsppSheetView
11726  * @func: (allow-none): the function to use to position the search dialog, or %NULL
11727  *    to use the default search position function
11728  * @data: (allow-none): user data to pass to @func, or %NULL
11729  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11730  *
11731  * Sets the function to use when positioning the search dialog.
11732  *
11733  * Since: 2.10
11734  **/
11735 void
11736 pspp_sheet_view_set_search_position_func (PsppSheetView                   *tree_view,
11737                                         PsppSheetViewSearchPositionFunc  func,
11738                                         gpointer                       user_data,
11739                                         GDestroyNotify                 destroy)
11740 {
11741   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11742
11743   if (tree_view->priv->search_position_destroy)
11744     tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
11745
11746   tree_view->priv->search_position_func = func;
11747   tree_view->priv->search_position_user_data = user_data;
11748   tree_view->priv->search_position_destroy = destroy;
11749   if (tree_view->priv->search_position_func == NULL)
11750     tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
11751 }
11752
11753 /**
11754  * pspp_sheet_view_get_search_position_func:
11755  * @tree_view: A #PsppSheetView
11756  *
11757  * Returns the positioning function currently in use.
11758  *
11759  * Return value: the currently used function for positioning the search dialog.
11760  *
11761  * Since: 2.10
11762  */
11763 PsppSheetViewSearchPositionFunc
11764 pspp_sheet_view_get_search_position_func (PsppSheetView *tree_view)
11765 {
11766   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11767
11768   return tree_view->priv->search_position_func;
11769 }
11770
11771
11772 static void
11773 pspp_sheet_view_search_dialog_hide (GtkWidget   *search_dialog,
11774                                   PsppSheetView *tree_view)
11775 {
11776   if (tree_view->priv->disable_popdown)
11777     return;
11778
11779   if (tree_view->priv->search_entry_changed_id)
11780     {
11781       g_signal_handler_disconnect (tree_view->priv->search_entry,
11782                                    tree_view->priv->search_entry_changed_id);
11783       tree_view->priv->search_entry_changed_id = 0;
11784     }
11785   if (tree_view->priv->typeselect_flush_timeout)
11786     {
11787       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11788       tree_view->priv->typeselect_flush_timeout = 0;
11789     }
11790         
11791   if (gtk_widget_get_visible (search_dialog))
11792     {
11793       /* send focus-in event */
11794       send_focus_change (GTK_WIDGET (tree_view->priv->search_entry), FALSE);
11795       gtk_widget_hide (search_dialog);
11796       gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
11797       send_focus_change (GTK_WIDGET (tree_view), TRUE);
11798     }
11799 }
11800
11801 static void
11802 pspp_sheet_view_search_position_func (PsppSheetView *tree_view,
11803                                     GtkWidget   *search_dialog,
11804                                     gpointer     user_data)
11805 {
11806   gint x, y;
11807   gint tree_x, tree_y;
11808   gint tree_width, tree_height;
11809   GdkWindow *tree_window = gtk_widget_get_window (GTK_WIDGET (tree_view));
11810   GdkScreen *screen = gdk_window_get_screen (tree_window);
11811   GtkRequisition requisition;
11812   gint monitor_num;
11813   GdkRectangle monitor;
11814
11815   monitor_num = gdk_screen_get_monitor_at_window (screen, tree_window);
11816   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
11817
11818   gtk_widget_realize (search_dialog);
11819
11820   gdk_window_get_origin (tree_window, &tree_x, &tree_y);
11821   tree_width = gdk_window_get_width (tree_window);
11822   tree_height = gdk_window_get_height (tree_window);
11823
11824   gtk_widget_size_request (search_dialog, &requisition);
11825
11826   if (tree_x + tree_width > gdk_screen_get_width (screen))
11827     x = gdk_screen_get_width (screen) - requisition.width;
11828   else if (tree_x + tree_width - requisition.width < 0)
11829     x = 0;
11830   else
11831     x = tree_x + tree_width - requisition.width;
11832
11833   if (tree_y + tree_height + requisition.height > gdk_screen_get_height (screen))
11834     y = gdk_screen_get_height (screen) - requisition.height;
11835   else if (tree_y + tree_height < 0) /* isn't really possible ... */
11836     y = 0;
11837   else
11838     y = tree_y + tree_height;
11839
11840   gtk_window_move (GTK_WINDOW (search_dialog), x, y);
11841 }
11842
11843 static void
11844 pspp_sheet_view_search_disable_popdown (GtkEntry *entry,
11845                                       GtkMenu  *menu,
11846                                       gpointer  data)
11847 {
11848   PsppSheetView *tree_view = (PsppSheetView *)data;
11849
11850   tree_view->priv->disable_popdown = 1;
11851   g_signal_connect (menu, "hide",
11852                     G_CALLBACK (pspp_sheet_view_search_enable_popdown), data);
11853 }
11854
11855 #if GTK3_TRANSITION
11856 /* Because we're visible but offscreen, we just set a flag in the preedit
11857  * callback.
11858  */
11859 static void
11860 pspp_sheet_view_search_preedit_changed (GtkIMContext *im_context,
11861                                       PsppSheetView  *tree_view)
11862 {
11863   tree_view->priv->imcontext_changed = 1;
11864   if (tree_view->priv->typeselect_flush_timeout)
11865     {
11866       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11867       tree_view->priv->typeselect_flush_timeout =
11868         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11869                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11870                        tree_view);
11871     }
11872
11873 }
11874 #endif
11875
11876 static void
11877 pspp_sheet_view_search_activate (GtkEntry    *entry,
11878                                PsppSheetView *tree_view)
11879 {
11880   GtkTreePath *path;
11881   int node;
11882
11883   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window,
11884                                     tree_view);
11885
11886   /* If we have a row selected and it's the cursor row, we activate
11887    * the row XXX */
11888   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
11889     {
11890       path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
11891       
11892       _pspp_sheet_view_find_node (tree_view, path, &node);
11893       
11894       if (node >= 0 && pspp_sheet_view_node_is_selected (tree_view, node))
11895         pspp_sheet_view_row_activated (tree_view, path, tree_view->priv->focus_column);
11896       
11897       gtk_tree_path_free (path);
11898     }
11899 }
11900
11901 static gboolean
11902 pspp_sheet_view_real_search_enable_popdown (gpointer data)
11903 {
11904   PsppSheetView *tree_view = (PsppSheetView *)data;
11905
11906   tree_view->priv->disable_popdown = 0;
11907
11908   return FALSE;
11909 }
11910
11911 static void
11912 pspp_sheet_view_search_enable_popdown (GtkWidget *widget,
11913                                      gpointer   data)
11914 {
11915   gdk_threads_add_timeout_full (G_PRIORITY_HIGH, 200, pspp_sheet_view_real_search_enable_popdown, g_object_ref (data), g_object_unref);
11916 }
11917
11918 static gboolean
11919 pspp_sheet_view_search_delete_event (GtkWidget *widget,
11920                                    GdkEventAny *event,
11921                                    PsppSheetView *tree_view)
11922 {
11923   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11924
11925   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11926
11927   return TRUE;
11928 }
11929
11930 static gboolean
11931 pspp_sheet_view_search_button_press_event (GtkWidget *widget,
11932                                          GdkEventButton *event,
11933                                          PsppSheetView *tree_view)
11934 {
11935   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11936
11937   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11938
11939   if (event->window == tree_view->priv->bin_window)
11940     pspp_sheet_view_button_press (GTK_WIDGET (tree_view), event);
11941
11942   return TRUE;
11943 }
11944
11945 static gboolean
11946 pspp_sheet_view_search_scroll_event (GtkWidget *widget,
11947                                    GdkEventScroll *event,
11948                                    PsppSheetView *tree_view)
11949 {
11950   gboolean retval = FALSE;
11951
11952   if (event->direction == GDK_SCROLL_UP)
11953     {
11954       pspp_sheet_view_search_move (widget, tree_view, TRUE);
11955       retval = TRUE;
11956     }
11957   else if (event->direction == GDK_SCROLL_DOWN)
11958     {
11959       pspp_sheet_view_search_move (widget, tree_view, FALSE);
11960       retval = TRUE;
11961     }
11962
11963   /* renew the flush timeout */
11964   if (retval && tree_view->priv->typeselect_flush_timeout
11965       && !tree_view->priv->search_custom_entry_set)
11966     {
11967       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11968       tree_view->priv->typeselect_flush_timeout =
11969         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11970                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11971                        tree_view);
11972     }
11973
11974   return retval;
11975 }
11976
11977 static gboolean
11978 pspp_sheet_view_search_key_press_event (GtkWidget *widget,
11979                                       GdkEventKey *event,
11980                                       PsppSheetView *tree_view)
11981 {
11982   gboolean retval = FALSE;
11983
11984   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11985   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11986
11987   /* close window and cancel the search */
11988   if (!tree_view->priv->search_custom_entry_set
11989       && (event->keyval == GDK_Escape ||
11990           event->keyval == GDK_Tab ||
11991             event->keyval == GDK_KP_Tab ||
11992             event->keyval == GDK_ISO_Left_Tab))
11993     {
11994       pspp_sheet_view_search_dialog_hide (widget, tree_view);
11995       return TRUE;
11996     }
11997
11998   /* select previous matching iter */
11999   if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
12000     {
12001       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
12002         gtk_widget_error_bell (widget);
12003
12004       retval = TRUE;
12005     }
12006
12007   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK))
12008       && (event->keyval == GDK_g || event->keyval == GDK_G))
12009     {
12010       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
12011         gtk_widget_error_bell (widget);
12012
12013       retval = TRUE;
12014     }
12015
12016   /* select next matching iter */
12017   if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
12018     {
12019       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12020         gtk_widget_error_bell (widget);
12021
12022       retval = TRUE;
12023     }
12024
12025   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == GTK_DEFAULT_ACCEL_MOD_MASK)
12026       && (event->keyval == GDK_g || event->keyval == GDK_G))
12027     {
12028       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12029         gtk_widget_error_bell (widget);
12030
12031       retval = TRUE;
12032     }
12033
12034   /* renew the flush timeout */
12035   if (retval && tree_view->priv->typeselect_flush_timeout
12036       && !tree_view->priv->search_custom_entry_set)
12037     {
12038       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12039       tree_view->priv->typeselect_flush_timeout =
12040         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12041                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12042                        tree_view);
12043     }
12044
12045   return retval;
12046 }
12047
12048 /*  this function returns FALSE if there is a search string but
12049  *  nothing was found, and TRUE otherwise.
12050  */
12051 static gboolean
12052 pspp_sheet_view_search_move (GtkWidget   *window,
12053                            PsppSheetView *tree_view,
12054                            gboolean     up)
12055 {
12056   gboolean ret;
12057   gint len;
12058   gint count = 0;
12059   const gchar *text;
12060   GtkTreeIter iter;
12061   GtkTreeModel *model;
12062   PsppSheetSelection *selection;
12063
12064   text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
12065
12066   g_return_val_if_fail (text != NULL, FALSE);
12067
12068   len = strlen (text);
12069
12070   if (up && tree_view->priv->selected_iter == 1)
12071     return strlen (text) < 1;
12072
12073   len = strlen (text);
12074
12075   if (len < 1)
12076     return TRUE;
12077
12078   model = pspp_sheet_view_get_model (tree_view);
12079   selection = pspp_sheet_view_get_selection (tree_view);
12080
12081   /* search */
12082   pspp_sheet_selection_unselect_all (selection);
12083   if (!gtk_tree_model_get_iter_first (model, &iter))
12084     return TRUE;
12085
12086   ret = pspp_sheet_view_search_iter (model, selection, &iter, text,
12087                                    &count, up?((tree_view->priv->selected_iter) - 1):((tree_view->priv->selected_iter + 1)));
12088
12089   if (ret)
12090     {
12091       /* found */
12092       tree_view->priv->selected_iter += up?(-1):(1);
12093       return TRUE;
12094     }
12095   else
12096     {
12097       /* return to old iter */
12098       count = 0;
12099       gtk_tree_model_get_iter_first (model, &iter);
12100       pspp_sheet_view_search_iter (model, selection,
12101                                  &iter, text,
12102                                  &count, tree_view->priv->selected_iter);
12103       return FALSE;
12104     }
12105 }
12106
12107 static gboolean
12108 pspp_sheet_view_search_equal_func (GtkTreeModel *model,
12109                                  gint          column,
12110                                  const gchar  *key,
12111                                  GtkTreeIter  *iter,
12112                                  gpointer      search_data)
12113 {
12114   gboolean retval = TRUE;
12115   const gchar *str;
12116   gchar *normalized_string;
12117   gchar *normalized_key;
12118   gchar *case_normalized_string = NULL;
12119   gchar *case_normalized_key = NULL;
12120   GValue value = {0,};
12121   GValue transformed = {0,};
12122
12123   gtk_tree_model_get_value (model, iter, column, &value);
12124
12125   g_value_init (&transformed, G_TYPE_STRING);
12126
12127   if (!g_value_transform (&value, &transformed))
12128     {
12129       g_value_unset (&value);
12130       return TRUE;
12131     }
12132
12133   g_value_unset (&value);
12134
12135   str = g_value_get_string (&transformed);
12136   if (!str)
12137     {
12138       g_value_unset (&transformed);
12139       return TRUE;
12140     }
12141
12142   normalized_string = g_utf8_normalize (str, -1, G_NORMALIZE_ALL);
12143   normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
12144
12145   if (normalized_string && normalized_key)
12146     {
12147       case_normalized_string = g_utf8_casefold (normalized_string, -1);
12148       case_normalized_key = g_utf8_casefold (normalized_key, -1);
12149
12150       if (strncmp (case_normalized_key, case_normalized_string, strlen (case_normalized_key)) == 0)
12151         retval = FALSE;
12152     }
12153
12154   g_value_unset (&transformed);
12155   g_free (normalized_key);
12156   g_free (normalized_string);
12157   g_free (case_normalized_key);
12158   g_free (case_normalized_string);
12159
12160   return retval;
12161 }
12162
12163 static gboolean
12164 pspp_sheet_view_search_iter (GtkTreeModel     *model,
12165                              PsppSheetSelection *selection,
12166                              GtkTreeIter      *iter,
12167                              const gchar      *text,
12168                              gint             *count,
12169                              gint              n)
12170 {
12171   int node = -1;
12172   GtkTreePath *path;
12173
12174   PsppSheetView *tree_view = pspp_sheet_selection_get_tree_view (selection);
12175
12176   path = gtk_tree_model_get_path (model, iter);
12177   _pspp_sheet_view_find_node (tree_view, path, &node);
12178
12179   do
12180     {
12181       gboolean done = FALSE;
12182
12183       if (! tree_view->priv->search_equal_func (model, tree_view->priv->search_column, text, iter, tree_view->priv->search_user_data))
12184         {
12185           (*count)++;
12186           if (*count == n)
12187             {
12188               pspp_sheet_view_scroll_to_cell (tree_view, path, NULL,
12189                                               TRUE, 0.5, 0.0);
12190               pspp_sheet_selection_select_iter (selection, iter);
12191               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12192
12193               if (path)
12194                 gtk_tree_path_free (path);
12195
12196               return TRUE;
12197             }
12198         }
12199
12200
12201       do
12202         {
12203           node = pspp_sheet_view_node_next (tree_view, node);
12204
12205           if (node >= 0)
12206             {
12207               gboolean has_next;
12208
12209               has_next = gtk_tree_model_iter_next (model, iter);
12210
12211               done = TRUE;
12212               gtk_tree_path_next (path);
12213
12214               /* sanity check */
12215               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
12216             }
12217           else
12218             {
12219               if (path)
12220                 gtk_tree_path_free (path);
12221
12222               /* we've run out of tree, done with this func */
12223               return FALSE;
12224             }
12225         }
12226       while (!done);
12227     }
12228   while (1);
12229
12230   return FALSE;
12231 }
12232
12233 static void
12234 pspp_sheet_view_search_init (GtkWidget   *entry,
12235                            PsppSheetView *tree_view)
12236 {
12237   gint ret;
12238   gint count = 0;
12239   const gchar *text;
12240   GtkTreeIter iter;
12241   GtkTreeModel *model;
12242   PsppSheetSelection *selection;
12243
12244   g_return_if_fail (GTK_IS_ENTRY (entry));
12245   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12246
12247   text = gtk_entry_get_text (GTK_ENTRY (entry));
12248
12249   model = pspp_sheet_view_get_model (tree_view);
12250   selection = pspp_sheet_view_get_selection (tree_view);
12251
12252   /* search */
12253   pspp_sheet_selection_unselect_all (selection);
12254   if (tree_view->priv->typeselect_flush_timeout
12255       && !tree_view->priv->search_custom_entry_set)
12256     {
12257       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12258       tree_view->priv->typeselect_flush_timeout =
12259         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12260                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12261                        tree_view);
12262     }
12263
12264   if (*text == '\0')
12265     return;
12266
12267   if (!gtk_tree_model_get_iter_first (model, &iter))
12268     return;
12269
12270   ret = pspp_sheet_view_search_iter (model, selection,
12271                                    &iter, text,
12272                                    &count, 1);
12273
12274   if (ret)
12275     tree_view->priv->selected_iter = 1;
12276 }
12277
12278 static void
12279 pspp_sheet_view_remove_widget (GtkCellEditable *cell_editable,
12280                              PsppSheetView     *tree_view)
12281 {
12282   if (tree_view->priv->edited_column == NULL)
12283     return;
12284
12285   _pspp_sheet_view_column_stop_editing (tree_view->priv->edited_column);
12286   tree_view->priv->edited_column = NULL;
12287
12288   if (gtk_widget_has_focus (GTK_WIDGET (cell_editable)))
12289     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12290
12291   g_signal_handlers_disconnect_by_func (cell_editable,
12292                                         pspp_sheet_view_remove_widget,
12293                                         tree_view);
12294   g_signal_handlers_disconnect_by_func (cell_editable,
12295                                         pspp_sheet_view_editable_button_press_event,
12296                                         tree_view);
12297   g_signal_handlers_disconnect_by_func (cell_editable,
12298                                         pspp_sheet_view_editable_clicked,
12299                                         tree_view);
12300
12301   gtk_container_remove (GTK_CONTAINER (tree_view),
12302                         GTK_WIDGET (cell_editable));  
12303
12304   /* FIXME should only redraw a single node */
12305   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12306 }
12307
12308 static gboolean
12309 pspp_sheet_view_start_editing (PsppSheetView *tree_view,
12310                              GtkTreePath *cursor_path)
12311 {
12312   GtkTreeIter iter;
12313   GdkRectangle background_area;
12314   GdkRectangle cell_area;
12315   GtkCellEditable *editable_widget = NULL;
12316   gchar *path_string;
12317   guint flags = 0; /* can be 0, as the flags are primarily for rendering */
12318   gint retval = FALSE;
12319   int cursor_node;
12320
12321   g_assert (tree_view->priv->focus_column);
12322
12323   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
12324     return FALSE;
12325
12326   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
12327   if (cursor_node < 0)
12328     return FALSE;
12329
12330   path_string = gtk_tree_path_to_string (cursor_path);
12331   gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path);
12332
12333   pspp_sheet_view_column_cell_set_cell_data (tree_view->priv->focus_column,
12334                                            tree_view->priv->model,
12335                                            &iter);
12336   pspp_sheet_view_get_background_area (tree_view,
12337                                      cursor_path,
12338                                      tree_view->priv->focus_column,
12339                                      &background_area);
12340   pspp_sheet_view_get_cell_area (tree_view,
12341                                cursor_path,
12342                                tree_view->priv->focus_column,
12343                                &cell_area);
12344
12345   if (_pspp_sheet_view_column_cell_event (tree_view->priv->focus_column,
12346                                         &editable_widget,
12347                                         NULL,
12348                                         path_string,
12349                                         &background_area,
12350                                         &cell_area,
12351                                         flags))
12352     {
12353       retval = TRUE;
12354       if (editable_widget != NULL)
12355         {
12356           gint left, right;
12357           GdkRectangle area;
12358           GtkCellRenderer *cell;
12359
12360           area = cell_area;
12361           cell = _pspp_sheet_view_column_get_edited_cell (tree_view->priv->focus_column);
12362
12363           _pspp_sheet_view_column_get_neighbor_sizes (tree_view->priv->focus_column, cell, &left, &right);
12364
12365           area.x += left;
12366           area.width -= right + left;
12367
12368           pspp_sheet_view_real_start_editing (tree_view,
12369                                             tree_view->priv->focus_column,
12370                                             cursor_path,
12371                                             editable_widget,
12372                                             &area,
12373                                             NULL,
12374                                             flags);
12375         }
12376
12377     }
12378   g_free (path_string);
12379   return retval;
12380 }
12381
12382 static gboolean
12383 pspp_sheet_view_editable_button_press_event (GtkWidget *widget,
12384                                              GdkEventButton *event,
12385                                              PsppSheetView *sheet_view)
12386 {
12387   gint node;
12388
12389   node = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
12390                                              "pspp-sheet-view-node"));
12391   return pspp_sheet_view_row_head_clicked (sheet_view,
12392                                            node,
12393                                            sheet_view->priv->edited_column,
12394                                            event);
12395 }
12396
12397 static void
12398 pspp_sheet_view_editable_clicked (GtkButton *button,
12399                                   PsppSheetView *sheet_view)
12400 {
12401   pspp_sheet_view_editable_button_press_event (GTK_WIDGET (button), NULL,
12402                                                sheet_view);
12403 }
12404
12405 static gboolean
12406 is_all_selected (GtkWidget *widget)
12407 {
12408   GtkEntryBuffer *buffer;
12409   gint start_pos, end_pos;
12410
12411   if (!GTK_IS_ENTRY (widget))
12412     return FALSE;
12413
12414   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12415   return (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget),
12416                                              &start_pos, &end_pos)
12417           && start_pos == 0
12418           && end_pos == gtk_entry_buffer_get_length (buffer));
12419 }
12420
12421 static gboolean
12422 is_at_left (GtkWidget *widget)
12423 {
12424   return (GTK_IS_ENTRY (widget)
12425           && gtk_editable_get_position (GTK_EDITABLE (widget)) == 0);
12426 }
12427
12428 static gboolean
12429 is_at_right (GtkWidget *widget)
12430 {
12431   GtkEntryBuffer *buffer;
12432   gint length;
12433
12434   if (!GTK_IS_ENTRY (widget))
12435     return FALSE;
12436
12437   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12438   length = gtk_entry_buffer_get_length (buffer);
12439   return gtk_editable_get_position (GTK_EDITABLE (widget)) == length;
12440 }
12441
12442 static gboolean
12443 pspp_sheet_view_event (GtkWidget *widget,
12444                        GdkEventKey *event,
12445                        PsppSheetView *tree_view)
12446 {
12447   PsppSheetViewColumn *column;
12448   GtkTreePath *path;
12449   gboolean handled;
12450   gboolean cancel;
12451   guint keyval;
12452   gint row;
12453
12454   /* Intercept only key press events.
12455      It would make sense to use "key-press-event" instead of "event", but
12456      GtkEntry attaches its own signal handler to "key-press-event" that runs
12457      before ours and overrides our desired behavior for GDK_Up and GDK_Down.
12458   */
12459   if (event->type != GDK_KEY_PRESS)
12460     return FALSE;
12461
12462   keyval = event->keyval;
12463   cancel = FALSE;
12464   switch (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK))
12465     {
12466     case 0:
12467       switch (event->keyval)
12468         {
12469         case GDK_Left:      case GDK_KP_Left:
12470         case GDK_Home:      case GDK_KP_Home:
12471           if (!is_all_selected (widget) && !is_at_left (widget))
12472             return FALSE;
12473           break;
12474
12475         case GDK_Right:     case GDK_KP_Right:
12476         case GDK_End:       case GDK_KP_End:
12477           if (!is_all_selected (widget) && !is_at_right (widget))
12478             return FALSE;
12479           break;
12480
12481         case GDK_Up:        case GDK_KP_Up:
12482         case GDK_Down:      case GDK_KP_Down:
12483           break;
12484
12485         case GDK_Page_Up:   case GDK_KP_Page_Up:
12486         case GDK_Page_Down: case GDK_KP_Page_Down:
12487           break;
12488
12489         case GDK_Escape:
12490           cancel = TRUE;
12491           break;
12492
12493         case GDK_Return:
12494           keyval = GDK_Down;
12495           break;
12496
12497         case GDK_Tab:       case GDK_KP_Tab:
12498         case GDK_ISO_Left_Tab:
12499           keyval = GDK_Tab;
12500           break;
12501
12502         default:
12503           return FALSE;
12504         }
12505       break;
12506
12507     case GDK_SHIFT_MASK:
12508       switch (event->keyval)
12509         {
12510         case GDK_Tab:
12511         case GDK_ISO_Left_Tab:
12512           keyval = GDK_Tab;
12513           break;
12514
12515         default:
12516           return FALSE;
12517         }
12518       break;
12519
12520     case GDK_CONTROL_MASK:
12521       switch (event->keyval)
12522         {
12523         case GDK_Left:      case GDK_KP_Left:
12524           if (!is_all_selected (widget) && !is_at_left (widget))
12525             return FALSE;
12526           break;
12527
12528         case GDK_Right:     case GDK_KP_Right:
12529           if (!is_all_selected (widget) && !is_at_right (widget))
12530             return FALSE;
12531           break;
12532
12533         case GDK_Up:        case GDK_KP_Up:
12534         case GDK_Down:      case GDK_KP_Down:
12535           break;
12536
12537         default:
12538           return FALSE;
12539         }
12540       break;
12541
12542     default:
12543       return FALSE;
12544     }
12545
12546   row = tree_view->priv->edited_row;
12547   column = tree_view->priv->edited_column;
12548   path = gtk_tree_path_new_from_indices (row, -1);
12549
12550   pspp_sheet_view_stop_editing (tree_view, cancel);
12551   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12552
12553   pspp_sheet_view_set_cursor (tree_view, path, column, FALSE);
12554   gtk_tree_path_free (path);
12555
12556   handled = gtk_binding_set_activate (edit_bindings, keyval, event->state,
12557                                       G_OBJECT (tree_view));
12558   if (handled)
12559     g_signal_stop_emission_by_name (widget, "event");
12560
12561   pspp_sheet_view_get_cursor (tree_view, &path, NULL);
12562   pspp_sheet_view_start_editing (tree_view, path);
12563   gtk_tree_path_free (path);
12564
12565   return handled;
12566 }
12567
12568 static void
12569 pspp_sheet_view_override_cell_keypresses (GtkWidget *widget,
12570                                           gpointer data)
12571 {
12572   PsppSheetView *sheet_view = data;
12573
12574   g_signal_connect (widget, "event",
12575                     G_CALLBACK (pspp_sheet_view_event),
12576                     sheet_view);
12577
12578   if (GTK_IS_CONTAINER (widget))
12579     gtk_container_foreach (GTK_CONTAINER (widget),
12580                            pspp_sheet_view_override_cell_keypresses,
12581                            data);
12582 }
12583
12584 static void
12585 pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
12586                                   PsppSheetViewColumn *column,
12587                                   GtkTreePath       *path,
12588                                   GtkCellEditable   *cell_editable,
12589                                   GdkRectangle      *cell_area,
12590                                   GdkEvent          *event,
12591                                   guint              flags)
12592 {
12593   PsppSheetSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
12594   gint pre_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
12595   GtkRequisition requisition;
12596   gint row;
12597
12598   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
12599
12600   tree_view->priv->edited_column = column;
12601   _pspp_sheet_view_column_start_editing (column, GTK_CELL_EDITABLE (cell_editable));
12602
12603   row = gtk_tree_path_get_indices (path)[0];
12604   tree_view->priv->edited_row = row;
12605   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12606   cell_area->y += pre_val - (int)gtk_adjustment_get_value (tree_view->priv->vadjustment);
12607
12608   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
12609   pspp_sheet_selection_select_column (tree_view->priv->selection, column);
12610   tree_view->priv->anchor_column = column;
12611
12612   gtk_widget_size_request (GTK_WIDGET (cell_editable), &requisition);
12613
12614   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
12615
12616   if (requisition.height < cell_area->height)
12617     {
12618       gint diff = cell_area->height - requisition.height;
12619       pspp_sheet_view_put (tree_view,
12620                          GTK_WIDGET (cell_editable),
12621                          cell_area->x, cell_area->y + diff/2,
12622                          cell_area->width, requisition.height);
12623     }
12624   else
12625     {
12626       pspp_sheet_view_put (tree_view,
12627                          GTK_WIDGET (cell_editable),
12628                          cell_area->x, cell_area->y,
12629                          cell_area->width, cell_area->height);
12630     }
12631
12632   gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (cell_editable),
12633                                    (GdkEvent *)event);
12634
12635   gtk_widget_grab_focus (GTK_WIDGET (cell_editable));
12636   g_signal_connect (cell_editable, "remove-widget",
12637                     G_CALLBACK (pspp_sheet_view_remove_widget), tree_view);
12638   if (mode == PSPP_SHEET_SELECTION_RECTANGLE && column->row_head &&
12639       GTK_IS_BUTTON (cell_editable))
12640     {
12641       g_signal_connect (cell_editable, "button-press-event",
12642                         G_CALLBACK (pspp_sheet_view_editable_button_press_event),
12643                         tree_view);
12644       g_object_set_data (G_OBJECT (cell_editable), "pspp-sheet-view-node",
12645                          GINT_TO_POINTER (row));
12646       g_signal_connect (cell_editable, "clicked",
12647                         G_CALLBACK (pspp_sheet_view_editable_clicked),
12648                         tree_view);
12649     }
12650
12651   pspp_sheet_view_override_cell_keypresses (GTK_WIDGET (cell_editable),
12652                                             tree_view);
12653 }
12654
12655 void
12656 pspp_sheet_view_stop_editing (PsppSheetView *tree_view,
12657                               gboolean     cancel_editing)
12658 {
12659   PsppSheetViewColumn *column;
12660   GtkCellRenderer *cell;
12661
12662   if (tree_view->priv->edited_column == NULL)
12663     return;
12664
12665   /*
12666    * This is very evil. We need to do this, because
12667    * gtk_cell_editable_editing_done may trigger pspp_sheet_view_row_changed
12668    * later on. If pspp_sheet_view_row_changed notices
12669    * tree_view->priv->edited_column != NULL, it'll call
12670    * pspp_sheet_view_stop_editing again. Bad things will happen then.
12671    *
12672    * Please read that again if you intend to modify anything here.
12673    */
12674
12675   column = tree_view->priv->edited_column;
12676   tree_view->priv->edited_column = NULL;
12677
12678   cell = _pspp_sheet_view_column_get_edited_cell (column);
12679   gtk_cell_renderer_stop_editing (cell, cancel_editing);
12680
12681   if (!cancel_editing)
12682     gtk_cell_editable_editing_done (column->editable_widget);
12683
12684   tree_view->priv->edited_column = column;
12685
12686   gtk_cell_editable_remove_widget (column->editable_widget);
12687 }
12688
12689
12690 /**
12691  * pspp_sheet_view_set_hover_selection:
12692  * @tree_view: a #PsppSheetView
12693  * @hover: %TRUE to enable hover selection mode
12694  *
12695  * Enables of disables the hover selection mode of @tree_view.
12696  * Hover selection makes the selected row follow the pointer.
12697  * Currently, this works only for the selection modes 
12698  * %PSPP_SHEET_SELECTION_SINGLE and %PSPP_SHEET_SELECTION_BROWSE.
12699  * 
12700  * Since: 2.6
12701  **/
12702 void     
12703 pspp_sheet_view_set_hover_selection (PsppSheetView *tree_view,
12704                                    gboolean     hover)
12705 {
12706   hover = hover != FALSE;
12707
12708   if (hover != tree_view->priv->hover_selection)
12709     {
12710       tree_view->priv->hover_selection = hover;
12711
12712       g_object_notify (G_OBJECT (tree_view), "hover-selection");
12713     }
12714 }
12715
12716 /**
12717  * pspp_sheet_view_get_hover_selection:
12718  * @tree_view: a #PsppSheetView
12719  * 
12720  * Returns whether hover selection mode is turned on for @tree_view.
12721  * 
12722  * Return value: %TRUE if @tree_view is in hover selection mode
12723  *
12724  * Since: 2.6 
12725  **/
12726 gboolean 
12727 pspp_sheet_view_get_hover_selection (PsppSheetView *tree_view)
12728 {
12729   return tree_view->priv->hover_selection;
12730 }
12731
12732 /**
12733  * pspp_sheet_view_set_rubber_banding:
12734  * @tree_view: a #PsppSheetView
12735  * @enable: %TRUE to enable rubber banding
12736  *
12737  * Enables or disables rubber banding in @tree_view.  If the selection mode is
12738  * #PSPP_SHEET_SELECTION_MULTIPLE or #PSPP_SHEET_SELECTION_RECTANGLE, rubber
12739  * banding will allow the user to select multiple rows by dragging the mouse.
12740  * 
12741  * Since: 2.10
12742  **/
12743 void
12744 pspp_sheet_view_set_rubber_banding (PsppSheetView *tree_view,
12745                                   gboolean     enable)
12746 {
12747   enable = enable != FALSE;
12748
12749   if (enable != tree_view->priv->rubber_banding_enable)
12750     {
12751       tree_view->priv->rubber_banding_enable = enable;
12752
12753       g_object_notify (G_OBJECT (tree_view), "rubber-banding");
12754     }
12755 }
12756
12757 /**
12758  * pspp_sheet_view_get_rubber_banding:
12759  * @tree_view: a #PsppSheetView
12760  * 
12761  * Returns whether rubber banding is turned on for @tree_view.  If the
12762  * selection mode is #PSPP_SHEET_SELECTION_MULTIPLE or
12763  * #PSPP_SHEET_SELECTION_RECTANGLE, rubber banding will allow the user to
12764  * select multiple rows by dragging the mouse.
12765  * 
12766  * Return value: %TRUE if rubber banding in @tree_view is enabled.
12767  *
12768  * Since: 2.10
12769  **/
12770 gboolean
12771 pspp_sheet_view_get_rubber_banding (PsppSheetView *tree_view)
12772 {
12773   return tree_view->priv->rubber_banding_enable;
12774 }
12775
12776 /**
12777  * pspp_sheet_view_is_rubber_banding_active:
12778  * @tree_view: a #PsppSheetView
12779  * 
12780  * Returns whether a rubber banding operation is currently being done
12781  * in @tree_view.
12782  *
12783  * Return value: %TRUE if a rubber banding operation is currently being
12784  * done in @tree_view.
12785  *
12786  * Since: 2.12
12787  **/
12788 gboolean
12789 pspp_sheet_view_is_rubber_banding_active (PsppSheetView *tree_view)
12790 {
12791   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12792
12793   if (tree_view->priv->rubber_banding_enable
12794       && tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
12795     return TRUE;
12796
12797   return FALSE;
12798 }
12799
12800 static void
12801 pspp_sheet_view_grab_notify (GtkWidget *widget,
12802                            gboolean   was_grabbed)
12803 {
12804   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12805
12806   tree_view->priv->in_grab = !was_grabbed;
12807
12808   if (!was_grabbed)
12809     {
12810       tree_view->priv->pressed_button = -1;
12811
12812       if (tree_view->priv->rubber_band_status)
12813         pspp_sheet_view_stop_rubber_band (tree_view);
12814     }
12815 }
12816
12817 static void
12818 pspp_sheet_view_state_changed (GtkWidget      *widget,
12819                              GtkStateType    previous_state)
12820 {
12821   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12822
12823   if (gtk_widget_get_realized (widget))
12824     {
12825       GtkStyle *style = gtk_widget_get_style (widget);
12826       gdk_window_set_background (tree_view->priv->bin_window, &style->base[gtk_widget_get_state (widget)]);
12827     }
12828
12829   gtk_widget_queue_draw (widget);
12830 }
12831
12832 /**
12833  * pspp_sheet_view_get_grid_lines:
12834  * @tree_view: a #PsppSheetView
12835  *
12836  * Returns which grid lines are enabled in @tree_view.
12837  *
12838  * Return value: a #PsppSheetViewGridLines value indicating which grid lines
12839  * are enabled.
12840  *
12841  * Since: 2.10
12842  */
12843 PsppSheetViewGridLines
12844 pspp_sheet_view_get_grid_lines (PsppSheetView *tree_view)
12845 {
12846   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12847
12848   return tree_view->priv->grid_lines;
12849 }
12850
12851 /**
12852  * pspp_sheet_view_set_grid_lines:
12853  * @tree_view: a #PsppSheetView
12854  * @grid_lines: a #PsppSheetViewGridLines value indicating which grid lines to
12855  * enable.
12856  *
12857  * Sets which grid lines to draw in @tree_view.
12858  *
12859  * Since: 2.10
12860  */
12861 void
12862 pspp_sheet_view_set_grid_lines (PsppSheetView           *tree_view,
12863                               PsppSheetViewGridLines   grid_lines)
12864 {
12865   PsppSheetViewPrivate *priv;
12866   PsppSheetViewGridLines old_grid_lines;
12867
12868   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12869
12870   priv = tree_view->priv;
12871
12872   old_grid_lines = priv->grid_lines;
12873   priv->grid_lines = grid_lines;
12874   
12875   if (old_grid_lines != grid_lines)
12876     {
12877       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12878       
12879       g_object_notify (G_OBJECT (tree_view), "enable-grid-lines");
12880     }
12881 }
12882
12883 /**
12884  * pspp_sheet_view_get_special_cells:
12885  * @tree_view: a #PsppSheetView
12886  *
12887  * Returns which grid lines are enabled in @tree_view.
12888  *
12889  * Return value: a #PsppSheetViewSpecialCells value indicating whether rows in
12890  * the sheet view contain special cells.
12891  */
12892 PsppSheetViewSpecialCells
12893 pspp_sheet_view_get_special_cells (PsppSheetView *tree_view)
12894 {
12895   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12896
12897   return tree_view->priv->special_cells;
12898 }
12899
12900 /**
12901  * pspp_sheet_view_set_special_cells:
12902  * @tree_view: a #PsppSheetView
12903  * @special_cells: a #PsppSheetViewSpecialCells value indicating whether rows in
12904  * the sheet view contain special cells.
12905  *
12906  * Sets whether rows in the sheet view contain special cells, controlling the
12907  * rendering of row selections.
12908  */
12909 void
12910 pspp_sheet_view_set_special_cells (PsppSheetView           *tree_view,
12911                               PsppSheetViewSpecialCells   special_cells)
12912 {
12913   PsppSheetViewPrivate *priv;
12914
12915   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12916
12917   priv = tree_view->priv;
12918
12919   if (priv->special_cells != special_cells)
12920     {
12921       priv->special_cells = special_cells;
12922       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12923       g_object_notify (G_OBJECT (tree_view), "special-cells");
12924     }
12925 }
12926
12927 int
12928 pspp_sheet_view_get_fixed_height (const PsppSheetView *tree_view)
12929 {
12930   /* XXX (re)calculate fixed_height if necessary */
12931   return tree_view->priv->fixed_height;
12932 }
12933
12934 void
12935 pspp_sheet_view_set_fixed_height (PsppSheetView *tree_view,
12936                                   int fixed_height)
12937 {
12938   g_return_if_fail (fixed_height > 0);
12939
12940   if (tree_view->priv->fixed_height != fixed_height)
12941     {
12942       tree_view->priv->fixed_height = fixed_height;
12943       g_object_notify (G_OBJECT (tree_view), "fixed-height");
12944     }
12945   if (!tree_view->priv->fixed_height_set)
12946     {
12947       tree_view->priv->fixed_height_set = TRUE;
12948       g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
12949     }
12950 }
12951
12952 /**
12953  * pspp_sheet_view_set_tooltip_row:
12954  * @tree_view: a #PsppSheetView
12955  * @tooltip: a #GtkTooltip
12956  * @path: a #GtkTreePath
12957  *
12958  * Sets the tip area of @tooltip to be the area covered by the row at @path.
12959  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
12960  * See also gtk_tooltip_set_tip_area().
12961  *
12962  * Since: 2.12
12963  */
12964 void
12965 pspp_sheet_view_set_tooltip_row (PsppSheetView *tree_view,
12966                                GtkTooltip  *tooltip,
12967                                GtkTreePath *path)
12968 {
12969   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12970   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
12971
12972   pspp_sheet_view_set_tooltip_cell (tree_view, tooltip, path, NULL, NULL);
12973 }
12974
12975 /**
12976  * pspp_sheet_view_set_tooltip_cell:
12977  * @tree_view: a #PsppSheetView
12978  * @tooltip: a #GtkTooltip
12979  * @path: (allow-none): a #GtkTreePath or %NULL
12980  * @column: (allow-none): a #PsppSheetViewColumn or %NULL
12981  * @cell: (allow-none): a #GtkCellRenderer or %NULL
12982  *
12983  * Sets the tip area of @tooltip to the area @path, @column and @cell have
12984  * in common.  For example if @path is %NULL and @column is set, the tip
12985  * area will be set to the full area covered by @column.  See also
12986  * gtk_tooltip_set_tip_area().
12987  *
12988  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
12989  *
12990  * Since: 2.12
12991  */
12992 void
12993 pspp_sheet_view_set_tooltip_cell (PsppSheetView       *tree_view,
12994                                 GtkTooltip        *tooltip,
12995                                 GtkTreePath       *path,
12996                                 PsppSheetViewColumn *column,
12997                                 GtkCellRenderer   *cell)
12998 {
12999   GdkRectangle rect;
13000
13001   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13002   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
13003   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
13004   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
13005
13006   /* Determine x values. */
13007   if (column && cell)
13008     {
13009       GdkRectangle tmp;
13010       gint start, width;
13011
13012       pspp_sheet_view_get_cell_area (tree_view, path, column, &tmp);
13013       pspp_sheet_view_column_cell_get_position (column, cell, &start, &width);
13014
13015       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13016                                                          tmp.x + start, 0,
13017                                                          &rect.x, NULL);
13018       rect.width = width;
13019     }
13020   else if (column)
13021     {
13022       GdkRectangle tmp;
13023
13024       pspp_sheet_view_get_background_area (tree_view, NULL, column, &tmp);
13025       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13026                                                          tmp.x, 0,
13027                                                          &rect.x, NULL);
13028       rect.width = tmp.width;
13029     }
13030   else
13031     {
13032       GtkAllocation allocation;
13033       gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
13034       rect.x = 0;
13035       rect.width = allocation.width;
13036     }
13037
13038   /* Determine y values. */
13039   if (path)
13040     {
13041       GdkRectangle tmp;
13042
13043       pspp_sheet_view_get_background_area (tree_view, path, NULL, &tmp);
13044       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13045                                                          0, tmp.y,
13046                                                          NULL, &rect.y);
13047       rect.height = tmp.height;
13048     }
13049   else
13050     {
13051       rect.y = 0;
13052       rect.height = gtk_adjustment_get_page_size (tree_view->priv->vadjustment);
13053     }
13054
13055   gtk_tooltip_set_tip_area (tooltip, &rect);
13056 }
13057
13058 /**
13059  * pspp_sheet_view_get_tooltip_context:
13060  * @tree_view: a #PsppSheetView
13061  * @x: the x coordinate (relative to widget coordinates)
13062  * @y: the y coordinate (relative to widget coordinates)
13063  * @keyboard_tip: whether this is a keyboard tooltip or not
13064  * @model: (allow-none): a pointer to receive a #GtkTreeModel or %NULL
13065  * @path: (allow-none): a pointer to receive a #GtkTreePath or %NULL
13066  * @iter: (allow-none): a pointer to receive a #GtkTreeIter or %NULL
13067  *
13068  * This function is supposed to be used in a #GtkWidget::query-tooltip
13069  * signal handler for #PsppSheetView.  The @x, @y and @keyboard_tip values
13070  * which are received in the signal handler, should be passed to this
13071  * function without modification.
13072  *
13073  * The return value indicates whether there is a tree view row at the given
13074  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips.  For keyboard
13075  * tooltips the row returned will be the cursor row.  When %TRUE, then any of
13076  * @model, @path and @iter which have been provided will be set to point to
13077  * that row and the corresponding model.  @x and @y will always be converted
13078  * to be relative to @tree_view's bin_window if @keyboard_tooltip is %FALSE.
13079  *
13080  * Return value: whether or not the given tooltip context points to a row.
13081  *
13082  * Since: 2.12
13083  */
13084 gboolean
13085 pspp_sheet_view_get_tooltip_context (PsppSheetView   *tree_view,
13086                                    gint          *x,
13087                                    gint          *y,
13088                                    gboolean       keyboard_tip,
13089                                    GtkTreeModel **model,
13090                                    GtkTreePath  **path,
13091                                    GtkTreeIter   *iter)
13092 {
13093   GtkTreePath *tmppath = NULL;
13094
13095   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
13096   g_return_val_if_fail (x != NULL, FALSE);
13097   g_return_val_if_fail (y != NULL, FALSE);
13098
13099   if (keyboard_tip)
13100     {
13101       pspp_sheet_view_get_cursor (tree_view, &tmppath, NULL);
13102
13103       if (!tmppath)
13104         return FALSE;
13105     }
13106   else
13107     {
13108       pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, *x, *y,
13109                                                          x, y);
13110
13111       if (!pspp_sheet_view_get_path_at_pos (tree_view, *x, *y,
13112                                           &tmppath, NULL, NULL, NULL))
13113         return FALSE;
13114     }
13115
13116   if (model)
13117     *model = pspp_sheet_view_get_model (tree_view);
13118
13119   if (iter)
13120     gtk_tree_model_get_iter (pspp_sheet_view_get_model (tree_view),
13121                              iter, tmppath);
13122
13123   if (path)
13124     *path = tmppath;
13125   else
13126     gtk_tree_path_free (tmppath);
13127
13128   return TRUE;
13129 }
13130
13131 static gboolean
13132 pspp_sheet_view_set_tooltip_query_cb (GtkWidget  *widget,
13133                                     gint        x,
13134                                     gint        y,
13135                                     gboolean    keyboard_tip,
13136                                     GtkTooltip *tooltip,
13137                                     gpointer    data)
13138 {
13139   GValue value = { 0, };
13140   GValue transformed = { 0, };
13141   GtkTreeIter iter;
13142   GtkTreePath *path;
13143   GtkTreeModel *model;
13144   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
13145
13146   if (!pspp_sheet_view_get_tooltip_context (PSPP_SHEET_VIEW (widget),
13147                                           &x, &y,
13148                                           keyboard_tip,
13149                                           &model, &path, &iter))
13150     return FALSE;
13151
13152   gtk_tree_model_get_value (model, &iter,
13153                             tree_view->priv->tooltip_column, &value);
13154
13155   g_value_init (&transformed, G_TYPE_STRING);
13156
13157   if (!g_value_transform (&value, &transformed))
13158     {
13159       g_value_unset (&value);
13160       gtk_tree_path_free (path);
13161
13162       return FALSE;
13163     }
13164
13165   g_value_unset (&value);
13166
13167   if (!g_value_get_string (&transformed))
13168     {
13169       g_value_unset (&transformed);
13170       gtk_tree_path_free (path);
13171
13172       return FALSE;
13173     }
13174
13175   gtk_tooltip_set_markup (tooltip, g_value_get_string (&transformed));
13176   pspp_sheet_view_set_tooltip_row (tree_view, tooltip, path);
13177
13178   gtk_tree_path_free (path);
13179   g_value_unset (&transformed);
13180
13181   return TRUE;
13182 }
13183
13184 /**
13185  * pspp_sheet_view_set_tooltip_column:
13186  * @tree_view: a #PsppSheetView
13187  * @column: an integer, which is a valid column number for @tree_view's model
13188  *
13189  * If you only plan to have simple (text-only) tooltips on full rows, you
13190  * can use this function to have #PsppSheetView handle these automatically
13191  * for you. @column should be set to the column in @tree_view's model
13192  * containing the tooltip texts, or -1 to disable this feature.
13193  *
13194  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
13195  * @tree_view will connect a #GtkWidget::query-tooltip signal handler.
13196  *
13197  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
13198  * so &amp;, &lt;, etc have to be escaped in the text.
13199  *
13200  * Since: 2.12
13201  */
13202 void
13203 pspp_sheet_view_set_tooltip_column (PsppSheetView *tree_view,
13204                                   gint         column)
13205 {
13206   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13207
13208   if (column == tree_view->priv->tooltip_column)
13209     return;
13210
13211   if (column == -1)
13212     {
13213       g_signal_handlers_disconnect_by_func (tree_view,
13214                                             pspp_sheet_view_set_tooltip_query_cb,
13215                                             NULL);
13216       gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), FALSE);
13217     }
13218   else
13219     {
13220       if (tree_view->priv->tooltip_column == -1)
13221         {
13222           g_signal_connect (tree_view, "query-tooltip",
13223                             G_CALLBACK (pspp_sheet_view_set_tooltip_query_cb), NULL);
13224           gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), TRUE);
13225         }
13226     }
13227
13228   tree_view->priv->tooltip_column = column;
13229   g_object_notify (G_OBJECT (tree_view), "tooltip-column");
13230 }
13231
13232 /**
13233  * pspp_sheet_view_get_tooltip_column:
13234  * @tree_view: a #PsppSheetView
13235  *
13236  * Returns the column of @tree_view's model which is being used for
13237  * displaying tooltips on @tree_view's rows.
13238  *
13239  * Return value: the index of the tooltip column that is currently being
13240  * used, or -1 if this is disabled.
13241  *
13242  * Since: 2.12
13243  */
13244 gint
13245 pspp_sheet_view_get_tooltip_column (PsppSheetView *tree_view)
13246 {
13247   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
13248
13249   return tree_view->priv->tooltip_column;
13250 }
13251
13252 gboolean
13253 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
13254                                   GValue                *return_accu,
13255                                   const GValue          *handler_return,
13256                                   gpointer               dummy)
13257 {
13258   gboolean continue_emission;
13259   gboolean signal_handled;
13260   
13261   signal_handled = g_value_get_boolean (handler_return);
13262   g_value_set_boolean (return_accu, signal_handled);
13263   continue_emission = !signal_handled;
13264   
13265   return continue_emission;
13266 }
13267
13268
13269 GType
13270 pspp_sheet_view_grid_lines_get_type (void)
13271 {
13272     static GType etype = 0;
13273     if (G_UNLIKELY(etype == 0)) {
13274         static const GEnumValue values[] = {
13275             { PSPP_SHEET_VIEW_GRID_LINES_NONE, "PSPP_SHEET_VIEW_GRID_LINES_NONE", "none" },
13276             { PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL, "PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL", "horizontal" },
13277             { PSPP_SHEET_VIEW_GRID_LINES_VERTICAL, "PSPP_SHEET_VIEW_GRID_LINES_VERTICAL", "vertical" },
13278             { PSPP_SHEET_VIEW_GRID_LINES_BOTH, "PSPP_SHEET_VIEW_GRID_LINES_BOTH", "both" },
13279             { 0, NULL, NULL }
13280         };
13281         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewGridLines"), values);
13282     }
13283     return etype;
13284 }
13285
13286 GType
13287 pspp_sheet_view_special_cells_get_type (void)
13288 {
13289     static GType etype = 0;
13290     if (G_UNLIKELY(etype == 0)) {
13291         static const GEnumValue values[] = {
13292             { PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT, "PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT", "detect" },
13293             { PSPP_SHEET_VIEW_SPECIAL_CELLS_YES, "PSPP_SHEET_VIEW_SPECIAL_CELLS_YES", "yes" },
13294             { PSPP_SHEET_VIEW_SPECIAL_CELLS_NO, "PSPP_SHEET_VIEW_SPECIAL_CELLS_NO", "no" },
13295             { 0, NULL, NULL }
13296         };
13297         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewSpecialCells"), values);
13298     }
13299     return etype;
13300 }