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