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