PsppSheetView: Convert expose callback to draw callback.
[pspp] / src / ui / gui / pspp-sheet-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 /* gtktreeview.c
18  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Library General Public
22  * License as published by the Free Software Foundation; either
23  * version 2 of the License, or (at your option) any later version.
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License along with this library; if not, write to the
32  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33  * Boston, MA 02111-1307, USA.
34  */
35
36 #include <config.h>
37
38 #include "ui/gui/pspp-sheet-private.h"
39
40 #include <gtk/gtk.h>
41 #include <gdk/gdk.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <gdk/gdkkeysyms-compat.h>
44 #include <string.h>
45
46 #include "ui/gui/psppire-marshal.h"
47 #include "ui/gui/pspp-sheet-selection.h"
48
49 #define P_(STRING) STRING
50 #define GTK_PARAM_READABLE G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
51 #define GTK_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
52
53 /* Many keyboard shortcuts for Mac are the same as for X
54  * except they use Command key instead of Control (e.g. Cut,
55  * Copy, Paste). This symbol is for those simple cases. */
56 #ifndef GDK_WINDOWING_QUARTZ
57 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_CONTROL_MASK
58 #else
59 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_META_MASK
60 #endif
61
62 #define PSPP_SHEET_VIEW_PRIORITY_VALIDATE (GDK_PRIORITY_REDRAW + 5)
63 #define PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC (PSPP_SHEET_VIEW_PRIORITY_VALIDATE + 2)
64 #define PSPP_SHEET_VIEW_TIME_MS_PER_IDLE 30
65 #define SCROLL_EDGE_SIZE 15
66 #define EXPANDER_EXTRA_PADDING 4
67 #define PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT 5000
68
69 /* The "background" areas of all rows/cells add up to cover the entire tree.
70  * The background includes all inter-row and inter-cell spacing.
71  * The "cell" areas are the cell_area passed in to gtk_cell_renderer_render(),
72  * i.e. just the cells, no spacing.
73  */
74
75 #define BACKGROUND_HEIGHT(tree_view) (tree_view->priv->fixed_height)
76 #define CELL_HEIGHT(tree_view, separator) ((BACKGROUND_HEIGHT (tree_view)) - (separator))
77
78 /* Translate from bin_window coordinates to rbtree (tree coordinates) and
79  * vice versa.
80  */
81 #define TREE_WINDOW_Y_TO_RBTREE_Y(tree_view,y) ((y) + tree_view->priv->dy)
82 #define RBTREE_Y_TO_TREE_WINDOW_Y(tree_view,y) ((y) - tree_view->priv->dy)
83
84 /* This is in bin_window coordinates */
85 #define BACKGROUND_FIRST_PIXEL(tree_view,node) (RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, pspp_sheet_view_node_find_offset (tree_view, (node))))
86 #define CELL_FIRST_PIXEL(tree_view,node,separator) (BACKGROUND_FIRST_PIXEL (tree_view,node) + separator/2)
87
88 #define ROW_HEIGHT(tree_view) \
89   ((tree_view->priv->fixed_height > 0) ? (tree_view->priv->fixed_height) : (tree_view)->priv->expander_size)
90
91
92 typedef struct _PsppSheetViewChild PsppSheetViewChild;
93 struct _PsppSheetViewChild
94 {
95   GtkWidget *widget;
96   gint x;
97   gint y;
98   gint width;
99   gint height;
100 };
101
102
103 typedef struct _TreeViewDragInfo TreeViewDragInfo;
104 struct _TreeViewDragInfo
105 {
106   GdkModifierType start_button_mask;
107   GtkTargetList *_unused_source_target_list;
108   GdkDragAction source_actions;
109
110   GtkTargetList *_unused_dest_target_list;
111
112   guint source_set : 1;
113   guint dest_set : 1;
114 };
115
116
117 /* Signals */
118 enum
119 {
120   ROW_ACTIVATED,
121   COLUMNS_CHANGED,
122   CURSOR_CHANGED,
123   MOVE_CURSOR,
124   SELECT_ALL,
125   UNSELECT_ALL,
126   SELECT_CURSOR_ROW,
127   TOGGLE_CURSOR_ROW,
128   START_INTERACTIVE_SEARCH,
129   LAST_SIGNAL
130 };
131
132 /* Properties */
133 enum {
134   PROP_0,
135   PROP_MODEL,
136   PROP_HADJUSTMENT,
137   PROP_VADJUSTMENT,
138   PROP_HEADERS_VISIBLE,
139   PROP_HEADERS_CLICKABLE,
140   PROP_REORDERABLE,
141   PROP_RULES_HINT,
142   PROP_ENABLE_SEARCH,
143   PROP_SEARCH_COLUMN,
144   PROP_HOVER_SELECTION,
145   PROP_RUBBER_BANDING,
146   PROP_ENABLE_GRID_LINES,
147   PROP_TOOLTIP_COLUMN,
148   PROP_SPECIAL_CELLS,
149   PROP_FIXED_HEIGHT,
150   PROP_FIXED_HEIGHT_SET
151 };
152
153 /* object signals */
154 static void     pspp_sheet_view_finalize             (GObject          *object);
155 static void     pspp_sheet_view_set_property         (GObject         *object,
156                                                     guint            prop_id,
157                                                     const GValue    *value,
158                                                     GParamSpec      *pspec);
159 static void     pspp_sheet_view_get_property         (GObject         *object,
160                                                     guint            prop_id,
161                                                     GValue          *value,
162                                                     GParamSpec      *pspec);
163
164 static void     pspp_sheet_view_dispose              (GObject        *object);
165
166 /* gtkwidget signals */
167 static void     pspp_sheet_view_realize              (GtkWidget        *widget);
168 static void     pspp_sheet_view_unrealize            (GtkWidget        *widget);
169 static void     pspp_sheet_view_map                  (GtkWidget        *widget);
170 static void     pspp_sheet_view_size_request         (GtkWidget        *widget,
171                                                     GtkRequisition   *requisition);
172 static void     pspp_sheet_view_size_allocate        (GtkWidget        *widget,
173                                                     GtkAllocation    *allocation);
174 static gboolean pspp_sheet_view_draw               (GtkWidget        *widget,
175                                                     cairo_t *cr);
176 static gboolean pspp_sheet_view_key_press            (GtkWidget        *widget,
177                                                     GdkEventKey      *event);
178 static gboolean pspp_sheet_view_key_release          (GtkWidget        *widget,
179                                                     GdkEventKey      *event);
180 static gboolean pspp_sheet_view_motion               (GtkWidget        *widget,
181                                                     GdkEventMotion   *event);
182 static gboolean pspp_sheet_view_enter_notify         (GtkWidget        *widget,
183                                                     GdkEventCrossing *event);
184 static gboolean pspp_sheet_view_leave_notify         (GtkWidget        *widget,
185                                                     GdkEventCrossing *event);
186 static gboolean pspp_sheet_view_button_press         (GtkWidget        *widget,
187                                                     GdkEventButton   *event);
188 static gboolean pspp_sheet_view_button_release       (GtkWidget        *widget,
189                                                     GdkEventButton   *event);
190 static gboolean pspp_sheet_view_grab_broken          (GtkWidget          *widget,
191                                                     GdkEventGrabBroken *event);
192
193 static void     pspp_sheet_view_set_focus_child      (GtkContainer     *container,
194                                                     GtkWidget        *child);
195 static gint     pspp_sheet_view_focus_out            (GtkWidget        *widget,
196                                                     GdkEventFocus    *event);
197 static gint     pspp_sheet_view_focus                (GtkWidget        *widget,
198                                                     GtkDirectionType  direction);
199 static void     pspp_sheet_view_grab_focus           (GtkWidget        *widget);
200 static void     pspp_sheet_view_style_set            (GtkWidget        *widget,
201                                                     GtkStyle         *previous_style);
202 static void     pspp_sheet_view_grab_notify          (GtkWidget        *widget,
203                                                     gboolean          was_grabbed);
204 static void     pspp_sheet_view_state_changed        (GtkWidget        *widget,
205                                                     GtkStateType      previous_state);
206
207 /* container signals */
208 static void     pspp_sheet_view_remove               (GtkContainer     *container,
209                                                     GtkWidget        *widget);
210 static void     pspp_sheet_view_forall               (GtkContainer     *container,
211                                                     gboolean          include_internals,
212                                                     GtkCallback       callback,
213                                                     gpointer          callback_data);
214
215 /* Source side drag signals */
216 static void pspp_sheet_view_drag_begin       (GtkWidget        *widget,
217                                             GdkDragContext   *context);
218 static void pspp_sheet_view_drag_end         (GtkWidget        *widget,
219                                             GdkDragContext   *context);
220 static void pspp_sheet_view_drag_data_get    (GtkWidget        *widget,
221                                             GdkDragContext   *context,
222                                             GtkSelectionData *selection_data,
223                                             guint             info,
224                                             guint             time);
225 static void pspp_sheet_view_drag_data_delete (GtkWidget        *widget,
226                                             GdkDragContext   *context);
227
228 /* Target side drag signals */
229 static void     pspp_sheet_view_drag_leave         (GtkWidget        *widget,
230                                                   GdkDragContext   *context,
231                                                   guint             time);
232 static gboolean pspp_sheet_view_drag_motion        (GtkWidget        *widget,
233                                                   GdkDragContext   *context,
234                                                   gint              x,
235                                                   gint              y,
236                                                   guint             time);
237 static gboolean pspp_sheet_view_drag_drop          (GtkWidget        *widget,
238                                                   GdkDragContext   *context,
239                                                   gint              x,
240                                                   gint              y,
241                                                   guint             time);
242 static void     pspp_sheet_view_drag_data_received (GtkWidget        *widget,
243                                                   GdkDragContext   *context,
244                                                   gint              x,
245                                                   gint              y,
246                                                   GtkSelectionData *selection_data,
247                                                   guint             info,
248                                                   guint             time);
249
250 /* tree_model signals */
251 static void pspp_sheet_view_set_adjustments                 (PsppSheetView     *tree_view,
252                                                            GtkAdjustment   *hadj,
253                                                            GtkAdjustment   *vadj);
254 static gboolean pspp_sheet_view_real_move_cursor            (PsppSheetView     *tree_view,
255                                                            GtkMovementStep  step,
256                                                            gint             count);
257 static gboolean pspp_sheet_view_real_select_all             (PsppSheetView     *tree_view);
258 static gboolean pspp_sheet_view_real_unselect_all           (PsppSheetView     *tree_view);
259 static gboolean pspp_sheet_view_real_select_cursor_row      (PsppSheetView     *tree_view,
260                                                              gboolean         start_editing,
261                                                              PsppSheetSelectMode mode);
262 static gboolean pspp_sheet_view_real_toggle_cursor_row      (PsppSheetView     *tree_view);
263 static void pspp_sheet_view_row_changed                     (GtkTreeModel    *model,
264                                                            GtkTreePath     *path,
265                                                            GtkTreeIter     *iter,
266                                                            gpointer         data);
267 static void pspp_sheet_view_row_inserted                    (GtkTreeModel    *model,
268                                                            GtkTreePath     *path,
269                                                            GtkTreeIter     *iter,
270                                                            gpointer         data);
271 static void pspp_sheet_view_row_deleted                     (GtkTreeModel    *model,
272                                                            GtkTreePath     *path,
273                                                            gpointer         data);
274 static void pspp_sheet_view_rows_reordered                  (GtkTreeModel    *model,
275                                                            GtkTreePath     *parent,
276                                                            GtkTreeIter     *iter,
277                                                            gint            *new_order,
278                                                            gpointer         data);
279
280 /* Incremental reflow */
281 static gint validate_row             (PsppSheetView *tree_view,
282                                           int node,
283                                           GtkTreeIter *iter,
284                                           GtkTreePath *path);
285 static void     validate_visible_area    (PsppSheetView *tree_view);
286 static gboolean validate_rows_handler    (PsppSheetView *tree_view);
287 static gboolean presize_handler_callback (gpointer     data);
288 static void     install_presize_handler  (PsppSheetView *tree_view);
289 static void     install_scroll_sync_handler (PsppSheetView *tree_view);
290 static void     pspp_sheet_view_set_top_row   (PsppSheetView *tree_view,
291                                              GtkTreePath *path,
292                                              gint         offset);
293 static void     pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view);
294 static void     pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view);
295 static void     invalidate_empty_focus      (PsppSheetView *tree_view);
296
297 /* Internal functions */
298 static void     pspp_sheet_view_add_move_binding               (GtkBindingSet      *binding_set,
299                                                               guint               keyval,
300                                                               guint               modmask,
301                                                               gboolean            add_shifted_binding,
302                                                               GtkMovementStep     step,
303                                                               gint                count);
304 static void     pspp_sheet_view_queue_draw_path                (PsppSheetView        *tree_view,
305                                                               GtkTreePath        *path,
306                                                               const GdkRectangle *clip_rect);
307 static gint     pspp_sheet_view_new_column_width               (PsppSheetView        *tree_view,
308                                                               gint                i,
309                                                               gint               *x);
310 static void     pspp_sheet_view_adjustment_changed             (GtkAdjustment      *adjustment,
311                                                               PsppSheetView        *tree_view);
312 static void     pspp_sheet_view_clamp_node_visible             (PsppSheetView        *tree_view,
313                                                               int node);
314 static void     pspp_sheet_view_clamp_column_visible           (PsppSheetView        *tree_view,
315                                                               PsppSheetViewColumn  *column,
316                                                               gboolean            focus_to_cell);
317 static gboolean pspp_sheet_view_maybe_begin_dragging_row       (PsppSheetView        *tree_view,
318                                                               GdkEventMotion     *event);
319 static void     pspp_sheet_view_focus_to_cursor                (PsppSheetView        *tree_view);
320 static gboolean pspp_sheet_view_move_cursor_up_down            (PsppSheetView        *tree_view,
321                                                               gint                count,
322                                                                 PsppSheetSelectMode mode);
323 static void     pspp_sheet_view_move_cursor_page_up_down       (PsppSheetView        *tree_view,
324                                                               gint                count,
325                                                                 PsppSheetSelectMode mode);
326 static void     pspp_sheet_view_move_cursor_left_right         (PsppSheetView        *tree_view,
327                                                                 gint                count,
328                                                                 PsppSheetSelectMode mode);
329 static void     pspp_sheet_view_move_cursor_line_start_end     (PsppSheetView        *tree_view,
330                                                                 gint                count,
331                                                                 PsppSheetSelectMode mode);
332 static void     pspp_sheet_view_move_cursor_tab                (PsppSheetView        *tree_view,
333                                                               gint                count);
334 static void     pspp_sheet_view_move_cursor_start_end          (PsppSheetView        *tree_view,
335                                                               gint                count,
336                                                                 PsppSheetSelectMode mode);
337 static void     pspp_sheet_view_real_set_cursor                (PsppSheetView        *tree_view,
338                                                               GtkTreePath        *path,
339                                                               gboolean            clear_and_select,
340                                                               gboolean            clamp_node,
341                                                               PsppSheetSelectMode mode);
342 static gboolean pspp_sheet_view_has_special_cell               (PsppSheetView        *tree_view);
343 static void     pspp_sheet_view_stop_rubber_band               (PsppSheetView        *tree_view);
344 static void     update_prelight                              (PsppSheetView        *tree_view,
345                                                               int                 x,
346                                                               int                 y);
347 static void initialize_fixed_height_mode (PsppSheetView *tree_view);
348
349 /* interactive search */
350 static void     pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view);
351 static void     pspp_sheet_view_search_dialog_hide     (GtkWidget        *search_dialog,
352                                                          PsppSheetView      *tree_view);
353 static void     pspp_sheet_view_search_position_func      (PsppSheetView      *tree_view,
354                                                          GtkWidget        *search_dialog,
355                                                          gpointer          user_data);
356 static void     pspp_sheet_view_search_disable_popdown    (GtkEntry         *entry,
357                                                          GtkMenu          *menu,
358                                                          gpointer          data);
359 #if GTK3_TRANSITION
360 static void     pspp_sheet_view_search_preedit_changed    (GtkIMContext     *im_context,
361                                                          PsppSheetView      *tree_view);
362 #endif
363 static void     pspp_sheet_view_search_activate           (GtkEntry         *entry,
364                                                          PsppSheetView      *tree_view);
365 static gboolean pspp_sheet_view_real_search_enable_popdown(gpointer          data);
366 static void     pspp_sheet_view_search_enable_popdown     (GtkWidget        *widget,
367                                                          gpointer          data);
368 static gboolean pspp_sheet_view_search_delete_event       (GtkWidget        *widget,
369                                                          GdkEventAny      *event,
370                                                          PsppSheetView      *tree_view);
371 static gboolean pspp_sheet_view_search_button_press_event (GtkWidget        *widget,
372                                                          GdkEventButton   *event,
373                                                          PsppSheetView      *tree_view);
374 static gboolean pspp_sheet_view_search_scroll_event       (GtkWidget        *entry,
375                                                          GdkEventScroll   *event,
376                                                          PsppSheetView      *tree_view);
377 static gboolean pspp_sheet_view_search_key_press_event    (GtkWidget        *entry,
378                                                          GdkEventKey      *event,
379                                                          PsppSheetView      *tree_view);
380 static gboolean pspp_sheet_view_search_move               (GtkWidget        *window,
381                                                          PsppSheetView      *tree_view,
382                                                          gboolean          up);
383 static gboolean pspp_sheet_view_search_equal_func         (GtkTreeModel     *model,
384                                                          gint              column,
385                                                          const gchar      *key,
386                                                          GtkTreeIter      *iter,
387                                                          gpointer          search_data);
388 static gboolean pspp_sheet_view_search_iter               (GtkTreeModel     *model,
389                                                          PsppSheetSelection *selection,
390                                                          GtkTreeIter      *iter,
391                                                          const gchar      *text,
392                                                          gint             *count,
393                                                          gint              n);
394 static void     pspp_sheet_view_search_init               (GtkWidget        *entry,
395                                                          PsppSheetView      *tree_view);
396 static void     pspp_sheet_view_put                       (PsppSheetView      *tree_view,
397                                                          GtkWidget        *child_widget,
398                                                          gint              x,
399                                                          gint              y,
400                                                          gint              width,
401                                                          gint              height);
402 static gboolean pspp_sheet_view_start_editing             (PsppSheetView      *tree_view,
403                                                          GtkTreePath      *cursor_path);
404 static gboolean pspp_sheet_view_editable_button_press_event (GtkWidget *,
405                                                              GdkEventButton *,
406                                                              PsppSheetView *);
407 static void pspp_sheet_view_editable_clicked (GtkButton *, PsppSheetView *);
408 static void pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
409                                               PsppSheetViewColumn *column,
410                                               GtkTreePath       *path,
411                                               GtkCellEditable   *cell_editable,
412                                               GdkRectangle      *cell_area,
413                                               GdkEvent          *event,
414                                               guint              flags);
415 static gboolean pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
416                                                              gboolean     keybinding);
417 static gboolean pspp_sheet_view_start_interactive_search      (PsppSheetView *tree_view);
418 static PsppSheetViewColumn *pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
419                                                          PsppSheetViewColumn *column,
420                                                          gint               drop_position);
421 static void
422 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
423                                   PsppSheetViewColumn  *column,
424                                   const GdkRectangle   *background_area,
425                                   gboolean              subtract_focus_rect,
426                                   GdkRectangle         *cell_area);
427 static gint pspp_sheet_view_find_offset (PsppSheetView *tree_view,
428                                          gint height,
429                                          int *new_node);
430
431 /* GtkBuildable */
432 static void pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
433                                                GtkBuilder  *builder,
434                                                GObject     *child,
435                                                const gchar *type);
436 static void pspp_sheet_view_buildable_init      (GtkBuildableIface *iface);
437
438
439 static gboolean scroll_row_timeout                   (gpointer     data);
440 static void     add_scroll_timeout                   (PsppSheetView *tree_view);
441 static void     remove_scroll_timeout                (PsppSheetView *tree_view);
442
443 static guint tree_view_signals [LAST_SIGNAL] = { 0 };
444
445 static GtkBindingSet *edit_bindings;
446
447 \f
448
449 /* GType Methods
450  */
451
452 G_DEFINE_TYPE_WITH_CODE (PsppSheetView, pspp_sheet_view, GTK_TYPE_CONTAINER,
453                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
454                                                 pspp_sheet_view_buildable_init))
455
456 static void
457 pspp_sheet_view_get_preferred_width (GtkWidget *widget,
458                                gint      *minimal_width,
459                                gint      *natural_width)
460 {
461   GtkRequisition requisition;
462
463   pspp_sheet_view_size_request (widget, &requisition);
464
465   *minimal_width = *natural_width = requisition.width;
466 }
467
468 static void
469 pspp_sheet_view_get_preferred_height (GtkWidget *widget,
470                                 gint      *minimal_height,
471                                 gint      *natural_height)
472 {
473   GtkRequisition requisition;
474
475   pspp_sheet_view_size_request (widget, &requisition);
476
477   *minimal_height = *natural_height = requisition.height;
478 }
479
480 static void
481 pspp_sheet_view_class_init (PsppSheetViewClass *class)
482 {
483   GObjectClass *o_class;
484   GtkWidgetClass *widget_class;
485   GtkContainerClass *container_class;
486   GtkBindingSet *binding_set[2];
487   int i;
488
489   binding_set[0] = gtk_binding_set_by_class (class);
490
491   binding_set[1] = gtk_binding_set_new ("PsppSheetViewEditing");
492   edit_bindings = binding_set[1];
493
494   o_class = (GObjectClass *) class;
495   widget_class = (GtkWidgetClass *) class;
496   container_class = (GtkContainerClass *) class;
497
498   /* GObject signals */
499   o_class->set_property = pspp_sheet_view_set_property;
500   o_class->get_property = pspp_sheet_view_get_property;
501   o_class->finalize = pspp_sheet_view_finalize;
502   o_class->dispose = pspp_sheet_view_dispose;
503
504   /* GtkWidget signals */
505   widget_class->map = pspp_sheet_view_map;
506   widget_class->realize = pspp_sheet_view_realize;
507   widget_class->unrealize = pspp_sheet_view_unrealize;
508   widget_class->get_preferred_width = pspp_sheet_view_get_preferred_width;
509   widget_class->get_preferred_height = pspp_sheet_view_get_preferred_height;
510   widget_class->size_allocate = pspp_sheet_view_size_allocate;
511   widget_class->button_press_event = pspp_sheet_view_button_press;
512   widget_class->button_release_event = pspp_sheet_view_button_release;
513   widget_class->grab_broken_event = pspp_sheet_view_grab_broken;
514   /*widget_class->configure_event = pspp_sheet_view_configure;*/
515   widget_class->motion_notify_event = pspp_sheet_view_motion;
516   widget_class->draw = pspp_sheet_view_draw;
517   widget_class->key_press_event = pspp_sheet_view_key_press;
518   widget_class->key_release_event = pspp_sheet_view_key_release;
519   widget_class->enter_notify_event = pspp_sheet_view_enter_notify;
520   widget_class->leave_notify_event = pspp_sheet_view_leave_notify;
521   widget_class->focus_out_event = pspp_sheet_view_focus_out;
522   widget_class->drag_begin = pspp_sheet_view_drag_begin;
523   widget_class->drag_end = pspp_sheet_view_drag_end;
524   widget_class->drag_data_get = pspp_sheet_view_drag_data_get;
525   widget_class->drag_data_delete = pspp_sheet_view_drag_data_delete;
526   widget_class->drag_leave = pspp_sheet_view_drag_leave;
527   widget_class->drag_motion = pspp_sheet_view_drag_motion;
528   widget_class->drag_drop = pspp_sheet_view_drag_drop;
529   widget_class->drag_data_received = pspp_sheet_view_drag_data_received;
530   widget_class->focus = pspp_sheet_view_focus;
531   widget_class->grab_focus = pspp_sheet_view_grab_focus;
532   widget_class->style_set = pspp_sheet_view_style_set;
533   widget_class->grab_notify = pspp_sheet_view_grab_notify;
534   widget_class->state_changed = pspp_sheet_view_state_changed;
535
536   /* GtkContainer signals */
537   container_class->remove = pspp_sheet_view_remove;
538   container_class->forall = pspp_sheet_view_forall;
539   container_class->set_focus_child = pspp_sheet_view_set_focus_child;
540
541   class->set_scroll_adjustments = pspp_sheet_view_set_adjustments;
542   class->move_cursor = pspp_sheet_view_real_move_cursor;
543   class->select_all = pspp_sheet_view_real_select_all;
544   class->unselect_all = pspp_sheet_view_real_unselect_all;
545   class->select_cursor_row = pspp_sheet_view_real_select_cursor_row;
546   class->toggle_cursor_row = pspp_sheet_view_real_toggle_cursor_row;
547   class->start_interactive_search = pspp_sheet_view_start_interactive_search;
548
549   /* Properties */
550
551   g_object_class_install_property (o_class,
552                                    PROP_MODEL,
553                                    g_param_spec_object ("model",
554                                                         P_("TreeView Model"),
555                                                         P_("The model for the tree view"),
556                                                         GTK_TYPE_TREE_MODEL,
557                                                         GTK_PARAM_READWRITE));
558
559   g_object_class_install_property (o_class,
560                                    PROP_HADJUSTMENT,
561                                    g_param_spec_object ("hadjustment",
562                                                         P_("Horizontal Adjustment"),
563                                                         P_("Horizontal Adjustment for the widget"),
564                                                         GTK_TYPE_ADJUSTMENT,
565                                                         GTK_PARAM_READWRITE));
566
567   g_object_class_install_property (o_class,
568                                    PROP_VADJUSTMENT,
569                                    g_param_spec_object ("vadjustment",
570                                                         P_("Vertical Adjustment"),
571                                                         P_("Vertical Adjustment for the widget"),
572                                                         GTK_TYPE_ADJUSTMENT,
573                                                         GTK_PARAM_READWRITE));
574
575   g_object_class_install_property (o_class,
576                                    PROP_HEADERS_VISIBLE,
577                                    g_param_spec_boolean ("headers-visible",
578                                                          P_("Headers Visible"),
579                                                          P_("Show the column header buttons"),
580                                                          TRUE,
581                                                          GTK_PARAM_READWRITE));
582
583   g_object_class_install_property (o_class,
584                                    PROP_HEADERS_CLICKABLE,
585                                    g_param_spec_boolean ("headers-clickable",
586                                                          P_("Headers Clickable"),
587                                                          P_("Column headers respond to click events"),
588                                                          TRUE,
589                                                          GTK_PARAM_READWRITE));
590
591   g_object_class_install_property (o_class,
592                                    PROP_REORDERABLE,
593                                    g_param_spec_boolean ("reorderable",
594                                                          P_("Reorderable"),
595                                                          P_("View is reorderable"),
596                                                          FALSE,
597                                                          GTK_PARAM_READWRITE));
598
599   g_object_class_install_property (o_class,
600                                    PROP_RULES_HINT,
601                                    g_param_spec_boolean ("rules-hint",
602                                                          P_("Rules Hint"),
603                                                          P_("Set a hint to the theme engine to draw rows in alternating colors"),
604                                                          FALSE,
605                                                          GTK_PARAM_READWRITE));
606
607     g_object_class_install_property (o_class,
608                                      PROP_ENABLE_SEARCH,
609                                      g_param_spec_boolean ("enable-search",
610                                                            P_("Enable Search"),
611                                                            P_("View allows user to search through columns interactively"),
612                                                            TRUE,
613                                                            GTK_PARAM_READWRITE));
614
615     g_object_class_install_property (o_class,
616                                      PROP_SEARCH_COLUMN,
617                                      g_param_spec_int ("search-column",
618                                                        P_("Search Column"),
619                                                        P_("Model column to search through during interactive search"),
620                                                        -1,
621                                                        G_MAXINT,
622                                                        -1,
623                                                        GTK_PARAM_READWRITE));
624
625     /**
626      * PsppSheetView:hover-selection:
627      * 
628      * Enables of disables the hover selection mode of @tree_view.
629      * Hover selection makes the selected row follow the pointer.
630      * Currently, this works only for the selection modes 
631      * %PSPP_SHEET_SELECTION_SINGLE and %PSPP_SHEET_SELECTION_BROWSE.
632      *
633      * This mode is primarily intended for treeviews in popups, e.g.
634      * in #GtkComboBox or #GtkEntryCompletion.
635      *
636      * Since: 2.6
637      */
638     g_object_class_install_property (o_class,
639                                      PROP_HOVER_SELECTION,
640                                      g_param_spec_boolean ("hover-selection",
641                                                            P_("Hover Selection"),
642                                                            P_("Whether the selection should follow the pointer"),
643                                                            FALSE,
644                                                            GTK_PARAM_READWRITE));
645
646     g_object_class_install_property (o_class,
647                                      PROP_RUBBER_BANDING,
648                                      g_param_spec_boolean ("rubber-banding",
649                                                            P_("Rubber Banding"),
650                                                            P_("Whether to enable selection of multiple items by dragging the mouse pointer"),
651                                                            FALSE,
652                                                            GTK_PARAM_READWRITE));
653
654     g_object_class_install_property (o_class,
655                                      PROP_ENABLE_GRID_LINES,
656                                      g_param_spec_enum ("enable-grid-lines",
657                                                         P_("Enable Grid Lines"),
658                                                         P_("Whether grid lines should be drawn in the tree view"),
659                                                         PSPP_TYPE_SHEET_VIEW_GRID_LINES,
660                                                         PSPP_SHEET_VIEW_GRID_LINES_NONE,
661                                                         GTK_PARAM_READWRITE));
662
663     g_object_class_install_property (o_class,
664                                      PROP_TOOLTIP_COLUMN,
665                                      g_param_spec_int ("tooltip-column",
666                                                        P_("Tooltip Column"),
667                                                        P_("The column in the model containing the tooltip texts for the rows"),
668                                                        -1,
669                                                        G_MAXINT,
670                                                        -1,
671                                                        GTK_PARAM_READWRITE));
672
673     g_object_class_install_property (o_class,
674                                      PROP_SPECIAL_CELLS,
675                                      g_param_spec_enum ("special-cells",
676                                                         P_("Special Cells"),
677                                                         P_("Whether rows have special cells."),
678                                                         PSPP_TYPE_SHEET_VIEW_SPECIAL_CELLS,
679                                                         PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT,
680                                                         GTK_PARAM_READWRITE));
681
682     g_object_class_install_property (o_class,
683                                      PROP_FIXED_HEIGHT,
684                                      g_param_spec_int ("fixed-height",
685                                                        P_("Fixed Height"),
686                                                        P_("Height of a single row.  Normally the height of a row is determined automatically.  Writing this property sets fixed-height-set to true, preventing this property's value from changing."),
687                                                        -1,
688                                                        G_MAXINT,
689                                                        -1,
690                                                        GTK_PARAM_READWRITE));
691
692     g_object_class_install_property (o_class,
693                                      PROP_FIXED_HEIGHT_SET,
694                                      g_param_spec_boolean ("fixed-height-set",
695                                                            P_("Fixed Height Set"),
696                                                            P_("Whether fixed-height was set externally."),
697                                                            FALSE,
698                                                            GTK_PARAM_READWRITE));
699
700   /* Style properties */
701 #define _TREE_VIEW_EXPANDER_SIZE 12
702 #define _TREE_VIEW_VERTICAL_SEPARATOR 2
703 #define _TREE_VIEW_HORIZONTAL_SEPARATOR 2
704
705   gtk_widget_class_install_style_property (widget_class,
706                                            g_param_spec_int ("expander-size",
707                                                              P_("Expander Size"),
708                                                              P_("Size of the expander arrow"),
709                                                              0,
710                                                              G_MAXINT,
711                                                              _TREE_VIEW_EXPANDER_SIZE,
712                                                              GTK_PARAM_READABLE));
713
714   gtk_widget_class_install_style_property (widget_class,
715                                            g_param_spec_int ("vertical-separator",
716                                                              P_("Vertical Separator Width"),
717                                                              P_("Vertical space between cells.  Must be an even number"),
718                                                              0,
719                                                              G_MAXINT,
720                                                              _TREE_VIEW_VERTICAL_SEPARATOR,
721                                                              GTK_PARAM_READABLE));
722
723   gtk_widget_class_install_style_property (widget_class,
724                                            g_param_spec_int ("horizontal-separator",
725                                                              P_("Horizontal Separator Width"),
726                                                              P_("Horizontal space between cells.  Must be an even number"),
727                                                              0,
728                                                              G_MAXINT,
729                                                              _TREE_VIEW_HORIZONTAL_SEPARATOR,
730                                                              GTK_PARAM_READABLE));
731
732   gtk_widget_class_install_style_property (widget_class,
733                                            g_param_spec_boolean ("allow-rules",
734                                                                  P_("Allow Rules"),
735                                                                  P_("Allow drawing of alternating color rows"),
736                                                                  TRUE,
737                                                                  GTK_PARAM_READABLE));
738
739   gtk_widget_class_install_style_property (widget_class,
740                                            g_param_spec_boxed ("even-row-color",
741                                                                P_("Even Row Color"),
742                                                                P_("Color to use for even rows"),
743                                                                GDK_TYPE_COLOR,
744                                                                GTK_PARAM_READABLE));
745
746   gtk_widget_class_install_style_property (widget_class,
747                                            g_param_spec_boxed ("odd-row-color",
748                                                                P_("Odd Row Color"),
749                                                                P_("Color to use for odd rows"),
750                                                                GDK_TYPE_COLOR,
751                                                                GTK_PARAM_READABLE));
752
753   gtk_widget_class_install_style_property (widget_class,
754                                            g_param_spec_boolean ("row-ending-details",
755                                                                  P_("Row Ending details"),
756                                                                  P_("Enable extended row background theming"),
757                                                                  FALSE,
758                                                                  GTK_PARAM_READABLE));
759
760   gtk_widget_class_install_style_property (widget_class,
761                                            g_param_spec_int ("grid-line-width",
762                                                              P_("Grid line width"),
763                                                              P_("Width, in pixels, of the tree view grid lines"),
764                                                              0, G_MAXINT, 1,
765                                                              GTK_PARAM_READABLE));
766
767   gtk_widget_class_install_style_property (widget_class,
768                                            g_param_spec_int ("tree-line-width",
769                                                              P_("Tree line width"),
770                                                              P_("Width, in pixels, of the tree view lines"),
771                                                              0, G_MAXINT, 1,
772                                                              GTK_PARAM_READABLE));
773
774   gtk_widget_class_install_style_property (widget_class,
775                                            g_param_spec_string ("tree-line-pattern",
776                                                                 P_("Tree line pattern"),
777                                                                 P_("Dash pattern used to draw the tree view lines"),
778                                                                 "\1\1",
779                                                                 GTK_PARAM_READABLE));
780
781   /* Signals */
782 #if GTK3_TRANSITION
783   /**
784    * PsppSheetView::set-scroll-adjustments
785    * @horizontal: the horizontal #GtkAdjustment
786    * @vertical: the vertical #GtkAdjustment
787    *
788    * Set the scroll adjustments for the tree view. Usually scrolled containers
789    * like #GtkScrolledWindow will emit this signal to connect two instances
790    * of #GtkScrollbar to the scroll directions of the #PsppSheetView.
791    */
792   widget_class->set_scroll_adjustments_signal =
793     g_signal_new ("set-scroll-adjustments",
794                   G_TYPE_FROM_CLASS (o_class),
795                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
796                   G_STRUCT_OFFSET (PsppSheetViewClass, set_scroll_adjustments),
797                   NULL, NULL,
798                   psppire_marshal_VOID__OBJECT_OBJECT,
799                   G_TYPE_NONE, 2,
800                   GTK_TYPE_ADJUSTMENT,
801                   GTK_TYPE_ADJUSTMENT);
802 #endif
803
804   /**
805    * PsppSheetView::row-activated:
806    * @tree_view: the object on which the signal is emitted
807    * @path: the #GtkTreePath for the activated row
808    * @column: the #PsppSheetViewColumn in which the activation occurred
809    *
810    * The "row-activated" signal is emitted when the method
811    * pspp_sheet_view_row_activated() is called or the user double clicks 
812    * a treeview row. It is also emitted when a non-editable row is 
813    * selected and one of the keys: Space, Shift+Space, Return or 
814    * Enter is pressed.
815    * 
816    * For selection handling refer to the <link linkend="TreeWidget">tree 
817    * widget conceptual overview</link> as well as #PsppSheetSelection.
818    */
819   tree_view_signals[ROW_ACTIVATED] =
820     g_signal_new ("row-activated",
821                   G_TYPE_FROM_CLASS (o_class),
822                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
823                   G_STRUCT_OFFSET (PsppSheetViewClass, row_activated),
824                   NULL, NULL,
825                   psppire_marshal_VOID__BOXED_OBJECT,
826                   G_TYPE_NONE, 2,
827                   GTK_TYPE_TREE_PATH,
828                   PSPP_TYPE_SHEET_VIEW_COLUMN);
829
830   /**
831    * PsppSheetView::columns-changed:
832    * @tree_view: the object on which the signal is emitted 
833    * 
834    * The number of columns of the treeview has changed.
835    */
836   tree_view_signals[COLUMNS_CHANGED] =
837     g_signal_new ("columns-changed",
838                   G_TYPE_FROM_CLASS (o_class),
839                   G_SIGNAL_RUN_LAST,
840                   G_STRUCT_OFFSET (PsppSheetViewClass, columns_changed),
841                   NULL, NULL,
842                   g_cclosure_marshal_VOID__VOID,
843                   G_TYPE_NONE, 0);
844
845   /**
846    * PsppSheetView::cursor-changed:
847    * @tree_view: the object on which the signal is emitted
848    * 
849    * The position of the cursor (focused cell) has changed.
850    */
851   tree_view_signals[CURSOR_CHANGED] =
852     g_signal_new ("cursor-changed",
853                   G_TYPE_FROM_CLASS (o_class),
854                   G_SIGNAL_RUN_LAST,
855                   G_STRUCT_OFFSET (PsppSheetViewClass, cursor_changed),
856                   NULL, NULL,
857                   g_cclosure_marshal_VOID__VOID,
858                   G_TYPE_NONE, 0);
859
860   tree_view_signals[MOVE_CURSOR] =
861     g_signal_new ("move-cursor",
862                   G_TYPE_FROM_CLASS (o_class),
863                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
864                   G_STRUCT_OFFSET (PsppSheetViewClass, move_cursor),
865                   NULL, NULL,
866                   psppire_marshal_BOOLEAN__ENUM_INT,
867                   G_TYPE_BOOLEAN, 2,
868                   GTK_TYPE_MOVEMENT_STEP,
869                   G_TYPE_INT);
870
871   tree_view_signals[SELECT_ALL] =
872     g_signal_new ("select-all",
873                   G_TYPE_FROM_CLASS (o_class),
874                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
875                   G_STRUCT_OFFSET (PsppSheetViewClass, select_all),
876                   NULL, NULL,
877                   psppire_marshal_BOOLEAN__VOID,
878                   G_TYPE_BOOLEAN, 0);
879
880   tree_view_signals[UNSELECT_ALL] =
881     g_signal_new ("unselect-all",
882                   G_TYPE_FROM_CLASS (o_class),
883                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
884                   G_STRUCT_OFFSET (PsppSheetViewClass, unselect_all),
885                   NULL, NULL,
886                   psppire_marshal_BOOLEAN__VOID,
887                   G_TYPE_BOOLEAN, 0);
888
889   tree_view_signals[SELECT_CURSOR_ROW] =
890     g_signal_new ("select-cursor-row",
891                   G_TYPE_FROM_CLASS (o_class),
892                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
893                   G_STRUCT_OFFSET (PsppSheetViewClass, select_cursor_row),
894                   NULL, NULL,
895                   psppire_marshal_BOOLEAN__BOOLEAN,
896                   G_TYPE_BOOLEAN, 2,
897                   G_TYPE_BOOLEAN, G_TYPE_INT);
898
899   tree_view_signals[TOGGLE_CURSOR_ROW] =
900     g_signal_new ("toggle-cursor-row",
901                   G_TYPE_FROM_CLASS (o_class),
902                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
903                   G_STRUCT_OFFSET (PsppSheetViewClass, toggle_cursor_row),
904                   NULL, NULL,
905                   psppire_marshal_BOOLEAN__VOID,
906                   G_TYPE_BOOLEAN, 0);
907
908   tree_view_signals[START_INTERACTIVE_SEARCH] =
909     g_signal_new ("start-interactive-search",
910                   G_TYPE_FROM_CLASS (o_class),
911                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
912                   G_STRUCT_OFFSET (PsppSheetViewClass, start_interactive_search),
913                   NULL, NULL,
914                   psppire_marshal_BOOLEAN__VOID,
915                   G_TYPE_BOOLEAN, 0);
916
917   /* Key bindings */
918   for (i = 0; i < 2; i++)
919     {
920       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Up, 0, TRUE,
921                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
922       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Up, 0, TRUE,
923                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
924
925       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Down, 0, TRUE,
926                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
927       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Down, 0, TRUE,
928                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
929
930       pspp_sheet_view_add_move_binding (binding_set[i], GDK_p, GDK_CONTROL_MASK, FALSE,
931                                         GTK_MOVEMENT_DISPLAY_LINES, -1);
932
933       pspp_sheet_view_add_move_binding (binding_set[i], GDK_n, GDK_CONTROL_MASK, FALSE,
934                                         GTK_MOVEMENT_DISPLAY_LINES, 1);
935
936       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Home, 0, TRUE,
937                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
938       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Home, 0, TRUE,
939                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
940
941       pspp_sheet_view_add_move_binding (binding_set[i], GDK_End, 0, TRUE,
942                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
943       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_End, 0, TRUE,
944                                         GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
945
946       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Page_Up, 0, TRUE,
947                                         GTK_MOVEMENT_PAGES, -1);
948       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Page_Up, 0, TRUE,
949                                         GTK_MOVEMENT_PAGES, -1);
950
951       pspp_sheet_view_add_move_binding (binding_set[i], GDK_Page_Down, 0, TRUE,
952                                         GTK_MOVEMENT_PAGES, 1);
953       pspp_sheet_view_add_move_binding (binding_set[i], GDK_KP_Page_Down, 0, TRUE,
954                                         GTK_MOVEMENT_PAGES, 1);
955
956
957       gtk_binding_entry_add_signal (binding_set[i], GDK_Up, GDK_CONTROL_MASK, "move-cursor", 2,
958                                     G_TYPE_ENUM, GTK_MOVEMENT_BUFFER_ENDS,
959                                     G_TYPE_INT, -1);
960
961       gtk_binding_entry_add_signal (binding_set[i], GDK_Down, GDK_CONTROL_MASK, "move-cursor", 2,
962                                     G_TYPE_ENUM, GTK_MOVEMENT_BUFFER_ENDS,
963                                     G_TYPE_INT, 1);
964
965       gtk_binding_entry_add_signal (binding_set[i], GDK_Right, 0, "move-cursor", 2,
966                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
967                                     G_TYPE_INT, 1);
968
969       gtk_binding_entry_add_signal (binding_set[i], GDK_Left, 0, "move-cursor", 2,
970                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
971                                     G_TYPE_INT, -1);
972
973       gtk_binding_entry_add_signal (binding_set[i], GDK_Tab, 0, "move-cursor", 2,
974                                     G_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
975                                     G_TYPE_INT, 1);
976
977       gtk_binding_entry_add_signal (binding_set[i], GDK_Tab, GDK_SHIFT_MASK, "move-cursor", 2,
978                                     G_TYPE_ENUM, GTK_MOVEMENT_LOGICAL_POSITIONS,
979                                     G_TYPE_INT, -1);
980
981       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Right, 0, "move-cursor", 2,
982                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
983                                     G_TYPE_INT, 1);
984
985       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Left, 0, "move-cursor", 2,
986                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
987                                     G_TYPE_INT, -1);
988
989       gtk_binding_entry_add_signal (binding_set[i], GDK_Right, GDK_CONTROL_MASK,
990                                     "move-cursor", 2,
991                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
992                                     G_TYPE_INT, 1);
993
994       gtk_binding_entry_add_signal (binding_set[i], GDK_Left, GDK_CONTROL_MASK,
995                                     "move-cursor", 2,
996                                     G_TYPE_ENUM, GTK_MOVEMENT_DISPLAY_LINE_ENDS,
997                                     G_TYPE_INT, -1);
998
999       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Right, GDK_CONTROL_MASK,
1000                                     "move-cursor", 2,
1001                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1002                                     G_TYPE_INT, 1);
1003
1004       gtk_binding_entry_add_signal (binding_set[i], GDK_KP_Left, GDK_CONTROL_MASK,
1005                                     "move-cursor", 2,
1006                                     G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1007                                     G_TYPE_INT, -1);
1008
1009       gtk_binding_entry_add_signal (binding_set[i], GDK_f, GDK_CONTROL_MASK, "start-interactive-search", 0);
1010
1011       gtk_binding_entry_add_signal (binding_set[i], GDK_F, GDK_CONTROL_MASK, "start-interactive-search", 0);
1012     }
1013
1014   gtk_binding_entry_add_signal (binding_set[0], GDK_space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
1015   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
1016
1017   gtk_binding_entry_add_signal (binding_set[0], GDK_a, GDK_CONTROL_MASK, "select-all", 0);
1018   gtk_binding_entry_add_signal (binding_set[0], GDK_slash, GDK_CONTROL_MASK, "select-all", 0);
1019
1020   gtk_binding_entry_add_signal (binding_set[0], GDK_A, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "unselect-all", 0);
1021   gtk_binding_entry_add_signal (binding_set[0], GDK_backslash, GDK_CONTROL_MASK, "unselect-all", 0);
1022
1023   gtk_binding_entry_add_signal (binding_set[0], GDK_space, GDK_SHIFT_MASK, "select-cursor-row", 1,
1024                                 G_TYPE_BOOLEAN, TRUE,
1025                                 G_TYPE_INT, PSPP_SHEET_SELECT_MODE_EXTEND);
1026   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, GDK_SHIFT_MASK, "select-cursor-row", 1,
1027                                 G_TYPE_BOOLEAN, TRUE,
1028                                 G_TYPE_INT, PSPP_SHEET_SELECT_MODE_EXTEND);
1029
1030   gtk_binding_entry_add_signal (binding_set[0], GDK_space, 0, "select-cursor-row", 1,
1031                                 G_TYPE_BOOLEAN, TRUE,
1032                                 G_TYPE_INT, 0);
1033   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Space, 0, "select-cursor-row", 1,
1034                                 G_TYPE_BOOLEAN, TRUE,
1035                                 G_TYPE_INT, 0);
1036   gtk_binding_entry_add_signal (binding_set[0], GDK_Return, 0, "select-cursor-row", 1,
1037                                 G_TYPE_BOOLEAN, TRUE,
1038                                 G_TYPE_INT, 0);
1039   gtk_binding_entry_add_signal (binding_set[0], GDK_ISO_Enter, 0, "select-cursor-row", 1,
1040                                 G_TYPE_BOOLEAN, TRUE,
1041                                 G_TYPE_INT, 0);
1042   gtk_binding_entry_add_signal (binding_set[0], GDK_KP_Enter, 0, "select-cursor-row", 1,
1043                                 G_TYPE_BOOLEAN, TRUE,
1044                                 G_TYPE_INT, 0);
1045
1046   gtk_binding_entry_add_signal (binding_set[0], GDK_BackSpace, 0, "select-cursor-parent", 0);
1047   gtk_binding_entry_add_signal (binding_set[0], GDK_BackSpace, GDK_CONTROL_MASK, "select-cursor-parent", 0);
1048
1049   g_type_class_add_private (o_class, sizeof (PsppSheetViewPrivate));
1050 }
1051
1052 static void
1053 pspp_sheet_view_buildable_init (GtkBuildableIface *iface)
1054 {
1055   iface->add_child = pspp_sheet_view_buildable_add_child;
1056 }
1057
1058 static void
1059 pspp_sheet_view_init (PsppSheetView *tree_view)
1060 {
1061   tree_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (tree_view, PSPP_TYPE_SHEET_VIEW, PsppSheetViewPrivate);
1062
1063   gtk_widget_set_can_focus (GTK_WIDGET (tree_view), TRUE);
1064   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (tree_view), FALSE);
1065
1066   tree_view->priv->flags =  PSPP_SHEET_VIEW_DRAW_KEYFOCUS
1067                             | PSPP_SHEET_VIEW_HEADERS_VISIBLE;
1068
1069   /* We need some padding */
1070   tree_view->priv->selected = range_tower_create ();
1071   tree_view->priv->dy = 0;
1072   tree_view->priv->cursor_offset = 0;
1073   tree_view->priv->n_columns = 0;
1074   tree_view->priv->header_height = 1;
1075   tree_view->priv->x_drag = 0;
1076   tree_view->priv->drag_pos = -1;
1077   tree_view->priv->header_has_focus = FALSE;
1078   tree_view->priv->pressed_button = -1;
1079   tree_view->priv->press_start_x = -1;
1080   tree_view->priv->press_start_y = -1;
1081   tree_view->priv->reorderable = FALSE;
1082   tree_view->priv->presize_handler_timer = 0;
1083   tree_view->priv->scroll_sync_timer = 0;
1084   tree_view->priv->fixed_height = -1;
1085   tree_view->priv->fixed_height_set = FALSE;
1086   pspp_sheet_view_set_adjustments (tree_view, NULL, NULL);
1087   tree_view->priv->selection = _pspp_sheet_selection_new_with_tree_view (tree_view);
1088   tree_view->priv->enable_search = TRUE;
1089   tree_view->priv->search_column = -1;
1090   tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
1091   tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
1092   tree_view->priv->search_custom_entry_set = FALSE;
1093   tree_view->priv->typeselect_flush_timeout = 0;
1094   tree_view->priv->init_hadjust_value = TRUE;    
1095   tree_view->priv->width = 0;
1096           
1097   tree_view->priv->hover_selection = FALSE;
1098
1099   tree_view->priv->rubber_banding_enable = FALSE;
1100
1101   tree_view->priv->grid_lines = PSPP_SHEET_VIEW_GRID_LINES_NONE;
1102
1103   tree_view->priv->tooltip_column = -1;
1104
1105   tree_view->priv->special_cells = PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT;
1106
1107   tree_view->priv->post_validation_flag = FALSE;
1108
1109   tree_view->priv->last_button_x = -1;
1110   tree_view->priv->last_button_y = -1;
1111
1112   tree_view->priv->event_last_x = -10000;
1113   tree_view->priv->event_last_y = -10000;
1114
1115   tree_view->priv->prelight_node = -1;
1116   tree_view->priv->rubber_band_start_node = -1;
1117   tree_view->priv->rubber_band_end_node = -1;
1118
1119   tree_view->priv->anchor_column = NULL;
1120
1121   tree_view->priv->button_style = NULL;
1122
1123   tree_view->dispose_has_run = FALSE;
1124 }
1125
1126 \f
1127
1128 /* GObject Methods
1129  */
1130
1131 static void
1132 pspp_sheet_view_set_property (GObject         *object,
1133                             guint            prop_id,
1134                             const GValue    *value,
1135                             GParamSpec      *pspec)
1136 {
1137   PsppSheetView *tree_view;
1138
1139   tree_view = PSPP_SHEET_VIEW (object);
1140
1141   switch (prop_id)
1142     {
1143     case PROP_MODEL:
1144       pspp_sheet_view_set_model (tree_view, g_value_get_object (value));
1145       break;
1146     case PROP_HADJUSTMENT:
1147       pspp_sheet_view_set_hadjustment (tree_view, g_value_get_object (value));
1148       break;
1149     case PROP_VADJUSTMENT:
1150       pspp_sheet_view_set_vadjustment (tree_view, g_value_get_object (value));
1151       break;
1152     case PROP_HEADERS_VISIBLE:
1153       pspp_sheet_view_set_headers_visible (tree_view, g_value_get_boolean (value));
1154       break;
1155     case PROP_HEADERS_CLICKABLE:
1156       pspp_sheet_view_set_headers_clickable (tree_view, g_value_get_boolean (value));
1157       break;
1158     case PROP_REORDERABLE:
1159       pspp_sheet_view_set_reorderable (tree_view, g_value_get_boolean (value));
1160       break;
1161     case PROP_RULES_HINT:
1162       pspp_sheet_view_set_rules_hint (tree_view, g_value_get_boolean (value));
1163       break;
1164     case PROP_ENABLE_SEARCH:
1165       pspp_sheet_view_set_enable_search (tree_view, g_value_get_boolean (value));
1166       break;
1167     case PROP_SEARCH_COLUMN:
1168       pspp_sheet_view_set_search_column (tree_view, g_value_get_int (value));
1169       break;
1170     case PROP_HOVER_SELECTION:
1171       tree_view->priv->hover_selection = g_value_get_boolean (value);
1172       break;
1173     case PROP_RUBBER_BANDING:
1174       tree_view->priv->rubber_banding_enable = g_value_get_boolean (value);
1175       break;
1176     case PROP_ENABLE_GRID_LINES:
1177       pspp_sheet_view_set_grid_lines (tree_view, g_value_get_enum (value));
1178       break;
1179     case PROP_TOOLTIP_COLUMN:
1180       pspp_sheet_view_set_tooltip_column (tree_view, g_value_get_int (value));
1181       break;
1182     case PROP_SPECIAL_CELLS:
1183       pspp_sheet_view_set_special_cells (tree_view, g_value_get_enum (value));
1184       break;
1185     case PROP_FIXED_HEIGHT:
1186       pspp_sheet_view_set_fixed_height (tree_view, g_value_get_int (value));
1187       break;
1188     case PROP_FIXED_HEIGHT_SET:
1189       if (g_value_get_boolean (value))
1190         {
1191           if (!tree_view->priv->fixed_height_set
1192               && tree_view->priv->fixed_height >= 0)
1193             {
1194               tree_view->priv->fixed_height_set = true;
1195               g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
1196             }
1197         }
1198       else
1199         {
1200           if (tree_view->priv->fixed_height_set)
1201             {
1202               tree_view->priv->fixed_height_set = false;
1203               g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
1204               install_presize_handler (tree_view);
1205             }
1206         }
1207       break;
1208     default:
1209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1210       break;
1211     }
1212 }
1213
1214 static void
1215 pspp_sheet_view_get_property (GObject    *object,
1216                             guint       prop_id,
1217                             GValue     *value,
1218                             GParamSpec *pspec)
1219 {
1220   PsppSheetView *tree_view;
1221
1222   tree_view = PSPP_SHEET_VIEW (object);
1223
1224   switch (prop_id)
1225     {
1226     case PROP_MODEL:
1227       g_value_set_object (value, tree_view->priv->model);
1228       break;
1229     case PROP_HADJUSTMENT:
1230       g_value_set_object (value, tree_view->priv->hadjustment);
1231       break;
1232     case PROP_VADJUSTMENT:
1233       g_value_set_object (value, tree_view->priv->vadjustment);
1234       break;
1235     case PROP_HEADERS_VISIBLE:
1236       g_value_set_boolean (value, pspp_sheet_view_get_headers_visible (tree_view));
1237       break;
1238     case PROP_HEADERS_CLICKABLE:
1239       g_value_set_boolean (value, pspp_sheet_view_get_headers_clickable (tree_view));
1240       break;
1241     case PROP_REORDERABLE:
1242       g_value_set_boolean (value, tree_view->priv->reorderable);
1243       break;
1244     case PROP_RULES_HINT:
1245       g_value_set_boolean (value, tree_view->priv->has_rules);
1246       break;
1247     case PROP_ENABLE_SEARCH:
1248       g_value_set_boolean (value, tree_view->priv->enable_search);
1249       break;
1250     case PROP_SEARCH_COLUMN:
1251       g_value_set_int (value, tree_view->priv->search_column);
1252       break;
1253     case PROP_HOVER_SELECTION:
1254       g_value_set_boolean (value, tree_view->priv->hover_selection);
1255       break;
1256     case PROP_RUBBER_BANDING:
1257       g_value_set_boolean (value, tree_view->priv->rubber_banding_enable);
1258       break;
1259     case PROP_ENABLE_GRID_LINES:
1260       g_value_set_enum (value, tree_view->priv->grid_lines);
1261       break;
1262     case PROP_TOOLTIP_COLUMN:
1263       g_value_set_int (value, tree_view->priv->tooltip_column);
1264       break;
1265     case PROP_SPECIAL_CELLS:
1266       g_value_set_enum (value, tree_view->priv->special_cells);
1267       break;
1268     case PROP_FIXED_HEIGHT:
1269       g_value_set_int (value, pspp_sheet_view_get_fixed_height (tree_view));
1270       break;
1271     case PROP_FIXED_HEIGHT_SET:
1272       g_value_set_boolean (value, tree_view->priv->fixed_height_set);
1273       break;
1274     default:
1275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1276       break;
1277     }
1278 }
1279
1280 static void
1281 pspp_sheet_view_dispose (GObject *object)
1282 {
1283   PsppSheetView *tree_view = PSPP_SHEET_VIEW (object);
1284
1285   if (tree_view->dispose_has_run)
1286     return;
1287
1288   tree_view->dispose_has_run = TRUE;
1289
1290   if (tree_view->priv->selection != NULL)
1291     {
1292       _pspp_sheet_selection_set_tree_view (tree_view->priv->selection, NULL);
1293       g_object_unref (tree_view->priv->selection);
1294       tree_view->priv->selection = NULL;
1295     }
1296
1297   if (tree_view->priv->hadjustment)
1298     {
1299       g_object_unref (tree_view->priv->hadjustment);
1300       tree_view->priv->hadjustment = NULL;
1301     }
1302   if (tree_view->priv->vadjustment)
1303     {
1304       g_object_unref (tree_view->priv->vadjustment);
1305       tree_view->priv->vadjustment = NULL;
1306     }
1307
1308   if (tree_view->priv->button_style)
1309     {
1310       g_object_unref (tree_view->priv->button_style);
1311       tree_view->priv->button_style = NULL;
1312     }
1313
1314
1315   G_OBJECT_CLASS (pspp_sheet_view_parent_class)->dispose (object);
1316 }
1317
1318 \f
1319
1320 static void
1321 pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
1322                                    GtkBuilder  *builder,
1323                                    GObject     *child,
1324                                    const gchar *type)
1325 {
1326   pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), PSPP_SHEET_VIEW_COLUMN (child));
1327 }
1328
1329 static void
1330 pspp_sheet_view_finalize (GObject *object)
1331 {
1332   PsppSheetView *tree_view = PSPP_SHEET_VIEW (object);
1333
1334   pspp_sheet_view_stop_editing (tree_view, TRUE);
1335
1336   if (tree_view->priv->selected != NULL)
1337     {
1338       range_tower_destroy (tree_view->priv->selected);
1339       tree_view->priv->selected = NULL;
1340     }
1341
1342
1343   tree_view->priv->prelight_node = -1;
1344
1345
1346   if (tree_view->priv->scroll_to_path != NULL)
1347     {
1348       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
1349       tree_view->priv->scroll_to_path = NULL;
1350     }
1351
1352   if (tree_view->priv->drag_dest_row != NULL)
1353     {
1354       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
1355       tree_view->priv->drag_dest_row = NULL;
1356     }
1357
1358   if (tree_view->priv->top_row != NULL)
1359     {
1360       gtk_tree_row_reference_free (tree_view->priv->top_row);
1361       tree_view->priv->top_row = NULL;
1362     }
1363
1364   if (tree_view->priv->column_drop_func_data &&
1365       tree_view->priv->column_drop_func_data_destroy)
1366     {
1367       tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
1368       tree_view->priv->column_drop_func_data = NULL;
1369     }
1370
1371   if (tree_view->priv->destroy_count_destroy &&
1372       tree_view->priv->destroy_count_data)
1373     {
1374       tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
1375       tree_view->priv->destroy_count_data = NULL;
1376     }
1377
1378   gtk_tree_row_reference_free (tree_view->priv->cursor);
1379   tree_view->priv->cursor = NULL;
1380
1381   gtk_tree_row_reference_free (tree_view->priv->anchor);
1382   tree_view->priv->anchor = NULL;
1383
1384   /* destroy interactive search dialog */
1385   if (tree_view->priv->search_window)
1386     {
1387       gtk_widget_destroy (tree_view->priv->search_window);
1388       tree_view->priv->search_window = NULL;
1389       tree_view->priv->search_entry = NULL;
1390       if (tree_view->priv->typeselect_flush_timeout)
1391         {
1392           g_source_remove (tree_view->priv->typeselect_flush_timeout);
1393           tree_view->priv->typeselect_flush_timeout = 0;
1394         }
1395     }
1396
1397   if (tree_view->priv->search_destroy && tree_view->priv->search_user_data)
1398     {
1399       tree_view->priv->search_destroy (tree_view->priv->search_user_data);
1400       tree_view->priv->search_user_data = NULL;
1401     }
1402
1403   if (tree_view->priv->search_position_destroy && tree_view->priv->search_position_user_data)
1404     {
1405       tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
1406       tree_view->priv->search_position_user_data = NULL;
1407     }
1408
1409   pspp_sheet_view_set_model (tree_view, NULL);
1410
1411
1412   G_OBJECT_CLASS (pspp_sheet_view_parent_class)->finalize (object);
1413 }
1414
1415 \f
1416
1417 /* GtkWidget Methods
1418  */
1419
1420 /* GtkWidget::map helper */
1421 static void
1422 pspp_sheet_view_map_buttons (PsppSheetView *tree_view)
1423 {
1424   GList *list;
1425
1426   g_return_if_fail (gtk_widget_get_mapped (GTK_WIDGET (tree_view)));
1427
1428   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
1429     {
1430       PsppSheetViewColumn *column;
1431
1432       for (list = tree_view->priv->columns; list; list = list->next)
1433         {
1434           column = list->data;
1435           if (column->button != NULL &&
1436               gtk_widget_get_visible (column->button) &&
1437               !gtk_widget_get_mapped (column->button))
1438             gtk_widget_map (column->button);
1439         }
1440       for (list = tree_view->priv->columns; list; list = list->next)
1441         {
1442           column = list->data;
1443           if (column->visible == FALSE || column->window == NULL)
1444             continue;
1445           if (column->resizable)
1446             {
1447               gdk_window_raise (column->window);
1448               gdk_window_show (column->window);
1449             }
1450           else
1451             gdk_window_hide (column->window);
1452         }
1453       gdk_window_show (tree_view->priv->header_window);
1454     }
1455 }
1456
1457 static void
1458 pspp_sheet_view_map (GtkWidget *widget)
1459 {
1460   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1461   GList *tmp_list;
1462
1463   gtk_widget_set_mapped (widget, TRUE);
1464
1465   tmp_list = tree_view->priv->children;
1466   while (tmp_list)
1467     {
1468       PsppSheetViewChild *child = tmp_list->data;
1469       tmp_list = tmp_list->next;
1470
1471       if (gtk_widget_get_visible (child->widget))
1472         {
1473           if (!gtk_widget_get_mapped (child->widget))
1474             gtk_widget_map (child->widget);
1475         }
1476     }
1477   gdk_window_show (tree_view->priv->bin_window);
1478
1479   pspp_sheet_view_map_buttons (tree_view);
1480
1481   gdk_window_show (gtk_widget_get_window (widget));
1482 }
1483
1484 static void
1485 pspp_sheet_view_realize (GtkWidget *widget)
1486 {
1487   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1488   GList *tmp_list;
1489   GdkWindowAttr attributes;
1490   gint attributes_mask;
1491   GtkAllocation allocation;
1492   GtkAllocation old_allocation;
1493
1494   gtk_widget_set_realized (widget, TRUE);
1495
1496   gtk_widget_get_allocation (widget, &allocation);
1497   gtk_widget_get_allocation (widget, &old_allocation);
1498
1499   /* Make the main, clipping window */
1500   attributes.window_type = GDK_WINDOW_CHILD;
1501   attributes.x =      allocation.x;
1502   attributes.y =      allocation.y;
1503   attributes.width =  allocation.width;
1504   attributes.height = allocation.height;
1505   attributes.wclass = GDK_INPUT_OUTPUT;
1506   attributes.visual = gtk_widget_get_visual (widget);
1507   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1508
1509   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1510
1511   gtk_widget_set_window (widget,
1512                          gdk_window_new (gtk_widget_get_parent_window (widget),
1513                                          &attributes, attributes_mask));
1514   gdk_window_set_user_data (gtk_widget_get_window (widget), widget);
1515
1516   /* Make the window for the tree */
1517   attributes.x = 0;
1518   attributes.y = TREE_VIEW_HEADER_HEIGHT (tree_view);
1519   attributes.width = MAX (tree_view->priv->width, old_allocation.width);
1520   attributes.height = old_allocation.height;
1521   attributes.event_mask = (GDK_EXPOSURE_MASK |
1522                            GDK_SCROLL_MASK |
1523                            GDK_POINTER_MOTION_MASK |
1524                            GDK_ENTER_NOTIFY_MASK |
1525                            GDK_LEAVE_NOTIFY_MASK |
1526                            GDK_BUTTON_PRESS_MASK |
1527                            GDK_BUTTON_RELEASE_MASK |
1528                            gtk_widget_get_events (widget));
1529
1530   tree_view->priv->bin_window = gdk_window_new (gtk_widget_get_window (widget),
1531                                                 &attributes, attributes_mask);
1532   gdk_window_set_user_data (tree_view->priv->bin_window, widget);
1533
1534   /* Make the column header window */
1535   attributes.x = 0;
1536   attributes.y = 0;
1537   attributes.width = MAX (tree_view->priv->width, old_allocation.width);
1538   attributes.height = tree_view->priv->header_height;
1539   attributes.event_mask = (GDK_EXPOSURE_MASK |
1540                            GDK_SCROLL_MASK |
1541                            GDK_BUTTON_PRESS_MASK |
1542                            GDK_BUTTON_RELEASE_MASK |
1543                            GDK_KEY_PRESS_MASK |
1544                            GDK_KEY_RELEASE_MASK |
1545                            gtk_widget_get_events (widget));
1546
1547   tree_view->priv->header_window = gdk_window_new (gtk_widget_get_window (widget),
1548                                                    &attributes, attributes_mask);
1549   gdk_window_set_user_data (tree_view->priv->header_window, widget);
1550
1551   /* Add them all up. */
1552   gtk_widget_set_style (widget,
1553                        gtk_style_attach (gtk_widget_get_style (widget), gtk_widget_get_window (widget)));
1554   gdk_window_set_background (tree_view->priv->bin_window, &gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
1555   gtk_style_set_background (gtk_widget_get_style (widget), tree_view->priv->header_window, GTK_STATE_NORMAL);
1556
1557   tmp_list = tree_view->priv->children;
1558   while (tmp_list)
1559     {
1560       PsppSheetViewChild *child = tmp_list->data;
1561       tmp_list = tmp_list->next;
1562
1563       gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
1564     }
1565
1566   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
1567     _pspp_sheet_view_column_realize_button (PSPP_SHEET_VIEW_COLUMN (tmp_list->data));
1568
1569   /* Need to call those here, since they create GCs */
1570   pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
1571
1572   install_presize_handler (tree_view); 
1573 }
1574
1575 static void
1576 pspp_sheet_view_unrealize (GtkWidget *widget)
1577 {
1578   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1579   PsppSheetViewPrivate *priv = tree_view->priv;
1580   GList *list;
1581
1582   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->unrealize (widget);
1583
1584   if (priv->scroll_timeout != 0)
1585     {
1586       g_source_remove (priv->scroll_timeout);
1587       priv->scroll_timeout = 0;
1588     }
1589
1590   if (priv->open_dest_timeout != 0)
1591     {
1592       g_source_remove (priv->open_dest_timeout);
1593       priv->open_dest_timeout = 0;
1594     }
1595
1596   if (priv->presize_handler_timer != 0)
1597     {
1598       g_source_remove (priv->presize_handler_timer);
1599       priv->presize_handler_timer = 0;
1600     }
1601
1602   if (priv->validate_rows_timer != 0)
1603     {
1604       g_source_remove (priv->validate_rows_timer);
1605       priv->validate_rows_timer = 0;
1606     }
1607
1608   if (priv->scroll_sync_timer != 0)
1609     {
1610       g_source_remove (priv->scroll_sync_timer);
1611       priv->scroll_sync_timer = 0;
1612     }
1613
1614   if (priv->typeselect_flush_timeout)
1615     {
1616       g_source_remove (priv->typeselect_flush_timeout);
1617       priv->typeselect_flush_timeout = 0;
1618     }
1619   
1620   for (list = priv->columns; list; list = list->next)
1621     _pspp_sheet_view_column_unrealize_button (PSPP_SHEET_VIEW_COLUMN (list->data));
1622
1623   gdk_window_set_user_data (priv->bin_window, NULL);
1624   gdk_window_destroy (priv->bin_window);
1625   priv->bin_window = NULL;
1626
1627   gdk_window_set_user_data (priv->header_window, NULL);
1628   gdk_window_destroy (priv->header_window);
1629   priv->header_window = NULL;
1630
1631   if (priv->drag_window)
1632     {
1633       gdk_window_set_user_data (priv->drag_window, NULL);
1634       gdk_window_destroy (priv->drag_window);
1635       priv->drag_window = NULL;
1636     }
1637
1638   if (priv->drag_highlight_window)
1639     {
1640       gdk_window_set_user_data (priv->drag_highlight_window, NULL);
1641       gdk_window_destroy (priv->drag_highlight_window);
1642       priv->drag_highlight_window = NULL;
1643     }
1644
1645   if (tree_view->priv->columns != NULL)
1646     {
1647       list = tree_view->priv->columns;
1648       while (list)
1649         {
1650           PsppSheetViewColumn *column;
1651           column = PSPP_SHEET_VIEW_COLUMN (list->data);
1652           list = list->next;
1653           pspp_sheet_view_remove_column (tree_view, column);
1654         }
1655       tree_view->priv->columns = NULL;
1656     }
1657 }
1658
1659 /* GtkWidget::size_request helper */
1660 static void
1661 pspp_sheet_view_size_request_columns (PsppSheetView *tree_view)
1662 {
1663   GList *list;
1664
1665   tree_view->priv->header_height = 0;
1666
1667   if (tree_view->priv->model)
1668     {
1669       for (list = tree_view->priv->columns; list; list = list->next)
1670         {
1671           GtkRequisition requisition;
1672           PsppSheetViewColumn *column = list->data;
1673
1674           pspp_sheet_view_column_size_request (column, &requisition);
1675           column->button_request = requisition.width;
1676           tree_view->priv->header_height = MAX (tree_view->priv->header_height, requisition.height);
1677         }
1678     }
1679 }
1680
1681
1682 /* Called only by ::size_request */
1683 static void
1684 pspp_sheet_view_update_size (PsppSheetView *tree_view)
1685 {
1686   GList *list;
1687   PsppSheetViewColumn *column;
1688   gint i;
1689
1690   if (tree_view->priv->model == NULL)
1691     {
1692       tree_view->priv->width = 0;
1693       tree_view->priv->prev_width = 0;                   
1694       tree_view->priv->height = 0;
1695       return;
1696     }
1697
1698   tree_view->priv->prev_width = tree_view->priv->width;  
1699   tree_view->priv->width = 0;
1700
1701   /* keep this in sync with size_allocate below */
1702   for (list = tree_view->priv->columns, i = 0; list; list = list->next, i++)
1703     {
1704       gint real_requested_width = 0;
1705       column = list->data;
1706       if (!column->visible)
1707         continue;
1708
1709       if (column->use_resized_width)
1710         {
1711           real_requested_width = column->resized_width;
1712         }
1713       else
1714         {
1715           real_requested_width = column->fixed_width;
1716         }
1717
1718       if (column->min_width != -1)
1719         real_requested_width = MAX (real_requested_width, column->min_width);
1720       if (column->max_width != -1)
1721         real_requested_width = MIN (real_requested_width, column->max_width);
1722
1723       tree_view->priv->width += real_requested_width;
1724     }
1725
1726   tree_view->priv->height = tree_view->priv->fixed_height * tree_view->priv->row_count;
1727 }
1728
1729 static void
1730 pspp_sheet_view_size_request (GtkWidget      *widget,
1731                             GtkRequisition *requisition)
1732 {
1733   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1734   GList *tmp_list;
1735
1736   /* we validate some rows initially just to make sure we have some size. 
1737    * In practice, with a lot of static lists, this should get a good width.
1738    */
1739   initialize_fixed_height_mode (tree_view);
1740   pspp_sheet_view_size_request_columns (tree_view);
1741   pspp_sheet_view_update_size (PSPP_SHEET_VIEW (widget));
1742
1743   requisition->width = tree_view->priv->width;
1744   requisition->height = tree_view->priv->height + TREE_VIEW_HEADER_HEIGHT (tree_view);
1745
1746   tmp_list = tree_view->priv->children;
1747
1748   while (tmp_list)
1749     {
1750       PsppSheetViewChild *child = tmp_list->data;
1751       GtkRequisition child_requisition;
1752
1753       tmp_list = tmp_list->next;
1754
1755       if (gtk_widget_get_visible (child->widget))
1756         gtk_widget_size_request (child->widget, &child_requisition);
1757     }
1758 }
1759
1760 static void
1761 invalidate_column (PsppSheetView       *tree_view,
1762                    PsppSheetViewColumn *column)
1763 {
1764   gint column_offset = 0;
1765   GList *list;
1766   GtkWidget *widget = GTK_WIDGET (tree_view);
1767   gboolean rtl;
1768
1769   if (!gtk_widget_get_realized (widget))
1770     return;
1771
1772   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1773   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
1774        list;
1775        list = (rtl ? list->prev : list->next))
1776     {
1777       PsppSheetViewColumn *tmpcolumn = list->data;
1778       if (tmpcolumn == column)
1779         {
1780           GdkRectangle invalid_rect;
1781           GtkAllocation allocation;
1782
1783           gtk_widget_get_allocation (widget, &allocation);
1784           invalid_rect.x = column_offset;
1785           invalid_rect.y = 0;
1786           invalid_rect.width = column->width;
1787           invalid_rect.height = allocation.height;
1788           
1789           gdk_window_invalidate_rect (gtk_widget_get_window (widget), &invalid_rect, TRUE);
1790           break;
1791         }
1792       
1793       column_offset += tmpcolumn->width;
1794     }
1795 }
1796
1797 static void
1798 invalidate_last_column (PsppSheetView *tree_view)
1799 {
1800   GList *last_column;
1801   gboolean rtl;
1802
1803   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
1804
1805   for (last_column = (rtl ? g_list_first (tree_view->priv->columns) : g_list_last (tree_view->priv->columns));
1806        last_column;
1807        last_column = (rtl ? last_column->next : last_column->prev))
1808     {
1809       if (PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible)
1810         {
1811           invalidate_column (tree_view, last_column->data);
1812           return;
1813         }
1814     }
1815 }
1816
1817 static gint
1818 pspp_sheet_view_get_real_requested_width_from_column (PsppSheetView       *tree_view,
1819                                                     PsppSheetViewColumn *column)
1820 {
1821   gint real_requested_width;
1822
1823   if (column->use_resized_width)
1824     {
1825       real_requested_width = column->resized_width;
1826     }
1827   else
1828     {
1829       real_requested_width = column->fixed_width;
1830     }
1831
1832   if (column->min_width != -1)
1833     real_requested_width = MAX (real_requested_width, column->min_width);
1834   if (column->max_width != -1)
1835     real_requested_width = MIN (real_requested_width, column->max_width);
1836
1837   return real_requested_width;
1838 }
1839
1840 static gboolean
1841 span_intersects (int a0, int a_width,
1842                  int b0, int b_width)
1843 {
1844   int a1 = a0 + a_width;
1845   int b1 = b0 + b_width;
1846   return (a0 >= b0 && a0 < b1) || (b0 >= a0 && b0 < a1);
1847 }
1848
1849 /* GtkWidget::size_allocate helper */
1850 static void
1851 pspp_sheet_view_size_allocate_columns (GtkWidget *widget,
1852                                      gboolean  *width_changed)
1853 {
1854   PsppSheetView *tree_view;
1855   GList *list, *first_column, *last_column;
1856   PsppSheetViewColumn *column;
1857   GtkAllocation col_allocation;
1858   GtkAllocation allocation;
1859   gint width = 0;
1860   gint extra, extra_per_column;
1861   gint full_requested_width = 0;
1862   gint number_of_expand_columns = 0;
1863   gboolean column_changed = FALSE;
1864   gboolean rtl;
1865
1866   tree_view = PSPP_SHEET_VIEW (widget);
1867
1868   for (last_column = g_list_last (tree_view->priv->columns);
1869        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
1870        last_column = last_column->prev)
1871     ;
1872
1873   if (last_column == NULL)
1874     return;
1875
1876   for (first_column = g_list_first (tree_view->priv->columns);
1877        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
1878        first_column = first_column->next)
1879     ;
1880
1881   col_allocation.y = 0;
1882   col_allocation.height = tree_view->priv->header_height;
1883
1884   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1885
1886   /* find out how many extra space and expandable columns we have */
1887   for (list = tree_view->priv->columns; list != last_column->next; list = list->next)
1888     {
1889       column = (PsppSheetViewColumn *)list->data;
1890
1891       if (!column->visible)
1892         continue;
1893
1894       full_requested_width += pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
1895
1896       if (column->expand)
1897         number_of_expand_columns++;
1898     }
1899
1900   gtk_widget_get_allocation (widget, &allocation);
1901   extra = MAX (allocation.width - full_requested_width, 0);
1902   if (number_of_expand_columns > 0)
1903     extra_per_column = extra/number_of_expand_columns;
1904   else
1905     extra_per_column = 0;
1906
1907   for (list = (rtl ? last_column : first_column); 
1908        list != (rtl ? first_column->prev : last_column->next);
1909        list = (rtl ? list->prev : list->next)) 
1910     {
1911       gint real_requested_width = 0;
1912       gint old_width;
1913
1914       column = list->data;
1915       old_width = column->width;
1916
1917       if (!column->visible)
1918         continue;
1919
1920       /* We need to handle the dragged button specially.
1921        */
1922       if (column == tree_view->priv->drag_column)
1923         {
1924           GtkAllocation drag_allocation;
1925           drag_allocation.width =  gdk_window_get_width (tree_view->priv->drag_window);
1926           drag_allocation.height = gdk_window_get_height (tree_view->priv->drag_window);
1927           drag_allocation.x = 0;
1928           drag_allocation.y = 0;
1929           pspp_sheet_view_column_size_allocate (tree_view->priv->drag_column,
1930                                                 &drag_allocation);
1931           width += drag_allocation.width;
1932           continue;
1933         }
1934
1935       real_requested_width = pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
1936
1937       col_allocation.x = width;
1938       column->width = real_requested_width;
1939
1940       if (column->expand)
1941         {
1942           if (number_of_expand_columns == 1)
1943             {
1944               /* We add the remander to the last column as
1945                * */
1946               column->width += extra;
1947             }
1948           else
1949             {
1950               column->width += extra_per_column;
1951               extra -= extra_per_column;
1952               number_of_expand_columns --;
1953             }
1954         }
1955
1956       if (column->width != old_width)
1957         g_object_notify (G_OBJECT (column), "width");
1958
1959       col_allocation.width = column->width;
1960       width += column->width;
1961
1962       if (column->width > old_width)
1963         column_changed = TRUE;
1964
1965       pspp_sheet_view_column_size_allocate (column, &col_allocation);
1966
1967       if (span_intersects (col_allocation.x, col_allocation.width,
1968                            gtk_adjustment_get_value (tree_view->priv->hadjustment),
1969                            allocation.width)
1970           && gtk_widget_get_realized (widget))
1971         pspp_sheet_view_column_set_need_button (column, TRUE);
1972
1973       if (column->window)
1974         gdk_window_move_resize (column->window,
1975                                 col_allocation.x + (rtl ? 0 : col_allocation.width) - TREE_VIEW_DRAG_WIDTH/2,
1976                                 col_allocation.y,
1977                                 TREE_VIEW_DRAG_WIDTH, col_allocation.height);
1978     }
1979
1980   /* We change the width here.  The user might have been resizing columns,
1981    * so the total width of the tree view changes.
1982    */
1983   tree_view->priv->width = width;
1984   if (width_changed)
1985     *width_changed = TRUE;
1986
1987   if (column_changed)
1988     gtk_widget_queue_draw (GTK_WIDGET (tree_view));
1989 }
1990
1991 static void
1992 pspp_sheet_view_size_allocate (GtkWidget     *widget,
1993                              GtkAllocation *allocation)
1994 {
1995   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1996   GList *tmp_list;
1997   gboolean width_changed = FALSE;
1998   GtkAllocation old_allocation;
1999   gtk_widget_get_allocation (widget, &old_allocation);
2000
2001   if (allocation->width != old_allocation.width)
2002     width_changed = TRUE;
2003
2004
2005   gtk_widget_set_allocation (widget, allocation);
2006
2007   tmp_list = tree_view->priv->children;
2008
2009   while (tmp_list)
2010     {
2011       GtkAllocation allocation;
2012
2013       PsppSheetViewChild *child = tmp_list->data;
2014       tmp_list = tmp_list->next;
2015
2016       /* totally ignore our child's requisition */
2017       allocation.x = child->x;
2018       allocation.y = child->y;
2019       allocation.width = child->width;
2020       allocation.height = child->height;
2021       gtk_widget_size_allocate (child->widget, &allocation);
2022     }
2023
2024   /* We size-allocate the columns first because the width of the
2025    * tree view (used in updating the adjustments below) might change.
2026    */
2027   pspp_sheet_view_size_allocate_columns (widget, &width_changed);
2028
2029   gtk_adjustment_set_page_size (tree_view->priv->hadjustment, allocation->width);
2030   gtk_adjustment_set_page_increment (tree_view->priv->hadjustment, allocation->width * 0.9);
2031   gtk_adjustment_set_step_increment (tree_view->priv->hadjustment, allocation->width * 0.1);
2032   gtk_adjustment_set_lower (tree_view->priv->hadjustment, 0);
2033   gtk_adjustment_set_upper (tree_view->priv->hadjustment, MAX (gtk_adjustment_get_page_size (tree_view->priv->hadjustment), tree_view->priv->width));
2034
2035   if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)   
2036     {
2037       if (allocation->width < tree_view->priv->width)
2038         {
2039           if (tree_view->priv->init_hadjust_value)
2040             {
2041               gtk_adjustment_set_value (tree_view->priv->hadjustment, MAX (tree_view->priv->width - allocation->width, 0));
2042               tree_view->priv->init_hadjust_value = FALSE;
2043             }
2044           else if (allocation->width != old_allocation.width)
2045             {
2046               gtk_adjustment_set_value (tree_view->priv->hadjustment, CLAMP (gtk_adjustment_get_value (tree_view->priv->hadjustment) - allocation->width + old_allocation.width, 0, tree_view->priv->width - allocation->width));
2047             }
2048           else
2049             gtk_adjustment_set_value (tree_view->priv->hadjustment, CLAMP (tree_view->priv->width - (tree_view->priv->prev_width - gtk_adjustment_get_value (tree_view->priv->hadjustment)), 0, tree_view->priv->width - allocation->width));
2050         }
2051       else
2052         {
2053           gtk_adjustment_set_value (tree_view->priv->hadjustment, 0);
2054           tree_view->priv->init_hadjust_value = TRUE;
2055         }
2056     }
2057   else
2058     if (gtk_adjustment_get_value (tree_view->priv->hadjustment) + allocation->width > tree_view->priv->width)
2059       gtk_adjustment_set_value (tree_view->priv->hadjustment, MAX (tree_view->priv->width - allocation->width, 0));
2060
2061   gtk_adjustment_changed (tree_view->priv->hadjustment);
2062
2063   gtk_adjustment_set_page_size (tree_view->priv->vadjustment, allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view));
2064   gtk_adjustment_set_step_increment (tree_view->priv->vadjustment, gtk_adjustment_get_page_size (tree_view->priv->vadjustment) * 0.1);
2065   gtk_adjustment_set_page_increment (tree_view->priv->vadjustment, gtk_adjustment_get_page_size (tree_view->priv->vadjustment) * 0.9);
2066   gtk_adjustment_set_lower (tree_view->priv->vadjustment, 0);
2067   gtk_adjustment_set_upper (tree_view->priv->vadjustment, MAX (gtk_adjustment_get_page_size (tree_view->priv->vadjustment), tree_view->priv->height));
2068
2069   gtk_adjustment_changed (tree_view->priv->vadjustment);
2070
2071   /* now the adjustments and window sizes are in sync, we can sync toprow/dy again */
2072   if (tree_view->priv->height <= gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
2073     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
2074   else if (gtk_adjustment_get_value (tree_view->priv->vadjustment) + gtk_adjustment_get_page_size (tree_view->priv->vadjustment) > tree_view->priv->height)
2075     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment),
2076                               tree_view->priv->height - gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
2077   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
2078     pspp_sheet_view_top_row_to_dy (tree_view);
2079   else
2080     pspp_sheet_view_dy_to_top_row (tree_view);
2081   
2082   if (gtk_widget_get_realized (widget))
2083     {
2084       gdk_window_move_resize (gtk_widget_get_window (widget),
2085                               allocation->x, allocation->y,
2086                               allocation->width, allocation->height);
2087       gdk_window_move_resize (tree_view->priv->header_window,
2088                               - (gint) gtk_adjustment_get_value (tree_view->priv->hadjustment),
2089                               0,
2090                               MAX (tree_view->priv->width, allocation->width),
2091                               tree_view->priv->header_height);
2092       gdk_window_move_resize (tree_view->priv->bin_window,
2093                               - (gint) gtk_adjustment_get_value (tree_view->priv->hadjustment),
2094                               TREE_VIEW_HEADER_HEIGHT (tree_view),
2095                               MAX (tree_view->priv->width, allocation->width),
2096                               allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view));
2097     }
2098
2099   if (tree_view->priv->row_count == 0)
2100     invalidate_empty_focus (tree_view);
2101
2102   if (gtk_widget_get_realized (widget))
2103     {
2104       gboolean has_expand_column = FALSE;
2105       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
2106         {
2107           if (pspp_sheet_view_column_get_expand (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)))
2108             {
2109               has_expand_column = TRUE;
2110               break;
2111             }
2112         }
2113
2114       /* This little hack only works if we have an LTR locale, and no column has the  */
2115       if (width_changed)
2116         {
2117           if (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_LTR &&
2118               ! has_expand_column)
2119             invalidate_last_column (tree_view);
2120           else
2121             gtk_widget_queue_draw (widget);
2122         }
2123     }
2124 }
2125
2126 /* Grabs the focus and unsets the PSPP_SHEET_VIEW_DRAW_KEYFOCUS flag */
2127 static void
2128 grab_focus_and_unset_draw_keyfocus (PsppSheetView *tree_view)
2129 {
2130   GtkWidget *widget = GTK_WIDGET (tree_view);
2131
2132   if (gtk_widget_get_can_focus (widget) && !gtk_widget_has_focus (widget))
2133     gtk_widget_grab_focus (widget);
2134   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
2135 }
2136
2137 gboolean
2138 pspp_sheet_view_node_is_selected (PsppSheetView *tree_view,
2139                                   int node)
2140 {
2141   return node >= 0 && range_tower_contains (tree_view->priv->selected, node);
2142 }
2143
2144 void
2145 pspp_sheet_view_node_select (PsppSheetView *tree_view,
2146                              int node)
2147 {
2148   range_tower_set1 (tree_view->priv->selected, node, 1);
2149 }
2150
2151 void
2152 pspp_sheet_view_node_unselect (PsppSheetView *tree_view,
2153                                int node)
2154 {
2155   range_tower_set0 (tree_view->priv->selected, node, 1);
2156 }
2157
2158 gint
2159 pspp_sheet_view_node_next (PsppSheetView *tree_view,
2160                            gint node)
2161 {
2162   return node + 1 < tree_view->priv->row_count ? node + 1 : -1;
2163 }
2164
2165 gint
2166 pspp_sheet_view_node_prev (PsppSheetView *tree_view,
2167                            gint node)
2168 {
2169   return node > 0 ? node - 1 : -1;
2170 }
2171
2172 static gboolean
2173 all_columns_selected (PsppSheetView *tree_view)
2174 {
2175   GList *list;
2176
2177   for (list = tree_view->priv->columns; list; list = list->next)
2178     {
2179       PsppSheetViewColumn *column = list->data;
2180       if (column->selectable && !column->selected)
2181         return FALSE;
2182     }
2183
2184   return TRUE;
2185 }
2186
2187 static gboolean
2188 pspp_sheet_view_row_head_clicked (PsppSheetView *tree_view,
2189                                   gint node,
2190                                   PsppSheetViewColumn *column,
2191                                   GdkEventButton *event)
2192 {
2193   PsppSheetSelection *selection;
2194   PsppSheetSelectionMode mode;
2195   GtkTreePath *path;
2196   gboolean update_anchor;
2197   gboolean handled;
2198   guint modifiers;
2199
2200   g_return_val_if_fail (tree_view != NULL, FALSE);
2201   g_return_val_if_fail (column != NULL, FALSE);
2202
2203   selection = tree_view->priv->selection;
2204   mode = pspp_sheet_selection_get_mode (selection);
2205   if (mode != PSPP_SHEET_SELECTION_RECTANGLE)
2206     return FALSE;
2207
2208   if (!column->row_head)
2209     return FALSE;
2210
2211   if (event)
2212     {
2213       modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
2214       if (event->type != GDK_BUTTON_PRESS
2215           || (modifiers != GDK_CONTROL_MASK && modifiers != GDK_SHIFT_MASK))
2216         return FALSE;
2217     }
2218   else
2219     modifiers = 0;
2220
2221   path = gtk_tree_path_new_from_indices (node, -1);
2222   if (event == NULL)
2223     {
2224       pspp_sheet_selection_unselect_all (selection);
2225       pspp_sheet_selection_select_path (selection, path);
2226       pspp_sheet_selection_select_all_columns (selection);
2227       update_anchor = TRUE;
2228       handled = TRUE;
2229     }
2230   else if (event->type == GDK_BUTTON_PRESS && event->button == 3)
2231     {
2232       if (pspp_sheet_selection_count_selected_rows (selection) <= 1
2233           || !all_columns_selected (tree_view))
2234         {
2235           pspp_sheet_selection_unselect_all (selection);
2236           pspp_sheet_selection_select_path (selection, path);
2237           pspp_sheet_selection_select_all_columns (selection);
2238           update_anchor = TRUE;
2239           handled = FALSE;
2240         }
2241       else
2242         update_anchor = handled = FALSE;
2243     }
2244   else if (event->type == GDK_BUTTON_PRESS && event->button == 1
2245            && modifiers == GDK_CONTROL_MASK)
2246     {
2247       if (!all_columns_selected (tree_view))
2248         {
2249           pspp_sheet_selection_unselect_all (selection);
2250           pspp_sheet_selection_select_all_columns (selection);
2251         }
2252
2253       if (pspp_sheet_selection_path_is_selected (selection, path))
2254         pspp_sheet_selection_unselect_path (selection, path);
2255       else
2256         pspp_sheet_selection_select_path (selection, path);
2257       update_anchor = TRUE;
2258       handled = TRUE;
2259     }
2260   else if (event->type == GDK_BUTTON_PRESS && event->button == 1
2261            && modifiers == GDK_SHIFT_MASK)
2262     {
2263       GtkTreeRowReference *anchor = tree_view->priv->anchor;
2264       GtkTreePath *anchor_path;
2265
2266       if (all_columns_selected (tree_view)
2267           && gtk_tree_row_reference_valid (anchor))
2268         {
2269           update_anchor = FALSE;
2270           anchor_path = gtk_tree_row_reference_get_path (anchor);
2271         }
2272       else
2273         {
2274           update_anchor = TRUE;
2275           anchor_path = gtk_tree_path_copy (path);
2276         }
2277
2278       pspp_sheet_selection_unselect_all (selection);
2279       pspp_sheet_selection_select_range (selection, anchor_path, path);
2280       pspp_sheet_selection_select_all_columns (selection);
2281
2282       gtk_tree_path_free (anchor_path);
2283
2284       handled = TRUE;
2285     }
2286   else
2287     update_anchor = handled = FALSE;
2288
2289   if (update_anchor)
2290     {
2291       if (tree_view->priv->anchor)
2292         gtk_tree_row_reference_free (tree_view->priv->anchor);
2293       tree_view->priv->anchor =
2294         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
2295                                           tree_view->priv->model,
2296                                           path);
2297     }
2298
2299   gtk_tree_path_free (path);
2300   return handled;
2301 }
2302
2303 static gboolean
2304 find_click (PsppSheetView *tree_view,
2305             gint x, gint y,
2306             gint *node,
2307             PsppSheetViewColumn **column,
2308             GdkRectangle *background_area,
2309             GdkRectangle *cell_area)
2310 {
2311   gint y_offset;
2312   gboolean rtl;
2313   GList *list;
2314   gint new_y;
2315
2316   /* find the node that was clicked */
2317   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, y);
2318   if (new_y < 0)
2319     new_y = 0;
2320   y_offset = -pspp_sheet_view_find_offset (tree_view, new_y, node);
2321
2322   if (*node < 0)
2323     return FALSE;
2324
2325   background_area->y = y_offset + y;
2326   background_area->height = ROW_HEIGHT (tree_view);
2327   background_area->x = 0;
2328
2329   /* Let the column have a chance at selecting it. */
2330   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2331   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
2332        list; list = (rtl ? list->prev : list->next))
2333     {
2334       PsppSheetViewColumn *candidate = list->data;
2335
2336       if (!candidate->visible)
2337         continue;
2338
2339       background_area->width = candidate->width;
2340       if ((background_area->x > x) ||
2341           (background_area->x + background_area->width <= x))
2342         {
2343           background_area->x += background_area->width;
2344           continue;
2345         }
2346
2347       /* we found the focus column */
2348
2349       pspp_sheet_view_adjust_cell_area (tree_view, candidate, background_area,
2350                                         TRUE, cell_area);
2351       *column = candidate;
2352       return TRUE;
2353     }
2354
2355   return FALSE;
2356 }
2357
2358 static gboolean
2359 pspp_sheet_view_button_press (GtkWidget      *widget,
2360                             GdkEventButton *event)
2361 {
2362   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2363   GList *list;
2364   PsppSheetViewColumn *column = NULL;
2365   gint i;
2366   GdkRectangle background_area;
2367   GdkRectangle cell_area;
2368   gboolean rtl;
2369
2370   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2371   pspp_sheet_view_stop_editing (tree_view, FALSE);
2372
2373
2374   /* Because grab_focus can cause reentrancy, we delay grab_focus until after
2375    * we're done handling the button press.
2376    */
2377
2378   if (event->window == tree_view->priv->bin_window)
2379     {
2380       int node;
2381       GtkTreePath *path;
2382       gint dval;
2383       gint pre_val, aft_val;
2384       PsppSheetViewColumn *column = NULL;
2385       GtkCellRenderer *focus_cell = NULL;
2386       gboolean row_double_click = FALSE;
2387
2388       /* Empty tree? */
2389       if (tree_view->priv->row_count == 0)
2390         {
2391           grab_focus_and_unset_draw_keyfocus (tree_view);
2392           return TRUE;
2393         }
2394
2395       if (!find_click (tree_view, event->x, event->y, &node, &column,
2396                        &background_area, &cell_area))
2397         {
2398           grab_focus_and_unset_draw_keyfocus (tree_view);
2399           return FALSE;
2400         }
2401
2402       tree_view->priv->focus_column = column;
2403
2404       if (pspp_sheet_view_row_head_clicked (tree_view, node, column, event))
2405         return TRUE;
2406
2407       /* select */
2408       pre_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
2409
2410       path = _pspp_sheet_view_find_path (tree_view, node);
2411
2412       /* we only handle selection modifications on the first button press
2413        */
2414       if (event->type == GDK_BUTTON_PRESS)
2415         {
2416           PsppSheetSelectionMode mode = 0;
2417
2418           if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2419             mode |= PSPP_SHEET_SELECT_MODE_TOGGLE;
2420           if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2421             mode |= PSPP_SHEET_SELECT_MODE_EXTEND;
2422
2423           focus_cell = _pspp_sheet_view_column_get_cell_at_pos (column, event->x - background_area.x);
2424           if (focus_cell)
2425             pspp_sheet_view_column_focus_cell (column, focus_cell);
2426
2427           if (event->state & GDK_CONTROL_MASK)
2428             {
2429               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, mode);
2430               pspp_sheet_view_real_toggle_cursor_row (tree_view);
2431             }
2432           else if (event->state & GDK_SHIFT_MASK)
2433             {
2434               pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, mode);
2435               pspp_sheet_view_real_select_cursor_row (tree_view, FALSE, mode);
2436             }
2437           else
2438             {
2439               pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, 0);
2440             }
2441
2442           if (tree_view->priv->anchor_column == NULL ||
2443               !(event->state & GDK_SHIFT_MASK))
2444             tree_view->priv->anchor_column = column;
2445           pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
2446           pspp_sheet_selection_select_column_range (tree_view->priv->selection,
2447                                                     tree_view->priv->anchor_column,
2448                                                     column);
2449         }
2450
2451       /* the treeview may have been scrolled because of _set_cursor,
2452        * correct here
2453        */
2454
2455       aft_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
2456       dval = pre_val - aft_val;
2457
2458       cell_area.y += dval;
2459       background_area.y += dval;
2460
2461       /* Save press to possibly begin a drag
2462        */
2463       if (!tree_view->priv->in_grab &&
2464           tree_view->priv->pressed_button < 0)
2465         {
2466           tree_view->priv->pressed_button = event->button;
2467           tree_view->priv->press_start_x = event->x;
2468           tree_view->priv->press_start_y = event->y;
2469           tree_view->priv->press_start_node = node;
2470
2471           if (tree_view->priv->rubber_banding_enable
2472               && (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_MULTIPLE ||
2473                   tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE))
2474             {
2475               tree_view->priv->press_start_y += tree_view->priv->dy;
2476               tree_view->priv->rubber_band_x = event->x;
2477               tree_view->priv->rubber_band_y = event->y + tree_view->priv->dy;
2478               tree_view->priv->rubber_band_status = RUBBER_BAND_MAYBE_START;
2479
2480               if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2481                 tree_view->priv->rubber_band_ctrl = TRUE;
2482               if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2483                 tree_view->priv->rubber_band_shift = TRUE;
2484
2485             }
2486         }
2487
2488       /* Test if a double click happened on the same row. */
2489       if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
2490         {
2491           int double_click_time, double_click_distance;
2492
2493           g_object_get (gtk_settings_get_for_screen (
2494                           gtk_widget_get_screen (widget)),
2495                         "gtk-double-click-time", &double_click_time,
2496                         "gtk-double-click-distance", &double_click_distance,
2497                         NULL);
2498
2499           /* Same conditions as _gdk_event_button_generate */
2500           if (tree_view->priv->last_button_x != -1 &&
2501               (event->time < tree_view->priv->last_button_time + double_click_time) &&
2502               (ABS (event->x - tree_view->priv->last_button_x) <= double_click_distance) &&
2503               (ABS (event->y - tree_view->priv->last_button_y) <= double_click_distance))
2504             {
2505               /* We do no longer compare paths of this row and the
2506                * row clicked previously.  We use the double click
2507                * distance to decide whether this is a valid click,
2508                * allowing the mouse to slightly move over another row.
2509                */
2510               row_double_click = TRUE;
2511
2512               tree_view->priv->last_button_time = 0;
2513               tree_view->priv->last_button_x = -1;
2514               tree_view->priv->last_button_y = -1;
2515             }
2516           else
2517             {
2518               tree_view->priv->last_button_time = event->time;
2519               tree_view->priv->last_button_x = event->x;
2520               tree_view->priv->last_button_y = event->y;
2521             }
2522         }
2523
2524       if (row_double_click)
2525         {
2526           gtk_grab_remove (widget);
2527           pspp_sheet_view_row_activated (tree_view, path, column);
2528
2529           if (tree_view->priv->pressed_button == event->button)
2530             tree_view->priv->pressed_button = -1;
2531         }
2532
2533       gtk_tree_path_free (path);
2534
2535       /* If we activated the row through a double click we don't want to grab
2536        * focus back, as moving focus to another widget is pretty common.
2537        */
2538       if (!row_double_click)
2539         grab_focus_and_unset_draw_keyfocus (tree_view);
2540
2541       return TRUE;
2542     }
2543
2544   /* We didn't click in the window.  Let's check to see if we clicked on a column resize window.
2545    */
2546   for (i = 0, list = tree_view->priv->columns; list; list = list->next, i++)
2547     {
2548       column = list->data;
2549       if (event->window == column->window &&
2550           column->resizable &&
2551           column->window)
2552         {
2553           gpointer drag_data;
2554
2555           if (gdk_pointer_grab (column->window, FALSE,
2556                                 GDK_POINTER_MOTION_HINT_MASK |
2557                                 GDK_BUTTON1_MOTION_MASK |
2558                                 GDK_BUTTON_RELEASE_MASK,
2559                                 NULL, NULL, event->time))
2560             return FALSE;
2561
2562           gtk_grab_add (widget);
2563           PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
2564           column->resized_width = column->width;
2565
2566           /* block attached dnd signal handler */
2567           drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2568           if (drag_data)
2569             g_signal_handlers_block_matched (widget,
2570                                              G_SIGNAL_MATCH_DATA,
2571                                              0, 0, NULL, NULL,
2572                                              drag_data);
2573
2574           tree_view->priv->drag_pos = i;
2575           tree_view->priv->x_drag = column->allocation.x + (rtl ? 0 : column->allocation.width);
2576
2577           if (!gtk_widget_has_focus (widget))
2578             gtk_widget_grab_focus (widget);
2579
2580           return TRUE;
2581         }
2582     }
2583   return FALSE;
2584 }
2585
2586 /* GtkWidget::button_release_event helper */
2587 static gboolean
2588 pspp_sheet_view_button_release_drag_column (GtkWidget      *widget,
2589                                           GdkEventButton *event)
2590 {
2591   PsppSheetView *tree_view;
2592   GList *l;
2593   gboolean rtl;
2594
2595   tree_view = PSPP_SHEET_VIEW (widget);
2596
2597   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2598   gdk_display_pointer_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2599   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2600
2601   /* Move the button back */
2602   g_return_val_if_fail (tree_view->priv->drag_column->button, FALSE);
2603
2604   g_object_ref (tree_view->priv->drag_column->button);
2605   gtk_container_remove (GTK_CONTAINER (tree_view), tree_view->priv->drag_column->button);
2606   gtk_widget_set_parent_window (tree_view->priv->drag_column->button, tree_view->priv->header_window);
2607   gtk_widget_set_parent (tree_view->priv->drag_column->button, GTK_WIDGET (tree_view));
2608   g_object_unref (tree_view->priv->drag_column->button);
2609   gtk_widget_queue_resize (widget);
2610   if (tree_view->priv->drag_column->resizable)
2611     {
2612       gdk_window_raise (tree_view->priv->drag_column->window);
2613       gdk_window_show (tree_view->priv->drag_column->window);
2614     }
2615   else
2616     gdk_window_hide (tree_view->priv->drag_column->window);
2617
2618   gtk_widget_grab_focus (tree_view->priv->drag_column->button);
2619
2620   if (rtl)
2621     {
2622       if (tree_view->priv->cur_reorder &&
2623           tree_view->priv->cur_reorder->right_column != tree_view->priv->drag_column)
2624         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2625                                          tree_view->priv->cur_reorder->right_column);
2626     }
2627   else
2628     {
2629       if (tree_view->priv->cur_reorder &&
2630           tree_view->priv->cur_reorder->left_column != tree_view->priv->drag_column)
2631         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2632                                          tree_view->priv->cur_reorder->left_column);
2633     }
2634   tree_view->priv->drag_column = NULL;
2635   gdk_window_hide (tree_view->priv->drag_window);
2636
2637   for (l = tree_view->priv->column_drag_info; l != NULL; l = l->next)
2638     g_slice_free (PsppSheetViewColumnReorder, l->data);
2639   g_list_free (tree_view->priv->column_drag_info);
2640   tree_view->priv->column_drag_info = NULL;
2641   tree_view->priv->cur_reorder = NULL;
2642
2643   if (tree_view->priv->drag_highlight_window)
2644     gdk_window_hide (tree_view->priv->drag_highlight_window);
2645
2646   /* Reset our flags */
2647   tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_UNSET;
2648   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
2649
2650   return TRUE;
2651 }
2652
2653 /* GtkWidget::button_release_event helper */
2654 static gboolean
2655 pspp_sheet_view_button_release_column_resize (GtkWidget      *widget,
2656                                             GdkEventButton *event)
2657 {
2658   PsppSheetView *tree_view;
2659   gpointer drag_data;
2660
2661   tree_view = PSPP_SHEET_VIEW (widget);
2662
2663   tree_view->priv->drag_pos = -1;
2664
2665   /* unblock attached dnd signal handler */
2666   drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2667   if (drag_data)
2668     g_signal_handlers_unblock_matched (widget,
2669                                        G_SIGNAL_MATCH_DATA,
2670                                        0, 0, NULL, NULL,
2671                                        drag_data);
2672
2673   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
2674   gtk_grab_remove (widget);
2675   gdk_display_pointer_ungrab (gdk_window_get_display (event->window),
2676                               event->time);
2677   return TRUE;
2678 }
2679
2680 static gboolean
2681 pspp_sheet_view_button_release_edit (PsppSheetView *tree_view,
2682                                      GdkEventButton *event)
2683 {
2684   GtkCellEditable *cell_editable;
2685   gchar *path_string;
2686   GtkTreePath *path;
2687   gint left, right;
2688   GtkTreeIter iter;
2689   PsppSheetViewColumn *column;
2690   GdkRectangle background_area;
2691   GdkRectangle cell_area;
2692   GdkRectangle area;
2693   guint modifiers;
2694   guint flags;
2695   int node;
2696
2697   if (event->window != tree_view->priv->bin_window)
2698     return FALSE;
2699
2700   /* Ignore a released button, if that button wasn't depressed */
2701   if (tree_view->priv->pressed_button != event->button)
2702     return FALSE;
2703
2704   if (!find_click (tree_view, event->x, event->y, &node, &column, &background_area,
2705                    &cell_area))
2706     return FALSE;
2707
2708   /* decide if we edit */
2709   path = _pspp_sheet_view_find_path (tree_view, node);
2710   modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
2711   if (event->button != 1 || modifiers)
2712     return FALSE;
2713
2714   gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
2715   pspp_sheet_view_column_cell_set_cell_data (column,
2716                                              tree_view->priv->model,
2717                                              &iter);
2718
2719   if (!pspp_sheet_view_column_get_quick_edit (column)
2720       && _pspp_sheet_view_column_has_editable_cell (column))
2721     return FALSE;
2722
2723   flags = 0;                    /* FIXME: get the right flags */
2724   path_string = gtk_tree_path_to_string (path);
2725
2726   if (!_pspp_sheet_view_column_cell_event (column,
2727                                            &cell_editable,
2728                                            (GdkEvent *)event,
2729                                            path_string,
2730                                            &background_area,
2731                                            &cell_area, flags))
2732     return FALSE;
2733
2734   if (cell_editable == NULL)
2735     return FALSE;
2736
2737   pspp_sheet_view_real_set_cursor (tree_view, path,
2738                                    TRUE, TRUE, 0); /* XXX mode? */
2739   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
2740
2741   area = cell_area;
2742   _pspp_sheet_view_column_get_neighbor_sizes (
2743     column, _pspp_sheet_view_column_get_edited_cell (column), &left, &right);
2744
2745   area.x += left;
2746   area.width -= right + left;
2747
2748   pspp_sheet_view_real_start_editing (tree_view,
2749                                       column,
2750                                       path,
2751                                       cell_editable,
2752                                       &area,
2753                                       (GdkEvent *)event,
2754                                       flags);
2755   g_free (path_string);
2756   gtk_tree_path_free (path);
2757   return TRUE;
2758 }
2759
2760 static gboolean
2761 pspp_sheet_view_button_release (GtkWidget      *widget,
2762                               GdkEventButton *event)
2763 {
2764   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2765
2766   pspp_sheet_view_stop_editing (tree_view, FALSE);
2767   if (tree_view->priv->rubber_band_status != RUBBER_BAND_ACTIVE
2768       && pspp_sheet_view_button_release_edit (tree_view, event))
2769     {
2770       if (tree_view->priv->pressed_button == event->button)
2771         tree_view->priv->pressed_button = -1;
2772
2773       tree_view->priv->rubber_band_status = RUBBER_BAND_OFF;
2774       return TRUE;
2775     }
2776
2777   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
2778     return pspp_sheet_view_button_release_drag_column (widget, event);
2779
2780   if (tree_view->priv->rubber_band_status)
2781     pspp_sheet_view_stop_rubber_band (tree_view);
2782
2783   if (tree_view->priv->pressed_button == event->button)
2784     tree_view->priv->pressed_button = -1;
2785
2786   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
2787     return pspp_sheet_view_button_release_column_resize (widget, event);
2788
2789   return FALSE;
2790 }
2791
2792 static gboolean
2793 pspp_sheet_view_grab_broken (GtkWidget          *widget,
2794                            GdkEventGrabBroken *event)
2795 {
2796   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2797
2798   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
2799     pspp_sheet_view_button_release_drag_column (widget, (GdkEventButton *)event);
2800
2801   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
2802     pspp_sheet_view_button_release_column_resize (widget, (GdkEventButton *)event);
2803
2804   return TRUE;
2805 }
2806
2807 /* GtkWidget::motion_event function set.
2808  */
2809
2810 static void
2811 do_prelight (PsppSheetView *tree_view,
2812              int node,
2813              /* these are in bin_window coords */
2814              gint         x,
2815              gint         y)
2816 {
2817   int prev_node = tree_view->priv->prelight_node;
2818
2819   if (prev_node != node)
2820     {
2821       tree_view->priv->prelight_node = node;
2822
2823       if (prev_node >= 0)
2824         _pspp_sheet_view_queue_draw_node (tree_view, prev_node, NULL);
2825
2826       if (node >= 0)
2827         _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
2828     }
2829 }
2830
2831
2832 static void
2833 prelight_or_select (PsppSheetView *tree_view,
2834                     int node,
2835                     /* these are in bin_window coords */
2836                     gint         x,
2837                     gint         y)
2838 {
2839   PsppSheetSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
2840   
2841   if (tree_view->priv->hover_selection &&
2842       (mode == PSPP_SHEET_SELECTION_SINGLE || mode == PSPP_SHEET_SELECTION_BROWSE) &&
2843       !(tree_view->priv->edited_column &&
2844         tree_view->priv->edited_column->editable_widget))
2845     {
2846       if (node >= 0)
2847         {
2848           if (!pspp_sheet_view_node_is_selected (tree_view, node))
2849             {
2850               GtkTreePath *path;
2851               
2852               path = _pspp_sheet_view_find_path (tree_view, node);
2853               pspp_sheet_selection_select_path (tree_view->priv->selection, path);
2854               if (pspp_sheet_view_node_is_selected (tree_view, node))
2855                 {
2856                   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
2857                   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, FALSE, 0); /* XXX mode? */
2858                 }
2859               gtk_tree_path_free (path);
2860             }
2861         }
2862
2863       else if (mode == PSPP_SHEET_SELECTION_SINGLE)
2864         pspp_sheet_selection_unselect_all (tree_view->priv->selection);
2865     }
2866
2867     do_prelight (tree_view, node, x, y);
2868 }
2869
2870 static void
2871 ensure_unprelighted (PsppSheetView *tree_view)
2872 {
2873   do_prelight (tree_view,
2874                -1,
2875                -1000, -1000); /* coords not possibly over an arrow */
2876
2877   g_assert (tree_view->priv->prelight_node < 0);
2878 }
2879
2880 static void
2881 update_prelight (PsppSheetView *tree_view,
2882                  gint         x,
2883                  gint         y)
2884 {
2885   int new_y;
2886   int node;
2887
2888   if (tree_view->priv->row_count == 0)
2889     return;
2890
2891   if (x == -10000)
2892     {
2893       ensure_unprelighted (tree_view);
2894       return;
2895     }
2896
2897   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y);
2898   if (new_y < 0)
2899     new_y = 0;
2900
2901   pspp_sheet_view_find_offset (tree_view, new_y, &node);
2902
2903   if (node >= 0)
2904     prelight_or_select (tree_view, node, x, y);
2905 }
2906
2907
2908
2909
2910 /* Our motion arrow is either a box (in the case of the original spot)
2911  * or an arrow.  It is expander_size wide.
2912  */
2913 /*
2914  * 11111111111111
2915  * 01111111111110
2916  * 00111111111100
2917  * 00011111111000
2918  * 00001111110000
2919  * 00000111100000
2920  * 00000111100000
2921  * 00000111100000
2922  * ~ ~ ~ ~ ~ ~ ~
2923  * 00000111100000
2924  * 00000111100000
2925  * 00000111100000
2926  * 00001111110000
2927  * 00011111111000
2928  * 00111111111100
2929  * 01111111111110
2930  * 11111111111111
2931  */
2932
2933 static void
2934 pspp_sheet_view_motion_draw_column_motion_arrow (PsppSheetView *tree_view)
2935 {
2936 #if GTK3_TRANSITION
2937   PsppSheetViewColumnReorder *reorder = tree_view->priv->cur_reorder;
2938   GtkWidget *widget = GTK_WIDGET (tree_view);
2939   GdkBitmap *mask = NULL;
2940   gint x;
2941   gint y;
2942   gint width;
2943   gint height;
2944   gint arrow_type = DRAG_COLUMN_WINDOW_STATE_UNSET;
2945   GdkWindowAttr attributes;
2946   guint attributes_mask;
2947
2948   if (!reorder ||
2949       reorder->left_column == tree_view->priv->drag_column ||
2950       reorder->right_column == tree_view->priv->drag_column)
2951     arrow_type = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
2952   else if (reorder->left_column || reorder->right_column)
2953     {
2954       GdkRectangle visible_rect;
2955       pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
2956       if (reorder->left_column)
2957         x = reorder->left_column->allocation.x + reorder->left_column->allocation.width;
2958       else
2959         x = reorder->right_column->allocation.x;
2960
2961       if (x < visible_rect.x)
2962         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT;
2963       else if (x > visible_rect.x + visible_rect.width)
2964         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT;
2965       else
2966         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW;
2967     }
2968
2969   /* We want to draw the rectangle over the initial location. */
2970   if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
2971     {
2972       GdkGC *gc;
2973       GdkColor col;
2974
2975       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
2976         {
2977           if (tree_view->priv->drag_highlight_window)
2978             {
2979               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
2980                                         NULL);
2981               gdk_window_destroy (tree_view->priv->drag_highlight_window);
2982             }
2983
2984           attributes.window_type = GDK_WINDOW_CHILD;
2985           attributes.wclass = GDK_INPUT_OUTPUT;
2986           attributes.x = tree_view->priv->drag_column_x;
2987           attributes.y = 0;
2988           width = attributes.width = tree_view->priv->drag_column->allocation.width;
2989           height = attributes.height = tree_view->priv->drag_column->allocation.height;
2990           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
2991           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
2992           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
2993           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2994           tree_view->priv->drag_highlight_window = gdk_window_new (tree_view->priv->header_window, &attributes, attributes_mask);
2995           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
2996
2997           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
2998           gc = gdk_gc_new (mask);
2999           col.pixel = 1;
3000           gdk_gc_set_foreground (gc, &col);
3001           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3002           col.pixel = 0;
3003           gdk_gc_set_foreground(gc, &col);
3004           gdk_draw_rectangle (mask, gc, TRUE, 2, 2, width - 4, height - 4);
3005           g_object_unref (gc);
3006
3007           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3008                                          mask, 0, 0);
3009           if (mask) g_object_unref (mask);
3010           tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
3011         }
3012     }
3013   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW)
3014     {
3015       gint i, j = 1;
3016       GdkGC *gc;
3017       GdkColor col;
3018
3019       width = tree_view->priv->expander_size;
3020
3021       /* Get x, y, width, height of arrow */
3022       gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
3023       if (reorder->left_column)
3024         {
3025           x += reorder->left_column->allocation.x + reorder->left_column->allocation.width - width/2;
3026           height = reorder->left_column->allocation.height;
3027         }
3028       else
3029         {
3030           x += reorder->right_column->allocation.x - width/2;
3031           height = reorder->right_column->allocation.height;
3032         }
3033       y -= tree_view->priv->expander_size/2; /* The arrow takes up only half the space */
3034       height += tree_view->priv->expander_size;
3035
3036       /* Create the new window */
3037       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW)
3038         {
3039           if (tree_view->priv->drag_highlight_window)
3040             {
3041               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3042                                         NULL);
3043               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3044             }
3045
3046           attributes.window_type = GDK_WINDOW_TEMP;
3047           attributes.wclass = GDK_INPUT_OUTPUT;
3048           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3049           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3050           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3051           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3052           attributes.x = x;
3053           attributes.y = y;
3054           attributes.width = width;
3055           attributes.height = height;
3056           tree_view->priv->drag_highlight_window = gdk_window_new (gtk_widget_get_root_window (widget),
3057                                                                    &attributes, attributes_mask);
3058           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3059
3060           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3061           gc = gdk_gc_new (mask);
3062           col.pixel = 1;
3063           gdk_gc_set_foreground (gc, &col);
3064           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3065
3066           /* Draw the 2 arrows as per above */
3067           col.pixel = 0;
3068           gdk_gc_set_foreground (gc, &col);
3069           for (i = 0; i < width; i ++)
3070             {
3071               if (i == (width/2 - 1))
3072                 continue;
3073               gdk_draw_line (mask, gc, i, j, i, height - j);
3074               if (i < (width/2 - 1))
3075                 j++;
3076               else
3077                 j--;
3078             }
3079           g_object_unref (gc);
3080           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3081                                          mask, 0, 0);
3082           if (mask) g_object_unref (mask);
3083         }
3084
3085       tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ARROW;
3086       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3087     }
3088   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT ||
3089            arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3090     {
3091       gint i, j = 1;
3092       GdkGC *gc;
3093       GdkColor col;
3094
3095       width = tree_view->priv->expander_size;
3096
3097       /* Get x, y, width, height of arrow */
3098       width = width/2; /* remember, the arrow only takes half the available width */
3099       gdk_window_get_origin (gtk_widget_get_window (widget), &x, &y);
3100       if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3101         x += widget->allocation.width - width;
3102
3103       if (reorder->left_column)
3104         height = reorder->left_column->allocation.height;
3105       else
3106         height = reorder->right_column->allocation.height;
3107
3108       y -= tree_view->priv->expander_size;
3109       height += 2*tree_view->priv->expander_size;
3110
3111       /* Create the new window */
3112       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT &&
3113           tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3114         {
3115           if (tree_view->priv->drag_highlight_window)
3116             {
3117               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3118                                         NULL);
3119               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3120             }
3121
3122           attributes.window_type = GDK_WINDOW_TEMP;
3123           attributes.wclass = GDK_INPUT_OUTPUT;
3124           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3125           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3126           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3127           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3128           attributes.x = x;
3129           attributes.y = y;
3130           attributes.width = width;
3131           attributes.height = height;
3132           tree_view->priv->drag_highlight_window = gdk_window_new (NULL, &attributes, attributes_mask);
3133           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3134
3135           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3136           gc = gdk_gc_new (mask);
3137           col.pixel = 1;
3138           gdk_gc_set_foreground (gc, &col);
3139           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3140
3141           /* Draw the 2 arrows as per above */
3142           col.pixel = 0;
3143           gdk_gc_set_foreground (gc, &col);
3144           j = tree_view->priv->expander_size;
3145           for (i = 0; i < width; i ++)
3146             {
3147               gint k;
3148               if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT)
3149                 k = width - i - 1;
3150               else
3151                 k = i;
3152               gdk_draw_line (mask, gc, k, j, k, height - j);
3153               gdk_draw_line (mask, gc, k, 0, k, tree_view->priv->expander_size - j);
3154               gdk_draw_line (mask, gc, k, height, k, height - tree_view->priv->expander_size + j);
3155               j--;
3156             }
3157           g_object_unref (gc);
3158           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3159                                          mask, 0, 0);
3160           if (mask) g_object_unref (mask);
3161         }
3162
3163       tree_view->priv->drag_column_window_state = arrow_type;
3164       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3165    }
3166   else
3167     {
3168       g_warning (G_STRLOC"Invalid PsppSheetViewColumnReorder struct");
3169       gdk_window_hide (tree_view->priv->drag_highlight_window);
3170       return;
3171     }
3172
3173   gdk_window_show (tree_view->priv->drag_highlight_window);
3174   gdk_window_raise (tree_view->priv->drag_highlight_window);
3175 #endif
3176 }
3177
3178 static gboolean
3179 pspp_sheet_view_motion_resize_column (GtkWidget      *widget,
3180                                     GdkEventMotion *event)
3181 {
3182   gint x;
3183   gint new_width;
3184   PsppSheetViewColumn *column;
3185   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3186
3187   column = pspp_sheet_view_get_column (tree_view, tree_view->priv->drag_pos);
3188
3189   if (event->is_hint || event->window != gtk_widget_get_window (widget))
3190     gtk_widget_get_pointer (widget, &x, NULL);
3191   else
3192     x = event->x;
3193
3194   if (tree_view->priv->hadjustment)
3195     x += gtk_adjustment_get_value (tree_view->priv->hadjustment);
3196
3197   new_width = pspp_sheet_view_new_column_width (tree_view,
3198                                               tree_view->priv->drag_pos, &x);
3199   if (x != tree_view->priv->x_drag &&
3200       (new_width != column->fixed_width))
3201     {
3202       column->use_resized_width = TRUE;
3203       column->resized_width = new_width;
3204 #if 0
3205       if (column->expand)
3206         column->resized_width -= tree_view->priv->last_extra_space_per_column;
3207 #endif
3208       gtk_widget_queue_resize (widget);
3209     }
3210
3211   return FALSE;
3212 }
3213
3214
3215 static void
3216 pspp_sheet_view_update_current_reorder (PsppSheetView *tree_view)
3217 {
3218   PsppSheetViewColumnReorder *reorder = NULL;
3219   GList *list;
3220   gint mouse_x;
3221
3222   gdk_window_get_pointer (tree_view->priv->header_window, &mouse_x, NULL, NULL);
3223   for (list = tree_view->priv->column_drag_info; list; list = list->next)
3224     {
3225       reorder = (PsppSheetViewColumnReorder *) list->data;
3226       if (mouse_x >= reorder->left_align && mouse_x < reorder->right_align)
3227         break;
3228       reorder = NULL;
3229     }
3230
3231   /*  if (reorder && reorder == tree_view->priv->cur_reorder)
3232       return;*/
3233
3234   tree_view->priv->cur_reorder = reorder;
3235   pspp_sheet_view_motion_draw_column_motion_arrow (tree_view);
3236 }
3237
3238 static void
3239 pspp_sheet_view_vertical_autoscroll (PsppSheetView *tree_view)
3240 {
3241   GdkRectangle visible_rect;
3242   gint y;
3243   gint offset;
3244   gfloat value;
3245
3246   gdk_window_get_pointer (tree_view->priv->bin_window, NULL, &y, NULL);
3247   y += tree_view->priv->dy;
3248
3249   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3250
3251   /* see if we are near the edge. */
3252   offset = y - (visible_rect.y + 2 * SCROLL_EDGE_SIZE);
3253   if (offset > 0)
3254     {
3255       offset = y - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE);
3256       if (offset < 0)
3257         return;
3258     }
3259
3260   value = CLAMP (gtk_adjustment_get_value (tree_view->priv->vadjustment) + offset, 0.0,
3261                  gtk_adjustment_get_upper (tree_view->priv->vadjustment) - gtk_adjustment_get_page_size (tree_view->priv->vadjustment));
3262   gtk_adjustment_set_value (tree_view->priv->vadjustment, value);
3263 }
3264
3265 static gboolean
3266 pspp_sheet_view_horizontal_autoscroll (PsppSheetView *tree_view)
3267 {
3268   GdkRectangle visible_rect;
3269   gint x;
3270   gint offset;
3271   gfloat value;
3272
3273   gdk_window_get_pointer (tree_view->priv->bin_window, &x, NULL, NULL);
3274
3275   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3276
3277   /* See if we are near the edge. */
3278   offset = x - (visible_rect.x + SCROLL_EDGE_SIZE);
3279   if (offset > 0)
3280     {
3281       offset = x - (visible_rect.x + visible_rect.width - SCROLL_EDGE_SIZE);
3282       if (offset < 0)
3283         return TRUE;
3284     }
3285   offset = offset/3;
3286
3287   value = CLAMP (gtk_adjustment_get_value (tree_view->priv->hadjustment) + offset,
3288                  0.0, gtk_adjustment_get_upper (tree_view->priv->hadjustment) - gtk_adjustment_get_page_size (tree_view->priv->hadjustment));
3289   gtk_adjustment_set_value (tree_view->priv->hadjustment, value);
3290
3291   return TRUE;
3292
3293 }
3294
3295 static gboolean
3296 pspp_sheet_view_motion_drag_column (GtkWidget      *widget,
3297                                   GdkEventMotion *event)
3298 {
3299   PsppSheetView *tree_view = (PsppSheetView *) widget;
3300   PsppSheetViewColumn *column = tree_view->priv->drag_column;
3301   gint x, y;
3302   GtkAllocation allocation;
3303
3304   /* Sanity Check */
3305   if ((column == NULL) ||
3306       (event->window != tree_view->priv->drag_window))
3307     return FALSE;
3308
3309   /* Handle moving the header */
3310   gdk_window_get_position (tree_view->priv->drag_window, &x, &y);
3311   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
3312   x = CLAMP (x + (gint)event->x - column->drag_x, 0,
3313              MAX (tree_view->priv->width, allocation.width) - column->allocation.width);
3314   gdk_window_move (tree_view->priv->drag_window, x, y);
3315   
3316   /* autoscroll, if needed */
3317   pspp_sheet_view_horizontal_autoscroll (tree_view);
3318   /* Update the current reorder position and arrow; */
3319   pspp_sheet_view_update_current_reorder (tree_view);
3320
3321   return TRUE;
3322 }
3323
3324 static void
3325 pspp_sheet_view_stop_rubber_band (PsppSheetView *tree_view)
3326 {
3327   remove_scroll_timeout (tree_view);
3328   gtk_grab_remove (GTK_WIDGET (tree_view));
3329
3330   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
3331     {
3332       GtkTreePath *tmp_path;
3333
3334       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
3335
3336       /* The anchor path should be set to the start path */
3337       tmp_path = _pspp_sheet_view_find_path (tree_view,
3338                                            tree_view->priv->rubber_band_start_node);
3339
3340       if (tree_view->priv->anchor)
3341         gtk_tree_row_reference_free (tree_view->priv->anchor);
3342
3343       tree_view->priv->anchor =
3344         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
3345                                           tree_view->priv->model,
3346                                           tmp_path);
3347
3348       gtk_tree_path_free (tmp_path);
3349
3350       /* ... and the cursor to the end path */
3351       tmp_path = _pspp_sheet_view_find_path (tree_view,
3352                                            tree_view->priv->rubber_band_end_node);
3353       pspp_sheet_view_real_set_cursor (PSPP_SHEET_VIEW (tree_view), tmp_path, FALSE, FALSE, 0); /* XXX mode? */
3354       gtk_tree_path_free (tmp_path);
3355
3356       _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
3357     }
3358
3359   /* Clear status variables */
3360   tree_view->priv->rubber_band_status = RUBBER_BAND_OFF;
3361   tree_view->priv->rubber_band_shift = 0;
3362   tree_view->priv->rubber_band_ctrl = 0;
3363
3364   tree_view->priv->rubber_band_start_node = -1;
3365   tree_view->priv->rubber_band_end_node = -1;
3366 }
3367
3368 static void
3369 pspp_sheet_view_update_rubber_band_selection_range (PsppSheetView *tree_view,
3370                                                  int start_node,
3371                                                  int end_node,
3372                                                  gboolean     select,
3373                                                  gboolean     skip_start,
3374                                                  gboolean     skip_end)
3375 {
3376   if (start_node == end_node)
3377     return;
3378
3379   /* We skip the first node and jump inside the loop */
3380   if (skip_start)
3381     goto skip_first;
3382
3383   do
3384     {
3385       /* Small optimization by assuming insensitive nodes are never
3386        * selected.
3387        */
3388       if (select)
3389         {
3390           if (tree_view->priv->rubber_band_shift)
3391             pspp_sheet_view_node_select (tree_view, start_node);
3392           else if (tree_view->priv->rubber_band_ctrl)
3393             {
3394               /* Toggle the selection state */
3395               if (pspp_sheet_view_node_is_selected (tree_view, start_node))
3396                 pspp_sheet_view_node_unselect (tree_view, start_node);
3397               else
3398                 pspp_sheet_view_node_select (tree_view, start_node);
3399             }
3400           else
3401             pspp_sheet_view_node_select (tree_view, start_node);
3402         }
3403       else
3404         {
3405           /* Mirror the above */
3406           if (tree_view->priv->rubber_band_shift)
3407                 pspp_sheet_view_node_unselect (tree_view, start_node);
3408           else if (tree_view->priv->rubber_band_ctrl)
3409             {
3410               /* Toggle the selection state */
3411               if (pspp_sheet_view_node_is_selected (tree_view, start_node))
3412                 pspp_sheet_view_node_unselect (tree_view, start_node);
3413               else
3414                 pspp_sheet_view_node_select (tree_view, start_node);
3415             }
3416           else
3417             pspp_sheet_view_node_unselect (tree_view, start_node);
3418         }
3419
3420       _pspp_sheet_view_queue_draw_node (tree_view, start_node, NULL);
3421
3422       if (start_node == end_node)
3423         break;
3424
3425 skip_first:
3426
3427       start_node = pspp_sheet_view_node_next (tree_view, start_node);
3428
3429       if (start_node < 0)
3430         /* Ran out of tree */
3431         break;
3432
3433       if (skip_end && start_node == end_node)
3434         break;
3435     }
3436   while (TRUE);
3437 }
3438
3439 static gint
3440 pspp_sheet_view_node_find_offset (PsppSheetView *tree_view,
3441                                   int node)
3442 {
3443   return node * tree_view->priv->fixed_height;
3444 }
3445
3446 static gint
3447 pspp_sheet_view_find_offset (PsppSheetView *tree_view,
3448                              gint height,
3449                              int *new_node)
3450 {
3451   int fixed_height = tree_view->priv->fixed_height;
3452   if (fixed_height <= 0
3453       || height < 0
3454       || height >= tree_view->priv->row_count * fixed_height)
3455     {
3456       *new_node = -1;
3457       return 0;
3458     }
3459   else
3460     {
3461       *new_node = height / fixed_height;
3462       return height % fixed_height;
3463     }
3464 }
3465
3466 static void
3467 pspp_sheet_view_update_rubber_band_selection (PsppSheetView *tree_view)
3468 {
3469   int start_node;
3470   int end_node;
3471
3472   pspp_sheet_view_find_offset (tree_view, MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &start_node);
3473   pspp_sheet_view_find_offset (tree_view, MAX (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &end_node);
3474
3475   /* Handle the start area first */
3476   if (tree_view->priv->rubber_band_start_node < 0)
3477     {
3478       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3479                                                        start_node,
3480                                                        end_node,
3481                                                        TRUE,
3482                                                        FALSE,
3483                                                        FALSE);
3484     }
3485   else if (start_node < tree_view->priv->rubber_band_start_node)
3486     {
3487       /* New node is above the old one; selection became bigger */
3488       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3489                                                        start_node,
3490                                                        tree_view->priv->rubber_band_start_node,
3491                                                        TRUE,
3492                                                        FALSE,
3493                                                        TRUE);
3494     }
3495   else if (start_node > tree_view->priv->rubber_band_start_node)
3496     {
3497       /* New node is below the old one; selection became smaller */
3498       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3499                                                        tree_view->priv->rubber_band_start_node,
3500                                                        start_node,
3501                                                        FALSE,
3502                                                        FALSE,
3503                                                        TRUE);
3504     }
3505
3506   tree_view->priv->rubber_band_start_node = start_node;
3507
3508   /* Next, handle the end area */
3509   if (tree_view->priv->rubber_band_end_node < 0)
3510     {
3511       /* In the event this happens, start_node was also -1; this case is
3512        * handled above.
3513        */
3514     }
3515   else if (end_node < 0)
3516     {
3517       /* Find the last node in the tree */
3518       pspp_sheet_view_find_offset (tree_view, tree_view->priv->height - 1,
3519                                &end_node);
3520
3521       /* Selection reached end of the tree */
3522       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3523                                                        tree_view->priv->rubber_band_end_node,
3524                                                        end_node,
3525                                                        TRUE,
3526                                                        TRUE,
3527                                                        FALSE);
3528     }
3529   else if (end_node > tree_view->priv->rubber_band_end_node)
3530     {
3531       /* New node is below the old one; selection became bigger */
3532       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3533                                                        tree_view->priv->rubber_band_end_node,
3534                                                        end_node,
3535                                                        TRUE,
3536                                                        TRUE,
3537                                                        FALSE);
3538     }
3539   else if (end_node < tree_view->priv->rubber_band_end_node)
3540     {
3541       /* New node is above the old one; selection became smaller */
3542       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3543                                                        end_node,
3544                                                        tree_view->priv->rubber_band_end_node,
3545                                                        FALSE,
3546                                                        TRUE,
3547                                                        FALSE);
3548     }
3549
3550   tree_view->priv->rubber_band_end_node = end_node;
3551 }
3552
3553 #define GDK_RECTANGLE_PTR(X) ((GdkRectangle *)(X))
3554
3555 static void
3556 pspp_sheet_view_update_rubber_band (PsppSheetView *tree_view)
3557 {
3558   gint x, y;
3559   cairo_rectangle_int_t old_area;
3560   cairo_rectangle_int_t new_area;
3561   cairo_rectangle_int_t common;
3562   cairo_region_t *invalid_region;
3563   PsppSheetViewColumn *column;
3564
3565   old_area.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
3566   old_area.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
3567   old_area.width = ABS (tree_view->priv->rubber_band_x - tree_view->priv->press_start_x) + 1;
3568   old_area.height = ABS (tree_view->priv->rubber_band_y - tree_view->priv->press_start_y) + 1;
3569
3570   gdk_window_get_pointer (tree_view->priv->bin_window, &x, &y, NULL);
3571
3572   x = MAX (x, 0);
3573   y = MAX (y, 0) + tree_view->priv->dy;
3574
3575   new_area.x = MIN (tree_view->priv->press_start_x, x);
3576   new_area.y = MIN (tree_view->priv->press_start_y, y) - tree_view->priv->dy;
3577   new_area.width = ABS (x - tree_view->priv->press_start_x) + 1;
3578   new_area.height = ABS (y - tree_view->priv->press_start_y) + 1;
3579
3580   invalid_region = cairo_region_create_rectangle (&old_area);
3581   cairo_region_union_rectangle (invalid_region, &new_area);
3582
3583   gdk_rectangle_intersect (GDK_RECTANGLE_PTR (&old_area), 
3584                            GDK_RECTANGLE_PTR (&new_area), GDK_RECTANGLE_PTR (&common));
3585   if (common.width > 2 && common.height > 2)
3586     {
3587       cairo_region_t *common_region;
3588
3589       /* make sure the border is invalidated */
3590       common.x += 1;
3591       common.y += 1;
3592       common.width -= 2;
3593       common.height -= 2;
3594
3595       common_region = cairo_region_create_rectangle (&common);
3596
3597       cairo_region_subtract (invalid_region, common_region);
3598       cairo_region_destroy (common_region);
3599     }
3600
3601 #if GTK_MAJOR_VERSION == 3
3602   gdk_window_invalidate_region (tree_view->priv->bin_window, invalid_region, TRUE);  
3603 #else
3604   {
3605     cairo_rectangle_int_t extents;
3606     GdkRegion *ereg;
3607     cairo_region_get_extents (invalid_region, &extents);
3608     ereg = gdk_region_rectangle (GDK_RECTANGLE_PTR (&extents));
3609     gdk_window_invalidate_region (tree_view->priv->bin_window, ereg, TRUE);
3610     gdk_region_destroy (ereg);
3611   }
3612 #endif
3613
3614   cairo_region_destroy (invalid_region);
3615
3616   tree_view->priv->rubber_band_x = x;
3617   tree_view->priv->rubber_band_y = y;
3618   pspp_sheet_view_get_path_at_pos (tree_view, x, y, NULL, &column, NULL, NULL);
3619
3620   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
3621   pspp_sheet_selection_select_column_range (tree_view->priv->selection,
3622                                             tree_view->priv->anchor_column,
3623                                             column);
3624
3625   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
3626
3627   pspp_sheet_view_update_rubber_band_selection (tree_view);
3628 }
3629
3630 #if GTK3_TRANSITION
3631 static void
3632 pspp_sheet_view_paint_rubber_band (PsppSheetView  *tree_view,
3633                                 GdkRectangle *area)
3634 {
3635   cairo_t *cr;
3636   GdkRectangle rect;
3637   GdkRectangle rubber_rect;
3638   GtkStyle *style;
3639
3640   return;
3641   rubber_rect.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
3642   rubber_rect.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
3643   rubber_rect.width = ABS (tree_view->priv->press_start_x - tree_view->priv->rubber_band_x) + 1;
3644   rubber_rect.height = ABS (tree_view->priv->press_start_y - tree_view->priv->rubber_band_y) + 1;
3645
3646   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
3647     return;
3648
3649   cr = gdk_cairo_create (tree_view->priv->bin_window);
3650   cairo_set_line_width (cr, 1.0);
3651
3652   style = gtk_widget_get_style (GTK_WIDGET (tree_view));
3653   cairo_set_source_rgba (cr,
3654                          style->fg[GTK_STATE_NORMAL].red / 65535.,
3655                          style->fg[GTK_STATE_NORMAL].green / 65535.,
3656                          style->fg[GTK_STATE_NORMAL].blue / 65535.,
3657                          .25);
3658
3659   gdk_cairo_rectangle (cr, &rect);
3660   cairo_clip (cr);
3661   cairo_paint (cr);
3662
3663   cairo_set_source_rgb (cr,
3664                         style->fg[GTK_STATE_NORMAL].red / 65535.,
3665                         style->fg[GTK_STATE_NORMAL].green / 65535.,
3666                         style->fg[GTK_STATE_NORMAL].blue / 65535.);
3667
3668   cairo_rectangle (cr,
3669                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
3670                    rubber_rect.width - 1, rubber_rect.height - 1);
3671   cairo_stroke (cr);
3672
3673   cairo_destroy (cr);
3674 }
3675 #endif
3676
3677
3678 static gboolean
3679 pspp_sheet_view_motion_bin_window (GtkWidget      *widget,
3680                                  GdkEventMotion *event)
3681 {
3682   PsppSheetView *tree_view;
3683   int node;
3684   gint new_y;
3685
3686   tree_view = (PsppSheetView *) widget;
3687
3688   if (tree_view->priv->row_count == 0)
3689     return FALSE;
3690
3691   if (tree_view->priv->rubber_band_status == RUBBER_BAND_MAYBE_START)
3692     {
3693       GdkRectangle background_area, cell_area;
3694       PsppSheetViewColumn *column;
3695
3696       if (find_click (tree_view, event->x, event->y, &node, &column,
3697                       &background_area, &cell_area)
3698           && tree_view->priv->focus_column == column
3699           && tree_view->priv->press_start_node == node)
3700         return FALSE;
3701
3702       gtk_grab_add (GTK_WIDGET (tree_view));
3703       pspp_sheet_view_update_rubber_band (tree_view);
3704
3705       tree_view->priv->rubber_band_status = RUBBER_BAND_ACTIVE;
3706     }
3707   else if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
3708     {
3709       pspp_sheet_view_update_rubber_band (tree_view);
3710
3711       add_scroll_timeout (tree_view);
3712     }
3713
3714   /* only check for an initiated drag when a button is pressed */
3715   if (tree_view->priv->pressed_button >= 0
3716       && !tree_view->priv->rubber_band_status)
3717     pspp_sheet_view_maybe_begin_dragging_row (tree_view, event);
3718
3719   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
3720   if (new_y < 0)
3721     new_y = 0;
3722
3723   pspp_sheet_view_find_offset (tree_view, new_y, &node);
3724
3725   tree_view->priv->event_last_x = event->x;
3726   tree_view->priv->event_last_y = event->y;
3727
3728   prelight_or_select (tree_view, node, event->x, event->y);
3729
3730   return TRUE;
3731 }
3732
3733 static gboolean
3734 pspp_sheet_view_motion (GtkWidget      *widget,
3735                       GdkEventMotion *event)
3736 {
3737   PsppSheetView *tree_view;
3738
3739   tree_view = (PsppSheetView *) widget;
3740
3741   /* Resizing a column */
3742   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
3743     return pspp_sheet_view_motion_resize_column (widget, event);
3744
3745   /* Drag column */
3746   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
3747     return pspp_sheet_view_motion_drag_column (widget, event);
3748
3749   /* Sanity check it */
3750   if (event->window == tree_view->priv->bin_window)
3751     return pspp_sheet_view_motion_bin_window (widget, event);
3752
3753   return FALSE;
3754 }
3755
3756 /* Invalidate the focus rectangle near the edge of the bin_window; used when
3757  * the tree is empty.
3758  */
3759 static void
3760 invalidate_empty_focus (PsppSheetView *tree_view)
3761 {
3762   GdkRectangle area;
3763
3764   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
3765     return;
3766
3767   area.x = 0;
3768   area.y = 0;
3769   area.width = gdk_window_get_width (tree_view->priv->bin_window);
3770   area.height = gdk_window_get_height (tree_view->priv->bin_window);
3771   gdk_window_invalidate_rect (tree_view->priv->bin_window, &area, FALSE);
3772 }
3773
3774 /* Draws a focus rectangle near the edge of the bin_window; used when the tree
3775  * is empty.
3776  */
3777 static void
3778 draw_empty_focus (PsppSheetView *tree_view)
3779 {
3780   GtkWidget *widget = GTK_WIDGET (tree_view);
3781   gint w, h;
3782   cairo_t *cr = gdk_cairo_create (tree_view->priv->bin_window);
3783
3784   if (!gtk_widget_has_focus (widget))
3785     return;
3786
3787   w = gdk_window_get_width (tree_view->priv->bin_window);
3788   h = gdk_window_get_height (tree_view->priv->bin_window);
3789
3790   w -= 2;
3791   h -= 2;
3792
3793   if (w > 0 && h > 0)
3794     gtk_paint_focus (gtk_widget_get_style (widget),
3795                      cr,
3796                      gtk_widget_get_state (widget),
3797                      widget,
3798                      NULL,
3799                      1, 1, w, h);
3800   cairo_destroy (cr);
3801 }
3802
3803 static void
3804 pspp_sheet_view_draw_vertical_grid_lines (PsppSheetView    *tree_view,
3805                                           cairo_t *cr,
3806                                           gint n_visible_columns,
3807                                           gint min_y,
3808                                           gint max_y)
3809 {
3810   GList *list = tree_view->priv->columns;
3811   gint i = 0;
3812   gint current_x = 0;
3813
3814   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
3815       && tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_BOTH)
3816     return;
3817
3818   /* Only draw the lines for visible rows and columns */
3819   for (list = tree_view->priv->columns; list; list = list->next, i++)
3820     {
3821       PsppSheetViewColumn *column = list->data;
3822
3823       /* We don't want a line for the last column */
3824       if (i == n_visible_columns - 1)
3825         break;
3826
3827       if (! column->visible)
3828         continue;
3829
3830       current_x += column->width;
3831
3832       cairo_set_line_width (cr, 1.0);
3833       cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
3834       cairo_move_to (cr, current_x - 0.5, min_y);
3835       cairo_line_to (cr, current_x - 0.5 , max_y - min_y);
3836       
3837       cairo_stroke (cr);
3838     }
3839 }
3840
3841 /* Warning: Very scary function.
3842  * Modify at your own risk
3843  *
3844  * KEEP IN SYNC WITH pspp_sheet_view_create_row_drag_icon()!
3845  * FIXME: It's not...
3846  */
3847 static gboolean
3848 pspp_sheet_view_bin_expose (GtkWidget      *widget,
3849                             cairo_t *cr)
3850 {
3851   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3852   GtkTreePath *path;
3853   GList *list;
3854   int node;
3855   int cursor = -1;
3856   int drag_highlight = -1;
3857   GtkTreeIter iter;
3858   gint new_y;
3859   gint y_offset, cell_offset;
3860   gint max_height;
3861   GdkRectangle background_area;
3862   GdkRectangle cell_area;
3863   guint flags;
3864   gint bin_window_width;
3865   gint bin_window_height;
3866   GtkTreePath *cursor_path;
3867   GtkTreePath *drag_dest_path;
3868   GList *first_column, *last_column;
3869   gint vertical_separator;
3870   gint horizontal_separator;
3871   gint focus_line_width;
3872   gboolean allow_rules;
3873   gboolean has_special_cell;
3874   gboolean rtl;
3875   gint n_visible_columns;
3876   gint grid_line_width;
3877   gboolean row_ending_details;
3878   gboolean draw_vgrid_lines, draw_hgrid_lines;
3879   gint min_y, max_y;
3880
3881   cairo_t *bwcr = gdk_cairo_create (tree_view->priv->bin_window);
3882   GdkRectangle Zarea;
3883   GtkAllocation allocation;
3884   gtk_widget_get_allocation (widget, &allocation);
3885
3886   Zarea.x =      0;
3887   Zarea.y =      0;
3888   Zarea.height = allocation.height;
3889
3890   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3891
3892   gtk_widget_style_get (widget,
3893                         "horizontal-separator", &horizontal_separator,
3894                         "vertical-separator", &vertical_separator,
3895                         "allow-rules", &allow_rules,
3896                         "focus-line-width", &focus_line_width,
3897                         "row-ending-details", &row_ending_details,
3898                         NULL);
3899
3900   if (tree_view->priv->row_count == 0)
3901     {
3902       draw_empty_focus (tree_view);
3903       return TRUE;
3904     }
3905
3906 #if GTK3_TRANSITION
3907   /* clip event->area to the visible area */
3908   if (Zarea.height < 0.5)
3909     return TRUE;
3910 #endif
3911
3912   validate_visible_area (tree_view);
3913
3914   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, Zarea.y);
3915
3916   if (new_y < 0)
3917     new_y = 0;
3918   y_offset = -pspp_sheet_view_find_offset (tree_view, new_y, &node);
3919   bin_window_width = 
3920     gdk_window_get_width (tree_view->priv->bin_window);
3921
3922   bin_window_height = 
3923     gdk_window_get_height (tree_view->priv->bin_window);
3924
3925
3926   if (tree_view->priv->height < bin_window_height)
3927     {
3928       gtk_paint_flat_box (gtk_widget_get_style (widget),
3929                           cr,
3930                           gtk_widget_get_state (widget),
3931                           GTK_SHADOW_NONE,
3932                           widget,
3933                           "cell_even",
3934                           0, tree_view->priv->height,
3935                           bin_window_width,
3936                           bin_window_height - tree_view->priv->height);
3937     }
3938
3939   if (node < 0)
3940     return TRUE;
3941
3942   /* find the path for the node */
3943   path = _pspp_sheet_view_find_path ((PsppSheetView *)widget, node);
3944   gtk_tree_model_get_iter (tree_view->priv->model,
3945                            &iter,
3946                            path);
3947   gtk_tree_path_free (path);
3948   
3949   cursor_path = NULL;
3950   drag_dest_path = NULL;
3951
3952   if (tree_view->priv->cursor)
3953     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
3954
3955   if (cursor_path)
3956     _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor);
3957
3958   if (tree_view->priv->drag_dest_row)
3959     drag_dest_path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
3960
3961   if (drag_dest_path)
3962     _pspp_sheet_view_find_node (tree_view, drag_dest_path,
3963                                 &drag_highlight);
3964
3965   draw_vgrid_lines =
3966     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
3967     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3968   draw_hgrid_lines =
3969     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
3970     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
3971
3972   if (draw_vgrid_lines || draw_hgrid_lines)
3973     gtk_widget_style_get (widget, "grid-line-width", &grid_line_width, NULL);
3974   
3975   n_visible_columns = 0;
3976   for (list = tree_view->priv->columns; list; list = list->next)
3977     {
3978       if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
3979         continue;
3980       n_visible_columns ++;
3981     }
3982
3983   /* Find the last column */
3984   for (last_column = g_list_last (tree_view->priv->columns);
3985        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
3986        last_column = last_column->prev)
3987     ;
3988
3989   /* and the first */
3990   for (first_column = g_list_first (tree_view->priv->columns);
3991        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
3992        first_column = first_column->next)
3993     ;
3994
3995   /* Actually process the expose event.  To do this, we want to
3996    * start at the first node of the event, and walk the tree in
3997    * order, drawing each successive node.
3998    */
3999
4000   min_y = y_offset;
4001   do
4002     {
4003       gboolean parity;
4004       gboolean is_first = FALSE;
4005       gboolean is_last = FALSE;
4006       gboolean done = FALSE;
4007       gboolean selected;
4008
4009       max_height = ROW_HEIGHT (tree_view);
4010
4011       cell_offset = 0;
4012
4013       background_area.y = y_offset + Zarea.y;
4014       background_area.height = max_height;
4015       max_y = background_area.y + max_height;
4016
4017       flags = 0;
4018
4019       if (node == tree_view->priv->prelight_node)
4020         flags |= GTK_CELL_RENDERER_PRELIT;
4021
4022       selected = pspp_sheet_view_node_is_selected (tree_view, node);
4023
4024       parity = node % 2;
4025
4026       if (tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT)
4027         {
4028           /* we *need* to set cell data on all cells before the call
4029            * to _has_special_cell, else _has_special_cell() does not
4030            * return a correct value.
4031            */
4032           for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4033                list;
4034                list = (rtl ? list->prev : list->next))
4035             {
4036               PsppSheetViewColumn *column = list->data;
4037               pspp_sheet_view_column_cell_set_cell_data (column,
4038                                                          tree_view->priv->model,
4039                                                          &iter);
4040             }
4041
4042           has_special_cell = pspp_sheet_view_has_special_cell (tree_view);
4043         }
4044       else
4045         has_special_cell = tree_view->priv->special_cells == PSPP_SHEET_VIEW_SPECIAL_CELLS_YES;
4046
4047       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4048            list;
4049            list = (rtl ? list->prev : list->next))
4050         {
4051           PsppSheetViewColumn *column = list->data;
4052           const gchar *detail = NULL;
4053           gboolean selected_column;
4054           GtkStateType state;
4055
4056           if (!column->visible)
4057             continue;
4058
4059           if (tree_view->priv->selection->type == PSPP_SHEET_SELECTION_RECTANGLE)
4060             selected_column = column->selected && column->selectable;
4061           else
4062             selected_column = TRUE;
4063
4064 #if GTK3_TRANSITION
4065           if (cell_offset > Zarea.x + Zarea.width ||
4066               cell_offset + column->width < Zarea.x)
4067             {
4068               cell_offset += column->width;
4069               continue;
4070             }
4071 #endif
4072
4073           if (selected && selected_column)
4074             flags |= GTK_CELL_RENDERER_SELECTED;
4075           else
4076             flags &= ~GTK_CELL_RENDERER_SELECTED;
4077
4078           if (column->show_sort_indicator)
4079             flags |= GTK_CELL_RENDERER_SORTED;
4080           else
4081             flags &= ~GTK_CELL_RENDERER_SORTED;
4082
4083           if (cursor == node)
4084             flags |= GTK_CELL_RENDERER_FOCUSED;
4085           else
4086             flags &= ~GTK_CELL_RENDERER_FOCUSED;
4087
4088           background_area.x = cell_offset;
4089           background_area.width = column->width;
4090
4091           cell_area = background_area;
4092           cell_area.y += vertical_separator / 2;
4093           cell_area.x += horizontal_separator / 2;
4094           cell_area.height -= vertical_separator;
4095           cell_area.width -= horizontal_separator;
4096
4097           if (draw_vgrid_lines)
4098             {
4099               if (list == first_column)
4100                 {
4101                   cell_area.width -= grid_line_width / 2;
4102                 }
4103               else if (list == last_column)
4104                 {
4105                   cell_area.x += grid_line_width / 2;
4106                   cell_area.width -= grid_line_width / 2;
4107                 }
4108               else
4109                 {
4110                   cell_area.x += grid_line_width / 2;
4111                   cell_area.width -= grid_line_width;
4112                 }
4113             }
4114
4115           if (draw_hgrid_lines)
4116             {
4117               cell_area.y += grid_line_width / 2;
4118               cell_area.height -= grid_line_width;
4119             }
4120
4121 #if GTK3_TRANSITION
4122           if (gdk_region_rect_in (event->region, &background_area) == GDK_OVERLAP_RECTANGLE_OUT)
4123             {
4124               cell_offset += column->width;
4125               continue;
4126             }
4127 #endif
4128
4129           pspp_sheet_view_column_cell_set_cell_data (column,
4130                                                      tree_view->priv->model,
4131                                                      &iter);
4132
4133           /* Select the detail for drawing the cell.  relevant
4134            * factors are parity, sortedness, and whether to
4135            * display rules.
4136            */
4137           if (allow_rules && tree_view->priv->has_rules)
4138             {
4139               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4140                   n_visible_columns >= 3)
4141                 {
4142                   if (parity)
4143                     detail = "cell_odd_ruled_sorted";
4144                   else
4145                     detail = "cell_even_ruled_sorted";
4146                 }
4147               else
4148                 {
4149                   if (parity)
4150                     detail = "cell_odd_ruled";
4151                   else
4152                     detail = "cell_even_ruled";
4153                 }
4154             }
4155           else
4156             {
4157               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4158                   n_visible_columns >= 3)
4159                 {
4160                   if (parity)
4161                     detail = "cell_odd_sorted";
4162                   else
4163                     detail = "cell_even_sorted";
4164                 }
4165               else
4166                 {
4167                   if (parity)
4168                     detail = "cell_odd";
4169                   else
4170                     detail = "cell_even";
4171                 }
4172             }
4173
4174           g_assert (detail);
4175
4176           if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
4177             state = GTK_STATE_INSENSITIVE;          
4178           else if (flags & GTK_CELL_RENDERER_SELECTED)
4179             state = GTK_STATE_SELECTED;
4180           else
4181             state = GTK_STATE_NORMAL;
4182
4183           /* Draw background */
4184           if (row_ending_details)
4185             {
4186               char new_detail[128];
4187
4188               is_first = (rtl ? !list->next : !list->prev);
4189               is_last = (rtl ? !list->prev : !list->next);
4190
4191               /* (I don't like the snprintfs either, but couldn't find a
4192                * less messy way).
4193                */
4194               if (is_first && is_last)
4195                 g_snprintf (new_detail, 127, "%s", detail);
4196               else if (is_first)
4197                 g_snprintf (new_detail, 127, "%s_start", detail);
4198               else if (is_last)
4199                 g_snprintf (new_detail, 127, "%s_end", detail);
4200               else
4201                 g_snprintf (new_detail, 128, "%s_middle", detail);
4202
4203               gtk_paint_flat_box (gtk_widget_get_style (widget),
4204                                   cr,
4205                                   state,
4206                                   GTK_SHADOW_NONE,
4207                                   widget,
4208                                   new_detail,
4209                                   background_area.x,
4210                                   background_area.y,
4211                                   background_area.width,
4212                                   background_area.height);
4213             }
4214           else
4215             {
4216               gtk_paint_flat_box (gtk_widget_get_style (widget),
4217                                   cr,
4218                                   state,
4219                                   GTK_SHADOW_NONE,
4220                                   widget,
4221                                   detail,
4222                                   background_area.x,
4223                                   background_area.y,
4224                                   background_area.width,
4225                                   background_area.height);
4226             }
4227
4228           if (draw_hgrid_lines)
4229             {
4230               cairo_set_line_width (cr, 1.0);
4231               cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
4232
4233               if (background_area.y >= 0)
4234                 {
4235 #if GTK3_TRANSITION
4236                   gdk_draw_line (event->window,
4237                                  tree_view->priv->grid_line_gc[widget->state],
4238                                  background_area.x, background_area.y,
4239                                  background_area.x + background_area.width,
4240                                  background_area.y);
4241 #else
4242                   cairo_move_to (cr, background_area.x, background_area.y - 0.5);
4243                   cairo_line_to (cr, background_area.x + background_area.width,
4244                                  background_area.y - 0.5);
4245 #endif
4246                 }
4247
4248               if (y_offset + max_height >= Zarea.height - 0.5)
4249                 {
4250 #if GTK3_TRANSITION
4251                   gdk_draw_line (event->window,
4252                                  tree_view->priv->grid_line_gc[widget->state],
4253                                  background_area.x, background_area.y + max_height,
4254                                  background_area.x + background_area.width,
4255                                  background_area.y + max_height);
4256 #else
4257
4258                   cairo_move_to (cr, background_area.x, background_area.y + max_height - 0.5);
4259                   cairo_line_to (cr, background_area.x + background_area.width,
4260                                  background_area.y + max_height - 0.5);
4261 #endif
4262                 }
4263               cairo_stroke (cr);
4264             }
4265
4266           _pspp_sheet_view_column_cell_render (column,
4267                                                cr,
4268                                                &background_area,
4269                                                &cell_area,
4270                                                flags);
4271
4272           if (node == cursor && has_special_cell &&
4273               ((column == tree_view->priv->focus_column &&
4274                 PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4275                 gtk_widget_has_focus (widget)) ||
4276                (column == tree_view->priv->edited_column)))
4277             {
4278               _pspp_sheet_view_column_cell_draw_focus (column,
4279                                                        cr,
4280                                                      &background_area,
4281                                                      &cell_area,
4282                                                      flags);
4283             }
4284
4285           cell_offset += column->width;
4286         }
4287
4288       if (cell_offset < Zarea.x)
4289         {
4290           gtk_paint_flat_box (gtk_widget_get_style (widget),
4291                               cr,
4292                               GTK_STATE_NORMAL,
4293                               GTK_SHADOW_NONE,
4294                               widget,
4295                               "base",
4296                               cell_offset,
4297                               background_area.y,
4298                               Zarea.x - cell_offset,
4299                               background_area.height);
4300         }
4301
4302       if (node == drag_highlight)
4303         {
4304           /* Draw indicator for the drop
4305            */
4306           gint highlight_y = -1;
4307           int node = -1;
4308           gint width;
4309
4310           switch (tree_view->priv->drag_dest_pos)
4311             {
4312             case PSPP_SHEET_VIEW_DROP_BEFORE:
4313               highlight_y = background_area.y - 1;
4314               if (highlight_y < 0)
4315                       highlight_y = 0;
4316               break;
4317
4318             case PSPP_SHEET_VIEW_DROP_AFTER:
4319               highlight_y = background_area.y + background_area.height - 1;
4320               break;
4321
4322             case PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE:
4323             case PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER:
4324               _pspp_sheet_view_find_node (tree_view, drag_dest_path, &node);
4325
4326               if (node < 0)
4327                 break;
4328               width = gdk_window_get_width (tree_view->priv->bin_window);
4329
4330               if (row_ending_details)
4331                 gtk_paint_focus (gtk_widget_get_style (widget),
4332                                  bwcr,
4333                                  gtk_widget_get_state (widget),
4334                                  widget,
4335                                  (is_first
4336                                   ? (is_last ? "treeview-drop-indicator" : "treeview-drop-indicator-left" )
4337                                   : (is_last ? "treeview-drop-indicator-right" : "tree-view-drop-indicator-middle" )),
4338                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4339                                  - focus_line_width / 2,
4340                                  width, ROW_HEIGHT (tree_view)
4341                                - focus_line_width + 1);
4342               else
4343                 gtk_paint_focus (gtk_widget_get_style (widget),
4344                                  bwcr,
4345                                  gtk_widget_get_state (widget),
4346                                  widget,
4347                                  "treeview-drop-indicator",
4348                                  0, BACKGROUND_FIRST_PIXEL (tree_view, node)
4349                                  - focus_line_width / 2,
4350                                  width, ROW_HEIGHT (tree_view)
4351                                  - focus_line_width + 1);
4352               break;
4353             }
4354
4355 #if GTK3_TRANSITION
4356           if (highlight_y >= 0)
4357             {
4358               gdk_draw_line (event->window,
4359                              widget->style->fg_gc[gtk_widget_get_state (widget)],
4360                              0,
4361                              highlight_y,
4362                              rtl ? 0 : bin_window_width,
4363                              highlight_y);
4364             }
4365 #endif
4366         }
4367
4368       /* draw the big row-spanning focus rectangle, if needed */
4369       if (!has_special_cell && node == cursor &&
4370           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4371           gtk_widget_has_focus (widget))
4372         {
4373           gint tmp_y, tmp_height;
4374           gint width;
4375           GtkStateType focus_rect_state;
4376
4377           focus_rect_state =
4378             flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
4379             (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
4380              (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE :
4381               GTK_STATE_NORMAL));
4382
4383           width = gdk_window_get_width (tree_view->priv->bin_window);
4384           
4385           if (draw_hgrid_lines)
4386             {
4387               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node) + grid_line_width / 2;
4388               tmp_height = ROW_HEIGHT (tree_view) - grid_line_width;
4389             }
4390           else
4391             {
4392               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, node);
4393               tmp_height = ROW_HEIGHT (tree_view);
4394             }
4395
4396           if (row_ending_details)
4397             gtk_paint_focus (gtk_widget_get_style (widget),
4398                              bwcr,
4399                              focus_rect_state,
4400                              widget,
4401                              (is_first
4402                               ? (is_last ? "treeview" : "treeview-left" )
4403                               : (is_last ? "treeview-right" : "treeview-middle" )),
4404                              0, tmp_y,
4405                              width, tmp_height);
4406           else
4407             gtk_paint_focus (gtk_widget_get_style (widget),
4408                              bwcr,
4409                              focus_rect_state,
4410                              widget,
4411                              "treeview",
4412                              0, tmp_y,
4413                              width, tmp_height);
4414         }
4415
4416       y_offset += max_height;
4417
4418       do
4419         {
4420           node = pspp_sheet_view_node_next (tree_view, node);
4421           if (node >= 0)
4422             {
4423               gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
4424               done = TRUE;
4425
4426               /* Sanity Check! */
4427               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
4428             }
4429           else
4430             goto done;
4431         }
4432       while (!done);
4433     }
4434   while (y_offset < Zarea.height);
4435
4436 done:
4437   pspp_sheet_view_draw_vertical_grid_lines (tree_view, cr, n_visible_columns,
4438                                    min_y, max_y);
4439
4440 #if GTK3_TRANSITION
4441  if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
4442    {
4443      GdkRectangle *rectangles;
4444      gint n_rectangles;
4445
4446      gdk_region_get_rectangles (event->region,
4447                                 &rectangles,
4448                                 &n_rectangles);
4449
4450      while (n_rectangles--)
4451        pspp_sheet_view_paint_rubber_band (tree_view, &rectangles[n_rectangles]);
4452
4453      g_free (rectangles);
4454    }
4455 #endif
4456
4457   if (cursor_path)
4458     gtk_tree_path_free (cursor_path);
4459
4460   if (drag_dest_path)
4461     gtk_tree_path_free (drag_dest_path);
4462
4463   return FALSE;
4464 }
4465
4466
4467 static gboolean
4468 window_intersects (GdkWindow *window, GdkRectangle *rect) 
4469 {
4470   GdkRectangle window_rect;
4471   gdk_window_get_position (window,
4472                            &window_rect.x, &window_rect.y);
4473     
4474   window_rect.width = gdk_window_get_width (window);
4475   window_rect.height = gdk_window_get_height (window);
4476
4477   return gdk_rectangle_intersect (&window_rect, rect, NULL);
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   GdkRectangle clip_rect;
4488   gtk_widget_get_allocation (widget, &allocation);
4489   
4490   gdk_cairo_get_clip_rectangle (cr, &clip_rect);
4491
4492   if (window_intersects (tree_view->priv->bin_window, &clip_rect))
4493     {
4494       gboolean retval;
4495       GList *tmp_list;
4496
4497       retval = pspp_sheet_view_bin_expose (widget, 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 (window_intersects (tree_view->priv->header_window, &clip_rect))
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 (window_intersects (tree_view->priv->drag_window, &clip_rect))
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   if (tree_view->priv->hadjustment == NULL)
9233     pspp_sheet_view_set_hadjustment (tree_view, NULL);
9234
9235   return tree_view->priv->hadjustment;
9236 }
9237
9238 /**
9239  * pspp_sheet_view_set_hadjustment:
9240  * @tree_view: A #PsppSheetView
9241  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9242  *
9243  * Sets the #GtkAdjustment for the current horizontal aspect.
9244  **/
9245 void
9246 pspp_sheet_view_set_hadjustment (PsppSheetView   *tree_view,
9247                                GtkAdjustment *adjustment)
9248 {
9249   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9250
9251   pspp_sheet_view_set_adjustments (tree_view,
9252                                  adjustment,
9253                                  tree_view->priv->vadjustment);
9254
9255   g_object_notify (G_OBJECT (tree_view), "hadjustment");
9256 }
9257
9258 /**
9259  * pspp_sheet_view_get_vadjustment:
9260  * @tree_view: A #PsppSheetView
9261  *
9262  * Gets the #GtkAdjustment currently being used for the vertical aspect.
9263  *
9264  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
9265  * used.
9266  **/
9267 GtkAdjustment *
9268 pspp_sheet_view_get_vadjustment (PsppSheetView *tree_view)
9269 {
9270   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9271
9272   if (tree_view->priv->vadjustment == NULL)
9273     pspp_sheet_view_set_vadjustment (tree_view, NULL);
9274
9275   return tree_view->priv->vadjustment;
9276 }
9277
9278 /**
9279  * pspp_sheet_view_set_vadjustment:
9280  * @tree_view: A #PsppSheetView
9281  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
9282  *
9283  * Sets the #GtkAdjustment for the current vertical aspect.
9284  **/
9285 void
9286 pspp_sheet_view_set_vadjustment (PsppSheetView   *tree_view,
9287                                GtkAdjustment *adjustment)
9288 {
9289   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9290
9291   pspp_sheet_view_set_adjustments (tree_view,
9292                                  tree_view->priv->hadjustment,
9293                                  adjustment);
9294
9295   g_object_notify (G_OBJECT (tree_view), "vadjustment");
9296 }
9297
9298 /* Column and header operations */
9299
9300 /**
9301  * pspp_sheet_view_get_headers_visible:
9302  * @tree_view: A #PsppSheetView.
9303  *
9304  * Returns %TRUE if the headers on the @tree_view are visible.
9305  *
9306  * Return value: Whether the headers are visible or not.
9307  **/
9308 gboolean
9309 pspp_sheet_view_get_headers_visible (PsppSheetView *tree_view)
9310 {
9311   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9312
9313   return PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9314 }
9315
9316 /**
9317  * pspp_sheet_view_set_headers_visible:
9318  * @tree_view: A #PsppSheetView.
9319  * @headers_visible: %TRUE if the headers are visible
9320  *
9321  * Sets the visibility state of the headers.
9322  **/
9323 void
9324 pspp_sheet_view_set_headers_visible (PsppSheetView *tree_view,
9325                                    gboolean     headers_visible)
9326 {
9327   gint x, y;
9328   GList *list;
9329   PsppSheetViewColumn *column;
9330   GtkAllocation allocation;
9331
9332   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9333
9334   gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
9335
9336   headers_visible = !! headers_visible;
9337
9338   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE) == headers_visible)
9339     return;
9340
9341   if (headers_visible)
9342     PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9343   else
9344     PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
9345
9346   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9347     {
9348       gdk_window_get_position (tree_view->priv->bin_window, &x, &y);
9349       if (headers_visible)
9350         {
9351           gdk_window_move_resize (tree_view->priv->bin_window, x, y  + TREE_VIEW_HEADER_HEIGHT (tree_view), 
9352                                   tree_view->priv->width, allocation.height -  + TREE_VIEW_HEADER_HEIGHT (tree_view));
9353
9354           if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
9355             pspp_sheet_view_map_buttons (tree_view);
9356         }
9357       else
9358         {
9359           gdk_window_move_resize (tree_view->priv->bin_window, x, y, tree_view->priv->width, tree_view->priv->height);
9360
9361           for (list = tree_view->priv->columns; list; list = list->next)
9362             {
9363               column = list->data;
9364               if (column->button)
9365                 gtk_widget_unmap (column->button);
9366             }
9367           gdk_window_hide (tree_view->priv->header_window);
9368         }
9369     }
9370
9371   gtk_adjustment_set_page_size (tree_view->priv->vadjustment, allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view));
9372   gtk_adjustment_set_page_increment (tree_view->priv->vadjustment, (allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view)) / 2);
9373   gtk_adjustment_set_lower (tree_view->priv->vadjustment, 0);
9374   gtk_adjustment_set_upper (tree_view->priv->vadjustment, tree_view->priv->height);
9375   gtk_adjustment_changed (tree_view->priv->vadjustment);
9376
9377   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9378
9379   g_object_notify (G_OBJECT (tree_view), "headers-visible");
9380 }
9381
9382 /**
9383  * pspp_sheet_view_columns_autosize:
9384  * @tree_view: A #PsppSheetView.
9385  *
9386  * Resizes all columns to their optimal width. Only works after the
9387  * treeview has been realized.
9388  **/
9389 void
9390 pspp_sheet_view_columns_autosize (PsppSheetView *tree_view)
9391 {
9392   gboolean dirty = FALSE;
9393   GList *list;
9394   PsppSheetViewColumn *column;
9395
9396   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9397
9398   for (list = tree_view->priv->columns; list; list = list->next)
9399     {
9400       column = list->data;
9401       _pspp_sheet_view_column_cell_set_dirty (column);
9402       dirty = TRUE;
9403     }
9404
9405   if (dirty)
9406     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9407 }
9408
9409 /**
9410  * pspp_sheet_view_set_headers_clickable:
9411  * @tree_view: A #PsppSheetView.
9412  * @setting: %TRUE if the columns are clickable.
9413  *
9414  * Allow the column title buttons to be clicked.
9415  **/
9416 void
9417 pspp_sheet_view_set_headers_clickable (PsppSheetView *tree_view,
9418                                      gboolean   setting)
9419 {
9420   GList *list;
9421
9422   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9423
9424   for (list = tree_view->priv->columns; list; list = list->next)
9425     pspp_sheet_view_column_set_clickable (PSPP_SHEET_VIEW_COLUMN (list->data), setting);
9426
9427   g_object_notify (G_OBJECT (tree_view), "headers-clickable");
9428 }
9429
9430
9431 /**
9432  * pspp_sheet_view_get_headers_clickable:
9433  * @tree_view: A #PsppSheetView.
9434  *
9435  * Returns whether all header columns are clickable.
9436  *
9437  * Return value: %TRUE if all header columns are clickable, otherwise %FALSE
9438  *
9439  * Since: 2.10
9440  **/
9441 gboolean 
9442 pspp_sheet_view_get_headers_clickable (PsppSheetView *tree_view)
9443 {
9444   GList *list;
9445   
9446   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9447
9448   for (list = tree_view->priv->columns; list; list = list->next)
9449     if (!PSPP_SHEET_VIEW_COLUMN (list->data)->clickable)
9450       return FALSE;
9451
9452   return TRUE;
9453 }
9454
9455 /**
9456  * pspp_sheet_view_set_rules_hint
9457  * @tree_view: a #PsppSheetView
9458  * @setting: %TRUE if the tree requires reading across rows
9459  *
9460  * This function tells GTK+ that the user interface for your
9461  * application requires users to read across tree rows and associate
9462  * cells with one another. By default, GTK+ will then render the tree
9463  * with alternating row colors. Do <emphasis>not</emphasis> use it
9464  * just because you prefer the appearance of the ruled tree; that's a
9465  * question for the theme. Some themes will draw tree rows in
9466  * alternating colors even when rules are turned off, and users who
9467  * prefer that appearance all the time can choose those themes. You
9468  * should call this function only as a <emphasis>semantic</emphasis>
9469  * hint to the theme engine that your tree makes alternating colors
9470  * useful from a functional standpoint (since it has lots of columns,
9471  * generally).
9472  *
9473  **/
9474 void
9475 pspp_sheet_view_set_rules_hint (PsppSheetView  *tree_view,
9476                               gboolean      setting)
9477 {
9478   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9479
9480   setting = setting != FALSE;
9481
9482   if (tree_view->priv->has_rules != setting)
9483     {
9484       tree_view->priv->has_rules = setting;
9485       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
9486     }
9487
9488   g_object_notify (G_OBJECT (tree_view), "rules-hint");
9489 }
9490
9491 /**
9492  * pspp_sheet_view_get_rules_hint
9493  * @tree_view: a #PsppSheetView
9494  *
9495  * Gets the setting set by pspp_sheet_view_set_rules_hint().
9496  *
9497  * Return value: %TRUE if rules are useful for the user of this tree
9498  **/
9499 gboolean
9500 pspp_sheet_view_get_rules_hint (PsppSheetView  *tree_view)
9501 {
9502   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
9503
9504   return tree_view->priv->has_rules;
9505 }
9506
9507 /* Public Column functions
9508  */
9509
9510 /**
9511  * pspp_sheet_view_append_column:
9512  * @tree_view: A #PsppSheetView.
9513  * @column: The #PsppSheetViewColumn to add.
9514  *
9515  * Appends @column to the list of columns.
9516  *
9517  * Return value: The number of columns in @tree_view after appending.
9518  **/
9519 gint
9520 pspp_sheet_view_append_column (PsppSheetView       *tree_view,
9521                              PsppSheetViewColumn *column)
9522 {
9523   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9524   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9525   g_return_val_if_fail (column->tree_view == NULL, -1);
9526
9527   return pspp_sheet_view_insert_column (tree_view, column, -1);
9528 }
9529
9530
9531 /**
9532  * pspp_sheet_view_remove_column:
9533  * @tree_view: A #PsppSheetView.
9534  * @column: The #PsppSheetViewColumn to remove.
9535  *
9536  * Removes @column from @tree_view.
9537  *
9538  * Return value: The number of columns in @tree_view after removing.
9539  **/
9540 gint
9541 pspp_sheet_view_remove_column (PsppSheetView       *tree_view,
9542                              PsppSheetViewColumn *column)
9543 {
9544   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9545   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9546   g_return_val_if_fail (column->tree_view == GTK_WIDGET (tree_view), -1);
9547
9548   if (tree_view->priv->focus_column == column)
9549     tree_view->priv->focus_column = NULL;
9550
9551   if (tree_view->priv->edited_column == column)
9552     {
9553       pspp_sheet_view_stop_editing (tree_view, TRUE);
9554
9555       /* no need to, but just to be sure ... */
9556       tree_view->priv->edited_column = NULL;
9557     }
9558
9559   _pspp_sheet_view_column_unset_tree_view (column);
9560
9561   tree_view->priv->columns = g_list_remove (tree_view->priv->columns, column);
9562   tree_view->priv->n_columns--;
9563
9564   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9565     {
9566       GList *list;
9567
9568       _pspp_sheet_view_column_unrealize_button (column);
9569       for (list = tree_view->priv->columns; list; list = list->next)
9570         {
9571           PsppSheetViewColumn *tmp_column;
9572
9573           tmp_column = PSPP_SHEET_VIEW_COLUMN (list->data);
9574           if (tmp_column->visible)
9575             _pspp_sheet_view_column_cell_set_dirty (tmp_column);
9576         }
9577
9578       if (tree_view->priv->n_columns == 0 &&
9579           pspp_sheet_view_get_headers_visible (tree_view) && 
9580           tree_view->priv->header_window)
9581         gdk_window_hide (tree_view->priv->header_window);
9582
9583       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9584     }
9585
9586   g_object_unref (column);
9587   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9588
9589   return tree_view->priv->n_columns;
9590 }
9591
9592 /**
9593  * pspp_sheet_view_insert_column:
9594  * @tree_view: A #PsppSheetView.
9595  * @column: The #PsppSheetViewColumn to be inserted.
9596  * @position: The position to insert @column in.
9597  *
9598  * This inserts the @column into the @tree_view at @position.  If @position is
9599  * -1, then the column is inserted at the end.
9600  *
9601  * Return value: The number of columns in @tree_view after insertion.
9602  **/
9603 gint
9604 pspp_sheet_view_insert_column (PsppSheetView       *tree_view,
9605                              PsppSheetViewColumn *column,
9606                              gint               position)
9607 {
9608   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9609   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
9610   g_return_val_if_fail (column->tree_view == NULL, -1);
9611
9612   g_object_ref_sink (column);
9613
9614   if (tree_view->priv->n_columns == 0 &&
9615       gtk_widget_get_realized (GTK_WIDGET (tree_view)) &&
9616       pspp_sheet_view_get_headers_visible (tree_view))
9617     {
9618       gdk_window_show (tree_view->priv->header_window);
9619     }
9620
9621   tree_view->priv->columns = g_list_insert (tree_view->priv->columns,
9622                                             column, position);
9623   tree_view->priv->n_columns++;
9624
9625   _pspp_sheet_view_column_set_tree_view (column, tree_view);
9626
9627   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9628     {
9629       GList *list;
9630
9631       _pspp_sheet_view_column_realize_button (column);
9632
9633       for (list = tree_view->priv->columns; list; list = list->next)
9634         {
9635           column = PSPP_SHEET_VIEW_COLUMN (list->data);
9636           if (column->visible)
9637             _pspp_sheet_view_column_cell_set_dirty (column);
9638         }
9639       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9640     }
9641
9642   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9643
9644   return tree_view->priv->n_columns;
9645 }
9646
9647 /**
9648  * pspp_sheet_view_insert_column_with_attributes:
9649  * @tree_view: A #PsppSheetView
9650  * @position: The position to insert the new column in.
9651  * @title: The title to set the header to.
9652  * @cell: The #GtkCellRenderer.
9653  * @Varargs: A %NULL-terminated list of attributes.
9654  *
9655  * Creates a new #PsppSheetViewColumn and inserts it into the @tree_view at
9656  * @position.  If @position is -1, then the newly created column is inserted at
9657  * the end.  The column is initialized with the attributes given.
9658  *
9659  * Return value: The number of columns in @tree_view after insertion.
9660  **/
9661 gint
9662 pspp_sheet_view_insert_column_with_attributes (PsppSheetView     *tree_view,
9663                                              gint             position,
9664                                              const gchar     *title,
9665                                              GtkCellRenderer *cell,
9666                                              ...)
9667 {
9668   PsppSheetViewColumn *column;
9669   gchar *attribute;
9670   va_list args;
9671   gint column_id;
9672
9673   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9674
9675   column = pspp_sheet_view_column_new ();
9676   pspp_sheet_view_column_set_title (column, title);
9677   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9678
9679   va_start (args, cell);
9680
9681   attribute = va_arg (args, gchar *);
9682
9683   while (attribute != NULL)
9684     {
9685       column_id = va_arg (args, gint);
9686       pspp_sheet_view_column_add_attribute (column, cell, attribute, column_id);
9687       attribute = va_arg (args, gchar *);
9688     }
9689
9690   va_end (args);
9691
9692   pspp_sheet_view_insert_column (tree_view, column, position);
9693
9694   return tree_view->priv->n_columns;
9695 }
9696
9697 /**
9698  * pspp_sheet_view_insert_column_with_data_func:
9699  * @tree_view: a #PsppSheetView
9700  * @position: Position to insert, -1 for append
9701  * @title: column title
9702  * @cell: cell renderer for column
9703  * @func: function to set attributes of cell renderer
9704  * @data: data for @func
9705  * @dnotify: destroy notifier for @data
9706  *
9707  * Convenience function that inserts a new column into the #PsppSheetView
9708  * with the given cell renderer and a #GtkCellDataFunc to set cell renderer
9709  * attributes (normally using data from the model). See also
9710  * pspp_sheet_view_column_set_cell_data_func(), pspp_sheet_view_column_pack_start().
9711  *
9712  * Return value: number of columns in the tree view post-insert
9713  **/
9714 gint
9715 pspp_sheet_view_insert_column_with_data_func  (PsppSheetView               *tree_view,
9716                                              gint                       position,
9717                                              const gchar               *title,
9718                                              GtkCellRenderer           *cell,
9719                                              PsppSheetCellDataFunc        func,
9720                                              gpointer                   data,
9721                                              GDestroyNotify             dnotify)
9722 {
9723   PsppSheetViewColumn *column;
9724
9725   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
9726
9727   column = pspp_sheet_view_column_new ();
9728   pspp_sheet_view_column_set_title (column, title);
9729   pspp_sheet_view_column_pack_start (column, cell, TRUE);
9730   pspp_sheet_view_column_set_cell_data_func (column, cell, func, data, dnotify);
9731
9732   pspp_sheet_view_insert_column (tree_view, column, position);
9733
9734   return tree_view->priv->n_columns;
9735 }
9736
9737 /**
9738  * pspp_sheet_view_get_column:
9739  * @tree_view: A #PsppSheetView.
9740  * @n: The position of the column, counting from 0.
9741  *
9742  * Gets the #PsppSheetViewColumn at the given position in the #tree_view.
9743  *
9744  * Return value: The #PsppSheetViewColumn, or %NULL if the position is outside the
9745  * range of columns.
9746  **/
9747 PsppSheetViewColumn *
9748 pspp_sheet_view_get_column (PsppSheetView *tree_view,
9749                           gint         n)
9750 {
9751   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9752
9753   if (n < 0 || n >= tree_view->priv->n_columns)
9754     return NULL;
9755
9756   if (tree_view->priv->columns == NULL)
9757     return NULL;
9758
9759   return PSPP_SHEET_VIEW_COLUMN (g_list_nth (tree_view->priv->columns, n)->data);
9760 }
9761
9762 /**
9763  * pspp_sheet_view_get_columns:
9764  * @tree_view: A #PsppSheetView
9765  *
9766  * Returns a #GList of all the #PsppSheetViewColumn s currently in @tree_view.
9767  * The returned list must be freed with g_list_free ().
9768  *
9769  * Return value: (element-type PsppSheetViewColumn) (transfer container): A list of #PsppSheetViewColumn s
9770  **/
9771 GList *
9772 pspp_sheet_view_get_columns (PsppSheetView *tree_view)
9773 {
9774   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
9775
9776   return g_list_copy (tree_view->priv->columns);
9777 }
9778
9779 /**
9780  * pspp_sheet_view_move_column_after:
9781  * @tree_view: A #PsppSheetView
9782  * @column: The #PsppSheetViewColumn to be moved.
9783  * @base_column: (allow-none): The #PsppSheetViewColumn to be moved relative to, or %NULL.
9784  *
9785  * Moves @column to be after to @base_column.  If @base_column is %NULL, then
9786  * @column is placed in the first position.
9787  **/
9788 void
9789 pspp_sheet_view_move_column_after (PsppSheetView       *tree_view,
9790                                  PsppSheetViewColumn *column,
9791                                  PsppSheetViewColumn *base_column)
9792 {
9793   GList *column_list_el, *base_el = NULL;
9794
9795   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9796
9797   column_list_el = g_list_find (tree_view->priv->columns, column);
9798   g_return_if_fail (column_list_el != NULL);
9799
9800   if (base_column)
9801     {
9802       base_el = g_list_find (tree_view->priv->columns, base_column);
9803       g_return_if_fail (base_el != NULL);
9804     }
9805
9806   if (column_list_el->prev == base_el)
9807     return;
9808
9809   tree_view->priv->columns = g_list_remove_link (tree_view->priv->columns, column_list_el);
9810   if (base_el == NULL)
9811     {
9812       column_list_el->prev = NULL;
9813       column_list_el->next = tree_view->priv->columns;
9814       if (column_list_el->next)
9815         column_list_el->next->prev = column_list_el;
9816       tree_view->priv->columns = column_list_el;
9817     }
9818   else
9819     {
9820       column_list_el->prev = base_el;
9821       column_list_el->next = base_el->next;
9822       if (column_list_el->next)
9823         column_list_el->next->prev = column_list_el;
9824       base_el->next = column_list_el;
9825     }
9826
9827   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9828     {
9829       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
9830       pspp_sheet_view_size_allocate_columns (GTK_WIDGET (tree_view), NULL);
9831     }
9832
9833   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
9834 }
9835
9836 /**
9837  * pspp_sheet_view_set_column_drag_function:
9838  * @tree_view: A #PsppSheetView.
9839  * @func: (allow-none): A function to determine which columns are reorderable, or %NULL.
9840  * @user_data: (allow-none): User data to be passed to @func, or %NULL
9841  * @destroy: (allow-none): Destroy notifier for @user_data, or %NULL
9842  *
9843  * Sets a user function for determining where a column may be dropped when
9844  * dragged.  This function is called on every column pair in turn at the
9845  * beginning of a column drag to determine where a drop can take place.  The
9846  * arguments passed to @func are: the @tree_view, the #PsppSheetViewColumn being
9847  * dragged, the two #PsppSheetViewColumn s determining the drop spot, and
9848  * @user_data.  If either of the #PsppSheetViewColumn arguments for the drop spot
9849  * are %NULL, then they indicate an edge.  If @func is set to be %NULL, then
9850  * @tree_view reverts to the default behavior of allowing all columns to be
9851  * dropped everywhere.
9852  **/
9853 void
9854 pspp_sheet_view_set_column_drag_function (PsppSheetView               *tree_view,
9855                                         PsppSheetViewColumnDropFunc  func,
9856                                         gpointer                   user_data,
9857                                         GDestroyNotify             destroy)
9858 {
9859   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9860
9861   if (tree_view->priv->column_drop_func_data_destroy)
9862     tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
9863
9864   tree_view->priv->column_drop_func = func;
9865   tree_view->priv->column_drop_func_data = user_data;
9866   tree_view->priv->column_drop_func_data_destroy = destroy;
9867 }
9868
9869 /**
9870  * pspp_sheet_view_scroll_to_point:
9871  * @tree_view: a #PsppSheetView
9872  * @tree_x: X coordinate of new top-left pixel of visible area, or -1
9873  * @tree_y: Y coordinate of new top-left pixel of visible area, or -1
9874  *
9875  * Scrolls the tree view such that the top-left corner of the visible
9876  * area is @tree_x, @tree_y, where @tree_x and @tree_y are specified
9877  * in tree coordinates.  The @tree_view must be realized before
9878  * this function is called.  If it isn't, you probably want to be
9879  * using pspp_sheet_view_scroll_to_cell().
9880  *
9881  * If either @tree_x or @tree_y are -1, then that direction isn't scrolled.
9882  **/
9883 void
9884 pspp_sheet_view_scroll_to_point (PsppSheetView *tree_view,
9885                                gint         tree_x,
9886                                gint         tree_y)
9887 {
9888   GtkAdjustment *hadj;
9889   GtkAdjustment *vadj;
9890
9891   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9892   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
9893
9894   hadj = tree_view->priv->hadjustment;
9895   vadj = tree_view->priv->vadjustment;
9896
9897   if (tree_x != -1)
9898     gtk_adjustment_set_value (hadj, CLAMP (tree_x, gtk_adjustment_get_lower (hadj), gtk_adjustment_get_upper (hadj) - gtk_adjustment_get_page_size (hadj)));
9899   if (tree_y != -1)
9900     gtk_adjustment_set_value (vadj, CLAMP (tree_y, gtk_adjustment_get_lower (vadj), gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj)));
9901 }
9902
9903 /**
9904  * pspp_sheet_view_scroll_to_cell:
9905  * @tree_view: A #PsppSheetView.
9906  * @path: (allow-none): The path of the row to move to, or %NULL.
9907  * @column: (allow-none): The #PsppSheetViewColumn to move horizontally to, or %NULL.
9908  * @use_align: whether to use alignment arguments, or %FALSE.
9909  * @row_align: The vertical alignment of the row specified by @path.
9910  * @col_align: The horizontal alignment of the column specified by @column.
9911  *
9912  * Moves the alignments of @tree_view to the position specified by @column and
9913  * @path.  If @column is %NULL, then no horizontal scrolling occurs.  Likewise,
9914  * if @path is %NULL no vertical scrolling occurs.  At a minimum, one of @column
9915  * or @path need to be non-%NULL.  @row_align determines where the row is
9916  * placed, and @col_align determines where @column is placed.  Both are expected
9917  * to be between 0.0 and 1.0. 0.0 means left/top alignment, 1.0 means
9918  * right/bottom alignment, 0.5 means center.
9919  *
9920  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
9921  * tree does the minimum amount of work to scroll the cell onto the screen.
9922  * This means that the cell will be scrolled to the edge closest to its current
9923  * position.  If the cell is currently visible on the screen, nothing is done.
9924  *
9925  * This function only works if the model is set, and @path is a valid row on the
9926  * model.  If the model changes before the @tree_view is realized, the centered
9927  * path will be modified to reflect this change.
9928  **/
9929 void
9930 pspp_sheet_view_scroll_to_cell (PsppSheetView       *tree_view,
9931                               GtkTreePath       *path,
9932                               PsppSheetViewColumn *column,
9933                               gboolean           use_align,
9934                               gfloat             row_align,
9935                               gfloat             col_align)
9936 {
9937   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
9938   g_return_if_fail (tree_view->priv->model != NULL);
9939   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
9940   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
9941   g_return_if_fail (path != NULL || column != NULL);
9942
9943 #if 0
9944   g_print ("pspp_sheet_view_scroll_to_cell:\npath: %s\ncolumn: %s\nuse_align: %d\nrow_align: %f\ncol_align: %f\n",
9945            gtk_tree_path_to_string (path), column?"non-null":"null", use_align, row_align, col_align);
9946 #endif
9947   row_align = CLAMP (row_align, 0.0, 1.0);
9948   col_align = CLAMP (col_align, 0.0, 1.0);
9949
9950
9951   /* Note: Despite the benefits that come from having one code path for the
9952    * scrolling code, we short-circuit validate_visible_area's immplementation as
9953    * it is much slower than just going to the point.
9954    */
9955   if (!gtk_widget_get_visible (GTK_WIDGET (tree_view)) ||
9956       !gtk_widget_get_realized (GTK_WIDGET (tree_view))
9957       /* XXX || GTK_WIDGET_ALLOC_NEEDED (tree_view) */)
9958     {
9959       if (tree_view->priv->scroll_to_path)
9960         gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
9961
9962       tree_view->priv->scroll_to_path = NULL;
9963       tree_view->priv->scroll_to_column = NULL;
9964
9965       if (path)
9966         tree_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
9967       if (column)
9968         tree_view->priv->scroll_to_column = column;
9969       tree_view->priv->scroll_to_use_align = use_align;
9970       tree_view->priv->scroll_to_row_align = row_align;
9971       tree_view->priv->scroll_to_col_align = col_align;
9972
9973       install_presize_handler (tree_view);
9974     }
9975   else
9976     {
9977       GdkRectangle cell_rect;
9978       GdkRectangle vis_rect;
9979       gint dest_x, dest_y;
9980
9981       pspp_sheet_view_get_background_area (tree_view, path, column, &cell_rect);
9982       pspp_sheet_view_get_visible_rect (tree_view, &vis_rect);
9983
9984       cell_rect.y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, cell_rect.y);
9985
9986       dest_x = vis_rect.x;
9987       dest_y = vis_rect.y;
9988
9989       if (column)
9990         {
9991           if (use_align)
9992             {
9993               dest_x = cell_rect.x - ((vis_rect.width - cell_rect.width) * col_align);
9994             }
9995           else
9996             {
9997               if (cell_rect.x < vis_rect.x)
9998                 dest_x = cell_rect.x;
9999               if (cell_rect.x + cell_rect.width > vis_rect.x + vis_rect.width)
10000                 dest_x = cell_rect.x + cell_rect.width - vis_rect.width;
10001             }
10002         }
10003
10004       if (path)
10005         {
10006           if (use_align)
10007             {
10008               dest_y = cell_rect.y - ((vis_rect.height - cell_rect.height) * row_align);
10009               dest_y = MAX (dest_y, 0);
10010             }
10011           else
10012             {
10013               if (cell_rect.y < vis_rect.y)
10014                 dest_y = cell_rect.y;
10015               if (cell_rect.y + cell_rect.height > vis_rect.y + vis_rect.height)
10016                 dest_y = cell_rect.y + cell_rect.height - vis_rect.height;
10017             }
10018         }
10019
10020       pspp_sheet_view_scroll_to_point (tree_view, dest_x, dest_y);
10021     }
10022 }
10023
10024 /**
10025  * pspp_sheet_view_row_activated:
10026  * @tree_view: A #PsppSheetView
10027  * @path: The #GtkTreePath to be activated.
10028  * @column: The #PsppSheetViewColumn to be activated.
10029  *
10030  * Activates the cell determined by @path and @column.
10031  **/
10032 void
10033 pspp_sheet_view_row_activated (PsppSheetView       *tree_view,
10034                              GtkTreePath       *path,
10035                              PsppSheetViewColumn *column)
10036 {
10037   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10038
10039   g_signal_emit (tree_view, tree_view_signals[ROW_ACTIVATED], 0, path, column);
10040 }
10041
10042
10043 /**
10044  * pspp_sheet_view_get_reorderable:
10045  * @tree_view: a #PsppSheetView
10046  *
10047  * Retrieves whether the user can reorder the tree via drag-and-drop. See
10048  * pspp_sheet_view_set_reorderable().
10049  *
10050  * Return value: %TRUE if the tree can be reordered.
10051  **/
10052 gboolean
10053 pspp_sheet_view_get_reorderable (PsppSheetView *tree_view)
10054 {
10055   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10056
10057   return tree_view->priv->reorderable;
10058 }
10059
10060 /**
10061  * pspp_sheet_view_set_reorderable:
10062  * @tree_view: A #PsppSheetView.
10063  * @reorderable: %TRUE, if the tree can be reordered.
10064  *
10065  * This function is a convenience function to allow you to reorder
10066  * models that support the #GtkDragSourceIface and the
10067  * #GtkDragDestIface.  Both #GtkTreeStore and #GtkListStore support
10068  * these.  If @reorderable is %TRUE, then the user can reorder the
10069  * model by dragging and dropping rows. The developer can listen to
10070  * these changes by connecting to the model's row_inserted and
10071  * row_deleted signals. The reordering is implemented by setting up
10072  * the tree view as a drag source and destination. Therefore, drag and
10073  * drop can not be used in a reorderable view for any other purpose.
10074  *
10075  * This function does not give you any degree of control over the order -- any
10076  * reordering is allowed.  If more control is needed, you should probably
10077  * handle drag and drop manually.
10078  **/
10079 void
10080 pspp_sheet_view_set_reorderable (PsppSheetView *tree_view,
10081                                gboolean     reorderable)
10082 {
10083   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10084
10085   reorderable = reorderable != FALSE;
10086
10087   if (tree_view->priv->reorderable == reorderable)
10088     return;
10089
10090   if (reorderable)
10091     {
10092       const GtkTargetEntry row_targets[] = {
10093         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
10094       };
10095
10096       pspp_sheet_view_enable_model_drag_source (tree_view,
10097                                               GDK_BUTTON1_MASK,
10098                                               row_targets,
10099                                               G_N_ELEMENTS (row_targets),
10100                                               GDK_ACTION_MOVE);
10101       pspp_sheet_view_enable_model_drag_dest (tree_view,
10102                                             row_targets,
10103                                             G_N_ELEMENTS (row_targets),
10104                                             GDK_ACTION_MOVE);
10105     }
10106   else
10107     {
10108       pspp_sheet_view_unset_rows_drag_source (tree_view);
10109       pspp_sheet_view_unset_rows_drag_dest (tree_view);
10110     }
10111
10112   tree_view->priv->reorderable = reorderable;
10113
10114   g_object_notify (G_OBJECT (tree_view), "reorderable");
10115 }
10116
10117 /* If CLEAR_AND_SELECT is true, then the row will be selected and, unless Shift
10118    is pressed, other rows will be unselected.
10119
10120    If CLAMP_NODE is true, then the sheetview will scroll to make the row
10121    visible. */
10122 static void
10123 pspp_sheet_view_real_set_cursor (PsppSheetView     *tree_view,
10124                                GtkTreePath     *path,
10125                                gboolean         clear_and_select,
10126                                gboolean         clamp_node,
10127                                PsppSheetSelectMode mode)
10128 {
10129   int node = -1;
10130
10131   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10132     {
10133       GtkTreePath *cursor_path;
10134       cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10135       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10136       gtk_tree_path_free (cursor_path);
10137     }
10138
10139   gtk_tree_row_reference_free (tree_view->priv->cursor);
10140   tree_view->priv->cursor = NULL;
10141
10142   _pspp_sheet_view_find_node (tree_view, path, &node);
10143   tree_view->priv->cursor =
10144     gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
10145                                       tree_view->priv->model,
10146                                       path);
10147
10148   if (tree_view->priv->row_count > 0)
10149     {
10150       int new_node = -1;
10151
10152       if (clear_and_select && !(mode & PSPP_SHEET_SELECT_MODE_TOGGLE))
10153         _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
10154                                                     node, path, mode,
10155                                                     FALSE);
10156
10157       /* We have to re-find tree and node here again, somebody might have
10158        * cleared the node or the whole tree in the PsppSheetSelection::changed
10159        * callback. If the nodes differ we bail out here.
10160        */
10161       _pspp_sheet_view_find_node (tree_view, path, &new_node);
10162
10163       if (node != new_node)
10164         return;
10165
10166       if (clamp_node)
10167         {
10168           pspp_sheet_view_clamp_node_visible (tree_view, node);
10169           _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
10170         }
10171     }
10172
10173   g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
10174 }
10175
10176 /**
10177  * pspp_sheet_view_get_cursor:
10178  * @tree_view: A #PsppSheetView
10179  * @path: (allow-none): A pointer to be filled with the current cursor path, or %NULL
10180  * @focus_column: (allow-none): A pointer to be filled with the current focus column, or %NULL
10181  *
10182  * Fills in @path and @focus_column with the current path and focus column.  If
10183  * the cursor isn't currently set, then *@path will be %NULL.  If no column
10184  * currently has focus, then *@focus_column will be %NULL.
10185  *
10186  * The returned #GtkTreePath must be freed with gtk_tree_path_free() when
10187  * you are done with it.
10188  **/
10189 void
10190 pspp_sheet_view_get_cursor (PsppSheetView        *tree_view,
10191                           GtkTreePath       **path,
10192                           PsppSheetViewColumn **focus_column)
10193 {
10194   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10195
10196   if (path)
10197     {
10198       if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
10199         *path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10200       else
10201         *path = NULL;
10202     }
10203
10204   if (focus_column)
10205     {
10206       *focus_column = tree_view->priv->focus_column;
10207     }
10208 }
10209
10210 /**
10211  * pspp_sheet_view_set_cursor:
10212  * @tree_view: A #PsppSheetView
10213  * @path: A #GtkTreePath
10214  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10215  * @start_editing: %TRUE if the specified cell should start being edited.
10216  *
10217  * Sets the current keyboard focus to be at @path, and selects it.  This is
10218  * useful when you want to focus the user's attention on a particular row.  If
10219  * @focus_column is not %NULL, then focus is given to the column specified by 
10220  * it. Additionally, if @focus_column is specified, and @start_editing is 
10221  * %TRUE, then editing should be started in the specified cell.  
10222  * This function is often followed by @gtk_widget_grab_focus (@tree_view) 
10223  * in order to give keyboard focus to the widget.  Please note that editing 
10224  * can only happen when the widget is realized.
10225  *
10226  * If @path is invalid for @model, the current cursor (if any) will be unset
10227  * and the function will return without failing.
10228  **/
10229 void
10230 pspp_sheet_view_set_cursor (PsppSheetView       *tree_view,
10231                           GtkTreePath       *path,
10232                           PsppSheetViewColumn *focus_column,
10233                           gboolean           start_editing)
10234 {
10235   pspp_sheet_view_set_cursor_on_cell (tree_view, path, focus_column,
10236                                     NULL, start_editing);
10237 }
10238
10239 /**
10240  * pspp_sheet_view_set_cursor_on_cell:
10241  * @tree_view: A #PsppSheetView
10242  * @path: A #GtkTreePath
10243  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
10244  * @focus_cell: (allow-none): A #GtkCellRenderer, or %NULL
10245  * @start_editing: %TRUE if the specified cell should start being edited.
10246  *
10247  * Sets the current keyboard focus to be at @path, and selects it.  This is
10248  * useful when you want to focus the user's attention on a particular row.  If
10249  * @focus_column is not %NULL, then focus is given to the column specified by
10250  * it. If @focus_column and @focus_cell are not %NULL, and @focus_column
10251  * contains 2 or more editable or activatable cells, then focus is given to
10252  * the cell specified by @focus_cell. Additionally, if @focus_column is
10253  * specified, and @start_editing is %TRUE, then editing should be started in
10254  * the specified cell.  This function is often followed by
10255  * @gtk_widget_grab_focus (@tree_view) in order to give keyboard focus to the
10256  * widget.  Please note that editing can only happen when the widget is
10257  * realized.
10258  *
10259  * If @path is invalid for @model, the current cursor (if any) will be unset
10260  * and the function will return without failing.
10261  *
10262  * Since: 2.2
10263  **/
10264 void
10265 pspp_sheet_view_set_cursor_on_cell (PsppSheetView       *tree_view,
10266                                   GtkTreePath       *path,
10267                                   PsppSheetViewColumn *focus_column,
10268                                   GtkCellRenderer   *focus_cell,
10269                                   gboolean           start_editing)
10270 {
10271   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10272   g_return_if_fail (path != NULL);
10273   g_return_if_fail (focus_column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (focus_column));
10274
10275   if (!tree_view->priv->model)
10276     return;
10277
10278   if (focus_cell)
10279     {
10280       g_return_if_fail (focus_column);
10281       g_return_if_fail (GTK_IS_CELL_RENDERER (focus_cell));
10282     }
10283
10284   /* cancel the current editing, if it exists */
10285   if (tree_view->priv->edited_column &&
10286       tree_view->priv->edited_column->editable_widget)
10287     pspp_sheet_view_stop_editing (tree_view, TRUE);
10288
10289   pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE, 0);
10290
10291   if (focus_column && focus_column->visible)
10292     {
10293       GList *list;
10294       gboolean column_in_tree = FALSE;
10295
10296       for (list = tree_view->priv->columns; list; list = list->next)
10297         if (list->data == focus_column)
10298           {
10299             column_in_tree = TRUE;
10300             break;
10301           }
10302       g_return_if_fail (column_in_tree);
10303       tree_view->priv->focus_column = focus_column;
10304       if (focus_cell)
10305         pspp_sheet_view_column_focus_cell (focus_column, focus_cell);
10306       if (start_editing)
10307         pspp_sheet_view_start_editing (tree_view, path);
10308
10309       pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
10310       pspp_sheet_selection_select_column (tree_view->priv->selection, focus_column);
10311
10312     }
10313 }
10314
10315 /**
10316  * pspp_sheet_view_get_bin_window:
10317  * @tree_view: A #PsppSheetView
10318  * 
10319  * Returns the window that @tree_view renders to.  This is used primarily to
10320  * compare to <literal>event->window</literal> to confirm that the event on
10321  * @tree_view is on the right window.
10322  * 
10323  * Return value: A #GdkWindow, or %NULL when @tree_view hasn't been realized yet
10324  **/
10325 GdkWindow *
10326 pspp_sheet_view_get_bin_window (PsppSheetView *tree_view)
10327 {
10328   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10329
10330   return tree_view->priv->bin_window;
10331 }
10332
10333 /**
10334  * pspp_sheet_view_get_path_at_pos:
10335  * @tree_view: A #PsppSheetView.
10336  * @x: The x position to be identified (relative to bin_window).
10337  * @y: The y position to be identified (relative to bin_window).
10338  * @path: (out) (allow-none): A pointer to a #GtkTreePath pointer to be filled in, or %NULL
10339  * @column: (out) (allow-none): A pointer to a #PsppSheetViewColumn pointer to be filled in, or %NULL
10340  * @cell_x: (out) (allow-none): A pointer where the X coordinate relative to the cell can be placed, or %NULL
10341  * @cell_y: (out) (allow-none): A pointer where the Y coordinate relative to the cell can be placed, or %NULL
10342  *
10343  * Finds the path at the point (@x, @y), relative to bin_window coordinates
10344  * (please see pspp_sheet_view_get_bin_window()).
10345  * That is, @x and @y are relative to an events coordinates. @x and @y must
10346  * come from an event on the @tree_view only where <literal>event->window ==
10347  * pspp_sheet_view_get_bin_window (<!-- -->)</literal>. It is primarily for
10348  * things like popup menus. If @path is non-%NULL, then it will be filled
10349  * with the #GtkTreePath at that point.  This path should be freed with
10350  * gtk_tree_path_free().  If @column is non-%NULL, then it will be filled
10351  * with the column at that point.  @cell_x and @cell_y return the coordinates
10352  * relative to the cell background (i.e. the @background_area passed to
10353  * gtk_cell_renderer_render()).  This function is only meaningful if
10354  * @tree_view is realized.  Therefore this function will always return %FALSE
10355  * if @tree_view is not realized or does not have a model.
10356  *
10357  * For converting widget coordinates (eg. the ones you get from
10358  * GtkWidget::query-tooltip), please see
10359  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10360  *
10361  * Return value: %TRUE if a row exists at that coordinate.
10362  **/
10363 gboolean
10364 pspp_sheet_view_get_path_at_pos (PsppSheetView        *tree_view,
10365                                gint                x,
10366                                gint                y,
10367                                GtkTreePath       **path,
10368                                PsppSheetViewColumn **column,
10369                                gint               *cell_x,
10370                                gint               *cell_y)
10371 {
10372   int node;
10373   gint y_offset;
10374
10375   g_return_val_if_fail (tree_view != NULL, FALSE);
10376
10377   if (path)
10378     *path = NULL;
10379   if (column)
10380     *column = NULL;
10381
10382   if (tree_view->priv->bin_window == NULL)
10383     return FALSE;
10384
10385   if (tree_view->priv->row_count == 0)
10386     return FALSE;
10387
10388   if (x > gtk_adjustment_get_upper (tree_view->priv->hadjustment))
10389     return FALSE;
10390
10391   if (x < 0 || y < 0)
10392     return FALSE;
10393
10394   if (column || cell_x)
10395     {
10396       PsppSheetViewColumn *tmp_column;
10397       PsppSheetViewColumn *last_column = NULL;
10398       GList *list;
10399       gint remaining_x = x;
10400       gboolean found = FALSE;
10401       gboolean rtl;
10402
10403       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
10404       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
10405            list;
10406            list = (rtl ? list->prev : list->next))
10407         {
10408           tmp_column = list->data;
10409
10410           if (tmp_column->visible == FALSE)
10411             continue;
10412
10413           last_column = tmp_column;
10414           if (remaining_x <= tmp_column->width)
10415             {
10416               found = TRUE;
10417
10418               if (column)
10419                 *column = tmp_column;
10420
10421               if (cell_x)
10422                 *cell_x = remaining_x;
10423
10424               break;
10425             }
10426           remaining_x -= tmp_column->width;
10427         }
10428
10429       /* If found is FALSE and there is a last_column, then it the remainder
10430        * space is in that area
10431        */
10432       if (!found)
10433         {
10434           if (last_column)
10435             {
10436               if (column)
10437                 *column = last_column;
10438               
10439               if (cell_x)
10440                 *cell_x = last_column->width + remaining_x;
10441             }
10442           else
10443             {
10444               return FALSE;
10445             }
10446         }
10447     }
10448
10449   y_offset = pspp_sheet_view_find_offset (tree_view,
10450                                           TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y),
10451                                           &node);
10452
10453   if (node < 0)
10454     return FALSE;
10455
10456   if (cell_y)
10457     *cell_y = y_offset;
10458
10459   if (path)
10460     *path = _pspp_sheet_view_find_path (tree_view, node);
10461
10462   return TRUE;
10463 }
10464
10465 /* Computes 'cell_area' from 'background_area', which must be the background
10466    area for a cell.  Set 'subtract_focus_rect' to TRUE to compute the cell area
10467    as passed to a GtkCellRenderer's "render" function, or to FALSE to compute
10468    the cell area as passed to _pspp_sheet_view_column_cell_render().
10469
10470    'column' is required to properly adjust 'cell_area->x' and
10471    'cell_area->width'.  It may be set to NULL if these values are not of
10472    interest.  In this case 'cell_area->x' and 'cell_area->width' will be
10473    returned as 0. */
10474 static void
10475 pspp_sheet_view_adjust_cell_area (PsppSheetView        *tree_view,
10476                                   PsppSheetViewColumn  *column,
10477                                   const GdkRectangle   *background_area,
10478                                   gboolean              subtract_focus_rect,
10479                                   GdkRectangle         *cell_area)
10480 {
10481   gint vertical_separator;
10482   gint horizontal_separator;
10483
10484   *cell_area = *background_area;
10485
10486   gtk_widget_style_get (GTK_WIDGET (tree_view),
10487                         "vertical-separator", &vertical_separator,
10488                         "horizontal-separator", &horizontal_separator,
10489                         NULL);
10490   cell_area->x += horizontal_separator / 2;
10491   cell_area->y += vertical_separator / 2;
10492   cell_area->width -= horizontal_separator;
10493   cell_area->height -= vertical_separator;
10494
10495   if (subtract_focus_rect)
10496     {
10497       int focus_line_width;
10498
10499       gtk_widget_style_get (GTK_WIDGET (tree_view),
10500                             "focus-line-width", &focus_line_width,
10501                             NULL);
10502       cell_area->x += focus_line_width;
10503       cell_area->y += focus_line_width;
10504       cell_area->width -= 2 * focus_line_width;
10505       cell_area->height -= 2 * focus_line_width;
10506     }
10507
10508   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_NONE)
10509     {
10510       gint grid_line_width;
10511       gtk_widget_style_get (GTK_WIDGET (tree_view),
10512                             "grid-line-width", &grid_line_width,
10513                             NULL);
10514
10515       if ((tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
10516            || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10517           && column != NULL)
10518         {
10519           PsppSheetViewColumn *first_column, *last_column;
10520           GList *list;
10521
10522           /* Find the last visible column. */
10523           last_column = NULL;
10524           for (list = g_list_last (tree_view->priv->columns);
10525                list;
10526                list = list->prev)
10527             {
10528               PsppSheetViewColumn *c = list->data;
10529               if (c->visible)
10530                 {
10531                   last_column = c;
10532                   break;
10533                 }
10534             }
10535
10536           /* Find the first visible column. */
10537           first_column = NULL;
10538           for (list = g_list_first (tree_view->priv->columns);
10539                list;
10540                list = list->next)
10541             {
10542               PsppSheetViewColumn *c = list->data;
10543               if (c->visible)
10544                 {
10545                   first_column = c;
10546                   break;
10547                 }
10548             }
10549
10550           if (column == first_column)
10551             {
10552               cell_area->width -= grid_line_width / 2;
10553             }
10554           else if (column == last_column)
10555             {
10556               cell_area->x += grid_line_width / 2;
10557               cell_area->width -= grid_line_width / 2;
10558             }
10559           else
10560             {
10561               cell_area->x += grid_line_width / 2;
10562               cell_area->width -= grid_line_width;
10563             }
10564         }
10565
10566       if (tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
10567           || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH)
10568         {
10569           cell_area->y += grid_line_width / 2;
10570           cell_area->height -= grid_line_width;
10571         }
10572     }
10573
10574   if (column == NULL)
10575     {
10576       cell_area->x = 0;
10577       cell_area->width = 0;
10578     }
10579 }
10580
10581 /**
10582  * pspp_sheet_view_get_cell_area:
10583  * @tree_view: a #PsppSheetView
10584  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10585  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordinates
10586  * @rect: rectangle to fill with cell rect
10587  *
10588  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10589  * row specified by @path and the column specified by @column.  If @path is
10590  * %NULL, or points to a path not currently displayed, the @y and @height fields
10591  * of the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10592  * fields will be filled with 0.  The sum of all cell rects does not cover the
10593  * entire tree; there are extra pixels in between rows, for example. The
10594  * returned rectangle is equivalent to the @cell_area passed to
10595  * gtk_cell_renderer_render().  This function is only valid if @tree_view is
10596  * realized.
10597  **/
10598 void
10599 pspp_sheet_view_get_cell_area (PsppSheetView        *tree_view,
10600                              GtkTreePath        *path,
10601                              PsppSheetViewColumn  *column,
10602                              GdkRectangle       *rect)
10603 {
10604   GdkRectangle background_area;
10605
10606   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10607   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10608   g_return_if_fail (rect != NULL);
10609   g_return_if_fail (!column || column->tree_view == (GtkWidget *) tree_view);
10610   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
10611
10612   pspp_sheet_view_get_background_area (tree_view, path, column,
10613                                        &background_area);
10614   pspp_sheet_view_adjust_cell_area (tree_view, column, &background_area,
10615                                     FALSE, rect);
10616 }
10617
10618 /**
10619  * pspp_sheet_view_get_background_area:
10620  * @tree_view: a #PsppSheetView
10621  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
10622  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordiantes
10623  * @rect: rectangle to fill with cell background rect
10624  *
10625  * Fills the bounding rectangle in bin_window coordinates for the cell at the
10626  * row specified by @path and the column specified by @column.  If @path is
10627  * %NULL, or points to a node not found in the tree, the @y and @height fields of
10628  * the rectangle will be filled with 0. If @column is %NULL, the @x and @width
10629  * fields will be filled with 0.  The returned rectangle is equivalent to the
10630  * @background_area passed to gtk_cell_renderer_render().  These background
10631  * areas tile to cover the entire bin window.  Contrast with the @cell_area,
10632  * returned by pspp_sheet_view_get_cell_area(), which returns only the cell
10633  * itself, excluding surrounding borders.
10634  *
10635  **/
10636 void
10637 pspp_sheet_view_get_background_area (PsppSheetView        *tree_view,
10638                                    GtkTreePath        *path,
10639                                    PsppSheetViewColumn  *column,
10640                                    GdkRectangle       *rect)
10641 {
10642   int node = -1;
10643
10644   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10645   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
10646   g_return_if_fail (rect != NULL);
10647
10648   rect->x = 0;
10649   rect->y = 0;
10650   rect->width = 0;
10651   rect->height = 0;
10652
10653   if (path)
10654     {
10655       /* Get vertical coords */
10656
10657       _pspp_sheet_view_find_node (tree_view, path, &node);
10658       if (node < 0)
10659         return;
10660
10661       rect->y = BACKGROUND_FIRST_PIXEL (tree_view, node);
10662
10663       rect->height = ROW_HEIGHT (tree_view);
10664     }
10665
10666   if (column)
10667     {
10668       gint x2 = 0;
10669
10670       pspp_sheet_view_get_background_xrange (tree_view, column, &rect->x, &x2);
10671       rect->width = x2 - rect->x;
10672     }
10673 }
10674
10675 /**
10676  * pspp_sheet_view_get_visible_rect:
10677  * @tree_view: a #PsppSheetView
10678  * @visible_rect: rectangle to fill
10679  *
10680  * Fills @visible_rect with the currently-visible region of the
10681  * buffer, in tree coordinates. Convert to bin_window coordinates with
10682  * pspp_sheet_view_convert_tree_to_bin_window_coords().
10683  * Tree coordinates start at 0,0 for row 0 of the tree, and cover the entire
10684  * scrollable area of the tree.
10685  **/
10686 void
10687 pspp_sheet_view_get_visible_rect (PsppSheetView  *tree_view,
10688                                 GdkRectangle *visible_rect)
10689 {
10690   GtkWidget *widget;
10691
10692   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10693
10694   widget = GTK_WIDGET (tree_view);
10695
10696   if (visible_rect)
10697     {
10698       GtkAllocation allocation;
10699       gtk_widget_get_allocation (widget, &allocation);
10700       visible_rect->x = gtk_adjustment_get_value (tree_view->priv->hadjustment);
10701       visible_rect->y = gtk_adjustment_get_value (tree_view->priv->vadjustment);
10702       visible_rect->width  = allocation.width;
10703       visible_rect->height = allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
10704     }
10705 }
10706
10707 /**
10708  * pspp_sheet_view_widget_to_tree_coords:
10709  * @tree_view: a #PsppSheetView
10710  * @wx: X coordinate relative to bin_window
10711  * @wy: Y coordinate relative to bin_window
10712  * @tx: return location for tree X coordinate
10713  * @ty: return location for tree Y coordinate
10714  *
10715  * Converts bin_window coordinates to coordinates for the
10716  * tree (the full scrollable area of the tree).
10717  *
10718  * Deprecated: 2.12: Due to historial reasons the name of this function is
10719  * incorrect.  For converting coordinates relative to the widget to
10720  * bin_window coordinates, please see
10721  * pspp_sheet_view_convert_widget_to_bin_window_coords().
10722  *
10723  **/
10724 void
10725 pspp_sheet_view_widget_to_tree_coords (PsppSheetView *tree_view,
10726                                       gint         wx,
10727                                       gint         wy,
10728                                       gint        *tx,
10729                                       gint        *ty)
10730 {
10731   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10732
10733   if (tx)
10734     *tx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10735   if (ty)
10736     *ty = wy + tree_view->priv->dy;
10737 }
10738
10739 /**
10740  * pspp_sheet_view_tree_to_widget_coords:
10741  * @tree_view: a #PsppSheetView
10742  * @tx: tree X coordinate
10743  * @ty: tree Y coordinate
10744  * @wx: return location for X coordinate relative to bin_window
10745  * @wy: return location for Y coordinate relative to bin_window
10746  *
10747  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10748  * to bin_window coordinates.
10749  *
10750  * Deprecated: 2.12: Due to historial reasons the name of this function is
10751  * incorrect.  For converting bin_window coordinates to coordinates relative
10752  * to bin_window, please see
10753  * pspp_sheet_view_convert_bin_window_to_widget_coords().
10754  *
10755  **/
10756 void
10757 pspp_sheet_view_tree_to_widget_coords (PsppSheetView *tree_view,
10758                                      gint         tx,
10759                                      gint         ty,
10760                                      gint        *wx,
10761                                      gint        *wy)
10762 {
10763   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10764
10765   if (wx)
10766     *wx = tx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10767   if (wy)
10768     *wy = ty - tree_view->priv->dy;
10769 }
10770
10771
10772 /**
10773  * pspp_sheet_view_convert_widget_to_tree_coords:
10774  * @tree_view: a #PsppSheetView
10775  * @wx: X coordinate relative to the widget
10776  * @wy: Y coordinate relative to the widget
10777  * @tx: return location for tree X coordinate
10778  * @ty: return location for tree Y coordinate
10779  *
10780  * Converts widget coordinates to coordinates for the
10781  * tree (the full scrollable area of the tree).
10782  *
10783  * Since: 2.12
10784  **/
10785 void
10786 pspp_sheet_view_convert_widget_to_tree_coords (PsppSheetView *tree_view,
10787                                              gint         wx,
10788                                              gint         wy,
10789                                              gint        *tx,
10790                                              gint        *ty)
10791 {
10792   gint x, y;
10793
10794   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10795
10796   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
10797                                                      wx, wy,
10798                                                      &x, &y);
10799   pspp_sheet_view_convert_bin_window_to_tree_coords (tree_view,
10800                                                    x, y,
10801                                                    tx, ty);
10802 }
10803
10804 /**
10805  * pspp_sheet_view_convert_tree_to_widget_coords:
10806  * @tree_view: a #PsppSheetView
10807  * @tx: X coordinate relative to the tree
10808  * @ty: Y coordinate relative to the tree
10809  * @wx: return location for widget X coordinate
10810  * @wy: return location for widget Y coordinate
10811  *
10812  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10813  * to widget coordinates.
10814  *
10815  * Since: 2.12
10816  **/
10817 void
10818 pspp_sheet_view_convert_tree_to_widget_coords (PsppSheetView *tree_view,
10819                                              gint         tx,
10820                                              gint         ty,
10821                                              gint        *wx,
10822                                              gint        *wy)
10823 {
10824   gint x, y;
10825
10826   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10827
10828   pspp_sheet_view_convert_tree_to_bin_window_coords (tree_view,
10829                                                    tx, ty,
10830                                                    &x, &y);
10831   pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
10832                                                      x, y,
10833                                                      wx, wy);
10834 }
10835
10836 /**
10837  * pspp_sheet_view_convert_widget_to_bin_window_coords:
10838  * @tree_view: a #PsppSheetView
10839  * @wx: X coordinate relative to the widget
10840  * @wy: Y coordinate relative to the widget
10841  * @bx: return location for bin_window X coordinate
10842  * @by: return location for bin_window Y coordinate
10843  *
10844  * Converts widget coordinates to coordinates for the bin_window
10845  * (see pspp_sheet_view_get_bin_window()).
10846  *
10847  * Since: 2.12
10848  **/
10849 void
10850 pspp_sheet_view_convert_widget_to_bin_window_coords (PsppSheetView *tree_view,
10851                                                    gint         wx,
10852                                                    gint         wy,
10853                                                    gint        *bx,
10854                                                    gint        *by)
10855 {
10856   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10857
10858   if (bx)
10859     *bx = wx + gtk_adjustment_get_value (tree_view->priv->hadjustment);
10860   if (by)
10861     *by = wy - TREE_VIEW_HEADER_HEIGHT (tree_view);
10862 }
10863
10864 /**
10865  * pspp_sheet_view_convert_bin_window_to_widget_coords:
10866  * @tree_view: a #PsppSheetView
10867  * @bx: bin_window X coordinate
10868  * @by: bin_window Y coordinate
10869  * @wx: return location for widget X coordinate
10870  * @wy: return location for widget Y coordinate
10871  *
10872  * Converts bin_window coordinates (see pspp_sheet_view_get_bin_window())
10873  * to widget relative coordinates.
10874  *
10875  * Since: 2.12
10876  **/
10877 void
10878 pspp_sheet_view_convert_bin_window_to_widget_coords (PsppSheetView *tree_view,
10879                                                    gint         bx,
10880                                                    gint         by,
10881                                                    gint        *wx,
10882                                                    gint        *wy)
10883 {
10884   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10885
10886   if (wx)
10887     *wx = bx - gtk_adjustment_get_value (tree_view->priv->hadjustment);
10888   if (wy)
10889     *wy = by + TREE_VIEW_HEADER_HEIGHT (tree_view);
10890 }
10891
10892 /**
10893  * pspp_sheet_view_convert_tree_to_bin_window_coords:
10894  * @tree_view: a #PsppSheetView
10895  * @tx: tree X coordinate
10896  * @ty: tree Y coordinate
10897  * @bx: return location for X coordinate relative to bin_window
10898  * @by: return location for Y coordinate relative to bin_window
10899  *
10900  * Converts tree coordinates (coordinates in full scrollable area of the tree)
10901  * to bin_window coordinates.
10902  *
10903  * Since: 2.12
10904  **/
10905 void
10906 pspp_sheet_view_convert_tree_to_bin_window_coords (PsppSheetView *tree_view,
10907                                                  gint         tx,
10908                                                  gint         ty,
10909                                                  gint        *bx,
10910                                                  gint        *by)
10911 {
10912   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10913
10914   if (bx)
10915     *bx = tx;
10916   if (by)
10917     *by = ty - tree_view->priv->dy;
10918 }
10919
10920 /**
10921  * pspp_sheet_view_convert_bin_window_to_tree_coords:
10922  * @tree_view: a #PsppSheetView
10923  * @bx: X coordinate relative to bin_window
10924  * @by: Y coordinate relative to bin_window
10925  * @tx: return location for tree X coordinate
10926  * @ty: return location for tree Y coordinate
10927  *
10928  * Converts bin_window coordinates to coordinates for the
10929  * tree (the full scrollable area of the tree).
10930  *
10931  * Since: 2.12
10932  **/
10933 void
10934 pspp_sheet_view_convert_bin_window_to_tree_coords (PsppSheetView *tree_view,
10935                                                  gint         bx,
10936                                                  gint         by,
10937                                                  gint        *tx,
10938                                                  gint        *ty)
10939 {
10940   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10941
10942   if (tx)
10943     *tx = bx;
10944   if (ty)
10945     *ty = by + tree_view->priv->dy;
10946 }
10947
10948
10949
10950 /**
10951  * pspp_sheet_view_get_visible_range:
10952  * @tree_view: A #PsppSheetView
10953  * @start_path: (allow-none): Return location for start of region, or %NULL.
10954  * @end_path: (allow-none): Return location for end of region, or %NULL.
10955  *
10956  * Sets @start_path and @end_path to be the first and last visible path.
10957  * Note that there may be invisible paths in between.
10958  *
10959  * The paths should be freed with gtk_tree_path_free() after use.
10960  *
10961  * Returns: %TRUE, if valid paths were placed in @start_path and @end_path.
10962  *
10963  * Since: 2.8
10964  **/
10965 gboolean
10966 pspp_sheet_view_get_visible_range (PsppSheetView  *tree_view,
10967                                  GtkTreePath **start_path,
10968                                  GtkTreePath **end_path)
10969 {
10970   int node;
10971   gboolean retval;
10972   
10973   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10974
10975   if (!tree_view->priv->row_count)
10976     return FALSE;
10977
10978   retval = TRUE;
10979
10980   if (start_path)
10981     {
10982       pspp_sheet_view_find_offset (tree_view,
10983                                    TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, 0),
10984                                    &node);
10985       if (node >= 0)
10986         *start_path = _pspp_sheet_view_find_path (tree_view, node);
10987       else
10988         retval = FALSE;
10989     }
10990
10991   if (end_path)
10992     {
10993       gint y;
10994
10995       if (tree_view->priv->height < gtk_adjustment_get_page_size (tree_view->priv->vadjustment))
10996         y = tree_view->priv->height - 1;
10997       else
10998         y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, gtk_adjustment_get_page_size (tree_view->priv->vadjustment)) - 1;
10999
11000       pspp_sheet_view_find_offset (tree_view, y, &node);
11001       if (node >= 0)
11002         *end_path = _pspp_sheet_view_find_path (tree_view, node);
11003       else
11004         retval = FALSE;
11005     }
11006
11007   return retval;
11008 }
11009
11010 static void
11011 unset_reorderable (PsppSheetView *tree_view)
11012 {
11013   if (tree_view->priv->reorderable)
11014     {
11015       tree_view->priv->reorderable = FALSE;
11016       g_object_notify (G_OBJECT (tree_view), "reorderable");
11017     }
11018 }
11019
11020 /**
11021  * pspp_sheet_view_enable_model_drag_source:
11022  * @tree_view: a #PsppSheetView
11023  * @start_button_mask: Mask of allowed buttons to start drag
11024  * @targets: the table of targets that the drag will support
11025  * @n_targets: the number of items in @targets
11026  * @actions: the bitmask of possible actions for a drag from this
11027  *    widget
11028  *
11029  * Turns @tree_view into a drag source for automatic DND. Calling this
11030  * method sets #PsppSheetView:reorderable to %FALSE.
11031  **/
11032 void
11033 pspp_sheet_view_enable_model_drag_source (PsppSheetView              *tree_view,
11034                                         GdkModifierType           start_button_mask,
11035                                         const GtkTargetEntry     *targets,
11036                                         gint                      n_targets,
11037                                         GdkDragAction             actions)
11038 {
11039   TreeViewDragInfo *di;
11040
11041   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11042
11043   gtk_drag_source_set (GTK_WIDGET (tree_view),
11044                        0,
11045                        targets,
11046                        n_targets,
11047                        actions);
11048
11049   di = ensure_info (tree_view);
11050
11051   di->start_button_mask = start_button_mask;
11052   di->source_actions = actions;
11053   di->source_set = TRUE;
11054
11055   unset_reorderable (tree_view);
11056 }
11057
11058 /**
11059  * pspp_sheet_view_enable_model_drag_dest:
11060  * @tree_view: a #PsppSheetView
11061  * @targets: the table of targets that the drag will support
11062  * @n_targets: the number of items in @targets
11063  * @actions: the bitmask of possible actions for a drag from this
11064  *    widget
11065  * 
11066  * Turns @tree_view into a drop destination for automatic DND. Calling
11067  * this method sets #PsppSheetView:reorderable to %FALSE.
11068  **/
11069 void
11070 pspp_sheet_view_enable_model_drag_dest (PsppSheetView              *tree_view,
11071                                       const GtkTargetEntry     *targets,
11072                                       gint                      n_targets,
11073                                       GdkDragAction             actions)
11074 {
11075   TreeViewDragInfo *di;
11076
11077   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11078
11079   gtk_drag_dest_set (GTK_WIDGET (tree_view),
11080                      0,
11081                      targets,
11082                      n_targets,
11083                      actions);
11084
11085   di = ensure_info (tree_view);
11086   di->dest_set = TRUE;
11087
11088   unset_reorderable (tree_view);
11089 }
11090
11091 /**
11092  * pspp_sheet_view_unset_rows_drag_source:
11093  * @tree_view: a #PsppSheetView
11094  *
11095  * Undoes the effect of
11096  * pspp_sheet_view_enable_model_drag_source(). Calling this method sets
11097  * #PsppSheetView:reorderable to %FALSE.
11098  **/
11099 void
11100 pspp_sheet_view_unset_rows_drag_source (PsppSheetView *tree_view)
11101 {
11102   TreeViewDragInfo *di;
11103
11104   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11105
11106   di = get_info (tree_view);
11107
11108   if (di)
11109     {
11110       if (di->source_set)
11111         {
11112           gtk_drag_source_unset (GTK_WIDGET (tree_view));
11113           di->source_set = FALSE;
11114         }
11115
11116       if (!di->dest_set && !di->source_set)
11117         remove_info (tree_view);
11118     }
11119   
11120   unset_reorderable (tree_view);
11121 }
11122
11123 /**
11124  * pspp_sheet_view_unset_rows_drag_dest:
11125  * @tree_view: a #PsppSheetView
11126  *
11127  * Undoes the effect of
11128  * pspp_sheet_view_enable_model_drag_dest(). Calling this method sets
11129  * #PsppSheetView:reorderable to %FALSE.
11130  **/
11131 void
11132 pspp_sheet_view_unset_rows_drag_dest (PsppSheetView *tree_view)
11133 {
11134   TreeViewDragInfo *di;
11135
11136   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11137
11138   di = get_info (tree_view);
11139
11140   if (di)
11141     {
11142       if (di->dest_set)
11143         {
11144           gtk_drag_dest_unset (GTK_WIDGET (tree_view));
11145           di->dest_set = FALSE;
11146         }
11147
11148       if (!di->dest_set && !di->source_set)
11149         remove_info (tree_view);
11150     }
11151
11152   unset_reorderable (tree_view);
11153 }
11154
11155 /**
11156  * pspp_sheet_view_set_drag_dest_row:
11157  * @tree_view: a #PsppSheetView
11158  * @path: (allow-none): The path of the row to highlight, or %NULL.
11159  * @pos: Specifies whether to drop before, after or into the row
11160  * 
11161  * Sets the row that is highlighted for feedback.
11162  **/
11163 void
11164 pspp_sheet_view_set_drag_dest_row (PsppSheetView            *tree_view,
11165                                  GtkTreePath            *path,
11166                                  PsppSheetViewDropPosition pos)
11167 {
11168   GtkTreePath *current_dest;
11169
11170   /* Note; this function is exported to allow a custom DND
11171    * implementation, so it can't touch TreeViewDragInfo
11172    */
11173
11174   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11175
11176   current_dest = NULL;
11177
11178   if (tree_view->priv->drag_dest_row)
11179     {
11180       current_dest = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11181       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
11182     }
11183
11184   /* special case a drop on an empty model */
11185   tree_view->priv->empty_view_drop = 0;
11186
11187   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE && path
11188       && gtk_tree_path_get_depth (path) == 1
11189       && gtk_tree_path_get_indices (path)[0] == 0)
11190     {
11191       gint n_children;
11192
11193       n_children = gtk_tree_model_iter_n_children (tree_view->priv->model,
11194                                                    NULL);
11195
11196       if (!n_children)
11197         tree_view->priv->empty_view_drop = 1;
11198     }
11199
11200   tree_view->priv->drag_dest_pos = pos;
11201
11202   if (path)
11203     {
11204       tree_view->priv->drag_dest_row =
11205         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
11206       pspp_sheet_view_queue_draw_path (tree_view, path, NULL);
11207     }
11208   else
11209     tree_view->priv->drag_dest_row = NULL;
11210
11211   if (current_dest)
11212     {
11213       int node, new_node;
11214
11215       _pspp_sheet_view_find_node (tree_view, current_dest, &node);
11216       _pspp_sheet_view_queue_draw_node (tree_view, node, NULL);
11217
11218       if (node >= 0)
11219         {
11220           new_node = pspp_sheet_view_node_next (tree_view, node);
11221           if (new_node >= 0)
11222             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11223
11224           new_node = pspp_sheet_view_node_prev (tree_view, node);
11225           if (new_node >= 0)
11226             _pspp_sheet_view_queue_draw_node (tree_view, new_node, NULL);
11227         }
11228       gtk_tree_path_free (current_dest);
11229     }
11230 }
11231
11232 /**
11233  * pspp_sheet_view_get_drag_dest_row:
11234  * @tree_view: a #PsppSheetView
11235  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11236  * @pos: (allow-none): Return location for the drop position, or %NULL
11237  * 
11238  * Gets information about the row that is highlighted for feedback.
11239  **/
11240 void
11241 pspp_sheet_view_get_drag_dest_row (PsppSheetView              *tree_view,
11242                                  GtkTreePath             **path,
11243                                  PsppSheetViewDropPosition  *pos)
11244 {
11245   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11246
11247   if (path)
11248     {
11249       if (tree_view->priv->drag_dest_row)
11250         *path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
11251       else
11252         {
11253           if (tree_view->priv->empty_view_drop)
11254             *path = gtk_tree_path_new_from_indices (0, -1);
11255           else
11256             *path = NULL;
11257         }
11258     }
11259
11260   if (pos)
11261     *pos = tree_view->priv->drag_dest_pos;
11262 }
11263
11264 /**
11265  * pspp_sheet_view_get_dest_row_at_pos:
11266  * @tree_view: a #PsppSheetView
11267  * @drag_x: the position to determine the destination row for
11268  * @drag_y: the position to determine the destination row for
11269  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
11270  * @pos: (allow-none): Return location for the drop position, or %NULL
11271  * 
11272  * Determines the destination row for a given position.  @drag_x and
11273  * @drag_y are expected to be in widget coordinates.  This function is only
11274  * meaningful if @tree_view is realized.  Therefore this function will always
11275  * return %FALSE if @tree_view is not realized or does not have a model.
11276  * 
11277  * Return value: whether there is a row at the given position, %TRUE if this
11278  * is indeed the case.
11279  **/
11280 gboolean
11281 pspp_sheet_view_get_dest_row_at_pos (PsppSheetView             *tree_view,
11282                                    gint                     drag_x,
11283                                    gint                     drag_y,
11284                                    GtkTreePath            **path,
11285                                    PsppSheetViewDropPosition *pos)
11286 {
11287   gint cell_y;
11288   gint bin_x, bin_y;
11289   gdouble offset_into_row;
11290   gdouble third;
11291   GdkRectangle cell;
11292   PsppSheetViewColumn *column = NULL;
11293   GtkTreePath *tmp_path = NULL;
11294
11295   /* Note; this function is exported to allow a custom DND
11296    * implementation, so it can't touch TreeViewDragInfo
11297    */
11298
11299   g_return_val_if_fail (tree_view != NULL, FALSE);
11300   g_return_val_if_fail (drag_x >= 0, FALSE);
11301   g_return_val_if_fail (drag_y >= 0, FALSE);
11302
11303   if (path)
11304     *path = NULL;
11305
11306   if (tree_view->priv->bin_window == NULL)
11307     return FALSE;
11308
11309   if (tree_view->priv->row_count == 0)
11310     return FALSE;
11311
11312   /* If in the top third of a row, we drop before that row; if
11313    * in the bottom third, drop after that row; if in the middle,
11314    * and the row has children, drop into the row.
11315    */
11316   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, drag_x, drag_y,
11317                                                      &bin_x, &bin_y);
11318
11319   if (!pspp_sheet_view_get_path_at_pos (tree_view,
11320                                       bin_x,
11321                                       bin_y,
11322                                       &tmp_path,
11323                                       &column,
11324                                       NULL,
11325                                       &cell_y))
11326     return FALSE;
11327
11328   pspp_sheet_view_get_background_area (tree_view, tmp_path, column,
11329                                      &cell);
11330
11331   offset_into_row = cell_y;
11332
11333   if (path)
11334     *path = tmp_path;
11335   else
11336     gtk_tree_path_free (tmp_path);
11337
11338   tmp_path = NULL;
11339
11340   third = cell.height / 3.0;
11341
11342   if (pos)
11343     {
11344       if (offset_into_row < third)
11345         {
11346           *pos = PSPP_SHEET_VIEW_DROP_BEFORE;
11347         }
11348       else if (offset_into_row < (cell.height / 2.0))
11349         {
11350           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE;
11351         }
11352       else if (offset_into_row < third * 2.0)
11353         {
11354           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER;
11355         }
11356       else
11357         {
11358           *pos = PSPP_SHEET_VIEW_DROP_AFTER;
11359         }
11360     }
11361
11362   return TRUE;
11363 }
11364
11365
11366 #if GTK3_TRANSITION
11367 /* KEEP IN SYNC WITH PSPP_SHEET_VIEW_BIN_EXPOSE */
11368 /**
11369  * pspp_sheet_view_create_row_drag_icon:
11370  * @tree_view: a #PsppSheetView
11371  * @path: a #GtkTreePath in @tree_view
11372  *
11373  * Creates a #GdkPixmap representation of the row at @path.  
11374  * This image is used for a drag icon.
11375  *
11376  * Return value: a newly-allocated pixmap of the drag icon.
11377  **/
11378 GdkPixmap *
11379 pspp_sheet_view_create_row_drag_icon (PsppSheetView  *tree_view,
11380                                     GtkTreePath  *path)
11381 {
11382   GtkTreeIter   iter;
11383   int node;
11384   gint cell_offset;
11385   GList *list;
11386   GdkRectangle background_area;
11387   GdkRectangle expose_area;
11388   GtkWidget *widget;
11389   /* start drawing inside the black outline */
11390   gint x = 1, y = 1;
11391   GdkDrawable *drawable;
11392   gint bin_window_width;
11393   gboolean rtl;
11394
11395   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11396   g_return_val_if_fail (path != NULL, NULL);
11397
11398   widget = GTK_WIDGET (tree_view);
11399
11400   if (!gtk_widget_get_realized (widget))
11401     return NULL;
11402
11403   _pspp_sheet_view_find_node (tree_view,
11404                             path,
11405                             &node);
11406
11407   if (node < 0)
11408     return NULL;
11409
11410   if (!gtk_tree_model_get_iter (tree_view->priv->model,
11411                                 &iter,
11412                                 path))
11413     return NULL;
11414   
11415   cell_offset = x;
11416
11417   background_area.y = y;
11418   background_area.height = ROW_HEIGHT (tree_view);
11419
11420   bin_window_width = gdk_window_get_width (tree_view->priv->bin_window);
11421
11422   drawable = gdk_pixmap_new (tree_view->priv->bin_window,
11423                              bin_window_width + 2,
11424                              background_area.height + 2,
11425                              -1);
11426
11427   expose_area.x = 0;
11428   expose_area.y = 0;
11429   expose_area.width = bin_window_width + 2;
11430   expose_area.height = background_area.height + 2;
11431
11432 #if GTK3_TRANSITION
11433   gdk_draw_rectangle (drawable,
11434                       widget->style->base_gc [gtk_widget_get_state (widget)],
11435                       TRUE,
11436                       0, 0,
11437                       bin_window_width + 2,
11438                       background_area.height + 2);
11439 #endif
11440
11441   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
11442
11443   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
11444       list;
11445       list = (rtl ? list->prev : list->next))
11446     {
11447       PsppSheetViewColumn *column = list->data;
11448       GdkRectangle cell_area;
11449       gint vertical_separator;
11450
11451       if (!column->visible)
11452         continue;
11453
11454       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, &iter);
11455
11456       background_area.x = cell_offset;
11457       background_area.width = column->width;
11458
11459       gtk_widget_style_get (widget,
11460                             "vertical-separator", &vertical_separator,
11461                             NULL);
11462
11463       cell_area = background_area;
11464
11465       cell_area.y += vertical_separator / 2;
11466       cell_area.height -= vertical_separator;
11467
11468       if (pspp_sheet_view_column_cell_is_visible (column))
11469         _pspp_sheet_view_column_cell_render (column,
11470                                              drawable,
11471                                              &background_area,
11472                                              &cell_area,
11473                                              &expose_area,
11474                                              0);
11475       cell_offset += column->width;
11476     }
11477
11478 #if GTK3_TRANSITION
11479   gdk_draw_rectangle (drawable,
11480                       widget->style->black_gc,
11481                       FALSE,
11482                       0, 0,
11483                       bin_window_width + 1,
11484                       background_area.height + 1);
11485 #endif
11486
11487   return drawable;
11488 }
11489 #endif
11490
11491 /**
11492  * pspp_sheet_view_set_destroy_count_func:
11493  * @tree_view: A #PsppSheetView
11494  * @func: (allow-none): Function to be called when a view row is destroyed, or %NULL
11495  * @data: (allow-none): User data to be passed to @func, or %NULL
11496  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11497  *
11498  * This function should almost never be used.  It is meant for private use by
11499  * ATK for determining the number of visible children that are removed when a row is deleted.
11500  **/
11501 void
11502 pspp_sheet_view_set_destroy_count_func (PsppSheetView             *tree_view,
11503                                       PsppSheetDestroyCountFunc  func,
11504                                       gpointer                 data,
11505                                       GDestroyNotify           destroy)
11506 {
11507   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11508
11509   if (tree_view->priv->destroy_count_destroy)
11510     tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
11511
11512   tree_view->priv->destroy_count_func = func;
11513   tree_view->priv->destroy_count_data = data;
11514   tree_view->priv->destroy_count_destroy = destroy;
11515 }
11516
11517
11518 /*
11519  * Interactive search
11520  */
11521
11522 /**
11523  * pspp_sheet_view_set_enable_search:
11524  * @tree_view: A #PsppSheetView
11525  * @enable_search: %TRUE, if the user can search interactively
11526  *
11527  * If @enable_search is set, then the user can type in text to search through
11528  * the tree interactively (this is sometimes called "typeahead find").
11529  * 
11530  * Note that even if this is %FALSE, the user can still initiate a search 
11531  * using the "start-interactive-search" key binding.
11532  */
11533 void
11534 pspp_sheet_view_set_enable_search (PsppSheetView *tree_view,
11535                                  gboolean     enable_search)
11536 {
11537   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11538
11539   enable_search = !!enable_search;
11540   
11541   if (tree_view->priv->enable_search != enable_search)
11542     {
11543        tree_view->priv->enable_search = enable_search;
11544        g_object_notify (G_OBJECT (tree_view), "enable-search");
11545     }
11546 }
11547
11548 /**
11549  * pspp_sheet_view_get_enable_search:
11550  * @tree_view: A #PsppSheetView
11551  *
11552  * Returns whether or not the tree allows to start interactive searching 
11553  * by typing in text.
11554  *
11555  * Return value: whether or not to let the user search interactively
11556  */
11557 gboolean
11558 pspp_sheet_view_get_enable_search (PsppSheetView *tree_view)
11559 {
11560   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11561
11562   return tree_view->priv->enable_search;
11563 }
11564
11565
11566 /**
11567  * pspp_sheet_view_get_search_column:
11568  * @tree_view: A #PsppSheetView
11569  *
11570  * Gets the column searched on by the interactive search code.
11571  *
11572  * Return value: the column the interactive search code searches in.
11573  */
11574 gint
11575 pspp_sheet_view_get_search_column (PsppSheetView *tree_view)
11576 {
11577   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11578
11579   return (tree_view->priv->search_column);
11580 }
11581
11582 /**
11583  * pspp_sheet_view_set_search_column:
11584  * @tree_view: A #PsppSheetView
11585  * @column: the column of the model to search in, or -1 to disable searching
11586  *
11587  * Sets @column as the column where the interactive search code should
11588  * search in for the current model. 
11589  * 
11590  * If the search column is set, users can use the "start-interactive-search"
11591  * key binding to bring up search popup. The enable-search property controls
11592  * whether simply typing text will also start an interactive search.
11593  *
11594  * Note that @column refers to a column of the current model. The search 
11595  * column is reset to -1 when the model is changed.
11596  */
11597 void
11598 pspp_sheet_view_set_search_column (PsppSheetView *tree_view,
11599                                  gint         column)
11600 {
11601   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11602   g_return_if_fail (column >= -1);
11603
11604   if (tree_view->priv->search_column == column)
11605     return;
11606
11607   tree_view->priv->search_column = column;
11608   g_object_notify (G_OBJECT (tree_view), "search-column");
11609 }
11610
11611 /**
11612  * pspp_sheet_view_get_search_equal_func:
11613  * @tree_view: A #PsppSheetView
11614  *
11615  * Returns the compare function currently in use.
11616  *
11617  * Return value: the currently used compare function for the search code.
11618  */
11619
11620 PsppSheetViewSearchEqualFunc
11621 pspp_sheet_view_get_search_equal_func (PsppSheetView *tree_view)
11622 {
11623   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11624
11625   return tree_view->priv->search_equal_func;
11626 }
11627
11628 /**
11629  * pspp_sheet_view_set_search_equal_func:
11630  * @tree_view: A #PsppSheetView
11631  * @search_equal_func: the compare function to use during the search
11632  * @search_user_data: (allow-none): user data to pass to @search_equal_func, or %NULL
11633  * @search_destroy: (allow-none): Destroy notifier for @search_user_data, or %NULL
11634  *
11635  * Sets the compare function for the interactive search capabilities; note
11636  * that somewhat like strcmp() returning 0 for equality
11637  * #PsppSheetViewSearchEqualFunc returns %FALSE on matches.
11638  **/
11639 void
11640 pspp_sheet_view_set_search_equal_func (PsppSheetView                *tree_view,
11641                                      PsppSheetViewSearchEqualFunc  search_equal_func,
11642                                      gpointer                    search_user_data,
11643                                      GDestroyNotify              search_destroy)
11644 {
11645   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11646   g_return_if_fail (search_equal_func != NULL);
11647
11648   if (tree_view->priv->search_destroy)
11649     tree_view->priv->search_destroy (tree_view->priv->search_user_data);
11650
11651   tree_view->priv->search_equal_func = search_equal_func;
11652   tree_view->priv->search_user_data = search_user_data;
11653   tree_view->priv->search_destroy = search_destroy;
11654   if (tree_view->priv->search_equal_func == NULL)
11655     tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
11656 }
11657
11658 /**
11659  * pspp_sheet_view_get_search_entry:
11660  * @tree_view: A #PsppSheetView
11661  *
11662  * Returns the #GtkEntry which is currently in use as interactive search
11663  * entry for @tree_view.  In case the built-in entry is being used, %NULL
11664  * will be returned.
11665  *
11666  * Return value: the entry currently in use as search entry.
11667  *
11668  * Since: 2.10
11669  */
11670 GtkEntry *
11671 pspp_sheet_view_get_search_entry (PsppSheetView *tree_view)
11672 {
11673   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11674
11675   if (tree_view->priv->search_custom_entry_set)
11676     return GTK_ENTRY (tree_view->priv->search_entry);
11677
11678   return NULL;
11679 }
11680
11681 /**
11682  * pspp_sheet_view_set_search_entry:
11683  * @tree_view: A #PsppSheetView
11684  * @entry: (allow-none): the entry the interactive search code of @tree_view should use or %NULL
11685  *
11686  * Sets the entry which the interactive search code will use for this
11687  * @tree_view.  This is useful when you want to provide a search entry
11688  * in our interface at all time at a fixed position.  Passing %NULL for
11689  * @entry will make the interactive search code use the built-in popup
11690  * entry again.
11691  *
11692  * Since: 2.10
11693  */
11694 void
11695 pspp_sheet_view_set_search_entry (PsppSheetView *tree_view,
11696                                 GtkEntry    *entry)
11697 {
11698   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11699   g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
11700
11701   if (tree_view->priv->search_custom_entry_set)
11702     {
11703       if (tree_view->priv->search_entry_changed_id)
11704         {
11705           g_signal_handler_disconnect (tree_view->priv->search_entry,
11706                                        tree_view->priv->search_entry_changed_id);
11707           tree_view->priv->search_entry_changed_id = 0;
11708         }
11709       g_signal_handlers_disconnect_by_func (tree_view->priv->search_entry,
11710                                             G_CALLBACK (pspp_sheet_view_search_key_press_event),
11711                                             tree_view);
11712
11713       g_object_unref (tree_view->priv->search_entry);
11714     }
11715   else if (tree_view->priv->search_window)
11716     {
11717       gtk_widget_destroy (tree_view->priv->search_window);
11718
11719       tree_view->priv->search_window = NULL;
11720     }
11721
11722   if (entry)
11723     {
11724       tree_view->priv->search_entry = g_object_ref (entry);
11725       tree_view->priv->search_custom_entry_set = TRUE;
11726
11727       if (tree_view->priv->search_entry_changed_id == 0)
11728         {
11729           tree_view->priv->search_entry_changed_id =
11730             g_signal_connect (tree_view->priv->search_entry, "changed",
11731                               G_CALLBACK (pspp_sheet_view_search_init),
11732                               tree_view);
11733         }
11734       
11735         g_signal_connect (tree_view->priv->search_entry, "key-press-event",
11736                           G_CALLBACK (pspp_sheet_view_search_key_press_event),
11737                           tree_view);
11738
11739         pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
11740     }
11741   else
11742     {
11743       tree_view->priv->search_entry = NULL;
11744       tree_view->priv->search_custom_entry_set = FALSE;
11745     }
11746 }
11747
11748 /**
11749  * pspp_sheet_view_set_search_position_func:
11750  * @tree_view: A #PsppSheetView
11751  * @func: (allow-none): the function to use to position the search dialog, or %NULL
11752  *    to use the default search position function
11753  * @data: (allow-none): user data to pass to @func, or %NULL
11754  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
11755  *
11756  * Sets the function to use when positioning the search dialog.
11757  *
11758  * Since: 2.10
11759  **/
11760 void
11761 pspp_sheet_view_set_search_position_func (PsppSheetView                   *tree_view,
11762                                         PsppSheetViewSearchPositionFunc  func,
11763                                         gpointer                       user_data,
11764                                         GDestroyNotify                 destroy)
11765 {
11766   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11767
11768   if (tree_view->priv->search_position_destroy)
11769     tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
11770
11771   tree_view->priv->search_position_func = func;
11772   tree_view->priv->search_position_user_data = user_data;
11773   tree_view->priv->search_position_destroy = destroy;
11774   if (tree_view->priv->search_position_func == NULL)
11775     tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
11776 }
11777
11778 /**
11779  * pspp_sheet_view_get_search_position_func:
11780  * @tree_view: A #PsppSheetView
11781  *
11782  * Returns the positioning function currently in use.
11783  *
11784  * Return value: the currently used function for positioning the search dialog.
11785  *
11786  * Since: 2.10
11787  */
11788 PsppSheetViewSearchPositionFunc
11789 pspp_sheet_view_get_search_position_func (PsppSheetView *tree_view)
11790 {
11791   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11792
11793   return tree_view->priv->search_position_func;
11794 }
11795
11796
11797 static void
11798 pspp_sheet_view_search_dialog_hide (GtkWidget   *search_dialog,
11799                                   PsppSheetView *tree_view)
11800 {
11801   if (tree_view->priv->disable_popdown)
11802     return;
11803
11804   if (tree_view->priv->search_entry_changed_id)
11805     {
11806       g_signal_handler_disconnect (tree_view->priv->search_entry,
11807                                    tree_view->priv->search_entry_changed_id);
11808       tree_view->priv->search_entry_changed_id = 0;
11809     }
11810   if (tree_view->priv->typeselect_flush_timeout)
11811     {
11812       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11813       tree_view->priv->typeselect_flush_timeout = 0;
11814     }
11815         
11816   if (gtk_widget_get_visible (search_dialog))
11817     {
11818       /* send focus-in event */
11819       send_focus_change (GTK_WIDGET (tree_view->priv->search_entry), FALSE);
11820       gtk_widget_hide (search_dialog);
11821       gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
11822       send_focus_change (GTK_WIDGET (tree_view), TRUE);
11823     }
11824 }
11825
11826 static void
11827 pspp_sheet_view_search_position_func (PsppSheetView *tree_view,
11828                                     GtkWidget   *search_dialog,
11829                                     gpointer     user_data)
11830 {
11831   gint x, y;
11832   gint tree_x, tree_y;
11833   gint tree_width, tree_height;
11834   GdkWindow *tree_window = gtk_widget_get_window (GTK_WIDGET (tree_view));
11835   GdkScreen *screen = gdk_window_get_screen (tree_window);
11836   GtkRequisition requisition;
11837   gint monitor_num;
11838   GdkRectangle monitor;
11839
11840   monitor_num = gdk_screen_get_monitor_at_window (screen, tree_window);
11841   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
11842
11843   gtk_widget_realize (search_dialog);
11844
11845   gdk_window_get_origin (tree_window, &tree_x, &tree_y);
11846   tree_width = gdk_window_get_width (tree_window);
11847   tree_height = gdk_window_get_height (tree_window);
11848
11849   gtk_widget_size_request (search_dialog, &requisition);
11850
11851   if (tree_x + tree_width > gdk_screen_get_width (screen))
11852     x = gdk_screen_get_width (screen) - requisition.width;
11853   else if (tree_x + tree_width - requisition.width < 0)
11854     x = 0;
11855   else
11856     x = tree_x + tree_width - requisition.width;
11857
11858   if (tree_y + tree_height + requisition.height > gdk_screen_get_height (screen))
11859     y = gdk_screen_get_height (screen) - requisition.height;
11860   else if (tree_y + tree_height < 0) /* isn't really possible ... */
11861     y = 0;
11862   else
11863     y = tree_y + tree_height;
11864
11865   gtk_window_move (GTK_WINDOW (search_dialog), x, y);
11866 }
11867
11868 static void
11869 pspp_sheet_view_search_disable_popdown (GtkEntry *entry,
11870                                       GtkMenu  *menu,
11871                                       gpointer  data)
11872 {
11873   PsppSheetView *tree_view = (PsppSheetView *)data;
11874
11875   tree_view->priv->disable_popdown = 1;
11876   g_signal_connect (menu, "hide",
11877                     G_CALLBACK (pspp_sheet_view_search_enable_popdown), data);
11878 }
11879
11880 #if GTK3_TRANSITION
11881 /* Because we're visible but offscreen, we just set a flag in the preedit
11882  * callback.
11883  */
11884 static void
11885 pspp_sheet_view_search_preedit_changed (GtkIMContext *im_context,
11886                                       PsppSheetView  *tree_view)
11887 {
11888   tree_view->priv->imcontext_changed = 1;
11889   if (tree_view->priv->typeselect_flush_timeout)
11890     {
11891       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11892       tree_view->priv->typeselect_flush_timeout =
11893         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11894                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11895                        tree_view);
11896     }
11897
11898 }
11899 #endif
11900
11901 static void
11902 pspp_sheet_view_search_activate (GtkEntry    *entry,
11903                                PsppSheetView *tree_view)
11904 {
11905   GtkTreePath *path;
11906   int node;
11907
11908   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window,
11909                                     tree_view);
11910
11911   /* If we have a row selected and it's the cursor row, we activate
11912    * the row XXX */
11913   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
11914     {
11915       path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
11916       
11917       _pspp_sheet_view_find_node (tree_view, path, &node);
11918       
11919       if (node >= 0 && pspp_sheet_view_node_is_selected (tree_view, node))
11920         pspp_sheet_view_row_activated (tree_view, path, tree_view->priv->focus_column);
11921       
11922       gtk_tree_path_free (path);
11923     }
11924 }
11925
11926 static gboolean
11927 pspp_sheet_view_real_search_enable_popdown (gpointer data)
11928 {
11929   PsppSheetView *tree_view = (PsppSheetView *)data;
11930
11931   tree_view->priv->disable_popdown = 0;
11932
11933   return FALSE;
11934 }
11935
11936 static void
11937 pspp_sheet_view_search_enable_popdown (GtkWidget *widget,
11938                                      gpointer   data)
11939 {
11940   gdk_threads_add_timeout_full (G_PRIORITY_HIGH, 200, pspp_sheet_view_real_search_enable_popdown, g_object_ref (data), g_object_unref);
11941 }
11942
11943 static gboolean
11944 pspp_sheet_view_search_delete_event (GtkWidget *widget,
11945                                    GdkEventAny *event,
11946                                    PsppSheetView *tree_view)
11947 {
11948   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11949
11950   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11951
11952   return TRUE;
11953 }
11954
11955 static gboolean
11956 pspp_sheet_view_search_button_press_event (GtkWidget *widget,
11957                                          GdkEventButton *event,
11958                                          PsppSheetView *tree_view)
11959 {
11960   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11961
11962   pspp_sheet_view_search_dialog_hide (widget, tree_view);
11963
11964   if (event->window == tree_view->priv->bin_window)
11965     pspp_sheet_view_button_press (GTK_WIDGET (tree_view), event);
11966
11967   return TRUE;
11968 }
11969
11970 static gboolean
11971 pspp_sheet_view_search_scroll_event (GtkWidget *widget,
11972                                    GdkEventScroll *event,
11973                                    PsppSheetView *tree_view)
11974 {
11975   gboolean retval = FALSE;
11976
11977   if (event->direction == GDK_SCROLL_UP)
11978     {
11979       pspp_sheet_view_search_move (widget, tree_view, TRUE);
11980       retval = TRUE;
11981     }
11982   else if (event->direction == GDK_SCROLL_DOWN)
11983     {
11984       pspp_sheet_view_search_move (widget, tree_view, FALSE);
11985       retval = TRUE;
11986     }
11987
11988   /* renew the flush timeout */
11989   if (retval && tree_view->priv->typeselect_flush_timeout
11990       && !tree_view->priv->search_custom_entry_set)
11991     {
11992       g_source_remove (tree_view->priv->typeselect_flush_timeout);
11993       tree_view->priv->typeselect_flush_timeout =
11994         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
11995                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
11996                        tree_view);
11997     }
11998
11999   return retval;
12000 }
12001
12002 static gboolean
12003 pspp_sheet_view_search_key_press_event (GtkWidget *widget,
12004                                       GdkEventKey *event,
12005                                       PsppSheetView *tree_view)
12006 {
12007   gboolean retval = FALSE;
12008
12009   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12010   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12011
12012   /* close window and cancel the search */
12013   if (!tree_view->priv->search_custom_entry_set
12014       && (event->keyval == GDK_Escape ||
12015           event->keyval == GDK_Tab ||
12016             event->keyval == GDK_KP_Tab ||
12017             event->keyval == GDK_ISO_Left_Tab))
12018     {
12019       pspp_sheet_view_search_dialog_hide (widget, tree_view);
12020       return TRUE;
12021     }
12022
12023   /* select previous matching iter */
12024   if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
12025     {
12026       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
12027         gtk_widget_error_bell (widget);
12028
12029       retval = TRUE;
12030     }
12031
12032   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK))
12033       && (event->keyval == GDK_g || event->keyval == GDK_G))
12034     {
12035       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
12036         gtk_widget_error_bell (widget);
12037
12038       retval = TRUE;
12039     }
12040
12041   /* select next matching iter */
12042   if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
12043     {
12044       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12045         gtk_widget_error_bell (widget);
12046
12047       retval = TRUE;
12048     }
12049
12050   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == GTK_DEFAULT_ACCEL_MOD_MASK)
12051       && (event->keyval == GDK_g || event->keyval == GDK_G))
12052     {
12053       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
12054         gtk_widget_error_bell (widget);
12055
12056       retval = TRUE;
12057     }
12058
12059   /* renew the flush timeout */
12060   if (retval && tree_view->priv->typeselect_flush_timeout
12061       && !tree_view->priv->search_custom_entry_set)
12062     {
12063       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12064       tree_view->priv->typeselect_flush_timeout =
12065         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12066                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12067                        tree_view);
12068     }
12069
12070   return retval;
12071 }
12072
12073 /*  this function returns FALSE if there is a search string but
12074  *  nothing was found, and TRUE otherwise.
12075  */
12076 static gboolean
12077 pspp_sheet_view_search_move (GtkWidget   *window,
12078                            PsppSheetView *tree_view,
12079                            gboolean     up)
12080 {
12081   gboolean ret;
12082   gint len;
12083   gint count = 0;
12084   const gchar *text;
12085   GtkTreeIter iter;
12086   GtkTreeModel *model;
12087   PsppSheetSelection *selection;
12088
12089   text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
12090
12091   g_return_val_if_fail (text != NULL, FALSE);
12092
12093   len = strlen (text);
12094
12095   if (up && tree_view->priv->selected_iter == 1)
12096     return strlen (text) < 1;
12097
12098   len = strlen (text);
12099
12100   if (len < 1)
12101     return TRUE;
12102
12103   model = pspp_sheet_view_get_model (tree_view);
12104   selection = pspp_sheet_view_get_selection (tree_view);
12105
12106   /* search */
12107   pspp_sheet_selection_unselect_all (selection);
12108   if (!gtk_tree_model_get_iter_first (model, &iter))
12109     return TRUE;
12110
12111   ret = pspp_sheet_view_search_iter (model, selection, &iter, text,
12112                                    &count, up?((tree_view->priv->selected_iter) - 1):((tree_view->priv->selected_iter + 1)));
12113
12114   if (ret)
12115     {
12116       /* found */
12117       tree_view->priv->selected_iter += up?(-1):(1);
12118       return TRUE;
12119     }
12120   else
12121     {
12122       /* return to old iter */
12123       count = 0;
12124       gtk_tree_model_get_iter_first (model, &iter);
12125       pspp_sheet_view_search_iter (model, selection,
12126                                  &iter, text,
12127                                  &count, tree_view->priv->selected_iter);
12128       return FALSE;
12129     }
12130 }
12131
12132 static gboolean
12133 pspp_sheet_view_search_equal_func (GtkTreeModel *model,
12134                                  gint          column,
12135                                  const gchar  *key,
12136                                  GtkTreeIter  *iter,
12137                                  gpointer      search_data)
12138 {
12139   gboolean retval = TRUE;
12140   const gchar *str;
12141   gchar *normalized_string;
12142   gchar *normalized_key;
12143   gchar *case_normalized_string = NULL;
12144   gchar *case_normalized_key = NULL;
12145   GValue value = {0,};
12146   GValue transformed = {0,};
12147
12148   gtk_tree_model_get_value (model, iter, column, &value);
12149
12150   g_value_init (&transformed, G_TYPE_STRING);
12151
12152   if (!g_value_transform (&value, &transformed))
12153     {
12154       g_value_unset (&value);
12155       return TRUE;
12156     }
12157
12158   g_value_unset (&value);
12159
12160   str = g_value_get_string (&transformed);
12161   if (!str)
12162     {
12163       g_value_unset (&transformed);
12164       return TRUE;
12165     }
12166
12167   normalized_string = g_utf8_normalize (str, -1, G_NORMALIZE_ALL);
12168   normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
12169
12170   if (normalized_string && normalized_key)
12171     {
12172       case_normalized_string = g_utf8_casefold (normalized_string, -1);
12173       case_normalized_key = g_utf8_casefold (normalized_key, -1);
12174
12175       if (strncmp (case_normalized_key, case_normalized_string, strlen (case_normalized_key)) == 0)
12176         retval = FALSE;
12177     }
12178
12179   g_value_unset (&transformed);
12180   g_free (normalized_key);
12181   g_free (normalized_string);
12182   g_free (case_normalized_key);
12183   g_free (case_normalized_string);
12184
12185   return retval;
12186 }
12187
12188 static gboolean
12189 pspp_sheet_view_search_iter (GtkTreeModel     *model,
12190                              PsppSheetSelection *selection,
12191                              GtkTreeIter      *iter,
12192                              const gchar      *text,
12193                              gint             *count,
12194                              gint              n)
12195 {
12196   int node = -1;
12197   GtkTreePath *path;
12198
12199   PsppSheetView *tree_view = pspp_sheet_selection_get_tree_view (selection);
12200
12201   path = gtk_tree_model_get_path (model, iter);
12202   _pspp_sheet_view_find_node (tree_view, path, &node);
12203
12204   do
12205     {
12206       gboolean done = FALSE;
12207
12208       if (! tree_view->priv->search_equal_func (model, tree_view->priv->search_column, text, iter, tree_view->priv->search_user_data))
12209         {
12210           (*count)++;
12211           if (*count == n)
12212             {
12213               pspp_sheet_view_scroll_to_cell (tree_view, path, NULL,
12214                                               TRUE, 0.5, 0.0);
12215               pspp_sheet_selection_select_iter (selection, iter);
12216               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12217
12218               if (path)
12219                 gtk_tree_path_free (path);
12220
12221               return TRUE;
12222             }
12223         }
12224
12225
12226       do
12227         {
12228           node = pspp_sheet_view_node_next (tree_view, node);
12229
12230           if (node >= 0)
12231             {
12232               gboolean has_next;
12233
12234               has_next = gtk_tree_model_iter_next (model, iter);
12235
12236               done = TRUE;
12237               gtk_tree_path_next (path);
12238
12239               /* sanity check */
12240               TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
12241             }
12242           else
12243             {
12244               if (path)
12245                 gtk_tree_path_free (path);
12246
12247               /* we've run out of tree, done with this func */
12248               return FALSE;
12249             }
12250         }
12251       while (!done);
12252     }
12253   while (1);
12254
12255   return FALSE;
12256 }
12257
12258 static void
12259 pspp_sheet_view_search_init (GtkWidget   *entry,
12260                            PsppSheetView *tree_view)
12261 {
12262   gint ret;
12263   gint count = 0;
12264   const gchar *text;
12265   GtkTreeIter iter;
12266   GtkTreeModel *model;
12267   PsppSheetSelection *selection;
12268
12269   g_return_if_fail (GTK_IS_ENTRY (entry));
12270   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12271
12272   text = gtk_entry_get_text (GTK_ENTRY (entry));
12273
12274   model = pspp_sheet_view_get_model (tree_view);
12275   selection = pspp_sheet_view_get_selection (tree_view);
12276
12277   /* search */
12278   pspp_sheet_selection_unselect_all (selection);
12279   if (tree_view->priv->typeselect_flush_timeout
12280       && !tree_view->priv->search_custom_entry_set)
12281     {
12282       g_source_remove (tree_view->priv->typeselect_flush_timeout);
12283       tree_view->priv->typeselect_flush_timeout =
12284         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
12285                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
12286                        tree_view);
12287     }
12288
12289   if (*text == '\0')
12290     return;
12291
12292   if (!gtk_tree_model_get_iter_first (model, &iter))
12293     return;
12294
12295   ret = pspp_sheet_view_search_iter (model, selection,
12296                                    &iter, text,
12297                                    &count, 1);
12298
12299   if (ret)
12300     tree_view->priv->selected_iter = 1;
12301 }
12302
12303 static void
12304 pspp_sheet_view_remove_widget (GtkCellEditable *cell_editable,
12305                              PsppSheetView     *tree_view)
12306 {
12307   if (tree_view->priv->edited_column == NULL)
12308     return;
12309
12310   _pspp_sheet_view_column_stop_editing (tree_view->priv->edited_column);
12311   tree_view->priv->edited_column = NULL;
12312
12313   if (gtk_widget_has_focus (GTK_WIDGET (cell_editable)))
12314     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12315
12316   g_signal_handlers_disconnect_by_func (cell_editable,
12317                                         pspp_sheet_view_remove_widget,
12318                                         tree_view);
12319   g_signal_handlers_disconnect_by_func (cell_editable,
12320                                         pspp_sheet_view_editable_button_press_event,
12321                                         tree_view);
12322   g_signal_handlers_disconnect_by_func (cell_editable,
12323                                         pspp_sheet_view_editable_clicked,
12324                                         tree_view);
12325
12326   gtk_container_remove (GTK_CONTAINER (tree_view),
12327                         GTK_WIDGET (cell_editable));  
12328
12329   /* FIXME should only redraw a single node */
12330   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12331 }
12332
12333 static gboolean
12334 pspp_sheet_view_start_editing (PsppSheetView *tree_view,
12335                              GtkTreePath *cursor_path)
12336 {
12337   GtkTreeIter iter;
12338   GdkRectangle background_area;
12339   GdkRectangle cell_area;
12340   GtkCellEditable *editable_widget = NULL;
12341   gchar *path_string;
12342   guint flags = 0; /* can be 0, as the flags are primarily for rendering */
12343   gint retval = FALSE;
12344   int cursor_node;
12345
12346   g_assert (tree_view->priv->focus_column);
12347
12348   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
12349     return FALSE;
12350
12351   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_node);
12352   if (cursor_node < 0)
12353     return FALSE;
12354
12355   path_string = gtk_tree_path_to_string (cursor_path);
12356   gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path);
12357
12358   pspp_sheet_view_column_cell_set_cell_data (tree_view->priv->focus_column,
12359                                            tree_view->priv->model,
12360                                            &iter);
12361   pspp_sheet_view_get_background_area (tree_view,
12362                                      cursor_path,
12363                                      tree_view->priv->focus_column,
12364                                      &background_area);
12365   pspp_sheet_view_get_cell_area (tree_view,
12366                                cursor_path,
12367                                tree_view->priv->focus_column,
12368                                &cell_area);
12369
12370   if (_pspp_sheet_view_column_cell_event (tree_view->priv->focus_column,
12371                                         &editable_widget,
12372                                         NULL,
12373                                         path_string,
12374                                         &background_area,
12375                                         &cell_area,
12376                                         flags))
12377     {
12378       retval = TRUE;
12379       if (editable_widget != NULL)
12380         {
12381           gint left, right;
12382           GdkRectangle area;
12383           GtkCellRenderer *cell;
12384
12385           area = cell_area;
12386           cell = _pspp_sheet_view_column_get_edited_cell (tree_view->priv->focus_column);
12387
12388           _pspp_sheet_view_column_get_neighbor_sizes (tree_view->priv->focus_column, cell, &left, &right);
12389
12390           area.x += left;
12391           area.width -= right + left;
12392
12393           pspp_sheet_view_real_start_editing (tree_view,
12394                                             tree_view->priv->focus_column,
12395                                             cursor_path,
12396                                             editable_widget,
12397                                             &area,
12398                                             NULL,
12399                                             flags);
12400         }
12401
12402     }
12403   g_free (path_string);
12404   return retval;
12405 }
12406
12407 static gboolean
12408 pspp_sheet_view_editable_button_press_event (GtkWidget *widget,
12409                                              GdkEventButton *event,
12410                                              PsppSheetView *sheet_view)
12411 {
12412   gint node;
12413
12414   node = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
12415                                              "pspp-sheet-view-node"));
12416   return pspp_sheet_view_row_head_clicked (sheet_view,
12417                                            node,
12418                                            sheet_view->priv->edited_column,
12419                                            event);
12420 }
12421
12422 static void
12423 pspp_sheet_view_editable_clicked (GtkButton *button,
12424                                   PsppSheetView *sheet_view)
12425 {
12426   pspp_sheet_view_editable_button_press_event (GTK_WIDGET (button), NULL,
12427                                                sheet_view);
12428 }
12429
12430 static gboolean
12431 is_all_selected (GtkWidget *widget)
12432 {
12433   GtkEntryBuffer *buffer;
12434   gint start_pos, end_pos;
12435
12436   if (!GTK_IS_ENTRY (widget))
12437     return FALSE;
12438
12439   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12440   return (gtk_editable_get_selection_bounds (GTK_EDITABLE (widget),
12441                                              &start_pos, &end_pos)
12442           && start_pos == 0
12443           && end_pos == gtk_entry_buffer_get_length (buffer));
12444 }
12445
12446 static gboolean
12447 is_at_left (GtkWidget *widget)
12448 {
12449   return (GTK_IS_ENTRY (widget)
12450           && gtk_editable_get_position (GTK_EDITABLE (widget)) == 0);
12451 }
12452
12453 static gboolean
12454 is_at_right (GtkWidget *widget)
12455 {
12456   GtkEntryBuffer *buffer;
12457   gint length;
12458
12459   if (!GTK_IS_ENTRY (widget))
12460     return FALSE;
12461
12462   buffer = gtk_entry_get_buffer (GTK_ENTRY (widget));
12463   length = gtk_entry_buffer_get_length (buffer);
12464   return gtk_editable_get_position (GTK_EDITABLE (widget)) == length;
12465 }
12466
12467 static gboolean
12468 pspp_sheet_view_event (GtkWidget *widget,
12469                        GdkEventKey *event,
12470                        PsppSheetView *tree_view)
12471 {
12472   PsppSheetViewColumn *column;
12473   GtkTreePath *path;
12474   gboolean handled;
12475   gboolean cancel;
12476   guint keyval;
12477   gint row;
12478
12479   /* Intercept only key press events.
12480      It would make sense to use "key-press-event" instead of "event", but
12481      GtkEntry attaches its own signal handler to "key-press-event" that runs
12482      before ours and overrides our desired behavior for GDK_Up and GDK_Down.
12483   */
12484   if (event->type != GDK_KEY_PRESS)
12485     return FALSE;
12486
12487   keyval = event->keyval;
12488   cancel = FALSE;
12489   switch (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK))
12490     {
12491     case 0:
12492       switch (event->keyval)
12493         {
12494         case GDK_Left:      case GDK_KP_Left:
12495         case GDK_Home:      case GDK_KP_Home:
12496           if (!is_all_selected (widget) && !is_at_left (widget))
12497             return FALSE;
12498           break;
12499
12500         case GDK_Right:     case GDK_KP_Right:
12501         case GDK_End:       case GDK_KP_End:
12502           if (!is_all_selected (widget) && !is_at_right (widget))
12503             return FALSE;
12504           break;
12505
12506         case GDK_Up:        case GDK_KP_Up:
12507         case GDK_Down:      case GDK_KP_Down:
12508           break;
12509
12510         case GDK_Page_Up:   case GDK_KP_Page_Up:
12511         case GDK_Page_Down: case GDK_KP_Page_Down:
12512           break;
12513
12514         case GDK_Escape:
12515           cancel = TRUE;
12516           break;
12517
12518         case GDK_Return:
12519           keyval = GDK_Down;
12520           break;
12521
12522         case GDK_Tab:       case GDK_KP_Tab:
12523         case GDK_ISO_Left_Tab:
12524           keyval = GDK_Tab;
12525           break;
12526
12527         default:
12528           return FALSE;
12529         }
12530       break;
12531
12532     case GDK_SHIFT_MASK:
12533       switch (event->keyval)
12534         {
12535         case GDK_Tab:
12536         case GDK_ISO_Left_Tab:
12537           keyval = GDK_Tab;
12538           break;
12539
12540         default:
12541           return FALSE;
12542         }
12543       break;
12544
12545     case GDK_CONTROL_MASK:
12546       switch (event->keyval)
12547         {
12548         case GDK_Left:      case GDK_KP_Left:
12549           if (!is_all_selected (widget) && !is_at_left (widget))
12550             return FALSE;
12551           break;
12552
12553         case GDK_Right:     case GDK_KP_Right:
12554           if (!is_all_selected (widget) && !is_at_right (widget))
12555             return FALSE;
12556           break;
12557
12558         case GDK_Up:        case GDK_KP_Up:
12559         case GDK_Down:      case GDK_KP_Down:
12560           break;
12561
12562         default:
12563           return FALSE;
12564         }
12565       break;
12566
12567     default:
12568       return FALSE;
12569     }
12570
12571   row = tree_view->priv->edited_row;
12572   column = tree_view->priv->edited_column;
12573   path = gtk_tree_path_new_from_indices (row, -1);
12574
12575   pspp_sheet_view_stop_editing (tree_view, cancel);
12576   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
12577
12578   pspp_sheet_view_set_cursor (tree_view, path, column, FALSE);
12579   gtk_tree_path_free (path);
12580
12581   handled = gtk_binding_set_activate (edit_bindings, keyval, event->state,
12582                                       G_OBJECT (tree_view));
12583   if (handled)
12584     g_signal_stop_emission_by_name (widget, "event");
12585
12586   pspp_sheet_view_get_cursor (tree_view, &path, NULL);
12587   pspp_sheet_view_start_editing (tree_view, path);
12588   gtk_tree_path_free (path);
12589
12590   return handled;
12591 }
12592
12593 static void
12594 pspp_sheet_view_override_cell_keypresses (GtkWidget *widget,
12595                                           gpointer data)
12596 {
12597   PsppSheetView *sheet_view = data;
12598
12599   g_signal_connect (widget, "event",
12600                     G_CALLBACK (pspp_sheet_view_event),
12601                     sheet_view);
12602
12603   if (GTK_IS_CONTAINER (widget))
12604     gtk_container_foreach (GTK_CONTAINER (widget),
12605                            pspp_sheet_view_override_cell_keypresses,
12606                            data);
12607 }
12608
12609 static void
12610 pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
12611                                   PsppSheetViewColumn *column,
12612                                   GtkTreePath       *path,
12613                                   GtkCellEditable   *cell_editable,
12614                                   GdkRectangle      *cell_area,
12615                                   GdkEvent          *event,
12616                                   guint              flags)
12617 {
12618   PsppSheetSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
12619   gint pre_val = gtk_adjustment_get_value (tree_view->priv->vadjustment);
12620   GtkRequisition requisition;
12621   gint row;
12622
12623   g_return_if_fail (gtk_tree_path_get_depth (path) == 1);
12624
12625   tree_view->priv->edited_column = column;
12626   _pspp_sheet_view_column_start_editing (column, GTK_CELL_EDITABLE (cell_editable));
12627
12628   row = gtk_tree_path_get_indices (path)[0];
12629   tree_view->priv->edited_row = row;
12630   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE, 0);
12631   cell_area->y += pre_val - (int)gtk_adjustment_get_value (tree_view->priv->vadjustment);
12632
12633   pspp_sheet_selection_unselect_all_columns (tree_view->priv->selection);
12634   pspp_sheet_selection_select_column (tree_view->priv->selection, column);
12635   tree_view->priv->anchor_column = column;
12636
12637   gtk_widget_size_request (GTK_WIDGET (cell_editable), &requisition);
12638
12639   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
12640
12641   if (requisition.height < cell_area->height)
12642     {
12643       gint diff = cell_area->height - requisition.height;
12644       pspp_sheet_view_put (tree_view,
12645                          GTK_WIDGET (cell_editable),
12646                          cell_area->x, cell_area->y + diff/2,
12647                          cell_area->width, requisition.height);
12648     }
12649   else
12650     {
12651       pspp_sheet_view_put (tree_view,
12652                          GTK_WIDGET (cell_editable),
12653                          cell_area->x, cell_area->y,
12654                          cell_area->width, cell_area->height);
12655     }
12656
12657   gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (cell_editable),
12658                                    (GdkEvent *)event);
12659
12660   gtk_widget_grab_focus (GTK_WIDGET (cell_editable));
12661   g_signal_connect (cell_editable, "remove-widget",
12662                     G_CALLBACK (pspp_sheet_view_remove_widget), tree_view);
12663   if (mode == PSPP_SHEET_SELECTION_RECTANGLE && column->row_head &&
12664       GTK_IS_BUTTON (cell_editable))
12665     {
12666       g_signal_connect (cell_editable, "button-press-event",
12667                         G_CALLBACK (pspp_sheet_view_editable_button_press_event),
12668                         tree_view);
12669       g_object_set_data (G_OBJECT (cell_editable), "pspp-sheet-view-node",
12670                          GINT_TO_POINTER (row));
12671       g_signal_connect (cell_editable, "clicked",
12672                         G_CALLBACK (pspp_sheet_view_editable_clicked),
12673                         tree_view);
12674     }
12675
12676   pspp_sheet_view_override_cell_keypresses (GTK_WIDGET (cell_editable),
12677                                             tree_view);
12678 }
12679
12680 void
12681 pspp_sheet_view_stop_editing (PsppSheetView *tree_view,
12682                               gboolean     cancel_editing)
12683 {
12684   PsppSheetViewColumn *column;
12685   GtkCellRenderer *cell;
12686
12687   if (tree_view->priv->edited_column == NULL)
12688     return;
12689
12690   /*
12691    * This is very evil. We need to do this, because
12692    * gtk_cell_editable_editing_done may trigger pspp_sheet_view_row_changed
12693    * later on. If pspp_sheet_view_row_changed notices
12694    * tree_view->priv->edited_column != NULL, it'll call
12695    * pspp_sheet_view_stop_editing again. Bad things will happen then.
12696    *
12697    * Please read that again if you intend to modify anything here.
12698    */
12699
12700   column = tree_view->priv->edited_column;
12701   tree_view->priv->edited_column = NULL;
12702
12703   cell = _pspp_sheet_view_column_get_edited_cell (column);
12704   gtk_cell_renderer_stop_editing (cell, cancel_editing);
12705
12706   if (!cancel_editing)
12707     gtk_cell_editable_editing_done (column->editable_widget);
12708
12709   tree_view->priv->edited_column = column;
12710
12711   gtk_cell_editable_remove_widget (column->editable_widget);
12712 }
12713
12714
12715 /**
12716  * pspp_sheet_view_set_hover_selection:
12717  * @tree_view: a #PsppSheetView
12718  * @hover: %TRUE to enable hover selection mode
12719  *
12720  * Enables of disables the hover selection mode of @tree_view.
12721  * Hover selection makes the selected row follow the pointer.
12722  * Currently, this works only for the selection modes 
12723  * %PSPP_SHEET_SELECTION_SINGLE and %PSPP_SHEET_SELECTION_BROWSE.
12724  * 
12725  * Since: 2.6
12726  **/
12727 void     
12728 pspp_sheet_view_set_hover_selection (PsppSheetView *tree_view,
12729                                    gboolean     hover)
12730 {
12731   hover = hover != FALSE;
12732
12733   if (hover != tree_view->priv->hover_selection)
12734     {
12735       tree_view->priv->hover_selection = hover;
12736
12737       g_object_notify (G_OBJECT (tree_view), "hover-selection");
12738     }
12739 }
12740
12741 /**
12742  * pspp_sheet_view_get_hover_selection:
12743  * @tree_view: a #PsppSheetView
12744  * 
12745  * Returns whether hover selection mode is turned on for @tree_view.
12746  * 
12747  * Return value: %TRUE if @tree_view is in hover selection mode
12748  *
12749  * Since: 2.6 
12750  **/
12751 gboolean 
12752 pspp_sheet_view_get_hover_selection (PsppSheetView *tree_view)
12753 {
12754   return tree_view->priv->hover_selection;
12755 }
12756
12757 /**
12758  * pspp_sheet_view_set_rubber_banding:
12759  * @tree_view: a #PsppSheetView
12760  * @enable: %TRUE to enable rubber banding
12761  *
12762  * Enables or disables rubber banding in @tree_view.  If the selection mode is
12763  * #PSPP_SHEET_SELECTION_MULTIPLE or #PSPP_SHEET_SELECTION_RECTANGLE, rubber
12764  * banding will allow the user to select multiple rows by dragging the mouse.
12765  * 
12766  * Since: 2.10
12767  **/
12768 void
12769 pspp_sheet_view_set_rubber_banding (PsppSheetView *tree_view,
12770                                   gboolean     enable)
12771 {
12772   enable = enable != FALSE;
12773
12774   if (enable != tree_view->priv->rubber_banding_enable)
12775     {
12776       tree_view->priv->rubber_banding_enable = enable;
12777
12778       g_object_notify (G_OBJECT (tree_view), "rubber-banding");
12779     }
12780 }
12781
12782 /**
12783  * pspp_sheet_view_get_rubber_banding:
12784  * @tree_view: a #PsppSheetView
12785  * 
12786  * Returns whether rubber banding is turned on for @tree_view.  If the
12787  * selection mode is #PSPP_SHEET_SELECTION_MULTIPLE or
12788  * #PSPP_SHEET_SELECTION_RECTANGLE, rubber banding will allow the user to
12789  * select multiple rows by dragging the mouse.
12790  * 
12791  * Return value: %TRUE if rubber banding in @tree_view is enabled.
12792  *
12793  * Since: 2.10
12794  **/
12795 gboolean
12796 pspp_sheet_view_get_rubber_banding (PsppSheetView *tree_view)
12797 {
12798   return tree_view->priv->rubber_banding_enable;
12799 }
12800
12801 /**
12802  * pspp_sheet_view_is_rubber_banding_active:
12803  * @tree_view: a #PsppSheetView
12804  * 
12805  * Returns whether a rubber banding operation is currently being done
12806  * in @tree_view.
12807  *
12808  * Return value: %TRUE if a rubber banding operation is currently being
12809  * done in @tree_view.
12810  *
12811  * Since: 2.12
12812  **/
12813 gboolean
12814 pspp_sheet_view_is_rubber_banding_active (PsppSheetView *tree_view)
12815 {
12816   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12817
12818   if (tree_view->priv->rubber_banding_enable
12819       && tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
12820     return TRUE;
12821
12822   return FALSE;
12823 }
12824
12825 static void
12826 pspp_sheet_view_grab_notify (GtkWidget *widget,
12827                            gboolean   was_grabbed)
12828 {
12829   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12830
12831   tree_view->priv->in_grab = !was_grabbed;
12832
12833   if (!was_grabbed)
12834     {
12835       tree_view->priv->pressed_button = -1;
12836
12837       if (tree_view->priv->rubber_band_status)
12838         pspp_sheet_view_stop_rubber_band (tree_view);
12839     }
12840 }
12841
12842 static void
12843 pspp_sheet_view_state_changed (GtkWidget      *widget,
12844                              GtkStateType    previous_state)
12845 {
12846   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
12847
12848   if (gtk_widget_get_realized (widget))
12849     {
12850       GtkStyle *style = gtk_widget_get_style (widget);
12851       gdk_window_set_background (tree_view->priv->bin_window, &style->base[gtk_widget_get_state (widget)]);
12852     }
12853
12854   gtk_widget_queue_draw (widget);
12855 }
12856
12857 /**
12858  * pspp_sheet_view_get_grid_lines:
12859  * @tree_view: a #PsppSheetView
12860  *
12861  * Returns which grid lines are enabled in @tree_view.
12862  *
12863  * Return value: a #PsppSheetViewGridLines value indicating which grid lines
12864  * are enabled.
12865  *
12866  * Since: 2.10
12867  */
12868 PsppSheetViewGridLines
12869 pspp_sheet_view_get_grid_lines (PsppSheetView *tree_view)
12870 {
12871   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12872
12873   return tree_view->priv->grid_lines;
12874 }
12875
12876 /**
12877  * pspp_sheet_view_set_grid_lines:
12878  * @tree_view: a #PsppSheetView
12879  * @grid_lines: a #PsppSheetViewGridLines value indicating which grid lines to
12880  * enable.
12881  *
12882  * Sets which grid lines to draw in @tree_view.
12883  *
12884  * Since: 2.10
12885  */
12886 void
12887 pspp_sheet_view_set_grid_lines (PsppSheetView           *tree_view,
12888                               PsppSheetViewGridLines   grid_lines)
12889 {
12890   PsppSheetViewPrivate *priv;
12891   PsppSheetViewGridLines old_grid_lines;
12892
12893   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12894
12895   priv = tree_view->priv;
12896
12897   old_grid_lines = priv->grid_lines;
12898   priv->grid_lines = grid_lines;
12899   
12900   if (old_grid_lines != grid_lines)
12901     {
12902       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12903       
12904       g_object_notify (G_OBJECT (tree_view), "enable-grid-lines");
12905     }
12906 }
12907
12908 /**
12909  * pspp_sheet_view_get_special_cells:
12910  * @tree_view: a #PsppSheetView
12911  *
12912  * Returns which grid lines are enabled in @tree_view.
12913  *
12914  * Return value: a #PsppSheetViewSpecialCells value indicating whether rows in
12915  * the sheet view contain special cells.
12916  */
12917 PsppSheetViewSpecialCells
12918 pspp_sheet_view_get_special_cells (PsppSheetView *tree_view)
12919 {
12920   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
12921
12922   return tree_view->priv->special_cells;
12923 }
12924
12925 /**
12926  * pspp_sheet_view_set_special_cells:
12927  * @tree_view: a #PsppSheetView
12928  * @special_cells: a #PsppSheetViewSpecialCells value indicating whether rows in
12929  * the sheet view contain special cells.
12930  *
12931  * Sets whether rows in the sheet view contain special cells, controlling the
12932  * rendering of row selections.
12933  */
12934 void
12935 pspp_sheet_view_set_special_cells (PsppSheetView           *tree_view,
12936                               PsppSheetViewSpecialCells   special_cells)
12937 {
12938   PsppSheetViewPrivate *priv;
12939
12940   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12941
12942   priv = tree_view->priv;
12943
12944   if (priv->special_cells != special_cells)
12945     {
12946       priv->special_cells = special_cells;
12947       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
12948       g_object_notify (G_OBJECT (tree_view), "special-cells");
12949     }
12950 }
12951
12952 int
12953 pspp_sheet_view_get_fixed_height (const PsppSheetView *tree_view)
12954 {
12955   /* XXX (re)calculate fixed_height if necessary */
12956   return tree_view->priv->fixed_height;
12957 }
12958
12959 void
12960 pspp_sheet_view_set_fixed_height (PsppSheetView *tree_view,
12961                                   int fixed_height)
12962 {
12963   g_return_if_fail (fixed_height > 0);
12964
12965   if (tree_view->priv->fixed_height != fixed_height)
12966     {
12967       tree_view->priv->fixed_height = fixed_height;
12968       g_object_notify (G_OBJECT (tree_view), "fixed-height");
12969     }
12970   if (!tree_view->priv->fixed_height_set)
12971     {
12972       tree_view->priv->fixed_height_set = TRUE;
12973       g_object_notify (G_OBJECT (tree_view), "fixed-height-set");
12974     }
12975 }
12976
12977 /**
12978  * pspp_sheet_view_set_tooltip_row:
12979  * @tree_view: a #PsppSheetView
12980  * @tooltip: a #GtkTooltip
12981  * @path: a #GtkTreePath
12982  *
12983  * Sets the tip area of @tooltip to be the area covered by the row at @path.
12984  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
12985  * See also gtk_tooltip_set_tip_area().
12986  *
12987  * Since: 2.12
12988  */
12989 void
12990 pspp_sheet_view_set_tooltip_row (PsppSheetView *tree_view,
12991                                GtkTooltip  *tooltip,
12992                                GtkTreePath *path)
12993 {
12994   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12995   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
12996
12997   pspp_sheet_view_set_tooltip_cell (tree_view, tooltip, path, NULL, NULL);
12998 }
12999
13000 /**
13001  * pspp_sheet_view_set_tooltip_cell:
13002  * @tree_view: a #PsppSheetView
13003  * @tooltip: a #GtkTooltip
13004  * @path: (allow-none): a #GtkTreePath or %NULL
13005  * @column: (allow-none): a #PsppSheetViewColumn or %NULL
13006  * @cell: (allow-none): a #GtkCellRenderer or %NULL
13007  *
13008  * Sets the tip area of @tooltip to the area @path, @column and @cell have
13009  * in common.  For example if @path is %NULL and @column is set, the tip
13010  * area will be set to the full area covered by @column.  See also
13011  * gtk_tooltip_set_tip_area().
13012  *
13013  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
13014  *
13015  * Since: 2.12
13016  */
13017 void
13018 pspp_sheet_view_set_tooltip_cell (PsppSheetView       *tree_view,
13019                                 GtkTooltip        *tooltip,
13020                                 GtkTreePath       *path,
13021                                 PsppSheetViewColumn *column,
13022                                 GtkCellRenderer   *cell)
13023 {
13024   GdkRectangle rect;
13025
13026   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13027   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
13028   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
13029   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
13030
13031   /* Determine x values. */
13032   if (column && cell)
13033     {
13034       GdkRectangle tmp;
13035       gint start, width;
13036
13037       pspp_sheet_view_get_cell_area (tree_view, path, column, &tmp);
13038       pspp_sheet_view_column_cell_get_position (column, cell, &start, &width);
13039
13040       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13041                                                          tmp.x + start, 0,
13042                                                          &rect.x, NULL);
13043       rect.width = width;
13044     }
13045   else if (column)
13046     {
13047       GdkRectangle tmp;
13048
13049       pspp_sheet_view_get_background_area (tree_view, NULL, column, &tmp);
13050       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13051                                                          tmp.x, 0,
13052                                                          &rect.x, NULL);
13053       rect.width = tmp.width;
13054     }
13055   else
13056     {
13057       GtkAllocation allocation;
13058       gtk_widget_get_allocation (GTK_WIDGET (tree_view), &allocation);
13059       rect.x = 0;
13060       rect.width = allocation.width;
13061     }
13062
13063   /* Determine y values. */
13064   if (path)
13065     {
13066       GdkRectangle tmp;
13067
13068       pspp_sheet_view_get_background_area (tree_view, path, NULL, &tmp);
13069       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13070                                                          0, tmp.y,
13071                                                          NULL, &rect.y);
13072       rect.height = tmp.height;
13073     }
13074   else
13075     {
13076       rect.y = 0;
13077       rect.height = gtk_adjustment_get_page_size (tree_view->priv->vadjustment);
13078     }
13079
13080   gtk_tooltip_set_tip_area (tooltip, &rect);
13081 }
13082
13083 /**
13084  * pspp_sheet_view_get_tooltip_context:
13085  * @tree_view: a #PsppSheetView
13086  * @x: the x coordinate (relative to widget coordinates)
13087  * @y: the y coordinate (relative to widget coordinates)
13088  * @keyboard_tip: whether this is a keyboard tooltip or not
13089  * @model: (allow-none): a pointer to receive a #GtkTreeModel or %NULL
13090  * @path: (allow-none): a pointer to receive a #GtkTreePath or %NULL
13091  * @iter: (allow-none): a pointer to receive a #GtkTreeIter or %NULL
13092  *
13093  * This function is supposed to be used in a #GtkWidget::query-tooltip
13094  * signal handler for #PsppSheetView.  The @x, @y and @keyboard_tip values
13095  * which are received in the signal handler, should be passed to this
13096  * function without modification.
13097  *
13098  * The return value indicates whether there is a tree view row at the given
13099  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips.  For keyboard
13100  * tooltips the row returned will be the cursor row.  When %TRUE, then any of
13101  * @model, @path and @iter which have been provided will be set to point to
13102  * that row and the corresponding model.  @x and @y will always be converted
13103  * to be relative to @tree_view's bin_window if @keyboard_tooltip is %FALSE.
13104  *
13105  * Return value: whether or not the given tooltip context points to a row.
13106  *
13107  * Since: 2.12
13108  */
13109 gboolean
13110 pspp_sheet_view_get_tooltip_context (PsppSheetView   *tree_view,
13111                                    gint          *x,
13112                                    gint          *y,
13113                                    gboolean       keyboard_tip,
13114                                    GtkTreeModel **model,
13115                                    GtkTreePath  **path,
13116                                    GtkTreeIter   *iter)
13117 {
13118   GtkTreePath *tmppath = NULL;
13119
13120   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
13121   g_return_val_if_fail (x != NULL, FALSE);
13122   g_return_val_if_fail (y != NULL, FALSE);
13123
13124   if (keyboard_tip)
13125     {
13126       pspp_sheet_view_get_cursor (tree_view, &tmppath, NULL);
13127
13128       if (!tmppath)
13129         return FALSE;
13130     }
13131   else
13132     {
13133       pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, *x, *y,
13134                                                          x, y);
13135
13136       if (!pspp_sheet_view_get_path_at_pos (tree_view, *x, *y,
13137                                           &tmppath, NULL, NULL, NULL))
13138         return FALSE;
13139     }
13140
13141   if (model)
13142     *model = pspp_sheet_view_get_model (tree_view);
13143
13144   if (iter)
13145     gtk_tree_model_get_iter (pspp_sheet_view_get_model (tree_view),
13146                              iter, tmppath);
13147
13148   if (path)
13149     *path = tmppath;
13150   else
13151     gtk_tree_path_free (tmppath);
13152
13153   return TRUE;
13154 }
13155
13156 static gboolean
13157 pspp_sheet_view_set_tooltip_query_cb (GtkWidget  *widget,
13158                                     gint        x,
13159                                     gint        y,
13160                                     gboolean    keyboard_tip,
13161                                     GtkTooltip *tooltip,
13162                                     gpointer    data)
13163 {
13164   GValue value = { 0, };
13165   GValue transformed = { 0, };
13166   GtkTreeIter iter;
13167   GtkTreePath *path;
13168   GtkTreeModel *model;
13169   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
13170
13171   if (!pspp_sheet_view_get_tooltip_context (PSPP_SHEET_VIEW (widget),
13172                                           &x, &y,
13173                                           keyboard_tip,
13174                                           &model, &path, &iter))
13175     return FALSE;
13176
13177   gtk_tree_model_get_value (model, &iter,
13178                             tree_view->priv->tooltip_column, &value);
13179
13180   g_value_init (&transformed, G_TYPE_STRING);
13181
13182   if (!g_value_transform (&value, &transformed))
13183     {
13184       g_value_unset (&value);
13185       gtk_tree_path_free (path);
13186
13187       return FALSE;
13188     }
13189
13190   g_value_unset (&value);
13191
13192   if (!g_value_get_string (&transformed))
13193     {
13194       g_value_unset (&transformed);
13195       gtk_tree_path_free (path);
13196
13197       return FALSE;
13198     }
13199
13200   gtk_tooltip_set_markup (tooltip, g_value_get_string (&transformed));
13201   pspp_sheet_view_set_tooltip_row (tree_view, tooltip, path);
13202
13203   gtk_tree_path_free (path);
13204   g_value_unset (&transformed);
13205
13206   return TRUE;
13207 }
13208
13209 /**
13210  * pspp_sheet_view_set_tooltip_column:
13211  * @tree_view: a #PsppSheetView
13212  * @column: an integer, which is a valid column number for @tree_view's model
13213  *
13214  * If you only plan to have simple (text-only) tooltips on full rows, you
13215  * can use this function to have #PsppSheetView handle these automatically
13216  * for you. @column should be set to the column in @tree_view's model
13217  * containing the tooltip texts, or -1 to disable this feature.
13218  *
13219  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
13220  * @tree_view will connect a #GtkWidget::query-tooltip signal handler.
13221  *
13222  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
13223  * so &amp;, &lt;, etc have to be escaped in the text.
13224  *
13225  * Since: 2.12
13226  */
13227 void
13228 pspp_sheet_view_set_tooltip_column (PsppSheetView *tree_view,
13229                                   gint         column)
13230 {
13231   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13232
13233   if (column == tree_view->priv->tooltip_column)
13234     return;
13235
13236   if (column == -1)
13237     {
13238       g_signal_handlers_disconnect_by_func (tree_view,
13239                                             pspp_sheet_view_set_tooltip_query_cb,
13240                                             NULL);
13241       gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), FALSE);
13242     }
13243   else
13244     {
13245       if (tree_view->priv->tooltip_column == -1)
13246         {
13247           g_signal_connect (tree_view, "query-tooltip",
13248                             G_CALLBACK (pspp_sheet_view_set_tooltip_query_cb), NULL);
13249           gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), TRUE);
13250         }
13251     }
13252
13253   tree_view->priv->tooltip_column = column;
13254   g_object_notify (G_OBJECT (tree_view), "tooltip-column");
13255 }
13256
13257 /**
13258  * pspp_sheet_view_get_tooltip_column:
13259  * @tree_view: a #PsppSheetView
13260  *
13261  * Returns the column of @tree_view's model which is being used for
13262  * displaying tooltips on @tree_view's rows.
13263  *
13264  * Return value: the index of the tooltip column that is currently being
13265  * used, or -1 if this is disabled.
13266  *
13267  * Since: 2.12
13268  */
13269 gint
13270 pspp_sheet_view_get_tooltip_column (PsppSheetView *tree_view)
13271 {
13272   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
13273
13274   return tree_view->priv->tooltip_column;
13275 }
13276
13277 gboolean
13278 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
13279                                   GValue                *return_accu,
13280                                   const GValue          *handler_return,
13281                                   gpointer               dummy)
13282 {
13283   gboolean continue_emission;
13284   gboolean signal_handled;
13285   
13286   signal_handled = g_value_get_boolean (handler_return);
13287   g_value_set_boolean (return_accu, signal_handled);
13288   continue_emission = !signal_handled;
13289   
13290   return continue_emission;
13291 }
13292
13293
13294 GType
13295 pspp_sheet_view_grid_lines_get_type (void)
13296 {
13297     static GType etype = 0;
13298     if (G_UNLIKELY(etype == 0)) {
13299         static const GEnumValue values[] = {
13300             { PSPP_SHEET_VIEW_GRID_LINES_NONE, "PSPP_SHEET_VIEW_GRID_LINES_NONE", "none" },
13301             { PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL, "PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL", "horizontal" },
13302             { PSPP_SHEET_VIEW_GRID_LINES_VERTICAL, "PSPP_SHEET_VIEW_GRID_LINES_VERTICAL", "vertical" },
13303             { PSPP_SHEET_VIEW_GRID_LINES_BOTH, "PSPP_SHEET_VIEW_GRID_LINES_BOTH", "both" },
13304             { 0, NULL, NULL }
13305         };
13306         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewGridLines"), values);
13307     }
13308     return etype;
13309 }
13310
13311 GType
13312 pspp_sheet_view_special_cells_get_type (void)
13313 {
13314     static GType etype = 0;
13315     if (G_UNLIKELY(etype == 0)) {
13316         static const GEnumValue values[] = {
13317             { PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT, "PSPP_SHEET_VIEW_SPECIAL_CELLS_DETECT", "detect" },
13318             { PSPP_SHEET_VIEW_SPECIAL_CELLS_YES, "PSPP_SHEET_VIEW_SPECIAL_CELLS_YES", "yes" },
13319             { PSPP_SHEET_VIEW_SPECIAL_CELLS_NO, "PSPP_SHEET_VIEW_SPECIAL_CELLS_NO", "no" },
13320             { 0, NULL, NULL }
13321         };
13322         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewSpecialCells"), values);
13323     }
13324     return etype;
13325 }