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