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