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