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