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