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