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