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