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