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