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