Import GtkTreeView from GTK+ 2.0-2.20.1 renamed as PsppSheetView.
[pspp] / src / ui / gui / pspp-sheet-private.h
1  /* PSPPIRE - a graphical user interface for PSPP.
2     Copyright (C) 2011 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 /* gtktreeprivate.h
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 #ifndef __GTK_TREE_PRIVATE_H__
37 #define __GTK_TREE_PRIVATE_H__
38
39
40 #include <gtk/gtk.h>
41 #include "ui/gui/pspp-sheet-view.h"
42 #include "ui/gui/pspp-sheet-view-column.h"
43 #include "ui/gui/pspp-rb-tree.h"
44
45 #define TREE_VIEW_DRAG_WIDTH 6
46
47 typedef enum
48 {
49   PSPP_SHEET_VIEW_IS_LIST = 1 << 0,
50   PSPP_SHEET_VIEW_SHOW_EXPANDERS = 1 << 1,
51   PSPP_SHEET_VIEW_IN_COLUMN_RESIZE = 1 << 2,
52   PSPP_SHEET_VIEW_ARROW_PRELIT = 1 << 3,
53   PSPP_SHEET_VIEW_HEADERS_VISIBLE = 1 << 4,
54   PSPP_SHEET_VIEW_DRAW_KEYFOCUS = 1 << 5,
55   PSPP_SHEET_VIEW_MODEL_SETUP = 1 << 6,
56   PSPP_SHEET_VIEW_IN_COLUMN_DRAG = 1 << 7
57 } PsppSheetViewFlags;
58
59 typedef enum
60 {
61   GTK_TREE_SELECT_MODE_TOGGLE = 1 << 0,
62   GTK_TREE_SELECT_MODE_EXTEND = 1 << 1
63 }
64 GtkTreeSelectMode;
65
66 enum
67 {
68   DRAG_COLUMN_WINDOW_STATE_UNSET = 0,
69   DRAG_COLUMN_WINDOW_STATE_ORIGINAL = 1,
70   DRAG_COLUMN_WINDOW_STATE_ARROW = 2,
71   DRAG_COLUMN_WINDOW_STATE_ARROW_LEFT = 3,
72   DRAG_COLUMN_WINDOW_STATE_ARROW_RIGHT = 4
73 };
74
75 enum
76 {
77   RUBBER_BAND_OFF = 0,
78   RUBBER_BAND_MAYBE_START = 1,
79   RUBBER_BAND_ACTIVE = 2
80 };
81
82 #define PSPP_SHEET_VIEW_SET_FLAG(tree_view, flag)   G_STMT_START{ (tree_view->priv->flags|=flag); }G_STMT_END
83 #define PSPP_SHEET_VIEW_UNSET_FLAG(tree_view, flag) G_STMT_START{ (tree_view->priv->flags&=~(flag)); }G_STMT_END
84 #define PSPP_SHEET_VIEW_FLAG_SET(tree_view, flag)   ((tree_view->priv->flags&flag)==flag)
85 #define TREE_VIEW_HEADER_HEIGHT(tree_view)        (PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_HEADERS_VISIBLE)?tree_view->priv->header_height:0)
86 #define TREE_VIEW_COLUMN_REQUESTED_WIDTH(column)  (CLAMP (column->requested_width, (column->min_width!=-1)?column->min_width:column->requested_width, (column->max_width!=-1)?column->max_width:column->requested_width))
87 #define TREE_VIEW_DRAW_EXPANDERS(tree_view)       (!PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_IS_LIST)&&PSPP_SHEET_VIEW_FLAG_SET (tree_view, PSPP_SHEET_VIEW_SHOW_EXPANDERS))
88
89  /* This lovely little value is used to determine how far away from the title bar
90   * you can move the mouse and still have a column drag work.
91   */
92 #define TREE_VIEW_COLUMN_DRAG_DEAD_MULTIPLIER(tree_view) (10*TREE_VIEW_HEADER_HEIGHT(tree_view))
93
94 typedef struct _PsppSheetViewColumnReorder PsppSheetViewColumnReorder;
95 struct _PsppSheetViewColumnReorder
96 {
97   gint left_align;
98   gint right_align;
99   PsppSheetViewColumn *left_column;
100   PsppSheetViewColumn *right_column;
101 };
102
103 struct _PsppSheetViewPrivate
104 {
105   GtkTreeModel *model;
106
107   guint flags;
108   /* tree information */
109   GtkRBTree *tree;
110
111   /* Container info */
112   GList *children;
113   gint width;
114   gint height;
115
116   /* Adjustments */
117   GtkAdjustment *hadjustment;
118   GtkAdjustment *vadjustment;
119
120   /* Sub windows */
121   GdkWindow *bin_window;
122   GdkWindow *header_window;
123
124   /* Scroll position state keeping */
125   GtkTreeRowReference *top_row;
126   gint top_row_dy;
127   /* dy == y pos of top_row + top_row_dy */
128   /* we cache it for simplicity of the code */
129   gint dy;
130
131   guint presize_handler_timer;
132   guint validate_rows_timer;
133   guint scroll_sync_timer;
134
135   /* Indentation and expander layout */
136   gint expander_size;
137   PsppSheetViewColumn *expander_column;
138
139   gint level_indentation;
140
141   /* Key navigation (focus), selection */
142   gint cursor_offset;
143
144   GtkTreeRowReference *anchor;
145   GtkTreeRowReference *cursor;
146
147   PsppSheetViewColumn *focus_column;
148
149   /* Current pressed node, previously pressed, prelight */
150   GtkRBNode *button_pressed_node;
151   GtkRBTree *button_pressed_tree;
152
153   gint pressed_button;
154   gint press_start_x;
155   gint press_start_y;
156
157   gint event_last_x;
158   gint event_last_y;
159
160   guint last_button_time;
161   gint last_button_x;
162   gint last_button_y;
163
164   GtkRBNode *prelight_node;
165   GtkRBTree *prelight_tree;
166
167   /* Cell Editing */
168   PsppSheetViewColumn *edited_column;
169
170   /* The node that's currently being collapsed or expanded */
171   GtkRBNode *expanded_collapsed_node;
172   GtkRBTree *expanded_collapsed_tree;
173   guint expand_collapse_timeout;
174
175   /* Auto expand/collapse timeout in hover mode */
176   guint auto_expand_timeout;
177
178   /* Selection information */
179   PsppSheetSelection *selection;
180
181   /* Header information */
182   gint n_columns;
183   GList *columns;
184   gint header_height;
185
186   PsppSheetViewColumnDropFunc column_drop_func;
187   gpointer column_drop_func_data;
188   GDestroyNotify column_drop_func_data_destroy;
189   GList *column_drag_info;
190   PsppSheetViewColumnReorder *cur_reorder;
191
192   gint prev_width_before_expander;
193
194   /* Interactive Header reordering */
195   GdkWindow *drag_window;
196   GdkWindow *drag_highlight_window;
197   PsppSheetViewColumn *drag_column;
198   gint drag_column_x;
199
200   /* Interactive Header Resizing */
201   gint drag_pos;
202   gint x_drag;
203
204   /* Non-interactive Header Resizing, expand flag support */
205   gint prev_width;
206
207   gint last_extra_space;
208   gint last_extra_space_per_column;
209   gint last_number_of_expand_columns;
210
211   /* ATK Hack */
212   PsppSheetDestroyCountFunc destroy_count_func;
213   gpointer destroy_count_data;
214   GDestroyNotify destroy_count_destroy;
215
216   /* Scroll timeout (e.g. during dnd, rubber banding) */
217   guint scroll_timeout;
218
219   /* Row drag-and-drop */
220   GtkTreeRowReference *drag_dest_row;
221   PsppSheetViewDropPosition drag_dest_pos;
222   guint open_dest_timeout;
223
224   /* Rubber banding */
225   gint rubber_band_status;
226   gint rubber_band_x;
227   gint rubber_band_y;
228   gint rubber_band_shift;
229   gint rubber_band_ctrl;
230
231   GtkRBNode *rubber_band_start_node;
232   GtkRBTree *rubber_band_start_tree;
233
234   GtkRBNode *rubber_band_end_node;
235   GtkRBTree *rubber_band_end_tree;
236
237   /* fixed height */
238   gint fixed_height;
239
240   /* Scroll-to functionality when unrealized */
241   GtkTreeRowReference *scroll_to_path;
242   PsppSheetViewColumn *scroll_to_column;
243   gfloat scroll_to_row_align;
244   gfloat scroll_to_col_align;
245
246   /* Interactive search */
247   gint selected_iter;
248   gint search_column;
249   PsppSheetViewSearchPositionFunc search_position_func;
250   PsppSheetViewSearchEqualFunc search_equal_func;
251   gpointer search_user_data;
252   GDestroyNotify search_destroy;
253   gpointer search_position_user_data;
254   GDestroyNotify search_position_destroy;
255   GtkWidget *search_window;
256   GtkWidget *search_entry;
257   guint search_entry_changed_id;
258   guint typeselect_flush_timeout;
259
260   /* Grid and tree lines */
261   PsppSheetViewGridLines grid_lines;
262   GdkGC *grid_line_gc;
263
264   gboolean tree_lines_enabled;
265   GdkGC *tree_line_gc;
266
267   /* Row separators */
268   PsppSheetViewRowSeparatorFunc row_separator_func;
269   gpointer row_separator_data;
270   GDestroyNotify row_separator_destroy;
271
272   /* Tooltip support */
273   gint tooltip_column;
274
275   /* Here comes the bitfield */
276   guint scroll_to_use_align : 1;
277
278   guint fixed_height_mode : 1;
279   guint fixed_height_check : 1;
280
281   guint reorderable : 1;
282   guint header_has_focus : 1;
283   guint drag_column_window_state : 3;
284   /* hint to display rows in alternating colors */
285   guint has_rules : 1;
286   guint mark_rows_col_dirty : 1;
287
288   /* for DnD */
289   guint empty_view_drop : 1;
290
291   guint ctrl_pressed : 1;
292   guint shift_pressed : 1;
293
294   guint init_hadjust_value : 1;
295
296   guint in_top_row_to_dy : 1;
297
298   /* interactive search */
299   guint enable_search : 1;
300   guint disable_popdown : 1;
301   guint search_custom_entry_set : 1;
302   
303   guint hover_selection : 1;
304   guint hover_expand : 1;
305   guint imcontext_changed : 1;
306
307   guint rubber_banding_enable : 1;
308
309   guint in_grab : 1;
310
311   guint post_validation_flag : 1;
312
313   /* Whether our key press handler is to avoid sending an unhandled binding to the search entry */
314   guint search_entry_avoid_unhandled_binding : 1;
315 };
316
317 #ifdef __GNUC__
318
319 #define TREE_VIEW_INTERNAL_ASSERT(expr, ret)     G_STMT_START{          \
320      if (!(expr))                                                       \
321        {                                                                \
322          g_log (G_LOG_DOMAIN,                                           \
323                 G_LOG_LEVEL_CRITICAL,                                   \
324                 "%s (%s): assertion `%s' failed.\n"                     \
325                 "There is a disparity between the internal view of the PsppSheetView,\n"    \
326                 "and the GtkTreeModel.  This generally means that the model has changed\n"\
327                 "without letting the view know.  Any display from now on is likely to\n"  \
328                 "be incorrect.\n",                                                        \
329                 G_STRLOC,                                               \
330                 G_STRFUNC,                                              \
331                 #expr);                                                 \
332          return ret;                                                    \
333        };                               }G_STMT_END
334
335 #define TREE_VIEW_INTERNAL_ASSERT_VOID(expr)     G_STMT_START{          \
336      if (!(expr))                                                       \
337        {                                                                \
338          g_log (G_LOG_DOMAIN,                                           \
339                 G_LOG_LEVEL_CRITICAL,                                   \
340                 "%s (%s): assertion `%s' failed.\n"                     \
341                 "There is a disparity between the internal view of the PsppSheetView,\n"    \
342                 "and the GtkTreeModel.  This generally means that the model has changed\n"\
343                 "without letting the view know.  Any display from now on is likely to\n"  \
344                 "be incorrect.\n",                                                        \
345                 G_STRLOC,                                               \
346                 G_STRFUNC,                                              \
347                 #expr);                                                 \
348          return;                                                        \
349        };                               }G_STMT_END
350
351 #else
352
353 #define TREE_VIEW_INTERNAL_ASSERT(expr, ret)     G_STMT_START{          \
354      if (!(expr))                                                       \
355        {                                                                \
356          g_log (G_LOG_DOMAIN,                                           \
357                 G_LOG_LEVEL_CRITICAL,                                   \
358                 "file %s: line %d: assertion `%s' failed.\n"       \
359                 "There is a disparity between the internal view of the PsppSheetView,\n"    \
360                 "and the GtkTreeModel.  This generally means that the model has changed\n"\
361                 "without letting the view know.  Any display from now on is likely to\n"  \
362                 "be incorrect.\n",                                                        \
363                 __FILE__,                                               \
364                 __LINE__,                                               \
365                 #expr);                                                 \
366          return ret;                                                    \
367        };                               }G_STMT_END
368
369 #define TREE_VIEW_INTERNAL_ASSERT_VOID(expr)     G_STMT_START{          \
370      if (!(expr))                                                       \
371        {                                                                \
372          g_log (G_LOG_DOMAIN,                                           \
373                 G_LOG_LEVEL_CRITICAL,                                   \
374                 "file %s: line %d: assertion '%s' failed.\n"            \
375                 "There is a disparity between the internal view of the PsppSheetView,\n"    \
376                 "and the GtkTreeModel.  This generally means that the model has changed\n"\
377                 "without letting the view know.  Any display from now on is likely to\n"  \
378                 "be incorrect.\n",                                                        \
379                 __FILE__,                                               \
380                 __LINE__,                                               \
381                 #expr);                                                 \
382          return;                                                        \
383        };                               }G_STMT_END
384 #endif
385
386
387 /* functions that shouldn't be exported */
388 void         _pspp_sheet_selection_internal_select_node (PsppSheetSelection  *selection,
389                                                        GtkRBNode         *node,
390                                                        GtkRBTree         *tree,
391                                                        GtkTreePath       *path,
392                                                        GtkTreeSelectMode  mode,
393                                                        gboolean           override_browse_mode);
394 void         _pspp_sheet_selection_emit_changed         (PsppSheetSelection  *selection);
395 gboolean     _pspp_sheet_view_find_node                 (PsppSheetView       *tree_view,
396                                                        GtkTreePath       *path,
397                                                        GtkRBTree        **tree,
398                                                        GtkRBNode        **node);
399 GtkTreePath *_pspp_sheet_view_find_path                 (PsppSheetView       *tree_view,
400                                                        GtkRBTree         *tree,
401                                                        GtkRBNode         *node);
402 void         _pspp_sheet_view_child_move_resize         (PsppSheetView       *tree_view,
403                                                        GtkWidget         *widget,
404                                                        gint               x,
405                                                        gint               y,
406                                                        gint               width,
407                                                        gint               height);
408 void         _pspp_sheet_view_queue_draw_node           (PsppSheetView       *tree_view,
409                                                        GtkRBTree         *tree,
410                                                        GtkRBNode         *node,
411                                                        const GdkRectangle *clip_rect);
412
413 void _pspp_sheet_view_column_realize_button   (PsppSheetViewColumn *column);
414 void _pspp_sheet_view_column_unrealize_button (PsppSheetViewColumn *column);
415 void _pspp_sheet_view_column_set_tree_view    (PsppSheetViewColumn *column,
416                                              PsppSheetView       *tree_view);
417 void _pspp_sheet_view_column_unset_model      (PsppSheetViewColumn *column,
418                                              GtkTreeModel      *old_model);
419 void _pspp_sheet_view_column_unset_tree_view  (PsppSheetViewColumn *column);
420 void _pspp_sheet_view_column_set_width        (PsppSheetViewColumn *column,
421                                              gint               width);
422 void _pspp_sheet_view_column_start_drag       (PsppSheetView       *tree_view,
423                                              PsppSheetViewColumn *column);
424 gboolean _pspp_sheet_view_column_cell_event   (PsppSheetViewColumn  *tree_column,
425                                              GtkCellEditable   **editable_widget,
426                                              GdkEvent           *event,
427                                              gchar              *path_string,
428                                              const GdkRectangle *background_area,
429                                              const GdkRectangle *cell_area,
430                                              guint               flags);
431 void _pspp_sheet_view_column_start_editing (PsppSheetViewColumn *tree_column,
432                                           GtkCellEditable   *editable_widget);
433 void _pspp_sheet_view_column_stop_editing  (PsppSheetViewColumn *tree_column);
434 void _pspp_sheet_view_install_mark_rows_col_dirty (PsppSheetView *tree_view);
435 void             _pspp_sheet_view_column_autosize          (PsppSheetView       *tree_view,
436                                                           PsppSheetViewColumn *column);
437
438 gboolean         _pspp_sheet_view_column_has_editable_cell (PsppSheetViewColumn *column);
439 GtkCellRenderer *_pspp_sheet_view_column_get_edited_cell   (PsppSheetViewColumn *column);
440 gint             _pspp_sheet_view_column_count_special_cells (PsppSheetViewColumn *column);
441 GtkCellRenderer *_pspp_sheet_view_column_get_cell_at_pos   (PsppSheetViewColumn *column,
442                                                           gint               x);
443
444 PsppSheetSelection* _pspp_sheet_selection_new                (void);
445 PsppSheetSelection* _pspp_sheet_selection_new_with_tree_view (PsppSheetView      *tree_view);
446 void              _pspp_sheet_selection_set_tree_view      (PsppSheetSelection *selection,
447                                                           PsppSheetView      *tree_view);
448 gboolean          _pspp_sheet_selection_row_is_selectable  (PsppSheetSelection *selection,
449                                                           GtkRBNode        *node,
450                                                           GtkTreePath      *path);
451
452 void              _pspp_sheet_view_column_cell_render      (PsppSheetViewColumn  *tree_column,
453                                                           GdkWindow          *window,
454                                                           const GdkRectangle *background_area,
455                                                           const GdkRectangle *cell_area,
456                                                           const GdkRectangle *expose_area,
457                                                           guint               flags);
458 void              _pspp_sheet_view_column_get_focus_area   (PsppSheetViewColumn  *tree_column,
459                                                           const GdkRectangle *background_area,
460                                                           const GdkRectangle *cell_area,
461                                                           GdkRectangle       *focus_area);
462 gboolean          _pspp_sheet_view_column_cell_focus       (PsppSheetViewColumn  *tree_column,
463                                                           gint                direction,
464                                                           gboolean            left,
465                                                           gboolean            right);
466 void              _pspp_sheet_view_column_cell_draw_focus  (PsppSheetViewColumn  *tree_column,
467                                                           GdkWindow          *window,
468                                                           const GdkRectangle *background_area,
469                                                           const GdkRectangle *cell_area,
470                                                           const GdkRectangle *expose_area,
471                                                           guint               flags);
472 void              _pspp_sheet_view_column_cell_set_dirty         (PsppSheetViewColumn  *tree_column,
473                                                           gboolean            install_handler);
474 void              _pspp_sheet_view_column_get_neighbor_sizes (PsppSheetViewColumn *column,
475                                                             GtkCellRenderer   *cell,
476                                                             gint              *left,
477                                                             gint              *right);
478
479 #endif /* __GTK_TREE_PRIVATE_H__ */
480