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