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