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