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