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