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