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