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