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