9b84dac31e9cd1ed21f17046be0824054928917e
[pspp] / src / ui / gui / pspp-sheet-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 /* gtktreeview.c
18  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Library General Public
22  * License as published by the Free Software Foundation; either
23  * version 2 of the License, or (at your option) any later version.
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License along with this library; if not, write to the
32  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33  * Boston, MA 02111-1307, USA.
34  */
35
36 #include <config.h>
37
38 #include "ui/gui/pspp-sheet-private.h"
39
40 #include <gtk/gtk.h>
41 #include <gdk/gdk.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <string.h>
44
45 #include "ui/gui/psppire-marshal.h"
46 #include "ui/gui/pspp-sheet-selection.h"
47
48 #define P_(STRING) STRING
49 #define GTK_PARAM_READABLE G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
50 #define GTK_PARAM_READWRITE G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
51
52 /* Many keyboard shortcuts for Mac are the same as for X
53  * except they use Command key instead of Control (e.g. Cut,
54  * Copy, Paste). This symbol is for those simple cases. */
55 #ifndef GDK_WINDOWING_QUARTZ
56 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_CONTROL_MASK
57 #else
58 #define GTK_DEFAULT_ACCEL_MOD_MASK GDK_META_MASK
59 #endif
60
61 #define PSPP_SHEET_VIEW_PRIORITY_VALIDATE (GDK_PRIORITY_REDRAW + 5)
62 #define PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC (PSPP_SHEET_VIEW_PRIORITY_VALIDATE + 2)
63 #define PSPP_SHEET_VIEW_TIME_MS_PER_IDLE 30
64 #define SCROLL_EDGE_SIZE 15
65 #define EXPANDER_EXTRA_PADDING 4
66 #define PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT 5000
67 #define AUTO_EXPAND_TIMEOUT 500
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(node) (PSPP_RBNODE_GET_HEIGHT (node))
76 #define CELL_HEIGHT(node, separator) ((BACKGROUND_HEIGHT (node)) - (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,tree,node) (RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, _pspp_rbtree_node_find_offset ((tree), (node))))
86 #define CELL_FIRST_PIXEL(tree_view,tree,node,separator) (BACKGROUND_FIRST_PIXEL (tree_view,tree,node) + separator/2)
87
88 #define ROW_HEIGHT(tree_view,height) \
89   ((height > 0) ? (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   TEST_EXPAND_ROW,
122   TEST_COLLAPSE_ROW,
123   ROW_EXPANDED,
124   ROW_COLLAPSED,
125   COLUMNS_CHANGED,
126   CURSOR_CHANGED,
127   MOVE_CURSOR,
128   SELECT_ALL,
129   UNSELECT_ALL,
130   SELECT_CURSOR_ROW,
131   TOGGLE_CURSOR_ROW,
132   EXPAND_COLLAPSE_CURSOR_ROW,
133   SELECT_CURSOR_PARENT,
134   START_INTERACTIVE_SEARCH,
135   LAST_SIGNAL
136 };
137
138 /* Properties */
139 enum {
140   PROP_0,
141   PROP_MODEL,
142   PROP_HADJUSTMENT,
143   PROP_VADJUSTMENT,
144   PROP_HEADERS_VISIBLE,
145   PROP_HEADERS_CLICKABLE,
146   PROP_EXPANDER_COLUMN,
147   PROP_REORDERABLE,
148   PROP_RULES_HINT,
149   PROP_ENABLE_SEARCH,
150   PROP_SEARCH_COLUMN,
151   PROP_FIXED_HEIGHT_MODE,
152   PROP_HOVER_SELECTION,
153   PROP_HOVER_EXPAND,
154   PROP_SHOW_EXPANDERS,
155   PROP_LEVEL_INDENTATION,
156   PROP_RUBBER_BANDING,
157   PROP_ENABLE_GRID_LINES,
158   PROP_ENABLE_TREE_LINES,
159   PROP_TOOLTIP_COLUMN
160 };
161
162 /* object signals */
163 static void     pspp_sheet_view_finalize             (GObject          *object);
164 static void     pspp_sheet_view_set_property         (GObject         *object,
165                                                     guint            prop_id,
166                                                     const GValue    *value,
167                                                     GParamSpec      *pspec);
168 static void     pspp_sheet_view_get_property         (GObject         *object,
169                                                     guint            prop_id,
170                                                     GValue          *value,
171                                                     GParamSpec      *pspec);
172
173 /* gtkobject signals */
174 static void     pspp_sheet_view_destroy              (GtkObject        *object);
175
176 /* gtkwidget signals */
177 static void     pspp_sheet_view_realize              (GtkWidget        *widget);
178 static void     pspp_sheet_view_unrealize            (GtkWidget        *widget);
179 static void     pspp_sheet_view_map                  (GtkWidget        *widget);
180 static void     pspp_sheet_view_size_request         (GtkWidget        *widget,
181                                                     GtkRequisition   *requisition);
182 static void     pspp_sheet_view_size_allocate        (GtkWidget        *widget,
183                                                     GtkAllocation    *allocation);
184 static gboolean pspp_sheet_view_expose               (GtkWidget        *widget,
185                                                     GdkEventExpose   *event);
186 static gboolean pspp_sheet_view_key_press            (GtkWidget        *widget,
187                                                     GdkEventKey      *event);
188 static gboolean pspp_sheet_view_key_release          (GtkWidget        *widget,
189                                                     GdkEventKey      *event);
190 static gboolean pspp_sheet_view_motion               (GtkWidget        *widget,
191                                                     GdkEventMotion   *event);
192 static gboolean pspp_sheet_view_enter_notify         (GtkWidget        *widget,
193                                                     GdkEventCrossing *event);
194 static gboolean pspp_sheet_view_leave_notify         (GtkWidget        *widget,
195                                                     GdkEventCrossing *event);
196 static gboolean pspp_sheet_view_button_press         (GtkWidget        *widget,
197                                                     GdkEventButton   *event);
198 static gboolean pspp_sheet_view_button_release       (GtkWidget        *widget,
199                                                     GdkEventButton   *event);
200 static gboolean pspp_sheet_view_grab_broken          (GtkWidget          *widget,
201                                                     GdkEventGrabBroken *event);
202 #if 0
203 static gboolean pspp_sheet_view_configure            (GtkWidget         *widget,
204                                                     GdkEventConfigure *event);
205 #endif
206
207 static void     pspp_sheet_view_set_focus_child      (GtkContainer     *container,
208                                                     GtkWidget        *child);
209 static gint     pspp_sheet_view_focus_out            (GtkWidget        *widget,
210                                                     GdkEventFocus    *event);
211 static gint     pspp_sheet_view_focus                (GtkWidget        *widget,
212                                                     GtkDirectionType  direction);
213 static void     pspp_sheet_view_grab_focus           (GtkWidget        *widget);
214 static void     pspp_sheet_view_style_set            (GtkWidget        *widget,
215                                                     GtkStyle         *previous_style);
216 static void     pspp_sheet_view_grab_notify          (GtkWidget        *widget,
217                                                     gboolean          was_grabbed);
218 static void     pspp_sheet_view_state_changed        (GtkWidget        *widget,
219                                                     GtkStateType      previous_state);
220
221 /* container signals */
222 static void     pspp_sheet_view_remove               (GtkContainer     *container,
223                                                     GtkWidget        *widget);
224 static void     pspp_sheet_view_forall               (GtkContainer     *container,
225                                                     gboolean          include_internals,
226                                                     GtkCallback       callback,
227                                                     gpointer          callback_data);
228
229 /* Source side drag signals */
230 static void pspp_sheet_view_drag_begin       (GtkWidget        *widget,
231                                             GdkDragContext   *context);
232 static void pspp_sheet_view_drag_end         (GtkWidget        *widget,
233                                             GdkDragContext   *context);
234 static void pspp_sheet_view_drag_data_get    (GtkWidget        *widget,
235                                             GdkDragContext   *context,
236                                             GtkSelectionData *selection_data,
237                                             guint             info,
238                                             guint             time);
239 static void pspp_sheet_view_drag_data_delete (GtkWidget        *widget,
240                                             GdkDragContext   *context);
241
242 /* Target side drag signals */
243 static void     pspp_sheet_view_drag_leave         (GtkWidget        *widget,
244                                                   GdkDragContext   *context,
245                                                   guint             time);
246 static gboolean pspp_sheet_view_drag_motion        (GtkWidget        *widget,
247                                                   GdkDragContext   *context,
248                                                   gint              x,
249                                                   gint              y,
250                                                   guint             time);
251 static gboolean pspp_sheet_view_drag_drop          (GtkWidget        *widget,
252                                                   GdkDragContext   *context,
253                                                   gint              x,
254                                                   gint              y,
255                                                   guint             time);
256 static void     pspp_sheet_view_drag_data_received (GtkWidget        *widget,
257                                                   GdkDragContext   *context,
258                                                   gint              x,
259                                                   gint              y,
260                                                   GtkSelectionData *selection_data,
261                                                   guint             info,
262                                                   guint             time);
263
264 /* tree_model signals */
265 static void pspp_sheet_view_set_adjustments                 (PsppSheetView     *tree_view,
266                                                            GtkAdjustment   *hadj,
267                                                            GtkAdjustment   *vadj);
268 static gboolean pspp_sheet_view_real_move_cursor            (PsppSheetView     *tree_view,
269                                                            GtkMovementStep  step,
270                                                            gint             count);
271 static gboolean pspp_sheet_view_real_select_all             (PsppSheetView     *tree_view);
272 static gboolean pspp_sheet_view_real_unselect_all           (PsppSheetView     *tree_view);
273 static gboolean pspp_sheet_view_real_select_cursor_row      (PsppSheetView     *tree_view,
274                                                            gboolean         start_editing);
275 static gboolean pspp_sheet_view_real_toggle_cursor_row      (PsppSheetView     *tree_view);
276 static gboolean pspp_sheet_view_real_expand_collapse_cursor_row (PsppSheetView     *tree_view,
277                                                                gboolean         logical,
278                                                                gboolean         expand,
279                                                                gboolean         open_all);
280 static gboolean pspp_sheet_view_real_select_cursor_parent   (PsppSheetView     *tree_view);
281 static void pspp_sheet_view_row_changed                     (GtkTreeModel    *model,
282                                                            GtkTreePath     *path,
283                                                            GtkTreeIter     *iter,
284                                                            gpointer         data);
285 static void pspp_sheet_view_row_inserted                    (GtkTreeModel    *model,
286                                                            GtkTreePath     *path,
287                                                            GtkTreeIter     *iter,
288                                                            gpointer         data);
289 static void pspp_sheet_view_row_has_child_toggled           (GtkTreeModel    *model,
290                                                            GtkTreePath     *path,
291                                                            GtkTreeIter     *iter,
292                                                            gpointer         data);
293 static void pspp_sheet_view_row_deleted                     (GtkTreeModel    *model,
294                                                            GtkTreePath     *path,
295                                                            gpointer         data);
296 static void pspp_sheet_view_rows_reordered                  (GtkTreeModel    *model,
297                                                            GtkTreePath     *parent,
298                                                            GtkTreeIter     *iter,
299                                                            gint            *new_order,
300                                                            gpointer         data);
301
302 /* Incremental reflow */
303 static gboolean validate_row             (PsppSheetView *tree_view,
304                                           GtkRBTree   *tree,
305                                           GtkRBNode   *node,
306                                           GtkTreeIter *iter,
307                                           GtkTreePath *path);
308 static void     validate_visible_area    (PsppSheetView *tree_view);
309 static gboolean validate_rows_handler    (PsppSheetView *tree_view);
310 static gboolean do_validate_rows         (PsppSheetView *tree_view,
311                                           gboolean     size_request);
312 static gboolean validate_rows            (PsppSheetView *tree_view);
313 static gboolean presize_handler_callback (gpointer     data);
314 static void     install_presize_handler  (PsppSheetView *tree_view);
315 static void     install_scroll_sync_handler (PsppSheetView *tree_view);
316 static void     pspp_sheet_view_set_top_row   (PsppSheetView *tree_view,
317                                              GtkTreePath *path,
318                                              gint         offset);
319 static void     pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view);
320 static void     pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view);
321 static void     invalidate_empty_focus      (PsppSheetView *tree_view);
322
323 /* Internal functions */
324 static gboolean pspp_sheet_view_is_expander_column             (PsppSheetView        *tree_view,
325                                                               PsppSheetViewColumn  *column);
326 static void     pspp_sheet_view_add_move_binding               (GtkBindingSet      *binding_set,
327                                                               guint               keyval,
328                                                               guint               modmask,
329                                                               gboolean            add_shifted_binding,
330                                                               GtkMovementStep     step,
331                                                               gint                count);
332 static gint     pspp_sheet_view_unref_and_check_selection_tree (PsppSheetView        *tree_view,
333                                                               GtkRBTree          *tree);
334 static void     pspp_sheet_view_queue_draw_path                (PsppSheetView        *tree_view,
335                                                               GtkTreePath        *path,
336                                                               const GdkRectangle *clip_rect);
337 static void     pspp_sheet_view_queue_draw_arrow               (PsppSheetView        *tree_view,
338                                                               GtkRBTree          *tree,
339                                                               GtkRBNode          *node,
340                                                               const GdkRectangle *clip_rect);
341 static void     pspp_sheet_view_draw_arrow                     (PsppSheetView        *tree_view,
342                                                               GtkRBTree          *tree,
343                                                               GtkRBNode          *node,
344                                                               gint                x,
345                                                               gint                y);
346 static void     pspp_sheet_view_get_arrow_xrange               (PsppSheetView        *tree_view,
347                                                               GtkRBTree          *tree,
348                                                               gint               *x1,
349                                                               gint               *x2);
350 static gint     pspp_sheet_view_new_column_width               (PsppSheetView        *tree_view,
351                                                               gint                i,
352                                                               gint               *x);
353 static void     pspp_sheet_view_adjustment_changed             (GtkAdjustment      *adjustment,
354                                                               PsppSheetView        *tree_view);
355 static void     pspp_sheet_view_build_tree                     (PsppSheetView        *tree_view,
356                                                               GtkRBTree          *tree,
357                                                               GtkTreeIter        *iter,
358                                                               gint                depth,
359                                                               gboolean            recurse);
360 static void     pspp_sheet_view_clamp_node_visible             (PsppSheetView        *tree_view,
361                                                               GtkRBTree          *tree,
362                                                               GtkRBNode          *node);
363 static void     pspp_sheet_view_clamp_column_visible           (PsppSheetView        *tree_view,
364                                                               PsppSheetViewColumn  *column,
365                                                               gboolean            focus_to_cell);
366 static gboolean pspp_sheet_view_maybe_begin_dragging_row       (PsppSheetView        *tree_view,
367                                                               GdkEventMotion     *event);
368 static void     pspp_sheet_view_focus_to_cursor                (PsppSheetView        *tree_view);
369 static void     pspp_sheet_view_move_cursor_up_down            (PsppSheetView        *tree_view,
370                                                               gint                count);
371 static void     pspp_sheet_view_move_cursor_page_up_down       (PsppSheetView        *tree_view,
372                                                               gint                count);
373 static void     pspp_sheet_view_move_cursor_left_right         (PsppSheetView        *tree_view,
374                                                               gint                count);
375 static void     pspp_sheet_view_move_cursor_start_end          (PsppSheetView        *tree_view,
376                                                               gint                count);
377 static gboolean pspp_sheet_view_real_collapse_row              (PsppSheetView        *tree_view,
378                                                               GtkTreePath        *path,
379                                                               GtkRBTree          *tree,
380                                                               GtkRBNode          *node,
381                                                               gboolean            animate);
382 static gboolean pspp_sheet_view_real_expand_row                (PsppSheetView        *tree_view,
383                                                               GtkTreePath        *path,
384                                                               GtkRBTree          *tree,
385                                                               GtkRBNode          *node,
386                                                               gboolean            open_all,
387                                                               gboolean            animate);
388 static void     pspp_sheet_view_real_set_cursor                (PsppSheetView        *tree_view,
389                                                               GtkTreePath        *path,
390                                                               gboolean            clear_and_select,
391                                                               gboolean            clamp_node);
392 static gboolean pspp_sheet_view_has_special_cell               (PsppSheetView        *tree_view);
393 static void     column_sizing_notify                         (GObject            *object,
394                                                               GParamSpec         *pspec,
395                                                               gpointer            data);
396 static gboolean expand_collapse_timeout                      (gpointer            data);
397 static void     add_expand_collapse_timeout                  (PsppSheetView        *tree_view,
398                                                               GtkRBTree          *tree,
399                                                               GtkRBNode          *node,
400                                                               gboolean            expand);
401 static void     remove_expand_collapse_timeout               (PsppSheetView        *tree_view);
402 static void     cancel_arrow_animation                       (PsppSheetView        *tree_view);
403 static gboolean do_expand_collapse                           (PsppSheetView        *tree_view);
404 static void     pspp_sheet_view_stop_rubber_band               (PsppSheetView        *tree_view);
405 static void     update_prelight                              (PsppSheetView        *tree_view,
406                                                               int                 x,
407                                                               int                 y);
408
409 /* interactive search */
410 static void     pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view);
411 static void     pspp_sheet_view_search_dialog_hide     (GtkWidget        *search_dialog,
412                                                          PsppSheetView      *tree_view);
413 static void     pspp_sheet_view_search_position_func      (PsppSheetView      *tree_view,
414                                                          GtkWidget        *search_dialog,
415                                                          gpointer          user_data);
416 static void     pspp_sheet_view_search_disable_popdown    (GtkEntry         *entry,
417                                                          GtkMenu          *menu,
418                                                          gpointer          data);
419 static void     pspp_sheet_view_search_preedit_changed    (GtkIMContext     *im_context,
420                                                          PsppSheetView      *tree_view);
421 static void     pspp_sheet_view_search_activate           (GtkEntry         *entry,
422                                                          PsppSheetView      *tree_view);
423 static gboolean pspp_sheet_view_real_search_enable_popdown(gpointer          data);
424 static void     pspp_sheet_view_search_enable_popdown     (GtkWidget        *widget,
425                                                          gpointer          data);
426 static gboolean pspp_sheet_view_search_delete_event       (GtkWidget        *widget,
427                                                          GdkEventAny      *event,
428                                                          PsppSheetView      *tree_view);
429 static gboolean pspp_sheet_view_search_button_press_event (GtkWidget        *widget,
430                                                          GdkEventButton   *event,
431                                                          PsppSheetView      *tree_view);
432 static gboolean pspp_sheet_view_search_scroll_event       (GtkWidget        *entry,
433                                                          GdkEventScroll   *event,
434                                                          PsppSheetView      *tree_view);
435 static gboolean pspp_sheet_view_search_key_press_event    (GtkWidget        *entry,
436                                                          GdkEventKey      *event,
437                                                          PsppSheetView      *tree_view);
438 static gboolean pspp_sheet_view_search_move               (GtkWidget        *window,
439                                                          PsppSheetView      *tree_view,
440                                                          gboolean          up);
441 static gboolean pspp_sheet_view_search_equal_func         (GtkTreeModel     *model,
442                                                          gint              column,
443                                                          const gchar      *key,
444                                                          GtkTreeIter      *iter,
445                                                          gpointer          search_data);
446 static gboolean pspp_sheet_view_search_iter               (GtkTreeModel     *model,
447                                                          PsppSheetSelection *selection,
448                                                          GtkTreeIter      *iter,
449                                                          const gchar      *text,
450                                                          gint             *count,
451                                                          gint              n);
452 static void     pspp_sheet_view_search_init               (GtkWidget        *entry,
453                                                          PsppSheetView      *tree_view);
454 static void     pspp_sheet_view_put                       (PsppSheetView      *tree_view,
455                                                          GtkWidget        *child_widget,
456                                                          gint              x,
457                                                          gint              y,
458                                                          gint              width,
459                                                          gint              height);
460 static gboolean pspp_sheet_view_start_editing             (PsppSheetView      *tree_view,
461                                                          GtkTreePath      *cursor_path);
462 static void pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
463                                               PsppSheetViewColumn *column,
464                                               GtkTreePath       *path,
465                                               GtkCellEditable   *cell_editable,
466                                               GdkRectangle      *cell_area,
467                                               GdkEvent          *event,
468                                               guint              flags);
469 static void pspp_sheet_view_stop_editing                  (PsppSheetView *tree_view,
470                                                          gboolean     cancel_editing);
471 static gboolean pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
472                                                              gboolean     keybinding);
473 static gboolean pspp_sheet_view_start_interactive_search      (PsppSheetView *tree_view);
474 static PsppSheetViewColumn *pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
475                                                          PsppSheetViewColumn *column,
476                                                          gint               drop_position);
477
478 /* GtkBuildable */
479 static void pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
480                                                GtkBuilder  *builder,
481                                                GObject     *child,
482                                                const gchar *type);
483 static void pspp_sheet_view_buildable_init      (GtkBuildableIface *iface);
484
485
486 static gboolean scroll_row_timeout                   (gpointer     data);
487 static void     add_scroll_timeout                   (PsppSheetView *tree_view);
488 static void     remove_scroll_timeout                (PsppSheetView *tree_view);
489
490 static guint tree_view_signals [LAST_SIGNAL] = { 0 };
491
492 \f
493
494 /* GType Methods
495  */
496
497 G_DEFINE_TYPE_WITH_CODE (PsppSheetView, pspp_sheet_view, GTK_TYPE_CONTAINER,
498                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
499                                                 pspp_sheet_view_buildable_init))
500
501 static void
502 pspp_sheet_view_class_init (PsppSheetViewClass *class)
503 {
504   GObjectClass *o_class;
505   GtkObjectClass *object_class;
506   GtkWidgetClass *widget_class;
507   GtkContainerClass *container_class;
508   GtkBindingSet *binding_set;
509
510   binding_set = gtk_binding_set_by_class (class);
511
512   o_class = (GObjectClass *) class;
513   object_class = (GtkObjectClass *) class;
514   widget_class = (GtkWidgetClass *) class;
515   container_class = (GtkContainerClass *) class;
516
517   /* GObject signals */
518   o_class->set_property = pspp_sheet_view_set_property;
519   o_class->get_property = pspp_sheet_view_get_property;
520   o_class->finalize = pspp_sheet_view_finalize;
521
522   /* GtkObject signals */
523   object_class->destroy = pspp_sheet_view_destroy;
524
525   /* GtkWidget signals */
526   widget_class->map = pspp_sheet_view_map;
527   widget_class->realize = pspp_sheet_view_realize;
528   widget_class->unrealize = pspp_sheet_view_unrealize;
529   widget_class->size_request = pspp_sheet_view_size_request;
530   widget_class->size_allocate = pspp_sheet_view_size_allocate;
531   widget_class->button_press_event = pspp_sheet_view_button_press;
532   widget_class->button_release_event = pspp_sheet_view_button_release;
533   widget_class->grab_broken_event = pspp_sheet_view_grab_broken;
534   /*widget_class->configure_event = pspp_sheet_view_configure;*/
535   widget_class->motion_notify_event = pspp_sheet_view_motion;
536   widget_class->expose_event = pspp_sheet_view_expose;
537   widget_class->key_press_event = pspp_sheet_view_key_press;
538   widget_class->key_release_event = pspp_sheet_view_key_release;
539   widget_class->enter_notify_event = pspp_sheet_view_enter_notify;
540   widget_class->leave_notify_event = pspp_sheet_view_leave_notify;
541   widget_class->focus_out_event = pspp_sheet_view_focus_out;
542   widget_class->drag_begin = pspp_sheet_view_drag_begin;
543   widget_class->drag_end = pspp_sheet_view_drag_end;
544   widget_class->drag_data_get = pspp_sheet_view_drag_data_get;
545   widget_class->drag_data_delete = pspp_sheet_view_drag_data_delete;
546   widget_class->drag_leave = pspp_sheet_view_drag_leave;
547   widget_class->drag_motion = pspp_sheet_view_drag_motion;
548   widget_class->drag_drop = pspp_sheet_view_drag_drop;
549   widget_class->drag_data_received = pspp_sheet_view_drag_data_received;
550   widget_class->focus = pspp_sheet_view_focus;
551   widget_class->grab_focus = pspp_sheet_view_grab_focus;
552   widget_class->style_set = pspp_sheet_view_style_set;
553   widget_class->grab_notify = pspp_sheet_view_grab_notify;
554   widget_class->state_changed = pspp_sheet_view_state_changed;
555
556   /* GtkContainer signals */
557   container_class->remove = pspp_sheet_view_remove;
558   container_class->forall = pspp_sheet_view_forall;
559   container_class->set_focus_child = pspp_sheet_view_set_focus_child;
560
561   class->set_scroll_adjustments = pspp_sheet_view_set_adjustments;
562   class->move_cursor = pspp_sheet_view_real_move_cursor;
563   class->select_all = pspp_sheet_view_real_select_all;
564   class->unselect_all = pspp_sheet_view_real_unselect_all;
565   class->select_cursor_row = pspp_sheet_view_real_select_cursor_row;
566   class->toggle_cursor_row = pspp_sheet_view_real_toggle_cursor_row;
567   class->expand_collapse_cursor_row = pspp_sheet_view_real_expand_collapse_cursor_row;
568   class->select_cursor_parent = pspp_sheet_view_real_select_cursor_parent;
569   class->start_interactive_search = pspp_sheet_view_start_interactive_search;
570
571   /* Properties */
572
573   g_object_class_install_property (o_class,
574                                    PROP_MODEL,
575                                    g_param_spec_object ("model",
576                                                         P_("TreeView Model"),
577                                                         P_("The model for the tree view"),
578                                                         GTK_TYPE_TREE_MODEL,
579                                                         GTK_PARAM_READWRITE));
580
581   g_object_class_install_property (o_class,
582                                    PROP_HADJUSTMENT,
583                                    g_param_spec_object ("hadjustment",
584                                                         P_("Horizontal Adjustment"),
585                                                         P_("Horizontal Adjustment for the widget"),
586                                                         GTK_TYPE_ADJUSTMENT,
587                                                         GTK_PARAM_READWRITE));
588
589   g_object_class_install_property (o_class,
590                                    PROP_VADJUSTMENT,
591                                    g_param_spec_object ("vadjustment",
592                                                         P_("Vertical Adjustment"),
593                                                         P_("Vertical Adjustment for the widget"),
594                                                         GTK_TYPE_ADJUSTMENT,
595                                                         GTK_PARAM_READWRITE));
596
597   g_object_class_install_property (o_class,
598                                    PROP_HEADERS_VISIBLE,
599                                    g_param_spec_boolean ("headers-visible",
600                                                          P_("Headers Visible"),
601                                                          P_("Show the column header buttons"),
602                                                          TRUE,
603                                                          GTK_PARAM_READWRITE));
604
605   g_object_class_install_property (o_class,
606                                    PROP_HEADERS_CLICKABLE,
607                                    g_param_spec_boolean ("headers-clickable",
608                                                          P_("Headers Clickable"),
609                                                          P_("Column headers respond to click events"),
610                                                          TRUE,
611                                                          GTK_PARAM_READWRITE));
612
613   g_object_class_install_property (o_class,
614                                    PROP_EXPANDER_COLUMN,
615                                    g_param_spec_object ("expander-column",
616                                                         P_("Expander Column"),
617                                                         P_("Set the column for the expander column"),
618                                                         PSPP_TYPE_SHEET_VIEW_COLUMN,
619                                                         GTK_PARAM_READWRITE));
620
621   g_object_class_install_property (o_class,
622                                    PROP_REORDERABLE,
623                                    g_param_spec_boolean ("reorderable",
624                                                          P_("Reorderable"),
625                                                          P_("View is reorderable"),
626                                                          FALSE,
627                                                          GTK_PARAM_READWRITE));
628
629   g_object_class_install_property (o_class,
630                                    PROP_RULES_HINT,
631                                    g_param_spec_boolean ("rules-hint",
632                                                          P_("Rules Hint"),
633                                                          P_("Set a hint to the theme engine to draw rows in alternating colors"),
634                                                          FALSE,
635                                                          GTK_PARAM_READWRITE));
636
637     g_object_class_install_property (o_class,
638                                      PROP_ENABLE_SEARCH,
639                                      g_param_spec_boolean ("enable-search",
640                                                            P_("Enable Search"),
641                                                            P_("View allows user to search through columns interactively"),
642                                                            TRUE,
643                                                            GTK_PARAM_READWRITE));
644
645     g_object_class_install_property (o_class,
646                                      PROP_SEARCH_COLUMN,
647                                      g_param_spec_int ("search-column",
648                                                        P_("Search Column"),
649                                                        P_("Model column to search through during interactive search"),
650                                                        -1,
651                                                        G_MAXINT,
652                                                        -1,
653                                                        GTK_PARAM_READWRITE));
654
655     /**
656      * PsppSheetView:fixed-height-mode:
657      *
658      * Setting the ::fixed-height-mode property to %TRUE speeds up 
659      * #PsppSheetView by assuming that all rows have the same height. 
660      * Only enable this option if all rows are the same height.  
661      * Please see pspp_sheet_view_set_fixed_height_mode() for more 
662      * information on this option.
663      *
664      * Since: 2.4
665      **/
666     g_object_class_install_property (o_class,
667                                      PROP_FIXED_HEIGHT_MODE,
668                                      g_param_spec_boolean ("fixed-height-mode",
669                                                            P_("Fixed Height Mode"),
670                                                            P_("Speeds up PsppSheetView by assuming that all rows have the same height"),
671                                                            FALSE,
672                                                            GTK_PARAM_READWRITE));
673     
674     /**
675      * PsppSheetView:hover-selection:
676      * 
677      * Enables of disables the hover selection mode of @tree_view.
678      * Hover selection makes the selected row follow the pointer.
679      * Currently, this works only for the selection modes 
680      * %GTK_SELECTION_SINGLE and %GTK_SELECTION_BROWSE.
681      *
682      * This mode is primarily intended for treeviews in popups, e.g.
683      * in #GtkComboBox or #GtkEntryCompletion.
684      *
685      * Since: 2.6
686      */
687     g_object_class_install_property (o_class,
688                                      PROP_HOVER_SELECTION,
689                                      g_param_spec_boolean ("hover-selection",
690                                                            P_("Hover Selection"),
691                                                            P_("Whether the selection should follow the pointer"),
692                                                            FALSE,
693                                                            GTK_PARAM_READWRITE));
694
695     /**
696      * PsppSheetView:hover-expand:
697      * 
698      * Enables of disables the hover expansion mode of @tree_view.
699      * Hover expansion makes rows expand or collapse if the pointer moves 
700      * over them.
701      *
702      * This mode is primarily intended for treeviews in popups, e.g.
703      * in #GtkComboBox or #GtkEntryCompletion.
704      *
705      * Since: 2.6
706      */
707     g_object_class_install_property (o_class,
708                                      PROP_HOVER_EXPAND,
709                                      g_param_spec_boolean ("hover-expand",
710                                                            P_("Hover Expand"),
711                                                            P_("Whether rows should be expanded/collapsed when the pointer moves over them"),
712                                                            FALSE,
713                                                            GTK_PARAM_READWRITE));
714
715     /**
716      * PsppSheetView:show-expanders:
717      *
718      * %TRUE if the view has expanders.
719      *
720      * Since: 2.12
721      */
722     g_object_class_install_property (o_class,
723                                      PROP_SHOW_EXPANDERS,
724                                      g_param_spec_boolean ("show-expanders",
725                                                            P_("Show Expanders"),
726                                                            P_("View has expanders"),
727                                                            TRUE,
728                                                            GTK_PARAM_READWRITE));
729
730     /**
731      * PsppSheetView:level-indentation:
732      *
733      * Extra indentation for each level.
734      *
735      * Since: 2.12
736      */
737     g_object_class_install_property (o_class,
738                                      PROP_LEVEL_INDENTATION,
739                                      g_param_spec_int ("level-indentation",
740                                                        P_("Level Indentation"),
741                                                        P_("Extra indentation for each level"),
742                                                        0,
743                                                        G_MAXINT,
744                                                        0,
745                                                        GTK_PARAM_READWRITE));
746
747     g_object_class_install_property (o_class,
748                                      PROP_RUBBER_BANDING,
749                                      g_param_spec_boolean ("rubber-banding",
750                                                            P_("Rubber Banding"),
751                                                            P_("Whether to enable selection of multiple items by dragging the mouse pointer"),
752                                                            FALSE,
753                                                            GTK_PARAM_READWRITE));
754
755     g_object_class_install_property (o_class,
756                                      PROP_ENABLE_GRID_LINES,
757                                      g_param_spec_enum ("enable-grid-lines",
758                                                         P_("Enable Grid Lines"),
759                                                         P_("Whether grid lines should be drawn in the tree view"),
760                                                         PSPP_TYPE_SHEET_VIEW_GRID_LINES,
761                                                         PSPP_SHEET_VIEW_GRID_LINES_NONE,
762                                                         GTK_PARAM_READWRITE));
763
764     g_object_class_install_property (o_class,
765                                      PROP_ENABLE_TREE_LINES,
766                                      g_param_spec_boolean ("enable-tree-lines",
767                                                            P_("Enable Tree Lines"),
768                                                            P_("Whether tree lines should be drawn in the tree view"),
769                                                            FALSE,
770                                                            GTK_PARAM_READWRITE));
771
772     g_object_class_install_property (o_class,
773                                      PROP_TOOLTIP_COLUMN,
774                                      g_param_spec_int ("tooltip-column",
775                                                        P_("Tooltip Column"),
776                                                        P_("The column in the model containing the tooltip texts for the rows"),
777                                                        -1,
778                                                        G_MAXINT,
779                                                        -1,
780                                                        GTK_PARAM_READWRITE));
781
782   /* Style properties */
783 #define _TREE_VIEW_EXPANDER_SIZE 12
784 #define _TREE_VIEW_VERTICAL_SEPARATOR 2
785 #define _TREE_VIEW_HORIZONTAL_SEPARATOR 2
786
787   gtk_widget_class_install_style_property (widget_class,
788                                            g_param_spec_int ("expander-size",
789                                                              P_("Expander Size"),
790                                                              P_("Size of the expander arrow"),
791                                                              0,
792                                                              G_MAXINT,
793                                                              _TREE_VIEW_EXPANDER_SIZE,
794                                                              GTK_PARAM_READABLE));
795
796   gtk_widget_class_install_style_property (widget_class,
797                                            g_param_spec_int ("vertical-separator",
798                                                              P_("Vertical Separator Width"),
799                                                              P_("Vertical space between cells.  Must be an even number"),
800                                                              0,
801                                                              G_MAXINT,
802                                                              _TREE_VIEW_VERTICAL_SEPARATOR,
803                                                              GTK_PARAM_READABLE));
804
805   gtk_widget_class_install_style_property (widget_class,
806                                            g_param_spec_int ("horizontal-separator",
807                                                              P_("Horizontal Separator Width"),
808                                                              P_("Horizontal space between cells.  Must be an even number"),
809                                                              0,
810                                                              G_MAXINT,
811                                                              _TREE_VIEW_HORIZONTAL_SEPARATOR,
812                                                              GTK_PARAM_READABLE));
813
814   gtk_widget_class_install_style_property (widget_class,
815                                            g_param_spec_boolean ("allow-rules",
816                                                                  P_("Allow Rules"),
817                                                                  P_("Allow drawing of alternating color rows"),
818                                                                  TRUE,
819                                                                  GTK_PARAM_READABLE));
820
821   gtk_widget_class_install_style_property (widget_class,
822                                            g_param_spec_boolean ("indent-expanders",
823                                                                  P_("Indent Expanders"),
824                                                                  P_("Make the expanders indented"),
825                                                                  TRUE,
826                                                                  GTK_PARAM_READABLE));
827
828   gtk_widget_class_install_style_property (widget_class,
829                                            g_param_spec_boxed ("even-row-color",
830                                                                P_("Even Row Color"),
831                                                                P_("Color to use for even rows"),
832                                                                GDK_TYPE_COLOR,
833                                                                GTK_PARAM_READABLE));
834
835   gtk_widget_class_install_style_property (widget_class,
836                                            g_param_spec_boxed ("odd-row-color",
837                                                                P_("Odd Row Color"),
838                                                                P_("Color to use for odd rows"),
839                                                                GDK_TYPE_COLOR,
840                                                                GTK_PARAM_READABLE));
841
842   gtk_widget_class_install_style_property (widget_class,
843                                            g_param_spec_boolean ("row-ending-details",
844                                                                  P_("Row Ending details"),
845                                                                  P_("Enable extended row background theming"),
846                                                                  FALSE,
847                                                                  GTK_PARAM_READABLE));
848
849   gtk_widget_class_install_style_property (widget_class,
850                                            g_param_spec_int ("grid-line-width",
851                                                              P_("Grid line width"),
852                                                              P_("Width, in pixels, of the tree view grid lines"),
853                                                              0, G_MAXINT, 1,
854                                                              GTK_PARAM_READABLE));
855
856   gtk_widget_class_install_style_property (widget_class,
857                                            g_param_spec_int ("tree-line-width",
858                                                              P_("Tree line width"),
859                                                              P_("Width, in pixels, of the tree view lines"),
860                                                              0, G_MAXINT, 1,
861                                                              GTK_PARAM_READABLE));
862
863   gtk_widget_class_install_style_property (widget_class,
864                                            g_param_spec_string ("grid-line-pattern",
865                                                                 P_("Grid line pattern"),
866                                                                 P_("Dash pattern used to draw the tree view grid lines"),
867                                                                 "\1\1",
868                                                                 GTK_PARAM_READABLE));
869
870   gtk_widget_class_install_style_property (widget_class,
871                                            g_param_spec_string ("tree-line-pattern",
872                                                                 P_("Tree line pattern"),
873                                                                 P_("Dash pattern used to draw the tree view lines"),
874                                                                 "\1\1",
875                                                                 GTK_PARAM_READABLE));
876
877   /* Signals */
878   /**
879    * PsppSheetView::set-scroll-adjustments
880    * @horizontal: the horizontal #GtkAdjustment
881    * @vertical: the vertical #GtkAdjustment
882    *
883    * Set the scroll adjustments for the tree view. Usually scrolled containers
884    * like #GtkScrolledWindow will emit this signal to connect two instances
885    * of #GtkScrollbar to the scroll directions of the #PsppSheetView.
886    */
887   widget_class->set_scroll_adjustments_signal =
888     g_signal_new ("set-scroll-adjustments",
889                   G_TYPE_FROM_CLASS (o_class),
890                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
891                   G_STRUCT_OFFSET (PsppSheetViewClass, set_scroll_adjustments),
892                   NULL, NULL,
893                   psppire_marshal_VOID__OBJECT_OBJECT,
894                   G_TYPE_NONE, 2,
895                   GTK_TYPE_ADJUSTMENT,
896                   GTK_TYPE_ADJUSTMENT);
897
898   /**
899    * PsppSheetView::row-activated:
900    * @tree_view: the object on which the signal is emitted
901    * @path: the #GtkTreePath for the activated row
902    * @column: the #PsppSheetViewColumn in which the activation occurred
903    *
904    * The "row-activated" signal is emitted when the method
905    * pspp_sheet_view_row_activated() is called or the user double clicks 
906    * a treeview row. It is also emitted when a non-editable row is 
907    * selected and one of the keys: Space, Shift+Space, Return or 
908    * Enter is pressed.
909    * 
910    * For selection handling refer to the <link linkend="TreeWidget">tree 
911    * widget conceptual overview</link> as well as #PsppSheetSelection.
912    */
913   tree_view_signals[ROW_ACTIVATED] =
914     g_signal_new ("row-activated",
915                   G_TYPE_FROM_CLASS (o_class),
916                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
917                   G_STRUCT_OFFSET (PsppSheetViewClass, row_activated),
918                   NULL, NULL,
919                   psppire_marshal_VOID__BOXED_OBJECT,
920                   G_TYPE_NONE, 2,
921                   GTK_TYPE_TREE_PATH,
922                   PSPP_TYPE_SHEET_VIEW_COLUMN);
923
924   /**
925    * PsppSheetView::test-expand-row:
926    * @tree_view: the object on which the signal is emitted
927    * @iter: the tree iter of the row to expand
928    * @path: a tree path that points to the row 
929    * 
930    * The given row is about to be expanded (show its children nodes). Use this
931    * signal if you need to control the expandability of individual rows.
932    *
933    * Returns: %FALSE to allow expansion, %TRUE to reject
934    */
935   tree_view_signals[TEST_EXPAND_ROW] =
936     g_signal_new ("test-expand-row",
937                   G_TYPE_FROM_CLASS (o_class),
938                   G_SIGNAL_RUN_LAST,
939                   G_STRUCT_OFFSET (PsppSheetViewClass, test_expand_row),
940                   _gtk_boolean_handled_accumulator, NULL,
941                   psppire_marshal_BOOLEAN__BOXED_BOXED,
942                   G_TYPE_BOOLEAN, 2,
943                   GTK_TYPE_TREE_ITER,
944                   GTK_TYPE_TREE_PATH);
945
946   /**
947    * PsppSheetView::test-collapse-row:
948    * @tree_view: the object on which the signal is emitted
949    * @iter: the tree iter of the row to collapse
950    * @path: a tree path that points to the row 
951    * 
952    * The given row is about to be collapsed (hide its children nodes). Use this
953    * signal if you need to control the collapsibility of individual rows.
954    *
955    * Returns: %FALSE to allow collapsing, %TRUE to reject
956    */
957   tree_view_signals[TEST_COLLAPSE_ROW] =
958     g_signal_new ("test-collapse-row",
959                   G_TYPE_FROM_CLASS (o_class),
960                   G_SIGNAL_RUN_LAST,
961                   G_STRUCT_OFFSET (PsppSheetViewClass, test_collapse_row),
962                   _gtk_boolean_handled_accumulator, NULL,
963                   psppire_marshal_BOOLEAN__BOXED_BOXED,
964                   G_TYPE_BOOLEAN, 2,
965                   GTK_TYPE_TREE_ITER,
966                   GTK_TYPE_TREE_PATH);
967
968   /**
969    * PsppSheetView::row-expanded:
970    * @tree_view: the object on which the signal is emitted
971    * @iter: the tree iter of the expanded row
972    * @path: a tree path that points to the row 
973    * 
974    * The given row has been expanded (child nodes are shown).
975    */
976   tree_view_signals[ROW_EXPANDED] =
977     g_signal_new ("row-expanded",
978                   G_TYPE_FROM_CLASS (o_class),
979                   G_SIGNAL_RUN_LAST,
980                   G_STRUCT_OFFSET (PsppSheetViewClass, row_expanded),
981                   NULL, NULL,
982                   psppire_marshal_VOID__BOXED_BOXED,
983                   G_TYPE_NONE, 2,
984                   GTK_TYPE_TREE_ITER,
985                   GTK_TYPE_TREE_PATH);
986
987   /**
988    * PsppSheetView::row-collapsed:
989    * @tree_view: the object on which the signal is emitted
990    * @iter: the tree iter of the collapsed row
991    * @path: a tree path that points to the row 
992    * 
993    * The given row has been collapsed (child nodes are hidden).
994    */
995   tree_view_signals[ROW_COLLAPSED] =
996     g_signal_new ("row-collapsed",
997                   G_TYPE_FROM_CLASS (o_class),
998                   G_SIGNAL_RUN_LAST,
999                   G_STRUCT_OFFSET (PsppSheetViewClass, row_collapsed),
1000                   NULL, NULL,
1001                   psppire_marshal_VOID__BOXED_BOXED,
1002                   G_TYPE_NONE, 2,
1003                   GTK_TYPE_TREE_ITER,
1004                   GTK_TYPE_TREE_PATH);
1005
1006   /**
1007    * PsppSheetView::columns-changed:
1008    * @tree_view: the object on which the signal is emitted 
1009    * 
1010    * The number of columns of the treeview has changed.
1011    */
1012   tree_view_signals[COLUMNS_CHANGED] =
1013     g_signal_new ("columns-changed",
1014                   G_TYPE_FROM_CLASS (o_class),
1015                   G_SIGNAL_RUN_LAST,
1016                   G_STRUCT_OFFSET (PsppSheetViewClass, columns_changed),
1017                   NULL, NULL,
1018                   g_cclosure_marshal_VOID__VOID,
1019                   G_TYPE_NONE, 0);
1020
1021   /**
1022    * PsppSheetView::cursor-changed:
1023    * @tree_view: the object on which the signal is emitted
1024    * 
1025    * The position of the cursor (focused cell) has changed.
1026    */
1027   tree_view_signals[CURSOR_CHANGED] =
1028     g_signal_new ("cursor-changed",
1029                   G_TYPE_FROM_CLASS (o_class),
1030                   G_SIGNAL_RUN_LAST,
1031                   G_STRUCT_OFFSET (PsppSheetViewClass, cursor_changed),
1032                   NULL, NULL,
1033                   g_cclosure_marshal_VOID__VOID,
1034                   G_TYPE_NONE, 0);
1035
1036   tree_view_signals[MOVE_CURSOR] =
1037     g_signal_new ("move-cursor",
1038                   G_TYPE_FROM_CLASS (object_class),
1039                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1040                   G_STRUCT_OFFSET (PsppSheetViewClass, move_cursor),
1041                   NULL, NULL,
1042                   psppire_marshal_BOOLEAN__ENUM_INT,
1043                   G_TYPE_BOOLEAN, 2,
1044                   GTK_TYPE_MOVEMENT_STEP,
1045                   G_TYPE_INT);
1046
1047   tree_view_signals[SELECT_ALL] =
1048     g_signal_new ("select-all",
1049                   G_TYPE_FROM_CLASS (object_class),
1050                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1051                   G_STRUCT_OFFSET (PsppSheetViewClass, select_all),
1052                   NULL, NULL,
1053                   psppire_marshal_BOOLEAN__VOID,
1054                   G_TYPE_BOOLEAN, 0);
1055
1056   tree_view_signals[UNSELECT_ALL] =
1057     g_signal_new ("unselect-all",
1058                   G_TYPE_FROM_CLASS (object_class),
1059                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1060                   G_STRUCT_OFFSET (PsppSheetViewClass, unselect_all),
1061                   NULL, NULL,
1062                   psppire_marshal_BOOLEAN__VOID,
1063                   G_TYPE_BOOLEAN, 0);
1064
1065   tree_view_signals[SELECT_CURSOR_ROW] =
1066     g_signal_new ("select-cursor-row",
1067                   G_TYPE_FROM_CLASS (object_class),
1068                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1069                   G_STRUCT_OFFSET (PsppSheetViewClass, select_cursor_row),
1070                   NULL, NULL,
1071                   psppire_marshal_BOOLEAN__BOOLEAN,
1072                   G_TYPE_BOOLEAN, 1,
1073                   G_TYPE_BOOLEAN);
1074
1075   tree_view_signals[TOGGLE_CURSOR_ROW] =
1076     g_signal_new ("toggle-cursor-row",
1077                   G_TYPE_FROM_CLASS (object_class),
1078                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1079                   G_STRUCT_OFFSET (PsppSheetViewClass, toggle_cursor_row),
1080                   NULL, NULL,
1081                   psppire_marshal_BOOLEAN__VOID,
1082                   G_TYPE_BOOLEAN, 0);
1083
1084   tree_view_signals[EXPAND_COLLAPSE_CURSOR_ROW] =
1085     g_signal_new ("expand-collapse-cursor-row",
1086                   G_TYPE_FROM_CLASS (object_class),
1087                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1088                   G_STRUCT_OFFSET (PsppSheetViewClass, expand_collapse_cursor_row),
1089                   NULL, NULL,
1090                   psppire_marshal_BOOLEAN__BOOLEAN_BOOLEAN_BOOLEAN,
1091                   G_TYPE_BOOLEAN, 3,
1092                   G_TYPE_BOOLEAN,
1093                   G_TYPE_BOOLEAN,
1094                   G_TYPE_BOOLEAN);
1095
1096   tree_view_signals[SELECT_CURSOR_PARENT] =
1097     g_signal_new ("select-cursor-parent",
1098                   G_TYPE_FROM_CLASS (object_class),
1099                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1100                   G_STRUCT_OFFSET (PsppSheetViewClass, select_cursor_parent),
1101                   NULL, NULL,
1102                   psppire_marshal_BOOLEAN__VOID,
1103                   G_TYPE_BOOLEAN, 0);
1104
1105   tree_view_signals[START_INTERACTIVE_SEARCH] =
1106     g_signal_new ("start-interactive-search",
1107                   G_TYPE_FROM_CLASS (object_class),
1108                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1109                   G_STRUCT_OFFSET (PsppSheetViewClass, start_interactive_search),
1110                   NULL, NULL,
1111                   psppire_marshal_BOOLEAN__VOID,
1112                   G_TYPE_BOOLEAN, 0);
1113
1114   /* Key bindings */
1115   pspp_sheet_view_add_move_binding (binding_set, GDK_Up, 0, TRUE,
1116                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
1117   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_Up, 0, TRUE,
1118                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
1119
1120   pspp_sheet_view_add_move_binding (binding_set, GDK_Down, 0, TRUE,
1121                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
1122   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_Down, 0, TRUE,
1123                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
1124
1125   pspp_sheet_view_add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK, FALSE,
1126                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
1127
1128   pspp_sheet_view_add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK, FALSE,
1129                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
1130
1131   pspp_sheet_view_add_move_binding (binding_set, GDK_Home, 0, TRUE,
1132                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
1133   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_Home, 0, TRUE,
1134                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
1135
1136   pspp_sheet_view_add_move_binding (binding_set, GDK_End, 0, TRUE,
1137                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
1138   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_End, 0, TRUE,
1139                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
1140
1141   pspp_sheet_view_add_move_binding (binding_set, GDK_Page_Up, 0, TRUE,
1142                                   GTK_MOVEMENT_PAGES, -1);
1143   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_Page_Up, 0, TRUE,
1144                                   GTK_MOVEMENT_PAGES, -1);
1145
1146   pspp_sheet_view_add_move_binding (binding_set, GDK_Page_Down, 0, TRUE,
1147                                   GTK_MOVEMENT_PAGES, 1);
1148   pspp_sheet_view_add_move_binding (binding_set, GDK_KP_Page_Down, 0, TRUE,
1149                                   GTK_MOVEMENT_PAGES, 1);
1150
1151
1152   gtk_binding_entry_add_signal (binding_set, GDK_Right, 0, "move-cursor", 2,
1153                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1154                                 G_TYPE_INT, 1);
1155
1156   gtk_binding_entry_add_signal (binding_set, GDK_Left, 0, "move-cursor", 2,
1157                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1158                                 G_TYPE_INT, -1);
1159
1160   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0, "move-cursor", 2,
1161                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1162                                 G_TYPE_INT, 1);
1163
1164   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0, "move-cursor", 2,
1165                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1166                                 G_TYPE_INT, -1);
1167
1168   gtk_binding_entry_add_signal (binding_set, GDK_Right, GDK_CONTROL_MASK,
1169                                 "move-cursor", 2,
1170                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1171                                 G_TYPE_INT, 1);
1172
1173   gtk_binding_entry_add_signal (binding_set, GDK_Left, GDK_CONTROL_MASK,
1174                                 "move-cursor", 2,
1175                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1176                                 G_TYPE_INT, -1);
1177
1178   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1179                                 "move-cursor", 2,
1180                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1181                                 G_TYPE_INT, 1);
1182
1183   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1184                                 "move-cursor", 2,
1185                                 G_TYPE_ENUM, GTK_MOVEMENT_VISUAL_POSITIONS,
1186                                 G_TYPE_INT, -1);
1187
1188   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
1189   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_CONTROL_MASK, "toggle-cursor-row", 0);
1190
1191   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK, "select-all", 0);
1192   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK, "select-all", 0);
1193
1194   gtk_binding_entry_add_signal (binding_set, GDK_A, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "unselect-all", 0);
1195   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK, "unselect-all", 0);
1196
1197   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_SHIFT_MASK, "select-cursor-row", 1,
1198                                 G_TYPE_BOOLEAN, TRUE);
1199   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_SHIFT_MASK, "select-cursor-row", 1,
1200                                 G_TYPE_BOOLEAN, TRUE);
1201
1202   gtk_binding_entry_add_signal (binding_set, GDK_space, 0, "select-cursor-row", 1,
1203                                 G_TYPE_BOOLEAN, TRUE);
1204   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0, "select-cursor-row", 1,
1205                                 G_TYPE_BOOLEAN, TRUE);
1206   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0, "select-cursor-row", 1,
1207                                 G_TYPE_BOOLEAN, TRUE);
1208   gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0, "select-cursor-row", 1,
1209                                 G_TYPE_BOOLEAN, TRUE);
1210   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0, "select-cursor-row", 1,
1211                                 G_TYPE_BOOLEAN, TRUE);
1212
1213   /* expand and collapse rows */
1214   gtk_binding_entry_add_signal (binding_set, GDK_plus, 0, "expand-collapse-cursor-row", 3,
1215                                 G_TYPE_BOOLEAN, TRUE,
1216                                 G_TYPE_BOOLEAN, TRUE,
1217                                 G_TYPE_BOOLEAN, FALSE);
1218
1219   gtk_binding_entry_add_signal (binding_set, GDK_asterisk, 0,
1220                                 "expand-collapse-cursor-row", 3,
1221                                 G_TYPE_BOOLEAN, TRUE,
1222                                 G_TYPE_BOOLEAN, TRUE,
1223                                 G_TYPE_BOOLEAN, TRUE);
1224   gtk_binding_entry_add_signal (binding_set, GDK_KP_Multiply, 0,
1225                                 "expand-collapse-cursor-row", 3,
1226                                 G_TYPE_BOOLEAN, TRUE,
1227                                 G_TYPE_BOOLEAN, TRUE,
1228                                 G_TYPE_BOOLEAN, TRUE);
1229
1230   gtk_binding_entry_add_signal (binding_set, GDK_slash, 0,
1231                                 "expand-collapse-cursor-row", 3,
1232                                 G_TYPE_BOOLEAN, TRUE,
1233                                 G_TYPE_BOOLEAN, FALSE,
1234                                 G_TYPE_BOOLEAN, FALSE);
1235   gtk_binding_entry_add_signal (binding_set, GDK_KP_Divide, 0,
1236                                 "expand-collapse-cursor-row", 3,
1237                                 G_TYPE_BOOLEAN, TRUE,
1238                                 G_TYPE_BOOLEAN, FALSE,
1239                                 G_TYPE_BOOLEAN, FALSE);
1240
1241   /* Not doable on US keyboards */
1242   gtk_binding_entry_add_signal (binding_set, GDK_plus, GDK_SHIFT_MASK, "expand-collapse-cursor-row", 3,
1243                                 G_TYPE_BOOLEAN, TRUE,
1244                                 G_TYPE_BOOLEAN, TRUE,
1245                                 G_TYPE_BOOLEAN, TRUE);
1246   gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, 0, "expand-collapse-cursor-row", 3,
1247                                 G_TYPE_BOOLEAN, TRUE,
1248                                 G_TYPE_BOOLEAN, TRUE,
1249                                 G_TYPE_BOOLEAN, FALSE);
1250   gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, GDK_SHIFT_MASK, "expand-collapse-cursor-row", 3,
1251                                 G_TYPE_BOOLEAN, TRUE,
1252                                 G_TYPE_BOOLEAN, TRUE,
1253                                 G_TYPE_BOOLEAN, TRUE);
1254   gtk_binding_entry_add_signal (binding_set, GDK_KP_Add, GDK_SHIFT_MASK, "expand-collapse-cursor-row", 3,
1255                                 G_TYPE_BOOLEAN, TRUE,
1256                                 G_TYPE_BOOLEAN, TRUE,
1257                                 G_TYPE_BOOLEAN, TRUE);
1258   gtk_binding_entry_add_signal (binding_set, GDK_Right, GDK_SHIFT_MASK,
1259                                 "expand-collapse-cursor-row", 3,
1260                                 G_TYPE_BOOLEAN, FALSE,
1261                                 G_TYPE_BOOLEAN, TRUE,
1262                                 G_TYPE_BOOLEAN, TRUE);
1263   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, GDK_SHIFT_MASK,
1264                                 "expand-collapse-cursor-row", 3,
1265                                 G_TYPE_BOOLEAN, FALSE,
1266                                 G_TYPE_BOOLEAN, TRUE,
1267                                 G_TYPE_BOOLEAN, TRUE);
1268   gtk_binding_entry_add_signal (binding_set, GDK_Right,
1269                                 GDK_CONTROL_MASK | GDK_SHIFT_MASK,
1270                                 "expand-collapse-cursor-row", 3,
1271                                 G_TYPE_BOOLEAN, FALSE,
1272                                 G_TYPE_BOOLEAN, TRUE,
1273                                 G_TYPE_BOOLEAN, TRUE);
1274   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right,
1275                                 GDK_CONTROL_MASK | GDK_SHIFT_MASK,
1276                                 "expand-collapse-cursor-row", 3,
1277                                 G_TYPE_BOOLEAN, FALSE,
1278                                 G_TYPE_BOOLEAN, TRUE,
1279                                 G_TYPE_BOOLEAN, TRUE);
1280
1281   gtk_binding_entry_add_signal (binding_set, GDK_minus, 0, "expand-collapse-cursor-row", 3,
1282                                 G_TYPE_BOOLEAN, TRUE,
1283                                 G_TYPE_BOOLEAN, FALSE,
1284                                 G_TYPE_BOOLEAN, FALSE);
1285   gtk_binding_entry_add_signal (binding_set, GDK_minus, GDK_SHIFT_MASK, "expand-collapse-cursor-row", 3,
1286                                 G_TYPE_BOOLEAN, TRUE,
1287                                 G_TYPE_BOOLEAN, FALSE,
1288                                 G_TYPE_BOOLEAN, TRUE);
1289   gtk_binding_entry_add_signal (binding_set, GDK_KP_Subtract, 0, "expand-collapse-cursor-row", 3,
1290                                 G_TYPE_BOOLEAN, TRUE,
1291                                 G_TYPE_BOOLEAN, FALSE,
1292                                 G_TYPE_BOOLEAN, FALSE);
1293   gtk_binding_entry_add_signal (binding_set, GDK_KP_Subtract, GDK_SHIFT_MASK, "expand-collapse-cursor-row", 3,
1294                                 G_TYPE_BOOLEAN, TRUE,
1295                                 G_TYPE_BOOLEAN, FALSE,
1296                                 G_TYPE_BOOLEAN, TRUE);
1297   gtk_binding_entry_add_signal (binding_set, GDK_Left, GDK_SHIFT_MASK,
1298                                 "expand-collapse-cursor-row", 3,
1299                                 G_TYPE_BOOLEAN, FALSE,
1300                                 G_TYPE_BOOLEAN, FALSE,
1301                                 G_TYPE_BOOLEAN, TRUE);
1302   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, GDK_SHIFT_MASK,
1303                                 "expand-collapse-cursor-row", 3,
1304                                 G_TYPE_BOOLEAN, FALSE,
1305                                 G_TYPE_BOOLEAN, FALSE,
1306                                 G_TYPE_BOOLEAN, TRUE);
1307   gtk_binding_entry_add_signal (binding_set, GDK_Left,
1308                                 GDK_CONTROL_MASK | GDK_SHIFT_MASK,
1309                                 "expand-collapse-cursor-row", 3,
1310                                 G_TYPE_BOOLEAN, FALSE,
1311                                 G_TYPE_BOOLEAN, FALSE,
1312                                 G_TYPE_BOOLEAN, TRUE);
1313   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left,
1314                                 GDK_CONTROL_MASK | GDK_SHIFT_MASK,
1315                                 "expand-collapse-cursor-row", 3,
1316                                 G_TYPE_BOOLEAN, FALSE,
1317                                 G_TYPE_BOOLEAN, FALSE,
1318                                 G_TYPE_BOOLEAN, TRUE);
1319
1320   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0, "select-cursor-parent", 0);
1321   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK, "select-cursor-parent", 0);
1322
1323   gtk_binding_entry_add_signal (binding_set, GDK_f, GDK_CONTROL_MASK, "start-interactive-search", 0);
1324
1325   gtk_binding_entry_add_signal (binding_set, GDK_F, GDK_CONTROL_MASK, "start-interactive-search", 0);
1326
1327   g_type_class_add_private (o_class, sizeof (PsppSheetViewPrivate));
1328 }
1329
1330 static void
1331 pspp_sheet_view_buildable_init (GtkBuildableIface *iface)
1332 {
1333   iface->add_child = pspp_sheet_view_buildable_add_child;
1334 }
1335
1336 static void
1337 pspp_sheet_view_init (PsppSheetView *tree_view)
1338 {
1339   tree_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (tree_view, PSPP_TYPE_SHEET_VIEW, PsppSheetViewPrivate);
1340
1341   gtk_widget_set_can_focus (GTK_WIDGET (tree_view), TRUE);
1342   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (tree_view), FALSE);
1343
1344   tree_view->priv->flags =  PSPP_SHEET_VIEW_SHOW_EXPANDERS
1345                             | PSPP_SHEET_VIEW_DRAW_KEYFOCUS
1346                             | PSPP_SHEET_VIEW_HEADERS_VISIBLE;
1347
1348   /* We need some padding */
1349   tree_view->priv->dy = 0;
1350   tree_view->priv->cursor_offset = 0;
1351   tree_view->priv->n_columns = 0;
1352   tree_view->priv->header_height = 1;
1353   tree_view->priv->x_drag = 0;
1354   tree_view->priv->drag_pos = -1;
1355   tree_view->priv->header_has_focus = FALSE;
1356   tree_view->priv->pressed_button = -1;
1357   tree_view->priv->press_start_x = -1;
1358   tree_view->priv->press_start_y = -1;
1359   tree_view->priv->reorderable = FALSE;
1360   tree_view->priv->presize_handler_timer = 0;
1361   tree_view->priv->scroll_sync_timer = 0;
1362   tree_view->priv->fixed_height = -1;
1363   tree_view->priv->fixed_height_mode = FALSE;
1364   tree_view->priv->fixed_height_check = 0;
1365   pspp_sheet_view_set_adjustments (tree_view, NULL, NULL);
1366   tree_view->priv->selection = _pspp_sheet_selection_new_with_tree_view (tree_view);
1367   tree_view->priv->enable_search = TRUE;
1368   tree_view->priv->search_column = -1;
1369   tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
1370   tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
1371   tree_view->priv->search_custom_entry_set = FALSE;
1372   tree_view->priv->typeselect_flush_timeout = 0;
1373   tree_view->priv->init_hadjust_value = TRUE;    
1374   tree_view->priv->width = 0;
1375           
1376   tree_view->priv->hover_selection = FALSE;
1377   tree_view->priv->hover_expand = FALSE;
1378
1379   tree_view->priv->level_indentation = 0;
1380
1381   tree_view->priv->rubber_banding_enable = FALSE;
1382
1383   tree_view->priv->grid_lines = PSPP_SHEET_VIEW_GRID_LINES_NONE;
1384   tree_view->priv->tree_lines_enabled = FALSE;
1385
1386   tree_view->priv->tooltip_column = -1;
1387
1388   tree_view->priv->post_validation_flag = FALSE;
1389
1390   tree_view->priv->last_button_x = -1;
1391   tree_view->priv->last_button_y = -1;
1392
1393   tree_view->priv->event_last_x = -10000;
1394   tree_view->priv->event_last_y = -10000;
1395 }
1396
1397 \f
1398
1399 /* GObject Methods
1400  */
1401
1402 static void
1403 pspp_sheet_view_set_property (GObject         *object,
1404                             guint            prop_id,
1405                             const GValue    *value,
1406                             GParamSpec      *pspec)
1407 {
1408   PsppSheetView *tree_view;
1409
1410   tree_view = PSPP_SHEET_VIEW (object);
1411
1412   switch (prop_id)
1413     {
1414     case PROP_MODEL:
1415       pspp_sheet_view_set_model (tree_view, g_value_get_object (value));
1416       break;
1417     case PROP_HADJUSTMENT:
1418       pspp_sheet_view_set_hadjustment (tree_view, g_value_get_object (value));
1419       break;
1420     case PROP_VADJUSTMENT:
1421       pspp_sheet_view_set_vadjustment (tree_view, g_value_get_object (value));
1422       break;
1423     case PROP_HEADERS_VISIBLE:
1424       pspp_sheet_view_set_headers_visible (tree_view, g_value_get_boolean (value));
1425       break;
1426     case PROP_HEADERS_CLICKABLE:
1427       pspp_sheet_view_set_headers_clickable (tree_view, g_value_get_boolean (value));
1428       break;
1429     case PROP_EXPANDER_COLUMN:
1430       pspp_sheet_view_set_expander_column (tree_view, g_value_get_object (value));
1431       break;
1432     case PROP_REORDERABLE:
1433       pspp_sheet_view_set_reorderable (tree_view, g_value_get_boolean (value));
1434       break;
1435     case PROP_RULES_HINT:
1436       pspp_sheet_view_set_rules_hint (tree_view, g_value_get_boolean (value));
1437       break;
1438     case PROP_ENABLE_SEARCH:
1439       pspp_sheet_view_set_enable_search (tree_view, g_value_get_boolean (value));
1440       break;
1441     case PROP_SEARCH_COLUMN:
1442       pspp_sheet_view_set_search_column (tree_view, g_value_get_int (value));
1443       break;
1444     case PROP_FIXED_HEIGHT_MODE:
1445       pspp_sheet_view_set_fixed_height_mode (tree_view, g_value_get_boolean (value));
1446       break;
1447     case PROP_HOVER_SELECTION:
1448       tree_view->priv->hover_selection = g_value_get_boolean (value);
1449       break;
1450     case PROP_HOVER_EXPAND:
1451       tree_view->priv->hover_expand = g_value_get_boolean (value);
1452       break;
1453     case PROP_SHOW_EXPANDERS:
1454       pspp_sheet_view_set_show_expanders (tree_view, g_value_get_boolean (value));
1455       break;
1456     case PROP_LEVEL_INDENTATION:
1457       tree_view->priv->level_indentation = g_value_get_int (value);
1458       break;
1459     case PROP_RUBBER_BANDING:
1460       tree_view->priv->rubber_banding_enable = g_value_get_boolean (value);
1461       break;
1462     case PROP_ENABLE_GRID_LINES:
1463       pspp_sheet_view_set_grid_lines (tree_view, g_value_get_enum (value));
1464       break;
1465     case PROP_ENABLE_TREE_LINES:
1466       pspp_sheet_view_set_enable_tree_lines (tree_view, g_value_get_boolean (value));
1467       break;
1468     case PROP_TOOLTIP_COLUMN:
1469       pspp_sheet_view_set_tooltip_column (tree_view, g_value_get_int (value));
1470       break;
1471     default:
1472       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1473       break;
1474     }
1475 }
1476
1477 static void
1478 pspp_sheet_view_get_property (GObject    *object,
1479                             guint       prop_id,
1480                             GValue     *value,
1481                             GParamSpec *pspec)
1482 {
1483   PsppSheetView *tree_view;
1484
1485   tree_view = PSPP_SHEET_VIEW (object);
1486
1487   switch (prop_id)
1488     {
1489     case PROP_MODEL:
1490       g_value_set_object (value, tree_view->priv->model);
1491       break;
1492     case PROP_HADJUSTMENT:
1493       g_value_set_object (value, tree_view->priv->hadjustment);
1494       break;
1495     case PROP_VADJUSTMENT:
1496       g_value_set_object (value, tree_view->priv->vadjustment);
1497       break;
1498     case PROP_HEADERS_VISIBLE:
1499       g_value_set_boolean (value, pspp_sheet_view_get_headers_visible (tree_view));
1500       break;
1501     case PROP_HEADERS_CLICKABLE:
1502       g_value_set_boolean (value, pspp_sheet_view_get_headers_clickable (tree_view));
1503       break;
1504     case PROP_EXPANDER_COLUMN:
1505       g_value_set_object (value, tree_view->priv->expander_column);
1506       break;
1507     case PROP_REORDERABLE:
1508       g_value_set_boolean (value, tree_view->priv->reorderable);
1509       break;
1510     case PROP_RULES_HINT:
1511       g_value_set_boolean (value, tree_view->priv->has_rules);
1512       break;
1513     case PROP_ENABLE_SEARCH:
1514       g_value_set_boolean (value, tree_view->priv->enable_search);
1515       break;
1516     case PROP_SEARCH_COLUMN:
1517       g_value_set_int (value, tree_view->priv->search_column);
1518       break;
1519     case PROP_FIXED_HEIGHT_MODE:
1520       g_value_set_boolean (value, tree_view->priv->fixed_height_mode);
1521       break;
1522     case PROP_HOVER_SELECTION:
1523       g_value_set_boolean (value, tree_view->priv->hover_selection);
1524       break;
1525     case PROP_HOVER_EXPAND:
1526       g_value_set_boolean (value, tree_view->priv->hover_expand);
1527       break;
1528     case PROP_SHOW_EXPANDERS:
1529       g_value_set_boolean (value, PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS));
1530       break;
1531     case PROP_LEVEL_INDENTATION:
1532       g_value_set_int (value, tree_view->priv->level_indentation);
1533       break;
1534     case PROP_RUBBER_BANDING:
1535       g_value_set_boolean (value, tree_view->priv->rubber_banding_enable);
1536       break;
1537     case PROP_ENABLE_GRID_LINES:
1538       g_value_set_enum (value, tree_view->priv->grid_lines);
1539       break;
1540     case PROP_ENABLE_TREE_LINES:
1541       g_value_set_boolean (value, tree_view->priv->tree_lines_enabled);
1542       break;
1543     case PROP_TOOLTIP_COLUMN:
1544       g_value_set_int (value, tree_view->priv->tooltip_column);
1545       break;
1546     default:
1547       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1548       break;
1549     }
1550 }
1551
1552 static void
1553 pspp_sheet_view_finalize (GObject *object)
1554 {
1555   G_OBJECT_CLASS (pspp_sheet_view_parent_class)->finalize (object);
1556 }
1557
1558 \f
1559
1560 static void
1561 pspp_sheet_view_buildable_add_child (GtkBuildable *tree_view,
1562                                    GtkBuilder  *builder,
1563                                    GObject     *child,
1564                                    const gchar *type)
1565 {
1566   pspp_sheet_view_append_column (PSPP_SHEET_VIEW (tree_view), PSPP_SHEET_VIEW_COLUMN (child));
1567 }
1568
1569 /* GtkObject Methods
1570  */
1571
1572 static void
1573 pspp_sheet_view_free_rbtree (PsppSheetView *tree_view)
1574 {
1575   _pspp_rbtree_free (tree_view->priv->tree);
1576   
1577   tree_view->priv->tree = NULL;
1578   tree_view->priv->button_pressed_node = NULL;
1579   tree_view->priv->button_pressed_tree = NULL;
1580   tree_view->priv->prelight_tree = NULL;
1581   tree_view->priv->prelight_node = NULL;
1582   tree_view->priv->expanded_collapsed_node = NULL;
1583   tree_view->priv->expanded_collapsed_tree = NULL;
1584 }
1585
1586 static void
1587 pspp_sheet_view_destroy (GtkObject *object)
1588 {
1589   PsppSheetView *tree_view = PSPP_SHEET_VIEW (object);
1590   GList *list;
1591
1592   pspp_sheet_view_stop_editing (tree_view, TRUE);
1593
1594   if (tree_view->priv->columns != NULL)
1595     {
1596       list = tree_view->priv->columns;
1597       while (list)
1598         {
1599           PsppSheetViewColumn *column;
1600           column = PSPP_SHEET_VIEW_COLUMN (list->data);
1601           list = list->next;
1602           pspp_sheet_view_remove_column (tree_view, column);
1603         }
1604       tree_view->priv->columns = NULL;
1605     }
1606
1607   if (tree_view->priv->tree != NULL)
1608     {
1609       pspp_sheet_view_unref_and_check_selection_tree (tree_view, tree_view->priv->tree);
1610
1611       pspp_sheet_view_free_rbtree (tree_view);
1612     }
1613
1614   if (tree_view->priv->selection != NULL)
1615     {
1616       _pspp_sheet_selection_set_tree_view (tree_view->priv->selection, NULL);
1617       g_object_unref (tree_view->priv->selection);
1618       tree_view->priv->selection = NULL;
1619     }
1620
1621   if (tree_view->priv->scroll_to_path != NULL)
1622     {
1623       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
1624       tree_view->priv->scroll_to_path = NULL;
1625     }
1626
1627   if (tree_view->priv->drag_dest_row != NULL)
1628     {
1629       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
1630       tree_view->priv->drag_dest_row = NULL;
1631     }
1632
1633   if (tree_view->priv->top_row != NULL)
1634     {
1635       gtk_tree_row_reference_free (tree_view->priv->top_row);
1636       tree_view->priv->top_row = NULL;
1637     }
1638
1639   if (tree_view->priv->column_drop_func_data &&
1640       tree_view->priv->column_drop_func_data_destroy)
1641     {
1642       tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
1643       tree_view->priv->column_drop_func_data = NULL;
1644     }
1645
1646   if (tree_view->priv->destroy_count_destroy &&
1647       tree_view->priv->destroy_count_data)
1648     {
1649       tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
1650       tree_view->priv->destroy_count_data = NULL;
1651     }
1652
1653   gtk_tree_row_reference_free (tree_view->priv->cursor);
1654   tree_view->priv->cursor = NULL;
1655
1656   gtk_tree_row_reference_free (tree_view->priv->anchor);
1657   tree_view->priv->anchor = NULL;
1658
1659   /* destroy interactive search dialog */
1660   if (tree_view->priv->search_window)
1661     {
1662       gtk_widget_destroy (tree_view->priv->search_window);
1663       tree_view->priv->search_window = NULL;
1664       tree_view->priv->search_entry = NULL;
1665       if (tree_view->priv->typeselect_flush_timeout)
1666         {
1667           g_source_remove (tree_view->priv->typeselect_flush_timeout);
1668           tree_view->priv->typeselect_flush_timeout = 0;
1669         }
1670     }
1671
1672   if (tree_view->priv->search_destroy && tree_view->priv->search_user_data)
1673     {
1674       tree_view->priv->search_destroy (tree_view->priv->search_user_data);
1675       tree_view->priv->search_user_data = NULL;
1676     }
1677
1678   if (tree_view->priv->search_position_destroy && tree_view->priv->search_position_user_data)
1679     {
1680       tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
1681       tree_view->priv->search_position_user_data = NULL;
1682     }
1683
1684   if (tree_view->priv->row_separator_destroy && tree_view->priv->row_separator_data)
1685     {
1686       tree_view->priv->row_separator_destroy (tree_view->priv->row_separator_data);
1687       tree_view->priv->row_separator_data = NULL;
1688     }
1689   
1690   pspp_sheet_view_set_model (tree_view, NULL);
1691
1692   if (tree_view->priv->hadjustment)
1693     {
1694       g_object_unref (tree_view->priv->hadjustment);
1695       tree_view->priv->hadjustment = NULL;
1696     }
1697   if (tree_view->priv->vadjustment)
1698     {
1699       g_object_unref (tree_view->priv->vadjustment);
1700       tree_view->priv->vadjustment = NULL;
1701     }
1702
1703   GTK_OBJECT_CLASS (pspp_sheet_view_parent_class)->destroy (object);
1704 }
1705
1706 \f
1707
1708 /* GtkWidget Methods
1709  */
1710
1711 /* GtkWidget::map helper */
1712 static void
1713 pspp_sheet_view_map_buttons (PsppSheetView *tree_view)
1714 {
1715   GList *list;
1716
1717   g_return_if_fail (gtk_widget_get_mapped (GTK_WIDGET (tree_view)));
1718
1719   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
1720     {
1721       PsppSheetViewColumn *column;
1722
1723       for (list = tree_view->priv->columns; list; list = list->next)
1724         {
1725           column = list->data;
1726           if (gtk_widget_get_visible (column->button) &&
1727               !gtk_widget_get_mapped (column->button))
1728             gtk_widget_map (column->button);
1729         }
1730       for (list = tree_view->priv->columns; list; list = list->next)
1731         {
1732           column = list->data;
1733           if (column->visible == FALSE)
1734             continue;
1735           if (column->resizable)
1736             {
1737               gdk_window_raise (column->window);
1738               gdk_window_show (column->window);
1739             }
1740           else
1741             gdk_window_hide (column->window);
1742         }
1743       gdk_window_show (tree_view->priv->header_window);
1744     }
1745 }
1746
1747 static void
1748 pspp_sheet_view_map (GtkWidget *widget)
1749 {
1750   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1751   GList *tmp_list;
1752
1753   gtk_widget_set_mapped (widget, TRUE);
1754
1755   tmp_list = tree_view->priv->children;
1756   while (tmp_list)
1757     {
1758       PsppSheetViewChild *child = tmp_list->data;
1759       tmp_list = tmp_list->next;
1760
1761       if (gtk_widget_get_visible (child->widget))
1762         {
1763           if (!gtk_widget_get_mapped (child->widget))
1764             gtk_widget_map (child->widget);
1765         }
1766     }
1767   gdk_window_show (tree_view->priv->bin_window);
1768
1769   pspp_sheet_view_map_buttons (tree_view);
1770
1771   gdk_window_show (widget->window);
1772 }
1773
1774 static void
1775 pspp_sheet_view_realize (GtkWidget *widget)
1776 {
1777   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1778   GList *tmp_list;
1779   GdkWindowAttr attributes;
1780   gint attributes_mask;
1781
1782   gtk_widget_set_realized (widget, TRUE);
1783
1784   /* Make the main, clipping window */
1785   attributes.window_type = GDK_WINDOW_CHILD;
1786   attributes.x = widget->allocation.x;
1787   attributes.y = widget->allocation.y;
1788   attributes.width = widget->allocation.width;
1789   attributes.height = widget->allocation.height;
1790   attributes.wclass = GDK_INPUT_OUTPUT;
1791   attributes.visual = gtk_widget_get_visual (widget);
1792   attributes.colormap = gtk_widget_get_colormap (widget);
1793   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1794
1795   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1796
1797   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1798                                    &attributes, attributes_mask);
1799   gdk_window_set_user_data (widget->window, widget);
1800
1801   /* Make the window for the tree */
1802   attributes.x = 0;
1803   attributes.y = TREE_VIEW_HEADER_HEIGHT (tree_view);
1804   attributes.width = MAX (tree_view->priv->width, widget->allocation.width);
1805   attributes.height = widget->allocation.height;
1806   attributes.event_mask = (GDK_EXPOSURE_MASK |
1807                            GDK_SCROLL_MASK |
1808                            GDK_POINTER_MOTION_MASK |
1809                            GDK_ENTER_NOTIFY_MASK |
1810                            GDK_LEAVE_NOTIFY_MASK |
1811                            GDK_BUTTON_PRESS_MASK |
1812                            GDK_BUTTON_RELEASE_MASK |
1813                            gtk_widget_get_events (widget));
1814
1815   tree_view->priv->bin_window = gdk_window_new (widget->window,
1816                                                 &attributes, attributes_mask);
1817   gdk_window_set_user_data (tree_view->priv->bin_window, widget);
1818
1819   /* Make the column header window */
1820   attributes.x = 0;
1821   attributes.y = 0;
1822   attributes.width = MAX (tree_view->priv->width, widget->allocation.width);
1823   attributes.height = tree_view->priv->header_height;
1824   attributes.event_mask = (GDK_EXPOSURE_MASK |
1825                            GDK_SCROLL_MASK |
1826                            GDK_BUTTON_PRESS_MASK |
1827                            GDK_BUTTON_RELEASE_MASK |
1828                            GDK_KEY_PRESS_MASK |
1829                            GDK_KEY_RELEASE_MASK |
1830                            gtk_widget_get_events (widget));
1831
1832   tree_view->priv->header_window = gdk_window_new (widget->window,
1833                                                    &attributes, attributes_mask);
1834   gdk_window_set_user_data (tree_view->priv->header_window, widget);
1835
1836   /* Add them all up. */
1837   widget->style = gtk_style_attach (widget->style, widget->window);
1838   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
1839   gdk_window_set_background (tree_view->priv->bin_window, &widget->style->base[widget->state]);
1840   gtk_style_set_background (widget->style, tree_view->priv->header_window, GTK_STATE_NORMAL);
1841
1842   tmp_list = tree_view->priv->children;
1843   while (tmp_list)
1844     {
1845       PsppSheetViewChild *child = tmp_list->data;
1846       tmp_list = tmp_list->next;
1847
1848       gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
1849     }
1850
1851   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
1852     _pspp_sheet_view_column_realize_button (PSPP_SHEET_VIEW_COLUMN (tmp_list->data));
1853
1854   /* Need to call those here, since they create GCs */
1855   pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
1856   pspp_sheet_view_set_enable_tree_lines (tree_view, tree_view->priv->tree_lines_enabled);
1857
1858   install_presize_handler (tree_view); 
1859 }
1860
1861 static void
1862 pspp_sheet_view_unrealize (GtkWidget *widget)
1863 {
1864   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
1865   PsppSheetViewPrivate *priv = tree_view->priv;
1866   GList *list;
1867
1868   if (priv->scroll_timeout != 0)
1869     {
1870       g_source_remove (priv->scroll_timeout);
1871       priv->scroll_timeout = 0;
1872     }
1873
1874   if (priv->auto_expand_timeout != 0)
1875     {
1876       g_source_remove (priv->auto_expand_timeout);
1877       priv->auto_expand_timeout = 0;
1878     }
1879
1880   if (priv->open_dest_timeout != 0)
1881     {
1882       g_source_remove (priv->open_dest_timeout);
1883       priv->open_dest_timeout = 0;
1884     }
1885
1886   remove_expand_collapse_timeout (tree_view);
1887   
1888   if (priv->presize_handler_timer != 0)
1889     {
1890       g_source_remove (priv->presize_handler_timer);
1891       priv->presize_handler_timer = 0;
1892     }
1893
1894   if (priv->validate_rows_timer != 0)
1895     {
1896       g_source_remove (priv->validate_rows_timer);
1897       priv->validate_rows_timer = 0;
1898     }
1899
1900   if (priv->scroll_sync_timer != 0)
1901     {
1902       g_source_remove (priv->scroll_sync_timer);
1903       priv->scroll_sync_timer = 0;
1904     }
1905
1906   if (priv->typeselect_flush_timeout)
1907     {
1908       g_source_remove (priv->typeselect_flush_timeout);
1909       priv->typeselect_flush_timeout = 0;
1910     }
1911   
1912   for (list = priv->columns; list; list = list->next)
1913     _pspp_sheet_view_column_unrealize_button (PSPP_SHEET_VIEW_COLUMN (list->data));
1914
1915   gdk_window_set_user_data (priv->bin_window, NULL);
1916   gdk_window_destroy (priv->bin_window);
1917   priv->bin_window = NULL;
1918
1919   gdk_window_set_user_data (priv->header_window, NULL);
1920   gdk_window_destroy (priv->header_window);
1921   priv->header_window = NULL;
1922
1923   if (priv->drag_window)
1924     {
1925       gdk_window_set_user_data (priv->drag_window, NULL);
1926       gdk_window_destroy (priv->drag_window);
1927       priv->drag_window = NULL;
1928     }
1929
1930   if (priv->drag_highlight_window)
1931     {
1932       gdk_window_set_user_data (priv->drag_highlight_window, NULL);
1933       gdk_window_destroy (priv->drag_highlight_window);
1934       priv->drag_highlight_window = NULL;
1935     }
1936
1937   if (priv->tree_line_gc)
1938     {
1939       g_object_unref (priv->tree_line_gc);
1940       priv->tree_line_gc = NULL;
1941     }
1942
1943   if (priv->grid_line_gc)
1944     {
1945       g_object_unref (priv->grid_line_gc);
1946       priv->grid_line_gc = NULL;
1947     }
1948
1949   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->unrealize (widget);
1950 }
1951
1952 /* GtkWidget::size_request helper */
1953 static void
1954 pspp_sheet_view_size_request_columns (PsppSheetView *tree_view)
1955 {
1956   GList *list;
1957
1958   tree_view->priv->header_height = 0;
1959
1960   if (tree_view->priv->model)
1961     {
1962       for (list = tree_view->priv->columns; list; list = list->next)
1963         {
1964           GtkRequisition requisition;
1965           PsppSheetViewColumn *column = list->data;
1966
1967           if (column->button == NULL)
1968             continue;
1969
1970           column = list->data;
1971           
1972           gtk_widget_size_request (column->button, &requisition);
1973           column->button_request = requisition.width;
1974           tree_view->priv->header_height = MAX (tree_view->priv->header_height, requisition.height);
1975         }
1976     }
1977 }
1978
1979
1980 /* Called only by ::size_request */
1981 static void
1982 pspp_sheet_view_update_size (PsppSheetView *tree_view)
1983 {
1984   GList *list;
1985   PsppSheetViewColumn *column;
1986   gint i;
1987
1988   if (tree_view->priv->model == NULL)
1989     {
1990       tree_view->priv->width = 0;
1991       tree_view->priv->prev_width = 0;                   
1992       tree_view->priv->height = 0;
1993       return;
1994     }
1995
1996   tree_view->priv->prev_width = tree_view->priv->width;  
1997   tree_view->priv->width = 0;
1998
1999   /* keep this in sync with size_allocate below */
2000   for (list = tree_view->priv->columns, i = 0; list; list = list->next, i++)
2001     {
2002       gint real_requested_width = 0;
2003       column = list->data;
2004       if (!column->visible)
2005         continue;
2006
2007       if (column->use_resized_width)
2008         {
2009           real_requested_width = column->resized_width;
2010         }
2011       else if (column->column_type == PSPP_SHEET_VIEW_COLUMN_FIXED)
2012         {
2013           real_requested_width = column->fixed_width;
2014         }
2015       else if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
2016         {
2017           real_requested_width = MAX (column->requested_width, column->button_request);
2018         }
2019       else
2020         {
2021           real_requested_width = column->requested_width;
2022         }
2023
2024       if (column->min_width != -1)
2025         real_requested_width = MAX (real_requested_width, column->min_width);
2026       if (column->max_width != -1)
2027         real_requested_width = MIN (real_requested_width, column->max_width);
2028
2029       tree_view->priv->width += real_requested_width;
2030     }
2031
2032   if (tree_view->priv->tree == NULL)
2033     tree_view->priv->height = 0;
2034   else
2035     tree_view->priv->height = tree_view->priv->tree->root->offset;
2036 }
2037
2038 static void
2039 pspp_sheet_view_size_request (GtkWidget      *widget,
2040                             GtkRequisition *requisition)
2041 {
2042   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2043   GList *tmp_list;
2044
2045   /* we validate some rows initially just to make sure we have some size. 
2046    * In practice, with a lot of static lists, this should get a good width.
2047    */
2048   do_validate_rows (tree_view, FALSE);
2049   pspp_sheet_view_size_request_columns (tree_view);
2050   pspp_sheet_view_update_size (PSPP_SHEET_VIEW (widget));
2051
2052   requisition->width = tree_view->priv->width;
2053   requisition->height = tree_view->priv->height + TREE_VIEW_HEADER_HEIGHT (tree_view);
2054
2055   tmp_list = tree_view->priv->children;
2056
2057   while (tmp_list)
2058     {
2059       PsppSheetViewChild *child = tmp_list->data;
2060       GtkRequisition child_requisition;
2061
2062       tmp_list = tmp_list->next;
2063
2064       if (gtk_widget_get_visible (child->widget))
2065         gtk_widget_size_request (child->widget, &child_requisition);
2066     }
2067 }
2068
2069 static int
2070 pspp_sheet_view_calculate_width_before_expander (PsppSheetView *tree_view)
2071 {
2072   int width = 0;
2073   GList *list;
2074   gboolean rtl;
2075
2076   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2077   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
2078        list->data != tree_view->priv->expander_column;
2079        list = (rtl ? list->prev : list->next))
2080     {
2081       PsppSheetViewColumn *column = list->data;
2082
2083       width += column->width;
2084     }
2085
2086   return width;
2087 }
2088
2089 static void
2090 invalidate_column (PsppSheetView       *tree_view,
2091                    PsppSheetViewColumn *column)
2092 {
2093   gint column_offset = 0;
2094   GList *list;
2095   GtkWidget *widget = GTK_WIDGET (tree_view);
2096   gboolean rtl;
2097
2098   if (!gtk_widget_get_realized (widget))
2099     return;
2100
2101   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2102   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
2103        list;
2104        list = (rtl ? list->prev : list->next))
2105     {
2106       PsppSheetViewColumn *tmpcolumn = list->data;
2107       if (tmpcolumn == column)
2108         {
2109           GdkRectangle invalid_rect;
2110           
2111           invalid_rect.x = column_offset;
2112           invalid_rect.y = 0;
2113           invalid_rect.width = column->width;
2114           invalid_rect.height = widget->allocation.height;
2115           
2116           gdk_window_invalidate_rect (widget->window, &invalid_rect, TRUE);
2117           break;
2118         }
2119       
2120       column_offset += tmpcolumn->width;
2121     }
2122 }
2123
2124 static void
2125 invalidate_last_column (PsppSheetView *tree_view)
2126 {
2127   GList *last_column;
2128   gboolean rtl;
2129
2130   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2131
2132   for (last_column = (rtl ? g_list_first (tree_view->priv->columns) : g_list_last (tree_view->priv->columns));
2133        last_column;
2134        last_column = (rtl ? last_column->next : last_column->prev))
2135     {
2136       if (PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible)
2137         {
2138           invalidate_column (tree_view, last_column->data);
2139           return;
2140         }
2141     }
2142 }
2143
2144 static gint
2145 pspp_sheet_view_get_real_requested_width_from_column (PsppSheetView       *tree_view,
2146                                                     PsppSheetViewColumn *column)
2147 {
2148   gint real_requested_width;
2149
2150   if (column->use_resized_width)
2151     {
2152       real_requested_width = column->resized_width;
2153     }
2154   else if (column->column_type == PSPP_SHEET_VIEW_COLUMN_FIXED)
2155     {
2156       real_requested_width = column->fixed_width;
2157     }
2158   else if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
2159     {
2160       real_requested_width = MAX (column->requested_width, column->button_request);
2161     }
2162   else
2163     {
2164       real_requested_width = column->requested_width;
2165       if (real_requested_width < 0)
2166         real_requested_width = 0;
2167     }
2168
2169   if (column->min_width != -1)
2170     real_requested_width = MAX (real_requested_width, column->min_width);
2171   if (column->max_width != -1)
2172     real_requested_width = MIN (real_requested_width, column->max_width);
2173
2174   return real_requested_width;
2175 }
2176
2177 /* GtkWidget::size_allocate helper */
2178 static void
2179 pspp_sheet_view_size_allocate_columns (GtkWidget *widget,
2180                                      gboolean  *width_changed)
2181 {
2182   PsppSheetView *tree_view;
2183   GList *list, *first_column, *last_column;
2184   PsppSheetViewColumn *column;
2185   GtkAllocation allocation;
2186   gint width = 0;
2187   gint extra, extra_per_column, extra_for_last;
2188   gint full_requested_width = 0;
2189   gint number_of_expand_columns = 0;
2190   gboolean column_changed = FALSE;
2191   gboolean rtl;
2192   gboolean update_expand;
2193   
2194   tree_view = PSPP_SHEET_VIEW (widget);
2195
2196   for (last_column = g_list_last (tree_view->priv->columns);
2197        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
2198        last_column = last_column->prev)
2199     ;
2200   if (last_column == NULL)
2201     return;
2202
2203   for (first_column = g_list_first (tree_view->priv->columns);
2204        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
2205        first_column = first_column->next)
2206     ;
2207
2208   allocation.y = 0;
2209   allocation.height = tree_view->priv->header_height;
2210
2211   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2212
2213   /* find out how many extra space and expandable columns we have */
2214   for (list = tree_view->priv->columns; list != last_column->next; list = list->next)
2215     {
2216       column = (PsppSheetViewColumn *)list->data;
2217
2218       if (!column->visible)
2219         continue;
2220
2221       full_requested_width += pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
2222
2223       if (column->expand)
2224         number_of_expand_columns++;
2225     }
2226
2227   /* Only update the expand value if the width of the widget has changed,
2228    * or the number of expand columns has changed, or if there are no expand
2229    * columns, or if we didn't have an size-allocation yet after the
2230    * last validated node.
2231    */
2232   update_expand = (width_changed && *width_changed == TRUE)
2233       || number_of_expand_columns != tree_view->priv->last_number_of_expand_columns
2234       || number_of_expand_columns == 0
2235       || tree_view->priv->post_validation_flag == TRUE;
2236
2237   tree_view->priv->post_validation_flag = FALSE;
2238
2239   if (!update_expand)
2240     {
2241       extra = tree_view->priv->last_extra_space;
2242       extra_for_last = MAX (widget->allocation.width - full_requested_width - extra, 0);
2243     }
2244   else
2245     {
2246       extra = MAX (widget->allocation.width - full_requested_width, 0);
2247       extra_for_last = 0;
2248
2249       tree_view->priv->last_extra_space = extra;
2250     }
2251
2252   if (number_of_expand_columns > 0)
2253     extra_per_column = extra/number_of_expand_columns;
2254   else
2255     extra_per_column = 0;
2256
2257   if (update_expand)
2258     {
2259       tree_view->priv->last_extra_space_per_column = extra_per_column;
2260       tree_view->priv->last_number_of_expand_columns = number_of_expand_columns;
2261     }
2262
2263   for (list = (rtl ? last_column : first_column); 
2264        list != (rtl ? first_column->prev : last_column->next);
2265        list = (rtl ? list->prev : list->next)) 
2266     {
2267       gint real_requested_width = 0;
2268       gint old_width;
2269
2270       column = list->data;
2271       old_width = column->width;
2272
2273       if (!column->visible)
2274         continue;
2275
2276       /* We need to handle the dragged button specially.
2277        */
2278       if (column == tree_view->priv->drag_column)
2279         {
2280           GtkAllocation drag_allocation;
2281           gdk_drawable_get_size (tree_view->priv->drag_window,
2282                                  &(drag_allocation.width),
2283                                  &(drag_allocation.height));
2284           drag_allocation.x = 0;
2285           drag_allocation.y = 0;
2286           gtk_widget_size_allocate (tree_view->priv->drag_column->button,
2287                                     &drag_allocation);
2288           width += drag_allocation.width;
2289           continue;
2290         }
2291
2292       real_requested_width = pspp_sheet_view_get_real_requested_width_from_column (tree_view, column);
2293
2294       allocation.x = width;
2295       column->width = real_requested_width;
2296
2297       if (column->expand)
2298         {
2299           if (number_of_expand_columns == 1)
2300             {
2301               /* We add the remander to the last column as
2302                * */
2303               column->width += extra;
2304             }
2305           else
2306             {
2307               column->width += extra_per_column;
2308               extra -= extra_per_column;
2309               number_of_expand_columns --;
2310             }
2311         }
2312       else if (number_of_expand_columns == 0 &&
2313                list == last_column)
2314         {
2315           column->width += extra;
2316         }
2317
2318       /* In addition to expand, the last column can get even more
2319        * extra space so all available space is filled up.
2320        */
2321       if (extra_for_last > 0 && list == last_column)
2322         column->width += extra_for_last;
2323
2324       g_object_notify (G_OBJECT (column), "width");
2325
2326       allocation.width = column->width;
2327       width += column->width;
2328
2329       if (column->width > old_width)
2330         column_changed = TRUE;
2331
2332       gtk_widget_size_allocate (column->button, &allocation);
2333
2334       if (column->window)
2335         gdk_window_move_resize (column->window,
2336                                 allocation.x + (rtl ? 0 : allocation.width) - TREE_VIEW_DRAG_WIDTH/2,
2337                                 allocation.y,
2338                                 TREE_VIEW_DRAG_WIDTH, allocation.height);
2339     }
2340
2341   /* We change the width here.  The user might have been resizing columns,
2342    * so the total width of the tree view changes.
2343    */
2344   tree_view->priv->width = width;
2345   if (width_changed)
2346     *width_changed = TRUE;
2347
2348   if (column_changed)
2349     gtk_widget_queue_draw (GTK_WIDGET (tree_view));
2350 }
2351
2352
2353 static void
2354 pspp_sheet_view_size_allocate (GtkWidget     *widget,
2355                              GtkAllocation *allocation)
2356 {
2357   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2358   GList *tmp_list;
2359   gboolean width_changed = FALSE;
2360   gint old_width = widget->allocation.width;
2361
2362   if (allocation->width != widget->allocation.width)
2363     width_changed = TRUE;
2364
2365   widget->allocation = *allocation;
2366
2367   tmp_list = tree_view->priv->children;
2368
2369   while (tmp_list)
2370     {
2371       GtkAllocation allocation;
2372
2373       PsppSheetViewChild *child = tmp_list->data;
2374       tmp_list = tmp_list->next;
2375
2376       /* totally ignore our child's requisition */
2377       allocation.x = child->x;
2378       allocation.y = child->y;
2379       allocation.width = child->width;
2380       allocation.height = child->height;
2381       gtk_widget_size_allocate (child->widget, &allocation);
2382     }
2383
2384   /* We size-allocate the columns first because the width of the
2385    * tree view (used in updating the adjustments below) might change.
2386    */
2387   pspp_sheet_view_size_allocate_columns (widget, &width_changed);
2388
2389   tree_view->priv->hadjustment->page_size = allocation->width;
2390   tree_view->priv->hadjustment->page_increment = allocation->width * 0.9;
2391   tree_view->priv->hadjustment->step_increment = allocation->width * 0.1;
2392   tree_view->priv->hadjustment->lower = 0;
2393   tree_view->priv->hadjustment->upper = MAX (tree_view->priv->hadjustment->page_size, tree_view->priv->width);
2394
2395   if (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL)   
2396     {
2397       if (allocation->width < tree_view->priv->width)
2398         {
2399           if (tree_view->priv->init_hadjust_value)
2400             {
2401               tree_view->priv->hadjustment->value = MAX (tree_view->priv->width - allocation->width, 0);
2402               tree_view->priv->init_hadjust_value = FALSE;
2403             }
2404           else if (allocation->width != old_width)
2405             {
2406               tree_view->priv->hadjustment->value = CLAMP (tree_view->priv->hadjustment->value - allocation->width + old_width, 0, tree_view->priv->width - allocation->width);
2407             }
2408           else
2409             tree_view->priv->hadjustment->value = CLAMP (tree_view->priv->width - (tree_view->priv->prev_width - tree_view->priv->hadjustment->value), 0, tree_view->priv->width - allocation->width);
2410         }
2411       else
2412         {
2413           tree_view->priv->hadjustment->value = 0;
2414           tree_view->priv->init_hadjust_value = TRUE;
2415         }
2416     }
2417   else
2418     if (tree_view->priv->hadjustment->value + allocation->width > tree_view->priv->width)
2419       tree_view->priv->hadjustment->value = MAX (tree_view->priv->width - allocation->width, 0);
2420
2421   gtk_adjustment_changed (tree_view->priv->hadjustment);
2422
2423   tree_view->priv->vadjustment->page_size = allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view);
2424   tree_view->priv->vadjustment->step_increment = tree_view->priv->vadjustment->page_size * 0.1;
2425   tree_view->priv->vadjustment->page_increment = tree_view->priv->vadjustment->page_size * 0.9;
2426   tree_view->priv->vadjustment->lower = 0;
2427   tree_view->priv->vadjustment->upper = MAX (tree_view->priv->vadjustment->page_size, tree_view->priv->height);
2428
2429   gtk_adjustment_changed (tree_view->priv->vadjustment);
2430
2431   /* now the adjustments and window sizes are in sync, we can sync toprow/dy again */
2432   if (tree_view->priv->height <= tree_view->priv->vadjustment->page_size)
2433     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
2434   else if (tree_view->priv->vadjustment->value + tree_view->priv->vadjustment->page_size > tree_view->priv->height)
2435     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment),
2436                               tree_view->priv->height - tree_view->priv->vadjustment->page_size);
2437   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
2438     pspp_sheet_view_top_row_to_dy (tree_view);
2439   else
2440     pspp_sheet_view_dy_to_top_row (tree_view);
2441   
2442   if (gtk_widget_get_realized (widget))
2443     {
2444       gdk_window_move_resize (widget->window,
2445                               allocation->x, allocation->y,
2446                               allocation->width, allocation->height);
2447       gdk_window_move_resize (tree_view->priv->header_window,
2448                               - (gint) tree_view->priv->hadjustment->value,
2449                               0,
2450                               MAX (tree_view->priv->width, allocation->width),
2451                               tree_view->priv->header_height);
2452       gdk_window_move_resize (tree_view->priv->bin_window,
2453                               - (gint) tree_view->priv->hadjustment->value,
2454                               TREE_VIEW_HEADER_HEIGHT (tree_view),
2455                               MAX (tree_view->priv->width, allocation->width),
2456                               allocation->height - TREE_VIEW_HEADER_HEIGHT (tree_view));
2457     }
2458
2459   if (tree_view->priv->tree == NULL)
2460     invalidate_empty_focus (tree_view);
2461
2462   if (gtk_widget_get_realized (widget))
2463     {
2464       gboolean has_expand_column = FALSE;
2465       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
2466         {
2467           if (pspp_sheet_view_column_get_expand (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)))
2468             {
2469               has_expand_column = TRUE;
2470               break;
2471             }
2472         }
2473
2474       if (width_changed && tree_view->priv->expander_column)
2475         {
2476           /* Might seem awkward, but is the best heuristic I could come up
2477            * with.  Only if the width of the columns before the expander
2478            * changes, we will update the prelight status.  It is this
2479            * width that makes the expander move vertically.  Always updating
2480            * prelight status causes trouble with hover selections.
2481            */
2482           gint width_before_expander;
2483
2484           width_before_expander = pspp_sheet_view_calculate_width_before_expander (tree_view);
2485
2486           if (tree_view->priv->prev_width_before_expander
2487               != width_before_expander)
2488               update_prelight (tree_view,
2489                                tree_view->priv->event_last_x,
2490                                tree_view->priv->event_last_y);
2491
2492           tree_view->priv->prev_width_before_expander = width_before_expander;
2493         }
2494
2495       /* This little hack only works if we have an LTR locale, and no column has the  */
2496       if (width_changed)
2497         {
2498           if (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_LTR &&
2499               ! has_expand_column)
2500             invalidate_last_column (tree_view);
2501           else
2502             gtk_widget_queue_draw (widget);
2503         }
2504     }
2505 }
2506
2507 /* Grabs the focus and unsets the PSPP_SHEET_VIEW_DRAW_KEYFOCUS flag */
2508 static void
2509 grab_focus_and_unset_draw_keyfocus (PsppSheetView *tree_view)
2510 {
2511   GtkWidget *widget = GTK_WIDGET (tree_view);
2512
2513   if (gtk_widget_get_can_focus (widget) && !gtk_widget_has_focus (widget))
2514     gtk_widget_grab_focus (widget);
2515   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
2516 }
2517
2518 static inline gboolean
2519 row_is_separator (PsppSheetView *tree_view,
2520                   GtkTreeIter *iter,
2521                   GtkTreePath *path)
2522 {
2523   gboolean is_separator = FALSE;
2524
2525   if (tree_view->priv->row_separator_func)
2526     {
2527       GtkTreeIter tmpiter;
2528
2529       if (iter)
2530         tmpiter = *iter;
2531       else
2532         gtk_tree_model_get_iter (tree_view->priv->model, &tmpiter, path);
2533
2534       is_separator = tree_view->priv->row_separator_func (tree_view->priv->model,
2535                                                           &tmpiter,
2536                                                           tree_view->priv->row_separator_data);
2537     }
2538
2539   return is_separator;
2540 }
2541
2542 static gboolean
2543 pspp_sheet_view_button_press (GtkWidget      *widget,
2544                             GdkEventButton *event)
2545 {
2546   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
2547   GList *list;
2548   PsppSheetViewColumn *column = NULL;
2549   gint i;
2550   GdkRectangle background_area;
2551   GdkRectangle cell_area;
2552   gint vertical_separator;
2553   gint horizontal_separator;
2554   gboolean path_is_selectable;
2555   gboolean rtl;
2556
2557   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2558   pspp_sheet_view_stop_editing (tree_view, FALSE);
2559   gtk_widget_style_get (widget,
2560                         "vertical-separator", &vertical_separator,
2561                         "horizontal-separator", &horizontal_separator,
2562                         NULL);
2563
2564
2565   /* Because grab_focus can cause reentrancy, we delay grab_focus until after
2566    * we're done handling the button press.
2567    */
2568
2569   if (event->window == tree_view->priv->bin_window)
2570     {
2571       GtkRBNode *node;
2572       GtkRBTree *tree;
2573       GtkTreePath *path;
2574       gchar *path_string;
2575       gint depth;
2576       gint new_y;
2577       gint y_offset;
2578       gint dval;
2579       gint pre_val, aft_val;
2580       PsppSheetViewColumn *column = NULL;
2581       GtkCellRenderer *focus_cell = NULL;
2582       gint column_handled_click = FALSE;
2583       gboolean row_double_click = FALSE;
2584       gboolean rtl;
2585       gboolean node_selected;
2586
2587       /* Empty tree? */
2588       if (tree_view->priv->tree == NULL)
2589         {
2590           grab_focus_and_unset_draw_keyfocus (tree_view);
2591           return TRUE;
2592         }
2593
2594       /* are we in an arrow? */
2595       if (tree_view->priv->prelight_node &&
2596           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT) &&
2597           TREE_VIEW_DRAW_EXPANDERS (tree_view))
2598         {
2599           if (event->button == 1)
2600             {
2601               gtk_grab_add (widget);
2602               tree_view->priv->button_pressed_node = tree_view->priv->prelight_node;
2603               tree_view->priv->button_pressed_tree = tree_view->priv->prelight_tree;
2604               pspp_sheet_view_draw_arrow (PSPP_SHEET_VIEW (widget),
2605                                         tree_view->priv->prelight_tree,
2606                                         tree_view->priv->prelight_node,
2607                                         event->x,
2608                                         event->y);
2609             }
2610
2611           grab_focus_and_unset_draw_keyfocus (tree_view);
2612           return TRUE;
2613         }
2614
2615       /* find the node that was clicked */
2616       new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
2617       if (new_y < 0)
2618         new_y = 0;
2619       y_offset = -_pspp_rbtree_find_offset (tree_view->priv->tree, new_y, &tree, &node);
2620
2621       if (node == NULL)
2622         {
2623           /* We clicked in dead space */
2624           grab_focus_and_unset_draw_keyfocus (tree_view);
2625           return TRUE;
2626         }
2627
2628       /* Get the path and the node */
2629       path = _pspp_sheet_view_find_path (tree_view, tree, node);
2630       path_is_selectable = !row_is_separator (tree_view, NULL, path);
2631
2632       if (!path_is_selectable)
2633         {
2634           gtk_tree_path_free (path);
2635           grab_focus_and_unset_draw_keyfocus (tree_view);
2636           return TRUE;
2637         }
2638
2639       depth = gtk_tree_path_get_depth (path);
2640       background_area.y = y_offset + event->y;
2641       background_area.height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
2642       background_area.x = 0;
2643
2644
2645       /* Let the column have a chance at selecting it. */
2646       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
2647       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
2648            list; list = (rtl ? list->prev : list->next))
2649         {
2650           PsppSheetViewColumn *candidate = list->data;
2651
2652           if (!candidate->visible)
2653             continue;
2654
2655           background_area.width = candidate->width;
2656           if ((background_area.x > (gint) event->x) ||
2657               (background_area.x + background_area.width <= (gint) event->x))
2658             {
2659               background_area.x += background_area.width;
2660               continue;
2661             }
2662
2663           /* we found the focus column */
2664           column = candidate;
2665           cell_area = background_area;
2666           cell_area.width -= horizontal_separator;
2667           cell_area.height -= vertical_separator;
2668           cell_area.x += horizontal_separator/2;
2669           cell_area.y += vertical_separator/2;
2670           if (pspp_sheet_view_is_expander_column (tree_view, column))
2671             {
2672               if (!rtl)
2673                 cell_area.x += (depth - 1) * tree_view->priv->level_indentation;
2674               cell_area.width -= (depth - 1) * tree_view->priv->level_indentation;
2675
2676               if (TREE_VIEW_DRAW_EXPANDERS (tree_view))
2677                 {
2678                   if (!rtl)
2679                     cell_area.x += depth * tree_view->priv->expander_size;
2680                   cell_area.width -= depth * tree_view->priv->expander_size;
2681                 }
2682             }
2683           break;
2684         }
2685
2686       if (column == NULL)
2687         {
2688           gtk_tree_path_free (path);
2689           grab_focus_and_unset_draw_keyfocus (tree_view);
2690           return FALSE;
2691         }
2692
2693       tree_view->priv->focus_column = column;
2694
2695       /* decide if we edit */
2696       if (event->type == GDK_BUTTON_PRESS && event->button == 1 &&
2697           !(event->state & gtk_accelerator_get_default_mod_mask ()))
2698         {
2699           GtkTreePath *anchor;
2700           GtkTreeIter iter;
2701
2702           gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
2703           pspp_sheet_view_column_cell_set_cell_data (column,
2704                                                    tree_view->priv->model,
2705                                                    &iter,
2706                                                    PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT),
2707                                                    node->children?TRUE:FALSE);
2708
2709           if (tree_view->priv->anchor)
2710             anchor = gtk_tree_row_reference_get_path (tree_view->priv->anchor);
2711           else
2712             anchor = NULL;
2713
2714           if ((anchor && !gtk_tree_path_compare (anchor, path))
2715               || !_pspp_sheet_view_column_has_editable_cell (column))
2716             {
2717               GtkCellEditable *cell_editable = NULL;
2718
2719               /* FIXME: get the right flags */
2720               guint flags = 0;
2721
2722               path_string = gtk_tree_path_to_string (path);
2723
2724               if (_pspp_sheet_view_column_cell_event (column,
2725                                                     &cell_editable,
2726                                                     (GdkEvent *)event,
2727                                                     path_string,
2728                                                     &background_area,
2729                                                     &cell_area, flags))
2730                 {
2731                   if (cell_editable != NULL)
2732                     {
2733                       gint left, right;
2734                       GdkRectangle area;
2735
2736                       area = cell_area;
2737                       _pspp_sheet_view_column_get_neighbor_sizes (column,       _pspp_sheet_view_column_get_edited_cell (column), &left, &right);
2738
2739                       area.x += left;
2740                       area.width -= right + left;
2741
2742                       pspp_sheet_view_real_start_editing (tree_view,
2743                                                         column,
2744                                                         path,
2745                                                         cell_editable,
2746                                                         &area,
2747                                                         (GdkEvent *)event,
2748                                                         flags);
2749                       g_free (path_string);
2750                       gtk_tree_path_free (path);
2751                       gtk_tree_path_free (anchor);
2752                       return TRUE;
2753                     }
2754                   column_handled_click = TRUE;
2755                 }
2756               g_free (path_string);
2757             }
2758           if (anchor)
2759             gtk_tree_path_free (anchor);
2760         }
2761
2762       /* select */
2763       node_selected = PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED);
2764       pre_val = tree_view->priv->vadjustment->value;
2765
2766       /* we only handle selection modifications on the first button press
2767        */
2768       if (event->type == GDK_BUTTON_PRESS)
2769         {
2770           if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2771             tree_view->priv->ctrl_pressed = TRUE;
2772           if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2773             tree_view->priv->shift_pressed = TRUE;
2774
2775           focus_cell = _pspp_sheet_view_column_get_cell_at_pos (column, event->x - background_area.x);
2776           if (focus_cell)
2777             pspp_sheet_view_column_focus_cell (column, focus_cell);
2778
2779           if (event->state & GDK_CONTROL_MASK)
2780             {
2781               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE);
2782               pspp_sheet_view_real_toggle_cursor_row (tree_view);
2783             }
2784           else if (event->state & GDK_SHIFT_MASK)
2785             {
2786               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE);
2787               pspp_sheet_view_real_select_cursor_row (tree_view, FALSE);
2788             }
2789           else
2790             {
2791               pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE);
2792             }
2793
2794           tree_view->priv->ctrl_pressed = FALSE;
2795           tree_view->priv->shift_pressed = FALSE;
2796         }
2797
2798       /* the treeview may have been scrolled because of _set_cursor,
2799        * correct here
2800        */
2801
2802       aft_val = tree_view->priv->vadjustment->value;
2803       dval = pre_val - aft_val;
2804
2805       cell_area.y += dval;
2806       background_area.y += dval;
2807
2808       /* Save press to possibly begin a drag
2809        */
2810       if (!column_handled_click &&
2811           !tree_view->priv->in_grab &&
2812           tree_view->priv->pressed_button < 0)
2813         {
2814           tree_view->priv->pressed_button = event->button;
2815           tree_view->priv->press_start_x = event->x;
2816           tree_view->priv->press_start_y = event->y;
2817
2818           if (tree_view->priv->rubber_banding_enable
2819               && !node_selected
2820               && tree_view->priv->selection->type == GTK_SELECTION_MULTIPLE)
2821             {
2822               tree_view->priv->press_start_y += tree_view->priv->dy;
2823               tree_view->priv->rubber_band_x = event->x;
2824               tree_view->priv->rubber_band_y = event->y + tree_view->priv->dy;
2825               tree_view->priv->rubber_band_status = RUBBER_BAND_MAYBE_START;
2826
2827               if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2828                 tree_view->priv->rubber_band_ctrl = TRUE;
2829               if ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2830                 tree_view->priv->rubber_band_shift = TRUE;
2831             }
2832         }
2833
2834       /* Test if a double click happened on the same row. */
2835       if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
2836         {
2837           int double_click_time, double_click_distance;
2838
2839           g_object_get (gtk_settings_get_for_screen (
2840                           gtk_widget_get_screen (widget)),
2841                         "gtk-double-click-time", &double_click_time,
2842                         "gtk-double-click-distance", &double_click_distance,
2843                         NULL);
2844
2845           /* Same conditions as _gdk_event_button_generate */
2846           if (tree_view->priv->last_button_x != -1 &&
2847               (event->time < tree_view->priv->last_button_time + double_click_time) &&
2848               (ABS (event->x - tree_view->priv->last_button_x) <= double_click_distance) &&
2849               (ABS (event->y - tree_view->priv->last_button_y) <= double_click_distance))
2850             {
2851               /* We do no longer compare paths of this row and the
2852                * row clicked previously.  We use the double click
2853                * distance to decide whether this is a valid click,
2854                * allowing the mouse to slightly move over another row.
2855                */
2856               row_double_click = TRUE;
2857
2858               tree_view->priv->last_button_time = 0;
2859               tree_view->priv->last_button_x = -1;
2860               tree_view->priv->last_button_y = -1;
2861             }
2862           else
2863             {
2864               tree_view->priv->last_button_time = event->time;
2865               tree_view->priv->last_button_x = event->x;
2866               tree_view->priv->last_button_y = event->y;
2867             }
2868         }
2869
2870       if (row_double_click)
2871         {
2872           gtk_grab_remove (widget);
2873           pspp_sheet_view_row_activated (tree_view, path, column);
2874
2875           if (tree_view->priv->pressed_button == event->button)
2876             tree_view->priv->pressed_button = -1;
2877         }
2878
2879       gtk_tree_path_free (path);
2880
2881       /* If we activated the row through a double click we don't want to grab
2882        * focus back, as moving focus to another widget is pretty common.
2883        */
2884       if (!row_double_click)
2885         grab_focus_and_unset_draw_keyfocus (tree_view);
2886
2887       return TRUE;
2888     }
2889
2890   /* We didn't click in the window.  Let's check to see if we clicked on a column resize window.
2891    */
2892   for (i = 0, list = tree_view->priv->columns; list; list = list->next, i++)
2893     {
2894       column = list->data;
2895       if (event->window == column->window &&
2896           column->resizable &&
2897           column->window)
2898         {
2899           gpointer drag_data;
2900
2901           if (event->type == GDK_2BUTTON_PRESS &&
2902               pspp_sheet_view_column_get_sizing (column) != PSPP_SHEET_VIEW_COLUMN_AUTOSIZE)
2903             {
2904               column->use_resized_width = FALSE;
2905               _pspp_sheet_view_column_autosize (tree_view, column);
2906               return TRUE;
2907             }
2908
2909           if (gdk_pointer_grab (column->window, FALSE,
2910                                 GDK_POINTER_MOTION_HINT_MASK |
2911                                 GDK_BUTTON1_MOTION_MASK |
2912                                 GDK_BUTTON_RELEASE_MASK,
2913                                 NULL, NULL, event->time))
2914             return FALSE;
2915
2916           gtk_grab_add (widget);
2917           PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
2918           column->resized_width = column->width - tree_view->priv->last_extra_space_per_column;
2919
2920           /* block attached dnd signal handler */
2921           drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2922           if (drag_data)
2923             g_signal_handlers_block_matched (widget,
2924                                              G_SIGNAL_MATCH_DATA,
2925                                              0, 0, NULL, NULL,
2926                                              drag_data);
2927
2928           tree_view->priv->drag_pos = i;
2929           tree_view->priv->x_drag = column->button->allocation.x + (rtl ? 0 : column->button->allocation.width);
2930
2931           if (!gtk_widget_has_focus (widget))
2932             gtk_widget_grab_focus (widget);
2933
2934           return TRUE;
2935         }
2936     }
2937   return FALSE;
2938 }
2939
2940 /* GtkWidget::button_release_event helper */
2941 static gboolean
2942 pspp_sheet_view_button_release_drag_column (GtkWidget      *widget,
2943                                           GdkEventButton *event)
2944 {
2945   PsppSheetView *tree_view;
2946   GList *l;
2947   gboolean rtl;
2948
2949   tree_view = PSPP_SHEET_VIEW (widget);
2950
2951   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
2952   gdk_display_pointer_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2953   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), GDK_CURRENT_TIME);
2954
2955   /* Move the button back */
2956   g_object_ref (tree_view->priv->drag_column->button);
2957   gtk_container_remove (GTK_CONTAINER (tree_view), tree_view->priv->drag_column->button);
2958   gtk_widget_set_parent_window (tree_view->priv->drag_column->button, tree_view->priv->header_window);
2959   gtk_widget_set_parent (tree_view->priv->drag_column->button, GTK_WIDGET (tree_view));
2960   g_object_unref (tree_view->priv->drag_column->button);
2961   gtk_widget_queue_resize (widget);
2962   if (tree_view->priv->drag_column->resizable)
2963     {
2964       gdk_window_raise (tree_view->priv->drag_column->window);
2965       gdk_window_show (tree_view->priv->drag_column->window);
2966     }
2967   else
2968     gdk_window_hide (tree_view->priv->drag_column->window);
2969
2970   gtk_widget_grab_focus (tree_view->priv->drag_column->button);
2971
2972   if (rtl)
2973     {
2974       if (tree_view->priv->cur_reorder &&
2975           tree_view->priv->cur_reorder->right_column != tree_view->priv->drag_column)
2976         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2977                                          tree_view->priv->cur_reorder->right_column);
2978     }
2979   else
2980     {
2981       if (tree_view->priv->cur_reorder &&
2982           tree_view->priv->cur_reorder->left_column != tree_view->priv->drag_column)
2983         pspp_sheet_view_move_column_after (tree_view, tree_view->priv->drag_column,
2984                                          tree_view->priv->cur_reorder->left_column);
2985     }
2986   tree_view->priv->drag_column = NULL;
2987   gdk_window_hide (tree_view->priv->drag_window);
2988
2989   for (l = tree_view->priv->column_drag_info; l != NULL; l = l->next)
2990     g_slice_free (PsppSheetViewColumnReorder, l->data);
2991   g_list_free (tree_view->priv->column_drag_info);
2992   tree_view->priv->column_drag_info = NULL;
2993   tree_view->priv->cur_reorder = NULL;
2994
2995   if (tree_view->priv->drag_highlight_window)
2996     gdk_window_hide (tree_view->priv->drag_highlight_window);
2997
2998   /* Reset our flags */
2999   tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_UNSET;
3000   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
3001
3002   return TRUE;
3003 }
3004
3005 /* GtkWidget::button_release_event helper */
3006 static gboolean
3007 pspp_sheet_view_button_release_column_resize (GtkWidget      *widget,
3008                                             GdkEventButton *event)
3009 {
3010   PsppSheetView *tree_view;
3011   gpointer drag_data;
3012
3013   tree_view = PSPP_SHEET_VIEW (widget);
3014
3015   tree_view->priv->drag_pos = -1;
3016
3017   /* unblock attached dnd signal handler */
3018   drag_data = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
3019   if (drag_data)
3020     g_signal_handlers_unblock_matched (widget,
3021                                        G_SIGNAL_MATCH_DATA,
3022                                        0, 0, NULL, NULL,
3023                                        drag_data);
3024
3025   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE);
3026   gtk_grab_remove (widget);
3027   gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
3028                               event->time);
3029   return TRUE;
3030 }
3031
3032 static gboolean
3033 pspp_sheet_view_button_release (GtkWidget      *widget,
3034                               GdkEventButton *event)
3035 {
3036   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3037
3038   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
3039     return pspp_sheet_view_button_release_drag_column (widget, event);
3040
3041   if (tree_view->priv->rubber_band_status)
3042     pspp_sheet_view_stop_rubber_band (tree_view);
3043
3044   if (tree_view->priv->pressed_button == event->button)
3045     tree_view->priv->pressed_button = -1;
3046
3047   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
3048     return pspp_sheet_view_button_release_column_resize (widget, event);
3049
3050   if (tree_view->priv->button_pressed_node == NULL)
3051     return FALSE;
3052
3053   if (event->button == 1)
3054     {
3055       gtk_grab_remove (widget);
3056       if (tree_view->priv->button_pressed_node == tree_view->priv->prelight_node &&
3057           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT))
3058         {
3059           GtkTreePath *path = NULL;
3060
3061           path = _pspp_sheet_view_find_path (tree_view,
3062                                            tree_view->priv->button_pressed_tree,
3063                                            tree_view->priv->button_pressed_node);
3064           /* Actually activate the node */
3065           if (tree_view->priv->button_pressed_node->children == NULL)
3066             pspp_sheet_view_real_expand_row (tree_view, path,
3067                                            tree_view->priv->button_pressed_tree,
3068                                            tree_view->priv->button_pressed_node,
3069                                            FALSE, TRUE);
3070           else
3071             pspp_sheet_view_real_collapse_row (PSPP_SHEET_VIEW (widget), path,
3072                                              tree_view->priv->button_pressed_tree,
3073                                              tree_view->priv->button_pressed_node, TRUE);
3074           gtk_tree_path_free (path);
3075         }
3076
3077       tree_view->priv->button_pressed_tree = NULL;
3078       tree_view->priv->button_pressed_node = NULL;
3079     }
3080
3081   return TRUE;
3082 }
3083
3084 static gboolean
3085 pspp_sheet_view_grab_broken (GtkWidget          *widget,
3086                            GdkEventGrabBroken *event)
3087 {
3088   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3089
3090   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
3091     pspp_sheet_view_button_release_drag_column (widget, (GdkEventButton *)event);
3092
3093   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
3094     pspp_sheet_view_button_release_column_resize (widget, (GdkEventButton *)event);
3095
3096   return TRUE;
3097 }
3098
3099 #if 0
3100 static gboolean
3101 pspp_sheet_view_configure (GtkWidget *widget,
3102                          GdkEventConfigure *event)
3103 {
3104   PsppSheetView *tree_view;
3105
3106   tree_view = PSPP_SHEET_VIEW (widget);
3107   tree_view->priv->search_position_func (tree_view, tree_view->priv->search_window);
3108
3109   return FALSE;
3110 }
3111 #endif
3112
3113 /* GtkWidget::motion_event function set.
3114  */
3115
3116 static gboolean
3117 coords_are_over_arrow (PsppSheetView *tree_view,
3118                        GtkRBTree   *tree,
3119                        GtkRBNode   *node,
3120                        /* these are in bin window coords */
3121                        gint         x,
3122                        gint         y)
3123 {
3124   GdkRectangle arrow;
3125   gint x2;
3126
3127   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
3128     return FALSE;
3129
3130   if ((node->flags & PSPP_RBNODE_IS_PARENT) == 0)
3131     return FALSE;
3132
3133   arrow.y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node);
3134
3135   arrow.height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
3136
3137   pspp_sheet_view_get_arrow_xrange (tree_view, tree, &arrow.x, &x2);
3138
3139   arrow.width = x2 - arrow.x;
3140
3141   return (x >= arrow.x &&
3142           x < (arrow.x + arrow.width) &&
3143           y >= arrow.y &&
3144           y < (arrow.y + arrow.height));
3145 }
3146
3147 static gboolean
3148 auto_expand_timeout (gpointer data)
3149 {
3150   PsppSheetView *tree_view = PSPP_SHEET_VIEW (data);
3151   GtkTreePath *path;
3152
3153   if (tree_view->priv->prelight_node)
3154     {
3155       path = _pspp_sheet_view_find_path (tree_view,
3156                                        tree_view->priv->prelight_tree,
3157                                        tree_view->priv->prelight_node);   
3158
3159       if (tree_view->priv->prelight_node->children)
3160         pspp_sheet_view_collapse_row (tree_view, path);
3161       else
3162         pspp_sheet_view_expand_row (tree_view, path, FALSE);
3163
3164       gtk_tree_path_free (path);
3165     }
3166
3167   tree_view->priv->auto_expand_timeout = 0;
3168
3169   return FALSE;
3170 }
3171
3172 static void
3173 remove_auto_expand_timeout (PsppSheetView *tree_view)
3174 {
3175   if (tree_view->priv->auto_expand_timeout != 0)
3176     {
3177       g_source_remove (tree_view->priv->auto_expand_timeout);
3178       tree_view->priv->auto_expand_timeout = 0;
3179     }
3180 }
3181
3182 static void
3183 do_prelight (PsppSheetView *tree_view,
3184              GtkRBTree   *tree,
3185              GtkRBNode   *node,
3186              /* these are in bin_window coords */
3187              gint         x,
3188              gint         y)
3189 {
3190   if (tree_view->priv->prelight_tree == tree &&
3191       tree_view->priv->prelight_node == node)
3192     {
3193       /*  We are still on the same node,
3194           but we might need to take care of the arrow  */
3195
3196       if (tree && node && TREE_VIEW_DRAW_EXPANDERS (tree_view))
3197         {
3198           gboolean over_arrow;
3199           gboolean flag_set;
3200
3201           over_arrow = coords_are_over_arrow (tree_view, tree, node, x, y);
3202           flag_set = PSPP_SHEET_VIEW_FLAG_SET (tree_view,
3203                                              PSPP_SHEET_VIEW_ARROW_PRELIT);
3204
3205           if (over_arrow != flag_set)
3206             {
3207               if (over_arrow)
3208                 PSPP_SHEET_VIEW_SET_FLAG (tree_view,
3209                                         PSPP_SHEET_VIEW_ARROW_PRELIT);
3210               else
3211                 PSPP_SHEET_VIEW_UNSET_FLAG (tree_view,
3212                                           PSPP_SHEET_VIEW_ARROW_PRELIT);
3213
3214               pspp_sheet_view_draw_arrow (tree_view, tree, node, x, y);
3215             }
3216         }
3217
3218       return;
3219     }
3220
3221   if (tree_view->priv->prelight_tree && tree_view->priv->prelight_node)
3222     {
3223       /*  Unprelight the old node and arrow  */
3224
3225       PSPP_RBNODE_UNSET_FLAG (tree_view->priv->prelight_node,
3226                              PSPP_RBNODE_IS_PRELIT);
3227
3228       if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT)
3229           && TREE_VIEW_DRAW_EXPANDERS (tree_view))
3230         {
3231           PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT);
3232           
3233           pspp_sheet_view_draw_arrow (tree_view,
3234                                     tree_view->priv->prelight_tree,
3235                                     tree_view->priv->prelight_node,
3236                                     x,
3237                                     y);
3238         }
3239
3240       _pspp_sheet_view_queue_draw_node (tree_view,
3241                                       tree_view->priv->prelight_tree,
3242                                       tree_view->priv->prelight_node,
3243                                       NULL);
3244     }
3245
3246
3247   if (tree_view->priv->hover_expand)
3248     remove_auto_expand_timeout (tree_view);
3249
3250   /*  Set the new prelight values  */
3251   tree_view->priv->prelight_node = node;
3252   tree_view->priv->prelight_tree = tree;
3253
3254   if (!node || !tree)
3255     return;
3256
3257   /*  Prelight the new node and arrow  */
3258
3259   if (TREE_VIEW_DRAW_EXPANDERS (tree_view)
3260       && coords_are_over_arrow (tree_view, tree, node, x, y))
3261     {
3262       PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT);
3263
3264       pspp_sheet_view_draw_arrow (tree_view, tree, node, x, y);
3265     }
3266
3267   PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_PRELIT);
3268
3269   _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
3270
3271   if (tree_view->priv->hover_expand)
3272     {
3273       tree_view->priv->auto_expand_timeout = 
3274         gdk_threads_add_timeout (AUTO_EXPAND_TIMEOUT, auto_expand_timeout, tree_view);
3275     }
3276 }
3277
3278 static void
3279 prelight_or_select (PsppSheetView *tree_view,
3280                     GtkRBTree   *tree,
3281                     GtkRBNode   *node,
3282                     /* these are in bin_window coords */
3283                     gint         x,
3284                     gint         y)
3285 {
3286   GtkSelectionMode mode = pspp_sheet_selection_get_mode (tree_view->priv->selection);
3287   
3288   if (tree_view->priv->hover_selection &&
3289       (mode == GTK_SELECTION_SINGLE || mode == GTK_SELECTION_BROWSE) &&
3290       !(tree_view->priv->edited_column &&
3291         tree_view->priv->edited_column->editable_widget))
3292     {
3293       if (node)
3294         {
3295           if (!PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED))
3296             {
3297               GtkTreePath *path;
3298               
3299               path = _pspp_sheet_view_find_path (tree_view, tree, node);
3300               pspp_sheet_selection_select_path (tree_view->priv->selection, path);
3301               if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED))
3302                 {
3303                   PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
3304                   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, FALSE);
3305                 }
3306               gtk_tree_path_free (path);
3307             }
3308         }
3309
3310       else if (mode == GTK_SELECTION_SINGLE)
3311         pspp_sheet_selection_unselect_all (tree_view->priv->selection);
3312     }
3313
3314     do_prelight (tree_view, tree, node, x, y);
3315 }
3316
3317 static void
3318 ensure_unprelighted (PsppSheetView *tree_view)
3319 {
3320   do_prelight (tree_view,
3321                NULL, NULL,
3322                -1000, -1000); /* coords not possibly over an arrow */
3323
3324   g_assert (tree_view->priv->prelight_node == NULL);
3325 }
3326
3327 static void
3328 update_prelight (PsppSheetView *tree_view,
3329                  gint         x,
3330                  gint         y)
3331 {
3332   int new_y;
3333   GtkRBTree *tree;
3334   GtkRBNode *node;
3335
3336   if (tree_view->priv->tree == NULL)
3337     return;
3338
3339   if (x == -10000)
3340     {
3341       ensure_unprelighted (tree_view);
3342       return;
3343     }
3344
3345   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y);
3346   if (new_y < 0)
3347     new_y = 0;
3348
3349   _pspp_rbtree_find_offset (tree_view->priv->tree,
3350                            new_y, &tree, &node);
3351
3352   if (node)
3353     prelight_or_select (tree_view, tree, node, x, y);
3354 }
3355
3356
3357
3358
3359 /* Our motion arrow is either a box (in the case of the original spot)
3360  * or an arrow.  It is expander_size wide.
3361  */
3362 /*
3363  * 11111111111111
3364  * 01111111111110
3365  * 00111111111100
3366  * 00011111111000
3367  * 00001111110000
3368  * 00000111100000
3369  * 00000111100000
3370  * 00000111100000
3371  * ~ ~ ~ ~ ~ ~ ~
3372  * 00000111100000
3373  * 00000111100000
3374  * 00000111100000
3375  * 00001111110000
3376  * 00011111111000
3377  * 00111111111100
3378  * 01111111111110
3379  * 11111111111111
3380  */
3381
3382 static void
3383 pspp_sheet_view_motion_draw_column_motion_arrow (PsppSheetView *tree_view)
3384 {
3385   PsppSheetViewColumnReorder *reorder = tree_view->priv->cur_reorder;
3386   GtkWidget *widget = GTK_WIDGET (tree_view);
3387   GdkBitmap *mask = NULL;
3388   gint x;
3389   gint y;
3390   gint width;
3391   gint height;
3392   gint arrow_type = DRAG_COLUMN_WINDOW_STATE_UNSET;
3393   GdkWindowAttr attributes;
3394   guint attributes_mask;
3395
3396   if (!reorder ||
3397       reorder->left_column == tree_view->priv->drag_column ||
3398       reorder->right_column == tree_view->priv->drag_column)
3399     arrow_type = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
3400   else if (reorder->left_column || reorder->right_column)
3401     {
3402       GdkRectangle visible_rect;
3403       pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3404       if (reorder->left_column)
3405         x = reorder->left_column->button->allocation.x + reorder->left_column->button->allocation.width;
3406       else
3407         x = reorder->right_column->button->allocation.x;
3408
3409       if (x < visible_rect.x)
3410         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT;
3411       else if (x > visible_rect.x + visible_rect.width)
3412         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT;
3413       else
3414         arrow_type = DRAG_COLUMN_WINDOW_STATE_ARROW;
3415     }
3416
3417   /* We want to draw the rectangle over the initial location. */
3418   if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
3419     {
3420       GdkGC *gc;
3421       GdkColor col;
3422
3423       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ORIGINAL)
3424         {
3425           if (tree_view->priv->drag_highlight_window)
3426             {
3427               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3428                                         NULL);
3429               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3430             }
3431
3432           attributes.window_type = GDK_WINDOW_CHILD;
3433           attributes.wclass = GDK_INPUT_OUTPUT;
3434           attributes.x = tree_view->priv->drag_column_x;
3435           attributes.y = 0;
3436           width = attributes.width = tree_view->priv->drag_column->button->allocation.width;
3437           height = attributes.height = tree_view->priv->drag_column->button->allocation.height;
3438           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3439           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3440           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3441           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3442           tree_view->priv->drag_highlight_window = gdk_window_new (tree_view->priv->header_window, &attributes, attributes_mask);
3443           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3444
3445           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3446           gc = gdk_gc_new (mask);
3447           col.pixel = 1;
3448           gdk_gc_set_foreground (gc, &col);
3449           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3450           col.pixel = 0;
3451           gdk_gc_set_foreground(gc, &col);
3452           gdk_draw_rectangle (mask, gc, TRUE, 2, 2, width - 4, height - 4);
3453           g_object_unref (gc);
3454
3455           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3456                                          mask, 0, 0);
3457           if (mask) g_object_unref (mask);
3458           tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ORIGINAL;
3459         }
3460     }
3461   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW)
3462     {
3463       gint i, j = 1;
3464       GdkGC *gc;
3465       GdkColor col;
3466
3467       width = tree_view->priv->expander_size;
3468
3469       /* Get x, y, width, height of arrow */
3470       gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
3471       if (reorder->left_column)
3472         {
3473           x += reorder->left_column->button->allocation.x + reorder->left_column->button->allocation.width - width/2;
3474           height = reorder->left_column->button->allocation.height;
3475         }
3476       else
3477         {
3478           x += reorder->right_column->button->allocation.x - width/2;
3479           height = reorder->right_column->button->allocation.height;
3480         }
3481       y -= tree_view->priv->expander_size/2; /* The arrow takes up only half the space */
3482       height += tree_view->priv->expander_size;
3483
3484       /* Create the new window */
3485       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW)
3486         {
3487           if (tree_view->priv->drag_highlight_window)
3488             {
3489               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3490                                         NULL);
3491               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3492             }
3493
3494           attributes.window_type = GDK_WINDOW_TEMP;
3495           attributes.wclass = GDK_INPUT_OUTPUT;
3496           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3497           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3498           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3499           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3500           attributes.x = x;
3501           attributes.y = y;
3502           attributes.width = width;
3503           attributes.height = height;
3504           tree_view->priv->drag_highlight_window = gdk_window_new (gtk_widget_get_root_window (widget),
3505                                                                    &attributes, attributes_mask);
3506           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3507
3508           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3509           gc = gdk_gc_new (mask);
3510           col.pixel = 1;
3511           gdk_gc_set_foreground (gc, &col);
3512           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3513
3514           /* Draw the 2 arrows as per above */
3515           col.pixel = 0;
3516           gdk_gc_set_foreground (gc, &col);
3517           for (i = 0; i < width; i ++)
3518             {
3519               if (i == (width/2 - 1))
3520                 continue;
3521               gdk_draw_line (mask, gc, i, j, i, height - j);
3522               if (i < (width/2 - 1))
3523                 j++;
3524               else
3525                 j--;
3526             }
3527           g_object_unref (gc);
3528           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3529                                          mask, 0, 0);
3530           if (mask) g_object_unref (mask);
3531         }
3532
3533       tree_view->priv->drag_column_window_state = DRAG_COLUMN_WINDOW_STATE_ARROW;
3534       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3535     }
3536   else if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT ||
3537            arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3538     {
3539       gint i, j = 1;
3540       GdkGC *gc;
3541       GdkColor col;
3542
3543       width = tree_view->priv->expander_size;
3544
3545       /* Get x, y, width, height of arrow */
3546       width = width/2; /* remember, the arrow only takes half the available width */
3547       gdk_window_get_origin (widget->window, &x, &y);
3548       if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3549         x += widget->allocation.width - width;
3550
3551       if (reorder->left_column)
3552         height = reorder->left_column->button->allocation.height;
3553       else
3554         height = reorder->right_column->button->allocation.height;
3555
3556       y -= tree_view->priv->expander_size;
3557       height += 2*tree_view->priv->expander_size;
3558
3559       /* Create the new window */
3560       if (tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT &&
3561           tree_view->priv->drag_column_window_state != DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT)
3562         {
3563           if (tree_view->priv->drag_highlight_window)
3564             {
3565               gdk_window_set_user_data (tree_view->priv->drag_highlight_window,
3566                                         NULL);
3567               gdk_window_destroy (tree_view->priv->drag_highlight_window);
3568             }
3569
3570           attributes.window_type = GDK_WINDOW_TEMP;
3571           attributes.wclass = GDK_INPUT_OUTPUT;
3572           attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
3573           attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
3574           attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
3575           attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3576           attributes.x = x;
3577           attributes.y = y;
3578           attributes.width = width;
3579           attributes.height = height;
3580           tree_view->priv->drag_highlight_window = gdk_window_new (NULL, &attributes, attributes_mask);
3581           gdk_window_set_user_data (tree_view->priv->drag_highlight_window, GTK_WIDGET (tree_view));
3582
3583           mask = gdk_pixmap_new (tree_view->priv->drag_highlight_window, width, height, 1);
3584           gc = gdk_gc_new (mask);
3585           col.pixel = 1;
3586           gdk_gc_set_foreground (gc, &col);
3587           gdk_draw_rectangle (mask, gc, TRUE, 0, 0, width, height);
3588
3589           /* Draw the 2 arrows as per above */
3590           col.pixel = 0;
3591           gdk_gc_set_foreground (gc, &col);
3592           j = tree_view->priv->expander_size;
3593           for (i = 0; i < width; i ++)
3594             {
3595               gint k;
3596               if (arrow_type == DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT)
3597                 k = width - i - 1;
3598               else
3599                 k = i;
3600               gdk_draw_line (mask, gc, k, j, k, height - j);
3601               gdk_draw_line (mask, gc, k, 0, k, tree_view->priv->expander_size - j);
3602               gdk_draw_line (mask, gc, k, height, k, height - tree_view->priv->expander_size + j);
3603               j--;
3604             }
3605           g_object_unref (gc);
3606           gdk_window_shape_combine_mask (tree_view->priv->drag_highlight_window,
3607                                          mask, 0, 0);
3608           if (mask) g_object_unref (mask);
3609         }
3610
3611       tree_view->priv->drag_column_window_state = arrow_type;
3612       gdk_window_move (tree_view->priv->drag_highlight_window, x, y);
3613    }
3614   else
3615     {
3616       g_warning (G_STRLOC"Invalid PsppSheetViewColumnReorder struct");
3617       gdk_window_hide (tree_view->priv->drag_highlight_window);
3618       return;
3619     }
3620
3621   gdk_window_show (tree_view->priv->drag_highlight_window);
3622   gdk_window_raise (tree_view->priv->drag_highlight_window);
3623 }
3624
3625 static gboolean
3626 pspp_sheet_view_motion_resize_column (GtkWidget      *widget,
3627                                     GdkEventMotion *event)
3628 {
3629   gint x;
3630   gint new_width;
3631   PsppSheetViewColumn *column;
3632   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
3633
3634   column = pspp_sheet_view_get_column (tree_view, tree_view->priv->drag_pos);
3635
3636   if (event->is_hint || event->window != widget->window)
3637     gtk_widget_get_pointer (widget, &x, NULL);
3638   else
3639     x = event->x;
3640
3641   if (tree_view->priv->hadjustment)
3642     x += tree_view->priv->hadjustment->value;
3643
3644   new_width = pspp_sheet_view_new_column_width (tree_view,
3645                                               tree_view->priv->drag_pos, &x);
3646   if (x != tree_view->priv->x_drag &&
3647       (new_width != column->fixed_width))
3648     {
3649       column->use_resized_width = TRUE;
3650       column->resized_width = new_width;
3651       if (column->expand)
3652         column->resized_width -= tree_view->priv->last_extra_space_per_column;
3653       gtk_widget_queue_resize (widget);
3654     }
3655
3656   return FALSE;
3657 }
3658
3659
3660 static void
3661 pspp_sheet_view_update_current_reorder (PsppSheetView *tree_view)
3662 {
3663   PsppSheetViewColumnReorder *reorder = NULL;
3664   GList *list;
3665   gint mouse_x;
3666
3667   gdk_window_get_pointer (tree_view->priv->header_window, &mouse_x, NULL, NULL);
3668   for (list = tree_view->priv->column_drag_info; list; list = list->next)
3669     {
3670       reorder = (PsppSheetViewColumnReorder *) list->data;
3671       if (mouse_x >= reorder->left_align && mouse_x < reorder->right_align)
3672         break;
3673       reorder = NULL;
3674     }
3675
3676   /*  if (reorder && reorder == tree_view->priv->cur_reorder)
3677       return;*/
3678
3679   tree_view->priv->cur_reorder = reorder;
3680   pspp_sheet_view_motion_draw_column_motion_arrow (tree_view);
3681 }
3682
3683 static void
3684 pspp_sheet_view_vertical_autoscroll (PsppSheetView *tree_view)
3685 {
3686   GdkRectangle visible_rect;
3687   gint y;
3688   gint offset;
3689   gfloat value;
3690
3691   gdk_window_get_pointer (tree_view->priv->bin_window, NULL, &y, NULL);
3692   y += tree_view->priv->dy;
3693
3694   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3695
3696   /* see if we are near the edge. */
3697   offset = y - (visible_rect.y + 2 * SCROLL_EDGE_SIZE);
3698   if (offset > 0)
3699     {
3700       offset = y - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE);
3701       if (offset < 0)
3702         return;
3703     }
3704
3705   value = CLAMP (tree_view->priv->vadjustment->value + offset, 0.0,
3706                  tree_view->priv->vadjustment->upper - tree_view->priv->vadjustment->page_size);
3707   gtk_adjustment_set_value (tree_view->priv->vadjustment, value);
3708 }
3709
3710 static gboolean
3711 pspp_sheet_view_horizontal_autoscroll (PsppSheetView *tree_view)
3712 {
3713   GdkRectangle visible_rect;
3714   gint x;
3715   gint offset;
3716   gfloat value;
3717
3718   gdk_window_get_pointer (tree_view->priv->bin_window, &x, NULL, NULL);
3719
3720   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
3721
3722   /* See if we are near the edge. */
3723   offset = x - (visible_rect.x + SCROLL_EDGE_SIZE);
3724   if (offset > 0)
3725     {
3726       offset = x - (visible_rect.x + visible_rect.width - SCROLL_EDGE_SIZE);
3727       if (offset < 0)
3728         return TRUE;
3729     }
3730   offset = offset/3;
3731
3732   value = CLAMP (tree_view->priv->hadjustment->value + offset,
3733                  0.0, tree_view->priv->hadjustment->upper - tree_view->priv->hadjustment->page_size);
3734   gtk_adjustment_set_value (tree_view->priv->hadjustment, value);
3735
3736   return TRUE;
3737
3738 }
3739
3740 static gboolean
3741 pspp_sheet_view_motion_drag_column (GtkWidget      *widget,
3742                                   GdkEventMotion *event)
3743 {
3744   PsppSheetView *tree_view = (PsppSheetView *) widget;
3745   PsppSheetViewColumn *column = tree_view->priv->drag_column;
3746   gint x, y;
3747
3748   /* Sanity Check */
3749   if ((column == NULL) ||
3750       (event->window != tree_view->priv->drag_window))
3751     return FALSE;
3752
3753   /* Handle moving the header */
3754   gdk_window_get_position (tree_view->priv->drag_window, &x, &y);
3755   x = CLAMP (x + (gint)event->x - column->drag_x, 0,
3756              MAX (tree_view->priv->width, GTK_WIDGET (tree_view)->allocation.width) - column->button->allocation.width);
3757   gdk_window_move (tree_view->priv->drag_window, x, y);
3758   
3759   /* autoscroll, if needed */
3760   pspp_sheet_view_horizontal_autoscroll (tree_view);
3761   /* Update the current reorder position and arrow; */
3762   pspp_sheet_view_update_current_reorder (tree_view);
3763
3764   return TRUE;
3765 }
3766
3767 static void
3768 pspp_sheet_view_stop_rubber_band (PsppSheetView *tree_view)
3769 {
3770   remove_scroll_timeout (tree_view);
3771   gtk_grab_remove (GTK_WIDGET (tree_view));
3772
3773   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
3774     {
3775       GtkTreePath *tmp_path;
3776
3777       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
3778
3779       /* The anchor path should be set to the start path */
3780       tmp_path = _pspp_sheet_view_find_path (tree_view,
3781                                            tree_view->priv->rubber_band_start_tree,
3782                                            tree_view->priv->rubber_band_start_node);
3783
3784       if (tree_view->priv->anchor)
3785         gtk_tree_row_reference_free (tree_view->priv->anchor);
3786
3787       tree_view->priv->anchor =
3788         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
3789                                           tree_view->priv->model,
3790                                           tmp_path);
3791
3792       gtk_tree_path_free (tmp_path);
3793
3794       /* ... and the cursor to the end path */
3795       tmp_path = _pspp_sheet_view_find_path (tree_view,
3796                                            tree_view->priv->rubber_band_end_tree,
3797                                            tree_view->priv->rubber_band_end_node);
3798       pspp_sheet_view_real_set_cursor (PSPP_SHEET_VIEW (tree_view), tmp_path, FALSE, FALSE);
3799       gtk_tree_path_free (tmp_path);
3800
3801       _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
3802     }
3803
3804   /* Clear status variables */
3805   tree_view->priv->rubber_band_status = RUBBER_BAND_OFF;
3806   tree_view->priv->rubber_band_shift = 0;
3807   tree_view->priv->rubber_band_ctrl = 0;
3808
3809   tree_view->priv->rubber_band_start_node = NULL;
3810   tree_view->priv->rubber_band_start_tree = NULL;
3811   tree_view->priv->rubber_band_end_node = NULL;
3812   tree_view->priv->rubber_band_end_tree = NULL;
3813 }
3814
3815 static void
3816 pspp_sheet_view_update_rubber_band_selection_range (PsppSheetView *tree_view,
3817                                                  GtkRBTree   *start_tree,
3818                                                  GtkRBNode   *start_node,
3819                                                  GtkRBTree   *end_tree,
3820                                                  GtkRBNode   *end_node,
3821                                                  gboolean     select,
3822                                                  gboolean     skip_start,
3823                                                  gboolean     skip_end)
3824 {
3825   if (start_node == end_node)
3826     return;
3827
3828   /* We skip the first node and jump inside the loop */
3829   if (skip_start)
3830     goto skip_first;
3831
3832   do
3833     {
3834       /* Small optimization by assuming insensitive nodes are never
3835        * selected.
3836        */
3837       if (!PSPP_RBNODE_FLAG_SET (start_node, PSPP_RBNODE_IS_SELECTED))
3838         {
3839           GtkTreePath *path;
3840           gboolean selectable;
3841
3842           path = _pspp_sheet_view_find_path (tree_view, start_tree, start_node);
3843           selectable = _pspp_sheet_selection_row_is_selectable (tree_view->priv->selection, start_node, path);
3844           gtk_tree_path_free (path);
3845
3846           if (!selectable)
3847             goto node_not_selectable;
3848         }
3849
3850       if (select)
3851         {
3852           if (tree_view->priv->rubber_band_shift)
3853             PSPP_RBNODE_SET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3854           else if (tree_view->priv->rubber_band_ctrl)
3855             {
3856               /* Toggle the selection state */
3857               if (PSPP_RBNODE_FLAG_SET (start_node, PSPP_RBNODE_IS_SELECTED))
3858                 PSPP_RBNODE_UNSET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3859               else
3860                 PSPP_RBNODE_SET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3861             }
3862           else
3863             PSPP_RBNODE_SET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3864         }
3865       else
3866         {
3867           /* Mirror the above */
3868           if (tree_view->priv->rubber_band_shift)
3869             PSPP_RBNODE_UNSET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3870           else if (tree_view->priv->rubber_band_ctrl)
3871             {
3872               /* Toggle the selection state */
3873               if (PSPP_RBNODE_FLAG_SET (start_node, PSPP_RBNODE_IS_SELECTED))
3874                 PSPP_RBNODE_UNSET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3875               else
3876                 PSPP_RBNODE_SET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3877             }
3878           else
3879             PSPP_RBNODE_UNSET_FLAG (start_node, PSPP_RBNODE_IS_SELECTED);
3880         }
3881
3882       _pspp_sheet_view_queue_draw_node (tree_view, start_tree, start_node, NULL);
3883
3884 node_not_selectable:
3885       if (start_node == end_node)
3886         break;
3887
3888 skip_first:
3889
3890       if (start_node->children)
3891         {
3892           start_tree = start_node->children;
3893           start_node = start_tree->root;
3894           while (start_node->left != start_tree->nil)
3895             start_node = start_node->left;
3896         }
3897       else
3898         {
3899           _pspp_rbtree_next_full (start_tree, start_node, &start_tree, &start_node);
3900
3901           if (!start_tree)
3902             /* Ran out of tree */
3903             break;
3904         }
3905
3906       if (skip_end && start_node == end_node)
3907         break;
3908     }
3909   while (TRUE);
3910 }
3911
3912 static void
3913 pspp_sheet_view_update_rubber_band_selection (PsppSheetView *tree_view)
3914 {
3915   GtkRBTree *start_tree, *end_tree;
3916   GtkRBNode *start_node, *end_node;
3917
3918   _pspp_rbtree_find_offset (tree_view->priv->tree, MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &start_tree, &start_node);
3919   _pspp_rbtree_find_offset (tree_view->priv->tree, MAX (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y), &end_tree, &end_node);
3920
3921   /* Handle the start area first */
3922   if (!tree_view->priv->rubber_band_start_node)
3923     {
3924       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3925                                                        start_tree,
3926                                                        start_node,
3927                                                        end_tree,
3928                                                        end_node,
3929                                                        TRUE,
3930                                                        FALSE,
3931                                                        FALSE);
3932     }
3933   else if (_pspp_rbtree_node_find_offset (start_tree, start_node) <
3934            _pspp_rbtree_node_find_offset (tree_view->priv->rubber_band_start_tree, tree_view->priv->rubber_band_start_node))
3935     {
3936       /* New node is above the old one; selection became bigger */
3937       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3938                                                        start_tree,
3939                                                        start_node,
3940                                                        tree_view->priv->rubber_band_start_tree,
3941                                                        tree_view->priv->rubber_band_start_node,
3942                                                        TRUE,
3943                                                        FALSE,
3944                                                        TRUE);
3945     }
3946   else if (_pspp_rbtree_node_find_offset (start_tree, start_node) >
3947            _pspp_rbtree_node_find_offset (tree_view->priv->rubber_band_start_tree, tree_view->priv->rubber_band_start_node))
3948     {
3949       /* New node is below the old one; selection became smaller */
3950       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3951                                                        tree_view->priv->rubber_band_start_tree,
3952                                                        tree_view->priv->rubber_band_start_node,
3953                                                        start_tree,
3954                                                        start_node,
3955                                                        FALSE,
3956                                                        FALSE,
3957                                                        TRUE);
3958     }
3959
3960   tree_view->priv->rubber_band_start_tree = start_tree;
3961   tree_view->priv->rubber_band_start_node = start_node;
3962
3963   /* Next, handle the end area */
3964   if (!tree_view->priv->rubber_band_end_node)
3965     {
3966       /* In the event this happens, start_node was also NULL; this case is
3967        * handled above.
3968        */
3969     }
3970   else if (!end_node)
3971     {
3972       /* Find the last node in the tree */
3973       _pspp_rbtree_find_offset (tree_view->priv->tree, tree_view->priv->height - 1,
3974                                &end_tree, &end_node);
3975
3976       /* Selection reached end of the tree */
3977       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3978                                                        tree_view->priv->rubber_band_end_tree,
3979                                                        tree_view->priv->rubber_band_end_node,
3980                                                        end_tree,
3981                                                        end_node,
3982                                                        TRUE,
3983                                                        TRUE,
3984                                                        FALSE);
3985     }
3986   else if (_pspp_rbtree_node_find_offset (end_tree, end_node) >
3987            _pspp_rbtree_node_find_offset (tree_view->priv->rubber_band_end_tree, tree_view->priv->rubber_band_end_node))
3988     {
3989       /* New node is below the old one; selection became bigger */
3990       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
3991                                                        tree_view->priv->rubber_band_end_tree,
3992                                                        tree_view->priv->rubber_band_end_node,
3993                                                        end_tree,
3994                                                        end_node,
3995                                                        TRUE,
3996                                                        TRUE,
3997                                                        FALSE);
3998     }
3999   else if (_pspp_rbtree_node_find_offset (end_tree, end_node) <
4000            _pspp_rbtree_node_find_offset (tree_view->priv->rubber_band_end_tree, tree_view->priv->rubber_band_end_node))
4001     {
4002       /* New node is above the old one; selection became smaller */
4003       pspp_sheet_view_update_rubber_band_selection_range (tree_view,
4004                                                        end_tree,
4005                                                        end_node,
4006                                                        tree_view->priv->rubber_band_end_tree,
4007                                                        tree_view->priv->rubber_band_end_node,
4008                                                        FALSE,
4009                                                        TRUE,
4010                                                        FALSE);
4011     }
4012
4013   tree_view->priv->rubber_band_end_tree = end_tree;
4014   tree_view->priv->rubber_band_end_node = end_node;
4015 }
4016
4017 static void
4018 pspp_sheet_view_update_rubber_band (PsppSheetView *tree_view)
4019 {
4020   gint x, y;
4021   GdkRectangle old_area;
4022   GdkRectangle new_area;
4023   GdkRectangle common;
4024   GdkRegion *invalid_region;
4025
4026   old_area.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
4027   old_area.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
4028   old_area.width = ABS (tree_view->priv->rubber_band_x - tree_view->priv->press_start_x) + 1;
4029   old_area.height = ABS (tree_view->priv->rubber_band_y - tree_view->priv->press_start_y) + 1;
4030
4031   gdk_window_get_pointer (tree_view->priv->bin_window, &x, &y, NULL);
4032
4033   x = MAX (x, 0);
4034   y = MAX (y, 0) + tree_view->priv->dy;
4035
4036   new_area.x = MIN (tree_view->priv->press_start_x, x);
4037   new_area.y = MIN (tree_view->priv->press_start_y, y) - tree_view->priv->dy;
4038   new_area.width = ABS (x - tree_view->priv->press_start_x) + 1;
4039   new_area.height = ABS (y - tree_view->priv->press_start_y) + 1;
4040
4041   invalid_region = gdk_region_rectangle (&old_area);
4042   gdk_region_union_with_rect (invalid_region, &new_area);
4043
4044   gdk_rectangle_intersect (&old_area, &new_area, &common);
4045   if (common.width > 2 && common.height > 2)
4046     {
4047       GdkRegion *common_region;
4048
4049       /* make sure the border is invalidated */
4050       common.x += 1;
4051       common.y += 1;
4052       common.width -= 2;
4053       common.height -= 2;
4054
4055       common_region = gdk_region_rectangle (&common);
4056
4057       gdk_region_subtract (invalid_region, common_region);
4058       gdk_region_destroy (common_region);
4059     }
4060
4061   gdk_window_invalidate_region (tree_view->priv->bin_window, invalid_region, TRUE);
4062
4063   gdk_region_destroy (invalid_region);
4064
4065   tree_view->priv->rubber_band_x = x;
4066   tree_view->priv->rubber_band_y = y;
4067
4068   pspp_sheet_view_update_rubber_band_selection (tree_view);
4069 }
4070
4071 static void
4072 pspp_sheet_view_paint_rubber_band (PsppSheetView  *tree_view,
4073                                 GdkRectangle *area)
4074 {
4075   cairo_t *cr;
4076   GdkRectangle rect;
4077   GdkRectangle rubber_rect;
4078
4079   rubber_rect.x = MIN (tree_view->priv->press_start_x, tree_view->priv->rubber_band_x);
4080   rubber_rect.y = MIN (tree_view->priv->press_start_y, tree_view->priv->rubber_band_y) - tree_view->priv->dy;
4081   rubber_rect.width = ABS (tree_view->priv->press_start_x - tree_view->priv->rubber_band_x) + 1;
4082   rubber_rect.height = ABS (tree_view->priv->press_start_y - tree_view->priv->rubber_band_y) + 1;
4083
4084   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
4085     return;
4086
4087   cr = gdk_cairo_create (tree_view->priv->bin_window);
4088   cairo_set_line_width (cr, 1.0);
4089
4090   cairo_set_source_rgba (cr,
4091                          GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].red / 65535.,
4092                          GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].green / 65535.,
4093                          GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].blue / 65535.,
4094                          .25);
4095
4096   gdk_cairo_rectangle (cr, &rect);
4097   cairo_clip (cr);
4098   cairo_paint (cr);
4099
4100   cairo_set_source_rgb (cr,
4101                         GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].red / 65535.,
4102                         GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].green / 65535.,
4103                         GTK_WIDGET (tree_view)->style->fg[GTK_STATE_NORMAL].blue / 65535.);
4104
4105   cairo_rectangle (cr,
4106                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
4107                    rubber_rect.width - 1, rubber_rect.height - 1);
4108   cairo_stroke (cr);
4109
4110   cairo_destroy (cr);
4111 }
4112
4113 static gboolean
4114 pspp_sheet_view_motion_bin_window (GtkWidget      *widget,
4115                                  GdkEventMotion *event)
4116 {
4117   PsppSheetView *tree_view;
4118   GtkRBTree *tree;
4119   GtkRBNode *node;
4120   gint new_y;
4121
4122   tree_view = (PsppSheetView *) widget;
4123
4124   if (tree_view->priv->tree == NULL)
4125     return FALSE;
4126
4127   if (tree_view->priv->rubber_band_status == RUBBER_BAND_MAYBE_START)
4128     {
4129       gtk_grab_add (GTK_WIDGET (tree_view));
4130       pspp_sheet_view_update_rubber_band (tree_view);
4131
4132       tree_view->priv->rubber_band_status = RUBBER_BAND_ACTIVE;
4133     }
4134   else if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
4135     {
4136       pspp_sheet_view_update_rubber_band (tree_view);
4137
4138       add_scroll_timeout (tree_view);
4139     }
4140
4141   /* only check for an initiated drag when a button is pressed */
4142   if (tree_view->priv->pressed_button >= 0
4143       && !tree_view->priv->rubber_band_status)
4144     pspp_sheet_view_maybe_begin_dragging_row (tree_view, event);
4145
4146   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
4147   if (new_y < 0)
4148     new_y = 0;
4149
4150   _pspp_rbtree_find_offset (tree_view->priv->tree, new_y, &tree, &node);
4151
4152   /* If we are currently pressing down a button, we don't want to prelight anything else. */
4153   if ((tree_view->priv->button_pressed_node != NULL) &&
4154       (tree_view->priv->button_pressed_node != node))
4155     node = NULL;
4156
4157   tree_view->priv->event_last_x = event->x;
4158   tree_view->priv->event_last_y = event->y;
4159
4160   prelight_or_select (tree_view, tree, node, event->x, event->y);
4161
4162   return TRUE;
4163 }
4164
4165 static gboolean
4166 pspp_sheet_view_motion (GtkWidget      *widget,
4167                       GdkEventMotion *event)
4168 {
4169   PsppSheetView *tree_view;
4170
4171   tree_view = (PsppSheetView *) widget;
4172
4173   /* Resizing a column */
4174   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_RESIZE))
4175     return pspp_sheet_view_motion_resize_column (widget, event);
4176
4177   /* Drag column */
4178   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
4179     return pspp_sheet_view_motion_drag_column (widget, event);
4180
4181   /* Sanity check it */
4182   if (event->window == tree_view->priv->bin_window)
4183     return pspp_sheet_view_motion_bin_window (widget, event);
4184
4185   return FALSE;
4186 }
4187
4188 /* Invalidate the focus rectangle near the edge of the bin_window; used when
4189  * the tree is empty.
4190  */
4191 static void
4192 invalidate_empty_focus (PsppSheetView *tree_view)
4193 {
4194   GdkRectangle area;
4195
4196   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
4197     return;
4198
4199   area.x = 0;
4200   area.y = 0;
4201   gdk_drawable_get_size (tree_view->priv->bin_window, &area.width, &area.height);
4202   gdk_window_invalidate_rect (tree_view->priv->bin_window, &area, FALSE);
4203 }
4204
4205 /* Draws a focus rectangle near the edge of the bin_window; used when the tree
4206  * is empty.
4207  */
4208 static void
4209 draw_empty_focus (PsppSheetView *tree_view, GdkRectangle *clip_area)
4210 {
4211   GtkWidget *widget = GTK_WIDGET (tree_view);
4212   gint w, h;
4213
4214   if (!gtk_widget_has_focus (widget))
4215     return;
4216
4217   gdk_drawable_get_size (tree_view->priv->bin_window, &w, &h);
4218
4219   w -= 2;
4220   h -= 2;
4221
4222   if (w > 0 && h > 0)
4223     gtk_paint_focus (gtk_widget_get_style (widget),
4224                      tree_view->priv->bin_window,
4225                      gtk_widget_get_state (widget),
4226                      clip_area,
4227                      widget,
4228                      NULL,
4229                      1, 1, w, h);
4230 }
4231
4232 static void
4233 pspp_sheet_view_draw_grid_lines (PsppSheetView    *tree_view,
4234                                GdkEventExpose *event,
4235                                gint            n_visible_columns)
4236 {
4237   GList *list = tree_view->priv->columns;
4238   gint i = 0;
4239   gint current_x = 0;
4240
4241   if (tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
4242       && tree_view->priv->grid_lines != PSPP_SHEET_VIEW_GRID_LINES_BOTH)
4243     return;
4244
4245   /* Only draw the lines for visible rows and columns */
4246   for (list = tree_view->priv->columns; list; list = list->next, i++)
4247     {
4248       PsppSheetViewColumn *column = list->data;
4249
4250       /* We don't want a line for the last column */
4251       if (i == n_visible_columns - 1)
4252         break;
4253
4254       if (! column->visible)
4255         continue;
4256
4257       current_x += column->width;
4258
4259       gdk_draw_line (event->window,
4260                      tree_view->priv->grid_line_gc,
4261                      current_x - 1, 0,
4262                      current_x - 1, tree_view->priv->height);
4263     }
4264 }
4265
4266 /* Warning: Very scary function.
4267  * Modify at your own risk
4268  *
4269  * KEEP IN SYNC WITH pspp_sheet_view_create_row_drag_icon()!
4270  * FIXME: It's not...
4271  */
4272 static gboolean
4273 pspp_sheet_view_bin_expose (GtkWidget      *widget,
4274                           GdkEventExpose *event)
4275 {
4276   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
4277   GtkTreePath *path;
4278   GtkRBTree *tree;
4279   GList *list;
4280   GtkRBNode *node;
4281   GtkRBNode *cursor = NULL;
4282   GtkRBTree *cursor_tree = NULL;
4283   GtkRBNode *drag_highlight = NULL;
4284   GtkRBTree *drag_highlight_tree = NULL;
4285   GtkTreeIter iter;
4286   gint new_y;
4287   gint y_offset, cell_offset;
4288   gint max_height;
4289   gint depth;
4290   GdkRectangle background_area;
4291   GdkRectangle cell_area;
4292   guint flags;
4293   gint highlight_x;
4294   gint expander_cell_width;
4295   gint bin_window_width;
4296   gint bin_window_height;
4297   GtkTreePath *cursor_path;
4298   GtkTreePath *drag_dest_path;
4299   GList *first_column, *last_column;
4300   gint vertical_separator;
4301   gint horizontal_separator;
4302   gint focus_line_width;
4303   gboolean allow_rules;
4304   gboolean has_special_cell;
4305   gboolean rtl;
4306   gint n_visible_columns;
4307   gint pointer_x, pointer_y;
4308   gint grid_line_width;
4309   gboolean got_pointer = FALSE;
4310   gboolean row_ending_details;
4311   gboolean draw_vgrid_lines, draw_hgrid_lines;
4312
4313   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4314
4315   gtk_widget_style_get (widget,
4316                         "horizontal-separator", &horizontal_separator,
4317                         "vertical-separator", &vertical_separator,
4318                         "allow-rules", &allow_rules,
4319                         "focus-line-width", &focus_line_width,
4320                         "row-ending-details", &row_ending_details,
4321                         NULL);
4322
4323   if (tree_view->priv->tree == NULL)
4324     {
4325       draw_empty_focus (tree_view, &event->area);
4326       return TRUE;
4327     }
4328
4329   /* clip event->area to the visible area */
4330   if (event->area.height < 0)
4331     return TRUE;
4332
4333   validate_visible_area (tree_view);
4334
4335   new_y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, event->area.y);
4336
4337   if (new_y < 0)
4338     new_y = 0;
4339   y_offset = -_pspp_rbtree_find_offset (tree_view->priv->tree, new_y, &tree, &node);
4340   gdk_drawable_get_size (tree_view->priv->bin_window,
4341                          &bin_window_width, &bin_window_height);
4342
4343   if (tree_view->priv->height < bin_window_height)
4344     {
4345       gtk_paint_flat_box (widget->style,
4346                           event->window,
4347                           widget->state,
4348                           GTK_SHADOW_NONE,
4349                           &event->area,
4350                           widget,
4351                           "cell_even",
4352                           0, tree_view->priv->height,
4353                           bin_window_width,
4354                           bin_window_height - tree_view->priv->height);
4355     }
4356
4357   if (node == NULL)
4358     return TRUE;
4359
4360   /* find the path for the node */
4361   path = _pspp_sheet_view_find_path ((PsppSheetView *)widget,
4362                                    tree,
4363                                    node);
4364   gtk_tree_model_get_iter (tree_view->priv->model,
4365                            &iter,
4366                            path);
4367   depth = gtk_tree_path_get_depth (path);
4368   gtk_tree_path_free (path);
4369   
4370   cursor_path = NULL;
4371   drag_dest_path = NULL;
4372
4373   if (tree_view->priv->cursor)
4374     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
4375
4376   if (cursor_path)
4377     _pspp_sheet_view_find_node (tree_view, cursor_path,
4378                               &cursor_tree, &cursor);
4379
4380   if (tree_view->priv->drag_dest_row)
4381     drag_dest_path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
4382
4383   if (drag_dest_path)
4384     _pspp_sheet_view_find_node (tree_view, drag_dest_path,
4385                               &drag_highlight_tree, &drag_highlight);
4386
4387   draw_vgrid_lines =
4388     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
4389     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
4390   draw_hgrid_lines =
4391     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
4392     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
4393
4394   if (draw_vgrid_lines || draw_hgrid_lines)
4395     gtk_widget_style_get (widget, "grid-line-width", &grid_line_width, NULL);
4396   
4397   n_visible_columns = 0;
4398   for (list = tree_view->priv->columns; list; list = list->next)
4399     {
4400       if (! PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
4401         continue;
4402       n_visible_columns ++;
4403     }
4404
4405   /* Find the last column */
4406   for (last_column = g_list_last (tree_view->priv->columns);
4407        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
4408        last_column = last_column->prev)
4409     ;
4410
4411   /* and the first */
4412   for (first_column = g_list_first (tree_view->priv->columns);
4413        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
4414        first_column = first_column->next)
4415     ;
4416
4417   /* Actually process the expose event.  To do this, we want to
4418    * start at the first node of the event, and walk the tree in
4419    * order, drawing each successive node.
4420    */
4421
4422   do
4423     {
4424       gboolean parity;
4425       gboolean is_separator = FALSE;
4426       gboolean is_first = FALSE;
4427       gboolean is_last = FALSE;
4428       
4429       is_separator = row_is_separator (tree_view, &iter, NULL);
4430
4431       max_height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
4432
4433       cell_offset = 0;
4434       highlight_x = 0; /* should match x coord of first cell */
4435       expander_cell_width = 0;
4436
4437       background_area.y = y_offset + event->area.y;
4438       background_area.height = max_height;
4439
4440       flags = 0;
4441
4442       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PRELIT))
4443         flags |= GTK_CELL_RENDERER_PRELIT;
4444
4445       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED))
4446         flags |= GTK_CELL_RENDERER_SELECTED;
4447
4448       parity = _pspp_rbtree_node_find_parity (tree, node);
4449
4450       /* we *need* to set cell data on all cells before the call
4451        * to _has_special_cell, else _has_special_cell() does not
4452        * return a correct value.
4453        */
4454       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4455            list;
4456            list = (rtl ? list->prev : list->next))
4457         {
4458           PsppSheetViewColumn *column = list->data;
4459           pspp_sheet_view_column_cell_set_cell_data (column,
4460                                                    tree_view->priv->model,
4461                                                    &iter,
4462                                                    PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT),
4463                                                    node->children?TRUE:FALSE);
4464         }
4465
4466       has_special_cell = pspp_sheet_view_has_special_cell (tree_view);
4467
4468       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
4469            list;
4470            list = (rtl ? list->prev : list->next))
4471         {
4472           PsppSheetViewColumn *column = list->data;
4473           const gchar *detail = NULL;
4474           GtkStateType state;
4475
4476           if (!column->visible)
4477             continue;
4478
4479           if (cell_offset > event->area.x + event->area.width ||
4480               cell_offset + column->width < event->area.x)
4481             {
4482               cell_offset += column->width;
4483               continue;
4484             }
4485
4486           if (column->show_sort_indicator)
4487             flags |= GTK_CELL_RENDERER_SORTED;
4488           else
4489             flags &= ~GTK_CELL_RENDERER_SORTED;
4490
4491           if (cursor == node)
4492             flags |= GTK_CELL_RENDERER_FOCUSED;
4493           else
4494             flags &= ~GTK_CELL_RENDERER_FOCUSED;
4495
4496           background_area.x = cell_offset;
4497           background_area.width = column->width;
4498
4499           cell_area = background_area;
4500           cell_area.y += vertical_separator / 2;
4501           cell_area.x += horizontal_separator / 2;
4502           cell_area.height -= vertical_separator;
4503           cell_area.width -= horizontal_separator;
4504
4505           if (draw_vgrid_lines)
4506             {
4507               if (list == first_column)
4508                 {
4509                   cell_area.width -= grid_line_width / 2;
4510                 }
4511               else if (list == last_column)
4512                 {
4513                   cell_area.x += grid_line_width / 2;
4514                   cell_area.width -= grid_line_width / 2;
4515                 }
4516               else
4517                 {
4518                   cell_area.x += grid_line_width / 2;
4519                   cell_area.width -= grid_line_width;
4520                 }
4521             }
4522
4523           if (draw_hgrid_lines)
4524             {
4525               cell_area.y += grid_line_width / 2;
4526               cell_area.height -= grid_line_width;
4527             }
4528
4529           if (gdk_region_rect_in (event->region, &background_area) == GDK_OVERLAP_RECTANGLE_OUT)
4530             {
4531               cell_offset += column->width;
4532               continue;
4533             }
4534
4535           pspp_sheet_view_column_cell_set_cell_data (column,
4536                                                    tree_view->priv->model,
4537                                                    &iter,
4538                                                    PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT),
4539                                                    node->children?TRUE:FALSE);
4540
4541           /* Select the detail for drawing the cell.  relevant
4542            * factors are parity, sortedness, and whether to
4543            * display rules.
4544            */
4545           if (allow_rules && tree_view->priv->has_rules)
4546             {
4547               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4548                   n_visible_columns >= 3)
4549                 {
4550                   if (parity)
4551                     detail = "cell_odd_ruled_sorted";
4552                   else
4553                     detail = "cell_even_ruled_sorted";
4554                 }
4555               else
4556                 {
4557                   if (parity)
4558                     detail = "cell_odd_ruled";
4559                   else
4560                     detail = "cell_even_ruled";
4561                 }
4562             }
4563           else
4564             {
4565               if ((flags & GTK_CELL_RENDERER_SORTED) &&
4566                   n_visible_columns >= 3)
4567                 {
4568                   if (parity)
4569                     detail = "cell_odd_sorted";
4570                   else
4571                     detail = "cell_even_sorted";
4572                 }
4573               else
4574                 {
4575                   if (parity)
4576                     detail = "cell_odd";
4577                   else
4578                     detail = "cell_even";
4579                 }
4580             }
4581
4582           g_assert (detail);
4583
4584           if (widget->state == GTK_STATE_INSENSITIVE)
4585             state = GTK_STATE_INSENSITIVE;          
4586           else if (flags & GTK_CELL_RENDERER_SELECTED)
4587             state = GTK_STATE_SELECTED;
4588           else
4589             state = GTK_STATE_NORMAL;
4590
4591           /* Draw background */
4592           if (row_ending_details)
4593             {
4594               char new_detail[128];
4595
4596               is_first = (rtl ? !list->next : !list->prev);
4597               is_last = (rtl ? !list->prev : !list->next);
4598
4599               /* (I don't like the snprintfs either, but couldn't find a
4600                * less messy way).
4601                */
4602               if (is_first && is_last)
4603                 g_snprintf (new_detail, 127, "%s", detail);
4604               else if (is_first)
4605                 g_snprintf (new_detail, 127, "%s_start", detail);
4606               else if (is_last)
4607                 g_snprintf (new_detail, 127, "%s_end", detail);
4608               else
4609                 g_snprintf (new_detail, 128, "%s_middle", detail);
4610
4611               gtk_paint_flat_box (widget->style,
4612                                   event->window,
4613                                   state,
4614                                   GTK_SHADOW_NONE,
4615                                   &event->area,
4616                                   widget,
4617                                   new_detail,
4618                                   background_area.x,
4619                                   background_area.y,
4620                                   background_area.width,
4621                                   background_area.height);
4622             }
4623           else
4624             {
4625               gtk_paint_flat_box (widget->style,
4626                                   event->window,
4627                                   state,
4628                                   GTK_SHADOW_NONE,
4629                                   &event->area,
4630                                   widget,
4631                                   detail,
4632                                   background_area.x,
4633                                   background_area.y,
4634                                   background_area.width,
4635                                   background_area.height);
4636             }
4637
4638           if (draw_hgrid_lines)
4639             {
4640               if (background_area.y > 0)
4641                 gdk_draw_line (event->window,
4642                                tree_view->priv->grid_line_gc,
4643                                background_area.x, background_area.y,
4644                                background_area.x + background_area.width,
4645                                background_area.y);
4646
4647               if (y_offset + max_height >= event->area.height)
4648                 gdk_draw_line (event->window,
4649                                tree_view->priv->grid_line_gc,
4650                                background_area.x, background_area.y + max_height,
4651                                background_area.x + background_area.width,
4652                                background_area.y + max_height);
4653             }
4654
4655           if (pspp_sheet_view_is_expander_column (tree_view, column))
4656             {
4657               if (!rtl)
4658                 cell_area.x += (depth - 1) * tree_view->priv->level_indentation;
4659               cell_area.width -= (depth - 1) * tree_view->priv->level_indentation;
4660
4661               if (TREE_VIEW_DRAW_EXPANDERS(tree_view))
4662                 {
4663                   if (!rtl)
4664                     cell_area.x += depth * tree_view->priv->expander_size;
4665                   cell_area.width -= depth * tree_view->priv->expander_size;
4666                 }
4667
4668               /* If we have an expander column, the highlight underline
4669                * starts with that column, so that it indicates which
4670                * level of the tree we're dropping at.
4671                */
4672               highlight_x = cell_area.x;
4673               expander_cell_width = cell_area.width;
4674
4675               if (is_separator)
4676                 gtk_paint_hline (widget->style,
4677                                  event->window,
4678                                  state,
4679                                  &cell_area,
4680                                  widget,
4681                                  NULL,
4682                                  cell_area.x,
4683                                  cell_area.x + cell_area.width,
4684                                  cell_area.y + cell_area.height / 2);
4685               else
4686                 _pspp_sheet_view_column_cell_render (column,
4687                                                    event->window,
4688                                                    &background_area,
4689                                                    &cell_area,
4690                                                    &event->area,
4691                                                    flags);
4692               if (TREE_VIEW_DRAW_EXPANDERS(tree_view)
4693                   && (node->flags & PSPP_RBNODE_IS_PARENT) == PSPP_RBNODE_IS_PARENT)
4694                 {
4695                   if (!got_pointer)
4696                     {
4697                       gdk_window_get_pointer (tree_view->priv->bin_window, 
4698                                               &pointer_x, &pointer_y, NULL);
4699                       got_pointer = TRUE;
4700                     }
4701
4702                   pspp_sheet_view_draw_arrow (PSPP_SHEET_VIEW (widget),
4703                                             tree,
4704                                             node,
4705                                             pointer_x, pointer_y);
4706                 }
4707             }
4708           else
4709             {
4710               if (is_separator)
4711                 gtk_paint_hline (widget->style,
4712                                  event->window,
4713                                  state,
4714                                  &cell_area,
4715                                  widget,
4716                                  NULL,
4717                                  cell_area.x,
4718                                  cell_area.x + cell_area.width,
4719                                  cell_area.y + cell_area.height / 2);
4720               else
4721                 _pspp_sheet_view_column_cell_render (column,
4722                                                    event->window,
4723                                                    &background_area,
4724                                                    &cell_area,
4725                                                    &event->area,
4726                                                    flags);
4727             }
4728
4729           if (pspp_sheet_view_is_expander_column (tree_view, column) &&
4730               tree_view->priv->tree_lines_enabled)
4731             {
4732               gint x = background_area.x;
4733               gint mult = rtl ? -1 : 1;
4734               gint y0 = background_area.y;
4735               gint y1 = background_area.y + background_area.height/2;
4736               gint y2 = background_area.y + background_area.height;
4737
4738               if (rtl)
4739                 x += background_area.width - 1;
4740
4741               if ((node->flags & PSPP_RBNODE_IS_PARENT) == PSPP_RBNODE_IS_PARENT
4742                   && depth > 1)
4743                 {
4744                   gdk_draw_line (event->window,
4745                                  tree_view->priv->tree_line_gc,
4746                                  x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4747                                  y1,
4748                                  x + tree_view->priv->expander_size * (depth - 1.1) * mult,
4749                                  y1);
4750                 }
4751               else if (depth > 1)
4752                 {
4753                   gdk_draw_line (event->window,
4754                                  tree_view->priv->tree_line_gc,
4755                                  x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4756                                  y1,
4757                                  x + tree_view->priv->expander_size * (depth - 0.5) * mult,
4758                                  y1);
4759                 }
4760
4761               if (depth > 1)
4762                 {
4763                   gint i;
4764                   GtkRBNode *tmp_node;
4765                   GtkRBTree *tmp_tree;
4766
4767                   if (!_pspp_rbtree_next (tree, node))
4768                     gdk_draw_line (event->window,
4769                                    tree_view->priv->tree_line_gc,
4770                                    x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4771                                    y0,
4772                                    x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4773                                    y1);
4774                   else
4775                     gdk_draw_line (event->window,
4776                                    tree_view->priv->tree_line_gc,
4777                                    x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4778                                    y0,
4779                                    x + tree_view->priv->expander_size * (depth - 1.5) * mult,
4780                                    y2);
4781
4782                   tmp_node = tree->parent_node;
4783                   tmp_tree = tree->parent_tree;
4784
4785                   for (i = depth - 2; i > 0; i--)
4786                     {
4787                       if (_pspp_rbtree_next (tmp_tree, tmp_node))
4788                         gdk_draw_line (event->window,
4789                                        tree_view->priv->tree_line_gc,
4790                                        x + tree_view->priv->expander_size * (i - 0.5) * mult,
4791                                        y0,
4792                                        x + tree_view->priv->expander_size * (i - 0.5) * mult,
4793                                        y2);
4794
4795                       tmp_node = tmp_tree->parent_node;
4796                       tmp_tree = tmp_tree->parent_tree;
4797                     }
4798                 }
4799             }
4800
4801           if (node == cursor && has_special_cell &&
4802               ((column == tree_view->priv->focus_column &&
4803                 PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4804                 gtk_widget_has_focus (widget)) ||
4805                (column == tree_view->priv->edited_column)))
4806             {
4807               _pspp_sheet_view_column_cell_draw_focus (column,
4808                                                      event->window,
4809                                                      &background_area,
4810                                                      &cell_area,
4811                                                      &event->area,
4812                                                      flags);
4813             }
4814
4815           cell_offset += column->width;
4816         }
4817
4818       if (node == drag_highlight)
4819         {
4820           /* Draw indicator for the drop
4821            */
4822           gint highlight_y = -1;
4823           GtkRBTree *tree = NULL;
4824           GtkRBNode *node = NULL;
4825           gint width;
4826
4827           switch (tree_view->priv->drag_dest_pos)
4828             {
4829             case PSPP_SHEET_VIEW_DROP_BEFORE:
4830               highlight_y = background_area.y - 1;
4831               if (highlight_y < 0)
4832                       highlight_y = 0;
4833               break;
4834
4835             case PSPP_SHEET_VIEW_DROP_AFTER:
4836               highlight_y = background_area.y + background_area.height - 1;
4837               break;
4838
4839             case PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE:
4840             case PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER:
4841               _pspp_sheet_view_find_node (tree_view, drag_dest_path, &tree, &node);
4842
4843               if (tree == NULL)
4844                 break;
4845               gdk_drawable_get_size (tree_view->priv->bin_window,
4846                                      &width, NULL);
4847
4848               if (row_ending_details)
4849                 gtk_paint_focus (widget->style,
4850                                  tree_view->priv->bin_window,
4851                                  gtk_widget_get_state (widget),
4852                                  &event->area,
4853                                  widget,
4854                                  (is_first
4855                                   ? (is_last ? "treeview-drop-indicator" : "treeview-drop-indicator-left" )
4856                                   : (is_last ? "treeview-drop-indicator-right" : "tree-view-drop-indicator-middle" )),
4857                                  0, BACKGROUND_FIRST_PIXEL (tree_view, tree, node)
4858                                  - focus_line_width / 2,
4859                                  width, ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node))
4860                                - focus_line_width + 1);
4861               else
4862                 gtk_paint_focus (widget->style,
4863                                  tree_view->priv->bin_window,
4864                                  gtk_widget_get_state (widget),
4865                                  &event->area,
4866                                  widget,
4867                                  "treeview-drop-indicator",
4868                                  0, BACKGROUND_FIRST_PIXEL (tree_view, tree, node)
4869                                  - focus_line_width / 2,
4870                                  width, ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node))
4871                                  - focus_line_width + 1);
4872               break;
4873             }
4874
4875           if (highlight_y >= 0)
4876             {
4877               gdk_draw_line (event->window,
4878                              widget->style->fg_gc[gtk_widget_get_state (widget)],
4879                              rtl ? highlight_x + expander_cell_width : highlight_x,
4880                              highlight_y,
4881                              rtl ? 0 : bin_window_width,
4882                              highlight_y);
4883             }
4884         }
4885
4886       /* draw the big row-spanning focus rectangle, if needed */
4887       if (!has_special_cell && node == cursor &&
4888           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS) &&
4889           gtk_widget_has_focus (widget))
4890         {
4891           gint tmp_y, tmp_height;
4892           gint width;
4893           GtkStateType focus_rect_state;
4894
4895           focus_rect_state =
4896             flags & GTK_CELL_RENDERER_SELECTED ? GTK_STATE_SELECTED :
4897             (flags & GTK_CELL_RENDERER_PRELIT ? GTK_STATE_PRELIGHT :
4898              (flags & GTK_CELL_RENDERER_INSENSITIVE ? GTK_STATE_INSENSITIVE :
4899               GTK_STATE_NORMAL));
4900
4901           gdk_drawable_get_size (tree_view->priv->bin_window,
4902                                  &width, NULL);
4903           
4904           if (draw_hgrid_lines)
4905             {
4906               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node) + grid_line_width / 2;
4907               tmp_height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node)) - grid_line_width;
4908             }
4909           else
4910             {
4911               tmp_y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node);
4912               tmp_height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
4913             }
4914
4915           if (row_ending_details)
4916             gtk_paint_focus (widget->style,
4917                              tree_view->priv->bin_window,
4918                              focus_rect_state,
4919                              &event->area,
4920                              widget,
4921                              (is_first
4922                               ? (is_last ? "treeview" : "treeview-left" )
4923                               : (is_last ? "treeview-right" : "treeview-middle" )),
4924                              0, tmp_y,
4925                              width, tmp_height);
4926           else
4927             gtk_paint_focus (widget->style,
4928                              tree_view->priv->bin_window,
4929                              focus_rect_state,
4930                              &event->area,
4931                              widget,
4932                              "treeview",
4933                              0, tmp_y,
4934                              width, tmp_height);
4935         }
4936
4937       y_offset += max_height;
4938       if (node->children)
4939         {
4940           GtkTreeIter parent = iter;
4941           gboolean has_child;
4942
4943           tree = node->children;
4944           node = tree->root;
4945
4946           g_assert (node != tree->nil);
4947
4948           while (node->left != tree->nil)
4949             node = node->left;
4950           has_child = gtk_tree_model_iter_children (tree_view->priv->model,
4951                                                     &iter,
4952                                                     &parent);
4953           depth++;
4954
4955           /* Sanity Check! */
4956           TREE_VIEW_INTERNAL_ASSERT (has_child, FALSE);
4957         }
4958       else
4959         {
4960           gboolean done = FALSE;
4961
4962           do
4963             {
4964               node = _pspp_rbtree_next (tree, node);
4965               if (node != NULL)
4966                 {
4967                   gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
4968                   done = TRUE;
4969
4970                   /* Sanity Check! */
4971                   TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
4972                 }
4973               else
4974                 {
4975                   GtkTreeIter parent_iter = iter;
4976                   gboolean has_parent;
4977
4978                   node = tree->parent_node;
4979                   tree = tree->parent_tree;
4980                   if (tree == NULL)
4981                     /* we should go to done to free some memory */
4982                     goto done;
4983                   has_parent = gtk_tree_model_iter_parent (tree_view->priv->model,
4984                                                            &iter,
4985                                                            &parent_iter);
4986                   depth--;
4987
4988                   /* Sanity check */
4989                   TREE_VIEW_INTERNAL_ASSERT (has_parent, FALSE);
4990                 }
4991             }
4992           while (!done);
4993         }
4994     }
4995   while (y_offset < event->area.height);
4996
4997 done:
4998   pspp_sheet_view_draw_grid_lines (tree_view, event, n_visible_columns);
4999
5000  if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
5001    {
5002      GdkRectangle *rectangles;
5003      gint n_rectangles;
5004
5005      gdk_region_get_rectangles (event->region,
5006                                 &rectangles,
5007                                 &n_rectangles);
5008
5009      while (n_rectangles--)
5010        pspp_sheet_view_paint_rubber_band (tree_view, &rectangles[n_rectangles]);
5011
5012      g_free (rectangles);
5013    }
5014
5015   if (cursor_path)
5016     gtk_tree_path_free (cursor_path);
5017
5018   if (drag_dest_path)
5019     gtk_tree_path_free (drag_dest_path);
5020
5021   return FALSE;
5022 }
5023
5024 static gboolean
5025 pspp_sheet_view_expose (GtkWidget      *widget,
5026                       GdkEventExpose *event)
5027 {
5028   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
5029
5030   if (event->window == tree_view->priv->bin_window)
5031     {
5032       gboolean retval;
5033       GList *tmp_list;
5034
5035       retval = pspp_sheet_view_bin_expose (widget, event);
5036
5037       /* We can't just chain up to Container::expose as it will try to send the
5038        * event to the headers, so we handle propagating it to our children
5039        * (eg. widgets being edited) ourselves.
5040        */
5041       tmp_list = tree_view->priv->children;
5042       while (tmp_list)
5043         {
5044           PsppSheetViewChild *child = tmp_list->data;
5045           tmp_list = tmp_list->next;
5046
5047           gtk_container_propagate_expose (GTK_CONTAINER (tree_view), child->widget, event);
5048         }
5049
5050       return retval;
5051     }
5052
5053   else if (event->window == tree_view->priv->header_window)
5054     {
5055       GList *list;
5056       
5057       for (list = tree_view->priv->columns; list != NULL; list = list->next)
5058         {
5059           PsppSheetViewColumn *column = list->data;
5060
5061           if (column == tree_view->priv->drag_column)
5062             continue;
5063
5064           if (column->visible)
5065             gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
5066                                             column->button,
5067                                             event);
5068         }
5069     }
5070   else if (event->window == tree_view->priv->drag_window)
5071     {
5072       gtk_container_propagate_expose (GTK_CONTAINER (tree_view),
5073                                       tree_view->priv->drag_column->button,
5074                                       event);
5075     }
5076   return TRUE;
5077 }
5078
5079 enum
5080 {
5081   DROP_HOME,
5082   DROP_RIGHT,
5083   DROP_LEFT,
5084   DROP_END
5085 };
5086
5087 /* returns 0x1 when no column has been found -- yes it's hackish */
5088 static PsppSheetViewColumn *
5089 pspp_sheet_view_get_drop_column (PsppSheetView       *tree_view,
5090                                PsppSheetViewColumn *column,
5091                                gint               drop_position)
5092 {
5093   PsppSheetViewColumn *left_column = NULL;
5094   PsppSheetViewColumn *cur_column = NULL;
5095   GList *tmp_list;
5096
5097   if (!column->reorderable)
5098     return (PsppSheetViewColumn *)0x1;
5099
5100   switch (drop_position)
5101     {
5102       case DROP_HOME:
5103         /* find first column where we can drop */
5104         tmp_list = tree_view->priv->columns;
5105         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
5106           return (PsppSheetViewColumn *)0x1;
5107
5108         while (tmp_list)
5109           {
5110             g_assert (tmp_list);
5111
5112             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5113             tmp_list = tmp_list->next;
5114
5115             if (left_column && left_column->visible == FALSE)
5116               continue;
5117
5118             if (!tree_view->priv->column_drop_func)
5119               return left_column;
5120
5121             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
5122               {
5123                 left_column = cur_column;
5124                 continue;
5125               }
5126
5127             return left_column;
5128           }
5129
5130         if (!tree_view->priv->column_drop_func)
5131           return left_column;
5132
5133         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
5134           return left_column;
5135         else
5136           return (PsppSheetViewColumn *)0x1;
5137         break;
5138
5139       case DROP_RIGHT:
5140         /* find first column after column where we can drop */
5141         tmp_list = tree_view->priv->columns;
5142
5143         for (; tmp_list; tmp_list = tmp_list->next)
5144           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
5145             break;
5146
5147         if (!tmp_list || !tmp_list->next)
5148           return (PsppSheetViewColumn *)0x1;
5149
5150         tmp_list = tmp_list->next;
5151         left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5152         tmp_list = tmp_list->next;
5153
5154         while (tmp_list)
5155           {
5156             g_assert (tmp_list);
5157
5158             cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5159             tmp_list = tmp_list->next;
5160
5161             if (left_column && left_column->visible == FALSE)
5162               {
5163                 left_column = cur_column;
5164                 if (tmp_list)
5165                   tmp_list = tmp_list->next;
5166                 continue;
5167               }
5168
5169             if (!tree_view->priv->column_drop_func)
5170               return left_column;
5171
5172             if (!tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
5173               {
5174                 left_column = cur_column;
5175                 continue;
5176               }
5177
5178             return left_column;
5179           }
5180
5181         if (!tree_view->priv->column_drop_func)
5182           return left_column;
5183
5184         if (tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data))
5185           return left_column;
5186         else
5187           return (PsppSheetViewColumn *)0x1;
5188         break;
5189
5190       case DROP_LEFT:
5191         /* find first column before column where we can drop */
5192         tmp_list = tree_view->priv->columns;
5193
5194         for (; tmp_list; tmp_list = tmp_list->next)
5195           if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data) == column)
5196             break;
5197
5198         if (!tmp_list || !tmp_list->prev)
5199           return (PsppSheetViewColumn *)0x1;
5200
5201         tmp_list = tmp_list->prev;
5202         cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5203         tmp_list = tmp_list->prev;
5204
5205         while (tmp_list)
5206           {
5207             g_assert (tmp_list);
5208
5209             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5210
5211             if (left_column && !left_column->visible)
5212               {
5213                 /*if (!tmp_list->prev)
5214                   return (PsppSheetViewColumn *)0x1;
5215                   */
5216 /*
5217                 cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->prev->data);
5218                 tmp_list = tmp_list->prev->prev;
5219                 continue;*/
5220
5221                 cur_column = left_column;
5222                 if (tmp_list)
5223                   tmp_list = tmp_list->prev;
5224                 continue;
5225               }
5226
5227             if (!tree_view->priv->column_drop_func)
5228               return left_column;
5229
5230             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
5231               return left_column;
5232
5233             cur_column = left_column;
5234             tmp_list = tmp_list->prev;
5235           }
5236
5237         if (!tree_view->priv->column_drop_func)
5238           return NULL;
5239
5240         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
5241           return NULL;
5242         else
5243           return (PsppSheetViewColumn *)0x1;
5244         break;
5245
5246       case DROP_END:
5247         /* same as DROP_HOME case, but doing it backwards */
5248         tmp_list = g_list_last (tree_view->priv->columns);
5249         cur_column = NULL;
5250
5251         if (column == PSPP_SHEET_VIEW_COLUMN (tmp_list->data))
5252           return (PsppSheetViewColumn *)0x1;
5253
5254         while (tmp_list)
5255           {
5256             g_assert (tmp_list);
5257
5258             left_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
5259
5260             if (left_column && !left_column->visible)
5261               {
5262                 cur_column = left_column;
5263                 tmp_list = tmp_list->prev;
5264               }
5265
5266             if (!tree_view->priv->column_drop_func)
5267               return left_column;
5268
5269             if (tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
5270               return left_column;
5271
5272             cur_column = left_column;
5273             tmp_list = tmp_list->prev;
5274           }
5275
5276         if (!tree_view->priv->column_drop_func)
5277           return NULL;
5278
5279         if (tree_view->priv->column_drop_func (tree_view, column, NULL, cur_column, tree_view->priv->column_drop_func_data))
5280           return NULL;
5281         else
5282           return (PsppSheetViewColumn *)0x1;
5283         break;
5284     }
5285
5286   return (PsppSheetViewColumn *)0x1;
5287 }
5288
5289 static gboolean
5290 pspp_sheet_view_key_press (GtkWidget   *widget,
5291                          GdkEventKey *event)
5292 {
5293   PsppSheetView *tree_view = (PsppSheetView *) widget;
5294
5295   if (tree_view->priv->rubber_band_status)
5296     {
5297       if (event->keyval == GDK_Escape)
5298         pspp_sheet_view_stop_rubber_band (tree_view);
5299
5300       return TRUE;
5301     }
5302
5303   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG))
5304     {
5305       if (event->keyval == GDK_Escape)
5306         {
5307           tree_view->priv->cur_reorder = NULL;
5308           pspp_sheet_view_button_release_drag_column (widget, NULL);
5309         }
5310       return TRUE;
5311     }
5312
5313   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
5314     {
5315       GList *focus_column;
5316       gboolean rtl;
5317
5318       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
5319
5320       for (focus_column = tree_view->priv->columns;
5321            focus_column;
5322            focus_column = focus_column->next)
5323         {
5324           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
5325
5326           if (gtk_widget_has_focus (column->button))
5327             break;
5328         }
5329
5330       if (focus_column &&
5331           (event->state & GDK_SHIFT_MASK) && (event->state & GDK_MOD1_MASK) &&
5332           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
5333            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right))
5334         {
5335           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
5336
5337           if (!column->resizable)
5338             {
5339               gtk_widget_error_bell (widget);
5340               return TRUE;
5341             }
5342
5343           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
5344               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
5345             {
5346               gint old_width = column->resized_width;
5347
5348               column->resized_width = MAX (column->resized_width,
5349                                            column->width);
5350               column->resized_width -= 2;
5351               if (column->resized_width < 0)
5352                 column->resized_width = 0;
5353
5354               if (column->min_width == -1)
5355                 column->resized_width = MAX (column->button->requisition.width,
5356                                              column->resized_width);
5357               else
5358                 column->resized_width = MAX (column->min_width,
5359                                              column->resized_width);
5360
5361               if (column->max_width != -1)
5362                 column->resized_width = MIN (column->resized_width,
5363                                              column->max_width);
5364
5365               column->use_resized_width = TRUE;
5366
5367               if (column->resized_width != old_width)
5368                 gtk_widget_queue_resize (widget);
5369               else
5370                 gtk_widget_error_bell (widget);
5371             }
5372           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
5373                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
5374             {
5375               gint old_width = column->resized_width;
5376
5377               column->resized_width = MAX (column->resized_width,
5378                                            column->width);
5379               column->resized_width += 2;
5380
5381               if (column->max_width != -1)
5382                 column->resized_width = MIN (column->resized_width,
5383                                              column->max_width);
5384
5385               column->use_resized_width = TRUE;
5386
5387               if (column->resized_width != old_width)
5388                 gtk_widget_queue_resize (widget);
5389               else
5390                 gtk_widget_error_bell (widget);
5391             }
5392
5393           return TRUE;
5394         }
5395
5396       if (focus_column &&
5397           (event->state & GDK_MOD1_MASK) &&
5398           (event->keyval == GDK_Left || event->keyval == GDK_KP_Left
5399            || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
5400            || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
5401            || event->keyval == GDK_End || event->keyval == GDK_KP_End))
5402         {
5403           PsppSheetViewColumn *column = PSPP_SHEET_VIEW_COLUMN (focus_column->data);
5404
5405           if (event->keyval == (rtl ? GDK_Right : GDK_Left)
5406               || event->keyval == (rtl ? GDK_KP_Right : GDK_KP_Left))
5407             {
5408               PsppSheetViewColumn *col;
5409               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_LEFT);
5410               if (col != (PsppSheetViewColumn *)0x1)
5411                 pspp_sheet_view_move_column_after (tree_view, column, col);
5412               else
5413                 gtk_widget_error_bell (widget);
5414             }
5415           else if (event->keyval == (rtl ? GDK_Left : GDK_Right)
5416                    || event->keyval == (rtl ? GDK_KP_Left : GDK_KP_Right))
5417             {
5418               PsppSheetViewColumn *col;
5419               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_RIGHT);
5420               if (col != (PsppSheetViewColumn *)0x1)
5421                 pspp_sheet_view_move_column_after (tree_view, column, col);
5422               else
5423                 gtk_widget_error_bell (widget);
5424             }
5425           else if (event->keyval == GDK_Home || event->keyval == GDK_KP_Home)
5426             {
5427               PsppSheetViewColumn *col;
5428               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_HOME);
5429               if (col != (PsppSheetViewColumn *)0x1)
5430                 pspp_sheet_view_move_column_after (tree_view, column, col);
5431               else
5432                 gtk_widget_error_bell (widget);
5433             }
5434           else if (event->keyval == GDK_End || event->keyval == GDK_KP_End)
5435             {
5436               PsppSheetViewColumn *col;
5437               col = pspp_sheet_view_get_drop_column (tree_view, column, DROP_END);
5438               if (col != (PsppSheetViewColumn *)0x1)
5439                 pspp_sheet_view_move_column_after (tree_view, column, col);
5440               else
5441                 gtk_widget_error_bell (widget);
5442             }
5443
5444           return TRUE;
5445         }
5446     }
5447
5448   /* Chain up to the parent class.  It handles the keybindings. */
5449   if (GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_press_event (widget, event))
5450     return TRUE;
5451
5452   if (tree_view->priv->search_entry_avoid_unhandled_binding)
5453     {
5454       tree_view->priv->search_entry_avoid_unhandled_binding = FALSE;
5455       return FALSE;
5456     }
5457
5458   /* We pass the event to the search_entry.  If its text changes, then we start
5459    * the typeahead find capabilities. */
5460   if (gtk_widget_has_focus (GTK_WIDGET (tree_view))
5461       && tree_view->priv->enable_search
5462       && !tree_view->priv->search_custom_entry_set)
5463     {
5464       GdkEvent *new_event;
5465       char *old_text;
5466       const char *new_text;
5467       gboolean retval;
5468       GdkScreen *screen;
5469       gboolean text_modified;
5470       gulong popup_menu_id;
5471
5472       pspp_sheet_view_ensure_interactive_directory (tree_view);
5473
5474       /* Make a copy of the current text */
5475       old_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry)));
5476       new_event = gdk_event_copy ((GdkEvent *) event);
5477       g_object_unref (((GdkEventKey *) new_event)->window);
5478       ((GdkEventKey *) new_event)->window = g_object_ref (tree_view->priv->search_window->window);
5479       gtk_widget_realize (tree_view->priv->search_window);
5480
5481       popup_menu_id = g_signal_connect (tree_view->priv->search_entry, 
5482                                         "popup-menu", G_CALLBACK (gtk_true),
5483                                         NULL);
5484
5485       /* Move the entry off screen */
5486       screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
5487       gtk_window_move (GTK_WINDOW (tree_view->priv->search_window),
5488                        gdk_screen_get_width (screen) + 1,
5489                        gdk_screen_get_height (screen) + 1);
5490       gtk_widget_show (tree_view->priv->search_window);
5491
5492       /* Send the event to the window.  If the preedit_changed signal is emitted
5493        * during this event, we will set priv->imcontext_changed  */
5494       tree_view->priv->imcontext_changed = FALSE;
5495       retval = gtk_widget_event (tree_view->priv->search_window, new_event);
5496       gdk_event_free (new_event);
5497       gtk_widget_hide (tree_view->priv->search_window);
5498
5499       g_signal_handler_disconnect (tree_view->priv->search_entry, 
5500                                    popup_menu_id);
5501
5502       /* We check to make sure that the entry tried to handle the text, and that
5503        * the text has changed.
5504        */
5505       new_text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
5506       text_modified = strcmp (old_text, new_text) != 0;
5507       g_free (old_text);
5508       if (tree_view->priv->imcontext_changed ||    /* we're in a preedit */
5509           (retval && text_modified))               /* ...or the text was modified */
5510         {
5511           if (pspp_sheet_view_real_start_interactive_search (tree_view, FALSE))
5512             {
5513               gtk_widget_grab_focus (GTK_WIDGET (tree_view));
5514               return TRUE;
5515             }
5516           else
5517             {
5518               gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
5519               return FALSE;
5520             }
5521         }
5522     }
5523
5524   return FALSE;
5525 }
5526
5527 static gboolean
5528 pspp_sheet_view_key_release (GtkWidget   *widget,
5529                            GdkEventKey *event)
5530 {
5531   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
5532
5533   if (tree_view->priv->rubber_band_status)
5534     return TRUE;
5535
5536   return GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->key_release_event (widget, event);
5537 }
5538
5539 /* FIXME Is this function necessary? Can I get an enter_notify event
5540  * w/o either an expose event or a mouse motion event?
5541  */
5542 static gboolean
5543 pspp_sheet_view_enter_notify (GtkWidget        *widget,
5544                             GdkEventCrossing *event)
5545 {
5546   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
5547   GtkRBTree *tree;
5548   GtkRBNode *node;
5549   gint new_y;
5550
5551   /* Sanity check it */
5552   if (event->window != tree_view->priv->bin_window)
5553     return FALSE;
5554
5555   if (tree_view->priv->tree == NULL)
5556     return FALSE;
5557
5558   if (event->mode == GDK_CROSSING_GRAB ||
5559       event->mode == GDK_CROSSING_GTK_GRAB ||
5560       event->mode == GDK_CROSSING_GTK_UNGRAB ||
5561       event->mode == GDK_CROSSING_STATE_CHANGED)
5562     return TRUE;
5563
5564   /* find the node internally */
5565   new_y = TREE_WINDOW_Y_TO_RBTREE_Y(tree_view, event->y);
5566   if (new_y < 0)
5567     new_y = 0;
5568   _pspp_rbtree_find_offset (tree_view->priv->tree, new_y, &tree, &node);
5569
5570   tree_view->priv->event_last_x = event->x;
5571   tree_view->priv->event_last_y = event->y;
5572
5573   if ((tree_view->priv->button_pressed_node == NULL) ||
5574       (tree_view->priv->button_pressed_node == node))
5575     prelight_or_select (tree_view, tree, node, event->x, event->y);
5576
5577   return TRUE;
5578 }
5579
5580 static gboolean
5581 pspp_sheet_view_leave_notify (GtkWidget        *widget,
5582                             GdkEventCrossing *event)
5583 {
5584   PsppSheetView *tree_view;
5585
5586   if (event->mode == GDK_CROSSING_GRAB)
5587     return TRUE;
5588
5589   tree_view = PSPP_SHEET_VIEW (widget);
5590
5591   if (tree_view->priv->prelight_node)
5592     _pspp_sheet_view_queue_draw_node (tree_view,
5593                                    tree_view->priv->prelight_tree,
5594                                    tree_view->priv->prelight_node,
5595                                    NULL);
5596
5597   tree_view->priv->event_last_x = -10000;
5598   tree_view->priv->event_last_y = -10000;
5599
5600   prelight_or_select (tree_view,
5601                       NULL, NULL,
5602                       -1000, -1000); /* coords not possibly over an arrow */
5603
5604   return TRUE;
5605 }
5606
5607
5608 static gint
5609 pspp_sheet_view_focus_out (GtkWidget     *widget,
5610                          GdkEventFocus *event)
5611 {
5612   PsppSheetView *tree_view;
5613
5614   tree_view = PSPP_SHEET_VIEW (widget);
5615
5616   gtk_widget_queue_draw (widget);
5617
5618   /* destroy interactive search dialog */
5619   if (tree_view->priv->search_window)
5620     pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
5621
5622   return FALSE;
5623 }
5624
5625
5626 /* Incremental Reflow
5627  */
5628
5629 static void
5630 pspp_sheet_view_node_queue_redraw (PsppSheetView *tree_view,
5631                                  GtkRBTree   *tree,
5632                                  GtkRBNode   *node)
5633 {
5634   gint y;
5635
5636   y = _pspp_rbtree_node_find_offset (tree, node)
5637     - tree_view->priv->vadjustment->value
5638     + TREE_VIEW_HEADER_HEIGHT (tree_view);
5639
5640   gtk_widget_queue_draw_area (GTK_WIDGET (tree_view),
5641                               0, y,
5642                               GTK_WIDGET (tree_view)->allocation.width,
5643                               PSPP_RBNODE_GET_HEIGHT (node));
5644 }
5645
5646 static gboolean
5647 node_is_visible (PsppSheetView *tree_view,
5648                  GtkRBTree   *tree,
5649                  GtkRBNode   *node)
5650 {
5651   int y;
5652   int height;
5653
5654   y = _pspp_rbtree_node_find_offset (tree, node);
5655   height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
5656
5657   if (y >= tree_view->priv->vadjustment->value &&
5658       y + height <= (tree_view->priv->vadjustment->value
5659                      + tree_view->priv->vadjustment->page_size))
5660     return TRUE;
5661
5662   return FALSE;
5663 }
5664
5665 /* Returns TRUE if it updated the size
5666  */
5667 static gboolean
5668 validate_row (PsppSheetView *tree_view,
5669               GtkRBTree   *tree,
5670               GtkRBNode   *node,
5671               GtkTreeIter *iter,
5672               GtkTreePath *path)
5673 {
5674   PsppSheetViewColumn *column;
5675   GList *list, *first_column, *last_column;
5676   gint height = 0;
5677   gint horizontal_separator;
5678   gint vertical_separator;
5679   gint focus_line_width;
5680   gint depth = gtk_tree_path_get_depth (path);
5681   gboolean retval = FALSE;
5682   gboolean is_separator = FALSE;
5683   gboolean draw_vgrid_lines, draw_hgrid_lines;
5684   gint focus_pad;
5685   gint grid_line_width;
5686   gboolean wide_separators;
5687   gint separator_height;
5688
5689   /* double check the row needs validating */
5690   if (! PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) &&
5691       ! PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
5692     return FALSE;
5693
5694   is_separator = row_is_separator (tree_view, iter, NULL);
5695
5696   gtk_widget_style_get (GTK_WIDGET (tree_view),
5697                         "focus-padding", &focus_pad,
5698                         "focus-line-width", &focus_line_width,
5699                         "horizontal-separator", &horizontal_separator,
5700                         "vertical-separator", &vertical_separator,
5701                         "grid-line-width", &grid_line_width,
5702                         "wide-separators",  &wide_separators,
5703                         "separator-height", &separator_height,
5704                         NULL);
5705   
5706   draw_vgrid_lines =
5707     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_VERTICAL
5708     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5709   draw_hgrid_lines =
5710     tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL
5711     || tree_view->priv->grid_lines == PSPP_SHEET_VIEW_GRID_LINES_BOTH;
5712
5713   for (last_column = g_list_last (tree_view->priv->columns);
5714        last_column && !(PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible);
5715        last_column = last_column->prev)
5716     ;
5717
5718   for (first_column = g_list_first (tree_view->priv->columns);
5719        first_column && !(PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible);
5720        first_column = first_column->next)
5721     ;
5722
5723   for (list = tree_view->priv->columns; list; list = list->next)
5724     {
5725       gint tmp_width;
5726       gint tmp_height;
5727
5728       column = list->data;
5729
5730       if (! column->visible)
5731         continue;
5732
5733       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID) && !column->dirty)
5734         continue;
5735
5736       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, iter,
5737                                                PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT),
5738                                                node->children?TRUE:FALSE);
5739       pspp_sheet_view_column_cell_get_size (column,
5740                                           NULL, NULL, NULL,
5741                                           &tmp_width, &tmp_height);
5742
5743       if (!is_separator)
5744         {
5745           tmp_height += vertical_separator;
5746           height = MAX (height, tmp_height);
5747           height = MAX (height, tree_view->priv->expander_size);
5748         }
5749       else
5750         {
5751           if (wide_separators)
5752             height = separator_height + 2 * focus_pad;
5753           else
5754             height = 2 + 2 * focus_pad;
5755         }
5756
5757       if (pspp_sheet_view_is_expander_column (tree_view, column))
5758         {
5759           tmp_width = tmp_width + horizontal_separator + (depth - 1) * tree_view->priv->level_indentation;
5760
5761           if (TREE_VIEW_DRAW_EXPANDERS (tree_view))
5762             tmp_width += depth * tree_view->priv->expander_size;
5763         }
5764       else
5765         tmp_width = tmp_width + horizontal_separator;
5766
5767       if (draw_vgrid_lines)
5768         {
5769           if (list->data == first_column || list->data == last_column)
5770             tmp_width += grid_line_width / 2.0;
5771           else
5772             tmp_width += grid_line_width;
5773         }
5774
5775       if (tmp_width > column->requested_width)
5776         {
5777           retval = TRUE;
5778           column->requested_width = tmp_width;
5779         }
5780     }
5781
5782   if (draw_hgrid_lines)
5783     height += grid_line_width;
5784
5785   if (height != PSPP_RBNODE_GET_HEIGHT (node))
5786     {
5787       retval = TRUE;
5788       _pspp_rbtree_node_set_height (tree, node, height);
5789     }
5790   _pspp_rbtree_node_mark_valid (tree, node);
5791   tree_view->priv->post_validation_flag = TRUE;
5792
5793   return retval;
5794 }
5795
5796
5797 static void
5798 validate_visible_area (PsppSheetView *tree_view)
5799 {
5800   GtkTreePath *path = NULL;
5801   GtkTreePath *above_path = NULL;
5802   GtkTreeIter iter;
5803   GtkRBTree *tree = NULL;
5804   GtkRBNode *node = NULL;
5805   gboolean need_redraw = FALSE;
5806   gboolean size_changed = FALSE;
5807   gint total_height;
5808   gint area_above = 0;
5809   gint area_below = 0;
5810
5811   if (tree_view->priv->tree == NULL)
5812     return;
5813
5814   if (! PSPP_RBNODE_FLAG_SET (tree_view->priv->tree->root, PSPP_RBNODE_DESCENDANTS_INVALID) &&
5815       tree_view->priv->scroll_to_path == NULL)
5816     return;
5817
5818   total_height = GTK_WIDGET (tree_view)->allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
5819
5820   if (total_height == 0)
5821     return;
5822
5823   /* First, we check to see if we need to scroll anywhere
5824    */
5825   if (tree_view->priv->scroll_to_path)
5826     {
5827       path = gtk_tree_row_reference_get_path (tree_view->priv->scroll_to_path);
5828       if (path && !_pspp_sheet_view_find_node (tree_view, path, &tree, &node))
5829         {
5830           /* we are going to scroll, and will update dy */
5831           gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5832           if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) ||
5833               PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
5834             {
5835               _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
5836               if (validate_row (tree_view, tree, node, &iter, path))
5837                 size_changed = TRUE;
5838             }
5839
5840           if (tree_view->priv->scroll_to_use_align)
5841             {
5842               gint height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
5843               area_above = (total_height - height) *
5844                 tree_view->priv->scroll_to_row_align;
5845               area_below = total_height - area_above - height;
5846               area_above = MAX (area_above, 0);
5847               area_below = MAX (area_below, 0);
5848             }
5849           else
5850             {
5851               /* two cases:
5852                * 1) row not visible
5853                * 2) row visible
5854                */
5855               gint dy;
5856               gint height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
5857
5858               dy = _pspp_rbtree_node_find_offset (tree, node);
5859
5860               if (dy >= tree_view->priv->vadjustment->value &&
5861                   dy + height <= (tree_view->priv->vadjustment->value
5862                                   + tree_view->priv->vadjustment->page_size))
5863                 {
5864                   /* row visible: keep the row at the same position */
5865                   area_above = dy - tree_view->priv->vadjustment->value;
5866                   area_below = (tree_view->priv->vadjustment->value +
5867                                 tree_view->priv->vadjustment->page_size)
5868                                - dy - height;
5869                 }
5870               else
5871                 {
5872                   /* row not visible */
5873                   if (dy >= 0
5874                       && dy + height <= tree_view->priv->vadjustment->page_size)
5875                     {
5876                       /* row at the beginning -- fixed */
5877                       area_above = dy;
5878                       area_below = tree_view->priv->vadjustment->page_size
5879                                    - area_above - height;
5880                     }
5881                   else if (dy >= (tree_view->priv->vadjustment->upper -
5882                                   tree_view->priv->vadjustment->page_size))
5883                     {
5884                       /* row at the end -- fixed */
5885                       area_above = dy - (tree_view->priv->vadjustment->upper -
5886                                    tree_view->priv->vadjustment->page_size);
5887                       area_below = tree_view->priv->vadjustment->page_size -
5888                                    area_above - height;
5889
5890                       if (area_below < 0)
5891                         {
5892                           area_above = tree_view->priv->vadjustment->page_size - height;
5893                           area_below = 0;
5894                         }
5895                     }
5896                   else
5897                     {
5898                       /* row somewhere in the middle, bring it to the top
5899                        * of the view
5900                        */
5901                       area_above = 0;
5902                       area_below = total_height - height;
5903                     }
5904                 }
5905             }
5906         }
5907       else
5908         /* the scroll to isn't valid; ignore it.
5909          */
5910         {
5911           if (tree_view->priv->scroll_to_path && !path)
5912             {
5913               gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
5914               tree_view->priv->scroll_to_path = NULL;
5915             }
5916           if (path)
5917             gtk_tree_path_free (path);
5918           path = NULL;
5919         }      
5920     }
5921
5922   /* We didn't have a scroll_to set, so we just handle things normally
5923    */
5924   if (path == NULL)
5925     {
5926       gint offset;
5927
5928       offset = _pspp_rbtree_find_offset (tree_view->priv->tree,
5929                                         TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, 0),
5930                                         &tree, &node);
5931       if (node == NULL)
5932         {
5933           /* In this case, nothing has been validated */
5934           path = gtk_tree_path_new_first ();
5935           _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
5936         }
5937       else
5938         {
5939           path = _pspp_sheet_view_find_path (tree_view, tree, node);
5940           total_height += offset;
5941         }
5942
5943       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
5944
5945       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) ||
5946           PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
5947         {
5948           _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
5949           if (validate_row (tree_view, tree, node, &iter, path))
5950             size_changed = TRUE;
5951         }
5952       area_above = 0;
5953       area_below = total_height - ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
5954     }
5955
5956   above_path = gtk_tree_path_copy (path);
5957
5958   /* if we do not validate any row above the new top_row, we will make sure
5959    * that the row immediately above top_row has been validated. (if we do not
5960    * do this, _pspp_rbtree_find_offset will find the row above top_row, because
5961    * when invalidated that row's height will be zero. and this will mess up
5962    * scrolling).
5963    */
5964   if (area_above == 0)
5965     {
5966       GtkRBTree *tmptree;
5967       GtkRBNode *tmpnode;
5968
5969       _pspp_sheet_view_find_node (tree_view, above_path, &tmptree, &tmpnode);
5970       _pspp_rbtree_prev_full (tmptree, tmpnode, &tmptree, &tmpnode);
5971
5972       if (tmpnode)
5973         {
5974           GtkTreePath *tmppath;
5975           GtkTreeIter tmpiter;
5976
5977           tmppath = _pspp_sheet_view_find_path (tree_view, tmptree, tmpnode);
5978           gtk_tree_model_get_iter (tree_view->priv->model, &tmpiter, tmppath);
5979
5980           if (PSPP_RBNODE_FLAG_SET (tmpnode, PSPP_RBNODE_INVALID) ||
5981               PSPP_RBNODE_FLAG_SET (tmpnode, PSPP_RBNODE_COLUMN_INVALID))
5982             {
5983               _pspp_sheet_view_queue_draw_node (tree_view, tmptree, tmpnode, NULL);
5984               if (validate_row (tree_view, tmptree, tmpnode, &tmpiter, tmppath))
5985                 size_changed = TRUE;
5986             }
5987
5988           gtk_tree_path_free (tmppath);
5989         }
5990     }
5991
5992   /* Now, we walk forwards and backwards, measuring rows. Unfortunately,
5993    * backwards is much slower then forward, as there is no iter_prev function.
5994    * We go forwards first in case we run out of tree.  Then we go backwards to
5995    * fill out the top.
5996    */
5997   while (node && area_below > 0)
5998     {
5999       if (node->children)
6000         {
6001           GtkTreeIter parent = iter;
6002           gboolean has_child;
6003
6004           tree = node->children;
6005           node = tree->root;
6006
6007           g_assert (node != tree->nil);
6008
6009           while (node->left != tree->nil)
6010             node = node->left;
6011           has_child = gtk_tree_model_iter_children (tree_view->priv->model,
6012                                                     &iter,
6013                                                     &parent);
6014           TREE_VIEW_INTERNAL_ASSERT_VOID (has_child);
6015           gtk_tree_path_down (path);
6016         }
6017       else
6018         {
6019           gboolean done = FALSE;
6020           do
6021             {
6022               node = _pspp_rbtree_next (tree, node);
6023               if (node != NULL)
6024                 {
6025                   gboolean has_next = gtk_tree_model_iter_next (tree_view->priv->model, &iter);
6026                   done = TRUE;
6027                   gtk_tree_path_next (path);
6028
6029                   /* Sanity Check! */
6030                   TREE_VIEW_INTERNAL_ASSERT_VOID (has_next);
6031                 }
6032               else
6033                 {
6034                   GtkTreeIter parent_iter = iter;
6035                   gboolean has_parent;
6036
6037                   node = tree->parent_node;
6038                   tree = tree->parent_tree;
6039                   if (tree == NULL)
6040                     break;
6041                   has_parent = gtk_tree_model_iter_parent (tree_view->priv->model,
6042                                                            &iter,
6043                                                            &parent_iter);
6044                   gtk_tree_path_up (path);
6045
6046                   /* Sanity check */
6047                   TREE_VIEW_INTERNAL_ASSERT_VOID (has_parent);
6048                 }
6049             }
6050           while (!done);
6051         }
6052
6053       if (!node)
6054         break;
6055
6056       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) ||
6057           PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
6058         {
6059           _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
6060           if (validate_row (tree_view, tree, node, &iter, path))
6061               size_changed = TRUE;
6062         }
6063
6064       area_below -= ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
6065     }
6066   gtk_tree_path_free (path);
6067
6068   /* If we ran out of tree, and have extra area_below left, we need to add it
6069    * to area_above */
6070   if (area_below > 0)
6071     area_above += area_below;
6072
6073   _pspp_sheet_view_find_node (tree_view, above_path, &tree, &node);
6074
6075   /* We walk backwards */
6076   while (area_above > 0)
6077     {
6078       _pspp_rbtree_prev_full (tree, node, &tree, &node);
6079
6080       /* Always find the new path in the tree.  We cannot just assume
6081        * a gtk_tree_path_prev() is enough here, as there might be children
6082        * in between this node and the previous sibling node.  If this
6083        * appears to be a performance hotspot in profiles, we can look into
6084        * intrigate logic for keeping path, node and iter in sync like
6085        * we do for forward walks.  (Which will be hard because of the lacking
6086        * iter_prev).
6087        */
6088
6089       if (node == NULL)
6090         break;
6091
6092       gtk_tree_path_free (above_path);
6093       above_path = _pspp_sheet_view_find_path (tree_view, tree, node);
6094
6095       gtk_tree_model_get_iter (tree_view->priv->model, &iter, above_path);
6096
6097       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) ||
6098           PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
6099         {
6100           _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
6101           if (validate_row (tree_view, tree, node, &iter, above_path))
6102             size_changed = TRUE;
6103         }
6104       area_above -= ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
6105     }
6106
6107   /* if we scrolled to a path, we need to set the dy here,
6108    * and sync the top row accordingly
6109    */
6110   if (tree_view->priv->scroll_to_path)
6111     {
6112       pspp_sheet_view_set_top_row (tree_view, above_path, -area_above);
6113       pspp_sheet_view_top_row_to_dy (tree_view);
6114
6115       need_redraw = TRUE;
6116     }
6117   else if (tree_view->priv->height <= tree_view->priv->vadjustment->page_size)
6118     {
6119       /* when we are not scrolling, we should never set dy to something
6120        * else than zero. we update top_row to be in sync with dy = 0.
6121        */
6122       gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
6123       pspp_sheet_view_dy_to_top_row (tree_view);
6124     }
6125   else if (tree_view->priv->vadjustment->value + tree_view->priv->vadjustment->page_size > tree_view->priv->height)
6126     {
6127       gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), tree_view->priv->height - tree_view->priv->vadjustment->page_size);
6128       pspp_sheet_view_dy_to_top_row (tree_view);
6129     }
6130   else
6131     pspp_sheet_view_top_row_to_dy (tree_view);
6132
6133   /* update width/height and queue a resize */
6134   if (size_changed)
6135     {
6136       GtkRequisition requisition;
6137
6138       /* We temporarily guess a size, under the assumption that it will be the
6139        * same when we get our next size_allocate.  If we don't do this, we'll be
6140        * in an inconsistent state if we call top_row_to_dy. */
6141
6142       gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
6143       tree_view->priv->hadjustment->upper = MAX (tree_view->priv->hadjustment->upper, (gfloat)requisition.width);
6144       tree_view->priv->vadjustment->upper = MAX (tree_view->priv->vadjustment->upper, (gfloat)requisition.height);
6145       gtk_adjustment_changed (tree_view->priv->hadjustment);
6146       gtk_adjustment_changed (tree_view->priv->vadjustment);
6147       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
6148     }
6149
6150   if (tree_view->priv->scroll_to_path)
6151     {
6152       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
6153       tree_view->priv->scroll_to_path = NULL;
6154     }
6155
6156   if (above_path)
6157     gtk_tree_path_free (above_path);
6158
6159   if (tree_view->priv->scroll_to_column)
6160     {
6161       tree_view->priv->scroll_to_column = NULL;
6162     }
6163   if (need_redraw)
6164     gtk_widget_queue_draw (GTK_WIDGET (tree_view));
6165 }
6166
6167 static void
6168 initialize_fixed_height_mode (PsppSheetView *tree_view)
6169 {
6170   if (!tree_view->priv->tree)
6171     return;
6172
6173   if (tree_view->priv->fixed_height < 0)
6174     {
6175       GtkTreeIter iter;
6176       GtkTreePath *path;
6177
6178       GtkRBTree *tree = NULL;
6179       GtkRBNode *node = NULL;
6180
6181       tree = tree_view->priv->tree;
6182       node = tree->root;
6183
6184       path = _pspp_sheet_view_find_path (tree_view, tree, node);
6185       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
6186
6187       validate_row (tree_view, tree, node, &iter, path);
6188
6189       gtk_tree_path_free (path);
6190
6191       tree_view->priv->fixed_height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
6192     }
6193
6194    _pspp_rbtree_set_fixed_height (tree_view->priv->tree,
6195                                  tree_view->priv->fixed_height, TRUE);
6196 }
6197
6198 /* Our strategy for finding nodes to validate is a little convoluted.  We find
6199  * the left-most uninvalidated node.  We then try walking right, validating
6200  * nodes.  Once we find a valid node, we repeat the previous process of finding
6201  * the first invalid node.
6202  */
6203
6204 static gboolean
6205 do_validate_rows (PsppSheetView *tree_view, gboolean queue_resize)
6206 {
6207   GtkRBTree *tree = NULL;
6208   GtkRBNode *node = NULL;
6209   gboolean validated_area = FALSE;
6210   gint retval = TRUE;
6211   GtkTreePath *path = NULL;
6212   GtkTreeIter iter;
6213   GTimer *timer;
6214   gint i = 0;
6215
6216   gint prev_height = -1;
6217   gboolean fixed_height = TRUE;
6218
6219   g_assert (tree_view);
6220
6221   if (tree_view->priv->tree == NULL)
6222       return FALSE;
6223
6224   if (tree_view->priv->fixed_height_mode)
6225     {
6226       if (tree_view->priv->fixed_height < 0)
6227         initialize_fixed_height_mode (tree_view);
6228
6229       return FALSE;
6230     }
6231
6232   timer = g_timer_new ();
6233   g_timer_start (timer);
6234
6235   do
6236     {
6237       if (! PSPP_RBNODE_FLAG_SET (tree_view->priv->tree->root, PSPP_RBNODE_DESCENDANTS_INVALID))
6238         {
6239           retval = FALSE;
6240           goto done;
6241         }
6242
6243       if (path != NULL)
6244         {
6245           node = _pspp_rbtree_next (tree, node);
6246           if (node != NULL)
6247             {
6248               TREE_VIEW_INTERNAL_ASSERT (gtk_tree_model_iter_next (tree_view->priv->model, &iter), FALSE);
6249               gtk_tree_path_next (path);
6250             }
6251           else
6252             {
6253               gtk_tree_path_free (path);
6254               path = NULL;
6255             }
6256         }
6257
6258       if (path == NULL)
6259         {
6260           tree = tree_view->priv->tree;
6261           node = tree_view->priv->tree->root;
6262
6263           g_assert (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_DESCENDANTS_INVALID));
6264
6265           do
6266             {
6267               if (node->left != tree->nil &&
6268                   PSPP_RBNODE_FLAG_SET (node->left, PSPP_RBNODE_DESCENDANTS_INVALID))
6269                 {
6270                   node = node->left;
6271                 }
6272               else if (node->right != tree->nil &&
6273                        PSPP_RBNODE_FLAG_SET (node->right, PSPP_RBNODE_DESCENDANTS_INVALID))
6274                 {
6275                   node = node->right;
6276                 }
6277               else if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID) ||
6278                        PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_COLUMN_INVALID))
6279                 {
6280                   break;
6281                 }
6282               else if (node->children != NULL)
6283                 {
6284                   tree = node->children;
6285                   node = tree->root;
6286                 }
6287               else
6288                 /* RBTree corruption!  All bad */
6289                 g_assert_not_reached ();
6290             }
6291           while (TRUE);
6292           path = _pspp_sheet_view_find_path (tree_view, tree, node);
6293           gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
6294         }
6295
6296       validated_area = validate_row (tree_view, tree, node, &iter, path) ||
6297                        validated_area;
6298
6299       if (!tree_view->priv->fixed_height_check)
6300         {
6301           gint height;
6302
6303           height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
6304           if (prev_height < 0)
6305             prev_height = height;
6306           else if (prev_height != height)
6307             fixed_height = FALSE;
6308         }
6309
6310       i++;
6311     }
6312   while (g_timer_elapsed (timer, NULL) < PSPP_SHEET_VIEW_TIME_MS_PER_IDLE / 1000.);
6313
6314   if (!tree_view->priv->fixed_height_check)
6315    {
6316      if (fixed_height)
6317        _pspp_rbtree_set_fixed_height (tree_view->priv->tree, prev_height, FALSE);
6318
6319      tree_view->priv->fixed_height_check = 1;
6320    }
6321   
6322  done:
6323   if (validated_area)
6324     {
6325       GtkRequisition requisition;
6326       /* We temporarily guess a size, under the assumption that it will be the
6327        * same when we get our next size_allocate.  If we don't do this, we'll be
6328        * in an inconsistent state when we call top_row_to_dy. */
6329
6330       gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
6331       tree_view->priv->hadjustment->upper = MAX (tree_view->priv->hadjustment->upper, (gfloat)requisition.width);
6332       tree_view->priv->vadjustment->upper = MAX (tree_view->priv->vadjustment->upper, (gfloat)requisition.height);
6333       gtk_adjustment_changed (tree_view->priv->hadjustment);
6334       gtk_adjustment_changed (tree_view->priv->vadjustment);
6335
6336       if (queue_resize)
6337         gtk_widget_queue_resize (GTK_WIDGET (tree_view));
6338     }
6339
6340   if (path) gtk_tree_path_free (path);
6341   g_timer_destroy (timer);
6342
6343   return retval;
6344 }
6345
6346 static gboolean
6347 validate_rows (PsppSheetView *tree_view)
6348 {
6349   gboolean retval;
6350   
6351   retval = do_validate_rows (tree_view, TRUE);
6352   
6353   if (! retval && tree_view->priv->validate_rows_timer)
6354     {
6355       g_source_remove (tree_view->priv->validate_rows_timer);
6356       tree_view->priv->validate_rows_timer = 0;
6357     }
6358
6359   return retval;
6360 }
6361
6362 static gboolean
6363 validate_rows_handler (PsppSheetView *tree_view)
6364 {
6365   gboolean retval;
6366
6367   retval = do_validate_rows (tree_view, TRUE);
6368   if (! retval && tree_view->priv->validate_rows_timer)
6369     {
6370       g_source_remove (tree_view->priv->validate_rows_timer);
6371       tree_view->priv->validate_rows_timer = 0;
6372     }
6373
6374   return retval;
6375 }
6376
6377 static gboolean
6378 do_presize_handler (PsppSheetView *tree_view)
6379 {
6380   if (tree_view->priv->mark_rows_col_dirty)
6381     {
6382       if (tree_view->priv->tree)
6383         _pspp_rbtree_column_invalid (tree_view->priv->tree);
6384       tree_view->priv->mark_rows_col_dirty = FALSE;
6385     }
6386   validate_visible_area (tree_view);
6387   tree_view->priv->presize_handler_timer = 0;
6388
6389   if (tree_view->priv->fixed_height_mode)
6390     {
6391       GtkRequisition requisition;
6392
6393       gtk_widget_size_request (GTK_WIDGET (tree_view), &requisition);
6394
6395       tree_view->priv->hadjustment->upper = MAX (tree_view->priv->hadjustment->upper, (gfloat)requisition.width);
6396       tree_view->priv->vadjustment->upper = MAX (tree_view->priv->vadjustment->upper, (gfloat)requisition.height);
6397       gtk_adjustment_changed (tree_view->priv->hadjustment);
6398       gtk_adjustment_changed (tree_view->priv->vadjustment);
6399       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
6400     }
6401                    
6402   return FALSE;
6403 }
6404
6405 static gboolean
6406 presize_handler_callback (gpointer data)
6407 {
6408   do_presize_handler (PSPP_SHEET_VIEW (data));
6409                    
6410   return FALSE;
6411 }
6412
6413 static void
6414 install_presize_handler (PsppSheetView *tree_view)
6415 {
6416   if (! gtk_widget_get_realized (GTK_WIDGET (tree_view)))
6417     return;
6418
6419   if (! tree_view->priv->presize_handler_timer)
6420     {
6421       tree_view->priv->presize_handler_timer =
6422         gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, presize_handler_callback, tree_view, NULL);
6423     }
6424   if (! tree_view->priv->validate_rows_timer)
6425     {
6426       tree_view->priv->validate_rows_timer =
6427         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_VALIDATE, (GSourceFunc) validate_rows_handler, tree_view, NULL);
6428     }
6429 }
6430
6431 static gboolean
6432 scroll_sync_handler (PsppSheetView *tree_view)
6433 {
6434   if (tree_view->priv->height <= tree_view->priv->vadjustment->page_size)
6435     gtk_adjustment_set_value (GTK_ADJUSTMENT (tree_view->priv->vadjustment), 0);
6436   else if (gtk_tree_row_reference_valid (tree_view->priv->top_row))
6437     pspp_sheet_view_top_row_to_dy (tree_view);
6438   else
6439     pspp_sheet_view_dy_to_top_row (tree_view);
6440
6441   tree_view->priv->scroll_sync_timer = 0;
6442
6443   return FALSE;
6444 }
6445
6446 static void
6447 install_scroll_sync_handler (PsppSheetView *tree_view)
6448 {
6449   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
6450     return;
6451
6452   if (!tree_view->priv->scroll_sync_timer)
6453     {
6454       tree_view->priv->scroll_sync_timer =
6455         gdk_threads_add_idle_full (PSPP_SHEET_VIEW_PRIORITY_SCROLL_SYNC, (GSourceFunc) scroll_sync_handler, tree_view, NULL);
6456     }
6457 }
6458
6459 static void
6460 pspp_sheet_view_set_top_row (PsppSheetView *tree_view,
6461                            GtkTreePath *path,
6462                            gint         offset)
6463 {
6464   gtk_tree_row_reference_free (tree_view->priv->top_row);
6465
6466   if (!path)
6467     {
6468       tree_view->priv->top_row = NULL;
6469       tree_view->priv->top_row_dy = 0;
6470     }
6471   else
6472     {
6473       tree_view->priv->top_row = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
6474       tree_view->priv->top_row_dy = offset;
6475     }
6476 }
6477
6478 /* Always call this iff dy is in the visible range.  If the tree is empty, then
6479  * it's set to be NULL, and top_row_dy is 0;
6480  */
6481 static void
6482 pspp_sheet_view_dy_to_top_row (PsppSheetView *tree_view)
6483 {
6484   gint offset;
6485   GtkTreePath *path;
6486   GtkRBTree *tree;
6487   GtkRBNode *node;
6488
6489   if (tree_view->priv->tree == NULL)
6490     {
6491       pspp_sheet_view_set_top_row (tree_view, NULL, 0);
6492     }
6493   else
6494     {
6495       offset = _pspp_rbtree_find_offset (tree_view->priv->tree,
6496                                         tree_view->priv->dy,
6497                                         &tree, &node);
6498
6499       if (tree == NULL)
6500         {
6501           pspp_sheet_view_set_top_row (tree_view, NULL, 0);
6502         }
6503       else
6504         {
6505           path = _pspp_sheet_view_find_path (tree_view, tree, node);
6506           pspp_sheet_view_set_top_row (tree_view, path, offset);
6507           gtk_tree_path_free (path);
6508         }
6509     }
6510 }
6511
6512 static void
6513 pspp_sheet_view_top_row_to_dy (PsppSheetView *tree_view)
6514 {
6515   GtkTreePath *path;
6516   GtkRBTree *tree;
6517   GtkRBNode *node;
6518   int new_dy;
6519
6520   /* Avoid recursive calls */
6521   if (tree_view->priv->in_top_row_to_dy)
6522     return;
6523
6524   if (tree_view->priv->top_row)
6525     path = gtk_tree_row_reference_get_path (tree_view->priv->top_row);
6526   else
6527     path = NULL;
6528
6529   if (!path)
6530     tree = NULL;
6531   else
6532     _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
6533
6534   if (path)
6535     gtk_tree_path_free (path);
6536
6537   if (tree == NULL)
6538     {
6539       /* keep dy and set new toprow */
6540       gtk_tree_row_reference_free (tree_view->priv->top_row);
6541       tree_view->priv->top_row = NULL;
6542       tree_view->priv->top_row_dy = 0;
6543       /* DO NOT install the idle handler */
6544       pspp_sheet_view_dy_to_top_row (tree_view);
6545       return;
6546     }
6547
6548   if (ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node))
6549       < tree_view->priv->top_row_dy)
6550     {
6551       /* new top row -- do NOT install the idle handler */
6552       pspp_sheet_view_dy_to_top_row (tree_view);
6553       return;
6554     }
6555
6556   new_dy = _pspp_rbtree_node_find_offset (tree, node);
6557   new_dy += tree_view->priv->top_row_dy;
6558
6559   if (new_dy + tree_view->priv->vadjustment->page_size > tree_view->priv->height)
6560     new_dy = tree_view->priv->height - tree_view->priv->vadjustment->page_size;
6561
6562   new_dy = MAX (0, new_dy);
6563
6564   tree_view->priv->in_top_row_to_dy = TRUE;
6565   gtk_adjustment_set_value (tree_view->priv->vadjustment, (gdouble)new_dy);
6566   tree_view->priv->in_top_row_to_dy = FALSE;
6567 }
6568
6569
6570 void
6571 _pspp_sheet_view_install_mark_rows_col_dirty (PsppSheetView *tree_view)
6572 {
6573   tree_view->priv->mark_rows_col_dirty = TRUE;
6574
6575   install_presize_handler (tree_view);
6576 }
6577
6578 /*
6579  * This function works synchronously (due to the while (validate_rows...)
6580  * loop).
6581  *
6582  * There was a check for column_type != PSPP_SHEET_VIEW_COLUMN_AUTOSIZE
6583  * here. You now need to check that yourself.
6584  */
6585 void
6586 _pspp_sheet_view_column_autosize (PsppSheetView *tree_view,
6587                                 PsppSheetViewColumn *column)
6588 {
6589   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
6590   g_return_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column));
6591
6592   _pspp_sheet_view_column_cell_set_dirty (column, FALSE);
6593
6594   do_presize_handler (tree_view);
6595   while (validate_rows (tree_view));
6596
6597   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
6598 }
6599
6600 /* Drag-and-drop */
6601
6602 static void
6603 set_source_row (GdkDragContext *context,
6604                 GtkTreeModel   *model,
6605                 GtkTreePath    *source_row)
6606 {
6607   g_object_set_data_full (G_OBJECT (context),
6608                           "gtk-tree-view-source-row",
6609                           source_row ? gtk_tree_row_reference_new (model, source_row) : NULL,
6610                           (GDestroyNotify) (source_row ? gtk_tree_row_reference_free : NULL));
6611 }
6612
6613 static GtkTreePath*
6614 get_source_row (GdkDragContext *context)
6615 {
6616   GtkTreeRowReference *ref =
6617     g_object_get_data (G_OBJECT (context), "gtk-tree-view-source-row");
6618
6619   if (ref)
6620     return gtk_tree_row_reference_get_path (ref);
6621   else
6622     return NULL;
6623 }
6624
6625 typedef struct
6626 {
6627   GtkTreeRowReference *dest_row;
6628   guint                path_down_mode   : 1;
6629   guint                empty_view_drop  : 1;
6630   guint                drop_append_mode : 1;
6631 }
6632 DestRow;
6633
6634 static void
6635 dest_row_free (gpointer data)
6636 {
6637   DestRow *dr = (DestRow *)data;
6638
6639   gtk_tree_row_reference_free (dr->dest_row);
6640   g_slice_free (DestRow, dr);
6641 }
6642
6643 static void
6644 set_dest_row (GdkDragContext *context,
6645               GtkTreeModel   *model,
6646               GtkTreePath    *dest_row,
6647               gboolean        path_down_mode,
6648               gboolean        empty_view_drop,
6649               gboolean        drop_append_mode)
6650 {
6651   DestRow *dr;
6652
6653   if (!dest_row)
6654     {
6655       g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
6656                               NULL, NULL);
6657       return;
6658     }
6659
6660   dr = g_slice_new (DestRow);
6661
6662   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6663   dr->path_down_mode = path_down_mode != FALSE;
6664   dr->empty_view_drop = empty_view_drop != FALSE;
6665   dr->drop_append_mode = drop_append_mode != FALSE;
6666
6667   g_object_set_data_full (G_OBJECT (context), "gtk-tree-view-dest-row",
6668                           dr, (GDestroyNotify) dest_row_free);
6669 }
6670
6671 static GtkTreePath*
6672 get_dest_row (GdkDragContext *context,
6673               gboolean       *path_down_mode)
6674 {
6675   DestRow *dr =
6676     g_object_get_data (G_OBJECT (context), "gtk-tree-view-dest-row");
6677
6678   if (dr)
6679     {
6680       GtkTreePath *path = NULL;
6681
6682       if (path_down_mode)
6683         *path_down_mode = dr->path_down_mode;
6684
6685       if (dr->dest_row)
6686         path = gtk_tree_row_reference_get_path (dr->dest_row);
6687       else if (dr->empty_view_drop)
6688         path = gtk_tree_path_new_from_indices (0, -1);
6689       else
6690         path = NULL;
6691
6692       if (path && dr->drop_append_mode)
6693         gtk_tree_path_next (path);
6694
6695       return path;
6696     }
6697   else
6698     return NULL;
6699 }
6700
6701 /* Get/set whether drag_motion requested the drag data and
6702  * drag_data_received should thus not actually insert the data,
6703  * since the data doesn't result from a drop.
6704  */
6705 static void
6706 set_status_pending (GdkDragContext *context,
6707                     GdkDragAction   suggested_action)
6708 {
6709   g_object_set_data (G_OBJECT (context),
6710                      "gtk-tree-view-status-pending",
6711                      GINT_TO_POINTER (suggested_action));
6712 }
6713
6714 static GdkDragAction
6715 get_status_pending (GdkDragContext *context)
6716 {
6717   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
6718                                              "gtk-tree-view-status-pending"));
6719 }
6720
6721 static TreeViewDragInfo*
6722 get_info (PsppSheetView *tree_view)
6723 {
6724   return g_object_get_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info");
6725 }
6726
6727 static void
6728 destroy_info (TreeViewDragInfo *di)
6729 {
6730   g_slice_free (TreeViewDragInfo, di);
6731 }
6732
6733 static TreeViewDragInfo*
6734 ensure_info (PsppSheetView *tree_view)
6735 {
6736   TreeViewDragInfo *di;
6737
6738   di = get_info (tree_view);
6739
6740   if (di == NULL)
6741     {
6742       di = g_slice_new0 (TreeViewDragInfo);
6743
6744       g_object_set_data_full (G_OBJECT (tree_view),
6745                               "gtk-tree-view-drag-info",
6746                               di,
6747                               (GDestroyNotify) destroy_info);
6748     }
6749
6750   return di;
6751 }
6752
6753 static void
6754 remove_info (PsppSheetView *tree_view)
6755 {
6756   g_object_set_data (G_OBJECT (tree_view), "gtk-tree-view-drag-info", NULL);
6757 }
6758
6759 #if 0
6760 static gint
6761 drag_scan_timeout (gpointer data)
6762 {
6763   PsppSheetView *tree_view;
6764   gint x, y;
6765   GdkModifierType state;
6766   GtkTreePath *path = NULL;
6767   PsppSheetViewColumn *column = NULL;
6768   GdkRectangle visible_rect;
6769
6770   GDK_THREADS_ENTER ();
6771
6772   tree_view = PSPP_SHEET_VIEW (data);
6773
6774   gdk_window_get_pointer (tree_view->priv->bin_window,
6775                           &x, &y, &state);
6776
6777   pspp_sheet_view_get_visible_rect (tree_view, &visible_rect);
6778
6779   /* See if we are near the edge. */
6780   if ((x - visible_rect.x) < SCROLL_EDGE_SIZE ||
6781       (visible_rect.x + visible_rect.width - x) < SCROLL_EDGE_SIZE ||
6782       (y - visible_rect.y) < SCROLL_EDGE_SIZE ||
6783       (visible_rect.y + visible_rect.height - y) < SCROLL_EDGE_SIZE)
6784     {
6785       pspp_sheet_view_get_path_at_pos (tree_view,
6786                                      tree_view->priv->bin_window,
6787                                      x, y,
6788                                      &path,
6789                                      &column,
6790                                      NULL,
6791                                      NULL);
6792
6793       if (path != NULL)
6794         {
6795           pspp_sheet_view_scroll_to_cell (tree_view,
6796                                         path,
6797                                         column,
6798                                         TRUE,
6799                                         0.5, 0.5);
6800
6801           gtk_tree_path_free (path);
6802         }
6803     }
6804
6805   GDK_THREADS_LEAVE ();
6806
6807   return TRUE;
6808 }
6809 #endif /* 0 */
6810
6811 static void
6812 add_scroll_timeout (PsppSheetView *tree_view)
6813 {
6814   if (tree_view->priv->scroll_timeout == 0)
6815     {
6816       tree_view->priv->scroll_timeout =
6817         gdk_threads_add_timeout (150, scroll_row_timeout, tree_view);
6818     }
6819 }
6820
6821 static void
6822 remove_scroll_timeout (PsppSheetView *tree_view)
6823 {
6824   if (tree_view->priv->scroll_timeout != 0)
6825     {
6826       g_source_remove (tree_view->priv->scroll_timeout);
6827       tree_view->priv->scroll_timeout = 0;
6828     }
6829 }
6830
6831 static gboolean
6832 check_model_dnd (GtkTreeModel *model,
6833                  GType         required_iface,
6834                  const gchar  *signal)
6835 {
6836   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6837     {
6838       g_warning ("You must override the default '%s' handler "
6839                  "on PsppSheetView when using models that don't support "
6840                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
6841                  "is to connect to '%s' and call "
6842                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
6843                  "the default handler from running. Look at the source code "
6844                  "for the default handler in gtktreeview.c to get an idea what "
6845                  "your handler should do. (gtktreeview.c is in the GTK source "
6846                  "code.) If you're using GTK from a language other than C, "
6847                  "there may be a more natural way to override default handlers, e.g. via derivation.",
6848                  signal, g_type_name (required_iface), signal);
6849       return FALSE;
6850     }
6851   else
6852     return TRUE;
6853 }
6854
6855 static void
6856 remove_open_timeout (PsppSheetView *tree_view)
6857 {
6858   if (tree_view->priv->open_dest_timeout != 0)
6859     {
6860       g_source_remove (tree_view->priv->open_dest_timeout);
6861       tree_view->priv->open_dest_timeout = 0;
6862     }
6863 }
6864
6865
6866 static gint
6867 open_row_timeout (gpointer data)
6868 {
6869   PsppSheetView *tree_view = data;
6870   GtkTreePath *dest_path = NULL;
6871   PsppSheetViewDropPosition pos;
6872   gboolean result = FALSE;
6873
6874   pspp_sheet_view_get_drag_dest_row (tree_view,
6875                                    &dest_path,
6876                                    &pos);
6877
6878   if (dest_path &&
6879       (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER ||
6880        pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE))
6881     {
6882       pspp_sheet_view_expand_row (tree_view, dest_path, FALSE);
6883       tree_view->priv->open_dest_timeout = 0;
6884
6885       gtk_tree_path_free (dest_path);
6886     }
6887   else
6888     {
6889       if (dest_path)
6890         gtk_tree_path_free (dest_path);
6891
6892       result = TRUE;
6893     }
6894
6895   return result;
6896 }
6897
6898 static gboolean
6899 scroll_row_timeout (gpointer data)
6900 {
6901   PsppSheetView *tree_view = data;
6902
6903   pspp_sheet_view_vertical_autoscroll (tree_view);
6904
6905   if (tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
6906     pspp_sheet_view_update_rubber_band (tree_view);
6907
6908   return TRUE;
6909 }
6910
6911 /* Returns TRUE if event should not be propagated to parent widgets */
6912 static gboolean
6913 set_destination_row (PsppSheetView    *tree_view,
6914                      GdkDragContext *context,
6915                      /* coordinates relative to the widget */
6916                      gint            x,
6917                      gint            y,
6918                      GdkDragAction  *suggested_action,
6919                      GdkAtom        *target)
6920 {
6921   GtkTreePath *path = NULL;
6922   PsppSheetViewDropPosition pos;
6923   PsppSheetViewDropPosition old_pos;
6924   TreeViewDragInfo *di;
6925   GtkWidget *widget;
6926   GtkTreePath *old_dest_path = NULL;
6927   gboolean can_drop = FALSE;
6928
6929   *suggested_action = 0;
6930   *target = GDK_NONE;
6931
6932   widget = GTK_WIDGET (tree_view);
6933
6934   di = get_info (tree_view);
6935
6936   if (di == NULL || y - TREE_VIEW_HEADER_HEIGHT (tree_view) < 0)
6937     {
6938       /* someone unset us as a drag dest, note that if
6939        * we return FALSE drag_leave isn't called
6940        */
6941
6942       pspp_sheet_view_set_drag_dest_row (tree_view,
6943                                        NULL,
6944                                        PSPP_SHEET_VIEW_DROP_BEFORE);
6945
6946       remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
6947       remove_open_timeout (PSPP_SHEET_VIEW (widget));
6948
6949       return FALSE; /* no longer a drop site */
6950     }
6951
6952   *target = gtk_drag_dest_find_target (widget, context,
6953                                        gtk_drag_dest_get_target_list (widget));
6954   if (*target == GDK_NONE)
6955     {
6956       return FALSE;
6957     }
6958
6959   if (!pspp_sheet_view_get_dest_row_at_pos (tree_view,
6960                                           x, y,
6961                                           &path,
6962                                           &pos))
6963     {
6964       gint n_children;
6965       GtkTreeModel *model;
6966
6967       remove_open_timeout (tree_view);
6968
6969       /* the row got dropped on empty space, let's setup a special case
6970        */
6971
6972       if (path)
6973         gtk_tree_path_free (path);
6974
6975       model = pspp_sheet_view_get_model (tree_view);
6976
6977       n_children = gtk_tree_model_iter_n_children (model, NULL);
6978       if (n_children)
6979         {
6980           pos = PSPP_SHEET_VIEW_DROP_AFTER;
6981           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6982         }
6983       else
6984         {
6985           pos = PSPP_SHEET_VIEW_DROP_BEFORE;
6986           path = gtk_tree_path_new_from_indices (0, -1);
6987         }
6988
6989       can_drop = TRUE;
6990
6991       goto out;
6992     }
6993
6994   g_assert (path);
6995
6996   /* If we left the current row's "open" zone, unset the timeout for
6997    * opening the row
6998    */
6999   pspp_sheet_view_get_drag_dest_row (tree_view,
7000                                    &old_dest_path,
7001                                    &old_pos);
7002
7003   if (old_dest_path &&
7004       (gtk_tree_path_compare (path, old_dest_path) != 0 ||
7005        !(pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER ||
7006          pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE)))
7007     remove_open_timeout (tree_view);
7008
7009   if (old_dest_path)
7010     gtk_tree_path_free (old_dest_path);
7011
7012   if (TRUE /* FIXME if the location droppable predicate */)
7013     {
7014       can_drop = TRUE;
7015     }
7016
7017 out:
7018   if (can_drop)
7019     {
7020       GtkWidget *source_widget;
7021
7022       *suggested_action = context->suggested_action;
7023       source_widget = gtk_drag_get_source_widget (context);
7024
7025       if (source_widget == widget)
7026         {
7027           /* Default to MOVE, unless the user has
7028            * pressed ctrl or shift to affect available actions
7029            */
7030           if ((context->actions & GDK_ACTION_MOVE) != 0)
7031             *suggested_action = GDK_ACTION_MOVE;
7032         }
7033
7034       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
7035                                        path, pos);
7036     }
7037   else
7038     {
7039       /* can't drop here */
7040       remove_open_timeout (tree_view);
7041
7042       pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
7043                                        NULL,
7044                                        PSPP_SHEET_VIEW_DROP_BEFORE);
7045     }
7046
7047   if (path)
7048     gtk_tree_path_free (path);
7049
7050   return TRUE;
7051 }
7052
7053 static GtkTreePath*
7054 get_logical_dest_row (PsppSheetView *tree_view,
7055                       gboolean    *path_down_mode,
7056                       gboolean    *drop_append_mode)
7057 {
7058   /* adjust path to point to the row the drop goes in front of */
7059   GtkTreePath *path = NULL;
7060   PsppSheetViewDropPosition pos;
7061
7062   g_return_val_if_fail (path_down_mode != NULL, NULL);
7063   g_return_val_if_fail (drop_append_mode != NULL, NULL);
7064
7065   *path_down_mode = FALSE;
7066   *drop_append_mode = 0;
7067
7068   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
7069
7070   if (path == NULL)
7071     return NULL;
7072
7073   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE)
7074     ; /* do nothing */
7075   else if (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE ||
7076            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER)
7077     *path_down_mode = TRUE;
7078   else
7079     {
7080       GtkTreeIter iter;
7081       GtkTreeModel *model = pspp_sheet_view_get_model (tree_view);
7082
7083       g_assert (pos == PSPP_SHEET_VIEW_DROP_AFTER);
7084
7085       if (!gtk_tree_model_get_iter (model, &iter, path) ||
7086           !gtk_tree_model_iter_next (model, &iter))
7087         *drop_append_mode = 1;
7088       else
7089         {
7090           *drop_append_mode = 0;
7091           gtk_tree_path_next (path);
7092         }
7093     }
7094
7095   return path;
7096 }
7097
7098 static gboolean
7099 pspp_sheet_view_maybe_begin_dragging_row (PsppSheetView      *tree_view,
7100                                         GdkEventMotion   *event)
7101 {
7102   GtkWidget *widget = GTK_WIDGET (tree_view);
7103   GdkDragContext *context;
7104   TreeViewDragInfo *di;
7105   GtkTreePath *path = NULL;
7106   gint button;
7107   gint cell_x, cell_y;
7108   GtkTreeModel *model;
7109   gboolean retval = FALSE;
7110
7111   di = get_info (tree_view);
7112
7113   if (di == NULL || !di->source_set)
7114     goto out;
7115
7116   if (tree_view->priv->pressed_button < 0)
7117     goto out;
7118
7119   if (!gtk_drag_check_threshold (widget,
7120                                  tree_view->priv->press_start_x,
7121                                  tree_view->priv->press_start_y,
7122                                  event->x, event->y))
7123     goto out;
7124
7125   model = pspp_sheet_view_get_model (tree_view);
7126
7127   if (model == NULL)
7128     goto out;
7129
7130   button = tree_view->priv->pressed_button;
7131   tree_view->priv->pressed_button = -1;
7132
7133   pspp_sheet_view_get_path_at_pos (tree_view,
7134                                  tree_view->priv->press_start_x,
7135                                  tree_view->priv->press_start_y,
7136                                  &path,
7137                                  NULL,
7138                                  &cell_x,
7139                                  &cell_y);
7140
7141   if (path == NULL)
7142     goto out;
7143
7144   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
7145       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
7146                                            path))
7147     goto out;
7148
7149   if (!(GDK_BUTTON1_MASK << (button - 1) & di->start_button_mask))
7150     goto out;
7151
7152   /* Now we can begin the drag */
7153
7154   retval = TRUE;
7155
7156   context = gtk_drag_begin (widget,
7157                             gtk_drag_source_get_target_list (widget),
7158                             di->source_actions,
7159                             button,
7160                             (GdkEvent*)event);
7161
7162   set_source_row (context, model, path);
7163
7164  out:
7165   if (path)
7166     gtk_tree_path_free (path);
7167
7168   return retval;
7169 }
7170
7171
7172 static void
7173 pspp_sheet_view_drag_begin (GtkWidget      *widget,
7174                           GdkDragContext *context)
7175 {
7176   PsppSheetView *tree_view;
7177   GtkTreePath *path = NULL;
7178   gint cell_x, cell_y;
7179   GdkPixmap *row_pix;
7180   TreeViewDragInfo *di;
7181
7182   tree_view = PSPP_SHEET_VIEW (widget);
7183
7184   /* if the user uses a custom DND source impl, we don't set the icon here */
7185   di = get_info (tree_view);
7186
7187   if (di == NULL || !di->source_set)
7188     return;
7189
7190   pspp_sheet_view_get_path_at_pos (tree_view,
7191                                  tree_view->priv->press_start_x,
7192                                  tree_view->priv->press_start_y,
7193                                  &path,
7194                                  NULL,
7195                                  &cell_x,
7196                                  &cell_y);
7197
7198   g_return_if_fail (path != NULL);
7199
7200   row_pix = pspp_sheet_view_create_row_drag_icon (tree_view,
7201                                                 path);
7202
7203   gtk_drag_set_icon_pixmap (context,
7204                             gdk_drawable_get_colormap (row_pix),
7205                             row_pix,
7206                             NULL,
7207                             /* the + 1 is for the black border in the icon */
7208                             tree_view->priv->press_start_x + 1,
7209                             cell_y + 1);
7210
7211   g_object_unref (row_pix);
7212   gtk_tree_path_free (path);
7213 }
7214
7215 static void
7216 pspp_sheet_view_drag_end (GtkWidget      *widget,
7217                         GdkDragContext *context)
7218 {
7219   /* do nothing */
7220 }
7221
7222 /* Default signal implementations for the drag signals */
7223 static void
7224 pspp_sheet_view_drag_data_get (GtkWidget        *widget,
7225                              GdkDragContext   *context,
7226                              GtkSelectionData *selection_data,
7227                              guint             info,
7228                              guint             time)
7229 {
7230   PsppSheetView *tree_view;
7231   GtkTreeModel *model;
7232   TreeViewDragInfo *di;
7233   GtkTreePath *source_row;
7234
7235   tree_view = PSPP_SHEET_VIEW (widget);
7236
7237   model = pspp_sheet_view_get_model (tree_view);
7238
7239   if (model == NULL)
7240     return;
7241
7242   di = get_info (PSPP_SHEET_VIEW (widget));
7243
7244   if (di == NULL)
7245     return;
7246
7247   source_row = get_source_row (context);
7248
7249   if (source_row == NULL)
7250     return;
7251
7252   /* We can implement the GTK_TREE_MODEL_ROW target generically for
7253    * any model; for DragSource models there are some other targets
7254    * we also support.
7255    */
7256
7257   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
7258       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
7259                                           source_row,
7260                                           selection_data))
7261     goto done;
7262
7263   /* If drag_data_get does nothing, try providing row data. */
7264   if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
7265     {
7266       gtk_tree_set_row_drag_data (selection_data,
7267                                   model,
7268                                   source_row);
7269     }
7270
7271  done:
7272   gtk_tree_path_free (source_row);
7273 }
7274
7275
7276 static void
7277 pspp_sheet_view_drag_data_delete (GtkWidget      *widget,
7278                                 GdkDragContext *context)
7279 {
7280   TreeViewDragInfo *di;
7281   GtkTreeModel *model;
7282   PsppSheetView *tree_view;
7283   GtkTreePath *source_row;
7284
7285   tree_view = PSPP_SHEET_VIEW (widget);
7286   model = pspp_sheet_view_get_model (tree_view);
7287
7288   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
7289     return;
7290
7291   di = get_info (tree_view);
7292
7293   if (di == NULL)
7294     return;
7295
7296   source_row = get_source_row (context);
7297
7298   if (source_row == NULL)
7299     return;
7300
7301   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
7302                                          source_row);
7303
7304   gtk_tree_path_free (source_row);
7305
7306   set_source_row (context, NULL, NULL);
7307 }
7308
7309 static void
7310 pspp_sheet_view_drag_leave (GtkWidget      *widget,
7311                           GdkDragContext *context,
7312                           guint             time)
7313 {
7314   /* unset any highlight row */
7315   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
7316                                    NULL,
7317                                    PSPP_SHEET_VIEW_DROP_BEFORE);
7318
7319   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
7320   remove_open_timeout (PSPP_SHEET_VIEW (widget));
7321 }
7322
7323
7324 static gboolean
7325 pspp_sheet_view_drag_motion (GtkWidget        *widget,
7326                            GdkDragContext   *context,
7327                            /* coordinates relative to the widget */
7328                            gint              x,
7329                            gint              y,
7330                            guint             time)
7331 {
7332   gboolean empty;
7333   GtkTreePath *path = NULL;
7334   PsppSheetViewDropPosition pos;
7335   PsppSheetView *tree_view;
7336   GdkDragAction suggested_action = 0;
7337   GdkAtom target;
7338
7339   tree_view = PSPP_SHEET_VIEW (widget);
7340
7341   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
7342     return FALSE;
7343
7344   pspp_sheet_view_get_drag_dest_row (tree_view, &path, &pos);
7345
7346   /* we only know this *after* set_desination_row */
7347   empty = tree_view->priv->empty_view_drop;
7348
7349   if (path == NULL && !empty)
7350     {
7351       /* Can't drop here. */
7352       gdk_drag_status (context, 0, time);
7353     }
7354   else
7355     {
7356       if (tree_view->priv->open_dest_timeout == 0 &&
7357           (pos == PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER ||
7358            pos == PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE))
7359         {
7360           tree_view->priv->open_dest_timeout =
7361             gdk_threads_add_timeout (AUTO_EXPAND_TIMEOUT, open_row_timeout, tree_view);
7362         }
7363       else
7364         {
7365           add_scroll_timeout (tree_view);
7366         }
7367
7368       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
7369         {
7370           /* Request data so we can use the source row when
7371            * determining whether to accept the drop
7372            */
7373           set_status_pending (context, suggested_action);
7374           gtk_drag_get_data (widget, context, target, time);
7375         }
7376       else
7377         {
7378           set_status_pending (context, 0);
7379           gdk_drag_status (context, suggested_action, time);
7380         }
7381     }
7382
7383   if (path)
7384     gtk_tree_path_free (path);
7385
7386   return TRUE;
7387 }
7388
7389
7390 static gboolean
7391 pspp_sheet_view_drag_drop (GtkWidget        *widget,
7392                          GdkDragContext   *context,
7393                          /* coordinates relative to the widget */
7394                          gint              x,
7395                          gint              y,
7396                          guint             time)
7397 {
7398   PsppSheetView *tree_view;
7399   GtkTreePath *path;
7400   GdkDragAction suggested_action = 0;
7401   GdkAtom target = GDK_NONE;
7402   TreeViewDragInfo *di;
7403   GtkTreeModel *model;
7404   gboolean path_down_mode;
7405   gboolean drop_append_mode;
7406
7407   tree_view = PSPP_SHEET_VIEW (widget);
7408
7409   model = pspp_sheet_view_get_model (tree_view);
7410
7411   remove_scroll_timeout (PSPP_SHEET_VIEW (widget));
7412   remove_open_timeout (PSPP_SHEET_VIEW (widget));
7413
7414   di = get_info (tree_view);
7415
7416   if (di == NULL)
7417     return FALSE;
7418
7419   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
7420     return FALSE;
7421
7422   if (!set_destination_row (tree_view, context, x, y, &suggested_action, &target))
7423     return FALSE;
7424
7425   path = get_logical_dest_row (tree_view, &path_down_mode, &drop_append_mode);
7426
7427   if (target != GDK_NONE && path != NULL)
7428     {
7429       /* in case a motion had requested drag data, change things so we
7430        * treat drag data receives as a drop.
7431        */
7432       set_status_pending (context, 0);
7433       set_dest_row (context, model, path,
7434                     path_down_mode, tree_view->priv->empty_view_drop,
7435                     drop_append_mode);
7436     }
7437
7438   if (path)
7439     gtk_tree_path_free (path);
7440
7441   /* Unset this thing */
7442   pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
7443                                    NULL,
7444                                    PSPP_SHEET_VIEW_DROP_BEFORE);
7445
7446   if (target != GDK_NONE)
7447     {
7448       gtk_drag_get_data (widget, context, target, time);
7449       return TRUE;
7450     }
7451   else
7452     return FALSE;
7453 }
7454
7455 static void
7456 pspp_sheet_view_drag_data_received (GtkWidget        *widget,
7457                                   GdkDragContext   *context,
7458                                   /* coordinates relative to the widget */
7459                                   gint              x,
7460                                   gint              y,
7461                                   GtkSelectionData *selection_data,
7462                                   guint             info,
7463                                   guint             time)
7464 {
7465   GtkTreePath *path;
7466   TreeViewDragInfo *di;
7467   gboolean accepted = FALSE;
7468   GtkTreeModel *model;
7469   PsppSheetView *tree_view;
7470   GtkTreePath *dest_row;
7471   GdkDragAction suggested_action;
7472   gboolean path_down_mode;
7473   gboolean drop_append_mode;
7474
7475   tree_view = PSPP_SHEET_VIEW (widget);
7476
7477   model = pspp_sheet_view_get_model (tree_view);
7478
7479   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
7480     return;
7481
7482   di = get_info (tree_view);
7483
7484   if (di == NULL)
7485     return;
7486
7487   suggested_action = get_status_pending (context);
7488
7489   if (suggested_action)
7490     {
7491       /* We are getting this data due to a request in drag_motion,
7492        * rather than due to a request in drag_drop, so we are just
7493        * supposed to call drag_status, not actually paste in the
7494        * data.
7495        */
7496       path = get_logical_dest_row (tree_view, &path_down_mode,
7497                                    &drop_append_mode);
7498
7499       if (path == NULL)
7500         suggested_action = 0;
7501       else if (path_down_mode)
7502         gtk_tree_path_down (path);
7503
7504       if (suggested_action)
7505         {
7506           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
7507                                                      path,
7508                                                      selection_data))
7509             {
7510               if (path_down_mode)
7511                 {
7512                   path_down_mode = FALSE;
7513                   gtk_tree_path_up (path);
7514
7515                   if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
7516                                                              path,
7517                                                              selection_data))
7518                     suggested_action = 0;
7519                 }
7520               else
7521                 suggested_action = 0;
7522             }
7523         }
7524
7525       gdk_drag_status (context, suggested_action, time);
7526
7527       if (path)
7528         gtk_tree_path_free (path);
7529
7530       /* If you can't drop, remove user drop indicator until the next motion */
7531       if (suggested_action == 0)
7532         pspp_sheet_view_set_drag_dest_row (PSPP_SHEET_VIEW (widget),
7533                                          NULL,
7534                                          PSPP_SHEET_VIEW_DROP_BEFORE);
7535
7536       return;
7537     }
7538
7539   dest_row = get_dest_row (context, &path_down_mode);
7540
7541   if (dest_row == NULL)
7542     return;
7543
7544   if (selection_data->length >= 0)
7545     {
7546       if (path_down_mode)
7547         {
7548           gtk_tree_path_down (dest_row);
7549           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
7550                                                      dest_row, selection_data))
7551             gtk_tree_path_up (dest_row);
7552         }
7553     }
7554
7555   if (selection_data->length >= 0)
7556     {
7557       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
7558                                                  dest_row,
7559                                                  selection_data))
7560         accepted = TRUE;
7561     }
7562
7563   gtk_drag_finish (context,
7564                    accepted,
7565                    (context->action == GDK_ACTION_MOVE),
7566                    time);
7567
7568   if (gtk_tree_path_get_depth (dest_row) == 1
7569       && gtk_tree_path_get_indices (dest_row)[0] == 0)
7570     {
7571       /* special special case drag to "0", scroll to first item */
7572       if (!tree_view->priv->scroll_to_path)
7573         pspp_sheet_view_scroll_to_cell (tree_view, dest_row, NULL, FALSE, 0.0, 0.0);
7574     }
7575
7576   gtk_tree_path_free (dest_row);
7577
7578   /* drop dest_row */
7579   set_dest_row (context, NULL, NULL, FALSE, FALSE, FALSE);
7580 }
7581
7582
7583
7584 /* GtkContainer Methods
7585  */
7586
7587
7588 static void
7589 pspp_sheet_view_remove (GtkContainer *container,
7590                       GtkWidget    *widget)
7591 {
7592   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
7593   PsppSheetViewChild *child = NULL;
7594   GList *tmp_list;
7595
7596   tmp_list = tree_view->priv->children;
7597   while (tmp_list)
7598     {
7599       child = tmp_list->data;
7600       if (child->widget == widget)
7601         {
7602           gtk_widget_unparent (widget);
7603
7604           tree_view->priv->children = g_list_remove_link (tree_view->priv->children, tmp_list);
7605           g_list_free_1 (tmp_list);
7606           g_slice_free (PsppSheetViewChild, child);
7607           return;
7608         }
7609
7610       tmp_list = tmp_list->next;
7611     }
7612
7613   tmp_list = tree_view->priv->columns;
7614
7615   while (tmp_list)
7616     {
7617       PsppSheetViewColumn *column;
7618       column = tmp_list->data;
7619       if (column->button == widget)
7620         {
7621           gtk_widget_unparent (widget);
7622           return;
7623         }
7624       tmp_list = tmp_list->next;
7625     }
7626 }
7627
7628 static void
7629 pspp_sheet_view_forall (GtkContainer *container,
7630                       gboolean      include_internals,
7631                       GtkCallback   callback,
7632                       gpointer      callback_data)
7633 {
7634   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
7635   PsppSheetViewChild *child = NULL;
7636   PsppSheetViewColumn *column;
7637   GList *tmp_list;
7638
7639   tmp_list = tree_view->priv->children;
7640   while (tmp_list)
7641     {
7642       child = tmp_list->data;
7643       tmp_list = tmp_list->next;
7644
7645       (* callback) (child->widget, callback_data);
7646     }
7647   if (include_internals == FALSE)
7648     return;
7649
7650   for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
7651     {
7652       column = tmp_list->data;
7653
7654       if (column->button)
7655         (* callback) (column->button, callback_data);
7656     }
7657 }
7658
7659 /* Returns TRUE if the treeview contains no "special" (editable or activatable)
7660  * cells. If so we draw one big row-spanning focus rectangle.
7661  */
7662 static gboolean
7663 pspp_sheet_view_has_special_cell (PsppSheetView *tree_view)
7664 {
7665   GList *list;
7666
7667   for (list = tree_view->priv->columns; list; list = list->next)
7668     {
7669       if (!((PsppSheetViewColumn *)list->data)->visible)
7670         continue;
7671       if (_pspp_sheet_view_column_count_special_cells (list->data))
7672         return TRUE;
7673     }
7674
7675   return FALSE;
7676 }
7677
7678 static void
7679 column_sizing_notify (GObject    *object,
7680                       GParamSpec *pspec,
7681                       gpointer    data)
7682 {
7683   PsppSheetViewColumn *c = PSPP_SHEET_VIEW_COLUMN (object);
7684
7685   if (pspp_sheet_view_column_get_sizing (c) != PSPP_SHEET_VIEW_COLUMN_FIXED)
7686     /* disable fixed height mode */
7687     g_object_set (data, "fixed-height-mode", FALSE, NULL);
7688 }
7689
7690 /**
7691  * pspp_sheet_view_set_fixed_height_mode:
7692  * @tree_view: a #PsppSheetView 
7693  * @enable: %TRUE to enable fixed height mode
7694  * 
7695  * Enables or disables the fixed height mode of @tree_view. 
7696  * Fixed height mode speeds up #PsppSheetView by assuming that all 
7697  * rows have the same height. 
7698  * Only enable this option if all rows are the same height and all
7699  * columns are of type %PSPP_SHEET_VIEW_COLUMN_FIXED.
7700  *
7701  * Since: 2.6 
7702  **/
7703 void
7704 pspp_sheet_view_set_fixed_height_mode (PsppSheetView *tree_view,
7705                                      gboolean     enable)
7706 {
7707   GList *l;
7708   
7709   enable = enable != FALSE;
7710
7711   if (enable == tree_view->priv->fixed_height_mode)
7712     return;
7713
7714   if (!enable)
7715     {
7716       tree_view->priv->fixed_height_mode = 0;
7717       tree_view->priv->fixed_height = -1;
7718
7719       /* force a revalidation */
7720       install_presize_handler (tree_view);
7721     }
7722   else 
7723     {
7724       /* make sure all columns are of type FIXED */
7725       for (l = tree_view->priv->columns; l; l = l->next)
7726         {
7727           PsppSheetViewColumn *c = l->data;
7728           
7729           g_return_if_fail (pspp_sheet_view_column_get_sizing (c) == PSPP_SHEET_VIEW_COLUMN_FIXED);
7730         }
7731       
7732       /* yes, we really have to do this is in a separate loop */
7733       for (l = tree_view->priv->columns; l; l = l->next)
7734         g_signal_connect (l->data, "notify::sizing",
7735                           G_CALLBACK (column_sizing_notify), tree_view);
7736       
7737       tree_view->priv->fixed_height_mode = 1;
7738       tree_view->priv->fixed_height = -1;
7739       
7740       if (tree_view->priv->tree)
7741         initialize_fixed_height_mode (tree_view);
7742     }
7743
7744   g_object_notify (G_OBJECT (tree_view), "fixed-height-mode");
7745 }
7746
7747 /**
7748  * pspp_sheet_view_get_fixed_height_mode:
7749  * @tree_view: a #PsppSheetView
7750  * 
7751  * Returns whether fixed height mode is turned on for @tree_view.
7752  * 
7753  * Return value: %TRUE if @tree_view is in fixed height mode
7754  * 
7755  * Since: 2.6
7756  **/
7757 gboolean
7758 pspp_sheet_view_get_fixed_height_mode (PsppSheetView *tree_view)
7759 {
7760   return tree_view->priv->fixed_height_mode;
7761 }
7762
7763 /* Returns TRUE if the focus is within the headers, after the focus operation is
7764  * done
7765  */
7766 static gboolean
7767 pspp_sheet_view_header_focus (PsppSheetView      *tree_view,
7768                             GtkDirectionType  dir,
7769                             gboolean          clamp_column_visible)
7770 {
7771   GtkWidget *focus_child;
7772
7773   GList *last_column, *first_column;
7774   GList *tmp_list;
7775   gboolean rtl;
7776
7777   if (! PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE))
7778     return FALSE;
7779
7780   focus_child = GTK_CONTAINER (tree_view)->focus_child;
7781
7782   first_column = tree_view->priv->columns;
7783   while (first_column)
7784     {
7785       if (gtk_widget_get_can_focus (PSPP_SHEET_VIEW_COLUMN (first_column->data)->button) &&
7786           PSPP_SHEET_VIEW_COLUMN (first_column->data)->visible &&
7787           (PSPP_SHEET_VIEW_COLUMN (first_column->data)->clickable ||
7788            PSPP_SHEET_VIEW_COLUMN (first_column->data)->reorderable))
7789         break;
7790       first_column = first_column->next;
7791     }
7792
7793   /* No headers are visible, or are focusable.  We can't focus in or out.
7794    */
7795   if (first_column == NULL)
7796     return FALSE;
7797
7798   last_column = g_list_last (tree_view->priv->columns);
7799   while (last_column)
7800     {
7801       if (gtk_widget_get_can_focus (PSPP_SHEET_VIEW_COLUMN (last_column->data)->button) &&
7802           PSPP_SHEET_VIEW_COLUMN (last_column->data)->visible &&
7803           (PSPP_SHEET_VIEW_COLUMN (last_column->data)->clickable ||
7804            PSPP_SHEET_VIEW_COLUMN (last_column->data)->reorderable))
7805         break;
7806       last_column = last_column->prev;
7807     }
7808
7809
7810   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
7811
7812   switch (dir)
7813     {
7814     case GTK_DIR_TAB_BACKWARD:
7815     case GTK_DIR_TAB_FORWARD:
7816     case GTK_DIR_UP:
7817     case GTK_DIR_DOWN:
7818       if (focus_child == NULL)
7819         {
7820           if (tree_view->priv->focus_column != NULL &&
7821               gtk_widget_get_can_focus (tree_view->priv->focus_column->button))
7822             focus_child = tree_view->priv->focus_column->button;
7823           else
7824             focus_child = PSPP_SHEET_VIEW_COLUMN (first_column->data)->button;
7825           gtk_widget_grab_focus (focus_child);
7826           break;
7827         }
7828       return FALSE;
7829
7830     case GTK_DIR_LEFT:
7831     case GTK_DIR_RIGHT:
7832       if (focus_child == NULL)
7833         {
7834           if (tree_view->priv->focus_column != NULL)
7835             focus_child = tree_view->priv->focus_column->button;
7836           else if (dir == GTK_DIR_LEFT)
7837             focus_child = PSPP_SHEET_VIEW_COLUMN (last_column->data)->button;
7838           else
7839             focus_child = PSPP_SHEET_VIEW_COLUMN (first_column->data)->button;
7840           gtk_widget_grab_focus (focus_child);
7841           break;
7842         }
7843
7844       if (gtk_widget_child_focus (focus_child, dir))
7845         {
7846           /* The focus moves inside the button. */
7847           /* This is probably a great example of bad UI */
7848           break;
7849         }
7850
7851       /* We need to move the focus among the row of buttons. */
7852       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
7853         if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)->button == focus_child)
7854           break;
7855
7856       if ((tmp_list == first_column && dir == (rtl ? GTK_DIR_RIGHT : GTK_DIR_LEFT))
7857           || (tmp_list == last_column && dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT)))
7858         {
7859           gtk_widget_error_bell (GTK_WIDGET (tree_view));
7860           break;
7861         }
7862
7863       while (tmp_list)
7864         {
7865           PsppSheetViewColumn *column;
7866
7867           if (dir == (rtl ? GTK_DIR_LEFT : GTK_DIR_RIGHT))
7868             tmp_list = tmp_list->next;
7869           else
7870             tmp_list = tmp_list->prev;
7871
7872           if (tmp_list == NULL)
7873             {
7874               g_warning ("Internal button not found");
7875               break;
7876             }
7877           column = tmp_list->data;
7878           if (column->button &&
7879               column->visible &&
7880               gtk_widget_get_can_focus (column->button))
7881             {
7882               focus_child = column->button;
7883               gtk_widget_grab_focus (column->button);
7884               break;
7885             }
7886         }
7887       break;
7888     default:
7889       g_assert_not_reached ();
7890       break;
7891     }
7892
7893   /* if focus child is non-null, we assume it's been set to the current focus child
7894    */
7895   if (focus_child)
7896     {
7897       for (tmp_list = tree_view->priv->columns; tmp_list; tmp_list = tmp_list->next)
7898         if (PSPP_SHEET_VIEW_COLUMN (tmp_list->data)->button == focus_child)
7899           {
7900             tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
7901             break;
7902           }
7903
7904       if (clamp_column_visible)
7905         {
7906           pspp_sheet_view_clamp_column_visible (tree_view,
7907                                               tree_view->priv->focus_column,
7908                                               FALSE);
7909         }
7910     }
7911
7912   return (focus_child != NULL);
7913 }
7914
7915 /* This function returns in 'path' the first focusable path, if the given path
7916  * is already focusable, it's the returned one.
7917  */
7918 static gboolean
7919 search_first_focusable_path (PsppSheetView  *tree_view,
7920                              GtkTreePath **path,
7921                              gboolean      search_forward,
7922                              GtkRBTree   **new_tree,
7923                              GtkRBNode   **new_node)
7924 {
7925   GtkRBTree *tree = NULL;
7926   GtkRBNode *node = NULL;
7927
7928   if (!path || !*path)
7929     return FALSE;
7930
7931   _pspp_sheet_view_find_node (tree_view, *path, &tree, &node);
7932
7933   if (!tree || !node)
7934     return FALSE;
7935
7936   while (node && row_is_separator (tree_view, NULL, *path))
7937     {
7938       if (search_forward)
7939         _pspp_rbtree_next_full (tree, node, &tree, &node);
7940       else
7941         _pspp_rbtree_prev_full (tree, node, &tree, &node);
7942
7943       if (*path)
7944         gtk_tree_path_free (*path);
7945
7946       if (node)
7947         *path = _pspp_sheet_view_find_path (tree_view, tree, node);
7948       else
7949         *path = NULL;
7950     }
7951
7952   if (new_tree)
7953     *new_tree = tree;
7954
7955   if (new_node)
7956     *new_node = node;
7957
7958   return (*path != NULL);
7959 }
7960
7961 static gint
7962 pspp_sheet_view_focus (GtkWidget        *widget,
7963                      GtkDirectionType  direction)
7964 {
7965   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
7966   GtkContainer *container = GTK_CONTAINER (widget);
7967   GtkWidget *focus_child;
7968
7969   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_can_focus (widget))
7970     return FALSE;
7971
7972   focus_child = container->focus_child;
7973
7974   pspp_sheet_view_stop_editing (PSPP_SHEET_VIEW (widget), FALSE);
7975   /* Case 1.  Headers currently have focus. */
7976   if (focus_child)
7977     {
7978       switch (direction)
7979         {
7980         case GTK_DIR_LEFT:
7981         case GTK_DIR_RIGHT:
7982           pspp_sheet_view_header_focus (tree_view, direction, TRUE);
7983           return TRUE;
7984         case GTK_DIR_TAB_BACKWARD:
7985         case GTK_DIR_UP:
7986           return FALSE;
7987         case GTK_DIR_TAB_FORWARD:
7988         case GTK_DIR_DOWN:
7989           gtk_widget_grab_focus (widget);
7990           return TRUE;
7991         default:
7992           g_assert_not_reached ();
7993           return FALSE;
7994         }
7995     }
7996
7997   /* Case 2. We don't have focus at all. */
7998   if (!gtk_widget_has_focus (widget))
7999     {
8000       if (!pspp_sheet_view_header_focus (tree_view, direction, FALSE))
8001         gtk_widget_grab_focus (widget);
8002       return TRUE;
8003     }
8004
8005   /* Case 3. We have focus already. */
8006   if (direction == GTK_DIR_TAB_BACKWARD)
8007     return (pspp_sheet_view_header_focus (tree_view, direction, FALSE));
8008   else if (direction == GTK_DIR_TAB_FORWARD)
8009     return FALSE;
8010
8011   /* Other directions caught by the keybindings */
8012   gtk_widget_grab_focus (widget);
8013   return TRUE;
8014 }
8015
8016 static void
8017 pspp_sheet_view_grab_focus (GtkWidget *widget)
8018 {
8019   GTK_WIDGET_CLASS (pspp_sheet_view_parent_class)->grab_focus (widget);
8020
8021   pspp_sheet_view_focus_to_cursor (PSPP_SHEET_VIEW (widget));
8022 }
8023
8024 static void
8025 pspp_sheet_view_style_set (GtkWidget *widget,
8026                          GtkStyle *previous_style)
8027 {
8028   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
8029   GList *list;
8030   PsppSheetViewColumn *column;
8031
8032   if (gtk_widget_get_realized (widget))
8033     {
8034       gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
8035       gdk_window_set_background (tree_view->priv->bin_window, &widget->style->base[widget->state]);
8036       gtk_style_set_background (widget->style, tree_view->priv->header_window, GTK_STATE_NORMAL);
8037
8038       pspp_sheet_view_set_grid_lines (tree_view, tree_view->priv->grid_lines);
8039       pspp_sheet_view_set_enable_tree_lines (tree_view, tree_view->priv->tree_lines_enabled);
8040     }
8041
8042   gtk_widget_style_get (widget,
8043                         "expander-size", &tree_view->priv->expander_size,
8044                         NULL);
8045   tree_view->priv->expander_size += EXPANDER_EXTRA_PADDING;
8046
8047   for (list = tree_view->priv->columns; list; list = list->next)
8048     {
8049       column = list->data;
8050       _pspp_sheet_view_column_cell_set_dirty (column, TRUE);
8051     }
8052
8053   tree_view->priv->fixed_height = -1;
8054   _pspp_rbtree_mark_invalid (tree_view->priv->tree);
8055
8056   gtk_widget_queue_resize (widget);
8057 }
8058
8059
8060 static void
8061 pspp_sheet_view_set_focus_child (GtkContainer *container,
8062                                GtkWidget    *child)
8063 {
8064   PsppSheetView *tree_view = PSPP_SHEET_VIEW (container);
8065   GList *list;
8066
8067   for (list = tree_view->priv->columns; list; list = list->next)
8068     {
8069       if (PSPP_SHEET_VIEW_COLUMN (list->data)->button == child)
8070         {
8071           tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
8072           break;
8073         }
8074     }
8075
8076   GTK_CONTAINER_CLASS (pspp_sheet_view_parent_class)->set_focus_child (container, child);
8077 }
8078
8079 static void
8080 pspp_sheet_view_set_adjustments (PsppSheetView   *tree_view,
8081                                GtkAdjustment *hadj,
8082                                GtkAdjustment *vadj)
8083 {
8084   gboolean need_adjust = FALSE;
8085
8086   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
8087
8088   if (hadj)
8089     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
8090   else
8091     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
8092   if (vadj)
8093     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
8094   else
8095     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
8096
8097   if (tree_view->priv->hadjustment && (tree_view->priv->hadjustment != hadj))
8098     {
8099       g_signal_handlers_disconnect_by_func (tree_view->priv->hadjustment,
8100                                             pspp_sheet_view_adjustment_changed,
8101                                             tree_view);
8102       g_object_unref (tree_view->priv->hadjustment);
8103     }
8104
8105   if (tree_view->priv->vadjustment && (tree_view->priv->vadjustment != vadj))
8106     {
8107       g_signal_handlers_disconnect_by_func (tree_view->priv->vadjustment,
8108                                             pspp_sheet_view_adjustment_changed,
8109                                             tree_view);
8110       g_object_unref (tree_view->priv->vadjustment);
8111     }
8112
8113   if (tree_view->priv->hadjustment != hadj)
8114     {
8115       tree_view->priv->hadjustment = hadj;
8116       g_object_ref_sink (tree_view->priv->hadjustment);
8117
8118       g_signal_connect (tree_view->priv->hadjustment, "value-changed",
8119                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
8120                         tree_view);
8121       need_adjust = TRUE;
8122     }
8123
8124   if (tree_view->priv->vadjustment != vadj)
8125     {
8126       tree_view->priv->vadjustment = vadj;
8127       g_object_ref_sink (tree_view->priv->vadjustment);
8128
8129       g_signal_connect (tree_view->priv->vadjustment, "value-changed",
8130                         G_CALLBACK (pspp_sheet_view_adjustment_changed),
8131                         tree_view);
8132       need_adjust = TRUE;
8133     }
8134
8135   if (need_adjust)
8136     pspp_sheet_view_adjustment_changed (NULL, tree_view);
8137 }
8138
8139
8140 static gboolean
8141 pspp_sheet_view_real_move_cursor (PsppSheetView       *tree_view,
8142                                 GtkMovementStep    step,
8143                                 gint               count)
8144 {
8145   GdkModifierType state;
8146
8147   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
8148   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
8149                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
8150                         step == GTK_MOVEMENT_DISPLAY_LINES ||
8151                         step == GTK_MOVEMENT_PAGES ||
8152                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
8153
8154   if (tree_view->priv->tree == NULL)
8155     return FALSE;
8156   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
8157     return FALSE;
8158
8159   pspp_sheet_view_stop_editing (tree_view, FALSE);
8160   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
8161   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
8162
8163   if (gtk_get_current_event_state (&state))
8164     {
8165       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
8166         tree_view->priv->ctrl_pressed = TRUE;
8167       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
8168         tree_view->priv->shift_pressed = TRUE;
8169     }
8170   /* else we assume not pressed */
8171
8172   switch (step)
8173     {
8174       /* currently we make no distinction.  When we go bi-di, we need to */
8175     case GTK_MOVEMENT_LOGICAL_POSITIONS:
8176     case GTK_MOVEMENT_VISUAL_POSITIONS:
8177       pspp_sheet_view_move_cursor_left_right (tree_view, count);
8178       break;
8179     case GTK_MOVEMENT_DISPLAY_LINES:
8180       pspp_sheet_view_move_cursor_up_down (tree_view, count);
8181       break;
8182     case GTK_MOVEMENT_PAGES:
8183       pspp_sheet_view_move_cursor_page_up_down (tree_view, count);
8184       break;
8185     case GTK_MOVEMENT_BUFFER_ENDS:
8186       pspp_sheet_view_move_cursor_start_end (tree_view, count);
8187       break;
8188     default:
8189       g_assert_not_reached ();
8190     }
8191
8192   tree_view->priv->ctrl_pressed = FALSE;
8193   tree_view->priv->shift_pressed = FALSE;
8194
8195   return TRUE;
8196 }
8197
8198 static void
8199 pspp_sheet_view_put (PsppSheetView *tree_view,
8200                    GtkWidget   *child_widget,
8201                    /* in bin_window coordinates */
8202                    gint         x,
8203                    gint         y,
8204                    gint         width,
8205                    gint         height)
8206 {
8207   PsppSheetViewChild *child;
8208   
8209   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
8210   g_return_if_fail (GTK_IS_WIDGET (child_widget));
8211
8212   child = g_slice_new (PsppSheetViewChild);
8213
8214   child->widget = child_widget;
8215   child->x = x;
8216   child->y = y;
8217   child->width = width;
8218   child->height = height;
8219
8220   tree_view->priv->children = g_list_append (tree_view->priv->children, child);
8221
8222   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8223     gtk_widget_set_parent_window (child->widget, tree_view->priv->bin_window);
8224   
8225   gtk_widget_set_parent (child_widget, GTK_WIDGET (tree_view));
8226 }
8227
8228 void
8229 _pspp_sheet_view_child_move_resize (PsppSheetView *tree_view,
8230                                   GtkWidget   *widget,
8231                                   /* in tree coordinates */
8232                                   gint         x,
8233                                   gint         y,
8234                                   gint         width,
8235                                   gint         height)
8236 {
8237   PsppSheetViewChild *child = NULL;
8238   GList *list;
8239   GdkRectangle allocation;
8240
8241   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
8242   g_return_if_fail (GTK_IS_WIDGET (widget));
8243
8244   for (list = tree_view->priv->children; list; list = list->next)
8245     {
8246       if (((PsppSheetViewChild *)list->data)->widget == widget)
8247         {
8248           child = list->data;
8249           break;
8250         }
8251     }
8252   if (child == NULL)
8253     return;
8254
8255   allocation.x = child->x = x;
8256   allocation.y = child->y = y;
8257   allocation.width = child->width = width;
8258   allocation.height = child->height = height;
8259
8260   if (gtk_widget_get_realized (widget))
8261     gtk_widget_size_allocate (widget, &allocation);
8262 }
8263
8264
8265 /* TreeModel Callbacks
8266  */
8267
8268 static void
8269 pspp_sheet_view_row_changed (GtkTreeModel *model,
8270                            GtkTreePath  *path,
8271                            GtkTreeIter  *iter,
8272                            gpointer      data)
8273 {
8274   PsppSheetView *tree_view = (PsppSheetView *)data;
8275   GtkRBTree *tree;
8276   GtkRBNode *node;
8277   gboolean free_path = FALSE;
8278   GList *list;
8279   GtkTreePath *cursor_path;
8280
8281   g_return_if_fail (path != NULL || iter != NULL);
8282
8283   if (tree_view->priv->cursor != NULL)
8284     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8285   else
8286     cursor_path = NULL;
8287
8288   if (tree_view->priv->edited_column &&
8289       (cursor_path == NULL || gtk_tree_path_compare (cursor_path, path) == 0))
8290     pspp_sheet_view_stop_editing (tree_view, TRUE);
8291
8292   if (cursor_path != NULL)
8293     gtk_tree_path_free (cursor_path);
8294
8295   if (path == NULL)
8296     {
8297       path = gtk_tree_model_get_path (model, iter);
8298       free_path = TRUE;
8299     }
8300   else if (iter == NULL)
8301     gtk_tree_model_get_iter (model, iter, path);
8302
8303   if (_pspp_sheet_view_find_node (tree_view,
8304                                 path,
8305                                 &tree,
8306                                 &node))
8307     /* We aren't actually showing the node */
8308     goto done;
8309
8310   if (tree == NULL)
8311     goto done;
8312
8313   if (tree_view->priv->fixed_height_mode
8314       && tree_view->priv->fixed_height >= 0)
8315     {
8316       _pspp_rbtree_node_set_height (tree, node, tree_view->priv->fixed_height);
8317       if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8318         pspp_sheet_view_node_queue_redraw (tree_view, tree, node);
8319     }
8320   else
8321     {
8322       _pspp_rbtree_node_mark_invalid (tree, node);
8323       for (list = tree_view->priv->columns; list; list = list->next)
8324         {
8325           PsppSheetViewColumn *column;
8326
8327           column = list->data;
8328           if (! column->visible)
8329             continue;
8330
8331           if (column->column_type == PSPP_SHEET_VIEW_COLUMN_AUTOSIZE)
8332             {
8333               _pspp_sheet_view_column_cell_set_dirty (column, TRUE);
8334             }
8335         }
8336     }
8337
8338  done:
8339   if (!tree_view->priv->fixed_height_mode &&
8340       gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8341     install_presize_handler (tree_view);
8342   if (free_path)
8343     gtk_tree_path_free (path);
8344 }
8345
8346 static void
8347 pspp_sheet_view_row_inserted (GtkTreeModel *model,
8348                             GtkTreePath  *path,
8349                             GtkTreeIter  *iter,
8350                             gpointer      data)
8351 {
8352   PsppSheetView *tree_view = (PsppSheetView *) data;
8353   gint *indices;
8354   GtkRBTree *tmptree, *tree;
8355   GtkRBNode *tmpnode = NULL;
8356   gint depth;
8357   gint i = 0;
8358   gint height;
8359   gboolean free_path = FALSE;
8360   gboolean node_visible = TRUE;
8361
8362   g_return_if_fail (path != NULL || iter != NULL);
8363
8364   if (tree_view->priv->fixed_height_mode
8365       && tree_view->priv->fixed_height >= 0)
8366     height = tree_view->priv->fixed_height;
8367   else
8368     height = 0;
8369
8370   if (path == NULL)
8371     {
8372       path = gtk_tree_model_get_path (model, iter);
8373       free_path = TRUE;
8374     }
8375   else if (iter == NULL)
8376     gtk_tree_model_get_iter (model, iter, path);
8377
8378   if (tree_view->priv->tree == NULL)
8379     tree_view->priv->tree = _pspp_rbtree_new ();
8380
8381   tmptree = tree = tree_view->priv->tree;
8382
8383   /* Update all row-references */
8384   gtk_tree_row_reference_inserted (G_OBJECT (data), path);
8385   depth = gtk_tree_path_get_depth (path);
8386   indices = gtk_tree_path_get_indices (path);
8387
8388   /* First, find the parent tree */
8389   while (i < depth - 1)
8390     {
8391       if (tmptree == NULL)
8392         {
8393           /* We aren't showing the node */
8394           node_visible = FALSE;
8395           goto done;
8396         }
8397
8398       tmpnode = _pspp_rbtree_find_count (tmptree, indices[i] + 1);
8399       if (tmpnode == NULL)
8400         {
8401           g_warning ("A node was inserted with a parent that's not in the tree.\n" \
8402                      "This possibly means that a GtkTreeModel inserted a child node\n" \
8403                      "before the parent was inserted.");
8404           goto done;
8405         }
8406       else if (!PSPP_RBNODE_FLAG_SET (tmpnode, PSPP_RBNODE_IS_PARENT))
8407         {
8408           /* FIXME enforce correct behavior on model, probably */
8409           /* In theory, the model should have emitted has_child_toggled here.  We
8410            * try to catch it anyway, just to be safe, in case the model hasn't.
8411            */
8412           GtkTreePath *tmppath = _pspp_sheet_view_find_path (tree_view,
8413                                                            tree,
8414                                                            tmpnode);
8415           pspp_sheet_view_row_has_child_toggled (model, tmppath, NULL, data);
8416           gtk_tree_path_free (tmppath);
8417           goto done;
8418         }
8419
8420       tmptree = tmpnode->children;
8421       tree = tmptree;
8422       i++;
8423     }
8424
8425   if (tree == NULL)
8426     {
8427       node_visible = FALSE;
8428       goto done;
8429     }
8430
8431   /* ref the node */
8432   gtk_tree_model_ref_node (tree_view->priv->model, iter);
8433   if (indices[depth - 1] == 0)
8434     {
8435       tmpnode = _pspp_rbtree_find_count (tree, 1);
8436       tmpnode = _pspp_rbtree_insert_before (tree, tmpnode, height, FALSE);
8437     }
8438   else
8439     {
8440       tmpnode = _pspp_rbtree_find_count (tree, indices[depth - 1]);
8441       tmpnode = _pspp_rbtree_insert_after (tree, tmpnode, height, FALSE);
8442     }
8443
8444  done:
8445   if (height > 0)
8446     {
8447       if (tree)
8448         _pspp_rbtree_node_mark_valid (tree, tmpnode);
8449
8450       if (node_visible && node_is_visible (tree_view, tree, tmpnode))
8451         gtk_widget_queue_resize (GTK_WIDGET (tree_view));
8452       else
8453         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (tree_view));
8454     }
8455   else
8456     install_presize_handler (tree_view);
8457   if (free_path)
8458     gtk_tree_path_free (path);
8459 }
8460
8461 static void
8462 pspp_sheet_view_row_has_child_toggled (GtkTreeModel *model,
8463                                      GtkTreePath  *path,
8464                                      GtkTreeIter  *iter,
8465                                      gpointer      data)
8466 {
8467   PsppSheetView *tree_view = (PsppSheetView *)data;
8468   GtkTreeIter real_iter;
8469   gboolean has_child;
8470   GtkRBTree *tree;
8471   GtkRBNode *node;
8472   gboolean free_path = FALSE;
8473
8474   g_return_if_fail (path != NULL || iter != NULL);
8475
8476   if (iter)
8477     real_iter = *iter;
8478
8479   if (path == NULL)
8480     {
8481       path = gtk_tree_model_get_path (model, iter);
8482       free_path = TRUE;
8483     }
8484   else if (iter == NULL)
8485     gtk_tree_model_get_iter (model, &real_iter, path);
8486
8487   if (_pspp_sheet_view_find_node (tree_view,
8488                                 path,
8489                                 &tree,
8490                                 &node))
8491     /* We aren't actually showing the node */
8492     goto done;
8493
8494   if (tree == NULL)
8495     goto done;
8496
8497   has_child = gtk_tree_model_iter_has_child (model, &real_iter);
8498   /* Sanity check.
8499    */
8500   if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT) == has_child)
8501     goto done;
8502
8503   if (has_child)
8504     PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_PARENT);
8505   else
8506     PSPP_RBNODE_UNSET_FLAG (node, PSPP_RBNODE_IS_PARENT);
8507
8508   if (has_child && PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IS_LIST))
8509     {
8510       PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IS_LIST);
8511       if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS))
8512         {
8513           GList *list;
8514
8515           for (list = tree_view->priv->columns; list; list = list->next)
8516             if (PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
8517               {
8518                 PSPP_SHEET_VIEW_COLUMN (list->data)->dirty = TRUE;
8519                 _pspp_sheet_view_column_cell_set_dirty (PSPP_SHEET_VIEW_COLUMN (list->data), TRUE);
8520                 break;
8521               }
8522         }
8523       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
8524     }
8525   else
8526     {
8527       _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
8528     }
8529
8530  done:
8531   if (free_path)
8532     gtk_tree_path_free (path);
8533 }
8534
8535 static void
8536 count_children_helper (GtkRBTree *tree,
8537                        GtkRBNode *node,
8538                        gpointer   data)
8539 {
8540   if (node->children)
8541     _pspp_rbtree_traverse (node->children, node->children->root, G_POST_ORDER, count_children_helper, data);
8542   (*((gint *)data))++;
8543 }
8544
8545 static void
8546 check_selection_helper (GtkRBTree *tree,
8547                         GtkRBNode *node,
8548                         gpointer   data)
8549 {
8550   gint *value = (gint *)data;
8551
8552   *value = PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED);
8553
8554   if (node->children && !*value)
8555     _pspp_rbtree_traverse (node->children, node->children->root, G_POST_ORDER, check_selection_helper, data);
8556 }
8557
8558 static void
8559 pspp_sheet_view_row_deleted (GtkTreeModel *model,
8560                            GtkTreePath  *path,
8561                            gpointer      data)
8562 {
8563   PsppSheetView *tree_view = (PsppSheetView *)data;
8564   GtkRBTree *tree;
8565   GtkRBNode *node;
8566   GList *list;
8567   gint selection_changed = FALSE;
8568
8569   g_return_if_fail (path != NULL);
8570
8571   gtk_tree_row_reference_deleted (G_OBJECT (data), path);
8572
8573   if (_pspp_sheet_view_find_node (tree_view, path, &tree, &node))
8574     return;
8575
8576   if (tree == NULL)
8577     return;
8578
8579   /* check if the selection has been changed */
8580   _pspp_rbtree_traverse (tree, node, G_POST_ORDER,
8581                         check_selection_helper, &selection_changed);
8582
8583   for (list = tree_view->priv->columns; list; list = list->next)
8584     if (((PsppSheetViewColumn *)list->data)->visible &&
8585         ((PsppSheetViewColumn *)list->data)->column_type == PSPP_SHEET_VIEW_COLUMN_AUTOSIZE)
8586       _pspp_sheet_view_column_cell_set_dirty ((PsppSheetViewColumn *)list->data, TRUE);
8587
8588   /* Ensure we don't have a dangling pointer to a dead node */
8589   ensure_unprelighted (tree_view);
8590
8591   /* Cancel editting if we've started */
8592   pspp_sheet_view_stop_editing (tree_view, TRUE);
8593
8594   /* If we have a node expanded/collapsed timeout, remove it */
8595   remove_expand_collapse_timeout (tree_view);
8596
8597   if (tree_view->priv->destroy_count_func)
8598     {
8599       gint child_count = 0;
8600       if (node->children)
8601         _pspp_rbtree_traverse (node->children, node->children->root, G_POST_ORDER, count_children_helper, &child_count);
8602       tree_view->priv->destroy_count_func (tree_view, path, child_count, tree_view->priv->destroy_count_data);
8603     }
8604
8605   if (tree->root->count == 1)
8606     {
8607       if (tree_view->priv->tree == tree)
8608         tree_view->priv->tree = NULL;
8609
8610       _pspp_rbtree_remove (tree);
8611     }
8612   else
8613     {
8614       _pspp_rbtree_remove_node (tree, node);
8615     }
8616
8617   if (! gtk_tree_row_reference_valid (tree_view->priv->top_row))
8618     {
8619       gtk_tree_row_reference_free (tree_view->priv->top_row);
8620       tree_view->priv->top_row = NULL;
8621     }
8622
8623   install_scroll_sync_handler (tree_view);
8624
8625   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
8626
8627   if (selection_changed)
8628     g_signal_emit_by_name (tree_view->priv->selection, "changed");
8629 }
8630
8631 static void
8632 pspp_sheet_view_rows_reordered (GtkTreeModel *model,
8633                               GtkTreePath  *parent,
8634                               GtkTreeIter  *iter,
8635                               gint         *new_order,
8636                               gpointer      data)
8637 {
8638   PsppSheetView *tree_view = PSPP_SHEET_VIEW (data);
8639   GtkRBTree *tree;
8640   GtkRBNode *node;
8641   gint len;
8642
8643   len = gtk_tree_model_iter_n_children (model, iter);
8644
8645   if (len < 2)
8646     return;
8647
8648   gtk_tree_row_reference_reordered (G_OBJECT (data),
8649                                     parent,
8650                                     iter,
8651                                     new_order);
8652
8653   if (_pspp_sheet_view_find_node (tree_view,
8654                                 parent,
8655                                 &tree,
8656                                 &node))
8657     return;
8658
8659   /* We need to special case the parent path */
8660   if (tree == NULL)
8661     tree = tree_view->priv->tree;
8662   else
8663     tree = node->children;
8664
8665   if (tree == NULL)
8666     return;
8667
8668   if (tree_view->priv->edited_column)
8669     pspp_sheet_view_stop_editing (tree_view, TRUE);
8670
8671   /* we need to be unprelighted */
8672   ensure_unprelighted (tree_view);
8673
8674   /* clear the timeout */
8675   cancel_arrow_animation (tree_view);
8676   
8677   _pspp_rbtree_reorder (tree, new_order, len);
8678
8679   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
8680
8681   pspp_sheet_view_dy_to_top_row (tree_view);
8682 }
8683
8684
8685 /* Internal tree functions
8686  */
8687
8688
8689 static void
8690 pspp_sheet_view_get_background_xrange (PsppSheetView       *tree_view,
8691                                      GtkRBTree         *tree,
8692                                      PsppSheetViewColumn *column,
8693                                      gint              *x1,
8694                                      gint              *x2)
8695 {
8696   PsppSheetViewColumn *tmp_column = NULL;
8697   gint total_width;
8698   GList *list;
8699   gboolean rtl;
8700
8701   if (x1)
8702     *x1 = 0;
8703
8704   if (x2)
8705     *x2 = 0;
8706
8707   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8708
8709   total_width = 0;
8710   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
8711        list;
8712        list = (rtl ? list->prev : list->next))
8713     {
8714       tmp_column = list->data;
8715
8716       if (tmp_column == column)
8717         break;
8718
8719       if (tmp_column->visible)
8720         total_width += tmp_column->width;
8721     }
8722
8723   if (tmp_column != column)
8724     {
8725       g_warning (G_STRLOC": passed-in column isn't in the tree");
8726       return;
8727     }
8728
8729   if (x1)
8730     *x1 = total_width;
8731
8732   if (x2)
8733     {
8734       if (column->visible)
8735         *x2 = total_width + column->width;
8736       else
8737         *x2 = total_width; /* width of 0 */
8738     }
8739 }
8740 static void
8741 pspp_sheet_view_get_arrow_xrange (PsppSheetView *tree_view,
8742                                 GtkRBTree   *tree,
8743                                 gint        *x1,
8744                                 gint        *x2)
8745 {
8746   gint x_offset = 0;
8747   GList *list;
8748   PsppSheetViewColumn *tmp_column = NULL;
8749   gint total_width;
8750   gboolean indent_expanders;
8751   gboolean rtl;
8752
8753   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
8754
8755   total_width = 0;
8756   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
8757        list;
8758        list = (rtl ? list->prev : list->next))
8759     {
8760       tmp_column = list->data;
8761
8762       if (pspp_sheet_view_is_expander_column (tree_view, tmp_column))
8763         {
8764           if (rtl)
8765             x_offset = total_width + tmp_column->width - tree_view->priv->expander_size;
8766           else
8767             x_offset = total_width;
8768           break;
8769         }
8770
8771       if (tmp_column->visible)
8772         total_width += tmp_column->width;
8773     }
8774
8775   gtk_widget_style_get (GTK_WIDGET (tree_view),
8776                         "indent-expanders", &indent_expanders,
8777                         NULL);
8778
8779   if (indent_expanders)
8780     {
8781       if (rtl)
8782         x_offset -= tree_view->priv->expander_size * _pspp_rbtree_get_depth (tree);
8783       else
8784         x_offset += tree_view->priv->expander_size * _pspp_rbtree_get_depth (tree);
8785     }
8786
8787   *x1 = x_offset;
8788   
8789   if (tmp_column && tmp_column->visible)
8790     /* +1 because x2 isn't included in the range. */
8791     *x2 = *x1 + tree_view->priv->expander_size + 1;
8792   else
8793     *x2 = *x1;
8794 }
8795
8796 static void
8797 pspp_sheet_view_build_tree (PsppSheetView *tree_view,
8798                           GtkRBTree   *tree,
8799                           GtkTreeIter *iter,
8800                           gint         depth,
8801                           gboolean     recurse)
8802 {
8803   GtkRBNode *temp = NULL;
8804   GtkTreePath *path = NULL;
8805   gboolean is_list = PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IS_LIST);
8806
8807   do
8808     {
8809       gtk_tree_model_ref_node (tree_view->priv->model, iter);
8810       temp = _pspp_rbtree_insert_after (tree, temp, 0, FALSE);
8811
8812       if (tree_view->priv->fixed_height > 0)
8813         {
8814           if (PSPP_RBNODE_FLAG_SET (temp, PSPP_RBNODE_INVALID))
8815             {
8816               _pspp_rbtree_node_set_height (tree, temp, tree_view->priv->fixed_height);
8817               _pspp_rbtree_node_mark_valid (tree, temp);
8818             }
8819         }
8820
8821       if (is_list)
8822         continue;
8823
8824       if (recurse)
8825         {
8826           GtkTreeIter child;
8827
8828           if (!path)
8829             path = gtk_tree_model_get_path (tree_view->priv->model, iter);
8830           else
8831             gtk_tree_path_next (path);
8832
8833           if (gtk_tree_model_iter_children (tree_view->priv->model, &child, iter))
8834             {
8835               gboolean expand;
8836
8837               g_signal_emit (tree_view, tree_view_signals[TEST_EXPAND_ROW], 0, iter, path, &expand);
8838
8839               if (gtk_tree_model_iter_has_child (tree_view->priv->model, iter)
8840                   && !expand)
8841                 {
8842                   temp->children = _pspp_rbtree_new ();
8843                   temp->children->parent_tree = tree;
8844                   temp->children->parent_node = temp;
8845                   pspp_sheet_view_build_tree (tree_view, temp->children, &child, depth + 1, recurse);
8846                 }
8847             }
8848         }
8849
8850       if (gtk_tree_model_iter_has_child (tree_view->priv->model, iter))
8851         {
8852           if ((temp->flags&PSPP_RBNODE_IS_PARENT) != PSPP_RBNODE_IS_PARENT)
8853             temp->flags ^= PSPP_RBNODE_IS_PARENT;
8854         }
8855     }
8856   while (gtk_tree_model_iter_next (tree_view->priv->model, iter));
8857
8858   if (path)
8859     gtk_tree_path_free (path);
8860 }
8861
8862 /* Make sure the node is visible vertically */
8863 static void
8864 pspp_sheet_view_clamp_node_visible (PsppSheetView *tree_view,
8865                                   GtkRBTree   *tree,
8866                                   GtkRBNode   *node)
8867 {
8868   gint node_dy, height;
8869   GtkTreePath *path = NULL;
8870
8871   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
8872     return;
8873
8874   /* just return if the node is visible, avoiding a costly expose */
8875   node_dy = _pspp_rbtree_node_find_offset (tree, node);
8876   height = ROW_HEIGHT (tree_view, PSPP_RBNODE_GET_HEIGHT (node));
8877   if (! PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_INVALID)
8878       && node_dy >= tree_view->priv->vadjustment->value
8879       && node_dy + height <= (tree_view->priv->vadjustment->value
8880                               + tree_view->priv->vadjustment->page_size))
8881     return;
8882
8883   path = _pspp_sheet_view_find_path (tree_view, tree, node);
8884   if (path)
8885     {
8886       /* We process updates because we want to clear old selected items when we scroll.
8887        * if this is removed, we get a "selection streak" at the bottom. */
8888       gdk_window_process_updates (tree_view->priv->bin_window, TRUE);
8889       pspp_sheet_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
8890       gtk_tree_path_free (path);
8891     }
8892 }
8893
8894 static void
8895 pspp_sheet_view_clamp_column_visible (PsppSheetView       *tree_view,
8896                                     PsppSheetViewColumn *column,
8897                                     gboolean           focus_to_cell)
8898 {
8899   gint x, width;
8900
8901   if (column == NULL)
8902     return;
8903
8904   x = column->button->allocation.x;
8905   width = column->button->allocation.width;
8906
8907   if (width > tree_view->priv->hadjustment->page_size)
8908     {
8909       /* The column is larger than the horizontal page size.  If the
8910        * column has cells which can be focussed individually, then we make
8911        * sure the cell which gets focus is fully visible (if even the
8912        * focus cell is bigger than the page size, we make sure the
8913        * left-hand side of the cell is visible).
8914        *
8915        * If the column does not have those so-called special cells, we
8916        * make sure the left-hand side of the column is visible.
8917        */
8918
8919       if (focus_to_cell && pspp_sheet_view_has_special_cell (tree_view))
8920         {
8921           GtkTreePath *cursor_path;
8922           GdkRectangle background_area, cell_area, focus_area;
8923
8924           cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
8925
8926           pspp_sheet_view_get_cell_area (tree_view,
8927                                        cursor_path, column, &cell_area);
8928           pspp_sheet_view_get_background_area (tree_view,
8929                                              cursor_path, column,
8930                                              &background_area);
8931
8932           gtk_tree_path_free (cursor_path);
8933
8934           _pspp_sheet_view_column_get_focus_area (column,
8935                                                 &background_area,
8936                                                 &cell_area,
8937                                                 &focus_area);
8938
8939           x = focus_area.x;
8940           width = focus_area.width;
8941
8942           if (width < tree_view->priv->hadjustment->page_size)
8943             {
8944               if ((tree_view->priv->hadjustment->value + tree_view->priv->hadjustment->page_size) < (x + width))
8945                 gtk_adjustment_set_value (tree_view->priv->hadjustment,
8946                                           x + width - tree_view->priv->hadjustment->page_size);
8947               else if (tree_view->priv->hadjustment->value > x)
8948                 gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
8949             }
8950         }
8951
8952       gtk_adjustment_set_value (tree_view->priv->hadjustment,
8953                                 CLAMP (x,
8954                                        tree_view->priv->hadjustment->lower,
8955                                        tree_view->priv->hadjustment->upper
8956                                        - tree_view->priv->hadjustment->page_size));
8957     }
8958   else
8959     {
8960       if ((tree_view->priv->hadjustment->value + tree_view->priv->hadjustment->page_size) < (x + width))
8961           gtk_adjustment_set_value (tree_view->priv->hadjustment,
8962                                     x + width - tree_view->priv->hadjustment->page_size);
8963       else if (tree_view->priv->hadjustment->value > x)
8964         gtk_adjustment_set_value (tree_view->priv->hadjustment, x);
8965   }
8966 }
8967
8968 /* This function could be more efficient.  I'll optimize it if profiling seems
8969  * to imply that it is important */
8970 GtkTreePath *
8971 _pspp_sheet_view_find_path (PsppSheetView *tree_view,
8972                           GtkRBTree   *tree,
8973                           GtkRBNode   *node)
8974 {
8975   GtkTreePath *path;
8976   GtkRBTree *tmp_tree;
8977   GtkRBNode *tmp_node, *last;
8978   gint count;
8979
8980   path = gtk_tree_path_new ();
8981
8982   g_return_val_if_fail (node != NULL, path);
8983   g_return_val_if_fail (node != tree->nil, path);
8984
8985   count = 1 + node->left->count;
8986
8987   last = node;
8988   tmp_node = node->parent;
8989   tmp_tree = tree;
8990   while (tmp_tree)
8991     {
8992       while (tmp_node != tmp_tree->nil)
8993         {
8994           if (tmp_node->right == last)
8995             count += 1 + tmp_node->left->count;
8996           last = tmp_node;
8997           tmp_node = tmp_node->parent;
8998         }
8999       gtk_tree_path_prepend_index (path, count - 1);
9000       last = tmp_tree->parent_node;
9001       tmp_tree = tmp_tree->parent_tree;
9002       if (last)
9003         {
9004           count = 1 + last->left->count;
9005           tmp_node = last->parent;
9006         }
9007     }
9008   return path;
9009 }
9010
9011 /* Returns TRUE if we ran out of tree before finding the path.  If the path is
9012  * invalid (ie. points to a node that's not in the tree), *tree and *node are
9013  * both set to NULL.
9014  */
9015 gboolean
9016 _pspp_sheet_view_find_node (PsppSheetView  *tree_view,
9017                           GtkTreePath  *path,
9018                           GtkRBTree   **tree,
9019                           GtkRBNode   **node)
9020 {
9021   GtkRBNode *tmpnode = NULL;
9022   GtkRBTree *tmptree = tree_view->priv->tree;
9023   gint *indices = gtk_tree_path_get_indices (path);
9024   gint depth = gtk_tree_path_get_depth (path);
9025   gint i = 0;
9026
9027   *node = NULL;
9028   *tree = NULL;
9029
9030   if (depth == 0 || tmptree == NULL)
9031     return FALSE;
9032   do
9033     {
9034       tmpnode = _pspp_rbtree_find_count (tmptree, indices[i] + 1);
9035       ++i;
9036       if (tmpnode == NULL)
9037         {
9038           *tree = NULL;
9039           *node = NULL;
9040           return FALSE;
9041         }
9042       if (i >= depth)
9043         {
9044           *tree = tmptree;
9045           *node = tmpnode;
9046           return FALSE;
9047         }
9048       *tree = tmptree;
9049       *node = tmpnode;
9050       tmptree = tmpnode->children;
9051       if (tmptree == NULL)
9052         return TRUE;
9053     }
9054   while (1);
9055 }
9056
9057 static gboolean
9058 pspp_sheet_view_is_expander_column (PsppSheetView       *tree_view,
9059                                   PsppSheetViewColumn *column)
9060 {
9061   GList *list;
9062
9063   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IS_LIST))
9064     return FALSE;
9065
9066   if (tree_view->priv->expander_column != NULL)
9067     {
9068       if (tree_view->priv->expander_column == column)
9069         return TRUE;
9070       return FALSE;
9071     }
9072   else
9073     {
9074       for (list = tree_view->priv->columns;
9075            list;
9076            list = list->next)
9077         if (((PsppSheetViewColumn *)list->data)->visible)
9078           break;
9079       if (list && list->data == column)
9080         return TRUE;
9081     }
9082   return FALSE;
9083 }
9084
9085 static void
9086 pspp_sheet_view_add_move_binding (GtkBindingSet  *binding_set,
9087                                 guint           keyval,
9088                                 guint           modmask,
9089                                 gboolean        add_shifted_binding,
9090                                 GtkMovementStep step,
9091                                 gint            count)
9092 {
9093   
9094   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
9095                                 "move-cursor", 2,
9096                                 G_TYPE_ENUM, step,
9097                                 G_TYPE_INT, count);
9098
9099   if (add_shifted_binding)
9100     gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
9101                                   "move-cursor", 2,
9102                                   G_TYPE_ENUM, step,
9103                                   G_TYPE_INT, count);
9104
9105   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
9106    return;
9107
9108   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
9109                                 "move-cursor", 2,
9110                                 G_TYPE_ENUM, step,
9111                                 G_TYPE_INT, count);
9112
9113   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
9114                                 "move-cursor", 2,
9115                                 G_TYPE_ENUM, step,
9116                                 G_TYPE_INT, count);
9117 }
9118
9119 static gint
9120 pspp_sheet_view_unref_tree_helper (GtkTreeModel *model,
9121                                  GtkTreeIter  *iter,
9122                                  GtkRBTree    *tree,
9123                                  GtkRBNode    *node)
9124 {
9125   gint retval = FALSE;
9126   do
9127     {
9128       g_return_val_if_fail (node != NULL, FALSE);
9129
9130       if (node->children)
9131         {
9132           GtkTreeIter child;
9133           GtkRBTree *new_tree;
9134           GtkRBNode *new_node;
9135
9136           new_tree = node->children;
9137           new_node = new_tree->root;
9138
9139           while (new_node && new_node->left != new_tree->nil)
9140             new_node = new_node->left;
9141
9142           if (!gtk_tree_model_iter_children (model, &child, iter))
9143             return FALSE;
9144
9145           retval = retval || pspp_sheet_view_unref_tree_helper (model, &child, new_tree, new_node);
9146         }
9147
9148       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED))
9149         retval = TRUE;
9150       gtk_tree_model_unref_node (model, iter);
9151       node = _pspp_rbtree_next (tree, node);
9152     }
9153   while (gtk_tree_model_iter_next (model, iter));
9154
9155   return retval;
9156 }
9157
9158 static gint
9159 pspp_sheet_view_unref_and_check_selection_tree (PsppSheetView *tree_view,
9160                                               GtkRBTree   *tree)
9161 {
9162   GtkTreeIter iter;
9163   GtkTreePath *path;
9164   GtkRBNode *node;
9165   gint retval;
9166
9167   if (!tree)
9168     return FALSE;
9169
9170   node = tree->root;
9171   while (node && node->left != tree->nil)
9172     node = node->left;
9173
9174   g_return_val_if_fail (node != NULL, FALSE);
9175   path = _pspp_sheet_view_find_path (tree_view, tree, node);
9176   gtk_tree_model_get_iter (GTK_TREE_MODEL (tree_view->priv->model),
9177                            &iter, path);
9178   retval = pspp_sheet_view_unref_tree_helper (GTK_TREE_MODEL (tree_view->priv->model), &iter, tree, node);
9179   gtk_tree_path_free (path);
9180
9181   return retval;
9182 }
9183
9184 static void
9185 pspp_sheet_view_set_column_drag_info (PsppSheetView       *tree_view,
9186                                     PsppSheetViewColumn *column)
9187 {
9188   PsppSheetViewColumn *left_column;
9189   PsppSheetViewColumn *cur_column = NULL;
9190   PsppSheetViewColumnReorder *reorder;
9191   gboolean rtl;
9192   GList *tmp_list;
9193   gint left;
9194
9195   /* We want to precalculate the motion list such that we know what column slots
9196    * are available.
9197    */
9198   left_column = NULL;
9199   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
9200
9201   /* First, identify all possible drop spots */
9202   if (rtl)
9203     tmp_list = g_list_last (tree_view->priv->columns);
9204   else
9205     tmp_list = g_list_first (tree_view->priv->columns);
9206
9207   while (tmp_list)
9208     {
9209       cur_column = PSPP_SHEET_VIEW_COLUMN (tmp_list->data);
9210       tmp_list = rtl?g_list_previous (tmp_list):g_list_next (tmp_list);
9211
9212       if (cur_column->visible == FALSE)
9213         continue;
9214
9215       /* If it's not the column moving and func tells us to skip over the column, we continue. */
9216       if (left_column != column && cur_column != column &&
9217           tree_view->priv->column_drop_func &&
9218           ! tree_view->priv->column_drop_func (tree_view, column, left_column, cur_column, tree_view->priv->column_drop_func_data))
9219         {
9220           left_column = cur_column;
9221           continue;
9222         }
9223       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
9224       reorder->left_column = left_column;
9225       left_column = reorder->right_column = cur_column;
9226
9227       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
9228     }
9229
9230   /* Add the last one */
9231   if (tree_view->priv->column_drop_func == NULL ||
9232       ((left_column != column) &&
9233        tree_view->priv->column_drop_func (tree_view, column, left_column, NULL, tree_view->priv->column_drop_func_data)))
9234     {
9235       reorder = g_slice_new0 (PsppSheetViewColumnReorder);
9236       reorder->left_column = left_column;
9237       reorder->right_column = NULL;
9238       tree_view->priv->column_drag_info = g_list_append (tree_view->priv->column_drag_info, reorder);
9239     }
9240
9241   /* We quickly check to see if it even makes sense to reorder columns. */
9242   /* If there is nothing that can be moved, then we return */
9243
9244   if (tree_view->priv->column_drag_info == NULL)
9245     return;
9246
9247   /* We know there are always 2 slots possbile, as you can always return column. */
9248   /* If that's all there is, return */
9249   if (tree_view->priv->column_drag_info->next == NULL || 
9250       (tree_view->priv->column_drag_info->next->next == NULL &&
9251        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->data)->right_column == column &&
9252        ((PsppSheetViewColumnReorder *)tree_view->priv->column_drag_info->next->data)->left_column == column))
9253     {
9254       for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
9255         g_slice_free (PsppSheetViewColumnReorder, tmp_list->data);
9256       g_list_free (tree_view->priv->column_drag_info);
9257       tree_view->priv->column_drag_info = NULL;
9258       return;
9259     }
9260   /* We fill in the ranges for the columns, now that we've isolated them */
9261   left = - TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
9262
9263   for (tmp_list = tree_view->priv->column_drag_info; tmp_list; tmp_list = tmp_list->next)
9264     {
9265       reorder = (PsppSheetViewColumnReorder *) tmp_list->data;
9266
9267       reorder->left_align = left;
9268       if (tmp_list->next != NULL)
9269         {
9270           g_assert (tmp_list->next->data);
9271           left = reorder->right_align = (reorder->right_column->button->allocation.x +
9272                                          reorder->right_column->button->allocation.width +
9273                                          ((PsppSheetViewColumnReorder *)tmp_list->next->data)->left_column->button->allocation.x)/2;
9274         }
9275       else
9276         {
9277           gint width;
9278
9279           gdk_drawable_get_size (tree_view->priv->header_window, &width, NULL);
9280           reorder->right_align = width + TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER (tree_view);
9281         }
9282     }
9283 }
9284
9285 void
9286 _pspp_sheet_view_column_start_drag (PsppSheetView       *tree_view,
9287                                   PsppSheetViewColumn *column)
9288 {
9289   GdkEvent *send_event;
9290   GtkAllocation allocation;
9291   gint x, y, width, height;
9292   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
9293   GdkDisplay *display = gdk_screen_get_display (screen);
9294
9295   g_return_if_fail (tree_view->priv->column_drag_info == NULL);
9296   g_return_if_fail (tree_view->priv->cur_reorder == NULL);
9297
9298   pspp_sheet_view_set_column_drag_info (tree_view, column);
9299
9300   if (tree_view->priv->column_drag_info == NULL)
9301     return;
9302
9303   if (tree_view->priv->drag_window == NULL)
9304     {
9305       GdkWindowAttr attributes;
9306       guint attributes_mask;
9307
9308       attributes.window_type = GDK_WINDOW_CHILD;
9309       attributes.wclass = GDK_INPUT_OUTPUT;
9310       attributes.x = column->button->allocation.x;
9311       attributes.y = 0;
9312       attributes.width = column->button->allocation.width;
9313       attributes.height = column->button->allocation.height;
9314       attributes.visual = gtk_widget_get_visual (GTK_WIDGET (tree_view));
9315       attributes.colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_view));
9316       attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
9317       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
9318
9319       tree_view->priv->drag_window = gdk_window_new (tree_view->priv->bin_window,
9320                                                      &attributes,
9321                                                      attributes_mask);
9322       gdk_window_set_user_data (tree_view->priv->drag_window, GTK_WIDGET (tree_view));
9323     }
9324
9325   gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
9326   gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
9327
9328   gtk_grab_remove (column->button);
9329
9330   send_event = gdk_event_new (GDK_LEAVE_NOTIFY);
9331   send_event->crossing.send_event = TRUE;
9332   send_event->crossing.window = g_object_ref (GTK_BUTTON (column->button)->event_window);
9333   send_event->crossing.subwindow = NULL;
9334   send_event->crossing.detail = GDK_NOTIFY_ANCESTOR;
9335   send_event->crossing.time = GDK_CURRENT_TIME;
9336
9337   gtk_propagate_event (column->button, send_event);
9338   gdk_event_free (send_event);
9339
9340   send_event = gdk_event_new (GDK_BUTTON_RELEASE);
9341   send_event->button.window = g_object_ref (gdk_screen_get_root_window (screen));
9342   send_event->button.send_event = TRUE;
9343   send_event->button.time = GDK_CURRENT_TIME;
9344   send_event->button.x = -1;
9345   send_event->button.y = -1;
9346   send_event->button.axes = NULL;
9347   send_event->button.state = 0;
9348   send_event->button.button = 1;
9349   send_event->button.device = gdk_display_get_core_pointer (display);
9350   send_event->button.x_root = 0;
9351   send_event->button.y_root = 0;
9352
9353   gtk_propagate_event (column->button, send_event);
9354   gdk_event_free (send_event);
9355
9356   /* Kids, don't try this at home */
9357   g_object_ref (column->button);
9358   gtk_container_remove (GTK_CONTAINER (tree_view), column->button);
9359   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
9360   gtk_widget_set_parent (column->button, GTK_WIDGET (tree_view));
9361   g_object_unref (column->button);
9362
9363   tree_view->priv->drag_column_x = column->button->allocation.x;
9364   allocation = column->button->allocation;
9365   allocation.x = 0;
9366   gtk_widget_size_allocate (column->button, &allocation);
9367   gtk_widget_set_parent_window (column->button, tree_view->priv->drag_window);
9368
9369   tree_view->priv->drag_column = column;
9370   gdk_window_show (tree_view->priv->drag_window);
9371
9372   gdk_window_get_origin (tree_view->priv->header_window, &x, &y);
9373   gdk_drawable_get_size (tree_view->priv->header_window, &width, &height);
9374
9375   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
9376   while (gtk_events_pending ())
9377     gtk_main_iteration ();
9378
9379   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IN_COLUMN_DRAG);
9380   gdk_pointer_grab (tree_view->priv->drag_window,
9381                     FALSE,
9382                     GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK,
9383                     NULL, NULL, GDK_CURRENT_TIME);
9384   gdk_keyboard_grab (tree_view->priv->drag_window,
9385                      FALSE,
9386                      GDK_CURRENT_TIME);
9387 }
9388
9389 static void
9390 pspp_sheet_view_queue_draw_arrow (PsppSheetView        *tree_view,
9391                                 GtkRBTree          *tree,
9392                                 GtkRBNode          *node,
9393                                 const GdkRectangle *clip_rect)
9394 {
9395   GdkRectangle rect;
9396
9397   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9398     return;
9399
9400   rect.x = 0;
9401   rect.width = MAX (tree_view->priv->expander_size, MAX (tree_view->priv->width, GTK_WIDGET (tree_view)->allocation.width));
9402
9403   rect.y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node);
9404   rect.height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
9405
9406   if (clip_rect)
9407     {
9408       GdkRectangle new_rect;
9409
9410       gdk_rectangle_intersect (clip_rect, &rect, &new_rect);
9411
9412       gdk_window_invalidate_rect (tree_view->priv->bin_window, &new_rect, TRUE);
9413     }
9414   else
9415     {
9416       gdk_window_invalidate_rect (tree_view->priv->bin_window, &rect, TRUE);
9417     }
9418 }
9419
9420 void
9421 _pspp_sheet_view_queue_draw_node (PsppSheetView        *tree_view,
9422                                 GtkRBTree          *tree,
9423                                 GtkRBNode          *node,
9424                                 const GdkRectangle *clip_rect)
9425 {
9426   GdkRectangle rect;
9427
9428   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
9429     return;
9430
9431   rect.x = 0;
9432   rect.width = MAX (tree_view->priv->width, GTK_WIDGET (tree_view)->allocation.width);
9433
9434   rect.y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node);
9435   rect.height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
9436
9437   if (clip_rect)
9438     {
9439       GdkRectangle new_rect;
9440
9441       gdk_rectangle_intersect (clip_rect, &rect, &new_rect);
9442
9443       gdk_window_invalidate_rect (tree_view->priv->bin_window, &new_rect, TRUE);
9444     }
9445   else
9446     {
9447       gdk_window_invalidate_rect (tree_view->priv->bin_window, &rect, TRUE);
9448     }
9449 }
9450
9451 static void
9452 pspp_sheet_view_queue_draw_path (PsppSheetView        *tree_view,
9453                                GtkTreePath        *path,
9454                                const GdkRectangle *clip_rect)
9455 {
9456   GtkRBTree *tree = NULL;
9457   GtkRBNode *node = NULL;
9458
9459   _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
9460
9461   if (tree)
9462     _pspp_sheet_view_queue_draw_node (tree_view, tree, node, clip_rect);
9463 }
9464
9465 /* x and y are the mouse position
9466  */
9467 static void
9468 pspp_sheet_view_draw_arrow (PsppSheetView *tree_view,
9469                           GtkRBTree   *tree,
9470                           GtkRBNode   *node,
9471                           /* in bin_window coordinates */
9472                           gint         x,
9473                           gint         y)
9474 {
9475   GdkRectangle area;
9476   GtkStateType state;
9477   GtkWidget *widget;
9478   gint x_offset = 0;
9479   gint x2;
9480   gint vertical_separator;
9481   gint expander_size;
9482   GtkExpanderStyle expander_style;
9483
9484   widget = GTK_WIDGET (tree_view);
9485
9486   gtk_widget_style_get (widget,
9487                         "vertical-separator", &vertical_separator,
9488                         NULL);
9489   expander_size = tree_view->priv->expander_size - EXPANDER_EXTRA_PADDING;
9490
9491   if (! PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT))
9492     return;
9493
9494   pspp_sheet_view_get_arrow_xrange (tree_view, tree, &x_offset, &x2);
9495
9496   area.x = x_offset;
9497   area.y = CELL_FIRST_PIXEL (tree_view, tree, node, vertical_separator);
9498   area.width = expander_size + 2;
9499   area.height = MAX (CELL_HEIGHT (node, vertical_separator), (expander_size - vertical_separator));
9500
9501   if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
9502     {
9503       state = GTK_STATE_INSENSITIVE;
9504     }
9505   else if (node == tree_view->priv->button_pressed_node)
9506     {
9507       if (x >= area.x && x <= (area.x + area.width) &&
9508           y >= area.y && y <= (area.y + area.height))
9509         state = GTK_STATE_ACTIVE;
9510       else
9511         state = GTK_STATE_NORMAL;
9512     }
9513   else
9514     {
9515       if (node == tree_view->priv->prelight_node &&
9516           PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_ARROW_PRELIT))
9517         state = GTK_STATE_PRELIGHT;
9518       else
9519         state = GTK_STATE_NORMAL;
9520     }
9521
9522   if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_EXPANDED))
9523     expander_style = GTK_EXPANDER_SEMI_EXPANDED;
9524   else if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_COLLAPSED))
9525     expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
9526   else if (node->children != NULL)
9527     expander_style = GTK_EXPANDER_EXPANDED;
9528   else
9529     expander_style = GTK_EXPANDER_COLLAPSED;
9530
9531   gtk_paint_expander (widget->style,
9532                       tree_view->priv->bin_window,
9533                       state,
9534                       &area,
9535                       widget,
9536                       "treeview",
9537                       area.x + area.width / 2,
9538                       area.y + area.height / 2,
9539                       expander_style);
9540 }
9541
9542 static void
9543 pspp_sheet_view_focus_to_cursor (PsppSheetView *tree_view)
9544
9545 {
9546   GtkTreePath *cursor_path;
9547
9548   if ((tree_view->priv->tree == NULL) ||
9549       (! gtk_widget_get_realized (GTK_WIDGET (tree_view))))
9550     return;
9551
9552   cursor_path = NULL;
9553   if (tree_view->priv->cursor)
9554     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
9555
9556   if (cursor_path == NULL)
9557     {
9558       /* Consult the selection before defaulting to the
9559        * first focusable element
9560        */
9561       GList *selected_rows;
9562       GtkTreeModel *model;
9563       PsppSheetSelection *selection;
9564
9565       selection = pspp_sheet_view_get_selection (tree_view);
9566       selected_rows = pspp_sheet_selection_get_selected_rows (selection, &model);
9567
9568       if (selected_rows)
9569         {
9570           cursor_path = gtk_tree_path_copy((const GtkTreePath *)(selected_rows->data));
9571           g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
9572           g_list_free (selected_rows);
9573         }
9574       else
9575         {
9576           cursor_path = gtk_tree_path_new_first ();
9577           search_first_focusable_path (tree_view, &cursor_path,
9578                                        TRUE, NULL, NULL);
9579         }
9580
9581       gtk_tree_row_reference_free (tree_view->priv->cursor);
9582       tree_view->priv->cursor = NULL;
9583
9584       if (cursor_path)
9585         {
9586           if (tree_view->priv->selection->type == GTK_SELECTION_MULTIPLE)
9587             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, FALSE, FALSE);
9588           else
9589             pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE);
9590         }
9591     }
9592
9593   if (cursor_path)
9594     {
9595       PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
9596
9597       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
9598       gtk_tree_path_free (cursor_path);
9599
9600       if (tree_view->priv->focus_column == NULL)
9601         {
9602           GList *list;
9603           for (list = tree_view->priv->columns; list; list = list->next)
9604             {
9605               if (PSPP_SHEET_VIEW_COLUMN (list->data)->visible)
9606                 {
9607                   tree_view->priv->focus_column = PSPP_SHEET_VIEW_COLUMN (list->data);
9608                   break;
9609                 }
9610             }
9611         }
9612     }
9613 }
9614
9615 static void
9616 pspp_sheet_view_move_cursor_up_down (PsppSheetView *tree_view,
9617                                    gint         count)
9618 {
9619   gint selection_count;
9620   GtkRBTree *cursor_tree = NULL;
9621   GtkRBNode *cursor_node = NULL;
9622   GtkRBTree *new_cursor_tree = NULL;
9623   GtkRBNode *new_cursor_node = NULL;
9624   GtkTreePath *cursor_path = NULL;
9625   gboolean grab_focus = TRUE;
9626   gboolean selectable;
9627
9628   if (! gtk_widget_has_focus (GTK_WIDGET (tree_view)))
9629     return;
9630
9631   cursor_path = NULL;
9632   if (!gtk_tree_row_reference_valid (tree_view->priv->cursor))
9633     /* FIXME: we lost the cursor; should we get the first? */
9634     return;
9635
9636   cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
9637   _pspp_sheet_view_find_node (tree_view, cursor_path,
9638                             &cursor_tree, &cursor_node);
9639
9640   if (cursor_tree == NULL)
9641     /* FIXME: we lost the cursor; should we get the first? */
9642     return;
9643
9644   selection_count = pspp_sheet_selection_count_selected_rows (tree_view->priv->selection);
9645   selectable = _pspp_sheet_selection_row_is_selectable (tree_view->priv->selection,
9646                                                       cursor_node,
9647                                                       cursor_path);
9648
9649   if (selection_count == 0
9650       && tree_view->priv->selection->type != GTK_SELECTION_NONE
9651       && !tree_view->priv->ctrl_pressed
9652       && selectable)
9653     {
9654       /* Don't move the cursor, but just select the current node */
9655       new_cursor_tree = cursor_tree;
9656       new_cursor_node = cursor_node;
9657     }
9658   else
9659     {
9660       if (count == -1)
9661         _pspp_rbtree_prev_full (cursor_tree, cursor_node,
9662                                &new_cursor_tree, &new_cursor_node);
9663       else
9664         _pspp_rbtree_next_full (cursor_tree, cursor_node,
9665                                &new_cursor_tree, &new_cursor_node);
9666     }
9667
9668   gtk_tree_path_free (cursor_path);
9669
9670   if (new_cursor_node)
9671     {
9672       cursor_path = _pspp_sheet_view_find_path (tree_view,
9673                                               new_cursor_tree, new_cursor_node);
9674
9675       search_first_focusable_path (tree_view, &cursor_path,
9676                                    (count != -1),
9677                                    &new_cursor_tree,
9678                                    &new_cursor_node);
9679
9680       if (cursor_path)
9681         gtk_tree_path_free (cursor_path);
9682     }
9683
9684   /*
9685    * If the list has only one item and multi-selection is set then select
9686    * the row (if not yet selected).
9687    */
9688   if (tree_view->priv->selection->type == GTK_SELECTION_MULTIPLE &&
9689       new_cursor_node == NULL)
9690     {
9691       if (count == -1)
9692         _pspp_rbtree_next_full (cursor_tree, cursor_node,
9693                                &new_cursor_tree, &new_cursor_node);
9694       else
9695         _pspp_rbtree_prev_full (cursor_tree, cursor_node,
9696                                &new_cursor_tree, &new_cursor_node);
9697
9698       if (new_cursor_node == NULL
9699           && !PSPP_RBNODE_FLAG_SET (cursor_node, PSPP_RBNODE_IS_SELECTED))
9700         {
9701           new_cursor_node = cursor_node;
9702           new_cursor_tree = cursor_tree;
9703         }
9704       else
9705         {
9706           new_cursor_node = NULL;
9707         }
9708     }
9709
9710   if (new_cursor_node)
9711     {
9712       cursor_path = _pspp_sheet_view_find_path (tree_view, new_cursor_tree, new_cursor_node);
9713       pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, TRUE);
9714       gtk_tree_path_free (cursor_path);
9715     }
9716   else
9717     {
9718       pspp_sheet_view_clamp_node_visible (tree_view, cursor_tree, cursor_node);
9719
9720       if (!tree_view->priv->shift_pressed)
9721         {
9722           if (! gtk_widget_keynav_failed (GTK_WIDGET (tree_view),
9723                                           count < 0 ?
9724                                           GTK_DIR_UP : GTK_DIR_DOWN))
9725             {
9726               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
9727
9728               if (toplevel)
9729                 gtk_widget_child_focus (toplevel,
9730                                         count < 0 ?
9731                                         GTK_DIR_TAB_BACKWARD :
9732                                         GTK_DIR_TAB_FORWARD);
9733
9734               grab_focus = FALSE;
9735             }
9736         }
9737       else
9738         {
9739           gtk_widget_error_bell (GTK_WIDGET (tree_view));
9740         }
9741     }
9742
9743   if (grab_focus)
9744     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
9745 }
9746
9747 static void
9748 pspp_sheet_view_move_cursor_page_up_down (PsppSheetView *tree_view,
9749                                         gint         count)
9750 {
9751   GtkRBTree *cursor_tree = NULL;
9752   GtkRBNode *cursor_node = NULL;
9753   GtkTreePath *old_cursor_path = NULL;
9754   GtkTreePath *cursor_path = NULL;
9755   GtkRBTree *start_cursor_tree = NULL;
9756   GtkRBNode *start_cursor_node = NULL;
9757   gint y;
9758   gint window_y;
9759   gint vertical_separator;
9760
9761   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
9762     return;
9763
9764   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
9765     old_cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
9766   else
9767     /* This is sorta weird.  Focus in should give us a cursor */
9768     return;
9769
9770   gtk_widget_style_get (GTK_WIDGET (tree_view), "vertical-separator", &vertical_separator, NULL);
9771   _pspp_sheet_view_find_node (tree_view, old_cursor_path,
9772                             &cursor_tree, &cursor_node);
9773
9774   if (cursor_tree == NULL)
9775     {
9776       /* FIXME: we lost the cursor.  Should we try to get one? */
9777       gtk_tree_path_free (old_cursor_path);
9778       return;
9779     }
9780   g_return_if_fail (cursor_node != NULL);
9781
9782   y = _pspp_rbtree_node_find_offset (cursor_tree, cursor_node);
9783   window_y = RBTREE_Y_TO_TREE_WINDOW_Y (tree_view, y);
9784   y += tree_view->priv->cursor_offset;
9785   y += count * (int)tree_view->priv->vadjustment->page_increment;
9786   y = CLAMP (y, (gint)tree_view->priv->vadjustment->lower,  (gint)tree_view->priv->vadjustment->upper - vertical_separator);
9787
9788   if (y >= tree_view->priv->height)
9789     y = tree_view->priv->height - 1;
9790
9791   tree_view->priv->cursor_offset =
9792     _pspp_rbtree_find_offset (tree_view->priv->tree, y,
9793                              &cursor_tree, &cursor_node);
9794
9795   if (tree_view->priv->cursor_offset > BACKGROUND_HEIGHT (cursor_node))
9796     {
9797       _pspp_rbtree_next_full (cursor_tree, cursor_node,
9798                              &cursor_tree, &cursor_node);
9799       tree_view->priv->cursor_offset -= BACKGROUND_HEIGHT (cursor_node);
9800     }
9801
9802   y -= tree_view->priv->cursor_offset;
9803   cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_tree, cursor_node);
9804
9805   start_cursor_tree = cursor_tree;
9806   start_cursor_node = cursor_node;
9807
9808   if (! search_first_focusable_path (tree_view, &cursor_path,
9809                                      (count != -1),
9810                                      &cursor_tree, &cursor_node))
9811     {
9812       /* It looks like we reached the end of the view without finding
9813        * a focusable row.  We will step backwards to find the last
9814        * focusable row.
9815        */
9816       cursor_tree = start_cursor_tree;
9817       cursor_node = start_cursor_node;
9818       cursor_path = _pspp_sheet_view_find_path (tree_view, cursor_tree, cursor_node);
9819
9820       search_first_focusable_path (tree_view, &cursor_path,
9821                                    (count == -1),
9822                                    &cursor_tree, &cursor_node);
9823     }
9824
9825   if (!cursor_path)
9826     goto cleanup;
9827
9828   /* update y */
9829   y = _pspp_rbtree_node_find_offset (cursor_tree, cursor_node);
9830
9831   pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE);
9832
9833   y -= window_y;
9834   pspp_sheet_view_scroll_to_point (tree_view, -1, y);
9835   pspp_sheet_view_clamp_node_visible (tree_view, cursor_tree, cursor_node);
9836   _pspp_sheet_view_queue_draw_node (tree_view, cursor_tree, cursor_node, NULL);
9837
9838   if (!gtk_tree_path_compare (old_cursor_path, cursor_path))
9839     gtk_widget_error_bell (GTK_WIDGET (tree_view));
9840
9841   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
9842
9843 cleanup:
9844   gtk_tree_path_free (old_cursor_path);
9845   gtk_tree_path_free (cursor_path);
9846 }
9847
9848 static void
9849 pspp_sheet_view_move_cursor_left_right (PsppSheetView *tree_view,
9850                                       gint         count)
9851 {
9852   GtkRBTree *cursor_tree = NULL;
9853   GtkRBNode *cursor_node = NULL;
9854   GtkTreePath *cursor_path = NULL;
9855   PsppSheetViewColumn *column;
9856   GtkTreeIter iter;
9857   GList *list;
9858   gboolean found_column = FALSE;
9859   gboolean rtl;
9860
9861   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
9862
9863   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
9864     return;
9865
9866   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
9867     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
9868   else
9869     return;
9870
9871   _pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_tree, &cursor_node);
9872   if (cursor_tree == NULL)
9873     return;
9874   if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path) == FALSE)
9875     {
9876       gtk_tree_path_free (cursor_path);
9877       return;
9878     }
9879   gtk_tree_path_free (cursor_path);
9880
9881   list = rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns);
9882   if (tree_view->priv->focus_column)
9883     {
9884       for (; list; list = (rtl ? list->prev : list->next))
9885         {
9886           if (list->data == tree_view->priv->focus_column)
9887             break;
9888         }
9889     }
9890
9891   while (list)
9892     {
9893       gboolean left, right;
9894
9895       column = list->data;
9896       if (column->visible == FALSE)
9897         goto loop_end;
9898
9899       pspp_sheet_view_column_cell_set_cell_data (column,
9900                                                tree_view->priv->model,
9901                                                &iter,
9902                                                PSPP_RBNODE_FLAG_SET (cursor_node, PSPP_RBNODE_IS_PARENT),
9903                                                cursor_node->children?TRUE:FALSE);
9904
9905       if (rtl)
9906         {
9907           right = list->prev ? TRUE : FALSE;
9908           left = list->next ? TRUE : FALSE;
9909         }
9910       else
9911         {
9912           left = list->prev ? TRUE : FALSE;
9913           right = list->next ? TRUE : FALSE;
9914         }
9915
9916       if (_pspp_sheet_view_column_cell_focus (column, count, left, right))
9917         {
9918           tree_view->priv->focus_column = column;
9919           found_column = TRUE;
9920           break;
9921         }
9922     loop_end:
9923       if (count == 1)
9924         list = rtl ? list->prev : list->next;
9925       else
9926         list = rtl ? list->next : list->prev;
9927     }
9928
9929   if (found_column)
9930     {
9931       if (!pspp_sheet_view_has_special_cell (tree_view))
9932         _pspp_sheet_view_queue_draw_node (tree_view,
9933                                         cursor_tree,
9934                                         cursor_node,
9935                                         NULL);
9936       g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
9937       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
9938     }
9939   else
9940     {
9941       gtk_widget_error_bell (GTK_WIDGET (tree_view));
9942     }
9943
9944   pspp_sheet_view_clamp_column_visible (tree_view,
9945                                       tree_view->priv->focus_column, TRUE);
9946 }
9947
9948 static void
9949 pspp_sheet_view_move_cursor_start_end (PsppSheetView *tree_view,
9950                                      gint         count)
9951 {
9952   GtkRBTree *cursor_tree;
9953   GtkRBNode *cursor_node;
9954   GtkTreePath *path;
9955   GtkTreePath *old_path;
9956
9957   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
9958     return;
9959
9960   g_return_if_fail (tree_view->priv->tree != NULL);
9961
9962   pspp_sheet_view_get_cursor (tree_view, &old_path, NULL);
9963
9964   cursor_tree = tree_view->priv->tree;
9965   cursor_node = cursor_tree->root;
9966
9967   if (count == -1)
9968     {
9969       while (cursor_node && cursor_node->left != cursor_tree->nil)
9970         cursor_node = cursor_node->left;
9971
9972       /* Now go forward to find the first focusable row. */
9973       path = _pspp_sheet_view_find_path (tree_view, cursor_tree, cursor_node);
9974       search_first_focusable_path (tree_view, &path,
9975                                    TRUE, &cursor_tree, &cursor_node);
9976     }
9977   else
9978     {
9979       do
9980         {
9981           while (cursor_node && cursor_node->right != cursor_tree->nil)
9982             cursor_node = cursor_node->right;
9983           if (cursor_node->children == NULL)
9984             break;
9985
9986           cursor_tree = cursor_node->children;
9987           cursor_node = cursor_tree->root;
9988         }
9989       while (1);
9990
9991       /* Now go backwards to find last focusable row. */
9992       path = _pspp_sheet_view_find_path (tree_view, cursor_tree, cursor_node);
9993       search_first_focusable_path (tree_view, &path,
9994                                    FALSE, &cursor_tree, &cursor_node);
9995     }
9996
9997   if (!path)
9998     goto cleanup;
9999
10000   if (gtk_tree_path_compare (old_path, path))
10001     {
10002       pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE);
10003       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
10004     }
10005   else
10006     {
10007       gtk_widget_error_bell (GTK_WIDGET (tree_view));
10008     }
10009
10010 cleanup:
10011   gtk_tree_path_free (old_path);
10012   gtk_tree_path_free (path);
10013 }
10014
10015 static gboolean
10016 pspp_sheet_view_real_select_all (PsppSheetView *tree_view)
10017 {
10018   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10019     return FALSE;
10020
10021   if (tree_view->priv->selection->type != GTK_SELECTION_MULTIPLE)
10022     return FALSE;
10023
10024   pspp_sheet_selection_select_all (tree_view->priv->selection);
10025
10026   return TRUE;
10027 }
10028
10029 static gboolean
10030 pspp_sheet_view_real_unselect_all (PsppSheetView *tree_view)
10031 {
10032   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10033     return FALSE;
10034
10035   if (tree_view->priv->selection->type != GTK_SELECTION_MULTIPLE)
10036     return FALSE;
10037
10038   pspp_sheet_selection_unselect_all (tree_view->priv->selection);
10039
10040   return TRUE;
10041 }
10042
10043 static gboolean
10044 pspp_sheet_view_real_select_cursor_row (PsppSheetView *tree_view,
10045                                       gboolean     start_editing)
10046 {
10047   GtkRBTree *new_tree = NULL;
10048   GtkRBNode *new_node = NULL;
10049   GtkRBTree *cursor_tree = NULL;
10050   GtkRBNode *cursor_node = NULL;
10051   GtkTreePath *cursor_path = NULL;
10052   GtkTreeSelectMode mode = 0;
10053
10054   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10055     return FALSE;
10056
10057   if (tree_view->priv->cursor)
10058     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10059
10060   if (cursor_path == NULL)
10061     return FALSE;
10062
10063   _pspp_sheet_view_find_node (tree_view, cursor_path,
10064                             &cursor_tree, &cursor_node);
10065
10066   if (cursor_tree == NULL)
10067     {
10068       gtk_tree_path_free (cursor_path);
10069       return FALSE;
10070     }
10071
10072   if (!tree_view->priv->shift_pressed && start_editing &&
10073       tree_view->priv->focus_column)
10074     {
10075       if (pspp_sheet_view_start_editing (tree_view, cursor_path))
10076         {
10077           gtk_tree_path_free (cursor_path);
10078           return TRUE;
10079         }
10080     }
10081
10082   if (tree_view->priv->ctrl_pressed)
10083     mode |= GTK_TREE_SELECT_MODE_TOGGLE;
10084   if (tree_view->priv->shift_pressed)
10085     mode |= GTK_TREE_SELECT_MODE_EXTEND;
10086
10087   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
10088                                             cursor_node,
10089                                             cursor_tree,
10090                                             cursor_path,
10091                                             mode,
10092                                             FALSE);
10093
10094   /* We bail out if the original (tree, node) don't exist anymore after
10095    * handling the selection-changed callback.  We do return TRUE because
10096    * the key press has been handled at this point.
10097    */
10098   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_tree, &new_node);
10099
10100   if (cursor_tree != new_tree || cursor_node != new_node)
10101     return FALSE;
10102
10103   pspp_sheet_view_clamp_node_visible (tree_view, cursor_tree, cursor_node);
10104
10105   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
10106   _pspp_sheet_view_queue_draw_node (tree_view, cursor_tree, cursor_node, NULL);
10107
10108   if (!tree_view->priv->shift_pressed)
10109     pspp_sheet_view_row_activated (tree_view, cursor_path,
10110                                  tree_view->priv->focus_column);
10111     
10112   gtk_tree_path_free (cursor_path);
10113
10114   return TRUE;
10115 }
10116
10117 static gboolean
10118 pspp_sheet_view_real_toggle_cursor_row (PsppSheetView *tree_view)
10119 {
10120   GtkRBTree *new_tree = NULL;
10121   GtkRBNode *new_node = NULL;
10122   GtkRBTree *cursor_tree = NULL;
10123   GtkRBNode *cursor_node = NULL;
10124   GtkTreePath *cursor_path = NULL;
10125
10126   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10127     return FALSE;
10128
10129   cursor_path = NULL;
10130   if (tree_view->priv->cursor)
10131     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10132
10133   if (cursor_path == NULL)
10134     return FALSE;
10135
10136   _pspp_sheet_view_find_node (tree_view, cursor_path,
10137                             &cursor_tree, &cursor_node);
10138   if (cursor_tree == NULL)
10139     {
10140       gtk_tree_path_free (cursor_path);
10141       return FALSE;
10142     }
10143
10144   _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
10145                                             cursor_node,
10146                                             cursor_tree,
10147                                             cursor_path,
10148                                             GTK_TREE_SELECT_MODE_TOGGLE,
10149                                             FALSE);
10150
10151   /* We bail out if the original (tree, node) don't exist anymore after
10152    * handling the selection-changed callback.  We do return TRUE because
10153    * the key press has been handled at this point.
10154    */
10155   _pspp_sheet_view_find_node (tree_view, cursor_path, &new_tree, &new_node);
10156
10157   if (cursor_tree != new_tree || cursor_node != new_node)
10158     return FALSE;
10159
10160   pspp_sheet_view_clamp_node_visible (tree_view, cursor_tree, cursor_node);
10161
10162   gtk_widget_grab_focus (GTK_WIDGET (tree_view));
10163   pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10164   gtk_tree_path_free (cursor_path);
10165
10166   return TRUE;
10167 }
10168
10169 static gboolean
10170 pspp_sheet_view_real_expand_collapse_cursor_row (PsppSheetView *tree_view,
10171                                                gboolean     logical,
10172                                                gboolean     expand,
10173                                                gboolean     open_all)
10174 {
10175   GtkTreePath *cursor_path = NULL;
10176   GtkRBTree *tree;
10177   GtkRBNode *node;
10178
10179   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10180     return FALSE;
10181
10182   cursor_path = NULL;
10183   if (tree_view->priv->cursor)
10184     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10185
10186   if (cursor_path == NULL)
10187     return FALSE;
10188
10189   if (_pspp_sheet_view_find_node (tree_view, cursor_path, &tree, &node))
10190     return FALSE;
10191
10192   /* Don't handle the event if we aren't an expander */
10193   if (!((node->flags & PSPP_RBNODE_IS_PARENT) == PSPP_RBNODE_IS_PARENT))
10194     return FALSE;
10195
10196   if (!logical
10197       && gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL)
10198     expand = !expand;
10199
10200   if (expand)
10201     pspp_sheet_view_real_expand_row (tree_view, cursor_path, tree, node, open_all, TRUE);
10202   else
10203     pspp_sheet_view_real_collapse_row (tree_view, cursor_path, tree, node, TRUE);
10204
10205   gtk_tree_path_free (cursor_path);
10206
10207   return TRUE;
10208 }
10209
10210 static gboolean
10211 pspp_sheet_view_real_select_cursor_parent (PsppSheetView *tree_view)
10212 {
10213   GtkRBTree *cursor_tree = NULL;
10214   GtkRBNode *cursor_node = NULL;
10215   GtkTreePath *cursor_path = NULL;
10216   GdkModifierType state;
10217
10218   if (!gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10219     goto out;
10220
10221   cursor_path = NULL;
10222   if (tree_view->priv->cursor)
10223     cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
10224
10225   if (cursor_path == NULL)
10226     goto out;
10227
10228   _pspp_sheet_view_find_node (tree_view, cursor_path,
10229                             &cursor_tree, &cursor_node);
10230   if (cursor_tree == NULL)
10231     {
10232       gtk_tree_path_free (cursor_path);
10233       goto out;
10234     }
10235
10236   if (cursor_tree->parent_node)
10237     {
10238       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10239       cursor_node = cursor_tree->parent_node;
10240       cursor_tree = cursor_tree->parent_tree;
10241
10242       gtk_tree_path_up (cursor_path);
10243
10244       if (gtk_get_current_event_state (&state))
10245         {
10246           if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
10247             tree_view->priv->ctrl_pressed = TRUE;
10248         }
10249
10250       pspp_sheet_view_real_set_cursor (tree_view, cursor_path, TRUE, FALSE);
10251       pspp_sheet_view_clamp_node_visible (tree_view, cursor_tree, cursor_node);
10252
10253       gtk_widget_grab_focus (GTK_WIDGET (tree_view));
10254       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
10255       gtk_tree_path_free (cursor_path);
10256
10257       tree_view->priv->ctrl_pressed = FALSE;
10258
10259       return TRUE;
10260     }
10261
10262  out:
10263
10264   tree_view->priv->search_entry_avoid_unhandled_binding = TRUE;
10265   return FALSE;
10266 }
10267
10268 static gboolean
10269 pspp_sheet_view_search_entry_flush_timeout (PsppSheetView *tree_view)
10270 {
10271   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window, tree_view);
10272   tree_view->priv->typeselect_flush_timeout = 0;
10273
10274   return FALSE;
10275 }
10276
10277 /* Cut and paste from gtkwindow.c */
10278 static void
10279 send_focus_change (GtkWidget *widget,
10280                    gboolean   in)
10281 {
10282   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
10283
10284   g_object_ref (widget);
10285    
10286  if (in)
10287     GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
10288   else
10289     GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
10290
10291   fevent->focus_change.type = GDK_FOCUS_CHANGE;
10292   fevent->focus_change.window = g_object_ref (widget->window);
10293   fevent->focus_change.in = in;
10294   
10295   gtk_widget_event (widget, fevent);
10296   
10297   g_object_notify (G_OBJECT (widget), "has-focus");
10298
10299   g_object_unref (widget);
10300   gdk_event_free (fevent);
10301 }
10302
10303 static void
10304 pspp_sheet_view_ensure_interactive_directory (PsppSheetView *tree_view)
10305 {
10306   GtkWidget *frame, *vbox, *toplevel;
10307   GdkScreen *screen;
10308
10309   if (tree_view->priv->search_custom_entry_set)
10310     return;
10311
10312   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tree_view));
10313   screen = gtk_widget_get_screen (GTK_WIDGET (tree_view));
10314
10315    if (tree_view->priv->search_window != NULL)
10316      {
10317        if (GTK_WINDOW (toplevel)->group)
10318          gtk_window_group_add_window (GTK_WINDOW (toplevel)->group,
10319                                       GTK_WINDOW (tree_view->priv->search_window));
10320        else if (GTK_WINDOW (tree_view->priv->search_window)->group)
10321          gtk_window_group_remove_window (GTK_WINDOW (tree_view->priv->search_window)->group,
10322                                          GTK_WINDOW (tree_view->priv->search_window));
10323        gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
10324        return;
10325      }
10326    
10327   tree_view->priv->search_window = gtk_window_new (GTK_WINDOW_POPUP);
10328   gtk_window_set_screen (GTK_WINDOW (tree_view->priv->search_window), screen);
10329
10330   if (GTK_WINDOW (toplevel)->group)
10331     gtk_window_group_add_window (GTK_WINDOW (toplevel)->group,
10332                                  GTK_WINDOW (tree_view->priv->search_window));
10333
10334   gtk_window_set_type_hint (GTK_WINDOW (tree_view->priv->search_window),
10335                             GDK_WINDOW_TYPE_HINT_UTILITY);
10336   gtk_window_set_modal (GTK_WINDOW (tree_view->priv->search_window), TRUE);
10337   g_signal_connect (tree_view->priv->search_window, "delete-event",
10338                     G_CALLBACK (pspp_sheet_view_search_delete_event),
10339                     tree_view);
10340   g_signal_connect (tree_view->priv->search_window, "key-press-event",
10341                     G_CALLBACK (pspp_sheet_view_search_key_press_event),
10342                     tree_view);
10343   g_signal_connect (tree_view->priv->search_window, "button-press-event",
10344                     G_CALLBACK (pspp_sheet_view_search_button_press_event),
10345                     tree_view);
10346   g_signal_connect (tree_view->priv->search_window, "scroll-event",
10347                     G_CALLBACK (pspp_sheet_view_search_scroll_event),
10348                     tree_view);
10349
10350   frame = gtk_frame_new (NULL);
10351   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
10352   gtk_widget_show (frame);
10353   gtk_container_add (GTK_CONTAINER (tree_view->priv->search_window), frame);
10354
10355   vbox = gtk_vbox_new (FALSE, 0);
10356   gtk_widget_show (vbox);
10357   gtk_container_add (GTK_CONTAINER (frame), vbox);
10358   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
10359
10360   /* add entry */
10361   tree_view->priv->search_entry = gtk_entry_new ();
10362   gtk_widget_show (tree_view->priv->search_entry);
10363   g_signal_connect (tree_view->priv->search_entry, "populate-popup",
10364                     G_CALLBACK (pspp_sheet_view_search_disable_popdown),
10365                     tree_view);
10366   g_signal_connect (tree_view->priv->search_entry,
10367                     "activate", G_CALLBACK (pspp_sheet_view_search_activate),
10368                     tree_view);
10369   g_signal_connect (GTK_ENTRY (tree_view->priv->search_entry)->im_context,
10370                     "preedit-changed",
10371                     G_CALLBACK (pspp_sheet_view_search_preedit_changed),
10372                     tree_view);
10373   gtk_container_add (GTK_CONTAINER (vbox),
10374                      tree_view->priv->search_entry);
10375
10376   gtk_widget_realize (tree_view->priv->search_entry);
10377 }
10378
10379 /* Pops up the interactive search entry.  If keybinding is TRUE then the user
10380  * started this by typing the start_interactive_search keybinding.  Otherwise, it came from 
10381  */
10382 static gboolean
10383 pspp_sheet_view_real_start_interactive_search (PsppSheetView *tree_view,
10384                                              gboolean     keybinding)
10385 {
10386   /* We only start interactive search if we have focus or the columns
10387    * have focus.  If one of our children have focus, we don't want to
10388    * start the search.
10389    */
10390   GList *list;
10391   gboolean found_focus = FALSE;
10392   GtkWidgetClass *entry_parent_class;
10393   
10394   if (!tree_view->priv->enable_search && !keybinding)
10395     return FALSE;
10396
10397   if (tree_view->priv->search_custom_entry_set)
10398     return FALSE;
10399
10400   if (tree_view->priv->search_window != NULL &&
10401       gtk_widget_get_visible (tree_view->priv->search_window))
10402     return TRUE;
10403
10404   for (list = tree_view->priv->columns; list; list = list->next)
10405     {
10406       PsppSheetViewColumn *column;
10407
10408       column = list->data;
10409       if (! column->visible)
10410         continue;
10411
10412       if (gtk_widget_has_focus (column->button))
10413         {
10414           found_focus = TRUE;
10415           break;
10416         }
10417     }
10418   
10419   if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
10420     found_focus = TRUE;
10421
10422   if (!found_focus)
10423     return FALSE;
10424
10425   if (tree_view->priv->search_column < 0)
10426     return FALSE;
10427
10428   pspp_sheet_view_ensure_interactive_directory (tree_view);
10429
10430   if (keybinding)
10431     gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
10432
10433   /* done, show it */
10434   tree_view->priv->search_position_func (tree_view, tree_view->priv->search_window, tree_view->priv->search_position_user_data);
10435   gtk_widget_show (tree_view->priv->search_window);
10436   if (tree_view->priv->search_entry_changed_id == 0)
10437     {
10438       tree_view->priv->search_entry_changed_id =
10439         g_signal_connect (tree_view->priv->search_entry, "changed",
10440                           G_CALLBACK (pspp_sheet_view_search_init),
10441                           tree_view);
10442     }
10443
10444   tree_view->priv->typeselect_flush_timeout =
10445     gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
10446                    (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
10447                    tree_view);
10448
10449   /* Grab focus will select all the text.  We don't want that to happen, so we
10450    * call the parent instance and bypass the selection change.  This is probably
10451    * really non-kosher. */
10452   entry_parent_class = g_type_class_peek_parent (GTK_ENTRY_GET_CLASS (tree_view->priv->search_entry));
10453   (entry_parent_class->grab_focus) (tree_view->priv->search_entry);
10454
10455   /* send focus-in event */
10456   send_focus_change (tree_view->priv->search_entry, TRUE);
10457
10458   /* search first matching iter */
10459   pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
10460
10461   return TRUE;
10462 }
10463
10464 static gboolean
10465 pspp_sheet_view_start_interactive_search (PsppSheetView *tree_view)
10466 {
10467   return pspp_sheet_view_real_start_interactive_search (tree_view, TRUE);
10468 }
10469
10470 /* this function returns the new width of the column being resized given
10471  * the column and x position of the cursor; the x cursor position is passed
10472  * in as a pointer and automagicly corrected if it's beyond min/max limits
10473  */
10474 static gint
10475 pspp_sheet_view_new_column_width (PsppSheetView *tree_view,
10476                                 gint       i,
10477                                 gint      *x)
10478 {
10479   PsppSheetViewColumn *column;
10480   gint width;
10481   gboolean rtl;
10482
10483   /* first translate the x position from widget->window
10484    * to clist->clist_window
10485    */
10486   rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
10487   column = g_list_nth (tree_view->priv->columns, i)->data;
10488   width = rtl ? (column->button->allocation.x + column->button->allocation.width - *x) : (*x - column->button->allocation.x);
10489  
10490   /* Clamp down the value */
10491   if (column->min_width == -1)
10492     width = MAX (column->button->requisition.width,
10493                  width);
10494   else
10495     width = MAX (column->min_width,
10496                  width);
10497   if (column->max_width != -1)
10498     width = MIN (width, column->max_width);
10499
10500   *x = rtl ? (column->button->allocation.x + column->button->allocation.width - width) : (column->button->allocation.x + width);
10501  
10502   return width;
10503 }
10504
10505
10506 /* FIXME this adjust_allocation is a big cut-and-paste from
10507  * GtkCList, needs to be some "official" way to do this
10508  * factored out.
10509  */
10510 typedef struct
10511 {
10512   GdkWindow *window;
10513   int dx;
10514   int dy;
10515 } ScrollData;
10516
10517 /* The window to which widget->window is relative */
10518 #define ALLOCATION_WINDOW(widget)               \
10519    (!gtk_widget_get_has_window (widget) ?               \
10520     (widget)->window :                          \
10521      gdk_window_get_parent ((widget)->window))
10522
10523 static void
10524 adjust_allocation_recurse (GtkWidget *widget,
10525                            gpointer   data)
10526 {
10527   ScrollData *scroll_data = data;
10528
10529   /* Need to really size allocate instead of just poking
10530    * into widget->allocation if the widget is not realized.
10531    * FIXME someone figure out why this was.
10532    */
10533   if (!gtk_widget_get_realized (widget))
10534     {
10535       if (gtk_widget_get_visible (widget))
10536         {
10537           GdkRectangle tmp_rectangle = widget->allocation;
10538           tmp_rectangle.x += scroll_data->dx;
10539           tmp_rectangle.y += scroll_data->dy;
10540           
10541           gtk_widget_size_allocate (widget, &tmp_rectangle);
10542         }
10543     }
10544   else
10545     {
10546       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
10547         {
10548           widget->allocation.x += scroll_data->dx;
10549           widget->allocation.y += scroll_data->dy;
10550           
10551           if (GTK_IS_CONTAINER (widget))
10552             gtk_container_forall (GTK_CONTAINER (widget),
10553                                   adjust_allocation_recurse,
10554                                   data);
10555         }
10556     }
10557 }
10558
10559 static void
10560 adjust_allocation (GtkWidget *widget,
10561                    int        dx,
10562                    int        dy)
10563 {
10564   ScrollData scroll_data;
10565
10566   if (gtk_widget_get_realized (widget))
10567     scroll_data.window = ALLOCATION_WINDOW (widget);
10568   else
10569     scroll_data.window = NULL;
10570     
10571   scroll_data.dx = dx;
10572   scroll_data.dy = dy;
10573   
10574   adjust_allocation_recurse (widget, &scroll_data);
10575 }
10576
10577 /* Callbacks */
10578 static void
10579 pspp_sheet_view_adjustment_changed (GtkAdjustment *adjustment,
10580                                   PsppSheetView   *tree_view)
10581 {
10582   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
10583     {
10584       gint dy;
10585         
10586       gdk_window_move (tree_view->priv->bin_window,
10587                        - tree_view->priv->hadjustment->value,
10588                        TREE_VIEW_HEADER_HEIGHT (tree_view));
10589       gdk_window_move (tree_view->priv->header_window,
10590                        - tree_view->priv->hadjustment->value,
10591                        0);
10592       dy = tree_view->priv->dy - (int) tree_view->priv->vadjustment->value;
10593       if (dy)
10594         {
10595           update_prelight (tree_view,
10596                            tree_view->priv->event_last_x,
10597                            tree_view->priv->event_last_y - dy);
10598
10599           if (tree_view->priv->edited_column &&
10600               GTK_IS_WIDGET (tree_view->priv->edited_column->editable_widget))
10601             {
10602               GList *list;
10603               GtkWidget *widget;
10604               PsppSheetViewChild *child = NULL;
10605
10606               widget = GTK_WIDGET (tree_view->priv->edited_column->editable_widget);
10607               adjust_allocation (widget, 0, dy); 
10608               
10609               for (list = tree_view->priv->children; list; list = list->next)
10610                 {
10611                   child = (PsppSheetViewChild *)list->data;
10612                   if (child->widget == widget)
10613                     {
10614                       child->y += dy;
10615                       break;
10616                     }
10617                 }
10618             }
10619         }
10620       gdk_window_scroll (tree_view->priv->bin_window, 0, dy);
10621
10622       if (tree_view->priv->dy != (int) tree_view->priv->vadjustment->value)
10623         {
10624           /* update our dy and top_row */
10625           tree_view->priv->dy = (int) tree_view->priv->vadjustment->value;
10626
10627           if (!tree_view->priv->in_top_row_to_dy)
10628             pspp_sheet_view_dy_to_top_row (tree_view);
10629         }
10630
10631       gdk_window_process_updates (tree_view->priv->header_window, TRUE);
10632       gdk_window_process_updates (tree_view->priv->bin_window, TRUE);
10633     }
10634 }
10635
10636 \f
10637
10638 /* Public methods
10639  */
10640
10641 /**
10642  * pspp_sheet_view_new:
10643  *
10644  * Creates a new #PsppSheetView widget.
10645  *
10646  * Return value: A newly created #PsppSheetView widget.
10647  **/
10648 GtkWidget *
10649 pspp_sheet_view_new (void)
10650 {
10651   return g_object_new (PSPP_TYPE_SHEET_VIEW, NULL);
10652 }
10653
10654 /**
10655  * pspp_sheet_view_new_with_model:
10656  * @model: the model.
10657  *
10658  * Creates a new #PsppSheetView widget with the model initialized to @model.
10659  *
10660  * Return value: A newly created #PsppSheetView widget.
10661  **/
10662 GtkWidget *
10663 pspp_sheet_view_new_with_model (GtkTreeModel *model)
10664 {
10665   return g_object_new (PSPP_TYPE_SHEET_VIEW, "model", model, NULL);
10666 }
10667
10668 /* Public Accessors
10669  */
10670
10671 /**
10672  * pspp_sheet_view_get_model:
10673  * @tree_view: a #PsppSheetView
10674  *
10675  * Returns the model the #PsppSheetView is based on.  Returns %NULL if the
10676  * model is unset.
10677  *
10678  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
10679  **/
10680 GtkTreeModel *
10681 pspp_sheet_view_get_model (PsppSheetView *tree_view)
10682 {
10683   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10684
10685   return tree_view->priv->model;
10686 }
10687
10688 /**
10689  * pspp_sheet_view_set_model:
10690  * @tree_view: A #GtkTreeNode.
10691  * @model: (allow-none): The model.
10692  *
10693  * Sets the model for a #PsppSheetView.  If the @tree_view already has a model
10694  * set, it will remove it before setting the new model.  If @model is %NULL,
10695  * then it will unset the old model.
10696  **/
10697 void
10698 pspp_sheet_view_set_model (PsppSheetView  *tree_view,
10699                          GtkTreeModel *model)
10700 {
10701   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10702   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
10703
10704   if (model == tree_view->priv->model)
10705     return;
10706
10707   if (tree_view->priv->scroll_to_path)
10708     {
10709       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
10710       tree_view->priv->scroll_to_path = NULL;
10711     }
10712
10713   if (tree_view->priv->model)
10714     {
10715       GList *tmplist = tree_view->priv->columns;
10716
10717       pspp_sheet_view_unref_and_check_selection_tree (tree_view, tree_view->priv->tree);
10718       pspp_sheet_view_stop_editing (tree_view, TRUE);
10719
10720       remove_expand_collapse_timeout (tree_view);
10721
10722       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
10723                                             pspp_sheet_view_row_changed,
10724                                             tree_view);
10725       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
10726                                             pspp_sheet_view_row_inserted,
10727                                             tree_view);
10728       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
10729                                             pspp_sheet_view_row_has_child_toggled,
10730                                             tree_view);
10731       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
10732                                             pspp_sheet_view_row_deleted,
10733                                             tree_view);
10734       g_signal_handlers_disconnect_by_func (tree_view->priv->model,
10735                                             pspp_sheet_view_rows_reordered,
10736                                             tree_view);
10737
10738       for (; tmplist; tmplist = tmplist->next)
10739         _pspp_sheet_view_column_unset_model (tmplist->data,
10740                                            tree_view->priv->model);
10741
10742       if (tree_view->priv->tree)
10743         pspp_sheet_view_free_rbtree (tree_view);
10744
10745       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
10746       tree_view->priv->drag_dest_row = NULL;
10747       gtk_tree_row_reference_free (tree_view->priv->cursor);
10748       tree_view->priv->cursor = NULL;
10749       gtk_tree_row_reference_free (tree_view->priv->anchor);
10750       tree_view->priv->anchor = NULL;
10751       gtk_tree_row_reference_free (tree_view->priv->top_row);
10752       tree_view->priv->top_row = NULL;
10753       gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
10754       tree_view->priv->scroll_to_path = NULL;
10755
10756       tree_view->priv->scroll_to_column = NULL;
10757
10758       g_object_unref (tree_view->priv->model);
10759
10760       tree_view->priv->search_column = -1;
10761       tree_view->priv->fixed_height_check = 0;
10762       tree_view->priv->fixed_height = -1;
10763       tree_view->priv->dy = tree_view->priv->top_row_dy = 0;
10764       tree_view->priv->last_button_x = -1;
10765       tree_view->priv->last_button_y = -1;
10766     }
10767
10768   tree_view->priv->model = model;
10769
10770   if (tree_view->priv->model)
10771     {
10772       gint i;
10773       GtkTreePath *path;
10774       GtkTreeIter iter;
10775       GtkTreeModelFlags flags;
10776
10777       if (tree_view->priv->search_column == -1)
10778         {
10779           for (i = 0; i < gtk_tree_model_get_n_columns (model); i++)
10780             {
10781               GType type = gtk_tree_model_get_column_type (model, i);
10782
10783               if (g_value_type_transformable (type, G_TYPE_STRING))
10784                 {
10785                   tree_view->priv->search_column = i;
10786                   break;
10787                 }
10788             }
10789         }
10790
10791       g_object_ref (tree_view->priv->model);
10792       g_signal_connect (tree_view->priv->model,
10793                         "row-changed",
10794                         G_CALLBACK (pspp_sheet_view_row_changed),
10795                         tree_view);
10796       g_signal_connect (tree_view->priv->model,
10797                         "row-inserted",
10798                         G_CALLBACK (pspp_sheet_view_row_inserted),
10799                         tree_view);
10800       g_signal_connect (tree_view->priv->model,
10801                         "row-has-child-toggled",
10802                         G_CALLBACK (pspp_sheet_view_row_has_child_toggled),
10803                         tree_view);
10804       g_signal_connect (tree_view->priv->model,
10805                         "row-deleted",
10806                         G_CALLBACK (pspp_sheet_view_row_deleted),
10807                         tree_view);
10808       g_signal_connect (tree_view->priv->model,
10809                         "rows-reordered",
10810                         G_CALLBACK (pspp_sheet_view_rows_reordered),
10811                         tree_view);
10812
10813       flags = gtk_tree_model_get_flags (tree_view->priv->model);
10814       if ((flags & GTK_TREE_MODEL_LIST_ONLY) == GTK_TREE_MODEL_LIST_ONLY)
10815         PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_IS_LIST);
10816       else
10817         PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_IS_LIST);
10818
10819       path = gtk_tree_path_new_first ();
10820       if (gtk_tree_model_get_iter (tree_view->priv->model, &iter, path))
10821         {
10822           tree_view->priv->tree = _pspp_rbtree_new ();
10823           pspp_sheet_view_build_tree (tree_view, tree_view->priv->tree, &iter, 1, FALSE);
10824         }
10825       gtk_tree_path_free (path);
10826
10827       /*  FIXME: do I need to do this? pspp_sheet_view_create_buttons (tree_view); */
10828       install_presize_handler (tree_view);
10829     }
10830
10831   g_object_notify (G_OBJECT (tree_view), "model");
10832
10833   if (tree_view->priv->selection)
10834   _pspp_sheet_selection_emit_changed (tree_view->priv->selection);
10835
10836   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
10837     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
10838 }
10839
10840 /**
10841  * pspp_sheet_view_get_selection:
10842  * @tree_view: A #PsppSheetView.
10843  *
10844  * Gets the #PsppSheetSelection associated with @tree_view.
10845  *
10846  * Return value: A #PsppSheetSelection object.
10847  **/
10848 PsppSheetSelection *
10849 pspp_sheet_view_get_selection (PsppSheetView *tree_view)
10850 {
10851   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10852
10853   return tree_view->priv->selection;
10854 }
10855
10856 /**
10857  * pspp_sheet_view_get_hadjustment:
10858  * @tree_view: A #PsppSheetView
10859  *
10860  * Gets the #GtkAdjustment currently being used for the horizontal aspect.
10861  *
10862  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
10863  * used.
10864  **/
10865 GtkAdjustment *
10866 pspp_sheet_view_get_hadjustment (PsppSheetView *tree_view)
10867 {
10868   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10869
10870   if (tree_view->priv->hadjustment == NULL)
10871     pspp_sheet_view_set_hadjustment (tree_view, NULL);
10872
10873   return tree_view->priv->hadjustment;
10874 }
10875
10876 /**
10877  * pspp_sheet_view_set_hadjustment:
10878  * @tree_view: A #PsppSheetView
10879  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
10880  *
10881  * Sets the #GtkAdjustment for the current horizontal aspect.
10882  **/
10883 void
10884 pspp_sheet_view_set_hadjustment (PsppSheetView   *tree_view,
10885                                GtkAdjustment *adjustment)
10886 {
10887   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10888
10889   pspp_sheet_view_set_adjustments (tree_view,
10890                                  adjustment,
10891                                  tree_view->priv->vadjustment);
10892
10893   g_object_notify (G_OBJECT (tree_view), "hadjustment");
10894 }
10895
10896 /**
10897  * pspp_sheet_view_get_vadjustment:
10898  * @tree_view: A #PsppSheetView
10899  *
10900  * Gets the #GtkAdjustment currently being used for the vertical aspect.
10901  *
10902  * Return value: A #GtkAdjustment object, or %NULL if none is currently being
10903  * used.
10904  **/
10905 GtkAdjustment *
10906 pspp_sheet_view_get_vadjustment (PsppSheetView *tree_view)
10907 {
10908   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
10909
10910   if (tree_view->priv->vadjustment == NULL)
10911     pspp_sheet_view_set_vadjustment (tree_view, NULL);
10912
10913   return tree_view->priv->vadjustment;
10914 }
10915
10916 /**
10917  * pspp_sheet_view_set_vadjustment:
10918  * @tree_view: A #PsppSheetView
10919  * @adjustment: (allow-none): The #GtkAdjustment to set, or %NULL
10920  *
10921  * Sets the #GtkAdjustment for the current vertical aspect.
10922  **/
10923 void
10924 pspp_sheet_view_set_vadjustment (PsppSheetView   *tree_view,
10925                                GtkAdjustment *adjustment)
10926 {
10927   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10928
10929   pspp_sheet_view_set_adjustments (tree_view,
10930                                  tree_view->priv->hadjustment,
10931                                  adjustment);
10932
10933   g_object_notify (G_OBJECT (tree_view), "vadjustment");
10934 }
10935
10936 /* Column and header operations */
10937
10938 /**
10939  * pspp_sheet_view_get_headers_visible:
10940  * @tree_view: A #PsppSheetView.
10941  *
10942  * Returns %TRUE if the headers on the @tree_view are visible.
10943  *
10944  * Return value: Whether the headers are visible or not.
10945  **/
10946 gboolean
10947 pspp_sheet_view_get_headers_visible (PsppSheetView *tree_view)
10948 {
10949   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
10950
10951   return PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
10952 }
10953
10954 /**
10955  * pspp_sheet_view_set_headers_visible:
10956  * @tree_view: A #PsppSheetView.
10957  * @headers_visible: %TRUE if the headers are visible
10958  *
10959  * Sets the visibility state of the headers.
10960  **/
10961 void
10962 pspp_sheet_view_set_headers_visible (PsppSheetView *tree_view,
10963                                    gboolean     headers_visible)
10964 {
10965   gint x, y;
10966   GList *list;
10967   PsppSheetViewColumn *column;
10968
10969   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
10970
10971   headers_visible = !! headers_visible;
10972
10973   if (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE) == headers_visible)
10974     return;
10975
10976   if (headers_visible)
10977     PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
10978   else
10979     PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE);
10980
10981   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
10982     {
10983       gdk_window_get_position (tree_view->priv->bin_window, &x, &y);
10984       if (headers_visible)
10985         {
10986           gdk_window_move_resize (tree_view->priv->bin_window, x, y  + TREE_VIEW_HEADER_HEIGHT (tree_view), tree_view->priv->width, GTK_WIDGET (tree_view)->allocation.height -  + TREE_VIEW_HEADER_HEIGHT (tree_view));
10987
10988           if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
10989             pspp_sheet_view_map_buttons (tree_view);
10990         }
10991       else
10992         {
10993           gdk_window_move_resize (tree_view->priv->bin_window, x, y, tree_view->priv->width, tree_view->priv->height);
10994
10995           for (list = tree_view->priv->columns; list; list = list->next)
10996             {
10997               column = list->data;
10998               gtk_widget_unmap (column->button);
10999             }
11000           gdk_window_hide (tree_view->priv->header_window);
11001         }
11002     }
11003
11004   tree_view->priv->vadjustment->page_size = GTK_WIDGET (tree_view)->allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
11005   tree_view->priv->vadjustment->page_increment = (GTK_WIDGET (tree_view)->allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view)) / 2;
11006   tree_view->priv->vadjustment->lower = 0;
11007   tree_view->priv->vadjustment->upper = tree_view->priv->height;
11008   gtk_adjustment_changed (tree_view->priv->vadjustment);
11009
11010   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
11011
11012   g_object_notify (G_OBJECT (tree_view), "headers-visible");
11013 }
11014
11015 /**
11016  * pspp_sheet_view_columns_autosize:
11017  * @tree_view: A #PsppSheetView.
11018  *
11019  * Resizes all columns to their optimal width. Only works after the
11020  * treeview has been realized.
11021  **/
11022 void
11023 pspp_sheet_view_columns_autosize (PsppSheetView *tree_view)
11024 {
11025   gboolean dirty = FALSE;
11026   GList *list;
11027   PsppSheetViewColumn *column;
11028
11029   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11030
11031   for (list = tree_view->priv->columns; list; list = list->next)
11032     {
11033       column = list->data;
11034       if (column->column_type == PSPP_SHEET_VIEW_COLUMN_AUTOSIZE)
11035         continue;
11036       _pspp_sheet_view_column_cell_set_dirty (column, TRUE);
11037       dirty = TRUE;
11038     }
11039
11040   if (dirty)
11041     gtk_widget_queue_resize (GTK_WIDGET (tree_view));
11042 }
11043
11044 /**
11045  * pspp_sheet_view_set_headers_clickable:
11046  * @tree_view: A #PsppSheetView.
11047  * @setting: %TRUE if the columns are clickable.
11048  *
11049  * Allow the column title buttons to be clicked.
11050  **/
11051 void
11052 pspp_sheet_view_set_headers_clickable (PsppSheetView *tree_view,
11053                                      gboolean   setting)
11054 {
11055   GList *list;
11056
11057   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11058
11059   for (list = tree_view->priv->columns; list; list = list->next)
11060     pspp_sheet_view_column_set_clickable (PSPP_SHEET_VIEW_COLUMN (list->data), setting);
11061
11062   g_object_notify (G_OBJECT (tree_view), "headers-clickable");
11063 }
11064
11065
11066 /**
11067  * pspp_sheet_view_get_headers_clickable:
11068  * @tree_view: A #PsppSheetView.
11069  *
11070  * Returns whether all header columns are clickable.
11071  *
11072  * Return value: %TRUE if all header columns are clickable, otherwise %FALSE
11073  *
11074  * Since: 2.10
11075  **/
11076 gboolean 
11077 pspp_sheet_view_get_headers_clickable (PsppSheetView *tree_view)
11078 {
11079   GList *list;
11080   
11081   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11082
11083   for (list = tree_view->priv->columns; list; list = list->next)
11084     if (!PSPP_SHEET_VIEW_COLUMN (list->data)->clickable)
11085       return FALSE;
11086
11087   return TRUE;
11088 }
11089
11090 /**
11091  * pspp_sheet_view_set_rules_hint
11092  * @tree_view: a #PsppSheetView
11093  * @setting: %TRUE if the tree requires reading across rows
11094  *
11095  * This function tells GTK+ that the user interface for your
11096  * application requires users to read across tree rows and associate
11097  * cells with one another. By default, GTK+ will then render the tree
11098  * with alternating row colors. Do <emphasis>not</emphasis> use it
11099  * just because you prefer the appearance of the ruled tree; that's a
11100  * question for the theme. Some themes will draw tree rows in
11101  * alternating colors even when rules are turned off, and users who
11102  * prefer that appearance all the time can choose those themes. You
11103  * should call this function only as a <emphasis>semantic</emphasis>
11104  * hint to the theme engine that your tree makes alternating colors
11105  * useful from a functional standpoint (since it has lots of columns,
11106  * generally).
11107  *
11108  **/
11109 void
11110 pspp_sheet_view_set_rules_hint (PsppSheetView  *tree_view,
11111                               gboolean      setting)
11112 {
11113   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11114
11115   setting = setting != FALSE;
11116
11117   if (tree_view->priv->has_rules != setting)
11118     {
11119       tree_view->priv->has_rules = setting;
11120       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
11121     }
11122
11123   g_object_notify (G_OBJECT (tree_view), "rules-hint");
11124 }
11125
11126 /**
11127  * pspp_sheet_view_get_rules_hint
11128  * @tree_view: a #PsppSheetView
11129  *
11130  * Gets the setting set by pspp_sheet_view_set_rules_hint().
11131  *
11132  * Return value: %TRUE if rules are useful for the user of this tree
11133  **/
11134 gboolean
11135 pspp_sheet_view_get_rules_hint (PsppSheetView  *tree_view)
11136 {
11137   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
11138
11139   return tree_view->priv->has_rules;
11140 }
11141
11142 /* Public Column functions
11143  */
11144
11145 /**
11146  * pspp_sheet_view_append_column:
11147  * @tree_view: A #PsppSheetView.
11148  * @column: The #PsppSheetViewColumn to add.
11149  *
11150  * Appends @column to the list of columns. If @tree_view has "fixed_height"
11151  * mode enabled, then @column must have its "sizing" property set to be
11152  * PSPP_SHEET_VIEW_COLUMN_FIXED.
11153  *
11154  * Return value: The number of columns in @tree_view after appending.
11155  **/
11156 gint
11157 pspp_sheet_view_append_column (PsppSheetView       *tree_view,
11158                              PsppSheetViewColumn *column)
11159 {
11160   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11161   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
11162   g_return_val_if_fail (column->tree_view == NULL, -1);
11163
11164   return pspp_sheet_view_insert_column (tree_view, column, -1);
11165 }
11166
11167
11168 /**
11169  * pspp_sheet_view_remove_column:
11170  * @tree_view: A #PsppSheetView.
11171  * @column: The #PsppSheetViewColumn to remove.
11172  *
11173  * Removes @column from @tree_view.
11174  *
11175  * Return value: The number of columns in @tree_view after removing.
11176  **/
11177 gint
11178 pspp_sheet_view_remove_column (PsppSheetView       *tree_view,
11179                              PsppSheetViewColumn *column)
11180 {
11181   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11182   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
11183   g_return_val_if_fail (column->tree_view == GTK_WIDGET (tree_view), -1);
11184
11185   if (tree_view->priv->focus_column == column)
11186     tree_view->priv->focus_column = NULL;
11187
11188   if (tree_view->priv->edited_column == column)
11189     {
11190       pspp_sheet_view_stop_editing (tree_view, TRUE);
11191
11192       /* no need to, but just to be sure ... */
11193       tree_view->priv->edited_column = NULL;
11194     }
11195
11196   if (tree_view->priv->expander_column == column)
11197     tree_view->priv->expander_column = NULL;
11198
11199   g_signal_handlers_disconnect_by_func (column,
11200                                         G_CALLBACK (column_sizing_notify),
11201                                         tree_view);
11202
11203   _pspp_sheet_view_column_unset_tree_view (column);
11204
11205   tree_view->priv->columns = g_list_remove (tree_view->priv->columns, column);
11206   tree_view->priv->n_columns--;
11207
11208   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
11209     {
11210       GList *list;
11211
11212       _pspp_sheet_view_column_unrealize_button (column);
11213       for (list = tree_view->priv->columns; list; list = list->next)
11214         {
11215           PsppSheetViewColumn *tmp_column;
11216
11217           tmp_column = PSPP_SHEET_VIEW_COLUMN (list->data);
11218           if (tmp_column->visible)
11219             _pspp_sheet_view_column_cell_set_dirty (tmp_column, TRUE);
11220         }
11221
11222       if (tree_view->priv->n_columns == 0 &&
11223           pspp_sheet_view_get_headers_visible (tree_view))
11224         gdk_window_hide (tree_view->priv->header_window);
11225
11226       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
11227     }
11228
11229   g_object_unref (column);
11230   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
11231
11232   return tree_view->priv->n_columns;
11233 }
11234
11235 /**
11236  * pspp_sheet_view_insert_column:
11237  * @tree_view: A #PsppSheetView.
11238  * @column: The #PsppSheetViewColumn to be inserted.
11239  * @position: The position to insert @column in.
11240  *
11241  * This inserts the @column into the @tree_view at @position.  If @position is
11242  * -1, then the column is inserted at the end. If @tree_view has
11243  * "fixed_height" mode enabled, then @column must have its "sizing" property
11244  * set to be PSPP_SHEET_VIEW_COLUMN_FIXED.
11245  *
11246  * Return value: The number of columns in @tree_view after insertion.
11247  **/
11248 gint
11249 pspp_sheet_view_insert_column (PsppSheetView       *tree_view,
11250                              PsppSheetViewColumn *column,
11251                              gint               position)
11252 {
11253   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11254   g_return_val_if_fail (PSPP_IS_SHEET_VIEW_COLUMN (column), -1);
11255   g_return_val_if_fail (column->tree_view == NULL, -1);
11256
11257   if (tree_view->priv->fixed_height_mode)
11258     g_return_val_if_fail (pspp_sheet_view_column_get_sizing (column)
11259                           == PSPP_SHEET_VIEW_COLUMN_FIXED, -1);
11260
11261   g_object_ref_sink (column);
11262
11263   if (tree_view->priv->n_columns == 0 &&
11264       gtk_widget_get_realized (GTK_WIDGET (tree_view)) &&
11265       pspp_sheet_view_get_headers_visible (tree_view))
11266     {
11267       gdk_window_show (tree_view->priv->header_window);
11268     }
11269
11270   g_signal_connect (column, "notify::sizing",
11271                     G_CALLBACK (column_sizing_notify), tree_view);
11272
11273   tree_view->priv->columns = g_list_insert (tree_view->priv->columns,
11274                                             column, position);
11275   tree_view->priv->n_columns++;
11276
11277   _pspp_sheet_view_column_set_tree_view (column, tree_view);
11278
11279   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
11280     {
11281       GList *list;
11282
11283       _pspp_sheet_view_column_realize_button (column);
11284
11285       for (list = tree_view->priv->columns; list; list = list->next)
11286         {
11287           column = PSPP_SHEET_VIEW_COLUMN (list->data);
11288           if (column->visible)
11289             _pspp_sheet_view_column_cell_set_dirty (column, TRUE);
11290         }
11291       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
11292     }
11293
11294   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
11295
11296   return tree_view->priv->n_columns;
11297 }
11298
11299 /**
11300  * pspp_sheet_view_insert_column_with_attributes:
11301  * @tree_view: A #PsppSheetView
11302  * @position: The position to insert the new column in.
11303  * @title: The title to set the header to.
11304  * @cell: The #GtkCellRenderer.
11305  * @Varargs: A %NULL-terminated list of attributes.
11306  *
11307  * Creates a new #PsppSheetViewColumn and inserts it into the @tree_view at
11308  * @position.  If @position is -1, then the newly created column is inserted at
11309  * the end.  The column is initialized with the attributes given. If @tree_view
11310  * has "fixed_height" mode enabled, then the new column will have its sizing
11311  * property set to be PSPP_SHEET_VIEW_COLUMN_FIXED.
11312  *
11313  * Return value: The number of columns in @tree_view after insertion.
11314  **/
11315 gint
11316 pspp_sheet_view_insert_column_with_attributes (PsppSheetView     *tree_view,
11317                                              gint             position,
11318                                              const gchar     *title,
11319                                              GtkCellRenderer *cell,
11320                                              ...)
11321 {
11322   PsppSheetViewColumn *column;
11323   gchar *attribute;
11324   va_list args;
11325   gint column_id;
11326
11327   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11328
11329   column = pspp_sheet_view_column_new ();
11330   if (tree_view->priv->fixed_height_mode)
11331     pspp_sheet_view_column_set_sizing (column, PSPP_SHEET_VIEW_COLUMN_FIXED);
11332
11333   pspp_sheet_view_column_set_title (column, title);
11334   pspp_sheet_view_column_pack_start (column, cell, TRUE);
11335
11336   va_start (args, cell);
11337
11338   attribute = va_arg (args, gchar *);
11339
11340   while (attribute != NULL)
11341     {
11342       column_id = va_arg (args, gint);
11343       pspp_sheet_view_column_add_attribute (column, cell, attribute, column_id);
11344       attribute = va_arg (args, gchar *);
11345     }
11346
11347   va_end (args);
11348
11349   pspp_sheet_view_insert_column (tree_view, column, position);
11350
11351   return tree_view->priv->n_columns;
11352 }
11353
11354 /**
11355  * pspp_sheet_view_insert_column_with_data_func:
11356  * @tree_view: a #PsppSheetView
11357  * @position: Position to insert, -1 for append
11358  * @title: column title
11359  * @cell: cell renderer for column
11360  * @func: function to set attributes of cell renderer
11361  * @data: data for @func
11362  * @dnotify: destroy notifier for @data
11363  *
11364  * Convenience function that inserts a new column into the #PsppSheetView
11365  * with the given cell renderer and a #GtkCellDataFunc to set cell renderer
11366  * attributes (normally using data from the model). See also
11367  * pspp_sheet_view_column_set_cell_data_func(), pspp_sheet_view_column_pack_start().
11368  * If @tree_view has "fixed_height" mode enabled, then the new column will have its
11369  * "sizing" property set to be PSPP_SHEET_VIEW_COLUMN_FIXED.
11370  *
11371  * Return value: number of columns in the tree view post-insert
11372  **/
11373 gint
11374 pspp_sheet_view_insert_column_with_data_func  (PsppSheetView               *tree_view,
11375                                              gint                       position,
11376                                              const gchar               *title,
11377                                              GtkCellRenderer           *cell,
11378                                              PsppSheetCellDataFunc        func,
11379                                              gpointer                   data,
11380                                              GDestroyNotify             dnotify)
11381 {
11382   PsppSheetViewColumn *column;
11383
11384   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
11385
11386   column = pspp_sheet_view_column_new ();
11387   if (tree_view->priv->fixed_height_mode)
11388     pspp_sheet_view_column_set_sizing (column, PSPP_SHEET_VIEW_COLUMN_FIXED);
11389
11390   pspp_sheet_view_column_set_title (column, title);
11391   pspp_sheet_view_column_pack_start (column, cell, TRUE);
11392   pspp_sheet_view_column_set_cell_data_func (column, cell, func, data, dnotify);
11393
11394   pspp_sheet_view_insert_column (tree_view, column, position);
11395
11396   return tree_view->priv->n_columns;
11397 }
11398
11399 /**
11400  * pspp_sheet_view_get_column:
11401  * @tree_view: A #PsppSheetView.
11402  * @n: The position of the column, counting from 0.
11403  *
11404  * Gets the #PsppSheetViewColumn at the given position in the #tree_view.
11405  *
11406  * Return value: The #PsppSheetViewColumn, or %NULL if the position is outside the
11407  * range of columns.
11408  **/
11409 PsppSheetViewColumn *
11410 pspp_sheet_view_get_column (PsppSheetView *tree_view,
11411                           gint         n)
11412 {
11413   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11414
11415   if (n < 0 || n >= tree_view->priv->n_columns)
11416     return NULL;
11417
11418   if (tree_view->priv->columns == NULL)
11419     return NULL;
11420
11421   return PSPP_SHEET_VIEW_COLUMN (g_list_nth (tree_view->priv->columns, n)->data);
11422 }
11423
11424 /**
11425  * pspp_sheet_view_get_columns:
11426  * @tree_view: A #PsppSheetView
11427  *
11428  * Returns a #GList of all the #PsppSheetViewColumn s currently in @tree_view.
11429  * The returned list must be freed with g_list_free ().
11430  *
11431  * Return value: (element-type PsppSheetViewColumn) (transfer container): A list of #PsppSheetViewColumn s
11432  **/
11433 GList *
11434 pspp_sheet_view_get_columns (PsppSheetView *tree_view)
11435 {
11436   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11437
11438   return g_list_copy (tree_view->priv->columns);
11439 }
11440
11441 /**
11442  * pspp_sheet_view_move_column_after:
11443  * @tree_view: A #PsppSheetView
11444  * @column: The #PsppSheetViewColumn to be moved.
11445  * @base_column: (allow-none): The #PsppSheetViewColumn to be moved relative to, or %NULL.
11446  *
11447  * Moves @column to be after to @base_column.  If @base_column is %NULL, then
11448  * @column is placed in the first position.
11449  **/
11450 void
11451 pspp_sheet_view_move_column_after (PsppSheetView       *tree_view,
11452                                  PsppSheetViewColumn *column,
11453                                  PsppSheetViewColumn *base_column)
11454 {
11455   GList *column_list_el, *base_el = NULL;
11456
11457   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11458
11459   column_list_el = g_list_find (tree_view->priv->columns, column);
11460   g_return_if_fail (column_list_el != NULL);
11461
11462   if (base_column)
11463     {
11464       base_el = g_list_find (tree_view->priv->columns, base_column);
11465       g_return_if_fail (base_el != NULL);
11466     }
11467
11468   if (column_list_el->prev == base_el)
11469     return;
11470
11471   tree_view->priv->columns = g_list_remove_link (tree_view->priv->columns, column_list_el);
11472   if (base_el == NULL)
11473     {
11474       column_list_el->prev = NULL;
11475       column_list_el->next = tree_view->priv->columns;
11476       if (column_list_el->next)
11477         column_list_el->next->prev = column_list_el;
11478       tree_view->priv->columns = column_list_el;
11479     }
11480   else
11481     {
11482       column_list_el->prev = base_el;
11483       column_list_el->next = base_el->next;
11484       if (column_list_el->next)
11485         column_list_el->next->prev = column_list_el;
11486       base_el->next = column_list_el;
11487     }
11488
11489   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
11490     {
11491       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
11492       pspp_sheet_view_size_allocate_columns (GTK_WIDGET (tree_view), NULL);
11493     }
11494
11495   g_signal_emit (tree_view, tree_view_signals[COLUMNS_CHANGED], 0);
11496 }
11497
11498 /**
11499  * pspp_sheet_view_set_expander_column:
11500  * @tree_view: A #PsppSheetView
11501  * @column: %NULL, or the column to draw the expander arrow at.
11502  *
11503  * Sets the column to draw the expander arrow at. It must be in @tree_view.  
11504  * If @column is %NULL, then the expander arrow is always at the first 
11505  * visible column.
11506  *
11507  * If you do not want expander arrow to appear in your tree, set the 
11508  * expander column to a hidden column.
11509  **/
11510 void
11511 pspp_sheet_view_set_expander_column (PsppSheetView       *tree_view,
11512                                    PsppSheetViewColumn *column)
11513 {
11514   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11515   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
11516
11517   if (tree_view->priv->expander_column != column)
11518     {
11519       GList *list;
11520
11521       if (column)
11522         {
11523           /* Confirm that column is in tree_view */
11524           for (list = tree_view->priv->columns; list; list = list->next)
11525             if (list->data == column)
11526               break;
11527           g_return_if_fail (list != NULL);
11528         }
11529
11530       tree_view->priv->expander_column = column;
11531       g_object_notify (G_OBJECT (tree_view), "expander-column");
11532     }
11533 }
11534
11535 /**
11536  * pspp_sheet_view_get_expander_column:
11537  * @tree_view: A #PsppSheetView
11538  *
11539  * Returns the column that is the current expander column.  This
11540  * column has the expander arrow drawn next to it.
11541  *
11542  * Return value: The expander column.
11543  **/
11544 PsppSheetViewColumn *
11545 pspp_sheet_view_get_expander_column (PsppSheetView *tree_view)
11546 {
11547   GList *list;
11548
11549   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
11550
11551   for (list = tree_view->priv->columns; list; list = list->next)
11552     if (pspp_sheet_view_is_expander_column (tree_view, PSPP_SHEET_VIEW_COLUMN (list->data)))
11553       return (PsppSheetViewColumn *) list->data;
11554   return NULL;
11555 }
11556
11557
11558 /**
11559  * pspp_sheet_view_set_column_drag_function:
11560  * @tree_view: A #PsppSheetView.
11561  * @func: (allow-none): A function to determine which columns are reorderable, or %NULL.
11562  * @user_data: (allow-none): User data to be passed to @func, or %NULL
11563  * @destroy: (allow-none): Destroy notifier for @user_data, or %NULL
11564  *
11565  * Sets a user function for determining where a column may be dropped when
11566  * dragged.  This function is called on every column pair in turn at the
11567  * beginning of a column drag to determine where a drop can take place.  The
11568  * arguments passed to @func are: the @tree_view, the #PsppSheetViewColumn being
11569  * dragged, the two #PsppSheetViewColumn s determining the drop spot, and
11570  * @user_data.  If either of the #PsppSheetViewColumn arguments for the drop spot
11571  * are %NULL, then they indicate an edge.  If @func is set to be %NULL, then
11572  * @tree_view reverts to the default behavior of allowing all columns to be
11573  * dropped everywhere.
11574  **/
11575 void
11576 pspp_sheet_view_set_column_drag_function (PsppSheetView               *tree_view,
11577                                         PsppSheetViewColumnDropFunc  func,
11578                                         gpointer                   user_data,
11579                                         GDestroyNotify             destroy)
11580 {
11581   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11582
11583   if (tree_view->priv->column_drop_func_data_destroy)
11584     tree_view->priv->column_drop_func_data_destroy (tree_view->priv->column_drop_func_data);
11585
11586   tree_view->priv->column_drop_func = func;
11587   tree_view->priv->column_drop_func_data = user_data;
11588   tree_view->priv->column_drop_func_data_destroy = destroy;
11589 }
11590
11591 /**
11592  * pspp_sheet_view_scroll_to_point:
11593  * @tree_view: a #PsppSheetView
11594  * @tree_x: X coordinate of new top-left pixel of visible area, or -1
11595  * @tree_y: Y coordinate of new top-left pixel of visible area, or -1
11596  *
11597  * Scrolls the tree view such that the top-left corner of the visible
11598  * area is @tree_x, @tree_y, where @tree_x and @tree_y are specified
11599  * in tree coordinates.  The @tree_view must be realized before
11600  * this function is called.  If it isn't, you probably want to be
11601  * using pspp_sheet_view_scroll_to_cell().
11602  *
11603  * If either @tree_x or @tree_y are -1, then that direction isn't scrolled.
11604  **/
11605 void
11606 pspp_sheet_view_scroll_to_point (PsppSheetView *tree_view,
11607                                gint         tree_x,
11608                                gint         tree_y)
11609 {
11610   GtkAdjustment *hadj;
11611   GtkAdjustment *vadj;
11612
11613   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11614   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
11615
11616   hadj = tree_view->priv->hadjustment;
11617   vadj = tree_view->priv->vadjustment;
11618
11619   if (tree_x != -1)
11620     gtk_adjustment_set_value (hadj, CLAMP (tree_x, hadj->lower, hadj->upper - hadj->page_size));
11621   if (tree_y != -1)
11622     gtk_adjustment_set_value (vadj, CLAMP (tree_y, vadj->lower, vadj->upper - vadj->page_size));
11623 }
11624
11625 /**
11626  * pspp_sheet_view_scroll_to_cell:
11627  * @tree_view: A #PsppSheetView.
11628  * @path: (allow-none): The path of the row to move to, or %NULL.
11629  * @column: (allow-none): The #PsppSheetViewColumn to move horizontally to, or %NULL.
11630  * @use_align: whether to use alignment arguments, or %FALSE.
11631  * @row_align: The vertical alignment of the row specified by @path.
11632  * @col_align: The horizontal alignment of the column specified by @column.
11633  *
11634  * Moves the alignments of @tree_view to the position specified by @column and
11635  * @path.  If @column is %NULL, then no horizontal scrolling occurs.  Likewise,
11636  * if @path is %NULL no vertical scrolling occurs.  At a minimum, one of @column
11637  * or @path need to be non-%NULL.  @row_align determines where the row is
11638  * placed, and @col_align determines where @column is placed.  Both are expected
11639  * to be between 0.0 and 1.0. 0.0 means left/top alignment, 1.0 means
11640  * right/bottom alignment, 0.5 means center.
11641  *
11642  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
11643  * tree does the minimum amount of work to scroll the cell onto the screen.
11644  * This means that the cell will be scrolled to the edge closest to its current
11645  * position.  If the cell is currently visible on the screen, nothing is done.
11646  *
11647  * This function only works if the model is set, and @path is a valid row on the
11648  * model.  If the model changes before the @tree_view is realized, the centered
11649  * path will be modified to reflect this change.
11650  **/
11651 void
11652 pspp_sheet_view_scroll_to_cell (PsppSheetView       *tree_view,
11653                               GtkTreePath       *path,
11654                               PsppSheetViewColumn *column,
11655                               gboolean           use_align,
11656                               gfloat             row_align,
11657                               gfloat             col_align)
11658 {
11659   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11660   g_return_if_fail (tree_view->priv->model != NULL);
11661   g_return_if_fail (tree_view->priv->tree != NULL);
11662   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
11663   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
11664   g_return_if_fail (path != NULL || column != NULL);
11665
11666 #if 0
11667   g_print ("pspp_sheet_view_scroll_to_cell:\npath: %s\ncolumn: %s\nuse_align: %d\nrow_align: %f\ncol_align: %f\n",
11668            gtk_tree_path_to_string (path), column?"non-null":"null", use_align, row_align, col_align);
11669 #endif
11670   row_align = CLAMP (row_align, 0.0, 1.0);
11671   col_align = CLAMP (col_align, 0.0, 1.0);
11672
11673
11674   /* Note: Despite the benefits that come from having one code path for the
11675    * scrolling code, we short-circuit validate_visible_area's immplementation as
11676    * it is much slower than just going to the point.
11677    */
11678   if (!gtk_widget_get_visible (GTK_WIDGET (tree_view)) ||
11679       !gtk_widget_get_realized (GTK_WIDGET (tree_view)) ||
11680       /* XXX GTK_WIDGET_ALLOC_NEEDED (tree_view) || */
11681       PSPP_RBNODE_FLAG_SET (tree_view->priv->tree->root, PSPP_RBNODE_DESCENDANTS_INVALID))
11682     {
11683       if (tree_view->priv->scroll_to_path)
11684         gtk_tree_row_reference_free (tree_view->priv->scroll_to_path);
11685
11686       tree_view->priv->scroll_to_path = NULL;
11687       tree_view->priv->scroll_to_column = NULL;
11688
11689       if (path)
11690         tree_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
11691       if (column)
11692         tree_view->priv->scroll_to_column = column;
11693       tree_view->priv->scroll_to_use_align = use_align;
11694       tree_view->priv->scroll_to_row_align = row_align;
11695       tree_view->priv->scroll_to_col_align = col_align;
11696
11697       install_presize_handler (tree_view);
11698     }
11699   else
11700     {
11701       GdkRectangle cell_rect;
11702       GdkRectangle vis_rect;
11703       gint dest_x, dest_y;
11704
11705       pspp_sheet_view_get_background_area (tree_view, path, column, &cell_rect);
11706       pspp_sheet_view_get_visible_rect (tree_view, &vis_rect);
11707
11708       cell_rect.y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, cell_rect.y);
11709
11710       dest_x = vis_rect.x;
11711       dest_y = vis_rect.y;
11712
11713       if (column)
11714         {
11715           if (use_align)
11716             {
11717               dest_x = cell_rect.x - ((vis_rect.width - cell_rect.width) * col_align);
11718             }
11719           else
11720             {
11721               if (cell_rect.x < vis_rect.x)
11722                 dest_x = cell_rect.x;
11723               if (cell_rect.x + cell_rect.width > vis_rect.x + vis_rect.width)
11724                 dest_x = cell_rect.x + cell_rect.width - vis_rect.width;
11725             }
11726         }
11727
11728       if (path)
11729         {
11730           if (use_align)
11731             {
11732               dest_y = cell_rect.y - ((vis_rect.height - cell_rect.height) * row_align);
11733               dest_y = MAX (dest_y, 0);
11734             }
11735           else
11736             {
11737               if (cell_rect.y < vis_rect.y)
11738                 dest_y = cell_rect.y;
11739               if (cell_rect.y + cell_rect.height > vis_rect.y + vis_rect.height)
11740                 dest_y = cell_rect.y + cell_rect.height - vis_rect.height;
11741             }
11742         }
11743
11744       pspp_sheet_view_scroll_to_point (tree_view, dest_x, dest_y);
11745     }
11746 }
11747
11748 /**
11749  * pspp_sheet_view_row_activated:
11750  * @tree_view: A #PsppSheetView
11751  * @path: The #GtkTreePath to be activated.
11752  * @column: The #PsppSheetViewColumn to be activated.
11753  *
11754  * Activates the cell determined by @path and @column.
11755  **/
11756 void
11757 pspp_sheet_view_row_activated (PsppSheetView       *tree_view,
11758                              GtkTreePath       *path,
11759                              PsppSheetViewColumn *column)
11760 {
11761   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11762
11763   g_signal_emit (tree_view, tree_view_signals[ROW_ACTIVATED], 0, path, column);
11764 }
11765
11766
11767 static void
11768 pspp_sheet_view_expand_all_emission_helper (GtkRBTree *tree,
11769                                           GtkRBNode *node,
11770                                           gpointer   data)
11771 {
11772   PsppSheetView *tree_view = data;
11773
11774   if ((node->flags & PSPP_RBNODE_IS_PARENT) == PSPP_RBNODE_IS_PARENT &&
11775       node->children)
11776     {
11777       GtkTreePath *path;
11778       GtkTreeIter iter;
11779
11780       path = _pspp_sheet_view_find_path (tree_view, tree, node);
11781       gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
11782
11783       g_signal_emit (tree_view, tree_view_signals[ROW_EXPANDED], 0, &iter, path);
11784
11785       gtk_tree_path_free (path);
11786     }
11787
11788   if (node->children)
11789     _pspp_rbtree_traverse (node->children,
11790                           node->children->root,
11791                           G_PRE_ORDER,
11792                           pspp_sheet_view_expand_all_emission_helper,
11793                           tree_view);
11794 }
11795
11796 /**
11797  * pspp_sheet_view_expand_all:
11798  * @tree_view: A #PsppSheetView.
11799  *
11800  * Recursively expands all nodes in the @tree_view.
11801  **/
11802 void
11803 pspp_sheet_view_expand_all (PsppSheetView *tree_view)
11804 {
11805   GtkTreePath *path;
11806   GtkRBTree *tree;
11807   GtkRBNode *node;
11808
11809   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11810
11811   if (tree_view->priv->tree == NULL)
11812     return;
11813
11814   path = gtk_tree_path_new_first ();
11815   _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
11816
11817   while (node)
11818     {
11819       pspp_sheet_view_real_expand_row (tree_view, path, tree, node, TRUE, FALSE);
11820       node = _pspp_rbtree_next (tree, node);
11821       gtk_tree_path_next (path);
11822   }
11823
11824   gtk_tree_path_free (path);
11825 }
11826
11827 /* Timeout to animate the expander during expands and collapses */
11828 static gboolean
11829 expand_collapse_timeout (gpointer data)
11830 {
11831   return do_expand_collapse (data);
11832 }
11833
11834 static void
11835 add_expand_collapse_timeout (PsppSheetView *tree_view,
11836                              GtkRBTree   *tree,
11837                              GtkRBNode   *node,
11838                              gboolean     expand)
11839 {
11840   if (tree_view->priv->expand_collapse_timeout != 0)
11841     return;
11842
11843   tree_view->priv->expand_collapse_timeout =
11844       gdk_threads_add_timeout (50, expand_collapse_timeout, tree_view);
11845   tree_view->priv->expanded_collapsed_tree = tree;
11846   tree_view->priv->expanded_collapsed_node = node;
11847
11848   if (expand)
11849     PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_SEMI_COLLAPSED);
11850   else
11851     PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_SEMI_EXPANDED);
11852 }
11853
11854 static void
11855 remove_expand_collapse_timeout (PsppSheetView *tree_view)
11856 {
11857   if (tree_view->priv->expand_collapse_timeout)
11858     {
11859       g_source_remove (tree_view->priv->expand_collapse_timeout);
11860       tree_view->priv->expand_collapse_timeout = 0;
11861     }
11862
11863   if (tree_view->priv->expanded_collapsed_node != NULL)
11864     {
11865       PSPP_RBNODE_UNSET_FLAG (tree_view->priv->expanded_collapsed_node, PSPP_RBNODE_IS_SEMI_EXPANDED);
11866       PSPP_RBNODE_UNSET_FLAG (tree_view->priv->expanded_collapsed_node, PSPP_RBNODE_IS_SEMI_COLLAPSED);
11867
11868       tree_view->priv->expanded_collapsed_node = NULL;
11869     }
11870 }
11871
11872 static void
11873 cancel_arrow_animation (PsppSheetView *tree_view)
11874 {
11875   if (tree_view->priv->expand_collapse_timeout)
11876     {
11877       while (do_expand_collapse (tree_view));
11878
11879       remove_expand_collapse_timeout (tree_view);
11880     }
11881 }
11882
11883 static gboolean
11884 do_expand_collapse (PsppSheetView *tree_view)
11885 {
11886   GtkRBNode *node;
11887   GtkRBTree *tree;
11888   gboolean expanding;
11889   gboolean redraw;
11890
11891   redraw = FALSE;
11892   expanding = TRUE;
11893
11894   node = tree_view->priv->expanded_collapsed_node;
11895   tree = tree_view->priv->expanded_collapsed_tree;
11896
11897   if (node->children == NULL)
11898     expanding = FALSE;
11899
11900   if (expanding)
11901     {
11902       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_COLLAPSED))
11903         {
11904           PSPP_RBNODE_UNSET_FLAG (node, PSPP_RBNODE_IS_SEMI_COLLAPSED);
11905           PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_SEMI_EXPANDED);
11906
11907           redraw = TRUE;
11908
11909         }
11910       else if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_EXPANDED))
11911         {
11912           PSPP_RBNODE_UNSET_FLAG (node, PSPP_RBNODE_IS_SEMI_EXPANDED);
11913
11914           redraw = TRUE;
11915         }
11916     }
11917   else
11918     {
11919       if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_EXPANDED))
11920         {
11921           PSPP_RBNODE_UNSET_FLAG (node, PSPP_RBNODE_IS_SEMI_EXPANDED);
11922           PSPP_RBNODE_SET_FLAG (node, PSPP_RBNODE_IS_SEMI_COLLAPSED);
11923
11924           redraw = TRUE;
11925         }
11926       else if (PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SEMI_COLLAPSED))
11927         {
11928           PSPP_RBNODE_UNSET_FLAG (node, PSPP_RBNODE_IS_SEMI_COLLAPSED);
11929
11930           redraw = TRUE;
11931
11932         }
11933     }
11934
11935   if (redraw)
11936     {
11937       pspp_sheet_view_queue_draw_arrow (tree_view, tree, node, NULL);
11938
11939       return TRUE;
11940     }
11941
11942   return FALSE;
11943 }
11944
11945 /**
11946  * pspp_sheet_view_collapse_all:
11947  * @tree_view: A #PsppSheetView.
11948  *
11949  * Recursively collapses all visible, expanded nodes in @tree_view.
11950  **/
11951 void
11952 pspp_sheet_view_collapse_all (PsppSheetView *tree_view)
11953 {
11954   GtkRBTree *tree;
11955   GtkRBNode *node;
11956   GtkTreePath *path;
11957   gint *indices;
11958
11959   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
11960
11961   if (tree_view->priv->tree == NULL)
11962     return;
11963
11964   path = gtk_tree_path_new ();
11965   gtk_tree_path_down (path);
11966   indices = gtk_tree_path_get_indices (path);
11967
11968   tree = tree_view->priv->tree;
11969   node = tree->root;
11970   while (node && node->left != tree->nil)
11971     node = node->left;
11972
11973   while (node)
11974     {
11975       if (node->children)
11976         pspp_sheet_view_real_collapse_row (tree_view, path, tree, node, FALSE);
11977       indices[0]++;
11978       node = _pspp_rbtree_next (tree, node);
11979     }
11980
11981   gtk_tree_path_free (path);
11982 }
11983
11984 /**
11985  * pspp_sheet_view_expand_to_path:
11986  * @tree_view: A #PsppSheetView.
11987  * @path: path to a row.
11988  *
11989  * Expands the row at @path. This will also expand all parent rows of
11990  * @path as necessary.
11991  *
11992  * Since: 2.2
11993  **/
11994 void
11995 pspp_sheet_view_expand_to_path (PsppSheetView *tree_view,
11996                               GtkTreePath *path)
11997 {
11998   gint i, depth;
11999   gint *indices;
12000   GtkTreePath *tmp;
12001
12002   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12003   g_return_if_fail (path != NULL);
12004
12005   depth = gtk_tree_path_get_depth (path);
12006   indices = gtk_tree_path_get_indices (path);
12007
12008   tmp = gtk_tree_path_new ();
12009   g_return_if_fail (tmp != NULL);
12010
12011   for (i = 0; i < depth; i++)
12012     {
12013       gtk_tree_path_append_index (tmp, indices[i]);
12014       pspp_sheet_view_expand_row (tree_view, tmp, FALSE);
12015     }
12016
12017   gtk_tree_path_free (tmp);
12018 }
12019
12020 /* FIXME the bool return values for expand_row and collapse_row are
12021  * not analagous; they should be TRUE if the row had children and
12022  * was not already in the requested state.
12023  */
12024
12025
12026 static gboolean
12027 pspp_sheet_view_real_expand_row (PsppSheetView *tree_view,
12028                                GtkTreePath *path,
12029                                GtkRBTree   *tree,
12030                                GtkRBNode   *node,
12031                                gboolean     open_all,
12032                                gboolean     animate)
12033 {
12034   GtkTreeIter iter;
12035   GtkTreeIter temp;
12036   gboolean expand;
12037
12038   if (animate)
12039     g_object_get (gtk_widget_get_settings (GTK_WIDGET (tree_view)),
12040                   "gtk-enable-animations", &animate,
12041                   NULL);
12042
12043   remove_auto_expand_timeout (tree_view);
12044
12045   if (node->children && !open_all)
12046     return FALSE;
12047
12048   if (! PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT))
12049     return FALSE;
12050
12051   gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
12052   if (! gtk_tree_model_iter_has_child (tree_view->priv->model, &iter))
12053     return FALSE;
12054
12055
12056    if (node->children && open_all)
12057     {
12058       gboolean retval = FALSE;
12059       GtkTreePath *tmp_path = gtk_tree_path_copy (path);
12060
12061       gtk_tree_path_append_index (tmp_path, 0);
12062       tree = node->children;
12063       node = tree->root;
12064       while (node->left != tree->nil)
12065         node = node->left;
12066       /* try to expand the children */
12067       do
12068         {
12069          gboolean t;
12070          t = pspp_sheet_view_real_expand_row (tree_view, tmp_path, tree, node,
12071                                             TRUE, animate);
12072          if (t)
12073            retval = TRUE;
12074
12075          gtk_tree_path_next (tmp_path);
12076          node = _pspp_rbtree_next (tree, node);
12077        }
12078       while (node != NULL);
12079
12080       gtk_tree_path_free (tmp_path);
12081
12082       return retval;
12083     }
12084
12085   g_signal_emit (tree_view, tree_view_signals[TEST_EXPAND_ROW], 0, &iter, path, &expand);
12086
12087   if (!gtk_tree_model_iter_has_child (tree_view->priv->model, &iter))
12088     return FALSE;
12089
12090   if (expand)
12091     return FALSE;
12092
12093   node->children = _pspp_rbtree_new ();
12094   node->children->parent_tree = tree;
12095   node->children->parent_node = node;
12096
12097   gtk_tree_model_iter_children (tree_view->priv->model, &temp, &iter);
12098
12099   pspp_sheet_view_build_tree (tree_view,
12100                             node->children,
12101                             &temp,
12102                             gtk_tree_path_get_depth (path) + 1,
12103                             open_all);
12104
12105   remove_expand_collapse_timeout (tree_view);
12106
12107   if (animate)
12108     add_expand_collapse_timeout (tree_view, tree, node, TRUE);
12109
12110   install_presize_handler (tree_view);
12111
12112   g_signal_emit (tree_view, tree_view_signals[ROW_EXPANDED], 0, &iter, path);
12113   if (open_all && node->children)
12114     {
12115       _pspp_rbtree_traverse (node->children,
12116                             node->children->root,
12117                             G_PRE_ORDER,
12118                             pspp_sheet_view_expand_all_emission_helper,
12119                             tree_view);
12120     }
12121   return TRUE;
12122 }
12123
12124
12125 /**
12126  * pspp_sheet_view_expand_row:
12127  * @tree_view: a #PsppSheetView
12128  * @path: path to a row
12129  * @open_all: whether to recursively expand, or just expand immediate children
12130  *
12131  * Opens the row so its children are visible.
12132  *
12133  * Return value: %TRUE if the row existed and had children
12134  **/
12135 gboolean
12136 pspp_sheet_view_expand_row (PsppSheetView *tree_view,
12137                           GtkTreePath *path,
12138                           gboolean     open_all)
12139 {
12140   GtkRBTree *tree;
12141   GtkRBNode *node;
12142
12143   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12144   g_return_val_if_fail (tree_view->priv->model != NULL, FALSE);
12145   g_return_val_if_fail (path != NULL, FALSE);
12146
12147   if (_pspp_sheet_view_find_node (tree_view,
12148                                 path,
12149                                 &tree,
12150                                 &node))
12151     return FALSE;
12152
12153   if (tree != NULL)
12154     return pspp_sheet_view_real_expand_row (tree_view, path, tree, node, open_all, FALSE);
12155   else
12156     return FALSE;
12157 }
12158
12159 static gboolean
12160 pspp_sheet_view_real_collapse_row (PsppSheetView *tree_view,
12161                                  GtkTreePath *path,
12162                                  GtkRBTree   *tree,
12163                                  GtkRBNode   *node,
12164                                  gboolean     animate)
12165 {
12166   GtkTreeIter iter;
12167   GtkTreeIter children;
12168   gboolean collapse;
12169   gint x, y;
12170   GList *list;
12171   GdkWindow *child, *parent;
12172
12173   if (animate)
12174     g_object_get (gtk_widget_get_settings (GTK_WIDGET (tree_view)),
12175                   "gtk-enable-animations", &animate,
12176                   NULL);
12177
12178   remove_auto_expand_timeout (tree_view);
12179
12180   if (node->children == NULL)
12181     return FALSE;
12182
12183   gtk_tree_model_get_iter (tree_view->priv->model, &iter, path);
12184
12185   g_signal_emit (tree_view, tree_view_signals[TEST_COLLAPSE_ROW], 0, &iter, path, &collapse);
12186
12187   if (collapse)
12188     return FALSE;
12189
12190   /* if the prelighted node is a child of us, we want to unprelight it.  We have
12191    * a chance to prelight the correct node below */
12192
12193   if (tree_view->priv->prelight_tree)
12194     {
12195       GtkRBTree *parent_tree;
12196       GtkRBNode *parent_node;
12197
12198       parent_tree = tree_view->priv->prelight_tree->parent_tree;
12199       parent_node = tree_view->priv->prelight_tree->parent_node;
12200       while (parent_tree)
12201         {
12202           if (parent_tree == tree && parent_node == node)
12203             {
12204               ensure_unprelighted (tree_view);
12205               break;
12206             }
12207           parent_node = parent_tree->parent_node;
12208           parent_tree = parent_tree->parent_tree;
12209         }
12210     }
12211
12212   TREE_VIEW_INTERNAL_ASSERT (gtk_tree_model_iter_children (tree_view->priv->model, &children, &iter), FALSE);
12213
12214   for (list = tree_view->priv->columns; list; list = list->next)
12215     {
12216       PsppSheetViewColumn *column = list->data;
12217
12218       if (column->visible == FALSE)
12219         continue;
12220       if (pspp_sheet_view_column_get_sizing (column) == PSPP_SHEET_VIEW_COLUMN_AUTOSIZE)
12221         _pspp_sheet_view_column_cell_set_dirty (column, TRUE);
12222     }
12223
12224   if (tree_view->priv->destroy_count_func)
12225     {
12226       GtkTreePath *child_path;
12227       gint child_count = 0;
12228       child_path = gtk_tree_path_copy (path);
12229       gtk_tree_path_down (child_path);
12230       if (node->children)
12231         _pspp_rbtree_traverse (node->children, node->children->root, G_POST_ORDER, count_children_helper, &child_count);
12232       tree_view->priv->destroy_count_func (tree_view, child_path, child_count, tree_view->priv->destroy_count_data);
12233       gtk_tree_path_free (child_path);
12234     }
12235
12236   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
12237     {
12238       GtkTreePath *cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
12239
12240       if (gtk_tree_path_is_ancestor (path, cursor_path))
12241         {
12242           gtk_tree_row_reference_free (tree_view->priv->cursor);
12243           tree_view->priv->cursor = gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
12244                                                                       tree_view->priv->model,
12245                                                                       path);
12246         }
12247       gtk_tree_path_free (cursor_path);
12248     }
12249
12250   if (gtk_tree_row_reference_valid (tree_view->priv->anchor))
12251     {
12252       GtkTreePath *anchor_path = gtk_tree_row_reference_get_path (tree_view->priv->anchor);
12253       if (gtk_tree_path_is_ancestor (path, anchor_path))
12254         {
12255           gtk_tree_row_reference_free (tree_view->priv->anchor);
12256           tree_view->priv->anchor = NULL;
12257         }
12258       gtk_tree_path_free (anchor_path);
12259     }
12260
12261   /* Stop a pending double click */
12262   tree_view->priv->last_button_x = -1;
12263   tree_view->priv->last_button_y = -1;
12264
12265   remove_expand_collapse_timeout (tree_view);
12266
12267   if (pspp_sheet_view_unref_and_check_selection_tree (tree_view, node->children))
12268     {
12269       _pspp_rbtree_remove (node->children);
12270       g_signal_emit_by_name (tree_view->priv->selection, "changed");
12271     }
12272   else
12273     _pspp_rbtree_remove (node->children);
12274   
12275   if (animate)
12276     add_expand_collapse_timeout (tree_view, tree, node, FALSE);
12277   
12278   if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
12279     {
12280       gtk_widget_queue_resize (GTK_WIDGET (tree_view));
12281     }
12282
12283   g_signal_emit (tree_view, tree_view_signals[ROW_COLLAPSED], 0, &iter, path);
12284
12285   if (gtk_widget_get_mapped (GTK_WIDGET (tree_view)))
12286     {
12287       /* now that we've collapsed all rows, we want to try to set the prelight
12288        * again. To do this, we fake a motion event and send it to ourselves. */
12289
12290       child = tree_view->priv->bin_window;
12291       parent = gdk_window_get_parent (child);
12292
12293       if (gdk_window_get_pointer (parent, &x, &y, NULL) == child)
12294         {
12295           GdkEventMotion event;
12296           gint child_x, child_y;
12297
12298           gdk_window_get_position (child, &child_x, &child_y);
12299
12300           event.window = tree_view->priv->bin_window;
12301           event.x = x - child_x;
12302           event.y = y - child_y;
12303
12304           /* despite the fact this isn't a real event, I'm almost positive it will
12305            * never trigger a drag event.  maybe_drag is the only function that uses
12306            * more than just event.x and event.y. */
12307           pspp_sheet_view_motion_bin_window (GTK_WIDGET (tree_view), &event);
12308         }
12309     }
12310
12311   return TRUE;
12312 }
12313
12314 /**
12315  * pspp_sheet_view_collapse_row:
12316  * @tree_view: a #PsppSheetView
12317  * @path: path to a row in the @tree_view
12318  *
12319  * Collapses a row (hides its child rows, if they exist).
12320  *
12321  * Return value: %TRUE if the row was collapsed.
12322  **/
12323 gboolean
12324 pspp_sheet_view_collapse_row (PsppSheetView *tree_view,
12325                             GtkTreePath *path)
12326 {
12327   GtkRBTree *tree;
12328   GtkRBNode *node;
12329
12330   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12331   g_return_val_if_fail (tree_view->priv->tree != NULL, FALSE);
12332   g_return_val_if_fail (path != NULL, FALSE);
12333
12334   if (_pspp_sheet_view_find_node (tree_view,
12335                                 path,
12336                                 &tree,
12337                                 &node))
12338     return FALSE;
12339
12340   if (tree == NULL || node->children == NULL)
12341     return FALSE;
12342
12343   return pspp_sheet_view_real_collapse_row (tree_view, path, tree, node, FALSE);
12344 }
12345
12346 static void
12347 pspp_sheet_view_map_expanded_rows_helper (PsppSheetView            *tree_view,
12348                                         GtkRBTree              *tree,
12349                                         GtkTreePath            *path,
12350                                         PsppSheetViewMappingFunc  func,
12351                                         gpointer                user_data)
12352 {
12353   GtkRBNode *node;
12354
12355   if (tree == NULL || tree->root == NULL)
12356     return;
12357
12358   node = tree->root;
12359
12360   while (node && node->left != tree->nil)
12361     node = node->left;
12362
12363   while (node)
12364     {
12365       if (node->children)
12366         {
12367           (* func) (tree_view, path, user_data);
12368           gtk_tree_path_down (path);
12369           pspp_sheet_view_map_expanded_rows_helper (tree_view, node->children, path, func, user_data);
12370           gtk_tree_path_up (path);
12371         }
12372       gtk_tree_path_next (path);
12373       node = _pspp_rbtree_next (tree, node);
12374     }
12375 }
12376
12377 /**
12378  * pspp_sheet_view_map_expanded_rows:
12379  * @tree_view: A #PsppSheetView
12380  * @func: A function to be called
12381  * @data: User data to be passed to the function.
12382  *
12383  * Calls @func on all expanded rows.
12384  **/
12385 void
12386 pspp_sheet_view_map_expanded_rows (PsppSheetView            *tree_view,
12387                                  PsppSheetViewMappingFunc  func,
12388                                  gpointer                user_data)
12389 {
12390   GtkTreePath *path;
12391
12392   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12393   g_return_if_fail (func != NULL);
12394
12395   path = gtk_tree_path_new_first ();
12396
12397   pspp_sheet_view_map_expanded_rows_helper (tree_view,
12398                                           tree_view->priv->tree,
12399                                           path, func, user_data);
12400
12401   gtk_tree_path_free (path);
12402 }
12403
12404 /**
12405  * pspp_sheet_view_row_expanded:
12406  * @tree_view: A #PsppSheetView.
12407  * @path: A #GtkTreePath to test expansion state.
12408  *
12409  * Returns %TRUE if the node pointed to by @path is expanded in @tree_view.
12410  *
12411  * Return value: %TRUE if #path is expanded.
12412  **/
12413 gboolean
12414 pspp_sheet_view_row_expanded (PsppSheetView *tree_view,
12415                             GtkTreePath *path)
12416 {
12417   GtkRBTree *tree;
12418   GtkRBNode *node;
12419
12420   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12421   g_return_val_if_fail (path != NULL, FALSE);
12422
12423   _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
12424
12425   if (node == NULL)
12426     return FALSE;
12427
12428   return (node->children != NULL);
12429 }
12430
12431 /**
12432  * pspp_sheet_view_get_reorderable:
12433  * @tree_view: a #PsppSheetView
12434  *
12435  * Retrieves whether the user can reorder the tree via drag-and-drop. See
12436  * pspp_sheet_view_set_reorderable().
12437  *
12438  * Return value: %TRUE if the tree can be reordered.
12439  **/
12440 gboolean
12441 pspp_sheet_view_get_reorderable (PsppSheetView *tree_view)
12442 {
12443   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
12444
12445   return tree_view->priv->reorderable;
12446 }
12447
12448 /**
12449  * pspp_sheet_view_set_reorderable:
12450  * @tree_view: A #PsppSheetView.
12451  * @reorderable: %TRUE, if the tree can be reordered.
12452  *
12453  * This function is a convenience function to allow you to reorder
12454  * models that support the #GtkDragSourceIface and the
12455  * #GtkDragDestIface.  Both #GtkTreeStore and #GtkListStore support
12456  * these.  If @reorderable is %TRUE, then the user can reorder the
12457  * model by dragging and dropping rows. The developer can listen to
12458  * these changes by connecting to the model's row_inserted and
12459  * row_deleted signals. The reordering is implemented by setting up
12460  * the tree view as a drag source and destination. Therefore, drag and
12461  * drop can not be used in a reorderable view for any other purpose.
12462  *
12463  * This function does not give you any degree of control over the order -- any
12464  * reordering is allowed.  If more control is needed, you should probably
12465  * handle drag and drop manually.
12466  **/
12467 void
12468 pspp_sheet_view_set_reorderable (PsppSheetView *tree_view,
12469                                gboolean     reorderable)
12470 {
12471   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12472
12473   reorderable = reorderable != FALSE;
12474
12475   if (tree_view->priv->reorderable == reorderable)
12476     return;
12477
12478   if (reorderable)
12479     {
12480       const GtkTargetEntry row_targets[] = {
12481         { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
12482       };
12483
12484       pspp_sheet_view_enable_model_drag_source (tree_view,
12485                                               GDK_BUTTON1_MASK,
12486                                               row_targets,
12487                                               G_N_ELEMENTS (row_targets),
12488                                               GDK_ACTION_MOVE);
12489       pspp_sheet_view_enable_model_drag_dest (tree_view,
12490                                             row_targets,
12491                                             G_N_ELEMENTS (row_targets),
12492                                             GDK_ACTION_MOVE);
12493     }
12494   else
12495     {
12496       pspp_sheet_view_unset_rows_drag_source (tree_view);
12497       pspp_sheet_view_unset_rows_drag_dest (tree_view);
12498     }
12499
12500   tree_view->priv->reorderable = reorderable;
12501
12502   g_object_notify (G_OBJECT (tree_view), "reorderable");
12503 }
12504
12505 static void
12506 pspp_sheet_view_real_set_cursor (PsppSheetView     *tree_view,
12507                                GtkTreePath     *path,
12508                                gboolean         clear_and_select,
12509                                gboolean         clamp_node)
12510 {
12511   GtkRBTree *tree = NULL;
12512   GtkRBNode *node = NULL;
12513
12514   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
12515     {
12516       GtkTreePath *cursor_path;
12517       cursor_path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
12518       pspp_sheet_view_queue_draw_path (tree_view, cursor_path, NULL);
12519       gtk_tree_path_free (cursor_path);
12520     }
12521
12522   gtk_tree_row_reference_free (tree_view->priv->cursor);
12523   tree_view->priv->cursor = NULL;
12524
12525   /* One cannot set the cursor on a separator.   Also, if
12526    * _pspp_sheet_view_find_node returns TRUE, it ran out of tree
12527    * before finding the tree and node belonging to path.  The
12528    * path maps to a non-existing path and we will silently bail out.
12529    * We unset tree and node to avoid further processing.
12530    */
12531   if (!row_is_separator (tree_view, NULL, path)
12532       && _pspp_sheet_view_find_node (tree_view, path, &tree, &node) == FALSE)
12533     {
12534       tree_view->priv->cursor =
12535           gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view),
12536                                             tree_view->priv->model,
12537                                             path);
12538     }
12539   else
12540     {
12541       tree = NULL;
12542       node = NULL;
12543     }
12544
12545   if (tree != NULL)
12546     {
12547       GtkRBTree *new_tree = NULL;
12548       GtkRBNode *new_node = NULL;
12549
12550       if (clear_and_select && !tree_view->priv->ctrl_pressed)
12551         {
12552           GtkTreeSelectMode mode = 0;
12553
12554           if (tree_view->priv->ctrl_pressed)
12555             mode |= GTK_TREE_SELECT_MODE_TOGGLE;
12556           if (tree_view->priv->shift_pressed)
12557             mode |= GTK_TREE_SELECT_MODE_EXTEND;
12558
12559           _pspp_sheet_selection_internal_select_node (tree_view->priv->selection,
12560                                                     node, tree, path, mode,
12561                                                     FALSE);
12562         }
12563
12564       /* We have to re-find tree and node here again, somebody might have
12565        * cleared the node or the whole tree in the PsppSheetSelection::changed
12566        * callback. If the nodes differ we bail out here.
12567        */
12568       _pspp_sheet_view_find_node (tree_view, path, &new_tree, &new_node);
12569
12570       if (tree != new_tree || node != new_node)
12571         return;
12572
12573       if (clamp_node)
12574         {
12575           pspp_sheet_view_clamp_node_visible (tree_view, tree, node);
12576           _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
12577         }
12578     }
12579
12580   g_signal_emit (tree_view, tree_view_signals[CURSOR_CHANGED], 0);
12581 }
12582
12583 /**
12584  * pspp_sheet_view_get_cursor:
12585  * @tree_view: A #PsppSheetView
12586  * @path: (allow-none): A pointer to be filled with the current cursor path, or %NULL
12587  * @focus_column: (allow-none): A pointer to be filled with the current focus column, or %NULL
12588  *
12589  * Fills in @path and @focus_column with the current path and focus column.  If
12590  * the cursor isn't currently set, then *@path will be %NULL.  If no column
12591  * currently has focus, then *@focus_column will be %NULL.
12592  *
12593  * The returned #GtkTreePath must be freed with gtk_tree_path_free() when
12594  * you are done with it.
12595  **/
12596 void
12597 pspp_sheet_view_get_cursor (PsppSheetView        *tree_view,
12598                           GtkTreePath       **path,
12599                           PsppSheetViewColumn **focus_column)
12600 {
12601   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12602
12603   if (path)
12604     {
12605       if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
12606         *path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
12607       else
12608         *path = NULL;
12609     }
12610
12611   if (focus_column)
12612     {
12613       *focus_column = tree_view->priv->focus_column;
12614     }
12615 }
12616
12617 /**
12618  * pspp_sheet_view_set_cursor:
12619  * @tree_view: A #PsppSheetView
12620  * @path: A #GtkTreePath
12621  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
12622  * @start_editing: %TRUE if the specified cell should start being edited.
12623  *
12624  * Sets the current keyboard focus to be at @path, and selects it.  This is
12625  * useful when you want to focus the user's attention on a particular row.  If
12626  * @focus_column is not %NULL, then focus is given to the column specified by 
12627  * it. Additionally, if @focus_column is specified, and @start_editing is 
12628  * %TRUE, then editing should be started in the specified cell.  
12629  * This function is often followed by @gtk_widget_grab_focus (@tree_view) 
12630  * in order to give keyboard focus to the widget.  Please note that editing 
12631  * can only happen when the widget is realized.
12632  *
12633  * If @path is invalid for @model, the current cursor (if any) will be unset
12634  * and the function will return without failing.
12635  **/
12636 void
12637 pspp_sheet_view_set_cursor (PsppSheetView       *tree_view,
12638                           GtkTreePath       *path,
12639                           PsppSheetViewColumn *focus_column,
12640                           gboolean           start_editing)
12641 {
12642   pspp_sheet_view_set_cursor_on_cell (tree_view, path, focus_column,
12643                                     NULL, start_editing);
12644 }
12645
12646 /**
12647  * pspp_sheet_view_set_cursor_on_cell:
12648  * @tree_view: A #PsppSheetView
12649  * @path: A #GtkTreePath
12650  * @focus_column: (allow-none): A #PsppSheetViewColumn, or %NULL
12651  * @focus_cell: (allow-none): A #GtkCellRenderer, or %NULL
12652  * @start_editing: %TRUE if the specified cell should start being edited.
12653  *
12654  * Sets the current keyboard focus to be at @path, and selects it.  This is
12655  * useful when you want to focus the user's attention on a particular row.  If
12656  * @focus_column is not %NULL, then focus is given to the column specified by
12657  * it. If @focus_column and @focus_cell are not %NULL, and @focus_column
12658  * contains 2 or more editable or activatable cells, then focus is given to
12659  * the cell specified by @focus_cell. Additionally, if @focus_column is
12660  * specified, and @start_editing is %TRUE, then editing should be started in
12661  * the specified cell.  This function is often followed by
12662  * @gtk_widget_grab_focus (@tree_view) in order to give keyboard focus to the
12663  * widget.  Please note that editing can only happen when the widget is
12664  * realized.
12665  *
12666  * If @path is invalid for @model, the current cursor (if any) will be unset
12667  * and the function will return without failing.
12668  *
12669  * Since: 2.2
12670  **/
12671 void
12672 pspp_sheet_view_set_cursor_on_cell (PsppSheetView       *tree_view,
12673                                   GtkTreePath       *path,
12674                                   PsppSheetViewColumn *focus_column,
12675                                   GtkCellRenderer   *focus_cell,
12676                                   gboolean           start_editing)
12677 {
12678   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12679   g_return_if_fail (path != NULL);
12680   g_return_if_fail (focus_column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (focus_column));
12681
12682   if (!tree_view->priv->model)
12683     return;
12684
12685   if (focus_cell)
12686     {
12687       g_return_if_fail (focus_column);
12688       g_return_if_fail (GTK_IS_CELL_RENDERER (focus_cell));
12689     }
12690
12691   /* cancel the current editing, if it exists */
12692   if (tree_view->priv->edited_column &&
12693       tree_view->priv->edited_column->editable_widget)
12694     pspp_sheet_view_stop_editing (tree_view, TRUE);
12695
12696   pspp_sheet_view_real_set_cursor (tree_view, path, TRUE, TRUE);
12697
12698   if (focus_column && focus_column->visible)
12699     {
12700       GList *list;
12701       gboolean column_in_tree = FALSE;
12702
12703       for (list = tree_view->priv->columns; list; list = list->next)
12704         if (list->data == focus_column)
12705           {
12706             column_in_tree = TRUE;
12707             break;
12708           }
12709       g_return_if_fail (column_in_tree);
12710       tree_view->priv->focus_column = focus_column;
12711       if (focus_cell)
12712         pspp_sheet_view_column_focus_cell (focus_column, focus_cell);
12713       if (start_editing)
12714         pspp_sheet_view_start_editing (tree_view, path);
12715     }
12716 }
12717
12718 /**
12719  * pspp_sheet_view_get_bin_window:
12720  * @tree_view: A #PsppSheetView
12721  * 
12722  * Returns the window that @tree_view renders to.  This is used primarily to
12723  * compare to <literal>event->window</literal> to confirm that the event on
12724  * @tree_view is on the right window.
12725  * 
12726  * Return value: A #GdkWindow, or %NULL when @tree_view hasn't been realized yet
12727  **/
12728 GdkWindow *
12729 pspp_sheet_view_get_bin_window (PsppSheetView *tree_view)
12730 {
12731   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
12732
12733   return tree_view->priv->bin_window;
12734 }
12735
12736 /**
12737  * pspp_sheet_view_get_path_at_pos:
12738  * @tree_view: A #PsppSheetView.
12739  * @x: The x position to be identified (relative to bin_window).
12740  * @y: The y position to be identified (relative to bin_window).
12741  * @path: (out) (allow-none): A pointer to a #GtkTreePath pointer to be filled in, or %NULL
12742  * @column: (out) (allow-none): A pointer to a #PsppSheetViewColumn pointer to be filled in, or %NULL
12743  * @cell_x: (out) (allow-none): A pointer where the X coordinate relative to the cell can be placed, or %NULL
12744  * @cell_y: (out) (allow-none): A pointer where the Y coordinate relative to the cell can be placed, or %NULL
12745  *
12746  * Finds the path at the point (@x, @y), relative to bin_window coordinates
12747  * (please see pspp_sheet_view_get_bin_window()).
12748  * That is, @x and @y are relative to an events coordinates. @x and @y must
12749  * come from an event on the @tree_view only where <literal>event->window ==
12750  * pspp_sheet_view_get_bin_window (<!-- -->)</literal>. It is primarily for
12751  * things like popup menus. If @path is non-%NULL, then it will be filled
12752  * with the #GtkTreePath at that point.  This path should be freed with
12753  * gtk_tree_path_free().  If @column is non-%NULL, then it will be filled
12754  * with the column at that point.  @cell_x and @cell_y return the coordinates
12755  * relative to the cell background (i.e. the @background_area passed to
12756  * gtk_cell_renderer_render()).  This function is only meaningful if
12757  * @tree_view is realized.  Therefore this function will always return %FALSE
12758  * if @tree_view is not realized or does not have a model.
12759  *
12760  * For converting widget coordinates (eg. the ones you get from
12761  * GtkWidget::query-tooltip), please see
12762  * pspp_sheet_view_convert_widget_to_bin_window_coords().
12763  *
12764  * Return value: %TRUE if a row exists at that coordinate.
12765  **/
12766 gboolean
12767 pspp_sheet_view_get_path_at_pos (PsppSheetView        *tree_view,
12768                                gint                x,
12769                                gint                y,
12770                                GtkTreePath       **path,
12771                                PsppSheetViewColumn **column,
12772                                gint               *cell_x,
12773                                gint               *cell_y)
12774 {
12775   GtkRBTree *tree;
12776   GtkRBNode *node;
12777   gint y_offset;
12778
12779   g_return_val_if_fail (tree_view != NULL, FALSE);
12780
12781   if (path)
12782     *path = NULL;
12783   if (column)
12784     *column = NULL;
12785
12786   if (tree_view->priv->bin_window == NULL)
12787     return FALSE;
12788
12789   if (tree_view->priv->tree == NULL)
12790     return FALSE;
12791
12792   if (x > tree_view->priv->hadjustment->upper)
12793     return FALSE;
12794
12795   if (x < 0 || y < 0)
12796     return FALSE;
12797
12798   if (column || cell_x)
12799     {
12800       PsppSheetViewColumn *tmp_column;
12801       PsppSheetViewColumn *last_column = NULL;
12802       GList *list;
12803       gint remaining_x = x;
12804       gboolean found = FALSE;
12805       gboolean rtl;
12806
12807       rtl = (gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL);
12808       for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
12809            list;
12810            list = (rtl ? list->prev : list->next))
12811         {
12812           tmp_column = list->data;
12813
12814           if (tmp_column->visible == FALSE)
12815             continue;
12816
12817           last_column = tmp_column;
12818           if (remaining_x <= tmp_column->width)
12819             {
12820               found = TRUE;
12821
12822               if (column)
12823                 *column = tmp_column;
12824
12825               if (cell_x)
12826                 *cell_x = remaining_x;
12827
12828               break;
12829             }
12830           remaining_x -= tmp_column->width;
12831         }
12832
12833       /* If found is FALSE and there is a last_column, then it the remainder
12834        * space is in that area
12835        */
12836       if (!found)
12837         {
12838           if (last_column)
12839             {
12840               if (column)
12841                 *column = last_column;
12842               
12843               if (cell_x)
12844                 *cell_x = last_column->width + remaining_x;
12845             }
12846           else
12847             {
12848               return FALSE;
12849             }
12850         }
12851     }
12852
12853   y_offset = _pspp_rbtree_find_offset (tree_view->priv->tree,
12854                                       TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, y),
12855                                       &tree, &node);
12856
12857   if (tree == NULL)
12858     return FALSE;
12859
12860   if (cell_y)
12861     *cell_y = y_offset;
12862
12863   if (path)
12864     *path = _pspp_sheet_view_find_path (tree_view, tree, node);
12865
12866   return TRUE;
12867 }
12868
12869
12870 /**
12871  * pspp_sheet_view_get_cell_area:
12872  * @tree_view: a #PsppSheetView
12873  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
12874  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordinates
12875  * @rect: rectangle to fill with cell rect
12876  *
12877  * Fills the bounding rectangle in bin_window coordinates for the cell at the
12878  * row specified by @path and the column specified by @column.  If @path is
12879  * %NULL, or points to a path not currently displayed, the @y and @height fields
12880  * of the rectangle will be filled with 0. If @column is %NULL, the @x and @width
12881  * fields will be filled with 0.  The sum of all cell rects does not cover the
12882  * entire tree; there are extra pixels in between rows, for example. The
12883  * returned rectangle is equivalent to the @cell_area passed to
12884  * gtk_cell_renderer_render().  This function is only valid if @tree_view is
12885  * realized.
12886  **/
12887 void
12888 pspp_sheet_view_get_cell_area (PsppSheetView        *tree_view,
12889                              GtkTreePath        *path,
12890                              PsppSheetViewColumn  *column,
12891                              GdkRectangle       *rect)
12892 {
12893   GtkRBTree *tree = NULL;
12894   GtkRBNode *node = NULL;
12895   gint vertical_separator;
12896   gint horizontal_separator;
12897
12898   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12899   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
12900   g_return_if_fail (rect != NULL);
12901   g_return_if_fail (!column || column->tree_view == (GtkWidget *) tree_view);
12902   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (tree_view)));
12903
12904   gtk_widget_style_get (GTK_WIDGET (tree_view),
12905                         "vertical-separator", &vertical_separator,
12906                         "horizontal-separator", &horizontal_separator,
12907                         NULL);
12908
12909   rect->x = 0;
12910   rect->y = 0;
12911   rect->width = 0;
12912   rect->height = 0;
12913
12914   if (column)
12915     {
12916       rect->x = column->button->allocation.x + horizontal_separator/2;
12917       rect->width = column->button->allocation.width - horizontal_separator;
12918     }
12919
12920   if (path)
12921     {
12922       gboolean ret = _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
12923
12924       /* Get vertical coords */
12925       if ((!ret && tree == NULL) || ret)
12926         return;
12927
12928       rect->y = CELL_FIRST_PIXEL (tree_view, tree, node, vertical_separator);
12929       rect->height = MAX (CELL_HEIGHT (node, vertical_separator), tree_view->priv->expander_size - vertical_separator);
12930
12931       if (column &&
12932           pspp_sheet_view_is_expander_column (tree_view, column))
12933         {
12934           gint depth = gtk_tree_path_get_depth (path);
12935           gboolean rtl;
12936
12937           rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
12938
12939           if (!rtl)
12940             rect->x += (depth - 1) * tree_view->priv->level_indentation;
12941           rect->width -= (depth - 1) * tree_view->priv->level_indentation;
12942
12943           if (TREE_VIEW_DRAW_EXPANDERS (tree_view))
12944             {
12945               if (!rtl)
12946                 rect->x += depth * tree_view->priv->expander_size;
12947               rect->width -= depth * tree_view->priv->expander_size;
12948             }
12949
12950           rect->width = MAX (rect->width, 0);
12951         }
12952     }
12953 }
12954
12955 /**
12956  * pspp_sheet_view_get_background_area:
12957  * @tree_view: a #PsppSheetView
12958  * @path: (allow-none): a #GtkTreePath for the row, or %NULL to get only horizontal coordinates
12959  * @column: (allow-none): a #PsppSheetViewColumn for the column, or %NULL to get only vertical coordiantes
12960  * @rect: rectangle to fill with cell background rect
12961  *
12962  * Fills the bounding rectangle in bin_window coordinates for the cell at the
12963  * row specified by @path and the column specified by @column.  If @path is
12964  * %NULL, or points to a node not found in the tree, the @y and @height fields of
12965  * the rectangle will be filled with 0. If @column is %NULL, the @x and @width
12966  * fields will be filled with 0.  The returned rectangle is equivalent to the
12967  * @background_area passed to gtk_cell_renderer_render().  These background
12968  * areas tile to cover the entire bin window.  Contrast with the @cell_area,
12969  * returned by pspp_sheet_view_get_cell_area(), which returns only the cell
12970  * itself, excluding surrounding borders and the tree expander area.
12971  *
12972  **/
12973 void
12974 pspp_sheet_view_get_background_area (PsppSheetView        *tree_view,
12975                                    GtkTreePath        *path,
12976                                    PsppSheetViewColumn  *column,
12977                                    GdkRectangle       *rect)
12978 {
12979   GtkRBTree *tree = NULL;
12980   GtkRBNode *node = NULL;
12981
12982   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
12983   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
12984   g_return_if_fail (rect != NULL);
12985
12986   rect->x = 0;
12987   rect->y = 0;
12988   rect->width = 0;
12989   rect->height = 0;
12990
12991   if (path)
12992     {
12993       /* Get vertical coords */
12994
12995       if (!_pspp_sheet_view_find_node (tree_view, path, &tree, &node) &&
12996           tree == NULL)
12997         return;
12998
12999       rect->y = BACKGROUND_FIRST_PIXEL (tree_view, tree, node);
13000
13001       rect->height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
13002     }
13003
13004   if (column)
13005     {
13006       gint x2 = 0;
13007
13008       pspp_sheet_view_get_background_xrange (tree_view, tree, column, &rect->x, &x2);
13009       rect->width = x2 - rect->x;
13010     }
13011 }
13012
13013 /**
13014  * pspp_sheet_view_get_visible_rect:
13015  * @tree_view: a #PsppSheetView
13016  * @visible_rect: rectangle to fill
13017  *
13018  * Fills @visible_rect with the currently-visible region of the
13019  * buffer, in tree coordinates. Convert to bin_window coordinates with
13020  * pspp_sheet_view_convert_tree_to_bin_window_coords().
13021  * Tree coordinates start at 0,0 for row 0 of the tree, and cover the entire
13022  * scrollable area of the tree.
13023  **/
13024 void
13025 pspp_sheet_view_get_visible_rect (PsppSheetView  *tree_view,
13026                                 GdkRectangle *visible_rect)
13027 {
13028   GtkWidget *widget;
13029
13030   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13031
13032   widget = GTK_WIDGET (tree_view);
13033
13034   if (visible_rect)
13035     {
13036       visible_rect->x = tree_view->priv->hadjustment->value;
13037       visible_rect->y = tree_view->priv->vadjustment->value;
13038       visible_rect->width = widget->allocation.width;
13039       visible_rect->height = widget->allocation.height - TREE_VIEW_HEADER_HEIGHT (tree_view);
13040     }
13041 }
13042
13043 /**
13044  * pspp_sheet_view_widget_to_tree_coords:
13045  * @tree_view: a #PsppSheetView
13046  * @wx: X coordinate relative to bin_window
13047  * @wy: Y coordinate relative to bin_window
13048  * @tx: return location for tree X coordinate
13049  * @ty: return location for tree Y coordinate
13050  *
13051  * Converts bin_window coordinates to coordinates for the
13052  * tree (the full scrollable area of the tree).
13053  *
13054  * Deprecated: 2.12: Due to historial reasons the name of this function is
13055  * incorrect.  For converting coordinates relative to the widget to
13056  * bin_window coordinates, please see
13057  * pspp_sheet_view_convert_widget_to_bin_window_coords().
13058  *
13059  **/
13060 void
13061 pspp_sheet_view_widget_to_tree_coords (PsppSheetView *tree_view,
13062                                       gint         wx,
13063                                       gint         wy,
13064                                       gint        *tx,
13065                                       gint        *ty)
13066 {
13067   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13068
13069   if (tx)
13070     *tx = wx + tree_view->priv->hadjustment->value;
13071   if (ty)
13072     *ty = wy + tree_view->priv->dy;
13073 }
13074
13075 /**
13076  * pspp_sheet_view_tree_to_widget_coords:
13077  * @tree_view: a #PsppSheetView
13078  * @tx: tree X coordinate
13079  * @ty: tree Y coordinate
13080  * @wx: return location for X coordinate relative to bin_window
13081  * @wy: return location for Y coordinate relative to bin_window
13082  *
13083  * Converts tree coordinates (coordinates in full scrollable area of the tree)
13084  * to bin_window coordinates.
13085  *
13086  * Deprecated: 2.12: Due to historial reasons the name of this function is
13087  * incorrect.  For converting bin_window coordinates to coordinates relative
13088  * to bin_window, please see
13089  * pspp_sheet_view_convert_bin_window_to_widget_coords().
13090  *
13091  **/
13092 void
13093 pspp_sheet_view_tree_to_widget_coords (PsppSheetView *tree_view,
13094                                      gint         tx,
13095                                      gint         ty,
13096                                      gint        *wx,
13097                                      gint        *wy)
13098 {
13099   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13100
13101   if (wx)
13102     *wx = tx - tree_view->priv->hadjustment->value;
13103   if (wy)
13104     *wy = ty - tree_view->priv->dy;
13105 }
13106
13107
13108 /**
13109  * pspp_sheet_view_convert_widget_to_tree_coords:
13110  * @tree_view: a #PsppSheetView
13111  * @wx: X coordinate relative to the widget
13112  * @wy: Y coordinate relative to the widget
13113  * @tx: return location for tree X coordinate
13114  * @ty: return location for tree Y coordinate
13115  *
13116  * Converts widget coordinates to coordinates for the
13117  * tree (the full scrollable area of the tree).
13118  *
13119  * Since: 2.12
13120  **/
13121 void
13122 pspp_sheet_view_convert_widget_to_tree_coords (PsppSheetView *tree_view,
13123                                              gint         wx,
13124                                              gint         wy,
13125                                              gint        *tx,
13126                                              gint        *ty)
13127 {
13128   gint x, y;
13129
13130   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13131
13132   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view,
13133                                                      wx, wy,
13134                                                      &x, &y);
13135   pspp_sheet_view_convert_bin_window_to_tree_coords (tree_view,
13136                                                    x, y,
13137                                                    tx, ty);
13138 }
13139
13140 /**
13141  * pspp_sheet_view_convert_tree_to_widget_coords:
13142  * @tree_view: a #PsppSheetView
13143  * @tx: X coordinate relative to the tree
13144  * @ty: Y coordinate relative to the tree
13145  * @wx: return location for widget X coordinate
13146  * @wy: return location for widget Y coordinate
13147  *
13148  * Converts tree coordinates (coordinates in full scrollable area of the tree)
13149  * to widget coordinates.
13150  *
13151  * Since: 2.12
13152  **/
13153 void
13154 pspp_sheet_view_convert_tree_to_widget_coords (PsppSheetView *tree_view,
13155                                              gint         tx,
13156                                              gint         ty,
13157                                              gint        *wx,
13158                                              gint        *wy)
13159 {
13160   gint x, y;
13161
13162   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13163
13164   pspp_sheet_view_convert_tree_to_bin_window_coords (tree_view,
13165                                                    tx, ty,
13166                                                    &x, &y);
13167   pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
13168                                                      x, y,
13169                                                      wx, wy);
13170 }
13171
13172 /**
13173  * pspp_sheet_view_convert_widget_to_bin_window_coords:
13174  * @tree_view: a #PsppSheetView
13175  * @wx: X coordinate relative to the widget
13176  * @wy: Y coordinate relative to the widget
13177  * @bx: return location for bin_window X coordinate
13178  * @by: return location for bin_window Y coordinate
13179  *
13180  * Converts widget coordinates to coordinates for the bin_window
13181  * (see pspp_sheet_view_get_bin_window()).
13182  *
13183  * Since: 2.12
13184  **/
13185 void
13186 pspp_sheet_view_convert_widget_to_bin_window_coords (PsppSheetView *tree_view,
13187                                                    gint         wx,
13188                                                    gint         wy,
13189                                                    gint        *bx,
13190                                                    gint        *by)
13191 {
13192   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13193
13194   if (bx)
13195     *bx = wx + tree_view->priv->hadjustment->value;
13196   if (by)
13197     *by = wy - TREE_VIEW_HEADER_HEIGHT (tree_view);
13198 }
13199
13200 /**
13201  * pspp_sheet_view_convert_bin_window_to_widget_coords:
13202  * @tree_view: a #PsppSheetView
13203  * @bx: bin_window X coordinate
13204  * @by: bin_window Y coordinate
13205  * @wx: return location for widget X coordinate
13206  * @wy: return location for widget Y coordinate
13207  *
13208  * Converts bin_window coordinates (see pspp_sheet_view_get_bin_window())
13209  * to widget relative coordinates.
13210  *
13211  * Since: 2.12
13212  **/
13213 void
13214 pspp_sheet_view_convert_bin_window_to_widget_coords (PsppSheetView *tree_view,
13215                                                    gint         bx,
13216                                                    gint         by,
13217                                                    gint        *wx,
13218                                                    gint        *wy)
13219 {
13220   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13221
13222   if (wx)
13223     *wx = bx - tree_view->priv->hadjustment->value;
13224   if (wy)
13225     *wy = by + TREE_VIEW_HEADER_HEIGHT (tree_view);
13226 }
13227
13228 /**
13229  * pspp_sheet_view_convert_tree_to_bin_window_coords:
13230  * @tree_view: a #PsppSheetView
13231  * @tx: tree X coordinate
13232  * @ty: tree Y coordinate
13233  * @bx: return location for X coordinate relative to bin_window
13234  * @by: return location for Y coordinate relative to bin_window
13235  *
13236  * Converts tree coordinates (coordinates in full scrollable area of the tree)
13237  * to bin_window coordinates.
13238  *
13239  * Since: 2.12
13240  **/
13241 void
13242 pspp_sheet_view_convert_tree_to_bin_window_coords (PsppSheetView *tree_view,
13243                                                  gint         tx,
13244                                                  gint         ty,
13245                                                  gint        *bx,
13246                                                  gint        *by)
13247 {
13248   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13249
13250   if (bx)
13251     *bx = tx;
13252   if (by)
13253     *by = ty - tree_view->priv->dy;
13254 }
13255
13256 /**
13257  * pspp_sheet_view_convert_bin_window_to_tree_coords:
13258  * @tree_view: a #PsppSheetView
13259  * @bx: X coordinate relative to bin_window
13260  * @by: Y coordinate relative to bin_window
13261  * @tx: return location for tree X coordinate
13262  * @ty: return location for tree Y coordinate
13263  *
13264  * Converts bin_window coordinates to coordinates for the
13265  * tree (the full scrollable area of the tree).
13266  *
13267  * Since: 2.12
13268  **/
13269 void
13270 pspp_sheet_view_convert_bin_window_to_tree_coords (PsppSheetView *tree_view,
13271                                                  gint         bx,
13272                                                  gint         by,
13273                                                  gint        *tx,
13274                                                  gint        *ty)
13275 {
13276   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13277
13278   if (tx)
13279     *tx = bx;
13280   if (ty)
13281     *ty = by + tree_view->priv->dy;
13282 }
13283
13284
13285
13286 /**
13287  * pspp_sheet_view_get_visible_range:
13288  * @tree_view: A #PsppSheetView
13289  * @start_path: (allow-none): Return location for start of region, or %NULL.
13290  * @end_path: (allow-none): Return location for end of region, or %NULL.
13291  *
13292  * Sets @start_path and @end_path to be the first and last visible path.
13293  * Note that there may be invisible paths in between.
13294  *
13295  * The paths should be freed with gtk_tree_path_free() after use.
13296  *
13297  * Returns: %TRUE, if valid paths were placed in @start_path and @end_path.
13298  *
13299  * Since: 2.8
13300  **/
13301 gboolean
13302 pspp_sheet_view_get_visible_range (PsppSheetView  *tree_view,
13303                                  GtkTreePath **start_path,
13304                                  GtkTreePath **end_path)
13305 {
13306   GtkRBTree *tree;
13307   GtkRBNode *node;
13308   gboolean retval;
13309   
13310   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
13311
13312   if (!tree_view->priv->tree)
13313     return FALSE;
13314
13315   retval = TRUE;
13316
13317   if (start_path)
13318     {
13319       _pspp_rbtree_find_offset (tree_view->priv->tree,
13320                                TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, 0),
13321                                &tree, &node);
13322       if (node)
13323         *start_path = _pspp_sheet_view_find_path (tree_view, tree, node);
13324       else
13325         retval = FALSE;
13326     }
13327
13328   if (end_path)
13329     {
13330       gint y;
13331
13332       if (tree_view->priv->height < tree_view->priv->vadjustment->page_size)
13333         y = tree_view->priv->height - 1;
13334       else
13335         y = TREE_WINDOW_Y_TO_RBTREE_Y (tree_view, tree_view->priv->vadjustment->page_size) - 1;
13336
13337       _pspp_rbtree_find_offset (tree_view->priv->tree, y, &tree, &node);
13338       if (node)
13339         *end_path = _pspp_sheet_view_find_path (tree_view, tree, node);
13340       else
13341         retval = FALSE;
13342     }
13343
13344   return retval;
13345 }
13346
13347 static void
13348 unset_reorderable (PsppSheetView *tree_view)
13349 {
13350   if (tree_view->priv->reorderable)
13351     {
13352       tree_view->priv->reorderable = FALSE;
13353       g_object_notify (G_OBJECT (tree_view), "reorderable");
13354     }
13355 }
13356
13357 /**
13358  * pspp_sheet_view_enable_model_drag_source:
13359  * @tree_view: a #PsppSheetView
13360  * @start_button_mask: Mask of allowed buttons to start drag
13361  * @targets: the table of targets that the drag will support
13362  * @n_targets: the number of items in @targets
13363  * @actions: the bitmask of possible actions for a drag from this
13364  *    widget
13365  *
13366  * Turns @tree_view into a drag source for automatic DND. Calling this
13367  * method sets #PsppSheetView:reorderable to %FALSE.
13368  **/
13369 void
13370 pspp_sheet_view_enable_model_drag_source (PsppSheetView              *tree_view,
13371                                         GdkModifierType           start_button_mask,
13372                                         const GtkTargetEntry     *targets,
13373                                         gint                      n_targets,
13374                                         GdkDragAction             actions)
13375 {
13376   TreeViewDragInfo *di;
13377
13378   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13379
13380   gtk_drag_source_set (GTK_WIDGET (tree_view),
13381                        0,
13382                        targets,
13383                        n_targets,
13384                        actions);
13385
13386   di = ensure_info (tree_view);
13387
13388   di->start_button_mask = start_button_mask;
13389   di->source_actions = actions;
13390   di->source_set = TRUE;
13391
13392   unset_reorderable (tree_view);
13393 }
13394
13395 /**
13396  * pspp_sheet_view_enable_model_drag_dest:
13397  * @tree_view: a #PsppSheetView
13398  * @targets: the table of targets that the drag will support
13399  * @n_targets: the number of items in @targets
13400  * @actions: the bitmask of possible actions for a drag from this
13401  *    widget
13402  * 
13403  * Turns @tree_view into a drop destination for automatic DND. Calling
13404  * this method sets #PsppSheetView:reorderable to %FALSE.
13405  **/
13406 void
13407 pspp_sheet_view_enable_model_drag_dest (PsppSheetView              *tree_view,
13408                                       const GtkTargetEntry     *targets,
13409                                       gint                      n_targets,
13410                                       GdkDragAction             actions)
13411 {
13412   TreeViewDragInfo *di;
13413
13414   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13415
13416   gtk_drag_dest_set (GTK_WIDGET (tree_view),
13417                      0,
13418                      targets,
13419                      n_targets,
13420                      actions);
13421
13422   di = ensure_info (tree_view);
13423   di->dest_set = TRUE;
13424
13425   unset_reorderable (tree_view);
13426 }
13427
13428 /**
13429  * pspp_sheet_view_unset_rows_drag_source:
13430  * @tree_view: a #PsppSheetView
13431  *
13432  * Undoes the effect of
13433  * pspp_sheet_view_enable_model_drag_source(). Calling this method sets
13434  * #PsppSheetView:reorderable to %FALSE.
13435  **/
13436 void
13437 pspp_sheet_view_unset_rows_drag_source (PsppSheetView *tree_view)
13438 {
13439   TreeViewDragInfo *di;
13440
13441   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13442
13443   di = get_info (tree_view);
13444
13445   if (di)
13446     {
13447       if (di->source_set)
13448         {
13449           gtk_drag_source_unset (GTK_WIDGET (tree_view));
13450           di->source_set = FALSE;
13451         }
13452
13453       if (!di->dest_set && !di->source_set)
13454         remove_info (tree_view);
13455     }
13456   
13457   unset_reorderable (tree_view);
13458 }
13459
13460 /**
13461  * pspp_sheet_view_unset_rows_drag_dest:
13462  * @tree_view: a #PsppSheetView
13463  *
13464  * Undoes the effect of
13465  * pspp_sheet_view_enable_model_drag_dest(). Calling this method sets
13466  * #PsppSheetView:reorderable to %FALSE.
13467  **/
13468 void
13469 pspp_sheet_view_unset_rows_drag_dest (PsppSheetView *tree_view)
13470 {
13471   TreeViewDragInfo *di;
13472
13473   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13474
13475   di = get_info (tree_view);
13476
13477   if (di)
13478     {
13479       if (di->dest_set)
13480         {
13481           gtk_drag_dest_unset (GTK_WIDGET (tree_view));
13482           di->dest_set = FALSE;
13483         }
13484
13485       if (!di->dest_set && !di->source_set)
13486         remove_info (tree_view);
13487     }
13488
13489   unset_reorderable (tree_view);
13490 }
13491
13492 /**
13493  * pspp_sheet_view_set_drag_dest_row:
13494  * @tree_view: a #PsppSheetView
13495  * @path: (allow-none): The path of the row to highlight, or %NULL.
13496  * @pos: Specifies whether to drop before, after or into the row
13497  * 
13498  * Sets the row that is highlighted for feedback.
13499  **/
13500 void
13501 pspp_sheet_view_set_drag_dest_row (PsppSheetView            *tree_view,
13502                                  GtkTreePath            *path,
13503                                  PsppSheetViewDropPosition pos)
13504 {
13505   GtkTreePath *current_dest;
13506
13507   /* Note; this function is exported to allow a custom DND
13508    * implementation, so it can't touch TreeViewDragInfo
13509    */
13510
13511   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13512
13513   current_dest = NULL;
13514
13515   if (tree_view->priv->drag_dest_row)
13516     {
13517       current_dest = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
13518       gtk_tree_row_reference_free (tree_view->priv->drag_dest_row);
13519     }
13520
13521   /* special case a drop on an empty model */
13522   tree_view->priv->empty_view_drop = 0;
13523
13524   if (pos == PSPP_SHEET_VIEW_DROP_BEFORE && path
13525       && gtk_tree_path_get_depth (path) == 1
13526       && gtk_tree_path_get_indices (path)[0] == 0)
13527     {
13528       gint n_children;
13529
13530       n_children = gtk_tree_model_iter_n_children (tree_view->priv->model,
13531                                                    NULL);
13532
13533       if (!n_children)
13534         tree_view->priv->empty_view_drop = 1;
13535     }
13536
13537   tree_view->priv->drag_dest_pos = pos;
13538
13539   if (path)
13540     {
13541       tree_view->priv->drag_dest_row =
13542         gtk_tree_row_reference_new_proxy (G_OBJECT (tree_view), tree_view->priv->model, path);
13543       pspp_sheet_view_queue_draw_path (tree_view, path, NULL);
13544     }
13545   else
13546     tree_view->priv->drag_dest_row = NULL;
13547
13548   if (current_dest)
13549     {
13550       GtkRBTree *tree, *new_tree;
13551       GtkRBNode *node, *new_node;
13552
13553       _pspp_sheet_view_find_node (tree_view, current_dest, &tree, &node);
13554       _pspp_sheet_view_queue_draw_node (tree_view, tree, node, NULL);
13555
13556       if (tree && node)
13557         {
13558           _pspp_rbtree_next_full (tree, node, &new_tree, &new_node);
13559           if (new_tree && new_node)
13560             _pspp_sheet_view_queue_draw_node (tree_view, new_tree, new_node, NULL);
13561
13562           _pspp_rbtree_prev_full (tree, node, &new_tree, &new_node);
13563           if (new_tree && new_node)
13564             _pspp_sheet_view_queue_draw_node (tree_view, new_tree, new_node, NULL);
13565         }
13566       gtk_tree_path_free (current_dest);
13567     }
13568 }
13569
13570 /**
13571  * pspp_sheet_view_get_drag_dest_row:
13572  * @tree_view: a #PsppSheetView
13573  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
13574  * @pos: (allow-none): Return location for the drop position, or %NULL
13575  * 
13576  * Gets information about the row that is highlighted for feedback.
13577  **/
13578 void
13579 pspp_sheet_view_get_drag_dest_row (PsppSheetView              *tree_view,
13580                                  GtkTreePath             **path,
13581                                  PsppSheetViewDropPosition  *pos)
13582 {
13583   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13584
13585   if (path)
13586     {
13587       if (tree_view->priv->drag_dest_row)
13588         *path = gtk_tree_row_reference_get_path (tree_view->priv->drag_dest_row);
13589       else
13590         {
13591           if (tree_view->priv->empty_view_drop)
13592             *path = gtk_tree_path_new_from_indices (0, -1);
13593           else
13594             *path = NULL;
13595         }
13596     }
13597
13598   if (pos)
13599     *pos = tree_view->priv->drag_dest_pos;
13600 }
13601
13602 /**
13603  * pspp_sheet_view_get_dest_row_at_pos:
13604  * @tree_view: a #PsppSheetView
13605  * @drag_x: the position to determine the destination row for
13606  * @drag_y: the position to determine the destination row for
13607  * @path: (allow-none): Return location for the path of the highlighted row, or %NULL.
13608  * @pos: (allow-none): Return location for the drop position, or %NULL
13609  * 
13610  * Determines the destination row for a given position.  @drag_x and
13611  * @drag_y are expected to be in widget coordinates.  This function is only
13612  * meaningful if @tree_view is realized.  Therefore this function will always
13613  * return %FALSE if @tree_view is not realized or does not have a model.
13614  * 
13615  * Return value: whether there is a row at the given position, %TRUE if this
13616  * is indeed the case.
13617  **/
13618 gboolean
13619 pspp_sheet_view_get_dest_row_at_pos (PsppSheetView             *tree_view,
13620                                    gint                     drag_x,
13621                                    gint                     drag_y,
13622                                    GtkTreePath            **path,
13623                                    PsppSheetViewDropPosition *pos)
13624 {
13625   gint cell_y;
13626   gint bin_x, bin_y;
13627   gdouble offset_into_row;
13628   gdouble third;
13629   GdkRectangle cell;
13630   PsppSheetViewColumn *column = NULL;
13631   GtkTreePath *tmp_path = NULL;
13632
13633   /* Note; this function is exported to allow a custom DND
13634    * implementation, so it can't touch TreeViewDragInfo
13635    */
13636
13637   g_return_val_if_fail (tree_view != NULL, FALSE);
13638   g_return_val_if_fail (drag_x >= 0, FALSE);
13639   g_return_val_if_fail (drag_y >= 0, FALSE);
13640
13641   if (path)
13642     *path = NULL;
13643
13644   if (tree_view->priv->bin_window == NULL)
13645     return FALSE;
13646
13647   if (tree_view->priv->tree == NULL)
13648     return FALSE;
13649
13650   /* If in the top third of a row, we drop before that row; if
13651    * in the bottom third, drop after that row; if in the middle,
13652    * and the row has children, drop into the row.
13653    */
13654   pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, drag_x, drag_y,
13655                                                      &bin_x, &bin_y);
13656
13657   if (!pspp_sheet_view_get_path_at_pos (tree_view,
13658                                       bin_x,
13659                                       bin_y,
13660                                       &tmp_path,
13661                                       &column,
13662                                       NULL,
13663                                       &cell_y))
13664     return FALSE;
13665
13666   pspp_sheet_view_get_background_area (tree_view, tmp_path, column,
13667                                      &cell);
13668
13669   offset_into_row = cell_y;
13670
13671   if (path)
13672     *path = tmp_path;
13673   else
13674     gtk_tree_path_free (tmp_path);
13675
13676   tmp_path = NULL;
13677
13678   third = cell.height / 3.0;
13679
13680   if (pos)
13681     {
13682       if (offset_into_row < third)
13683         {
13684           *pos = PSPP_SHEET_VIEW_DROP_BEFORE;
13685         }
13686       else if (offset_into_row < (cell.height / 2.0))
13687         {
13688           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_BEFORE;
13689         }
13690       else if (offset_into_row < third * 2.0)
13691         {
13692           *pos = PSPP_SHEET_VIEW_DROP_INTO_OR_AFTER;
13693         }
13694       else
13695         {
13696           *pos = PSPP_SHEET_VIEW_DROP_AFTER;
13697         }
13698     }
13699
13700   return TRUE;
13701 }
13702
13703
13704
13705 /* KEEP IN SYNC WITH PSPP_SHEET_VIEW_BIN_EXPOSE */
13706 /**
13707  * pspp_sheet_view_create_row_drag_icon:
13708  * @tree_view: a #PsppSheetView
13709  * @path: a #GtkTreePath in @tree_view
13710  *
13711  * Creates a #GdkPixmap representation of the row at @path.  
13712  * This image is used for a drag icon.
13713  *
13714  * Return value: a newly-allocated pixmap of the drag icon.
13715  **/
13716 GdkPixmap *
13717 pspp_sheet_view_create_row_drag_icon (PsppSheetView  *tree_view,
13718                                     GtkTreePath  *path)
13719 {
13720   GtkTreeIter   iter;
13721   GtkRBTree    *tree;
13722   GtkRBNode    *node;
13723   gint cell_offset;
13724   GList *list;
13725   GdkRectangle background_area;
13726   GdkRectangle expose_area;
13727   GtkWidget *widget;
13728   gint depth;
13729   /* start drawing inside the black outline */
13730   gint x = 1, y = 1;
13731   GdkDrawable *drawable;
13732   gint bin_window_width;
13733   gboolean is_separator = FALSE;
13734   gboolean rtl;
13735
13736   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
13737   g_return_val_if_fail (path != NULL, NULL);
13738
13739   widget = GTK_WIDGET (tree_view);
13740
13741   if (!gtk_widget_get_realized (widget))
13742     return NULL;
13743
13744   depth = gtk_tree_path_get_depth (path);
13745
13746   _pspp_sheet_view_find_node (tree_view,
13747                             path,
13748                             &tree,
13749                             &node);
13750
13751   if (tree == NULL)
13752     return NULL;
13753
13754   if (!gtk_tree_model_get_iter (tree_view->priv->model,
13755                                 &iter,
13756                                 path))
13757     return NULL;
13758   
13759   is_separator = row_is_separator (tree_view, &iter, NULL);
13760
13761   cell_offset = x;
13762
13763   background_area.y = y;
13764   background_area.height = ROW_HEIGHT (tree_view, BACKGROUND_HEIGHT (node));
13765
13766   gdk_drawable_get_size (tree_view->priv->bin_window,
13767                          &bin_window_width, NULL);
13768
13769   drawable = gdk_pixmap_new (tree_view->priv->bin_window,
13770                              bin_window_width + 2,
13771                              background_area.height + 2,
13772                              -1);
13773
13774   expose_area.x = 0;
13775   expose_area.y = 0;
13776   expose_area.width = bin_window_width + 2;
13777   expose_area.height = background_area.height + 2;
13778
13779   gdk_draw_rectangle (drawable,
13780                       widget->style->base_gc [gtk_widget_get_state (widget)],
13781                       TRUE,
13782                       0, 0,
13783                       bin_window_width + 2,
13784                       background_area.height + 2);
13785
13786   rtl = gtk_widget_get_direction (GTK_WIDGET (tree_view)) == GTK_TEXT_DIR_RTL;
13787
13788   for (list = (rtl ? g_list_last (tree_view->priv->columns) : g_list_first (tree_view->priv->columns));
13789       list;
13790       list = (rtl ? list->prev : list->next))
13791     {
13792       PsppSheetViewColumn *column = list->data;
13793       GdkRectangle cell_area;
13794       gint vertical_separator;
13795
13796       if (!column->visible)
13797         continue;
13798
13799       pspp_sheet_view_column_cell_set_cell_data (column, tree_view->priv->model, &iter,
13800                                                PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_PARENT),
13801                                                node->children?TRUE:FALSE);
13802
13803       background_area.x = cell_offset;
13804       background_area.width = column->width;
13805
13806       gtk_widget_style_get (widget,
13807                             "vertical-separator", &vertical_separator,
13808                             NULL);
13809
13810       cell_area = background_area;
13811
13812       cell_area.y += vertical_separator / 2;
13813       cell_area.height -= vertical_separator;
13814
13815       if (pspp_sheet_view_is_expander_column (tree_view, column))
13816         {
13817           if (!rtl)
13818             cell_area.x += (depth - 1) * tree_view->priv->level_indentation;
13819           cell_area.width -= (depth - 1) * tree_view->priv->level_indentation;
13820
13821           if (TREE_VIEW_DRAW_EXPANDERS(tree_view))
13822             {
13823               if (!rtl)
13824                 cell_area.x += depth * tree_view->priv->expander_size;
13825               cell_area.width -= depth * tree_view->priv->expander_size;
13826             }
13827         }
13828
13829       if (pspp_sheet_view_column_cell_is_visible (column))
13830         {
13831           if (is_separator)
13832             gtk_paint_hline (widget->style,
13833                              drawable,
13834                              GTK_STATE_NORMAL,
13835                              &cell_area,
13836                              widget,
13837                              NULL,
13838                              cell_area.x,
13839                              cell_area.x + cell_area.width,
13840                              cell_area.y + cell_area.height / 2);
13841           else
13842             _pspp_sheet_view_column_cell_render (column,
13843                                                drawable,
13844                                                &background_area,
13845                                                &cell_area,
13846                                                &expose_area,
13847                                                0);
13848         }
13849       cell_offset += column->width;
13850     }
13851
13852   gdk_draw_rectangle (drawable,
13853                       widget->style->black_gc,
13854                       FALSE,
13855                       0, 0,
13856                       bin_window_width + 1,
13857                       background_area.height + 1);
13858
13859   return drawable;
13860 }
13861
13862
13863 /**
13864  * pspp_sheet_view_set_destroy_count_func:
13865  * @tree_view: A #PsppSheetView
13866  * @func: (allow-none): Function to be called when a view row is destroyed, or %NULL
13867  * @data: (allow-none): User data to be passed to @func, or %NULL
13868  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
13869  *
13870  * This function should almost never be used.  It is meant for private use by
13871  * ATK for determining the number of visible children that are removed when the
13872  * user collapses a row, or a row is deleted.
13873  **/
13874 void
13875 pspp_sheet_view_set_destroy_count_func (PsppSheetView             *tree_view,
13876                                       PsppSheetDestroyCountFunc  func,
13877                                       gpointer                 data,
13878                                       GDestroyNotify           destroy)
13879 {
13880   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13881
13882   if (tree_view->priv->destroy_count_destroy)
13883     tree_view->priv->destroy_count_destroy (tree_view->priv->destroy_count_data);
13884
13885   tree_view->priv->destroy_count_func = func;
13886   tree_view->priv->destroy_count_data = data;
13887   tree_view->priv->destroy_count_destroy = destroy;
13888 }
13889
13890
13891 /*
13892  * Interactive search
13893  */
13894
13895 /**
13896  * pspp_sheet_view_set_enable_search:
13897  * @tree_view: A #PsppSheetView
13898  * @enable_search: %TRUE, if the user can search interactively
13899  *
13900  * If @enable_search is set, then the user can type in text to search through
13901  * the tree interactively (this is sometimes called "typeahead find").
13902  * 
13903  * Note that even if this is %FALSE, the user can still initiate a search 
13904  * using the "start-interactive-search" key binding.
13905  */
13906 void
13907 pspp_sheet_view_set_enable_search (PsppSheetView *tree_view,
13908                                  gboolean     enable_search)
13909 {
13910   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13911
13912   enable_search = !!enable_search;
13913   
13914   if (tree_view->priv->enable_search != enable_search)
13915     {
13916        tree_view->priv->enable_search = enable_search;
13917        g_object_notify (G_OBJECT (tree_view), "enable-search");
13918     }
13919 }
13920
13921 /**
13922  * pspp_sheet_view_get_enable_search:
13923  * @tree_view: A #PsppSheetView
13924  *
13925  * Returns whether or not the tree allows to start interactive searching 
13926  * by typing in text.
13927  *
13928  * Return value: whether or not to let the user search interactively
13929  */
13930 gboolean
13931 pspp_sheet_view_get_enable_search (PsppSheetView *tree_view)
13932 {
13933   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
13934
13935   return tree_view->priv->enable_search;
13936 }
13937
13938
13939 /**
13940  * pspp_sheet_view_get_search_column:
13941  * @tree_view: A #PsppSheetView
13942  *
13943  * Gets the column searched on by the interactive search code.
13944  *
13945  * Return value: the column the interactive search code searches in.
13946  */
13947 gint
13948 pspp_sheet_view_get_search_column (PsppSheetView *tree_view)
13949 {
13950   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), -1);
13951
13952   return (tree_view->priv->search_column);
13953 }
13954
13955 /**
13956  * pspp_sheet_view_set_search_column:
13957  * @tree_view: A #PsppSheetView
13958  * @column: the column of the model to search in, or -1 to disable searching
13959  *
13960  * Sets @column as the column where the interactive search code should
13961  * search in for the current model. 
13962  * 
13963  * If the search column is set, users can use the "start-interactive-search"
13964  * key binding to bring up search popup. The enable-search property controls
13965  * whether simply typing text will also start an interactive search.
13966  *
13967  * Note that @column refers to a column of the current model. The search 
13968  * column is reset to -1 when the model is changed.
13969  */
13970 void
13971 pspp_sheet_view_set_search_column (PsppSheetView *tree_view,
13972                                  gint         column)
13973 {
13974   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
13975   g_return_if_fail (column >= -1);
13976
13977   if (tree_view->priv->search_column == column)
13978     return;
13979
13980   tree_view->priv->search_column = column;
13981   g_object_notify (G_OBJECT (tree_view), "search-column");
13982 }
13983
13984 /**
13985  * pspp_sheet_view_get_search_equal_func:
13986  * @tree_view: A #PsppSheetView
13987  *
13988  * Returns the compare function currently in use.
13989  *
13990  * Return value: the currently used compare function for the search code.
13991  */
13992
13993 PsppSheetViewSearchEqualFunc
13994 pspp_sheet_view_get_search_equal_func (PsppSheetView *tree_view)
13995 {
13996   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
13997
13998   return tree_view->priv->search_equal_func;
13999 }
14000
14001 /**
14002  * pspp_sheet_view_set_search_equal_func:
14003  * @tree_view: A #PsppSheetView
14004  * @search_equal_func: the compare function to use during the search
14005  * @search_user_data: (allow-none): user data to pass to @search_equal_func, or %NULL
14006  * @search_destroy: (allow-none): Destroy notifier for @search_user_data, or %NULL
14007  *
14008  * Sets the compare function for the interactive search capabilities; note
14009  * that somewhat like strcmp() returning 0 for equality
14010  * #PsppSheetViewSearchEqualFunc returns %FALSE on matches.
14011  **/
14012 void
14013 pspp_sheet_view_set_search_equal_func (PsppSheetView                *tree_view,
14014                                      PsppSheetViewSearchEqualFunc  search_equal_func,
14015                                      gpointer                    search_user_data,
14016                                      GDestroyNotify              search_destroy)
14017 {
14018   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
14019   g_return_if_fail (search_equal_func != NULL);
14020
14021   if (tree_view->priv->search_destroy)
14022     tree_view->priv->search_destroy (tree_view->priv->search_user_data);
14023
14024   tree_view->priv->search_equal_func = search_equal_func;
14025   tree_view->priv->search_user_data = search_user_data;
14026   tree_view->priv->search_destroy = search_destroy;
14027   if (tree_view->priv->search_equal_func == NULL)
14028     tree_view->priv->search_equal_func = pspp_sheet_view_search_equal_func;
14029 }
14030
14031 /**
14032  * pspp_sheet_view_get_search_entry:
14033  * @tree_view: A #PsppSheetView
14034  *
14035  * Returns the #GtkEntry which is currently in use as interactive search
14036  * entry for @tree_view.  In case the built-in entry is being used, %NULL
14037  * will be returned.
14038  *
14039  * Return value: the entry currently in use as search entry.
14040  *
14041  * Since: 2.10
14042  */
14043 GtkEntry *
14044 pspp_sheet_view_get_search_entry (PsppSheetView *tree_view)
14045 {
14046   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
14047
14048   if (tree_view->priv->search_custom_entry_set)
14049     return GTK_ENTRY (tree_view->priv->search_entry);
14050
14051   return NULL;
14052 }
14053
14054 /**
14055  * pspp_sheet_view_set_search_entry:
14056  * @tree_view: A #PsppSheetView
14057  * @entry: (allow-none): the entry the interactive search code of @tree_view should use or %NULL
14058  *
14059  * Sets the entry which the interactive search code will use for this
14060  * @tree_view.  This is useful when you want to provide a search entry
14061  * in our interface at all time at a fixed position.  Passing %NULL for
14062  * @entry will make the interactive search code use the built-in popup
14063  * entry again.
14064  *
14065  * Since: 2.10
14066  */
14067 void
14068 pspp_sheet_view_set_search_entry (PsppSheetView *tree_view,
14069                                 GtkEntry    *entry)
14070 {
14071   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
14072   g_return_if_fail (entry == NULL || GTK_IS_ENTRY (entry));
14073
14074   if (tree_view->priv->search_custom_entry_set)
14075     {
14076       if (tree_view->priv->search_entry_changed_id)
14077         {
14078           g_signal_handler_disconnect (tree_view->priv->search_entry,
14079                                        tree_view->priv->search_entry_changed_id);
14080           tree_view->priv->search_entry_changed_id = 0;
14081         }
14082       g_signal_handlers_disconnect_by_func (tree_view->priv->search_entry,
14083                                             G_CALLBACK (pspp_sheet_view_search_key_press_event),
14084                                             tree_view);
14085
14086       g_object_unref (tree_view->priv->search_entry);
14087     }
14088   else if (tree_view->priv->search_window)
14089     {
14090       gtk_widget_destroy (tree_view->priv->search_window);
14091
14092       tree_view->priv->search_window = NULL;
14093     }
14094
14095   if (entry)
14096     {
14097       tree_view->priv->search_entry = g_object_ref (entry);
14098       tree_view->priv->search_custom_entry_set = TRUE;
14099
14100       if (tree_view->priv->search_entry_changed_id == 0)
14101         {
14102           tree_view->priv->search_entry_changed_id =
14103             g_signal_connect (tree_view->priv->search_entry, "changed",
14104                               G_CALLBACK (pspp_sheet_view_search_init),
14105                               tree_view);
14106         }
14107       
14108         g_signal_connect (tree_view->priv->search_entry, "key-press-event",
14109                           G_CALLBACK (pspp_sheet_view_search_key_press_event),
14110                           tree_view);
14111
14112         pspp_sheet_view_search_init (tree_view->priv->search_entry, tree_view);
14113     }
14114   else
14115     {
14116       tree_view->priv->search_entry = NULL;
14117       tree_view->priv->search_custom_entry_set = FALSE;
14118     }
14119 }
14120
14121 /**
14122  * pspp_sheet_view_set_search_position_func:
14123  * @tree_view: A #PsppSheetView
14124  * @func: (allow-none): the function to use to position the search dialog, or %NULL
14125  *    to use the default search position function
14126  * @data: (allow-none): user data to pass to @func, or %NULL
14127  * @destroy: (allow-none): Destroy notifier for @data, or %NULL
14128  *
14129  * Sets the function to use when positioning the search dialog.
14130  *
14131  * Since: 2.10
14132  **/
14133 void
14134 pspp_sheet_view_set_search_position_func (PsppSheetView                   *tree_view,
14135                                         PsppSheetViewSearchPositionFunc  func,
14136                                         gpointer                       user_data,
14137                                         GDestroyNotify                 destroy)
14138 {
14139   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
14140
14141   if (tree_view->priv->search_position_destroy)
14142     tree_view->priv->search_position_destroy (tree_view->priv->search_position_user_data);
14143
14144   tree_view->priv->search_position_func = func;
14145   tree_view->priv->search_position_user_data = user_data;
14146   tree_view->priv->search_position_destroy = destroy;
14147   if (tree_view->priv->search_position_func == NULL)
14148     tree_view->priv->search_position_func = pspp_sheet_view_search_position_func;
14149 }
14150
14151 /**
14152  * pspp_sheet_view_get_search_position_func:
14153  * @tree_view: A #PsppSheetView
14154  *
14155  * Returns the positioning function currently in use.
14156  *
14157  * Return value: the currently used function for positioning the search dialog.
14158  *
14159  * Since: 2.10
14160  */
14161 PsppSheetViewSearchPositionFunc
14162 pspp_sheet_view_get_search_position_func (PsppSheetView *tree_view)
14163 {
14164   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
14165
14166   return tree_view->priv->search_position_func;
14167 }
14168
14169
14170 static void
14171 pspp_sheet_view_search_dialog_hide (GtkWidget   *search_dialog,
14172                                   PsppSheetView *tree_view)
14173 {
14174   if (tree_view->priv->disable_popdown)
14175     return;
14176
14177   if (tree_view->priv->search_entry_changed_id)
14178     {
14179       g_signal_handler_disconnect (tree_view->priv->search_entry,
14180                                    tree_view->priv->search_entry_changed_id);
14181       tree_view->priv->search_entry_changed_id = 0;
14182     }
14183   if (tree_view->priv->typeselect_flush_timeout)
14184     {
14185       g_source_remove (tree_view->priv->typeselect_flush_timeout);
14186       tree_view->priv->typeselect_flush_timeout = 0;
14187     }
14188         
14189   if (gtk_widget_get_visible (search_dialog))
14190     {
14191       /* send focus-in event */
14192       send_focus_change (GTK_WIDGET (tree_view->priv->search_entry), FALSE);
14193       gtk_widget_hide (search_dialog);
14194       gtk_entry_set_text (GTK_ENTRY (tree_view->priv->search_entry), "");
14195       send_focus_change (GTK_WIDGET (tree_view), TRUE);
14196     }
14197 }
14198
14199 static void
14200 pspp_sheet_view_search_position_func (PsppSheetView *tree_view,
14201                                     GtkWidget   *search_dialog,
14202                                     gpointer     user_data)
14203 {
14204   gint x, y;
14205   gint tree_x, tree_y;
14206   gint tree_width, tree_height;
14207   GdkWindow *tree_window = GTK_WIDGET (tree_view)->window;
14208   GdkScreen *screen = gdk_drawable_get_screen (tree_window);
14209   GtkRequisition requisition;
14210   gint monitor_num;
14211   GdkRectangle monitor;
14212
14213   monitor_num = gdk_screen_get_monitor_at_window (screen, tree_window);
14214   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
14215
14216   gtk_widget_realize (search_dialog);
14217
14218   gdk_window_get_origin (tree_window, &tree_x, &tree_y);
14219   gdk_drawable_get_size (tree_window,
14220                          &tree_width,
14221                          &tree_height);
14222   gtk_widget_size_request (search_dialog, &requisition);
14223
14224   if (tree_x + tree_width > gdk_screen_get_width (screen))
14225     x = gdk_screen_get_width (screen) - requisition.width;
14226   else if (tree_x + tree_width - requisition.width < 0)
14227     x = 0;
14228   else
14229     x = tree_x + tree_width - requisition.width;
14230
14231   if (tree_y + tree_height + requisition.height > gdk_screen_get_height (screen))
14232     y = gdk_screen_get_height (screen) - requisition.height;
14233   else if (tree_y + tree_height < 0) /* isn't really possible ... */
14234     y = 0;
14235   else
14236     y = tree_y + tree_height;
14237
14238   gtk_window_move (GTK_WINDOW (search_dialog), x, y);
14239 }
14240
14241 static void
14242 pspp_sheet_view_search_disable_popdown (GtkEntry *entry,
14243                                       GtkMenu  *menu,
14244                                       gpointer  data)
14245 {
14246   PsppSheetView *tree_view = (PsppSheetView *)data;
14247
14248   tree_view->priv->disable_popdown = 1;
14249   g_signal_connect (menu, "hide",
14250                     G_CALLBACK (pspp_sheet_view_search_enable_popdown), data);
14251 }
14252
14253 /* Because we're visible but offscreen, we just set a flag in the preedit
14254  * callback.
14255  */
14256 static void
14257 pspp_sheet_view_search_preedit_changed (GtkIMContext *im_context,
14258                                       PsppSheetView  *tree_view)
14259 {
14260   tree_view->priv->imcontext_changed = 1;
14261   if (tree_view->priv->typeselect_flush_timeout)
14262     {
14263       g_source_remove (tree_view->priv->typeselect_flush_timeout);
14264       tree_view->priv->typeselect_flush_timeout =
14265         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
14266                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
14267                        tree_view);
14268     }
14269
14270 }
14271
14272 static void
14273 pspp_sheet_view_search_activate (GtkEntry    *entry,
14274                                PsppSheetView *tree_view)
14275 {
14276   GtkTreePath *path;
14277   GtkRBNode *node;
14278   GtkRBTree *tree;
14279
14280   pspp_sheet_view_search_dialog_hide (tree_view->priv->search_window,
14281                                     tree_view);
14282
14283   /* If we have a row selected and it's the cursor row, we activate
14284    * the row XXX */
14285   if (gtk_tree_row_reference_valid (tree_view->priv->cursor))
14286     {
14287       path = gtk_tree_row_reference_get_path (tree_view->priv->cursor);
14288       
14289       _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
14290       
14291       if (node && PSPP_RBNODE_FLAG_SET (node, PSPP_RBNODE_IS_SELECTED))
14292         pspp_sheet_view_row_activated (tree_view, path, tree_view->priv->focus_column);
14293       
14294       gtk_tree_path_free (path);
14295     }
14296 }
14297
14298 static gboolean
14299 pspp_sheet_view_real_search_enable_popdown (gpointer data)
14300 {
14301   PsppSheetView *tree_view = (PsppSheetView *)data;
14302
14303   tree_view->priv->disable_popdown = 0;
14304
14305   return FALSE;
14306 }
14307
14308 static void
14309 pspp_sheet_view_search_enable_popdown (GtkWidget *widget,
14310                                      gpointer   data)
14311 {
14312   gdk_threads_add_timeout_full (G_PRIORITY_HIGH, 200, pspp_sheet_view_real_search_enable_popdown, g_object_ref (data), g_object_unref);
14313 }
14314
14315 static gboolean
14316 pspp_sheet_view_search_delete_event (GtkWidget *widget,
14317                                    GdkEventAny *event,
14318                                    PsppSheetView *tree_view)
14319 {
14320   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14321
14322   pspp_sheet_view_search_dialog_hide (widget, tree_view);
14323
14324   return TRUE;
14325 }
14326
14327 static gboolean
14328 pspp_sheet_view_search_button_press_event (GtkWidget *widget,
14329                                          GdkEventButton *event,
14330                                          PsppSheetView *tree_view)
14331 {
14332   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14333
14334   pspp_sheet_view_search_dialog_hide (widget, tree_view);
14335
14336   if (event->window == tree_view->priv->bin_window)
14337     pspp_sheet_view_button_press (GTK_WIDGET (tree_view), event);
14338
14339   return TRUE;
14340 }
14341
14342 static gboolean
14343 pspp_sheet_view_search_scroll_event (GtkWidget *widget,
14344                                    GdkEventScroll *event,
14345                                    PsppSheetView *tree_view)
14346 {
14347   gboolean retval = FALSE;
14348
14349   if (event->direction == GDK_SCROLL_UP)
14350     {
14351       pspp_sheet_view_search_move (widget, tree_view, TRUE);
14352       retval = TRUE;
14353     }
14354   else if (event->direction == GDK_SCROLL_DOWN)
14355     {
14356       pspp_sheet_view_search_move (widget, tree_view, FALSE);
14357       retval = TRUE;
14358     }
14359
14360   /* renew the flush timeout */
14361   if (retval && tree_view->priv->typeselect_flush_timeout
14362       && !tree_view->priv->search_custom_entry_set)
14363     {
14364       g_source_remove (tree_view->priv->typeselect_flush_timeout);
14365       tree_view->priv->typeselect_flush_timeout =
14366         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
14367                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
14368                        tree_view);
14369     }
14370
14371   return retval;
14372 }
14373
14374 static gboolean
14375 pspp_sheet_view_search_key_press_event (GtkWidget *widget,
14376                                       GdkEventKey *event,
14377                                       PsppSheetView *tree_view)
14378 {
14379   gboolean retval = FALSE;
14380
14381   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14382   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
14383
14384   /* close window and cancel the search */
14385   if (!tree_view->priv->search_custom_entry_set
14386       && (event->keyval == GDK_Escape ||
14387           event->keyval == GDK_Tab ||
14388             event->keyval == GDK_KP_Tab ||
14389             event->keyval == GDK_ISO_Left_Tab))
14390     {
14391       pspp_sheet_view_search_dialog_hide (widget, tree_view);
14392       return TRUE;
14393     }
14394
14395   /* select previous matching iter */
14396   if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
14397     {
14398       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
14399         gtk_widget_error_bell (widget);
14400
14401       retval = TRUE;
14402     }
14403
14404   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK))
14405       && (event->keyval == GDK_g || event->keyval == GDK_G))
14406     {
14407       if (!pspp_sheet_view_search_move (widget, tree_view, TRUE))
14408         gtk_widget_error_bell (widget);
14409
14410       retval = TRUE;
14411     }
14412
14413   /* select next matching iter */
14414   if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
14415     {
14416       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
14417         gtk_widget_error_bell (widget);
14418
14419       retval = TRUE;
14420     }
14421
14422   if (((event->state & (GTK_DEFAULT_ACCEL_MOD_MASK | GDK_SHIFT_MASK)) == GTK_DEFAULT_ACCEL_MOD_MASK)
14423       && (event->keyval == GDK_g || event->keyval == GDK_G))
14424     {
14425       if (!pspp_sheet_view_search_move (widget, tree_view, FALSE))
14426         gtk_widget_error_bell (widget);
14427
14428       retval = TRUE;
14429     }
14430
14431   /* renew the flush timeout */
14432   if (retval && tree_view->priv->typeselect_flush_timeout
14433       && !tree_view->priv->search_custom_entry_set)
14434     {
14435       g_source_remove (tree_view->priv->typeselect_flush_timeout);
14436       tree_view->priv->typeselect_flush_timeout =
14437         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
14438                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
14439                        tree_view);
14440     }
14441
14442   return retval;
14443 }
14444
14445 /*  this function returns FALSE if there is a search string but
14446  *  nothing was found, and TRUE otherwise.
14447  */
14448 static gboolean
14449 pspp_sheet_view_search_move (GtkWidget   *window,
14450                            PsppSheetView *tree_view,
14451                            gboolean     up)
14452 {
14453   gboolean ret;
14454   gint len;
14455   gint count = 0;
14456   const gchar *text;
14457   GtkTreeIter iter;
14458   GtkTreeModel *model;
14459   PsppSheetSelection *selection;
14460
14461   text = gtk_entry_get_text (GTK_ENTRY (tree_view->priv->search_entry));
14462
14463   g_return_val_if_fail (text != NULL, FALSE);
14464
14465   len = strlen (text);
14466
14467   if (up && tree_view->priv->selected_iter == 1)
14468     return strlen (text) < 1;
14469
14470   len = strlen (text);
14471
14472   if (len < 1)
14473     return TRUE;
14474
14475   model = pspp_sheet_view_get_model (tree_view);
14476   selection = pspp_sheet_view_get_selection (tree_view);
14477
14478   /* search */
14479   pspp_sheet_selection_unselect_all (selection);
14480   if (!gtk_tree_model_get_iter_first (model, &iter))
14481     return TRUE;
14482
14483   ret = pspp_sheet_view_search_iter (model, selection, &iter, text,
14484                                    &count, up?((tree_view->priv->selected_iter) - 1):((tree_view->priv->selected_iter + 1)));
14485
14486   if (ret)
14487     {
14488       /* found */
14489       tree_view->priv->selected_iter += up?(-1):(1);
14490       return TRUE;
14491     }
14492   else
14493     {
14494       /* return to old iter */
14495       count = 0;
14496       gtk_tree_model_get_iter_first (model, &iter);
14497       pspp_sheet_view_search_iter (model, selection,
14498                                  &iter, text,
14499                                  &count, tree_view->priv->selected_iter);
14500       return FALSE;
14501     }
14502 }
14503
14504 static gboolean
14505 pspp_sheet_view_search_equal_func (GtkTreeModel *model,
14506                                  gint          column,
14507                                  const gchar  *key,
14508                                  GtkTreeIter  *iter,
14509                                  gpointer      search_data)
14510 {
14511   gboolean retval = TRUE;
14512   const gchar *str;
14513   gchar *normalized_string;
14514   gchar *normalized_key;
14515   gchar *case_normalized_string = NULL;
14516   gchar *case_normalized_key = NULL;
14517   GValue value = {0,};
14518   GValue transformed = {0,};
14519
14520   gtk_tree_model_get_value (model, iter, column, &value);
14521
14522   g_value_init (&transformed, G_TYPE_STRING);
14523
14524   if (!g_value_transform (&value, &transformed))
14525     {
14526       g_value_unset (&value);
14527       return TRUE;
14528     }
14529
14530   g_value_unset (&value);
14531
14532   str = g_value_get_string (&transformed);
14533   if (!str)
14534     {
14535       g_value_unset (&transformed);
14536       return TRUE;
14537     }
14538
14539   normalized_string = g_utf8_normalize (str, -1, G_NORMALIZE_ALL);
14540   normalized_key = g_utf8_normalize (key, -1, G_NORMALIZE_ALL);
14541
14542   if (normalized_string && normalized_key)
14543     {
14544       case_normalized_string = g_utf8_casefold (normalized_string, -1);
14545       case_normalized_key = g_utf8_casefold (normalized_key, -1);
14546
14547       if (strncmp (case_normalized_key, case_normalized_string, strlen (case_normalized_key)) == 0)
14548         retval = FALSE;
14549     }
14550
14551   g_value_unset (&transformed);
14552   g_free (normalized_key);
14553   g_free (normalized_string);
14554   g_free (case_normalized_key);
14555   g_free (case_normalized_string);
14556
14557   return retval;
14558 }
14559
14560 static gboolean
14561 pspp_sheet_view_search_iter (GtkTreeModel     *model,
14562                            PsppSheetSelection *selection,
14563                            GtkTreeIter      *iter,
14564                            const gchar      *text,
14565                            gint             *count,
14566                            gint              n)
14567 {
14568   GtkRBTree *tree = NULL;
14569   GtkRBNode *node = NULL;
14570   GtkTreePath *path;
14571
14572   PsppSheetView *tree_view = pspp_sheet_selection_get_tree_view (selection);
14573
14574   path = gtk_tree_model_get_path (model, iter);
14575   _pspp_sheet_view_find_node (tree_view, path, &tree, &node);
14576
14577   do
14578     {
14579       if (! tree_view->priv->search_equal_func (model, tree_view->priv->search_column, text, iter, tree_view->priv->search_user_data))
14580         {
14581           (*count)++;
14582           if (*count == n)
14583             {
14584               pspp_sheet_view_scroll_to_cell (tree_view, path, NULL,
14585                                             TRUE, 0.5, 0.0);
14586               pspp_sheet_selection_select_iter (selection, iter);
14587               pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE);
14588
14589               if (path)
14590                 gtk_tree_path_free (path);
14591
14592               return TRUE;
14593             }
14594         }
14595
14596       if (node->children)
14597         {
14598           gboolean has_child;
14599           GtkTreeIter tmp;
14600
14601           tree = node->children;
14602           node = tree->root;
14603
14604           while (node->left != tree->nil)
14605             node = node->left;
14606
14607           tmp = *iter;
14608           has_child = gtk_tree_model_iter_children (model, iter, &tmp);
14609           gtk_tree_path_down (path);
14610
14611           /* sanity check */
14612           TREE_VIEW_INTERNAL_ASSERT (has_child, FALSE);
14613         }
14614       else
14615         {
14616           gboolean done = FALSE;
14617
14618           do
14619             {
14620               node = _pspp_rbtree_next (tree, node);
14621
14622               if (node)
14623                 {
14624                   gboolean has_next;
14625
14626                   has_next = gtk_tree_model_iter_next (model, iter);
14627
14628                   done = TRUE;
14629                   gtk_tree_path_next (path);
14630
14631                   /* sanity check */
14632                   TREE_VIEW_INTERNAL_ASSERT (has_next, FALSE);
14633                 }
14634               else
14635                 {
14636                   gboolean has_parent;
14637                   GtkTreeIter tmp_iter = *iter;
14638
14639                   node = tree->parent_node;
14640                   tree = tree->parent_tree;
14641
14642                   if (!tree)
14643                     {
14644                       if (path)
14645                         gtk_tree_path_free (path);
14646
14647                       /* we've run out of tree, done with this func */
14648                       return FALSE;
14649                     }
14650
14651                   has_parent = gtk_tree_model_iter_parent (model,
14652                                                            iter,
14653                                                            &tmp_iter);
14654                   gtk_tree_path_up (path);
14655
14656                   /* sanity check */
14657                   TREE_VIEW_INTERNAL_ASSERT (has_parent, FALSE);
14658                 }
14659             }
14660           while (!done);
14661         }
14662     }
14663   while (1);
14664
14665   return FALSE;
14666 }
14667
14668 static void
14669 pspp_sheet_view_search_init (GtkWidget   *entry,
14670                            PsppSheetView *tree_view)
14671 {
14672   gint ret;
14673   gint count = 0;
14674   const gchar *text;
14675   GtkTreeIter iter;
14676   GtkTreeModel *model;
14677   PsppSheetSelection *selection;
14678
14679   g_return_if_fail (GTK_IS_ENTRY (entry));
14680   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
14681
14682   text = gtk_entry_get_text (GTK_ENTRY (entry));
14683
14684   model = pspp_sheet_view_get_model (tree_view);
14685   selection = pspp_sheet_view_get_selection (tree_view);
14686
14687   /* search */
14688   pspp_sheet_selection_unselect_all (selection);
14689   if (tree_view->priv->typeselect_flush_timeout
14690       && !tree_view->priv->search_custom_entry_set)
14691     {
14692       g_source_remove (tree_view->priv->typeselect_flush_timeout);
14693       tree_view->priv->typeselect_flush_timeout =
14694         gdk_threads_add_timeout (PSPP_SHEET_VIEW_SEARCH_DIALOG_TIMEOUT,
14695                        (GSourceFunc) pspp_sheet_view_search_entry_flush_timeout,
14696                        tree_view);
14697     }
14698
14699   if (*text == '\0')
14700     return;
14701
14702   if (!gtk_tree_model_get_iter_first (model, &iter))
14703     return;
14704
14705   ret = pspp_sheet_view_search_iter (model, selection,
14706                                    &iter, text,
14707                                    &count, 1);
14708
14709   if (ret)
14710     tree_view->priv->selected_iter = 1;
14711 }
14712
14713 static void
14714 pspp_sheet_view_remove_widget (GtkCellEditable *cell_editable,
14715                              PsppSheetView     *tree_view)
14716 {
14717   if (tree_view->priv->edited_column == NULL)
14718     return;
14719
14720   _pspp_sheet_view_column_stop_editing (tree_view->priv->edited_column);
14721   tree_view->priv->edited_column = NULL;
14722
14723   if (gtk_widget_has_focus (GTK_WIDGET (cell_editable)))
14724     gtk_widget_grab_focus (GTK_WIDGET (tree_view));
14725
14726   g_signal_handlers_disconnect_by_func (cell_editable,
14727                                         pspp_sheet_view_remove_widget,
14728                                         tree_view);
14729
14730   gtk_container_remove (GTK_CONTAINER (tree_view),
14731                         GTK_WIDGET (cell_editable));  
14732
14733   /* FIXME should only redraw a single node */
14734   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
14735 }
14736
14737 static gboolean
14738 pspp_sheet_view_start_editing (PsppSheetView *tree_view,
14739                              GtkTreePath *cursor_path)
14740 {
14741   GtkTreeIter iter;
14742   GdkRectangle background_area;
14743   GdkRectangle cell_area;
14744   GtkCellEditable *editable_widget = NULL;
14745   gchar *path_string;
14746   guint flags = 0; /* can be 0, as the flags are primarily for rendering */
14747   gint retval = FALSE;
14748   GtkRBTree *cursor_tree;
14749   GtkRBNode *cursor_node;
14750
14751   g_assert (tree_view->priv->focus_column);
14752
14753   if (!gtk_widget_get_realized (GTK_WIDGET (tree_view)))
14754     return FALSE;
14755
14756   if (_pspp_sheet_view_find_node (tree_view, cursor_path, &cursor_tree, &cursor_node) ||
14757       cursor_node == NULL)
14758     return FALSE;
14759
14760   path_string = gtk_tree_path_to_string (cursor_path);
14761   gtk_tree_model_get_iter (tree_view->priv->model, &iter, cursor_path);
14762
14763   validate_row (tree_view, cursor_tree, cursor_node, &iter, cursor_path);
14764
14765   pspp_sheet_view_column_cell_set_cell_data (tree_view->priv->focus_column,
14766                                            tree_view->priv->model,
14767                                            &iter,
14768                                            PSPP_RBNODE_FLAG_SET (cursor_node, PSPP_RBNODE_IS_PARENT),
14769                                            cursor_node->children?TRUE:FALSE);
14770   pspp_sheet_view_get_background_area (tree_view,
14771                                      cursor_path,
14772                                      tree_view->priv->focus_column,
14773                                      &background_area);
14774   pspp_sheet_view_get_cell_area (tree_view,
14775                                cursor_path,
14776                                tree_view->priv->focus_column,
14777                                &cell_area);
14778
14779   if (_pspp_sheet_view_column_cell_event (tree_view->priv->focus_column,
14780                                         &editable_widget,
14781                                         NULL,
14782                                         path_string,
14783                                         &background_area,
14784                                         &cell_area,
14785                                         flags))
14786     {
14787       retval = TRUE;
14788       if (editable_widget != NULL)
14789         {
14790           gint left, right;
14791           GdkRectangle area;
14792           GtkCellRenderer *cell;
14793
14794           area = cell_area;
14795           cell = _pspp_sheet_view_column_get_edited_cell (tree_view->priv->focus_column);
14796
14797           _pspp_sheet_view_column_get_neighbor_sizes (tree_view->priv->focus_column, cell, &left, &right);
14798
14799           area.x += left;
14800           area.width -= right + left;
14801
14802           pspp_sheet_view_real_start_editing (tree_view,
14803                                             tree_view->priv->focus_column,
14804                                             cursor_path,
14805                                             editable_widget,
14806                                             &area,
14807                                             NULL,
14808                                             flags);
14809         }
14810
14811     }
14812   g_free (path_string);
14813   return retval;
14814 }
14815
14816 static void
14817 pspp_sheet_view_real_start_editing (PsppSheetView       *tree_view,
14818                                   PsppSheetViewColumn *column,
14819                                   GtkTreePath       *path,
14820                                   GtkCellEditable   *cell_editable,
14821                                   GdkRectangle      *cell_area,
14822                                   GdkEvent          *event,
14823                                   guint              flags)
14824 {
14825   gint pre_val = tree_view->priv->vadjustment->value;
14826   GtkRequisition requisition;
14827
14828   tree_view->priv->edited_column = column;
14829   _pspp_sheet_view_column_start_editing (column, GTK_CELL_EDITABLE (cell_editable));
14830
14831   pspp_sheet_view_real_set_cursor (tree_view, path, FALSE, TRUE);
14832   cell_area->y += pre_val - (int)tree_view->priv->vadjustment->value;
14833
14834   gtk_widget_size_request (GTK_WIDGET (cell_editable), &requisition);
14835
14836   PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_DRAW_KEYFOCUS);
14837
14838   if (requisition.height < cell_area->height)
14839     {
14840       gint diff = cell_area->height - requisition.height;
14841       pspp_sheet_view_put (tree_view,
14842                          GTK_WIDGET (cell_editable),
14843                          cell_area->x, cell_area->y + diff/2,
14844                          cell_area->width, requisition.height);
14845     }
14846   else
14847     {
14848       pspp_sheet_view_put (tree_view,
14849                          GTK_WIDGET (cell_editable),
14850                          cell_area->x, cell_area->y,
14851                          cell_area->width, cell_area->height);
14852     }
14853
14854   gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (cell_editable),
14855                                    (GdkEvent *)event);
14856
14857   gtk_widget_grab_focus (GTK_WIDGET (cell_editable));
14858   g_signal_connect (cell_editable, "remove-widget",
14859                     G_CALLBACK (pspp_sheet_view_remove_widget), tree_view);
14860 }
14861
14862 static void
14863 pspp_sheet_view_stop_editing (PsppSheetView *tree_view,
14864                             gboolean     cancel_editing)
14865 {
14866   PsppSheetViewColumn *column;
14867   GtkCellRenderer *cell;
14868
14869   if (tree_view->priv->edited_column == NULL)
14870     return;
14871
14872   /*
14873    * This is very evil. We need to do this, because
14874    * gtk_cell_editable_editing_done may trigger pspp_sheet_view_row_changed
14875    * later on. If pspp_sheet_view_row_changed notices
14876    * tree_view->priv->edited_column != NULL, it'll call
14877    * pspp_sheet_view_stop_editing again. Bad things will happen then.
14878    *
14879    * Please read that again if you intend to modify anything here.
14880    */
14881
14882   column = tree_view->priv->edited_column;
14883   tree_view->priv->edited_column = NULL;
14884
14885   cell = _pspp_sheet_view_column_get_edited_cell (column);
14886   gtk_cell_renderer_stop_editing (cell, cancel_editing);
14887
14888   if (!cancel_editing)
14889     gtk_cell_editable_editing_done (column->editable_widget);
14890
14891   tree_view->priv->edited_column = column;
14892
14893   gtk_cell_editable_remove_widget (column->editable_widget);
14894 }
14895
14896
14897 /**
14898  * pspp_sheet_view_set_hover_selection:
14899  * @tree_view: a #PsppSheetView
14900  * @hover: %TRUE to enable hover selection mode
14901  *
14902  * Enables of disables the hover selection mode of @tree_view.
14903  * Hover selection makes the selected row follow the pointer.
14904  * Currently, this works only for the selection modes 
14905  * %GTK_SELECTION_SINGLE and %GTK_SELECTION_BROWSE.
14906  * 
14907  * Since: 2.6
14908  **/
14909 void     
14910 pspp_sheet_view_set_hover_selection (PsppSheetView *tree_view,
14911                                    gboolean     hover)
14912 {
14913   hover = hover != FALSE;
14914
14915   if (hover != tree_view->priv->hover_selection)
14916     {
14917       tree_view->priv->hover_selection = hover;
14918
14919       g_object_notify (G_OBJECT (tree_view), "hover-selection");
14920     }
14921 }
14922
14923 /**
14924  * pspp_sheet_view_get_hover_selection:
14925  * @tree_view: a #PsppSheetView
14926  * 
14927  * Returns whether hover selection mode is turned on for @tree_view.
14928  * 
14929  * Return value: %TRUE if @tree_view is in hover selection mode
14930  *
14931  * Since: 2.6 
14932  **/
14933 gboolean 
14934 pspp_sheet_view_get_hover_selection (PsppSheetView *tree_view)
14935 {
14936   return tree_view->priv->hover_selection;
14937 }
14938
14939 /**
14940  * pspp_sheet_view_set_hover_expand:
14941  * @tree_view: a #PsppSheetView
14942  * @expand: %TRUE to enable hover selection mode
14943  *
14944  * Enables of disables the hover expansion mode of @tree_view.
14945  * Hover expansion makes rows expand or collapse if the pointer 
14946  * moves over them.
14947  * 
14948  * Since: 2.6
14949  **/
14950 void     
14951 pspp_sheet_view_set_hover_expand (PsppSheetView *tree_view,
14952                                 gboolean     expand)
14953 {
14954   expand = expand != FALSE;
14955
14956   if (expand != tree_view->priv->hover_expand)
14957     {
14958       tree_view->priv->hover_expand = expand;
14959
14960       g_object_notify (G_OBJECT (tree_view), "hover-expand");
14961     }
14962 }
14963
14964 /**
14965  * pspp_sheet_view_get_hover_expand:
14966  * @tree_view: a #PsppSheetView
14967  * 
14968  * Returns whether hover expansion mode is turned on for @tree_view.
14969  * 
14970  * Return value: %TRUE if @tree_view is in hover expansion mode
14971  *
14972  * Since: 2.6 
14973  **/
14974 gboolean 
14975 pspp_sheet_view_get_hover_expand (PsppSheetView *tree_view)
14976 {
14977   return tree_view->priv->hover_expand;
14978 }
14979
14980 /**
14981  * pspp_sheet_view_set_rubber_banding:
14982  * @tree_view: a #PsppSheetView
14983  * @enable: %TRUE to enable rubber banding
14984  *
14985  * Enables or disables rubber banding in @tree_view.  If the selection mode
14986  * is #GTK_SELECTION_MULTIPLE, rubber banding will allow the user to select
14987  * multiple rows by dragging the mouse.
14988  * 
14989  * Since: 2.10
14990  **/
14991 void
14992 pspp_sheet_view_set_rubber_banding (PsppSheetView *tree_view,
14993                                   gboolean     enable)
14994 {
14995   enable = enable != FALSE;
14996
14997   if (enable != tree_view->priv->rubber_banding_enable)
14998     {
14999       tree_view->priv->rubber_banding_enable = enable;
15000
15001       g_object_notify (G_OBJECT (tree_view), "rubber-banding");
15002     }
15003 }
15004
15005 /**
15006  * pspp_sheet_view_get_rubber_banding:
15007  * @tree_view: a #PsppSheetView
15008  * 
15009  * Returns whether rubber banding is turned on for @tree_view.  If the
15010  * selection mode is #GTK_SELECTION_MULTIPLE, rubber banding will allow the
15011  * user to select multiple rows by dragging the mouse.
15012  * 
15013  * Return value: %TRUE if rubber banding in @tree_view is enabled.
15014  *
15015  * Since: 2.10
15016  **/
15017 gboolean
15018 pspp_sheet_view_get_rubber_banding (PsppSheetView *tree_view)
15019 {
15020   return tree_view->priv->rubber_banding_enable;
15021 }
15022
15023 /**
15024  * pspp_sheet_view_is_rubber_banding_active:
15025  * @tree_view: a #PsppSheetView
15026  * 
15027  * Returns whether a rubber banding operation is currently being done
15028  * in @tree_view.
15029  *
15030  * Return value: %TRUE if a rubber banding operation is currently being
15031  * done in @tree_view.
15032  *
15033  * Since: 2.12
15034  **/
15035 gboolean
15036 pspp_sheet_view_is_rubber_banding_active (PsppSheetView *tree_view)
15037 {
15038   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
15039
15040   if (tree_view->priv->rubber_banding_enable
15041       && tree_view->priv->rubber_band_status == RUBBER_BAND_ACTIVE)
15042     return TRUE;
15043
15044   return FALSE;
15045 }
15046
15047 /**
15048  * pspp_sheet_view_get_row_separator_func:
15049  * @tree_view: a #PsppSheetView
15050  * 
15051  * Returns the current row separator function.
15052  * 
15053  * Return value: the current row separator function.
15054  *
15055  * Since: 2.6
15056  **/
15057 PsppSheetViewRowSeparatorFunc 
15058 pspp_sheet_view_get_row_separator_func (PsppSheetView *tree_view)
15059 {
15060   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), NULL);
15061
15062   return tree_view->priv->row_separator_func;
15063 }
15064
15065 /**
15066  * pspp_sheet_view_set_row_separator_func:
15067  * @tree_view: a #PsppSheetView
15068  * @func: a #PsppSheetViewRowSeparatorFunc
15069  * @data: (allow-none): user data to pass to @func, or %NULL
15070  * @destroy: (allow-none): destroy notifier for @data, or %NULL
15071  * 
15072  * Sets the row separator function, which is used to determine
15073  * whether a row should be drawn as a separator. If the row separator
15074  * function is %NULL, no separators are drawn. This is the default value.
15075  *
15076  * Since: 2.6
15077  **/
15078 void
15079 pspp_sheet_view_set_row_separator_func (PsppSheetView                 *tree_view,
15080                                       PsppSheetViewRowSeparatorFunc  func,
15081                                       gpointer                     data,
15082                                       GDestroyNotify               destroy)
15083 {
15084   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15085
15086   if (tree_view->priv->row_separator_destroy)
15087     tree_view->priv->row_separator_destroy (tree_view->priv->row_separator_data);
15088
15089   tree_view->priv->row_separator_func = func;
15090   tree_view->priv->row_separator_data = data;
15091   tree_view->priv->row_separator_destroy = destroy;
15092
15093   /* Have the tree recalculate heights */
15094   _pspp_rbtree_mark_invalid (tree_view->priv->tree);
15095   gtk_widget_queue_resize (GTK_WIDGET (tree_view));
15096 }
15097
15098   
15099 static void
15100 pspp_sheet_view_grab_notify (GtkWidget *widget,
15101                            gboolean   was_grabbed)
15102 {
15103   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
15104
15105   tree_view->priv->in_grab = !was_grabbed;
15106
15107   if (!was_grabbed)
15108     {
15109       tree_view->priv->pressed_button = -1;
15110
15111       if (tree_view->priv->rubber_band_status)
15112         pspp_sheet_view_stop_rubber_band (tree_view);
15113     }
15114 }
15115
15116 static void
15117 pspp_sheet_view_state_changed (GtkWidget      *widget,
15118                              GtkStateType    previous_state)
15119 {
15120   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
15121
15122   if (gtk_widget_get_realized (widget))
15123     {
15124       gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
15125       gdk_window_set_background (tree_view->priv->bin_window, &widget->style->base[widget->state]);
15126     }
15127
15128   gtk_widget_queue_draw (widget);
15129 }
15130
15131 /**
15132  * pspp_sheet_view_get_grid_lines:
15133  * @tree_view: a #PsppSheetView
15134  *
15135  * Returns which grid lines are enabled in @tree_view.
15136  *
15137  * Return value: a #PsppSheetViewGridLines value indicating which grid lines
15138  * are enabled.
15139  *
15140  * Since: 2.10
15141  */
15142 PsppSheetViewGridLines
15143 pspp_sheet_view_get_grid_lines (PsppSheetView *tree_view)
15144 {
15145   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
15146
15147   return tree_view->priv->grid_lines;
15148 }
15149
15150 /**
15151  * pspp_sheet_view_set_grid_lines:
15152  * @tree_view: a #PsppSheetView
15153  * @grid_lines: a #PsppSheetViewGridLines value indicating which grid lines to
15154  * enable.
15155  *
15156  * Sets which grid lines to draw in @tree_view.
15157  *
15158  * Since: 2.10
15159  */
15160 void
15161 pspp_sheet_view_set_grid_lines (PsppSheetView           *tree_view,
15162                               PsppSheetViewGridLines   grid_lines)
15163 {
15164   PsppSheetViewPrivate *priv;
15165   GtkWidget *widget;
15166   PsppSheetViewGridLines old_grid_lines;
15167
15168   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15169
15170   priv = tree_view->priv;
15171   widget = GTK_WIDGET (tree_view);
15172
15173   old_grid_lines = priv->grid_lines;
15174   priv->grid_lines = grid_lines;
15175   
15176   if (gtk_widget_get_realized (widget))
15177     {
15178       if (grid_lines == PSPP_SHEET_VIEW_GRID_LINES_NONE &&
15179           priv->grid_line_gc)
15180         {
15181           g_object_unref (priv->grid_line_gc);
15182           priv->grid_line_gc = NULL;
15183         }
15184       
15185       if (grid_lines != PSPP_SHEET_VIEW_GRID_LINES_NONE && 
15186           !priv->grid_line_gc)
15187         {
15188           gint line_width;
15189           gint8 *dash_list;
15190
15191           gtk_widget_style_get (widget,
15192                                 "grid-line-width", &line_width,
15193                                 "grid-line-pattern", (gchar *)&dash_list,
15194                                 NULL);
15195       
15196           priv->grid_line_gc = gdk_gc_new (widget->window);
15197           gdk_gc_copy (priv->grid_line_gc, widget->style->black_gc);
15198           
15199           gdk_gc_set_line_attributes (priv->grid_line_gc, line_width,
15200                                       GDK_LINE_ON_OFF_DASH,
15201                                       GDK_CAP_BUTT, GDK_JOIN_MITER);
15202           gdk_gc_set_dashes (priv->grid_line_gc, 0, dash_list, 2);
15203
15204           g_free (dash_list);
15205         }      
15206     }
15207
15208   if (old_grid_lines != grid_lines)
15209     {
15210       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
15211       
15212       g_object_notify (G_OBJECT (tree_view), "enable-grid-lines");
15213     }
15214 }
15215
15216 /**
15217  * pspp_sheet_view_get_enable_tree_lines:
15218  * @tree_view: a #PsppSheetView.
15219  *
15220  * Returns whether or not tree lines are drawn in @tree_view.
15221  *
15222  * Return value: %TRUE if tree lines are drawn in @tree_view, %FALSE
15223  * otherwise.
15224  *
15225  * Since: 2.10
15226  */
15227 gboolean
15228 pspp_sheet_view_get_enable_tree_lines (PsppSheetView *tree_view)
15229 {
15230   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
15231
15232   return tree_view->priv->tree_lines_enabled;
15233 }
15234
15235 /**
15236  * pspp_sheet_view_set_enable_tree_lines:
15237  * @tree_view: a #PsppSheetView
15238  * @enabled: %TRUE to enable tree line drawing, %FALSE otherwise.
15239  *
15240  * Sets whether to draw lines interconnecting the expanders in @tree_view.
15241  * This does not have any visible effects for lists.
15242  *
15243  * Since: 2.10
15244  */
15245 void
15246 pspp_sheet_view_set_enable_tree_lines (PsppSheetView *tree_view,
15247                                      gboolean     enabled)
15248 {
15249   PsppSheetViewPrivate *priv;
15250   GtkWidget *widget;
15251   gboolean was_enabled;
15252
15253   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15254
15255   enabled = enabled != FALSE;
15256
15257   priv = tree_view->priv;
15258   widget = GTK_WIDGET (tree_view);
15259
15260   was_enabled = priv->tree_lines_enabled;
15261
15262   priv->tree_lines_enabled = enabled;
15263
15264   if (gtk_widget_get_realized (widget))
15265     {
15266       if (!enabled && priv->tree_line_gc)
15267         {
15268           g_object_unref (priv->tree_line_gc);
15269           priv->tree_line_gc = NULL;
15270         }
15271       
15272       if (enabled && !priv->tree_line_gc)
15273         {
15274           gint line_width;
15275           gint8 *dash_list;
15276           gtk_widget_style_get (widget,
15277                                 "tree-line-width", &line_width,
15278                                 "tree-line-pattern", (gchar *)&dash_list,
15279                                 NULL);
15280           
15281           priv->tree_line_gc = gdk_gc_new (widget->window);
15282           gdk_gc_copy (priv->tree_line_gc, widget->style->black_gc);
15283           
15284           gdk_gc_set_line_attributes (priv->tree_line_gc, line_width,
15285                                       GDK_LINE_ON_OFF_DASH,
15286                                       GDK_CAP_BUTT, GDK_JOIN_MITER);
15287           gdk_gc_set_dashes (priv->tree_line_gc, 0, dash_list, 2);
15288
15289           g_free (dash_list);
15290         }
15291     }
15292
15293   if (was_enabled != enabled)
15294     {
15295       gtk_widget_queue_draw (GTK_WIDGET (tree_view));
15296
15297       g_object_notify (G_OBJECT (tree_view), "enable-tree-lines");
15298     }
15299 }
15300
15301
15302 /**
15303  * pspp_sheet_view_set_show_expanders:
15304  * @tree_view: a #PsppSheetView
15305  * @enabled: %TRUE to enable expander drawing, %FALSE otherwise.
15306  *
15307  * Sets whether to draw and enable expanders and indent child rows in
15308  * @tree_view.  When disabled there will be no expanders visible in trees
15309  * and there will be no way to expand and collapse rows by default.  Also
15310  * note that hiding the expanders will disable the default indentation.  You
15311  * can set a custom indentation in this case using
15312  * pspp_sheet_view_set_level_indentation().
15313  * This does not have any visible effects for lists.
15314  *
15315  * Since: 2.12
15316  */
15317 void
15318 pspp_sheet_view_set_show_expanders (PsppSheetView *tree_view,
15319                                   gboolean     enabled)
15320 {
15321   gboolean was_enabled;
15322
15323   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15324
15325   enabled = enabled != FALSE;
15326   was_enabled = PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS);
15327
15328   if (enabled)
15329     PSPP_SHEET_VIEW_SET_FLAG (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS);
15330   else
15331     PSPP_SHEET_VIEW_UNSET_FLAG (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS);
15332
15333   if (enabled != was_enabled)
15334     gtk_widget_queue_draw (GTK_WIDGET (tree_view));
15335 }
15336
15337 /**
15338  * pspp_sheet_view_get_show_expanders:
15339  * @tree_view: a #PsppSheetView.
15340  *
15341  * Returns whether or not expanders are drawn in @tree_view.
15342  *
15343  * Return value: %TRUE if expanders are drawn in @tree_view, %FALSE
15344  * otherwise.
15345  *
15346  * Since: 2.12
15347  */
15348 gboolean
15349 pspp_sheet_view_get_show_expanders (PsppSheetView *tree_view)
15350 {
15351   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
15352
15353   return PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS);
15354 }
15355
15356 /**
15357  * pspp_sheet_view_set_level_indentation:
15358  * @tree_view: a #PsppSheetView
15359  * @indentation: the amount, in pixels, of extra indentation in @tree_view.
15360  *
15361  * Sets the amount of extra indentation for child levels to use in @tree_view
15362  * in addition to the default indentation.  The value should be specified in
15363  * pixels, a value of 0 disables this feature and in this case only the default
15364  * indentation will be used.
15365  * This does not have any visible effects for lists.
15366  *
15367  * Since: 2.12
15368  */
15369 void
15370 pspp_sheet_view_set_level_indentation (PsppSheetView *tree_view,
15371                                      gint         indentation)
15372 {
15373   tree_view->priv->level_indentation = indentation;
15374
15375   gtk_widget_queue_draw (GTK_WIDGET (tree_view));
15376 }
15377
15378 /**
15379  * pspp_sheet_view_get_level_indentation:
15380  * @tree_view: a #PsppSheetView.
15381  *
15382  * Returns the amount, in pixels, of extra indentation for child levels
15383  * in @tree_view.
15384  *
15385  * Return value: the amount of extra indentation for child levels in
15386  * @tree_view.  A return value of 0 means that this feature is disabled.
15387  *
15388  * Since: 2.12
15389  */
15390 gint
15391 pspp_sheet_view_get_level_indentation (PsppSheetView *tree_view)
15392 {
15393   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
15394
15395   return tree_view->priv->level_indentation;
15396 }
15397
15398 /**
15399  * pspp_sheet_view_set_tooltip_row:
15400  * @tree_view: a #PsppSheetView
15401  * @tooltip: a #GtkTooltip
15402  * @path: a #GtkTreePath
15403  *
15404  * Sets the tip area of @tooltip to be the area covered by the row at @path.
15405  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
15406  * See also gtk_tooltip_set_tip_area().
15407  *
15408  * Since: 2.12
15409  */
15410 void
15411 pspp_sheet_view_set_tooltip_row (PsppSheetView *tree_view,
15412                                GtkTooltip  *tooltip,
15413                                GtkTreePath *path)
15414 {
15415   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15416   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
15417
15418   pspp_sheet_view_set_tooltip_cell (tree_view, tooltip, path, NULL, NULL);
15419 }
15420
15421 /**
15422  * pspp_sheet_view_set_tooltip_cell:
15423  * @tree_view: a #PsppSheetView
15424  * @tooltip: a #GtkTooltip
15425  * @path: (allow-none): a #GtkTreePath or %NULL
15426  * @column: (allow-none): a #PsppSheetViewColumn or %NULL
15427  * @cell: (allow-none): a #GtkCellRenderer or %NULL
15428  *
15429  * Sets the tip area of @tooltip to the area @path, @column and @cell have
15430  * in common.  For example if @path is %NULL and @column is set, the tip
15431  * area will be set to the full area covered by @column.  See also
15432  * gtk_tooltip_set_tip_area().
15433  *
15434  * Note that if @path is not specified and @cell is set and part of a column
15435  * containing the expander, the tooltip might not show and hide at the correct
15436  * position.  In such cases @path must be set to the current node under the
15437  * mouse cursor for this function to operate correctly.
15438  *
15439  * See also pspp_sheet_view_set_tooltip_column() for a simpler alternative.
15440  *
15441  * Since: 2.12
15442  */
15443 void
15444 pspp_sheet_view_set_tooltip_cell (PsppSheetView       *tree_view,
15445                                 GtkTooltip        *tooltip,
15446                                 GtkTreePath       *path,
15447                                 PsppSheetViewColumn *column,
15448                                 GtkCellRenderer   *cell)
15449 {
15450   GdkRectangle rect;
15451
15452   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15453   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
15454   g_return_if_fail (column == NULL || PSPP_IS_SHEET_VIEW_COLUMN (column));
15455   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
15456
15457   /* Determine x values. */
15458   if (column && cell)
15459     {
15460       GdkRectangle tmp;
15461       gint start, width;
15462
15463       /* We always pass in path here, whether it is NULL or not.
15464        * For cells in expander columns path must be specified so that
15465        * we can correctly account for the indentation.  This also means
15466        * that the tooltip is constrained vertically by the "Determine y
15467        * values" code below; this is not a real problem since cells actually
15468        * don't stretch vertically in constrast to columns.
15469        */
15470       pspp_sheet_view_get_cell_area (tree_view, path, column, &tmp);
15471       pspp_sheet_view_column_cell_get_position (column, cell, &start, &width);
15472
15473       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
15474                                                          tmp.x + start, 0,
15475                                                          &rect.x, NULL);
15476       rect.width = width;
15477     }
15478   else if (column)
15479     {
15480       GdkRectangle tmp;
15481
15482       pspp_sheet_view_get_background_area (tree_view, NULL, column, &tmp);
15483       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
15484                                                          tmp.x, 0,
15485                                                          &rect.x, NULL);
15486       rect.width = tmp.width;
15487     }
15488   else
15489     {
15490       rect.x = 0;
15491       rect.width = GTK_WIDGET (tree_view)->allocation.width;
15492     }
15493
15494   /* Determine y values. */
15495   if (path)
15496     {
15497       GdkRectangle tmp;
15498
15499       pspp_sheet_view_get_background_area (tree_view, path, NULL, &tmp);
15500       pspp_sheet_view_convert_bin_window_to_widget_coords (tree_view,
15501                                                          0, tmp.y,
15502                                                          NULL, &rect.y);
15503       rect.height = tmp.height;
15504     }
15505   else
15506     {
15507       rect.y = 0;
15508       rect.height = tree_view->priv->vadjustment->page_size;
15509     }
15510
15511   gtk_tooltip_set_tip_area (tooltip, &rect);
15512 }
15513
15514 /**
15515  * pspp_sheet_view_get_tooltip_context:
15516  * @tree_view: a #PsppSheetView
15517  * @x: the x coordinate (relative to widget coordinates)
15518  * @y: the y coordinate (relative to widget coordinates)
15519  * @keyboard_tip: whether this is a keyboard tooltip or not
15520  * @model: (allow-none): a pointer to receive a #GtkTreeModel or %NULL
15521  * @path: (allow-none): a pointer to receive a #GtkTreePath or %NULL
15522  * @iter: (allow-none): a pointer to receive a #GtkTreeIter or %NULL
15523  *
15524  * This function is supposed to be used in a #GtkWidget::query-tooltip
15525  * signal handler for #PsppSheetView.  The @x, @y and @keyboard_tip values
15526  * which are received in the signal handler, should be passed to this
15527  * function without modification.
15528  *
15529  * The return value indicates whether there is a tree view row at the given
15530  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips.  For keyboard
15531  * tooltips the row returned will be the cursor row.  When %TRUE, then any of
15532  * @model, @path and @iter which have been provided will be set to point to
15533  * that row and the corresponding model.  @x and @y will always be converted
15534  * to be relative to @tree_view's bin_window if @keyboard_tooltip is %FALSE.
15535  *
15536  * Return value: whether or not the given tooltip context points to a row.
15537  *
15538  * Since: 2.12
15539  */
15540 gboolean
15541 pspp_sheet_view_get_tooltip_context (PsppSheetView   *tree_view,
15542                                    gint          *x,
15543                                    gint          *y,
15544                                    gboolean       keyboard_tip,
15545                                    GtkTreeModel **model,
15546                                    GtkTreePath  **path,
15547                                    GtkTreeIter   *iter)
15548 {
15549   GtkTreePath *tmppath = NULL;
15550
15551   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), FALSE);
15552   g_return_val_if_fail (x != NULL, FALSE);
15553   g_return_val_if_fail (y != NULL, FALSE);
15554
15555   if (keyboard_tip)
15556     {
15557       pspp_sheet_view_get_cursor (tree_view, &tmppath, NULL);
15558
15559       if (!tmppath)
15560         return FALSE;
15561     }
15562   else
15563     {
15564       pspp_sheet_view_convert_widget_to_bin_window_coords (tree_view, *x, *y,
15565                                                          x, y);
15566
15567       if (!pspp_sheet_view_get_path_at_pos (tree_view, *x, *y,
15568                                           &tmppath, NULL, NULL, NULL))
15569         return FALSE;
15570     }
15571
15572   if (model)
15573     *model = pspp_sheet_view_get_model (tree_view);
15574
15575   if (iter)
15576     gtk_tree_model_get_iter (pspp_sheet_view_get_model (tree_view),
15577                              iter, tmppath);
15578
15579   if (path)
15580     *path = tmppath;
15581   else
15582     gtk_tree_path_free (tmppath);
15583
15584   return TRUE;
15585 }
15586
15587 static gboolean
15588 pspp_sheet_view_set_tooltip_query_cb (GtkWidget  *widget,
15589                                     gint        x,
15590                                     gint        y,
15591                                     gboolean    keyboard_tip,
15592                                     GtkTooltip *tooltip,
15593                                     gpointer    data)
15594 {
15595   GValue value = { 0, };
15596   GValue transformed = { 0, };
15597   GtkTreeIter iter;
15598   GtkTreePath *path;
15599   GtkTreeModel *model;
15600   PsppSheetView *tree_view = PSPP_SHEET_VIEW (widget);
15601
15602   if (!pspp_sheet_view_get_tooltip_context (PSPP_SHEET_VIEW (widget),
15603                                           &x, &y,
15604                                           keyboard_tip,
15605                                           &model, &path, &iter))
15606     return FALSE;
15607
15608   gtk_tree_model_get_value (model, &iter,
15609                             tree_view->priv->tooltip_column, &value);
15610
15611   g_value_init (&transformed, G_TYPE_STRING);
15612
15613   if (!g_value_transform (&value, &transformed))
15614     {
15615       g_value_unset (&value);
15616       gtk_tree_path_free (path);
15617
15618       return FALSE;
15619     }
15620
15621   g_value_unset (&value);
15622
15623   if (!g_value_get_string (&transformed))
15624     {
15625       g_value_unset (&transformed);
15626       gtk_tree_path_free (path);
15627
15628       return FALSE;
15629     }
15630
15631   gtk_tooltip_set_markup (tooltip, g_value_get_string (&transformed));
15632   pspp_sheet_view_set_tooltip_row (tree_view, tooltip, path);
15633
15634   gtk_tree_path_free (path);
15635   g_value_unset (&transformed);
15636
15637   return TRUE;
15638 }
15639
15640 /**
15641  * pspp_sheet_view_set_tooltip_column:
15642  * @tree_view: a #PsppSheetView
15643  * @column: an integer, which is a valid column number for @tree_view's model
15644  *
15645  * If you only plan to have simple (text-only) tooltips on full rows, you
15646  * can use this function to have #PsppSheetView handle these automatically
15647  * for you. @column should be set to the column in @tree_view's model
15648  * containing the tooltip texts, or -1 to disable this feature.
15649  *
15650  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
15651  * @tree_view will connect a #GtkWidget::query-tooltip signal handler.
15652  *
15653  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
15654  * so &amp;, &lt;, etc have to be escaped in the text.
15655  *
15656  * Since: 2.12
15657  */
15658 void
15659 pspp_sheet_view_set_tooltip_column (PsppSheetView *tree_view,
15660                                   gint         column)
15661 {
15662   g_return_if_fail (PSPP_IS_SHEET_VIEW (tree_view));
15663
15664   if (column == tree_view->priv->tooltip_column)
15665     return;
15666
15667   if (column == -1)
15668     {
15669       g_signal_handlers_disconnect_by_func (tree_view,
15670                                             pspp_sheet_view_set_tooltip_query_cb,
15671                                             NULL);
15672       gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), FALSE);
15673     }
15674   else
15675     {
15676       if (tree_view->priv->tooltip_column == -1)
15677         {
15678           g_signal_connect (tree_view, "query-tooltip",
15679                             G_CALLBACK (pspp_sheet_view_set_tooltip_query_cb), NULL);
15680           gtk_widget_set_has_tooltip (GTK_WIDGET (tree_view), TRUE);
15681         }
15682     }
15683
15684   tree_view->priv->tooltip_column = column;
15685   g_object_notify (G_OBJECT (tree_view), "tooltip-column");
15686 }
15687
15688 /**
15689  * pspp_sheet_view_get_tooltip_column:
15690  * @tree_view: a #PsppSheetView
15691  *
15692  * Returns the column of @tree_view's model which is being used for
15693  * displaying tooltips on @tree_view's rows.
15694  *
15695  * Return value: the index of the tooltip column that is currently being
15696  * used, or -1 if this is disabled.
15697  *
15698  * Since: 2.12
15699  */
15700 gint
15701 pspp_sheet_view_get_tooltip_column (PsppSheetView *tree_view)
15702 {
15703   g_return_val_if_fail (PSPP_IS_SHEET_VIEW (tree_view), 0);
15704
15705   return tree_view->priv->tooltip_column;
15706 }
15707
15708 gboolean
15709 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
15710                                   GValue                *return_accu,
15711                                   const GValue          *handler_return,
15712                                   gpointer               dummy)
15713 {
15714   gboolean continue_emission;
15715   gboolean signal_handled;
15716   
15717   signal_handled = g_value_get_boolean (handler_return);
15718   g_value_set_boolean (return_accu, signal_handled);
15719   continue_emission = !signal_handled;
15720   
15721   return continue_emission;
15722 }
15723
15724 GType
15725 pspp_sheet_view_grid_lines_get_type (void)
15726 {
15727     static GType etype = 0;
15728     if (G_UNLIKELY(etype == 0)) {
15729         static const GEnumValue values[] = {
15730             { PSPP_SHEET_VIEW_GRID_LINES_NONE, "PSPP_SHEET_VIEW_GRID_LINES_NONE", "none" },
15731             { PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL, "PSPP_SHEET_VIEW_GRID_LINES_HORIZONTAL", "horizontal" },
15732             { PSPP_SHEET_VIEW_GRID_LINES_VERTICAL, "PSPP_SHEET_VIEW_GRID_LINES_VERTICAL", "vertical" },
15733             { PSPP_SHEET_VIEW_GRID_LINES_BOTH, "PSPP_SHEET_VIEW_GRID_LINES_BOTH", "both" },
15734             { 0, NULL, NULL }
15735         };
15736         etype = g_enum_register_static (g_intern_static_string ("PsppSheetViewGridLines"), values);
15737     }
15738     return etype;
15739 }