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