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