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