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