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