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