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