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