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