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