2 * Copyright (C) 2006, 2008 Free Software Foundation
4 * This version of GtkSheet has been *heavily* modified, for the specific
5 * requirements of PSPPIRE. The changes are copyright by the
6 * Free Software Foundation. The copyright notice for the original work is
10 /* GtkSheet widget for Gtk+.
11 * Copyright (C) 1999-2001 Adrian E. Feiguin <adrian@ifir.ifir.edu.ar>
13 * Based on GtkClist widget by Jay Painter, but major changes.
14 * Memory allocation routines inspired on SC (Spreadsheet Calculator)
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 * @short_description: spreadsheet widget for gtk2
35 * GtkSheet is a matrix widget for GTK+. It consists of an scrollable grid of
36 * cells where you can allocate text. Cell contents can be edited interactively
37 * through a specially designed entry, GtkItemEntry.
47 #include <gdk/gdkkeysyms.h>
48 #include <gtk/gtksignal.h>
49 #include <gtk/gtkbutton.h>
50 #include <gtk/gtkadjustment.h>
51 #include <gtk/gtktypeutils.h>
52 #include <gtk/gtkentry.h>
53 #include <gtk/gtkcontainer.h>
54 #include <pango/pango.h>
56 #include <gtksheet/psppire-marshal.h>
57 #include "gsheetmodel.h"
58 #include <libpspp/misc.h>
64 GTK_SHEET_IN_XDRAG = 1 << 1,
65 GTK_SHEET_IN_YDRAG = 1 << 2,
66 GTK_SHEET_IN_DRAG = 1 << 3,
67 GTK_SHEET_IN_SELECTION = 1 << 4,
68 GTK_SHEET_IN_RESIZE = 1 << 5
71 #define GTK_SHEET_FLAGS(sheet) (GTK_SHEET (sheet)->flags)
72 #define GTK_SHEET_SET_FLAGS(sheet,flag) (GTK_SHEET_FLAGS (sheet) |= (flag))
73 #define GTK_SHEET_UNSET_FLAGS(sheet,flag) (GTK_SHEET_FLAGS (sheet) &= ~ (flag))
75 #define GTK_SHEET_IN_XDRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_XDRAG)
76 #define GTK_SHEET_IN_YDRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_YDRAG)
77 #define GTK_SHEET_IN_DRAG(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_DRAG)
78 #define GTK_SHEET_IN_SELECTION(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_SELECTION)
79 #define GTK_SHEET_IN_RESIZE(sheet) (GTK_SHEET_FLAGS (sheet) & GTK_SHEET_IN_RESIZE)
81 #define CELL_SPACING 1
83 #define TIMEOUT_HOVER 300
84 #define COLUMN_MIN_WIDTH 10
85 #define COLUMN_TITLES_HEIGHT 4
86 #define DEFAULT_COLUMN_WIDTH 80
87 #define DEFAULT_ROW_HEIGHT 25
89 static void set_entry_widget_font (GtkSheet *sheet);
91 static void gtk_sheet_update_primary_selection (GtkSheet *sheet);
92 static void draw_column_title_buttons_range (GtkSheet *sheet, gint first, gint n);
93 static void draw_row_title_buttons_range (GtkSheet *sheet, gint first, gint n);
96 static void gtk_sheet_set_row_height (GtkSheet *sheet,
100 static void destroy_hover_window (GtkSheetHoverTitle *);
101 static GtkSheetHoverTitle *create_hover_window (void);
103 static GtkStateType gtk_sheet_cell_get_state (GtkSheet *sheet, gint row, gint col);
107 dispose_string (const GtkSheet *sheet, gchar *text)
109 GSheetModel *model = gtk_sheet_get_model (sheet);
114 if (g_sheet_model_free_strings (model))
119 /* FIXME: Why bother with these two ? */
121 /* returns the column index from a pixel location */
123 column_from_xpixel (const GtkSheet *sheet, gint pixel)
125 return psppire_axis_get_unit_at_pixel (sheet->haxis, pixel);
129 row_from_ypixel (const GtkSheet *sheet, gint pixel)
131 return psppire_axis_get_unit_at_pixel (sheet->vaxis, pixel);
135 /* Return the lowest row number which is wholly or partially on
136 the visible range of the sheet */
138 min_visible_row (const GtkSheet *sheet)
140 return row_from_ypixel (sheet, sheet->vadjustment->value);
144 min_fully_visible_row (const GtkSheet *sheet)
146 glong row = min_visible_row (sheet);
148 if ( psppire_axis_pixel_start (sheet->vaxis, row) < sheet->vadjustment->value)
155 max_visible_row (const GtkSheet *sheet)
157 return row_from_ypixel (sheet,
158 sheet->vadjustment->value +
159 sheet->vadjustment->page_size);
164 max_fully_visible_row (const GtkSheet *sheet)
166 glong row = max_visible_row (sheet);
168 if ( psppire_axis_pixel_start (sheet->vaxis, row)
170 psppire_axis_unit_size (sheet->vaxis, row)
171 > sheet->vadjustment->value)
178 /* Returns the lowest column number which is wholly or partially
181 min_visible_column (const GtkSheet *sheet)
183 return column_from_xpixel (sheet, sheet->hadjustment->value);
187 min_fully_visible_column (const GtkSheet *sheet)
189 glong col = min_visible_column (sheet);
191 if ( psppire_axis_pixel_start (sheet->haxis, col) < sheet->hadjustment->value)
198 /* Returns the highest column number which is wholly or partially
201 max_visible_column (const GtkSheet *sheet)
203 return column_from_xpixel (sheet,
204 sheet->hadjustment->value +
205 sheet->hadjustment->page_size);
209 max_fully_visible_column (const GtkSheet *sheet)
211 glong col = max_visible_column (sheet);
213 if ( psppire_axis_pixel_start (sheet->haxis, col)
215 psppire_axis_unit_size (sheet->haxis, col)
216 > sheet->hadjustment->value)
224 /* The size of the region (in pixels) around the row/column boundaries
225 where the height/width may be grabbed to change size */
229 on_column_boundary (const GtkSheet *sheet, gint x, gint *column)
233 x += sheet->hadjustment->value;
235 col = column_from_xpixel (sheet, x);
237 if ( column_from_xpixel (sheet, x - DRAG_WIDTH / 2) < col )
243 if ( column_from_xpixel (sheet, x + DRAG_WIDTH / 2) > col )
252 static inline gboolean
253 POSSIBLE_DRAG (const GtkSheet *sheet, gint x, gint y,
254 gint *drag_row, gint *drag_column)
258 /* Can't drag if nothing is selected */
259 if ( sheet->range.row0 < 0 || sheet->range.rowi < 0 ||
260 sheet->range.col0 < 0 || sheet->range.coli < 0 )
263 *drag_column = column_from_xpixel (sheet, x);
264 *drag_row = row_from_ypixel (sheet, y);
266 if (x >= psppire_axis_pixel_start (sheet->haxis, sheet->range.col0) - DRAG_WIDTH / 2 &&
267 x <= psppire_axis_pixel_start (sheet->haxis, sheet->range.coli) +
268 psppire_axis_unit_size (sheet->haxis, sheet->range.coli) + DRAG_WIDTH / 2)
270 ydrag = psppire_axis_pixel_start (sheet->vaxis, sheet->range.row0);
271 if (y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2)
273 *drag_row = sheet->range.row0;
276 ydrag = psppire_axis_pixel_start (sheet->vaxis, sheet->range.rowi) +
277 psppire_axis_unit_size (sheet->vaxis, sheet->range.rowi);
278 if (y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2)
280 *drag_row = sheet->range.rowi;
285 if (y >= psppire_axis_pixel_start (sheet->vaxis, sheet->range.row0) - DRAG_WIDTH / 2 &&
286 y <= psppire_axis_pixel_start (sheet->vaxis, sheet->range.rowi) +
287 psppire_axis_unit_size (sheet->vaxis, sheet->range.rowi) + DRAG_WIDTH / 2)
289 xdrag = psppire_axis_pixel_start (sheet->haxis, sheet->range.col0);
290 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2)
292 *drag_column = sheet->range.col0;
295 xdrag = psppire_axis_pixel_start (sheet->haxis, sheet->range.coli) +
296 psppire_axis_unit_size (sheet->haxis, sheet->range.coli);
297 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2)
299 *drag_column = sheet->range.coli;
307 static inline gboolean
308 POSSIBLE_RESIZE (const GtkSheet *sheet, gint x, gint y,
309 gint *drag_row, gint *drag_column)
313 /* Can't drag if nothing is selected */
314 if ( sheet->range.row0 < 0 || sheet->range.rowi < 0 ||
315 sheet->range.col0 < 0 || sheet->range.coli < 0 )
318 xdrag = psppire_axis_pixel_start (sheet->haxis, sheet->range.coli)+
319 psppire_axis_unit_size (sheet->haxis, sheet->range.coli);
321 ydrag = psppire_axis_pixel_start (sheet->vaxis, sheet->range.rowi) +
322 psppire_axis_unit_size (sheet->vaxis, sheet->range.rowi);
324 if (sheet->state == GTK_SHEET_COLUMN_SELECTED)
325 ydrag = psppire_axis_pixel_start (sheet->vaxis, min_visible_row (sheet));
327 if (sheet->state == GTK_SHEET_ROW_SELECTED)
328 xdrag = psppire_axis_pixel_start (sheet->haxis, min_visible_column (sheet));
330 *drag_column = column_from_xpixel (sheet, x);
331 *drag_row = row_from_ypixel (sheet, y);
333 if (x >= xdrag - DRAG_WIDTH / 2 && x <= xdrag + DRAG_WIDTH / 2 &&
334 y >= ydrag - DRAG_WIDTH / 2 && y <= ydrag + DRAG_WIDTH / 2) return TRUE;
341 rectangle_from_range (GtkSheet *sheet, const GtkSheetRange *range,
344 g_return_val_if_fail (range, FALSE);
346 r->x = psppire_axis_pixel_start (sheet->haxis, range->col0);
347 r->x -= round (sheet->hadjustment->value);
349 if ( sheet->row_titles_visible)
350 r->x += sheet->row_title_area.width;
353 r->y = psppire_axis_pixel_start (sheet->vaxis, range->row0);
354 r->y -= round (sheet->vadjustment->value);
356 if ( sheet->column_titles_visible)
357 r->y += sheet->column_title_area.height;
359 r->width = psppire_axis_pixel_start (sheet->haxis, range->coli) -
360 psppire_axis_pixel_start (sheet->haxis, range->col0) +
361 psppire_axis_unit_size (sheet->haxis, range->coli);
363 r->height = psppire_axis_pixel_start (sheet->vaxis, range->rowi) -
364 psppire_axis_pixel_start (sheet->vaxis, range->row0) +
365 psppire_axis_unit_size (sheet->vaxis, range->rowi);
371 rectangle_from_cell (GtkSheet *sheet, gint row, gint col,
375 g_return_val_if_fail (row >= 0, FALSE);
376 g_return_val_if_fail (col >= 0, FALSE);
378 range.row0 = range.rowi = row;
379 range.col0 = range.coli = col;
381 return rectangle_from_range (sheet, &range, r);
385 static void gtk_sheet_class_init (GtkSheetClass *klass);
386 static void gtk_sheet_init (GtkSheet *sheet);
387 static void gtk_sheet_dispose (GObject *object);
388 static void gtk_sheet_finalize (GObject *object);
389 static void gtk_sheet_style_set (GtkWidget *widget,
390 GtkStyle *previous_style);
391 static void gtk_sheet_realize (GtkWidget *widget);
392 static void gtk_sheet_unrealize (GtkWidget *widget);
393 static void gtk_sheet_map (GtkWidget *widget);
394 static void gtk_sheet_unmap (GtkWidget *widget);
395 static gint gtk_sheet_expose (GtkWidget *widget,
396 GdkEventExpose *event);
398 static void gtk_sheet_forall (GtkContainer *container,
399 gboolean include_internals,
400 GtkCallback callback,
401 gpointer callback_data);
403 static void gtk_sheet_set_scroll_adjustments (GtkSheet *sheet,
404 GtkAdjustment *hadjustment,
405 GtkAdjustment *vadjustment);
407 static gint gtk_sheet_button_press (GtkWidget *widget,
408 GdkEventButton *event);
409 static gint gtk_sheet_button_release (GtkWidget *widget,
410 GdkEventButton *event);
411 static gint gtk_sheet_motion (GtkWidget *widget,
412 GdkEventMotion *event);
413 static gboolean gtk_sheet_crossing_notify (GtkWidget *widget,
414 GdkEventCrossing *event);
415 static gint gtk_sheet_entry_key_press (GtkWidget *widget,
417 static gboolean gtk_sheet_key_press (GtkWidget *widget,
419 static void gtk_sheet_size_request (GtkWidget *widget,
420 GtkRequisition *requisition);
421 static void gtk_sheet_size_allocate (GtkWidget *widget,
422 GtkAllocation *allocation);
426 static gboolean gtk_sheet_range_isvisible (const GtkSheet *sheet,
427 const GtkSheetRange *range);
428 static gboolean gtk_sheet_cell_isvisible (GtkSheet *sheet,
429 gint row, gint column);
430 /* Drawing Routines */
433 static void gtk_sheet_cell_draw (GtkSheet *sheet, gint row, gint column);
436 /* draw visible part of range. If range == NULL then draw the whole screen */
437 static void gtk_sheet_range_draw (GtkSheet *sheet,
438 const GtkSheetRange *range);
440 /* highlight the visible part of the selected range */
441 static void gtk_sheet_range_draw_selection (GtkSheet *sheet,
442 GtkSheetRange range);
446 static void gtk_sheet_real_select_range (GtkSheet *sheet,
447 const GtkSheetRange *range);
448 static void gtk_sheet_real_unselect_range (GtkSheet *sheet,
449 const GtkSheetRange *range);
450 static void gtk_sheet_extend_selection (GtkSheet *sheet,
451 gint row, gint column);
452 static void gtk_sheet_new_selection (GtkSheet *sheet,
453 GtkSheetRange *range);
454 static void gtk_sheet_draw_border (GtkSheet *sheet,
455 GtkSheetRange range);
457 /* Active Cell handling */
459 static void gtk_sheet_entry_changed (GtkWidget *widget,
461 static void gtk_sheet_hide_entry_widget (GtkSheet *sheet);
462 static void change_active_cell (GtkSheet *sheet,
464 static void gtk_sheet_draw_active_cell (GtkSheet *sheet);
465 static void gtk_sheet_show_entry_widget (GtkSheet *sheet);
466 static gboolean gtk_sheet_click_cell (GtkSheet *sheet,
473 static void adjust_scrollbars (GtkSheet *sheet);
474 static void vadjustment_value_changed (GtkAdjustment *adjustment,
476 static void hadjustment_value_changed (GtkAdjustment *adjustment,
480 static void draw_xor_vline (GtkSheet *sheet);
481 static void draw_xor_hline (GtkSheet *sheet);
482 static void draw_xor_rectangle (GtkSheet *sheet,
483 GtkSheetRange range);
485 static guint new_column_width (GtkSheet *sheet,
488 static guint new_row_height (GtkSheet *sheet,
493 static void create_global_button (GtkSheet *sheet);
494 static void global_button_clicked (GtkWidget *widget,
498 static void create_sheet_entry (GtkSheet *sheet);
499 static void gtk_sheet_size_allocate_entry (GtkSheet *sheet);
501 /* Sheet button gadgets */
503 static void draw_column_title_buttons (GtkSheet *sheet);
504 static void draw_row_title_buttons (GtkSheet *sheet);
507 static void size_allocate_global_button (GtkSheet *sheet);
508 static void gtk_sheet_button_size_request (GtkSheet *sheet,
509 const GtkSheetButton *button,
510 GtkRequisition *requisition);
512 static void gtk_sheet_real_cell_clear (GtkSheet *sheet,
517 static void gtk_sheet_column_size_request (GtkSheet *sheet,
520 static void gtk_sheet_row_size_request (GtkSheet *sheet,
542 static GtkContainerClass *parent_class = NULL;
543 static guint sheet_signals[LAST_SIGNAL] = { 0 };
547 gtk_sheet_get_type ()
549 static GType sheet_type = 0;
553 static const GTypeInfo sheet_info =
555 sizeof (GtkSheetClass),
558 (GClassInitFunc) gtk_sheet_class_init,
563 (GInstanceInitFunc) gtk_sheet_init,
568 g_type_register_static (GTK_TYPE_BIN, "GtkSheet",
576 static GtkSheetRange*
577 gtk_sheet_range_copy (const GtkSheetRange *range)
579 GtkSheetRange *new_range;
581 g_return_val_if_fail (range != NULL, NULL);
583 new_range = g_new (GtkSheetRange, 1);
591 gtk_sheet_range_free (GtkSheetRange *range)
593 g_return_if_fail (range != NULL);
599 gtk_sheet_range_get_type (void)
601 static GType sheet_range_type = 0;
603 if (!sheet_range_type)
606 g_boxed_type_register_static ("GtkSheetRange",
607 (GBoxedCopyFunc) gtk_sheet_range_copy,
608 (GBoxedFreeFunc) gtk_sheet_range_free);
611 return sheet_range_type;
615 gtk_sheet_cell_copy (const GtkSheetCell *cell)
617 GtkSheetCell *new_cell;
619 g_return_val_if_fail (cell != NULL, NULL);
621 new_cell = g_new (GtkSheetCell, 1);
629 gtk_sheet_cell_free (GtkSheetCell *cell)
631 g_return_if_fail (cell != NULL);
637 gtk_sheet_cell_get_type (void)
639 static GType sheet_cell_type = 0;
641 if (!sheet_cell_type)
644 g_boxed_type_register_static ("GtkSheetCell",
645 (GBoxedCopyFunc) gtk_sheet_cell_copy,
646 (GBoxedFreeFunc) gtk_sheet_cell_free);
649 return sheet_cell_type;
663 gtk_sheet_set_horizontal_axis (GtkSheet *sheet, PsppireAxis *a)
666 g_object_unref (sheet->haxis);
671 g_object_ref (sheet->haxis);
675 gtk_sheet_set_vertical_axis (GtkSheet *sheet, PsppireAxis *a)
678 g_object_unref (sheet->vaxis);
683 g_object_ref (sheet->vaxis);
688 gtk_sheet_set_property (GObject *object,
694 GtkSheet *sheet = GTK_SHEET (object);
699 gtk_sheet_set_vertical_axis (sheet, g_value_get_pointer (value));
702 gtk_sheet_set_horizontal_axis (sheet, g_value_get_pointer (value));
705 gtk_sheet_set_model (sheet, g_value_get_pointer (value));
708 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
714 gtk_sheet_get_property (GObject *object,
719 GtkSheet *sheet = GTK_SHEET (object);
724 g_value_set_pointer (value, sheet->vaxis);
727 g_value_set_pointer (value, sheet->haxis);
730 g_value_set_pointer (value, sheet->model);
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
740 gtk_sheet_class_init (GtkSheetClass *klass)
742 GObjectClass *object_class = G_OBJECT_CLASS (klass);
744 GParamSpec *haxis_spec ;
745 GParamSpec *vaxis_spec ;
746 GParamSpec *model_spec ;
748 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
749 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
751 parent_class = g_type_class_peek_parent (klass);
754 * GtkSheet::select-row
755 * @sheet: the sheet widget that emitted the signal
756 * @row: the newly selected row index
758 * A row has been selected.
760 sheet_signals[SELECT_ROW] =
761 g_signal_new ("select-row",
762 G_TYPE_FROM_CLASS (object_class),
764 offsetof (GtkSheetClass, select_row),
766 g_cclosure_marshal_VOID__INT,
773 * GtkSheet::select - column
774 * @sheet: the sheet widget that emitted the signal
775 * @column: the newly selected column index
777 * A column has been selected.
779 sheet_signals[SELECT_COLUMN] =
780 g_signal_new ("select-column",
781 G_TYPE_FROM_CLASS (object_class),
783 offsetof (GtkSheetClass, select_column),
785 g_cclosure_marshal_VOID__INT,
792 * GtkSheet::double-click-row
793 * @sheet: the sheet widget that emitted the signal
794 * @row: the row that was double clicked.
796 * A row's title button has been double clicked
798 sheet_signals[DOUBLE_CLICK_ROW] =
799 g_signal_new ("double-click-row",
800 G_TYPE_FROM_CLASS (object_class),
804 g_cclosure_marshal_VOID__INT,
811 * GtkSheet::double-click-column
812 * @sheet: the sheet widget that emitted the signal
813 * @column: the column that was double clicked.
815 * A column's title button has been double clicked
817 sheet_signals[DOUBLE_CLICK_COLUMN] =
818 g_signal_new ("double-click-column",
819 G_TYPE_FROM_CLASS (object_class),
823 g_cclosure_marshal_VOID__INT,
830 * GtkSheet::button-event-column
831 * @sheet: the sheet widget that emitted the signal
832 * @column: the column on which the event occured.
834 * A button event occured on a column title button
836 sheet_signals[BUTTON_EVENT_COLUMN] =
837 g_signal_new ("button-event-column",
838 G_TYPE_FROM_CLASS (object_class),
842 psppire_marshal_VOID__INT_POINTER,
851 * GtkSheet::button-event-row
852 * @sheet: the sheet widget that emitted the signal
853 * @column: the column on which the event occured.
855 * A button event occured on a row title button
857 sheet_signals[BUTTON_EVENT_ROW] =
858 g_signal_new ("button-event-row",
859 G_TYPE_FROM_CLASS (object_class),
863 psppire_marshal_VOID__INT_POINTER,
871 sheet_signals[SELECT_RANGE] =
872 g_signal_new ("select-range",
873 G_TYPE_FROM_CLASS (object_class),
875 offsetof (GtkSheetClass, select_range),
877 g_cclosure_marshal_VOID__BOXED,
880 GTK_TYPE_SHEET_RANGE);
883 sheet_signals[RESIZE_RANGE] =
884 g_signal_new ("resize-range",
885 G_TYPE_FROM_CLASS (object_class),
887 offsetof (GtkSheetClass, resize_range),
889 psppire_marshal_VOID__BOXED_BOXED,
892 GTK_TYPE_SHEET_RANGE, GTK_TYPE_SHEET_RANGE
895 sheet_signals[MOVE_RANGE] =
896 g_signal_new ("move-range",
897 G_TYPE_FROM_CLASS (object_class),
899 offsetof (GtkSheetClass, move_range),
901 psppire_marshal_VOID__BOXED_BOXED,
904 GTK_TYPE_SHEET_RANGE, GTK_TYPE_SHEET_RANGE
907 sheet_signals[TRAVERSE] =
908 g_signal_new ("traverse",
909 G_TYPE_FROM_CLASS (object_class),
911 offsetof (GtkSheetClass, traverse),
913 psppire_marshal_BOOLEAN__BOXED_POINTER,
919 sheet_signals[ACTIVATE] =
920 g_signal_new ("activate",
921 G_TYPE_FROM_CLASS (object_class),
923 offsetof (GtkSheetClass, activate),
925 psppire_marshal_VOID__INT_INT_INT_INT,
927 G_TYPE_INT, G_TYPE_INT,
928 G_TYPE_INT, G_TYPE_INT);
930 widget_class->set_scroll_adjustments_signal =
931 g_signal_new ("set-scroll-adjustments",
932 G_TYPE_FROM_CLASS (object_class),
934 offsetof (GtkSheetClass, set_scroll_adjustments),
936 psppire_marshal_VOID__OBJECT_OBJECT,
937 G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
940 container_class->add = NULL;
941 container_class->remove = NULL;
942 container_class->forall = gtk_sheet_forall;
944 object_class->dispose = gtk_sheet_dispose;
945 object_class->finalize = gtk_sheet_finalize;
949 g_param_spec_pointer ("vertical-axis",
951 "A pointer to the PsppireAxis object for the rows",
952 G_PARAM_READABLE | G_PARAM_WRITABLE );
955 g_param_spec_pointer ("horizontal-axis",
957 "A pointer to the PsppireAxis object for the columns",
958 G_PARAM_READABLE | G_PARAM_WRITABLE );
961 g_param_spec_pointer ("model",
963 "A pointer to the data model",
964 G_PARAM_READABLE | G_PARAM_WRITABLE );
967 object_class->set_property = gtk_sheet_set_property;
968 object_class->get_property = gtk_sheet_get_property;
970 g_object_class_install_property (object_class,
974 g_object_class_install_property (object_class,
978 g_object_class_install_property (object_class,
983 widget_class->realize = gtk_sheet_realize;
984 widget_class->unrealize = gtk_sheet_unrealize;
985 widget_class->map = gtk_sheet_map;
986 widget_class->unmap = gtk_sheet_unmap;
987 widget_class->style_set = gtk_sheet_style_set;
988 widget_class->button_press_event = gtk_sheet_button_press;
989 widget_class->button_release_event = gtk_sheet_button_release;
990 widget_class->motion_notify_event = gtk_sheet_motion;
991 widget_class->enter_notify_event = gtk_sheet_crossing_notify;
992 widget_class->leave_notify_event = gtk_sheet_crossing_notify;
993 widget_class->key_press_event = gtk_sheet_key_press;
994 widget_class->expose_event = gtk_sheet_expose;
995 widget_class->size_request = gtk_sheet_size_request;
996 widget_class->size_allocate = gtk_sheet_size_allocate;
997 widget_class->focus_in_event = NULL;
998 widget_class->focus_out_event = NULL;
1000 klass->set_scroll_adjustments = gtk_sheet_set_scroll_adjustments;
1001 klass->select_row = NULL;
1002 klass->select_column = NULL;
1003 klass->select_range = NULL;
1004 klass->resize_range = NULL;
1005 klass->move_range = NULL;
1006 klass->traverse = NULL;
1007 klass->activate = NULL;
1008 klass->changed = NULL;
1012 gtk_sheet_init (GtkSheet *sheet)
1014 sheet->model = NULL;
1015 sheet->haxis = NULL;
1016 sheet->vaxis = NULL;
1019 sheet->selection_mode = GTK_SELECTION_NONE;
1020 sheet->state = GTK_SHEET_NORMAL;
1022 GTK_WIDGET_UNSET_FLAGS (sheet, GTK_NO_WINDOW);
1023 GTK_WIDGET_SET_FLAGS (sheet, GTK_CAN_FOCUS);
1025 sheet->column_title_window = NULL;
1026 sheet->column_title_area.x = 0;
1027 sheet->column_title_area.y = 0;
1028 sheet->column_title_area.width = 0;
1029 sheet->column_title_area.height = DEFAULT_ROW_HEIGHT;
1031 sheet->row_title_window = NULL;
1032 sheet->row_title_area.x = 0;
1033 sheet->row_title_area.y = 0;
1034 sheet->row_title_area.width = DEFAULT_COLUMN_WIDTH;
1035 sheet->row_title_area.height = 0;
1038 sheet->active_cell.row = 0;
1039 sheet->active_cell.col = 0;
1040 sheet->selection_cell.row = 0;
1041 sheet->selection_cell.col = 0;
1043 sheet->range.row0 = 0;
1044 sheet->range.rowi = 0;
1045 sheet->range.col0 = 0;
1046 sheet->range.coli = 0;
1048 sheet->state = GTK_SHEET_NORMAL;
1050 sheet->sheet_window = NULL;
1051 sheet->entry_widget = NULL;
1052 sheet->entry_handler_id = 0;
1053 sheet->button = NULL;
1055 sheet->hadjustment = NULL;
1056 sheet->vadjustment = NULL;
1058 sheet->cursor_drag = NULL;
1060 sheet->xor_gc = NULL;
1061 sheet->fg_gc = NULL;
1062 sheet->bg_gc = NULL;
1065 sheet->show_grid = TRUE;
1067 sheet->motion_timer = 0;
1069 sheet->columns_resizable = TRUE;
1070 sheet->rows_resizable = TRUE;
1072 sheet->row_titles_visible = TRUE;
1073 sheet->row_title_area.width = DEFAULT_COLUMN_WIDTH;
1075 sheet->column_titles_visible = TRUE;
1078 /* create sheet entry */
1079 sheet->entry_type = GTK_TYPE_ENTRY;
1080 create_sheet_entry (sheet);
1082 /* create global selection button */
1083 create_global_button (sheet);
1087 /* Callback which occurs whenever columns are inserted / deleted in the model */
1089 columns_inserted_deleted_callback (GSheetModel *model, gint first_column,
1093 GtkSheet *sheet = GTK_SHEET (data);
1095 GtkSheetRange range;
1096 gint model_columns = g_sheet_model_get_column_count (model);
1099 /* Need to update all the columns starting from the first column and onwards.
1100 * Previous column are unchanged, so don't need to be updated.
1102 range.col0 = first_column;
1104 range.coli = psppire_axis_unit_count (sheet->haxis) - 1;
1105 range.rowi = psppire_axis_unit_count (sheet->vaxis) - 1;
1107 adjust_scrollbars (sheet);
1109 if (sheet->active_cell.col >= model_columns)
1110 change_active_cell (sheet, sheet->active_cell.row, model_columns - 1);
1112 draw_column_title_buttons_range (sheet,
1113 first_column, max_visible_column (sheet));
1115 gtk_sheet_range_draw (sheet, &range);
1119 /* Callback which occurs whenever rows are inserted / deleted in the model */
1121 rows_inserted_deleted_callback (GSheetModel *model, gint first_row,
1122 gint n_rows, gpointer data)
1124 GtkSheet *sheet = GTK_SHEET (data);
1126 GtkSheetRange range;
1128 gint model_rows = g_sheet_model_get_row_count (model);
1130 /* Need to update all the rows starting from the first row and onwards.
1131 * Previous rows are unchanged, so don't need to be updated.
1133 range.row0 = first_row;
1135 range.rowi = psppire_axis_unit_count (sheet->vaxis) - 1;
1136 range.coli = psppire_axis_unit_count (sheet->haxis) - 1;
1138 adjust_scrollbars (sheet);
1140 if (sheet->active_cell.row >= model_rows)
1141 change_active_cell (sheet, model_rows - 1, sheet->active_cell.col);
1143 draw_row_title_buttons_range (sheet, first_row, max_visible_row (sheet));
1145 gtk_sheet_range_draw (sheet, &range);
1149 If row0 or rowi are negative, then all rows will be updated.
1150 If col0 or coli are negative, then all columns will be updated.
1153 range_update_callback (GSheetModel *m, gint row0, gint col0,
1154 gint rowi, gint coli, gpointer data)
1156 GtkSheet *sheet = GTK_SHEET (data);
1158 GtkSheetRange range;
1165 if ( !GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1168 if ( max_visible_row (sheet) > g_sheet_model_get_row_count (sheet->model)
1170 max_visible_column (sheet) > g_sheet_model_get_column_count (sheet->model))
1172 gtk_sheet_moveto (sheet, 0, 0, 0, 0);
1175 if ( ( row0 < 0 && col0 < 0 ) || ( rowi < 0 && coli < 0 ) )
1177 gtk_sheet_range_draw (sheet, NULL);
1178 adjust_scrollbars (sheet);
1180 draw_row_title_buttons_range (sheet, min_visible_row (sheet),
1181 max_visible_row (sheet));
1183 draw_column_title_buttons_range (sheet, min_visible_column (sheet),
1184 max_visible_column (sheet));
1188 else if ( row0 < 0 || rowi < 0 )
1190 range.row0 = min_visible_row (sheet);
1191 range.rowi = max_visible_row (sheet);
1193 else if ( col0 < 0 || coli < 0 )
1195 range.col0 = min_visible_column (sheet);
1196 range.coli = max_visible_column (sheet);
1199 gtk_sheet_range_draw (sheet, &range);
1205 * @rows: initial number of rows
1206 * @columns: initial number of columns
1207 * @title: sheet title
1208 * @model: the model to use for the sheet data
1210 * Creates a new sheet widget with the given number of rows and columns.
1212 * Returns: the new sheet widget
1215 gtk_sheet_new (GSheetModel *model)
1217 GtkWidget *widget = g_object_new (GTK_TYPE_SHEET,
1225 * gtk_sheet_set_model
1226 * @sheet: the sheet to set the model for
1227 * @model: the model to use for the sheet data
1229 * Sets the model for a GtkSheet
1233 gtk_sheet_set_model (GtkSheet *sheet, GSheetModel *model)
1235 g_return_if_fail (GTK_IS_SHEET (sheet));
1237 if (sheet->model ) g_object_unref (sheet->model);
1239 sheet->model = model;
1243 g_object_ref (model);
1245 g_signal_connect (model, "range_changed",
1246 G_CALLBACK (range_update_callback), sheet);
1248 g_signal_connect (model, "rows_inserted",
1249 G_CALLBACK (rows_inserted_deleted_callback), sheet);
1251 g_signal_connect (model, "rows_deleted",
1252 G_CALLBACK (rows_inserted_deleted_callback), sheet);
1254 g_signal_connect (model, "columns_inserted",
1255 G_CALLBACK (columns_inserted_deleted_callback), sheet);
1257 g_signal_connect (model, "columns_deleted",
1258 G_CALLBACK (columns_inserted_deleted_callback), sheet);
1264 gtk_sheet_change_entry (GtkSheet *sheet, GtkType entry_type)
1268 g_return_if_fail (sheet != NULL);
1269 g_return_if_fail (GTK_IS_SHEET (sheet));
1271 state = sheet->state;
1273 if (sheet->state == GTK_SHEET_NORMAL)
1274 gtk_sheet_hide_entry_widget (sheet);
1276 sheet->entry_type = entry_type;
1278 create_sheet_entry (sheet);
1280 if (state == GTK_SHEET_NORMAL)
1282 gtk_sheet_show_entry_widget (sheet);
1288 gtk_sheet_show_grid (GtkSheet *sheet, gboolean show)
1290 g_return_if_fail (sheet != NULL);
1291 g_return_if_fail (GTK_IS_SHEET (sheet));
1293 if (show == sheet->show_grid) return;
1295 sheet->show_grid = show;
1297 gtk_sheet_range_draw (sheet, NULL);
1301 gtk_sheet_grid_visible (GtkSheet *sheet)
1303 g_return_val_if_fail (sheet != NULL, 0);
1304 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1306 return sheet->show_grid;
1310 gtk_sheet_get_columns_count (GtkSheet *sheet)
1312 g_return_val_if_fail (sheet != NULL, 0);
1313 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
1315 return psppire_axis_unit_count (sheet->haxis);
1319 gtk_sheet_set_column_width (GtkSheet *sheet,
1325 gtk_sheet_show_column_titles (GtkSheet *sheet)
1327 if (sheet->column_titles_visible) return;
1329 sheet->column_titles_visible = TRUE;
1331 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1334 gdk_window_show (sheet->column_title_window);
1335 gdk_window_move_resize (sheet->column_title_window,
1336 sheet->column_title_area.x,
1337 sheet->column_title_area.y,
1338 sheet->column_title_area.width,
1339 sheet->column_title_area.height);
1341 adjust_scrollbars (sheet);
1343 if (sheet->vadjustment)
1344 g_signal_emit_by_name (sheet->vadjustment,
1346 size_allocate_global_button (sheet);
1351 gtk_sheet_show_row_titles (GtkSheet *sheet)
1353 if (sheet->row_titles_visible) return;
1355 sheet->row_titles_visible = TRUE;
1358 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1360 gdk_window_show (sheet->row_title_window);
1361 gdk_window_move_resize (sheet->row_title_window,
1362 sheet->row_title_area.x,
1363 sheet->row_title_area.y,
1364 sheet->row_title_area.width,
1365 sheet->row_title_area.height);
1367 adjust_scrollbars (sheet);
1370 if (sheet->hadjustment)
1371 g_signal_emit_by_name (sheet->hadjustment,
1373 size_allocate_global_button (sheet);
1377 gtk_sheet_hide_column_titles (GtkSheet *sheet)
1379 if (!sheet->column_titles_visible) return;
1381 sheet->column_titles_visible = FALSE;
1383 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1385 if (sheet->column_title_window)
1386 gdk_window_hide (sheet->column_title_window);
1387 if (GTK_WIDGET_VISIBLE (sheet->button))
1388 gtk_widget_hide (sheet->button);
1390 adjust_scrollbars (sheet);
1393 if (sheet->vadjustment)
1394 g_signal_emit_by_name (sheet->vadjustment,
1399 gtk_sheet_hide_row_titles (GtkSheet *sheet)
1401 if (!sheet->row_titles_visible) return;
1403 sheet->row_titles_visible = FALSE;
1405 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
1407 if (sheet->row_title_window)
1408 gdk_window_hide (sheet->row_title_window);
1410 if (GTK_WIDGET_VISIBLE (sheet->button))
1411 gtk_widget_hide (sheet->button);
1413 adjust_scrollbars (sheet);
1416 if (sheet->hadjustment)
1417 g_signal_emit_by_name (sheet->hadjustment,
1422 /* Scroll the sheet so that the cell ROW, COLUMN is visible.
1423 If {ROW,COL}_ALIGN is zero, then the cell will be placed
1424 at the {top,left} of the sheet. If it's 1, then it'll
1425 be placed at the {bottom,right}.
1426 ROW or COL may be -1, in which case scrolling in that dimension
1430 gtk_sheet_moveto (GtkSheet *sheet,
1438 g_return_if_fail (row_align >= 0);
1439 g_return_if_fail (col_align >= 0);
1441 g_return_if_fail (row_align <= 1);
1442 g_return_if_fail (col_align <= 1);
1444 g_return_if_fail (col <
1445 psppire_axis_unit_count (sheet->haxis));
1446 g_return_if_fail (row <
1447 psppire_axis_unit_count (sheet->vaxis));
1449 gdk_drawable_get_size (sheet->sheet_window, &width, &height);
1454 gint y = psppire_axis_pixel_start (sheet->vaxis, row);
1456 gtk_adjustment_set_value (sheet->vadjustment, y - height * row_align);
1462 gint x = psppire_axis_pixel_start (sheet->haxis, col);
1464 gtk_adjustment_set_value (sheet->hadjustment, x - width * col_align);
1470 gtk_sheet_columns_resizable (GtkSheet *sheet)
1472 g_return_val_if_fail (sheet != NULL, FALSE);
1473 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1475 return sheet->columns_resizable;
1480 gtk_sheet_rows_resizable (GtkSheet *sheet)
1482 g_return_val_if_fail (sheet != NULL, FALSE);
1483 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
1485 return sheet->rows_resizable;
1490 gtk_sheet_select_row (GtkSheet *sheet, gint row)
1492 g_return_if_fail (sheet != NULL);
1493 g_return_if_fail (GTK_IS_SHEET (sheet));
1495 if (row < 0 || row >= psppire_axis_unit_count (sheet->vaxis))
1498 if (sheet->state != GTK_SHEET_NORMAL)
1499 gtk_sheet_real_unselect_range (sheet, NULL);
1501 sheet->state = GTK_SHEET_ROW_SELECTED;
1502 sheet->range.row0 = row;
1503 sheet->range.col0 = 0;
1504 sheet->range.rowi = row;
1505 sheet->range.coli = psppire_axis_unit_count (sheet->haxis) - 1;
1506 sheet->active_cell.row = row;
1507 sheet->active_cell.col = 0;
1509 g_signal_emit (sheet, sheet_signals[SELECT_ROW], 0, row);
1510 gtk_sheet_real_select_range (sheet, NULL);
1515 gtk_sheet_select_column (GtkSheet *sheet, gint column)
1517 g_return_if_fail (sheet != NULL);
1518 g_return_if_fail (GTK_IS_SHEET (sheet));
1520 if (column < 0 || column >= psppire_axis_unit_count (sheet->haxis))
1523 if (sheet->state != GTK_SHEET_NORMAL)
1524 gtk_sheet_real_unselect_range (sheet, NULL);
1526 sheet->state = GTK_SHEET_COLUMN_SELECTED;
1527 sheet->range.row0 = 0;
1528 sheet->range.col0 = column;
1529 sheet->range.rowi = psppire_axis_unit_count (sheet->vaxis) - 1;
1530 sheet->range.coli = column;
1531 sheet->active_cell.row = 0;
1532 sheet->active_cell.col = column;
1534 g_signal_emit (sheet, sheet_signals[SELECT_COLUMN], 0, column);
1535 gtk_sheet_real_select_range (sheet, NULL);
1542 gtk_sheet_range_isvisible (const GtkSheet *sheet,
1543 const GtkSheetRange *range)
1545 g_return_val_if_fail (sheet != NULL, FALSE);
1547 if (range->row0 < 0 || range->row0 >= psppire_axis_unit_count (sheet->vaxis))
1550 if (range->rowi < 0 || range->rowi >= psppire_axis_unit_count (sheet->vaxis))
1553 if (range->col0 < 0 || range->col0 >= psppire_axis_unit_count (sheet->haxis))
1556 if (range->coli < 0 || range->coli >= psppire_axis_unit_count (sheet->haxis))
1559 if (range->rowi < min_visible_row (sheet))
1562 if (range->row0 > max_visible_row (sheet))
1565 if (range->coli < min_visible_column (sheet))
1568 if (range->col0 > max_visible_column (sheet))
1575 gtk_sheet_cell_isvisible (GtkSheet *sheet,
1576 gint row, gint column)
1578 GtkSheetRange range;
1581 range.col0 = column;
1583 range.coli = column;
1585 return gtk_sheet_range_isvisible (sheet, &range);
1589 gtk_sheet_get_visible_range (GtkSheet *sheet, GtkSheetRange *range)
1591 g_return_if_fail (sheet != NULL);
1592 g_return_if_fail (GTK_IS_SHEET (sheet)) ;
1593 g_return_if_fail (range != NULL);
1595 range->row0 = min_visible_row (sheet);
1596 range->col0 = min_visible_column (sheet);
1597 range->rowi = max_visible_row (sheet);
1598 range->coli = max_visible_column (sheet);
1603 gtk_sheet_set_scroll_adjustments (GtkSheet *sheet,
1604 GtkAdjustment *hadjustment,
1605 GtkAdjustment *vadjustment)
1607 if ( sheet->vadjustment != vadjustment )
1609 if (sheet->vadjustment)
1610 g_object_unref (sheet->vadjustment);
1611 sheet->vadjustment = vadjustment;
1612 g_object_ref (vadjustment);
1614 g_signal_connect (sheet->vadjustment, "value_changed",
1615 G_CALLBACK (vadjustment_value_changed),
1619 if ( sheet->hadjustment != hadjustment )
1621 if (sheet->hadjustment)
1622 g_object_unref (sheet->hadjustment);
1623 sheet->hadjustment = hadjustment;
1624 g_object_ref (hadjustment);
1626 g_signal_connect (sheet->hadjustment, "value_changed",
1627 G_CALLBACK (hadjustment_value_changed),
1633 gtk_sheet_finalize (GObject *object)
1637 g_return_if_fail (object != NULL);
1638 g_return_if_fail (GTK_IS_SHEET (object));
1640 sheet = GTK_SHEET (object);
1642 if (G_OBJECT_CLASS (parent_class)->finalize)
1643 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
1647 gtk_sheet_dispose (GObject *object)
1649 GtkSheet *sheet = GTK_SHEET (object);
1651 g_return_if_fail (object != NULL);
1652 g_return_if_fail (GTK_IS_SHEET (object));
1654 if ( sheet->dispose_has_run )
1657 sheet->dispose_has_run = TRUE;
1659 if (sheet->model) g_object_unref (sheet->model);
1660 if (sheet->vaxis) g_object_unref (sheet->vaxis);
1661 if (sheet->haxis) g_object_unref (sheet->haxis);
1663 g_object_unref (sheet->button);
1664 sheet->button = NULL;
1666 /* unref adjustments */
1667 if (sheet->hadjustment)
1669 g_signal_handlers_disconnect_matched (sheet->hadjustment,
1670 G_SIGNAL_MATCH_DATA,
1674 g_object_unref (sheet->hadjustment);
1675 sheet->hadjustment = NULL;
1678 if (sheet->vadjustment)
1680 g_signal_handlers_disconnect_matched (sheet->vadjustment,
1681 G_SIGNAL_MATCH_DATA,
1685 g_object_unref (sheet->vadjustment);
1687 sheet->vadjustment = NULL;
1690 if (G_OBJECT_CLASS (parent_class)->dispose)
1691 (*G_OBJECT_CLASS (parent_class)->dispose) (object);
1695 gtk_sheet_style_set (GtkWidget *widget,
1696 GtkStyle *previous_style)
1700 g_return_if_fail (widget != NULL);
1701 g_return_if_fail (GTK_IS_SHEET (widget));
1703 if (GTK_WIDGET_CLASS (parent_class)->style_set)
1704 (*GTK_WIDGET_CLASS (parent_class)->style_set) (widget, previous_style);
1706 sheet = GTK_SHEET (widget);
1708 if (GTK_WIDGET_REALIZED (widget))
1710 gtk_style_set_background (widget->style, widget->window, widget->state);
1713 set_entry_widget_font (sheet);
1716 #define BORDER_WIDTH 2
1719 gtk_sheet_realize (GtkWidget *widget)
1722 GdkWindowAttr attributes;
1723 const gint attributes_mask =
1724 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_CURSOR;
1727 GdkColormap *colormap;
1728 GdkDisplay *display;
1730 g_return_if_fail (widget != NULL);
1731 g_return_if_fail (GTK_IS_SHEET (widget));
1733 sheet = GTK_SHEET (widget);
1735 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1737 colormap = gtk_widget_get_colormap (widget);
1738 display = gtk_widget_get_display (widget);
1740 attributes.window_type = GDK_WINDOW_CHILD;
1741 attributes.x = widget->allocation.x;
1742 attributes.y = widget->allocation.y;
1743 attributes.width = widget->allocation.width;
1744 attributes.height = widget->allocation.height;
1745 attributes.wclass = GDK_INPUT_OUTPUT;
1747 attributes.visual = gtk_widget_get_visual (widget);
1748 attributes.colormap = colormap;
1750 attributes.event_mask = gtk_widget_get_events (widget);
1751 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1752 GDK_BUTTON_PRESS_MASK |
1753 GDK_BUTTON_RELEASE_MASK |
1754 GDK_KEY_PRESS_MASK |
1755 GDK_ENTER_NOTIFY_MASK |
1756 GDK_LEAVE_NOTIFY_MASK |
1757 GDK_POINTER_MOTION_MASK |
1758 GDK_POINTER_MOTION_HINT_MASK);
1760 attributes.cursor = gdk_cursor_new_for_display (display, GDK_TOP_LEFT_ARROW);
1763 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1765 gdk_window_set_user_data (widget->window, sheet);
1767 widget->style = gtk_style_attach (widget->style, widget->window);
1769 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1771 gdk_color_parse ("white", &sheet->color[BG_COLOR]);
1772 gdk_colormap_alloc_color (colormap, &sheet->color[BG_COLOR], FALSE,
1774 gdk_color_parse ("gray", &sheet->color[GRID_COLOR]);
1775 gdk_colormap_alloc_color (colormap, &sheet->color[GRID_COLOR], FALSE,
1780 attributes.width = sheet->column_title_area.width;
1781 attributes.height = sheet->column_title_area.height;
1784 /* column - title window */
1785 sheet->column_title_window =
1786 gdk_window_new (widget->window, &attributes, attributes_mask);
1787 gdk_window_set_user_data (sheet->column_title_window, sheet);
1788 gtk_style_set_background (widget->style, sheet->column_title_window,
1794 attributes.width = sheet->row_title_area.width;
1795 attributes.height = sheet->row_title_area.height;
1797 /* row - title window */
1798 sheet->row_title_window = gdk_window_new (widget->window,
1799 &attributes, attributes_mask);
1800 gdk_window_set_user_data (sheet->row_title_window, sheet);
1801 gtk_style_set_background (widget->style, sheet->row_title_window,
1804 /* sheet - window */
1805 attributes.cursor = gdk_cursor_new_for_display (display, GDK_PLUS);
1810 sheet->sheet_window = gdk_window_new (widget->window,
1811 &attributes, attributes_mask);
1812 gdk_window_set_user_data (sheet->sheet_window, sheet);
1814 gdk_cursor_unref (attributes.cursor);
1816 gdk_window_set_background (sheet->sheet_window, &widget->style->white);
1817 gdk_window_show (sheet->sheet_window);
1820 sheet->fg_gc = gdk_gc_new (widget->window);
1821 sheet->bg_gc = gdk_gc_new (widget->window);
1823 values.foreground = widget->style->white;
1824 values.function = GDK_INVERT;
1825 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1826 values.line_width = BORDER_WIDTH;
1828 sheet->xor_gc = gdk_gc_new_with_values (widget->window,
1837 gtk_widget_set_parent_window (sheet->entry_widget, sheet->sheet_window);
1838 gtk_widget_set_parent (sheet->entry_widget, GTK_WIDGET (sheet));
1840 gtk_widget_set_parent_window (sheet->button, sheet->sheet_window);
1841 gtk_widget_set_parent (sheet->button, GTK_WIDGET (sheet));
1844 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_PLUS);
1846 if (sheet->column_titles_visible)
1847 gdk_window_show (sheet->column_title_window);
1848 if (sheet->row_titles_visible)
1849 gdk_window_show (sheet->row_title_window);
1851 sheet->hover_window = create_hover_window ();
1853 draw_row_title_buttons (sheet);
1854 draw_column_title_buttons (sheet);
1856 gtk_sheet_update_primary_selection (sheet);
1860 create_global_button (GtkSheet *sheet)
1862 sheet->button = gtk_button_new_with_label (" ");
1864 g_object_ref_sink (sheet->button);
1866 g_signal_connect (sheet->button,
1868 G_CALLBACK (global_button_clicked),
1873 size_allocate_global_button (GtkSheet *sheet)
1875 GtkAllocation allocation;
1877 if (!sheet->column_titles_visible) return;
1878 if (!sheet->row_titles_visible) return;
1880 gtk_widget_size_request (sheet->button, NULL);
1884 allocation.width = sheet->row_title_area.width;
1885 allocation.height = sheet->column_title_area.height;
1887 gtk_widget_size_allocate (sheet->button, &allocation);
1888 gtk_widget_show (sheet->button);
1892 global_button_clicked (GtkWidget *widget, gpointer data)
1894 gtk_sheet_click_cell (GTK_SHEET (data), -1, -1);
1895 gtk_widget_grab_focus (GTK_WIDGET (data));
1900 gtk_sheet_unrealize (GtkWidget *widget)
1904 g_return_if_fail (widget != NULL);
1905 g_return_if_fail (GTK_IS_SHEET (widget));
1907 sheet = GTK_SHEET (widget);
1909 gdk_cursor_unref (sheet->cursor_drag);
1910 sheet->cursor_drag = NULL;
1912 gdk_colormap_free_colors (gtk_widget_get_colormap (widget),
1913 sheet->color, n_COLORS);
1915 g_object_unref (sheet->xor_gc);
1916 g_object_unref (sheet->fg_gc);
1917 g_object_unref (sheet->bg_gc);
1919 destroy_hover_window (sheet->hover_window);
1921 gdk_window_destroy (sheet->sheet_window);
1922 gdk_window_destroy (sheet->column_title_window);
1923 gdk_window_destroy (sheet->row_title_window);
1925 gtk_widget_unparent (sheet->entry_widget);
1926 if (sheet->button != NULL)
1927 gtk_widget_unparent (sheet->button);
1929 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1930 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1934 gtk_sheet_map (GtkWidget *widget)
1936 GtkSheet *sheet = GTK_SHEET (widget);
1938 g_return_if_fail (widget != NULL);
1939 g_return_if_fail (GTK_IS_SHEET (widget));
1941 if (!GTK_WIDGET_MAPPED (widget))
1943 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
1945 gdk_window_show (widget->window);
1946 gdk_window_show (sheet->sheet_window);
1948 if (sheet->column_titles_visible)
1950 draw_column_title_buttons (sheet);
1951 gdk_window_show (sheet->column_title_window);
1953 if (sheet->row_titles_visible)
1955 draw_row_title_buttons (sheet);
1956 gdk_window_show (sheet->row_title_window);
1959 if (!GTK_WIDGET_MAPPED (sheet->entry_widget)
1960 && sheet->active_cell.row >= 0
1961 && sheet->active_cell.col >= 0 )
1963 gtk_widget_show (sheet->entry_widget);
1964 gtk_widget_map (sheet->entry_widget);
1967 if (GTK_WIDGET_VISIBLE (sheet->button) &&
1968 !GTK_WIDGET_MAPPED (sheet->button))
1970 gtk_widget_show (sheet->button);
1971 gtk_widget_map (sheet->button);
1974 if (GTK_BIN (sheet->button)->child)
1975 if (GTK_WIDGET_VISIBLE (GTK_BIN (sheet->button)->child) &&
1976 !GTK_WIDGET_MAPPED (GTK_BIN (sheet->button)->child))
1977 gtk_widget_map (GTK_BIN (sheet->button)->child);
1979 gtk_sheet_range_draw (sheet, NULL);
1980 change_active_cell (sheet,
1981 sheet->active_cell.row,
1982 sheet->active_cell.col);
1987 gtk_sheet_unmap (GtkWidget *widget)
1989 GtkSheet *sheet = GTK_SHEET (widget);
1991 if (!GTK_WIDGET_MAPPED (widget))
1994 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
1996 gdk_window_hide (sheet->sheet_window);
1997 if (sheet->column_titles_visible)
1998 gdk_window_hide (sheet->column_title_window);
1999 if (sheet->row_titles_visible)
2000 gdk_window_hide (sheet->row_title_window);
2001 gdk_window_hide (widget->window);
2003 if (GTK_WIDGET_MAPPED (sheet->entry_widget))
2004 gtk_widget_unmap (sheet->entry_widget);
2006 if (GTK_WIDGET_MAPPED (sheet->button))
2007 gtk_widget_unmap (sheet->button);
2012 gtk_sheet_cell_draw (GtkSheet *sheet, gint row, gint col)
2014 PangoLayout *layout;
2015 PangoRectangle text;
2016 PangoFontDescription *font_desc = GTK_WIDGET (sheet)->style->font_desc;
2021 GtkSheetCellAttr attributes;
2024 g_return_if_fail (sheet != NULL);
2026 /* bail now if we aren't yet drawable */
2027 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
2030 row >= psppire_axis_unit_count (sheet->vaxis))
2034 col >= psppire_axis_unit_count (sheet->haxis))
2037 gtk_sheet_get_attributes (sheet, row, col, &attributes);
2039 /* select GC for background rectangle */
2040 gdk_gc_set_foreground (sheet->fg_gc, &attributes.foreground);
2041 gdk_gc_set_foreground (sheet->bg_gc, &attributes.background);
2043 rectangle_from_cell (sheet, row, col, &area);
2045 gdk_gc_set_line_attributes (sheet->fg_gc, 1, 0, 0, 0);
2047 if (sheet->show_grid)
2049 gdk_gc_set_foreground (sheet->bg_gc, &sheet->color[GRID_COLOR]);
2051 gdk_draw_rectangle (sheet->sheet_window,
2055 area.width, area.height);
2059 label = gtk_sheet_cell_get_text (sheet, row, col);
2064 layout = gtk_widget_create_pango_layout (GTK_WIDGET (sheet), label);
2065 dispose_string (sheet, label);
2068 pango_layout_set_font_description (layout, font_desc);
2070 pango_layout_get_pixel_extents (layout, NULL, &text);
2072 gdk_gc_set_clip_rectangle (sheet->fg_gc, &area);
2074 font_height = pango_font_description_get_size (font_desc);
2075 if ( !pango_font_description_get_size_is_absolute (font_desc))
2076 font_height /= PANGO_SCALE;
2078 /* Centre the text vertically */
2079 area.y += (area.height - font_height) / 2.0;
2081 switch (attributes.justification)
2083 case GTK_JUSTIFY_RIGHT:
2084 area.x += area.width - text.width;
2086 case GTK_JUSTIFY_CENTER:
2087 area.x += (area.width - text.width) / 2.0;
2089 case GTK_JUSTIFY_LEFT:
2093 g_critical ("Unhandled justification %d in column %d\n",
2094 attributes.justification, col);
2098 gdk_draw_layout (sheet->sheet_window, sheet->fg_gc,
2103 gdk_gc_set_clip_rectangle (sheet->fg_gc, NULL);
2104 g_object_unref (layout);
2110 gtk_sheet_range_draw (GtkSheet *sheet, const GtkSheetRange *range)
2115 GtkSheetRange drawing_range;
2117 g_return_if_fail (sheet != NULL);
2118 g_return_if_fail (GTK_SHEET (sheet));
2120 if (!GTK_WIDGET_DRAWABLE (GTK_WIDGET (sheet))) return;
2121 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
2122 if (!GTK_WIDGET_MAPPED (GTK_WIDGET (sheet))) return;
2126 drawing_range.row0 = min_visible_row (sheet);
2127 drawing_range.col0 = min_visible_column (sheet);
2128 drawing_range.rowi = MIN (max_visible_row (sheet),
2129 psppire_axis_unit_count (sheet->vaxis) - 1);
2130 drawing_range.coli = max_visible_column (sheet);
2131 gdk_drawable_get_size (sheet->sheet_window, &area.width, &area.height);
2132 area.x = area.y = 0;
2136 drawing_range.row0 = MAX (range->row0, min_visible_row (sheet));
2137 drawing_range.col0 = MAX (range->col0, min_visible_column (sheet));
2138 drawing_range.rowi = MIN (range->rowi, max_visible_row (sheet));
2139 drawing_range.coli = MIN (range->coli, max_visible_column (sheet));
2141 rectangle_from_range (sheet, &drawing_range, &area);
2144 g_return_if_fail (drawing_range.rowi >= drawing_range.row0);
2145 g_return_if_fail (drawing_range.coli >= drawing_range.col0);
2147 gdk_draw_rectangle (sheet->sheet_window,
2148 GTK_WIDGET (sheet)->style->white_gc,
2151 area.width, area.height);
2153 for (i = drawing_range.row0; i <= drawing_range.rowi; i++)
2154 for (j = drawing_range.col0; j <= drawing_range.coli; j++)
2156 gtk_sheet_cell_draw (sheet, i, j);
2159 if (sheet->state != GTK_SHEET_NORMAL &&
2160 gtk_sheet_range_isvisible (sheet, &sheet->range))
2161 gtk_sheet_range_draw_selection (sheet, drawing_range);
2163 if (sheet->state == GTK_STATE_NORMAL &&
2164 sheet->active_cell.row >= drawing_range.row0 &&
2165 sheet->active_cell.row <= drawing_range.rowi &&
2166 sheet->active_cell.col >= drawing_range.col0 &&
2167 sheet->active_cell.col <= drawing_range.coli)
2168 gtk_sheet_show_entry_widget (sheet);
2172 gtk_sheet_range_draw_selection (GtkSheet *sheet, GtkSheetRange range)
2178 if (range.col0 > sheet->range.coli || range.coli < sheet->range.col0 ||
2179 range.row0 > sheet->range.rowi || range.rowi < sheet->range.row0)
2182 if (!gtk_sheet_range_isvisible (sheet, &range)) return;
2183 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
2187 range.col0 = MAX (sheet->range.col0, range.col0);
2188 range.coli = MIN (sheet->range.coli, range.coli);
2189 range.row0 = MAX (sheet->range.row0, range.row0);
2190 range.rowi = MIN (sheet->range.rowi, range.rowi);
2192 range.col0 = MAX (range.col0, min_visible_column (sheet));
2193 range.coli = MIN (range.coli, max_visible_column (sheet));
2194 range.row0 = MAX (range.row0, min_visible_row (sheet));
2195 range.rowi = MIN (range.rowi, max_visible_row (sheet));
2197 for (i = range.row0; i <= range.rowi; i++)
2199 for (j = range.col0; j <= range.coli; j++)
2201 if (gtk_sheet_cell_get_state (sheet, i, j) == GTK_STATE_SELECTED)
2203 rectangle_from_cell (sheet, i, j, &area);
2205 if (i == sheet->range.row0)
2207 area.y = area.y + 2;
2208 area.height = area.height - 2;
2210 if (i == sheet->range.rowi) area.height = area.height - 3;
2211 if (j == sheet->range.col0)
2213 area.x = area.x + 2;
2214 area.width = area.width - 2;
2216 if (j == sheet->range.coli) area.width = area.width - 3;
2218 if (i != sheet->active_cell.row || j != sheet->active_cell.col)
2220 gdk_draw_rectangle (sheet->sheet_window,
2223 area.x + 1, area.y + 1,
2224 area.width, area.height);
2231 gtk_sheet_draw_border (sheet, sheet->range);
2235 safe_strcmp (const gchar *s1, const gchar *s2)
2237 if ( !s1 && !s2) return 0;
2238 if ( !s1) return -1;
2239 if ( !s2) return +1;
2240 return strcmp (s1, s2);
2244 gtk_sheet_set_cell (GtkSheet *sheet, gint row, gint col,
2245 GtkJustification justification,
2248 GSheetModel *model ;
2251 g_return_if_fail (sheet != NULL);
2252 g_return_if_fail (GTK_IS_SHEET (sheet));
2254 if (col >= psppire_axis_unit_count (sheet->haxis)
2255 || row >= psppire_axis_unit_count (sheet->vaxis))
2258 if (col < 0 || row < 0) return;
2260 model = gtk_sheet_get_model (sheet);
2262 old_text = g_sheet_model_get_string (model, row, col);
2264 if (0 != safe_strcmp (old_text, text))
2265 g_sheet_model_set_string (model, text, row, col);
2267 if ( g_sheet_model_free_strings (model))
2273 gtk_sheet_cell_clear (GtkSheet *sheet, gint row, gint column)
2275 GtkSheetRange range;
2277 g_return_if_fail (sheet != NULL);
2278 g_return_if_fail (GTK_IS_SHEET (sheet));
2279 if (column >= psppire_axis_unit_count (sheet->haxis) ||
2280 row >= psppire_axis_unit_count (sheet->vaxis)) return;
2282 if (column < 0 || row < 0) return;
2286 range.col0 = min_visible_column (sheet);
2287 range.coli = max_visible_column (sheet);
2289 gtk_sheet_real_cell_clear (sheet, row, column);
2291 gtk_sheet_range_draw (sheet, &range);
2295 gtk_sheet_real_cell_clear (GtkSheet *sheet, gint row, gint column)
2297 GSheetModel *model = gtk_sheet_get_model (sheet);
2299 gchar *old_text = gtk_sheet_cell_get_text (sheet, row, column);
2301 if (old_text && strlen (old_text) > 0 )
2303 g_sheet_model_datum_clear (model, row, column);
2306 dispose_string (sheet, old_text);
2310 gtk_sheet_cell_get_text (const GtkSheet *sheet, gint row, gint col)
2313 g_return_val_if_fail (sheet != NULL, NULL);
2314 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
2316 if (col >= psppire_axis_unit_count (sheet->haxis) || row >= psppire_axis_unit_count (sheet->vaxis))
2318 if (col < 0 || row < 0) return NULL;
2320 model = gtk_sheet_get_model (sheet);
2325 return g_sheet_model_get_string (model, row, col);
2330 gtk_sheet_cell_get_state (GtkSheet *sheet, gint row, gint col)
2333 GtkSheetRange *range;
2335 g_return_val_if_fail (sheet != NULL, 0);
2336 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
2337 if (col >= psppire_axis_unit_count (sheet->haxis) || row >= psppire_axis_unit_count (sheet->vaxis)) return 0;
2338 if (col < 0 || row < 0) return 0;
2340 state = sheet->state;
2341 range = &sheet->range;
2345 case GTK_SHEET_NORMAL:
2346 return GTK_STATE_NORMAL;
2348 case GTK_SHEET_ROW_SELECTED:
2349 if (row >= range->row0 && row <= range->rowi)
2350 return GTK_STATE_SELECTED;
2352 case GTK_SHEET_COLUMN_SELECTED:
2353 if (col >= range->col0 && col <= range->coli)
2354 return GTK_STATE_SELECTED;
2356 case GTK_SHEET_RANGE_SELECTED:
2357 if (row >= range->row0 && row <= range->rowi && \
2358 col >= range->col0 && col <= range->coli)
2359 return GTK_STATE_SELECTED;
2362 return GTK_STATE_NORMAL;
2365 /* Convert X, Y (in pixels) to *ROW, *COLUMN
2366 If the function returns FALSE, then the results will be unreliable.
2369 gtk_sheet_get_pixel_info (GtkSheet *sheet,
2377 *column = -G_MAXINT;
2379 g_return_val_if_fail (sheet != NULL, 0);
2380 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
2382 /* bounds checking, return false if the user clicked
2390 if ( sheet->column_titles_visible)
2391 y -= sheet->column_title_area.height;
2393 y += sheet->vadjustment->value;
2395 if ( y < 0 && sheet->column_titles_visible)
2401 trow = row_from_ypixel (sheet, y);
2402 if (trow > psppire_axis_unit_count (sheet->vaxis))
2408 if ( sheet->row_titles_visible)
2409 x -= sheet->row_title_area.width;
2411 x += sheet->hadjustment->value;
2413 if ( x < 0 && sheet->row_titles_visible)
2419 tcol = column_from_xpixel (sheet, x);
2420 if (tcol > psppire_axis_unit_count (sheet->haxis))
2430 gtk_sheet_get_cell_area (GtkSheet *sheet,
2435 g_return_val_if_fail (sheet != NULL, 0);
2436 g_return_val_if_fail (GTK_IS_SHEET (sheet), 0);
2438 if (row >= psppire_axis_unit_count (sheet->vaxis) || column >= psppire_axis_unit_count (sheet->haxis))
2441 area->x = (column == -1) ? 0 : psppire_axis_pixel_start (sheet->haxis, column);
2442 area->y = (row == -1) ? 0 : psppire_axis_pixel_start (sheet->vaxis, row);
2444 area->width= (column == -1) ? sheet->row_title_area.width
2445 : psppire_axis_unit_size (sheet->haxis, column);
2447 area->height= (row == -1) ? sheet->column_title_area.height
2448 : psppire_axis_unit_size (sheet->vaxis, row);
2454 gtk_sheet_set_active_cell (GtkSheet *sheet, gint row, gint col)
2456 g_return_if_fail (sheet != NULL);
2457 g_return_if_fail (GTK_IS_SHEET (sheet));
2459 if (row < -1 || col < -1)
2462 if (row >= psppire_axis_unit_count (sheet->vaxis)
2464 col >= psppire_axis_unit_count (sheet->haxis))
2467 sheet->active_cell.row = row;
2468 sheet->active_cell.col = col;
2470 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
2473 if ( row == -1 || col == -1)
2475 gtk_sheet_hide_entry_widget (sheet);
2479 change_active_cell (sheet, row, col);
2483 gtk_sheet_get_active_cell (GtkSheet *sheet, gint *row, gint *column)
2485 g_return_if_fail (sheet != NULL);
2486 g_return_if_fail (GTK_IS_SHEET (sheet));
2488 if ( row ) *row = sheet->active_cell.row;
2489 if (column) *column = sheet->active_cell.col;
2493 gtk_sheet_entry_changed (GtkWidget *widget, gpointer data)
2498 GtkJustification justification;
2499 GtkSheetCellAttr attributes;
2501 g_return_if_fail (data != NULL);
2502 g_return_if_fail (GTK_IS_SHEET (data));
2504 sheet = GTK_SHEET (data);
2506 if (!GTK_WIDGET_VISIBLE (widget)) return;
2507 if (sheet->state != GTK_STATE_NORMAL) return;
2509 row = sheet->active_cell.row;
2510 col = sheet->active_cell.col;
2512 if (row < 0 || col < 0) return;
2514 sheet->active_cell.row = -1;
2515 sheet->active_cell.col = -1;
2517 text = gtk_entry_get_text (gtk_sheet_get_entry (sheet));
2519 if (text && strlen (text) > 0)
2521 gtk_sheet_get_attributes (sheet, row, col, &attributes);
2522 justification = attributes.justification;
2523 gtk_sheet_set_cell (sheet, row, col, justification, text);
2526 sheet->active_cell.row = row;;
2527 sheet->active_cell.col = col;
2532 gtk_sheet_hide_entry_widget (GtkSheet *sheet)
2534 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
2537 if (sheet->active_cell.row < 0 ||
2538 sheet->active_cell.col < 0) return;
2540 gtk_widget_hide (sheet->entry_widget);
2541 gtk_widget_unmap (sheet->entry_widget);
2543 GTK_WIDGET_UNSET_FLAGS (GTK_WIDGET (sheet->entry_widget), GTK_VISIBLE);
2547 change_active_cell (GtkSheet *sheet, gint row, gint col)
2549 gint old_row, old_col;
2550 glong old_handler_id = sheet->entry_handler_id;
2552 g_return_if_fail (GTK_IS_SHEET (sheet));
2554 if (row < 0 || col < 0)
2557 if ( row > psppire_axis_unit_count (sheet->vaxis)
2558 || col > psppire_axis_unit_count (sheet->haxis))
2561 if (sheet->state != GTK_SHEET_NORMAL)
2563 sheet->state = GTK_SHEET_NORMAL;
2564 gtk_sheet_real_unselect_range (sheet, NULL);
2568 g_signal_handler_block (sheet->entry_widget, sheet->entry_handler_id);
2570 old_row = sheet->active_cell.row;
2571 old_col = sheet->active_cell.col;
2574 /* Redraw the neighbourhood of the old active cell */
2576 r.col0 = old_col - 1;
2577 r.coli = old_col + 1;
2578 r.row0 = old_row - 1;
2579 r.rowi = old_row + 1;
2581 maximize_int (&r.row0, 0);
2582 maximize_int (&r.col0, 0);
2583 minimize_int (&r.rowi, psppire_axis_unit_count (sheet->vaxis) - 1);
2584 minimize_int (&r.coli, psppire_axis_unit_count (sheet->haxis) - 1);
2587 if ( gtk_sheet_range_isvisible (sheet, &r))
2588 gtk_sheet_range_draw (sheet, &r);
2591 sheet->range.row0 = row;
2592 sheet->range.col0 = col;
2593 sheet->range.rowi = row;
2594 sheet->range.coli = col;
2595 sheet->active_cell.row = row;
2596 sheet->active_cell.col = col;
2597 sheet->selection_cell.row = row;
2598 sheet->selection_cell.col = col;
2600 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
2602 gtk_sheet_draw_active_cell (sheet);
2603 gtk_sheet_show_entry_widget (sheet);
2606 g_signal_emit (sheet, sheet_signals [ACTIVATE], 0,
2607 row, col, old_row, old_col);
2609 if ( old_handler_id == sheet->entry_handler_id)
2610 g_signal_handler_unblock (sheet->entry_widget, sheet->entry_handler_id);
2614 gtk_sheet_show_entry_widget (GtkSheet *sheet)
2616 GtkEntry *sheet_entry;
2617 GtkSheetCellAttr attributes;
2621 g_return_if_fail (sheet != NULL);
2622 g_return_if_fail (GTK_IS_SHEET (sheet));
2624 row = sheet->active_cell.row;
2625 col = sheet->active_cell.col;
2627 /* Don't show the active cell, if there is no active cell: */
2628 if (! (row >= 0 && col >= 0)) /* e.g row or coll == -1. */
2631 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
2632 if (sheet->state != GTK_SHEET_NORMAL) return;
2633 if (GTK_SHEET_IN_SELECTION (sheet)) return;
2635 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (sheet->entry_widget), GTK_VISIBLE);
2637 sheet_entry = gtk_sheet_get_entry (sheet);
2639 gtk_sheet_get_attributes (sheet, row, col, &attributes);
2642 text = gtk_sheet_cell_get_text (sheet, row, col);
2644 text = g_strdup ("");
2646 if ( GTK_IS_ENTRY (sheet_entry))
2648 const gchar *old_text = gtk_entry_get_text (GTK_ENTRY (sheet_entry));
2649 if (strcmp (old_text, text) != 0)
2650 gtk_entry_set_text (sheet_entry, text);
2652 switch (attributes.justification)
2654 case GTK_JUSTIFY_RIGHT:
2655 gtk_entry_set_alignment (GTK_ENTRY (sheet_entry), 1.0);
2657 case GTK_JUSTIFY_CENTER:
2658 gtk_entry_set_alignment (GTK_ENTRY (sheet_entry), 0.5);
2660 case GTK_JUSTIFY_LEFT:
2662 gtk_entry_set_alignment (GTK_ENTRY (sheet_entry), 0.0);
2667 gtk_sheet_size_allocate_entry (sheet);
2669 gtk_widget_set_sensitive (GTK_WIDGET (sheet_entry),
2670 g_sheet_model_is_editable (sheet->model,
2672 gtk_widget_map (sheet->entry_widget);
2674 gtk_widget_grab_focus (GTK_WIDGET (sheet_entry));
2676 dispose_string (sheet, text);
2680 gtk_sheet_draw_active_cell (GtkSheet *sheet)
2683 GtkSheetRange range;
2685 if (!GTK_WIDGET_DRAWABLE (GTK_WIDGET (sheet))) return;
2686 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
2688 row = sheet->active_cell.row;
2689 col = sheet->active_cell.col;
2691 if (row < 0 || col < 0) return;
2693 if (!gtk_sheet_cell_isvisible (sheet, row, col))
2696 range.col0 = range.coli = col;
2697 range.row0 = range.rowi = row;
2699 gtk_sheet_draw_border (sheet, range);
2705 gtk_sheet_new_selection (GtkSheet *sheet, GtkSheetRange *range)
2707 gint i, j, mask1, mask2;
2708 gint state, selected;
2709 gint x, y, width, height;
2710 GtkSheetRange new_range, aux_range;
2712 g_return_if_fail (sheet != NULL);
2714 if (range == NULL) range=&sheet->range;
2718 range->row0 = MIN (range->row0, sheet->range.row0);
2719 range->rowi = MAX (range->rowi, sheet->range.rowi);
2720 range->col0 = MIN (range->col0, sheet->range.col0);
2721 range->coli = MAX (range->coli, sheet->range.coli);
2723 range->row0 = MAX (range->row0, min_visible_row (sheet));
2724 range->rowi = MIN (range->rowi, max_visible_row (sheet));
2725 range->col0 = MAX (range->col0, min_visible_column (sheet));
2726 range->coli = MIN (range->coli, max_visible_column (sheet));
2728 aux_range.row0 = MAX (new_range.row0, min_visible_row (sheet));
2729 aux_range.rowi = MIN (new_range.rowi, max_visible_row (sheet));
2730 aux_range.col0 = MAX (new_range.col0, min_visible_column (sheet));
2731 aux_range.coli = MIN (new_range.coli, max_visible_column (sheet));
2733 for (i = range->row0; i <= range->rowi; i++)
2735 for (j = range->col0; j <= range->coli; j++)
2738 state = gtk_sheet_cell_get_state (sheet, i, j);
2739 selected= (i <= new_range.rowi && i >= new_range.row0 &&
2740 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
2742 if (state == GTK_STATE_SELECTED && selected &&
2743 (i == sheet->range.row0 || i == sheet->range.rowi ||
2744 j == sheet->range.col0 || j == sheet->range.coli ||
2745 i == new_range.row0 || i == new_range.rowi ||
2746 j == new_range.col0 || j == new_range.coli))
2749 mask1 = i == sheet->range.row0 ? 1 : 0;
2750 mask1 = i == sheet->range.rowi ? mask1 + 2 : mask1;
2751 mask1 = j == sheet->range.col0 ? mask1 + 4 : mask1;
2752 mask1 = j == sheet->range.coli ? mask1 + 8 : mask1;
2754 mask2 = i == new_range.row0 ? 1 : 0;
2755 mask2 = i == new_range.rowi ? mask2 + 2 : mask2;
2756 mask2 = j == new_range.col0 ? mask2 + 4 : mask2;
2757 mask2 = j == new_range.coli ? mask2 + 8 : mask2;
2761 x = psppire_axis_pixel_start (sheet->haxis, j);
2762 y = psppire_axis_pixel_start (sheet->vaxis, i);
2763 width = psppire_axis_pixel_start (sheet->haxis, j)- x+
2764 psppire_axis_unit_size (sheet->haxis, j);
2765 height = psppire_axis_pixel_start (sheet->vaxis, i) - y + psppire_axis_unit_size (sheet->vaxis, i);
2767 if (i == sheet->range.row0)
2770 height = height + 3;
2772 if (i == sheet->range.rowi) height = height + 3;
2773 if (j == sheet->range.col0)
2778 if (j == sheet->range.coli) width = width + 3;
2780 if (i != sheet->active_cell.row || j != sheet->active_cell.col)
2782 x = psppire_axis_pixel_start (sheet->haxis, j);
2783 y = psppire_axis_pixel_start (sheet->vaxis, i);
2784 width = psppire_axis_pixel_start (sheet->haxis, j)- x+
2785 psppire_axis_unit_size (sheet->haxis, j);
2787 height = psppire_axis_pixel_start (sheet->vaxis, i) - y + psppire_axis_unit_size (sheet->vaxis, i);
2789 if (i == new_range.row0)
2792 height = height - 2;
2794 if (i == new_range.rowi) height = height - 3;
2795 if (j == new_range.col0)
2800 if (j == new_range.coli) width = width - 3;
2802 gdk_draw_rectangle (sheet->sheet_window,
2813 for (i = range->row0; i <= range->rowi; i++)
2815 for (j = range->col0; j <= range->coli; j++)
2818 state = gtk_sheet_cell_get_state (sheet, i, j);
2819 selected= (i <= new_range.rowi && i >= new_range.row0 &&
2820 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
2822 if (state == GTK_STATE_SELECTED && !selected)
2825 x = psppire_axis_pixel_start (sheet->haxis, j);
2826 y = psppire_axis_pixel_start (sheet->vaxis, i);
2827 width = psppire_axis_pixel_start (sheet->haxis, j) - x + psppire_axis_unit_size (sheet->haxis, j);
2828 height = psppire_axis_pixel_start (sheet->vaxis, i) - y + psppire_axis_unit_size (sheet->vaxis, i);
2830 if (i == sheet->range.row0)
2833 height = height + 3;
2835 if (i == sheet->range.rowi) height = height + 3;
2836 if (j == sheet->range.col0)
2841 if (j == sheet->range.coli) width = width + 3;
2847 for (i = range->row0; i <= range->rowi; i++)
2849 for (j = range->col0; j <= range->coli; j++)
2852 state = gtk_sheet_cell_get_state (sheet, i, j);
2853 selected= (i <= new_range.rowi && i >= new_range.row0 &&
2854 j <= new_range.coli && j >= new_range.col0) ? TRUE : FALSE;
2856 if (state != GTK_STATE_SELECTED && selected &&
2857 (i != sheet->active_cell.row || j != sheet->active_cell.col))
2860 x = psppire_axis_pixel_start (sheet->haxis, j);
2861 y = psppire_axis_pixel_start (sheet->vaxis, i);
2862 width = psppire_axis_pixel_start (sheet->haxis, j) - x + psppire_axis_unit_size (sheet->haxis, j);
2863 height = psppire_axis_pixel_start (sheet->vaxis, i) - y + psppire_axis_unit_size (sheet->vaxis, i);
2865 if (i == new_range.row0)
2868 height = height - 2;
2870 if (i == new_range.rowi) height = height - 3;
2871 if (j == new_range.col0)
2876 if (j == new_range.coli) width = width - 3;
2878 gdk_draw_rectangle (sheet->sheet_window,
2889 for (i = aux_range.row0; i <= aux_range.rowi; i++)
2891 for (j = aux_range.col0; j <= aux_range.coli; j++)
2893 state = gtk_sheet_cell_get_state (sheet, i, j);
2895 mask1 = i == sheet->range.row0 ? 1 : 0;
2896 mask1 = i == sheet->range.rowi ? mask1 + 2 : mask1;
2897 mask1 = j == sheet->range.col0 ? mask1 + 4 : mask1;
2898 mask1 = j == sheet->range.coli ? mask1 + 8 : mask1;
2900 mask2 = i == new_range.row0 ? 1 : 0;
2901 mask2 = i == new_range.rowi ? mask2 + 2 : mask2;
2902 mask2 = j == new_range.col0 ? mask2 + 4 : mask2;
2903 mask2 = j == new_range.coli ? mask2 + 8 : mask2;
2904 if (mask2 != mask1 || (mask2 == mask1 && state != GTK_STATE_SELECTED))
2906 x = psppire_axis_pixel_start (sheet->haxis, j);
2907 y = psppire_axis_pixel_start (sheet->vaxis, i);
2908 width = psppire_axis_unit_size (sheet->haxis, j);
2909 height = psppire_axis_unit_size (sheet->vaxis, i);
2911 gdk_draw_rectangle (sheet->sheet_window,
2919 gdk_draw_rectangle (sheet->sheet_window,
2922 x + 1, y + height - 1,
2926 gdk_draw_rectangle (sheet->sheet_window,
2934 gdk_draw_rectangle (sheet->sheet_window,
2937 x + width - 1, y + 1,
2949 gtk_sheet_draw_border (GtkSheet *sheet, GtkSheetRange new_range)
2953 rectangle_from_range (sheet, &new_range, &area);
2955 gdk_draw_rectangle (sheet->sheet_window,
2966 gtk_sheet_real_select_range (GtkSheet *sheet,
2967 const GtkSheetRange *range)
2971 g_return_if_fail (sheet != NULL);
2973 if (range == NULL) range = &sheet->range;
2975 memcpy (&sheet->range, range, sizeof (*range));
2977 if (range->row0 < 0 || range->rowi < 0) return;
2978 if (range->col0 < 0 || range->coli < 0) return;
2980 state = sheet->state;
2983 if (range->coli != sheet->range.coli || range->col0 != sheet->range.col0 ||
2984 range->rowi != sheet->range.rowi || range->row0 != sheet->range.row0)
2986 gtk_sheet_new_selection (sheet, &sheet->range);
2990 gtk_sheet_range_draw_selection (sheet, sheet->range);
2994 gtk_sheet_update_primary_selection (sheet);
2996 g_signal_emit (sheet, sheet_signals[SELECT_RANGE], 0, &sheet->range);
3001 gtk_sheet_get_selected_range (GtkSheet *sheet, GtkSheetRange *range)
3003 g_return_if_fail (sheet != NULL);
3004 *range = sheet->range;
3009 gtk_sheet_select_range (GtkSheet *sheet, const GtkSheetRange *range)
3011 g_return_if_fail (sheet != NULL);
3013 if (range == NULL) range=&sheet->range;
3015 if (range->row0 < 0 || range->rowi < 0) return;
3016 if (range->col0 < 0 || range->coli < 0) return;
3019 if (sheet->state != GTK_SHEET_NORMAL)
3020 gtk_sheet_real_unselect_range (sheet, NULL);
3022 sheet->range.row0 = range->row0;
3023 sheet->range.rowi = range->rowi;
3024 sheet->range.col0 = range->col0;
3025 sheet->range.coli = range->coli;
3026 sheet->active_cell.row = range->row0;
3027 sheet->active_cell.col = range->col0;
3028 sheet->selection_cell.row = range->rowi;
3029 sheet->selection_cell.col = range->coli;
3031 sheet->state = GTK_SHEET_RANGE_SELECTED;
3032 gtk_sheet_real_select_range (sheet, NULL);
3036 gtk_sheet_unselect_range (GtkSheet *sheet)
3038 if (! GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
3041 gtk_sheet_real_unselect_range (sheet, NULL);
3042 sheet->state = GTK_STATE_NORMAL;
3044 change_active_cell (sheet,
3045 sheet->active_cell.row, sheet->active_cell.col);
3050 gtk_sheet_real_unselect_range (GtkSheet *sheet,
3051 const GtkSheetRange *range)
3053 g_return_if_fail (sheet != NULL);
3054 g_return_if_fail (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)));
3057 range = &sheet->range;
3059 if (range->row0 < 0 || range->rowi < 0) return;
3060 if (range->col0 < 0 || range->coli < 0) return;
3062 g_signal_emit (sheet, sheet_signals[SELECT_COLUMN], 0, -1);
3063 g_signal_emit (sheet, sheet_signals[SELECT_ROW], 0, -1);
3065 sheet->range.row0 = -1;
3066 sheet->range.rowi = -1;
3067 sheet->range.col0 = -1;
3068 sheet->range.coli = -1;
3073 gtk_sheet_expose (GtkWidget *widget,
3074 GdkEventExpose *event)
3077 GtkSheetRange range;
3079 g_return_val_if_fail (widget != NULL, FALSE);
3080 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
3081 g_return_val_if_fail (event != NULL, FALSE);
3083 sheet = GTK_SHEET (widget);
3085 if (!GTK_WIDGET_DRAWABLE (widget))
3088 /* exposure events on the sheet */
3089 if (event->window == sheet->row_title_window &&
3090 sheet->row_titles_visible)
3092 draw_row_title_buttons_range (sheet,
3093 min_visible_row (sheet),
3094 max_visible_row (sheet));
3097 if (event->window == sheet->column_title_window &&
3098 sheet->column_titles_visible)
3100 draw_column_title_buttons_range (sheet,
3101 min_visible_column (sheet),
3102 max_visible_column (sheet));
3107 row_from_ypixel (sheet,
3108 event->area.y + sheet->vadjustment->value);
3112 row_from_ypixel (sheet,
3114 event->area.height + sheet->vadjustment->value);
3118 column_from_xpixel (sheet,
3119 event->area.x + sheet->hadjustment->value);
3123 column_from_xpixel (sheet,
3124 event->area.x + event->area.width +
3125 sheet->hadjustment->value);
3129 if (event->window == sheet->sheet_window)
3131 gtk_sheet_range_draw (sheet, &range);
3133 if (sheet->state != GTK_SHEET_NORMAL)
3135 if (gtk_sheet_range_isvisible (sheet, &sheet->range))
3136 gtk_sheet_range_draw (sheet, &sheet->range);
3138 if (GTK_SHEET_IN_RESIZE (sheet) || GTK_SHEET_IN_DRAG (sheet))
3139 gtk_sheet_range_draw (sheet, &sheet->drag_range);
3141 if (gtk_sheet_range_isvisible (sheet, &sheet->range))
3142 gtk_sheet_range_draw_selection (sheet, sheet->range);
3143 if (GTK_SHEET_IN_RESIZE (sheet) || GTK_SHEET_IN_DRAG (sheet))
3144 draw_xor_rectangle (sheet, sheet->drag_range);
3147 if ((!GTK_SHEET_IN_XDRAG (sheet)) && (!GTK_SHEET_IN_YDRAG (sheet)))
3149 if (sheet->state == GTK_SHEET_NORMAL)
3150 gtk_sheet_draw_active_cell (sheet);
3154 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
3161 gtk_sheet_button_press (GtkWidget *widget,
3162 GdkEventButton *event)
3165 GdkModifierType mods;
3170 g_return_val_if_fail (widget != NULL, FALSE);
3171 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
3172 g_return_val_if_fail (event != NULL, FALSE);
3174 sheet = GTK_SHEET (widget);
3176 /* Cancel any pending tooltips */
3177 if (sheet->motion_timer)
3179 g_source_remove (sheet->motion_timer);
3180 sheet->motion_timer = 0;
3183 gtk_widget_get_pointer (widget, &x, &y);
3184 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
3187 if (event->window == sheet->column_title_window)
3189 g_signal_emit (sheet,
3190 sheet_signals[BUTTON_EVENT_COLUMN], 0,
3193 if (g_sheet_model_get_column_sensitivity (sheet->model, column))
3195 if ( event->type == GDK_2BUTTON_PRESS && event->button == 1)
3196 g_signal_emit (sheet,
3197 sheet_signals[DOUBLE_CLICK_COLUMN], 0, column);
3200 else if (event->window == sheet->row_title_window)
3202 g_signal_emit (sheet,
3203 sheet_signals[BUTTON_EVENT_ROW], 0,
3206 if (g_sheet_model_get_row_sensitivity (sheet->model, row))
3208 if ( event->type == GDK_2BUTTON_PRESS && event->button == 1)
3209 g_signal_emit (sheet,
3210 sheet_signals[DOUBLE_CLICK_ROW], 0, row);
3214 gdk_window_get_pointer (widget->window, NULL, NULL, &mods);
3216 if (! (mods & GDK_BUTTON1_MASK)) return TRUE;
3219 /* press on resize windows */
3220 if (event->window == sheet->column_title_window &&
3221 gtk_sheet_columns_resizable (sheet))
3224 gtk_widget_get_pointer (widget, &sheet->x_drag, NULL);
3225 if ( sheet->row_titles_visible)
3226 sheet->x_drag -= sheet->row_title_area.width;
3229 sheet->x_drag = event->x;
3231 if (on_column_boundary (sheet, sheet->x_drag, &sheet->drag_cell.col))
3234 gtk_sheet_column_size_request (sheet, sheet->drag_cell.col, &req);
3235 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_XDRAG);
3236 gdk_pointer_grab (sheet->column_title_window, FALSE,
3237 GDK_POINTER_MOTION_HINT_MASK |
3238 GDK_BUTTON1_MOTION_MASK |
3239 GDK_BUTTON_RELEASE_MASK,
3240 NULL, NULL, event->time);
3242 draw_xor_vline (sheet);
3247 if (event->window == sheet->row_title_window && gtk_sheet_rows_resizable (sheet))
3249 gtk_widget_get_pointer (widget, NULL, &sheet->y_drag);
3252 if (POSSIBLE_YDRAG (sheet, sheet->y_drag, &sheet->drag_cell.row))
3255 gtk_sheet_row_size_request (sheet, sheet->drag_cell.row, &req);
3256 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_YDRAG);
3257 gdk_pointer_grab (sheet->row_title_window, FALSE,
3258 GDK_POINTER_MOTION_HINT_MASK |
3259 GDK_BUTTON1_MOTION_MASK |
3260 GDK_BUTTON_RELEASE_MASK,
3261 NULL, NULL, event->time);
3263 draw_xor_hline (sheet);
3269 /* the sheet itself does not handle other than single click events */
3270 if (event->type != GDK_BUTTON_PRESS) return FALSE;
3272 /* selections on the sheet */
3273 if (event->window == sheet->sheet_window)
3275 gtk_widget_get_pointer (widget, &x, &y);
3276 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
3277 gdk_pointer_grab (sheet->sheet_window, FALSE,
3278 GDK_POINTER_MOTION_HINT_MASK |
3279 GDK_BUTTON1_MOTION_MASK |
3280 GDK_BUTTON_RELEASE_MASK,
3281 NULL, NULL, event->time);
3282 gtk_grab_add (GTK_WIDGET (sheet));
3284 /* This seems to be a kludge to work around a problem where the sheet
3285 scrolls to another position. The timeout scrolls it back to its
3286 original posn. JMD 3 July 2007
3288 gtk_widget_grab_focus (GTK_WIDGET (sheet));
3290 if (sheet->selection_mode != GTK_SELECTION_SINGLE &&
3291 sheet->selection_mode != GTK_SELECTION_NONE &&
3292 sheet->cursor_drag->type == GDK_SIZING &&
3293 !GTK_SHEET_IN_SELECTION (sheet) && !GTK_SHEET_IN_RESIZE (sheet))
3295 if (sheet->state == GTK_STATE_NORMAL)
3297 row = sheet->active_cell.row;
3298 column = sheet->active_cell.col;
3299 sheet->active_cell.row = row;
3300 sheet->active_cell.col = column;
3301 sheet->drag_range = sheet->range;
3302 sheet->state = GTK_SHEET_RANGE_SELECTED;
3303 gtk_sheet_select_range (sheet, &sheet->drag_range);
3307 if (row > sheet->range.rowi) row--;
3308 if (column > sheet->range.coli) column--;
3309 sheet->drag_cell.row = row;
3310 sheet->drag_cell.col = column;
3311 sheet->drag_range = sheet->range;
3312 draw_xor_rectangle (sheet, sheet->drag_range);
3313 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_RESIZE);
3315 else if (sheet->cursor_drag->type == GDK_TOP_LEFT_ARROW &&
3316 !GTK_SHEET_IN_SELECTION (sheet)
3317 && ! GTK_SHEET_IN_DRAG (sheet)
3318 && sheet->active_cell.row >= 0
3319 && sheet->active_cell.col >= 0
3322 if (sheet->state == GTK_STATE_NORMAL)
3324 row = sheet->active_cell.row;
3325 column = sheet->active_cell.col;
3326 sheet->active_cell.row = row;
3327 sheet->active_cell.col = column;
3328 sheet->drag_range = sheet->range;
3329 sheet->state = GTK_SHEET_RANGE_SELECTED;
3330 gtk_sheet_select_range (sheet, &sheet->drag_range);
3334 if (row < sheet->range.row0) row++;
3335 if (row > sheet->range.rowi) row--;
3336 if (column < sheet->range.col0) column++;
3337 if (column > sheet->range.coli) column--;
3338 sheet->drag_cell.row = row;
3339 sheet->drag_cell.col = column;
3340 sheet->drag_range = sheet->range;
3341 draw_xor_rectangle (sheet, sheet->drag_range);
3342 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_DRAG);
3346 veto = gtk_sheet_click_cell (sheet, row, column);
3347 if (veto) GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3351 if (event->window == sheet->column_title_window)
3353 gtk_widget_get_pointer (widget, &x, &y);
3354 if ( sheet->row_titles_visible)
3355 x -= sheet->row_title_area.width;
3357 x += sheet->hadjustment->value;
3359 column = column_from_xpixel (sheet, x);
3361 if (g_sheet_model_get_column_sensitivity (sheet->model, column))
3363 veto = gtk_sheet_click_cell (sheet, -1, column);
3364 gtk_grab_add (GTK_WIDGET (sheet));
3365 gtk_widget_grab_focus (GTK_WIDGET (sheet));
3366 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3370 if (event->window == sheet->row_title_window)
3372 gtk_widget_get_pointer (widget, &x, &y);
3373 if ( sheet->column_titles_visible)
3374 y -= sheet->column_title_area.height;
3376 y += sheet->vadjustment->value;
3378 row = row_from_ypixel (sheet, y);
3379 if (g_sheet_model_get_row_sensitivity (sheet->model, row))
3381 veto = gtk_sheet_click_cell (sheet, row, -1);
3382 gtk_grab_add (GTK_WIDGET (sheet));
3383 gtk_widget_grab_focus (GTK_WIDGET (sheet));
3384 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3392 gtk_sheet_click_cell (GtkSheet *sheet, gint row, gint column)
3395 gboolean forbid_move;
3400 if (row >= psppire_axis_unit_count (sheet->vaxis)
3401 || column >= psppire_axis_unit_count (sheet->haxis))
3406 g_signal_emit (sheet, sheet_signals[TRAVERSE], 0,
3407 &sheet->active_cell,
3413 if (sheet->state == GTK_STATE_NORMAL)
3416 row = sheet->active_cell.row;
3417 column = sheet->active_cell.col;
3419 change_active_cell (sheet, row, column);
3423 if (row == -1 && column >= 0)
3425 gtk_sheet_select_column (sheet, column);
3429 if (column == -1 && row >= 0)
3431 gtk_sheet_select_row (sheet, row);
3435 if (row == -1 && column == -1)
3437 sheet->range.row0 = 0;
3438 sheet->range.col0 = 0;
3439 sheet->range.rowi = psppire_axis_unit_count (sheet->vaxis) - 1;
3441 psppire_axis_unit_count (sheet->haxis) - 1;
3442 sheet->active_cell.row = 0;
3443 sheet->active_cell.col = 0;
3444 gtk_sheet_select_range (sheet, NULL);
3448 if (sheet->state != GTK_SHEET_NORMAL)
3450 sheet->state = GTK_SHEET_NORMAL;
3451 gtk_sheet_real_unselect_range (sheet, NULL);
3455 change_active_cell (sheet, row, column);
3458 sheet->active_cell.row = row;
3459 sheet->active_cell.col = column;
3460 sheet->selection_cell.row = row;
3461 sheet->selection_cell.col = column;
3462 sheet->range.row0 = row;
3463 sheet->range.col0 = column;
3464 sheet->range.rowi = row;
3465 sheet->range.coli = column;
3466 sheet->state = GTK_SHEET_NORMAL;
3467 GTK_SHEET_SET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3468 gtk_sheet_draw_active_cell (sheet);
3473 gtk_sheet_button_release (GtkWidget *widget,
3474 GdkEventButton *event)
3477 GdkDisplay *display = gtk_widget_get_display (widget);
3479 GtkSheet *sheet = GTK_SHEET (widget);
3481 /* release on resize windows */
3482 if (GTK_SHEET_IN_XDRAG (sheet))
3484 gint xpos = event->x;
3486 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_XDRAG);
3487 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3489 gdk_display_pointer_ungrab (display, event->time);
3490 draw_xor_vline (sheet);
3492 width = new_column_width (sheet, sheet->drag_cell.col, &xpos);
3494 gtk_sheet_set_column_width (sheet, sheet->drag_cell.col, width);
3498 if (GTK_SHEET_IN_YDRAG (sheet))
3500 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_YDRAG);
3501 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3502 gtk_widget_get_pointer (widget, NULL, &y);
3503 gdk_display_pointer_ungrab (display, event->time);
3504 draw_xor_hline (sheet);
3506 gtk_sheet_set_row_height (sheet, sheet->drag_cell.row,
3507 new_row_height (sheet, sheet->drag_cell.row, &y));
3508 g_signal_emit_by_name (sheet->vadjustment, "value_changed");
3513 if (GTK_SHEET_IN_DRAG (sheet))
3515 GtkSheetRange old_range;
3516 draw_xor_rectangle (sheet, sheet->drag_range);
3517 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_DRAG);
3518 gdk_display_pointer_ungrab (display, event->time);
3520 gtk_sheet_real_unselect_range (sheet, NULL);
3522 sheet->active_cell.row = sheet->active_cell.row +
3523 (sheet->drag_range.row0 - sheet->range.row0);
3524 sheet->active_cell.col = sheet->active_cell.col +
3525 (sheet->drag_range.col0 - sheet->range.col0);
3526 sheet->selection_cell.row = sheet->selection_cell.row +
3527 (sheet->drag_range.row0 - sheet->range.row0);
3528 sheet->selection_cell.col = sheet->selection_cell.col +
3529 (sheet->drag_range.col0 - sheet->range.col0);
3530 old_range = sheet->range;
3531 sheet->range = sheet->drag_range;
3532 sheet->drag_range = old_range;
3533 g_signal_emit (sheet, sheet_signals[MOVE_RANGE], 0,
3534 &sheet->drag_range, &sheet->range);
3535 gtk_sheet_select_range (sheet, &sheet->range);
3538 if (GTK_SHEET_IN_RESIZE (sheet))
3540 GtkSheetRange old_range;
3541 draw_xor_rectangle (sheet, sheet->drag_range);
3542 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_RESIZE);
3543 gdk_display_pointer_ungrab (display, event->time);
3545 gtk_sheet_real_unselect_range (sheet, NULL);
3547 sheet->active_cell.row = sheet->active_cell.row +
3548 (sheet->drag_range.row0 - sheet->range.row0);
3549 sheet->active_cell.col = sheet->active_cell.col +
3550 (sheet->drag_range.col0 - sheet->range.col0);
3551 if (sheet->drag_range.row0 < sheet->range.row0)
3552 sheet->selection_cell.row = sheet->drag_range.row0;
3553 if (sheet->drag_range.rowi >= sheet->range.rowi)
3554 sheet->selection_cell.row = sheet->drag_range.rowi;
3555 if (sheet->drag_range.col0 < sheet->range.col0)
3556 sheet->selection_cell.col = sheet->drag_range.col0;
3557 if (sheet->drag_range.coli >= sheet->range.coli)
3558 sheet->selection_cell.col = sheet->drag_range.coli;
3559 old_range = sheet->range;
3560 sheet->range = sheet->drag_range;
3561 sheet->drag_range = old_range;
3563 if (sheet->state == GTK_STATE_NORMAL) sheet->state = GTK_SHEET_RANGE_SELECTED;
3564 g_signal_emit (sheet, sheet_signals[RESIZE_RANGE], 0,
3565 &sheet->drag_range, &sheet->range);
3566 gtk_sheet_select_range (sheet, &sheet->range);
3569 if (sheet->state == GTK_SHEET_NORMAL && GTK_SHEET_IN_SELECTION (sheet))
3571 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3572 gdk_display_pointer_ungrab (display, event->time);
3573 change_active_cell (sheet, sheet->active_cell.row,
3574 sheet->active_cell.col);
3577 if (GTK_SHEET_IN_SELECTION)
3578 gdk_display_pointer_ungrab (display, event->time);
3579 gtk_grab_remove (GTK_WIDGET (sheet));
3581 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
3590 /* Shamelessly lifted from gtktooltips */
3592 gtk_sheet_subtitle_paint_window (GtkWidget *tip_window)
3596 gtk_widget_size_request (tip_window, &req);
3597 gtk_paint_flat_box (tip_window->style, tip_window->window,
3598 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
3599 NULL, GTK_WIDGET(tip_window), "tooltip",
3600 0, 0, req.width, req.height);
3606 destroy_hover_window (GtkSheetHoverTitle *h)
3608 gtk_widget_destroy (h->window);
3612 static GtkSheetHoverTitle *
3613 create_hover_window (void)
3615 GtkSheetHoverTitle *hw = g_malloc (sizeof (*hw));
3617 hw->window = gtk_window_new (GTK_WINDOW_POPUP);
3619 #if GTK_CHECK_VERSION (2, 9, 0)
3620 gtk_window_set_type_hint (GTK_WINDOW (hw->window),
3621 GDK_WINDOW_TYPE_HINT_TOOLTIP);
3624 gtk_widget_set_app_paintable (hw->window, TRUE);
3625 gtk_window_set_resizable (GTK_WINDOW (hw->window), FALSE);
3626 gtk_widget_set_name (hw->window, "gtk-tooltips");
3627 gtk_container_set_border_width (GTK_CONTAINER (hw->window), 4);
3629 g_signal_connect (hw->window,
3631 G_CALLBACK (gtk_sheet_subtitle_paint_window),
3634 hw->label = gtk_label_new (NULL);
3637 gtk_label_set_line_wrap (GTK_LABEL (hw->label), TRUE);
3638 gtk_misc_set_alignment (GTK_MISC (hw->label), 0.5, 0.5);
3640 gtk_container_add (GTK_CONTAINER (hw->window), hw->label);
3642 gtk_widget_show (hw->label);
3644 g_signal_connect (hw->window,
3646 G_CALLBACK (gtk_widget_destroyed),
3652 #define HOVER_WINDOW_Y_OFFSET 2
3655 show_subtitle (GtkSheet *sheet, gint row, gint column, const gchar *subtitle)
3664 gtk_label_set_text (GTK_LABEL (sheet->hover_window->label),
3668 sheet->hover_window->row = row;
3669 sheet->hover_window->column = column;
3671 gdk_window_get_origin (GTK_WIDGET (sheet)->window, &x, &y);
3673 gtk_widget_get_pointer (GTK_WIDGET (sheet), &px, &py);
3675 gtk_widget_show (sheet->hover_window->window);
3677 width = GTK_WIDGET (sheet->hover_window->label)->allocation.width;
3683 y += sheet->column_title_area.y;
3684 y += sheet->column_title_area.height;
3685 y += HOVER_WINDOW_Y_OFFSET;
3691 x += sheet->row_title_area.x;
3692 x += sheet->row_title_area.width * 2 / 3.0;
3695 gtk_window_move (GTK_WINDOW (sheet->hover_window->window),
3700 motion_timeout_callback (gpointer data)
3702 GtkSheet *sheet = GTK_SHEET (data);
3705 gtk_widget_get_pointer (GTK_WIDGET (sheet), &x, &y);
3707 if ( gtk_sheet_get_pixel_info (sheet, x, y, &row, &column) )
3709 if (sheet->row_title_under)
3711 gchar *text = g_sheet_model_get_row_subtitle (sheet->model, row);
3713 show_subtitle (sheet, row, -1, text);
3717 if (sheet->column_title_under)
3719 gchar *text = g_sheet_model_get_column_subtitle (sheet->model,
3722 show_subtitle (sheet, -1, column, text);
3732 gtk_sheet_motion (GtkWidget *widget, GdkEventMotion *event)
3735 GdkModifierType mods;
3736 GdkCursorType new_cursor;
3739 GdkDisplay *display;
3741 g_return_val_if_fail (widget != NULL, FALSE);
3742 g_return_val_if_fail (GTK_IS_SHEET (widget), FALSE);
3743 g_return_val_if_fail (event != NULL, FALSE);
3745 sheet = GTK_SHEET (widget);
3747 display = gtk_widget_get_display (widget);
3749 /* selections on the sheet */
3753 if (!GTK_WIDGET_VISIBLE (sheet->hover_window->window))
3755 if ( sheet->motion_timer > 0 )
3756 g_source_remove (sheet->motion_timer);
3757 sheet->motion_timer =
3758 g_timeout_add (TIMEOUT_HOVER, motion_timeout_callback, sheet);
3764 gtk_widget_get_pointer (widget, &wx, &wy);
3766 if ( gtk_sheet_get_pixel_info (sheet, wx, wy, &row, &column) )
3768 if ( row != sheet->hover_window->row ||
3769 column != sheet->hover_window->column)
3771 gtk_widget_hide (sheet->hover_window->window);
3776 if (event->window == sheet->column_title_window &&
3777 gtk_sheet_columns_resizable (sheet))
3779 if (!GTK_SHEET_IN_SELECTION (sheet) &&
3780 on_column_boundary (sheet, x, &column))
3782 new_cursor = GDK_SB_H_DOUBLE_ARROW;
3783 if (new_cursor != sheet->cursor_drag->type)
3785 gdk_cursor_unref (sheet->cursor_drag);
3786 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_SB_H_DOUBLE_ARROW);
3787 gdk_window_set_cursor (sheet->column_title_window,
3788 sheet->cursor_drag);
3793 new_cursor = GDK_TOP_LEFT_ARROW;
3794 if (!GTK_SHEET_IN_XDRAG (sheet) &&
3795 new_cursor != sheet->cursor_drag->type)
3797 gdk_cursor_unref (sheet->cursor_drag);
3798 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_TOP_LEFT_ARROW);
3799 gdk_window_set_cursor (sheet->column_title_window,
3800 sheet->cursor_drag);
3805 if (event->window == sheet->row_title_window &&
3806 gtk_sheet_rows_resizable (sheet))
3809 if (!GTK_SHEET_IN_SELECTION (sheet) && POSSIBLE_YDRAG (sheet, y, &column))
3811 new_cursor = GDK_SB_V_DOUBLE_ARROW;
3812 if (new_cursor != sheet->cursor_drag->type)
3814 gdk_cursor_unref (sheet->cursor_drag);
3815 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_SB_V_DOUBLE_ARROW);
3816 gdk_window_set_cursor (sheet->row_title_window, sheet->cursor_drag);
3822 new_cursor = GDK_TOP_LEFT_ARROW;
3823 if (!GTK_SHEET_IN_YDRAG (sheet) &&
3824 new_cursor != sheet->cursor_drag->type)
3826 gdk_cursor_unref (sheet->cursor_drag);
3827 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_TOP_LEFT_ARROW);
3828 gdk_window_set_cursor (sheet->row_title_window, sheet->cursor_drag);
3833 new_cursor = GDK_PLUS;
3834 if ( event->window == sheet->sheet_window &&
3835 !POSSIBLE_DRAG (sheet, x, y, &row, &column) &&
3836 !GTK_SHEET_IN_DRAG (sheet) &&
3837 !POSSIBLE_RESIZE (sheet, x, y, &row, &column) &&
3838 !GTK_SHEET_IN_RESIZE (sheet) &&
3839 new_cursor != sheet->cursor_drag->type)
3841 gdk_cursor_unref (sheet->cursor_drag);
3842 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_PLUS);
3843 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
3846 new_cursor = GDK_TOP_LEFT_ARROW;
3847 if ( event->window == sheet->sheet_window &&
3848 ! (POSSIBLE_RESIZE (sheet, x, y, &row, &column) ||
3849 GTK_SHEET_IN_RESIZE (sheet)) &&
3850 (POSSIBLE_DRAG (sheet, x, y, &row, &column) ||
3851 GTK_SHEET_IN_DRAG (sheet)) &&
3852 new_cursor != sheet->cursor_drag->type)
3854 gdk_cursor_unref (sheet->cursor_drag);
3855 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_TOP_LEFT_ARROW);
3856 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
3859 new_cursor = GDK_SIZING;
3860 if ( event->window == sheet->sheet_window &&
3861 sheet->selection_mode != GTK_SELECTION_NONE &&
3862 !GTK_SHEET_IN_DRAG (sheet) &&
3863 (POSSIBLE_RESIZE (sheet, x, y, &row, &column) ||
3864 GTK_SHEET_IN_RESIZE (sheet)) &&
3865 new_cursor != sheet->cursor_drag->type)
3867 gdk_cursor_unref (sheet->cursor_drag);
3868 sheet->cursor_drag = gdk_cursor_new_for_display (display, GDK_SIZING);
3869 gdk_window_set_cursor (sheet->sheet_window, sheet->cursor_drag);
3873 gdk_window_get_pointer (widget->window, &x, &y, &mods);
3874 if (! (mods & GDK_BUTTON1_MASK)) return FALSE;
3876 if (GTK_SHEET_IN_XDRAG (sheet))
3880 new_column_width (sheet, sheet->drag_cell.col, &x);
3882 if (x != sheet->x_drag)
3884 draw_xor_vline (sheet);
3886 draw_xor_vline (sheet);
3892 if (GTK_SHEET_IN_YDRAG (sheet))
3894 if (event->is_hint || event->window != widget->window)
3895 gtk_widget_get_pointer (widget, NULL, &y);
3899 new_row_height (sheet, sheet->drag_cell.row, &y);
3900 if (y != sheet->y_drag)
3902 draw_xor_hline (sheet);
3904 draw_xor_hline (sheet);
3909 if (GTK_SHEET_IN_DRAG (sheet))
3912 column = column_from_xpixel (sheet, x)- sheet->drag_cell.col;
3913 row = row_from_ypixel (sheet, y) - sheet->drag_cell.row;
3914 if (sheet->state == GTK_SHEET_COLUMN_SELECTED) row = 0;
3915 if (sheet->state == GTK_SHEET_ROW_SELECTED) column = 0;
3919 if (aux.row0 + row >= 0 && aux.rowi + row < psppire_axis_unit_count (sheet->vaxis) &&
3920 aux.col0 + column >= 0 && aux.coli + column < psppire_axis_unit_count (sheet->haxis))
3922 aux = sheet->drag_range;
3923 sheet->drag_range.row0 = sheet->range.row0 + row;
3924 sheet->drag_range.col0 = sheet->range.col0 + column;
3925 sheet->drag_range.rowi = sheet->range.rowi + row;
3926 sheet->drag_range.coli = sheet->range.coli + column;
3927 if (aux.row0 != sheet->drag_range.row0 ||
3928 aux.col0 != sheet->drag_range.col0)
3930 draw_xor_rectangle (sheet, aux);
3931 draw_xor_rectangle (sheet, sheet->drag_range);
3937 if (GTK_SHEET_IN_RESIZE (sheet))
3940 gint v_h, current_col, current_row, col_threshold, row_threshold;
3942 if (abs (x - psppire_axis_pixel_start (sheet->haxis, sheet->drag_cell.col)) >
3943 abs (y - psppire_axis_pixel_start (sheet->vaxis, sheet->drag_cell.row))) v_h = 2;
3945 current_col = column_from_xpixel (sheet, x);
3946 current_row = row_from_ypixel (sheet, y);
3947 column = current_col - sheet->drag_cell.col;
3948 row = current_row - sheet->drag_cell.row;
3950 /*use half of column width resp. row height as threshold to
3952 col_threshold = psppire_axis_pixel_start (sheet->haxis, current_col) +
3953 psppire_axis_unit_size (sheet->haxis, current_col) / 2;
3956 if (x < col_threshold)
3959 else if (column < 0)
3961 if (x > col_threshold)
3964 row_threshold = psppire_axis_pixel_start (sheet->vaxis, current_row) +
3965 psppire_axis_unit_size (sheet->vaxis, current_row)/2;
3968 if (y < row_threshold)
3973 if (y > row_threshold)
3977 if (sheet->state == GTK_SHEET_COLUMN_SELECTED) row = 0;
3978 if (sheet->state == GTK_SHEET_ROW_SELECTED) column = 0;
3988 if (aux.row0 + row >= 0 && aux.rowi + row < psppire_axis_unit_count (sheet->vaxis) &&
3989 aux.col0 + column >= 0 && aux.coli + column < psppire_axis_unit_count (sheet->haxis))
3991 aux = sheet->drag_range;
3992 sheet->drag_range = sheet->range;
3994 if (row < 0) sheet->drag_range.row0 = sheet->range.row0 + row;
3995 if (row > 0) sheet->drag_range.rowi = sheet->range.rowi + row;
3996 if (column < 0) sheet->drag_range.col0 = sheet->range.col0 + column;
3997 if (column > 0) sheet->drag_range.coli = sheet->range.coli + column;
3999 if (aux.row0 != sheet->drag_range.row0 ||
4000 aux.rowi != sheet->drag_range.rowi ||
4001 aux.col0 != sheet->drag_range.col0 ||
4002 aux.coli != sheet->drag_range.coli)
4004 draw_xor_rectangle (sheet, aux);
4005 draw_xor_rectangle (sheet, sheet->drag_range);
4011 gtk_sheet_get_pixel_info (sheet, x, y, &row, &column);
4013 if (sheet->state == GTK_SHEET_NORMAL && row == sheet->active_cell.row &&
4014 column == sheet->active_cell.col) return TRUE;
4016 if (GTK_SHEET_IN_SELECTION (sheet) && mods&GDK_BUTTON1_MASK)
4017 gtk_sheet_extend_selection (sheet, row, column);
4023 gtk_sheet_crossing_notify (GtkWidget *widget,
4024 GdkEventCrossing *event)
4026 GtkSheet *sheet = GTK_SHEET (widget);
4028 if (event->window == sheet->column_title_window)
4029 sheet->column_title_under = event->type == GDK_ENTER_NOTIFY;
4030 else if (event->window == sheet->row_title_window)
4031 sheet->row_title_under = event->type == GDK_ENTER_NOTIFY;
4037 gtk_sheet_extend_selection (GtkSheet *sheet, gint row, gint column)
4039 GtkSheetRange range;
4043 if (row == sheet->selection_cell.row && column == sheet->selection_cell.col)
4046 if (sheet->selection_mode == GTK_SELECTION_SINGLE) return;
4048 gtk_widget_grab_focus (GTK_WIDGET (sheet));
4050 if (GTK_SHEET_IN_DRAG (sheet)) return;
4052 state = sheet->state;
4054 switch (sheet->state)
4056 case GTK_SHEET_ROW_SELECTED:
4057 column = psppire_axis_unit_count (sheet->haxis) - 1;
4059 case GTK_SHEET_COLUMN_SELECTED:
4060 row = psppire_axis_unit_count (sheet->vaxis) - 1;
4062 case GTK_SHEET_NORMAL:
4063 sheet->state = GTK_SHEET_RANGE_SELECTED;
4064 r = sheet->active_cell.row;
4065 c = sheet->active_cell.col;
4066 sheet->range.col0 = c;
4067 sheet->range.row0 = r;
4068 sheet->range.coli = c;
4069 sheet->range.rowi = r;
4070 gtk_sheet_range_draw_selection (sheet, sheet->range);
4071 case GTK_SHEET_RANGE_SELECTED:
4072 sheet->state = GTK_SHEET_RANGE_SELECTED;
4075 sheet->selection_cell.row = row;
4076 sheet->selection_cell.col = column;
4078 range.col0 = MIN (column, sheet->active_cell.col);
4079 range.coli = MAX (column, sheet->active_cell.col);
4080 range.row0 = MIN (row, sheet->active_cell.row);
4081 range.rowi = MAX (row, sheet->active_cell.row);
4083 if (range.row0 != sheet->range.row0 || range.rowi != sheet->range.rowi ||
4084 range.col0 != sheet->range.col0 || range.coli != sheet->range.coli ||
4085 state == GTK_SHEET_NORMAL)
4086 gtk_sheet_real_select_range (sheet, &range);
4091 gtk_sheet_entry_key_press (GtkWidget *widget,
4095 g_signal_emit_by_name (widget, "key_press_event", key, &focus);
4100 /* Number of rows in a step-increment */
4101 #define ROWS_PER_STEP 1
4105 page_vertical (GtkSheet *sheet, GtkScrollType dir)
4107 gint old_row = sheet->active_cell.row ;
4108 glong vpixel = psppire_axis_pixel_start (sheet->vaxis, old_row);
4112 vpixel -= psppire_axis_pixel_start (sheet->vaxis,
4113 min_visible_row (sheet));
4117 case GTK_SCROLL_PAGE_DOWN:
4118 gtk_adjustment_set_value (sheet->vadjustment,
4119 sheet->vadjustment->value +
4120 sheet->vadjustment->page_increment);
4122 case GTK_SCROLL_PAGE_UP:
4123 gtk_adjustment_set_value (sheet->vadjustment,
4124 sheet->vadjustment->value -
4125 sheet->vadjustment->page_increment);
4129 g_assert_not_reached ();
4134 vpixel += psppire_axis_pixel_start (sheet->vaxis,
4135 min_visible_row (sheet));
4137 new_row = row_from_ypixel (sheet, vpixel);
4139 change_active_cell (sheet, new_row,
4140 sheet->active_cell.col);
4145 step_sheet (GtkSheet *sheet, GtkScrollType dir)
4147 gint current_row = sheet->active_cell.row;
4148 gint current_col = sheet->active_cell.col;
4149 GtkSheetCell new_cell ;
4150 gboolean forbidden = FALSE;
4152 new_cell.row = current_row;
4153 new_cell.col = current_col;
4157 case GTK_SCROLL_STEP_DOWN:
4160 case GTK_SCROLL_STEP_UP:
4163 case GTK_SCROLL_STEP_RIGHT:
4166 case GTK_SCROLL_STEP_LEFT:
4170 g_assert_not_reached ();
4175 maximize_int (&new_cell.row, 0);
4176 maximize_int (&new_cell.col, 0);
4178 minimize_int (&new_cell.row,
4179 psppire_axis_unit_count (sheet->vaxis) - 1);
4181 minimize_int (&new_cell.col,
4182 psppire_axis_unit_count (sheet->haxis) - 1);
4184 g_signal_emit (sheet, sheet_signals[TRAVERSE], 0,
4185 &sheet->active_cell,
4192 change_active_cell (sheet, new_cell.row, new_cell.col);
4194 if ( new_cell.col > max_fully_visible_column (sheet))
4197 psppire_axis_pixel_start (sheet->haxis,
4199 hpos -= sheet->hadjustment->page_size;
4201 gtk_adjustment_set_value (sheet->hadjustment,
4204 else if ( new_cell.col < min_fully_visible_column (sheet))
4207 psppire_axis_pixel_start (sheet->haxis,
4210 gtk_adjustment_set_value (sheet->hadjustment,
4215 if ( new_cell.row > max_fully_visible_row (sheet))
4218 psppire_axis_pixel_start (sheet->vaxis,
4220 vpos -= sheet->vadjustment->page_size;
4222 gtk_adjustment_set_value (sheet->vadjustment,
4225 else if ( new_cell.row < min_fully_visible_row (sheet))
4228 psppire_axis_pixel_start (sheet->vaxis,
4231 gtk_adjustment_set_value (sheet->vadjustment,
4238 gtk_sheet_key_press (GtkWidget *widget,
4241 GtkSheet *sheet = GTK_SHEET (widget);
4243 GTK_SHEET_UNSET_FLAGS (sheet, GTK_SHEET_IN_SELECTION);
4245 switch (key->keyval)
4249 step_sheet (sheet, GTK_SCROLL_STEP_RIGHT);
4251 case GDK_ISO_Left_Tab:
4253 step_sheet (sheet, GTK_SCROLL_STEP_LEFT);
4257 step_sheet (sheet, GTK_SCROLL_STEP_DOWN);
4260 step_sheet (sheet, GTK_SCROLL_STEP_UP);
4264 page_vertical (sheet, GTK_SCROLL_PAGE_DOWN);
4267 page_vertical (sheet, GTK_SCROLL_PAGE_UP);
4271 gtk_adjustment_set_value (sheet->vadjustment,
4272 sheet->vadjustment->lower);
4274 change_active_cell (sheet, 0,
4275 sheet->active_cell.col);
4280 gtk_adjustment_set_value (sheet->vadjustment,
4281 sheet->vadjustment->upper -
4282 sheet->vadjustment->page_size -
4283 sheet->vadjustment->page_increment);
4286 change_active_cellx (sheet,
4287 psppire_axis_unit_count (sheet->vaxis) - 1,
4288 sheet->active_cell.col);
4292 gtk_sheet_real_cell_clear (sheet, sheet->active_cell.row, sheet->active_cell.col);
4303 gtk_sheet_size_request (GtkWidget *widget,
4304 GtkRequisition *requisition)
4308 g_return_if_fail (widget != NULL);
4309 g_return_if_fail (GTK_IS_SHEET (widget));
4310 g_return_if_fail (requisition != NULL);
4312 sheet = GTK_SHEET (widget);
4314 requisition->width = 3 * DEFAULT_COLUMN_WIDTH;
4315 requisition->height = 3 * DEFAULT_ROW_HEIGHT;
4317 /* compute the size of the column title area */
4318 if (sheet->column_titles_visible)
4319 requisition->height += sheet->column_title_area.height;
4321 /* compute the size of the row title area */
4322 if (sheet->row_titles_visible)
4323 requisition->width += sheet->row_title_area.width;
4328 gtk_sheet_size_allocate (GtkWidget *widget,
4329 GtkAllocation *allocation)
4332 GtkAllocation sheet_allocation;
4335 g_return_if_fail (widget != NULL);
4336 g_return_if_fail (GTK_IS_SHEET (widget));
4337 g_return_if_fail (allocation != NULL);
4339 sheet = GTK_SHEET (widget);
4340 widget->allocation = *allocation;
4341 border_width = GTK_CONTAINER (widget)->border_width;
4343 if (GTK_WIDGET_REALIZED (widget))
4344 gdk_window_move_resize (widget->window,
4345 allocation->x + border_width,
4346 allocation->y + border_width,
4347 allocation->width - 2 * border_width,
4348 allocation->height - 2 * border_width);
4350 sheet_allocation.x = 0;
4351 sheet_allocation.y = 0;
4352 sheet_allocation.width = allocation->width - 2 * border_width;
4353 sheet_allocation.height = allocation->height - 2 * border_width;
4355 if (GTK_WIDGET_REALIZED (widget))
4356 gdk_window_move_resize (sheet->sheet_window,
4359 sheet_allocation.width,
4360 sheet_allocation.height);
4362 /* position the window which holds the column title buttons */
4363 sheet->column_title_area.x = 0;
4364 sheet->column_title_area.y = 0;
4365 sheet->column_title_area.width = sheet_allocation.width ;
4368 /* position the window which holds the row title buttons */
4369 sheet->row_title_area.x = 0;
4370 sheet->row_title_area.y = 0;
4371 sheet->row_title_area.height = sheet_allocation.height;
4373 if (sheet->row_titles_visible)
4374 sheet->column_title_area.x += sheet->row_title_area.width;
4376 if (sheet->column_titles_visible)
4377 sheet->row_title_area.y += sheet->column_title_area.height;
4380 if (GTK_WIDGET_REALIZED (widget) && sheet->column_titles_visible)
4381 gdk_window_move_resize (sheet->column_title_window,
4382 sheet->column_title_area.x,
4383 sheet->column_title_area.y,
4384 sheet->column_title_area.width,
4385 sheet->column_title_area.height);
4388 if (GTK_WIDGET_REALIZED (widget) && sheet->row_titles_visible)
4389 gdk_window_move_resize (sheet->row_title_window,
4390 sheet->row_title_area.x,
4391 sheet->row_title_area.y,
4392 sheet->row_title_area.width,
4393 sheet->row_title_area.height);
4397 gint width = sheet->column_title_area.width;
4399 if ( sheet->row_titles_visible)
4400 width -= sheet->row_title_area.width;
4402 g_object_set (sheet->haxis,
4403 "minimum-extent", width,
4410 gint height = sheet->row_title_area.height;
4412 if ( sheet->column_titles_visible)
4413 height -= sheet->column_title_area.height;
4415 g_object_set (sheet->vaxis,
4416 "minimum-extent", height,
4421 /* set the scrollbars adjustments */
4422 adjust_scrollbars (sheet);
4426 draw_column_title_buttons (GtkSheet *sheet)
4430 if (!sheet->column_titles_visible) return;
4431 if (!GTK_WIDGET_REALIZED (sheet))
4434 gdk_drawable_get_size (sheet->sheet_window, &width, NULL);
4437 if (sheet->row_titles_visible)
4439 x = sheet->row_title_area.width;
4442 if (sheet->column_title_area.width != width || sheet->column_title_area.x != x)
4444 sheet->column_title_area.width = width;
4445 sheet->column_title_area.x = x;
4446 gdk_window_move_resize (sheet->column_title_window,
4447 sheet->column_title_area.x,
4448 sheet->column_title_area.y,
4449 sheet->column_title_area.width,
4450 sheet->column_title_area.height);
4453 if (max_visible_column (sheet) ==
4454 psppire_axis_unit_count (sheet->haxis) - 1)
4455 gdk_window_clear_area (sheet->column_title_window,
4457 sheet->column_title_area.width,
4458 sheet->column_title_area.height);
4460 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
4462 size_allocate_global_button (sheet);
4464 draw_column_title_buttons_range (sheet, min_visible_column (sheet),
4465 max_visible_column (sheet));
4469 draw_row_title_buttons (GtkSheet *sheet)
4474 if (!sheet->row_titles_visible) return;
4475 if (!GTK_WIDGET_REALIZED (sheet))
4478 gdk_drawable_get_size (sheet->sheet_window, NULL, &height);
4480 if (sheet->column_titles_visible)
4482 y = sheet->column_title_area.height;
4485 if (sheet->row_title_area.height != height || sheet->row_title_area.y != y)
4487 sheet->row_title_area.y = y;
4488 sheet->row_title_area.height = height;
4489 gdk_window_move_resize (sheet->row_title_window,
4490 sheet->row_title_area.x,
4491 sheet->row_title_area.y,
4492 sheet->row_title_area.width,
4493 sheet->row_title_area.height);
4496 if (max_visible_row (sheet) == psppire_axis_unit_count (sheet->vaxis) - 1)
4497 gdk_window_clear_area (sheet->row_title_window,
4499 sheet->row_title_area.width,
4500 sheet->row_title_area.height);
4502 if (!GTK_WIDGET_DRAWABLE (sheet)) return;
4504 size_allocate_global_button (sheet);
4507 draw_row_title_buttons_range (sheet, min_visible_row (sheet),
4508 max_visible_row (sheet));
4513 gtk_sheet_size_allocate_entry (GtkSheet *sheet)
4515 GtkAllocation entry_alloc;
4516 GtkSheetCellAttr attributes = { 0 };
4517 GtkEntry *sheet_entry;
4519 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
4520 if (!GTK_WIDGET_MAPPED (GTK_WIDGET (sheet))) return;
4522 sheet_entry = gtk_sheet_get_entry (sheet);
4524 if ( ! gtk_sheet_get_attributes (sheet, sheet->active_cell.row,
4525 sheet->active_cell.col,
4529 if ( GTK_WIDGET_REALIZED (sheet->entry_widget) )
4531 GtkStyle *style = GTK_WIDGET (sheet_entry)->style;
4533 style->bg[GTK_STATE_NORMAL] = attributes.background;
4534 style->fg[GTK_STATE_NORMAL] = attributes.foreground;
4535 style->text[GTK_STATE_NORMAL] = attributes.foreground;
4536 style->bg[GTK_STATE_ACTIVE] = attributes.background;
4537 style->fg[GTK_STATE_ACTIVE] = attributes.foreground;
4538 style->text[GTK_STATE_ACTIVE] = attributes.foreground;
4541 rectangle_from_cell (sheet, sheet->active_cell.row,
4542 sheet->active_cell.col, &entry_alloc);
4544 entry_alloc.width -= BORDER_WIDTH;
4545 entry_alloc.height -= BORDER_WIDTH;
4546 entry_alloc.x += BORDER_WIDTH / 2;
4547 entry_alloc.y += BORDER_WIDTH / 2;
4550 gtk_widget_set_size_request (sheet->entry_widget, entry_alloc.width,
4551 entry_alloc.height);
4552 gtk_widget_size_allocate (sheet->entry_widget, &entry_alloc);
4556 /* Copy the sheet's font to the entry widget */
4558 set_entry_widget_font (GtkSheet *sheet)
4560 GtkRcStyle *style = gtk_widget_get_modifier_style (sheet->entry_widget);
4562 pango_font_description_free (style->font_desc);
4563 style->font_desc = pango_font_description_copy (GTK_WIDGET (sheet)->style->font_desc);
4565 gtk_widget_modify_style (sheet->entry_widget, style);
4571 create_sheet_entry (GtkSheet *sheet)
4573 if (sheet->entry_widget)
4575 gtk_widget_unparent (sheet->entry_widget);
4578 sheet->entry_widget = g_object_new (sheet->entry_type, NULL);
4579 g_object_ref_sink (sheet->entry_widget);
4581 gtk_widget_size_request (sheet->entry_widget, NULL);
4583 if ( GTK_IS_ENTRY (sheet->entry_widget))
4585 g_object_set (sheet->entry_widget,
4590 if (GTK_WIDGET_REALIZED (sheet))
4592 gtk_widget_set_parent_window (sheet->entry_widget, sheet->sheet_window);
4593 gtk_widget_set_parent (sheet->entry_widget, GTK_WIDGET (sheet));
4594 gtk_widget_realize (sheet->entry_widget);
4597 g_signal_connect_swapped (sheet->entry_widget, "key_press_event",
4598 G_CALLBACK (gtk_sheet_entry_key_press),
4601 sheet->entry_handler_id =
4602 g_signal_connect (sheet->entry_widget,
4604 G_CALLBACK (gtk_sheet_entry_changed),
4607 set_entry_widget_font (sheet);
4609 gtk_widget_show (sheet->entry_widget);
4613 /* Finds the last child widget that happens to be of type GtkEntry */
4615 find_entry (GtkWidget *w, gpointer user_data)
4617 GtkWidget **entry = user_data;
4618 if ( GTK_IS_ENTRY (w))
4626 gtk_sheet_get_entry (GtkSheet *sheet)
4628 GtkWidget *w = sheet->entry_widget;
4630 g_return_val_if_fail (sheet != NULL, NULL);
4631 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
4632 g_return_val_if_fail (sheet->entry_widget != NULL, NULL);
4634 while (! GTK_IS_ENTRY (w))
4636 GtkWidget *entry = NULL;
4638 if (GTK_IS_CONTAINER (w))
4640 gtk_container_forall (GTK_CONTAINER (w), find_entry, &entry);
4649 return GTK_ENTRY (w);
4654 draw_button (GtkSheet *sheet, GdkWindow *window,
4655 GtkSheetButton *button, gboolean is_sensitive,
4656 GdkRectangle allocation)
4658 GtkShadowType shadow_type;
4659 gint text_width = 0, text_height = 0;
4660 PangoAlignment align = PANGO_ALIGN_LEFT;
4666 g_return_if_fail (sheet != NULL);
4667 g_return_if_fail (button != NULL);
4670 rtl = gtk_widget_get_direction (GTK_WIDGET (sheet)) == GTK_TEXT_DIR_RTL;
4672 gdk_window_clear_area (window,
4673 allocation.x, allocation.y,
4674 allocation.width, allocation.height);
4676 gtk_paint_box (sheet->button->style, window,
4677 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
4678 &allocation, GTK_WIDGET (sheet->button),
4680 allocation.x, allocation.y,
4681 allocation.width, allocation.height);
4683 state = button->state;
4684 if (!is_sensitive) state = GTK_STATE_INSENSITIVE;
4686 if (state == GTK_STATE_ACTIVE)
4687 shadow_type = GTK_SHADOW_IN;
4689 shadow_type = GTK_SHADOW_OUT;
4691 if (state != GTK_STATE_NORMAL && state != GTK_STATE_INSENSITIVE)
4692 gtk_paint_box (sheet->button->style, window,
4693 button->state, shadow_type,
4694 &allocation, GTK_WIDGET (sheet->button),
4696 allocation.x, allocation.y,
4697 allocation.width, allocation.height);
4699 if (button->label_visible)
4701 text_height = DEFAULT_ROW_HEIGHT -
4702 2 * COLUMN_TITLES_HEIGHT;
4704 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->fg_gc[button->state],
4706 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->white_gc,
4709 allocation.y += 2 * sheet->button->style->ythickness;
4711 if (button->label && strlen (button->label)>0)
4713 PangoRectangle rect;
4714 gchar *line = button->label;
4716 PangoLayout *layout = NULL;
4717 gint real_x = allocation.x;
4718 gint real_y = allocation.y;
4720 layout = gtk_widget_create_pango_layout (GTK_WIDGET (sheet), line);
4721 pango_layout_get_extents (layout, NULL, &rect);
4723 text_width = PANGO_PIXELS (rect.width);
4724 switch (button->justification)
4726 case GTK_JUSTIFY_LEFT:
4727 real_x = allocation.x + COLUMN_TITLES_HEIGHT;
4728 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
4730 case GTK_JUSTIFY_RIGHT:
4731 real_x = allocation.x + allocation.width - text_width - COLUMN_TITLES_HEIGHT;
4732 align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
4734 case GTK_JUSTIFY_CENTER:
4736 real_x = allocation.x + (allocation.width - text_width)/2;
4737 align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
4738 pango_layout_set_justify (layout, TRUE);
4740 pango_layout_set_alignment (layout, align);
4741 gtk_paint_layout (GTK_WIDGET (sheet)->style,
4750 g_object_unref (layout);
4753 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->fg_gc[button->state],
4755 gdk_gc_set_clip_rectangle (GTK_WIDGET (sheet)->style->white_gc, NULL);
4759 gtk_sheet_button_free (button);
4763 /* Draw the column title buttons FIRST through to LAST */
4765 draw_column_title_buttons_range (GtkSheet *sheet, gint first, gint last)
4769 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
4771 if (!sheet->column_titles_visible) return;
4773 g_return_if_fail (first >= min_visible_column (sheet));
4774 g_return_if_fail (last <= max_visible_column (sheet));
4777 rect.height = sheet->column_title_area.height;
4778 rect.x = psppire_axis_pixel_start (sheet->haxis, first) + CELL_SPACING;
4779 rect.width = psppire_axis_pixel_start (sheet->haxis, last) + CELL_SPACING
4780 + psppire_axis_unit_size (sheet->haxis, last);
4782 rect.x -= sheet->hadjustment->value;
4784 minimize_int (&rect.width, sheet->column_title_area.width);
4785 maximize_int (&rect.x, 0);
4787 gdk_window_begin_paint_rect (sheet->column_title_window, &rect);
4789 for (col = first ; col <= last ; ++col)
4791 GdkRectangle allocation;
4792 gboolean is_sensitive = FALSE;
4795 button = g_sheet_model_get_column_button (sheet->model, col);
4797 allocation.x = psppire_axis_pixel_start (sheet->haxis, col)
4799 allocation.x -= sheet->hadjustment->value;
4801 allocation.height = sheet->column_title_area.height;
4802 allocation.width = psppire_axis_unit_size (sheet->haxis, col);
4803 is_sensitive = g_sheet_model_get_column_sensitivity (sheet->model, col);
4805 draw_button (sheet, sheet->column_title_window,
4806 button, is_sensitive, allocation);
4809 gdk_window_end_paint (sheet->column_title_window);
4814 draw_row_title_buttons_range (GtkSheet *sheet, gint first, gint last)
4818 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet))) return;
4820 if (!sheet->row_titles_visible) return;
4822 g_return_if_fail (first >= min_visible_row (sheet));
4823 g_return_if_fail (last <= max_visible_row (sheet));
4826 rect.width = sheet->row_title_area.width;
4827 rect.y = psppire_axis_pixel_start (sheet->vaxis, first) + CELL_SPACING;
4828 rect.height = psppire_axis_pixel_start (sheet->vaxis, last) + CELL_SPACING
4829 + psppire_axis_unit_size (sheet->vaxis, last);
4831 rect.y -= sheet->vadjustment->value;
4833 minimize_int (&rect.height, sheet->row_title_area.height);
4834 maximize_int (&rect.y, 0);
4836 gdk_window_begin_paint_rect (sheet->row_title_window, &rect);
4837 for (row = first; row <= last; ++row)
4839 GdkRectangle allocation;
4841 gboolean is_sensitive = FALSE;
4843 GtkSheetButton *button =
4844 g_sheet_model_get_row_button (sheet->model, row);
4846 allocation.y = psppire_axis_pixel_start (sheet->vaxis, row)
4848 allocation.y -= sheet->vadjustment->value;
4850 allocation.width = sheet->row_title_area.width;
4851 allocation.height = psppire_axis_unit_size (sheet->vaxis, row);
4852 is_sensitive = g_sheet_model_get_row_sensitivity (sheet->model, row);
4854 draw_button (sheet, sheet->row_title_window,
4855 button, is_sensitive, allocation);
4858 gdk_window_end_paint (sheet->row_title_window);
4865 * vadjustment_value_changed
4866 * hadjustment_value_changed */
4869 adjust_scrollbars (GtkSheet *sheet)
4873 if (!GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
4876 gdk_drawable_get_size (sheet->sheet_window, &width, &height);
4878 if ( sheet->row_titles_visible)
4879 width -= sheet->row_title_area.width;
4881 if (sheet->column_titles_visible)
4882 height -= sheet->column_title_area.height;
4884 if (sheet->vadjustment)
4886 glong last_row = psppire_axis_unit_count (sheet->vaxis) - 1;
4888 sheet->vadjustment->step_increment =
4890 psppire_axis_unit_size (sheet->vaxis, last_row);
4892 sheet->vadjustment->page_increment =
4894 sheet->column_title_area.height -
4895 psppire_axis_unit_size (sheet->vaxis, last_row);
4899 sheet->vadjustment->upper =
4900 psppire_axis_pixel_start (sheet->vaxis, last_row)
4902 psppire_axis_unit_size (sheet->vaxis, last_row)
4905 sheet->vadjustment->lower = 0;
4906 sheet->vadjustment->page_size = height;
4908 g_signal_emit_by_name (sheet->vadjustment, "changed");
4911 if (sheet->hadjustment)
4914 sheet->hadjustment->step_increment = 1;
4916 sheet->hadjustment->page_increment = width;
4918 last_col = psppire_axis_unit_count (sheet->haxis) - 1;
4920 sheet->hadjustment->upper =
4921 psppire_axis_pixel_start (sheet->haxis, last_col)
4923 psppire_axis_unit_size (sheet->haxis, last_col)
4926 sheet->hadjustment->lower = 0;
4927 sheet->hadjustment->page_size = width;
4929 g_signal_emit_by_name (sheet->hadjustment, "changed");
4934 vadjustment_value_changed (GtkAdjustment *adjustment,
4938 GtkSheet *sheet = GTK_SHEET (data);
4940 g_return_if_fail (adjustment != NULL);
4942 if ( ! GTK_WIDGET_REALIZED (sheet)) return;
4945 gtk_widget_hide (sheet->entry_widget);
4948 gdk_drawable_get_visible_region (GDK_DRAWABLE (sheet->sheet_window));
4950 gdk_window_begin_paint_region (sheet->sheet_window, region);
4953 gtk_sheet_range_draw (sheet, NULL);
4954 draw_row_title_buttons (sheet);
4955 // size_allocate_global_button (sheet);
4956 gtk_sheet_draw_active_cell (sheet);
4958 gdk_window_end_paint (sheet->sheet_window);
4963 hadjustment_value_changed (GtkAdjustment *adjustment,
4967 GtkSheet *sheet = GTK_SHEET (data);
4969 g_return_if_fail (adjustment != NULL);
4971 if ( ! GTK_WIDGET_REALIZED (sheet)) return;
4973 gtk_widget_hide (sheet->entry_widget);
4977 gdk_drawable_get_visible_region (GDK_DRAWABLE (sheet->sheet_window));
4979 gdk_window_begin_paint_region (sheet->sheet_window, region);
4981 gtk_sheet_range_draw (sheet, NULL);
4982 draw_column_title_buttons (sheet);
4983 // size_allocate_global_button (sheet);
4985 gtk_sheet_draw_active_cell (sheet);
4987 gdk_window_end_paint (sheet->sheet_window);
4991 /* COLUMN RESIZING */
4993 draw_xor_vline (GtkSheet *sheet)
4996 gint xpos = sheet->x_drag;
4997 gdk_drawable_get_size (sheet->sheet_window,
5001 if (sheet->row_titles_visible)
5002 xpos += sheet->row_title_area.width;
5004 gdk_draw_line (GTK_WIDGET (sheet)->window, sheet->xor_gc,
5006 sheet->column_title_area.height,
5008 height + CELL_SPACING);
5013 draw_xor_hline (GtkSheet *sheet)
5017 gint ypos = sheet->y_drag;
5019 gdk_drawable_get_size (sheet->sheet_window,
5023 if (sheet->column_titles_visible)
5024 ypos += sheet->column_title_area.height;
5026 gdk_draw_line (GTK_WIDGET (sheet)->window, sheet->xor_gc,
5027 sheet->row_title_area.width,
5029 width + CELL_SPACING,
5033 /* SELECTED RANGE */
5035 draw_xor_rectangle (GtkSheet *sheet, GtkSheetRange range)
5038 GdkRectangle clip_area, area;
5041 area.x = psppire_axis_pixel_start (sheet->haxis, range.col0);
5042 area.y = psppire_axis_pixel_start (sheet->vaxis, range.row0);
5043 area.width = psppire_axis_pixel_start (sheet->haxis, range.coli)- area.x+
5044 psppire_axis_unit_size (sheet->haxis, range.coli);
5045 area.height = psppire_axis_pixel_start (sheet->vaxis, range.rowi)- area.y +
5046 psppire_axis_unit_size (sheet->vaxis, range.rowi);
5048 clip_area.x = sheet->row_title_area.width;
5049 clip_area.y = sheet->column_title_area.height;
5051 gdk_drawable_get_size (sheet->sheet_window,
5052 &clip_area.width, &clip_area.height);
5054 if (!sheet->row_titles_visible) clip_area.x = 0;
5055 if (!sheet->column_titles_visible) clip_area.y = 0;
5059 area.width = area.width + area.x;
5062 if (area.width > clip_area.width) area.width = clip_area.width + 10;
5065 area.height = area.height + area.y;
5068 if (area.height > clip_area.height) area.height = clip_area.height + 10;
5072 clip_area.width += 3;
5073 clip_area.height += 3;
5075 gdk_gc_get_values (sheet->xor_gc, &values);
5077 gdk_gc_set_clip_rectangle (sheet->xor_gc, &clip_area);
5079 gdk_draw_rectangle (sheet->sheet_window,
5082 area.x + i, area.y + i,
5083 area.width - 2 * i, area.height - 2 * i);
5086 gdk_gc_set_clip_rectangle (sheet->xor_gc, NULL);
5088 gdk_gc_set_foreground (sheet->xor_gc, &values.foreground);
5092 /* this function returns the new width of the column being resized given
5093 * the COLUMN and X position of the cursor; the x cursor position is passed
5094 * in as a pointer and automaticaly corrected if it's outside the acceptable
5097 new_column_width (GtkSheet *sheet, gint column, gint *x)
5099 gint left_pos = psppire_axis_pixel_start (sheet->haxis, column)
5100 - sheet->hadjustment->value;
5102 gint width = *x - left_pos;
5104 if ( width < sheet->column_requisition)
5106 width = sheet->column_requisition;
5107 *x = left_pos + width;
5111 g_sheet_column_set_width (sheet->column_geometry, column, width);
5114 draw_column_title_buttons (sheet);
5119 /* this function returns the new height of the row being resized given
5120 * the row and y position of the cursor; the y cursor position is passed
5121 * in as a pointer and automaticaly corrected if it's beyond min / max limits */
5123 new_row_height (GtkSheet *sheet, gint row, gint *y)
5129 min_height = sheet->row_requisition;
5131 /* you can't shrink a row to less than its minimum height */
5132 if (cy < psppire_axis_pixel_start (sheet->vaxis, row) + min_height)
5135 *y = cy = psppire_axis_pixel_start (sheet->vaxis, row) + min_height;
5138 /* calculate new row height making sure it doesn't end up
5139 * less than the minimum height */
5140 height = (cy - psppire_axis_pixel_start (sheet->vaxis, row));
5141 if (height < min_height)
5142 height = min_height;
5145 g_sheet_row_set_height (sheet->row_geometry, row, height);
5147 draw_row_title_buttons (sheet);
5153 gtk_sheet_set_column_width (GtkSheet *sheet,
5159 g_return_if_fail (sheet != NULL);
5160 g_return_if_fail (GTK_IS_SHEET (sheet));
5162 if (column < 0 || column >= psppire_axis_unit_count (sheet->haxis))
5165 gtk_sheet_column_size_request (sheet, column, &min_width);
5166 if (width < min_width) return;
5169 g_sheet_column_set_width (sheet->column_geometry, column, width);
5172 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
5174 draw_column_title_buttons (sheet);
5175 adjust_scrollbars (sheet);
5176 gtk_sheet_size_allocate_entry (sheet);
5177 gtk_sheet_range_draw (sheet, NULL);
5184 gtk_sheet_set_row_height (GtkSheet *sheet,
5190 g_return_if_fail (sheet != NULL);
5191 g_return_if_fail (GTK_IS_SHEET (sheet));
5193 if (row < 0 || row >= psppire_axis_unit_count (sheet->vaxis))
5196 gtk_sheet_row_size_request (sheet, row, &min_height);
5197 if (height < min_height) return;
5200 g_sheet_row_set_height (sheet->row_geometry, row, height);
5203 if (GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)) )
5205 draw_row_title_buttons (sheet);
5206 adjust_scrollbars (sheet);
5207 gtk_sheet_size_allocate_entry (sheet);
5208 gtk_sheet_range_draw (sheet, NULL);
5213 gtk_sheet_get_attributes (const GtkSheet *sheet, gint row, gint col,
5214 GtkSheetCellAttr *attr)
5217 const GtkJustification *j ;
5218 const GtkSheetCellBorder *border ;
5219 GdkColormap *colormap;
5221 g_return_val_if_fail (sheet != NULL, FALSE);
5222 g_return_val_if_fail (GTK_IS_SHEET (sheet), FALSE);
5224 if (row < 0 || col < 0) return FALSE;
5226 attr->foreground = GTK_WIDGET (sheet)->style->black;
5227 attr->background = sheet->color[BG_COLOR];
5229 attr->border.width = 0;
5230 attr->border.line_style = GDK_LINE_SOLID;
5231 attr->border.cap_style = GDK_CAP_NOT_LAST;
5232 attr->border.join_style = GDK_JOIN_MITER;
5233 attr->border.mask = 0;
5234 attr->border.color = GTK_WIDGET (sheet)->style->black;
5236 attr->is_editable = g_sheet_model_is_editable (sheet->model, row, col);
5238 colormap = gtk_widget_get_colormap (GTK_WIDGET (sheet));
5239 fg = g_sheet_model_get_foreground (sheet->model, row, col);
5242 gdk_colormap_alloc_color (colormap, fg, TRUE, TRUE);
5243 attr->foreground = *fg;
5246 bg = g_sheet_model_get_background (sheet->model, row, col);
5249 gdk_colormap_alloc_color (colormap, bg, TRUE, TRUE);
5250 attr->background = *bg;
5253 attr->justification =
5254 g_sheet_model_get_column_justification (sheet->model, col);
5256 j = g_sheet_model_get_justification (sheet->model, row, col);
5258 attr->justification = *j;
5260 border = g_sheet_model_get_cell_border (sheet->model, row, col);
5262 if ( border ) attr->border = *border;
5268 gtk_sheet_button_size_request (GtkSheet *sheet,
5269 const GtkSheetButton *button,
5270 GtkRequisition *button_requisition)
5272 GtkRequisition requisition;
5273 GtkRequisition label_requisition;
5275 label_requisition.height = DEFAULT_ROW_HEIGHT;
5276 label_requisition.width = COLUMN_MIN_WIDTH;
5278 requisition.height = DEFAULT_ROW_HEIGHT;
5279 requisition.width = COLUMN_MIN_WIDTH;
5282 *button_requisition = requisition;
5283 button_requisition->width = MAX (requisition.width, label_requisition.width);
5284 button_requisition->height = MAX (requisition.height, label_requisition.height);
5289 gtk_sheet_row_size_request (GtkSheet *sheet,
5293 GtkRequisition button_requisition;
5295 gtk_sheet_button_size_request (sheet,
5296 g_sheet_model_get_row_button (sheet->model, row),
5297 &button_requisition);
5299 *requisition = button_requisition.height;
5301 sheet->row_requisition = *requisition;
5305 gtk_sheet_column_size_request (GtkSheet *sheet,
5309 GtkRequisition button_requisition;
5311 GtkSheetButton *button = g_sheet_model_get_column_button (sheet->model, col);
5313 gtk_sheet_button_size_request (sheet,
5315 &button_requisition);
5317 gtk_sheet_button_free (button);
5319 *requisition = button_requisition.width;
5321 sheet->column_requisition = *requisition;
5326 gtk_sheet_forall (GtkContainer *container,
5327 gboolean include_internals,
5328 GtkCallback callback,
5329 gpointer callback_data)
5331 GtkSheet *sheet = GTK_SHEET (container);
5333 g_return_if_fail (callback != NULL);
5335 if (sheet->button && sheet->button->parent)
5336 (* callback) (sheet->button, callback_data);
5338 if (sheet->entry_widget && GTK_IS_CONTAINER (sheet->entry_widget))
5339 (* callback) (sheet->entry_widget, callback_data);
5344 gtk_sheet_get_model (const GtkSheet *sheet)
5346 g_return_val_if_fail (GTK_IS_SHEET (sheet), NULL);
5348 return sheet->model;
5353 gtk_sheet_button_new (void)
5355 GtkSheetButton *button = g_malloc (sizeof (GtkSheetButton));
5357 button->state = GTK_STATE_NORMAL;
5358 button->label = NULL;
5359 button->label_visible = TRUE;
5360 button->justification = GTK_JUSTIFY_FILL;
5367 gtk_sheet_button_free (GtkSheetButton *button)
5369 if (!button) return ;
5371 g_free (button->label);
5376 append_cell_text (GString *string, const GtkSheet *sheet, gint r, gint c)
5378 gchar *celltext = gtk_sheet_cell_get_text (sheet, r, c);
5380 if ( NULL == celltext)
5383 g_string_append (string, celltext);
5389 range_to_text (const GtkSheet *sheet)
5394 if ( !gtk_sheet_range_isvisible (sheet, &sheet->range))
5397 string = g_string_sized_new (80);
5399 for (r = sheet->range.row0; r <= sheet->range.rowi; ++r)
5401 for (c = sheet->range.col0; c < sheet->range.coli; ++c)
5403 append_cell_text (string, sheet, r, c);
5404 g_string_append (string, "\t");
5406 append_cell_text (string, sheet, r, c);
5407 if ( r < sheet->range.rowi)
5408 g_string_append (string, "\n");
5415 range_to_html (const GtkSheet *sheet)
5420 if ( !gtk_sheet_range_isvisible (sheet, &sheet->range))
5423 string = g_string_sized_new (480);
5425 g_string_append (string, "<html>\n");
5426 g_string_append (string, "<body>\n");
5427 g_string_append (string, "<table>\n");
5428 for (r = sheet->range.row0; r <= sheet->range.rowi; ++r)
5430 g_string_append (string, "<tr>\n");
5431 for (c = sheet->range.col0; c <= sheet->range.coli; ++c)
5433 g_string_append (string, "<td>");
5434 append_cell_text (string, sheet, r, c);
5435 g_string_append (string, "</td>\n");
5437 g_string_append (string, "</tr>\n");
5439 g_string_append (string, "</table>\n");
5440 g_string_append (string, "</body>\n");
5441 g_string_append (string, "</html>\n");
5453 primary_get_cb (GtkClipboard *clipboard,
5454 GtkSelectionData *selection_data,
5458 GtkSheet *sheet = GTK_SHEET (data);
5459 GString *string = NULL;
5463 case SELECT_FMT_TEXT:
5464 string = range_to_text (sheet);
5466 case SELECT_FMT_HTML:
5467 string = range_to_html (sheet);
5470 g_assert_not_reached ();
5473 gtk_selection_data_set (selection_data, selection_data->target,
5475 (const guchar *) string->str, string->len);
5476 g_string_free (string, TRUE);
5480 primary_clear_cb (GtkClipboard *clipboard,
5483 GtkSheet *sheet = GTK_SHEET (data);
5484 if ( ! GTK_WIDGET_REALIZED (GTK_WIDGET (sheet)))
5487 gtk_sheet_real_unselect_range (sheet, NULL);
5491 gtk_sheet_update_primary_selection (GtkSheet *sheet)
5493 static const GtkTargetEntry targets[] = {
5494 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
5495 { "STRING", 0, SELECT_FMT_TEXT },
5496 { "TEXT", 0, SELECT_FMT_TEXT },
5497 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
5498 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
5499 { "text/plain", 0, SELECT_FMT_TEXT },
5500 { "text/html", 0, SELECT_FMT_HTML }
5503 GtkClipboard *clipboard;
5505 if (!GTK_WIDGET_REALIZED (sheet))
5508 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (sheet),
5509 GDK_SELECTION_PRIMARY);
5511 if (gtk_sheet_range_isvisible (sheet, &sheet->range))
5513 if (!gtk_clipboard_set_with_owner (clipboard, targets,
5514 G_N_ELEMENTS (targets),
5515 primary_get_cb, primary_clear_cb,
5517 primary_clear_cb (clipboard, sheet);
5521 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (sheet))
5522 gtk_clipboard_clear (clipboard);