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